bios32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * linux/arch/arm/kernel/bios32.c
  3. *
  4. * PCI bios-type initialisation for PCI machines
  5. *
  6. * Bits taken from various places.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <asm/mach-types.h>
  15. #include <asm/mach/pci.h>
  16. static int debug_pci;
  17. /*
  18. * We can't use pci_find_device() here since we are
  19. * called from interrupt context.
  20. */
  21. static void pcibios_bus_report_status(struct pci_bus *bus, u_int status_mask, int warn)
  22. {
  23. struct pci_dev *dev;
  24. list_for_each_entry(dev, &bus->devices, bus_list) {
  25. u16 status;
  26. /*
  27. * ignore host bridge - we handle
  28. * that separately
  29. */
  30. if (dev->bus->number == 0 && dev->devfn == 0)
  31. continue;
  32. pci_read_config_word(dev, PCI_STATUS, &status);
  33. if (status == 0xffff)
  34. continue;
  35. if ((status & status_mask) == 0)
  36. continue;
  37. /* clear the status errors */
  38. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  39. if (warn)
  40. printk("(%s: %04X) ", pci_name(dev), status);
  41. }
  42. list_for_each_entry(dev, &bus->devices, bus_list)
  43. if (dev->subordinate)
  44. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  45. }
  46. void pcibios_report_status(u_int status_mask, int warn)
  47. {
  48. struct list_head *l;
  49. list_for_each(l, &pci_root_buses) {
  50. struct pci_bus *bus = pci_bus_b(l);
  51. pcibios_bus_report_status(bus, status_mask, warn);
  52. }
  53. }
  54. /*
  55. * We don't use this to fix the device, but initialisation of it.
  56. * It's not the correct use for this, but it works.
  57. * Note that the arbiter/ISA bridge appears to be buggy, specifically in
  58. * the following area:
  59. * 1. park on CPU
  60. * 2. ISA bridge ping-pong
  61. * 3. ISA bridge master handling of target RETRY
  62. *
  63. * Bug 3 is responsible for the sound DMA grinding to a halt. We now
  64. * live with bug 2.
  65. */
  66. static void __devinit pci_fixup_83c553(struct pci_dev *dev)
  67. {
  68. /*
  69. * Set memory region to start at address 0, and enable IO
  70. */
  71. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_SPACE_MEMORY);
  72. pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_IO);
  73. dev->resource[0].end -= dev->resource[0].start;
  74. dev->resource[0].start = 0;
  75. /*
  76. * All memory requests from ISA to be channelled to PCI
  77. */
  78. pci_write_config_byte(dev, 0x48, 0xff);
  79. /*
  80. * Enable ping-pong on bus master to ISA bridge transactions.
  81. * This improves the sound DMA substantially. The fixed
  82. * priority arbiter also helps (see below).
  83. */
  84. pci_write_config_byte(dev, 0x42, 0x01);
  85. /*
  86. * Enable PCI retry
  87. */
  88. pci_write_config_byte(dev, 0x40, 0x22);
  89. /*
  90. * We used to set the arbiter to "park on last master" (bit
  91. * 1 set), but unfortunately the CyberPro does not park the
  92. * bus. We must therefore park on CPU. Unfortunately, this
  93. * may trigger yet another bug in the 553.
  94. */
  95. pci_write_config_byte(dev, 0x83, 0x02);
  96. /*
  97. * Make the ISA DMA request lowest priority, and disable
  98. * rotating priorities completely.
  99. */
  100. pci_write_config_byte(dev, 0x80, 0x11);
  101. pci_write_config_byte(dev, 0x81, 0x00);
  102. /*
  103. * Route INTA input to IRQ 11, and set IRQ11 to be level
  104. * sensitive.
  105. */
  106. pci_write_config_word(dev, 0x44, 0xb000);
  107. outb(0x08, 0x4d1);
  108. }
  109. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_83C553, pci_fixup_83c553);
  110. static void __devinit pci_fixup_unassign(struct pci_dev *dev)
  111. {
  112. dev->resource[0].end -= dev->resource[0].start;
  113. dev->resource[0].start = 0;
  114. }
  115. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_89C940F, pci_fixup_unassign);
  116. /*
  117. * Prevent the PCI layer from seeing the resources allocated to this device
  118. * if it is the host bridge by marking it as such. These resources are of
  119. * no consequence to the PCI layer (they are handled elsewhere).
  120. */
  121. static void __devinit pci_fixup_dec21285(struct pci_dev *dev)
  122. {
  123. int i;
  124. if (dev->devfn == 0) {
  125. dev->class &= 0xff;
  126. dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
  127. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  128. dev->resource[i].start = 0;
  129. dev->resource[i].end = 0;
  130. dev->resource[i].flags = 0;
  131. }
  132. }
  133. }
  134. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, pci_fixup_dec21285);
  135. /*
  136. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  137. */
  138. static void __devinit pci_fixup_ide_bases(struct pci_dev *dev)
  139. {
  140. struct resource *r;
  141. int i;
  142. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  143. return;
  144. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  145. r = dev->resource + i;
  146. if ((r->start & ~0x80) == 0x374) {
  147. r->start |= 2;
  148. r->end = r->start;
  149. }
  150. }
  151. }
  152. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  153. /*
  154. * Put the DEC21142 to sleep
  155. */
  156. static void __devinit pci_fixup_dec21142(struct pci_dev *dev)
  157. {
  158. pci_write_config_dword(dev, 0x40, 0x80000000);
  159. }
  160. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142, pci_fixup_dec21142);
  161. /*
  162. * The CY82C693 needs some rather major fixups to ensure that it does
  163. * the right thing. Idea from the Alpha people, with a few additions.
  164. *
  165. * We ensure that the IDE base registers are set to 1f0/3f4 for the
  166. * primary bus, and 170/374 for the secondary bus. Also, hide them
  167. * from the PCI subsystem view as well so we won't try to perform
  168. * our own auto-configuration on them.
  169. *
  170. * In addition, we ensure that the PCI IDE interrupts are routed to
  171. * IRQ 14 and IRQ 15 respectively.
  172. *
  173. * The above gets us to a point where the IDE on this device is
  174. * functional. However, The CY82C693U _does not work_ in bus
  175. * master mode without locking the PCI bus solid.
  176. */
  177. static void __devinit pci_fixup_cy82c693(struct pci_dev *dev)
  178. {
  179. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  180. u32 base0, base1;
  181. if (dev->class & 0x80) { /* primary */
  182. base0 = 0x1f0;
  183. base1 = 0x3f4;
  184. } else { /* secondary */
  185. base0 = 0x170;
  186. base1 = 0x374;
  187. }
  188. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0,
  189. base0 | PCI_BASE_ADDRESS_SPACE_IO);
  190. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1,
  191. base1 | PCI_BASE_ADDRESS_SPACE_IO);
  192. dev->resource[0].start = 0;
  193. dev->resource[0].end = 0;
  194. dev->resource[0].flags = 0;
  195. dev->resource[1].start = 0;
  196. dev->resource[1].end = 0;
  197. dev->resource[1].flags = 0;
  198. } else if (PCI_FUNC(dev->devfn) == 0) {
  199. /*
  200. * Setup IDE IRQ routing.
  201. */
  202. pci_write_config_byte(dev, 0x4b, 14);
  203. pci_write_config_byte(dev, 0x4c, 15);
  204. /*
  205. * Disable FREQACK handshake, enable USB.
  206. */
  207. pci_write_config_byte(dev, 0x4d, 0x41);
  208. /*
  209. * Enable PCI retry, and PCI post-write buffer.
  210. */
  211. pci_write_config_byte(dev, 0x44, 0x17);
  212. /*
  213. * Enable ISA master and DMA post write buffering.
  214. */
  215. pci_write_config_byte(dev, 0x45, 0x03);
  216. }
  217. }
  218. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, pci_fixup_cy82c693);
  219. static void __init pci_fixup_it8152(struct pci_dev *dev)
  220. {
  221. int i;
  222. /* fixup for ITE 8152 devices */
  223. /* FIXME: add defines for class 0x68000 and 0x80103 */
  224. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST ||
  225. dev->class == 0x68000 ||
  226. dev->class == 0x80103) {
  227. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  228. dev->resource[i].start = 0;
  229. dev->resource[i].end = 0;
  230. dev->resource[i].flags = 0;
  231. }
  232. }
  233. }
  234. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8152, pci_fixup_it8152);
  235. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  236. {
  237. if (debug_pci)
  238. printk("PCI: Assigning IRQ %02d to %s\n", irq, pci_name(dev));
  239. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  240. }
  241. /*
  242. * If the bus contains any of these devices, then we must not turn on
  243. * parity checking of any kind. Currently this is CyberPro 20x0 only.
  244. */
  245. static inline int pdev_bad_for_parity(struct pci_dev *dev)
  246. {
  247. return ((dev->vendor == PCI_VENDOR_ID_INTERG &&
  248. (dev->device == PCI_DEVICE_ID_INTERG_2000 ||
  249. dev->device == PCI_DEVICE_ID_INTERG_2010)) ||
  250. (dev->vendor == PCI_VENDOR_ID_ITE &&
  251. dev->device == PCI_DEVICE_ID_ITE_8152));
  252. }
  253. /*
  254. * pcibios_fixup_bus - Called after each bus is probed,
  255. * but before its children are examined.
  256. */
  257. void pcibios_fixup_bus(struct pci_bus *bus)
  258. {
  259. struct pci_sys_data *root = bus->sysdata;
  260. struct pci_dev *dev;
  261. u16 features = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_FAST_BACK;
  262. /*
  263. * Walk the devices on this bus, working out what we can
  264. * and can't support.
  265. */
  266. list_for_each_entry(dev, &bus->devices, bus_list) {
  267. u16 status;
  268. pci_read_config_word(dev, PCI_STATUS, &status);
  269. /*
  270. * If any device on this bus does not support fast back
  271. * to back transfers, then the bus as a whole is not able
  272. * to support them. Having fast back to back transfers
  273. * on saves us one PCI cycle per transaction.
  274. */
  275. if (!(status & PCI_STATUS_FAST_BACK))
  276. features &= ~PCI_COMMAND_FAST_BACK;
  277. if (pdev_bad_for_parity(dev))
  278. features &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  279. switch (dev->class >> 8) {
  280. case PCI_CLASS_BRIDGE_PCI:
  281. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &status);
  282. status |= PCI_BRIDGE_CTL_PARITY|PCI_BRIDGE_CTL_MASTER_ABORT;
  283. status &= ~(PCI_BRIDGE_CTL_BUS_RESET|PCI_BRIDGE_CTL_FAST_BACK);
  284. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, status);
  285. break;
  286. case PCI_CLASS_BRIDGE_CARDBUS:
  287. pci_read_config_word(dev, PCI_CB_BRIDGE_CONTROL, &status);
  288. status |= PCI_CB_BRIDGE_CTL_PARITY|PCI_CB_BRIDGE_CTL_MASTER_ABORT;
  289. pci_write_config_word(dev, PCI_CB_BRIDGE_CONTROL, status);
  290. break;
  291. }
  292. }
  293. /*
  294. * Now walk the devices again, this time setting them up.
  295. */
  296. list_for_each_entry(dev, &bus->devices, bus_list) {
  297. u16 cmd;
  298. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  299. cmd |= features;
  300. pci_write_config_word(dev, PCI_COMMAND, cmd);
  301. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  302. L1_CACHE_BYTES >> 2);
  303. }
  304. /*
  305. * Propagate the flags to the PCI bridge.
  306. */
  307. if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  308. if (features & PCI_COMMAND_FAST_BACK)
  309. bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
  310. if (features & PCI_COMMAND_PARITY)
  311. bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
  312. }
  313. /*
  314. * Report what we did for this bus
  315. */
  316. printk(KERN_INFO "PCI: bus%d: Fast back to back transfers %sabled\n",
  317. bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
  318. }
  319. #ifdef CONFIG_HOTPLUG
  320. EXPORT_SYMBOL(pcibios_fixup_bus);
  321. #endif
  322. /*
  323. * Swizzle the device pin each time we cross a bridge.
  324. * This might update pin and returns the slot number.
  325. */
  326. static u8 __devinit pcibios_swizzle(struct pci_dev *dev, u8 *pin)
  327. {
  328. struct pci_sys_data *sys = dev->sysdata;
  329. int slot = 0, oldpin = *pin;
  330. if (sys->swizzle)
  331. slot = sys->swizzle(dev, pin);
  332. if (debug_pci)
  333. printk("PCI: %s swizzling pin %d => pin %d slot %d\n",
  334. pci_name(dev), oldpin, *pin, slot);
  335. return slot;
  336. }
  337. /*
  338. * Map a slot/pin to an IRQ.
  339. */
  340. static int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  341. {
  342. struct pci_sys_data *sys = dev->sysdata;
  343. int irq = -1;
  344. if (sys->map_irq)
  345. irq = sys->map_irq(dev, slot, pin);
  346. if (debug_pci)
  347. printk("PCI: %s mapping slot %d pin %d => irq %d\n",
  348. pci_name(dev), slot, pin, irq);
  349. return irq;
  350. }
  351. static void __init pcibios_init_hw(struct hw_pci *hw)
  352. {
  353. struct pci_sys_data *sys = NULL;
  354. int ret;
  355. int nr, busnr;
  356. for (nr = busnr = 0; nr < hw->nr_controllers; nr++) {
  357. sys = kzalloc(sizeof(struct pci_sys_data), GFP_KERNEL);
  358. if (!sys)
  359. panic("PCI: unable to allocate sys data!");
  360. #ifdef CONFIG_PCI_DOMAINS
  361. sys->domain = hw->domain;
  362. #endif
  363. sys->hw = hw;
  364. sys->busnr = busnr;
  365. sys->swizzle = hw->swizzle;
  366. sys->map_irq = hw->map_irq;
  367. INIT_LIST_HEAD(&sys->resources);
  368. ret = hw->setup(nr, sys);
  369. if (ret > 0) {
  370. if (list_empty(&sys->resources)) {
  371. pci_add_resource_offset(&sys->resources,
  372. &ioport_resource, sys->io_offset);
  373. pci_add_resource_offset(&sys->resources,
  374. &iomem_resource, sys->mem_offset);
  375. }
  376. sys->bus = hw->scan(nr, sys);
  377. if (!sys->bus)
  378. panic("PCI: unable to scan bus!");
  379. busnr = sys->bus->subordinate + 1;
  380. list_add(&sys->node, &hw->buses);
  381. } else {
  382. kfree(sys);
  383. if (ret < 0)
  384. break;
  385. }
  386. }
  387. }
  388. void __init pci_common_init(struct hw_pci *hw)
  389. {
  390. struct pci_sys_data *sys;
  391. INIT_LIST_HEAD(&hw->buses);
  392. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  393. if (hw->preinit)
  394. hw->preinit();
  395. pcibios_init_hw(hw);
  396. if (hw->postinit)
  397. hw->postinit();
  398. pci_fixup_irqs(pcibios_swizzle, pcibios_map_irq);
  399. list_for_each_entry(sys, &hw->buses, node) {
  400. struct pci_bus *bus = sys->bus;
  401. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  402. /*
  403. * Size the bridge windows.
  404. */
  405. pci_bus_size_bridges(bus);
  406. /*
  407. * Assign resources.
  408. */
  409. pci_bus_assign_resources(bus);
  410. /*
  411. * Enable bridges
  412. */
  413. pci_enable_bridges(bus);
  414. }
  415. /*
  416. * Tell drivers about devices found.
  417. */
  418. pci_bus_add_devices(bus);
  419. }
  420. }
  421. #ifndef CONFIG_PCI_HOST_ITE8152
  422. void pcibios_set_master(struct pci_dev *dev)
  423. {
  424. /* No special bus mastering setup handling */
  425. }
  426. #endif
  427. char * __init pcibios_setup(char *str)
  428. {
  429. if (!strcmp(str, "debug")) {
  430. debug_pci = 1;
  431. return NULL;
  432. } else if (!strcmp(str, "firmware")) {
  433. pci_add_flags(PCI_PROBE_ONLY);
  434. return NULL;
  435. }
  436. return str;
  437. }
  438. /*
  439. * From arch/i386/kernel/pci-i386.c:
  440. *
  441. * We need to avoid collisions with `mirrored' VGA ports
  442. * and other strange ISA hardware, so we always want the
  443. * addresses to be allocated in the 0x000-0x0ff region
  444. * modulo 0x400.
  445. *
  446. * Why? Because some silly external IO cards only decode
  447. * the low 10 bits of the IO address. The 0x00-0xff region
  448. * is reserved for motherboard devices that decode all 16
  449. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  450. * but we want to try to avoid allocating at 0x2900-0x2bff
  451. * which might be mirrored at 0x0100-0x03ff..
  452. */
  453. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  454. resource_size_t size, resource_size_t align)
  455. {
  456. resource_size_t start = res->start;
  457. if (res->flags & IORESOURCE_IO && start & 0x300)
  458. start = (start + 0x3ff) & ~0x3ff;
  459. start = (start + align - 1) & ~(align - 1);
  460. return start;
  461. }
  462. /**
  463. * pcibios_enable_device - Enable I/O and memory.
  464. * @dev: PCI device to be enabled
  465. */
  466. int pcibios_enable_device(struct pci_dev *dev, int mask)
  467. {
  468. u16 cmd, old_cmd;
  469. int idx;
  470. struct resource *r;
  471. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  472. old_cmd = cmd;
  473. for (idx = 0; idx < 6; idx++) {
  474. /* Only set up the requested stuff */
  475. if (!(mask & (1 << idx)))
  476. continue;
  477. r = dev->resource + idx;
  478. if (!r->start && r->end) {
  479. printk(KERN_ERR "PCI: Device %s not available because"
  480. " of resource collisions\n", pci_name(dev));
  481. return -EINVAL;
  482. }
  483. if (r->flags & IORESOURCE_IO)
  484. cmd |= PCI_COMMAND_IO;
  485. if (r->flags & IORESOURCE_MEM)
  486. cmd |= PCI_COMMAND_MEMORY;
  487. }
  488. /*
  489. * Bridges (eg, cardbus bridges) need to be fully enabled
  490. */
  491. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
  492. cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  493. if (cmd != old_cmd) {
  494. printk("PCI: enabling device %s (%04x -> %04x)\n",
  495. pci_name(dev), old_cmd, cmd);
  496. pci_write_config_word(dev, PCI_COMMAND, cmd);
  497. }
  498. return 0;
  499. }
  500. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  501. enum pci_mmap_state mmap_state, int write_combine)
  502. {
  503. struct pci_sys_data *root = dev->sysdata;
  504. unsigned long phys;
  505. if (mmap_state == pci_mmap_io) {
  506. return -EINVAL;
  507. } else {
  508. phys = vma->vm_pgoff + (root->mem_offset >> PAGE_SHIFT);
  509. }
  510. /*
  511. * Mark this as IO
  512. */
  513. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  514. if (remap_pfn_range(vma, vma->vm_start, phys,
  515. vma->vm_end - vma->vm_start,
  516. vma->vm_page_prot))
  517. return -EAGAIN;
  518. return 0;
  519. }