blob: 5454b63af6708e311d7a666eaaf671a026879e43 [file] [log] [blame]
hailfinger8a0f84b2010-05-21 22:28:19 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Joerg Fischer <turboj@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
hailfinger8a0f84b2010-05-21 22:28:19 +000015 */
16
hailfinger324a9cc2010-05-26 01:45:41 +000017#if defined(__i386__) || defined(__x86_64__)
18
hailfinger8a0f84b2010-05-21 22:28:19 +000019#include <stdlib.h>
hailfinger8a0f84b2010-05-21 22:28:19 +000020#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000021#include "programmer.h"
Patrick Georgi13860de2017-04-11 20:33:00 +020022#include "hwaccess.h"
hailfinger8a0f84b2010-05-21 22:28:19 +000023
24#define PCI_VENDOR_ID_REALTEK 0x10ec
25#define PCI_VENDOR_ID_SMC1211 0x1113
26
Patrick Georgi7c30fa92017-03-28 22:47:12 +020027static uint32_t io_base_addr = 0;
Patrick Georgi13860de2017-04-11 20:33:00 +020028static int bios_rom_addr, bios_rom_data;
Patrick Georgi7c30fa92017-03-28 22:47:12 +020029
Patrick Georgi8ae16572017-03-09 15:59:25 +010030const struct dev_entry nics_realtek[] = {
uweae6a69a2010-05-24 17:39:14 +000031 {0x10ec, 0x8139, OK, "Realtek", "RTL8139/8139C/8139C+"},
Patrick Georgi13860de2017-04-11 20:33:00 +020032 {0x10ec, 0x8169, NT, "Realtek", "RTL8169"},
33 {0x1113, 0x1211, OK, "SMC", "1211TX"}, /* RTL8139 clone */
34
Patrick Georgi8ddfee92017-03-20 14:54:28 +010035 {0},
hailfinger8a0f84b2010-05-21 22:28:19 +000036};
37
Patrick Georgid4caa6b2017-03-28 21:22:55 +020038static void nicrealtek_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
39static uint8_t nicrealtek_chip_readb(const struct flashctx *flash, const chipaddr addr);
Patrick Georgi0a9533a2017-02-03 19:28:38 +010040static const struct par_master par_master_nicrealtek = {
hailfinger76bb7e92011-11-09 23:40:00 +000041 .chip_readb = nicrealtek_chip_readb,
42 .chip_readw = fallback_chip_readw,
43 .chip_readl = fallback_chip_readl,
44 .chip_readn = fallback_chip_readn,
45 .chip_writeb = nicrealtek_chip_writeb,
46 .chip_writew = fallback_chip_writew,
47 .chip_writel = fallback_chip_writel,
48 .chip_writen = fallback_chip_writen,
49};
50
David Hendricks93784b42016-08-09 17:00:38 -070051static int nicrealtek_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +000052{
53 /* FIXME: We forgot to disable software access again. */
dhendrix0ffc2eb2011-06-14 01:35:36 +000054 return 0;
55}
56
David Hendricksac1d25c2016-08-09 17:00:58 -070057int nicrealtek_init(void)
hailfinger8a0f84b2010-05-21 22:28:19 +000058{
Patrick Georgi7c30fa92017-03-28 22:47:12 +020059 struct pci_dev *dev = NULL;
60
Patrick Georgi2a2d67f2017-03-09 10:15:39 +010061 if (rget_io_perms())
62 return 1;
uweae6a69a2010-05-24 17:39:14 +000063
Patrick Georgi7c30fa92017-03-28 22:47:12 +020064 dev = pcidev_init(nics_realtek, PCI_BASE_ADDRESS_0);
65 if (!dev)
66 return 1;
67
68 io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
69 if (!io_base_addr)
70 return 1;
uweae6a69a2010-05-24 17:39:14 +000071
Patrick Georgi13860de2017-04-11 20:33:00 +020072 /* Beware, this ignores the vendor ID! */
73 switch (dev->device_id) {
74 case 0x8139: /* RTL8139 */
75 case 0x1211: /* SMC 1211TX */
76 default:
77 bios_rom_addr = 0xD4;
78 bios_rom_data = 0xD7;
79 break;
80 case 0x8169: /* RTL8169 */
81 bios_rom_addr = 0x30;
82 bios_rom_data = 0x33;
83 break;
84 }
85
dhendrix0ffc2eb2011-06-14 01:35:36 +000086 if (register_shutdown(nicrealtek_shutdown, NULL))
87 return 1;
hailfinger76bb7e92011-11-09 23:40:00 +000088
Patrick Georgi0a9533a2017-02-03 19:28:38 +010089 register_par_master(&par_master_nicrealtek, BUS_PARALLEL);
hailfinger76bb7e92011-11-09 23:40:00 +000090
hailfinger8a0f84b2010-05-21 22:28:19 +000091 return 0;
92}
93
Patrick Georgid4caa6b2017-03-28 21:22:55 +020094static void nicrealtek_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfinger8a0f84b2010-05-21 22:28:19 +000095{
hailfinger6ee8beb2010-06-14 14:18:37 +000096 /* Output addr and data, set WE to 0, set OE to 1, set CS to 0,
97 * enable software access.
98 */
uweae6a69a2010-05-24 17:39:14 +000099 OUTL(((uint32_t)addr & 0x01FFFF) | 0x0A0000 | (val << 24),
Patrick Georgi13860de2017-04-11 20:33:00 +0200100 io_base_addr + bios_rom_addr);
hailfinger6ee8beb2010-06-14 14:18:37 +0000101 /* Output addr and data, set WE to 1, set OE to 1, set CS to 1,
102 * enable software access.
103 */
uweae6a69a2010-05-24 17:39:14 +0000104 OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24),
Patrick Georgi13860de2017-04-11 20:33:00 +0200105 io_base_addr + bios_rom_addr);
hailfinger8a0f84b2010-05-21 22:28:19 +0000106}
107
Patrick Georgid4caa6b2017-03-28 21:22:55 +0200108static uint8_t nicrealtek_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfinger8a0f84b2010-05-21 22:28:19 +0000109{
uweae6a69a2010-05-24 17:39:14 +0000110 uint8_t val;
hailfinger8a0f84b2010-05-21 22:28:19 +0000111
hailfinger6ee8beb2010-06-14 14:18:37 +0000112 /* FIXME: Can we skip reading the old data and simply use 0? */
113 /* Read old data. */
Patrick Georgi13860de2017-04-11 20:33:00 +0200114 val = INB(io_base_addr + bios_rom_data);
hailfinger6ee8beb2010-06-14 14:18:37 +0000115 /* Output new addr and old data, set WE to 1, set OE to 0, set CS to 0,
116 * enable software access.
117 */
uweae6a69a2010-05-24 17:39:14 +0000118 OUTL(((uint32_t)addr & 0x01FFFF) | 0x060000 | (val << 24),
Patrick Georgi13860de2017-04-11 20:33:00 +0200119 io_base_addr + bios_rom_addr);
uweae6a69a2010-05-24 17:39:14 +0000120
hailfinger6ee8beb2010-06-14 14:18:37 +0000121 /* Read new data. */
Patrick Georgi13860de2017-04-11 20:33:00 +0200122 val = INB(io_base_addr + bios_rom_data);
hailfinger6ee8beb2010-06-14 14:18:37 +0000123 /* Output addr and new data, set WE to 1, set OE to 1, set CS to 1,
124 * enable software access.
125 */
uweae6a69a2010-05-24 17:39:14 +0000126 OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24),
Patrick Georgi13860de2017-04-11 20:33:00 +0200127 io_base_addr + bios_rom_addr);
uweae6a69a2010-05-24 17:39:14 +0000128
129 return val;
hailfinger8a0f84b2010-05-21 22:28:19 +0000130}
hailfinger324a9cc2010-05-26 01:45:41 +0000131
132#else
133#error PCI port I/O access is not supported on this architecture yet.
134#endif