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. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 16 | */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 17 | |
| 18 | #include <console/console.h> |
| 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame^] | 21 | #include <device/pci_ops.h> |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 22 | #include <device/pci_ids.h> |
| 23 | #include <device/cardbus.h> |
| 24 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 25 | /* |
| 26 | * 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] | 27 | * Anyone with a cardbus bridge and a little time should be able |
| 28 | * to make it usable quickly. -- Eric Biederman 24 March 2005 |
| 29 | */ |
| 30 | |
| 31 | /* |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 32 | * IO should be max 256 bytes. However, since we may have a P2P bridge below |
| 33 | * a cardbus bridge, we need 4K. |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 34 | */ |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 35 | #define CARDBUS_IO_SIZE 4096 |
| 36 | #define CARDBUS_MEM_SIZE (32 * 1024 * 1024) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 37 | |
Elyes HAOUAS | e18cbea | 2018-05-02 21:20:59 +0200 | [diff] [blame] | 38 | static void cardbus_record_bridge_resource(struct device *dev, resource_t moving, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 39 | resource_t min_size, unsigned int index, unsigned long type) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 40 | { |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 41 | struct resource *resource; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 42 | unsigned long gran; |
| 43 | resource_t step; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 44 | |
| 45 | /* Initialize the constraints on the current bus. */ |
Myles Watson | 03adcfd | 2010-06-07 16:51:11 +0000 | [diff] [blame] | 46 | resource = NULL; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 47 | if (!moving) |
| 48 | return; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 49 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 50 | resource = new_resource(dev, index); |
| 51 | resource->size = 0; |
| 52 | gran = 0; |
| 53 | step = 1; |
| 54 | while ((moving & step) == 0) { |
| 55 | gran += 1; |
| 56 | step <<= 1; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 57 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 58 | resource->gran = gran; |
| 59 | resource->align = gran; |
| 60 | resource->limit = moving | (step - 1); |
| 61 | resource->flags = type; |
| 62 | |
| 63 | /* Don't let the minimum size exceed what we can put in the resource. */ |
| 64 | if ((min_size - 1) > resource->limit) |
| 65 | min_size = resource->limit + 1; |
| 66 | |
| 67 | resource->size = min_size; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Elyes HAOUAS | e18cbea | 2018-05-02 21:20:59 +0200 | [diff] [blame] | 70 | static void cardbus_size_bridge_resource(struct device *dev, unsigned int index) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 71 | { |
| 72 | struct resource *resource; |
| 73 | resource_t min_size; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 74 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 75 | resource = find_resource(dev, index); |
| 76 | if (resource) { |
| 77 | min_size = resource->size; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 78 | /* |
Elyes HAOUAS | 394ec02 | 2018-08-07 12:23:43 +0200 | [diff] [blame] | 79 | * Always allocate at least the minimum size to a |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 80 | * cardbus bridge in case a new card is plugged in. |
| 81 | */ |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 82 | if (resource->size < min_size) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 83 | resource->size = min_size; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Elyes HAOUAS | e18cbea | 2018-05-02 21:20:59 +0200 | [diff] [blame] | 87 | void cardbus_read_resources(struct device *dev) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 88 | { |
| 89 | resource_t moving_base, moving_limit, moving; |
| 90 | unsigned long type; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 91 | u16 ctl; |
Ronald G. Minnich | 43225bc | 2005-11-22 00:07:02 +0000 | [diff] [blame] | 92 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 93 | /* See if needs a card control registers base address. */ |
Ronald G. Minnich | 43225bc | 2005-11-22 00:07:02 +0000 | [diff] [blame] | 94 | |
| 95 | pci_get_resource(dev, PCI_BASE_ADDRESS_0); |
| 96 | |
| 97 | compact_resources(dev); |
| 98 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 99 | /* See which bridge I/O resources are implemented. */ |
| 100 | moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 101 | moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_0); |
| 102 | moving = moving_base & moving_limit; |
| 103 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 104 | /* Initialize the I/O space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 105 | cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 106 | PCI_CB_IO_BASE_0, IORESOURCE_IO); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 107 | cardbus_size_bridge_resource(dev, PCI_CB_IO_BASE_0); |
| 108 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 109 | /* See which bridge I/O resources are implemented. */ |
| 110 | moving_base = pci_moving_config32(dev, PCI_CB_IO_BASE_1); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 111 | moving_limit = pci_moving_config32(dev, PCI_CB_IO_LIMIT_1); |
| 112 | moving = moving_base & moving_limit; |
| 113 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 114 | /* Initialize the I/O space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 115 | cardbus_record_bridge_resource(dev, moving, CARDBUS_IO_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 116 | PCI_CB_IO_BASE_1, IORESOURCE_IO); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 117 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 118 | /* If I can, enable prefetch for mem0. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 119 | ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
| 120 | ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 121 | ctl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; |
| 122 | ctl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 123 | pci_write_config16(dev, PCI_CB_BRIDGE_CONTROL, ctl); |
| 124 | ctl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
| 125 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 126 | /* See which bridge memory resources are implemented. */ |
| 127 | moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 128 | moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_0); |
| 129 | moving = moving_base & moving_limit; |
| 130 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 131 | /* Initialize the memory space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 132 | type = IORESOURCE_MEM; |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 133 | if (ctl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 134 | type |= IORESOURCE_PREFETCH; |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 135 | cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 136 | PCI_CB_MEMORY_BASE_0, type); |
| 137 | if (type & IORESOURCE_PREFETCH) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 138 | cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_0); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 139 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 140 | /* See which bridge memory resources are implemented. */ |
| 141 | moving_base = pci_moving_config32(dev, PCI_CB_MEMORY_BASE_1); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 142 | moving_limit = pci_moving_config32(dev, PCI_CB_MEMORY_LIMIT_1); |
| 143 | moving = moving_base & moving_limit; |
| 144 | |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 145 | /* Initialize the memory space constraints on the current bus. */ |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 146 | cardbus_record_bridge_resource(dev, moving, CARDBUS_MEM_SIZE, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 147 | PCI_CB_MEMORY_BASE_1, IORESOURCE_MEM); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 148 | cardbus_size_bridge_resource(dev, PCI_CB_MEMORY_BASE_1); |
| 149 | |
| 150 | compact_resources(dev); |
| 151 | } |
| 152 | |
Elyes HAOUAS | e18cbea | 2018-05-02 21:20:59 +0200 | [diff] [blame] | 153 | void cardbus_enable_resources(struct device *dev) |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 154 | { |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 155 | u16 ctrl; |
| 156 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 157 | ctrl = pci_read_config16(dev, PCI_CB_BRIDGE_CONTROL); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 158 | ctrl |= (dev->link_list->bridge_ctrl & ( |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 159 | PCI_BRIDGE_CTL_PARITY | |
| 160 | PCI_BRIDGE_CTL_SERR | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 161 | PCI_BRIDGE_CTL_NO_ISA | |
| 162 | PCI_BRIDGE_CTL_VGA | |
| 163 | PCI_BRIDGE_CTL_MASTER_ABORT | |
| 164 | PCI_BRIDGE_CTL_BUS_RESET)); |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 165 | /* Error check */ |
| 166 | ctrl |= (PCI_CB_BRIDGE_CTL_PARITY + PCI_CB_BRIDGE_CTL_SERR); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 167 | printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 168 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 169 | |
| 170 | pci_dev_enable_resources(dev); |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 173 | struct device_operations default_cardbus_ops_bus = { |
| 174 | .read_resources = cardbus_read_resources, |
| 175 | .set_resources = pci_dev_set_resources, |
| 176 | .enable_resources = cardbus_enable_resources, |
Uwe Hermann | d453dd0 | 2010-10-18 00:00:57 +0000 | [diff] [blame] | 177 | .init = 0, |
| 178 | .scan_bus = pci_scan_bridge, |
Yinghai Lu | 304f24c | 2005-07-08 02:56:47 +0000 | [diff] [blame] | 179 | .enable = 0, |
| 180 | .reset_bus = pci_bus_reset, |
| 181 | }; |