Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 Linux Networx |
| 5 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
| 6 | * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com> |
| 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; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 21 | |
| 22 | #include <console/console.h> |
| 23 | #include <device/device.h> |
| 24 | #include <device/pci.h> |
| 25 | #include <device/pci_ids.h> |
| 26 | #include <device/cardbus.h> |
| 27 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 28 | /* |
| 29 | * I don't think this code is quite correct but it is close. |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 30 | * Anyone with a cardbus bridge and a little time should be able |
| 31 | * to make it usable quickly. -- Eric Biederman 24 March 2005 |
| 32 | */ |
| 33 | |
| 34 | /* |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 35 | * IO should be max 256 bytes. However, since we may have a P2P bridge below |
| 36 | * a cardbus bridge, we need 4K. |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 37 | */ |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 38 | #define CARDBUS_IO_SIZE 4096 |
| 39 | #define CARDBUS_MEM_SIZE (32 * 1024 * 1024) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 40 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 41 | static void cardbus_record_bridge_resource(device_t dev, resource_t moving, |
| 42 | resource_t min_size, unsigned int index, unsigned long type) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 43 | { |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 44 | struct resource *resource; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 45 | |
| 46 | /* Initialize the constraints on the current bus. */ |
Myles Watson | 03adcfd | 2010-06-07 16:51:11 +0000 | [diff] [blame] | 47 | resource = NULL; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 48 | if (moving) { |
| 49 | unsigned long gran; |
| 50 | resource_t step; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 51 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 52 | resource = new_resource(dev, index); |
| 53 | resource->size = 0; |
| 54 | gran = 0; |
| 55 | step = 1; |
Myles Watson | 03adcfd | 2010-06-07 16:51:11 +0000 | [diff] [blame] | 56 | while ((moving & step) == 0) { |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 57 | gran += 1; |
| 58 | step <<= 1; |
| 59 | } |
| 60 | resource->gran = gran; |
| 61 | resource->align = gran; |
| 62 | resource->limit = moving | (step - 1); |
| 63 | resource->flags = type; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 64 | |
| 65 | /* |
| 66 | * Don't let the minimum size exceed what we |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 67 | * can put in the resource. |
| 68 | */ |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 69 | if ((min_size - 1) > resource->limit) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 70 | min_size = resource->limit + 1; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 71 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 72 | resource->size = min_size; |
| 73 | } |
| 74 | return; |
| 75 | } |
| 76 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 77 | static void cardbus_size_bridge_resource(device_t dev, unsigned int index) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 78 | { |
| 79 | struct resource *resource; |
| 80 | resource_t min_size; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 81 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 82 | resource = find_resource(dev, index); |
| 83 | if (resource) { |
| 84 | min_size = resource->size; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 85 | /* |
| 86 | * Always allocate at least the miniumum size to a |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 87 | * cardbus bridge in case a new card is plugged in. |
| 88 | */ |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 89 | if (resource->size < min_size) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 90 | resource->size = min_size; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | void cardbus_read_resources(device_t dev) |
| 95 | { |
| 96 | resource_t moving_base, moving_limit, moving; |
| 97 | unsigned long type; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 98 | u16 ctl; |
Ronald G. Minnich | 43225bc | 2005-11-22 00:07:02 +0000 | [diff] [blame] | 99 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 100 | /* See if needs a card control registers base address. */ |
Ronald G. Minnich | 43225bc | 2005-11-22 00:07:02 +0000 | [diff] [blame] | 101 | |
| 102 | pci_get_resource(dev, PCI_BASE_ADDRESS_0); |
| 103 | |
| 104 | compact_resources(dev); |
| 105 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 106 | /* See which bridge I/O resources are implemented. */ |
| 107 | moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 108 | moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0); |
| 109 | moving = moving_base & moving_limit; |
| 110 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 111 | /* Initialize the I/O space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 112 | cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 113 | PCI_CB_IO_BASE_0, IORESOURCE_IO); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 114 | cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0); |
| 115 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 116 | /* See which bridge I/O resources are implemented. */ |
| 117 | moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 118 | moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1); |
| 119 | moving = moving_base & moving_limit; |
| 120 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 121 | /* Initialize the I/O space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 122 | cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 123 | PCI_CB_IO_BASE_1, IORESOURCE_IO); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 124 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 125 | /* If I can, enable prefetch for mem0. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 126 | ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
| 127 | ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 128 | ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; |
| 129 | ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 130 | pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl); |
| 131 | ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
| 132 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 133 | /* See which bridge memory resources are implemented. */ |
| 134 | moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 135 | moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0); |
| 136 | moving = moving_base & moving_limit; |
| 137 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 138 | /* Initialize the memory space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 139 | type = IORESOURCE_MEM; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 140 | if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 141 | type |= IORESOURCE_PREFETCH; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 142 | cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 143 | PCI_CB_MEMORY_BASE_0, type); |
| 144 | if (type & IORESOURCE_PREFETCH) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 145 | cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 146 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 147 | /* See which bridge memory resources are implemented. */ |
| 148 | moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 149 | moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1); |
| 150 | moving = moving_base & moving_limit; |
| 151 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 152 | /* Initialize the memory space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 153 | cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 154 | PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 155 | cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1); |
| 156 | |
| 157 | compact_resources(dev); |
| 158 | } |
| 159 | |
| 160 | void cardbus_enable_resources(device_t dev) |
| 161 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 162 | u16 ctrl; |
| 163 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 164 | ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 165 | ctrl |= (dev->link_list->bridge_ctrl & ( |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 166 | PCI_BRIDGE_CTL_PARITY | |
| 167 | PCI_BRIDGE_CTL_SERR | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 168 | PCI_BRIDGE_CTL_NO_ISA | |
| 169 | PCI_BRIDGE_CTL_VGA | |
| 170 | PCI_BRIDGE_CTL_MASTER_ABORT | |
| 171 | PCI_BRIDGE_CTL_BUS_RESET)); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 172 | /* Error check */ |
| 173 | ctrl |= (PCI_CB_BRIDGE_CTL_PARITY + PCI_CB_BRIDGE_CTL_SERR); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 174 | printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 175 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 176 | |
| 177 | pci_dev_enable_resources(dev); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 180 | struct device_operations default_cardbus_ops_bus = { |
| 181 | .read_resources = cardbus_read_resources, |
| 182 | .set_resources = pci_dev_set_resources, |
| 183 | .enable_resources = cardbus_enable_resources, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame^] | 184 | .init = 0, |
| 185 | .scan_bus = pci_scan_bridge, |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 186 | .enable = 0, |
| 187 | .reset_bus = pci_bus_reset, |
| 188 | }; |