blob: 86373314f45829a1e9d12560fd45660ffbe3e31b [file] [log] [blame]
Paul Mundt5283ecb2006-09-27 15:59:17 +09001/*
Paul Mundt62c7ae82009-04-17 20:37:16 +09002 * Low-Level PCI Support for the SH7780
Paul Mundt5283ecb2006-09-27 15:59:17 +09003 *
Paul Mundta45635d2010-01-29 22:19:04 +09004 * Copyright (C) 2005 - 2010 Paul Mundt
Paul Mundt5283ecb2006-09-27 15:59:17 +09005 *
Paul Mundt62c7ae82009-04-17 20:37:16 +09006 * 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.
Paul Mundt5283ecb2006-09-27 15:59:17 +09009 */
Paul Mundt5283ecb2006-09-27 15:59:17 +090010#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/pci.h>
Paul Mundtef407be2010-02-01 16:39:46 +090014#include <linux/interrupt.h>
15#include <linux/timer.h>
16#include <linux/irq.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090017#include <linux/errno.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090018#include <linux/delay.h>
Paul Mundtaee44672010-02-01 11:33:22 +090019#include <linux/log2.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090020#include "pci-sh4.h"
Paul Mundta45635d2010-01-29 22:19:04 +090021#include <asm/mmu.h>
22#include <asm/sizes.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090023
Paul Mundte79066a2009-04-20 18:29:22 +090024static struct resource sh7785_io_resource = {
25 .name = "SH7785_IO",
Paul Mundta45635d2010-01-29 22:19:04 +090026 .start = 0x1000,
27 .end = SH7780_PCI_IO_SIZE - 1,
Paul Mundte79066a2009-04-20 18:29:22 +090028 .flags = IORESOURCE_IO
29};
30
31static struct resource sh7785_mem_resource = {
32 .name = "SH7785_mem",
33 .start = SH7780_PCI_MEMORY_BASE,
34 .end = SH7780_PCI_MEMORY_BASE + SH7780_PCI_MEM_SIZE - 1,
35 .flags = IORESOURCE_MEM
36};
37
38static struct pci_channel sh7780_pci_controller = {
39 .pci_ops = &sh4_pci_ops,
40 .mem_resource = &sh7785_mem_resource,
Paul Mundt09cfeb12009-04-20 18:42:00 +090041 .mem_offset = 0x00000000,
Paul Mundte79066a2009-04-20 18:29:22 +090042 .io_resource = &sh7785_io_resource,
Paul Mundt09cfeb12009-04-20 18:42:00 +090043 .io_offset = 0x00000000,
Paul Mundt5582b062009-05-27 00:12:58 +090044 .io_map_base = SH7780_PCI_IO_BASE,
Paul Mundtef407be2010-02-01 16:39:46 +090045 .serr_irq = evt2irq(0xa00),
46 .err_irq = evt2irq(0xaa0),
Paul Mundte79066a2009-04-20 18:29:22 +090047};
48
Paul Mundtef407be2010-02-01 16:39:46 +090049struct pci_errors {
50 unsigned int mask;
51 const char *str;
52} pci_arbiter_errors[] = {
53 { SH4_PCIAINT_MBKN, "master broken" },
54 { SH4_PCIAINT_TBTO, "target bus time out" },
55 { SH4_PCIAINT_MBTO, "master bus time out" },
56 { SH4_PCIAINT_TABT, "target abort" },
57 { SH4_PCIAINT_MABT, "master abort" },
58 { SH4_PCIAINT_RDPE, "read data parity error" },
59 { SH4_PCIAINT_WDPE, "write data parity error" },
60}, pci_interrupt_errors[] = {
61 { SH4_PCIINT_MLCK, "master lock error" },
62 { SH4_PCIINT_TABT, "target-target abort" },
63 { SH4_PCIINT_TRET, "target retry time out" },
64 { SH4_PCIINT_MFDE, "master function disable erorr" },
65 { SH4_PCIINT_PRTY, "address parity error" },
66 { SH4_PCIINT_SERR, "SERR" },
67 { SH4_PCIINT_TWDP, "data parity error for target write" },
68 { SH4_PCIINT_TRDP, "PERR detected for target read" },
69 { SH4_PCIINT_MTABT, "target abort for master" },
70 { SH4_PCIINT_MMABT, "master abort for master" },
71 { SH4_PCIINT_MWPD, "master write data parity error" },
72 { SH4_PCIINT_MRPD, "master read data parity error" },
73};
74
75static irqreturn_t sh7780_pci_err_irq(int irq, void *dev_id)
76{
77 struct pci_channel *hose = dev_id;
78 unsigned long addr;
79 unsigned int status;
80 unsigned int cmd;
81 int i;
82
83 addr = __raw_readl(hose->reg_base + SH4_PCIALR);
84
85 /*
86 * Handle status errors.
87 */
88 status = __raw_readw(hose->reg_base + PCI_STATUS);
89 if (status & (PCI_STATUS_PARITY |
90 PCI_STATUS_DETECTED_PARITY |
91 PCI_STATUS_SIG_TARGET_ABORT |
92 PCI_STATUS_REC_TARGET_ABORT |
93 PCI_STATUS_REC_MASTER_ABORT)) {
94 cmd = pcibios_handle_status_errors(addr, status, hose);
95 if (likely(cmd))
96 __raw_writew(cmd, hose->reg_base + PCI_STATUS);
97 }
98
99 /*
100 * Handle arbiter errors.
101 */
102 status = __raw_readl(hose->reg_base + SH4_PCIAINT);
103 for (i = cmd = 0; i < ARRAY_SIZE(pci_arbiter_errors); i++) {
104 if (status & pci_arbiter_errors[i].mask) {
105 printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
106 pci_arbiter_errors[i].str, addr);
107 cmd |= pci_arbiter_errors[i].mask;
108 }
109 }
110 __raw_writel(cmd, hose->reg_base + SH4_PCIAINT);
111
112 /*
113 * Handle the remaining PCI errors.
114 */
115 status = __raw_readl(hose->reg_base + SH4_PCIINT);
116 for (i = cmd = 0; i < ARRAY_SIZE(pci_interrupt_errors); i++) {
117 if (status & pci_interrupt_errors[i].mask) {
118 printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
119 pci_interrupt_errors[i].str, addr);
120 cmd |= pci_interrupt_errors[i].mask;
121 }
122 }
123 __raw_writel(cmd, hose->reg_base + SH4_PCIINT);
124
125 return IRQ_HANDLED;
126}
127
128static irqreturn_t sh7780_pci_serr_irq(int irq, void *dev_id)
129{
130 struct pci_channel *hose = dev_id;
131
132 printk(KERN_DEBUG "PCI: system error received: ");
133 pcibios_report_status(PCI_STATUS_SIG_SYSTEM_ERROR, 1);
134 printk("\n");
135
136 /* Deassert SERR */
137 __raw_writel(SH4_PCIINTM_SDIM, hose->reg_base + SH4_PCIINTM);
138
139 /* Back off the IRQ for awhile */
140 disable_irq(irq);
141 hose->serr_timer.expires = jiffies + HZ;
142 add_timer(&hose->serr_timer);
143
144 return IRQ_HANDLED;
145}
146
147static int __init sh7780_pci_setup_irqs(struct pci_channel *hose)
148{
149 int ret;
150
151 /* Clear out PCI arbiter IRQs */
152 __raw_writel(0, hose->reg_base + SH4_PCIAINT);
153
154 /* Clear all error conditions */
155 __raw_writew(PCI_STATUS_DETECTED_PARITY | \
156 PCI_STATUS_SIG_SYSTEM_ERROR | \
157 PCI_STATUS_REC_MASTER_ABORT | \
158 PCI_STATUS_REC_TARGET_ABORT | \
159 PCI_STATUS_SIG_TARGET_ABORT | \
160 PCI_STATUS_PARITY, hose->reg_base + PCI_STATUS);
161
162 ret = request_irq(hose->serr_irq, sh7780_pci_serr_irq, IRQF_DISABLED,
163 "PCI SERR interrupt", hose);
164 if (unlikely(ret)) {
165 printk(KERN_ERR "PCI: Failed hooking SERR IRQ\n");
166 return ret;
167 }
168
169 /*
170 * The PCI ERR IRQ needs to be IRQF_SHARED since all of the power
171 * down IRQ vectors are routed through the ERR IRQ vector. We
172 * only request_irq() once as there is only a single masking
173 * source for multiple events.
174 */
175 ret = request_irq(hose->err_irq, sh7780_pci_err_irq, IRQF_SHARED,
176 "PCI ERR interrupt", hose);
177 if (unlikely(ret)) {
178 free_irq(hose->serr_irq, hose);
179 return ret;
180 }
181
182 /* Unmask all of the arbiter IRQs. */
183 __raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
184 SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
185 SH4_PCIAINT_WDPE, hose->reg_base + SH4_PCIAINTM);
186
187 /* Unmask all of the PCI IRQs */
188 __raw_writel(SH4_PCIINTM_TTADIM | SH4_PCIINTM_TMTOIM | \
189 SH4_PCIINTM_MDEIM | SH4_PCIINTM_APEDIM | \
190 SH4_PCIINTM_SDIM | SH4_PCIINTM_DPEITWM | \
191 SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM | \
192 SH4_PCIINTM_MADIMM | SH4_PCIINTM_MWPDIM | \
193 SH4_PCIINTM_MRDPEIM, hose->reg_base + SH4_PCIINTM);
194
195 return ret;
196}
197
198static inline void __init sh7780_pci_teardown_irqs(struct pci_channel *hose)
199{
200 free_irq(hose->err_irq, hose);
201 free_irq(hose->serr_irq, hose);
202}
203
Paul Mundt85b59f52010-02-01 13:01:42 +0900204static void __init sh7780_pci66_init(struct pci_channel *hose)
205{
206 unsigned int tmp;
207
208 if (!pci_is_66mhz_capable(hose, 0, 0))
209 return;
210
211 /* Enable register access */
212 tmp = __raw_readl(hose->reg_base + SH4_PCICR);
213 tmp |= SH4_PCICR_PREFIX;
214 __raw_writel(tmp, hose->reg_base + SH4_PCICR);
215
216 /* Enable 66MHz operation */
217 tmp = __raw_readw(hose->reg_base + PCI_STATUS);
218 tmp |= PCI_STATUS_66MHZ;
219 __raw_writew(tmp, hose->reg_base + PCI_STATUS);
220
221 /* Done */
222 tmp = __raw_readl(hose->reg_base + SH4_PCICR);
223 tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
224 __raw_writel(tmp, hose->reg_base + SH4_PCICR);
225}
226
Paul Mundte79066a2009-04-20 18:29:22 +0900227static int __init sh7780_pci_init(void)
Paul Mundt5283ecb2006-09-27 15:59:17 +0900228{
Paul Mundte79066a2009-04-20 18:29:22 +0900229 struct pci_channel *chan = &sh7780_pci_controller;
Paul Mundta45635d2010-01-29 22:19:04 +0900230 phys_addr_t memphys;
231 size_t memsize;
Paul Mundt959f85f2006-09-27 16:43:28 +0900232 unsigned int id;
Paul Mundta45635d2010-01-29 22:19:04 +0900233 const char *type;
Paul Mundtbcf39352010-02-01 13:11:25 +0900234 int ret;
Paul Mundt5283ecb2006-09-27 15:59:17 +0900235
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900236 printk(KERN_NOTICE "PCI: Starting intialization.\n");
Paul Mundt5283ecb2006-09-27 15:59:17 +0900237
Magnus Damme4c6a362008-02-19 21:35:04 +0900238 chan->reg_base = 0xfe040000;
239
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900240 /* Enable CPU access to the PCIC registers. */
241 __raw_writel(PCIECR_ENBL, PCIECR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900242
Paul Mundta45635d2010-01-29 22:19:04 +0900243 /* Reset */
244 __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
245 chan->reg_base + SH4_PCICR);
246
Paul Mundtaee44672010-02-01 11:33:22 +0900247 /*
248 * Wait for it to come back up. The spec says to allow for up to
249 * 1 second after toggling the reset pin, but in practice 100ms
250 * is more than enough.
251 */
Paul Mundta45635d2010-01-29 22:19:04 +0900252 mdelay(100);
253
254 id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
255 if (id != PCI_VENDOR_ID_RENESAS) {
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900256 printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
Paul Mundt959f85f2006-09-27 16:43:28 +0900257 return -ENODEV;
258 }
259
Paul Mundta45635d2010-01-29 22:19:04 +0900260 id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
261 type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
262 (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
263 (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
264 (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900265 NULL;
266 if (unlikely(!type)) {
267 printk(KERN_ERR "PCI: Found an unsupported Renesas host "
268 "controller, device id 0x%04x.\n", id);
269 return -EINVAL;
270 }
271
272 printk(KERN_NOTICE "PCI: Found a Renesas %s host "
273 "controller, revision %d.\n", type,
Paul Mundta45635d2010-01-29 22:19:04 +0900274 __raw_readb(chan->reg_base + PCI_REVISION_ID));
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900275
Paul Mundtc66c1d72009-04-17 16:38:00 +0900276 /*
Paul Mundta45635d2010-01-29 22:19:04 +0900277 * Now throw it in to register initialization mode and
278 * start the real work.
Paul Mundtc66c1d72009-04-17 16:38:00 +0900279 */
Paul Mundta45635d2010-01-29 22:19:04 +0900280 __raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
281
Paul Mundta45635d2010-01-29 22:19:04 +0900282 __raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
Paul Mundt5283ecb2006-09-27 15:59:17 +0900283
Paul Mundtaee44672010-02-01 11:33:22 +0900284 memphys = __pa(memory_start);
285 memsize = roundup_pow_of_two(memory_end - memory_start);
286
287 /*
288 * If there's more than 512MB of memory, we need to roll over to
289 * LAR1/LSR1.
290 */
291 if (memsize > SZ_512M) {
292 __raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
293 __raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
294 chan->reg_base + SH4_PCILSR1);
295 memsize = SZ_512M;
296 } else {
297 /*
298 * Otherwise just zero it out and disable it.
299 */
300 __raw_writel(0, chan->reg_base + SH4_PCILAR1);
301 __raw_writel(0, chan->reg_base + SH4_PCILSR1);
302 }
303
304 /*
305 * LAR0/LSR0 covers up to the first 512MB, which is enough to
306 * cover all of lowmem on most platforms.
307 */
Paul Mundta45635d2010-01-29 22:19:04 +0900308 __raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
Paul Mundtaee44672010-02-01 11:33:22 +0900309 __raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
Paul Mundta45635d2010-01-29 22:19:04 +0900310 chan->reg_base + SH4_PCILSR0);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900311
Paul Mundtef407be2010-02-01 16:39:46 +0900312 /*
313 * Hook up the ERR and SERR IRQs.
314 */
315 ret = sh7780_pci_setup_irqs(chan);
316 if (unlikely(ret))
317 return ret;
Paul Mundt62c7ae82009-04-17 20:37:16 +0900318
Paul Mundta45635d2010-01-29 22:19:04 +0900319 /*
320 * Disable the cache snoop controller for non-coherent DMA.
321 */
322 __raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
323 __raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
324 __raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
325 __raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900326
Paul Mundta45635d2010-01-29 22:19:04 +0900327 __raw_writel(0xfd000000, chan->reg_base + SH7780_PCIMBR0);
328 __raw_writel(0x00fc0000, chan->reg_base + SH7780_PCIMBMR0);
329
330 __raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
331 __raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
332
Paul Mundtef407be2010-02-01 16:39:46 +0900333 __raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
334 PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
335 PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
336
Paul Mundta45635d2010-01-29 22:19:04 +0900337 /*
338 * Initialization mode complete, release the control register and
339 * enable round robin mode to stop device overruns/starvation.
340 */
341 __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
342 chan->reg_base + SH4_PCICR);
Paul Mundt5283ecb2006-09-27 15:59:17 +0900343
Paul Mundtbcf39352010-02-01 13:11:25 +0900344 ret = register_pci_controller(chan);
345 if (unlikely(ret))
Paul Mundtef407be2010-02-01 16:39:46 +0900346 goto err;
Paul Mundte79066a2009-04-20 18:29:22 +0900347
Paul Mundt85b59f52010-02-01 13:01:42 +0900348 sh7780_pci66_init(chan);
349
350 printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
351 (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
352 66 : 33);
353
Magnus Dammd0e3db42009-03-11 15:46:14 +0900354 return 0;
Paul Mundtef407be2010-02-01 16:39:46 +0900355
356err:
357 sh7780_pci_teardown_irqs(chan);
358 return ret;
Paul Mundt5283ecb2006-09-27 15:59:17 +0900359}
Paul Mundte79066a2009-04-20 18:29:22 +0900360arch_initcall(sh7780_pci_init);