pata_optidma.c 14 KB

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