Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
Simon Glass | 2b81e8a | 2015-11-29 13:17:46 -0700 | [diff] [blame] | 3 | * PCI autoconfiguration library (legacy version, do not change) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 4 | * |
| 5 | * Author: Matt Porter <mporter@mvista.com> |
| 6 | * |
| 7 | * Copyright 2000 MontaVista Software Inc. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 4a2708a | 2015-01-14 21:37:04 -0700 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #include <pci.h> |
| 14 | |
Simon Glass | 2b81e8a | 2015-11-29 13:17:46 -0700 | [diff] [blame] | 15 | /* |
| 16 | * Do not change this file. Instead, convert your board to use CONFIG_DM_PCI |
| 17 | * and change pci_auto.c. |
| 18 | */ |
| 19 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 20 | /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */ |
| 21 | #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE |
| 22 | #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8 |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 23 | #endif |
| 24 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 25 | /* |
| 26 | * |
| 27 | */ |
| 28 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | void pciauto_setup_device(struct pci_controller *hose, |
| 30 | pci_dev_t dev, int bars_num, |
| 31 | struct pci_region *mem, |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 32 | struct pci_region *prefetch, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | struct pci_region *io) |
| 34 | { |
Kumar Gala | cf5787f | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 35 | u32 bar_response; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 36 | pci_size_t bar_size; |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 37 | u16 cmdstat = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | int bar, bar_nr = 0; |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 39 | #ifndef CONFIG_PCI_ENUM_ONLY |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 40 | u8 header_type; |
| 41 | int rom_addr; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 42 | pci_addr_t bar_value; |
| 43 | struct pci_region *bar_res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | int found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 45 | #endif |
Bin Meng | cdf9f08 | 2015-10-01 00:35:59 -0700 | [diff] [blame] | 46 | u16 class; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 47 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 48 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER; |
| 50 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 51 | for (bar = PCI_BASE_ADDRESS_0; |
| 52 | bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | /* Tickle the BAR and get the response */ |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 54 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 56 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
| 58 | |
| 59 | /* If BAR is not implemented go to the next BAR */ |
| 60 | if (!bar_response) |
| 61 | continue; |
| 62 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 63 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | found_mem64 = 0; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 65 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 66 | |
| 67 | /* Check the BAR type and set our address mask */ |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 68 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
Jin Zhengxiong-R64188 | bd22c2b | 2006-06-27 18:12:02 +0800 | [diff] [blame] | 69 | bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK)) |
| 70 | & 0xffff) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 71 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | bar_res = io; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 73 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 75 | debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", |
| 76 | bar_nr, (unsigned long long)bar_size); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 77 | } else { |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 78 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 79 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 80 | u32 bar_response_upper; |
| 81 | u64 bar64; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 82 | |
| 83 | #ifndef CONFIG_PCI_ENUM_ONLY |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 84 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 85 | 0xffffffff); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 86 | #endif |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 87 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 88 | &bar_response_upper); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 90 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
| 91 | |
| 92 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 93 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 94 | found_mem64 = 1; |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 95 | #endif |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 96 | } else { |
| 97 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 98 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 99 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 100 | if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH)) |
| 101 | bar_res = prefetch; |
| 102 | else |
| 103 | bar_res = mem; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | |
Simon Glass | 4bad2e7 | 2015-07-27 15:47:18 -0600 | [diff] [blame] | 105 | debug("PCI Autoconfig: BAR %d, %s, size=0x%llx, ", |
| 106 | bar_nr, bar_res == prefetch ? "Prf" : "Mem", |
| 107 | (unsigned long long)bar_size); |
Phil Sutter | 1113146 | 2015-12-25 14:41:17 +0100 | [diff] [blame] | 108 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 111 | #ifndef CONFIG_PCI_ENUM_ONLY |
Tuomas Tynkkynen | d71975a | 2018-05-14 19:38:13 +0300 | [diff] [blame] | 112 | if (pciauto_region_allocate(bar_res, bar_size, |
| 113 | &bar_value, found_mem64) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | /* Write it out and update our limit */ |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 115 | pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 117 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | bar += 4; |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 119 | #ifdef CONFIG_SYS_PCI_64BIT |
| 120 | pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32)); |
| 121 | #else |
| 122 | /* |
| 123 | * If we are a 64-bit decoder then increment to the |
| 124 | * upper 32 bits of the bar and force it to locate |
| 125 | * in the lower 4GB of memory. |
| 126 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | 30e76d5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 128 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | } |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 132 | #endif |
| 133 | cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ? |
| 134 | PCI_COMMAND_IO : PCI_COMMAND_MEMORY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 136 | debug("\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | |
| 138 | bar_nr++; |
| 139 | } |
| 140 | |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 141 | #ifndef CONFIG_PCI_ENUM_ONLY |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 142 | /* Configure the expansion ROM address */ |
| 143 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
Bin Meng | 7445435 | 2015-10-07 02:13:18 -0700 | [diff] [blame] | 144 | header_type &= 0x7f; |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 145 | if (header_type != PCI_HEADER_TYPE_CARDBUS) { |
| 146 | rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ? |
| 147 | PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1; |
| 148 | pci_hose_write_config_dword(hose, dev, rom_addr, 0xfffffffe); |
| 149 | pci_hose_read_config_dword(hose, dev, rom_addr, &bar_response); |
| 150 | if (bar_response) { |
| 151 | bar_size = -(bar_response & ~1); |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 152 | debug("PCI Autoconfig: ROM, size=%#x, ", |
| 153 | (unsigned int)bar_size); |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 154 | if (pciauto_region_allocate(mem, bar_size, |
Tuomas Tynkkynen | d71975a | 2018-05-14 19:38:13 +0300 | [diff] [blame] | 155 | &bar_value, false) == 0) { |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 156 | pci_hose_write_config_dword(hose, dev, rom_addr, |
| 157 | bar_value); |
| 158 | } |
| 159 | cmdstat |= PCI_COMMAND_MEMORY; |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 160 | debug("\n"); |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 161 | } |
| 162 | } |
Simon Glass | 53292ad | 2015-07-31 09:31:34 -0600 | [diff] [blame] | 163 | #endif |
Bin Meng | 6c89663 | 2015-07-08 13:06:40 +0800 | [diff] [blame] | 164 | |
Bin Meng | cdf9f08 | 2015-10-01 00:35:59 -0700 | [diff] [blame] | 165 | /* PCI_COMMAND_IO must be set for VGA device */ |
| 166 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 167 | if (class == PCI_CLASS_DISPLAY_VGA) |
| 168 | cmdstat |= PCI_COMMAND_IO; |
| 169 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 170 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat); |
Gary Jennejohn | 81b73de | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 171 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | CONFIG_SYS_PCI_CACHE_LINE_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 174 | } |
| 175 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 176 | void pciauto_prescan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 177 | pci_dev_t dev, int sub_bus) |
| 178 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 179 | struct pci_region *pci_mem; |
| 180 | struct pci_region *pci_prefetch; |
| 181 | struct pci_region *pci_io; |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 182 | u16 cmdstat, prefechable_64; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 183 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 184 | pci_mem = hose->pci_mem; |
| 185 | pci_prefetch = hose->pci_prefetch; |
| 186 | pci_io = hose->pci_io; |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 187 | |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 188 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 189 | pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 190 | &prefechable_64); |
| 191 | prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | |
| 193 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 194 | pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, |
| 195 | PCI_BUS(dev) - hose->first_busno); |
| 196 | pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, |
| 197 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff); |
| 199 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 200 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 201 | /* Round memory allocator to 1MB boundary */ |
| 202 | pciauto_region_align(pci_mem, 0x100000); |
| 203 | |
| 204 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 205 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE, |
| 206 | (pci_mem->bus_lower & 0xfff00000) >> 16); |
| 207 | |
| 208 | cmdstat |= PCI_COMMAND_MEMORY; |
| 209 | } |
| 210 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 211 | if (pci_prefetch) { |
| 212 | /* Round memory allocator to 1MB boundary */ |
| 213 | pciauto_region_align(pci_prefetch, 0x100000); |
| 214 | |
| 215 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 216 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 217 | (pci_prefetch->bus_lower & 0xfff00000) >> 16); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 218 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) |
| 219 | #ifdef CONFIG_SYS_PCI_64BIT |
| 220 | pci_hose_write_config_dword(hose, dev, |
| 221 | PCI_PREF_BASE_UPPER32, |
| 222 | pci_prefetch->bus_lower >> 32); |
| 223 | #else |
| 224 | pci_hose_write_config_dword(hose, dev, |
| 225 | PCI_PREF_BASE_UPPER32, |
| 226 | 0x0); |
| 227 | #endif |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 228 | |
| 229 | cmdstat |= PCI_COMMAND_MEMORY; |
| 230 | } else { |
| 231 | /* We don't support prefetchable memory for now, so disable */ |
| 232 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000); |
Matthew McClintock | a4e1155 | 2006-06-28 10:44:23 -0500 | [diff] [blame] | 233 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 234 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) { |
| 235 | pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0); |
| 236 | pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0); |
| 237 | } |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 238 | } |
| 239 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 240 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | /* Round I/O allocator to 4KB boundary */ |
| 242 | pciauto_region_align(pci_io, 0x1000); |
| 243 | |
| 244 | pci_hose_write_config_byte(hose, dev, PCI_IO_BASE, |
| 245 | (pci_io->bus_lower & 0x0000f000) >> 8); |
| 246 | pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16, |
| 247 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 248 | |
| 249 | cmdstat |= PCI_COMMAND_IO; |
| 250 | } |
| 251 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | /* Enable memory and I/O accesses, enable bus master */ |
Andrew Sharp | af778c6 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 253 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, |
| 254 | cmdstat | PCI_COMMAND_MASTER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Ed Swarthout | ba5feb1 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 257 | void pciauto_postscan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 258 | pci_dev_t dev, int sub_bus) |
| 259 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 260 | struct pci_region *pci_mem; |
| 261 | struct pci_region *pci_prefetch; |
| 262 | struct pci_region *pci_io; |
| 263 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 264 | pci_mem = hose->pci_mem; |
| 265 | pci_prefetch = hose->pci_prefetch; |
| 266 | pci_io = hose->pci_io; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | |
| 268 | /* Configure bus number registers */ |
Ed Swarthout | e8b85f3 | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 269 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, |
| 270 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 272 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 273 | /* Round memory allocator to 1MB boundary */ |
| 274 | pciauto_region_align(pci_mem, 0x100000); |
| 275 | |
| 276 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 277 | (pci_mem->bus_lower - 1) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 280 | if (pci_prefetch) { |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 281 | u16 prefechable_64; |
| 282 | |
| 283 | pci_hose_read_config_word(hose, dev, |
| 284 | PCI_PREF_MEMORY_LIMIT, |
| 285 | &prefechable_64); |
| 286 | prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK; |
| 287 | |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 288 | /* Round memory allocator to 1MB boundary */ |
| 289 | pciauto_region_align(pci_prefetch, 0x100000); |
| 290 | |
| 291 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 292 | (pci_prefetch->bus_lower - 1) >> 16); |
David Feng | 6eefd52 | 2015-02-02 16:53:13 +0800 | [diff] [blame] | 293 | if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) |
| 294 | #ifdef CONFIG_SYS_PCI_64BIT |
| 295 | pci_hose_write_config_dword(hose, dev, |
| 296 | PCI_PREF_LIMIT_UPPER32, |
| 297 | (pci_prefetch->bus_lower - 1) >> 32); |
| 298 | #else |
| 299 | pci_hose_write_config_dword(hose, dev, |
| 300 | PCI_PREF_LIMIT_UPPER32, |
| 301 | 0x0); |
| 302 | #endif |
Kumar Gala | a179012 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 303 | } |
| 304 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 305 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 306 | /* Round I/O allocator to 4KB boundary */ |
| 307 | pciauto_region_align(pci_io, 0x1000); |
| 308 | |
| 309 | pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 310 | ((pci_io->bus_lower - 1) & 0x0000f000) >> 8); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16, |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 312 | ((pci_io->bus_lower - 1) & 0xffff0000) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 316 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 317 | /* |
| 318 | * HJF: Changed this to return int. I think this is required |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 319 | * to get the correct result when scanning bridges |
| 320 | */ |
| 321 | int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | { |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 323 | struct pci_region *pci_mem; |
| 324 | struct pci_region *pci_prefetch; |
| 325 | struct pci_region *pci_io; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 326 | unsigned int sub_bus = PCI_BUS(dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 327 | unsigned short class; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 328 | int n; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 329 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 330 | pci_mem = hose->pci_mem; |
| 331 | pci_prefetch = hose->pci_prefetch; |
| 332 | pci_io = hose->pci_io; |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 333 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 335 | |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 336 | switch (class) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | case PCI_CLASS_BRIDGE_PCI: |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 338 | debug("PCI Autoconfig: Found P2P bridge, device %d\n", |
| 339 | PCI_DEV(dev)); |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 340 | |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 341 | pciauto_setup_device(hose, dev, 2, pci_mem, |
| 342 | pci_prefetch, pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 344 | /* Passing in current_busno allows for sibling P2P bridges */ |
Simon Glass | ff3e077 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 345 | hose->current_busno++; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 346 | pciauto_prescan_setup_bridge(hose, dev, hose->current_busno); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 347 | /* |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 348 | * need to figure out if this is a subordinate bridge on the bus |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 349 | * to be able to properly set the pri/sec/sub bridge registers. |
| 350 | */ |
| 351 | n = pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 352 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 353 | /* figure out the deepest we've gone for this leg */ |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 354 | sub_bus = max((unsigned int)n, sub_bus); |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 355 | pciauto_postscan_setup_bridge(hose, dev, sub_bus); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 356 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 357 | sub_bus = hose->current_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 358 | break; |
| 359 | |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 360 | case PCI_CLASS_BRIDGE_CARDBUS: |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 361 | /* |
| 362 | * just do a minimal setup of the bridge, |
| 363 | * let the OS take care of the rest |
| 364 | */ |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 365 | pciauto_setup_device(hose, dev, 0, pci_mem, |
| 366 | pci_prefetch, pci_io); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 367 | |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 368 | debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n", |
| 369 | PCI_DEV(dev)); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 370 | |
| 371 | hose->current_busno++; |
| 372 | break; |
| 373 | |
TsiChung Liew | f33fca2 | 2008-03-30 01:19:06 -0500 | [diff] [blame] | 374 | #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE) |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 375 | case PCI_CLASS_BRIDGE_OTHER: |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 376 | debug("PCI Autoconfig: Skipping bridge device %d\n", |
| 377 | PCI_DEV(dev)); |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 378 | break; |
| 379 | #endif |
Mario Six | 904c47f | 2019-01-21 09:17:38 +0100 | [diff] [blame] | 380 | #if defined(CONFIG_ARCH_MPC834X) && !defined(CONFIG_TARGET_VME8349) && \ |
| 381 | !defined(CONFIG_TARGET_CADDY2) |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 382 | case PCI_CLASS_BRIDGE_OTHER: |
| 383 | /* |
| 384 | * The host/PCI bridge 1 seems broken in 8349 - it presents |
| 385 | * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_ |
| 386 | * device claiming resources io/mem/irq.. we only allow for |
| 387 | * the PIMMR window to be allocated (BAR0 - 1MB size) |
| 388 | */ |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 389 | debug("PCI Autoconfig: Broken bridge found, only minimal config\n"); |
Andrew Sharp | cb2bf93 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 390 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 391 | hose->pci_prefetch, hose->pci_io); |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 392 | break; |
| 393 | #endif |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 394 | |
| 395 | case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */ |
Simon Glass | da4b159 | 2015-07-31 09:31:33 -0600 | [diff] [blame] | 396 | debug("PCI AutoConfig: Found PowerPC device\n"); |
Andrew Sharp | 69fd2d3 | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 397 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 398 | default: |
Bin Meng | d11d9ef | 2015-07-19 00:20:06 +0800 | [diff] [blame] | 399 | pciauto_setup_device(hose, dev, 6, pci_mem, |
| 400 | pci_prefetch, pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 401 | break; |
| 402 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 403 | |
| 404 | return sub_bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | } |