blob: b5eddf533c0be36f4e384dbb8bd2372fb85802b6 [file] [log] [blame]
Manuel Lauss7517de32011-08-12 11:39:44 +02001/*
2 * Alchemy PCI host mode support.
3 *
4 * Copyright 2001-2003, 2007-2008 MontaVista Software Inc.
5 * Author: MontaVista Software, Inc. <source@mvista.com>
6 *
7 * Support for all devices (greater than 16) added by David Gathright.
8 */
9
Ralf Baechle71ca8692011-11-10 14:15:57 +000010#include <linux/export.h>
Manuel Lauss7517de32011-08-12 11:39:44 +020011#include <linux/types.h>
12#include <linux/pci.h>
13#include <linux/platform_device.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
Manuel Lauss864c6c222011-11-16 15:42:28 +010016#include <linux/syscore_ops.h>
Manuel Lauss7517de32011-08-12 11:39:44 +020017#include <linux/vmalloc.h>
18
19#include <asm/mach-au1x00/au1000.h>
20
21#ifdef CONFIG_DEBUG_PCI
22#define DBG(x...) printk(KERN_DEBUG x)
23#else
24#define DBG(x...) do {} while (0)
25#endif
26
27#define PCI_ACCESS_READ 0
28#define PCI_ACCESS_WRITE 1
29
30struct alchemy_pci_context {
31 struct pci_controller alchemy_pci_ctrl; /* leave as first member! */
32 void __iomem *regs; /* ctrl base */
33 /* tools for wired entry for config space access */
34 unsigned long last_elo0;
35 unsigned long last_elo1;
36 int wired_entry;
37 struct vm_struct *pci_cfg_vm;
38
39 unsigned long pm[12];
40
41 int (*board_map_irq)(const struct pci_dev *d, u8 slot, u8 pin);
42 int (*board_pci_idsel)(unsigned int devsel, int assert);
43};
44
Manuel Lauss864c6c222011-11-16 15:42:28 +010045/* for syscore_ops. There's only one PCI controller on Alchemy chips, so this
46 * should suffice for now.
47 */
48static struct alchemy_pci_context *__alchemy_pci_ctx;
49
50
Manuel Lauss7517de32011-08-12 11:39:44 +020051/* IO/MEM resources for PCI. Keep the memres in sync with __fixup_bigphys_addr
52 * in arch/mips/alchemy/common/setup.c
53 */
54static struct resource alchemy_pci_def_memres = {
55 .start = ALCHEMY_PCI_MEMWIN_START,
56 .end = ALCHEMY_PCI_MEMWIN_END,
57 .name = "PCI memory space",
58 .flags = IORESOURCE_MEM
59};
60
61static struct resource alchemy_pci_def_iores = {
62 .start = ALCHEMY_PCI_IOWIN_START,
63 .end = ALCHEMY_PCI_IOWIN_END,
64 .name = "PCI IO space",
65 .flags = IORESOURCE_IO
66};
67
68static void mod_wired_entry(int entry, unsigned long entrylo0,
69 unsigned long entrylo1, unsigned long entryhi,
70 unsigned long pagemask)
71{
72 unsigned long old_pagemask;
73 unsigned long old_ctx;
74
75 /* Save old context and create impossible VPN2 value */
76 old_ctx = read_c0_entryhi() & 0xff;
77 old_pagemask = read_c0_pagemask();
78 write_c0_index(entry);
79 write_c0_pagemask(pagemask);
80 write_c0_entryhi(entryhi);
81 write_c0_entrylo0(entrylo0);
82 write_c0_entrylo1(entrylo1);
83 tlb_write_indexed();
84 write_c0_entryhi(old_ctx);
85 write_c0_pagemask(old_pagemask);
86}
87
88static void alchemy_pci_wired_entry(struct alchemy_pci_context *ctx)
89{
90 ctx->wired_entry = read_c0_wired();
91 add_wired_entry(0, 0, (unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
92 ctx->last_elo0 = ctx->last_elo1 = ~0;
93}
94
95static int config_access(unsigned char access_type, struct pci_bus *bus,
96 unsigned int dev_fn, unsigned char where, u32 *data)
97{
98 struct alchemy_pci_context *ctx = bus->sysdata;
99 unsigned int device = PCI_SLOT(dev_fn);
100 unsigned int function = PCI_FUNC(dev_fn);
101 unsigned long offset, status, cfg_base, flags, entryLo0, entryLo1, r;
102 int error = PCIBIOS_SUCCESSFUL;
103
104 if (device > 19) {
105 *data = 0xffffffff;
106 return -1;
107 }
108
Manuel Lauss7517de32011-08-12 11:39:44 +0200109 local_irq_save(flags);
110 r = __raw_readl(ctx->regs + PCI_REG_STATCMD) & 0x0000ffff;
111 r |= PCI_STATCMD_STATUS(0x2000);
112 __raw_writel(r, ctx->regs + PCI_REG_STATCMD);
113 wmb();
114
115 /* Allow board vendors to implement their own off-chip IDSEL.
116 * If it doesn't succeed, may as well bail out at this point.
117 */
118 if (ctx->board_pci_idsel(device, 1) == 0) {
119 *data = 0xffffffff;
120 local_irq_restore(flags);
121 return -1;
122 }
123
124 /* Setup the config window */
125 if (bus->number == 0)
126 cfg_base = (1 << device) << 11;
127 else
128 cfg_base = 0x80000000 | (bus->number << 16) | (device << 11);
129
130 /* Setup the lower bits of the 36-bit address */
131 offset = (function << 8) | (where & ~0x3);
132 /* Pick up any address that falls below the page mask */
133 offset |= cfg_base & ~PAGE_MASK;
134
135 /* Page boundary */
136 cfg_base = cfg_base & PAGE_MASK;
137
138 /* To improve performance, if the current device is the same as
139 * the last device accessed, we don't touch the TLB.
140 */
141 entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
142 entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
143 if ((entryLo0 != ctx->last_elo0) || (entryLo1 != ctx->last_elo1)) {
144 mod_wired_entry(ctx->wired_entry, entryLo0, entryLo1,
145 (unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
146 ctx->last_elo0 = entryLo0;
147 ctx->last_elo1 = entryLo1;
148 }
149
150 if (access_type == PCI_ACCESS_WRITE)
151 __raw_writel(*data, ctx->pci_cfg_vm->addr + offset);
152 else
153 *data = __raw_readl(ctx->pci_cfg_vm->addr + offset);
154 wmb();
155
156 DBG("alchemy-pci: cfg access %d bus %u dev %u at %x dat %x conf %lx\n",
157 access_type, bus->number, device, where, *data, offset);
158
159 /* check for errors, master abort */
160 status = __raw_readl(ctx->regs + PCI_REG_STATCMD);
161 if (status & (1 << 29)) {
162 *data = 0xffffffff;
163 error = -1;
164 DBG("alchemy-pci: master abort on cfg access %d bus %d dev %d",
165 access_type, bus->number, device);
166 } else if ((status >> 28) & 0xf) {
167 DBG("alchemy-pci: PCI ERR detected: dev %d, status %lx\n",
168 device, (status >> 28) & 0xf);
169
170 /* clear errors */
171 __raw_writel(status & 0xf000ffff, ctx->regs + PCI_REG_STATCMD);
172
173 *data = 0xffffffff;
174 error = -1;
175 }
176
177 /* Take away the IDSEL. */
178 (void)ctx->board_pci_idsel(device, 0);
179
180 local_irq_restore(flags);
181 return error;
182}
183
184static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
185 int where, u8 *val)
186{
187 u32 data;
188 int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
189
190 if (where & 1)
191 data >>= 8;
192 if (where & 2)
193 data >>= 16;
194 *val = data & 0xff;
195 return ret;
196}
197
198static int read_config_word(struct pci_bus *bus, unsigned int devfn,
199 int where, u16 *val)
200{
201 u32 data;
202 int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
203
204 if (where & 2)
205 data >>= 16;
206 *val = data & 0xffff;
207 return ret;
208}
209
210static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
211 int where, u32 *val)
212{
213 return config_access(PCI_ACCESS_READ, bus, devfn, where, val);
214}
215
216static int write_config_byte(struct pci_bus *bus, unsigned int devfn,
217 int where, u8 val)
218{
219 u32 data = 0;
220
221 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
222 return -1;
223
224 data = (data & ~(0xff << ((where & 3) << 3))) |
225 (val << ((where & 3) << 3));
226
227 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
228 return -1;
229
230 return PCIBIOS_SUCCESSFUL;
231}
232
233static int write_config_word(struct pci_bus *bus, unsigned int devfn,
234 int where, u16 val)
235{
236 u32 data = 0;
237
238 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
239 return -1;
240
241 data = (data & ~(0xffff << ((where & 3) << 3))) |
242 (val << ((where & 3) << 3));
243
244 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
245 return -1;
246
247 return PCIBIOS_SUCCESSFUL;
248}
249
250static int write_config_dword(struct pci_bus *bus, unsigned int devfn,
251 int where, u32 val)
252{
253 return config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val);
254}
255
256static int alchemy_pci_read(struct pci_bus *bus, unsigned int devfn,
257 int where, int size, u32 *val)
258{
259 switch (size) {
260 case 1: {
261 u8 _val;
262 int rc = read_config_byte(bus, devfn, where, &_val);
263
264 *val = _val;
265 return rc;
266 }
267 case 2: {
268 u16 _val;
269 int rc = read_config_word(bus, devfn, where, &_val);
270
271 *val = _val;
272 return rc;
273 }
274 default:
275 return read_config_dword(bus, devfn, where, val);
276 }
277}
278
279static int alchemy_pci_write(struct pci_bus *bus, unsigned int devfn,
280 int where, int size, u32 val)
281{
282 switch (size) {
283 case 1:
284 return write_config_byte(bus, devfn, where, (u8) val);
285 case 2:
286 return write_config_word(bus, devfn, where, (u16) val);
287 default:
288 return write_config_dword(bus, devfn, where, val);
289 }
290}
291
292static struct pci_ops alchemy_pci_ops = {
293 .read = alchemy_pci_read,
294 .write = alchemy_pci_write,
295};
296
297static int alchemy_pci_def_idsel(unsigned int devsel, int assert)
298{
299 return 1; /* success */
300}
301
Manuel Lauss864c6c222011-11-16 15:42:28 +0100302/* save PCI controller register contents. */
303static int alchemy_pci_suspend(void)
304{
305 struct alchemy_pci_context *ctx = __alchemy_pci_ctx;
306 if (!ctx)
307 return 0;
308
309 ctx->pm[0] = __raw_readl(ctx->regs + PCI_REG_CMEM);
310 ctx->pm[1] = __raw_readl(ctx->regs + PCI_REG_CONFIG) & 0x0009ffff;
311 ctx->pm[2] = __raw_readl(ctx->regs + PCI_REG_B2BMASK_CCH);
312 ctx->pm[3] = __raw_readl(ctx->regs + PCI_REG_B2BBASE0_VID);
313 ctx->pm[4] = __raw_readl(ctx->regs + PCI_REG_B2BBASE1_SID);
314 ctx->pm[5] = __raw_readl(ctx->regs + PCI_REG_MWMASK_DEV);
315 ctx->pm[6] = __raw_readl(ctx->regs + PCI_REG_MWBASE_REV_CCL);
316 ctx->pm[7] = __raw_readl(ctx->regs + PCI_REG_ID);
317 ctx->pm[8] = __raw_readl(ctx->regs + PCI_REG_CLASSREV);
318 ctx->pm[9] = __raw_readl(ctx->regs + PCI_REG_PARAM);
319 ctx->pm[10] = __raw_readl(ctx->regs + PCI_REG_MBAR);
320 ctx->pm[11] = __raw_readl(ctx->regs + PCI_REG_TIMEOUT);
321
322 return 0;
323}
324
325static void alchemy_pci_resume(void)
326{
327 struct alchemy_pci_context *ctx = __alchemy_pci_ctx;
328 if (!ctx)
329 return;
330
331 __raw_writel(ctx->pm[0], ctx->regs + PCI_REG_CMEM);
332 __raw_writel(ctx->pm[2], ctx->regs + PCI_REG_B2BMASK_CCH);
333 __raw_writel(ctx->pm[3], ctx->regs + PCI_REG_B2BBASE0_VID);
334 __raw_writel(ctx->pm[4], ctx->regs + PCI_REG_B2BBASE1_SID);
335 __raw_writel(ctx->pm[5], ctx->regs + PCI_REG_MWMASK_DEV);
336 __raw_writel(ctx->pm[6], ctx->regs + PCI_REG_MWBASE_REV_CCL);
337 __raw_writel(ctx->pm[7], ctx->regs + PCI_REG_ID);
338 __raw_writel(ctx->pm[8], ctx->regs + PCI_REG_CLASSREV);
339 __raw_writel(ctx->pm[9], ctx->regs + PCI_REG_PARAM);
340 __raw_writel(ctx->pm[10], ctx->regs + PCI_REG_MBAR);
341 __raw_writel(ctx->pm[11], ctx->regs + PCI_REG_TIMEOUT);
342 wmb();
343 __raw_writel(ctx->pm[1], ctx->regs + PCI_REG_CONFIG);
344 wmb();
345
346 /* YAMON on all db1xxx boards wipes the TLB and writes zero to C0_wired
347 * on resume, making it necessary to recreate it as soon as possible.
348 */
349 ctx->wired_entry = 8191; /* impossibly high value */
350 alchemy_pci_wired_entry(ctx); /* install it */
351}
352
353static struct syscore_ops alchemy_pci_pmops = {
354 .suspend = alchemy_pci_suspend,
355 .resume = alchemy_pci_resume,
356};
357
Manuel Lauss7517de32011-08-12 11:39:44 +0200358static int __devinit alchemy_pci_probe(struct platform_device *pdev)
359{
360 struct alchemy_pci_platdata *pd = pdev->dev.platform_data;
361 struct alchemy_pci_context *ctx;
362 void __iomem *virt_io;
363 unsigned long val;
364 struct resource *r;
365 int ret;
366
367 /* need at least PCI IRQ mapping table */
368 if (!pd) {
369 dev_err(&pdev->dev, "need platform data for PCI setup\n");
370 ret = -ENODEV;
371 goto out;
372 }
373
374 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
375 if (!ctx) {
376 dev_err(&pdev->dev, "no memory for pcictl context\n");
377 ret = -ENOMEM;
378 goto out;
379 }
380
381 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382 if (!r) {
383 dev_err(&pdev->dev, "no pcictl ctrl regs resource\n");
384 ret = -ENODEV;
385 goto out1;
386 }
387
388 if (!request_mem_region(r->start, resource_size(r), pdev->name)) {
389 dev_err(&pdev->dev, "cannot claim pci regs\n");
390 ret = -ENODEV;
391 goto out1;
392 }
393
394 ctx->regs = ioremap_nocache(r->start, resource_size(r));
395 if (!ctx->regs) {
396 dev_err(&pdev->dev, "cannot map pci regs\n");
397 ret = -ENODEV;
398 goto out2;
399 }
400
401 /* map parts of the PCI IO area */
402 /* REVISIT: if this changes with a newer variant (doubt it) make this
403 * a platform resource.
404 */
405 virt_io = ioremap(AU1500_PCI_IO_PHYS_ADDR, 0x00100000);
406 if (!virt_io) {
407 dev_err(&pdev->dev, "cannot remap pci io space\n");
408 ret = -ENODEV;
409 goto out3;
410 }
411 ctx->alchemy_pci_ctrl.io_map_base = (unsigned long)virt_io;
412
413#ifdef CONFIG_DMA_NONCOHERENT
414 /* Au1500 revisions older than AD have borked coherent PCI */
415 if ((alchemy_get_cputype() == ALCHEMY_CPU_AU1500) &&
416 (read_c0_prid() < 0x01030202)) {
417 val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
418 val |= PCI_CONFIG_NC;
419 __raw_writel(val, ctx->regs + PCI_REG_CONFIG);
420 wmb();
421 dev_info(&pdev->dev, "non-coherent PCI on Au1500 AA/AB/AC\n");
422 }
423#endif
424
425 if (pd->board_map_irq)
426 ctx->board_map_irq = pd->board_map_irq;
427
428 if (pd->board_pci_idsel)
429 ctx->board_pci_idsel = pd->board_pci_idsel;
430 else
431 ctx->board_pci_idsel = alchemy_pci_def_idsel;
432
433 /* fill in relevant pci_controller members */
434 ctx->alchemy_pci_ctrl.pci_ops = &alchemy_pci_ops;
435 ctx->alchemy_pci_ctrl.mem_resource = &alchemy_pci_def_memres;
436 ctx->alchemy_pci_ctrl.io_resource = &alchemy_pci_def_iores;
437
438 /* we can't ioremap the entire pci config space because it's too large,
439 * nor can we dynamically ioremap it because some drivers use the
440 * PCI config routines from within atomic contex and that becomes a
441 * problem in get_vm_area(). Instead we use one wired TLB entry to
442 * handle all config accesses for all busses.
443 */
444 ctx->pci_cfg_vm = get_vm_area(0x2000, VM_IOREMAP);
445 if (!ctx->pci_cfg_vm) {
446 dev_err(&pdev->dev, "unable to get vm area\n");
447 ret = -ENOMEM;
448 goto out4;
449 }
Manuel Lauss864c6c222011-11-16 15:42:28 +0100450 ctx->wired_entry = 8191; /* impossibly high value */
451 alchemy_pci_wired_entry(ctx); /* install it */
Manuel Lauss7517de32011-08-12 11:39:44 +0200452
453 set_io_port_base((unsigned long)ctx->alchemy_pci_ctrl.io_map_base);
454
455 /* board may want to modify bits in the config register, do it now */
456 val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
457 val &= ~pd->pci_cfg_clr;
458 val |= pd->pci_cfg_set;
459 val &= ~PCI_CONFIG_PD; /* clear disable bit */
460 __raw_writel(val, ctx->regs + PCI_REG_CONFIG);
461 wmb();
462
Manuel Lauss864c6c222011-11-16 15:42:28 +0100463 __alchemy_pci_ctx = ctx;
Manuel Lauss7517de32011-08-12 11:39:44 +0200464 platform_set_drvdata(pdev, ctx);
Manuel Lauss864c6c222011-11-16 15:42:28 +0100465 register_syscore_ops(&alchemy_pci_pmops);
Manuel Lauss7517de32011-08-12 11:39:44 +0200466 register_pci_controller(&ctx->alchemy_pci_ctrl);
467
468 return 0;
469
470out4:
471 iounmap(virt_io);
472out3:
473 iounmap(ctx->regs);
474out2:
475 release_mem_region(r->start, resource_size(r));
476out1:
477 kfree(ctx);
478out:
479 return ret;
480}
481
Manuel Lauss7517de32011-08-12 11:39:44 +0200482static struct platform_driver alchemy_pcictl_driver = {
483 .probe = alchemy_pci_probe,
484 .driver = {
485 .name = "alchemy-pci",
486 .owner = THIS_MODULE,
Manuel Lauss7517de32011-08-12 11:39:44 +0200487 },
488};
489
490static int __init alchemy_pci_init(void)
491{
492 /* Au1500/Au1550 have PCI */
493 switch (alchemy_get_cputype()) {
494 case ALCHEMY_CPU_AU1500:
495 case ALCHEMY_CPU_AU1550:
496 return platform_driver_register(&alchemy_pcictl_driver);
497 }
498 return 0;
499}
500arch_initcall(alchemy_pci_init);
501
502
503int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
504{
505 struct alchemy_pci_context *ctx = dev->sysdata;
506 if (ctx && ctx->board_map_irq)
507 return ctx->board_map_irq(dev, slot, pin);
508 return -1;
509}
510
511int pcibios_plat_dev_init(struct pci_dev *dev)
512{
513 return 0;
514}