setup-pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 1995-1998 Mark Lord
  4. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  5. *
  6. * May be copied or modified under the terms of the GNU General Public License
  7. */
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/ide.h>
  14. #include <linux/dma-mapping.h>
  15. #include <asm/io.h>
  16. /**
  17. * ide_setup_pci_baseregs - place a PCI IDE controller native
  18. * @dev: PCI device of interface to switch native
  19. * @name: Name of interface
  20. *
  21. * We attempt to place the PCI interface into PCI native mode. If
  22. * we succeed the BARs are ok and the controller is in PCI mode.
  23. * Returns 0 on success or an errno code.
  24. *
  25. * FIXME: if we program the interface and then fail to set the BARS
  26. * we don't switch it back to legacy mode. Do we actually care ??
  27. */
  28. static int ide_setup_pci_baseregs(struct pci_dev *dev, const char *name)
  29. {
  30. u8 progif = 0;
  31. /*
  32. * Place both IDE interfaces into PCI "native" mode:
  33. */
  34. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  35. (progif & 5) != 5) {
  36. if ((progif & 0xa) != 0xa) {
  37. printk(KERN_INFO "%s: device not capable of full "
  38. "native PCI mode\n", name);
  39. return -EOPNOTSUPP;
  40. }
  41. printk("%s: placing both ports into native PCI mode\n", name);
  42. (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
  43. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  44. (progif & 5) != 5) {
  45. printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted "
  46. "0x%04x, got 0x%04x\n",
  47. name, progif|5, progif);
  48. return -EOPNOTSUPP;
  49. }
  50. }
  51. return 0;
  52. }
  53. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  54. static void ide_pci_clear_simplex(unsigned long dma_base, const char *name)
  55. {
  56. u8 dma_stat = inb(dma_base + 2);
  57. outb(dma_stat & 0x60, dma_base + 2);
  58. dma_stat = inb(dma_base + 2);
  59. if (dma_stat & 0x80)
  60. printk(KERN_INFO "%s: simplex device: DMA forced\n", name);
  61. }
  62. /**
  63. * ide_pci_dma_base - setup BMIBA
  64. * @hwif: IDE interface
  65. * @d: IDE port info
  66. *
  67. * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space.
  68. */
  69. unsigned long ide_pci_dma_base(ide_hwif_t *hwif, const struct ide_port_info *d)
  70. {
  71. struct pci_dev *dev = to_pci_dev(hwif->dev);
  72. unsigned long dma_base = 0;
  73. if (hwif->host_flags & IDE_HFLAG_MMIO)
  74. return hwif->dma_base;
  75. if (hwif->mate && hwif->mate->dma_base) {
  76. dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
  77. } else {
  78. u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
  79. dma_base = pci_resource_start(dev, baridx);
  80. if (dma_base == 0) {
  81. printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
  82. return 0;
  83. }
  84. }
  85. if (hwif->channel)
  86. dma_base += 8;
  87. return dma_base;
  88. }
  89. EXPORT_SYMBOL_GPL(ide_pci_dma_base);
  90. int ide_pci_check_simplex(ide_hwif_t *hwif, const struct ide_port_info *d)
  91. {
  92. u8 dma_stat;
  93. if (d->host_flags & (IDE_HFLAG_MMIO | IDE_HFLAG_CS5520))
  94. goto out;
  95. if (d->host_flags & IDE_HFLAG_CLEAR_SIMPLEX) {
  96. ide_pci_clear_simplex(hwif->dma_base, d->name);
  97. goto out;
  98. }
  99. /*
  100. * If the device claims "simplex" DMA, this means that only one of
  101. * the two interfaces can be trusted with DMA at any point in time
  102. * (so we should enable DMA only on one of the two interfaces).
  103. *
  104. * FIXME: At this point we haven't probed the drives so we can't make
  105. * the appropriate decision. Really we should defer this problem until
  106. * we tune the drive then try to grab DMA ownership if we want to be
  107. * the DMA end. This has to be become dynamic to handle hot-plug.
  108. */
  109. dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  110. if ((dma_stat & 0x80) && hwif->mate && hwif->mate->dma_base) {
  111. printk(KERN_INFO "%s: simplex device: DMA disabled\n", d->name);
  112. return -1;
  113. }
  114. out:
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(ide_pci_check_simplex);
  118. /*
  119. * Set up BM-DMA capability (PnP BIOS should have done this)
  120. */
  121. int ide_pci_set_master(struct pci_dev *dev, const char *name)
  122. {
  123. u16 pcicmd;
  124. pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
  125. if ((pcicmd & PCI_COMMAND_MASTER) == 0) {
  126. pci_set_master(dev);
  127. if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) ||
  128. (pcicmd & PCI_COMMAND_MASTER) == 0) {
  129. printk(KERN_ERR "%s: error updating PCICMD on %s\n",
  130. name, pci_name(dev));
  131. return -EIO;
  132. }
  133. }
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(ide_pci_set_master);
  137. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  138. void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
  139. {
  140. printk(KERN_INFO "%s: IDE controller (0x%04x:0x%04x rev 0x%02x) at "
  141. " PCI slot %s\n", d->name, dev->vendor, dev->device,
  142. dev->revision, pci_name(dev));
  143. }
  144. EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
  145. /**
  146. * ide_pci_enable - do PCI enables
  147. * @dev: PCI device
  148. * @d: IDE port info
  149. *
  150. * Enable the IDE PCI device. We attempt to enable the device in full
  151. * but if that fails then we only need IO space. The PCI code should
  152. * have setup the proper resources for us already for controllers in
  153. * legacy mode.
  154. *
  155. * Returns zero on success or an error code
  156. */
  157. static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
  158. {
  159. int ret, bars;
  160. if (pci_enable_device(dev)) {
  161. ret = pci_enable_device_io(dev);
  162. if (ret < 0) {
  163. printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
  164. "Could not enable device.\n", d->name);
  165. goto out;
  166. }
  167. printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
  168. }
  169. /*
  170. * assume all devices can do 32-bit DMA for now, we can add
  171. * a DMA mask field to the struct ide_port_info if we need it
  172. * (or let lower level driver set the DMA mask)
  173. */
  174. ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
  175. if (ret < 0) {
  176. printk(KERN_ERR "%s: can't set dma mask\n", d->name);
  177. goto out;
  178. }
  179. if (d->host_flags & IDE_HFLAG_SINGLE)
  180. bars = (1 << 2) - 1;
  181. else
  182. bars = (1 << 4) - 1;
  183. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
  184. if (d->host_flags & IDE_HFLAG_CS5520)
  185. bars |= (1 << 2);
  186. else
  187. bars |= (1 << 4);
  188. }
  189. ret = pci_request_selected_regions(dev, bars, d->name);
  190. if (ret < 0)
  191. printk(KERN_ERR "%s: can't reserve resources\n", d->name);
  192. out:
  193. return ret;
  194. }
  195. /**
  196. * ide_pci_configure - configure an unconfigured device
  197. * @dev: PCI device
  198. * @d: IDE port info
  199. *
  200. * Enable and configure the PCI device we have been passed.
  201. * Returns zero on success or an error code.
  202. */
  203. static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
  204. {
  205. u16 pcicmd = 0;
  206. /*
  207. * PnP BIOS was *supposed* to have setup this device, but we
  208. * can do it ourselves, so long as the BIOS has assigned an IRQ
  209. * (or possibly the device is using a "legacy header" for IRQs).
  210. * Maybe the user deliberately *disabled* the device,
  211. * but we'll eventually ignore it again if no drives respond.
  212. */
  213. if (ide_setup_pci_baseregs(dev, d->name) ||
  214. pci_write_config_word(dev, PCI_COMMAND, pcicmd | PCI_COMMAND_IO)) {
  215. printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
  216. return -ENODEV;
  217. }
  218. if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
  219. printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
  220. return -EIO;
  221. }
  222. if (!(pcicmd & PCI_COMMAND_IO)) {
  223. printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
  224. return -ENXIO;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * ide_pci_check_iomem - check a register is I/O
  230. * @dev: PCI device
  231. * @d: IDE port info
  232. * @bar: BAR number
  233. *
  234. * Checks if a BAR is configured and points to MMIO space. If so,
  235. * return an error code. Otherwise return 0
  236. */
  237. static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d,
  238. int bar)
  239. {
  240. ulong flags = pci_resource_flags(dev, bar);
  241. /* Unconfigured ? */
  242. if (!flags || pci_resource_len(dev, bar) == 0)
  243. return 0;
  244. /* I/O space */
  245. if (flags & IORESOURCE_IO)
  246. return 0;
  247. /* Bad */
  248. return -EINVAL;
  249. }
  250. /**
  251. * ide_hw_configure - configure a hw_regs_t instance
  252. * @dev: PCI device holding interface
  253. * @d: IDE port info
  254. * @port: port number
  255. * @irq: PCI IRQ
  256. * @hw: hw_regs_t instance corresponding to this port
  257. *
  258. * Perform the initial set up for the hardware interface structure. This
  259. * is done per interface port rather than per PCI device. There may be
  260. * more than one port per device.
  261. *
  262. * Returns zero on success or an error code.
  263. */
  264. static int ide_hw_configure(struct pci_dev *dev, const struct ide_port_info *d,
  265. unsigned int port, int irq, hw_regs_t *hw)
  266. {
  267. unsigned long ctl = 0, base = 0;
  268. if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) {
  269. if (ide_pci_check_iomem(dev, d, 2 * port) ||
  270. ide_pci_check_iomem(dev, d, 2 * port + 1)) {
  271. printk(KERN_ERR "%s: I/O baseregs (BIOS) are reported "
  272. "as MEM for port %d!\n", d->name, port);
  273. return -EINVAL;
  274. }
  275. ctl = pci_resource_start(dev, 2*port+1);
  276. base = pci_resource_start(dev, 2*port);
  277. } else {
  278. /* Use default values */
  279. ctl = port ? 0x374 : 0x3f4;
  280. base = port ? 0x170 : 0x1f0;
  281. }
  282. if (!base || !ctl) {
  283. printk(KERN_ERR "%s: bad PCI BARs for port %d, skipping\n",
  284. d->name, port);
  285. return -EINVAL;
  286. }
  287. memset(hw, 0, sizeof(*hw));
  288. hw->irq = irq;
  289. hw->dev = &dev->dev;
  290. hw->chipset = d->chipset ? d->chipset : ide_pci;
  291. ide_std_init_ports(hw, base, ctl | 2);
  292. return 0;
  293. }
  294. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  295. /**
  296. * ide_hwif_setup_dma - configure DMA interface
  297. * @hwif: IDE interface
  298. * @d: IDE port info
  299. *
  300. * Set up the DMA base for the interface. Enable the master bits as
  301. * necessary and attempt to bring the device DMA into a ready to use
  302. * state
  303. */
  304. int ide_hwif_setup_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
  305. {
  306. struct pci_dev *dev = to_pci_dev(hwif->dev);
  307. if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
  308. ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
  309. (dev->class & 0x80))) {
  310. unsigned long base = ide_pci_dma_base(hwif, d);
  311. if (base == 0)
  312. return -1;
  313. hwif->dma_base = base;
  314. if (ide_pci_check_simplex(hwif, d) < 0)
  315. return -1;
  316. if (ide_pci_set_master(dev, d->name) < 0)
  317. return -1;
  318. if (hwif->host_flags & IDE_HFLAG_MMIO)
  319. printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
  320. else
  321. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  322. hwif->name, base, base + 7);
  323. hwif->extra_base = base + (hwif->channel ? 8 : 16);
  324. if (ide_allocate_dma_engine(hwif))
  325. return -1;
  326. hwif->dma_ops = &sff_dma_ops;
  327. }
  328. return 0;
  329. }
  330. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  331. /**
  332. * ide_setup_pci_controller - set up IDE PCI
  333. * @dev: PCI device
  334. * @d: IDE port info
  335. * @noisy: verbose flag
  336. *
  337. * Set up the PCI and controller side of the IDE interface. This brings
  338. * up the PCI side of the device, checks that the device is enabled
  339. * and enables it if need be
  340. */
  341. static int ide_setup_pci_controller(struct pci_dev *dev,
  342. const struct ide_port_info *d, int noisy)
  343. {
  344. int ret;
  345. u16 pcicmd;
  346. if (noisy)
  347. ide_setup_pci_noise(dev, d);
  348. ret = ide_pci_enable(dev, d);
  349. if (ret < 0)
  350. goto out;
  351. ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
  352. if (ret < 0) {
  353. printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
  354. goto out;
  355. }
  356. if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
  357. ret = ide_pci_configure(dev, d);
  358. if (ret < 0)
  359. goto out;
  360. printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
  361. }
  362. out:
  363. return ret;
  364. }
  365. /**
  366. * ide_pci_setup_ports - configure ports/devices on PCI IDE
  367. * @dev: PCI device
  368. * @d: IDE port info
  369. * @pciirq: IRQ line
  370. * @hw: hw_regs_t instances corresponding to this PCI IDE device
  371. * @hws: hw_regs_t pointers table to update
  372. *
  373. * Scan the interfaces attached to this device and do any
  374. * necessary per port setup. Attach the devices and ask the
  375. * generic DMA layer to do its work for us.
  376. *
  377. * Normally called automaticall from do_ide_pci_setup_device,
  378. * but is also used directly as a helper function by some controllers
  379. * where the chipset setup is not the default PCI IDE one.
  380. */
  381. void ide_pci_setup_ports(struct pci_dev *dev, const struct ide_port_info *d,
  382. int pciirq, hw_regs_t *hw, hw_regs_t **hws)
  383. {
  384. int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
  385. u8 tmp;
  386. /*
  387. * Set up the IDE ports
  388. */
  389. for (port = 0; port < channels; ++port) {
  390. const ide_pci_enablebit_t *e = &(d->enablebits[port]);
  391. if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
  392. (tmp & e->mask) != e->val)) {
  393. printk(KERN_INFO "%s: IDE port disabled\n", d->name);
  394. continue; /* port not enabled */
  395. }
  396. if (ide_hw_configure(dev, d, port, pciirq, hw + port))
  397. continue;
  398. *(hws + port) = hw + port;
  399. }
  400. }
  401. EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
  402. /*
  403. * ide_setup_pci_device() looks at the primary/secondary interfaces
  404. * on a PCI IDE device and, if they are enabled, prepares the IDE driver
  405. * for use with them. This generic code works for most PCI chipsets.
  406. *
  407. * One thing that is not standardized is the location of the
  408. * primary/secondary interface "enable/disable" bits. For chipsets that
  409. * we "know" about, this information is in the struct ide_port_info;
  410. * for all other chipsets, we just assume both interfaces are enabled.
  411. */
  412. static int do_ide_setup_pci_device(struct pci_dev *dev,
  413. const struct ide_port_info *d,
  414. u8 noisy)
  415. {
  416. int pciirq, ret;
  417. /*
  418. * Can we trust the reported IRQ?
  419. */
  420. pciirq = dev->irq;
  421. /*
  422. * This allows offboard ide-pci cards the enable a BIOS,
  423. * verify interrupt settings of split-mirror pci-config
  424. * space, place chipset into init-mode, and/or preserve
  425. * an interrupt if the card is not native ide support.
  426. */
  427. ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
  428. if (ret < 0)
  429. goto out;
  430. /* Is it an "IDE storage" device in non-PCI mode? */
  431. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
  432. if (noisy)
  433. printk(KERN_INFO "%s: not 100%% native mode: "
  434. "will probe irqs later\n", d->name);
  435. pciirq = ret;
  436. } else if (!pciirq) {
  437. if (noisy)
  438. printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
  439. d->name, pciirq);
  440. pciirq = 0;
  441. } else {
  442. if (noisy)
  443. printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
  444. d->name, pciirq);
  445. }
  446. ret = pciirq;
  447. out:
  448. return ret;
  449. }
  450. int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
  451. {
  452. hw_regs_t hw[4], *hws[] = { NULL, NULL, NULL, NULL };
  453. int ret;
  454. ret = ide_setup_pci_controller(dev, d, 1);
  455. if (ret < 0)
  456. goto out;
  457. ide_pci_setup_ports(dev, d, 0, &hw[0], &hws[0]);
  458. ret = do_ide_setup_pci_device(dev, d, 1);
  459. if (ret < 0)
  460. goto out;
  461. /* fixup IRQ */
  462. hw[1].irq = hw[0].irq = ret;
  463. ret = ide_host_add(d, hws, NULL);
  464. out:
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(ide_setup_pci_device);
  468. int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
  469. const struct ide_port_info *d)
  470. {
  471. struct pci_dev *pdev[] = { dev1, dev2 };
  472. int ret, i;
  473. hw_regs_t hw[4], *hws[] = { NULL, NULL, NULL, NULL };
  474. for (i = 0; i < 2; i++) {
  475. ret = ide_setup_pci_controller(pdev[i], d, !i);
  476. if (ret < 0)
  477. goto out;
  478. ide_pci_setup_ports(pdev[i], d, 0, &hw[i*2], &hws[i*2]);
  479. ret = do_ide_setup_pci_device(pdev[i], d, !i);
  480. /*
  481. * FIXME: Mom, mom, they stole me the helper function to undo
  482. * do_ide_setup_pci_device() on the first device!
  483. */
  484. if (ret < 0)
  485. goto out;
  486. /* fixup IRQ */
  487. hw[i*2 + 1].irq = hw[i*2].irq = ret;
  488. }
  489. ret = ide_host_add(d, hws, NULL);
  490. out:
  491. return ret;
  492. }
  493. EXPORT_SYMBOL_GPL(ide_setup_pci_devices);