xref: /linux/arch/powerpc/platforms/pasemi/pci.c (revision 68f211a4d1e5882e881bbb84b6505908dde5f625)
11e76875eSOlof Johansson /*
21e76875eSOlof Johansson  * Copyright (C) 2006 PA Semi, Inc
31e76875eSOlof Johansson  *
41e76875eSOlof Johansson  * Authors: Kip Walker, PA Semi
51e76875eSOlof Johansson  *	    Olof Johansson, PA Semi
61e76875eSOlof Johansson  *
71e76875eSOlof Johansson  * Maintained by: Olof Johansson <olof@lixom.net>
81e76875eSOlof Johansson  *
91e76875eSOlof Johansson  * Based on arch/powerpc/platforms/maple/pci.c
101e76875eSOlof Johansson  *
111e76875eSOlof Johansson  * This program is free software; you can redistribute it and/or modify
121e76875eSOlof Johansson  * it under the terms of the GNU General Public License version 2 as
131e76875eSOlof Johansson  * published by the Free Software Foundation.
141e76875eSOlof Johansson  *
151e76875eSOlof Johansson  * This program is distributed in the hope that it will be useful,
161e76875eSOlof Johansson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
171e76875eSOlof Johansson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181e76875eSOlof Johansson  * GNU General Public License for more details.
191e76875eSOlof Johansson  *
201e76875eSOlof Johansson  * You should have received a copy of the GNU General Public License
211e76875eSOlof Johansson  * along with this program; if not, write to the Free Software
221e76875eSOlof Johansson  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
231e76875eSOlof Johansson  */
241e76875eSOlof Johansson 
251e76875eSOlof Johansson 
261e76875eSOlof Johansson #include <linux/kernel.h>
271e76875eSOlof Johansson #include <linux/pci.h>
281e76875eSOlof Johansson 
291e76875eSOlof Johansson #include <asm/pci-bridge.h>
301e76875eSOlof Johansson #include <asm/machdep.h>
311e76875eSOlof Johansson 
321e76875eSOlof Johansson #include <asm/ppc-pci.h>
331e76875eSOlof Johansson 
34d28a0d94SDaniel Axtens #include "pasemi.h"
35d28a0d94SDaniel Axtens 
361e76875eSOlof Johansson #define PA_PXP_CFA(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
371e76875eSOlof Johansson 
38df7e70a2SOlof Johansson static inline int pa_pxp_offset_valid(u8 bus, u8 devfn, int offset)
39df7e70a2SOlof Johansson {
40df7e70a2SOlof Johansson 	/* Device 0 Function 0 is special: It's config space spans function 1 as
41df7e70a2SOlof Johansson 	 * well, so allow larger offset. It's really a two-function device but the
42df7e70a2SOlof Johansson 	 * second function does not probe.
43df7e70a2SOlof Johansson 	 */
44df7e70a2SOlof Johansson 	if (bus == 0 && devfn == 0)
45df7e70a2SOlof Johansson 		return offset < 8192;
46df7e70a2SOlof Johansson 	else
47df7e70a2SOlof Johansson 		return offset < 4096;
48df7e70a2SOlof Johansson }
491e76875eSOlof Johansson 
507c84ace9SAl Viro static void volatile __iomem *pa_pxp_cfg_addr(struct pci_controller *hose,
511e76875eSOlof Johansson 				       u8 bus, u8 devfn, int offset)
521e76875eSOlof Johansson {
537c84ace9SAl Viro 	return hose->cfg_data + PA_PXP_CFA(bus, devfn, offset);
541e76875eSOlof Johansson }
551e76875eSOlof Johansson 
564d442331SOlof Johansson static inline int is_root_port(int busno, int devfn)
574d442331SOlof Johansson {
584d442331SOlof Johansson 	return ((busno == 0) && (PCI_FUNC(devfn) < 4) &&
594d442331SOlof Johansson 		 ((PCI_SLOT(devfn) == 16) || (PCI_SLOT(devfn) == 17)));
604d442331SOlof Johansson }
614d442331SOlof Johansson 
624d442331SOlof Johansson static inline int is_5945_reg(int reg)
634d442331SOlof Johansson {
644d442331SOlof Johansson 	return (((reg >= 0x18) && (reg < 0x34)) ||
654d442331SOlof Johansson 		((reg >= 0x158) && (reg < 0x178)));
664d442331SOlof Johansson }
674d442331SOlof Johansson 
684d442331SOlof Johansson static int workaround_5945(struct pci_bus *bus, unsigned int devfn,
694d442331SOlof Johansson 			   int offset, int len, u32 *val)
704d442331SOlof Johansson {
714d442331SOlof Johansson 	struct pci_controller *hose;
724d442331SOlof Johansson 	void volatile __iomem *addr, *dummy;
734d442331SOlof Johansson 	int byte;
744d442331SOlof Johansson 	u32 tmp;
754d442331SOlof Johansson 
764d442331SOlof Johansson 	if (!is_root_port(bus->number, devfn) || !is_5945_reg(offset))
774d442331SOlof Johansson 		return 0;
784d442331SOlof Johansson 
794d442331SOlof Johansson 	hose = pci_bus_to_host(bus);
804d442331SOlof Johansson 
814d442331SOlof Johansson 	addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset & ~0x3);
824d442331SOlof Johansson 	byte = offset & 0x3;
834d442331SOlof Johansson 
844d442331SOlof Johansson 	/* Workaround bug 5945: write 0 to a dummy register before reading,
854d442331SOlof Johansson 	 * and write back what we read. We must read/write the full 32-bit
864d442331SOlof Johansson 	 * contents so we need to shift and mask by hand.
874d442331SOlof Johansson 	 */
884d442331SOlof Johansson 	dummy = pa_pxp_cfg_addr(hose, bus->number, devfn, 0x10);
894d442331SOlof Johansson 	out_le32(dummy, 0);
904d442331SOlof Johansson 	tmp = in_le32(addr);
914d442331SOlof Johansson 	out_le32(addr, tmp);
924d442331SOlof Johansson 
934d442331SOlof Johansson 	switch (len) {
944d442331SOlof Johansson 	case 1:
954d442331SOlof Johansson 		*val = (tmp >> (8*byte)) & 0xff;
964d442331SOlof Johansson 		break;
974d442331SOlof Johansson 	case 2:
984d442331SOlof Johansson 		if (byte == 0)
994d442331SOlof Johansson 			*val = tmp & 0xffff;
1004d442331SOlof Johansson 		else
1014d442331SOlof Johansson 			*val = (tmp >> 16) & 0xffff;
1024d442331SOlof Johansson 		break;
1034d442331SOlof Johansson 	default:
1044d442331SOlof Johansson 		*val = tmp;
1054d442331SOlof Johansson 		break;
1064d442331SOlof Johansson 	}
1074d442331SOlof Johansson 
1084d442331SOlof Johansson 	return 1;
1094d442331SOlof Johansson }
1104d442331SOlof Johansson 
111*68f211a4SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
112*68f211a4SDarren Stevens #define PXP_ERR_CFG_REG	0x4
113*68f211a4SDarren Stevens #define PXP_IGNORE_PCIE_ERRORS	0x800
114*68f211a4SDarren Stevens #define SB600_BUS 5
115*68f211a4SDarren Stevens 
116*68f211a4SDarren Stevens static void sb600_set_flag(int bus)
117*68f211a4SDarren Stevens {
118*68f211a4SDarren Stevens 	static void __iomem *iob_mapbase = NULL;
119*68f211a4SDarren Stevens 	struct resource res;
120*68f211a4SDarren Stevens 	struct device_node *dn;
121*68f211a4SDarren Stevens 	int err;
122*68f211a4SDarren Stevens 
123*68f211a4SDarren Stevens 	if (iob_mapbase == NULL) {
124*68f211a4SDarren Stevens 		dn = of_find_compatible_node(NULL, "isa", "pasemi,1682m-iob");
125*68f211a4SDarren Stevens 		if (!dn) {
126*68f211a4SDarren Stevens 			pr_crit("NEMO SB600 missing iob node\n");
127*68f211a4SDarren Stevens 			return;
128*68f211a4SDarren Stevens 		}
129*68f211a4SDarren Stevens 
130*68f211a4SDarren Stevens 		err = of_address_to_resource(dn, 0, &res);
131*68f211a4SDarren Stevens 		of_node_put(dn);
132*68f211a4SDarren Stevens 
133*68f211a4SDarren Stevens 		if (err) {
134*68f211a4SDarren Stevens 			pr_crit("NEMO SB600 missing resource\n");
135*68f211a4SDarren Stevens 			return;
136*68f211a4SDarren Stevens 		}
137*68f211a4SDarren Stevens 
138*68f211a4SDarren Stevens 		pr_info("NEMO SB600 IOB base %08llx\n",res.start);
139*68f211a4SDarren Stevens 
140*68f211a4SDarren Stevens 		iob_mapbase = ioremap(res.start + 0x100, 0x94);
141*68f211a4SDarren Stevens 	}
142*68f211a4SDarren Stevens 
143*68f211a4SDarren Stevens 	if (iob_mapbase != NULL) {
144*68f211a4SDarren Stevens 		if (bus == SB600_BUS) {
145*68f211a4SDarren Stevens 			/*
146*68f211a4SDarren Stevens 			 * This is the SB600's bus, tell the PCI-e root port
147*68f211a4SDarren Stevens 			 * to allow non-zero devices to enumerate.
148*68f211a4SDarren Stevens 			 */
149*68f211a4SDarren Stevens 			out_le32(iob_mapbase + PXP_ERR_CFG_REG, in_le32(iob_mapbase + PXP_ERR_CFG_REG) | PXP_IGNORE_PCIE_ERRORS);
150*68f211a4SDarren Stevens 		} else {
151*68f211a4SDarren Stevens 			/*
152*68f211a4SDarren Stevens 			 * Only scan device 0 on other busses
153*68f211a4SDarren Stevens 			 */
154*68f211a4SDarren Stevens 			out_le32(iob_mapbase + PXP_ERR_CFG_REG, in_le32(iob_mapbase + PXP_ERR_CFG_REG) & ~PXP_IGNORE_PCIE_ERRORS);
155*68f211a4SDarren Stevens 		}
156*68f211a4SDarren Stevens 	}
157*68f211a4SDarren Stevens }
158*68f211a4SDarren Stevens 
159*68f211a4SDarren Stevens #else
160*68f211a4SDarren Stevens 
161*68f211a4SDarren Stevens static void sb600_set_flag(int bus)
162*68f211a4SDarren Stevens {
163*68f211a4SDarren Stevens }
164*68f211a4SDarren Stevens #endif
165*68f211a4SDarren Stevens 
1661e76875eSOlof Johansson static int pa_pxp_read_config(struct pci_bus *bus, unsigned int devfn,
1671e76875eSOlof Johansson 			      int offset, int len, u32 *val)
1681e76875eSOlof Johansson {
1691e76875eSOlof Johansson 	struct pci_controller *hose;
1707c84ace9SAl Viro 	void volatile __iomem *addr;
1711e76875eSOlof Johansson 
1721e76875eSOlof Johansson 	hose = pci_bus_to_host(bus);
1731e76875eSOlof Johansson 	if (!hose)
1741e76875eSOlof Johansson 		return PCIBIOS_DEVICE_NOT_FOUND;
1751e76875eSOlof Johansson 
176df7e70a2SOlof Johansson 	if (!pa_pxp_offset_valid(bus->number, devfn, offset))
1771e76875eSOlof Johansson 		return PCIBIOS_BAD_REGISTER_NUMBER;
1781e76875eSOlof Johansson 
1794d442331SOlof Johansson 	if (workaround_5945(bus, devfn, offset, len, val))
1804d442331SOlof Johansson 		return PCIBIOS_SUCCESSFUL;
1814d442331SOlof Johansson 
1821e76875eSOlof Johansson 	addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
1831e76875eSOlof Johansson 
1841e76875eSOlof Johansson 	/*
1851e76875eSOlof Johansson 	 * Note: the caller has already checked that offset is
1861e76875eSOlof Johansson 	 * suitably aligned and that len is 1, 2 or 4.
1871e76875eSOlof Johansson 	 */
1881e76875eSOlof Johansson 	switch (len) {
1891e76875eSOlof Johansson 	case 1:
1907c84ace9SAl Viro 		*val = in_8(addr);
1911e76875eSOlof Johansson 		break;
1921e76875eSOlof Johansson 	case 2:
1937c84ace9SAl Viro 		*val = in_le16(addr);
1941e76875eSOlof Johansson 		break;
1951e76875eSOlof Johansson 	default:
1967c84ace9SAl Viro 		*val = in_le32(addr);
1971e76875eSOlof Johansson 		break;
1981e76875eSOlof Johansson 	}
1991e76875eSOlof Johansson 
2001e76875eSOlof Johansson 	return PCIBIOS_SUCCESSFUL;
2011e76875eSOlof Johansson }
2021e76875eSOlof Johansson 
2031e76875eSOlof Johansson static int pa_pxp_write_config(struct pci_bus *bus, unsigned int devfn,
2041e76875eSOlof Johansson 			       int offset, int len, u32 val)
2051e76875eSOlof Johansson {
2061e76875eSOlof Johansson 	struct pci_controller *hose;
2077c84ace9SAl Viro 	void volatile __iomem *addr;
2081e76875eSOlof Johansson 
2091e76875eSOlof Johansson 	hose = pci_bus_to_host(bus);
2101e76875eSOlof Johansson 	if (!hose)
2111e76875eSOlof Johansson 		return PCIBIOS_DEVICE_NOT_FOUND;
2121e76875eSOlof Johansson 
213df7e70a2SOlof Johansson 	if (!pa_pxp_offset_valid(bus->number, devfn, offset))
2141e76875eSOlof Johansson 		return PCIBIOS_BAD_REGISTER_NUMBER;
2151e76875eSOlof Johansson 
2161e76875eSOlof Johansson 	addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
2171e76875eSOlof Johansson 
2181e76875eSOlof Johansson 	/*
2191e76875eSOlof Johansson 	 * Note: the caller has already checked that offset is
2201e76875eSOlof Johansson 	 * suitably aligned and that len is 1, 2 or 4.
2211e76875eSOlof Johansson 	 */
2221e76875eSOlof Johansson 	switch (len) {
2231e76875eSOlof Johansson 	case 1:
2247c84ace9SAl Viro 		out_8(addr, val);
2251e76875eSOlof Johansson 		break;
2261e76875eSOlof Johansson 	case 2:
2277c84ace9SAl Viro 		out_le16(addr, val);
2281e76875eSOlof Johansson 		break;
2291e76875eSOlof Johansson 	default:
2307c84ace9SAl Viro 		out_le32(addr, val);
2311e76875eSOlof Johansson 		break;
2321e76875eSOlof Johansson 	}
2331e76875eSOlof Johansson 	return PCIBIOS_SUCCESSFUL;
2341e76875eSOlof Johansson }
2351e76875eSOlof Johansson 
2361e76875eSOlof Johansson static struct pci_ops pa_pxp_ops = {
2371bb8c621SNathan Lynch 	.read = pa_pxp_read_config,
2381bb8c621SNathan Lynch 	.write = pa_pxp_write_config,
2391e76875eSOlof Johansson };
2401e76875eSOlof Johansson 
2411e76875eSOlof Johansson static void __init setup_pa_pxp(struct pci_controller *hose)
2421e76875eSOlof Johansson {
2431e76875eSOlof Johansson 	hose->ops = &pa_pxp_ops;
2441e76875eSOlof Johansson 	hose->cfg_data = ioremap(0xe0000000, 0x10000000);
2451e76875eSOlof Johansson }
2461e76875eSOlof Johansson 
24709b55f76SArnd Bergmann static int __init pas_add_bridge(struct device_node *dev)
2481e76875eSOlof Johansson {
2491e76875eSOlof Johansson 	struct pci_controller *hose;
2501e76875eSOlof Johansson 
251b7c670d6SRob Herring 	pr_debug("Adding PCI host bridge %pOF\n", dev);
2521e76875eSOlof Johansson 
2531e76875eSOlof Johansson 	hose = pcibios_alloc_controller(dev);
2541e76875eSOlof Johansson 	if (!hose)
2551e76875eSOlof Johansson 		return -ENOMEM;
2561e76875eSOlof Johansson 
2571e76875eSOlof Johansson 	hose->first_busno = 0;
2581e76875eSOlof Johansson 	hose->last_busno = 0xff;
259d28a0d94SDaniel Axtens 	hose->controller_ops = pasemi_pci_controller_ops;
2601e76875eSOlof Johansson 
2611e76875eSOlof Johansson 	setup_pa_pxp(hose);
2621e76875eSOlof Johansson 
263e13606d7SDarren Stevens 	pr_info("Found PA-PXP PCI host bridge.\n");
2641e76875eSOlof Johansson 
2651e76875eSOlof Johansson 	/* Interpret the "ranges" property */
2661e76875eSOlof Johansson 	pci_process_bridge_OF_ranges(hose, dev, 1);
2671e76875eSOlof Johansson 
2681e76875eSOlof Johansson 	return 0;
2691e76875eSOlof Johansson }
2701e76875eSOlof Johansson 
2711e76875eSOlof Johansson void __init pas_pci_init(void)
2721e76875eSOlof Johansson {
2731e76875eSOlof Johansson 	struct device_node *np, *root;
274250a9350SDarren Stevens 	int res;
2751e76875eSOlof Johansson 
2761e76875eSOlof Johansson 	root = of_find_node_by_path("/");
2771e76875eSOlof Johansson 	if (!root) {
278e13606d7SDarren Stevens 		pr_crit("pas_pci_init: can't find root of device tree\n");
2791e76875eSOlof Johansson 		return;
2801e76875eSOlof Johansson 	}
2811e76875eSOlof Johansson 
282eff06ef0SOlof Johansson 	pci_set_flags(PCI_SCAN_ALL_PCIE_DEVS);
283eff06ef0SOlof Johansson 
284250a9350SDarren Stevens 	np = of_find_compatible_node(root, NULL, "pasemi,rootbus");
285250a9350SDarren Stevens 	if (np) {
286250a9350SDarren Stevens 		res = pas_add_bridge(np);
287250a9350SDarren Stevens 		of_node_put(np);
288250a9350SDarren Stevens 	}
2891e76875eSOlof Johansson }
29068c8404cSOlof Johansson 
29168c8404cSOlof Johansson void __iomem *pasemi_pci_getcfgaddr(struct pci_dev *dev, int offset)
29268c8404cSOlof Johansson {
29368c8404cSOlof Johansson 	struct pci_controller *hose;
29468c8404cSOlof Johansson 
2954d442331SOlof Johansson 	hose = pci_bus_to_host(dev->bus);
29668c8404cSOlof Johansson 
2974d442331SOlof Johansson 	return (void __iomem *)pa_pxp_cfg_addr(hose, dev->bus->number, dev->devfn, offset);
29868c8404cSOlof Johansson }
299d28a0d94SDaniel Axtens 
300d28a0d94SDaniel Axtens struct pci_controller_ops pasemi_pci_controller_ops;
301