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> |
| 28 | #include <pci/pci.h> |
| 29 | #include "flash.h" |
| 30 | |
| 31 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 32 | int io_fd; |
| 33 | #endif |
| 34 | |
| 35 | struct pci_access *pacc; /* For board and chipset_enable */ |
| 36 | |
| 37 | struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) |
| 38 | { |
| 39 | struct pci_dev *temp; |
| 40 | struct pci_filter filter; |
| 41 | |
| 42 | pci_filter_init(NULL, &filter); |
| 43 | filter.vendor = vendor; |
| 44 | filter.device = device; |
| 45 | |
| 46 | for (temp = pacc->devices; temp; temp = temp->next) |
| 47 | if (pci_filter_match(&filter, temp)) |
| 48 | return temp; |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device, |
| 54 | uint16_t card_vendor, uint16_t card_device) |
| 55 | { |
| 56 | struct pci_dev *temp; |
| 57 | struct pci_filter filter; |
| 58 | |
| 59 | pci_filter_init(NULL, &filter); |
| 60 | filter.vendor = vendor; |
| 61 | filter.device = device; |
| 62 | |
| 63 | for (temp = pacc->devices; temp; temp = temp->next) |
| 64 | if (pci_filter_match(&filter, temp)) { |
| 65 | if ((card_vendor == |
| 66 | pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID)) |
| 67 | && (card_device == |
| 68 | pci_read_word(temp, PCI_SUBSYSTEM_ID))) |
| 69 | return temp; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | int internal_init(void) |
| 76 | { |
| 77 | int ret = 0; |
| 78 | |
| 79 | /* First get full io access */ |
| 80 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 81 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
| 82 | #elif defined(__FreeBSD__) || defined (__DragonFly__) |
| 83 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
| 84 | #else |
| 85 | if (iopl(3) != 0) { |
| 86 | #endif |
| 87 | fprintf(stderr, "ERROR: Could not get IO privileges (%s).\nYou need to be root.\n", strerror(errno)); |
| 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | /* Initialize PCI access for flash enables */ |
| 92 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 93 | /* Set all options you want -- here we stick with the defaults */ |
| 94 | pci_init(pacc); /* Initialize the PCI library */ |
| 95 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 96 | |
| 97 | /* We look at the lbtable first to see if we need a |
| 98 | * mainboard specific flash enable sequence. |
| 99 | */ |
| 100 | coreboot_init(); |
| 101 | |
| 102 | /* try to enable it. Failure IS an option, since not all motherboards |
| 103 | * really need this to be done, etc., etc. |
| 104 | */ |
| 105 | ret = chipset_flash_enable(); |
| 106 | if (ret == -2) { |
| 107 | printf("WARNING: No chipset found. Flash detection " |
| 108 | "will most likely fail.\n"); |
| 109 | } |
| 110 | |
| 111 | board_flash_enable(lb_vendor, lb_part); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | int internal_shutdown(void) |
| 117 | { |
| 118 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 119 | close(io_fd); |
| 120 | #endif |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void internal_chip_writeb(uint8_t val, volatile void *addr) |
| 126 | { |
| 127 | *(volatile uint8_t *) addr = val; |
| 128 | } |
| 129 | |
| 130 | void internal_chip_writew(uint16_t val, volatile void *addr) |
| 131 | { |
| 132 | *(volatile uint16_t *) addr = val; |
| 133 | } |
| 134 | |
| 135 | void internal_chip_writel(uint32_t val, volatile void *addr) |
| 136 | { |
| 137 | *(volatile uint32_t *) addr = val; |
| 138 | } |
| 139 | |
| 140 | uint8_t internal_chip_readb(const volatile void *addr) |
| 141 | { |
| 142 | return *(volatile uint8_t *) addr; |
| 143 | } |
| 144 | |
| 145 | uint16_t internal_chip_readw(const volatile void *addr) |
| 146 | { |
| 147 | return *(volatile uint16_t *) addr; |
| 148 | } |
| 149 | |
| 150 | uint32_t internal_chip_readl(const volatile void *addr) |
| 151 | { |
| 152 | return *(volatile uint32_t *) addr; |
| 153 | } |
| 154 | |