setup-pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. * Where a device has a partner that is already in DMA mode we check
  69. * and enforce IDE simplex rules.
  70. */
  71. unsigned long ide_pci_dma_base(ide_hwif_t *hwif, const struct ide_port_info *d)
  72. {
  73. struct pci_dev *dev = to_pci_dev(hwif->dev);
  74. unsigned long dma_base = 0;
  75. u8 dma_stat = 0;
  76. if (hwif->host_flags & IDE_HFLAG_MMIO)
  77. return hwif->dma_base;
  78. if (hwif->mate && hwif->mate->dma_base) {
  79. dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
  80. } else {
  81. u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
  82. dma_base = pci_resource_start(dev, baridx);
  83. if (dma_base == 0) {
  84. printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
  85. return 0;
  86. }
  87. }
  88. if (hwif->channel)
  89. dma_base += 8;
  90. if (d->host_flags & IDE_HFLAG_CS5520)
  91. goto out;
  92. if (d->host_flags & IDE_HFLAG_CLEAR_SIMPLEX) {
  93. ide_pci_clear_simplex(dma_base, d->name);
  94. goto out;
  95. }
  96. /*
  97. * If the device claims "simplex" DMA, this means that only one of
  98. * the two interfaces can be trusted with DMA at any point in time
  99. * (so we should enable DMA only on one of the two interfaces).
  100. *
  101. * FIXME: At this point we haven't probed the drives so we can't make
  102. * the appropriate decision. Really we should defer this problem until
  103. * we tune the drive then try to grab DMA ownership if we want to be
  104. * the DMA end. This has to be become dynamic to handle hot-plug.
  105. */
  106. dma_stat = hwif->INB(dma_base + 2);
  107. if ((dma_stat & 0x80) && hwif->mate && hwif->mate->dma_base) {
  108. printk(KERN_INFO "%s: simplex device: DMA disabled\n", d->name);
  109. dma_base = 0;
  110. }
  111. out:
  112. return dma_base;
  113. }
  114. EXPORT_SYMBOL_GPL(ide_pci_dma_base);
  115. /*
  116. * Set up BM-DMA capability (PnP BIOS should have done this)
  117. */
  118. int ide_pci_set_master(struct pci_dev *dev, const char *name)
  119. {
  120. u16 pcicmd;
  121. pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
  122. if ((pcicmd & PCI_COMMAND_MASTER) == 0) {
  123. pci_set_master(dev);
  124. if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) ||
  125. (pcicmd & PCI_COMMAND_MASTER) == 0) {
  126. printk(KERN_ERR "%s: error updating PCICMD on %s\n",
  127. name, pci_name(dev));
  128. return -EIO;
  129. }
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(ide_pci_set_master);
  134. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  135. void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
  136. {
  137. printk(KERN_INFO "%s: IDE controller (0x%04x:0x%04x rev 0x%02x) at "
  138. " PCI slot %s\n", d->name, dev->vendor, dev->device,
  139. dev->revision, pci_name(dev));
  140. }
  141. EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
  142. /**
  143. * ide_pci_enable - do PCI enables
  144. * @dev: PCI device
  145. * @d: IDE port info
  146. *
  147. * Enable the IDE PCI device. We attempt to enable the device in full
  148. * but if that fails then we only need IO space. The PCI code should
  149. * have setup the proper resources for us already for controllers in
  150. * legacy mode.
  151. *
  152. * Returns zero on success or an error code
  153. */
  154. static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
  155. {
  156. int ret, bars;
  157. if (pci_enable_device(dev)) {
  158. ret = pci_enable_device_io(dev);
  159. if (ret < 0) {
  160. printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
  161. "Could not enable device.\n", d->name);
  162. goto out;
  163. }
  164. printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
  165. }
  166. /*
  167. * assume all devices can do 32-bit DMA for now, we can add
  168. * a DMA mask field to the struct ide_port_info if we need it
  169. * (or let lower level driver set the DMA mask)
  170. */
  171. ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
  172. if (ret < 0) {
  173. printk(KERN_ERR "%s: can't set dma mask\n", d->name);
  174. goto out;
  175. }
  176. if (d->host_flags & IDE_HFLAG_SINGLE)
  177. bars = (1 << 2) - 1;
  178. else
  179. bars = (1 << 4) - 1;
  180. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
  181. if (d->host_flags & IDE_HFLAG_CS5520)
  182. bars |= (1 << 2);
  183. else
  184. bars |= (1 << 4);
  185. }
  186. ret = pci_request_selected_regions(dev, bars, d->name);
  187. if (ret < 0)
  188. printk(KERN_ERR "%s: can't reserve resources\n", d->name);
  189. out:
  190. return ret;
  191. }
  192. /**
  193. * ide_pci_configure - configure an unconfigured device
  194. * @dev: PCI device
  195. * @d: IDE port info
  196. *
  197. * Enable and configure the PCI device we have been passed.
  198. * Returns zero on success or an error code.
  199. */
  200. static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
  201. {
  202. u16 pcicmd = 0;
  203. /*
  204. * PnP BIOS was *supposed* to have setup this device, but we
  205. * can do it ourselves, so long as the BIOS has assigned an IRQ
  206. * (or possibly the device is using a "legacy header" for IRQs).
  207. * Maybe the user deliberately *disabled* the device,
  208. * but we'll eventually ignore it again if no drives respond.
  209. */
  210. if (ide_setup_pci_baseregs(dev, d->name) ||
  211. pci_write_config_word(dev, PCI_COMMAND, pcicmd | PCI_COMMAND_IO)) {
  212. printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
  213. return -ENODEV;
  214. }
  215. if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
  216. printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
  217. return -EIO;
  218. }
  219. if (!(pcicmd & PCI_COMMAND_IO)) {
  220. printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
  221. return -ENXIO;
  222. }
  223. return 0;
  224. }
  225. /**
  226. * ide_pci_check_iomem - check a register is I/O
  227. * @dev: PCI device
  228. * @d: IDE port info
  229. * @bar: BAR number
  230. *
  231. * Checks if a BAR is configured and points to MMIO space. If so,
  232. * return an error code. Otherwise return 0
  233. */
  234. static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d,
  235. int bar)
  236. {
  237. ulong flags = pci_resource_flags(dev, bar);
  238. /* Unconfigured ? */
  239. if (!flags || pci_resource_len(dev, bar) == 0)
  240. return 0;
  241. /* I/O space */
  242. if (flags & IORESOURCE_IO)
  243. return 0;
  244. /* Bad */
  245. return -EINVAL;
  246. }
  247. /**
  248. * ide_hwif_configure - configure an IDE interface
  249. * @dev: PCI device holding interface
  250. * @d: IDE port info
  251. * @port: port number
  252. * @irq: PCI IRQ
  253. * @hw: hw_regs_t instance corresponding to this port
  254. *
  255. * Perform the initial set up for the hardware interface structure. This
  256. * is done per interface port rather than per PCI device. There may be
  257. * more than one port per device.
  258. *
  259. * Returns the new hardware interface structure, or NULL on a failure
  260. */
  261. static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev,
  262. const struct ide_port_info *d,
  263. unsigned int port, int irq,
  264. hw_regs_t *hw)
  265. {
  266. unsigned long ctl = 0, base = 0;
  267. ide_hwif_t *hwif;
  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 NULL;
  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 NULL;
  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. hwif = ide_find_port_slot(d);
  293. if (hwif == NULL)
  294. return NULL;
  295. hwif->chipset = hw->chipset;
  296. return hwif;
  297. }
  298. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  299. /**
  300. * ide_hwif_setup_dma - configure DMA interface
  301. * @hwif: IDE interface
  302. * @d: IDE port info
  303. *
  304. * Set up the DMA base for the interface. Enable the master bits as
  305. * necessary and attempt to bring the device DMA into a ready to use
  306. * state
  307. */
  308. int ide_hwif_setup_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
  309. {
  310. struct pci_dev *dev = to_pci_dev(hwif->dev);
  311. if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
  312. ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
  313. (dev->class & 0x80))) {
  314. unsigned long base = ide_pci_dma_base(hwif, d);
  315. if (base == 0 || ide_pci_set_master(dev, d->name) < 0)
  316. return -1;
  317. if (hwif->host_flags & IDE_HFLAG_MMIO)
  318. printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
  319. else
  320. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  321. hwif->name, base, base + 7);
  322. hwif->extra_base = base + (hwif->channel ? 8 : 16);
  323. if (ide_allocate_dma_engine(hwif))
  324. return -1;
  325. hwif->dma_base = base;
  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. * @config: returned as 1 if we configured the hardware
  337. *
  338. * Set up the PCI and controller side of the IDE interface. This brings
  339. * up the PCI side of the device, checks that the device is enabled
  340. * and enables it if need be
  341. */
  342. static int ide_setup_pci_controller(struct pci_dev *dev, const struct ide_port_info *d, int noisy, int *config)
  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. *config = 1;
  361. printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
  362. }
  363. out:
  364. return ret;
  365. }
  366. /**
  367. * ide_pci_setup_ports - configure ports/devices on PCI IDE
  368. * @dev: PCI device
  369. * @d: IDE port info
  370. * @pciirq: IRQ line
  371. * @idx: ATA index table to update
  372. * @hw: hw_regs_t instances corresponding to this PCI IDE device
  373. * @hws: hw_regs_t pointers table to update
  374. *
  375. * Scan the interfaces attached to this device and do any
  376. * necessary per port setup. Attach the devices and ask the
  377. * generic DMA layer to do its work for us.
  378. *
  379. * Normally called automaticall from do_ide_pci_setup_device,
  380. * but is also used directly as a helper function by some controllers
  381. * where the chipset setup is not the default PCI IDE one.
  382. */
  383. void ide_pci_setup_ports(struct pci_dev *dev, const struct ide_port_info *d,
  384. int pciirq, u8 *idx, hw_regs_t *hw, hw_regs_t **hws)
  385. {
  386. int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
  387. ide_hwif_t *hwif;
  388. u8 tmp;
  389. /*
  390. * Set up the IDE ports
  391. */
  392. for (port = 0; port < channels; ++port) {
  393. const ide_pci_enablebit_t *e = &(d->enablebits[port]);
  394. if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
  395. (tmp & e->mask) != e->val)) {
  396. printk(KERN_INFO "%s: IDE port disabled\n", d->name);
  397. continue; /* port not enabled */
  398. }
  399. hwif = ide_hwif_configure(dev, d, port, pciirq, hw + port);
  400. if (hwif == NULL)
  401. continue;
  402. *(hws + port) = hw + port;
  403. *(idx + port) = hwif->index;
  404. }
  405. }
  406. EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
  407. /*
  408. * ide_setup_pci_device() looks at the primary/secondary interfaces
  409. * on a PCI IDE device and, if they are enabled, prepares the IDE driver
  410. * for use with them. This generic code works for most PCI chipsets.
  411. *
  412. * One thing that is not standardized is the location of the
  413. * primary/secondary interface "enable/disable" bits. For chipsets that
  414. * we "know" about, this information is in the struct ide_port_info;
  415. * for all other chipsets, we just assume both interfaces are enabled.
  416. */
  417. static int do_ide_setup_pci_device(struct pci_dev *dev,
  418. const struct ide_port_info *d,
  419. u8 noisy)
  420. {
  421. int tried_config = 0;
  422. int pciirq, ret;
  423. ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
  424. if (ret < 0)
  425. goto out;
  426. /*
  427. * Can we trust the reported IRQ?
  428. */
  429. pciirq = dev->irq;
  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. /*
  436. * This allows offboard ide-pci cards the enable a BIOS,
  437. * verify interrupt settings of split-mirror pci-config
  438. * space, place chipset into init-mode, and/or preserve
  439. * an interrupt if the card is not native ide support.
  440. */
  441. ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
  442. if (ret < 0)
  443. goto out;
  444. pciirq = ret;
  445. } else if (tried_config) {
  446. if (noisy)
  447. printk(KERN_INFO "%s: will probe irqs later\n", d->name);
  448. pciirq = 0;
  449. } else if (!pciirq) {
  450. if (noisy)
  451. printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
  452. d->name, pciirq);
  453. pciirq = 0;
  454. } else {
  455. if (d->init_chipset) {
  456. ret = d->init_chipset(dev, d->name);
  457. if (ret < 0)
  458. goto out;
  459. }
  460. if (noisy)
  461. printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
  462. d->name, pciirq);
  463. }
  464. ret = pciirq;
  465. out:
  466. return ret;
  467. }
  468. int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
  469. {
  470. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  471. hw_regs_t hw[4], *hws[] = { NULL, NULL, NULL, NULL };
  472. int ret;
  473. ret = do_ide_setup_pci_device(dev, d, 1);
  474. if (ret >= 0) {
  475. /* FIXME: silent failure can happen */
  476. ide_pci_setup_ports(dev, d, ret, &idx[0], &hw[0], &hws[0]);
  477. ide_device_add(idx, d, hws);
  478. }
  479. return ret;
  480. }
  481. EXPORT_SYMBOL_GPL(ide_setup_pci_device);
  482. int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
  483. const struct ide_port_info *d)
  484. {
  485. struct pci_dev *pdev[] = { dev1, dev2 };
  486. int ret, i;
  487. hw_regs_t hw[4], *hws[] = { NULL, NULL, NULL, NULL };
  488. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  489. for (i = 0; i < 2; i++) {
  490. ret = do_ide_setup_pci_device(pdev[i], d, !i);
  491. /*
  492. * FIXME: Mom, mom, they stole me the helper function to undo
  493. * do_ide_setup_pci_device() on the first device!
  494. */
  495. if (ret < 0)
  496. goto out;
  497. /* FIXME: silent failure can happen */
  498. ide_pci_setup_ports(pdev[i], d, ret, &idx[i*2], &hw[i*2],
  499. &hws[i*2]);
  500. }
  501. ide_device_add(idx, d, hws);
  502. out:
  503. return ret;
  504. }
  505. EXPORT_SYMBOL_GPL(ide_setup_pci_devices);