xref: /openbmc/linux/arch/sh/drivers/pci/pcie-sh7786.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1ff4a7481SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
25713e602SPaul Mundt /*
35713e602SPaul Mundt  * Low-Level PCI Express Support for the SH7786
45713e602SPaul Mundt  *
51da09c43SPaul Mundt  *  Copyright (C) 2009 - 2011  Paul Mundt
65713e602SPaul Mundt  */
71da09c43SPaul Mundt #define pr_fmt(fmt) "PCI: " fmt
81da09c43SPaul Mundt 
95713e602SPaul Mundt #include <linux/pci.h>
105713e602SPaul Mundt #include <linux/init.h>
115713e602SPaul Mundt #include <linux/kernel.h>
125713e602SPaul Mundt #include <linux/io.h>
131da09c43SPaul Mundt #include <linux/async.h>
145713e602SPaul Mundt #include <linux/delay.h>
1516fee29bSChristoph Hellwig #include <linux/dma-map-ops.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
17c524ebf5SPaul Mundt #include <linux/clk.h>
18c524ebf5SPaul Mundt #include <linux/sh_clk.h>
1958796ce6SPaul Mundt #include <linux/sh_intc.h>
20d62e9bf5SThomas Petazzoni #include <cpu/sh7786.h>
215713e602SPaul Mundt #include "pcie-sh7786.h"
2287dfb311SMasahiro Yamada #include <linux/sizes.h>
235713e602SPaul Mundt 
245713e602SPaul Mundt struct sh7786_pcie_port {
255713e602SPaul Mundt 	struct pci_channel	*hose;
26c524ebf5SPaul Mundt 	struct clk		*fclk, phy_clk;
275713e602SPaul Mundt 	unsigned int		index;
285713e602SPaul Mundt 	int			endpoint;
295713e602SPaul Mundt 	int			link;
305713e602SPaul Mundt };
315713e602SPaul Mundt 
325713e602SPaul Mundt static struct sh7786_pcie_port *sh7786_pcie_ports;
335713e602SPaul Mundt static unsigned int nr_ports;
34e0d07278SJim Quinlan size_t memsize;
35e0d07278SJim Quinlan u64 memstart;
365713e602SPaul Mundt 
375713e602SPaul Mundt static struct sh7786_pcie_hwops {
385713e602SPaul Mundt 	int (*core_init)(void);
39362f2b09SLai Jiangshan 	async_func_t port_init_hw;
405713e602SPaul Mundt } *sh7786_pcie_hwops;
415713e602SPaul Mundt 
427561f2ddSPaul Mundt static struct resource sh7786_pci0_resources[] = {
435713e602SPaul Mundt 	{
445da1bb96SThomas Petazzoni 		.name	= "PCIe0 MEM 0",
457561f2ddSPaul Mundt 		.start	= 0xfd000000,
467561f2ddSPaul Mundt 		.end	= 0xfd000000 + SZ_8M - 1,
475da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_MEM,
487561f2ddSPaul Mundt 	}, {
495da1bb96SThomas Petazzoni 		.name	= "PCIe0 MEM 1",
507561f2ddSPaul Mundt 		.start	= 0xc0000000,
517561f2ddSPaul Mundt 		.end	= 0xc0000000 + SZ_512M - 1,
527561f2ddSPaul Mundt 		.flags	= IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
537561f2ddSPaul Mundt 	}, {
545da1bb96SThomas Petazzoni 		.name	= "PCIe0 MEM 2",
557561f2ddSPaul Mundt 		.start	= 0x10000000,
567561f2ddSPaul Mundt 		.end	= 0x10000000 + SZ_64M - 1,
575713e602SPaul Mundt 		.flags	= IORESOURCE_MEM,
585713e602SPaul Mundt 	}, {
595da1bb96SThomas Petazzoni 		.name	= "PCIe0 IO",
607561f2ddSPaul Mundt 		.start	= 0xfe100000,
617561f2ddSPaul Mundt 		.end	= 0xfe100000 + SZ_1M - 1,
625da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_IO,
635713e602SPaul Mundt 	},
645713e602SPaul Mundt };
655713e602SPaul Mundt 
667561f2ddSPaul Mundt static struct resource sh7786_pci1_resources[] = {
677561f2ddSPaul Mundt 	{
685da1bb96SThomas Petazzoni 		.name	= "PCIe1 MEM 0",
697561f2ddSPaul Mundt 		.start	= 0xfd800000,
707561f2ddSPaul Mundt 		.end	= 0xfd800000 + SZ_8M - 1,
715da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_MEM,
727561f2ddSPaul Mundt 	}, {
735da1bb96SThomas Petazzoni 		.name	= "PCIe1 MEM 1",
747561f2ddSPaul Mundt 		.start	= 0xa0000000,
757561f2ddSPaul Mundt 		.end	= 0xa0000000 + SZ_512M - 1,
767561f2ddSPaul Mundt 		.flags	= IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
777561f2ddSPaul Mundt 	}, {
785da1bb96SThomas Petazzoni 		.name	= "PCIe1 MEM 2",
797561f2ddSPaul Mundt 		.start	= 0x30000000,
807561f2ddSPaul Mundt 		.end	= 0x30000000 + SZ_256M - 1,
817561f2ddSPaul Mundt 		.flags	= IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
827561f2ddSPaul Mundt 	}, {
835da1bb96SThomas Petazzoni 		.name	= "PCIe1 IO",
847561f2ddSPaul Mundt 		.start	= 0xfe300000,
857561f2ddSPaul Mundt 		.end	= 0xfe300000 + SZ_1M - 1,
865da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_IO,
877561f2ddSPaul Mundt 	},
885713e602SPaul Mundt };
895713e602SPaul Mundt 
907561f2ddSPaul Mundt static struct resource sh7786_pci2_resources[] = {
915713e602SPaul Mundt 	{
925da1bb96SThomas Petazzoni 		.name	= "PCIe2 MEM 0",
937561f2ddSPaul Mundt 		.start	= 0xfc800000,
947561f2ddSPaul Mundt 		.end	= 0xfc800000 + SZ_4M - 1,
955da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_MEM,
965713e602SPaul Mundt 	}, {
975da1bb96SThomas Petazzoni 		.name	= "PCIe2 MEM 1",
987561f2ddSPaul Mundt 		.start	= 0x80000000,
997561f2ddSPaul Mundt 		.end	= 0x80000000 + SZ_512M - 1,
1007561f2ddSPaul Mundt 		.flags	= IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
1015713e602SPaul Mundt 	}, {
1025da1bb96SThomas Petazzoni 		.name	= "PCIe2 MEM 2",
1037561f2ddSPaul Mundt 		.start	= 0x20000000,
1047561f2ddSPaul Mundt 		.end	= 0x20000000 + SZ_256M - 1,
1057561f2ddSPaul Mundt 		.flags	= IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
1067561f2ddSPaul Mundt 	}, {
1075da1bb96SThomas Petazzoni 		.name	= "PCIe2 IO",
1087561f2ddSPaul Mundt 		.start	= 0xfcd00000,
1097561f2ddSPaul Mundt 		.end	= 0xfcd00000 + SZ_1M - 1,
1105da1bb96SThomas Petazzoni 		.flags	= IORESOURCE_IO,
1115713e602SPaul Mundt 	},
1125713e602SPaul Mundt };
1135713e602SPaul Mundt 
1145713e602SPaul Mundt extern struct pci_ops sh7786_pci_ops;
1155713e602SPaul Mundt 
1165713e602SPaul Mundt #define DEFINE_CONTROLLER(start, idx)					\
1175713e602SPaul Mundt {									\
1185713e602SPaul Mundt 	.pci_ops	= &sh7786_pci_ops,				\
1197561f2ddSPaul Mundt 	.resources	= sh7786_pci##idx##_resources,			\
1207561f2ddSPaul Mundt 	.nr_resources	= ARRAY_SIZE(sh7786_pci##idx##_resources),	\
1215713e602SPaul Mundt 	.reg_base	= start,					\
1225713e602SPaul Mundt 	.mem_offset	= 0,						\
1235713e602SPaul Mundt 	.io_offset	= 0,						\
1245713e602SPaul Mundt }
1255713e602SPaul Mundt 
1265713e602SPaul Mundt static struct pci_channel sh7786_pci_channels[] = {
1275713e602SPaul Mundt 	DEFINE_CONTROLLER(0xfe000000, 0),
1285713e602SPaul Mundt 	DEFINE_CONTROLLER(0xfe200000, 1),
1295713e602SPaul Mundt 	DEFINE_CONTROLLER(0xfcc00000, 2),
1305713e602SPaul Mundt };
1315713e602SPaul Mundt 
132c524ebf5SPaul Mundt static struct clk fixed_pciexclkp = {
133c524ebf5SPaul Mundt 	.rate = 100000000,	/* 100 MHz reference clock */
134c524ebf5SPaul Mundt };
135c524ebf5SPaul Mundt 
sh7786_pci_fixup(struct pci_dev * dev)136b881bc46SGreg Kroah-Hartman static void sh7786_pci_fixup(struct pci_dev *dev)
1372c65d75eSPaul Mundt {
1382c65d75eSPaul Mundt 	/*
1392c65d75eSPaul Mundt 	 * Prevent enumeration of root complex resources.
1402c65d75eSPaul Mundt 	 */
1412c65d75eSPaul Mundt 	if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {
142*09cc9006SMika Westerberg 		struct resource *r;
1432c65d75eSPaul Mundt 
144*09cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r) {
145*09cc9006SMika Westerberg 			r->start	= 0;
146*09cc9006SMika Westerberg 			r->end		= 0;
147*09cc9006SMika Westerberg 			r->flags	= 0;
1482c65d75eSPaul Mundt 		}
1492c65d75eSPaul Mundt 	}
1502c65d75eSPaul Mundt }
1512c65d75eSPaul Mundt DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786,
1522c65d75eSPaul Mundt 			 sh7786_pci_fixup);
1532c65d75eSPaul Mundt 
phy_wait_for_ack(struct pci_channel * chan)154c524ebf5SPaul Mundt static int __init phy_wait_for_ack(struct pci_channel *chan)
1555713e602SPaul Mundt {
1565713e602SPaul Mundt 	unsigned int timeout = 100;
1575713e602SPaul Mundt 
1585713e602SPaul Mundt 	while (timeout--) {
1595713e602SPaul Mundt 		if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
1605713e602SPaul Mundt 			return 0;
1615713e602SPaul Mundt 
1625713e602SPaul Mundt 		udelay(100);
1635713e602SPaul Mundt 	}
1645713e602SPaul Mundt 
1655713e602SPaul Mundt 	return -ETIMEDOUT;
1665713e602SPaul Mundt }
1675713e602SPaul Mundt 
pci_wait_for_irq(struct pci_channel * chan,unsigned int mask)168c524ebf5SPaul Mundt static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
1695713e602SPaul Mundt {
1705713e602SPaul Mundt 	unsigned int timeout = 100;
1715713e602SPaul Mundt 
1725713e602SPaul Mundt 	while (timeout--) {
1735713e602SPaul Mundt 		if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
1745713e602SPaul Mundt 			return 0;
1755713e602SPaul Mundt 
1765713e602SPaul Mundt 		udelay(100);
1775713e602SPaul Mundt 	}
1785713e602SPaul Mundt 
1795713e602SPaul Mundt 	return -ETIMEDOUT;
1805713e602SPaul Mundt }
1815713e602SPaul Mundt 
phy_write_reg(struct pci_channel * chan,unsigned int addr,unsigned int lane,unsigned int data)182c524ebf5SPaul Mundt static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr,
1835713e602SPaul Mundt 				 unsigned int lane, unsigned int data)
1845713e602SPaul Mundt {
18553178d71SPaul Mundt 	unsigned long phyaddr;
1865713e602SPaul Mundt 
1875713e602SPaul Mundt 	phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
1885713e602SPaul Mundt 			((addr & 0xff) << BITS_ADR);
1895713e602SPaul Mundt 
1905713e602SPaul Mundt 	/* Set write data */
1915713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
1925713e602SPaul Mundt 	pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
1935713e602SPaul Mundt 
1945713e602SPaul Mundt 	phy_wait_for_ack(chan);
1955713e602SPaul Mundt 
1965713e602SPaul Mundt 	/* Clear command */
19753178d71SPaul Mundt 	pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
1985713e602SPaul Mundt 	pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
1995713e602SPaul Mundt 
2005713e602SPaul Mundt 	phy_wait_for_ack(chan);
2015713e602SPaul Mundt }
2025713e602SPaul Mundt 
pcie_clk_init(struct sh7786_pcie_port * port)203c524ebf5SPaul Mundt static int __init pcie_clk_init(struct sh7786_pcie_port *port)
2045713e602SPaul Mundt {
205c524ebf5SPaul Mundt 	struct pci_channel *chan = port->hose;
206c524ebf5SPaul Mundt 	struct clk *clk;
207c524ebf5SPaul Mundt 	char fclk_name[16];
208c524ebf5SPaul Mundt 	int ret;
209c524ebf5SPaul Mundt 
210c524ebf5SPaul Mundt 	/*
211c524ebf5SPaul Mundt 	 * First register the fixed clock
212c524ebf5SPaul Mundt 	 */
213c524ebf5SPaul Mundt 	ret = clk_register(&fixed_pciexclkp);
214c524ebf5SPaul Mundt 	if (unlikely(ret != 0))
215c524ebf5SPaul Mundt 		return ret;
216c524ebf5SPaul Mundt 
217c524ebf5SPaul Mundt 	/*
218c524ebf5SPaul Mundt 	 * Grab the port's function clock, which the PHY clock depends
219c524ebf5SPaul Mundt 	 * on. clock lookups don't help us much at this point, since no
220c524ebf5SPaul Mundt 	 * dev_id is available this early. Lame.
221c524ebf5SPaul Mundt 	 */
222c524ebf5SPaul Mundt 	snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index);
223c524ebf5SPaul Mundt 
224c524ebf5SPaul Mundt 	port->fclk = clk_get(NULL, fclk_name);
225c524ebf5SPaul Mundt 	if (IS_ERR(port->fclk)) {
226c524ebf5SPaul Mundt 		ret = PTR_ERR(port->fclk);
227c524ebf5SPaul Mundt 		goto err_fclk;
228c524ebf5SPaul Mundt 	}
229c524ebf5SPaul Mundt 
230c524ebf5SPaul Mundt 	clk_enable(port->fclk);
231c524ebf5SPaul Mundt 
232c524ebf5SPaul Mundt 	/*
233c524ebf5SPaul Mundt 	 * And now, set up the PHY clock
234c524ebf5SPaul Mundt 	 */
235c524ebf5SPaul Mundt 	clk = &port->phy_clk;
236c524ebf5SPaul Mundt 
237c524ebf5SPaul Mundt 	memset(clk, 0, sizeof(struct clk));
238c524ebf5SPaul Mundt 
239c524ebf5SPaul Mundt 	clk->parent = &fixed_pciexclkp;
240c524ebf5SPaul Mundt 	clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR);
241c524ebf5SPaul Mundt 	clk->enable_bit = BITS_CKE;
242c524ebf5SPaul Mundt 
243ad3337cbSNobuhiro Iwamatsu 	ret = sh_clk_mstp_register(clk, 1);
244c524ebf5SPaul Mundt 	if (unlikely(ret < 0))
245c524ebf5SPaul Mundt 		goto err_phy;
246c524ebf5SPaul Mundt 
247c524ebf5SPaul Mundt 	return 0;
248c524ebf5SPaul Mundt 
249c524ebf5SPaul Mundt err_phy:
250c524ebf5SPaul Mundt 	clk_disable(port->fclk);
251c524ebf5SPaul Mundt 	clk_put(port->fclk);
252c524ebf5SPaul Mundt err_fclk:
253c524ebf5SPaul Mundt 	clk_unregister(&fixed_pciexclkp);
254c524ebf5SPaul Mundt 
255c524ebf5SPaul Mundt 	return ret;
256c524ebf5SPaul Mundt }
257c524ebf5SPaul Mundt 
phy_init(struct sh7786_pcie_port * port)258c524ebf5SPaul Mundt static int __init phy_init(struct sh7786_pcie_port *port)
259c524ebf5SPaul Mundt {
260c524ebf5SPaul Mundt 	struct pci_channel *chan = port->hose;
2615713e602SPaul Mundt 	unsigned int timeout = 100;
2625713e602SPaul Mundt 
263c524ebf5SPaul Mundt 	clk_enable(&port->phy_clk);
26453178d71SPaul Mundt 
2655713e602SPaul Mundt 	/* Initialize the phy */
2665713e602SPaul Mundt 	phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
2675713e602SPaul Mundt 	phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
2685713e602SPaul Mundt 	phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
2695713e602SPaul Mundt 	phy_write_reg(chan, 0x65, 0xf, 0x09070907);
2705713e602SPaul Mundt 	phy_write_reg(chan, 0x66, 0xf, 0x00000010);
2715713e602SPaul Mundt 	phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
2725713e602SPaul Mundt 	phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
27353178d71SPaul Mundt 	phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
2745713e602SPaul Mundt 
2755713e602SPaul Mundt 	/* Deassert Standby */
27653178d71SPaul Mundt 	phy_write_reg(chan, 0x67, 0x1, 0x00000400);
27753178d71SPaul Mundt 
27853178d71SPaul Mundt 	/* Disable clock */
279c524ebf5SPaul Mundt 	clk_disable(&port->phy_clk);
2805713e602SPaul Mundt 
2815713e602SPaul Mundt 	while (timeout--) {
2825713e602SPaul Mundt 		if (pci_read_reg(chan, SH4A_PCIEPHYSR))
2835713e602SPaul Mundt 			return 0;
2845713e602SPaul Mundt 
2855713e602SPaul Mundt 		udelay(100);
2865713e602SPaul Mundt 	}
2875713e602SPaul Mundt 
2885713e602SPaul Mundt 	return -ETIMEDOUT;
2895713e602SPaul Mundt }
2905713e602SPaul Mundt 
pcie_reset(struct sh7786_pcie_port * port)291c524ebf5SPaul Mundt static void __init pcie_reset(struct sh7786_pcie_port *port)
2922dbfa1e3SPaul Mundt {
2932dbfa1e3SPaul Mundt 	struct pci_channel *chan = port->hose;
2942dbfa1e3SPaul Mundt 
2952dbfa1e3SPaul Mundt 	pci_write_reg(chan, 1, SH4A_PCIESRSTR);
2962dbfa1e3SPaul Mundt 	pci_write_reg(chan, 0, SH4A_PCIETCTLR);
2972dbfa1e3SPaul Mundt 	pci_write_reg(chan, 0, SH4A_PCIESRSTR);
2982dbfa1e3SPaul Mundt 	pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);
2992dbfa1e3SPaul Mundt }
3002dbfa1e3SPaul Mundt 
pcie_init(struct sh7786_pcie_port * port)301c524ebf5SPaul Mundt static int __init pcie_init(struct sh7786_pcie_port *port)
3025713e602SPaul Mundt {
3035713e602SPaul Mundt 	struct pci_channel *chan = port->hose;
3045713e602SPaul Mundt 	unsigned int data;
30579e1c5e7SThomas Petazzoni 	phys_addr_t memstart, memend;
306da03a63aSPaul Mundt 	int ret, i, win;
3075713e602SPaul Mundt 
3085713e602SPaul Mundt 	/* Begin initialization */
3092dbfa1e3SPaul Mundt 	pcie_reset(port);
3105713e602SPaul Mundt 
3112c65d75eSPaul Mundt 	/*
3122c65d75eSPaul Mundt 	 * Initial header for port config space is type 1, set the device
3132c65d75eSPaul Mundt 	 * class to match. Hardware takes care of propagating the IDSETR
3142c65d75eSPaul Mundt 	 * settings, so there is no need to bother with a quirk.
3152c65d75eSPaul Mundt 	 */
316904b10fbSPali Rohár 	pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI_NORMAL << 8, SH4A_PCIEIDSETR1);
3175713e602SPaul Mundt 
3185713e602SPaul Mundt 	/* Initialize default capabilities. */
3195713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
3205713e602SPaul Mundt 	data &= ~(PCI_EXP_FLAGS_TYPE << 16);
3215713e602SPaul Mundt 
3225713e602SPaul Mundt 	if (port->endpoint)
3235713e602SPaul Mundt 		data |= PCI_EXP_TYPE_ENDPOINT << 20;
3245713e602SPaul Mundt 	else
3255713e602SPaul Mundt 		data |= PCI_EXP_TYPE_ROOT_PORT << 20;
3265713e602SPaul Mundt 
3275713e602SPaul Mundt 	data |= PCI_CAP_ID_EXP;
3285713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
3295713e602SPaul Mundt 
3307578a4c6SPaul Mundt 	/* Enable data link layer active state reporting */
3317578a4c6SPaul Mundt 	pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
3327578a4c6SPaul Mundt 
3337578a4c6SPaul Mundt 	/* Enable extended sync and ASPM L0s support */
3345713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
3357578a4c6SPaul Mundt 	data &= ~PCI_EXP_LNKCTL_ASPMC;
3367578a4c6SPaul Mundt 	data |= PCI_EXP_LNKCTL_ES | 1;
3375713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
3385713e602SPaul Mundt 
3397578a4c6SPaul Mundt 	/* Write out the physical slot number */
3407578a4c6SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
3417578a4c6SPaul Mundt 	data &= ~PCI_EXP_SLTCAP_PSN;
3427578a4c6SPaul Mundt 	data |= (port->index + 1) << 19;
3437578a4c6SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
3447578a4c6SPaul Mundt 
3455713e602SPaul Mundt 	/* Set the completion timer timeout to the maximum 32ms. */
3465713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIETLCTLR);
3477578a4c6SPaul Mundt 	data &= ~0x3f00;
3485713e602SPaul Mundt 	data |= 0x32 << 8;
3495713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIETLCTLR);
3505713e602SPaul Mundt 
3515713e602SPaul Mundt 	/*
3525713e602SPaul Mundt 	 * Set fast training sequences to the maximum 255,
3535713e602SPaul Mundt 	 * and enable MAC data scrambling.
3545713e602SPaul Mundt 	 */
3555713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
3565713e602SPaul Mundt 	data &= ~PCIEMACCTLR_SCR_DIS;
3575713e602SPaul Mundt 	data |= (0xff << 16);
3585713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
3595713e602SPaul Mundt 
36079e1c5e7SThomas Petazzoni 	memstart = __pa(memory_start);
36179e1c5e7SThomas Petazzoni 	memend   = __pa(memory_end);
36279e1c5e7SThomas Petazzoni 	memsize = roundup_pow_of_two(memend - memstart);
36379e1c5e7SThomas Petazzoni 
36479e1c5e7SThomas Petazzoni 	/*
36579e1c5e7SThomas Petazzoni 	 * The start address must be aligned on its size. So we round
36679e1c5e7SThomas Petazzoni 	 * it down, and then recalculate the size so that it covers
36779e1c5e7SThomas Petazzoni 	 * the entire memory.
36879e1c5e7SThomas Petazzoni 	 */
36979e1c5e7SThomas Petazzoni 	memstart = ALIGN_DOWN(memstart, memsize);
37079e1c5e7SThomas Petazzoni 	memsize = roundup_pow_of_two(memend - memstart);
3717578a4c6SPaul Mundt 
3727578a4c6SPaul Mundt 	/*
3737578a4c6SPaul Mundt 	 * If there's more than 512MB of memory, we need to roll over to
3747578a4c6SPaul Mundt 	 * LAR1/LAMR1.
3757578a4c6SPaul Mundt 	 */
3767578a4c6SPaul Mundt 	if (memsize > SZ_512M) {
37779e1c5e7SThomas Petazzoni 		pci_write_reg(chan, memstart + SZ_512M, SH4A_PCIELAR1);
378cecf48e2SPaul Mundt 		pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1,
379cecf48e2SPaul Mundt 			      SH4A_PCIELAMR1);
3807578a4c6SPaul Mundt 		memsize = SZ_512M;
3817578a4c6SPaul Mundt 	} else {
3827578a4c6SPaul Mundt 		/*
3837578a4c6SPaul Mundt 		 * Otherwise just zero it out and disable it.
3847578a4c6SPaul Mundt 		 */
385cecf48e2SPaul Mundt 		pci_write_reg(chan, 0, SH4A_PCIELAR1);
386cecf48e2SPaul Mundt 		pci_write_reg(chan, 0, SH4A_PCIELAMR1);
3877578a4c6SPaul Mundt 	}
3887578a4c6SPaul Mundt 
3897578a4c6SPaul Mundt 	/*
3907578a4c6SPaul Mundt 	 * LAR0/LAMR0 covers up to the first 512MB, which is enough to
3917578a4c6SPaul Mundt 	 * cover all of lowmem on most platforms.
3927578a4c6SPaul Mundt 	 */
39379e1c5e7SThomas Petazzoni 	pci_write_reg(chan, memstart, SH4A_PCIELAR0);
394cecf48e2SPaul Mundt 	pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0);
3957578a4c6SPaul Mundt 
3965713e602SPaul Mundt 	/* Finish initialization */
3975713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIETCTLR);
3985713e602SPaul Mundt 	data |= 0x1;
3995713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIETCTLR);
4005713e602SPaul Mundt 
40181df84f4SPaul Mundt 	/* Let things settle down a bit.. */
40281df84f4SPaul Mundt 	mdelay(100);
40381df84f4SPaul Mundt 
4045713e602SPaul Mundt 	/* Enable DL_Active Interrupt generation */
4055713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
4065713e602SPaul Mundt 	data |= PCIEDLINTENR_DLL_ACT_ENABLE;
4075713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
4085713e602SPaul Mundt 
4095713e602SPaul Mundt 	/* Disable MAC data scrambling. */
4105713e602SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
4115713e602SPaul Mundt 	data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
4125713e602SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
4135713e602SPaul Mundt 
414bd792aeaSPaul Mundt 	/*
415bd792aeaSPaul Mundt 	 * This will timeout if we don't have a link, but we permit the
416bd792aeaSPaul Mundt 	 * port to register anyways in order to support hotplug on future
417bd792aeaSPaul Mundt 	 * hardware.
418bd792aeaSPaul Mundt 	 */
4195713e602SPaul Mundt 	ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
4205713e602SPaul Mundt 
4217578a4c6SPaul Mundt 	data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
4227578a4c6SPaul Mundt 	data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
4237578a4c6SPaul Mundt 	data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
4247578a4c6SPaul Mundt 		(PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
4257578a4c6SPaul Mundt 	pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
4267578a4c6SPaul Mundt 
4275713e602SPaul Mundt 	pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
4285713e602SPaul Mundt 	pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
4295713e602SPaul Mundt 
4305713e602SPaul Mundt 	wmb();
4315713e602SPaul Mundt 
432bd792aeaSPaul Mundt 	if (ret == 0) {
4335713e602SPaul Mundt 		data = pci_read_reg(chan, SH4A_PCIEMACSR);
434bd792aeaSPaul Mundt 		printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n",
4355713e602SPaul Mundt 		       port->index, (data >> 20) & 0x3f);
436bd792aeaSPaul Mundt 	} else
437bd792aeaSPaul Mundt 		printk(KERN_NOTICE "PCI: PCIe#%d link down\n",
438bd792aeaSPaul Mundt 		       port->index);
4395713e602SPaul Mundt 
440da03a63aSPaul Mundt 	for (i = win = 0; i < chan->nr_resources; i++) {
4417578a4c6SPaul Mundt 		struct resource *res = chan->resources + i;
4427578a4c6SPaul Mundt 		resource_size_t size;
443cecf48e2SPaul Mundt 		u32 mask;
4447578a4c6SPaul Mundt 
445da03a63aSPaul Mundt 		/*
446da03a63aSPaul Mundt 		 * We can't use the 32-bit mode windows in legacy 29-bit
447da03a63aSPaul Mundt 		 * mode, so just skip them entirely.
448da03a63aSPaul Mundt 		 */
449da03a63aSPaul Mundt 		if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())
4507dd7f698SThomas Petazzoni 			res->flags |= IORESOURCE_DISABLED;
4517dd7f698SThomas Petazzoni 
4527dd7f698SThomas Petazzoni 		if (res->flags & IORESOURCE_DISABLED)
453da03a63aSPaul Mundt 			continue;
454da03a63aSPaul Mundt 
455da03a63aSPaul Mundt 		pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));
4567578a4c6SPaul Mundt 
4577578a4c6SPaul Mundt 		/*
4587578a4c6SPaul Mundt 		 * The PAMR mask is calculated in units of 256kB, which
4597578a4c6SPaul Mundt 		 * keeps things pretty simple.
4607578a4c6SPaul Mundt 		 */
461cecf48e2SPaul Mundt 		size = resource_size(res);
462cecf48e2SPaul Mundt 		mask = (roundup_pow_of_two(size) / SZ_256K) - 1;
463cecf48e2SPaul Mundt 		pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win));
4647578a4c6SPaul Mundt 
465a80be168SPaul Mundt 		pci_write_reg(chan, upper_32_bits(res->start),
466cecf48e2SPaul Mundt 			      SH4A_PCIEPARH(win));
467a80be168SPaul Mundt 		pci_write_reg(chan, lower_32_bits(res->start),
468cecf48e2SPaul Mundt 			      SH4A_PCIEPARL(win));
4697578a4c6SPaul Mundt 
470cecf48e2SPaul Mundt 		mask = MASK_PARE;
4717578a4c6SPaul Mundt 		if (res->flags & IORESOURCE_IO)
472cecf48e2SPaul Mundt 			mask |= MASK_SPC;
4737578a4c6SPaul Mundt 
474cecf48e2SPaul Mundt 		pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win));
475da03a63aSPaul Mundt 
476da03a63aSPaul Mundt 		win++;
4777578a4c6SPaul Mundt 	}
4785713e602SPaul Mundt 
4795713e602SPaul Mundt 	return 0;
4805713e602SPaul Mundt }
4815713e602SPaul Mundt 
pcibios_map_platform_irq(const struct pci_dev * pdev,u8 slot,u8 pin)4822b8ff9f2SMatthew Minter int pcibios_map_platform_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
4835713e602SPaul Mundt {
48458796ce6SPaul Mundt         return evt2irq(0xae0);
4855713e602SPaul Mundt }
4865713e602SPaul Mundt 
pcibios_bus_add_device(struct pci_dev * pdev)487bf9c7e3dSThomas Petazzoni void pcibios_bus_add_device(struct pci_dev *pdev)
488bf9c7e3dSThomas Petazzoni {
489e0d07278SJim Quinlan 	dma_direct_set_offset(&pdev->dev, __pa(memory_start),
490e0d07278SJim Quinlan 			      __pa(memory_start) - memstart, memsize);
491bf9c7e3dSThomas Petazzoni }
492bf9c7e3dSThomas Petazzoni 
sh7786_pcie_core_init(void)493c524ebf5SPaul Mundt static int __init sh7786_pcie_core_init(void)
4945713e602SPaul Mundt {
4955713e602SPaul Mundt 	/* Return the number of ports */
4965713e602SPaul Mundt 	return test_mode_pin(MODE_PIN12) ? 3 : 2;
4975713e602SPaul Mundt }
4985713e602SPaul Mundt 
sh7786_pcie_init_hw(void * data,async_cookie_t cookie)4991da09c43SPaul Mundt static void __init sh7786_pcie_init_hw(void *data, async_cookie_t cookie)
5005713e602SPaul Mundt {
5011da09c43SPaul Mundt 	struct sh7786_pcie_port *port = data;
5025713e602SPaul Mundt 	int ret;
5035713e602SPaul Mundt 
5045713e602SPaul Mundt 	/*
5055713e602SPaul Mundt 	 * Check if we are configured in endpoint or root complex mode,
5065713e602SPaul Mundt 	 * this is a fixed pin setting that applies to all PCIe ports.
5075713e602SPaul Mundt 	 */
5085713e602SPaul Mundt 	port->endpoint = test_mode_pin(MODE_PIN11);
5095713e602SPaul Mundt 
510c524ebf5SPaul Mundt 	/*
511c524ebf5SPaul Mundt 	 * Setup clocks, needed both for PHY and PCIe registers.
512c524ebf5SPaul Mundt 	 */
513c524ebf5SPaul Mundt 	ret = pcie_clk_init(port);
5141da09c43SPaul Mundt 	if (unlikely(ret < 0)) {
5151da09c43SPaul Mundt 		pr_err("clock initialization failed for port#%d\n",
5161da09c43SPaul Mundt 		       port->index);
5171da09c43SPaul Mundt 		return;
5181da09c43SPaul Mundt 	}
519c524ebf5SPaul Mundt 
520c524ebf5SPaul Mundt 	ret = phy_init(port);
5211da09c43SPaul Mundt 	if (unlikely(ret < 0)) {
5221da09c43SPaul Mundt 		pr_err("phy initialization failed for port#%d\n",
5231da09c43SPaul Mundt 		       port->index);
5241da09c43SPaul Mundt 		return;
5251da09c43SPaul Mundt 	}
526c524ebf5SPaul Mundt 
5275713e602SPaul Mundt 	ret = pcie_init(port);
5281da09c43SPaul Mundt 	if (unlikely(ret < 0)) {
5291da09c43SPaul Mundt 		pr_err("core initialization failed for port#%d\n",
5301da09c43SPaul Mundt 			       port->index);
5311da09c43SPaul Mundt 		return;
5321da09c43SPaul Mundt 	}
5335713e602SPaul Mundt 
5341da09c43SPaul Mundt 	/* In the interest of preserving device ordering, synchronize */
5351da09c43SPaul Mundt 	async_synchronize_cookie(cookie);
5361da09c43SPaul Mundt 
5371da09c43SPaul Mundt 	register_pci_controller(port->hose);
5385713e602SPaul Mundt }
5395713e602SPaul Mundt 
5405713e602SPaul Mundt static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
5415713e602SPaul Mundt 	.core_init	= sh7786_pcie_core_init,
5425713e602SPaul Mundt 	.port_init_hw	= sh7786_pcie_init_hw,
5435713e602SPaul Mundt };
5445713e602SPaul Mundt 
sh7786_pcie_init(void)5455713e602SPaul Mundt static int __init sh7786_pcie_init(void)
5465713e602SPaul Mundt {
547b6b77b2dSPaul Mundt 	struct clk *platclk;
548d62e9bf5SThomas Petazzoni 	u32 mm_sel;
5491da09c43SPaul Mundt 	int i;
5505713e602SPaul Mundt 
5513b554c33SMatt Fleming 	printk(KERN_NOTICE "PCI: Starting initialization.\n");
5525713e602SPaul Mundt 
5535713e602SPaul Mundt 	sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
5545713e602SPaul Mundt 
5555713e602SPaul Mundt 	nr_ports = sh7786_pcie_hwops->core_init();
5565713e602SPaul Mundt 	BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
5575713e602SPaul Mundt 
5585713e602SPaul Mundt 	if (unlikely(nr_ports == 0))
5595713e602SPaul Mundt 		return -ENODEV;
5605713e602SPaul Mundt 
5616396bb22SKees Cook 	sh7786_pcie_ports = kcalloc(nr_ports, sizeof(struct sh7786_pcie_port),
5625713e602SPaul Mundt 				    GFP_KERNEL);
5635713e602SPaul Mundt 	if (unlikely(!sh7786_pcie_ports))
5645713e602SPaul Mundt 		return -ENOMEM;
5655713e602SPaul Mundt 
566b6b77b2dSPaul Mundt 	/*
567b6b77b2dSPaul Mundt 	 * Fetch any optional platform clock associated with this block.
568b6b77b2dSPaul Mundt 	 *
569b6b77b2dSPaul Mundt 	 * This is a rather nasty hack for boards with spec-mocking FPGAs
570b6b77b2dSPaul Mundt 	 * that have a secondary set of clocks outside of the on-chip
571b6b77b2dSPaul Mundt 	 * ones that need to be accounted for before there is any chance
572b6b77b2dSPaul Mundt 	 * of touching the existing MSTP bits or CPG clocks.
573b6b77b2dSPaul Mundt 	 */
574b6b77b2dSPaul Mundt 	platclk = clk_get(NULL, "pcie_plat_clk");
575b6b77b2dSPaul Mundt 	if (IS_ERR(platclk)) {
576b6b77b2dSPaul Mundt 		/* Sane hardware should probably get a WARN_ON.. */
577b6b77b2dSPaul Mundt 		platclk = NULL;
578b6b77b2dSPaul Mundt 	}
579b6b77b2dSPaul Mundt 
580b6b77b2dSPaul Mundt 	clk_enable(platclk);
581b6b77b2dSPaul Mundt 
582d62e9bf5SThomas Petazzoni 	mm_sel = sh7786_mm_sel();
583d62e9bf5SThomas Petazzoni 
584d62e9bf5SThomas Petazzoni 	/*
585d62e9bf5SThomas Petazzoni 	 * Depending on the MMSELR register value, the PCIe0 MEM 1
586d62e9bf5SThomas Petazzoni 	 * area may not be available. See Table 13.11 of the SH7786
587d62e9bf5SThomas Petazzoni 	 * datasheet.
588d62e9bf5SThomas Petazzoni 	 */
589d62e9bf5SThomas Petazzoni 	if (mm_sel != 1 && mm_sel != 2 && mm_sel != 5 && mm_sel != 6)
590d62e9bf5SThomas Petazzoni 		sh7786_pci0_resources[2].flags |= IORESOURCE_DISABLED;
591d62e9bf5SThomas Petazzoni 
5925713e602SPaul Mundt 	printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
5935713e602SPaul Mundt 
5945713e602SPaul Mundt 	for (i = 0; i < nr_ports; i++) {
5955713e602SPaul Mundt 		struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
5965713e602SPaul Mundt 
5975713e602SPaul Mundt 		port->index		= i;
5985713e602SPaul Mundt 		port->hose		= sh7786_pci_channels + i;
5997561f2ddSPaul Mundt 		port->hose->io_map_base	= port->hose->resources[0].start;
6005713e602SPaul Mundt 
6011da09c43SPaul Mundt 		async_schedule(sh7786_pcie_hwops->port_init_hw, port);
602b6b77b2dSPaul Mundt 	}
6035713e602SPaul Mundt 
604cd7bb53fSPaul Mundt 	async_synchronize_full();
605cd7bb53fSPaul Mundt 
6065713e602SPaul Mundt 	return 0;
6075713e602SPaul Mundt }
6085713e602SPaul Mundt arch_initcall(sh7786_pcie_init);
609