pci-vdk.c 12 KB

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