pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* 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>
  20. #include <linux/irq.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include "pci-asb2305.h"
  24. unsigned int pci_probe = 1;
  25. struct pci_ops *pci_root_ops;
  26. /*
  27. * The accessible PCI window does not cover the entire CPU address space, but
  28. * there are devices we want to access outside of that window, so we need to
  29. * insert specific PCI bus resources instead of using the platform-level bus
  30. * resources directly for the PCI root bus.
  31. *
  32. * These are configured and inserted by pcibios_init().
  33. */
  34. static struct resource pci_ioport_resource = {
  35. .name = "PCI IO",
  36. .start = 0xbe000000,
  37. .end = 0xbe03ffff,
  38. .flags = IORESOURCE_IO,
  39. };
  40. static struct resource pci_iomem_resource = {
  41. .name = "PCI mem",
  42. .start = 0xb8000000,
  43. .end = 0xbbffffff,
  44. .flags = IORESOURCE_MEM,
  45. };
  46. /*
  47. * Functions for accessing PCI configuration space
  48. */
  49. #define CONFIG_CMD(bus, devfn, where) \
  50. (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  51. #define MEM_PAGING_REG (*(volatile __u32 *) 0xBFFFFFF4)
  52. #define CONFIG_ADDRESS (*(volatile __u32 *) 0xBFFFFFF8)
  53. #define CONFIG_DATAL(X) (*(volatile __u32 *) 0xBFFFFFFC)
  54. #define CONFIG_DATAW(X) (*(volatile __u16 *) (0xBFFFFFFC + ((X) & 2)))
  55. #define CONFIG_DATAB(X) (*(volatile __u8 *) (0xBFFFFFFC + ((X) & 3)))
  56. #define BRIDGEREGB(X) (*(volatile __u8 *) (0xBE040000 + (X)))
  57. #define BRIDGEREGW(X) (*(volatile __u16 *) (0xBE040000 + (X)))
  58. #define BRIDGEREGL(X) (*(volatile __u32 *) (0xBE040000 + (X)))
  59. static inline int __query(const struct pci_bus *bus, unsigned int devfn)
  60. {
  61. #if 0
  62. return bus->number == 0 && (devfn == PCI_DEVFN(0, 0));
  63. return bus->number == 1;
  64. return bus->number == 0 &&
  65. (devfn == PCI_DEVFN(2, 0) || devfn == PCI_DEVFN(3, 0));
  66. #endif
  67. return 1;
  68. }
  69. /*
  70. *
  71. */
  72. static int pci_ampci_read_config_byte(struct pci_bus *bus, unsigned int devfn,
  73. int where, u32 *_value)
  74. {
  75. u32 rawval, value;
  76. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  77. value = BRIDGEREGB(where);
  78. __pcbdebug("=> %02hx", &BRIDGEREGL(where), value);
  79. } else {
  80. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  81. rawval = CONFIG_ADDRESS;
  82. value = CONFIG_DATAB(where);
  83. if (__query(bus, devfn))
  84. __pcidebug("=> %02hx", bus, devfn, where, value);
  85. }
  86. *_value = value;
  87. return PCIBIOS_SUCCESSFUL;
  88. }
  89. static int pci_ampci_read_config_word(struct pci_bus *bus, unsigned int devfn,
  90. int where, u32 *_value)
  91. {
  92. u32 rawval, value;
  93. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  94. value = BRIDGEREGW(where);
  95. __pcbdebug("=> %04hx", &BRIDGEREGL(where), value);
  96. } else {
  97. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  98. rawval = CONFIG_ADDRESS;
  99. value = CONFIG_DATAW(where);
  100. if (__query(bus, devfn))
  101. __pcidebug("=> %04hx", bus, devfn, where, value);
  102. }
  103. *_value = value;
  104. return PCIBIOS_SUCCESSFUL;
  105. }
  106. static int pci_ampci_read_config_dword(struct pci_bus *bus, unsigned int devfn,
  107. int where, u32 *_value)
  108. {
  109. u32 rawval, value;
  110. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  111. value = BRIDGEREGL(where);
  112. __pcbdebug("=> %08x", &BRIDGEREGL(where), value);
  113. } else {
  114. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  115. rawval = CONFIG_ADDRESS;
  116. value = CONFIG_DATAL(where);
  117. if (__query(bus, devfn))
  118. __pcidebug("=> %08x", bus, devfn, where, value);
  119. }
  120. *_value = value;
  121. return PCIBIOS_SUCCESSFUL;
  122. }
  123. static int pci_ampci_write_config_byte(struct pci_bus *bus, unsigned int devfn,
  124. int where, u8 value)
  125. {
  126. u32 rawval;
  127. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  128. __pcbdebug("<= %02x", &BRIDGEREGB(where), value);
  129. BRIDGEREGB(where) = value;
  130. } else {
  131. if (bus->number == 0 &&
  132. (devfn == PCI_DEVFN(2, 0) || devfn == PCI_DEVFN(3, 0))
  133. )
  134. __pcidebug("<= %02x", bus, devfn, where, value);
  135. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  136. rawval = CONFIG_ADDRESS;
  137. CONFIG_DATAB(where) = value;
  138. }
  139. return PCIBIOS_SUCCESSFUL;
  140. }
  141. static int pci_ampci_write_config_word(struct pci_bus *bus, unsigned int devfn,
  142. int where, u16 value)
  143. {
  144. u32 rawval;
  145. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  146. __pcbdebug("<= %04hx", &BRIDGEREGW(where), value);
  147. BRIDGEREGW(where) = value;
  148. } else {
  149. if (__query(bus, devfn))
  150. __pcidebug("<= %04hx", bus, devfn, where, value);
  151. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  152. rawval = CONFIG_ADDRESS;
  153. CONFIG_DATAW(where) = value;
  154. }
  155. return PCIBIOS_SUCCESSFUL;
  156. }
  157. static int pci_ampci_write_config_dword(struct pci_bus *bus, unsigned int devfn,
  158. int where, u32 value)
  159. {
  160. u32 rawval;
  161. if (bus->number == 0 && devfn == PCI_DEVFN(0, 0)) {
  162. __pcbdebug("<= %08x", &BRIDGEREGL(where), value);
  163. BRIDGEREGL(where) = value;
  164. } else {
  165. if (__query(bus, devfn))
  166. __pcidebug("<= %08x", bus, devfn, where, value);
  167. CONFIG_ADDRESS = CONFIG_CMD(bus, devfn, where);
  168. rawval = CONFIG_ADDRESS;
  169. CONFIG_DATAL(where) = value;
  170. }
  171. return PCIBIOS_SUCCESSFUL;
  172. }
  173. static int pci_ampci_read_config(struct pci_bus *bus, unsigned int devfn,
  174. int where, int size, u32 *val)
  175. {
  176. switch (size) {
  177. case 1:
  178. return pci_ampci_read_config_byte(bus, devfn, where, val);
  179. case 2:
  180. return pci_ampci_read_config_word(bus, devfn, where, val);
  181. case 4:
  182. return pci_ampci_read_config_dword(bus, devfn, where, val);
  183. default:
  184. BUG();
  185. return -EOPNOTSUPP;
  186. }
  187. }
  188. static int pci_ampci_write_config(struct pci_bus *bus, unsigned int devfn,
  189. int where, int size, u32 val)
  190. {
  191. switch (size) {
  192. case 1:
  193. return pci_ampci_write_config_byte(bus, devfn, where, val);
  194. case 2:
  195. return pci_ampci_write_config_word(bus, devfn, where, val);
  196. case 4:
  197. return pci_ampci_write_config_dword(bus, devfn, where, val);
  198. default:
  199. BUG();
  200. return -EOPNOTSUPP;
  201. }
  202. }
  203. static struct pci_ops pci_direct_ampci = {
  204. .read = pci_ampci_read_config,
  205. .write = pci_ampci_write_config,
  206. };
  207. /*
  208. * Before we decide to use direct hardware access mechanisms, we try to do some
  209. * trivial checks to ensure it at least _seems_ to be working -- we just test
  210. * whether bus 00 contains a host bridge (this is similar to checking
  211. * techniques used in XFree86, but ours should be more reliable since we
  212. * attempt to make use of direct access hints provided by the PCI BIOS).
  213. *
  214. * This should be close to trivial, but it isn't, because there are buggy
  215. * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  216. */
  217. static int __init pci_sanity_check(struct pci_ops *o)
  218. {
  219. struct pci_bus bus; /* Fake bus and device */
  220. u32 x;
  221. bus.number = 0;
  222. if ((!o->read(&bus, 0, PCI_CLASS_DEVICE, 2, &x) &&
  223. (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)) ||
  224. (!o->read(&bus, 0, PCI_VENDOR_ID, 2, &x) &&
  225. (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)))
  226. return 1;
  227. printk(KERN_ERR "PCI: Sanity check failed\n");
  228. return 0;
  229. }
  230. static int __init pci_check_direct(void)
  231. {
  232. unsigned long flags;
  233. local_irq_save(flags);
  234. /*
  235. * Check if access works.
  236. */
  237. if (pci_sanity_check(&pci_direct_ampci)) {
  238. local_irq_restore(flags);
  239. printk(KERN_INFO "PCI: Using configuration ampci\n");
  240. request_mem_region(0xBE040000, 256, "AMPCI bridge");
  241. request_mem_region(0xBFFFFFF4, 12, "PCI ampci");
  242. request_mem_region(0xBC000000, 32 * 1024 * 1024, "PCI SRAM");
  243. return 0;
  244. }
  245. local_irq_restore(flags);
  246. return -ENODEV;
  247. }
  248. static void pcibios_fixup_device_resources(struct pci_dev *dev)
  249. {
  250. int idx;
  251. if (!dev->bus)
  252. return;
  253. for (idx = 0; idx < PCI_BRIDGE_RESOURCES; idx++) {
  254. struct resource *r = &dev->resource[idx];
  255. if (!r->flags || r->parent || !r->start)
  256. continue;
  257. pci_claim_resource(dev, idx);
  258. }
  259. }
  260. static void pcibios_fixup_bridge_resources(struct pci_dev *dev)
  261. {
  262. int idx;
  263. if (!dev->bus)
  264. return;
  265. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  266. struct resource *r = &dev->resource[idx];
  267. if (!r->flags || r->parent || !r->start)
  268. continue;
  269. pci_claim_bridge_resource(dev, idx);
  270. }
  271. }
  272. /*
  273. * Called after each bus is probed, but before its children
  274. * are examined.
  275. */
  276. void pcibios_fixup_bus(struct pci_bus *bus)
  277. {
  278. struct pci_dev *dev;
  279. if (bus->self) {
  280. pci_read_bridge_bases(bus);
  281. pcibios_fixup_bridge_resources(bus->self);
  282. }
  283. list_for_each_entry(dev, &bus->devices, bus_list)
  284. pcibios_fixup_device_resources(dev);
  285. }
  286. /*
  287. * Initialization. Try all known PCI access methods. Note that we support
  288. * using both PCI BIOS and direct access: in such cases, we use I/O ports
  289. * to access config space, but we still keep BIOS order of cards to be
  290. * compatible with 2.0.X. This should go away some day.
  291. */
  292. static int __init pcibios_init(void)
  293. {
  294. resource_size_t io_offset, mem_offset;
  295. LIST_HEAD(resources);
  296. struct pci_bus *bus;
  297. ioport_resource.start = 0xA0000000;
  298. ioport_resource.end = 0xDFFFFFFF;
  299. iomem_resource.start = 0xA0000000;
  300. iomem_resource.end = 0xDFFFFFFF;
  301. if (insert_resource(&iomem_resource, &pci_iomem_resource) < 0)
  302. panic("Unable to insert PCI IOMEM resource\n");
  303. if (insert_resource(&ioport_resource, &pci_ioport_resource) < 0)
  304. panic("Unable to insert PCI IOPORT resource\n");
  305. if (!pci_probe)
  306. return 0;
  307. if (pci_check_direct() < 0) {
  308. printk(KERN_WARNING "PCI: No PCI bus detected\n");
  309. return 0;
  310. }
  311. printk(KERN_INFO "PCI: Probing PCI hardware [mempage %08x]\n",
  312. MEM_PAGING_REG);
  313. io_offset = pci_ioport_resource.start -
  314. (pci_ioport_resource.start & 0x00ffffff);
  315. mem_offset = pci_iomem_resource.start -
  316. ((pci_iomem_resource.start & 0x03ffffff) | MEM_PAGING_REG);
  317. pci_add_resource_offset(&resources, &pci_ioport_resource, io_offset);
  318. pci_add_resource_offset(&resources, &pci_iomem_resource, mem_offset);
  319. bus = pci_scan_root_bus(NULL, 0, &pci_direct_ampci, NULL, &resources);
  320. if (!bus)
  321. return 0;
  322. pcibios_irq_init();
  323. pcibios_fixup_irqs();
  324. pcibios_resource_survey();
  325. pci_bus_add_devices(bus);
  326. return 0;
  327. }
  328. arch_initcall(pcibios_init);
  329. char *__init pcibios_setup(char *str)
  330. {
  331. if (!strcmp(str, "off")) {
  332. pci_probe = 0;
  333. return NULL;
  334. }
  335. return str;
  336. }
  337. int pcibios_enable_device(struct pci_dev *dev, int mask)
  338. {
  339. int err;
  340. err = pci_enable_resources(dev, mask);
  341. if (err == 0)
  342. pcibios_enable_irq(dev);
  343. return err;
  344. }
  345. /*
  346. * disable the ethernet chipset
  347. */
  348. static void __init unit_disable_pcnet(struct pci_bus *bus, struct pci_ops *o)
  349. {
  350. u32 x;
  351. bus->number = 0;
  352. o->read (bus, PCI_DEVFN(2, 0), PCI_VENDOR_ID, 4, &x);
  353. o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
  354. x |= PCI_COMMAND_MASTER |
  355. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  356. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  357. o->write(bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, x);
  358. o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
  359. o->write(bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, 0x00030001);
  360. o->read (bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, &x);
  361. #define RDP (*(volatile u32 *) 0xBE030010)
  362. #define RAP (*(volatile u32 *) 0xBE030014)
  363. #define __set_RAP(X) do { RAP = (X); x = RAP; } while (0)
  364. #define __set_RDP(X) do { RDP = (X); x = RDP; } while (0)
  365. #define __get_RDP() ({ RDP & 0xffff; })
  366. __set_RAP(0);
  367. __set_RDP(0x0004); /* CSR0 = STOP */
  368. __set_RAP(88); /* check CSR88 indicates an Am79C973 */
  369. BUG_ON(__get_RDP() != 0x5003);
  370. for (x = 0; x < 100; x++)
  371. asm volatile("nop");
  372. __set_RDP(0x0004); /* CSR0 = STOP */
  373. }
  374. /*
  375. * initialise the unit hardware
  376. */
  377. asmlinkage void __init unit_pci_init(void)
  378. {
  379. struct pci_bus bus; /* Fake bus and device */
  380. struct pci_ops *o = &pci_direct_ampci;
  381. u32 x;
  382. set_intr_level(XIRQ1, NUM2GxICR_LEVEL(CONFIG_PCI_IRQ_LEVEL));
  383. memset(&bus, 0, sizeof(bus));
  384. MEM_PAGING_REG = 0xE8000000;
  385. /* we need to set up the bridge _now_ or we won't be able to access the
  386. * PCI config registers
  387. */
  388. BRIDGEREGW(PCI_COMMAND) |=
  389. PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
  390. PCI_COMMAND_MEMORY | PCI_COMMAND_IO | PCI_COMMAND_MASTER;
  391. BRIDGEREGW(PCI_STATUS) = 0xF800;
  392. BRIDGEREGB(PCI_LATENCY_TIMER) = 0x10;
  393. BRIDGEREGL(PCI_BASE_ADDRESS_0) = 0x80000000;
  394. BRIDGEREGB(PCI_INTERRUPT_LINE) = 1;
  395. BRIDGEREGL(0x48) = 0x98000000; /* AMPCI base addr */
  396. BRIDGEREGB(0x41) = 0x00; /* secondary bus
  397. * number */
  398. BRIDGEREGB(0x42) = 0x01; /* subordinate bus
  399. * number */
  400. BRIDGEREGB(0x44) = 0x01;
  401. BRIDGEREGL(0x50) = 0x00000001;
  402. BRIDGEREGL(0x58) = 0x00001002;
  403. BRIDGEREGL(0x5C) = 0x00000011;
  404. /* we also need to set up the PCI-PCI bridge */
  405. bus.number = 0;
  406. /* IO: 0x00000000-0x00020000 */
  407. o->read (&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, &x);
  408. x |= PCI_COMMAND_MASTER |
  409. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  410. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  411. o->write(&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, x);
  412. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
  413. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
  414. o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
  415. o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
  416. o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, 0x01);
  417. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
  418. o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, 0x00020000);
  419. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
  420. o->write(&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, 0xEBB0EA00);
  421. o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
  422. o->write(&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, 0xE9F0E800);
  423. o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
  424. unit_disable_pcnet(&bus, o);
  425. }