Path: blob/master/arch/powerpc/platforms/cell/celleb_pci.c
10818 views
/*1* Support for PCI on Celleb platform.2*3* (C) Copyright 2006-2007 TOSHIBA CORPORATION4*5* This code is based on arch/powerpc/kernel/rtas_pci.c:6* Copyright (C) 2001 Dave Engebretsen, IBM Corporation7* Copyright (C) 2003 Anton Blanchard <[email protected]>, IBM8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17* GNU General Public License for more details.18*19* You should have received a copy of the GNU General Public License along20* with this program; if not, write to the Free Software Foundation, Inc.,21* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.22*/2324#undef DEBUG2526#include <linux/kernel.h>27#include <linux/threads.h>28#include <linux/pci.h>29#include <linux/string.h>30#include <linux/init.h>31#include <linux/bootmem.h>32#include <linux/pci_regs.h>33#include <linux/of.h>34#include <linux/of_device.h>35#include <linux/slab.h>3637#include <asm/io.h>38#include <asm/irq.h>39#include <asm/prom.h>40#include <asm/pci-bridge.h>41#include <asm/ppc-pci.h>4243#include "celleb_pci.h"4445#define MAX_PCI_DEVICES 3246#define MAX_PCI_FUNCTIONS 847#define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */4849/* definition for fake pci configuration area for GbE, .... ,and etc. */5051struct celleb_pci_resource {52struct resource r[MAX_PCI_BASE_ADDRS];53};5455struct celleb_pci_private {56unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];57struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];58};5960static inline u8 celleb_fake_config_readb(void *addr)61{62u8 *p = addr;63return *p;64}6566static inline u16 celleb_fake_config_readw(void *addr)67{68__le16 *p = addr;69return le16_to_cpu(*p);70}7172static inline u32 celleb_fake_config_readl(void *addr)73{74__le32 *p = addr;75return le32_to_cpu(*p);76}7778static inline void celleb_fake_config_writeb(u32 val, void *addr)79{80u8 *p = addr;81*p = val;82}8384static inline void celleb_fake_config_writew(u32 val, void *addr)85{86__le16 val16;87__le16 *p = addr;88val16 = cpu_to_le16(val);89*p = val16;90}9192static inline void celleb_fake_config_writel(u32 val, void *addr)93{94__le32 val32;95__le32 *p = addr;96val32 = cpu_to_le32(val);97*p = val32;98}99100static unsigned char *get_fake_config_start(struct pci_controller *hose,101int devno, int fn)102{103struct celleb_pci_private *private = hose->private_data;104105if (private == NULL)106return NULL;107108return private->fake_config[devno][fn];109}110111static struct celleb_pci_resource *get_resource_start(112struct pci_controller *hose,113int devno, int fn)114{115struct celleb_pci_private *private = hose->private_data;116117if (private == NULL)118return NULL;119120return private->res[devno][fn];121}122123124static void celleb_config_read_fake(unsigned char *config, int where,125int size, u32 *val)126{127char *p = config + where;128129switch (size) {130case 1:131*val = celleb_fake_config_readb(p);132break;133case 2:134*val = celleb_fake_config_readw(p);135break;136case 4:137*val = celleb_fake_config_readl(p);138break;139}140}141142static void celleb_config_write_fake(unsigned char *config, int where,143int size, u32 val)144{145char *p = config + where;146147switch (size) {148case 1:149celleb_fake_config_writeb(val, p);150break;151case 2:152celleb_fake_config_writew(val, p);153break;154case 4:155celleb_fake_config_writel(val, p);156break;157}158}159160static int celleb_fake_pci_read_config(struct pci_bus *bus,161unsigned int devfn, int where, int size, u32 *val)162{163char *config;164struct pci_controller *hose = pci_bus_to_host(bus);165unsigned int devno = devfn >> 3;166unsigned int fn = devfn & 0x7;167168/* allignment check */169BUG_ON(where % size);170171pr_debug(" fake read: bus=0x%x, ", bus->number);172config = get_fake_config_start(hose, devno, fn);173174pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);175if (!config) {176pr_debug("failed\n");177return PCIBIOS_DEVICE_NOT_FOUND;178}179180celleb_config_read_fake(config, where, size, val);181pr_debug("val=0x%x\n", *val);182183return PCIBIOS_SUCCESSFUL;184}185186187static int celleb_fake_pci_write_config(struct pci_bus *bus,188unsigned int devfn, int where, int size, u32 val)189{190char *config;191struct pci_controller *hose = pci_bus_to_host(bus);192struct celleb_pci_resource *res;193unsigned int devno = devfn >> 3;194unsigned int fn = devfn & 0x7;195196/* allignment check */197BUG_ON(where % size);198199config = get_fake_config_start(hose, devno, fn);200201if (!config)202return PCIBIOS_DEVICE_NOT_FOUND;203204if (val == ~0) {205int i = (where - PCI_BASE_ADDRESS_0) >> 3;206207switch (where) {208case PCI_BASE_ADDRESS_0:209case PCI_BASE_ADDRESS_2:210if (size != 4)211return PCIBIOS_DEVICE_NOT_FOUND;212res = get_resource_start(hose, devno, fn);213if (!res)214return PCIBIOS_DEVICE_NOT_FOUND;215celleb_config_write_fake(config, where, size,216(res->r[i].end - res->r[i].start));217return PCIBIOS_SUCCESSFUL;218case PCI_BASE_ADDRESS_1:219case PCI_BASE_ADDRESS_3:220case PCI_BASE_ADDRESS_4:221case PCI_BASE_ADDRESS_5:222break;223default:224break;225}226}227228celleb_config_write_fake(config, where, size, val);229pr_debug(" fake write: where=%x, size=%d, val=%x\n",230where, size, val);231232return PCIBIOS_SUCCESSFUL;233}234235static struct pci_ops celleb_fake_pci_ops = {236.read = celleb_fake_pci_read_config,237.write = celleb_fake_pci_write_config,238};239240static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,241unsigned int devno, unsigned int fn,242unsigned int num_base_addr)243{244u32 val;245unsigned char *config;246struct celleb_pci_resource *res;247248config = get_fake_config_start(hose, devno, fn);249res = get_resource_start(hose, devno, fn);250251if (!config || !res)252return;253254switch (num_base_addr) {255case 3:256val = (res->r[2].start & 0xfffffff0)257| PCI_BASE_ADDRESS_MEM_TYPE_64;258celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);259val = res->r[2].start >> 32;260celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);261/* FALLTHROUGH */262case 2:263val = (res->r[1].start & 0xfffffff0)264| PCI_BASE_ADDRESS_MEM_TYPE_64;265celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);266val = res->r[1].start >> 32;267celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);268/* FALLTHROUGH */269case 1:270val = (res->r[0].start & 0xfffffff0)271| PCI_BASE_ADDRESS_MEM_TYPE_64;272celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);273val = res->r[0].start >> 32;274celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);275break;276}277278val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;279celleb_config_write_fake(config, PCI_COMMAND, 2, val);280}281282static int __init celleb_setup_fake_pci_device(struct device_node *node,283struct pci_controller *hose)284{285unsigned int rlen;286int num_base_addr = 0;287u32 val;288const u32 *wi0, *wi1, *wi2, *wi3, *wi4;289unsigned int devno, fn;290struct celleb_pci_private *private = hose->private_data;291unsigned char **config = NULL;292struct celleb_pci_resource **res = NULL;293const char *name;294const unsigned long *li;295int size, result;296297if (private == NULL) {298printk(KERN_ERR "PCI: "299"memory space for pci controller is not assigned\n");300goto error;301}302303name = of_get_property(node, "model", &rlen);304if (!name) {305printk(KERN_ERR "PCI: model property not found.\n");306goto error;307}308309wi4 = of_get_property(node, "reg", &rlen);310if (wi4 == NULL)311goto error;312313devno = ((wi4[0] >> 8) & 0xff) >> 3;314fn = (wi4[0] >> 8) & 0x7;315316pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,317devno, fn);318319size = 256;320config = &private->fake_config[devno][fn];321*config = zalloc_maybe_bootmem(size, GFP_KERNEL);322if (*config == NULL) {323printk(KERN_ERR "PCI: "324"not enough memory for fake configuration space\n");325goto error;326}327pr_debug("PCI: fake config area assigned 0x%016lx\n",328(unsigned long)*config);329330size = sizeof(struct celleb_pci_resource);331res = &private->res[devno][fn];332*res = zalloc_maybe_bootmem(size, GFP_KERNEL);333if (*res == NULL) {334printk(KERN_ERR335"PCI: not enough memory for resource data space\n");336goto error;337}338pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);339340wi0 = of_get_property(node, "device-id", NULL);341wi1 = of_get_property(node, "vendor-id", NULL);342wi2 = of_get_property(node, "class-code", NULL);343wi3 = of_get_property(node, "revision-id", NULL);344if (!wi0 || !wi1 || !wi2 || !wi3) {345printk(KERN_ERR "PCI: Missing device tree properties.\n");346goto error;347}348349celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);350celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);351pr_debug("class-code = 0x%08x\n", wi2[0]);352353celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);354celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,355(wi2[0] >> 8) & 0xffff);356celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);357358while (num_base_addr < MAX_PCI_BASE_ADDRS) {359result = of_address_to_resource(node,360num_base_addr, &(*res)->r[num_base_addr]);361if (result)362break;363num_base_addr++;364}365366celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);367368li = of_get_property(node, "interrupts", &rlen);369if (!li) {370printk(KERN_ERR "PCI: interrupts not found.\n");371goto error;372}373val = li[0];374celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);375celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);376377#ifdef DEBUG378pr_debug("PCI: %s irq=%ld\n", name, li[0]);379for (i = 0; i < 6; i++) {380celleb_config_read_fake(*config,381PCI_BASE_ADDRESS_0 + 0x4 * i, 4,382&val);383pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",384name, fn, i, val);385}386#endif387388celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,389PCI_HEADER_TYPE_NORMAL);390391return 0;392393error:394if (mem_init_done) {395if (config && *config)396kfree(*config);397if (res && *res)398kfree(*res);399400} else {401if (config && *config) {402size = 256;403free_bootmem((unsigned long)(*config), size);404}405if (res && *res) {406size = sizeof(struct celleb_pci_resource);407free_bootmem((unsigned long)(*res), size);408}409}410411return 1;412}413414static int __init phb_set_bus_ranges(struct device_node *dev,415struct pci_controller *phb)416{417const int *bus_range;418unsigned int len;419420bus_range = of_get_property(dev, "bus-range", &len);421if (bus_range == NULL || len < 2 * sizeof(int))422return 1;423424phb->first_busno = bus_range[0];425phb->last_busno = bus_range[1];426427return 0;428}429430static void __init celleb_alloc_private_mem(struct pci_controller *hose)431{432hose->private_data =433zalloc_maybe_bootmem(sizeof(struct celleb_pci_private),434GFP_KERNEL);435}436437static int __init celleb_setup_fake_pci(struct device_node *dev,438struct pci_controller *phb)439{440struct device_node *node;441442phb->ops = &celleb_fake_pci_ops;443celleb_alloc_private_mem(phb);444445for (node = of_get_next_child(dev, NULL);446node != NULL; node = of_get_next_child(dev, node))447celleb_setup_fake_pci_device(node, phb);448449return 0;450}451452static struct celleb_phb_spec celleb_fake_pci_spec __initdata = {453.setup = celleb_setup_fake_pci,454};455456static struct of_device_id celleb_phb_match[] __initdata = {457{458.name = "pci-pseudo",459.data = &celleb_fake_pci_spec,460}, {461.name = "epci",462.data = &celleb_epci_spec,463}, {464.name = "pcie",465.data = &celleb_pciex_spec,466}, {467},468};469470int __init celleb_setup_phb(struct pci_controller *phb)471{472struct device_node *dev = phb->dn;473const struct of_device_id *match;474struct celleb_phb_spec *phb_spec;475int rc;476477match = of_match_node(celleb_phb_match, dev);478if (!match)479return 1;480481phb_set_bus_ranges(dev, phb);482phb->buid = 1;483484phb_spec = match->data;485rc = (*phb_spec->setup)(dev, phb);486if (rc)487return 1;488489if (phb_spec->ops)490iowa_register_bus(phb, phb_spec->ops,491phb_spec->iowa_init,492phb_spec->iowa_data);493return 0;494}495496int celleb_pci_probe_mode(struct pci_bus *bus)497{498return PCI_PROBE_DEVTREE;499}500501502