blob: deb67843693cddbf94ce2c2044999730a9425845 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pci-vdk.c: MB93090-MB00 (VDK) PCI support
2 *
3 * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/pci.h>
16#include <linux/init.h>
17#include <linux/ioport.h>
18#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/segment.h>
21#include <asm/io.h>
22#include <asm/mb-regs.h>
23#include <asm/mb86943a.h>
24#include "pci-frv.h"
25
26unsigned int __nongpreldata pci_probe = 1;
27
28int __nongpreldata pcibios_last_bus = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct pci_ops *__nongpreldata pci_root_ops;
30
31/*
David Howells77e38a52009-04-21 12:24:51 -070032 * The accessible PCI window does not cover the entire CPU address space, but
33 * there are devices we want to access outside of that window, so we need to
34 * insert specific PCI bus resources instead of using the platform-level bus
35 * resources directly for the PCI root bus.
36 *
37 * These are configured and inserted by pcibios_init() and are attached to the
38 * root bus by pcibios_fixup_bus().
39 */
40static struct resource pci_ioport_resource = {
41 .name = "PCI IO",
42 .start = 0,
43 .end = IO_SPACE_LIMIT,
44 .flags = IORESOURCE_IO,
45};
46
47static struct resource pci_iomem_resource = {
48 .name = "PCI mem",
49 .start = 0,
50 .end = -1,
51 .flags = IORESOURCE_MEM,
52};
53
54/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * Functions for accessing PCI configuration space
56 */
57
58#define CONFIG_CMD(bus, dev, where) \
59 (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
60
61#define __set_PciCfgAddr(A) writel((A), (volatile void __iomem *) __region_CS1 + 0x80)
62
63#define __get_PciCfgDataB(A) readb((volatile void __iomem *) __region_CS1 + 0x88 + ((A) & 3))
64#define __get_PciCfgDataW(A) readw((volatile void __iomem *) __region_CS1 + 0x88 + ((A) & 2))
65#define __get_PciCfgDataL(A) readl((volatile void __iomem *) __region_CS1 + 0x88)
66
67#define __set_PciCfgDataB(A,V) \
68 writeb((V), (volatile void __iomem *) __region_CS1 + 0x88 + (3 - ((A) & 3)))
69
70#define __set_PciCfgDataW(A,V) \
71 writew((V), (volatile void __iomem *) __region_CS1 + 0x88 + (2 - ((A) & 2)))
72
73#define __set_PciCfgDataL(A,V) \
74 writel((V), (volatile void __iomem *) __region_CS1 + 0x88)
75
76#define __get_PciBridgeDataB(A) readb((volatile void __iomem *) __region_CS1 + 0x800 + (A))
77#define __get_PciBridgeDataW(A) readw((volatile void __iomem *) __region_CS1 + 0x800 + (A))
78#define __get_PciBridgeDataL(A) readl((volatile void __iomem *) __region_CS1 + 0x800 + (A))
79
80#define __set_PciBridgeDataB(A,V) writeb((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
81#define __set_PciBridgeDataW(A,V) writew((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
82#define __set_PciBridgeDataL(A,V) writel((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
83
84static inline int __query(const struct pci_dev *dev)
85{
86// return dev->bus->number==0 && (dev->devfn==PCI_DEVFN(0,0));
87// return dev->bus->number==1;
88// return dev->bus->number==0 &&
89// (dev->devfn==PCI_DEVFN(2,0) || dev->devfn==PCI_DEVFN(3,0));
90 return 0;
91}
92
93/*****************************************************************************/
94/*
95 *
96 */
97static int pci_frv_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size,
98 u32 *val)
99{
100 u32 _value;
101
102 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
103 _value = __get_PciBridgeDataL(where & ~3);
104 }
105 else {
106 __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
107 _value = __get_PciCfgDataL(where & ~3);
108 }
109
110 switch (size) {
111 case 1:
112 _value = _value >> ((where & 3) * 8);
113 break;
114
115 case 2:
116 _value = _value >> ((where & 2) * 8);
117 break;
118
119 case 4:
120 break;
121
122 default:
123 BUG();
124 }
125
126 *val = _value;
127 return PCIBIOS_SUCCESSFUL;
128}
129
130static int pci_frv_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size,
131 u32 value)
132{
133 switch (size) {
134 case 1:
135 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
136 __set_PciBridgeDataB(where, value);
137 }
138 else {
139 __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
140 __set_PciCfgDataB(where, value);
141 }
142 break;
143
144 case 2:
145 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
146 __set_PciBridgeDataW(where, value);
147 }
148 else {
149 __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
150 __set_PciCfgDataW(where, value);
151 }
152 break;
153
154 case 4:
155 if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
156 __set_PciBridgeDataL(where, value);
157 }
158 else {
159 __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
160 __set_PciCfgDataL(where, value);
161 }
162 break;
163
164 default:
165 BUG();
166 }
167
168 return PCIBIOS_SUCCESSFUL;
169}
170
171static struct pci_ops pci_direct_frv = {
172 pci_frv_read_config,
173 pci_frv_write_config,
174};
175
176/*
177 * Before we decide to use direct hardware access mechanisms, we try to do some
178 * trivial checks to ensure it at least _seems_ to be working -- we just test
179 * whether bus 00 contains a host bridge (this is similar to checking
180 * techniques used in XFree86, but ours should be more reliable since we
181 * attempt to make use of direct access hints provided by the PCI BIOS).
182 *
183 * This should be close to trivial, but it isn't, because there are buggy
184 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
185 */
186static int __init pci_sanity_check(struct pci_ops *o)
187{
188 struct pci_bus bus; /* Fake bus and device */
189 u32 id;
190
191 bus.number = 0;
192
193 if (o->read(&bus, 0, PCI_VENDOR_ID, 4, &id) == PCIBIOS_SUCCESSFUL) {
194 printk("PCI: VDK Bridge device:vendor: %08x\n", id);
195 if (id == 0x200e10cf)
196 return 1;
197 }
198
199 printk("PCI: VDK Bridge: Sanity check failed\n");
200 return 0;
201}
202
203static struct pci_ops * __init pci_check_direct(void)
204{
205 unsigned long flags;
206
207 local_irq_save(flags);
208
209 /* check if access works */
210 if (pci_sanity_check(&pci_direct_frv)) {
211 local_irq_restore(flags);
212 printk("PCI: Using configuration frv\n");
213// request_mem_region(0xBE040000, 256, "FRV bridge");
214// request_mem_region(0xBFFFFFF4, 12, "PCI frv");
215 return &pci_direct_frv;
216 }
217
218 local_irq_restore(flags);
219 return NULL;
220}
221
222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Discover remaining PCI buses in case there are peer host bridges.
224 * We use the number of last PCI bus provided by the PCI BIOS.
225 */
226static void __init pcibios_fixup_peer_bridges(void)
227{
228 struct pci_bus bus;
229 struct pci_dev dev;
230 int n;
231 u16 l;
232
233 if (pcibios_last_bus <= 0 || pcibios_last_bus >= 0xff)
234 return;
235 printk("PCI: Peer bridge fixup\n");
236 for (n=0; n <= pcibios_last_bus; n++) {
237 if (pci_find_bus(0, n))
238 continue;
239 bus.number = n;
240 bus.ops = pci_root_ops;
241 dev.bus = &bus;
242 for(dev.devfn=0; dev.devfn<256; dev.devfn += 8)
243 if (!pci_read_config_word(&dev, PCI_VENDOR_ID, &l) &&
244 l != 0x0000 && l != 0xffff) {
245 printk("Found device at %02x:%02x [%04x]\n", n, dev.devfn, l);
246 printk("PCI: Discovered peer bus %02x\n", n);
247 pci_scan_bus(n, pci_root_ops, NULL);
248 break;
249 }
250 }
251}
252
253/*
254 * Exceptions for specific devices. Usually work-arounds for fatal design flaws.
255 */
256
257static void __init pci_fixup_umc_ide(struct pci_dev *d)
258{
259 /*
260 * UM8886BF IDE controller sets region type bits incorrectly,
261 * therefore they look like memory despite of them being I/O.
262 */
263 int i;
264
265 printk("PCI: Fixing base address flags for device %s\n", pci_name(d));
266 for(i=0; i<4; i++)
267 d->resource[i].flags |= PCI_BASE_ADDRESS_SPACE_IO;
268}
269
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -0800270static void pci_fixup_ide_bases(struct pci_dev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int i;
273
274 /*
275 * PCI IDE controllers use non-standard I/O port decoding, respect it.
276 */
277 if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
278 return;
279 printk("PCI: IDE base address fixup for %s\n", pci_name(d));
280 for(i=0; i<4; i++) {
281 struct resource *r = &d->resource[i];
282 if ((r->start & ~0x80) == 0x374) {
283 r->start |= 2;
284 r->end = r->start;
285 }
286 }
287}
288
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -0800289static void pci_fixup_ide_trash(struct pci_dev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
292
293 /*
294 * There exist PCI IDE controllers which have utter garbage
295 * in first four base registers. Ignore that.
296 */
297 printk("PCI: IDE base address trash cleared for %s\n", pci_name(d));
298 for(i=0; i<4; i++)
299 d->resource[i].start = d->resource[i].end = d->resource[i].flags = 0;
300}
301
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -0800302static void pci_fixup_latency(struct pci_dev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 /*
305 * SiS 5597 and 5598 chipsets require latency timer set to
306 * at most 32 to avoid lockups.
307 */
308 DBG("PCI: Setting max latency to 32\n");
309 pcibios_max_latency = 32;
310}
311
312DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_UMC, PCI_DEVICE_ID_UMC_UM8886BF, pci_fixup_umc_ide);
313DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, pci_fixup_ide_trash);
314DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5597, pci_fixup_latency);
315DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5598, pci_fixup_latency);
316DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
317
318/*
319 * Called after each bus is probed, but before its children
320 * are examined.
321 */
322
Bjorn Helgaas24336a62013-07-05 15:17:27 -0600323void pcibios_fixup_bus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325#if 0
326 printk("### PCIBIOS_FIXUP_BUS(%d)\n",bus->number);
327#endif
David Howells77e38a52009-04-21 12:24:51 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 pci_read_bridge_bases(bus);
330
331 if (bus->number == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct pci_dev *dev;
Bjorn Helgaasfd376832012-08-16 16:44:18 -0600333 list_for_each_entry(dev, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (dev->devfn == 0) {
335 dev->resource[0].start = 0;
336 dev->resource[0].end = 0;
337 }
338 }
339 }
340}
341
342/*
343 * Initialization. Try all known PCI access methods. Note that we support
344 * using both PCI BIOS and direct access: in such cases, we use I/O ports
345 * to access config space, but we still keep BIOS order of cards to be
346 * compatible with 2.0.X. This should go away some day.
347 */
348
349int __init pcibios_init(void)
350{
351 struct pci_ops *dir = NULL;
Bjorn Helgaase2a79652011-10-28 16:26:21 -0600352 LIST_HEAD(resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!mb93090_mb00_detected)
355 return -ENXIO;
356
357 __reg_MB86943_sl_ctl |= MB86943_SL_CTL_DRCT_MASTER_SWAP | MB86943_SL_CTL_DRCT_SLAVE_SWAP;
358
359 __reg_MB86943_ecs_base(1) = ((__region_CS2 + 0x01000000) >> 9) | 0x08000000;
360 __reg_MB86943_ecs_base(2) = ((__region_CS2 + 0x00000000) >> 9) | 0x08000000;
361
362 *(volatile uint32_t *) (__region_CS1 + 0x848) = 0xe0000000;
363 *(volatile uint32_t *) (__region_CS1 + 0x8b8) = 0x00000000;
364
365 __reg_MB86943_sl_pci_io_base = (__region_CS2 + 0x04000000) >> 9;
366 __reg_MB86943_sl_pci_mem_base = (__region_CS2 + 0x08000000) >> 9;
367 __reg_MB86943_pci_sl_io_base = __region_CS2 + 0x04000000;
368 __reg_MB86943_pci_sl_mem_base = __region_CS2 + 0x08000000;
369 mb();
370
David Howells04668872007-08-01 19:04:51 +0100371 /* enable PCI arbitration */
372 __reg_MB86943_pci_arbiter = MB86943_PCIARB_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howells77e38a52009-04-21 12:24:51 -0700374 pci_ioport_resource.start = (__reg_MB86943_sl_pci_io_base << 9) & 0xfffffc00;
375 pci_ioport_resource.end = (__reg_MB86943_sl_pci_io_range << 9) | 0x3ff;
376 pci_ioport_resource.end += pci_ioport_resource.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Howells9dec17e2006-07-10 04:44:51 -0700378 printk("PCI IO window: %08llx-%08llx\n",
David Howells77e38a52009-04-21 12:24:51 -0700379 (unsigned long long) pci_ioport_resource.start,
380 (unsigned long long) pci_ioport_resource.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David Howells77e38a52009-04-21 12:24:51 -0700382 pci_iomem_resource.start = (__reg_MB86943_sl_pci_mem_base << 9) & 0xfffffc00;
383 pci_iomem_resource.end = (__reg_MB86943_sl_pci_mem_range << 9) | 0x3ff;
384 pci_iomem_resource.end += pci_iomem_resource.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Howells77e38a52009-04-21 12:24:51 -0700386 /* Reserve somewhere to write to flush posted writes. This is used by
387 * __flush_PCI_writes() from asm/io.h to force the write FIFO in the
388 * CPU-PCI bridge to flush as this doesn't happen automatically when a
389 * read is performed on the MB93090 development kit motherboard.
390 */
391 pci_iomem_resource.start += 0x400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howells9dec17e2006-07-10 04:44:51 -0700393 printk("PCI MEM window: %08llx-%08llx\n",
David Howells77e38a52009-04-21 12:24:51 -0700394 (unsigned long long) pci_iomem_resource.start,
395 (unsigned long long) pci_iomem_resource.end);
David Howells9dec17e2006-07-10 04:44:51 -0700396 printk("PCI DMA memory: %08lx-%08lx\n",
397 dma_coherent_mem_start, dma_coherent_mem_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howells77e38a52009-04-21 12:24:51 -0700399 if (insert_resource(&iomem_resource, &pci_iomem_resource) < 0)
400 panic("Unable to insert PCI IOMEM resource\n");
401 if (insert_resource(&ioport_resource, &pci_ioport_resource) < 0)
402 panic("Unable to insert PCI IOPORT resource\n");
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!pci_probe)
405 return -ENXIO;
406
407 dir = pci_check_direct();
408 if (dir)
409 pci_root_ops = dir;
410 else {
411 printk("PCI: No PCI bus detected\n");
412 return -ENXIO;
413 }
414
415 printk("PCI: Probing PCI hardware\n");
Bjorn Helgaase2a79652011-10-28 16:26:21 -0600416 pci_add_resource(&resources, &pci_ioport_resource);
417 pci_add_resource(&resources, &pci_iomem_resource);
Bjorn Helgaasca75e032013-01-03 15:30:09 -0700418 pci_scan_root_bus(NULL, 0, pci_root_ops, NULL, &resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 pcibios_irq_init();
421 pcibios_fixup_peer_bridges();
422 pcibios_fixup_irqs();
423 pcibios_resource_survey();
424
425 return 0;
426}
427
428arch_initcall(pcibios_init);
429
430char * __init pcibios_setup(char *str)
431{
432 if (!strcmp(str, "off")) {
433 pci_probe = 0;
434 return NULL;
435 } else if (!strncmp(str, "lastbus=", 8)) {
436 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
437 return NULL;
438 }
439 return str;
440}
441
442int pcibios_enable_device(struct pci_dev *dev, int mask)
443{
444 int err;
445
Bjorn Helgaas9bd8f9c2008-10-15 16:50:48 +0100446 if ((err = pci_enable_resources(dev, mask)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return err;
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200448 if (!dev->msi_enabled)
449 pcibios_enable_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451}