]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ide/pci/jmicron.c
ide: rework the code for selecting the best DMA transfer mode (v3)
[mirror_ubuntu-artful-kernel.git] / drivers / ide / pci / jmicron.c
CommitLineData
bbb3bbdb
AC
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
bbb3bbdb
AC
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
18typedef enum {
19 PORT_PATA0 = 0,
20 PORT_PATA1 = 1,
21 PORT_SATA = 2,
22} port_type;
23
bbb3bbdb
AC
24/**
25 * ata66_jmicron - Cable check
26 * @hwif: IDE port
27 *
28 * Return 1 if the cable is 80pin
29 */
30
31static int __devinit ata66_jmicron(ide_hwif_t *hwif)
32{
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 */
99abaf51 73 return 0;
74 return 1;
bbb3bbdb
AC
75 case PORT_PATA1:
76 if (control5 & (1 << 19)) /* 40/80 pin secondary */
77 return 0;
78 return 1;
79 case PORT_SATA:
a51545ab 80 break;
bbb3bbdb 81 }
a51545ab 82 return 1; /* Avoid bogus "control reaches end of non-void function" */
bbb3bbdb
AC
83}
84
85static void jmicron_tuneproc (ide_drive_t *drive, byte mode_wanted)
86{
87 return;
88}
89
90/**
91 * config_jmicron_chipset_for_pio - set drive timings
92 * @drive: drive to tune
93 * @speed we want
94 *
95 */
96
97static void config_jmicron_chipset_for_pio (ide_drive_t *drive, byte set_speed)
98{
99 u8 speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, 5, NULL);
100 if (set_speed)
101 (void) ide_config_drive_speed(drive, speed);
102}
103
104/**
105 * jmicron_tune_chipset - set controller timings
106 * @drive: Drive to set up
107 * @xferspeed: speed we want to achieve
108 *
109 * As the JMicron snoops for timings all we actually need to do is
110 * make sure we don't set an invalid mode. We do need to honour
111 * the cable detect here.
112 */
113
114static int jmicron_tune_chipset (ide_drive_t *drive, byte xferspeed)
115{
2d5eaa6d 116 u8 speed = ide_rate_filter(drive, xferspeed);
bbb3bbdb
AC
117
118 return ide_config_drive_speed(drive, speed);
119}
120
121/**
122 * config_chipset_for_dma - configure for DMA
123 * @drive: drive to configure
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.
127 */
128
129static int config_chipset_for_dma (ide_drive_t *drive)
130{
2d5eaa6d 131 u8 speed = ide_max_dma_mode(drive);
bbb3bbdb 132
39baf8a7
BZ
133 if (!speed)
134 return 0;
135
bbb3bbdb
AC
136 jmicron_tune_chipset(drive, speed);
137 return ide_dma_enable(drive);
138}
139
140/**
141 * jmicron_configure_drive_for_dma - set up for DMA transfers
142 * @drive: drive we are going to set up
143 *
144 * As the JMicron snoops for timings all we actually need to do is
145 * make sure we don't set an invalid mode.
146 */
147
148static int jmicron_config_drive_for_dma (ide_drive_t *drive)
149{
3608b5d7
BZ
150 if (ide_use_dma(drive) && config_chipset_for_dma(drive))
151 return 0;
bbb3bbdb 152
bbb3bbdb 153 config_jmicron_chipset_for_pio(drive, 1);
3608b5d7
BZ
154
155 return -1;
bbb3bbdb
AC
156}
157
158/**
159 * init_hwif_jmicron - set up hwif structs
160 * @hwif: interface to set up
161 *
162 * Minimal set up is required for the Jmicron hardware.
163 */
164
165static void __devinit init_hwif_jmicron(ide_hwif_t *hwif)
166{
167 hwif->speedproc = &jmicron_tune_chipset;
168 hwif->tuneproc = &jmicron_tuneproc;
169
170 hwif->drives[0].autotune = 1;
171 hwif->drives[1].autotune = 1;
172
173 if (!hwif->dma_base)
174 goto fallback;
175
176 hwif->atapi_dma = 1;
177 hwif->ultra_mask = 0x7f;
178 hwif->mwdma_mask = 0x07;
179
180 hwif->ide_dma_check = &jmicron_config_drive_for_dma;
181 if (!(hwif->udma_four))
182 hwif->udma_four = ata66_jmicron(hwif);
183
184 hwif->autodma = 1;
185 hwif->drives[0].autodma = hwif->autodma;
186 hwif->drives[1].autodma = hwif->autodma;
187 return;
188fallback:
189 hwif->autodma = 0;
190 return;
191}
192
193#define DECLARE_JMB_DEV(name_str) \
194 { \
195 .name = name_str, \
196 .init_hwif = init_hwif_jmicron, \
197 .channels = 2, \
198 .autodma = AUTODMA, \
199 .bootable = ON_BOARD, \
200 .enablebits = { {0x40, 1, 1}, {0x40, 0x10, 0x10} }, \
201 }
202
203static ide_pci_device_t jmicron_chipsets[] __devinitdata = {
204 /* 0 */ DECLARE_JMB_DEV("JMB361"),
205 /* 1 */ DECLARE_JMB_DEV("JMB363"),
206 /* 2 */ DECLARE_JMB_DEV("JMB365"),
207 /* 3 */ DECLARE_JMB_DEV("JMB366"),
208 /* 4 */ DECLARE_JMB_DEV("JMB368"),
209};
210
211/**
212 * jmicron_init_one - pci layer discovery entry
213 * @dev: PCI device
214 * @id: ident table entry
215 *
216 * Called by the PCI code when it finds a Jmicron controller.
217 * We then use the IDE PCI generic helper to do most of the work.
218 */
219
220static int __devinit jmicron_init_one(struct pci_dev *dev, const struct pci_device_id *id)
221{
222 ide_setup_pci_device(dev, &jmicron_chipsets[id->driver_data]);
223 return 0;
224}
225
ebbc2031
TH
226/* If libata is configured, jmicron PCI quirk will configure it such
227 * that the SATA ports are in AHCI function while the PATA ports are
228 * in a separate IDE function. In such cases, match device class and
229 * attach only to IDE. If libata isn't configured, keep the old
230 * behavior for backward compatibility.
231 */
232#if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE)
233#define JMB_CLASS PCI_CLASS_STORAGE_IDE << 8
234#define JMB_CLASS_MASK 0xffff00
235#else
236#define JMB_CLASS 0
237#define JMB_CLASS_MASK 0
238#endif
239
bbb3bbdb 240static struct pci_device_id jmicron_pci_tbl[] = {
ebbc2031
TH
241 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361,
242 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 0},
243 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363,
244 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 1},
245 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365,
246 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 2},
247 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366,
248 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 3},
249 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368,
250 PCI_ANY_ID, PCI_ANY_ID, JMB_CLASS, JMB_CLASS_MASK, 4},
bbb3bbdb
AC
251 { 0, },
252};
253
254MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
255
256static struct pci_driver driver = {
257 .name = "JMicron IDE",
258 .id_table = jmicron_pci_tbl,
259 .probe = jmicron_init_one,
260};
261
262static int __init jmicron_ide_init(void)
263{
264 return ide_pci_register_driver(&driver);
265}
266
267module_init(jmicron_ide_init);
268
269MODULE_AUTHOR("Alan Cox");
270MODULE_DESCRIPTION("PCI driver module for the JMicron in legacy modes");
271MODULE_LICENSE("GPL");