1fd29d4eaSIlpo Järvinen // SPDX-License-Identifier: GPL-2.0 2fd29d4eaSIlpo Järvinen /* 3fd29d4eaSIlpo Järvinen * Cardbus bridge setup routines. 4fd29d4eaSIlpo Järvinen */ 5fd29d4eaSIlpo Järvinen 6*5d413c73SIlpo Järvinen #include <linux/bitfield.h> 708b3af83SIlpo Järvinen #include <linux/errno.h> 8fd29d4eaSIlpo Järvinen #include <linux/ioport.h> 9fd29d4eaSIlpo Järvinen #include <linux/pci.h> 1008b3af83SIlpo Järvinen #include <linux/sizes.h> 11*5d413c73SIlpo Järvinen #include <linux/sprintf.h> 12fd29d4eaSIlpo Järvinen #include <linux/types.h> 13fd29d4eaSIlpo Järvinen 14fd29d4eaSIlpo Järvinen #include "pci.h" 15fd29d4eaSIlpo Järvinen 16*5d413c73SIlpo Järvinen #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */ 17*5d413c73SIlpo Järvinen #define CARDBUS_RESERVE_BUSNR 3 18*5d413c73SIlpo Järvinen 1908b3af83SIlpo Järvinen #define DEFAULT_CARDBUS_IO_SIZE SZ_256 2008b3af83SIlpo Järvinen #define DEFAULT_CARDBUS_MEM_SIZE SZ_64M 2108b3af83SIlpo Järvinen /* pci=cbmemsize=nnM,cbiosize=nn can override this */ 2208b3af83SIlpo Järvinen static unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE; 2308b3af83SIlpo Järvinen static unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE; 2408b3af83SIlpo Järvinen 25fd29d4eaSIlpo Järvinen unsigned long pci_cardbus_resource_alignment(struct resource *res) 26fd29d4eaSIlpo Järvinen { 27fd29d4eaSIlpo Järvinen if (res->flags & IORESOURCE_IO) 28fd29d4eaSIlpo Järvinen return pci_cardbus_io_size; 29fd29d4eaSIlpo Järvinen if (res->flags & IORESOURCE_MEM) 30fd29d4eaSIlpo Järvinen return pci_cardbus_mem_size; 31fd29d4eaSIlpo Järvinen return 0; 32fd29d4eaSIlpo Järvinen } 33fd29d4eaSIlpo Järvinen 34fd29d4eaSIlpo Järvinen int pci_bus_size_cardbus_bridge(struct pci_bus *bus, 35fd29d4eaSIlpo Järvinen struct list_head *realloc_head) 36fd29d4eaSIlpo Järvinen { 37fd29d4eaSIlpo Järvinen struct pci_dev *bridge = bus->self; 38fd29d4eaSIlpo Järvinen struct resource *b_res; 39fd29d4eaSIlpo Järvinen resource_size_t b_res_3_size = pci_cardbus_mem_size * 2; 40fd29d4eaSIlpo Järvinen u16 ctrl; 41fd29d4eaSIlpo Järvinen 42fd29d4eaSIlpo Järvinen b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW]; 43fd29d4eaSIlpo Järvinen if (resource_assigned(b_res)) 44fd29d4eaSIlpo Järvinen goto handle_b_res_1; 45fd29d4eaSIlpo Järvinen /* 46fd29d4eaSIlpo Järvinen * Reserve some resources for CardBus. We reserve a fixed amount 47fd29d4eaSIlpo Järvinen * of bus space for CardBus bridges. 48fd29d4eaSIlpo Järvinen */ 49fd29d4eaSIlpo Järvinen resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size); 50fd29d4eaSIlpo Järvinen b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 51fd29d4eaSIlpo Järvinen if (realloc_head) { 52fd29d4eaSIlpo Järvinen b_res->end -= pci_cardbus_io_size; 53fd29d4eaSIlpo Järvinen pci_dev_res_add_to_list(realloc_head, bridge, b_res, 54fd29d4eaSIlpo Järvinen pci_cardbus_io_size, 55fd29d4eaSIlpo Järvinen pci_cardbus_io_size); 56fd29d4eaSIlpo Järvinen } 57fd29d4eaSIlpo Järvinen 58fd29d4eaSIlpo Järvinen handle_b_res_1: 59fd29d4eaSIlpo Järvinen b_res = &bridge->resource[PCI_CB_BRIDGE_IO_1_WINDOW]; 60fd29d4eaSIlpo Järvinen if (resource_assigned(b_res)) 61fd29d4eaSIlpo Järvinen goto handle_b_res_2; 62fd29d4eaSIlpo Järvinen resource_set_range(b_res, pci_cardbus_io_size, pci_cardbus_io_size); 63fd29d4eaSIlpo Järvinen b_res->flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN; 64fd29d4eaSIlpo Järvinen if (realloc_head) { 65fd29d4eaSIlpo Järvinen b_res->end -= pci_cardbus_io_size; 66fd29d4eaSIlpo Järvinen pci_dev_res_add_to_list(realloc_head, bridge, b_res, 67fd29d4eaSIlpo Järvinen pci_cardbus_io_size, 68fd29d4eaSIlpo Järvinen pci_cardbus_io_size); 69fd29d4eaSIlpo Järvinen } 70fd29d4eaSIlpo Järvinen 71fd29d4eaSIlpo Järvinen handle_b_res_2: 72fd29d4eaSIlpo Järvinen /* MEM1 must not be pref MMIO */ 73fd29d4eaSIlpo Järvinen pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 74fd29d4eaSIlpo Järvinen if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) { 75fd29d4eaSIlpo Järvinen ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1; 76fd29d4eaSIlpo Järvinen pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 77fd29d4eaSIlpo Järvinen pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 78fd29d4eaSIlpo Järvinen } 79fd29d4eaSIlpo Järvinen 80fd29d4eaSIlpo Järvinen /* Check whether prefetchable memory is supported by this bridge. */ 81fd29d4eaSIlpo Järvinen pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 82fd29d4eaSIlpo Järvinen if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { 83fd29d4eaSIlpo Järvinen ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; 84fd29d4eaSIlpo Järvinen pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); 85fd29d4eaSIlpo Järvinen pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 86fd29d4eaSIlpo Järvinen } 87fd29d4eaSIlpo Järvinen 88fd29d4eaSIlpo Järvinen b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_0_WINDOW]; 89fd29d4eaSIlpo Järvinen if (resource_assigned(b_res)) 90fd29d4eaSIlpo Järvinen goto handle_b_res_3; 91fd29d4eaSIlpo Järvinen /* 92fd29d4eaSIlpo Järvinen * If we have prefetchable memory support, allocate two regions. 93fd29d4eaSIlpo Järvinen * Otherwise, allocate one region of twice the size. 94fd29d4eaSIlpo Järvinen */ 95fd29d4eaSIlpo Järvinen if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 96fd29d4eaSIlpo Järvinen resource_set_range(b_res, pci_cardbus_mem_size, 97fd29d4eaSIlpo Järvinen pci_cardbus_mem_size); 98fd29d4eaSIlpo Järvinen b_res->flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | 99fd29d4eaSIlpo Järvinen IORESOURCE_STARTALIGN; 100fd29d4eaSIlpo Järvinen if (realloc_head) { 101fd29d4eaSIlpo Järvinen b_res->end -= pci_cardbus_mem_size; 102fd29d4eaSIlpo Järvinen pci_dev_res_add_to_list(realloc_head, bridge, b_res, 103fd29d4eaSIlpo Järvinen pci_cardbus_mem_size, 104fd29d4eaSIlpo Järvinen pci_cardbus_mem_size); 105fd29d4eaSIlpo Järvinen } 106fd29d4eaSIlpo Järvinen 107fd29d4eaSIlpo Järvinen /* Reduce that to half */ 108fd29d4eaSIlpo Järvinen b_res_3_size = pci_cardbus_mem_size; 109fd29d4eaSIlpo Järvinen } 110fd29d4eaSIlpo Järvinen 111fd29d4eaSIlpo Järvinen handle_b_res_3: 112fd29d4eaSIlpo Järvinen b_res = &bridge->resource[PCI_CB_BRIDGE_MEM_1_WINDOW]; 113fd29d4eaSIlpo Järvinen if (resource_assigned(b_res)) 114fd29d4eaSIlpo Järvinen goto handle_done; 115fd29d4eaSIlpo Järvinen resource_set_range(b_res, pci_cardbus_mem_size, b_res_3_size); 116fd29d4eaSIlpo Järvinen b_res->flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN; 117fd29d4eaSIlpo Järvinen if (realloc_head) { 118fd29d4eaSIlpo Järvinen b_res->end -= b_res_3_size; 119fd29d4eaSIlpo Järvinen pci_dev_res_add_to_list(realloc_head, bridge, b_res, 120fd29d4eaSIlpo Järvinen b_res_3_size, pci_cardbus_mem_size); 121fd29d4eaSIlpo Järvinen } 122fd29d4eaSIlpo Järvinen 123fd29d4eaSIlpo Järvinen handle_done: 124fd29d4eaSIlpo Järvinen return 0; 125fd29d4eaSIlpo Järvinen } 126fd29d4eaSIlpo Järvinen 127fd29d4eaSIlpo Järvinen void pci_setup_cardbus_bridge(struct pci_bus *bus) 128fd29d4eaSIlpo Järvinen { 129fd29d4eaSIlpo Järvinen struct pci_dev *bridge = bus->self; 130fd29d4eaSIlpo Järvinen struct resource *res; 131fd29d4eaSIlpo Järvinen struct pci_bus_region region; 132fd29d4eaSIlpo Järvinen 133fd29d4eaSIlpo Järvinen pci_info(bridge, "CardBus bridge to %pR\n", 134fd29d4eaSIlpo Järvinen &bus->busn_res); 135fd29d4eaSIlpo Järvinen 136fd29d4eaSIlpo Järvinen res = bus->resource[0]; 137fd29d4eaSIlpo Järvinen pcibios_resource_to_bus(bridge->bus, ®ion, res); 138fd29d4eaSIlpo Järvinen if (resource_assigned(res) && res->flags & IORESOURCE_IO) { 139fd29d4eaSIlpo Järvinen /* 140fd29d4eaSIlpo Järvinen * The IO resource is allocated a range twice as large as it 141fd29d4eaSIlpo Järvinen * would normally need. This allows us to set both IO regs. 142fd29d4eaSIlpo Järvinen */ 143fd29d4eaSIlpo Järvinen pci_info(bridge, " bridge window %pR\n", res); 144fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 145fd29d4eaSIlpo Järvinen region.start); 146fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, 147fd29d4eaSIlpo Järvinen region.end); 148fd29d4eaSIlpo Järvinen } 149fd29d4eaSIlpo Järvinen 150fd29d4eaSIlpo Järvinen res = bus->resource[1]; 151fd29d4eaSIlpo Järvinen pcibios_resource_to_bus(bridge->bus, ®ion, res); 152fd29d4eaSIlpo Järvinen if (resource_assigned(res) && res->flags & IORESOURCE_IO) { 153fd29d4eaSIlpo Järvinen pci_info(bridge, " bridge window %pR\n", res); 154fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 155fd29d4eaSIlpo Järvinen region.start); 156fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, 157fd29d4eaSIlpo Järvinen region.end); 158fd29d4eaSIlpo Järvinen } 159fd29d4eaSIlpo Järvinen 160fd29d4eaSIlpo Järvinen res = bus->resource[2]; 161fd29d4eaSIlpo Järvinen pcibios_resource_to_bus(bridge->bus, ®ion, res); 162fd29d4eaSIlpo Järvinen if (resource_assigned(res) && res->flags & IORESOURCE_MEM) { 163fd29d4eaSIlpo Järvinen pci_info(bridge, " bridge window %pR\n", res); 164fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 165fd29d4eaSIlpo Järvinen region.start); 166fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, 167fd29d4eaSIlpo Järvinen region.end); 168fd29d4eaSIlpo Järvinen } 169fd29d4eaSIlpo Järvinen 170fd29d4eaSIlpo Järvinen res = bus->resource[3]; 171fd29d4eaSIlpo Järvinen pcibios_resource_to_bus(bridge->bus, ®ion, res); 172fd29d4eaSIlpo Järvinen if (resource_assigned(res) && res->flags & IORESOURCE_MEM) { 173fd29d4eaSIlpo Järvinen pci_info(bridge, " bridge window %pR\n", res); 174fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 175fd29d4eaSIlpo Järvinen region.start); 176fd29d4eaSIlpo Järvinen pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, 177fd29d4eaSIlpo Järvinen region.end); 178fd29d4eaSIlpo Järvinen } 179fd29d4eaSIlpo Järvinen } 180fd29d4eaSIlpo Järvinen EXPORT_SYMBOL(pci_setup_cardbus_bridge); 18108b3af83SIlpo Järvinen 18208b3af83SIlpo Järvinen int pci_setup_cardbus(char *str) 18308b3af83SIlpo Järvinen { 18408b3af83SIlpo Järvinen if (!strncmp(str, "cbiosize=", 9)) { 18508b3af83SIlpo Järvinen pci_cardbus_io_size = memparse(str + 9, &str); 18608b3af83SIlpo Järvinen return 0; 18708b3af83SIlpo Järvinen } else if (!strncmp(str, "cbmemsize=", 10)) { 18808b3af83SIlpo Järvinen pci_cardbus_mem_size = memparse(str + 10, &str); 18908b3af83SIlpo Järvinen return 0; 19008b3af83SIlpo Järvinen } 19108b3af83SIlpo Järvinen 19208b3af83SIlpo Järvinen return -ENOENT; 19308b3af83SIlpo Järvinen } 194*5d413c73SIlpo Järvinen 195*5d413c73SIlpo Järvinen int pci_cardbus_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, 196*5d413c73SIlpo Järvinen u32 buses, int max, 197*5d413c73SIlpo Järvinen unsigned int available_buses, int pass) 198*5d413c73SIlpo Järvinen { 199*5d413c73SIlpo Järvinen struct pci_bus *child; 200*5d413c73SIlpo Järvinen bool fixed_buses; 201*5d413c73SIlpo Järvinen u8 fixed_sec, fixed_sub; 202*5d413c73SIlpo Järvinen int next_busnr; 203*5d413c73SIlpo Järvinen u32 i, j = 0; 204*5d413c73SIlpo Järvinen 205*5d413c73SIlpo Järvinen /* 206*5d413c73SIlpo Järvinen * We need to assign a number to this bus which we always do in the 207*5d413c73SIlpo Järvinen * second pass. 208*5d413c73SIlpo Järvinen */ 209*5d413c73SIlpo Järvinen if (!pass) { 210*5d413c73SIlpo Järvinen /* 211*5d413c73SIlpo Järvinen * Temporarily disable forwarding of the configuration 212*5d413c73SIlpo Järvinen * cycles on all bridges in this bus segment to avoid 213*5d413c73SIlpo Järvinen * possible conflicts in the second pass between two bridges 214*5d413c73SIlpo Järvinen * programmed with overlapping bus ranges. 215*5d413c73SIlpo Järvinen */ 216*5d413c73SIlpo Järvinen pci_write_config_dword(dev, PCI_PRIMARY_BUS, 217*5d413c73SIlpo Järvinen buses & PCI_SEC_LATENCY_TIMER_MASK); 218*5d413c73SIlpo Järvinen return max; 219*5d413c73SIlpo Järvinen } 220*5d413c73SIlpo Järvinen 221*5d413c73SIlpo Järvinen /* Clear errors */ 222*5d413c73SIlpo Järvinen pci_write_config_word(dev, PCI_STATUS, 0xffff); 223*5d413c73SIlpo Järvinen 224*5d413c73SIlpo Järvinen /* Read bus numbers from EA Capability (if present) */ 225*5d413c73SIlpo Järvinen fixed_buses = pci_ea_fixed_busnrs(dev, &fixed_sec, &fixed_sub); 226*5d413c73SIlpo Järvinen if (fixed_buses) 227*5d413c73SIlpo Järvinen next_busnr = fixed_sec; 228*5d413c73SIlpo Järvinen else 229*5d413c73SIlpo Järvinen next_busnr = max + 1; 230*5d413c73SIlpo Järvinen 231*5d413c73SIlpo Järvinen /* 232*5d413c73SIlpo Järvinen * Prevent assigning a bus number that already exists. This can 233*5d413c73SIlpo Järvinen * happen when a bridge is hot-plugged, so in this case we only 234*5d413c73SIlpo Järvinen * re-scan this bus. 235*5d413c73SIlpo Järvinen */ 236*5d413c73SIlpo Järvinen child = pci_find_bus(pci_domain_nr(bus), next_busnr); 237*5d413c73SIlpo Järvinen if (!child) { 238*5d413c73SIlpo Järvinen child = pci_add_new_bus(bus, dev, next_busnr); 239*5d413c73SIlpo Järvinen if (!child) 240*5d413c73SIlpo Järvinen return max; 241*5d413c73SIlpo Järvinen pci_bus_insert_busn_res(child, next_busnr, bus->busn_res.end); 242*5d413c73SIlpo Järvinen } 243*5d413c73SIlpo Järvinen max++; 244*5d413c73SIlpo Järvinen if (available_buses) 245*5d413c73SIlpo Järvinen available_buses--; 246*5d413c73SIlpo Järvinen 247*5d413c73SIlpo Järvinen buses = (buses & PCI_SEC_LATENCY_TIMER_MASK) | 248*5d413c73SIlpo Järvinen FIELD_PREP(PCI_PRIMARY_BUS_MASK, child->primary) | 249*5d413c73SIlpo Järvinen FIELD_PREP(PCI_SECONDARY_BUS_MASK, child->busn_res.start) | 250*5d413c73SIlpo Järvinen FIELD_PREP(PCI_SUBORDINATE_BUS_MASK, child->busn_res.end); 251*5d413c73SIlpo Järvinen 252*5d413c73SIlpo Järvinen /* 253*5d413c73SIlpo Järvinen * yenta.c forces a secondary latency timer of 176. 254*5d413c73SIlpo Järvinen * Copy that behaviour here. 255*5d413c73SIlpo Järvinen */ 256*5d413c73SIlpo Järvinen buses &= ~PCI_SEC_LATENCY_TIMER_MASK; 257*5d413c73SIlpo Järvinen buses |= FIELD_PREP(PCI_SEC_LATENCY_TIMER_MASK, CARDBUS_LATENCY_TIMER); 258*5d413c73SIlpo Järvinen 259*5d413c73SIlpo Järvinen /* We need to blast all three values with a single write */ 260*5d413c73SIlpo Järvinen pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses); 261*5d413c73SIlpo Järvinen 262*5d413c73SIlpo Järvinen /* 263*5d413c73SIlpo Järvinen * For CardBus bridges, we leave 4 bus numbers as cards with a 264*5d413c73SIlpo Järvinen * PCI-to-PCI bridge can be inserted later. 265*5d413c73SIlpo Järvinen */ 266*5d413c73SIlpo Järvinen for (i = 0; i < CARDBUS_RESERVE_BUSNR; i++) { 267*5d413c73SIlpo Järvinen struct pci_bus *parent = bus; 268*5d413c73SIlpo Järvinen 269*5d413c73SIlpo Järvinen if (pci_find_bus(pci_domain_nr(bus), max + i + 1)) 270*5d413c73SIlpo Järvinen break; 271*5d413c73SIlpo Järvinen 272*5d413c73SIlpo Järvinen while (parent->parent) { 273*5d413c73SIlpo Järvinen if (!pcibios_assign_all_busses() && 274*5d413c73SIlpo Järvinen (parent->busn_res.end > max) && 275*5d413c73SIlpo Järvinen (parent->busn_res.end <= max + i)) { 276*5d413c73SIlpo Järvinen j = 1; 277*5d413c73SIlpo Järvinen } 278*5d413c73SIlpo Järvinen parent = parent->parent; 279*5d413c73SIlpo Järvinen } 280*5d413c73SIlpo Järvinen if (j) { 281*5d413c73SIlpo Järvinen /* 282*5d413c73SIlpo Järvinen * Often, there are two CardBus bridges -- try to 283*5d413c73SIlpo Järvinen * leave one valid bus number for each one. 284*5d413c73SIlpo Järvinen */ 285*5d413c73SIlpo Järvinen i /= 2; 286*5d413c73SIlpo Järvinen break; 287*5d413c73SIlpo Järvinen } 288*5d413c73SIlpo Järvinen } 289*5d413c73SIlpo Järvinen max += i; 290*5d413c73SIlpo Järvinen 291*5d413c73SIlpo Järvinen /* 292*5d413c73SIlpo Järvinen * Set subordinate bus number to its real value. If fixed 293*5d413c73SIlpo Järvinen * subordinate bus number exists from EA capability then use it. 294*5d413c73SIlpo Järvinen */ 295*5d413c73SIlpo Järvinen if (fixed_buses) 296*5d413c73SIlpo Järvinen max = fixed_sub; 297*5d413c73SIlpo Järvinen pci_bus_update_busn_res_end(child, max); 298*5d413c73SIlpo Järvinen pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max); 299*5d413c73SIlpo Järvinen 300*5d413c73SIlpo Järvinen scnprintf(child->name, sizeof(child->name), "PCI CardBus %04x:%02x", 301*5d413c73SIlpo Järvinen pci_domain_nr(bus), child->number); 302*5d413c73SIlpo Järvinen 303*5d413c73SIlpo Järvinen pbus_validate_busn(child); 304*5d413c73SIlpo Järvinen 305*5d413c73SIlpo Järvinen return max; 306*5d413c73SIlpo Järvinen } 307