pata_optidma.c 14 KB

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