pata_optidma.c 14 KB

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