4 * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
5 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
7 * pSeries specific routines for PCI.
9 * Based on code from pci.c and chrp_pci.c
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/kernel.h>
27 #include <linux/threads.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/string.h>
31 #include <linux/init.h>
32 #include <linux/bootmem.h>
35 #include <asm/pgtable.h>
38 #include <asm/machdep.h>
39 #include <asm/pci-bridge.h>
40 #include <asm/ppcdebug.h>
42 #include <asm/iommu.h>
49 static int read_pci_config;
50 static int write_pci_config;
51 static int ibm_read_pci_config;
52 static int ibm_write_pci_config;
54 static int s7a_workaround;
56 extern unsigned long pci_probe_only;
58 static int rtas_read_config(struct device_node *dn, int where, int size, u32 *val)
61 unsigned long buid, addr;
65 return PCIBIOS_DEVICE_NOT_FOUND;
66 if (where & (size - 1))
67 return PCIBIOS_BAD_REGISTER_NUMBER;
69 addr = (dn->busno << 16) | (dn->devfn << 8) | where;
72 ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
73 addr, buid >> 32, buid & 0xffffffff, size);
75 ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
80 return PCIBIOS_DEVICE_NOT_FOUND;
82 if (returnval == EEH_IO_ERROR_VALUE(size)
83 && eeh_dn_check_failure (dn, NULL))
84 return PCIBIOS_DEVICE_NOT_FOUND;
86 return PCIBIOS_SUCCESSFUL;
89 static int rtas_pci_read_config(struct pci_bus *bus,
91 int where, int size, u32 *val)
93 struct device_node *busdn, *dn;
96 busdn = pci_device_to_OF_node(bus->self);
98 busdn = bus->sysdata; /* must be a phb */
100 /* Search only direct children of the bus */
101 for (dn = busdn->child; dn; dn = dn->sibling)
102 if (dn->devfn == devfn)
103 return rtas_read_config(dn, where, size, val);
104 return PCIBIOS_DEVICE_NOT_FOUND;
107 static int rtas_write_config(struct device_node *dn, int where, int size, u32 val)
109 unsigned long buid, addr;
113 return PCIBIOS_DEVICE_NOT_FOUND;
114 if (where & (size - 1))
115 return PCIBIOS_BAD_REGISTER_NUMBER;
117 addr = (dn->busno << 16) | (dn->devfn << 8) | where;
118 buid = dn->phb->buid;
120 ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, buid >> 32, buid & 0xffffffff, size, (ulong) val);
122 ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
126 return PCIBIOS_DEVICE_NOT_FOUND;
128 return PCIBIOS_SUCCESSFUL;
131 static int rtas_pci_write_config(struct pci_bus *bus,
133 int where, int size, u32 val)
135 struct device_node *busdn, *dn;
138 busdn = pci_device_to_OF_node(bus->self);
140 busdn = bus->sysdata; /* must be a phb */
142 /* Search only direct children of the bus */
143 for (dn = busdn->child; dn; dn = dn->sibling)
144 if (dn->devfn == devfn)
145 return rtas_write_config(dn, where, size, val);
146 return PCIBIOS_DEVICE_NOT_FOUND;
149 struct pci_ops rtas_pci_ops = {
150 rtas_pci_read_config,
151 rtas_pci_write_config
154 static void python_countermeasures(unsigned long addr)
157 volatile u32 *tmp, i;
159 /* Python's register file is 1 MB in size. */
160 chip_regs = ioremap(addr & ~(0xfffffUL), 0x100000);
163 * Firmware doesn't always clear this bit which is critical
164 * for good performance - Anton
167 #define PRG_CL_RESET_VALID 0x00010000
169 tmp = (u32 *)((unsigned long)chip_regs + 0xf6030);
171 if (*tmp & PRG_CL_RESET_VALID) {
172 printk(KERN_INFO "Python workaround: ");
173 *tmp &= ~PRG_CL_RESET_VALID;
175 * We must read it back for changes to
179 printk("reg0: %x\n", i);
185 void __init init_pci_config_tokens (void)
187 read_pci_config = rtas_token("read-pci-config");
188 write_pci_config = rtas_token("write-pci-config");
189 ibm_read_pci_config = rtas_token("ibm,read-pci-config");
190 ibm_write_pci_config = rtas_token("ibm,write-pci-config");
193 unsigned long __devinit get_phb_buid (struct device_node *phb)
196 unsigned int *buid_vals;
200 if (ibm_read_pci_config == -1) return 0;
202 /* PHB's will always be children of the root node,
203 * or so it is promised by the current firmware. */
204 if (phb->parent == NULL)
206 if (phb->parent->parent)
209 buid_vals = (unsigned int *) get_property(phb, "reg", &len);
210 if (buid_vals == NULL)
213 addr_cells = prom_n_addr_cells(phb);
214 if (addr_cells == 1) {
215 buid = (unsigned long) buid_vals[0];
217 buid = (((unsigned long)buid_vals[0]) << 32UL) |
218 (((unsigned long)buid_vals[1]) & 0xffffffff);
223 static enum phb_types get_phb_type(struct device_node *dev)
228 model = (char *)get_property(dev, "model", NULL);
231 printk(KERN_ERR "%s: phb has no model property\n",
236 if (strstr(model, "Python")) {
237 type = phb_type_python;
238 } else if (strstr(model, "Speedwagon")) {
239 type = phb_type_speedwagon;
240 } else if (strstr(model, "Winnipeg")) {
241 type = phb_type_winnipeg;
243 printk(KERN_ERR "%s: unknown PHB %s\n", __FUNCTION__, model);
244 type = phb_type_unknown;
250 int get_phb_reg_prop(struct device_node *dev, unsigned int addr_size_words,
251 struct reg_property64 *reg)
253 unsigned int *ui_ptr = NULL, len;
255 /* Found a PHB, now figure out where his registers are mapped. */
256 ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
257 if (ui_ptr == NULL) {
258 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n");
262 if (addr_size_words == 1) {
263 reg->address = ((struct reg_property32 *)ui_ptr)->address;
264 reg->size = ((struct reg_property32 *)ui_ptr)->size;
266 *reg = *((struct reg_property64 *)ui_ptr);
272 int phb_set_bus_ranges(struct device_node *dev, struct pci_controller *phb)
277 bus_range = (int *) get_property(dev, "bus-range", &len);
278 if (bus_range == NULL || len < 2 * sizeof(int)) {
282 phb->first_busno = bus_range[0];
283 phb->last_busno = bus_range[1];
288 static struct pci_controller *alloc_phb(struct device_node *dev,
289 unsigned int addr_size_words)
291 struct pci_controller *phb;
292 struct reg_property64 reg_struct;
293 enum phb_types phb_type;
294 struct property *of_prop;
297 phb_type = get_phb_type(dev);
299 rc = get_phb_reg_prop(dev, addr_size_words, ®_struct);
303 phb = pci_alloc_pci_controller(phb_type);
307 if (phb_type == phb_type_python)
308 python_countermeasures(reg_struct.address);
310 rc = phb_set_bus_ranges(dev, phb);
314 of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
315 sizeof(phb->global_number));
322 memset(of_prop, 0, sizeof(struct property));
323 of_prop->name = "linux,pci-domain";
324 of_prop->length = sizeof(phb->global_number);
325 of_prop->value = (unsigned char *)&of_prop[1];
326 memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
327 prom_add_property(dev, of_prop);
329 phb->arch_data = dev;
330 phb->ops = &rtas_pci_ops;
332 phb->buid = get_phb_buid(dev);
337 static struct pci_controller * __devinit alloc_phb_dynamic(struct device_node *dev, unsigned int addr_size_words)
339 struct pci_controller *phb;
340 struct reg_property64 reg_struct;
341 enum phb_types phb_type;
344 phb_type = get_phb_type(dev);
346 rc = get_phb_reg_prop(dev, addr_size_words, ®_struct);
350 phb = pci_alloc_phb_dynamic(phb_type);
354 if (phb_type == phb_type_python)
355 python_countermeasures(reg_struct.address);
357 rc = phb_set_bus_ranges(dev, phb);
361 /* TODO: linux,pci-domain? */
363 phb->arch_data = dev;
364 phb->ops = &rtas_pci_ops;
366 phb->buid = get_phb_buid(dev);
371 unsigned long __init find_and_init_phbs(void)
373 struct device_node *node;
374 struct pci_controller *phb;
375 unsigned int root_size_cells = 0;
377 unsigned int *opprop = NULL;
378 struct device_node *root = of_find_node_by_path("/");
380 if (naca->interrupt_controller == IC_OPEN_PIC) {
381 opprop = (unsigned int *)get_property(root,
382 "platform-open-pic", NULL);
385 root_size_cells = prom_n_size_cells(root);
389 for (node = of_get_next_child(root, NULL);
391 node = of_get_next_child(root, node)) {
392 if (node->type == NULL || strcmp(node->type, "pci") != 0)
395 phb = alloc_phb(node, root_size_cells);
399 pci_process_bridge_OF_ranges(phb, node);
400 pci_setup_phb_io(phb, index == 0);
402 if (naca->interrupt_controller == IC_OPEN_PIC) {
403 int addr = root_size_cells * (index + 2) - 1;
404 openpic_setup_ISU(index, opprop[addr]);
416 struct pci_controller * __devinit init_phb_dynamic(struct device_node *dn)
418 struct device_node *root = of_find_node_by_path("/");
419 unsigned int root_size_cells = 0;
420 struct pci_controller *phb;
423 root_size_cells = prom_n_size_cells(root);
425 phb = alloc_phb_dynamic(dn, root_size_cells);
429 pci_process_bridge_OF_ranges(phb, dn);
431 pci_setup_phb_io_dynamic(phb);
434 pci_devs_phb_init_dynamic(phb);
435 phb->last_busno = 0xff;
436 bus = pci_scan_bus(phb->first_busno, phb->ops, phb->arch_data);
438 phb->last_busno = bus->subordinate;
442 EXPORT_SYMBOL(init_phb_dynamic);
445 void pcibios_name_device(struct pci_dev *dev)
447 struct device_node *dn;
450 * Add IBM loc code (slot) as a prefix to the device names for service
452 dn = pci_device_to_OF_node(dev);
454 char *loc_code = get_property(dn, "ibm,loc-code", 0);
456 int loc_len = strlen(loc_code);
457 if (loc_len < sizeof(dev->dev.name)) {
458 memmove(dev->dev.name+loc_len+1, dev->dev.name,
459 sizeof(dev->dev.name)-loc_len-1);
460 memcpy(dev->dev.name, loc_code, loc_len);
461 dev->dev.name[loc_len] = ' ';
462 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
467 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_name_device);
470 static void check_s7a(void)
472 struct device_node *root;
475 root = of_find_node_by_path("/");
477 model = get_property(root, "model", NULL);
478 if (model && !strcmp(model, "IBM,7013-S7A"))
484 static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
485 unsigned long *start_virt, unsigned long *size)
487 struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
488 struct pci_bus_region region;
489 struct resource *res;
492 res = bus->resource[0];
493 pcibios_resource_to_bus(bus->self, ®ion, res);
494 *start_phys = hose->io_base_phys + region.start;
495 *start_virt = (unsigned long) hose->io_base_virt +
497 if (region.end > region.start)
498 *size = region.end - region.start + 1;
500 printk("%s(): unexpected region 0x%lx->0x%lx\n",
501 __FUNCTION__, region.start, region.end);
507 res = &hose->io_resource;
508 *start_phys = hose->io_base_phys;
509 *start_virt = (unsigned long) hose->io_base_virt;
510 if (res->end > res->start)
511 *size = res->end - res->start + 1;
513 printk("%s(): unexpected region 0x%lx->0x%lx\n",
514 __FUNCTION__, res->start, res->end);
522 int unmap_bus_range(struct pci_bus *bus)
524 unsigned long start_phys;
525 unsigned long start_virt;
529 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
533 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
535 if (iounmap_explicit((void *) start_virt, size))
540 EXPORT_SYMBOL(unmap_bus_range);
542 int remap_bus_range(struct pci_bus *bus)
544 unsigned long start_phys;
545 unsigned long start_virt;
549 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
553 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
555 if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
560 EXPORT_SYMBOL(remap_bus_range);
562 static void phbs_remap_io(void)
564 struct pci_controller *hose, *tmp;
566 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
567 remap_bus_range(hose->bus);
570 static void __init pSeries_request_regions(void)
572 struct device_node *i8042;
574 request_region(0x20,0x20,"pic1");
575 request_region(0xa0,0x20,"pic2");
576 request_region(0x00,0x20,"dma1");
577 request_region(0x40,0x20,"timer");
578 request_region(0x80,0x10,"dma page reg");
579 request_region(0xc0,0x20,"dma2");
581 #define I8042_DATA_REG 0x60
584 * Some machines have an unterminated i8042 so check the device
585 * tree and reserve the region if it does not appear. Later on
586 * the i8042 code will try and reserve this region and fail.
588 if (!(i8042 = of_find_node_by_type(NULL, "8042")))
589 request_region(I8042_DATA_REG, 16, "reserved (no i8042)");
593 /* RPA-specific bits for removing PHBs */
594 int pcibios_remove_root_bus(struct pci_controller *phb)
596 struct pci_bus *b = phb->bus;
597 struct resource *res;
600 res = b->resource[0];
602 printk(KERN_ERR "%s: no IO resource for PHB %s\n", __FUNCTION__,
607 rc = unmap_bus_range(b);
609 printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
610 __FUNCTION__, b->name);
614 if (release_resource(res)) {
615 printk(KERN_ERR "%s: failed to release IO on bus %s\n",
616 __FUNCTION__, b->name);
620 for (i = 1; i < 3; ++i) {
621 res = b->resource[i];
622 if (!res->flags && i == 0) {
623 printk(KERN_ERR "%s: no MEM resource for PHB %s\n",
624 __FUNCTION__, b->name);
627 if (res->flags && release_resource(res)) {
629 "%s: failed to release IO %d on bus %s\n",
630 __FUNCTION__, i, b->name);
635 list_del(&phb->list_node);
641 EXPORT_SYMBOL(pcibios_remove_root_bus);
643 void __init pSeries_final_fixup(void)
645 struct pci_dev *dev = NULL;
649 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
650 pci_read_irq_line(dev);
651 if (s7a_workaround) {
654 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
660 pSeries_request_regions();
661 pci_fix_bus_sysdata();
663 if (!of_chosen || !get_property(of_chosen, "linux,iommu-off", NULL))
664 iommu_setup_pSeries();
666 pci_addr_cache_build();