blob: 96e9b058aa1d9afb0245004fc8843a279b30a0eb [file] [log] [blame]
Paul Mundt5713e602009-06-17 18:20:48 +09001/*
2 * Low-Level PCI Express Support for the SH7786
3 *
Paul Mundt7561f2d2010-02-08 16:36:56 +09004 * Copyright (C) 2009 - 2010 Paul Mundt
Paul Mundt5713e602009-06-17 18:20:48 +09005 *
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
8 * for more details.
9 */
10#include <linux/pci.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/io.h>
14#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Mundtc524ebf2010-09-20 18:45:11 +090016#include <linux/clk.h>
17#include <linux/sh_clk.h>
Paul Mundt5713e602009-06-17 18:20:48 +090018#include "pcie-sh7786.h"
19#include <asm/sizes.h>
20
21struct sh7786_pcie_port {
22 struct pci_channel *hose;
Paul Mundtc524ebf2010-09-20 18:45:11 +090023 struct clk *fclk, phy_clk;
Paul Mundt5713e602009-06-17 18:20:48 +090024 unsigned int index;
25 int endpoint;
26 int link;
27};
28
29static struct sh7786_pcie_port *sh7786_pcie_ports;
30static unsigned int nr_ports;
31
32static struct sh7786_pcie_hwops {
33 int (*core_init)(void);
34 int (*port_init_hw)(struct sh7786_pcie_port *port);
35} *sh7786_pcie_hwops;
36
Paul Mundt7561f2d2010-02-08 16:36:56 +090037static struct resource sh7786_pci0_resources[] = {
Paul Mundt5713e602009-06-17 18:20:48 +090038 {
Paul Mundt7561f2d2010-02-08 16:36:56 +090039 .name = "PCIe0 IO",
40 .start = 0xfd000000,
41 .end = 0xfd000000 + SZ_8M - 1,
42 .flags = IORESOURCE_IO,
43 }, {
44 .name = "PCIe0 MEM 0",
45 .start = 0xc0000000,
46 .end = 0xc0000000 + SZ_512M - 1,
47 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
48 }, {
49 .name = "PCIe0 MEM 1",
50 .start = 0x10000000,
51 .end = 0x10000000 + SZ_64M - 1,
Paul Mundt5713e602009-06-17 18:20:48 +090052 .flags = IORESOURCE_MEM,
53 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090054 .name = "PCIe0 MEM 2",
55 .start = 0xfe100000,
56 .end = 0xfe100000 + SZ_1M - 1,
Paul Mundt1c3bb382010-09-07 17:07:05 +090057 .flags = IORESOURCE_MEM,
Paul Mundt5713e602009-06-17 18:20:48 +090058 },
59};
60
Paul Mundt7561f2d2010-02-08 16:36:56 +090061static struct resource sh7786_pci1_resources[] = {
62 {
63 .name = "PCIe1 IO",
64 .start = 0xfd800000,
65 .end = 0xfd800000 + SZ_8M - 1,
66 .flags = IORESOURCE_IO,
67 }, {
68 .name = "PCIe1 MEM 0",
69 .start = 0xa0000000,
70 .end = 0xa0000000 + SZ_512M - 1,
71 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
72 }, {
73 .name = "PCIe1 MEM 1",
74 .start = 0x30000000,
75 .end = 0x30000000 + SZ_256M - 1,
76 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
77 }, {
78 .name = "PCIe1 MEM 2",
79 .start = 0xfe300000,
80 .end = 0xfe300000 + SZ_1M - 1,
Paul Mundt1c3bb382010-09-07 17:07:05 +090081 .flags = IORESOURCE_MEM,
Paul Mundt7561f2d2010-02-08 16:36:56 +090082 },
Paul Mundt5713e602009-06-17 18:20:48 +090083};
84
Paul Mundt7561f2d2010-02-08 16:36:56 +090085static struct resource sh7786_pci2_resources[] = {
Paul Mundt5713e602009-06-17 18:20:48 +090086 {
Paul Mundt7561f2d2010-02-08 16:36:56 +090087 .name = "PCIe2 IO",
88 .start = 0xfc800000,
89 .end = 0xfc800000 + SZ_4M - 1,
Paul Mundtf0485192010-09-07 17:05:08 +090090 .flags = IORESOURCE_IO,
Paul Mundt5713e602009-06-17 18:20:48 +090091 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090092 .name = "PCIe2 MEM 0",
93 .start = 0x80000000,
94 .end = 0x80000000 + SZ_512M - 1,
95 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
Paul Mundt5713e602009-06-17 18:20:48 +090096 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090097 .name = "PCIe2 MEM 1",
98 .start = 0x20000000,
99 .end = 0x20000000 + SZ_256M - 1,
100 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
101 }, {
102 .name = "PCIe2 MEM 2",
103 .start = 0xfcd00000,
104 .end = 0xfcd00000 + SZ_1M - 1,
Paul Mundt1c3bb382010-09-07 17:07:05 +0900105 .flags = IORESOURCE_MEM,
Paul Mundt5713e602009-06-17 18:20:48 +0900106 },
107};
108
109extern struct pci_ops sh7786_pci_ops;
110
Paul Mundt7561f2d2010-02-08 16:36:56 +0900111#define DEFINE_CONTROLLER(start, idx) \
112{ \
113 .pci_ops = &sh7786_pci_ops, \
114 .resources = sh7786_pci##idx##_resources, \
115 .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
116 .reg_base = start, \
117 .mem_offset = 0, \
118 .io_offset = 0, \
Paul Mundt5713e602009-06-17 18:20:48 +0900119}
120
121static struct pci_channel sh7786_pci_channels[] = {
122 DEFINE_CONTROLLER(0xfe000000, 0),
123 DEFINE_CONTROLLER(0xfe200000, 1),
124 DEFINE_CONTROLLER(0xfcc00000, 2),
125};
126
Paul Mundtc524ebf2010-09-20 18:45:11 +0900127static struct clk fixed_pciexclkp = {
128 .rate = 100000000, /* 100 MHz reference clock */
129};
130
Paul Mundt2c65d752010-09-20 15:39:54 +0900131static void __devinit sh7786_pci_fixup(struct pci_dev *dev)
132{
133 /*
134 * Prevent enumeration of root complex resources.
135 */
136 if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {
137 int i;
138
139 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
140 dev->resource[i].start = 0;
141 dev->resource[i].end = 0;
142 dev->resource[i].flags = 0;
143 }
144 }
145}
146DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786,
147 sh7786_pci_fixup);
148
Paul Mundtc524ebf2010-09-20 18:45:11 +0900149static int __init phy_wait_for_ack(struct pci_channel *chan)
Paul Mundt5713e602009-06-17 18:20:48 +0900150{
151 unsigned int timeout = 100;
152
153 while (timeout--) {
154 if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
155 return 0;
156
157 udelay(100);
158 }
159
160 return -ETIMEDOUT;
161}
162
Paul Mundtc524ebf2010-09-20 18:45:11 +0900163static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
Paul Mundt5713e602009-06-17 18:20:48 +0900164{
165 unsigned int timeout = 100;
166
167 while (timeout--) {
168 if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
169 return 0;
170
171 udelay(100);
172 }
173
174 return -ETIMEDOUT;
175}
176
Paul Mundtc524ebf2010-09-20 18:45:11 +0900177static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr,
178 unsigned int lane, unsigned int data)
Paul Mundt5713e602009-06-17 18:20:48 +0900179{
Paul Mundt53178d72010-08-20 16:04:59 +0900180 unsigned long phyaddr;
Paul Mundt5713e602009-06-17 18:20:48 +0900181
182 phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
183 ((addr & 0xff) << BITS_ADR);
184
Paul Mundt5713e602009-06-17 18:20:48 +0900185 /* Set write data */
186 pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
187 pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
188
189 phy_wait_for_ack(chan);
190
191 /* Clear command */
Paul Mundt53178d72010-08-20 16:04:59 +0900192 pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
Paul Mundt5713e602009-06-17 18:20:48 +0900193 pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
194
195 phy_wait_for_ack(chan);
Paul Mundt5713e602009-06-17 18:20:48 +0900196}
197
Paul Mundtc524ebf2010-09-20 18:45:11 +0900198static int __init pcie_clk_init(struct sh7786_pcie_port *port)
Paul Mundt5713e602009-06-17 18:20:48 +0900199{
Paul Mundtc524ebf2010-09-20 18:45:11 +0900200 struct pci_channel *chan = port->hose;
201 struct clk *clk;
202 char fclk_name[16];
203 int ret;
204
205 /*
206 * First register the fixed clock
207 */
208 ret = clk_register(&fixed_pciexclkp);
209 if (unlikely(ret != 0))
210 return ret;
211
212 /*
213 * Grab the port's function clock, which the PHY clock depends
214 * on. clock lookups don't help us much at this point, since no
215 * dev_id is available this early. Lame.
216 */
217 snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index);
218
219 port->fclk = clk_get(NULL, fclk_name);
220 if (IS_ERR(port->fclk)) {
221 ret = PTR_ERR(port->fclk);
222 goto err_fclk;
223 }
224
225 clk_enable(port->fclk);
226
227 /*
228 * And now, set up the PHY clock
229 */
230 clk = &port->phy_clk;
231
232 memset(clk, 0, sizeof(struct clk));
233
234 clk->parent = &fixed_pciexclkp;
235 clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR);
236 clk->enable_bit = BITS_CKE;
237
238 ret = sh_clk_mstp32_register(clk, 1);
239 if (unlikely(ret < 0))
240 goto err_phy;
241
242 return 0;
243
244err_phy:
245 clk_disable(port->fclk);
246 clk_put(port->fclk);
247err_fclk:
248 clk_unregister(&fixed_pciexclkp);
249
250 return ret;
251}
252
253static int __init phy_init(struct sh7786_pcie_port *port)
254{
255 struct pci_channel *chan = port->hose;
Paul Mundt5713e602009-06-17 18:20:48 +0900256 unsigned int timeout = 100;
257
Paul Mundtc524ebf2010-09-20 18:45:11 +0900258 clk_enable(&port->phy_clk);
Paul Mundt53178d72010-08-20 16:04:59 +0900259
Paul Mundt5713e602009-06-17 18:20:48 +0900260 /* Initialize the phy */
261 phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
262 phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
263 phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
264 phy_write_reg(chan, 0x65, 0xf, 0x09070907);
265 phy_write_reg(chan, 0x66, 0xf, 0x00000010);
266 phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
267 phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
Paul Mundt53178d72010-08-20 16:04:59 +0900268 phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
Paul Mundt5713e602009-06-17 18:20:48 +0900269
270 /* Deassert Standby */
Paul Mundt53178d72010-08-20 16:04:59 +0900271 phy_write_reg(chan, 0x67, 0x1, 0x00000400);
272
273 /* Disable clock */
Paul Mundtc524ebf2010-09-20 18:45:11 +0900274 clk_disable(&port->phy_clk);
Paul Mundt5713e602009-06-17 18:20:48 +0900275
276 while (timeout--) {
277 if (pci_read_reg(chan, SH4A_PCIEPHYSR))
278 return 0;
279
280 udelay(100);
281 }
282
283 return -ETIMEDOUT;
284}
285
Paul Mundtc524ebf2010-09-20 18:45:11 +0900286static void __init pcie_reset(struct sh7786_pcie_port *port)
Paul Mundt2dbfa1e2010-09-07 16:11:04 +0900287{
288 struct pci_channel *chan = port->hose;
289
290 pci_write_reg(chan, 1, SH4A_PCIESRSTR);
291 pci_write_reg(chan, 0, SH4A_PCIETCTLR);
292 pci_write_reg(chan, 0, SH4A_PCIESRSTR);
293 pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);
294}
295
Paul Mundtc524ebf2010-09-20 18:45:11 +0900296static int __init pcie_init(struct sh7786_pcie_port *port)
Paul Mundt5713e602009-06-17 18:20:48 +0900297{
298 struct pci_channel *chan = port->hose;
299 unsigned int data;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900300 phys_addr_t memphys;
301 size_t memsize;
Paul Mundtda03a632010-09-07 17:03:10 +0900302 int ret, i, win;
Paul Mundt5713e602009-06-17 18:20:48 +0900303
304 /* Begin initialization */
Paul Mundt2dbfa1e2010-09-07 16:11:04 +0900305 pcie_reset(port);
Paul Mundt5713e602009-06-17 18:20:48 +0900306
Paul Mundt2c65d752010-09-20 15:39:54 +0900307 /*
308 * Initial header for port config space is type 1, set the device
309 * class to match. Hardware takes care of propagating the IDSETR
310 * settings, so there is no need to bother with a quirk.
311 */
312 pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI << 16, SH4A_PCIEIDSETR1);
Paul Mundt5713e602009-06-17 18:20:48 +0900313
314 /* Initialize default capabilities. */
315 data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
316 data &= ~(PCI_EXP_FLAGS_TYPE << 16);
317
318 if (port->endpoint)
319 data |= PCI_EXP_TYPE_ENDPOINT << 20;
320 else
321 data |= PCI_EXP_TYPE_ROOT_PORT << 20;
322
323 data |= PCI_CAP_ID_EXP;
324 pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
325
Paul Mundt7578a4c2010-02-10 16:00:58 +0900326 /* Enable data link layer active state reporting */
327 pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
328
329 /* Enable extended sync and ASPM L0s support */
Paul Mundt5713e602009-06-17 18:20:48 +0900330 data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900331 data &= ~PCI_EXP_LNKCTL_ASPMC;
332 data |= PCI_EXP_LNKCTL_ES | 1;
Paul Mundt5713e602009-06-17 18:20:48 +0900333 pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
334
Paul Mundt7578a4c2010-02-10 16:00:58 +0900335 /* Write out the physical slot number */
336 data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
337 data &= ~PCI_EXP_SLTCAP_PSN;
338 data |= (port->index + 1) << 19;
339 pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
340
Paul Mundt5713e602009-06-17 18:20:48 +0900341 /* Set the completion timer timeout to the maximum 32ms. */
342 data = pci_read_reg(chan, SH4A_PCIETLCTLR);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900343 data &= ~0x3f00;
Paul Mundt5713e602009-06-17 18:20:48 +0900344 data |= 0x32 << 8;
345 pci_write_reg(chan, data, SH4A_PCIETLCTLR);
346
347 /*
348 * Set fast training sequences to the maximum 255,
349 * and enable MAC data scrambling.
350 */
351 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
352 data &= ~PCIEMACCTLR_SCR_DIS;
353 data |= (0xff << 16);
354 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
355
Paul Mundt7578a4c2010-02-10 16:00:58 +0900356 memphys = __pa(memory_start);
357 memsize = roundup_pow_of_two(memory_end - memory_start);
358
359 /*
360 * If there's more than 512MB of memory, we need to roll over to
361 * LAR1/LAMR1.
362 */
363 if (memsize > SZ_512M) {
Paul Mundtcecf48e2010-09-20 17:10:02 +0900364 pci_write_reg(chan, memphys + SZ_512M, SH4A_PCIELAR1);
365 pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1,
366 SH4A_PCIELAMR1);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900367 memsize = SZ_512M;
368 } else {
369 /*
370 * Otherwise just zero it out and disable it.
371 */
Paul Mundtcecf48e2010-09-20 17:10:02 +0900372 pci_write_reg(chan, 0, SH4A_PCIELAR1);
373 pci_write_reg(chan, 0, SH4A_PCIELAMR1);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900374 }
375
376 /*
377 * LAR0/LAMR0 covers up to the first 512MB, which is enough to
378 * cover all of lowmem on most platforms.
379 */
Paul Mundtcecf48e2010-09-20 17:10:02 +0900380 pci_write_reg(chan, memphys, SH4A_PCIELAR0);
381 pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900382
Paul Mundt5713e602009-06-17 18:20:48 +0900383 /* Finish initialization */
384 data = pci_read_reg(chan, SH4A_PCIETCTLR);
385 data |= 0x1;
386 pci_write_reg(chan, data, SH4A_PCIETCTLR);
387
Paul Mundt81df84f2010-09-19 13:57:51 +0900388 /* Let things settle down a bit.. */
389 mdelay(100);
390
Paul Mundt5713e602009-06-17 18:20:48 +0900391 /* Enable DL_Active Interrupt generation */
392 data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
393 data |= PCIEDLINTENR_DLL_ACT_ENABLE;
394 pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
395
396 /* Disable MAC data scrambling. */
397 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
398 data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
399 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
400
Paul Mundtbd792ae2010-09-20 16:12:58 +0900401 /*
402 * This will timeout if we don't have a link, but we permit the
403 * port to register anyways in order to support hotplug on future
404 * hardware.
405 */
Paul Mundt5713e602009-06-17 18:20:48 +0900406 ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
Paul Mundt5713e602009-06-17 18:20:48 +0900407
Paul Mundt7578a4c2010-02-10 16:00:58 +0900408 data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
409 data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
410 data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
411 (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
412 pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
413
Paul Mundt5713e602009-06-17 18:20:48 +0900414 pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
415 pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
Paul Mundt5713e602009-06-17 18:20:48 +0900416
417 wmb();
418
Paul Mundtbd792ae2010-09-20 16:12:58 +0900419 if (ret == 0) {
420 data = pci_read_reg(chan, SH4A_PCIEMACSR);
421 printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n",
422 port->index, (data >> 20) & 0x3f);
423 } else
424 printk(KERN_NOTICE "PCI: PCIe#%d link down\n",
425 port->index);
Paul Mundt5713e602009-06-17 18:20:48 +0900426
Paul Mundtda03a632010-09-07 17:03:10 +0900427 for (i = win = 0; i < chan->nr_resources; i++) {
Paul Mundt7578a4c2010-02-10 16:00:58 +0900428 struct resource *res = chan->resources + i;
429 resource_size_t size;
Paul Mundtcecf48e2010-09-20 17:10:02 +0900430 u32 mask;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900431
Paul Mundtda03a632010-09-07 17:03:10 +0900432 /*
433 * We can't use the 32-bit mode windows in legacy 29-bit
434 * mode, so just skip them entirely.
435 */
436 if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())
437 continue;
438
439 pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900440
Paul Mundt7578a4c2010-02-10 16:00:58 +0900441 /*
442 * The PAMR mask is calculated in units of 256kB, which
443 * keeps things pretty simple.
444 */
Paul Mundtcecf48e2010-09-20 17:10:02 +0900445 size = resource_size(res);
446 mask = (roundup_pow_of_two(size) / SZ_256K) - 1;
447 pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900448
Paul Mundta80be1682010-10-15 06:15:56 +0900449 pci_write_reg(chan, upper_32_bits(res->start),
Paul Mundtcecf48e2010-09-20 17:10:02 +0900450 SH4A_PCIEPARH(win));
Paul Mundta80be1682010-10-15 06:15:56 +0900451 pci_write_reg(chan, lower_32_bits(res->start),
Paul Mundtcecf48e2010-09-20 17:10:02 +0900452 SH4A_PCIEPARL(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900453
Paul Mundtcecf48e2010-09-20 17:10:02 +0900454 mask = MASK_PARE;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900455 if (res->flags & IORESOURCE_IO)
Paul Mundtcecf48e2010-09-20 17:10:02 +0900456 mask |= MASK_SPC;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900457
Paul Mundtcecf48e2010-09-20 17:10:02 +0900458 pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win));
Paul Mundtda03a632010-09-07 17:03:10 +0900459
460 win++;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900461 }
Paul Mundt5713e602009-06-17 18:20:48 +0900462
463 return 0;
464}
465
466int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
467{
468 return 71;
469}
470
Paul Mundtc524ebf2010-09-20 18:45:11 +0900471static int __init sh7786_pcie_core_init(void)
Paul Mundt5713e602009-06-17 18:20:48 +0900472{
473 /* Return the number of ports */
474 return test_mode_pin(MODE_PIN12) ? 3 : 2;
475}
476
Paul Mundtc524ebf2010-09-20 18:45:11 +0900477static int __init sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
Paul Mundt5713e602009-06-17 18:20:48 +0900478{
479 int ret;
480
Paul Mundt5713e602009-06-17 18:20:48 +0900481 /*
482 * Check if we are configured in endpoint or root complex mode,
483 * this is a fixed pin setting that applies to all PCIe ports.
484 */
485 port->endpoint = test_mode_pin(MODE_PIN11);
486
Paul Mundtc524ebf2010-09-20 18:45:11 +0900487 /*
488 * Setup clocks, needed both for PHY and PCIe registers.
489 */
490 ret = pcie_clk_init(port);
491 if (unlikely(ret < 0))
492 return ret;
493
494 ret = phy_init(port);
495 if (unlikely(ret < 0))
496 return ret;
497
Paul Mundt5713e602009-06-17 18:20:48 +0900498 ret = pcie_init(port);
499 if (unlikely(ret < 0))
500 return ret;
501
Paul Mundtbcf39352010-02-01 13:11:25 +0900502 return register_pci_controller(port->hose);
Paul Mundt5713e602009-06-17 18:20:48 +0900503}
504
505static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
506 .core_init = sh7786_pcie_core_init,
507 .port_init_hw = sh7786_pcie_init_hw,
508};
509
510static int __init sh7786_pcie_init(void)
511{
Paul Mundtb6b77b22010-10-14 08:44:55 +0900512 struct clk *platclk;
Paul Mundt5713e602009-06-17 18:20:48 +0900513 int ret = 0, i;
514
Matt Fleming3b554c32010-06-19 00:01:03 +0100515 printk(KERN_NOTICE "PCI: Starting initialization.\n");
Paul Mundt5713e602009-06-17 18:20:48 +0900516
517 sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
518
519 nr_ports = sh7786_pcie_hwops->core_init();
520 BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
521
522 if (unlikely(nr_ports == 0))
523 return -ENODEV;
524
525 sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
526 GFP_KERNEL);
527 if (unlikely(!sh7786_pcie_ports))
528 return -ENOMEM;
529
Paul Mundtb6b77b22010-10-14 08:44:55 +0900530 /*
531 * Fetch any optional platform clock associated with this block.
532 *
533 * This is a rather nasty hack for boards with spec-mocking FPGAs
534 * that have a secondary set of clocks outside of the on-chip
535 * ones that need to be accounted for before there is any chance
536 * of touching the existing MSTP bits or CPG clocks.
537 */
538 platclk = clk_get(NULL, "pcie_plat_clk");
539 if (IS_ERR(platclk)) {
540 /* Sane hardware should probably get a WARN_ON.. */
541 platclk = NULL;
542 }
543
544 clk_enable(platclk);
545
Paul Mundt5713e602009-06-17 18:20:48 +0900546 printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
547
548 for (i = 0; i < nr_ports; i++) {
549 struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
550
551 port->index = i;
552 port->hose = sh7786_pci_channels + i;
Paul Mundt7561f2d2010-02-08 16:36:56 +0900553 port->hose->io_map_base = port->hose->resources[0].start;
Paul Mundt5713e602009-06-17 18:20:48 +0900554
555 ret |= sh7786_pcie_hwops->port_init_hw(port);
556 }
557
Paul Mundtb6b77b22010-10-14 08:44:55 +0900558 if (unlikely(ret)) {
559 clk_disable(platclk);
560 clk_put(platclk);
Paul Mundt5713e602009-06-17 18:20:48 +0900561 return ret;
Paul Mundtb6b77b22010-10-14 08:44:55 +0900562 }
Paul Mundt5713e602009-06-17 18:20:48 +0900563
564 return 0;
565}
566arch_initcall(sh7786_pcie_init);