xref: /openbmc/linux/arch/mips/pci/pci-alchemy.c (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27517de34SManuel Lauss /*
37517de34SManuel Lauss  * Alchemy PCI host mode support.
47517de34SManuel Lauss  *
57517de34SManuel Lauss  * Copyright 2001-2003, 2007-2008 MontaVista Software Inc.
67517de34SManuel Lauss  * Author: MontaVista Software, Inc. <source@mvista.com>
77517de34SManuel Lauss  *
87517de34SManuel Lauss  * Support for all devices (greater than 16) added by David Gathright.
97517de34SManuel Lauss  */
107517de34SManuel Lauss 
118e211705SManuel Lauss #include <linux/clk.h>
1271ca8693SRalf Baechle #include <linux/export.h>
137517de34SManuel Lauss #include <linux/types.h>
147517de34SManuel Lauss #include <linux/pci.h>
157517de34SManuel Lauss #include <linux/platform_device.h>
167517de34SManuel Lauss #include <linux/kernel.h>
177517de34SManuel Lauss #include <linux/init.h>
18864c6c22SManuel Lauss #include <linux/syscore_ops.h>
197517de34SManuel Lauss #include <linux/vmalloc.h>
20*6d4e9a8eSChristoph Hellwig #include <linux/dma-map-ops.h> /* for dma_default_coherent */
217517de34SManuel Lauss 
227517de34SManuel Lauss #include <asm/mach-au1x00/au1000.h>
233d18c983SRalf Baechle #include <asm/tlbmisc.h>
247517de34SManuel Lauss 
25143f0f65SPaul Bolle #ifdef CONFIG_PCI_DEBUG
267517de34SManuel Lauss #define DBG(x...) printk(KERN_DEBUG x)
277517de34SManuel Lauss #else
287517de34SManuel Lauss #define DBG(x...) do {} while (0)
297517de34SManuel Lauss #endif
307517de34SManuel Lauss 
317517de34SManuel Lauss #define PCI_ACCESS_READ		0
327517de34SManuel Lauss #define PCI_ACCESS_WRITE	1
337517de34SManuel Lauss 
347517de34SManuel Lauss struct alchemy_pci_context {
357517de34SManuel Lauss 	struct pci_controller alchemy_pci_ctrl; /* leave as first member! */
367517de34SManuel Lauss 	void __iomem *regs;			/* ctrl base */
377517de34SManuel Lauss 	/* tools for wired entry for config space access */
387517de34SManuel Lauss 	unsigned long last_elo0;
397517de34SManuel Lauss 	unsigned long last_elo1;
407517de34SManuel Lauss 	int wired_entry;
417517de34SManuel Lauss 	struct vm_struct *pci_cfg_vm;
427517de34SManuel Lauss 
437517de34SManuel Lauss 	unsigned long pm[12];
447517de34SManuel Lauss 
457517de34SManuel Lauss 	int (*board_map_irq)(const struct pci_dev *d, u8 slot, u8 pin);
467517de34SManuel Lauss 	int (*board_pci_idsel)(unsigned int devsel, int assert);
477517de34SManuel Lauss };
487517de34SManuel Lauss 
49864c6c22SManuel Lauss /* for syscore_ops. There's only one PCI controller on Alchemy chips, so this
50864c6c22SManuel Lauss  * should suffice for now.
51864c6c22SManuel Lauss  */
52864c6c22SManuel Lauss static struct alchemy_pci_context *__alchemy_pci_ctx;
53864c6c22SManuel Lauss 
54864c6c22SManuel Lauss 
55d3991572SChristoph Hellwig /* IO/MEM resources for PCI. Keep the memres in sync with fixup_bigphys_addr
567517de34SManuel Lauss  * in arch/mips/alchemy/common/setup.c
577517de34SManuel Lauss  */
587517de34SManuel Lauss static struct resource alchemy_pci_def_memres = {
597517de34SManuel Lauss 	.start	= ALCHEMY_PCI_MEMWIN_START,
607517de34SManuel Lauss 	.end	= ALCHEMY_PCI_MEMWIN_END,
617517de34SManuel Lauss 	.name	= "PCI memory space",
627517de34SManuel Lauss 	.flags	= IORESOURCE_MEM
637517de34SManuel Lauss };
647517de34SManuel Lauss 
657517de34SManuel Lauss static struct resource alchemy_pci_def_iores = {
667517de34SManuel Lauss 	.start	= ALCHEMY_PCI_IOWIN_START,
677517de34SManuel Lauss 	.end	= ALCHEMY_PCI_IOWIN_END,
687517de34SManuel Lauss 	.name	= "PCI IO space",
697517de34SManuel Lauss 	.flags	= IORESOURCE_IO
707517de34SManuel Lauss };
717517de34SManuel Lauss 
mod_wired_entry(int entry,unsigned long entrylo0,unsigned long entrylo1,unsigned long entryhi,unsigned long pagemask)727517de34SManuel Lauss static void mod_wired_entry(int entry, unsigned long entrylo0,
737517de34SManuel Lauss 		unsigned long entrylo1, unsigned long entryhi,
747517de34SManuel Lauss 		unsigned long pagemask)
757517de34SManuel Lauss {
767517de34SManuel Lauss 	unsigned long old_pagemask;
777517de34SManuel Lauss 	unsigned long old_ctx;
787517de34SManuel Lauss 
797517de34SManuel Lauss 	/* Save old context and create impossible VPN2 value */
809b5c3399SJames Hogan 	old_ctx = read_c0_entryhi() & MIPS_ENTRYHI_ASID;
817517de34SManuel Lauss 	old_pagemask = read_c0_pagemask();
827517de34SManuel Lauss 	write_c0_index(entry);
837517de34SManuel Lauss 	write_c0_pagemask(pagemask);
847517de34SManuel Lauss 	write_c0_entryhi(entryhi);
857517de34SManuel Lauss 	write_c0_entrylo0(entrylo0);
867517de34SManuel Lauss 	write_c0_entrylo1(entrylo1);
877517de34SManuel Lauss 	tlb_write_indexed();
887517de34SManuel Lauss 	write_c0_entryhi(old_ctx);
897517de34SManuel Lauss 	write_c0_pagemask(old_pagemask);
907517de34SManuel Lauss }
917517de34SManuel Lauss 
alchemy_pci_wired_entry(struct alchemy_pci_context * ctx)927517de34SManuel Lauss static void alchemy_pci_wired_entry(struct alchemy_pci_context *ctx)
937517de34SManuel Lauss {
947517de34SManuel Lauss 	ctx->wired_entry = read_c0_wired();
957517de34SManuel Lauss 	add_wired_entry(0, 0, (unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
967517de34SManuel Lauss 	ctx->last_elo0 = ctx->last_elo1 = ~0;
977517de34SManuel Lauss }
987517de34SManuel Lauss 
config_access(unsigned char access_type,struct pci_bus * bus,unsigned int dev_fn,unsigned char where,u32 * data)997517de34SManuel Lauss static int config_access(unsigned char access_type, struct pci_bus *bus,
1007517de34SManuel Lauss 			 unsigned int dev_fn, unsigned char where, u32 *data)
1017517de34SManuel Lauss {
1027517de34SManuel Lauss 	struct alchemy_pci_context *ctx = bus->sysdata;
1037517de34SManuel Lauss 	unsigned int device = PCI_SLOT(dev_fn);
1047517de34SManuel Lauss 	unsigned int function = PCI_FUNC(dev_fn);
1057517de34SManuel Lauss 	unsigned long offset, status, cfg_base, flags, entryLo0, entryLo1, r;
1067517de34SManuel Lauss 	int error = PCIBIOS_SUCCESSFUL;
1077517de34SManuel Lauss 
1087517de34SManuel Lauss 	if (device > 19) {
1097517de34SManuel Lauss 		*data = 0xffffffff;
1107517de34SManuel Lauss 		return -1;
1117517de34SManuel Lauss 	}
1127517de34SManuel Lauss 
1137517de34SManuel Lauss 	local_irq_save(flags);
1147517de34SManuel Lauss 	r = __raw_readl(ctx->regs + PCI_REG_STATCMD) & 0x0000ffff;
1157517de34SManuel Lauss 	r |= PCI_STATCMD_STATUS(0x2000);
1167517de34SManuel Lauss 	__raw_writel(r, ctx->regs + PCI_REG_STATCMD);
1177517de34SManuel Lauss 	wmb();
1187517de34SManuel Lauss 
1197517de34SManuel Lauss 	/* Allow board vendors to implement their own off-chip IDSEL.
1207517de34SManuel Lauss 	 * If it doesn't succeed, may as well bail out at this point.
1217517de34SManuel Lauss 	 */
1227517de34SManuel Lauss 	if (ctx->board_pci_idsel(device, 1) == 0) {
1237517de34SManuel Lauss 		*data = 0xffffffff;
1247517de34SManuel Lauss 		local_irq_restore(flags);
1257517de34SManuel Lauss 		return -1;
1267517de34SManuel Lauss 	}
1277517de34SManuel Lauss 
1287517de34SManuel Lauss 	/* Setup the config window */
1297517de34SManuel Lauss 	if (bus->number == 0)
1307517de34SManuel Lauss 		cfg_base = (1 << device) << 11;
1317517de34SManuel Lauss 	else
1327517de34SManuel Lauss 		cfg_base = 0x80000000 | (bus->number << 16) | (device << 11);
1337517de34SManuel Lauss 
1347517de34SManuel Lauss 	/* Setup the lower bits of the 36-bit address */
1357517de34SManuel Lauss 	offset = (function << 8) | (where & ~0x3);
1367517de34SManuel Lauss 	/* Pick up any address that falls below the page mask */
1377517de34SManuel Lauss 	offset |= cfg_base & ~PAGE_MASK;
1387517de34SManuel Lauss 
1397517de34SManuel Lauss 	/* Page boundary */
1407517de34SManuel Lauss 	cfg_base = cfg_base & PAGE_MASK;
1417517de34SManuel Lauss 
1427517de34SManuel Lauss 	/* To improve performance, if the current device is the same as
1437517de34SManuel Lauss 	 * the last device accessed, we don't touch the TLB.
1447517de34SManuel Lauss 	 */
1457517de34SManuel Lauss 	entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
1467517de34SManuel Lauss 	entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
1477517de34SManuel Lauss 	if ((entryLo0 != ctx->last_elo0) || (entryLo1 != ctx->last_elo1)) {
1487517de34SManuel Lauss 		mod_wired_entry(ctx->wired_entry, entryLo0, entryLo1,
1497517de34SManuel Lauss 				(unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
1507517de34SManuel Lauss 		ctx->last_elo0 = entryLo0;
1517517de34SManuel Lauss 		ctx->last_elo1 = entryLo1;
1527517de34SManuel Lauss 	}
1537517de34SManuel Lauss 
1547517de34SManuel Lauss 	if (access_type == PCI_ACCESS_WRITE)
1557517de34SManuel Lauss 		__raw_writel(*data, ctx->pci_cfg_vm->addr + offset);
1567517de34SManuel Lauss 	else
1577517de34SManuel Lauss 		*data = __raw_readl(ctx->pci_cfg_vm->addr + offset);
1587517de34SManuel Lauss 	wmb();
1597517de34SManuel Lauss 
1607517de34SManuel Lauss 	DBG("alchemy-pci: cfg access %d bus %u dev %u at %x dat %x conf %lx\n",
1617517de34SManuel Lauss 	    access_type, bus->number, device, where, *data, offset);
1627517de34SManuel Lauss 
1637517de34SManuel Lauss 	/* check for errors, master abort */
1647517de34SManuel Lauss 	status = __raw_readl(ctx->regs + PCI_REG_STATCMD);
1657517de34SManuel Lauss 	if (status & (1 << 29)) {
1667517de34SManuel Lauss 		*data = 0xffffffff;
1677517de34SManuel Lauss 		error = -1;
168143f0f65SPaul Bolle 		DBG("alchemy-pci: master abort on cfg access %d bus %d dev %d\n",
1697517de34SManuel Lauss 		    access_type, bus->number, device);
1707517de34SManuel Lauss 	} else if ((status >> 28) & 0xf) {
1717517de34SManuel Lauss 		DBG("alchemy-pci: PCI ERR detected: dev %d, status %lx\n",
1727517de34SManuel Lauss 		    device, (status >> 28) & 0xf);
1737517de34SManuel Lauss 
1747517de34SManuel Lauss 		/* clear errors */
1757517de34SManuel Lauss 		__raw_writel(status & 0xf000ffff, ctx->regs + PCI_REG_STATCMD);
1767517de34SManuel Lauss 
1777517de34SManuel Lauss 		*data = 0xffffffff;
1787517de34SManuel Lauss 		error = -1;
1797517de34SManuel Lauss 	}
1807517de34SManuel Lauss 
1817517de34SManuel Lauss 	/* Take away the IDSEL. */
1827517de34SManuel Lauss 	(void)ctx->board_pci_idsel(device, 0);
1837517de34SManuel Lauss 
1847517de34SManuel Lauss 	local_irq_restore(flags);
1857517de34SManuel Lauss 	return error;
1867517de34SManuel Lauss }
1877517de34SManuel Lauss 
read_config_byte(struct pci_bus * bus,unsigned int devfn,int where,u8 * val)1887517de34SManuel Lauss static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
1897517de34SManuel Lauss 			    int where,	u8 *val)
1907517de34SManuel Lauss {
1917517de34SManuel Lauss 	u32 data;
1927517de34SManuel Lauss 	int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
1937517de34SManuel Lauss 
1947517de34SManuel Lauss 	if (where & 1)
1957517de34SManuel Lauss 		data >>= 8;
1967517de34SManuel Lauss 	if (where & 2)
1977517de34SManuel Lauss 		data >>= 16;
1987517de34SManuel Lauss 	*val = data & 0xff;
1997517de34SManuel Lauss 	return ret;
2007517de34SManuel Lauss }
2017517de34SManuel Lauss 
read_config_word(struct pci_bus * bus,unsigned int devfn,int where,u16 * val)2027517de34SManuel Lauss static int read_config_word(struct pci_bus *bus, unsigned int devfn,
2037517de34SManuel Lauss 			    int where, u16 *val)
2047517de34SManuel Lauss {
2057517de34SManuel Lauss 	u32 data;
2067517de34SManuel Lauss 	int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
2077517de34SManuel Lauss 
2087517de34SManuel Lauss 	if (where & 2)
2097517de34SManuel Lauss 		data >>= 16;
2107517de34SManuel Lauss 	*val = data & 0xffff;
2117517de34SManuel Lauss 	return ret;
2127517de34SManuel Lauss }
2137517de34SManuel Lauss 
read_config_dword(struct pci_bus * bus,unsigned int devfn,int where,u32 * val)2147517de34SManuel Lauss static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
2157517de34SManuel Lauss 			     int where, u32 *val)
2167517de34SManuel Lauss {
2177517de34SManuel Lauss 	return config_access(PCI_ACCESS_READ, bus, devfn, where, val);
2187517de34SManuel Lauss }
2197517de34SManuel Lauss 
write_config_byte(struct pci_bus * bus,unsigned int devfn,int where,u8 val)2207517de34SManuel Lauss static int write_config_byte(struct pci_bus *bus, unsigned int devfn,
2217517de34SManuel Lauss 			     int where, u8 val)
2227517de34SManuel Lauss {
2237517de34SManuel Lauss 	u32 data = 0;
2247517de34SManuel Lauss 
2257517de34SManuel Lauss 	if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
2267517de34SManuel Lauss 		return -1;
2277517de34SManuel Lauss 
2287517de34SManuel Lauss 	data = (data & ~(0xff << ((where & 3) << 3))) |
2297517de34SManuel Lauss 	       (val << ((where & 3) << 3));
2307517de34SManuel Lauss 
2317517de34SManuel Lauss 	if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
2327517de34SManuel Lauss 		return -1;
2337517de34SManuel Lauss 
2347517de34SManuel Lauss 	return PCIBIOS_SUCCESSFUL;
2357517de34SManuel Lauss }
2367517de34SManuel Lauss 
write_config_word(struct pci_bus * bus,unsigned int devfn,int where,u16 val)2377517de34SManuel Lauss static int write_config_word(struct pci_bus *bus, unsigned int devfn,
2387517de34SManuel Lauss 			     int where, u16 val)
2397517de34SManuel Lauss {
2407517de34SManuel Lauss 	u32 data = 0;
2417517de34SManuel Lauss 
2427517de34SManuel Lauss 	if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
2437517de34SManuel Lauss 		return -1;
2447517de34SManuel Lauss 
2457517de34SManuel Lauss 	data = (data & ~(0xffff << ((where & 3) << 3))) |
2467517de34SManuel Lauss 	       (val << ((where & 3) << 3));
2477517de34SManuel Lauss 
2487517de34SManuel Lauss 	if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
2497517de34SManuel Lauss 		return -1;
2507517de34SManuel Lauss 
2517517de34SManuel Lauss 	return PCIBIOS_SUCCESSFUL;
2527517de34SManuel Lauss }
2537517de34SManuel Lauss 
write_config_dword(struct pci_bus * bus,unsigned int devfn,int where,u32 val)2547517de34SManuel Lauss static int write_config_dword(struct pci_bus *bus, unsigned int devfn,
2557517de34SManuel Lauss 			      int where, u32 val)
2567517de34SManuel Lauss {
2577517de34SManuel Lauss 	return config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val);
2587517de34SManuel Lauss }
2597517de34SManuel Lauss 
alchemy_pci_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)2607517de34SManuel Lauss static int alchemy_pci_read(struct pci_bus *bus, unsigned int devfn,
2617517de34SManuel Lauss 		       int where, int size, u32 *val)
2627517de34SManuel Lauss {
2637517de34SManuel Lauss 	switch (size) {
2647517de34SManuel Lauss 	case 1: {
2657517de34SManuel Lauss 			u8 _val;
2667517de34SManuel Lauss 			int rc = read_config_byte(bus, devfn, where, &_val);
2677517de34SManuel Lauss 
2687517de34SManuel Lauss 			*val = _val;
2697517de34SManuel Lauss 			return rc;
2707517de34SManuel Lauss 		}
2717517de34SManuel Lauss 	case 2: {
2727517de34SManuel Lauss 			u16 _val;
2737517de34SManuel Lauss 			int rc = read_config_word(bus, devfn, where, &_val);
2747517de34SManuel Lauss 
2757517de34SManuel Lauss 			*val = _val;
2767517de34SManuel Lauss 			return rc;
2777517de34SManuel Lauss 		}
2787517de34SManuel Lauss 	default:
2797517de34SManuel Lauss 		return read_config_dword(bus, devfn, where, val);
2807517de34SManuel Lauss 	}
2817517de34SManuel Lauss }
2827517de34SManuel Lauss 
alchemy_pci_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)2837517de34SManuel Lauss static int alchemy_pci_write(struct pci_bus *bus, unsigned int devfn,
2847517de34SManuel Lauss 			     int where, int size, u32 val)
2857517de34SManuel Lauss {
2867517de34SManuel Lauss 	switch (size) {
2877517de34SManuel Lauss 	case 1:
2887517de34SManuel Lauss 		return write_config_byte(bus, devfn, where, (u8) val);
2897517de34SManuel Lauss 	case 2:
2907517de34SManuel Lauss 		return write_config_word(bus, devfn, where, (u16) val);
2917517de34SManuel Lauss 	default:
2927517de34SManuel Lauss 		return write_config_dword(bus, devfn, where, val);
2937517de34SManuel Lauss 	}
2947517de34SManuel Lauss }
2957517de34SManuel Lauss 
2967517de34SManuel Lauss static struct pci_ops alchemy_pci_ops = {
2977517de34SManuel Lauss 	.read	= alchemy_pci_read,
2987517de34SManuel Lauss 	.write	= alchemy_pci_write,
2997517de34SManuel Lauss };
3007517de34SManuel Lauss 
alchemy_pci_def_idsel(unsigned int devsel,int assert)3017517de34SManuel Lauss static int alchemy_pci_def_idsel(unsigned int devsel, int assert)
3027517de34SManuel Lauss {
3037517de34SManuel Lauss 	return 1;	/* success */
3047517de34SManuel Lauss }
3057517de34SManuel Lauss 
306864c6c22SManuel Lauss /* save PCI controller register contents. */
alchemy_pci_suspend(void)307864c6c22SManuel Lauss static int alchemy_pci_suspend(void)
308864c6c22SManuel Lauss {
309864c6c22SManuel Lauss 	struct alchemy_pci_context *ctx = __alchemy_pci_ctx;
310864c6c22SManuel Lauss 	if (!ctx)
311864c6c22SManuel Lauss 		return 0;
312864c6c22SManuel Lauss 
313864c6c22SManuel Lauss 	ctx->pm[0]  = __raw_readl(ctx->regs + PCI_REG_CMEM);
314864c6c22SManuel Lauss 	ctx->pm[1]  = __raw_readl(ctx->regs + PCI_REG_CONFIG) & 0x0009ffff;
315864c6c22SManuel Lauss 	ctx->pm[2]  = __raw_readl(ctx->regs + PCI_REG_B2BMASK_CCH);
316864c6c22SManuel Lauss 	ctx->pm[3]  = __raw_readl(ctx->regs + PCI_REG_B2BBASE0_VID);
317864c6c22SManuel Lauss 	ctx->pm[4]  = __raw_readl(ctx->regs + PCI_REG_B2BBASE1_SID);
318864c6c22SManuel Lauss 	ctx->pm[5]  = __raw_readl(ctx->regs + PCI_REG_MWMASK_DEV);
319864c6c22SManuel Lauss 	ctx->pm[6]  = __raw_readl(ctx->regs + PCI_REG_MWBASE_REV_CCL);
320864c6c22SManuel Lauss 	ctx->pm[7]  = __raw_readl(ctx->regs + PCI_REG_ID);
321864c6c22SManuel Lauss 	ctx->pm[8]  = __raw_readl(ctx->regs + PCI_REG_CLASSREV);
322864c6c22SManuel Lauss 	ctx->pm[9]  = __raw_readl(ctx->regs + PCI_REG_PARAM);
323864c6c22SManuel Lauss 	ctx->pm[10] = __raw_readl(ctx->regs + PCI_REG_MBAR);
324864c6c22SManuel Lauss 	ctx->pm[11] = __raw_readl(ctx->regs + PCI_REG_TIMEOUT);
325864c6c22SManuel Lauss 
326864c6c22SManuel Lauss 	return 0;
327864c6c22SManuel Lauss }
328864c6c22SManuel Lauss 
alchemy_pci_resume(void)329864c6c22SManuel Lauss static void alchemy_pci_resume(void)
330864c6c22SManuel Lauss {
331864c6c22SManuel Lauss 	struct alchemy_pci_context *ctx = __alchemy_pci_ctx;
332864c6c22SManuel Lauss 	if (!ctx)
333864c6c22SManuel Lauss 		return;
334864c6c22SManuel Lauss 
335864c6c22SManuel Lauss 	__raw_writel(ctx->pm[0],  ctx->regs + PCI_REG_CMEM);
336864c6c22SManuel Lauss 	__raw_writel(ctx->pm[2],  ctx->regs + PCI_REG_B2BMASK_CCH);
337864c6c22SManuel Lauss 	__raw_writel(ctx->pm[3],  ctx->regs + PCI_REG_B2BBASE0_VID);
338864c6c22SManuel Lauss 	__raw_writel(ctx->pm[4],  ctx->regs + PCI_REG_B2BBASE1_SID);
339864c6c22SManuel Lauss 	__raw_writel(ctx->pm[5],  ctx->regs + PCI_REG_MWMASK_DEV);
340864c6c22SManuel Lauss 	__raw_writel(ctx->pm[6],  ctx->regs + PCI_REG_MWBASE_REV_CCL);
341864c6c22SManuel Lauss 	__raw_writel(ctx->pm[7],  ctx->regs + PCI_REG_ID);
342864c6c22SManuel Lauss 	__raw_writel(ctx->pm[8],  ctx->regs + PCI_REG_CLASSREV);
343864c6c22SManuel Lauss 	__raw_writel(ctx->pm[9],  ctx->regs + PCI_REG_PARAM);
344864c6c22SManuel Lauss 	__raw_writel(ctx->pm[10], ctx->regs + PCI_REG_MBAR);
345864c6c22SManuel Lauss 	__raw_writel(ctx->pm[11], ctx->regs + PCI_REG_TIMEOUT);
346864c6c22SManuel Lauss 	wmb();
347864c6c22SManuel Lauss 	__raw_writel(ctx->pm[1],  ctx->regs + PCI_REG_CONFIG);
348864c6c22SManuel Lauss 	wmb();
349864c6c22SManuel Lauss 
350864c6c22SManuel Lauss 	/* YAMON on all db1xxx boards wipes the TLB and writes zero to C0_wired
351864c6c22SManuel Lauss 	 * on resume, making it necessary to recreate it as soon as possible.
352864c6c22SManuel Lauss 	 */
353864c6c22SManuel Lauss 	ctx->wired_entry = 8191;	/* impossibly high value */
354864c6c22SManuel Lauss 	alchemy_pci_wired_entry(ctx);	/* install it */
355864c6c22SManuel Lauss }
356864c6c22SManuel Lauss 
357864c6c22SManuel Lauss static struct syscore_ops alchemy_pci_pmops = {
358864c6c22SManuel Lauss 	.suspend	= alchemy_pci_suspend,
359864c6c22SManuel Lauss 	.resume		= alchemy_pci_resume,
360864c6c22SManuel Lauss };
361864c6c22SManuel Lauss 
alchemy_pci_probe(struct platform_device * pdev)36228eb0e46SGreg Kroah-Hartman static int alchemy_pci_probe(struct platform_device *pdev)
3637517de34SManuel Lauss {
3647517de34SManuel Lauss 	struct alchemy_pci_platdata *pd = pdev->dev.platform_data;
3657517de34SManuel Lauss 	struct alchemy_pci_context *ctx;
3667517de34SManuel Lauss 	void __iomem *virt_io;
3677517de34SManuel Lauss 	unsigned long val;
3687517de34SManuel Lauss 	struct resource *r;
3698e211705SManuel Lauss 	struct clk *c;
3707517de34SManuel Lauss 	int ret;
3717517de34SManuel Lauss 
3727517de34SManuel Lauss 	/* need at least PCI IRQ mapping table */
3737517de34SManuel Lauss 	if (!pd) {
3747517de34SManuel Lauss 		dev_err(&pdev->dev, "need platform data for PCI setup\n");
3757517de34SManuel Lauss 		ret = -ENODEV;
3767517de34SManuel Lauss 		goto out;
3777517de34SManuel Lauss 	}
3787517de34SManuel Lauss 
3797517de34SManuel Lauss 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3807517de34SManuel Lauss 	if (!ctx) {
3817517de34SManuel Lauss 		dev_err(&pdev->dev, "no memory for pcictl context\n");
3827517de34SManuel Lauss 		ret = -ENOMEM;
3837517de34SManuel Lauss 		goto out;
3847517de34SManuel Lauss 	}
3857517de34SManuel Lauss 
3867517de34SManuel Lauss 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3877517de34SManuel Lauss 	if (!r) {
3887517de34SManuel Lauss 		dev_err(&pdev->dev, "no	 pcictl ctrl regs resource\n");
3897517de34SManuel Lauss 		ret = -ENODEV;
3907517de34SManuel Lauss 		goto out1;
3917517de34SManuel Lauss 	}
3927517de34SManuel Lauss 
3937517de34SManuel Lauss 	if (!request_mem_region(r->start, resource_size(r), pdev->name)) {
3947517de34SManuel Lauss 		dev_err(&pdev->dev, "cannot claim pci regs\n");
3957517de34SManuel Lauss 		ret = -ENODEV;
3967517de34SManuel Lauss 		goto out1;
3977517de34SManuel Lauss 	}
3987517de34SManuel Lauss 
3998e211705SManuel Lauss 	c = clk_get(&pdev->dev, "pci_clko");
4008e211705SManuel Lauss 	if (IS_ERR(c)) {
4018e211705SManuel Lauss 		dev_err(&pdev->dev, "unable to find PCI clock\n");
4028e211705SManuel Lauss 		ret = PTR_ERR(c);
4038e211705SManuel Lauss 		goto out2;
4048e211705SManuel Lauss 	}
4058e211705SManuel Lauss 
4068e211705SManuel Lauss 	ret = clk_prepare_enable(c);
4078e211705SManuel Lauss 	if (ret) {
4088e211705SManuel Lauss 		dev_err(&pdev->dev, "cannot enable PCI clock\n");
4098e211705SManuel Lauss 		goto out6;
4108e211705SManuel Lauss 	}
4118e211705SManuel Lauss 
4124bdc0d67SChristoph Hellwig 	ctx->regs = ioremap(r->start, resource_size(r));
4137517de34SManuel Lauss 	if (!ctx->regs) {
4147517de34SManuel Lauss 		dev_err(&pdev->dev, "cannot map pci regs\n");
4157517de34SManuel Lauss 		ret = -ENODEV;
4168e211705SManuel Lauss 		goto out5;
4177517de34SManuel Lauss 	}
4187517de34SManuel Lauss 
4197517de34SManuel Lauss 	/* map parts of the PCI IO area */
4207517de34SManuel Lauss 	/* REVISIT: if this changes with a newer variant (doubt it) make this
4217517de34SManuel Lauss 	 * a platform resource.
4227517de34SManuel Lauss 	 */
4237517de34SManuel Lauss 	virt_io = ioremap(AU1500_PCI_IO_PHYS_ADDR, 0x00100000);
4247517de34SManuel Lauss 	if (!virt_io) {
4257517de34SManuel Lauss 		dev_err(&pdev->dev, "cannot remap pci io space\n");
4267517de34SManuel Lauss 		ret = -ENODEV;
4277517de34SManuel Lauss 		goto out3;
4287517de34SManuel Lauss 	}
4297517de34SManuel Lauss 	ctx->alchemy_pci_ctrl.io_map_base = (unsigned long)virt_io;
4307517de34SManuel Lauss 
4317517de34SManuel Lauss 	/* Au1500 revisions older than AD have borked coherent PCI */
43214ac09a6SChristoph Hellwig 	if (alchemy_get_cputype() == ALCHEMY_CPU_AU1500 &&
43314ac09a6SChristoph Hellwig 	    read_c0_prid() < 0x01030202 && !dma_default_coherent) {
4347517de34SManuel Lauss 		val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
4357517de34SManuel Lauss 		val |= PCI_CONFIG_NC;
4367517de34SManuel Lauss 		__raw_writel(val, ctx->regs + PCI_REG_CONFIG);
4377517de34SManuel Lauss 		wmb();
4387517de34SManuel Lauss 		dev_info(&pdev->dev, "non-coherent PCI on Au1500 AA/AB/AC\n");
4397517de34SManuel Lauss 	}
4407517de34SManuel Lauss 
4417517de34SManuel Lauss 	if (pd->board_map_irq)
4427517de34SManuel Lauss 		ctx->board_map_irq = pd->board_map_irq;
4437517de34SManuel Lauss 
4447517de34SManuel Lauss 	if (pd->board_pci_idsel)
4457517de34SManuel Lauss 		ctx->board_pci_idsel = pd->board_pci_idsel;
4467517de34SManuel Lauss 	else
4477517de34SManuel Lauss 		ctx->board_pci_idsel = alchemy_pci_def_idsel;
4487517de34SManuel Lauss 
4497517de34SManuel Lauss 	/* fill in relevant pci_controller members */
4507517de34SManuel Lauss 	ctx->alchemy_pci_ctrl.pci_ops = &alchemy_pci_ops;
4517517de34SManuel Lauss 	ctx->alchemy_pci_ctrl.mem_resource = &alchemy_pci_def_memres;
4527517de34SManuel Lauss 	ctx->alchemy_pci_ctrl.io_resource = &alchemy_pci_def_iores;
4537517de34SManuel Lauss 
4547517de34SManuel Lauss 	/* we can't ioremap the entire pci config space because it's too large,
4557517de34SManuel Lauss 	 * nor can we dynamically ioremap it because some drivers use the
4567517de34SManuel Lauss 	 * PCI config routines from within atomic contex and that becomes a
4577517de34SManuel Lauss 	 * problem in get_vm_area().  Instead we use one wired TLB entry to
4587517de34SManuel Lauss 	 * handle all config accesses for all busses.
4597517de34SManuel Lauss 	 */
4607517de34SManuel Lauss 	ctx->pci_cfg_vm = get_vm_area(0x2000, VM_IOREMAP);
4617517de34SManuel Lauss 	if (!ctx->pci_cfg_vm) {
4627517de34SManuel Lauss 		dev_err(&pdev->dev, "unable to get vm area\n");
4637517de34SManuel Lauss 		ret = -ENOMEM;
4647517de34SManuel Lauss 		goto out4;
4657517de34SManuel Lauss 	}
466864c6c22SManuel Lauss 	ctx->wired_entry = 8191;	/* impossibly high value */
467864c6c22SManuel Lauss 	alchemy_pci_wired_entry(ctx);	/* install it */
4687517de34SManuel Lauss 
4697517de34SManuel Lauss 	set_io_port_base((unsigned long)ctx->alchemy_pci_ctrl.io_map_base);
4707517de34SManuel Lauss 
4717517de34SManuel Lauss 	/* board may want to modify bits in the config register, do it now */
4727517de34SManuel Lauss 	val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
4737517de34SManuel Lauss 	val &= ~pd->pci_cfg_clr;
4747517de34SManuel Lauss 	val |= pd->pci_cfg_set;
4757517de34SManuel Lauss 	val &= ~PCI_CONFIG_PD;		/* clear disable bit */
4767517de34SManuel Lauss 	__raw_writel(val, ctx->regs + PCI_REG_CONFIG);
4777517de34SManuel Lauss 	wmb();
4787517de34SManuel Lauss 
479864c6c22SManuel Lauss 	__alchemy_pci_ctx = ctx;
4807517de34SManuel Lauss 	platform_set_drvdata(pdev, ctx);
481864c6c22SManuel Lauss 	register_syscore_ops(&alchemy_pci_pmops);
4827517de34SManuel Lauss 	register_pci_controller(&ctx->alchemy_pci_ctrl);
4837517de34SManuel Lauss 
4848e211705SManuel Lauss 	dev_info(&pdev->dev, "PCI controller at %ld MHz\n",
4858e211705SManuel Lauss 		 clk_get_rate(c) / 1000000);
4868e211705SManuel Lauss 
4877517de34SManuel Lauss 	return 0;
4887517de34SManuel Lauss 
4897517de34SManuel Lauss out4:
4907517de34SManuel Lauss 	iounmap(virt_io);
4917517de34SManuel Lauss out3:
4927517de34SManuel Lauss 	iounmap(ctx->regs);
4938e211705SManuel Lauss out5:
4948e211705SManuel Lauss 	clk_disable_unprepare(c);
4958e211705SManuel Lauss out6:
4968e211705SManuel Lauss 	clk_put(c);
4977517de34SManuel Lauss out2:
4987517de34SManuel Lauss 	release_mem_region(r->start, resource_size(r));
4997517de34SManuel Lauss out1:
5007517de34SManuel Lauss 	kfree(ctx);
5017517de34SManuel Lauss out:
5027517de34SManuel Lauss 	return ret;
5037517de34SManuel Lauss }
5047517de34SManuel Lauss 
5057517de34SManuel Lauss static struct platform_driver alchemy_pcictl_driver = {
5067517de34SManuel Lauss 	.probe		= alchemy_pci_probe,
5077517de34SManuel Lauss 	.driver = {
5087517de34SManuel Lauss 		.name	= "alchemy-pci",
5097517de34SManuel Lauss 	},
5107517de34SManuel Lauss };
5117517de34SManuel Lauss 
alchemy_pci_init(void)5127517de34SManuel Lauss static int __init alchemy_pci_init(void)
5137517de34SManuel Lauss {
5147517de34SManuel Lauss 	/* Au1500/Au1550 have PCI */
5157517de34SManuel Lauss 	switch (alchemy_get_cputype()) {
5167517de34SManuel Lauss 	case ALCHEMY_CPU_AU1500:
5177517de34SManuel Lauss 	case ALCHEMY_CPU_AU1550:
5187517de34SManuel Lauss 		return platform_driver_register(&alchemy_pcictl_driver);
5197517de34SManuel Lauss 	}
5207517de34SManuel Lauss 	return 0;
5217517de34SManuel Lauss }
5227517de34SManuel Lauss arch_initcall(alchemy_pci_init);
5237517de34SManuel Lauss 
5247517de34SManuel Lauss 
pcibios_map_irq(const struct pci_dev * dev,u8 slot,u8 pin)5258eba3651SManuel Lauss int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
5267517de34SManuel Lauss {
5277517de34SManuel Lauss 	struct alchemy_pci_context *ctx = dev->sysdata;
5287517de34SManuel Lauss 	if (ctx && ctx->board_map_irq)
5297517de34SManuel Lauss 		return ctx->board_map_irq(dev, slot, pin);
5307517de34SManuel Lauss 	return -1;
5317517de34SManuel Lauss }
5327517de34SManuel Lauss 
pcibios_plat_dev_init(struct pci_dev * dev)5337517de34SManuel Lauss int pcibios_plat_dev_init(struct pci_dev *dev)
5347517de34SManuel Lauss {
5357517de34SManuel Lauss 	return 0;
5367517de34SManuel Lauss }
537