blob: a6008f63e71ef677721411e7d2f36cb1696e9141 [file] [log] [blame]
Alan Coxbbb3bbd2006-10-03 01:14:12 -07001
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 Coxbbb3bbd2006-10-03 01:14:12 -07008#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
18typedef enum {
19 PORT_PATA0 = 0,
20 PORT_PATA1 = 1,
21 PORT_SATA = 2,
22} port_type;
23
24/**
Alan Coxbbb3bbd2006-10-03 01:14:12 -070025 * ata66_jmicron - Cable check
26 * @hwif: IDE port
27 *
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +020028 * Returns the cable type.
Alan Coxbbb3bbd2006-10-03 01:14:12 -070029 */
30
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +020031static u8 __devinit ata66_jmicron(ide_hwif_t *hwif)
Alan Coxbbb3bbd2006-10-03 01:14:12 -070032{
33 struct pci_dev *pdev = hwif->pci_dev;
34
35 u32 control;
36 u32 control5;
37
38 int port = hwif->channel;
39 port_type port_map[2];
40
41 pci_read_config_dword(pdev, 0x40, &control);
42
43 /* There are two basic mappings. One has the two SATA ports merged
44 as master/slave and the secondary as PATA, the other has only the
45 SATA port mapped */
46 if (control & (1 << 23)) {
47 port_map[0] = PORT_SATA;
48 port_map[1] = PORT_PATA0;
49 } else {
50 port_map[0] = PORT_SATA;
51 port_map[1] = PORT_SATA;
52 }
53
54 /* The 365/366 may have this bit set to map the second PATA port
55 as the internal primary channel */
56 pci_read_config_dword(pdev, 0x80, &control5);
57 if (control5 & (1<<24))
58 port_map[0] = PORT_PATA1;
59
60 /* The two ports may then be logically swapped by the firmware */
61 if (control & (1 << 22))
62 port = port ^ 1;
63
64 /*
65 * Now we know which physical port we are talking about we can
66 * actually do our cable checking etc. Thankfully we don't need
67 * to do the plumbing for other cases.
68 */
69 switch (port_map[port])
70 {
71 case PORT_PATA0:
72 if (control & (1 << 3)) /* 40/80 pin primary */
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +020073 return ATA_CBL_PATA40;
74 return ATA_CBL_PATA80;
Alan Coxbbb3bbd2006-10-03 01:14:12 -070075 case PORT_PATA1:
76 if (control5 & (1 << 19)) /* 40/80 pin secondary */
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +020077 return ATA_CBL_PATA40;
78 return ATA_CBL_PATA80;
Alan Coxbbb3bbd2006-10-03 01:14:12 -070079 case PORT_SATA:
Andrew Mortona51545a2007-01-27 13:46:21 +010080 break;
Alan Coxbbb3bbd2006-10-03 01:14:12 -070081 }
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +020082 /* Avoid bogus "control reaches end of non-void function" */
83 return ATA_CBL_PATA80;
Alan Coxbbb3bbd2006-10-03 01:14:12 -070084}
85
86static void jmicron_tuneproc (ide_drive_t *drive, byte mode_wanted)
87{
88 return;
89}
90
91/**
92 * config_jmicron_chipset_for_pio - set drive timings
93 * @drive: drive to tune
94 * @speed we want
95 *
96 */
97
98static void config_jmicron_chipset_for_pio (ide_drive_t *drive, byte set_speed)
99{
100 u8 speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, 5, NULL);
101 if (set_speed)
102 (void) ide_config_drive_speed(drive, speed);
103}
104
105/**
106 * jmicron_tune_chipset - set controller timings
107 * @drive: Drive to set up
108 * @xferspeed: speed we want to achieve
109 *
110 * As the JMicron snoops for timings all we actually need to do is
111 * make sure we don't set an invalid mode. We do need to honour
112 * the cable detect here.
113 */
114
115static int jmicron_tune_chipset (ide_drive_t *drive, byte xferspeed)
116{
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200117 u8 speed = ide_rate_filter(drive, xferspeed);
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700118
119 return ide_config_drive_speed(drive, speed);
120}
121
122/**
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700123 * jmicron_configure_drive_for_dma - set up for DMA transfers
124 * @drive: drive we are going to set up
125 *
126 * As the JMicron snoops for timings all we actually need to do is
127 * make sure we don't set an invalid mode.
128 */
129
130static int jmicron_config_drive_for_dma (ide_drive_t *drive)
131{
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200132 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100133 return 0;
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700134
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700135 config_jmicron_chipset_for_pio(drive, 1);
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100136
137 return -1;
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700138}
139
140/**
141 * init_hwif_jmicron - set up hwif structs
142 * @hwif: interface to set up
143 *
144 * Minimal set up is required for the Jmicron hardware.
145 */
146
147static void __devinit init_hwif_jmicron(ide_hwif_t *hwif)
148{
149 hwif->speedproc = &jmicron_tune_chipset;
150 hwif->tuneproc = &jmicron_tuneproc;
151
152 hwif->drives[0].autotune = 1;
153 hwif->drives[1].autotune = 1;
154
155 if (!hwif->dma_base)
156 goto fallback;
157
158 hwif->atapi_dma = 1;
159 hwif->ultra_mask = 0x7f;
160 hwif->mwdma_mask = 0x07;
161
162 hwif->ide_dma_check = &jmicron_config_drive_for_dma;
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200163
164 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
165 hwif->cbl = ata66_jmicron(hwif);
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700166
167 hwif->autodma = 1;
168 hwif->drives[0].autodma = hwif->autodma;
169 hwif->drives[1].autodma = hwif->autodma;
170 return;
171fallback:
172 hwif->autodma = 0;
173 return;
174}
175
176#define DECLARE_JMB_DEV(name_str) \
177 { \
178 .name = name_str, \
179 .init_hwif = init_hwif_jmicron, \
180 .channels = 2, \
181 .autodma = AUTODMA, \
182 .bootable = ON_BOARD, \
183 .enablebits = { {0x40, 1, 1}, {0x40, 0x10, 0x10} }, \
184 }
185
186static ide_pci_device_t jmicron_chipsets[] __devinitdata = {
187 /* 0 */ DECLARE_JMB_DEV("JMB361"),
188 /* 1 */ DECLARE_JMB_DEV("JMB363"),
189 /* 2 */ DECLARE_JMB_DEV("JMB365"),
190 /* 3 */ DECLARE_JMB_DEV("JMB366"),
191 /* 4 */ DECLARE_JMB_DEV("JMB368"),
192};
193
194/**
195 * jmicron_init_one - pci layer discovery entry
196 * @dev: PCI device
197 * @id: ident table entry
198 *
199 * Called by the PCI code when it finds a Jmicron controller.
200 * We then use the IDE PCI generic helper to do most of the work.
201 */
202
203static int __devinit jmicron_init_one(struct pci_dev *dev, const struct pci_device_id *id)
204{
205 ide_setup_pci_device(dev, &jmicron_chipsets[id->driver_data]);
206 return 0;
207}
208
Tejun Heoebbc2032007-03-17 21:57:25 +0100209/* If libata is configured, jmicron PCI quirk will configure it such
210 * that the SATA ports are in AHCI function while the PATA ports are
211 * in a separate IDE function. In such cases, match device class and
212 * attach only to IDE. If libata isn't configured, keep the old
213 * behavior for backward compatibility.
214 */
215#if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE)
216#define JMB_CLASS PCI_CLASS_STORAGE_IDE << 8
217#define JMB_CLASS_MASK 0xffff00
218#else
219#define JMB_CLASS 0
220#define JMB_CLASS_MASK 0
221#endif
222
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700223static struct pci_device_id jmicron_pci_tbl[] = {
Tejun Heoebbc2032007-03-17 21:57:25 +0100224 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361,
225 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 0},
226 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363,
227 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 1},
228 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365,
229 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 2},
230 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366,
231 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 3},
232 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368,
233 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 4},
Alan Coxbbb3bbd2006-10-03 01:14:12 -0700234 { 0, },
235};
236
237MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
238
239static struct pci_driver driver = {
240 .name = "JMicron IDE",
241 .id_table = jmicron_pci_tbl,
242 .probe = jmicron_init_one,
243};
244
245static int __init jmicron_ide_init(void)
246{
247 return ide_pci_register_driver(&driver);
248}
249
250module_init(jmicron_ide_init);
251
252MODULE_AUTHOR("Alan Cox");
253MODULE_DESCRIPTION("PCI driver module for the JMicron in legacy modes");
254MODULE_LICENSE("GPL");