1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2003 Christoph Hellwig (hch@lst.de)
4 * Copyright (C) 1999, 2000, 04 Ralf Baechle (ralf@linux-mips.org)
5 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
7 #include <linux/kernel.h>
8 #include <linux/export.h>
10 #include <linux/smp.h>
11 #include <linux/dma-direct.h>
12 #include <linux/platform_device.h>
13 #include <linux/platform_data/xtalk-bridge.h>
14 #include <linux/nvmem-consumer.h>
15 #include <linux/crc16.h>
17 #include <asm/pci/bridge.h>
18 #include <asm/paccess.h>
19 #include <asm/sn/irq_alloc.h>
20 #include <asm/sn/ioc3.h>
23 #define CRC16_VALID 0xb001
26 * Most of the IOC3 PCI config register aren't present
27 * we emulate what is needed for a normal PCI enumeration
29 static int ioc3_cfg_rd(void *addr, int where, int size, u32 *value, u32 sid)
36 if (get_dbe(cf, (u32 *)addr))
37 return PCIBIOS_DEVICE_NOT_FOUND;
43 /* emulate sane interrupt pin value */
50 shift = (where & 3) << 3;
51 mask = 0xffffffffU >> ((4 - size) << 3);
52 *value = (cf >> shift) & mask;
54 return PCIBIOS_SUCCESSFUL;
57 static int ioc3_cfg_wr(void *addr, int where, int size, u32 value)
59 u32 cf, shift, mask, smask;
61 if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
62 return PCIBIOS_SUCCESSFUL;
64 if (get_dbe(cf, (u32 *)addr))
65 return PCIBIOS_DEVICE_NOT_FOUND;
67 shift = ((where & 3) << 3);
68 mask = (0xffffffffU >> ((4 - size) << 3));
69 smask = mask << shift;
71 cf = (cf & ~smask) | ((value & mask) << shift);
72 if (put_dbe(cf, (u32 *)addr))
73 return PCIBIOS_DEVICE_NOT_FOUND;
75 return PCIBIOS_SUCCESSFUL;
78 static void bridge_disable_swapping(struct pci_dev *dev)
80 struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
81 int slot = PCI_SLOT(dev->devfn);
83 /* Turn off byte swapping */
84 bridge_clr(bc, b_device[slot].reg, BRIDGE_DEV_SWAP_DIR);
85 bridge_read(bc, b_widget.w_tflush); /* Flush */
88 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
89 bridge_disable_swapping);
93 * The Bridge ASIC supports both type 0 and type 1 access. Type 1 is
94 * not really documented, so right now I can't write code which uses it.
95 * Therefore we use type 0 accesses for now even though they won't work
96 * correctly for PCI-to-PCI bridges.
98 * The function is complicated by the ultimate brokenness of the IOC3 chip
99 * which is used in SGI systems. The IOC3 can only handle 32-bit PCI
100 * accesses and does only decode parts of it's address space.
102 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
103 int where, int size, u32 *value)
105 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
106 struct bridge_regs *bridge = bc->base;
107 int slot = PCI_SLOT(devfn);
108 int fn = PCI_FUNC(devfn);
113 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
114 if (get_dbe(cf, (u32 *)addr))
115 return PCIBIOS_DEVICE_NOT_FOUND;
118 * IOC3 is broken beyond belief ... Don't even give the
119 * generic PCI code a chance to look at it for real ...
121 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))) {
122 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
123 return ioc3_cfg_rd(addr, where, size, value,
127 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
130 res = get_dbe(*value, (u8 *)addr);
132 res = get_dbe(*value, (u16 *)addr);
134 res = get_dbe(*value, (u32 *)addr);
136 return res ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
139 static int pci_conf1_read_config(struct pci_bus *bus, unsigned int devfn,
140 int where, int size, u32 *value)
142 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
143 struct bridge_regs *bridge = bc->base;
144 int busno = bus->number;
145 int slot = PCI_SLOT(devfn);
146 int fn = PCI_FUNC(devfn);
151 bridge_write(bc, b_pci_cfg, (busno << 16) | (slot << 11));
152 addr = &bridge->b_type1_cfg.c[(fn << 8) | PCI_VENDOR_ID];
153 if (get_dbe(cf, (u32 *)addr))
154 return PCIBIOS_DEVICE_NOT_FOUND;
157 * IOC3 is broken beyond belief ... Don't even give the
158 * generic PCI code a chance to look at it for real ...
160 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))) {
161 addr = &bridge->b_type1_cfg.c[(fn << 8) | (where & ~3)];
162 return ioc3_cfg_rd(addr, where, size, value,
166 addr = &bridge->b_type1_cfg.c[(fn << 8) | (where ^ (4 - size))];
169 res = get_dbe(*value, (u8 *)addr);
171 res = get_dbe(*value, (u16 *)addr);
173 res = get_dbe(*value, (u32 *)addr);
175 return res ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
178 static int pci_read_config(struct pci_bus *bus, unsigned int devfn,
179 int where, int size, u32 *value)
181 if (!pci_is_root_bus(bus))
182 return pci_conf1_read_config(bus, devfn, where, size, value);
184 return pci_conf0_read_config(bus, devfn, where, size, value);
187 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
188 int where, int size, u32 value)
190 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
191 struct bridge_regs *bridge = bc->base;
192 int slot = PCI_SLOT(devfn);
193 int fn = PCI_FUNC(devfn);
198 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
199 if (get_dbe(cf, (u32 *)addr))
200 return PCIBIOS_DEVICE_NOT_FOUND;
203 * IOC3 is broken beyond belief ... Don't even give the
204 * generic PCI code a chance to look at it for real ...
206 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))) {
207 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
208 return ioc3_cfg_wr(addr, where, size, value);
211 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
214 res = put_dbe(value, (u8 *)addr);
216 res = put_dbe(value, (u16 *)addr);
218 res = put_dbe(value, (u32 *)addr);
221 return PCIBIOS_DEVICE_NOT_FOUND;
223 return PCIBIOS_SUCCESSFUL;
226 static int pci_conf1_write_config(struct pci_bus *bus, unsigned int devfn,
227 int where, int size, u32 value)
229 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
230 struct bridge_regs *bridge = bc->base;
231 int slot = PCI_SLOT(devfn);
232 int fn = PCI_FUNC(devfn);
233 int busno = bus->number;
238 bridge_write(bc, b_pci_cfg, (busno << 16) | (slot << 11));
239 addr = &bridge->b_type1_cfg.c[(fn << 8) | PCI_VENDOR_ID];
240 if (get_dbe(cf, (u32 *)addr))
241 return PCIBIOS_DEVICE_NOT_FOUND;
244 * IOC3 is broken beyond belief ... Don't even give the
245 * generic PCI code a chance to look at it for real ...
247 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))) {
248 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
249 return ioc3_cfg_wr(addr, where, size, value);
252 addr = &bridge->b_type1_cfg.c[(fn << 8) | (where ^ (4 - size))];
255 res = put_dbe(value, (u8 *)addr);
257 res = put_dbe(value, (u16 *)addr);
259 res = put_dbe(value, (u32 *)addr);
262 return PCIBIOS_DEVICE_NOT_FOUND;
264 return PCIBIOS_SUCCESSFUL;
267 static int pci_write_config(struct pci_bus *bus, unsigned int devfn,
268 int where, int size, u32 value)
270 if (!pci_is_root_bus(bus))
271 return pci_conf1_write_config(bus, devfn, where, size, value);
273 return pci_conf0_write_config(bus, devfn, where, size, value);
276 static struct pci_ops bridge_pci_ops = {
277 .read = pci_read_config,
278 .write = pci_write_config,
281 struct bridge_irq_chip_data {
282 struct bridge_controller *bc;
286 static int bridge_set_affinity(struct irq_data *d, const struct cpumask *mask,
290 struct bridge_irq_chip_data *data = d->chip_data;
291 int bit = d->parent_data->hwirq;
296 ret = irq_chip_set_affinity_parent(d, mask, force);
298 cpu = cpumask_first_and(mask, cpu_online_mask);
299 nasid = cpu_to_node(cpu);
300 bridge_write(data->bc, b_int_addr[pin].addr,
301 (((data->bc->intr_addr >> 30) & 0x30000) |
302 bit | (nasid << 8)));
303 bridge_read(data->bc, b_wid_tflush);
307 return irq_chip_set_affinity_parent(d, mask, force);
311 struct irq_chip bridge_irq_chip = {
313 .irq_mask = irq_chip_mask_parent,
314 .irq_unmask = irq_chip_unmask_parent,
315 .irq_set_affinity = bridge_set_affinity
318 static int bridge_domain_alloc(struct irq_domain *domain, unsigned int virq,
319 unsigned int nr_irqs, void *arg)
321 struct bridge_irq_chip_data *data;
322 struct irq_alloc_info *info = arg;
325 if (nr_irqs > 1 || !info)
328 data = kzalloc(sizeof(*data), GFP_KERNEL);
332 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
334 data->bc = info->ctrl;
335 data->nasid = info->nasid;
336 irq_domain_set_info(domain, virq, info->pin, &bridge_irq_chip,
337 data, handle_level_irq, NULL, NULL);
345 static void bridge_domain_free(struct irq_domain *domain, unsigned int virq,
346 unsigned int nr_irqs)
348 struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
353 kfree(irqd->chip_data);
354 irq_domain_free_irqs_top(domain, virq, nr_irqs);
357 static int bridge_domain_activate(struct irq_domain *domain,
358 struct irq_data *irqd, bool reserve)
360 struct bridge_irq_chip_data *data = irqd->chip_data;
361 struct bridge_controller *bc = data->bc;
362 int bit = irqd->parent_data->hwirq;
363 int pin = irqd->hwirq;
366 bridge_write(bc, b_int_addr[pin].addr,
367 (((bc->intr_addr >> 30) & 0x30000) |
368 bit | (data->nasid << 8)));
369 bridge_set(bc, b_int_enable, (1 << pin));
370 bridge_set(bc, b_int_enable, 0x7ffffe00); /* more stuff in int_enable */
373 * Enable sending of an interrupt clear packt to the hub on a high to
374 * low transition of the interrupt pin.
376 * IRIX sets additional bits in the address which are documented as
377 * reserved in the bridge docs.
379 bridge_set(bc, b_int_mode, (1UL << pin));
382 * We assume the bridge to have a 1:1 mapping between devices
383 * (slots) and intr pins.
385 device = bridge_read(bc, b_int_device);
386 device &= ~(7 << (pin*3));
387 device |= (pin << (pin*3));
388 bridge_write(bc, b_int_device, device);
390 bridge_read(bc, b_wid_tflush);
394 static void bridge_domain_deactivate(struct irq_domain *domain,
395 struct irq_data *irqd)
397 struct bridge_irq_chip_data *data = irqd->chip_data;
399 bridge_clr(data->bc, b_int_enable, (1 << irqd->hwirq));
400 bridge_read(data->bc, b_wid_tflush);
403 static const struct irq_domain_ops bridge_domain_ops = {
404 .alloc = bridge_domain_alloc,
405 .free = bridge_domain_free,
406 .activate = bridge_domain_activate,
407 .deactivate = bridge_domain_deactivate
411 * All observed requests have pin == 1. We could have a global here, that
412 * gets incremented and returned every time - unfortunately, pci_map_irq
413 * may be called on the same device over and over, and need to return the
414 * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
416 * A given PCI device, in general, should be able to intr any of the cpus
417 * on any one of the hubs connected to its xbow.
419 static int bridge_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
421 struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
422 struct irq_alloc_info info;
425 irq = bc->pci_int[slot];
428 info.nasid = bc->nasid;
431 irq = irq_domain_alloc_irqs(bc->domain, 1, bc->nasid, &info);
435 bc->pci_int[slot] = irq;
440 #define IOC3_SID(sid) (PCI_VENDOR_ID_SGI << 16 | (sid))
442 static void bridge_setup_ip27_baseio6g(struct bridge_controller *bc)
444 bc->ioc3_sid[2] = IOC3_SID(IOC3_SUBSYS_IP27_BASEIO6G);
445 bc->ioc3_sid[6] = IOC3_SID(IOC3_SUBSYS_IP27_MIO);
448 static void bridge_setup_ip27_baseio(struct bridge_controller *bc)
450 bc->ioc3_sid[2] = IOC3_SID(IOC3_SUBSYS_IP27_BASEIO);
453 static void bridge_setup_ip29_baseio(struct bridge_controller *bc)
455 bc->ioc3_sid[2] = IOC3_SID(IOC3_SUBSYS_IP29_SYSBOARD);
458 static void bridge_setup_ip30_sysboard(struct bridge_controller *bc)
460 bc->ioc3_sid[2] = IOC3_SID(IOC3_SUBSYS_IP30_SYSBOARD);
463 static void bridge_setup_menet(struct bridge_controller *bc)
465 bc->ioc3_sid[0] = IOC3_SID(IOC3_SUBSYS_MENET);
466 bc->ioc3_sid[1] = IOC3_SID(IOC3_SUBSYS_MENET);
467 bc->ioc3_sid[2] = IOC3_SID(IOC3_SUBSYS_MENET);
468 bc->ioc3_sid[3] = IOC3_SID(IOC3_SUBSYS_MENET4);
471 #define BRIDGE_BOARD_SETUP(_partno, _setup) \
472 { .match = _partno, .setup = _setup }
474 static const struct {
476 void (*setup)(struct bridge_controller *bc);
477 } bridge_ioc3_devid[] = {
478 BRIDGE_BOARD_SETUP("030-0734-", bridge_setup_ip27_baseio6g),
479 BRIDGE_BOARD_SETUP("030-0880-", bridge_setup_ip27_baseio6g),
480 BRIDGE_BOARD_SETUP("030-1023-", bridge_setup_ip27_baseio),
481 BRIDGE_BOARD_SETUP("030-1124-", bridge_setup_ip27_baseio),
482 BRIDGE_BOARD_SETUP("030-1025-", bridge_setup_ip29_baseio),
483 BRIDGE_BOARD_SETUP("030-1244-", bridge_setup_ip29_baseio),
484 BRIDGE_BOARD_SETUP("030-1389-", bridge_setup_ip29_baseio),
485 BRIDGE_BOARD_SETUP("030-0887-", bridge_setup_ip30_sysboard),
486 BRIDGE_BOARD_SETUP("030-1467-", bridge_setup_ip30_sysboard),
487 BRIDGE_BOARD_SETUP("030-0873-", bridge_setup_menet),
490 static void bridge_setup_board(struct bridge_controller *bc, char *partnum)
494 for (i = 0; i < ARRAY_SIZE(bridge_ioc3_devid); i++)
495 if (!strncmp(partnum, bridge_ioc3_devid[i].match,
496 strlen(bridge_ioc3_devid[i].match))) {
497 bridge_ioc3_devid[i].setup(bc);
501 static int bridge_nvmem_match(struct device *dev, const void *data)
503 const char *name = dev_name(dev);
504 const char *prefix = data;
506 if (strlen(name) < strlen(prefix))
509 return memcmp(prefix, dev_name(dev), strlen(prefix)) == 0;
512 static int bridge_get_partnum(u64 baddr, char *partnum)
514 struct nvmem_device *nvmem;
520 snprintf(prefix, sizeof(prefix), "bridge-%012llx-0b-", baddr);
522 nvmem = nvmem_device_find(prefix, bridge_nvmem_match);
524 return PTR_ERR(nvmem);
526 ret = nvmem_device_read(nvmem, 0, 64, prom);
527 nvmem_device_put(nvmem);
532 if (crc16(CRC16_INIT, prom, 32) != CRC16_VALID ||
533 crc16(CRC16_INIT, prom + 32, 32) != CRC16_VALID)
536 /* Assemble part number */
538 for (i = 0; i < 19; i++)
539 if (prom[i + 11] != ' ')
540 partnum[j++] = prom[i + 11];
542 for (i = 0; i < 6; i++)
543 if (prom[i + 32] != ' ')
544 partnum[j++] = prom[i + 32];
551 static int bridge_probe(struct platform_device *pdev)
553 struct xtalk_bridge_platform_data *bd = dev_get_platdata(&pdev->dev);
554 struct device *dev = &pdev->dev;
555 struct bridge_controller *bc;
556 struct pci_host_bridge *host;
557 struct irq_domain *domain, *parent;
558 struct fwnode_handle *fn;
563 /* get part number from one wire prom */
564 if (bridge_get_partnum(virt_to_phys((void *)bd->bridge_addr), partnum))
565 return -EPROBE_DEFER; /* not available yet */
567 parent = irq_get_default_host();
570 fn = irq_domain_alloc_named_fwnode("BRIDGE");
573 domain = irq_domain_create_hierarchy(parent, 0, 8, fn,
574 &bridge_domain_ops, NULL);
575 irq_domain_free_fwnode(fn);
579 pci_set_flags(PCI_PROBE_ONLY);
581 host = devm_pci_alloc_host_bridge(dev, sizeof(*bc));
584 goto err_remove_domain;
587 bc = pci_host_bridge_priv(host);
589 bc->busn.name = "Bridge PCI busn";
592 bc->busn.flags = IORESOURCE_BUS;
596 pci_add_resource_offset(&host->windows, &bd->mem, bd->mem_offset);
597 pci_add_resource_offset(&host->windows, &bd->io, bd->io_offset);
598 pci_add_resource(&host->windows, &bc->busn);
600 err = devm_request_pci_bus_resources(dev, &host->windows);
602 goto err_free_resource;
604 bc->nasid = bd->nasid;
606 bc->baddr = (u64)bd->masterwid << 60 | PCI64_ATTR_BAR;
607 bc->base = (struct bridge_regs *)bd->bridge_addr;
608 bc->intr_addr = bd->intr_addr;
611 * Clear all pending interrupts.
613 bridge_write(bc, b_int_rst_stat, BRIDGE_IRR_ALL_CLR);
616 * Until otherwise set up, assume all interrupts are from slot 0
618 bridge_write(bc, b_int_device, 0x0);
621 * disable swapping for big windows
623 bridge_clr(bc, b_wid_control,
624 BRIDGE_CTRL_IO_SWAP | BRIDGE_CTRL_MEM_SWAP);
625 #ifdef CONFIG_PAGE_SIZE_4KB
626 bridge_clr(bc, b_wid_control, BRIDGE_CTRL_PAGE_SIZE);
627 #else /* 16kB or larger */
628 bridge_set(bc, b_wid_control, BRIDGE_CTRL_PAGE_SIZE);
632 * Hmm... IRIX sets additional bits in the address which
633 * are documented as reserved in the bridge docs.
635 bridge_write(bc, b_wid_int_upper,
636 ((bc->intr_addr >> 32) & 0xffff) | (bd->masterwid << 16));
637 bridge_write(bc, b_wid_int_lower, bc->intr_addr & 0xffffffff);
638 bridge_write(bc, b_dir_map, (bd->masterwid << 20)); /* DMA */
639 bridge_write(bc, b_int_enable, 0);
641 for (slot = 0; slot < 8; slot++) {
642 bridge_set(bc, b_device[slot].reg, BRIDGE_DEV_SWAP_DIR);
643 bc->pci_int[slot] = -1;
645 bridge_read(bc, b_wid_tflush); /* wait until Bridge PIO complete */
647 bridge_setup_board(bc, partnum);
649 host->dev.parent = dev;
652 host->ops = &bridge_pci_ops;
653 host->map_irq = bridge_map_irq;
654 host->swizzle_irq = pci_common_swizzle;
656 err = pci_scan_root_bus_bridge(host);
658 goto err_free_resource;
660 pci_bus_claim_resources(host->bus);
661 pci_bus_add_devices(host->bus);
663 platform_set_drvdata(pdev, host->bus);
668 pci_free_resource_list(&host->windows);
670 irq_domain_remove(domain);
674 static int bridge_remove(struct platform_device *pdev)
676 struct pci_bus *bus = platform_get_drvdata(pdev);
677 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
679 irq_domain_remove(bc->domain);
680 pci_lock_rescan_remove();
681 pci_stop_root_bus(bus);
682 pci_remove_root_bus(bus);
683 pci_unlock_rescan_remove();
688 static struct platform_driver bridge_driver = {
689 .probe = bridge_probe,
690 .remove = bridge_remove,
692 .name = "xtalk-bridge",
696 builtin_platform_driver(bridge_driver);