pata_optidma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.2.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. ap->cbl = ATA_CBL_PATA40;
  58. return ata_std_prereset(ap);
  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. unsigned long regio = ap->ioaddr.cmd_addr;
  83. /* These 3 unlock the control register access */
  84. inw(regio + 1);
  85. inw(regio + 1);
  86. outb(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. unsigned long regio = ap->ioaddr.cmd_addr;
  97. /* Relock */
  98. outb(0x83, regio + 2);
  99. }
  100. /**
  101. * optidma_set_mode - 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_set_mode(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. unsigned long 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. outb(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. outb(data_rec_timing[pci_clock][pio], regio + READ_REG);
  160. outb(data_rec_timing[pci_clock][pio], regio + WRITE_REG);
  161. } else if (mode < XFER_UDMA_0) {
  162. outb(dma_data_rec_timing[pci_clock][dma], regio + READ_REG);
  163. outb(dma_data_rec_timing[pci_clock][dma], regio + WRITE_REG);
  164. }
  165. /* Finally we load the address setup into the misc register */
  166. outb(addr | adev->devno, regio + MISC_REG);
  167. /* Programming sequence complete, timing 0 dev 0, timing 1 dev 1 */
  168. outb(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_set_mode - 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_set_mode(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_set_mode(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_set_mode(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_set_mode(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_set_mode(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_set_mode(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_post_set_mode - finalize PCI setup
  284. * @ap: port to set up
  285. *
  286. * Finalise the configuration by writing the nibble of extra bits
  287. * of data into the chip.
  288. */
  289. static void optidma_post_set_mode(struct ata_port *ap)
  290. {
  291. u8 r;
  292. int nybble = 4 * ap->port_no;
  293. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  294. pci_read_config_byte(pdev, 0x43, &r);
  295. r &= (0x0F << nybble);
  296. r |= (optidma_make_bits43(&ap->device[0]) +
  297. (optidma_make_bits43(&ap->device[0]) << 2)) << nybble;
  298. pci_write_config_byte(pdev, 0x43, r);
  299. }
  300. static struct scsi_host_template optidma_sht = {
  301. .module = THIS_MODULE,
  302. .name = DRV_NAME,
  303. .ioctl = ata_scsi_ioctl,
  304. .queuecommand = ata_scsi_queuecmd,
  305. .can_queue = ATA_DEF_QUEUE,
  306. .this_id = ATA_SHT_THIS_ID,
  307. .sg_tablesize = LIBATA_MAX_PRD,
  308. .max_sectors = ATA_MAX_SECTORS,
  309. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  310. .emulated = ATA_SHT_EMULATED,
  311. .use_clustering = ATA_SHT_USE_CLUSTERING,
  312. .proc_name = DRV_NAME,
  313. .dma_boundary = ATA_DMA_BOUNDARY,
  314. .slave_configure = ata_scsi_slave_config,
  315. .bios_param = ata_std_bios_param,
  316. };
  317. static struct ata_port_operations optidma_port_ops = {
  318. .port_disable = ata_port_disable,
  319. .set_piomode = optidma_set_pio_mode,
  320. .set_dmamode = optidma_set_dma_mode,
  321. .tf_load = ata_tf_load,
  322. .tf_read = ata_tf_read,
  323. .check_status = ata_check_status,
  324. .exec_command = ata_exec_command,
  325. .dev_select = ata_std_dev_select,
  326. .freeze = ata_bmdma_freeze,
  327. .thaw = ata_bmdma_thaw,
  328. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  329. .error_handler = optidma_error_handler,
  330. .post_set_mode = optidma_post_set_mode,
  331. .bmdma_setup = ata_bmdma_setup,
  332. .bmdma_start = ata_bmdma_start,
  333. .bmdma_stop = ata_bmdma_stop,
  334. .bmdma_status = ata_bmdma_status,
  335. .qc_prep = ata_qc_prep,
  336. .qc_issue = ata_qc_issue_prot,
  337. .data_xfer = ata_pio_data_xfer,
  338. .irq_handler = ata_interrupt,
  339. .irq_clear = ata_bmdma_irq_clear,
  340. .port_start = ata_port_start,
  341. .port_stop = ata_port_stop,
  342. .host_stop = ata_host_stop
  343. };
  344. static struct ata_port_operations optiplus_port_ops = {
  345. .port_disable = ata_port_disable,
  346. .set_piomode = optiplus_set_pio_mode,
  347. .set_dmamode = optiplus_set_dma_mode,
  348. .tf_load = ata_tf_load,
  349. .tf_read = ata_tf_read,
  350. .check_status = ata_check_status,
  351. .exec_command = ata_exec_command,
  352. .dev_select = ata_std_dev_select,
  353. .freeze = ata_bmdma_freeze,
  354. .thaw = ata_bmdma_thaw,
  355. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  356. .error_handler = optidma_error_handler,
  357. .post_set_mode = optidma_post_set_mode,
  358. .bmdma_setup = ata_bmdma_setup,
  359. .bmdma_start = ata_bmdma_start,
  360. .bmdma_stop = ata_bmdma_stop,
  361. .bmdma_status = ata_bmdma_status,
  362. .qc_prep = ata_qc_prep,
  363. .qc_issue = ata_qc_issue_prot,
  364. .data_xfer = ata_pio_data_xfer,
  365. .irq_handler = ata_interrupt,
  366. .irq_clear = ata_bmdma_irq_clear,
  367. .port_start = ata_port_start,
  368. .port_stop = ata_port_stop,
  369. .host_stop = ata_host_stop
  370. };
  371. /**
  372. * optiplus_with_udma - Look for UDMA capable setup
  373. * @pdev; ATA controller
  374. */
  375. static int optiplus_with_udma(struct pci_dev *pdev)
  376. {
  377. u8 r;
  378. int ret = 0;
  379. int ioport = 0x22;
  380. struct pci_dev *dev1;
  381. /* Find function 1 */
  382. dev1 = pci_get_device(0x1045, 0xC701, NULL);
  383. if(dev1 == NULL)
  384. return 0;
  385. /* Rev must be >= 0x10 */
  386. pci_read_config_byte(dev1, 0x08, &r);
  387. if (r < 0x10)
  388. goto done_nomsg;
  389. /* Read the chipset system configuration to check our mode */
  390. pci_read_config_byte(dev1, 0x5F, &r);
  391. ioport |= (r << 8);
  392. outb(0x10, ioport);
  393. /* Must be 66Mhz sync */
  394. if ((inb(ioport + 2) & 1) == 0)
  395. goto done;
  396. /* Check the ATA arbitration/timing is suitable */
  397. pci_read_config_byte(pdev, 0x42, &r);
  398. if ((r & 0x36) != 0x36)
  399. goto done;
  400. pci_read_config_byte(dev1, 0x52, &r);
  401. if (r & 0x80) /* IDEDIR disabled */
  402. ret = 1;
  403. done:
  404. printk(KERN_WARNING "UDMA not supported in this configuration.\n");
  405. done_nomsg: /* Wrong chip revision */
  406. pci_dev_put(dev1);
  407. return ret;
  408. }
  409. static int optidma_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  410. {
  411. static struct ata_port_info info_82c700 = {
  412. .sht = &optidma_sht,
  413. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  414. .pio_mask = 0x1f,
  415. .mwdma_mask = 0x07,
  416. .port_ops = &optidma_port_ops
  417. };
  418. static struct ata_port_info info_82c700_udma = {
  419. .sht = &optidma_sht,
  420. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  421. .pio_mask = 0x1f,
  422. .mwdma_mask = 0x07,
  423. .udma_mask = 0x07,
  424. .port_ops = &optiplus_port_ops
  425. };
  426. static struct ata_port_info *port_info[2];
  427. struct ata_port_info *info = &info_82c700;
  428. static int printed_version;
  429. if (!printed_version++)
  430. dev_printk(KERN_DEBUG, &dev->dev, "version " DRV_VERSION "\n");
  431. /* Fixed location chipset magic */
  432. inw(0x1F1);
  433. inw(0x1F1);
  434. pci_clock = inb(0x1F5) & 1; /* 0 = 33Mhz, 1 = 25Mhz */
  435. if (optiplus_with_udma(dev))
  436. info = &info_82c700_udma;
  437. port_info[0] = port_info[1] = info;
  438. return ata_pci_init_one(dev, port_info, 2);
  439. }
  440. static const struct pci_device_id optidma[] = {
  441. { PCI_VDEVICE(OPTI, 0xD568), }, /* Opti 82C700 */
  442. { },
  443. };
  444. static struct pci_driver optidma_pci_driver = {
  445. .name = DRV_NAME,
  446. .id_table = optidma,
  447. .probe = optidma_init_one,
  448. .remove = ata_pci_remove_one
  449. };
  450. static int __init optidma_init(void)
  451. {
  452. return pci_register_driver(&optidma_pci_driver);
  453. }
  454. static void __exit optidma_exit(void)
  455. {
  456. pci_unregister_driver(&optidma_pci_driver);
  457. }
  458. MODULE_AUTHOR("Alan Cox");
  459. MODULE_DESCRIPTION("low-level driver for Opti Firestar/Firestar Plus");
  460. MODULE_LICENSE("GPL");
  461. MODULE_DEVICE_TABLE(pci, optidma);
  462. MODULE_VERSION(DRV_VERSION);
  463. module_init(optidma_init);
  464. module_exit(optidma_exit);