pci-vdk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* 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. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.h>
  16. #include <linux/ioport.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <asm/segment.h>
  20. #include <asm/io.h>
  21. #include <asm/mb-regs.h>
  22. #include <asm/mb86943a.h>
  23. #include "pci-frv.h"
  24. unsigned int __nongpreldata pci_probe = 1;
  25. int __nongpreldata pcibios_last_bus = -1;
  26. struct pci_bus *__nongpreldata pci_root_bus;
  27. struct pci_ops *__nongpreldata pci_root_ops;
  28. /*
  29. * Functions for accessing PCI configuration space
  30. */
  31. #define CONFIG_CMD(bus, dev, where) \
  32. (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  33. #define __set_PciCfgAddr(A) writel((A), (volatile void __iomem *) __region_CS1 + 0x80)
  34. #define __get_PciCfgDataB(A) readb((volatile void __iomem *) __region_CS1 + 0x88 + ((A) & 3))
  35. #define __get_PciCfgDataW(A) readw((volatile void __iomem *) __region_CS1 + 0x88 + ((A) & 2))
  36. #define __get_PciCfgDataL(A) readl((volatile void __iomem *) __region_CS1 + 0x88)
  37. #define __set_PciCfgDataB(A,V) \
  38. writeb((V), (volatile void __iomem *) __region_CS1 + 0x88 + (3 - ((A) & 3)))
  39. #define __set_PciCfgDataW(A,V) \
  40. writew((V), (volatile void __iomem *) __region_CS1 + 0x88 + (2 - ((A) & 2)))
  41. #define __set_PciCfgDataL(A,V) \
  42. writel((V), (volatile void __iomem *) __region_CS1 + 0x88)
  43. #define __get_PciBridgeDataB(A) readb((volatile void __iomem *) __region_CS1 + 0x800 + (A))
  44. #define __get_PciBridgeDataW(A) readw((volatile void __iomem *) __region_CS1 + 0x800 + (A))
  45. #define __get_PciBridgeDataL(A) readl((volatile void __iomem *) __region_CS1 + 0x800 + (A))
  46. #define __set_PciBridgeDataB(A,V) writeb((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
  47. #define __set_PciBridgeDataW(A,V) writew((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
  48. #define __set_PciBridgeDataL(A,V) writel((V), (volatile void __iomem *) __region_CS1 + 0x800 + (A))
  49. static inline int __query(const struct pci_dev *dev)
  50. {
  51. // return dev->bus->number==0 && (dev->devfn==PCI_DEVFN(0,0));
  52. // return dev->bus->number==1;
  53. // return dev->bus->number==0 &&
  54. // (dev->devfn==PCI_DEVFN(2,0) || dev->devfn==PCI_DEVFN(3,0));
  55. return 0;
  56. }
  57. /*****************************************************************************/
  58. /*
  59. *
  60. */
  61. static int pci_frv_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size,
  62. u32 *val)
  63. {
  64. u32 _value;
  65. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  66. _value = __get_PciBridgeDataL(where & ~3);
  67. }
  68. else {
  69. __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
  70. _value = __get_PciCfgDataL(where & ~3);
  71. }
  72. switch (size) {
  73. case 1:
  74. _value = _value >> ((where & 3) * 8);
  75. break;
  76. case 2:
  77. _value = _value >> ((where & 2) * 8);
  78. break;
  79. case 4:
  80. break;
  81. default:
  82. BUG();
  83. }
  84. *val = _value;
  85. return PCIBIOS_SUCCESSFUL;
  86. }
  87. static int pci_frv_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size,
  88. u32 value)
  89. {
  90. switch (size) {
  91. case 1:
  92. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  93. __set_PciBridgeDataB(where, value);
  94. }
  95. else {
  96. __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
  97. __set_PciCfgDataB(where, value);
  98. }
  99. break;
  100. case 2:
  101. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  102. __set_PciBridgeDataW(where, value);
  103. }
  104. else {
  105. __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
  106. __set_PciCfgDataW(where, value);
  107. }
  108. break;
  109. case 4:
  110. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  111. __set_PciBridgeDataL(where, value);
  112. }
  113. else {
  114. __set_PciCfgAddr(CONFIG_CMD(bus, devfn, where));
  115. __set_PciCfgDataL(where, value);
  116. }
  117. break;
  118. default:
  119. BUG();
  120. }
  121. return PCIBIOS_SUCCESSFUL;
  122. }
  123. static struct pci_ops pci_direct_frv = {
  124. pci_frv_read_config,
  125. pci_frv_write_config,
  126. };
  127. /*
  128. * Before we decide to use direct hardware access mechanisms, we try to do some
  129. * trivial checks to ensure it at least _seems_ to be working -- we just test
  130. * whether bus 00 contains a host bridge (this is similar to checking
  131. * techniques used in XFree86, but ours should be more reliable since we
  132. * attempt to make use of direct access hints provided by the PCI BIOS).
  133. *
  134. * This should be close to trivial, but it isn't, because there are buggy
  135. * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  136. */
  137. static int __init pci_sanity_check(struct pci_ops *o)
  138. {
  139. struct pci_bus bus; /* Fake bus and device */
  140. u32 id;
  141. bus.number = 0;
  142. if (o->read(&bus, 0, PCI_VENDOR_ID, 4, &id) == PCIBIOS_SUCCESSFUL) {
  143. printk("PCI: VDK Bridge device:vendor: %08x\n", id);
  144. if (id == 0x200e10cf)
  145. return 1;
  146. }
  147. printk("PCI: VDK Bridge: Sanity check failed\n");
  148. return 0;
  149. }
  150. static struct pci_ops * __init pci_check_direct(void)
  151. {
  152. unsigned long flags;
  153. local_irq_save(flags);
  154. /* check if access works */
  155. if (pci_sanity_check(&pci_direct_frv)) {
  156. local_irq_restore(flags);
  157. printk("PCI: Using configuration frv\n");
  158. // request_mem_region(0xBE040000, 256, "FRV bridge");
  159. // request_mem_region(0xBFFFFFF4, 12, "PCI frv");
  160. return &pci_direct_frv;
  161. }
  162. local_irq_restore(flags);
  163. return NULL;
  164. }
  165. /*
  166. * Several buggy motherboards address only 16 devices and mirror
  167. * them to next 16 IDs. We try to detect this `feature' on all
  168. * primary buses (those containing host bridges as they are
  169. * expected to be unique) and remove the ghost devices.
  170. */
  171. static void __init pcibios_fixup_ghosts(struct pci_bus *b)
  172. {
  173. struct list_head *ln, *mn;
  174. struct pci_dev *d, *e;
  175. int mirror = PCI_DEVFN(16,0);
  176. int seen_host_bridge = 0;
  177. int i;
  178. for (ln=b->devices.next; ln != &b->devices; ln=ln->next) {
  179. d = pci_dev_b(ln);
  180. if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  181. seen_host_bridge++;
  182. for (mn=ln->next; mn != &b->devices; mn=mn->next) {
  183. e = pci_dev_b(mn);
  184. if (e->devfn != d->devfn + mirror ||
  185. e->vendor != d->vendor ||
  186. e->device != d->device ||
  187. e->class != d->class)
  188. continue;
  189. for(i=0; i<PCI_NUM_RESOURCES; i++)
  190. if (e->resource[i].start != d->resource[i].start ||
  191. e->resource[i].end != d->resource[i].end ||
  192. e->resource[i].flags != d->resource[i].flags)
  193. continue;
  194. break;
  195. }
  196. if (mn == &b->devices)
  197. return;
  198. }
  199. if (!seen_host_bridge)
  200. return;
  201. printk("PCI: Ignoring ghost devices on bus %02x\n", b->number);
  202. ln = &b->devices;
  203. while (ln->next != &b->devices) {
  204. d = pci_dev_b(ln->next);
  205. if (d->devfn >= mirror) {
  206. list_del(&d->global_list);
  207. list_del(&d->bus_list);
  208. kfree(d);
  209. } else
  210. ln = ln->next;
  211. }
  212. }
  213. /*
  214. * Discover remaining PCI buses in case there are peer host bridges.
  215. * We use the number of last PCI bus provided by the PCI BIOS.
  216. */
  217. static void __init pcibios_fixup_peer_bridges(void)
  218. {
  219. struct pci_bus bus;
  220. struct pci_dev dev;
  221. int n;
  222. u16 l;
  223. if (pcibios_last_bus <= 0 || pcibios_last_bus >= 0xff)
  224. return;
  225. printk("PCI: Peer bridge fixup\n");
  226. for (n=0; n <= pcibios_last_bus; n++) {
  227. if (pci_find_bus(0, n))
  228. continue;
  229. bus.number = n;
  230. bus.ops = pci_root_ops;
  231. dev.bus = &bus;
  232. for(dev.devfn=0; dev.devfn<256; dev.devfn += 8)
  233. if (!pci_read_config_word(&dev, PCI_VENDOR_ID, &l) &&
  234. l != 0x0000 && l != 0xffff) {
  235. printk("Found device at %02x:%02x [%04x]\n", n, dev.devfn, l);
  236. printk("PCI: Discovered peer bus %02x\n", n);
  237. pci_scan_bus(n, pci_root_ops, NULL);
  238. break;
  239. }
  240. }
  241. }
  242. /*
  243. * Exceptions for specific devices. Usually work-arounds for fatal design flaws.
  244. */
  245. static void __init pci_fixup_umc_ide(struct pci_dev *d)
  246. {
  247. /*
  248. * UM8886BF IDE controller sets region type bits incorrectly,
  249. * therefore they look like memory despite of them being I/O.
  250. */
  251. int i;
  252. printk("PCI: Fixing base address flags for device %s\n", pci_name(d));
  253. for(i=0; i<4; i++)
  254. d->resource[i].flags |= PCI_BASE_ADDRESS_SPACE_IO;
  255. }
  256. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  257. {
  258. int i;
  259. /*
  260. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  261. */
  262. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  263. return;
  264. printk("PCI: IDE base address fixup for %s\n", pci_name(d));
  265. for(i=0; i<4; i++) {
  266. struct resource *r = &d->resource[i];
  267. if ((r->start & ~0x80) == 0x374) {
  268. r->start |= 2;
  269. r->end = r->start;
  270. }
  271. }
  272. }
  273. static void __init pci_fixup_ide_trash(struct pci_dev *d)
  274. {
  275. int i;
  276. /*
  277. * There exist PCI IDE controllers which have utter garbage
  278. * in first four base registers. Ignore that.
  279. */
  280. printk("PCI: IDE base address trash cleared for %s\n", pci_name(d));
  281. for(i=0; i<4; i++)
  282. d->resource[i].start = d->resource[i].end = d->resource[i].flags = 0;
  283. }
  284. static void __devinit pci_fixup_latency(struct pci_dev *d)
  285. {
  286. /*
  287. * SiS 5597 and 5598 chipsets require latency timer set to
  288. * at most 32 to avoid lockups.
  289. */
  290. DBG("PCI: Setting max latency to 32\n");
  291. pcibios_max_latency = 32;
  292. }
  293. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_UMC, PCI_DEVICE_ID_UMC_UM8886BF, pci_fixup_umc_ide);
  294. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, pci_fixup_ide_trash);
  295. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5597, pci_fixup_latency);
  296. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5598, pci_fixup_latency);
  297. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  298. /*
  299. * Called after each bus is probed, but before its children
  300. * are examined.
  301. */
  302. void __init pcibios_fixup_bus(struct pci_bus *bus)
  303. {
  304. #if 0
  305. printk("### PCIBIOS_FIXUP_BUS(%d)\n",bus->number);
  306. #endif
  307. pcibios_fixup_ghosts(bus);
  308. pci_read_bridge_bases(bus);
  309. if (bus->number == 0) {
  310. struct list_head *ln;
  311. struct pci_dev *dev;
  312. for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
  313. dev = pci_dev_b(ln);
  314. if (dev->devfn == 0) {
  315. dev->resource[0].start = 0;
  316. dev->resource[0].end = 0;
  317. }
  318. }
  319. }
  320. }
  321. /*
  322. * Initialization. Try all known PCI access methods. Note that we support
  323. * using both PCI BIOS and direct access: in such cases, we use I/O ports
  324. * to access config space, but we still keep BIOS order of cards to be
  325. * compatible with 2.0.X. This should go away some day.
  326. */
  327. int __init pcibios_init(void)
  328. {
  329. struct pci_ops *dir = NULL;
  330. if (!mb93090_mb00_detected)
  331. return -ENXIO;
  332. __reg_MB86943_sl_ctl |= MB86943_SL_CTL_DRCT_MASTER_SWAP | MB86943_SL_CTL_DRCT_SLAVE_SWAP;
  333. __reg_MB86943_ecs_base(1) = ((__region_CS2 + 0x01000000) >> 9) | 0x08000000;
  334. __reg_MB86943_ecs_base(2) = ((__region_CS2 + 0x00000000) >> 9) | 0x08000000;
  335. *(volatile uint32_t *) (__region_CS1 + 0x848) = 0xe0000000;
  336. *(volatile uint32_t *) (__region_CS1 + 0x8b8) = 0x00000000;
  337. __reg_MB86943_sl_pci_io_base = (__region_CS2 + 0x04000000) >> 9;
  338. __reg_MB86943_sl_pci_mem_base = (__region_CS2 + 0x08000000) >> 9;
  339. __reg_MB86943_pci_sl_io_base = __region_CS2 + 0x04000000;
  340. __reg_MB86943_pci_sl_mem_base = __region_CS2 + 0x08000000;
  341. mb();
  342. *(volatile unsigned long *)(__region_CS2+0x01300014) == 1;
  343. ioport_resource.start = (__reg_MB86943_sl_pci_io_base << 9) & 0xfffffc00;
  344. ioport_resource.end = (__reg_MB86943_sl_pci_io_range << 9) | 0x3ff;
  345. ioport_resource.end += ioport_resource.start;
  346. printk("PCI IO window: %08llx-%08llx\n",
  347. (unsigned long long) ioport_resource.start,
  348. (unsigned long long) ioport_resource.end);
  349. iomem_resource.start = (__reg_MB86943_sl_pci_mem_base << 9) & 0xfffffc00;
  350. /* Reserve somewhere to write to flush posted writes. */
  351. iomem_resource.start += 0x400;
  352. iomem_resource.end = (__reg_MB86943_sl_pci_mem_range << 9) | 0x3ff;
  353. iomem_resource.end += iomem_resource.start;
  354. printk("PCI MEM window: %08llx-%08llx\n",
  355. (unsigned long long) iomem_resource.start,
  356. (unsigned long long) iomem_resource.end);
  357. printk("PCI DMA memory: %08lx-%08lx\n",
  358. dma_coherent_mem_start, dma_coherent_mem_end);
  359. if (!pci_probe)
  360. return -ENXIO;
  361. dir = pci_check_direct();
  362. if (dir)
  363. pci_root_ops = dir;
  364. else {
  365. printk("PCI: No PCI bus detected\n");
  366. return -ENXIO;
  367. }
  368. printk("PCI: Probing PCI hardware\n");
  369. pci_root_bus = pci_scan_bus(0, pci_root_ops, NULL);
  370. pcibios_irq_init();
  371. pcibios_fixup_peer_bridges();
  372. pcibios_fixup_irqs();
  373. pcibios_resource_survey();
  374. return 0;
  375. }
  376. arch_initcall(pcibios_init);
  377. char * __init pcibios_setup(char *str)
  378. {
  379. if (!strcmp(str, "off")) {
  380. pci_probe = 0;
  381. return NULL;
  382. } else if (!strncmp(str, "lastbus=", 8)) {
  383. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  384. return NULL;
  385. }
  386. return str;
  387. }
  388. int pcibios_enable_device(struct pci_dev *dev, int mask)
  389. {
  390. int err;
  391. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  392. return err;
  393. pcibios_enable_irq(dev);
  394. return 0;
  395. }