blob: 03e6d8217b0cdb015c24cbe206850910b8305608 [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 Mundt5713e602009-06-17 18:20:48 +090085 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090086 .name = "PCIe2 MEM 0",
87 .start = 0x80000000,
88 .end = 0x80000000 + SZ_512M - 1,
89 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
Paul Mundt5713e602009-06-17 18:20:48 +090090 }, {
Paul Mundt7561f2d2010-02-08 16:36:56 +090091 .name = "PCIe2 MEM 1",
92 .start = 0x20000000,
93 .end = 0x20000000 + SZ_256M - 1,
94 .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
95 }, {
96 .name = "PCIe2 MEM 2",
97 .start = 0xfcd00000,
98 .end = 0xfcd00000 + SZ_1M - 1,
Paul Mundt5713e602009-06-17 18:20:48 +090099 },
100};
101
102extern struct pci_ops sh7786_pci_ops;
103
Paul Mundt7561f2d2010-02-08 16:36:56 +0900104#define DEFINE_CONTROLLER(start, idx) \
105{ \
106 .pci_ops = &sh7786_pci_ops, \
107 .resources = sh7786_pci##idx##_resources, \
108 .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
109 .reg_base = start, \
110 .mem_offset = 0, \
111 .io_offset = 0, \
Paul Mundt5713e602009-06-17 18:20:48 +0900112}
113
114static struct pci_channel sh7786_pci_channels[] = {
115 DEFINE_CONTROLLER(0xfe000000, 0),
116 DEFINE_CONTROLLER(0xfe200000, 1),
117 DEFINE_CONTROLLER(0xfcc00000, 2),
118};
119
120static int phy_wait_for_ack(struct pci_channel *chan)
121{
122 unsigned int timeout = 100;
123
124 while (timeout--) {
125 if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
126 return 0;
127
128 udelay(100);
129 }
130
131 return -ETIMEDOUT;
132}
133
134static int pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
135{
136 unsigned int timeout = 100;
137
138 while (timeout--) {
139 if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
140 return 0;
141
142 udelay(100);
143 }
144
145 return -ETIMEDOUT;
146}
147
148static void phy_write_reg(struct pci_channel *chan, unsigned int addr,
149 unsigned int lane, unsigned int data)
150{
Paul Mundt53178d72010-08-20 16:04:59 +0900151 unsigned long phyaddr;
Paul Mundt5713e602009-06-17 18:20:48 +0900152
153 phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
154 ((addr & 0xff) << BITS_ADR);
155
Paul Mundt5713e602009-06-17 18:20:48 +0900156 /* Set write data */
157 pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
158 pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
159
160 phy_wait_for_ack(chan);
161
162 /* Clear command */
Paul Mundt53178d72010-08-20 16:04:59 +0900163 pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
Paul Mundt5713e602009-06-17 18:20:48 +0900164 pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
165
166 phy_wait_for_ack(chan);
Paul Mundt5713e602009-06-17 18:20:48 +0900167}
168
169static int phy_init(struct pci_channel *chan)
170{
Paul Mundt53178d72010-08-20 16:04:59 +0900171 unsigned long ctrl;
Paul Mundt5713e602009-06-17 18:20:48 +0900172 unsigned int timeout = 100;
173
Paul Mundt53178d72010-08-20 16:04:59 +0900174 /* Enable clock */
175 ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
176 ctrl |= (1 << BITS_CKE);
177 pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
178
Paul Mundt5713e602009-06-17 18:20:48 +0900179 /* Initialize the phy */
180 phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
181 phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
182 phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
183 phy_write_reg(chan, 0x65, 0xf, 0x09070907);
184 phy_write_reg(chan, 0x66, 0xf, 0x00000010);
185 phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
186 phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
Paul Mundt53178d72010-08-20 16:04:59 +0900187 phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
Paul Mundt5713e602009-06-17 18:20:48 +0900188
189 /* Deassert Standby */
Paul Mundt53178d72010-08-20 16:04:59 +0900190 phy_write_reg(chan, 0x67, 0x1, 0x00000400);
191
192 /* Disable clock */
193 ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
194 ctrl &= ~(1 << BITS_CKE);
195 pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
Paul Mundt5713e602009-06-17 18:20:48 +0900196
197 while (timeout--) {
198 if (pci_read_reg(chan, SH4A_PCIEPHYSR))
199 return 0;
200
201 udelay(100);
202 }
203
204 return -ETIMEDOUT;
205}
206
207static int pcie_init(struct sh7786_pcie_port *port)
208{
209 struct pci_channel *chan = port->hose;
210 unsigned int data;
Paul Mundt7578a4c2010-02-10 16:00:58 +0900211 phys_addr_t memphys;
212 size_t memsize;
213 int ret, i;
Paul Mundt5713e602009-06-17 18:20:48 +0900214
215 /* Begin initialization */
216 pci_write_reg(chan, 0, SH4A_PCIETCTLR);
217
218 /* Initialize as type1. */
219 data = pci_read_reg(chan, SH4A_PCIEPCICONF3);
220 data &= ~(0x7f << 16);
221 data |= PCI_HEADER_TYPE_BRIDGE << 16;
222 pci_write_reg(chan, data, SH4A_PCIEPCICONF3);
223
224 /* Initialize default capabilities. */
225 data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
226 data &= ~(PCI_EXP_FLAGS_TYPE << 16);
227
228 if (port->endpoint)
229 data |= PCI_EXP_TYPE_ENDPOINT << 20;
230 else
231 data |= PCI_EXP_TYPE_ROOT_PORT << 20;
232
233 data |= PCI_CAP_ID_EXP;
234 pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
235
Paul Mundt7578a4c2010-02-10 16:00:58 +0900236 /* Enable data link layer active state reporting */
237 pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
238
239 /* Enable extended sync and ASPM L0s support */
Paul Mundt5713e602009-06-17 18:20:48 +0900240 data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900241 data &= ~PCI_EXP_LNKCTL_ASPMC;
242 data |= PCI_EXP_LNKCTL_ES | 1;
Paul Mundt5713e602009-06-17 18:20:48 +0900243 pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
244
Paul Mundt7578a4c2010-02-10 16:00:58 +0900245 /* Write out the physical slot number */
246 data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
247 data &= ~PCI_EXP_SLTCAP_PSN;
248 data |= (port->index + 1) << 19;
249 pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
250
Paul Mundt5713e602009-06-17 18:20:48 +0900251 /* Set the completion timer timeout to the maximum 32ms. */
252 data = pci_read_reg(chan, SH4A_PCIETLCTLR);
Paul Mundt7578a4c2010-02-10 16:00:58 +0900253 data &= ~0x3f00;
Paul Mundt5713e602009-06-17 18:20:48 +0900254 data |= 0x32 << 8;
255 pci_write_reg(chan, data, SH4A_PCIETLCTLR);
256
257 /*
258 * Set fast training sequences to the maximum 255,
259 * and enable MAC data scrambling.
260 */
261 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
262 data &= ~PCIEMACCTLR_SCR_DIS;
263 data |= (0xff << 16);
264 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
265
Paul Mundt7578a4c2010-02-10 16:00:58 +0900266 memphys = __pa(memory_start);
267 memsize = roundup_pow_of_two(memory_end - memory_start);
268
269 /*
270 * If there's more than 512MB of memory, we need to roll over to
271 * LAR1/LAMR1.
272 */
273 if (memsize > SZ_512M) {
274 __raw_writel(memphys + SZ_512M, chan->reg_base + SH4A_PCIELAR1);
275 __raw_writel(((memsize - SZ_512M) - SZ_256) | 1,
276 chan->reg_base + SH4A_PCIELAMR1);
277 memsize = SZ_512M;
278 } else {
279 /*
280 * Otherwise just zero it out and disable it.
281 */
282 __raw_writel(0, chan->reg_base + SH4A_PCIELAR1);
283 __raw_writel(0, chan->reg_base + SH4A_PCIELAMR1);
284 }
285
286 /*
287 * LAR0/LAMR0 covers up to the first 512MB, which is enough to
288 * cover all of lowmem on most platforms.
289 */
290 __raw_writel(memphys, chan->reg_base + SH4A_PCIELAR0);
291 __raw_writel((memsize - SZ_256) | 1, chan->reg_base + SH4A_PCIELAMR0);
292
Paul Mundt53178d72010-08-20 16:04:59 +0900293 __raw_writel(memphys, chan->reg_base + SH4A_PCIEPCICONF4);
294 __raw_writel(0, chan->reg_base + SH4A_PCIEPCICONF5);
295
Paul Mundt5713e602009-06-17 18:20:48 +0900296 /* Finish initialization */
297 data = pci_read_reg(chan, SH4A_PCIETCTLR);
298 data |= 0x1;
299 pci_write_reg(chan, data, SH4A_PCIETCTLR);
300
301 /* Enable DL_Active Interrupt generation */
302 data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
303 data |= PCIEDLINTENR_DLL_ACT_ENABLE;
304 pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
305
306 /* Disable MAC data scrambling. */
307 data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
308 data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
309 pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
310
311 ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
312 if (unlikely(ret != 0))
313 return -ENODEV;
314
Paul Mundt7578a4c2010-02-10 16:00:58 +0900315 data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
316 data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
317 data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
318 (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
319 pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
320
Paul Mundt5713e602009-06-17 18:20:48 +0900321 pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
322 pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
Paul Mundt5713e602009-06-17 18:20:48 +0900323
324 wmb();
325
326 data = pci_read_reg(chan, SH4A_PCIEMACSR);
327 printk(KERN_NOTICE "PCI: PCIe#%d link width %d\n",
328 port->index, (data >> 20) & 0x3f);
329
Paul Mundt5713e602009-06-17 18:20:48 +0900330
Paul Mundt7578a4c2010-02-10 16:00:58 +0900331 for (i = 0; i < chan->nr_resources; i++) {
332 struct resource *res = chan->resources + i;
333 resource_size_t size;
334 u32 enable_mask;
335
336 pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(i));
337
338 size = resource_size(res);
339
340 /*
341 * The PAMR mask is calculated in units of 256kB, which
342 * keeps things pretty simple.
343 */
344 __raw_writel(((roundup_pow_of_two(size) / SZ_256K) - 1) << 18,
345 chan->reg_base + SH4A_PCIEPAMR(i));
346
347 pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH(i));
348 pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL(i));
349
350 enable_mask = MASK_PARE;
351 if (res->flags & IORESOURCE_IO)
352 enable_mask |= MASK_SPC;
353
354 pci_write_reg(chan, enable_mask, SH4A_PCIEPTCTLR(i));
355 }
Paul Mundt5713e602009-06-17 18:20:48 +0900356
357 return 0;
358}
359
360int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
361{
362 return 71;
363}
364
365static int sh7786_pcie_core_init(void)
366{
367 /* Return the number of ports */
368 return test_mode_pin(MODE_PIN12) ? 3 : 2;
369}
370
371static int __devinit sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
372{
373 int ret;
374
375 ret = phy_init(port->hose);
376 if (unlikely(ret < 0))
377 return ret;
378
379 /*
380 * Check if we are configured in endpoint or root complex mode,
381 * this is a fixed pin setting that applies to all PCIe ports.
382 */
383 port->endpoint = test_mode_pin(MODE_PIN11);
384
385 ret = pcie_init(port);
386 if (unlikely(ret < 0))
387 return ret;
388
Paul Mundtbcf39352010-02-01 13:11:25 +0900389 return register_pci_controller(port->hose);
Paul Mundt5713e602009-06-17 18:20:48 +0900390}
391
392static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
393 .core_init = sh7786_pcie_core_init,
394 .port_init_hw = sh7786_pcie_init_hw,
395};
396
397static int __init sh7786_pcie_init(void)
398{
399 int ret = 0, i;
400
401 printk(KERN_NOTICE "PCI: Starting intialization.\n");
402
403 sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
404
405 nr_ports = sh7786_pcie_hwops->core_init();
406 BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
407
408 if (unlikely(nr_ports == 0))
409 return -ENODEV;
410
411 sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
412 GFP_KERNEL);
413 if (unlikely(!sh7786_pcie_ports))
414 return -ENOMEM;
415
416 printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
417
418 for (i = 0; i < nr_ports; i++) {
419 struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
420
421 port->index = i;
422 port->hose = sh7786_pci_channels + i;
Paul Mundt7561f2d2010-02-08 16:36:56 +0900423 port->hose->io_map_base = port->hose->resources[0].start;
Paul Mundt5713e602009-06-17 18:20:48 +0900424
425 ret |= sh7786_pcie_hwops->port_init_hw(port);
426 }
427
428 if (unlikely(ret))
429 return ret;
430
431 return 0;
432}
433arch_initcall(sh7786_pcie_init);