pata_jmicron.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * pata_jmicron.c - JMicron ATA driver for non AHCI mode. This drives the
  3. * PATA port of the controller. The SATA ports are
  4. * driven by AHCI in the usual configuration although
  5. * this driver can handle other setups if we need it.
  6. *
  7. * (c) 2006 Red Hat <alan@redhat.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/init.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <linux/libata.h>
  18. #include <linux/ata.h>
  19. #define DRV_NAME "pata_jmicron"
  20. #define DRV_VERSION "0.1.4"
  21. typedef enum {
  22. PORT_PATA0 = 0,
  23. PORT_PATA1 = 1,
  24. PORT_SATA = 2,
  25. } port_type;
  26. /**
  27. * jmicron_pre_reset - check for 40/80 pin
  28. * @ap: Port
  29. *
  30. * Perform the PATA port setup we need.
  31. * On the Jmicron 361/363 there is a single PATA port that can be mapped
  32. * either as primary or secondary (or neither). We don't do any policy
  33. * and setup here. We assume that has been done by init_one and the
  34. * BIOS.
  35. */
  36. static int jmicron_pre_reset(struct ata_port *ap)
  37. {
  38. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  39. u32 control;
  40. u32 control5;
  41. int port_mask = 1<< (4 * ap->port_no);
  42. int port = ap->port_no;
  43. port_type port_map[2];
  44. /* Check if our port is enabled */
  45. pci_read_config_dword(pdev, 0x40, &control);
  46. if ((control & port_mask) == 0)
  47. return -ENOENT;
  48. /* There are two basic mappings. One has the two SATA ports merged
  49. as master/slave and the secondary as PATA, the other has only the
  50. SATA port mapped */
  51. if (control & (1 << 23)) {
  52. port_map[0] = PORT_SATA;
  53. port_map[1] = PORT_PATA0;
  54. } else {
  55. port_map[0] = PORT_SATA;
  56. port_map[1] = PORT_SATA;
  57. }
  58. /* The 365/366 may have this bit set to map the second PATA port
  59. as the internal primary channel */
  60. pci_read_config_dword(pdev, 0x80, &control5);
  61. if (control5 & (1<<24))
  62. port_map[0] = PORT_PATA1;
  63. /* The two ports may then be logically swapped by the firmware */
  64. if (control & (1 << 22))
  65. port = port ^ 1;
  66. /*
  67. * Now we know which physical port we are talking about we can
  68. * actually do our cable checking etc. Thankfully we don't need
  69. * to do the plumbing for other cases.
  70. */
  71. switch (port_map[port])
  72. {
  73. case PORT_PATA0:
  74. if (control & (1 << 5))
  75. return 0;
  76. if (control & (1 << 3)) /* 40/80 pin primary */
  77. ap->cbl = ATA_CBL_PATA40;
  78. else
  79. ap->cbl = ATA_CBL_PATA80;
  80. break;
  81. case PORT_PATA1:
  82. /* Bit 21 is set if the port is enabled */
  83. if ((control5 & (1 << 21)) == 0)
  84. return 0;
  85. if (control5 & (1 << 19)) /* 40/80 pin secondary */
  86. ap->cbl = ATA_CBL_PATA40;
  87. else
  88. ap->cbl = ATA_CBL_PATA80;
  89. break;
  90. case PORT_SATA:
  91. ap->cbl = ATA_CBL_SATA;
  92. break;
  93. }
  94. return ata_std_prereset(ap);
  95. }
  96. /**
  97. * jmicron_error_handler - Setup and error handler
  98. * @ap: Port to handle
  99. *
  100. * LOCKING:
  101. * None (inherited from caller).
  102. */
  103. static void jmicron_error_handler(struct ata_port *ap)
  104. {
  105. return ata_bmdma_drive_eh(ap, jmicron_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  106. }
  107. /* No PIO or DMA methods needed for this device */
  108. static struct scsi_host_template jmicron_sht = {
  109. .module = THIS_MODULE,
  110. .name = DRV_NAME,
  111. .ioctl = ata_scsi_ioctl,
  112. .queuecommand = ata_scsi_queuecmd,
  113. .can_queue = ATA_DEF_QUEUE,
  114. .this_id = ATA_SHT_THIS_ID,
  115. .sg_tablesize = LIBATA_MAX_PRD,
  116. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  117. .emulated = ATA_SHT_EMULATED,
  118. .use_clustering = ATA_SHT_USE_CLUSTERING,
  119. .proc_name = DRV_NAME,
  120. .dma_boundary = ATA_DMA_BOUNDARY,
  121. .slave_configure = ata_scsi_slave_config,
  122. .slave_destroy = ata_scsi_slave_destroy,
  123. /* Use standard CHS mapping rules */
  124. .bios_param = ata_std_bios_param,
  125. };
  126. static const struct ata_port_operations jmicron_ops = {
  127. .port_disable = ata_port_disable,
  128. /* Task file is PCI ATA format, use helpers */
  129. .tf_load = ata_tf_load,
  130. .tf_read = ata_tf_read,
  131. .check_status = ata_check_status,
  132. .exec_command = ata_exec_command,
  133. .dev_select = ata_std_dev_select,
  134. .freeze = ata_bmdma_freeze,
  135. .thaw = ata_bmdma_thaw,
  136. .error_handler = jmicron_error_handler,
  137. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  138. /* BMDMA handling is PCI ATA format, use helpers */
  139. .bmdma_setup = ata_bmdma_setup,
  140. .bmdma_start = ata_bmdma_start,
  141. .bmdma_stop = ata_bmdma_stop,
  142. .bmdma_status = ata_bmdma_status,
  143. .qc_prep = ata_qc_prep,
  144. .qc_issue = ata_qc_issue_prot,
  145. .data_xfer = ata_data_xfer,
  146. /* IRQ-related hooks */
  147. .irq_handler = ata_interrupt,
  148. .irq_clear = ata_bmdma_irq_clear,
  149. .irq_on = ata_irq_on,
  150. .irq_ack = ata_irq_ack,
  151. /* Generic PATA PCI ATA helpers */
  152. .port_start = ata_port_start,
  153. };
  154. /**
  155. * jmicron_init_one - Register Jmicron ATA PCI device with kernel services
  156. * @pdev: PCI device to register
  157. * @ent: Entry in jmicron_pci_tbl matching with @pdev
  158. *
  159. * Called from kernel PCI layer.
  160. *
  161. * LOCKING:
  162. * Inherited from PCI layer (may sleep).
  163. *
  164. * RETURNS:
  165. * Zero on success, or -ERRNO value.
  166. */
  167. static int jmicron_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  168. {
  169. static struct ata_port_info info = {
  170. .sht = &jmicron_sht,
  171. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  172. .pio_mask = 0x1f,
  173. .mwdma_mask = 0x07,
  174. .udma_mask = 0x3f,
  175. .port_ops = &jmicron_ops,
  176. };
  177. struct ata_port_info *port_info[2] = { &info, &info };
  178. u32 reg;
  179. /* PATA controller is fn 1, AHCI is fn 0 */
  180. if (id->driver_data != 368 && PCI_FUNC(pdev->devfn) != 1)
  181. return -ENODEV;
  182. /* The 365/66 have two PATA channels, redirect the second */
  183. if (id->driver_data == 365 || id->driver_data == 366) {
  184. pci_read_config_dword(pdev, 0x80, &reg);
  185. reg |= (1 << 24); /* IDE1 to PATA IDE secondary */
  186. pci_write_config_dword(pdev, 0x80, reg);
  187. }
  188. return ata_pci_init_one(pdev, port_info, 2);
  189. }
  190. static int jmicron_reinit_one(struct pci_dev *pdev)
  191. {
  192. u32 reg;
  193. switch(pdev->device) {
  194. case PCI_DEVICE_ID_JMICRON_JMB368:
  195. break;
  196. case PCI_DEVICE_ID_JMICRON_JMB365:
  197. case PCI_DEVICE_ID_JMICRON_JMB366:
  198. /* Restore mapping or disks swap and boy does it get ugly */
  199. pci_read_config_dword(pdev, 0x80, &reg);
  200. reg |= (1 << 24); /* IDE1 to PATA IDE secondary */
  201. pci_write_config_dword(pdev, 0x80, reg);
  202. /* Fall through */
  203. default:
  204. /* Make sure AHCI is turned back on */
  205. pci_write_config_byte(pdev, 0x41, 0xa1);
  206. }
  207. return ata_pci_device_resume(pdev);
  208. }
  209. static const struct pci_device_id jmicron_pci_tbl[] = {
  210. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB361), 361},
  211. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB363), 363},
  212. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB365), 365},
  213. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB366), 366},
  214. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB368), 368},
  215. { } /* terminate list */
  216. };
  217. static struct pci_driver jmicron_pci_driver = {
  218. .name = DRV_NAME,
  219. .id_table = jmicron_pci_tbl,
  220. .probe = jmicron_init_one,
  221. .remove = ata_pci_remove_one,
  222. .suspend = ata_pci_device_suspend,
  223. .resume = jmicron_reinit_one,
  224. };
  225. static int __init jmicron_init(void)
  226. {
  227. return pci_register_driver(&jmicron_pci_driver);
  228. }
  229. static void __exit jmicron_exit(void)
  230. {
  231. pci_unregister_driver(&jmicron_pci_driver);
  232. }
  233. module_init(jmicron_init);
  234. module_exit(jmicron_exit);
  235. MODULE_AUTHOR("Alan Cox");
  236. MODULE_DESCRIPTION("SCSI low-level driver for Jmicron PATA ports");
  237. MODULE_LICENSE("GPL");
  238. MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
  239. MODULE_VERSION(DRV_VERSION);