| 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> |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +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 | 74b4aa0 | 2021-12-14 17:52:30 +0100 | [diff] [blame] | 21 | #include "hwaccess_physmap.h" |
| Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 22 | #include "platform/pci.h" |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 23 | |
| 24 | #define MAX_ROM_DECODE (32 * 1024) |
| 25 | #define ADDR_MASK (MAX_ROM_DECODE - 1) |
| 26 | |
| 27 | /* |
| 28 | * In the absence of any public docs on the PDC2026x family, this programmer was created through a mix of |
| 29 | * reverse-engineering and trial and error. |
| 30 | * |
| 31 | * The only device tested is an Ultra100 controller, but the logic for programming the other 2026x controllers |
| 32 | * is the same, so it should, in theory, work for those as well. |
| 33 | * |
| 34 | * While the tested Ultra100 controller used a 128 kB MX29F001T chip, A16 and A15 showed continuity to ground, |
| 35 | * thus limiting the the programmer on this card to 32 kB. Without other controllers to test this programmer on, |
| 36 | * this is currently a hard limit. Note that ROM files for these controllers are 16 kB only. |
| 37 | * |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 38 | * Since flashprog does not support accessing flash chips larger than the size limit of the programmer (the |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 39 | * tested Ultra100 uses a 128 kB MX29F001T chip), the chip size is hackishly adjusted in atapromise_limit_chip. |
| 40 | */ |
| 41 | |
| 42 | static uint32_t io_base_addr = 0; |
| 43 | static uint32_t rom_base_addr = 0; |
| 44 | |
| 45 | static uint8_t *atapromise_bar = NULL; |
| 46 | static size_t rom_size = 0; |
| 47 | |
| Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 48 | static const struct dev_entry ata_promise[] = { |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 49 | {0x105a, 0x4d38, NT, "Promise", "PDC20262 (FastTrak66/Ultra66)"}, |
| 50 | {0x105a, 0x0d30, NT, "Promise", "PDC20265 (FastTrak100 Lite/Ultra100)"}, |
| 51 | {0x105a, 0x4d30, OK, "Promise", "PDC20267 (FastTrak100/Ultra100)"}, |
| 52 | {0}, |
| 53 | }; |
| 54 | |
| Nico Huber | dd6e07a | 2026-02-21 17:55:26 +0100 | [diff] [blame^] | 55 | static void atapromise_chip_writeb(const struct par_master *, uint8_t val, chipaddr); |
| 56 | static uint8_t atapromise_chip_readb(const struct par_master *, chipaddr); |
| Nico Huber | 0e76d99 | 2023-01-12 20:22:55 +0100 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | static const struct par_master par_master_atapromise = { |
| Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 60 | .chip_readb = atapromise_chip_readb, |
| 61 | .chip_readw = fallback_chip_readw, |
| 62 | .chip_readl = fallback_chip_readl, |
| 63 | .chip_readn = fallback_chip_readn, |
| 64 | .chip_writeb = atapromise_chip_writeb, |
| 65 | .chip_writew = fallback_chip_writew, |
| 66 | .chip_writel = fallback_chip_writel, |
| 67 | .chip_writen = fallback_chip_writen, |
| Nico Huber | 0e76d99 | 2023-01-12 20:22:55 +0100 | [diff] [blame] | 68 | .map_flash = atapromise_map, |
| 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 | |
| Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 77 | static int atapromise_init(struct flashprog_programmer *const prog) |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 78 | { |
| 79 | struct pci_dev *dev = NULL; |
| 80 | |
| 81 | if (rget_io_perms()) |
| 82 | return 1; |
| 83 | |
| 84 | dev = pcidev_init(ata_promise, PCI_BASE_ADDRESS_4); |
| 85 | if (!dev) |
| 86 | return 1; |
| 87 | |
| 88 | io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_4) & 0xfffe; |
| 89 | if (!io_base_addr) { |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | /* Not exactly sure what this does, because flashing seems to work |
| 94 | * well without it. However, PTIFLASH does it, so we do it too. |
| 95 | */ |
| 96 | OUTB(1, io_base_addr + 0x10); |
| 97 | |
| 98 | rom_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_5); |
| 99 | if (!rom_base_addr) { |
| 100 | msg_pdbg("Failed to read BAR5\n"); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | rom_size = dev->rom_size > MAX_ROM_DECODE ? MAX_ROM_DECODE : dev->rom_size; |
| 105 | atapromise_bar = (uint8_t*)rphysmap("Promise", rom_base_addr, rom_size); |
| 106 | if (atapromise_bar == ERROR_PTR) { |
| 107 | return 1; |
| 108 | } |
| 109 | |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 110 | msg_pwarn("Do not use this device as a generic programmer. It will leave anything outside\n" |
| 111 | "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] | 112 | "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] | 113 | rom_size / 1024); |
| 114 | |
| Nico Huber | 47aa85c | 2026-02-21 14:57:20 +0100 | [diff] [blame] | 115 | return register_par_master(&par_master_atapromise, BUS_PARALLEL, 0, rom_size, NULL); |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| Nico Huber | dd6e07a | 2026-02-21 17:55:26 +0100 | [diff] [blame^] | 118 | static void atapromise_chip_writeb(const struct par_master *par, uint8_t val, chipaddr addr) |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 119 | { |
| 120 | uint32_t data; |
| 121 | |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 122 | data = (rom_base_addr + (addr & ADDR_MASK)) << 8 | val; |
| 123 | OUTL(data, io_base_addr + 0x14); |
| 124 | } |
| 125 | |
| Nico Huber | dd6e07a | 2026-02-21 17:55:26 +0100 | [diff] [blame^] | 126 | static uint8_t atapromise_chip_readb(const struct par_master *par, const chipaddr addr) |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 127 | { |
| Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 128 | return pci_mmio_readb(atapromise_bar + (addr & ADDR_MASK)); |
| 129 | } |
| 130 | |
| Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 131 | const struct programmer_entry programmer_atapromise = { |
| 132 | .name = "atapromise", |
| 133 | .type = PCI, |
| 134 | .devs.dev = ata_promise, |
| 135 | .init = atapromise_init, |
| Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 136 | }; |