2 * Support for PCI on Celleb platform.
4 * (C) Copyright 2006-2007 TOSHIBA CORPORATION
6 * This code is based on arch/powerpc/kernel/rtas_pci.c:
7 * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
8 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <linux/kernel.h>
28 #include <linux/threads.h>
29 #include <linux/pci.h>
30 #include <linux/string.h>
31 #include <linux/init.h>
32 #include <linux/bootmem.h>
33 #include <linux/pci_regs.h>
38 #include <asm/machdep.h>
39 #include <asm/pci-bridge.h>
40 #include <asm/ppc-pci.h>
43 #include "interrupt.h"
45 #define MAX_PCI_DEVICES 32
46 #define MAX_PCI_FUNCTIONS 8
47 #define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
49 /* definition for fake pci configuration area for GbE, .... ,and etc. */
51 struct celleb_pci_resource {
52 struct resource r[MAX_PCI_BASE_ADDRS];
55 struct celleb_pci_private {
56 unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
57 struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
60 static inline u8 celleb_fake_config_readb(void *addr)
66 static inline u16 celleb_fake_config_readw(void *addr)
69 return le16_to_cpu(*p);
72 static inline u32 celleb_fake_config_readl(void *addr)
75 return le32_to_cpu(*p);
78 static inline void celleb_fake_config_writeb(u32 val, void *addr)
84 static inline void celleb_fake_config_writew(u32 val, void *addr)
88 val16 = cpu_to_le16(val);
92 static inline void celleb_fake_config_writel(u32 val, void *addr)
96 val32 = cpu_to_le32(val);
100 static unsigned char *get_fake_config_start(struct pci_controller *hose,
103 struct celleb_pci_private *private = hose->private_data;
108 return private->fake_config[devno][fn];
111 static struct celleb_pci_resource *get_resource_start(
112 struct pci_controller *hose,
115 struct celleb_pci_private *private = hose->private_data;
120 return private->res[devno][fn];
124 static void celleb_config_read_fake(unsigned char *config, int where,
127 char *p = config + where;
131 *val = celleb_fake_config_readb(p);
134 *val = celleb_fake_config_readw(p);
137 *val = celleb_fake_config_readl(p);
144 static void celleb_config_write_fake(unsigned char *config, int where,
147 char *p = config + where;
151 celleb_fake_config_writeb(val, p);
154 celleb_fake_config_writew(val, p);
157 celleb_fake_config_writel(val, p);
163 static int celleb_fake_pci_read_config(struct pci_bus *bus,
164 unsigned int devfn, int where, int size, u32 *val)
167 struct device_node *node;
168 struct pci_controller *hose;
169 unsigned int devno = devfn >> 3;
170 unsigned int fn = devfn & 0x7;
172 /* allignment check */
173 BUG_ON(where % size);
175 pr_debug(" fake read: bus=0x%x, ", bus->number);
176 node = (struct device_node *)bus->sysdata;
177 hose = pci_find_hose_for_OF_device(node);
178 config = get_fake_config_start(hose, devno, fn);
180 pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
182 pr_debug("failed\n");
183 return PCIBIOS_DEVICE_NOT_FOUND;
186 celleb_config_read_fake(config, where, size, val);
187 pr_debug("val=0x%x\n", *val);
189 return PCIBIOS_SUCCESSFUL;
193 static int celleb_fake_pci_write_config(struct pci_bus *bus,
194 unsigned int devfn, int where, int size, u32 val)
197 struct device_node *node;
198 struct pci_controller *hose;
199 struct celleb_pci_resource *res;
200 unsigned int devno = devfn >> 3;
201 unsigned int fn = devfn & 0x7;
203 /* allignment check */
204 BUG_ON(where % size);
206 node = (struct device_node *)bus->sysdata;
207 hose = pci_find_hose_for_OF_device(node);
208 config = get_fake_config_start(hose, devno, fn);
211 return PCIBIOS_DEVICE_NOT_FOUND;
214 int i = (where - PCI_BASE_ADDRESS_0) >> 3;
217 case PCI_BASE_ADDRESS_0:
218 case PCI_BASE_ADDRESS_2:
220 return PCIBIOS_DEVICE_NOT_FOUND;
221 res = get_resource_start(hose, devno, fn);
223 return PCIBIOS_DEVICE_NOT_FOUND;
224 celleb_config_write_fake(config, where, size,
225 (res->r[i].end - res->r[i].start));
226 return PCIBIOS_SUCCESSFUL;
227 case PCI_BASE_ADDRESS_1:
228 case PCI_BASE_ADDRESS_3:
229 case PCI_BASE_ADDRESS_4:
230 case PCI_BASE_ADDRESS_5:
237 celleb_config_write_fake(config, where, size, val);
238 pr_debug(" fake write: where=%x, size=%d, val=%x\n",
241 return PCIBIOS_SUCCESSFUL;
244 static struct pci_ops celleb_fake_pci_ops = {
245 celleb_fake_pci_read_config,
246 celleb_fake_pci_write_config
249 static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
250 unsigned int devno, unsigned int fn,
251 unsigned int num_base_addr)
254 unsigned char *config;
255 struct celleb_pci_resource *res;
257 config = get_fake_config_start(hose, devno, fn);
258 res = get_resource_start(hose, devno, fn);
263 switch (num_base_addr) {
265 val = (res->r[2].start & 0xfffffff0)
266 | PCI_BASE_ADDRESS_MEM_TYPE_64;
267 celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
268 val = res->r[2].start >> 32;
269 celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
272 val = (res->r[1].start & 0xfffffff0)
273 | PCI_BASE_ADDRESS_MEM_TYPE_64;
274 celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
275 val = res->r[1].start >> 32;
276 celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
279 val = (res->r[0].start & 0xfffffff0)
280 | PCI_BASE_ADDRESS_MEM_TYPE_64;
281 celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
282 val = res->r[0].start >> 32;
283 celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
287 val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
288 celleb_config_write_fake(config, PCI_COMMAND, 2, val);
291 static int __devinit celleb_setup_fake_pci_device(struct device_node *node,
292 struct pci_controller *hose)
295 int num_base_addr = 0;
297 const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
298 unsigned int devno, fn;
299 struct celleb_pci_private *private = hose->private_data;
300 unsigned char **config = NULL;
301 struct celleb_pci_resource **res = NULL;
303 const unsigned long *li;
306 if (private == NULL) {
307 printk(KERN_ERR "PCI: "
308 "memory space for pci controller is not assigned\n");
312 name = get_property(node, "model", &rlen);
314 printk(KERN_ERR "PCI: model property not found.\n");
318 wi4 = get_property(node, "reg", &rlen);
322 devno = ((wi4[0] >> 8) & 0xff) >> 3;
323 fn = (wi4[0] >> 8) & 0x7;
325 pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
329 config = &private->fake_config[devno][fn];
331 *config = kzalloc(size, GFP_KERNEL);
333 *config = alloc_bootmem(size);
334 if (*config == NULL) {
335 printk(KERN_ERR "PCI: "
336 "not enough memory for fake configuration space\n");
339 pr_debug("PCI: fake config area assigned 0x%016lx\n",
340 (unsigned long)*config);
342 size = sizeof(struct celleb_pci_resource);
343 res = &private->res[devno][fn];
345 *res = kzalloc(size, GFP_KERNEL);
347 *res = alloc_bootmem(size);
350 "PCI: not enough memory for resource data space\n");
353 pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
355 wi0 = get_property(node, "device-id", NULL);
356 wi1 = get_property(node, "vendor-id", NULL);
357 wi2 = get_property(node, "class-code", NULL);
358 wi3 = get_property(node, "revision-id", NULL);
360 celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
361 celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
362 pr_debug("class-code = 0x%08x\n", wi2[0]);
364 celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
365 celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
366 (wi2[0] >> 8) & 0xffff);
367 celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
369 while (num_base_addr < MAX_PCI_BASE_ADDRS) {
370 result = of_address_to_resource(node,
371 num_base_addr, &(*res)->r[num_base_addr]);
377 celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
379 li = get_property(node, "interrupts", &rlen);
381 celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
382 celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
385 pr_debug("PCI: %s irq=%ld\n", name, li[0]);
386 for (i = 0; i < 6; i++) {
387 celleb_config_read_fake(*config,
388 PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
390 pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
395 celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
396 PCI_HEADER_TYPE_NORMAL);
402 if (config && *config)
408 if (config && *config) {
410 free_bootmem((unsigned long)(*config), size);
413 size = sizeof(struct celleb_pci_resource);
414 free_bootmem((unsigned long)(*res), size);
421 static int __devinit phb_set_bus_ranges(struct device_node *dev,
422 struct pci_controller *phb)
424 const int *bus_range;
427 bus_range = get_property(dev, "bus-range", &len);
428 if (bus_range == NULL || len < 2 * sizeof(int))
431 phb->first_busno = bus_range[0];
432 phb->last_busno = bus_range[1];
437 static void __devinit celleb_alloc_private_mem(struct pci_controller *hose)
441 kzalloc(sizeof(struct celleb_pci_private), GFP_KERNEL);
444 alloc_bootmem(sizeof(struct celleb_pci_private));
447 int __devinit celleb_setup_phb(struct pci_controller *phb)
450 struct device_node *dev = phb->arch_data;
451 struct device_node *node;
454 name = get_property(dev, "name", &rlen);
458 pr_debug("PCI: celleb_setup_phb() %s\n", name);
459 phb_set_bus_ranges(dev, phb);
461 if (strcmp(name, "epci") == 0) {
462 phb->ops = &celleb_epci_ops;
463 return celleb_setup_epci(dev, phb);
465 } else if (strcmp(name, "pci-pseudo") == 0) {
466 phb->ops = &celleb_fake_pci_ops;
467 celleb_alloc_private_mem(phb);
468 for (node = of_get_next_child(dev, NULL);
469 node != NULL; node = of_get_next_child(dev, node))
470 celleb_setup_fake_pci_device(node, phb);
478 int celleb_pci_probe_mode(struct pci_bus *bus)
480 return PCI_PROBE_DEVTREE;