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. |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include "flash.h" |
| 20 | #include "programmer.h" |
| 21 | #include "hwaccess.h" |
Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 22 | #include "hwaccess_x86_io.h" |
Thomas Heijligen | 74b4aa0 | 2021-12-14 17:52:30 +0100 | [diff] [blame] | 23 | #include "hwaccess_physmap.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 24 | #include "platform/pci.h" |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 25 | |
| 26 | #define MAX_ROM_DECODE (32 * 1024) |
| 27 | #define ADDR_MASK (MAX_ROM_DECODE - 1) |
| 28 | |
| 29 | /* |
| 30 | * In the absence of any public docs on the PDC2026x family, this programmer was created through a mix of |
| 31 | * reverse-engineering and trial and error. |
| 32 | * |
| 33 | * The only device tested is an Ultra100 controller, but the logic for programming the other 2026x controllers |
| 34 | * is the same, so it should, in theory, work for those as well. |
| 35 | * |
| 36 | * While the tested Ultra100 controller used a 128 kB MX29F001T chip, A16 and A15 showed continuity to ground, |
| 37 | * thus limiting the the programmer on this card to 32 kB. Without other controllers to test this programmer on, |
| 38 | * this is currently a hard limit. Note that ROM files for these controllers are 16 kB only. |
| 39 | * |
| 40 | * Since flashrom does not support accessing flash chips larger than the size limit of the programmer (the |
| 41 | * tested Ultra100 uses a 128 kB MX29F001T chip), the chip size is hackishly adjusted in atapromise_limit_chip. |
| 42 | */ |
| 43 | |
| 44 | static uint32_t io_base_addr = 0; |
| 45 | static uint32_t rom_base_addr = 0; |
| 46 | |
| 47 | static uint8_t *atapromise_bar = NULL; |
| 48 | static size_t rom_size = 0; |
| 49 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 50 | static const struct dev_entry ata_promise[] = { |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 51 | {0x105a, 0x4d38, NT, "Promise", "PDC20262 (FastTrak66/Ultra66)"}, |
| 52 | {0x105a, 0x0d30, NT, "Promise", "PDC20265 (FastTrak100 Lite/Ultra100)"}, |
| 53 | {0x105a, 0x4d30, OK, "Promise", "PDC20267 (FastTrak100/Ultra100)"}, |
| 54 | {0}, |
| 55 | }; |
| 56 | |
| 57 | static void atapromise_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 58 | static uint8_t atapromise_chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 59 | |
| 60 | static const struct par_master par_master_atapromise = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 61 | .chip_readb = atapromise_chip_readb, |
| 62 | .chip_readw = fallback_chip_readw, |
| 63 | .chip_readl = fallback_chip_readl, |
| 64 | .chip_readn = fallback_chip_readn, |
| 65 | .chip_writeb = atapromise_chip_writeb, |
| 66 | .chip_writew = fallback_chip_writew, |
| 67 | .chip_writel = fallback_chip_writel, |
| 68 | .chip_writen = fallback_chip_writen, |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 71 | static void *atapromise_map(const char *descr, uintptr_t phys_addr, size_t len) |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 72 | { |
| 73 | /* In case fallback_map ever returns something other than NULL. */ |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | static void atapromise_limit_chip(struct flashchip *chip) |
| 78 | { |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 79 | unsigned int i, size; |
Carl-Daniel Hailfinger | 1c2d23a | 2016-02-18 23:11:52 +0000 | [diff] [blame] | 80 | unsigned int usable_erasers = 0; |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 81 | |
| 82 | size = chip->total_size * 1024; |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 83 | |
Carl-Daniel Hailfinger | 1c2d23a | 2016-02-18 23:11:52 +0000 | [diff] [blame] | 84 | /* Chip is small enough or already limited. */ |
| 85 | if (size <= rom_size) |
| 86 | return; |
| 87 | |
| 88 | /* Undefine all block_erasers that don't operate on the whole chip, |
| 89 | * and adjust the eraseblock size of those which do. |
| 90 | */ |
| 91 | for (i = 0; i < NUM_ERASEFUNCTIONS; ++i) { |
| 92 | if (chip->block_erasers[i].eraseblocks[0].size != size) { |
| 93 | chip->block_erasers[i].eraseblocks[0].count = 0; |
| 94 | chip->block_erasers[i].block_erase = NULL; |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 95 | } else { |
Carl-Daniel Hailfinger | 1c2d23a | 2016-02-18 23:11:52 +0000 | [diff] [blame] | 96 | chip->block_erasers[i].eraseblocks[0].size = rom_size; |
| 97 | usable_erasers++; |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Carl-Daniel Hailfinger | 1c2d23a | 2016-02-18 23:11:52 +0000 | [diff] [blame] | 101 | if (usable_erasers) { |
| 102 | chip->total_size = rom_size / 1024; |
| 103 | if (chip->page_size > rom_size) |
| 104 | chip->page_size = rom_size; |
| 105 | } else { |
| 106 | msg_pdbg("Failed to adjust size of chip \"%s\" (%d kB).\n", chip->name, chip->total_size); |
| 107 | } |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 110 | static int atapromise_init(void) |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 111 | { |
| 112 | struct pci_dev *dev = NULL; |
| 113 | |
| 114 | if (rget_io_perms()) |
| 115 | return 1; |
| 116 | |
| 117 | dev = pcidev_init(ata_promise, PCI_BASE_ADDRESS_4); |
| 118 | if (!dev) |
| 119 | return 1; |
| 120 | |
| 121 | io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_4) & 0xfffe; |
| 122 | if (!io_base_addr) { |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | /* Not exactly sure what this does, because flashing seems to work |
| 127 | * well without it. However, PTIFLASH does it, so we do it too. |
| 128 | */ |
| 129 | OUTB(1, io_base_addr + 0x10); |
| 130 | |
| 131 | rom_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_5); |
| 132 | if (!rom_base_addr) { |
| 133 | msg_pdbg("Failed to read BAR5\n"); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | rom_size = dev->rom_size > MAX_ROM_DECODE ? MAX_ROM_DECODE : dev->rom_size; |
| 138 | atapromise_bar = (uint8_t*)rphysmap("Promise", rom_base_addr, rom_size); |
| 139 | if (atapromise_bar == ERROR_PTR) { |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | max_rom_decode.parallel = rom_size; |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 144 | |
| 145 | msg_pwarn("Do not use this device as a generic programmer. It will leave anything outside\n" |
| 146 | "the first %zu kB of the flash chip in an undefined state. It works fine for the\n" |
Elyes HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 147 | "purpose of updating the firmware of this device (padding may necessary).\n", |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 148 | rom_size / 1024); |
| 149 | |
Anastasia Klimchuk | c1f2a47 | 2021-08-27 15:47:46 +1000 | [diff] [blame] | 150 | return register_par_master(&par_master_atapromise, BUS_PARALLEL, NULL); |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void atapromise_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
| 154 | { |
| 155 | uint32_t data; |
| 156 | |
| 157 | atapromise_limit_chip(flash->chip); |
| 158 | data = (rom_base_addr + (addr & ADDR_MASK)) << 8 | val; |
| 159 | OUTL(data, io_base_addr + 0x14); |
| 160 | } |
| 161 | |
| 162 | static uint8_t atapromise_chip_readb(const struct flashctx *flash, const chipaddr addr) |
| 163 | { |
| 164 | atapromise_limit_chip(flash->chip); |
| 165 | return pci_mmio_readb(atapromise_bar + (addr & ADDR_MASK)); |
| 166 | } |
| 167 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 168 | const struct programmer_entry programmer_atapromise = { |
| 169 | .name = "atapromise", |
| 170 | .type = PCI, |
| 171 | .devs.dev = ata_promise, |
| 172 | .init = atapromise_init, |
| 173 | .map_flash_region = atapromise_map, |
| 174 | .unmap_flash_region = fallback_unmap, |
| 175 | .delay = internal_delay, |
| 176 | }; |