Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Carl-Daniel Hailfinger |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <errno.h> |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 28 | #include "flash.h" |
| 29 | |
| 30 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 31 | int io_fd; |
| 32 | #endif |
| 33 | |
Christian Ruppert | 0cdb031 | 2009-05-14 18:57:26 +0000 | [diff] [blame] | 34 | struct pci_dev *pci_dev_find_filter(struct pci_filter filter) |
| 35 | { |
| 36 | struct pci_dev *temp; |
| 37 | |
| 38 | for (temp = pacc->devices; temp; temp = temp->next) |
| 39 | if (pci_filter_match(&filter, temp)) |
| 40 | return temp; |
| 41 | |
| 42 | return NULL; |
| 43 | } |
| 44 | |
Carl-Daniel Hailfinger | 9f46cfc | 2009-11-15 17:13:29 +0000 | [diff] [blame] | 45 | struct pci_dev *pci_dev_find_vendorclass(uint16_t vendor, uint16_t class) |
| 46 | { |
| 47 | struct pci_dev *temp; |
| 48 | struct pci_filter filter; |
| 49 | uint16_t tmp2; |
| 50 | |
| 51 | pci_filter_init(NULL, &filter); |
| 52 | filter.vendor = vendor; |
| 53 | |
| 54 | for (temp = pacc->devices; temp; temp = temp->next) |
| 55 | if (pci_filter_match(&filter, temp)) { |
| 56 | /* Read PCI class */ |
| 57 | tmp2 = pci_read_word(temp, 0x0a); |
| 58 | if (tmp2 == class) |
| 59 | return temp; |
| 60 | } |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 65 | struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) |
| 66 | { |
| 67 | struct pci_dev *temp; |
| 68 | struct pci_filter filter; |
| 69 | |
| 70 | pci_filter_init(NULL, &filter); |
| 71 | filter.vendor = vendor; |
| 72 | filter.device = device; |
| 73 | |
| 74 | for (temp = pacc->devices; temp; temp = temp->next) |
| 75 | if (pci_filter_match(&filter, temp)) |
| 76 | return temp; |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device, |
| 82 | uint16_t card_vendor, uint16_t card_device) |
| 83 | { |
| 84 | struct pci_dev *temp; |
| 85 | struct pci_filter filter; |
| 86 | |
| 87 | pci_filter_init(NULL, &filter); |
| 88 | filter.vendor = vendor; |
| 89 | filter.device = device; |
| 90 | |
| 91 | for (temp = pacc->devices; temp; temp = temp->next) |
| 92 | if (pci_filter_match(&filter, temp)) { |
| 93 | if ((card_vendor == |
| 94 | pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID)) |
| 95 | && (card_device == |
| 96 | pci_read_word(temp, PCI_SUBSYSTEM_ID))) |
| 97 | return temp; |
| 98 | } |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Uwe Hermann | a086932 | 2009-05-14 20:41:57 +0000 | [diff] [blame] | 103 | void get_io_perms(void) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 104 | { |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 105 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 106 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
| 107 | #elif defined(__FreeBSD__) || defined (__DragonFly__) |
| 108 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
| 109 | #else |
| 110 | if (iopl(3) != 0) { |
| 111 | #endif |
Uwe Hermann | a086932 | 2009-05-14 20:41:57 +0000 | [diff] [blame] | 112 | fprintf(stderr, "ERROR: Could not get I/O privileges (%s).\n" |
| 113 | "You need to be root.\n", strerror(errno)); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 114 | exit(1); |
| 115 | } |
Uwe Hermann | a086932 | 2009-05-14 20:41:57 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Carl-Daniel Hailfinger | db41c59 | 2009-08-09 21:50:24 +0000 | [diff] [blame] | 118 | void release_io_perms(void) |
| 119 | { |
| 120 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 121 | close(io_fd); |
| 122 | #endif |
| 123 | } |
| 124 | |
Uwe Hermann | a086932 | 2009-05-14 20:41:57 +0000 | [diff] [blame] | 125 | int internal_init(void) |
| 126 | { |
| 127 | int ret = 0; |
| 128 | |
Carl-Daniel Hailfinger | 3b7e75a | 2009-05-14 21:41:10 +0000 | [diff] [blame] | 129 | get_io_perms(); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 130 | |
| 131 | /* Initialize PCI access for flash enables */ |
| 132 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 133 | /* Set all options you want -- here we stick with the defaults */ |
| 134 | pci_init(pacc); /* Initialize the PCI library */ |
| 135 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 136 | |
| 137 | /* We look at the lbtable first to see if we need a |
| 138 | * mainboard specific flash enable sequence. |
| 139 | */ |
| 140 | coreboot_init(); |
| 141 | |
| 142 | /* try to enable it. Failure IS an option, since not all motherboards |
| 143 | * really need this to be done, etc., etc. |
| 144 | */ |
| 145 | ret = chipset_flash_enable(); |
| 146 | if (ret == -2) { |
| 147 | printf("WARNING: No chipset found. Flash detection " |
| 148 | "will most likely fail.\n"); |
| 149 | } |
| 150 | |
| 151 | board_flash_enable(lb_vendor, lb_part); |
| 152 | |
Carl-Daniel Hailfinger | 9246ff4 | 2009-09-02 13:43:56 +0000 | [diff] [blame] | 153 | /* Even if chipset init returns an error code, we don't want to abort. |
| 154 | * The error code might have been a warning only. |
| 155 | * Besides that, we don't check the board enable return code either. |
| 156 | */ |
| 157 | return 0; |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int internal_shutdown(void) |
| 161 | { |
Carl-Daniel Hailfinger | db41c59 | 2009-08-09 21:50:24 +0000 | [diff] [blame] | 162 | release_io_perms(); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 167 | void internal_chip_writeb(uint8_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 168 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 169 | mmio_writeb(val, (void *) addr); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 172 | void internal_chip_writew(uint16_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 173 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 174 | mmio_writew(val, (void *) addr); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 177 | void internal_chip_writel(uint32_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 178 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 179 | mmio_writel(val, (void *) addr); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 182 | uint8_t internal_chip_readb(const chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 183 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 184 | return mmio_readb((void *) addr); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 187 | uint16_t internal_chip_readw(const chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 188 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 189 | return mmio_readw((void *) addr); |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 192 | uint32_t internal_chip_readl(const chipaddr addr) |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 193 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 194 | return mmio_readl((void *) addr); |
| 195 | } |
| 196 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 197 | void internal_chip_readn(uint8_t *buf, const chipaddr addr, size_t len) |
| 198 | { |
| 199 | memcpy(buf, (void *)addr, len); |
| 200 | return; |
| 201 | } |
| 202 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 203 | void mmio_writeb(uint8_t val, void *addr) |
| 204 | { |
| 205 | *(volatile uint8_t *) addr = val; |
| 206 | } |
| 207 | |
| 208 | void mmio_writew(uint16_t val, void *addr) |
| 209 | { |
| 210 | *(volatile uint16_t *) addr = val; |
| 211 | } |
| 212 | |
| 213 | void mmio_writel(uint32_t val, void *addr) |
| 214 | { |
| 215 | *(volatile uint32_t *) addr = val; |
| 216 | } |
| 217 | |
| 218 | uint8_t mmio_readb(void *addr) |
| 219 | { |
| 220 | return *(volatile uint8_t *) addr; |
| 221 | } |
| 222 | |
| 223 | uint16_t mmio_readw(void *addr) |
| 224 | { |
| 225 | return *(volatile uint16_t *) addr; |
| 226 | } |
| 227 | |
| 228 | uint32_t mmio_readl(void *addr) |
| 229 | { |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 230 | return *(volatile uint32_t *) addr; |
| 231 | } |
| 232 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 233 | void internal_delay(int usecs) |
| 234 | { |
| 235 | /* If the delay is >1 s, use usleep because timing does not need to |
| 236 | * be so precise. |
| 237 | */ |
| 238 | if (usecs > 1000000) { |
| 239 | usleep(usecs); |
| 240 | } else { |
| 241 | myusec_delay(usecs); |
| 242 | } |
| 243 | } |
| 244 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 245 | /* No-op shutdown() for programmers which don't need special handling */ |
| 246 | int noop_shutdown(void) |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 251 | /* Fallback map() for programmers which don't need special handling */ |
| 252 | void *fallback_map(const char *descr, unsigned long phys_addr, size_t len) |
| 253 | { |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 254 | /* FIXME: Should return phys_addr. */ |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 258 | /* No-op/fallback unmap() for programmers which don't need special handling */ |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 259 | void fallback_unmap(void *virt_addr, size_t len) |
| 260 | { |
| 261 | } |
| 262 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 263 | /* No-op chip_writeb() for drivers not supporting addr/data pair accesses */ |
| 264 | uint8_t noop_chip_readb(const chipaddr addr) |
| 265 | { |
| 266 | return 0xff; |
| 267 | } |
| 268 | |
| 269 | /* No-op chip_writeb() for drivers not supporting addr/data pair accesses */ |
| 270 | void noop_chip_writeb(uint8_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 271 | { |
| 272 | } |
| 273 | |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 274 | /* Little-endian fallback for drivers not supporting 16 bit accesses */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 275 | void fallback_chip_writew(uint16_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 276 | { |
| 277 | chip_writeb(val & 0xff, addr); |
| 278 | chip_writeb((val >> 8) & 0xff, addr + 1); |
| 279 | } |
| 280 | |
| 281 | /* Little-endian fallback for drivers not supporting 16 bit accesses */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 282 | uint16_t fallback_chip_readw(const chipaddr addr) |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 283 | { |
| 284 | uint16_t val; |
| 285 | val = chip_readb(addr); |
| 286 | val |= chip_readb(addr + 1) << 8; |
| 287 | return val; |
| 288 | } |
| 289 | |
| 290 | /* Little-endian fallback for drivers not supporting 32 bit accesses */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 291 | void fallback_chip_writel(uint32_t val, chipaddr addr) |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 292 | { |
| 293 | chip_writew(val & 0xffff, addr); |
| 294 | chip_writew((val >> 16) & 0xffff, addr + 2); |
| 295 | } |
| 296 | |
| 297 | /* Little-endian fallback for drivers not supporting 32 bit accesses */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 298 | uint32_t fallback_chip_readl(const chipaddr addr) |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 299 | { |
| 300 | uint32_t val; |
| 301 | val = chip_readw(addr); |
| 302 | val |= chip_readw(addr + 2) << 16; |
| 303 | return val; |
| 304 | } |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 305 | |
| 306 | void fallback_chip_writen(uint8_t *buf, chipaddr addr, size_t len) |
| 307 | { |
| 308 | size_t i; |
| 309 | for (i = 0; i < len; i++) |
| 310 | chip_writeb(buf[i], addr + i); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | void fallback_chip_readn(uint8_t *buf, chipaddr addr, size_t len) |
| 315 | { |
| 316 | size_t i; |
| 317 | for (i = 0; i < len; i++) |
| 318 | buf[i] = chip_readb(addr + i); |
| 319 | return; |
| 320 | } |