setup-pci.c 15 KB

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