jmicron.c 6.4 KB

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