jmicron.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2006 Red Hat <alan@redhat.com>
  3. *
  4. * May be copied or modified under the terms of the GNU General Public License
  5. */
  6. #include <linux/types.h>
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include <linux/delay.h>
  10. #include <linux/hdreg.h>
  11. #include <linux/ide.h>
  12. #include <linux/init.h>
  13. #include <asm/io.h>
  14. typedef enum {
  15. PORT_PATA0 = 0,
  16. PORT_PATA1 = 1,
  17. PORT_SATA = 2,
  18. } port_type;
  19. /**
  20. * ata66_jmicron - Cable check
  21. * @hwif: IDE port
  22. *
  23. * Return 1 if the cable is 80pin
  24. */
  25. static int __devinit ata66_jmicron(ide_hwif_t *hwif)
  26. {
  27. struct pci_dev *pdev = hwif->pci_dev;
  28. u32 control;
  29. u32 control5;
  30. int port = hwif->channel;
  31. port_type port_map[2];
  32. pci_read_config_dword(pdev, 0x40, &control);
  33. /* There are two basic mappings. One has the two SATA ports merged
  34. as master/slave and the secondary as PATA, the other has only the
  35. SATA port mapped */
  36. if (control & (1 << 23)) {
  37. port_map[0] = PORT_SATA;
  38. port_map[1] = PORT_PATA0;
  39. } else {
  40. port_map[0] = PORT_SATA;
  41. port_map[1] = PORT_SATA;
  42. }
  43. /* The 365/366 may have this bit set to map the second PATA port
  44. as the internal primary channel */
  45. pci_read_config_dword(pdev, 0x80, &control5);
  46. if (control5 & (1<<24))
  47. port_map[0] = PORT_PATA1;
  48. /* The two ports may then be logically swapped by the firmware */
  49. if (control & (1 << 22))
  50. port = port ^ 1;
  51. /*
  52. * Now we know which physical port we are talking about we can
  53. * actually do our cable checking etc. Thankfully we don't need
  54. * to do the plumbing for other cases.
  55. */
  56. switch (port_map[port])
  57. {
  58. case PORT_PATA0:
  59. if (control & (1 << 3)) /* 40/80 pin primary */
  60. return 0;
  61. return 1;
  62. case PORT_PATA1:
  63. if (control5 & (1 << 19)) /* 40/80 pin secondary */
  64. return 0;
  65. return 1;
  66. case PORT_SATA:
  67. break;
  68. }
  69. return 1; /* Avoid bogus "control reaches end of non-void function" */
  70. }
  71. static void jmicron_tuneproc (ide_drive_t *drive, byte mode_wanted)
  72. {
  73. return;
  74. }
  75. /**
  76. * config_jmicron_chipset_for_pio - set drive timings
  77. * @drive: drive to tune
  78. * @speed we want
  79. *
  80. */
  81. static void config_jmicron_chipset_for_pio (ide_drive_t *drive, byte set_speed)
  82. {
  83. u8 speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, 5, NULL);
  84. if (set_speed)
  85. (void) ide_config_drive_speed(drive, speed);
  86. }
  87. /**
  88. * jmicron_tune_chipset - set controller timings
  89. * @drive: Drive to set up
  90. * @xferspeed: speed we want to achieve
  91. *
  92. * As the JMicron snoops for timings all we actually need to do is
  93. * make sure we don't set an invalid mode. We do need to honour
  94. * the cable detect here.
  95. */
  96. static int jmicron_tune_chipset (ide_drive_t *drive, byte xferspeed)
  97. {
  98. u8 speed = ide_rate_filter(drive, xferspeed);
  99. return ide_config_drive_speed(drive, speed);
  100. }
  101. /**
  102. * config_chipset_for_dma - configure for DMA
  103. * @drive: drive to configure
  104. *
  105. * As the JMicron snoops for timings all we actually need to do is
  106. * make sure we don't set an invalid mode.
  107. */
  108. static int config_chipset_for_dma (ide_drive_t *drive)
  109. {
  110. u8 speed = ide_max_dma_mode(drive);
  111. if (!speed)
  112. return 0;
  113. jmicron_tune_chipset(drive, speed);
  114. return ide_dma_enable(drive);
  115. }
  116. /**
  117. * jmicron_configure_drive_for_dma - set up for DMA transfers
  118. * @drive: drive we are going to set up
  119. *
  120. * As the JMicron snoops for timings all we actually need to do is
  121. * make sure we don't set an invalid mode.
  122. */
  123. static int jmicron_config_drive_for_dma (ide_drive_t *drive)
  124. {
  125. if (ide_use_dma(drive) && config_chipset_for_dma(drive))
  126. return 0;
  127. config_jmicron_chipset_for_pio(drive, 1);
  128. return -1;
  129. }
  130. /**
  131. * init_hwif_jmicron - set up hwif structs
  132. * @hwif: interface to set up
  133. *
  134. * Minimal set up is required for the Jmicron hardware.
  135. */
  136. static void __devinit init_hwif_jmicron(ide_hwif_t *hwif)
  137. {
  138. hwif->speedproc = &jmicron_tune_chipset;
  139. hwif->tuneproc = &jmicron_tuneproc;
  140. hwif->drives[0].autotune = 1;
  141. hwif->drives[1].autotune = 1;
  142. if (!hwif->dma_base)
  143. goto fallback;
  144. hwif->atapi_dma = 1;
  145. hwif->ultra_mask = 0x7f;
  146. hwif->mwdma_mask = 0x07;
  147. hwif->ide_dma_check = &jmicron_config_drive_for_dma;
  148. if (!(hwif->udma_four))
  149. hwif->udma_four = ata66_jmicron(hwif);
  150. hwif->autodma = 1;
  151. hwif->drives[0].autodma = hwif->autodma;
  152. hwif->drives[1].autodma = hwif->autodma;
  153. return;
  154. fallback:
  155. hwif->autodma = 0;
  156. return;
  157. }
  158. #define DECLARE_JMB_DEV(name_str) \
  159. { \
  160. .name = name_str, \
  161. .init_hwif = init_hwif_jmicron, \
  162. .channels = 2, \
  163. .autodma = AUTODMA, \
  164. .bootable = ON_BOARD, \
  165. .enablebits = { {0x40, 1, 1}, {0x40, 0x10, 0x10} }, \
  166. }
  167. static ide_pci_device_t jmicron_chipsets[] __devinitdata = {
  168. /* 0 */ DECLARE_JMB_DEV("JMB361"),
  169. /* 1 */ DECLARE_JMB_DEV("JMB363"),
  170. /* 2 */ DECLARE_JMB_DEV("JMB365"),
  171. /* 3 */ DECLARE_JMB_DEV("JMB366"),
  172. /* 4 */ DECLARE_JMB_DEV("JMB368"),
  173. };
  174. /**
  175. * jmicron_init_one - pci layer discovery entry
  176. * @dev: PCI device
  177. * @id: ident table entry
  178. *
  179. * Called by the PCI code when it finds a Jmicron controller.
  180. * We then use the IDE PCI generic helper to do most of the work.
  181. */
  182. static int __devinit jmicron_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  183. {
  184. ide_setup_pci_device(dev, &jmicron_chipsets[id->driver_data]);
  185. return 0;
  186. }
  187. /* If libata is configured, jmicron PCI quirk will configure it such
  188. * that the SATA ports are in AHCI function while the PATA ports are
  189. * in a separate IDE function. In such cases, match device class and
  190. * attach only to IDE. If libata isn't configured, keep the old
  191. * behavior for backward compatibility.
  192. */
  193. #if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE)
  194. #define JMB_CLASS PCI_CLASS_STORAGE_IDE << 8
  195. #define JMB_CLASS_MASK 0xffff00
  196. #else
  197. #define JMB_CLASS 0
  198. #define JMB_CLASS_MASK 0
  199. #endif
  200. static struct pci_device_id jmicron_pci_tbl[] = {
  201. { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361,
  202. PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 0},
  203. { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363,
  204. PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 1},
  205. { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365,
  206. PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 2},
  207. { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366,
  208. PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 3},
  209. { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368,
  210. PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 4},
  211. { 0, },
  212. };
  213. MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
  214. static struct pci_driver driver = {
  215. .name = "JMicron IDE",
  216. .id_table = jmicron_pci_tbl,
  217. .probe = jmicron_init_one,
  218. };
  219. static int __init jmicron_ide_init(void)
  220. {
  221. return ide_pci_register_driver(&driver);
  222. }
  223. module_init(jmicron_ide_init);
  224. MODULE_AUTHOR("Alan Cox");
  225. MODULE_DESCRIPTION("PCI driver module for the JMicron in legacy modes");
  226. MODULE_LICENSE("GPL");