pata_optidma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * pata_optidma.c - Opti DMA PATA for new ATA layer
  3. * (C) 2006 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * The Opti DMA controllers are related to the older PIO PCI controllers
  7. * and indeed the VLB ones. The main differences are that the timing
  8. * numbers are now based off PCI clocks not VLB and differ, and that
  9. * MWDMA is supported.
  10. *
  11. * This driver should support Viper-N+, FireStar, FireStar Plus.
  12. *
  13. * These devices support virtual DMA for read (aka the CS5520). Later
  14. * chips support UDMA33, but only if the rest of the board logic does,
  15. * so you have to get this right. We don't support the virtual DMA
  16. * but we do handle UDMA.
  17. *
  18. * Bits that are worth knowing
  19. * Most control registers are shadowed into I/O registers
  20. * 0x1F5 bit 0 tells you if the PCI/VLB clock is 33 or 25Mhz
  21. * Virtual DMA registers *move* between rev 0x02 and rev 0x10
  22. * UDMA requires a 66MHz FSB
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/init.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/delay.h>
  31. #include <scsi/scsi_host.h>
  32. #include <linux/libata.h>
  33. #define DRV_NAME "pata_optidma"
  34. #define DRV_VERSION "0.3.2"
  35. enum {
  36. READ_REG = 0, /* index of Read cycle timing register */
  37. WRITE_REG = 1, /* index of Write cycle timing register */
  38. CNTRL_REG = 3, /* index of Control register */
  39. STRAP_REG = 5, /* index of Strap register */
  40. MISC_REG = 6 /* index of Miscellaneous register */
  41. };
  42. static int pci_clock; /* 0 = 33 1 = 25 */
  43. /**
  44. * optidma_pre_reset - probe begin
  45. * @link: ATA link
  46. * @deadline: deadline jiffies for the operation
  47. *
  48. * Set up cable type and use generic probe init
  49. */
  50. static int optidma_pre_reset(struct ata_link *link, unsigned long deadline)
  51. {
  52. struct ata_port *ap = link->ap;
  53. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  54. static const struct pci_bits optidma_enable_bits = {
  55. 0x40, 1, 0x08, 0x00
  56. };
  57. if (ap->port_no && !pci_test_config_bits(pdev, &optidma_enable_bits))
  58. return -ENOENT;
  59. return ata_std_prereset(link, deadline);
  60. }
  61. /**
  62. * optidma_probe_reset - probe reset
  63. * @ap: ATA port
  64. *
  65. * Perform the ATA probe and bus reset sequence plus specific handling
  66. * for this hardware. The Opti needs little handling - we have no UDMA66
  67. * capability that needs cable detection. All we must do is check the port
  68. * is enabled.
  69. */
  70. static void optidma_error_handler(struct ata_port *ap)
  71. {
  72. ata_bmdma_drive_eh(ap, optidma_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  73. }
  74. /**
  75. * optidma_unlock - unlock control registers
  76. * @ap: ATA port
  77. *
  78. * Unlock the control register block for this adapter. Registers must not
  79. * be unlocked in a situation where libata might look at them.
  80. */
  81. static void optidma_unlock(struct ata_port *ap)
  82. {
  83. void __iomem *regio = ap->ioaddr.cmd_addr;
  84. /* These 3 unlock the control register access */
  85. ioread16(regio + 1);
  86. ioread16(regio + 1);
  87. iowrite8(3, regio + 2);
  88. }
  89. /**
  90. * optidma_lock - issue temporary relock
  91. * @ap: ATA port
  92. *
  93. * Re-lock the configuration register settings.
  94. */
  95. static void optidma_lock(struct ata_port *ap)
  96. {
  97. void __iomem *regio = ap->ioaddr.cmd_addr;
  98. /* Relock */
  99. iowrite8(0x83, regio + 2);
  100. }
  101. /**
  102. * optidma_mode_setup - set mode data
  103. * @ap: ATA interface
  104. * @adev: ATA device
  105. * @mode: Mode to set
  106. *
  107. * Called to do the DMA or PIO mode setup. Timing numbers are all
  108. * pre computed to keep the code clean. There are two tables depending
  109. * on the hardware clock speed.
  110. *
  111. * WARNING: While we do this the IDE registers vanish. If we take an
  112. * IRQ here we depend on the host set locking to avoid catastrophe.
  113. */
  114. static void optidma_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  115. {
  116. struct ata_device *pair = ata_dev_pair(adev);
  117. int pio = adev->pio_mode - XFER_PIO_0;
  118. int dma = adev->dma_mode - XFER_MW_DMA_0;
  119. void __iomem *regio = ap->ioaddr.cmd_addr;
  120. u8 addr;
  121. /* Address table precomputed with a DCLK of 2 */
  122. static const u8 addr_timing[2][5] = {
  123. { 0x30, 0x20, 0x20, 0x10, 0x10 },
  124. { 0x20, 0x20, 0x10, 0x10, 0x10 }
  125. };
  126. static const u8 data_rec_timing[2][5] = {
  127. { 0x59, 0x46, 0x30, 0x20, 0x20 },
  128. { 0x46, 0x32, 0x20, 0x20, 0x10 }
  129. };
  130. static const u8 dma_data_rec_timing[2][3] = {
  131. { 0x76, 0x20, 0x20 },
  132. { 0x54, 0x20, 0x10 }
  133. };
  134. /* Switch from IDE to control mode */
  135. optidma_unlock(ap);
  136. /*
  137. * As with many controllers the address setup time is shared
  138. * and must suit both devices if present. FIXME: Check if we
  139. * need to look at slowest of PIO/DMA mode of either device
  140. */
  141. if (mode >= XFER_MW_DMA_0)
  142. addr = 0;
  143. else
  144. addr = addr_timing[pci_clock][pio];
  145. if (pair) {
  146. u8 pair_addr;
  147. /* Hardware constraint */
  148. if (pair->dma_mode)
  149. pair_addr = 0;
  150. else
  151. pair_addr = addr_timing[pci_clock][pair->pio_mode - XFER_PIO_0];
  152. if (pair_addr > addr)
  153. addr = pair_addr;
  154. }
  155. /* Commence primary programming sequence */
  156. /* First we load the device number into the timing select */
  157. iowrite8(adev->devno, regio + MISC_REG);
  158. /* Now we load the data timings into read data/write data */
  159. if (mode < XFER_MW_DMA_0) {
  160. iowrite8(data_rec_timing[pci_clock][pio], regio + READ_REG);
  161. iowrite8(data_rec_timing[pci_clock][pio], regio + WRITE_REG);
  162. } else if (mode < XFER_UDMA_0) {
  163. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + READ_REG);
  164. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + WRITE_REG);
  165. }
  166. /* Finally we load the address setup into the misc register */
  167. iowrite8(addr | adev->devno, regio + MISC_REG);
  168. /* Programming sequence complete, timing 0 dev 0, timing 1 dev 1 */
  169. iowrite8(0x85, regio + CNTRL_REG);
  170. /* Switch back to IDE mode */
  171. optidma_lock(ap);
  172. /* Note: at this point our programming is incomplete. We are
  173. not supposed to program PCI 0x43 "things we hacked onto the chip"
  174. until we've done both sets of PIO/DMA timings */
  175. }
  176. /**
  177. * optiplus_mode_setup - DMA setup for Firestar Plus
  178. * @ap: ATA port
  179. * @adev: device
  180. * @mode: desired mode
  181. *
  182. * The Firestar plus has additional UDMA functionality for UDMA0-2 and
  183. * requires we do some additional work. Because the base work we must do
  184. * is mostly shared we wrap the Firestar setup functionality in this
  185. * one
  186. */
  187. static void optiplus_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  188. {
  189. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  190. u8 udcfg;
  191. u8 udslave;
  192. int dev2 = 2 * adev->devno;
  193. int unit = 2 * ap->port_no + adev->devno;
  194. int udma = mode - XFER_UDMA_0;
  195. pci_read_config_byte(pdev, 0x44, &udcfg);
  196. if (mode <= XFER_UDMA_0) {
  197. udcfg &= ~(1 << unit);
  198. optidma_mode_setup(ap, adev, adev->dma_mode);
  199. } else {
  200. udcfg |= (1 << unit);
  201. if (ap->port_no) {
  202. pci_read_config_byte(pdev, 0x45, &udslave);
  203. udslave &= ~(0x03 << dev2);
  204. udslave |= (udma << dev2);
  205. pci_write_config_byte(pdev, 0x45, udslave);
  206. } else {
  207. udcfg &= ~(0x30 << dev2);
  208. udcfg |= (udma << dev2);
  209. }
  210. }
  211. pci_write_config_byte(pdev, 0x44, udcfg);
  212. }
  213. /**
  214. * optidma_set_pio_mode - PIO setup callback
  215. * @ap: ATA port
  216. * @adev: Device
  217. *
  218. * The libata core provides separate functions for handling PIO and
  219. * DMA programming. The architecture of the Firestar makes it easier
  220. * for us to have a common function so we provide wrappers
  221. */
  222. static void optidma_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  223. {
  224. optidma_mode_setup(ap, adev, adev->pio_mode);
  225. }
  226. /**
  227. * optidma_set_dma_mode - DMA setup callback
  228. * @ap: ATA port
  229. * @adev: Device
  230. *
  231. * The libata core provides separate functions for handling PIO and
  232. * DMA programming. The architecture of the Firestar makes it easier
  233. * for us to have a common function so we provide wrappers
  234. */
  235. static void optidma_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  236. {
  237. optidma_mode_setup(ap, adev, adev->dma_mode);
  238. }
  239. /**
  240. * optiplus_set_pio_mode - PIO setup callback
  241. * @ap: ATA port
  242. * @adev: Device
  243. *
  244. * The libata core provides separate functions for handling PIO and
  245. * DMA programming. The architecture of the Firestar makes it easier
  246. * for us to have a common function so we provide wrappers
  247. */
  248. static void optiplus_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  249. {
  250. optiplus_mode_setup(ap, adev, adev->pio_mode);
  251. }
  252. /**
  253. * optiplus_set_dma_mode - DMA setup callback
  254. * @ap: ATA port
  255. * @adev: Device
  256. *
  257. * The libata core provides separate functions for handling PIO and
  258. * DMA programming. The architecture of the Firestar makes it easier
  259. * for us to have a common function so we provide wrappers
  260. */
  261. static void optiplus_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  262. {
  263. optiplus_mode_setup(ap, adev, adev->dma_mode);
  264. }
  265. /**
  266. * optidma_make_bits - PCI setup helper
  267. * @adev: ATA device
  268. *
  269. * Turn the ATA device setup into PCI configuration bits
  270. * for register 0x43 and return the two bits needed.
  271. */
  272. static u8 optidma_make_bits43(struct ata_device *adev)
  273. {
  274. static const u8 bits43[5] = {
  275. 0, 0, 0, 1, 2
  276. };
  277. if (!ata_dev_enabled(adev))
  278. return 0;
  279. if (adev->dma_mode)
  280. return adev->dma_mode - XFER_MW_DMA_0;
  281. return bits43[adev->pio_mode - XFER_PIO_0];
  282. }
  283. /**
  284. * optidma_set_mode - mode setup
  285. * @link: link to set up
  286. *
  287. * Use the standard setup to tune the chipset and then finalise the
  288. * configuration by writing the nibble of extra bits of data into
  289. * the chip.
  290. */
  291. static int optidma_set_mode(struct ata_link *link, struct ata_device **r_failed)
  292. {
  293. struct ata_port *ap = link->ap;
  294. u8 r;
  295. int nybble = 4 * ap->port_no;
  296. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  297. int rc = ata_do_set_mode(link, r_failed);
  298. if (rc == 0) {
  299. pci_read_config_byte(pdev, 0x43, &r);
  300. r &= (0x0F << nybble);
  301. r |= (optidma_make_bits43(&link->device[0]) +
  302. (optidma_make_bits43(&link->device[0]) << 2)) << nybble;
  303. pci_write_config_byte(pdev, 0x43, r);
  304. }
  305. return rc;
  306. }
  307. static struct scsi_host_template optidma_sht = {
  308. .module = THIS_MODULE,
  309. .name = DRV_NAME,
  310. .ioctl = ata_scsi_ioctl,
  311. .queuecommand = ata_scsi_queuecmd,
  312. .can_queue = ATA_DEF_QUEUE,
  313. .this_id = ATA_SHT_THIS_ID,
  314. .sg_tablesize = LIBATA_MAX_PRD,
  315. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  316. .emulated = ATA_SHT_EMULATED,
  317. .use_clustering = ATA_SHT_USE_CLUSTERING,
  318. .proc_name = DRV_NAME,
  319. .dma_boundary = ATA_DMA_BOUNDARY,
  320. .slave_configure = ata_scsi_slave_config,
  321. .slave_destroy = ata_scsi_slave_destroy,
  322. .bios_param = ata_std_bios_param,
  323. };
  324. static struct ata_port_operations optidma_port_ops = {
  325. .set_piomode = optidma_set_pio_mode,
  326. .set_dmamode = optidma_set_dma_mode,
  327. .tf_load = ata_tf_load,
  328. .tf_read = ata_tf_read,
  329. .check_status = ata_check_status,
  330. .exec_command = ata_exec_command,
  331. .dev_select = ata_std_dev_select,
  332. .freeze = ata_bmdma_freeze,
  333. .thaw = ata_bmdma_thaw,
  334. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  335. .error_handler = optidma_error_handler,
  336. .set_mode = optidma_set_mode,
  337. .cable_detect = ata_cable_40wire,
  338. .bmdma_setup = ata_bmdma_setup,
  339. .bmdma_start = ata_bmdma_start,
  340. .bmdma_stop = ata_bmdma_stop,
  341. .bmdma_status = ata_bmdma_status,
  342. .qc_prep = ata_qc_prep,
  343. .qc_issue = ata_qc_issue_prot,
  344. .data_xfer = ata_data_xfer,
  345. .irq_handler = ata_interrupt,
  346. .irq_clear = ata_bmdma_irq_clear,
  347. .irq_on = ata_irq_on,
  348. .port_start = ata_sff_port_start,
  349. };
  350. static struct ata_port_operations optiplus_port_ops = {
  351. .set_piomode = optiplus_set_pio_mode,
  352. .set_dmamode = optiplus_set_dma_mode,
  353. .tf_load = ata_tf_load,
  354. .tf_read = ata_tf_read,
  355. .check_status = ata_check_status,
  356. .exec_command = ata_exec_command,
  357. .dev_select = ata_std_dev_select,
  358. .freeze = ata_bmdma_freeze,
  359. .thaw = ata_bmdma_thaw,
  360. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  361. .error_handler = optidma_error_handler,
  362. .set_mode = optidma_set_mode,
  363. .cable_detect = ata_cable_40wire,
  364. .bmdma_setup = ata_bmdma_setup,
  365. .bmdma_start = ata_bmdma_start,
  366. .bmdma_stop = ata_bmdma_stop,
  367. .bmdma_status = ata_bmdma_status,
  368. .qc_prep = ata_qc_prep,
  369. .qc_issue = ata_qc_issue_prot,
  370. .data_xfer = ata_data_xfer,
  371. .irq_handler = ata_interrupt,
  372. .irq_clear = ata_bmdma_irq_clear,
  373. .irq_on = ata_irq_on,
  374. .port_start = ata_sff_port_start,
  375. };
  376. /**
  377. * optiplus_with_udma - Look for UDMA capable setup
  378. * @pdev; ATA controller
  379. */
  380. static int optiplus_with_udma(struct pci_dev *pdev)
  381. {
  382. u8 r;
  383. int ret = 0;
  384. int ioport = 0x22;
  385. struct pci_dev *dev1;
  386. /* Find function 1 */
  387. dev1 = pci_get_device(0x1045, 0xC701, NULL);
  388. if(dev1 == NULL)
  389. return 0;
  390. /* Rev must be >= 0x10 */
  391. pci_read_config_byte(dev1, 0x08, &r);
  392. if (r < 0x10)
  393. goto done_nomsg;
  394. /* Read the chipset system configuration to check our mode */
  395. pci_read_config_byte(dev1, 0x5F, &r);
  396. ioport |= (r << 8);
  397. outb(0x10, ioport);
  398. /* Must be 66Mhz sync */
  399. if ((inb(ioport + 2) & 1) == 0)
  400. goto done;
  401. /* Check the ATA arbitration/timing is suitable */
  402. pci_read_config_byte(pdev, 0x42, &r);
  403. if ((r & 0x36) != 0x36)
  404. goto done;
  405. pci_read_config_byte(dev1, 0x52, &r);
  406. if (r & 0x80) /* IDEDIR disabled */
  407. ret = 1;
  408. done:
  409. printk(KERN_WARNING "UDMA not supported in this configuration.\n");
  410. done_nomsg: /* Wrong chip revision */
  411. pci_dev_put(dev1);
  412. return ret;
  413. }
  414. static int optidma_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  415. {
  416. static const struct ata_port_info info_82c700 = {
  417. .sht = &optidma_sht,
  418. .flags = ATA_FLAG_SLAVE_POSS,
  419. .pio_mask = 0x1f,
  420. .mwdma_mask = 0x07,
  421. .port_ops = &optidma_port_ops
  422. };
  423. static const struct ata_port_info info_82c700_udma = {
  424. .sht = &optidma_sht,
  425. .flags = ATA_FLAG_SLAVE_POSS,
  426. .pio_mask = 0x1f,
  427. .mwdma_mask = 0x07,
  428. .udma_mask = 0x07,
  429. .port_ops = &optiplus_port_ops
  430. };
  431. const struct ata_port_info *ppi[] = { &info_82c700, NULL };
  432. static int printed_version;
  433. if (!printed_version++)
  434. dev_printk(KERN_DEBUG, &dev->dev, "version " DRV_VERSION "\n");
  435. /* Fixed location chipset magic */
  436. inw(0x1F1);
  437. inw(0x1F1);
  438. pci_clock = inb(0x1F5) & 1; /* 0 = 33Mhz, 1 = 25Mhz */
  439. if (optiplus_with_udma(dev))
  440. ppi[0] = &info_82c700_udma;
  441. return ata_pci_init_one(dev, ppi);
  442. }
  443. static const struct pci_device_id optidma[] = {
  444. { PCI_VDEVICE(OPTI, 0xD568), }, /* Opti 82C700 */
  445. { },
  446. };
  447. static struct pci_driver optidma_pci_driver = {
  448. .name = DRV_NAME,
  449. .id_table = optidma,
  450. .probe = optidma_init_one,
  451. .remove = ata_pci_remove_one,
  452. #ifdef CONFIG_PM
  453. .suspend = ata_pci_device_suspend,
  454. .resume = ata_pci_device_resume,
  455. #endif
  456. };
  457. static int __init optidma_init(void)
  458. {
  459. return pci_register_driver(&optidma_pci_driver);
  460. }
  461. static void __exit optidma_exit(void)
  462. {
  463. pci_unregister_driver(&optidma_pci_driver);
  464. }
  465. MODULE_AUTHOR("Alan Cox");
  466. MODULE_DESCRIPTION("low-level driver for Opti Firestar/Firestar Plus");
  467. MODULE_LICENSE("GPL");
  468. MODULE_DEVICE_TABLE(pci, optidma);
  469. MODULE_VERSION(DRV_VERSION);
  470. module_init(optidma_init);
  471. module_exit(optidma_exit);