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 __init 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 struct pci_controller * __init alloc_phb(struct device_node *dev,
224 unsigned int addr_size_words)
226 struct pci_controller *phb;
227 unsigned int *ui_ptr = NULL, len;
228 struct reg_property64 reg_struct;
231 enum phb_types phb_type;
232 struct property *of_prop;
234 model = (char *)get_property(dev, "model", NULL);
237 printk(KERN_ERR "alloc_phb: phb has no model property\n");
241 /* Found a PHB, now figure out where his registers are mapped. */
242 ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
243 if (ui_ptr == NULL) {
244 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n");
248 if (addr_size_words == 1) {
249 reg_struct.address = ((struct reg_property32 *)ui_ptr)->address;
250 reg_struct.size = ((struct reg_property32 *)ui_ptr)->size;
252 reg_struct = *((struct reg_property64 *)ui_ptr);
255 if (strstr(model, "Python")) {
256 phb_type = phb_type_python;
257 } else if (strstr(model, "Speedwagon")) {
258 phb_type = phb_type_speedwagon;
259 } else if (strstr(model, "Winnipeg")) {
260 phb_type = phb_type_winnipeg;
262 printk(KERN_ERR "alloc_phb: unknown PHB %s\n", model);
263 phb_type = phb_type_unknown;
266 phb = pci_alloc_pci_controller(phb_type);
270 if (phb_type == phb_type_python)
271 python_countermeasures(reg_struct.address);
273 bus_range = (int *) get_property(dev, "bus-range", &len);
274 if (bus_range == NULL || len < 2 * sizeof(int)) {
279 of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
280 sizeof(phb->global_number));
287 memset(of_prop, 0, sizeof(struct property));
288 of_prop->name = "linux,pci-domain";
289 of_prop->length = sizeof(phb->global_number);
290 of_prop->value = (unsigned char *)&of_prop[1];
291 memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
292 prom_add_property(dev, of_prop);
294 phb->first_busno = bus_range[0];
295 phb->last_busno = bus_range[1];
297 phb->arch_data = dev;
298 phb->ops = &rtas_pci_ops;
300 phb->buid = get_phb_buid(dev);
305 unsigned long __init find_and_init_phbs(void)
307 struct device_node *node;
308 struct pci_controller *phb;
309 unsigned int root_size_cells = 0;
311 unsigned int *opprop = NULL;
312 struct device_node *root = of_find_node_by_path("/");
314 if (naca->interrupt_controller == IC_OPEN_PIC) {
315 opprop = (unsigned int *)get_property(root,
316 "platform-open-pic", NULL);
319 root_size_cells = prom_n_size_cells(root);
323 for (node = of_get_next_child(root, NULL);
325 node = of_get_next_child(root, node)) {
326 if (node->type == NULL || strcmp(node->type, "pci") != 0)
329 phb = alloc_phb(node, root_size_cells);
333 pci_process_bridge_OF_ranges(phb, node, index == 0);
335 if (naca->interrupt_controller == IC_OPEN_PIC) {
336 int addr = root_size_cells * (index + 2) - 1;
337 openpic_setup_ISU(index, opprop[addr]);
350 void pcibios_name_device(struct pci_dev *dev)
352 struct device_node *dn;
355 * Add IBM loc code (slot) as a prefix to the device names for service
357 dn = pci_device_to_OF_node(dev);
359 char *loc_code = get_property(dn, "ibm,loc-code", 0);
361 int loc_len = strlen(loc_code);
362 if (loc_len < sizeof(dev->dev.name)) {
363 memmove(dev->dev.name+loc_len+1, dev->dev.name,
364 sizeof(dev->dev.name)-loc_len-1);
365 memcpy(dev->dev.name, loc_code, loc_len);
366 dev->dev.name[loc_len] = ' ';
367 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
372 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_name_device);
375 static void check_s7a(void)
377 struct device_node *root;
380 root = of_find_node_by_path("/");
382 model = get_property(root, "model", NULL);
383 if (model && !strcmp(model, "IBM,7013-S7A"))
389 static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
390 unsigned long *start_virt, unsigned long *size)
392 struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
393 struct pci_bus_region region;
394 struct resource *res;
397 res = bus->resource[0];
398 pcibios_resource_to_bus(bus->self, ®ion, res);
399 *start_phys = hose->io_base_phys + region.start;
400 *start_virt = (unsigned long) hose->io_base_virt +
402 if (region.end > region.start)
403 *size = region.end - region.start + 1;
405 printk("%s(): unexpected region 0x%lx->0x%lx\n",
406 __FUNCTION__, region.start, region.end);
412 res = &hose->io_resource;
413 *start_phys = hose->io_base_phys;
414 *start_virt = (unsigned long) hose->io_base_virt;
415 if (res->end > res->start)
416 *size = res->end - res->start + 1;
418 printk("%s(): unexpected region 0x%lx->0x%lx\n",
419 __FUNCTION__, res->start, res->end);
427 int unmap_bus_range(struct pci_bus *bus)
429 unsigned long start_phys;
430 unsigned long start_virt;
434 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
438 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
440 if (iounmap_explicit((void *) start_virt, size))
445 EXPORT_SYMBOL(unmap_bus_range);
447 int remap_bus_range(struct pci_bus *bus)
449 unsigned long start_phys;
450 unsigned long start_virt;
454 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
458 if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
460 if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
465 EXPORT_SYMBOL(remap_bus_range);
467 static void phbs_fixup_io(void)
469 struct pci_controller *hose, *tmp;
471 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
472 remap_bus_range(hose->bus);
475 static void __init pSeries_request_regions(void)
477 struct device_node *i8042;
479 request_region(0x20,0x20,"pic1");
480 request_region(0xa0,0x20,"pic2");
481 request_region(0x00,0x20,"dma1");
482 request_region(0x40,0x20,"timer");
483 request_region(0x80,0x10,"dma page reg");
484 request_region(0xc0,0x20,"dma2");
486 #define I8042_DATA_REG 0x60
489 * Some machines have an unterminated i8042 so check the device
490 * tree and reserve the region if it does not appear. Later on
491 * the i8042 code will try and reserve this region and fail.
493 if (!(i8042 = of_find_node_by_type(NULL, "8042")))
494 request_region(I8042_DATA_REG, 16, "reserved (no i8042)");
498 void __init pSeries_final_fixup(void)
500 struct pci_dev *dev = NULL;
504 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
505 pci_read_irq_line(dev);
506 if (s7a_workaround) {
509 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
515 pSeries_request_regions();
516 pci_fix_bus_sysdata();
518 if (!of_chosen || !get_property(of_chosen, "linux,iommu-off", NULL))
519 iommu_setup_pSeries();
521 pci_addr_cache_build();