pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <asm/io.h>
  21. #include "pci-asb2305.h"
  22. unsigned int pci_probe = 1;
  23. int pcibios_last_bus = -1;
  24. struct pci_bus *pci_root_bus;
  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. pci_ampci_read_config,
  205. 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 int __devinit is_valid_resource(struct pci_dev *dev, int idx)
  249. {
  250. unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  251. struct resource *devr = &dev->resource[idx], *busr;
  252. if (dev->bus) {
  253. pci_bus_for_each_resource(dev->bus, busr, i) {
  254. if (!busr || (busr->flags ^ devr->flags) & type_mask)
  255. continue;
  256. if (devr->start &&
  257. devr->start >= busr->start &&
  258. devr->end <= busr->end)
  259. return 1;
  260. }
  261. }
  262. return 0;
  263. }
  264. static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
  265. {
  266. struct pci_bus_region region;
  267. int i;
  268. int limit;
  269. if (dev->bus->number != 0)
  270. return;
  271. limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ?
  272. PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
  273. for (i = 0; i < limit; i++) {
  274. if (!dev->resource[i].flags)
  275. continue;
  276. if (is_valid_resource(dev, i))
  277. pci_claim_resource(dev, i);
  278. }
  279. }
  280. /*
  281. * Called after each bus is probed, but before its children
  282. * are examined.
  283. */
  284. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  285. {
  286. struct pci_dev *dev;
  287. if (bus->self) {
  288. pci_read_bridge_bases(bus);
  289. pcibios_fixup_device_resources(bus->self);
  290. }
  291. list_for_each_entry(dev, &bus->devices, bus_list)
  292. pcibios_fixup_device_resources(dev);
  293. }
  294. /*
  295. * Initialization. Try all known PCI access methods. Note that we support
  296. * using both PCI BIOS and direct access: in such cases, we use I/O ports
  297. * to access config space, but we still keep BIOS order of cards to be
  298. * compatible with 2.0.X. This should go away some day.
  299. */
  300. static int __init pcibios_init(void)
  301. {
  302. resource_size_t io_offset, mem_offset;
  303. LIST_HEAD(resources);
  304. ioport_resource.start = 0xA0000000;
  305. ioport_resource.end = 0xDFFFFFFF;
  306. iomem_resource.start = 0xA0000000;
  307. iomem_resource.end = 0xDFFFFFFF;
  308. if (insert_resource(&iomem_resource, &pci_iomem_resource) < 0)
  309. panic("Unable to insert PCI IOMEM resource\n");
  310. if (insert_resource(&ioport_resource, &pci_ioport_resource) < 0)
  311. panic("Unable to insert PCI IOPORT resource\n");
  312. if (!pci_probe)
  313. return 0;
  314. if (pci_check_direct() < 0) {
  315. printk(KERN_WARNING "PCI: No PCI bus detected\n");
  316. return 0;
  317. }
  318. printk(KERN_INFO "PCI: Probing PCI hardware [mempage %08x]\n",
  319. MEM_PAGING_REG);
  320. io_offset = pci_ioport_resource.start -
  321. (pci_ioport_resource.start & 0x00ffffff);
  322. mem_offset = pci_iomem_resource.start -
  323. ((pci_iomem_resource.start & 0x03ffffff) | MEM_PAGING_REG);
  324. pci_add_resource_offset(&resources, &pci_ioport_resource, io_offset);
  325. pci_add_resource_offset(&resources, &pci_iomem_resource, mem_offset);
  326. pci_root_bus = pci_scan_root_bus(NULL, 0, &pci_direct_ampci, NULL,
  327. &resources);
  328. pcibios_irq_init();
  329. pcibios_fixup_irqs();
  330. pcibios_resource_survey();
  331. return 0;
  332. }
  333. arch_initcall(pcibios_init);
  334. char *__init pcibios_setup(char *str)
  335. {
  336. if (!strcmp(str, "off")) {
  337. pci_probe = 0;
  338. return NULL;
  339. } else if (!strncmp(str, "lastbus=", 8)) {
  340. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  341. return NULL;
  342. }
  343. return str;
  344. }
  345. int pcibios_enable_device(struct pci_dev *dev, int mask)
  346. {
  347. int err;
  348. err = pci_enable_resources(dev, mask);
  349. if (err == 0)
  350. pcibios_enable_irq(dev);
  351. return err;
  352. }
  353. /*
  354. * disable the ethernet chipset
  355. */
  356. static void __init unit_disable_pcnet(struct pci_bus *bus, struct pci_ops *o)
  357. {
  358. u32 x;
  359. bus->number = 0;
  360. o->read (bus, PCI_DEVFN(2, 0), PCI_VENDOR_ID, 4, &x);
  361. o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
  362. x |= PCI_COMMAND_MASTER |
  363. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  364. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  365. o->write(bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, x);
  366. o->read (bus, PCI_DEVFN(2, 0), PCI_COMMAND, 2, &x);
  367. o->write(bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, 0x00030001);
  368. o->read (bus, PCI_DEVFN(2, 0), PCI_BASE_ADDRESS_0, 4, &x);
  369. #define RDP (*(volatile u32 *) 0xBE030010)
  370. #define RAP (*(volatile u32 *) 0xBE030014)
  371. #define __set_RAP(X) do { RAP = (X); x = RAP; } while (0)
  372. #define __set_RDP(X) do { RDP = (X); x = RDP; } while (0)
  373. #define __get_RDP() ({ RDP & 0xffff; })
  374. __set_RAP(0);
  375. __set_RDP(0x0004); /* CSR0 = STOP */
  376. __set_RAP(88); /* check CSR88 indicates an Am79C973 */
  377. BUG_ON(__get_RDP() != 0x5003);
  378. for (x = 0; x < 100; x++)
  379. asm volatile("nop");
  380. __set_RDP(0x0004); /* CSR0 = STOP */
  381. }
  382. /*
  383. * initialise the unit hardware
  384. */
  385. asmlinkage void __init unit_pci_init(void)
  386. {
  387. struct pci_bus bus; /* Fake bus and device */
  388. struct pci_ops *o = &pci_direct_ampci;
  389. u32 x;
  390. set_intr_level(XIRQ1, NUM2GxICR_LEVEL(CONFIG_PCI_IRQ_LEVEL));
  391. memset(&bus, 0, sizeof(bus));
  392. MEM_PAGING_REG = 0xE8000000;
  393. /* we need to set up the bridge _now_ or we won't be able to access the
  394. * PCI config registers
  395. */
  396. BRIDGEREGW(PCI_COMMAND) |=
  397. PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
  398. PCI_COMMAND_MEMORY | PCI_COMMAND_IO | PCI_COMMAND_MASTER;
  399. BRIDGEREGW(PCI_STATUS) = 0xF800;
  400. BRIDGEREGB(PCI_LATENCY_TIMER) = 0x10;
  401. BRIDGEREGL(PCI_BASE_ADDRESS_0) = 0x80000000;
  402. BRIDGEREGB(PCI_INTERRUPT_LINE) = 1;
  403. BRIDGEREGL(0x48) = 0x98000000; /* AMPCI base addr */
  404. BRIDGEREGB(0x41) = 0x00; /* secondary bus
  405. * number */
  406. BRIDGEREGB(0x42) = 0x01; /* subordinate bus
  407. * number */
  408. BRIDGEREGB(0x44) = 0x01;
  409. BRIDGEREGL(0x50) = 0x00000001;
  410. BRIDGEREGL(0x58) = 0x00001002;
  411. BRIDGEREGL(0x5C) = 0x00000011;
  412. /* we also need to set up the PCI-PCI bridge */
  413. bus.number = 0;
  414. /* IO: 0x00000000-0x00020000 */
  415. o->read (&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, &x);
  416. x |= PCI_COMMAND_MASTER |
  417. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  418. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  419. o->write(&bus, PCI_DEVFN(3, 0), PCI_COMMAND, 2, x);
  420. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
  421. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
  422. o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
  423. o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
  424. o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, 0x01);
  425. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE, 1, &x);
  426. o->write(&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, 0x00020000);
  427. o->read (&bus, PCI_DEVFN(3, 0), PCI_IO_BASE_UPPER16, 4, &x);
  428. o->write(&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, 0xEBB0EA00);
  429. o->read (&bus, PCI_DEVFN(3, 0), PCI_MEMORY_BASE, 4, &x);
  430. o->write(&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, 0xE9F0E800);
  431. o->read (&bus, PCI_DEVFN(3, 0), PCI_PREF_MEMORY_BASE, 4, &x);
  432. unit_disable_pcnet(&bus, o);
  433. }