bios32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/arm/kernel/bios32.c
  4. *
  5. * PCI bios-type initialisation for PCI machines
  6. *
  7. * Bits taken from various places.
  8. */
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <asm/mach-types.h>
  16. #include <asm/mach/map.h>
  17. #include <asm/mach/pci.h>
  18. static int debug_pci;
  19. /*
  20. * We can't use pci_get_device() here since we are
  21. * called from interrupt context.
  22. */
  23. static void pcibios_bus_report_status(struct pci_bus *bus, u_int status_mask, int warn)
  24. {
  25. struct pci_dev *dev;
  26. list_for_each_entry(dev, &bus->devices, bus_list) {
  27. u16 status;
  28. /*
  29. * ignore host bridge - we handle
  30. * that separately
  31. */
  32. if (dev->bus->number == 0 && dev->devfn == 0)
  33. continue;
  34. pci_read_config_word(dev, PCI_STATUS, &status);
  35. if (status == 0xffff)
  36. continue;
  37. if ((status & status_mask) == 0)
  38. continue;
  39. /* clear the status errors */
  40. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  41. if (warn)
  42. printk("(%s: %04X) ", pci_name(dev), status);
  43. }
  44. list_for_each_entry(dev, &bus->devices, bus_list)
  45. if (dev->subordinate)
  46. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  47. }
  48. void pcibios_report_status(u_int status_mask, int warn)
  49. {
  50. struct pci_bus *bus;
  51. list_for_each_entry(bus, &pci_root_buses, node)
  52. pcibios_bus_report_status(bus, status_mask, warn);
  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 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 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 pci_fixup_dec21285(struct pci_dev *dev)
  122. {
  123. if (dev->devfn == 0) {
  124. struct resource *r;
  125. dev->class &= 0xff;
  126. dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
  127. pci_dev_for_each_resource(dev, r) {
  128. r->start = 0;
  129. r->end = 0;
  130. r->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 pci_fixup_ide_bases(struct pci_dev *dev)
  139. {
  140. struct resource *r;
  141. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  142. return;
  143. pci_dev_for_each_resource(dev, r) {
  144. if ((r->start & ~0x80) == 0x374) {
  145. r->start |= 2;
  146. r->end = r->start;
  147. }
  148. }
  149. }
  150. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  151. /*
  152. * Put the DEC21142 to sleep
  153. */
  154. static void pci_fixup_dec21142(struct pci_dev *dev)
  155. {
  156. pci_write_config_dword(dev, 0x40, 0x80000000);
  157. }
  158. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142, pci_fixup_dec21142);
  159. /*
  160. * The CY82C693 needs some rather major fixups to ensure that it does
  161. * the right thing. Idea from the Alpha people, with a few additions.
  162. *
  163. * We ensure that the IDE base registers are set to 1f0/3f4 for the
  164. * primary bus, and 170/374 for the secondary bus. Also, hide them
  165. * from the PCI subsystem view as well so we won't try to perform
  166. * our own auto-configuration on them.
  167. *
  168. * In addition, we ensure that the PCI IDE interrupts are routed to
  169. * IRQ 14 and IRQ 15 respectively.
  170. *
  171. * The above gets us to a point where the IDE on this device is
  172. * functional. However, The CY82C693U _does not work_ in bus
  173. * master mode without locking the PCI bus solid.
  174. */
  175. static void pci_fixup_cy82c693(struct pci_dev *dev)
  176. {
  177. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  178. u32 base0, base1;
  179. if (dev->class & 0x80) { /* primary */
  180. base0 = 0x1f0;
  181. base1 = 0x3f4;
  182. } else { /* secondary */
  183. base0 = 0x170;
  184. base1 = 0x374;
  185. }
  186. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0,
  187. base0 | PCI_BASE_ADDRESS_SPACE_IO);
  188. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1,
  189. base1 | PCI_BASE_ADDRESS_SPACE_IO);
  190. dev->resource[0].start = 0;
  191. dev->resource[0].end = 0;
  192. dev->resource[0].flags = 0;
  193. dev->resource[1].start = 0;
  194. dev->resource[1].end = 0;
  195. dev->resource[1].flags = 0;
  196. } else if (PCI_FUNC(dev->devfn) == 0) {
  197. /*
  198. * Setup IDE IRQ routing.
  199. */
  200. pci_write_config_byte(dev, 0x4b, 14);
  201. pci_write_config_byte(dev, 0x4c, 15);
  202. /*
  203. * Disable FREQACK handshake, enable USB.
  204. */
  205. pci_write_config_byte(dev, 0x4d, 0x41);
  206. /*
  207. * Enable PCI retry, and PCI post-write buffer.
  208. */
  209. pci_write_config_byte(dev, 0x44, 0x17);
  210. /*
  211. * Enable ISA master and DMA post write buffering.
  212. */
  213. pci_write_config_byte(dev, 0x45, 0x03);
  214. }
  215. }
  216. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, pci_fixup_cy82c693);
  217. /*
  218. * If the bus contains any of these devices, then we must not turn on
  219. * parity checking of any kind. Currently this is CyberPro 20x0 only.
  220. */
  221. static inline int pdev_bad_for_parity(struct pci_dev *dev)
  222. {
  223. return ((dev->vendor == PCI_VENDOR_ID_INTERG &&
  224. (dev->device == PCI_DEVICE_ID_INTERG_2000 ||
  225. dev->device == PCI_DEVICE_ID_INTERG_2010)) ||
  226. (dev->vendor == PCI_VENDOR_ID_ITE &&
  227. dev->device == PCI_DEVICE_ID_ITE_8152));
  228. }
  229. /*
  230. * pcibios_fixup_bus - Called after each bus is probed,
  231. * but before its children are examined.
  232. */
  233. void pcibios_fixup_bus(struct pci_bus *bus)
  234. {
  235. struct pci_dev *dev;
  236. u16 features = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_FAST_BACK;
  237. /*
  238. * Walk the devices on this bus, working out what we can
  239. * and can't support.
  240. */
  241. list_for_each_entry(dev, &bus->devices, bus_list) {
  242. u16 status;
  243. pci_read_config_word(dev, PCI_STATUS, &status);
  244. /*
  245. * If any device on this bus does not support fast back
  246. * to back transfers, then the bus as a whole is not able
  247. * to support them. Having fast back to back transfers
  248. * on saves us one PCI cycle per transaction.
  249. */
  250. if (!(status & PCI_STATUS_FAST_BACK))
  251. features &= ~PCI_COMMAND_FAST_BACK;
  252. if (pdev_bad_for_parity(dev))
  253. features &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  254. switch (dev->class >> 8) {
  255. case PCI_CLASS_BRIDGE_PCI:
  256. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &status);
  257. status |= PCI_BRIDGE_CTL_PARITY|PCI_BRIDGE_CTL_MASTER_ABORT;
  258. status &= ~(PCI_BRIDGE_CTL_BUS_RESET|PCI_BRIDGE_CTL_FAST_BACK);
  259. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, status);
  260. break;
  261. case PCI_CLASS_BRIDGE_CARDBUS:
  262. pci_read_config_word(dev, PCI_CB_BRIDGE_CONTROL, &status);
  263. status |= PCI_CB_BRIDGE_CTL_PARITY|PCI_CB_BRIDGE_CTL_MASTER_ABORT;
  264. pci_write_config_word(dev, PCI_CB_BRIDGE_CONTROL, status);
  265. break;
  266. }
  267. }
  268. /*
  269. * Now walk the devices again, this time setting them up.
  270. */
  271. list_for_each_entry(dev, &bus->devices, bus_list) {
  272. u16 cmd;
  273. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  274. cmd |= features;
  275. pci_write_config_word(dev, PCI_COMMAND, cmd);
  276. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  277. L1_CACHE_BYTES >> 2);
  278. }
  279. /*
  280. * Propagate the flags to the PCI bridge.
  281. */
  282. if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  283. if (features & PCI_COMMAND_FAST_BACK)
  284. bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
  285. if (features & PCI_COMMAND_PARITY)
  286. bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
  287. }
  288. /*
  289. * Report what we did for this bus
  290. */
  291. pr_info("PCI: bus%d: Fast back to back transfers %sabled\n",
  292. bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
  293. }
  294. EXPORT_SYMBOL(pcibios_fixup_bus);
  295. /*
  296. * Swizzle the device pin each time we cross a bridge. If a platform does
  297. * not provide a swizzle function, we perform the standard PCI swizzling.
  298. *
  299. * The default swizzling walks up the bus tree one level at a time, applying
  300. * the standard swizzle function at each step, stopping when it finds the PCI
  301. * root bus. This will return the slot number of the bridge device on the
  302. * root bus and the interrupt pin on that device which should correspond
  303. * with the downstream device interrupt.
  304. *
  305. * Platforms may override this, in which case the slot and pin returned
  306. * depend entirely on the platform code. However, please note that the
  307. * PCI standard swizzle is implemented on plug-in cards and Cardbus based
  308. * PCI extenders, so it can not be ignored.
  309. */
  310. static u8 pcibios_swizzle(struct pci_dev *dev, u8 *pin)
  311. {
  312. struct pci_sys_data *sys = dev->sysdata;
  313. int slot, oldpin = *pin;
  314. if (sys->swizzle)
  315. slot = sys->swizzle(dev, pin);
  316. else
  317. slot = pci_common_swizzle(dev, pin);
  318. if (debug_pci)
  319. printk("PCI: %s swizzling pin %d => pin %d slot %d\n",
  320. pci_name(dev), oldpin, *pin, slot);
  321. return slot;
  322. }
  323. /*
  324. * Map a slot/pin to an IRQ.
  325. */
  326. static int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  327. {
  328. struct pci_sys_data *sys = dev->sysdata;
  329. int irq = -1;
  330. if (sys->map_irq)
  331. irq = sys->map_irq(dev, slot, pin);
  332. if (debug_pci)
  333. printk("PCI: %s mapping slot %d pin %d => irq %d\n",
  334. pci_name(dev), slot, pin, irq);
  335. return irq;
  336. }
  337. static int pcibios_init_resource(int busnr, struct pci_sys_data *sys)
  338. {
  339. int ret;
  340. struct resource_entry *window;
  341. if (list_empty(&sys->resources)) {
  342. pci_add_resource_offset(&sys->resources,
  343. &iomem_resource, sys->mem_offset);
  344. }
  345. resource_list_for_each_entry(window, &sys->resources)
  346. if (resource_type(window->res) == IORESOURCE_IO)
  347. return 0;
  348. sys->io_res.start = (busnr * SZ_64K) ? : pcibios_min_io;
  349. sys->io_res.end = (busnr + 1) * SZ_64K - 1;
  350. sys->io_res.flags = IORESOURCE_IO;
  351. sys->io_res.name = sys->io_res_name;
  352. sprintf(sys->io_res_name, "PCI%d I/O", busnr);
  353. ret = request_resource(&ioport_resource, &sys->io_res);
  354. if (ret) {
  355. pr_err("PCI: unable to allocate I/O port region (%d)\n", ret);
  356. return ret;
  357. }
  358. pci_add_resource_offset(&sys->resources, &sys->io_res,
  359. sys->io_offset);
  360. return 0;
  361. }
  362. static void pcibios_init_hw(struct device *parent, struct hw_pci *hw,
  363. struct list_head *head)
  364. {
  365. struct pci_sys_data *sys = NULL;
  366. int ret;
  367. int nr, busnr;
  368. for (nr = busnr = 0; nr < hw->nr_controllers; nr++) {
  369. struct pci_host_bridge *bridge;
  370. bridge = pci_alloc_host_bridge(sizeof(struct pci_sys_data));
  371. if (WARN(!bridge, "PCI: unable to allocate bridge!"))
  372. break;
  373. sys = pci_host_bridge_priv(bridge);
  374. sys->busnr = busnr;
  375. sys->swizzle = hw->swizzle;
  376. sys->map_irq = hw->map_irq;
  377. INIT_LIST_HEAD(&sys->resources);
  378. if (hw->private_data)
  379. sys->private_data = hw->private_data[nr];
  380. ret = hw->setup(nr, sys);
  381. if (ret > 0) {
  382. ret = pcibios_init_resource(nr, sys);
  383. if (ret) {
  384. pci_free_host_bridge(bridge);
  385. break;
  386. }
  387. bridge->map_irq = pcibios_map_irq;
  388. bridge->swizzle_irq = pcibios_swizzle;
  389. if (hw->scan)
  390. ret = hw->scan(nr, bridge);
  391. else {
  392. list_splice_init(&sys->resources,
  393. &bridge->windows);
  394. bridge->dev.parent = parent;
  395. bridge->sysdata = sys;
  396. bridge->busnr = sys->busnr;
  397. bridge->ops = hw->ops;
  398. ret = pci_scan_root_bus_bridge(bridge);
  399. }
  400. if (WARN(ret < 0, "PCI: unable to scan bus!")) {
  401. pci_free_host_bridge(bridge);
  402. break;
  403. }
  404. sys->bus = bridge->bus;
  405. busnr = sys->bus->busn_res.end + 1;
  406. list_add(&sys->node, head);
  407. } else {
  408. pci_free_host_bridge(bridge);
  409. if (ret < 0)
  410. break;
  411. }
  412. }
  413. }
  414. void pci_common_init_dev(struct device *parent, struct hw_pci *hw)
  415. {
  416. struct pci_sys_data *sys;
  417. LIST_HEAD(head);
  418. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  419. if (hw->preinit)
  420. hw->preinit();
  421. pcibios_init_hw(parent, hw, &head);
  422. if (hw->postinit)
  423. hw->postinit();
  424. list_for_each_entry(sys, &head, node) {
  425. struct pci_bus *bus = sys->bus;
  426. /*
  427. * We insert PCI resources into the iomem_resource and
  428. * ioport_resource trees in either pci_bus_claim_resources()
  429. * or pci_bus_assign_resources().
  430. */
  431. if (pci_has_flag(PCI_PROBE_ONLY)) {
  432. pci_bus_claim_resources(bus);
  433. } else {
  434. struct pci_bus *child;
  435. pci_bus_size_bridges(bus);
  436. pci_bus_assign_resources(bus);
  437. list_for_each_entry(child, &bus->children, node)
  438. pcie_bus_configure_settings(child);
  439. }
  440. pci_bus_add_devices(bus);
  441. }
  442. }
  443. #ifndef CONFIG_PCI_HOST_ITE8152
  444. void pcibios_set_master(struct pci_dev *dev)
  445. {
  446. /* No special bus mastering setup handling */
  447. }
  448. #endif
  449. char * __init pcibios_setup(char *str)
  450. {
  451. if (!strcmp(str, "debug")) {
  452. debug_pci = 1;
  453. return NULL;
  454. }
  455. return str;
  456. }
  457. /*
  458. * From arch/i386/kernel/pci-i386.c:
  459. *
  460. * We need to avoid collisions with `mirrored' VGA ports
  461. * and other strange ISA hardware, so we always want the
  462. * addresses to be allocated in the 0x000-0x0ff region
  463. * modulo 0x400.
  464. *
  465. * Why? Because some silly external IO cards only decode
  466. * the low 10 bits of the IO address. The 0x00-0xff region
  467. * is reserved for motherboard devices that decode all 16
  468. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  469. * but we want to try to avoid allocating at 0x2900-0x2bff
  470. * which might be mirrored at 0x0100-0x03ff..
  471. */
  472. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  473. resource_size_t size, resource_size_t align)
  474. {
  475. struct pci_dev *dev = data;
  476. resource_size_t start = res->start;
  477. struct pci_host_bridge *host_bridge;
  478. if (res->flags & IORESOURCE_IO && start & 0x300)
  479. start = (start + 0x3ff) & ~0x3ff;
  480. start = (start + align - 1) & ~(align - 1);
  481. host_bridge = pci_find_host_bridge(dev->bus);
  482. if (host_bridge->align_resource)
  483. return host_bridge->align_resource(dev, res,
  484. start, size, align);
  485. return start;
  486. }
  487. void __init pci_map_io_early(unsigned long pfn)
  488. {
  489. struct map_desc pci_io_desc = {
  490. .virtual = PCI_IO_VIRT_BASE,
  491. .type = MT_DEVICE,
  492. .length = SZ_64K,
  493. };
  494. pci_io_desc.pfn = pfn;
  495. iotable_init(&pci_io_desc, 1);
  496. }