pci.c 14 KB

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