Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 18 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 19 | #include "programmer.h" |
Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 20 | #include "hwaccess_x86_io.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 21 | #include "platform/pci.h" |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 22 | |
| 23 | #define PCI_VENDOR_ID_REALTEK 0x10ec |
| 24 | #define PCI_VENDOR_ID_SMC1211 0x1113 |
| 25 | |
Stefan Tauner | 0ccec8f | 2014-06-01 23:49:03 +0000 | [diff] [blame] | 26 | static uint32_t io_base_addr = 0; |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 27 | static int bios_rom_addr, bios_rom_data; |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 28 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 29 | static const struct dev_entry nics_realtek[] = { |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 30 | {0x10ec, 0x8139, OK, "Realtek", "RTL8139/8139C/8139C+"}, |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 31 | {0x10ec, 0x8169, NT, "Realtek", "RTL8169"}, |
| 32 | {0x1113, 0x1211, OK, "SMC", "1211TX"}, /* RTL8139 clone */ |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 33 | |
| 34 | {0}, |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 37 | static void nicrealtek_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 38 | static uint8_t nicrealtek_chip_readb(const struct flashctx *flash, const chipaddr addr); |
Anastasia Klimchuk | d66dded | 2021-08-27 15:42:46 +1000 | [diff] [blame] | 39 | static int nicrealtek_shutdown(void *data); |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 40 | static const struct par_master par_master_nicrealtek = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 41 | .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, |
Anastasia Klimchuk | d66dded | 2021-08-27 15:42:46 +1000 | [diff] [blame] | 49 | .shutdown = nicrealtek_shutdown, |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 52 | static int nicrealtek_shutdown(void *data) |
| 53 | { |
| 54 | /* FIXME: We forgot to disable software access again. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 58 | static int nicrealtek_init(struct flashprog_programmer *const prog) |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 59 | { |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 60 | struct pci_dev *dev = NULL; |
| 61 | |
Stefan Tauner | d7d423b | 2012-10-20 09:13:16 +0000 | [diff] [blame] | 62 | if (rget_io_perms()) |
| 63 | return 1; |
| 64 | |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 65 | dev = pcidev_init(nics_realtek, PCI_BASE_ADDRESS_0); |
| 66 | if (!dev) |
Stefan Tauner | d7d423b | 2012-10-20 09:13:16 +0000 | [diff] [blame] | 67 | return 1; |
| 68 | |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 69 | io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0); |
Niklas Söderlund | 89edf36 | 2013-08-23 23:29:23 +0000 | [diff] [blame] | 70 | if (!io_base_addr) |
| 71 | return 1; |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 72 | |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 73 | /* Beware, this ignores the vendor ID! */ |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 74 | switch (dev->device_id) { |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 75 | case 0x8139: /* RTL8139 */ |
| 76 | case 0x1211: /* SMC 1211TX */ |
| 77 | default: |
| 78 | bios_rom_addr = 0xD4; |
| 79 | bios_rom_data = 0xD7; |
| 80 | break; |
| 81 | case 0x8169: /* RTL8169 */ |
| 82 | bios_rom_addr = 0x30; |
| 83 | bios_rom_data = 0x33; |
| 84 | break; |
| 85 | } |
| 86 | |
Anastasia Klimchuk | d66dded | 2021-08-27 15:42:46 +1000 | [diff] [blame] | 87 | return register_par_master(&par_master_nicrealtek, BUS_PARALLEL, NULL); |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 90 | static void nicrealtek_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 91 | { |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 92 | /* Output addr and data, set WE to 0, set OE to 1, set CS to 0, |
| 93 | * enable software access. |
| 94 | */ |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 95 | OUTL(((uint32_t)addr & 0x01FFFF) | 0x0A0000 | (val << 24), |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 96 | io_base_addr + bios_rom_addr); |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 97 | /* Output addr and data, set WE to 1, set OE to 1, set CS to 1, |
| 98 | * enable software access. |
| 99 | */ |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 100 | OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24), |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 101 | io_base_addr + bios_rom_addr); |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 104 | static uint8_t nicrealtek_chip_readb(const struct flashctx *flash, const chipaddr addr) |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 105 | { |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 106 | uint8_t val; |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 107 | |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 108 | /* FIXME: Can we skip reading the old data and simply use 0? */ |
| 109 | /* Read old data. */ |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 110 | val = INB(io_base_addr + bios_rom_data); |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 111 | /* Output new addr and old data, set WE to 1, set OE to 0, set CS to 0, |
| 112 | * enable software access. |
| 113 | */ |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 114 | OUTL(((uint32_t)addr & 0x01FFFF) | 0x060000 | (val << 24), |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 115 | io_base_addr + bios_rom_addr); |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 116 | |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 117 | /* Read new data. */ |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 118 | val = INB(io_base_addr + bios_rom_data); |
Carl-Daniel Hailfinger | 2eda391 | 2010-06-14 14:18:37 +0000 | [diff] [blame] | 119 | /* Output addr and new data, set WE to 1, set OE to 1, set CS to 1, |
| 120 | * enable software access. |
| 121 | */ |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 122 | OUTL(((uint32_t)addr & 0x01FFFF) | 0x1E0000 | (val << 24), |
Sergey Lichack | 98f4710 | 2012-08-27 01:24:15 +0000 | [diff] [blame] | 123 | io_base_addr + bios_rom_addr); |
Uwe Hermann | 829ed84 | 2010-05-24 17:39:14 +0000 | [diff] [blame] | 124 | |
| 125 | return val; |
Joerg Fischer | 52a1549 | 2010-05-21 22:28:19 +0000 | [diff] [blame] | 126 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 127 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 128 | const struct programmer_entry programmer_nicrealtek = { |
| 129 | .name = "nicrealtek", |
| 130 | .type = PCI, |
| 131 | .devs.dev = nics_realtek, |
| 132 | .init = nicrealtek_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 133 | }; |