blob: a3239b0f96562620b9a3734f321e54e8895b4517 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* ASB2305 PCI support
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * Derived from arch/i386/kernel/pci-pc.c
6 * (c) 1999--2000 Martin Mares <mj@suse.cz>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
12 */
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/pci.h>
17#include <linux/init.h>
18#include <linux/ioport.h>
19#include <linux/delay.h>
David Howells00a2f912012-12-12 15:36:40 +000020#include <linux/irq.h>
David Howellsb920de12008-02-08 04:19:31 -080021#include <asm/io.h>
22#include "pci-asb2305.h"
23
24unsigned int pci_probe = 1;
25
26int pcibios_last_bus = -1;
27struct pci_bus *pci_root_bus;
28struct pci_ops *pci_root_ops;
29
30/*
David Howells112b4a02010-01-08 14:43:20 -080031 * The accessible PCI window does not cover the entire CPU address space, but
32 * there are devices we want to access outside of that window, so we need to
33 * insert specific PCI bus resources instead of using the platform-level bus
34 * resources directly for the PCI root bus.
35 *
Bjorn Helgaas4b84b6e2012-02-23 20:19:02 -070036 * These are configured and inserted by pcibios_init().
David Howells112b4a02010-01-08 14:43:20 -080037 */
38static struct resource pci_ioport_resource = {
39 .name = "PCI IO",
40 .start = 0xbe000000,
41 .end = 0xbe03ffff,
42 .flags = IORESOURCE_IO,
43};
44
45static struct resource pci_iomem_resource = {
46 .name = "PCI mem",
47 .start = 0xb8000000,
48 .end = 0xbbffffff,
49 .flags = IORESOURCE_MEM,
50};
51
52/*
David Howellsb920de12008-02-08 04:19:31 -080053 * Functions for accessing PCI configuration space
54 */
55
56#define CONFIG_CMD(bus, devfn, where) \
57 (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
58
59#define MEM_PAGING_REG (*(volatile __u32 *) 0xBFFFFFF4)
60#define CONFIG_ADDRESS (*(volatile __u32 *) 0xBFFFFFF8)
61#define CONFIG_DATAL(X) (*(volatile __u32 *) 0xBFFFFFFC)
62#define CONFIG_DATAW(X) (*(volatile __u16 *) (0xBFFFFFFC + ((X) & 2)))
63#define CONFIG_DATAB(X) (*(volatile __u8 *) (0xBFFFFFFC + ((X) & 3)))
64
65#define BRIDGEREGB(X) (*(volatile __u8 *) (0xBE040000 + (X)))
66#define BRIDGEREGW(X) (*(volatile __u16 *) (0xBE040000 + (X)))
67#define BRIDGEREGL(X) (*(volatile __u32 *) (0xBE040000 + (X)))
68
69static inline int __query(const struct pci_bus *bus, unsigned int devfn)
70{
71#if 0
72 return bus->number == 0 && (devfn == PCI_DEVFN(0, 0));
73 return bus->number == 1;
74 return bus->number == 0 &&
75 (devfn == PCI_DEVFN(2, 0) || devfn == PCI_DEVFN(3, 0));
76#endif
77 return 1;
78}
79
80/*
David Howellsb920de12008-02-08 04:19:31 -080081 *
82 */
83static int pci_ampci_read_config_byte(struct pci_bus *bus, unsigned int devfn,
84 int where, u32 *_value)
85{
86 u32 rawval, value;
87
88 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
89 value = BRIDGEREGB(where);
90 __pcbdebug("=> %02hx", &BRIDGEREGL(where), value);
91 } else {
92 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
93 rawval = CONFIG_ADDRESS;
94 value = CONFIG_DATAB(where);
95 if (__query(bus, devfn))
96 __pcidebug("=> %02hx", bus, devfn, where, value);
97 }
98
99 *_value = value;
100 return PCIBIOS_SUCCESSFUL;
101}
102
103static int pci_ampci_read_config_word(struct pci_bus *bus, unsigned int devfn,
104 int where, u32 *_value)
105{
106 u32 rawval, value;
107
108 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
109 value = BRIDGEREGW(where);
110 __pcbdebug("=> %04hx", &BRIDGEREGL(where), value);
111 } else {
112 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
113 rawval = CONFIG_ADDRESS;
114 value = CONFIG_DATAW(where);
115 if (__query(bus, devfn))
116 __pcidebug("=> %04hx", bus, devfn, where, value);
117 }
118
119 *_value = value;
120 return PCIBIOS_SUCCESSFUL;
121}
122
123static int pci_ampci_read_config_dword(struct pci_bus *bus, unsigned int devfn,
124 int where, u32 *_value)
125{
126 u32 rawval, value;
127
128 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
129 value = BRIDGEREGL(where);
130 __pcbdebug("=> %08x", &BRIDGEREGL(where), value);
131 } else {
132 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
133 rawval = CONFIG_ADDRESS;
134 value = CONFIG_DATAL(where);
135 if (__query(bus, devfn))
136 __pcidebug("=> %08x", bus, devfn, where, value);
137 }
138
139 *_value = value;
140 return PCIBIOS_SUCCESSFUL;
141}
142
143static int pci_ampci_write_config_byte(struct pci_bus *bus, unsigned int devfn,
144 int where, u8 value)
145{
146 u32 rawval;
147
148 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
149 __pcbdebug("<= %02x", &BRIDGEREGB(where), value);
150 BRIDGEREGB(where) = value;
151 } else {
152 if (bus->number == 0 &&
Wei Yongjund9190912009-02-20 15:38:40 -0800153 (devfn == PCI_DEVFN(2, 0) || devfn == PCI_DEVFN(3, 0))
David Howellsb920de12008-02-08 04:19:31 -0800154 )
155 __pcidebug("<= %02x", bus, devfn, where, value);
156 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
157 rawval = CONFIG_ADDRESS;
158 CONFIG_DATAB(where) = value;
159 }
160 return PCIBIOS_SUCCESSFUL;
161}
162
163static int pci_ampci_write_config_word(struct pci_bus *bus, unsigned int devfn,
164 int where, u16 value)
165{
166 u32 rawval;
167
168 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
169 __pcbdebug("<= %04hx", &BRIDGEREGW(where), value);
170 BRIDGEREGW(where) = value;
171 } else {
172 if (__query(bus, devfn))
173 __pcidebug("<= %04hx", bus, devfn, where, value);
174 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
175 rawval = CONFIG_ADDRESS;
176 CONFIG_DATAW(where) = value;
177 }
178 return PCIBIOS_SUCCESSFUL;
179}
180
181static int pci_ampci_write_config_dword(struct pci_bus *bus, unsigned int devfn,
182 int where, u32 value)
183{
184 u32 rawval;
185
186 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
187 __pcbdebug("<= %08x", &BRIDGEREGL(where), value);
188 BRIDGEREGL(where) = value;
189 } else {
190 if (__query(bus, devfn))
191 __pcidebug("<= %08x", bus, devfn, where, value);
192 CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
193 rawval = CONFIG_ADDRESS;
194 CONFIG_DATAL(where) = value;
195 }
196 return PCIBIOS_SUCCESSFUL;
197}
198
199static int pci_ampci_read_config(struct pci_bus *bus, unsigned int devfn,
200 int where, int size, u32 *val)
201{
202 switch (size) {
203 case 1:
204 return pci_ampci_read_config_byte(bus, devfn, where, val);
205 case 2:
206 return pci_ampci_read_config_word(bus, devfn, where, val);
207 case 4:
208 return pci_ampci_read_config_dword(bus, devfn, where, val);
209 default:
210 BUG();
211 return -EOPNOTSUPP;
212 }
213}
214
215static int pci_ampci_write_config(struct pci_bus *bus, unsigned int devfn,
216 int where, int size, u32 val)
217{
218 switch (size) {
219 case 1:
220 return pci_ampci_write_config_byte(bus, devfn, where, val);
221 case 2:
222 return pci_ampci_write_config_word(bus, devfn, where, val);
223 case 4:
224 return pci_ampci_write_config_dword(bus, devfn, where, val);
225 default:
226 BUG();
227 return -EOPNOTSUPP;
228 }
229}
230
231static struct pci_ops pci_direct_ampci = {
232 pci_ampci_read_config,
233 pci_ampci_write_config,
234};
235
236/*
237 * Before we decide to use direct hardware access mechanisms, we try to do some
238 * trivial checks to ensure it at least _seems_ to be working -- we just test
239 * whether bus 00 contains a host bridge (this is similar to checking
240 * techniques used in XFree86, but ours should be more reliable since we
241 * attempt to make use of direct access hints provided by the PCI BIOS).
242 *
243 * This should be close to trivial, but it isn't, because there are buggy
244 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
245 */
246static int __init pci_sanity_check(struct pci_ops *o)
247{
248 struct pci_bus bus; /* Fake bus and device */
249 u32 x;
250
251 bus.number = 0;
252
253 if ((!o->read(&bus, 0, PCI_CLASS_DEVICE, 2, &x) &&
254 (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)) ||
255 (!o->read(&bus, 0, PCI_VENDOR_ID, 2, &x) &&
256 (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)))
257 return 1;
258
David Howellse7163812010-01-08 14:43:18 -0800259 printk(KERN_ERR "PCI: Sanity check failed\n");
David Howellsb920de12008-02-08 04:19:31 -0800260 return 0;
261}
262
263static int __init pci_check_direct(void)
264{
265 unsigned long flags;
266
267 local_irq_save(flags);
268
269 /*
270 * Check if access works.
271 */
272 if (pci_sanity_check(&pci_direct_ampci)) {
273 local_irq_restore(flags);
274 printk(KERN_INFO "PCI: Using configuration ampci\n");
275 request_mem_region(0xBE040000, 256, "AMPCI bridge");
276 request_mem_region(0xBFFFFFF4, 12, "PCI ampci");
David Howells112b4a02010-01-08 14:43:20 -0800277 request_mem_region(0xBC000000, 32 * 1024 * 1024, "PCI SRAM");
David Howellsb920de12008-02-08 04:19:31 -0800278 return 0;
279 }
280
281 local_irq_restore(flags);
282 return -ENODEV;
283}
284
285static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
286{
287 unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700288 struct resource *devr = &dev->resource[idx], *busr;
David Howellsb920de12008-02-08 04:19:31 -0800289
290 if (dev->bus) {
Bjorn Helgaas89a74ec2010-02-23 10:24:31 -0700291 pci_bus_for_each_resource(dev->bus, busr, i) {
David Howellsb920de12008-02-08 04:19:31 -0800292 if (!busr || (busr->flags ^ devr->flags) & type_mask)
293 continue;
294
295 if (devr->start &&
296 devr->start >= busr->start &&
297 devr->end <= busr->end)
298 return 1;
299 }
300 }
301
302 return 0;
303}
304
305static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
306{
307 struct pci_bus_region region;
308 int i;
309 int limit;
310
311 if (dev->bus->number != 0)
312 return;
313
314 limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ?
315 PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
316
317 for (i = 0; i < limit; i++) {
318 if (!dev->resource[i].flags)
319 continue;
320
David Howellsb920de12008-02-08 04:19:31 -0800321 if (is_valid_resource(dev, i))
322 pci_claim_resource(dev, i);
323 }
324}
325
326/*
327 * Called after each bus is probed, but before its children
328 * are examined.
329 */
330void __devinit pcibios_fixup_bus(struct pci_bus *bus)
331{
332 struct pci_dev *dev;
333
334 if (bus->self) {
335 pci_read_bridge_bases(bus);
336 pcibios_fixup_device_resources(bus->self);
337 }
338
339 list_for_each_entry(dev, &bus->devices, bus_list)
340 pcibios_fixup_device_resources(dev);
341}
342
343/*
344 * Initialization. Try all known PCI access methods. Note that we support
345 * using both PCI BIOS and direct access: in such cases, we use I/O ports
346 * to access config space, but we still keep BIOS order of cards to be
347 * compatible with 2.0.X. This should go away some day.
348 */
349static int __init pcibios_init(void)
350{
Bjorn Helgaas4b84b6e2012-02-23 20:19:02 -0700351 resource_size_t io_offset, mem_offset;
Bjorn Helgaas9a458002011-10-28 16:27:02 -0600352 LIST_HEAD(resources);
353
David Howellsb920de12008-02-08 04:19:31 -0800354 ioport_resource.start = 0xA0000000;
355 ioport_resource.end = 0xDFFFFFFF;
356 iomem_resource.start = 0xA0000000;
357 iomem_resource.end = 0xDFFFFFFF;
358
David Howells112b4a02010-01-08 14:43:20 -0800359 if (insert_resource(&iomem_resource, &pci_iomem_resource) < 0)
360 panic("Unable to insert PCI IOMEM resource\n");
361 if (insert_resource(&ioport_resource, &pci_ioport_resource) < 0)
362 panic("Unable to insert PCI IOPORT resource\n");
363
David Howellsb920de12008-02-08 04:19:31 -0800364 if (!pci_probe)
365 return 0;
366
367 if (pci_check_direct() < 0) {
368 printk(KERN_WARNING "PCI: No PCI bus detected\n");
369 return 0;
370 }
371
372 printk(KERN_INFO "PCI: Probing PCI hardware [mempage %08x]\n",
373 MEM_PAGING_REG);
374
Bjorn Helgaas4b84b6e2012-02-23 20:19:02 -0700375 io_offset = pci_ioport_resource.start -
376 (pci_ioport_resource.start & 0x00ffffff);
377 mem_offset = pci_iomem_resource.start -
378 ((pci_iomem_resource.start & 0x03ffffff) | MEM_PAGING_REG);
379
380 pci_add_resource_offset(&resources, &pci_ioport_resource, io_offset);
381 pci_add_resource_offset(&resources, &pci_iomem_resource, mem_offset);
Bjorn Helgaas9a458002011-10-28 16:27:02 -0600382 pci_root_bus = pci_scan_root_bus(NULL, 0, &pci_direct_ampci, NULL,
383 &resources);
David Howellsb920de12008-02-08 04:19:31 -0800384
385 pcibios_irq_init();
386 pcibios_fixup_irqs();
David Howellsb920de12008-02-08 04:19:31 -0800387 pcibios_resource_survey();
David Howellsb920de12008-02-08 04:19:31 -0800388 return 0;
389}
390
391arch_initcall(pcibios_init);
392
393char *__init pcibios_setup(char *str)
394{
395 if (!strcmp(str, "off")) {
396 pci_probe = 0;
397 return NULL;
398
399 } else if (!strncmp(str, "lastbus=", 8)) {
400 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
401 return NULL;
402 }
403
404 return str;
405}
406
407int pcibios_enable_device(struct pci_dev *dev, int mask)
408{
409 int err;
410
Bjorn Helgaas126cda52010-01-08 14:43:19 -0800411 err = pci_enable_resources(dev, mask);
David Howellsb920de12008-02-08 04:19:31 -0800412 if (err == 0)
413 pcibios_enable_irq(dev);
414 return err;
415}
416
417/*
418 * disable the ethernet chipset
419 */
420static void __init unit_disable_pcnet(struct pci_bus *bus, struct pci_ops *o)
421{
422 u32 x;
423
424 bus->number = 0;
425
Bjorn Helgaas126cda52010-01-08 14:43:19 -0800426 o->read (bus, PCI_DEVFN(2, 0), PCI_VENDOR_ID, 4, &x);
David Howellsb920de12008-02-08 04:19:31 -0800427 o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
428 x |= PCI_COMMAND_MASTER |
429 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
430 PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
431 o->write(bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, x);
432 o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
433 o->write(bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, 0x00030001);
434 o->read (bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, &x);
435
436#define RDP (*(volatile u32 *) 0xBE030010)
437#define RAP (*(volatile u32 *) 0xBE030014)
438#define __set_RAP(X) do { RAP = (X); x = RAP; } while (0)
439#define __set_RDP(X) do { RDP = (X); x = RDP; } while (0)
440#define __get_RDP() ({ RDP & 0xffff; })
441
442 __set_RAP(0);
443 __set_RDP(0x0004); /* CSR0 = STOP */
444
445 __set_RAP(88); /* check CSR88 indicates an Am79C973 */
446 BUG_ON(__get_RDP() != 0x5003);
447
448 for (x = 0; x < 100; x++)
449 asm volatile("nop");
450
451 __set_RDP(0x0004); /* CSR0 = STOP */
452}
453
454/*
455 * initialise the unit hardware
456 */
457asmlinkage void __init unit_pci_init(void)
458{
459 struct pci_bus bus; /* Fake bus and device */
460 struct pci_ops *o = &pci_direct_ampci;
461 u32 x;
462
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100463 set_intr_level(XIRQ1, NUM2GxICR_LEVEL(CONFIG_PCI_IRQ_LEVEL));
David Howellsb920de12008-02-08 04:19:31 -0800464
465 memset(&bus, 0, sizeof(bus));
466
467 MEM_PAGING_REG = 0xE8000000;
468
469 /* we need to set up the bridge _now_ or we won't be able to access the
470 * PCI config registers
471 */
472 BRIDGEREGW(PCI_COMMAND) |=
473 PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
474 PCI_COMMAND_MEMORY | PCI_COMMAND_IO | PCI_COMMAND_MASTER;
475 BRIDGEREGW(PCI_STATUS) = 0xF800;
476 BRIDGEREGB(PCI_LATENCY_TIMER) = 0x10;
477 BRIDGEREGL(PCI_BASE_ADDRESS_0) = 0x80000000;
478 BRIDGEREGB(PCI_INTERRUPT_LINE) = 1;
479 BRIDGEREGL(0x48) = 0x98000000; /* AMPCI base addr */
480 BRIDGEREGB(0x41) = 0x00; /* secondary bus
481 * number */
482 BRIDGEREGB(0x42) = 0x01; /* subordinate bus
483 * number */
484 BRIDGEREGB(0x44) = 0x01;
485 BRIDGEREGL(0x50) = 0x00000001;
486 BRIDGEREGL(0x58) = 0x00001002;
487 BRIDGEREGL(0x5C) = 0x00000011;
488
489 /* we also need to set up the PCI-PCI bridge */
490 bus.number = 0;
491
492 /* IO: 0x00000000-0x00020000 */
493 o->read (&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, &x);
494 x |= PCI_COMMAND_MASTER |
495 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
496 PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
497 o->write(&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, x);
498
499 o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
500 o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
501 o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
502 o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
503
504 o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, 0x01);
505 o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
506 o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, 0x00020000);
507 o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
508 o->write(&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, 0xEBB0EA00);
509 o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
510 o->write(&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, 0xE9F0E800);
511 o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
512
513 unit_disable_pcnet(&bus, o);
514}