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. |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include "flash.h" |
| 22 | #include "programmer.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 23 | #include "platform/pci.h" |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 24 | |
| 25 | #define PCI_VENDOR_ID_VIA 0x1106 |
| 26 | |
| 27 | #define VIA_MAX_RETRIES 300 |
| 28 | |
| 29 | #define BROM_ADDR 0x60 |
| 30 | |
| 31 | #define BROM_DATA 0x64 |
| 32 | |
| 33 | #define BROM_ACCESS 0x68 |
| 34 | #define BROM_TRIGGER 0x80 |
| 35 | #define BROM_WRITE 0x40 |
| 36 | #define BROM_SIZE_MASK 0x30 |
| 37 | #define BROM_SIZE_64K 0x00 |
| 38 | #define BROM_SIZE_32K 0x10 |
| 39 | #define BROM_SIZE_16K 0x20 |
| 40 | #define BROM_SIZE_0K 0x30 |
| 41 | #define BROM_BYTE_ENABLE_MASK 0x0f |
| 42 | |
| 43 | #define BROM_STATUS 0x69 |
| 44 | #define BROM_ERROR_STATUS 0x80 |
| 45 | |
| 46 | /* Select the byte we want to access. This is done by clearing the bit corresponding to the byte we want to |
| 47 | * access, leaving the others set (yes, really). */ |
| 48 | #define ENABLE_BYTE(address) ((~(1 << ((address) & 3))) & BROM_BYTE_ENABLE_MASK) |
Alexander Goncharov | c50f4ae | 2022-06-17 08:32:47 +0300 | [diff] [blame] | 49 | #define BYTE_OFFSET(address) (((address) & 3) * 8) |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 50 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 51 | static const struct dev_entry ata_via[] = { |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 52 | {PCI_VENDOR_ID_VIA, 0x3249, DEP, "VIA", "VT6421A"}, |
| 53 | |
Evgeny Zinoviev | 83c56b8 | 2019-11-05 17:47:43 +0300 | [diff] [blame] | 54 | {0}, |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static void atavia_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 58 | static uint8_t atavia_chip_readb(const struct flashctx *flash, const chipaddr addr); |
Nico Huber | 0e76d99 | 2023-01-12 20:22:55 +0100 | [diff] [blame^] | 59 | static void *atavia_map(const char *descr, uintptr_t phys_addr, size_t len); |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 60 | static const struct par_master lpc_master_atavia = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 61 | .chip_readb = atavia_chip_readb, |
| 62 | .chip_readw = fallback_chip_readw, |
| 63 | .chip_readl = fallback_chip_readl, |
| 64 | .chip_readn = fallback_chip_readn, |
| 65 | .chip_writeb = atavia_chip_writeb, |
| 66 | .chip_writew = fallback_chip_writew, |
| 67 | .chip_writel = fallback_chip_writel, |
| 68 | .chip_writen = fallback_chip_writen, |
Nico Huber | 0e76d99 | 2023-01-12 20:22:55 +0100 | [diff] [blame^] | 69 | .map_flash = atavia_map, |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static void *atavia_offset = NULL; |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 73 | static struct pci_dev *dev = NULL; |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 74 | |
| 75 | static void atavia_prettyprint_access(uint8_t access) |
| 76 | { |
| 77 | uint8_t bmask = access & BROM_BYTE_ENABLE_MASK; |
| 78 | uint8_t size = access & BROM_SIZE_MASK; |
| 79 | |
| 80 | msg_pspew("Accessing byte(s):%s%s%s%s\n", |
| 81 | ((bmask & (1<<3)) == 0) ? " 3" : "", |
| 82 | ((bmask & (1<<2)) == 0) ? " 2" : "", |
| 83 | ((bmask & (1<<1)) == 0) ? " 1" : "", |
| 84 | ((bmask & (1<<0)) == 0) ? " 0" : ""); |
| 85 | if (size == BROM_SIZE_0K) { |
| 86 | msg_pspew("No ROM device found.\n"); |
| 87 | } else |
| 88 | msg_pspew("ROM device with %s kB attached.\n", |
| 89 | (size == BROM_SIZE_64K) ? ">=64" : |
| 90 | (size == BROM_SIZE_32K) ? "32" : "16"); |
| 91 | msg_pspew("Access is a %s.\n", (access & BROM_WRITE) ? "write" : "read"); |
| 92 | msg_pspew("Device is %s.\n", (access & BROM_TRIGGER) ? "busy" : "ready"); |
| 93 | } |
| 94 | |
| 95 | static bool atavia_ready(struct pci_dev *pcidev_dev) |
| 96 | { |
| 97 | int try; |
| 98 | uint8_t access, status; |
| 99 | bool ready = false; |
| 100 | |
| 101 | for (try = 0; try < VIA_MAX_RETRIES; try++) { |
| 102 | access = pci_read_byte(pcidev_dev, BROM_ACCESS); |
| 103 | status = pci_read_byte(pcidev_dev, BROM_STATUS); |
| 104 | if (((access & BROM_TRIGGER) == 0) && (status & BROM_ERROR_STATUS) == 0) { |
| 105 | ready = true; |
| 106 | break; |
| 107 | } else { |
| 108 | programmer_delay(1); |
| 109 | continue; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | msg_pdbg2("\n%s: %s after %d tries (access=0x%02x, status=0x%02x)\n", |
Stefan Tauner | 6697f71 | 2014-08-06 15:09:15 +0000 | [diff] [blame] | 114 | __func__, ready ? "succeeded" : "failed", try, access, status); |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 115 | atavia_prettyprint_access(access); |
| 116 | return ready; |
| 117 | } |
| 118 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 119 | static 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] | 120 | { |
| 121 | return (atavia_offset != 0) ? atavia_offset : (void *)phys_addr; |
| 122 | } |
| 123 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 124 | static int atavia_init(void) |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 125 | { |
| 126 | char *arg = extract_programmer_param("offset"); |
| 127 | if (arg) { |
| 128 | if (strlen(arg) == 0) { |
| 129 | msg_perr("Missing argument for offset.\n"); |
| 130 | free(arg); |
| 131 | return ERROR_FATAL; |
| 132 | } |
| 133 | char *endptr; |
| 134 | atavia_offset = (void *)strtoul(arg, &endptr, 0); |
| 135 | if (*endptr) { |
| 136 | msg_perr("Error: Invalid offset specified: \"%s\".\n", arg); |
| 137 | free(arg); |
| 138 | return ERROR_FATAL; |
| 139 | } |
| 140 | msg_pinfo("Mapping addresses to base %p.\n", atavia_offset); |
| 141 | } |
| 142 | free(arg); |
| 143 | |
Carl-Daniel Hailfinger | e4c2b48 | 2020-01-20 11:22:41 +0100 | [diff] [blame] | 144 | dev = pcidev_init(ata_via, PCI_ROM_ADDRESS); /* Actually no BAR setup needed at all. */ |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 145 | if (!dev) |
| 146 | return 1; |
| 147 | |
| 148 | /* Test if a flash chip is attached. */ |
| 149 | pci_write_long(dev, PCI_ROM_ADDRESS, (uint32_t)PCI_ROM_ADDRESS_MASK); |
| 150 | programmer_delay(90); |
| 151 | uint32_t base = pci_read_long(dev, PCI_ROM_ADDRESS); |
| 152 | msg_pdbg2("BROM base=0x%08x\n", base); |
| 153 | if ((base & PCI_ROM_ADDRESS_MASK) == 0) { |
| 154 | msg_pwarn("Controller thinks there is no ROM attached.\n"); |
| 155 | } |
| 156 | |
| 157 | if (!atavia_ready(dev)) { |
| 158 | msg_perr("Controller not ready.\n"); |
| 159 | return 1; |
| 160 | } |
| 161 | |
Anastasia Klimchuk | c1f2a47 | 2021-08-27 15:47:46 +1000 | [diff] [blame] | 162 | return register_par_master(&lpc_master_atavia, BUS_LPC, NULL); |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void atavia_chip_writeb(const struct flashctx *flash, uint8_t val, const chipaddr addr) |
| 166 | { |
Stefan Tauner | 4f09475 | 2014-06-01 22:36:30 +0000 | [diff] [blame] | 167 | 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] | 168 | pci_write_long(dev, BROM_ADDR, (addr & ~3)); |
| 169 | pci_write_long(dev, BROM_DATA, val << BYTE_OFFSET(addr)); |
| 170 | pci_write_byte(dev, BROM_ACCESS, BROM_TRIGGER | BROM_WRITE | ENABLE_BYTE(addr)); |
| 171 | |
| 172 | if (!atavia_ready(dev)) { |
| 173 | msg_perr("not ready after write\n"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static uint8_t atavia_chip_readb(const struct flashctx *flash, const chipaddr addr) |
| 178 | { |
| 179 | pci_write_long(dev, BROM_ADDR, (addr & ~3)); |
| 180 | pci_write_byte(dev, BROM_ACCESS, BROM_TRIGGER | ENABLE_BYTE(addr)); |
| 181 | |
| 182 | if (!atavia_ready(dev)) { |
| 183 | msg_perr("not ready after read\n"); |
| 184 | } |
| 185 | |
| 186 | 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] | 187 | 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] | 188 | return val; |
| 189 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 190 | |
| 191 | const struct programmer_entry programmer_atavia = { |
| 192 | .name = "atavia", |
| 193 | .type = PCI, |
| 194 | .devs.dev = ata_via, |
| 195 | .init = atavia_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 196 | }; |