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