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