pci.c 14 KB

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