blob: ca63df76662ca57a60ace8c957af3cf8f0da333f [file] [log] [blame]
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +00001/*
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. Lehnerc2644a32016-01-16 23:45:25 +000015 */
16
17#if defined(__i386__) || defined(__x86_64__)
18
19#include <string.h>
20#include <stdlib.h>
21#include "flash.h"
22#include "programmer.h"
23#include "hwaccess.h"
24
25#define MAX_ROM_DECODE (32 * 1024)
26#define ADDR_MASK (MAX_ROM_DECODE - 1)
27
28/*
29 * In the absence of any public docs on the PDC2026x family, this programmer was created through a mix of
30 * reverse-engineering and trial and error.
31 *
32 * The only device tested is an Ultra100 controller, but the logic for programming the other 2026x controllers
33 * is the same, so it should, in theory, work for those as well.
34 *
35 * While the tested Ultra100 controller used a 128 kB MX29F001T chip, A16 and A15 showed continuity to ground,
36 * thus limiting the the programmer on this card to 32 kB. Without other controllers to test this programmer on,
37 * this is currently a hard limit. Note that ROM files for these controllers are 16 kB only.
38 *
39 * Since flashrom does not support accessing flash chips larger than the size limit of the programmer (the
40 * tested Ultra100 uses a 128 kB MX29F001T chip), the chip size is hackishly adjusted in atapromise_limit_chip.
41 */
42
43static uint32_t io_base_addr = 0;
44static uint32_t rom_base_addr = 0;
45
46static uint8_t *atapromise_bar = NULL;
47static size_t rom_size = 0;
48
49const struct dev_entry ata_promise[] = {
50 {0x105a, 0x4d38, NT, "Promise", "PDC20262 (FastTrak66/Ultra66)"},
51 {0x105a, 0x0d30, NT, "Promise", "PDC20265 (FastTrak100 Lite/Ultra100)"},
52 {0x105a, 0x4d30, OK, "Promise", "PDC20267 (FastTrak100/Ultra100)"},
53 {0},
54};
55
56static void atapromise_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
57static uint8_t atapromise_chip_readb(const struct flashctx *flash, const chipaddr addr);
58
59static const struct par_master par_master_atapromise = {
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,
68};
69
70void *atapromise_map(const char *descr, uintptr_t phys_addr, size_t len)
71{
72 /* In case fallback_map ever returns something other than NULL. */
73 return NULL;
74}
75
76static void atapromise_limit_chip(struct flashchip *chip)
77{
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +000078 unsigned int i, size;
Carl-Daniel Hailfinger1c2d23a2016-02-18 23:11:52 +000079 unsigned int usable_erasers = 0;
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +000080
81 size = chip->total_size * 1024;
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +000082
Carl-Daniel Hailfinger1c2d23a2016-02-18 23:11:52 +000083 /* Chip is small enough or already limited. */
84 if (size <= rom_size)
85 return;
86
87 /* Undefine all block_erasers that don't operate on the whole chip,
88 * and adjust the eraseblock size of those which do.
89 */
90 for (i = 0; i < NUM_ERASEFUNCTIONS; ++i) {
91 if (chip->block_erasers[i].eraseblocks[0].size != size) {
92 chip->block_erasers[i].eraseblocks[0].count = 0;
93 chip->block_erasers[i].block_erase = NULL;
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +000094 } else {
Carl-Daniel Hailfinger1c2d23a2016-02-18 23:11:52 +000095 chip->block_erasers[i].eraseblocks[0].size = rom_size;
96 usable_erasers++;
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +000097 }
98 }
99
Carl-Daniel Hailfinger1c2d23a2016-02-18 23:11:52 +0000100 if (usable_erasers) {
101 chip->total_size = rom_size / 1024;
102 if (chip->page_size > rom_size)
103 chip->page_size = rom_size;
104 } else {
105 msg_pdbg("Failed to adjust size of chip \"%s\" (%d kB).\n", chip->name, chip->total_size);
106 }
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +0000107}
108
109int atapromise_init(void)
110{
111 struct pci_dev *dev = NULL;
112
113 if (rget_io_perms())
114 return 1;
115
116 dev = pcidev_init(ata_promise, PCI_BASE_ADDRESS_4);
117 if (!dev)
118 return 1;
119
120 io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_4) & 0xfffe;
121 if (!io_base_addr) {
122 return 1;
123 }
124
125 /* Not exactly sure what this does, because flashing seems to work
126 * well without it. However, PTIFLASH does it, so we do it too.
127 */
128 OUTB(1, io_base_addr + 0x10);
129
130 rom_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_5);
131 if (!rom_base_addr) {
132 msg_pdbg("Failed to read BAR5\n");
133 return 1;
134 }
135
136 rom_size = dev->rom_size > MAX_ROM_DECODE ? MAX_ROM_DECODE : dev->rom_size;
137 atapromise_bar = (uint8_t*)rphysmap("Promise", rom_base_addr, rom_size);
138 if (atapromise_bar == ERROR_PTR) {
139 return 1;
140 }
141
142 max_rom_decode.parallel = rom_size;
143 register_par_master(&par_master_atapromise, BUS_PARALLEL);
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"
147 "purpose of updating the firmware of this device (padding may neccessary).\n",
148 rom_size / 1024);
149
150 return 0;
151}
152
153static 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
162static 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
168#else
169#error PCI port I/O access is not supported on this architecture yet.
170#endif