blob: c1e862af9b634e7ce8243b1d35101c14fba146f1 [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 Mundt5713e602009-06-17 18:20:48 +090016#include "pcie-sh7786.h"
17#include <asm/sizes.h>
18
19struct sh7786_pcie_port {
20 struct pci_channel *hose;
21 unsigned int index;
22 int endpoint;
23 int link;
24};
25
26static struct sh7786_pcie_port *sh7786_pcie_ports;
27static unsigned int nr_ports;
28
29static struct sh7786_pcie_hwops {
30 int (*core_init)(void);
31 int (*port_init_hw)(struct sh7786_pcie_port *port);
32} *sh7786_pcie_hwops;
33
Paul Mundt7561f2d2010-02-08 16:36:56 +090034static struct resource sh7786_pci0_resources[] = {
Paul Mundt5713e602009-06-17 18:20:48 +090035 {
Paul Mundt7561f2d2010-02-08 16:36:56 +090036 .name = "PCIe0 IO",
37 .start = 0xfd000000,
38 .end = 0xfd000000 + SZ_8M - 1,
39 .flags = IORESOURCE_IO,
40 }, {
41 .name = "PCIe0 MEM 0",
42 .start = 0xc0000000,
43 .end = 0xc0000000 + SZ_512M - 1,
44 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
45 }, {
46 .name = "PCIe0 MEM 1",
47 .start = 0x10000000,
48 .end = 0x10000000 + SZ_64M - 1,
Paul Mundt5713e602009-06-17 18:20:48 +090049 .flags = IORESOURCE_MEM,
50 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090051 .name = "PCIe0 MEM 2",
52 .start = 0xfe100000,
53 .end = 0xfe100000 + SZ_1M - 1,
Paul Mundt5713e602009-06-17 18:20:48 +090054 },
55};
56
Paul Mundt7561f2d2010-02-08 16:36:56 +090057static struct resource sh7786_pci1_resources[] = {
58 {
59 .name = "PCIe1 IO",
60 .start = 0xfd800000,
61 .end = 0xfd800000 + SZ_8M - 1,
62 .flags = IORESOURCE_IO,
63 }, {
64 .name = "PCIe1 MEM 0",
65 .start = 0xa0000000,
66 .end = 0xa0000000 + SZ_512M - 1,
67 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
68 }, {
69 .name = "PCIe1 MEM 1",
70 .start = 0x30000000,
71 .end = 0x30000000 + SZ_256M - 1,
72 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
73 }, {
74 .name = "PCIe1 MEM 2",
75 .start = 0xfe300000,
76 .end = 0xfe300000 + SZ_1M - 1,
77 },
Paul Mundt5713e602009-06-17 18:20:48 +090078};
79
Paul Mundt7561f2d2010-02-08 16:36:56 +090080static struct resource sh7786_pci2_resources[] = {
Paul Mundt5713e602009-06-17 18:20:48 +090081 {
Paul Mundt7561f2d2010-02-08 16:36:56 +090082 .name = "PCIe2 IO",
83 .start = 0xfc800000,
84 .end = 0xfc800000 + SZ_4M - 1,
Paul Mundtf0485192010-09-07 17:05:08 +090085 .flags = IORESOURCE_IO,
Paul Mundt5713e602009-06-17 18:20:48 +090086 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090087 .name = "PCIe2 MEM 0",
88 .start = 0x80000000,
89 .end = 0x80000000 + SZ_512M - 1,
90 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
Paul Mundt5713e602009-06-17 18:20:48 +090091 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090092 .name = "PCIe2 MEM 1",
93 .start = 0x20000000,
94 .end = 0x20000000 + SZ_256M - 1,
95 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
96 }, {
97 .name = "PCIe2 MEM 2",
98 .start = 0xfcd00000,
99 .end = 0xfcd00000 + SZ_1M - 1,
Paul Mundt5713e602009-06-17 18:20:48 +0900100 },
101};
102
103extern struct pci_ops sh7786_pci_ops;
104
Paul Mundt7561f2d2010-02-08 16:36:56 +0900105#define DEFINE_CONTROLLER(start, idx) \
106{ \
107 .pci_ops = &sh7786_pci_ops, \
108 .resources = sh7786_pci##idx##_resources, \
109 .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
110 .reg_base = start, \
111 .mem_offset = 0, \
112 .io_offset = 0, \
Paul Mundt5713e602009-06-17 18:20:48 +0900113}
114
115static struct pci_channel sh7786_pci_channels[] = {
116 DEFINE_CONTROLLER(0xfe000000, 0),
117 DEFINE_CONTROLLER(0xfe200000, 1),
118 DEFINE_CONTROLLER(0xfcc00000, 2),
119};
120
121static int phy_wait_for_ack(struct pci_channel *chan)
122{
123 unsigned int timeout = 100;
124
125 while (timeout--) {
126 if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
127 return 0;
128
129 udelay(100);
130 }
131
132 return -ETIMEDOUT;
133}
134
135static int pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
136{
137 unsigned int timeout = 100;
138
139 while (timeout--) {
140 if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
141 return 0;
142
143 udelay(100);
144 }
145
146 return -ETIMEDOUT;
147}
148
149static void phy_write_reg(struct pci_channel *chan, unsigned int addr,
150 unsigned int lane, unsigned int data)
151{
Paul Mundt53178d72010-08-20 16:04:59 +0900152 unsigned long phyaddr;
Paul Mundt5713e602009-06-17 18:20:48 +0900153
154 phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
155 ((addr & 0xff) << BITS_ADR);
156
Paul Mundt5713e602009-06-17 18:20:48 +0900157 /* Set write data */
158 pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
159 pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
160
161 phy_wait_for_ack(chan);
162
163 /* Clear command */
Paul Mundt53178d72010-08-20 16:04:59 +0900164 pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
Paul Mundt5713e602009-06-17 18:20:48 +0900165 pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
166
167 phy_wait_for_ack(chan);
Paul Mundt5713e602009-06-17 18:20:48 +0900168}
169
170static int phy_init(struct pci_channel *chan)
171{
Paul Mundt53178d72010-08-20 16:04:59 +0900172 unsigned long ctrl;
Paul Mundt5713e602009-06-17 18:20:48 +0900173 unsigned int timeout = 100;
174
Paul Mundt53178d72010-08-20 16:04:59 +0900175 /* Enable clock */
176 ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
177 ctrl |= (1 << BITS_CKE);
178 pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
179
Paul Mundt5713e602009-06-17 18:20:48 +0900180 /* Initialize the phy */
181 phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
182 phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
183 phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
184 phy_write_reg(chan, 0x65, 0xf, 0x09070907);
185 phy_write_reg(chan, 0x66, 0xf, 0x00000010);
186 phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
187 phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
Paul Mundt53178d72010-08-20 16:04:59 +0900188 phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
Paul Mundt5713e602009-06-17 18:20:48 +0900189
190 /* Deassert Standby */
Paul Mundt53178d72010-08-20 16:04:59 +0900191 phy_write_reg(chan, 0x67, 0x1, 0x00000400);
192
193 /* Disable clock */
194 ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
195 ctrl &= ~(1 << BITS_CKE);
196 pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
Paul Mundt5713e602009-06-17 18:20:48 +0900197
198 while (timeout--) {
199 if (pci_read_reg(chan, SH4A_PCIEPHYSR))
200 return 0;
201
202 udelay(100);
203 }
204
205 return -ETIMEDOUT;
206}
207
Paul Mundt2dbfa1e2010-09-07 16:11:04 +0900208static void pcie_reset(struct sh7786_pcie_port *port)
209{
210 struct pci_channel *chan = port->hose;
211
212 pci_write_reg(chan, 1, SH4A_PCIESRSTR);
213 pci_write_reg(chan, 0, SH4A_PCIETCTLR);
214 pci_write_reg(chan, 0, SH4A_PCIESRSTR);
215 pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);
216}
217
Paul Mundt5713e602009-06-17 18:20:48 +0900218static int pcie_init(struct sh7786_pcie_port *port)
219{
220 struct pci_channel *chan = port->hose;
221 unsigned int data;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900222 phys_addr_t memphys;
223 size_t memsize;
Paul Mundtda03a632010-09-07 17:03:10 +0900224 int ret, i, win;
Paul Mundt5713e602009-06-17 18:20:48 +0900225
226 /* Begin initialization */
Paul Mundt2dbfa1e2010-09-07 16:11:04 +0900227 pcie_reset(port);
Paul Mundt5713e602009-06-17 18:20:48 +0900228
229 /* Initialize as type1. */
230 data = pci_read_reg(chan, SH4A_PCIEPCICONF3);
231 data &= ~(0x7f << 16);
232 data |= PCI_HEADER_TYPE_BRIDGE << 16;
233 pci_write_reg(chan, data, SH4A_PCIEPCICONF3);
234
235 /* Initialize default capabilities. */
236 data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
237 data &= ~(PCI_EXP_FLAGS_TYPE << 16);
238
239 if (port->endpoint)
240 data |= PCI_EXP_TYPE_ENDPOINT << 20;
241 else
242 data |= PCI_EXP_TYPE_ROOT_PORT << 20;
243
244 data |= PCI_CAP_ID_EXP;
245 pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
246
Paul Mundt7578a4c2010-02-10 16:00:58 +0900247 /* Enable data link layer active state reporting */
248 pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
249
250 /* Enable extended sync and ASPM L0s support */
Paul Mundt5713e602009-06-17 18:20:48 +0900251 data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900252 data &= ~PCI_EXP_LNKCTL_ASPMC;
253 data |= PCI_EXP_LNKCTL_ES | 1;
Paul Mundt5713e602009-06-17 18:20:48 +0900254 pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
255
Paul Mundt7578a4c2010-02-10 16:00:58 +0900256 /* Write out the physical slot number */
257 data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
258 data &= ~PCI_EXP_SLTCAP_PSN;
259 data |= (port->index + 1) << 19;
260 pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
261
Paul Mundt5713e602009-06-17 18:20:48 +0900262 /* Set the completion timer timeout to the maximum 32ms. */
263 data = pci_read_reg(chan, SH4A_PCIETLCTLR);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900264 data &= ~0x3f00;
Paul Mundt5713e602009-06-17 18:20:48 +0900265 data |= 0x32 << 8;
266 pci_write_reg(chan, data, SH4A_PCIETLCTLR);
267
268 /*
269 * Set fast training sequences to the maximum 255,
270 * and enable MAC data scrambling.
271 */
272 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
273 data &= ~PCIEMACCTLR_SCR_DIS;
274 data |= (0xff << 16);
275 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
276
Paul Mundt7578a4c2010-02-10 16:00:58 +0900277 memphys = __pa(memory_start);
278 memsize = roundup_pow_of_two(memory_end - memory_start);
279
280 /*
281 * If there's more than 512MB of memory, we need to roll over to
282 * LAR1/LAMR1.
283 */
284 if (memsize > SZ_512M) {
285 __raw_writel(memphys + SZ_512M, chan->reg_base + SH4A_PCIELAR1);
286 __raw_writel(((memsize - SZ_512M) - SZ_256) | 1,
287 chan->reg_base + SH4A_PCIELAMR1);
288 memsize = SZ_512M;
289 } else {
290 /*
291 * Otherwise just zero it out and disable it.
292 */
293 __raw_writel(0, chan->reg_base + SH4A_PCIELAR1);
294 __raw_writel(0, chan->reg_base + SH4A_PCIELAMR1);
295 }
296
297 /*
298 * LAR0/LAMR0 covers up to the first 512MB, which is enough to
299 * cover all of lowmem on most platforms.
300 */
301 __raw_writel(memphys, chan->reg_base + SH4A_PCIELAR0);
302 __raw_writel((memsize - SZ_256) | 1, chan->reg_base + SH4A_PCIELAMR0);
303
Paul Mundt53178d72010-08-20 16:04:59 +0900304 __raw_writel(memphys, chan->reg_base + SH4A_PCIEPCICONF4);
305 __raw_writel(0, chan->reg_base + SH4A_PCIEPCICONF5);
306
Paul Mundt5713e602009-06-17 18:20:48 +0900307 /* Finish initialization */
308 data = pci_read_reg(chan, SH4A_PCIETCTLR);
309 data |= 0x1;
310 pci_write_reg(chan, data, SH4A_PCIETCTLR);
311
312 /* Enable DL_Active Interrupt generation */
313 data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
314 data |= PCIEDLINTENR_DLL_ACT_ENABLE;
315 pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
316
317 /* Disable MAC data scrambling. */
318 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
319 data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
320 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
321
322 ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
323 if (unlikely(ret != 0))
324 return -ENODEV;
325
Paul Mundt7578a4c2010-02-10 16:00:58 +0900326 data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
327 data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
328 data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
329 (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
330 pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
331
Paul Mundt5713e602009-06-17 18:20:48 +0900332 pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
333 pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
Paul Mundt5713e602009-06-17 18:20:48 +0900334
335 wmb();
336
337 data = pci_read_reg(chan, SH4A_PCIEMACSR);
338 printk(KERN_NOTICE "PCI: PCIe#%d link width %d\n",
339 port->index, (data >> 20) & 0x3f);
340
Paul Mundtda03a632010-09-07 17:03:10 +0900341 for (i = win = 0; i < chan->nr_resources; i++) {
Paul Mundt7578a4c2010-02-10 16:00:58 +0900342 struct resource *res = chan->resources + i;
343 resource_size_t size;
344 u32 enable_mask;
345
Paul Mundtda03a632010-09-07 17:03:10 +0900346 /*
347 * We can't use the 32-bit mode windows in legacy 29-bit
348 * mode, so just skip them entirely.
349 */
350 if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())
351 continue;
352
353 pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900354
355 size = resource_size(res);
356
357 /*
358 * The PAMR mask is calculated in units of 256kB, which
359 * keeps things pretty simple.
360 */
361 __raw_writel(((roundup_pow_of_two(size) / SZ_256K) - 1) << 18,
Paul Mundtda03a632010-09-07 17:03:10 +0900362 chan->reg_base + SH4A_PCIEPAMR(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900363
Paul Mundtda03a632010-09-07 17:03:10 +0900364 pci_write_reg(chan, res->start, SH4A_PCIEPARL(win));
365 pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH(win));
Paul Mundt7578a4c2010-02-10 16:00:58 +0900366
367 enable_mask = MASK_PARE;
368 if (res->flags & IORESOURCE_IO)
369 enable_mask |= MASK_SPC;
370
Paul Mundtda03a632010-09-07 17:03:10 +0900371 pci_write_reg(chan, enable_mask, SH4A_PCIEPTCTLR(win));
372
373 win++;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900374 }
Paul Mundt5713e602009-06-17 18:20:48 +0900375
376 return 0;
377}
378
379int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
380{
381 return 71;
382}
383
384static int sh7786_pcie_core_init(void)
385{
386 /* Return the number of ports */
387 return test_mode_pin(MODE_PIN12) ? 3 : 2;
388}
389
390static int __devinit sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
391{
392 int ret;
393
394 ret = phy_init(port->hose);
395 if (unlikely(ret < 0))
396 return ret;
397
398 /*
399 * Check if we are configured in endpoint or root complex mode,
400 * this is a fixed pin setting that applies to all PCIe ports.
401 */
402 port->endpoint = test_mode_pin(MODE_PIN11);
403
404 ret = pcie_init(port);
405 if (unlikely(ret < 0))
406 return ret;
407
Paul Mundtbcf39352010-02-01 13:11:25 +0900408 return register_pci_controller(port->hose);
Paul Mundt5713e602009-06-17 18:20:48 +0900409}
410
411static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
412 .core_init = sh7786_pcie_core_init,
413 .port_init_hw = sh7786_pcie_init_hw,
414};
415
416static int __init sh7786_pcie_init(void)
417{
418 int ret = 0, i;
419
420 printk(KERN_NOTICE "PCI: Starting intialization.\n");
421
422 sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
423
424 nr_ports = sh7786_pcie_hwops->core_init();
425 BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
426
427 if (unlikely(nr_ports == 0))
428 return -ENODEV;
429
430 sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
431 GFP_KERNEL);
432 if (unlikely(!sh7786_pcie_ports))
433 return -ENOMEM;
434
435 printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
436
437 for (i = 0; i < nr_ports; i++) {
438 struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
439
440 port->index = i;
441 port->hose = sh7786_pci_channels + i;
Paul Mundt7561f2d2010-02-08 16:36:56 +0900442 port->hose->io_map_base = port->hose->resources[0].start;
Paul Mundt5713e602009-06-17 18:20:48 +0900443
444 ret |= sh7786_pcie_hwops->port_init_hw(port);
445 }
446
447 if (unlikely(ret))
448 return ret;
449
450 return 0;
451}
452arch_initcall(sh7786_pcie_init);