pci.c 15 KB

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