setup-pci.c 16 KB

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