2 * Low-Level PCI Express Support for the SH7786
4 * Copyright (C) 2009 - 2010 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/pci.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/clk.h>
17 #include <linux/sh_clk.h>
18 #include "pcie-sh7786.h"
19 #include <asm/sizes.h>
20 #include <asm/clock.h>
22 struct sh7786_pcie_port {
23 struct pci_channel *hose;
24 struct clk *fclk, phy_clk;
30 static struct sh7786_pcie_port *sh7786_pcie_ports;
31 static unsigned int nr_ports;
33 static struct sh7786_pcie_hwops {
34 int (*core_init)(void);
35 int (*port_init_hw)(struct sh7786_pcie_port *port);
38 static struct resource sh7786_pci0_resources[] = {
42 .end = 0xfd000000 + SZ_8M - 1,
43 .flags = IORESOURCE_IO,
45 .name = "PCIe0 MEM 0",
47 .end = 0xc0000000 + SZ_512M - 1,
48 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
50 .name = "PCIe0 MEM 1",
52 .end = 0x10000000 + SZ_64M - 1,
53 .flags = IORESOURCE_MEM,
55 .name = "PCIe0 MEM 2",
57 .end = 0xfe100000 + SZ_1M - 1,
58 .flags = IORESOURCE_MEM,
62 static struct resource sh7786_pci1_resources[] = {
66 .end = 0xfd800000 + SZ_8M - 1,
67 .flags = IORESOURCE_IO,
69 .name = "PCIe1 MEM 0",
71 .end = 0xa0000000 + SZ_512M - 1,
72 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
74 .name = "PCIe1 MEM 1",
76 .end = 0x30000000 + SZ_256M - 1,
77 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
79 .name = "PCIe1 MEM 2",
81 .end = 0xfe300000 + SZ_1M - 1,
82 .flags = IORESOURCE_MEM,
86 static struct resource sh7786_pci2_resources[] = {
90 .end = 0xfc800000 + SZ_4M - 1,
91 .flags = IORESOURCE_IO,
93 .name = "PCIe2 MEM 0",
95 .end = 0x80000000 + SZ_512M - 1,
96 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
98 .name = "PCIe2 MEM 1",
100 .end = 0x20000000 + SZ_256M - 1,
101 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
103 .name = "PCIe2 MEM 2",
105 .end = 0xfcd00000 + SZ_1M - 1,
106 .flags = IORESOURCE_MEM,
110 extern struct pci_ops sh7786_pci_ops;
112 #define DEFINE_CONTROLLER(start, idx) \
114 .pci_ops = &sh7786_pci_ops, \
115 .resources = sh7786_pci##idx##_resources, \
116 .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
122 static struct pci_channel sh7786_pci_channels[] = {
123 DEFINE_CONTROLLER(0xfe000000, 0),
124 DEFINE_CONTROLLER(0xfe200000, 1),
125 DEFINE_CONTROLLER(0xfcc00000, 2),
128 static struct clk fixed_pciexclkp = {
129 .rate = 100000000, /* 100 MHz reference clock */
132 static void __devinit sh7786_pci_fixup(struct pci_dev *dev)
135 * Prevent enumeration of root complex resources.
137 if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {
140 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
141 dev->resource[i].start = 0;
142 dev->resource[i].end = 0;
143 dev->resource[i].flags = 0;
147 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786,
150 static int __init phy_wait_for_ack(struct pci_channel *chan)
152 unsigned int timeout = 100;
155 if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
164 static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
166 unsigned int timeout = 100;
169 if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
178 static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr,
179 unsigned int lane, unsigned int data)
181 unsigned long phyaddr;
183 phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
184 ((addr & 0xff) << BITS_ADR);
187 pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
188 pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
190 phy_wait_for_ack(chan);
193 pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
194 pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
196 phy_wait_for_ack(chan);
199 static int __init pcie_clk_init(struct sh7786_pcie_port *port)
201 struct pci_channel *chan = port->hose;
207 * First register the fixed clock
209 ret = clk_register(&fixed_pciexclkp);
210 if (unlikely(ret != 0))
214 * Grab the port's function clock, which the PHY clock depends
215 * on. clock lookups don't help us much at this point, since no
216 * dev_id is available this early. Lame.
218 snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index);
220 port->fclk = clk_get(NULL, fclk_name);
221 if (IS_ERR(port->fclk)) {
222 ret = PTR_ERR(port->fclk);
226 clk_enable(port->fclk);
229 * And now, set up the PHY clock
231 clk = &port->phy_clk;
233 memset(clk, 0, sizeof(struct clk));
235 clk->parent = &fixed_pciexclkp;
236 clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR);
237 clk->enable_bit = BITS_CKE;
239 ret = sh_clk_mstp32_register(clk, 1);
240 if (unlikely(ret < 0))
246 clk_disable(port->fclk);
249 clk_unregister(&fixed_pciexclkp);
254 static int __init phy_init(struct sh7786_pcie_port *port)
256 struct pci_channel *chan = port->hose;
257 unsigned int timeout = 100;
259 clk_enable(&port->phy_clk);
261 /* Initialize the phy */
262 phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
263 phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
264 phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
265 phy_write_reg(chan, 0x65, 0xf, 0x09070907);
266 phy_write_reg(chan, 0x66, 0xf, 0x00000010);
267 phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
268 phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
269 phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
271 /* Deassert Standby */
272 phy_write_reg(chan, 0x67, 0x1, 0x00000400);
275 clk_disable(&port->phy_clk);
278 if (pci_read_reg(chan, SH4A_PCIEPHYSR))
287 static void __init pcie_reset(struct sh7786_pcie_port *port)
289 struct pci_channel *chan = port->hose;
291 pci_write_reg(chan, 1, SH4A_PCIESRSTR);
292 pci_write_reg(chan, 0, SH4A_PCIETCTLR);
293 pci_write_reg(chan, 0, SH4A_PCIESRSTR);
294 pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);
297 static int __init pcie_init(struct sh7786_pcie_port *port)
299 struct pci_channel *chan = port->hose;
305 /* Begin initialization */
309 * Initial header for port config space is type 1, set the device
310 * class to match. Hardware takes care of propagating the IDSETR
311 * settings, so there is no need to bother with a quirk.
313 pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI << 16, SH4A_PCIEIDSETR1);
315 /* Initialize default capabilities. */
316 data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
317 data &= ~(PCI_EXP_FLAGS_TYPE << 16);
320 data |= PCI_EXP_TYPE_ENDPOINT << 20;
322 data |= PCI_EXP_TYPE_ROOT_PORT << 20;
324 data |= PCI_CAP_ID_EXP;
325 pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
327 /* Enable data link layer active state reporting */
328 pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
330 /* Enable extended sync and ASPM L0s support */
331 data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
332 data &= ~PCI_EXP_LNKCTL_ASPMC;
333 data |= PCI_EXP_LNKCTL_ES | 1;
334 pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
336 /* Write out the physical slot number */
337 data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
338 data &= ~PCI_EXP_SLTCAP_PSN;
339 data |= (port->index + 1) << 19;
340 pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
342 /* Set the completion timer timeout to the maximum 32ms. */
343 data = pci_read_reg(chan, SH4A_PCIETLCTLR);
346 pci_write_reg(chan, data, SH4A_PCIETLCTLR);
349 * Set fast training sequences to the maximum 255,
350 * and enable MAC data scrambling.
352 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
353 data &= ~PCIEMACCTLR_SCR_DIS;
354 data |= (0xff << 16);
355 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
357 memphys = __pa(memory_start);
358 memsize = roundup_pow_of_two(memory_end - memory_start);
361 * If there's more than 512MB of memory, we need to roll over to
364 if (memsize > SZ_512M) {
365 pci_write_reg(chan, memphys + SZ_512M, SH4A_PCIELAR1);
366 pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1,
371 * Otherwise just zero it out and disable it.
373 pci_write_reg(chan, 0, SH4A_PCIELAR1);
374 pci_write_reg(chan, 0, SH4A_PCIELAMR1);
378 * LAR0/LAMR0 covers up to the first 512MB, which is enough to
379 * cover all of lowmem on most platforms.
381 pci_write_reg(chan, memphys, SH4A_PCIELAR0);
382 pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0);
384 /* Finish initialization */
385 data = pci_read_reg(chan, SH4A_PCIETCTLR);
387 pci_write_reg(chan, data, SH4A_PCIETCTLR);
389 /* Let things settle down a bit.. */
392 /* Enable DL_Active Interrupt generation */
393 data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
394 data |= PCIEDLINTENR_DLL_ACT_ENABLE;
395 pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
397 /* Disable MAC data scrambling. */
398 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
399 data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
400 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
403 * This will timeout if we don't have a link, but we permit the
404 * port to register anyways in order to support hotplug on future
407 ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
409 data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
410 data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
411 data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
412 (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
413 pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
415 pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
416 pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
421 data = pci_read_reg(chan, SH4A_PCIEMACSR);
422 printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n",
423 port->index, (data >> 20) & 0x3f);
425 printk(KERN_NOTICE "PCI: PCIe#%d link down\n",
428 for (i = win = 0; i < chan->nr_resources; i++) {
429 struct resource *res = chan->resources + i;
430 resource_size_t size;
434 * We can't use the 32-bit mode windows in legacy 29-bit
435 * mode, so just skip them entirely.
437 if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())
440 pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));
443 * The PAMR mask is calculated in units of 256kB, which
444 * keeps things pretty simple.
446 size = resource_size(res);
447 mask = (roundup_pow_of_two(size) / SZ_256K) - 1;
448 pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win));
450 pci_write_reg(chan, RES_TO_U32_HIGH(res->start),
452 pci_write_reg(chan, RES_TO_U32_LOW(res->start),
456 if (res->flags & IORESOURCE_IO)
459 pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win));
467 int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
472 static int __init sh7786_pcie_core_init(void)
474 /* Return the number of ports */
475 return test_mode_pin(MODE_PIN12) ? 3 : 2;
478 static int __init sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
483 * Check if we are configured in endpoint or root complex mode,
484 * this is a fixed pin setting that applies to all PCIe ports.
486 port->endpoint = test_mode_pin(MODE_PIN11);
489 * Setup clocks, needed both for PHY and PCIe registers.
491 ret = pcie_clk_init(port);
492 if (unlikely(ret < 0))
495 ret = phy_init(port);
496 if (unlikely(ret < 0))
499 ret = pcie_init(port);
500 if (unlikely(ret < 0))
503 return register_pci_controller(port->hose);
506 static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
507 .core_init = sh7786_pcie_core_init,
508 .port_init_hw = sh7786_pcie_init_hw,
511 static int __init sh7786_pcie_init(void)
515 printk(KERN_NOTICE "PCI: Starting initialization.\n");
517 sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
519 nr_ports = sh7786_pcie_hwops->core_init();
520 BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
522 if (unlikely(nr_ports == 0))
525 sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
527 if (unlikely(!sh7786_pcie_ports))
530 printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
532 for (i = 0; i < nr_ports; i++) {
533 struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
536 port->hose = sh7786_pci_channels + i;
537 port->hose->io_map_base = port->hose->resources[0].start;
539 ret |= sh7786_pcie_hwops->port_init_hw(port);
547 arch_initcall(sh7786_pcie_init);