Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> |
| 5 | * Copyright (C) 2011 Jonathan Kollasch <jakllsch@kollasch.net> |
| 6 | * Copyright (C) 2012-2013 Stefan Tauner |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include "flash.h" |
| 26 | #include "programmer.h" |
| 27 | #include "hwaccess.h" |
| 28 | |
| 29 | #define PCI_VENDOR_ID_VIA 0x1106 |
| 30 | |
| 31 | #define VIA_MAX_RETRIES 300 |
| 32 | |
| 33 | #define BROM_ADDR 0x60 |
| 34 | |
| 35 | #define BROM_DATA 0x64 |
| 36 | |
| 37 | #define BROM_ACCESS 0x68 |
| 38 | #define BROM_TRIGGER 0x80 |
| 39 | #define BROM_WRITE 0x40 |
| 40 | #define BROM_SIZE_MASK 0x30 |
| 41 | #define BROM_SIZE_64K 0x00 |
| 42 | #define BROM_SIZE_32K 0x10 |
| 43 | #define BROM_SIZE_16K 0x20 |
| 44 | #define BROM_SIZE_0K 0x30 |
| 45 | #define BROM_BYTE_ENABLE_MASK 0x0f |
| 46 | |
| 47 | #define BROM_STATUS 0x69 |
| 48 | #define BROM_ERROR_STATUS 0x80 |
| 49 | |
| 50 | /* Select the byte we want to access. This is done by clearing the bit corresponding to the byte we want to |
| 51 | * access, leaving the others set (yes, really). */ |
| 52 | #define ENABLE_BYTE(address) ((~(1 << ((address) & 3))) & BROM_BYTE_ENABLE_MASK) |
| 53 | #define BYTE_OFFSET(address) (((addr) & 3) * 8) |
| 54 | |
| 55 | const struct dev_entry ata_via[] = { |
| 56 | {PCI_VENDOR_ID_VIA, 0x3249, DEP, "VIA", "VT6421A"}, |
| 57 | |
| 58 | {}, |
| 59 | }; |
| 60 | |
| 61 | static void atavia_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 62 | static uint8_t atavia_chip_readb(const struct flashctx *flash, const chipaddr addr); |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame^] | 63 | static const struct par_master lpc_master_atavia = { |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 64 | .chip_readb = atavia_chip_readb, |
| 65 | .chip_readw = fallback_chip_readw, |
| 66 | .chip_readl = fallback_chip_readl, |
| 67 | .chip_readn = fallback_chip_readn, |
| 68 | .chip_writeb = atavia_chip_writeb, |
| 69 | .chip_writew = fallback_chip_writew, |
| 70 | .chip_writel = fallback_chip_writel, |
| 71 | .chip_writen = fallback_chip_writen, |
| 72 | }; |
| 73 | |
| 74 | static void *atavia_offset = NULL; |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 75 | static struct pci_dev *dev = NULL; |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 76 | |
| 77 | static void atavia_prettyprint_access(uint8_t access) |
| 78 | { |
| 79 | uint8_t bmask = access & BROM_BYTE_ENABLE_MASK; |
| 80 | uint8_t size = access & BROM_SIZE_MASK; |
| 81 | |
| 82 | msg_pspew("Accessing byte(s):%s%s%s%s\n", |
| 83 | ((bmask & (1<<3)) == 0) ? " 3" : "", |
| 84 | ((bmask & (1<<2)) == 0) ? " 2" : "", |
| 85 | ((bmask & (1<<1)) == 0) ? " 1" : "", |
| 86 | ((bmask & (1<<0)) == 0) ? " 0" : ""); |
| 87 | if (size == BROM_SIZE_0K) { |
| 88 | msg_pspew("No ROM device found.\n"); |
| 89 | } else |
| 90 | msg_pspew("ROM device with %s kB attached.\n", |
| 91 | (size == BROM_SIZE_64K) ? ">=64" : |
| 92 | (size == BROM_SIZE_32K) ? "32" : "16"); |
| 93 | msg_pspew("Access is a %s.\n", (access & BROM_WRITE) ? "write" : "read"); |
| 94 | msg_pspew("Device is %s.\n", (access & BROM_TRIGGER) ? "busy" : "ready"); |
| 95 | } |
| 96 | |
| 97 | static bool atavia_ready(struct pci_dev *pcidev_dev) |
| 98 | { |
| 99 | int try; |
| 100 | uint8_t access, status; |
| 101 | bool ready = false; |
| 102 | |
| 103 | for (try = 0; try < VIA_MAX_RETRIES; try++) { |
| 104 | access = pci_read_byte(pcidev_dev, BROM_ACCESS); |
| 105 | status = pci_read_byte(pcidev_dev, BROM_STATUS); |
| 106 | if (((access & BROM_TRIGGER) == 0) && (status & BROM_ERROR_STATUS) == 0) { |
| 107 | ready = true; |
| 108 | break; |
| 109 | } else { |
| 110 | programmer_delay(1); |
| 111 | continue; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | msg_pdbg2("\n%s: %s after %d tries (access=0x%02x, status=0x%02x)\n", |
| 116 | __func__, ready ? "suceeded" : "failed", try, access, status); |
| 117 | atavia_prettyprint_access(access); |
| 118 | return ready; |
| 119 | } |
| 120 | |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 121 | void *atavia_map(const char *descr, uintptr_t phys_addr, size_t len) |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 122 | { |
| 123 | return (atavia_offset != 0) ? atavia_offset : (void *)phys_addr; |
| 124 | } |
| 125 | |
| 126 | int atavia_init(void) |
| 127 | { |
| 128 | char *arg = extract_programmer_param("offset"); |
| 129 | if (arg) { |
| 130 | if (strlen(arg) == 0) { |
| 131 | msg_perr("Missing argument for offset.\n"); |
| 132 | free(arg); |
| 133 | return ERROR_FATAL; |
| 134 | } |
| 135 | char *endptr; |
| 136 | atavia_offset = (void *)strtoul(arg, &endptr, 0); |
| 137 | if (*endptr) { |
| 138 | msg_perr("Error: Invalid offset specified: \"%s\".\n", arg); |
| 139 | free(arg); |
| 140 | return ERROR_FATAL; |
| 141 | } |
| 142 | msg_pinfo("Mapping addresses to base %p.\n", atavia_offset); |
| 143 | } |
| 144 | free(arg); |
| 145 | |
| 146 | if (rget_io_perms()) |
| 147 | return 1; |
| 148 | |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 149 | dev = pcidev_init(ata_via, PCI_ROM_ADDRESS); /* Acutally no BAR setup needed at all. */ |
| 150 | if (!dev) |
| 151 | return 1; |
| 152 | |
| 153 | /* Test if a flash chip is attached. */ |
| 154 | pci_write_long(dev, PCI_ROM_ADDRESS, (uint32_t)PCI_ROM_ADDRESS_MASK); |
| 155 | programmer_delay(90); |
| 156 | uint32_t base = pci_read_long(dev, PCI_ROM_ADDRESS); |
| 157 | msg_pdbg2("BROM base=0x%08x\n", base); |
| 158 | if ((base & PCI_ROM_ADDRESS_MASK) == 0) { |
| 159 | msg_pwarn("Controller thinks there is no ROM attached.\n"); |
| 160 | } |
| 161 | |
| 162 | if (!atavia_ready(dev)) { |
| 163 | msg_perr("Controller not ready.\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame^] | 167 | register_par_master(&lpc_master_atavia, BUS_LPC); |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void atavia_chip_writeb(const struct flashctx *flash, uint8_t val, const chipaddr addr) |
| 173 | { |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 174 | msg_pspew("%s: 0x%02x to 0x%*" PRIxPTR ".\n", __func__, val, PRIxPTR_WIDTH, addr); |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 175 | pci_write_long(dev, BROM_ADDR, (addr & ~3)); |
| 176 | pci_write_long(dev, BROM_DATA, val << BYTE_OFFSET(addr)); |
| 177 | pci_write_byte(dev, BROM_ACCESS, BROM_TRIGGER | BROM_WRITE | ENABLE_BYTE(addr)); |
| 178 | |
| 179 | if (!atavia_ready(dev)) { |
| 180 | msg_perr("not ready after write\n"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static uint8_t atavia_chip_readb(const struct flashctx *flash, const chipaddr addr) |
| 185 | { |
| 186 | pci_write_long(dev, BROM_ADDR, (addr & ~3)); |
| 187 | pci_write_byte(dev, BROM_ACCESS, BROM_TRIGGER | ENABLE_BYTE(addr)); |
| 188 | |
| 189 | if (!atavia_ready(dev)) { |
| 190 | msg_perr("not ready after read\n"); |
| 191 | } |
| 192 | |
| 193 | uint8_t val = (pci_read_long(dev, BROM_DATA) >> BYTE_OFFSET(addr)) & 0xff; |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 194 | msg_pspew("%s: 0x%02x from 0x%*" PRIxPTR ".\n", __func__, val, PRIxPTR_WIDTH, addr); |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 195 | return val; |
| 196 | } |