pci.c 14 KB

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