jmicron.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 1;
  75. return 0;
  76. case PORT_PATA1:
  77. if (control5 & (1 << 19)) /* 40/80 pin secondary */
  78. return 0;
  79. return 1;
  80. case PORT_SATA:
  81. return 1;
  82. }
  83. }
  84. static void jmicron_tuneproc (ide_drive_t *drive, byte mode_wanted)
  85. {
  86. return;
  87. }
  88. /**
  89. * config_jmicron_chipset_for_pio - set drive timings
  90. * @drive: drive to tune
  91. * @speed we want
  92. *
  93. */
  94. static void config_jmicron_chipset_for_pio (ide_drive_t *drive, byte set_speed)
  95. {
  96. u8 speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, 5, NULL);
  97. if (set_speed)
  98. (void) ide_config_drive_speed(drive, speed);
  99. }
  100. /**
  101. * jmicron_tune_chipset - set controller timings
  102. * @drive: Drive to set up
  103. * @xferspeed: speed we want to achieve
  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. We do need to honour
  107. * the cable detect here.
  108. */
  109. static int jmicron_tune_chipset (ide_drive_t *drive, byte xferspeed)
  110. {
  111. u8 speed = ide_rate_filter(jmicron_ratemask(drive), xferspeed);
  112. return ide_config_drive_speed(drive, speed);
  113. }
  114. /**
  115. * config_chipset_for_dma - configure for DMA
  116. * @drive: drive to configure
  117. *
  118. * As the JMicron snoops for timings all we actually need to do is
  119. * make sure we don't set an invalid mode.
  120. */
  121. static int config_chipset_for_dma (ide_drive_t *drive)
  122. {
  123. u8 speed = ide_dma_speed(drive, jmicron_ratemask(drive));
  124. config_jmicron_chipset_for_pio(drive, !speed);
  125. jmicron_tune_chipset(drive, speed);
  126. return ide_dma_enable(drive);
  127. }
  128. /**
  129. * jmicron_configure_drive_for_dma - set up for DMA transfers
  130. * @drive: drive we are going to set up
  131. *
  132. * As the JMicron snoops for timings all we actually need to do is
  133. * make sure we don't set an invalid mode.
  134. */
  135. static int jmicron_config_drive_for_dma (ide_drive_t *drive)
  136. {
  137. ide_hwif_t *hwif = drive->hwif;
  138. if (ide_use_dma(drive)) {
  139. if (config_chipset_for_dma(drive))
  140. return hwif->ide_dma_on(drive);
  141. }
  142. config_jmicron_chipset_for_pio(drive, 1);
  143. return hwif->ide_dma_off_quietly(drive);
  144. }
  145. /**
  146. * init_hwif_jmicron - set up hwif structs
  147. * @hwif: interface to set up
  148. *
  149. * Minimal set up is required for the Jmicron hardware.
  150. */
  151. static void __devinit init_hwif_jmicron(ide_hwif_t *hwif)
  152. {
  153. hwif->speedproc = &jmicron_tune_chipset;
  154. hwif->tuneproc = &jmicron_tuneproc;
  155. hwif->drives[0].autotune = 1;
  156. hwif->drives[1].autotune = 1;
  157. if (!hwif->dma_base)
  158. goto fallback;
  159. hwif->atapi_dma = 1;
  160. hwif->ultra_mask = 0x7f;
  161. hwif->mwdma_mask = 0x07;
  162. hwif->ide_dma_check = &jmicron_config_drive_for_dma;
  163. if (!(hwif->udma_four))
  164. hwif->udma_four = ata66_jmicron(hwif);
  165. hwif->autodma = 1;
  166. hwif->drives[0].autodma = hwif->autodma;
  167. hwif->drives[1].autodma = hwif->autodma;
  168. return;
  169. fallback:
  170. hwif->autodma = 0;
  171. return;
  172. }
  173. #define DECLARE_JMB_DEV(name_str) \
  174. { \
  175. .name = name_str, \
  176. .init_hwif = init_hwif_jmicron, \
  177. .channels = 2, \
  178. .autodma = AUTODMA, \
  179. .bootable = ON_BOARD, \
  180. .enablebits = { {0x40, 1, 1}, {0x40, 0x10, 0x10} }, \
  181. }
  182. static ide_pci_device_t jmicron_chipsets[] __devinitdata = {
  183. /* 0 */ DECLARE_JMB_DEV("JMB361"),
  184. /* 1 */ DECLARE_JMB_DEV("JMB363"),
  185. /* 2 */ DECLARE_JMB_DEV("JMB365"),
  186. /* 3 */ DECLARE_JMB_DEV("JMB366"),
  187. /* 4 */ DECLARE_JMB_DEV("JMB368"),
  188. };
  189. /**
  190. * jmicron_init_one - pci layer discovery entry
  191. * @dev: PCI device
  192. * @id: ident table entry
  193. *
  194. * Called by the PCI code when it finds a Jmicron controller.
  195. * We then use the IDE PCI generic helper to do most of the work.
  196. */
  197. static int __devinit jmicron_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  198. {
  199. ide_setup_pci_device(dev, &jmicron_chipsets[id->driver_data]);
  200. return 0;
  201. }
  202. static struct pci_device_id jmicron_pci_tbl[] = {
  203. { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361), 0},
  204. { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363), 1},
  205. { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365), 2},
  206. { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366), 3},
  207. { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368), 4},
  208. { 0, },
  209. };
  210. MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
  211. static struct pci_driver driver = {
  212. .name = "JMicron IDE",
  213. .id_table = jmicron_pci_tbl,
  214. .probe = jmicron_init_one,
  215. };
  216. static int __init jmicron_ide_init(void)
  217. {
  218. return ide_pci_register_driver(&driver);
  219. }
  220. module_init(jmicron_ide_init);
  221. MODULE_AUTHOR("Alan Cox");
  222. MODULE_DESCRIPTION("PCI driver module for the JMicron in legacy modes");
  223. MODULE_LICENSE("GPL");