Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Joseph C. Lehner <joseph.c.lehner@gmail.com> |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #if defined(__i386__) || defined(__x86_64__) |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include "flash.h" |
| 26 | #include "programmer.h" |
| 27 | #include "hwaccess.h" |
| 28 | |
| 29 | #define MAX_ROM_DECODE (32 * 1024) |
| 30 | #define ADDR_MASK (MAX_ROM_DECODE - 1) |
| 31 | |
| 32 | /* |
| 33 | * In the absence of any public docs on the PDC2026x family, this programmer was created through a mix of |
| 34 | * reverse-engineering and trial and error. |
| 35 | * |
| 36 | * The only device tested is an Ultra100 controller, but the logic for programming the other 2026x controllers |
| 37 | * is the same, so it should, in theory, work for those as well. |
| 38 | * |
| 39 | * While the tested Ultra100 controller used a 128 kB MX29F001T chip, A16 and A15 showed continuity to ground, |
| 40 | * thus limiting the the programmer on this card to 32 kB. Without other controllers to test this programmer on, |
| 41 | * this is currently a hard limit. Note that ROM files for these controllers are 16 kB only. |
| 42 | * |
| 43 | * Since flashrom does not support accessing flash chips larger than the size limit of the programmer (the |
| 44 | * tested Ultra100 uses a 128 kB MX29F001T chip), the chip size is hackishly adjusted in atapromise_limit_chip. |
| 45 | */ |
| 46 | |
| 47 | static uint32_t io_base_addr = 0; |
| 48 | static uint32_t rom_base_addr = 0; |
| 49 | |
| 50 | static uint8_t *atapromise_bar = NULL; |
| 51 | static size_t rom_size = 0; |
| 52 | |
| 53 | const struct dev_entry ata_promise[] = { |
| 54 | {0x105a, 0x4d38, NT, "Promise", "PDC20262 (FastTrak66/Ultra66)"}, |
| 55 | {0x105a, 0x0d30, NT, "Promise", "PDC20265 (FastTrak100 Lite/Ultra100)"}, |
| 56 | {0x105a, 0x4d30, OK, "Promise", "PDC20267 (FastTrak100/Ultra100)"}, |
| 57 | {0}, |
| 58 | }; |
| 59 | |
| 60 | static void atapromise_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 61 | static uint8_t atapromise_chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 62 | |
| 63 | static const struct par_master par_master_atapromise = { |
| 64 | .chip_readb = atapromise_chip_readb, |
| 65 | .chip_readw = fallback_chip_readw, |
| 66 | .chip_readl = fallback_chip_readl, |
| 67 | .chip_readn = fallback_chip_readn, |
| 68 | .chip_writeb = atapromise_chip_writeb, |
| 69 | .chip_writew = fallback_chip_writew, |
| 70 | .chip_writel = fallback_chip_writel, |
| 71 | .chip_writen = fallback_chip_writen, |
| 72 | }; |
| 73 | |
| 74 | void *atapromise_map(const char *descr, uintptr_t phys_addr, size_t len) |
| 75 | { |
| 76 | /* In case fallback_map ever returns something other than NULL. */ |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | static void atapromise_limit_chip(struct flashchip *chip) |
| 81 | { |
| 82 | static uint32_t last_model_id = 0; |
| 83 | unsigned int i, size; |
| 84 | |
| 85 | if (chip->model_id == last_model_id) |
| 86 | return; |
| 87 | |
| 88 | size = chip->total_size * 1024; |
| 89 | if (size > rom_size) { |
| 90 | /* Undefine all block_erasers that don't operate on the whole chip, |
| 91 | * and adjust the eraseblock size of the one that does. |
| 92 | */ |
| 93 | for (i = 0; i < NUM_ERASEFUNCTIONS; ++i) { |
| 94 | if (chip->block_erasers[i].eraseblocks[0].size != size) { |
| 95 | chip->block_erasers[i].eraseblocks[0].count = 0; |
| 96 | chip->block_erasers[i].block_erase = NULL; |
| 97 | } else { |
| 98 | chip->block_erasers[i].eraseblocks[0].size = rom_size; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (i != NUM_ERASEFUNCTIONS) { |
| 104 | chip->total_size = rom_size / 1024; |
| 105 | if (chip->page_size > rom_size) |
| 106 | chip->page_size = rom_size; |
| 107 | } else { |
| 108 | msg_pdbg("Failed to adjust size of chip \"%s\" (%d kB).\n", chip->name, |
| 109 | chip->total_size); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | last_model_id = chip->model_id; |
| 114 | } |
| 115 | |
| 116 | int atapromise_init(void) |
| 117 | { |
| 118 | struct pci_dev *dev = NULL; |
| 119 | |
| 120 | if (rget_io_perms()) |
| 121 | return 1; |
| 122 | |
| 123 | dev = pcidev_init(ata_promise, PCI_BASE_ADDRESS_4); |
| 124 | if (!dev) |
| 125 | return 1; |
| 126 | |
| 127 | io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_4) & 0xfffe; |
| 128 | if (!io_base_addr) { |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /* Not exactly sure what this does, because flashing seems to work |
| 133 | * well without it. However, PTIFLASH does it, so we do it too. |
| 134 | */ |
| 135 | OUTB(1, io_base_addr + 0x10); |
| 136 | |
| 137 | rom_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_5); |
| 138 | if (!rom_base_addr) { |
| 139 | msg_pdbg("Failed to read BAR5\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | rom_size = dev->rom_size > MAX_ROM_DECODE ? MAX_ROM_DECODE : dev->rom_size; |
| 144 | atapromise_bar = (uint8_t*)rphysmap("Promise", rom_base_addr, rom_size); |
| 145 | if (atapromise_bar == ERROR_PTR) { |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | max_rom_decode.parallel = rom_size; |
| 150 | register_par_master(&par_master_atapromise, BUS_PARALLEL); |
| 151 | |
| 152 | msg_pwarn("Do not use this device as a generic programmer. It will leave anything outside\n" |
| 153 | "the first %zu kB of the flash chip in an undefined state. It works fine for the\n" |
| 154 | "purpose of updating the firmware of this device (padding may neccessary).\n", |
| 155 | rom_size / 1024); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void atapromise_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
| 161 | { |
| 162 | uint32_t data; |
| 163 | |
| 164 | atapromise_limit_chip(flash->chip); |
| 165 | data = (rom_base_addr + (addr & ADDR_MASK)) << 8 | val; |
| 166 | OUTL(data, io_base_addr + 0x14); |
| 167 | } |
| 168 | |
| 169 | static uint8_t atapromise_chip_readb(const struct flashctx *flash, const chipaddr addr) |
| 170 | { |
| 171 | atapromise_limit_chip(flash->chip); |
| 172 | return pci_mmio_readb(atapromise_bar + (addr & ADDR_MASK)); |
| 173 | } |
| 174 | |
| 175 | #else |
| 176 | #error PCI port I/O access is not supported on this architecture yet. |
| 177 | #endif |