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