blob: 4f39bc303dac6c0110fac8486fd3f91f111c259c [file] [log] [blame]
Joerg Fischer52a15492010-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.
Joerg Fischer52a15492010-05-21 22:28:19 +000015 */
16
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000017#if defined(__i386__) || defined(__x86_64__)
18
Joerg Fischer52a15492010-05-21 22:28:19 +000019#include <stdlib.h>
Joerg Fischer52a15492010-05-21 22:28:19 +000020#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000021#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000022#include "hwaccess.h"
Joerg Fischer52a15492010-05-21 22:28:19 +000023
24#define PCI_VENDOR_ID_REALTEK 0x10ec
25#define PCI_VENDOR_ID_SMC1211 0x1113
26
Stefan Tauner0ccec8f2014-06-01 23:49:03 +000027static uint32_t io_base_addr = 0;
Sergey Lichack98f47102012-08-27 01:24:15 +000028static int bios_rom_addr, bios_rom_data;
Joerg Fischer52a15492010-05-21 22:28:19 +000029
Thomas Heijligencc853d82021-05-04 15:32:17 +020030static const struct dev_entry nics_realtek[] = {
Uwe Hermann829ed842010-05-24 17:39:14 +000031 {0x10ec, 0x8139, OK, "Realtek", "RTL8139/8139C/8139C+"},
Sergey Lichack98f47102012-08-27 01:24:15 +000032 {0x10ec, 0x8169, NT, "Realtek", "RTL8169"},
33 {0x1113, 0x1211, OK, "SMC", "1211TX"}, /* RTL8139 clone */
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000034
35 {0},
Joerg Fischer52a15492010-05-21 22:28:19 +000036};
37
Sergey Lichack98f47102012-08-27 01:24:15 +000038static 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);
Anastasia Klimchukd66dded2021-08-27 15:42:46 +100040static int nicrealtek_shutdown(void *data);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000041static const struct par_master par_master_nicrealtek = {
Thomas Heijligen43040f22022-06-23 14:38:35 +020042 .chip_readb = nicrealtek_chip_readb,
43 .chip_readw = fallback_chip_readw,
44 .chip_readl = fallback_chip_readl,
45 .chip_readn = fallback_chip_readn,
46 .chip_writeb = nicrealtek_chip_writeb,
47 .chip_writew = fallback_chip_writew,
48 .chip_writel = fallback_chip_writel,
49 .chip_writen = fallback_chip_writen,
Anastasia Klimchukd66dded2021-08-27 15:42:46 +100050 .shutdown = nicrealtek_shutdown,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000051};
52
David Hendricks8bb20212011-06-14 01:35:36 +000053static int nicrealtek_shutdown(void *data)
54{
55 /* FIXME: We forgot to disable software access again. */
David Hendricks8bb20212011-06-14 01:35:36 +000056 return 0;
57}
58
Thomas Heijligencc853d82021-05-04 15:32:17 +020059static int nicrealtek_init(void)
Joerg Fischer52a15492010-05-21 22:28:19 +000060{
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000061 struct pci_dev *dev = NULL;
62
Stefan Taunerd7d423b2012-10-20 09:13:16 +000063 if (rget_io_perms())
64 return 1;
65
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000066 dev = pcidev_init(nics_realtek, PCI_BASE_ADDRESS_0);
67 if (!dev)
Stefan Taunerd7d423b2012-10-20 09:13:16 +000068 return 1;
69
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000070 io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
Niklas Söderlund89edf362013-08-23 23:29:23 +000071 if (!io_base_addr)
72 return 1;
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000073
Sergey Lichack98f47102012-08-27 01:24:15 +000074 /* Beware, this ignores the vendor ID! */
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000075 switch (dev->device_id) {
Sergey Lichack98f47102012-08-27 01:24:15 +000076 case 0x8139: /* RTL8139 */
77 case 0x1211: /* SMC 1211TX */
78 default:
79 bios_rom_addr = 0xD4;
80 bios_rom_data = 0xD7;
81 break;
82 case 0x8169: /* RTL8169 */
83 bios_rom_addr = 0x30;
84 bios_rom_data = 0x33;
85 break;
86 }
87
Anastasia Klimchukd66dded2021-08-27 15:42:46 +100088 return register_par_master(&par_master_nicrealtek, BUS_PARALLEL, NULL);
Joerg Fischer52a15492010-05-21 22:28:19 +000089}
90
Sergey Lichack98f47102012-08-27 01:24:15 +000091static void nicrealtek_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Joerg Fischer52a15492010-05-21 22:28:19 +000092{
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +000093 /* Output addr and data, set WE to 0, set OE to 1, set CS to 0,
94 * enable software access.
95 */
Uwe Hermann829ed842010-05-24 17:39:14 +000096 OUTL(((uint32_t)addr & 0x01FFFF) | 0x0A0000 | (val << 24),
Sergey Lichack98f47102012-08-27 01:24:15 +000097 io_base_addr + bios_rom_addr);
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +000098 /* Output addr and data, set WE to 1, set OE to 1, set CS to 1,
99 * enable software access.
100 */
Uwe Hermann829ed842010-05-24 17:39:14 +0000101 OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24),
Sergey Lichack98f47102012-08-27 01:24:15 +0000102 io_base_addr + bios_rom_addr);
Joerg Fischer52a15492010-05-21 22:28:19 +0000103}
104
Sergey Lichack98f47102012-08-27 01:24:15 +0000105static uint8_t nicrealtek_chip_readb(const struct flashctx *flash, const chipaddr addr)
Joerg Fischer52a15492010-05-21 22:28:19 +0000106{
Uwe Hermann829ed842010-05-24 17:39:14 +0000107 uint8_t val;
Joerg Fischer52a15492010-05-21 22:28:19 +0000108
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +0000109 /* FIXME: Can we skip reading the old data and simply use 0? */
110 /* Read old data. */
Sergey Lichack98f47102012-08-27 01:24:15 +0000111 val = INB(io_base_addr + bios_rom_data);
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +0000112 /* Output new addr and old data, set WE to 1, set OE to 0, set CS to 0,
113 * enable software access.
114 */
Uwe Hermann829ed842010-05-24 17:39:14 +0000115 OUTL(((uint32_t)addr & 0x01FFFF) | 0x060000 | (val << 24),
Sergey Lichack98f47102012-08-27 01:24:15 +0000116 io_base_addr + bios_rom_addr);
Uwe Hermann829ed842010-05-24 17:39:14 +0000117
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +0000118 /* Read new data. */
Sergey Lichack98f47102012-08-27 01:24:15 +0000119 val = INB(io_base_addr + bios_rom_data);
Carl-Daniel Hailfinger2eda3912010-06-14 14:18:37 +0000120 /* Output addr and new data, set WE to 1, set OE to 1, set CS to 1,
121 * enable software access.
122 */
Uwe Hermann829ed842010-05-24 17:39:14 +0000123 OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24),
Sergey Lichack98f47102012-08-27 01:24:15 +0000124 io_base_addr + bios_rom_addr);
Uwe Hermann829ed842010-05-24 17:39:14 +0000125
126 return val;
Joerg Fischer52a15492010-05-21 22:28:19 +0000127}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000128
Thomas Heijligencc853d82021-05-04 15:32:17 +0200129const struct programmer_entry programmer_nicrealtek = {
130 .name = "nicrealtek",
131 .type = PCI,
132 .devs.dev = nics_realtek,
133 .init = nicrealtek_init,
134 .map_flash_region = fallback_map,
135 .unmap_flash_region = fallback_unmap,
136 .delay = internal_delay,
137};
138
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000139#else
140#error PCI port I/O access is not supported on this architecture yet.
141#endif