11
11
#include <linux/kernel.h>
12
12
#include <linux/init.h>
13
13
#include <linux/pci.h>
14
#include <linux/interrupt.h>
15
#include <linux/timer.h>
16
#include <linux/irq.h>
14
17
#include <linux/errno.h>
15
18
#include <linux/delay.h>
19
#include <linux/log2.h>
16
20
#include "pci-sh4.h"
18
static struct resource sh7785_io_resource = {
20
.start = SH7780_PCI_IO_BASE,
21
.end = SH7780_PCI_IO_BASE + SH7780_PCI_IO_SIZE - 1,
22
.flags = IORESOURCE_IO
25
static struct resource sh7785_mem_resource = {
27
.start = SH7780_PCI_MEMORY_BASE,
28
.end = SH7780_PCI_MEMORY_BASE + SH7780_PCI_MEM_SIZE - 1,
29
.flags = IORESOURCE_MEM
22
#include <asm/sizes.h>
24
static struct resource sh7785_pci_resources[] = {
29
.flags = IORESOURCE_IO,
33
.end = 0xfd000000 + SZ_16M - 1,
34
.flags = IORESOURCE_MEM,
38
.end = 0x10000000 + SZ_64M - 1,
39
.flags = IORESOURCE_MEM,
42
* 32-bit only resources must be last.
46
.end = 0xc0000000 + SZ_512M - 1,
47
.flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
32
51
static struct pci_channel sh7780_pci_controller = {
33
52
.pci_ops = &sh4_pci_ops,
34
.mem_resource = &sh7785_mem_resource,
35
.mem_offset = 0x00000000,
36
.io_resource = &sh7785_io_resource,
37
.io_offset = 0x00000000,
38
.io_map_base = SH7780_PCI_IO_BASE,
41
static struct sh4_pci_address_map sh7780_pci_map = {
43
#if defined(CONFIG_32BIT)
44
.base = SH7780_32BIT_DDR_BASE_ADDR,
47
.base = SH7780_CS0_BASE_ADDR,
53
.resources = sh7785_pci_resources,
54
.nr_resources = ARRAY_SIZE(sh7785_pci_resources),
57
.io_map_base = 0xfe200000,
58
.serr_irq = evt2irq(0xa00),
59
.err_irq = evt2irq(0xaa0),
65
} pci_arbiter_errors[] = {
66
{ SH4_PCIAINT_MBKN, "master broken" },
67
{ SH4_PCIAINT_TBTO, "target bus time out" },
68
{ SH4_PCIAINT_MBTO, "master bus time out" },
69
{ SH4_PCIAINT_TABT, "target abort" },
70
{ SH4_PCIAINT_MABT, "master abort" },
71
{ SH4_PCIAINT_RDPE, "read data parity error" },
72
{ SH4_PCIAINT_WDPE, "write data parity error" },
73
}, pci_interrupt_errors[] = {
74
{ SH4_PCIINT_MLCK, "master lock error" },
75
{ SH4_PCIINT_TABT, "target-target abort" },
76
{ SH4_PCIINT_TRET, "target retry time out" },
77
{ SH4_PCIINT_MFDE, "master function disable erorr" },
78
{ SH4_PCIINT_PRTY, "address parity error" },
79
{ SH4_PCIINT_SERR, "SERR" },
80
{ SH4_PCIINT_TWDP, "data parity error for target write" },
81
{ SH4_PCIINT_TRDP, "PERR detected for target read" },
82
{ SH4_PCIINT_MTABT, "target abort for master" },
83
{ SH4_PCIINT_MMABT, "master abort for master" },
84
{ SH4_PCIINT_MWPD, "master write data parity error" },
85
{ SH4_PCIINT_MRPD, "master read data parity error" },
88
static irqreturn_t sh7780_pci_err_irq(int irq, void *dev_id)
90
struct pci_channel *hose = dev_id;
96
addr = __raw_readl(hose->reg_base + SH4_PCIALR);
99
* Handle status errors.
101
status = __raw_readw(hose->reg_base + PCI_STATUS);
102
if (status & (PCI_STATUS_PARITY |
103
PCI_STATUS_DETECTED_PARITY |
104
PCI_STATUS_SIG_TARGET_ABORT |
105
PCI_STATUS_REC_TARGET_ABORT |
106
PCI_STATUS_REC_MASTER_ABORT)) {
107
cmd = pcibios_handle_status_errors(addr, status, hose);
109
__raw_writew(cmd, hose->reg_base + PCI_STATUS);
113
* Handle arbiter errors.
115
status = __raw_readl(hose->reg_base + SH4_PCIAINT);
116
for (i = cmd = 0; i < ARRAY_SIZE(pci_arbiter_errors); i++) {
117
if (status & pci_arbiter_errors[i].mask) {
118
printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
119
pci_arbiter_errors[i].str, addr);
120
cmd |= pci_arbiter_errors[i].mask;
123
__raw_writel(cmd, hose->reg_base + SH4_PCIAINT);
126
* Handle the remaining PCI errors.
128
status = __raw_readl(hose->reg_base + SH4_PCIINT);
129
for (i = cmd = 0; i < ARRAY_SIZE(pci_interrupt_errors); i++) {
130
if (status & pci_interrupt_errors[i].mask) {
131
printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
132
pci_interrupt_errors[i].str, addr);
133
cmd |= pci_interrupt_errors[i].mask;
136
__raw_writel(cmd, hose->reg_base + SH4_PCIINT);
141
static irqreturn_t sh7780_pci_serr_irq(int irq, void *dev_id)
143
struct pci_channel *hose = dev_id;
145
printk(KERN_DEBUG "PCI: system error received: ");
146
pcibios_report_status(PCI_STATUS_SIG_SYSTEM_ERROR, 1);
150
__raw_writel(SH4_PCIINTM_SDIM, hose->reg_base + SH4_PCIINTM);
152
/* Back off the IRQ for awhile */
153
disable_irq_nosync(irq);
154
hose->serr_timer.expires = jiffies + HZ;
155
add_timer(&hose->serr_timer);
160
static int __init sh7780_pci_setup_irqs(struct pci_channel *hose)
164
/* Clear out PCI arbiter IRQs */
165
__raw_writel(0, hose->reg_base + SH4_PCIAINT);
167
/* Clear all error conditions */
168
__raw_writew(PCI_STATUS_DETECTED_PARITY | \
169
PCI_STATUS_SIG_SYSTEM_ERROR | \
170
PCI_STATUS_REC_MASTER_ABORT | \
171
PCI_STATUS_REC_TARGET_ABORT | \
172
PCI_STATUS_SIG_TARGET_ABORT | \
173
PCI_STATUS_PARITY, hose->reg_base + PCI_STATUS);
175
ret = request_irq(hose->serr_irq, sh7780_pci_serr_irq, IRQF_DISABLED,
176
"PCI SERR interrupt", hose);
178
printk(KERN_ERR "PCI: Failed hooking SERR IRQ\n");
183
* The PCI ERR IRQ needs to be IRQF_SHARED since all of the power
184
* down IRQ vectors are routed through the ERR IRQ vector. We
185
* only request_irq() once as there is only a single masking
186
* source for multiple events.
188
ret = request_irq(hose->err_irq, sh7780_pci_err_irq, IRQF_SHARED,
189
"PCI ERR interrupt", hose);
191
free_irq(hose->serr_irq, hose);
195
/* Unmask all of the arbiter IRQs. */
196
__raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
197
SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
198
SH4_PCIAINT_WDPE, hose->reg_base + SH4_PCIAINTM);
200
/* Unmask all of the PCI IRQs */
201
__raw_writel(SH4_PCIINTM_TTADIM | SH4_PCIINTM_TMTOIM | \
202
SH4_PCIINTM_MDEIM | SH4_PCIINTM_APEDIM | \
203
SH4_PCIINTM_SDIM | SH4_PCIINTM_DPEITWM | \
204
SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM | \
205
SH4_PCIINTM_MADIMM | SH4_PCIINTM_MWPDIM | \
206
SH4_PCIINTM_MRDPEIM, hose->reg_base + SH4_PCIINTM);
211
static inline void __init sh7780_pci_teardown_irqs(struct pci_channel *hose)
213
free_irq(hose->err_irq, hose);
214
free_irq(hose->serr_irq, hose);
217
static void __init sh7780_pci66_init(struct pci_channel *hose)
221
if (!pci_is_66mhz_capable(hose, 0, 0))
224
/* Enable register access */
225
tmp = __raw_readl(hose->reg_base + SH4_PCICR);
226
tmp |= SH4_PCICR_PREFIX;
227
__raw_writel(tmp, hose->reg_base + SH4_PCICR);
229
/* Enable 66MHz operation */
230
tmp = __raw_readw(hose->reg_base + PCI_STATUS);
231
tmp |= PCI_STATUS_66MHZ;
232
__raw_writew(tmp, hose->reg_base + PCI_STATUS);
235
tmp = __raw_readl(hose->reg_base + SH4_PCICR);
236
tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
237
__raw_writel(tmp, hose->reg_base + SH4_PCICR);
53
240
static int __init sh7780_pci_init(void)
55
242
struct pci_channel *chan = &sh7780_pci_controller;
57
const char *type = NULL;
61
printk(KERN_NOTICE "PCI: Starting intialization.\n");
249
printk(KERN_NOTICE "PCI: Starting initialization.\n");
63
251
chan->reg_base = 0xfe040000;
65
253
/* Enable CPU access to the PCIC registers. */
66
254
__raw_writel(PCIECR_ENBL, PCIECR);
68
id = __raw_readw(chan->reg_base + SH7780_PCIVID);
69
if (id != SH7780_VENDOR_ID) {
257
__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
258
chan->reg_base + SH4_PCICR);
261
* Wait for it to come back up. The spec says to allow for up to
262
* 1 second after toggling the reset pin, but in practice 100ms
263
* is more than enough.
267
id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
268
if (id != PCI_VENDOR_ID_RENESAS) {
70
269
printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
74
id = __raw_readw(chan->reg_base + SH7780_PCIDID);
75
type = (id == SH7763_DEVICE_ID) ? "SH7763" :
76
(id == SH7780_DEVICE_ID) ? "SH7780" :
77
(id == SH7781_DEVICE_ID) ? "SH7781" :
78
(id == SH7785_DEVICE_ID) ? "SH7785" :
273
id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
274
type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
275
(id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
276
(id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
277
(id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
80
279
if (unlikely(!type)) {
81
280
printk(KERN_ERR "PCI: Found an unsupported Renesas host "
86
285
printk(KERN_NOTICE "PCI: Found a Renesas %s host "
87
286
"controller, revision %d.\n", type,
88
__raw_readb(chan->reg_base + SH7780_PCIRID));
90
if ((ret = sh4_pci_check_direct(chan)) != 0)
287
__raw_readb(chan->reg_base + PCI_REVISION_ID));
290
* Now throw it in to register initialization mode and
291
* start the real work.
293
__raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
295
memphys = __pa(memory_start);
296
memsize = roundup_pow_of_two(memory_end - memory_start);
299
* If there's more than 512MB of memory, we need to roll over to
302
if (memsize > SZ_512M) {
303
__raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
304
__raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
305
chan->reg_base + SH4_PCILSR1);
309
* Otherwise just zero it out and disable it.
311
__raw_writel(0, chan->reg_base + SH4_PCILAR1);
312
__raw_writel(0, chan->reg_base + SH4_PCILSR1);
316
* LAR0/LSR0 covers up to the first 512MB, which is enough to
317
* cover all of lowmem on most platforms.
319
__raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
320
__raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
321
chan->reg_base + SH4_PCILSR0);
324
* Hook up the ERR and SERR IRQs.
326
ret = sh7780_pci_setup_irqs(chan);
94
* Set the class and sub-class codes.
96
__raw_writeb(PCI_CLASS_BRIDGE_HOST >> 8,
97
chan->reg_base + SH7780_PCIBCC);
98
__raw_writeb(PCI_CLASS_BRIDGE_HOST & 0xff,
99
chan->reg_base + SH7780_PCISUB);
102
* Set IO and Mem windows to local address
103
* Make PCI and local address the same for easy 1 to 1 mapping
105
pci_write_reg(chan, sh7780_pci_map.window0.size - 0xfffff, SH4_PCILSR0);
106
/* Set the values on window 0 PCI config registers */
107
pci_write_reg(chan, sh7780_pci_map.window0.base, SH4_PCILAR0);
108
pci_write_reg(chan, sh7780_pci_map.window0.base, SH7780_PCIMBAR0);
110
pci_write_reg(chan, 0x0000380f, SH4_PCIAINTM);
112
/* Set up standard PCI config registers */
113
__raw_writew(0xFB00, chan->reg_base + SH7780_PCISTATUS);
114
__raw_writew(0x0047, chan->reg_base + SH7780_PCICMD);
115
__raw_writew(0x1912, chan->reg_base + SH7780_PCISVID);
116
__raw_writew(0x0001, chan->reg_base + SH7780_PCISID);
118
__raw_writeb(0x00, chan->reg_base + SH7780_PCIPIF);
120
/* Apply any last-minute PCIC fixups */
121
pci_fixup_pcic(chan);
123
pci_write_reg(chan, 0xfd000000, SH7780_PCIMBR0);
124
pci_write_reg(chan, 0x00fc0000, SH7780_PCIMBMR0);
127
pci_write_reg(chan, 0xc0000000, SH7780_PCIMBR2);
128
pci_write_reg(chan, 0x20000000 - SH7780_PCI_IO_SIZE, SH7780_PCIMBMR2);
131
/* Set IOBR for windows containing area specified in pci.h */
132
pci_write_reg(chan, chan->io_resource->start & ~(SH7780_PCI_IO_SIZE-1),
134
pci_write_reg(chan, ((SH7780_PCI_IO_SIZE-1) & (7<<18)),
137
/* SH7780 init done, set central function init complete */
138
/* use round robin mode to stop a device starving/overruning */
139
word = SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO;
140
pci_write_reg(chan, word, SH4_PCICR);
142
register_pci_controller(chan);
331
* Disable the cache snoop controller for non-coherent DMA.
333
__raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
334
__raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
335
__raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
336
__raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
339
* Setup the memory BARs
341
for (i = 1; i < chan->nr_resources; i++) {
342
struct resource *res = chan->resources + i;
343
resource_size_t size;
345
if (unlikely(res->flags & IORESOURCE_IO))
349
* Make sure we're in the right physical addressing mode
350
* for dealing with the resource.
352
if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode()) {
353
chan->nr_resources--;
357
size = resource_size(res);
360
* The MBMR mask is calculated in units of 256kB, which
361
* keeps things pretty simple.
363
__raw_writel(((roundup_pow_of_two(size) / SZ_256K) - 1) << 18,
364
chan->reg_base + SH7780_PCIMBMR(i - 1));
365
__raw_writel(res->start, chan->reg_base + SH7780_PCIMBR(i - 1));
371
__raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
372
__raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
373
__raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
375
__raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
376
PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
377
PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
380
* Initialization mode complete, release the control register and
381
* enable round robin mode to stop device overruns/starvation.
383
__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
384
chan->reg_base + SH4_PCICR);
386
ret = register_pci_controller(chan);
390
sh7780_pci66_init(chan);
392
printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
393
(__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
399
sh7780_pci_teardown_irqs(chan);
146
402
arch_initcall(sh7780_pci_init);