diff options
Diffstat (limited to 'drivers/ide/pci/it8213.c')
-rw-r--r-- | drivers/ide/pci/it8213.c | 362 |
1 files changed, 362 insertions, 0 deletions
diff --git a/drivers/ide/pci/it8213.c b/drivers/ide/pci/it8213.c new file mode 100644 index 000000000000..63248b6909fa --- /dev/null +++ b/drivers/ide/pci/it8213.c | |||
@@ -0,0 +1,362 @@ | |||
1 | /* | ||
2 | * ITE 8213 IDE driver | ||
3 | * | ||
4 | * Copyright (C) 2006 Jack Lee | ||
5 | * Copyright (C) 2006 Alan Cox | ||
6 | * Copyright (C) 2007 Bartlomiej Zolnierkiewicz | ||
7 | */ | ||
8 | |||
9 | #include <linux/kernel.h> | ||
10 | #include <linux/types.h> | ||
11 | #include <linux/module.h> | ||
12 | #include <linux/pci.h> | ||
13 | #include <linux/delay.h> | ||
14 | #include <linux/hdreg.h> | ||
15 | #include <linux/ide.h> | ||
16 | #include <linux/init.h> | ||
17 | |||
18 | #include <asm/io.h> | ||
19 | |||
20 | /* | ||
21 | * it8213_ratemask - Compute available modes | ||
22 | * @drive: IDE drive | ||
23 | * | ||
24 | * Compute the available speeds for the devices on the interface. This | ||
25 | * is all modes to ATA133 clipped by drive cable setup. | ||
26 | */ | ||
27 | |||
28 | static u8 it8213_ratemask (ide_drive_t *drive) | ||
29 | { | ||
30 | u8 mode = 4; | ||
31 | if (!eighty_ninty_three(drive)) | ||
32 | mode = min_t(u8, mode, 1); | ||
33 | return mode; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * it8213_dma_2_pio - return the PIO mode matching DMA | ||
38 | * @xfer_rate: transfer speed | ||
39 | * | ||
40 | * Returns the nearest equivalent PIO timing for the PIO or DMA | ||
41 | * mode requested by the controller. | ||
42 | */ | ||
43 | |||
44 | static u8 it8213_dma_2_pio (u8 xfer_rate) { | ||
45 | switch(xfer_rate) { | ||
46 | case XFER_UDMA_6: | ||
47 | case XFER_UDMA_5: | ||
48 | case XFER_UDMA_4: | ||
49 | case XFER_UDMA_3: | ||
50 | case XFER_UDMA_2: | ||
51 | case XFER_UDMA_1: | ||
52 | case XFER_UDMA_0: | ||
53 | case XFER_MW_DMA_2: | ||
54 | case XFER_PIO_4: | ||
55 | return 4; | ||
56 | case XFER_MW_DMA_1: | ||
57 | case XFER_PIO_3: | ||
58 | return 3; | ||
59 | case XFER_SW_DMA_2: | ||
60 | case XFER_PIO_2: | ||
61 | return 2; | ||
62 | case XFER_MW_DMA_0: | ||
63 | case XFER_SW_DMA_1: | ||
64 | case XFER_SW_DMA_0: | ||
65 | case XFER_PIO_1: | ||
66 | case XFER_PIO_0: | ||
67 | case XFER_PIO_SLOW: | ||
68 | default: | ||
69 | return 0; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | /* | ||
74 | * it8213_tuneproc - tune a drive | ||
75 | * @drive: drive to tune | ||
76 | * @pio: desired PIO mode | ||
77 | * | ||
78 | * Set the interface PIO mode. | ||
79 | */ | ||
80 | |||
81 | static void it8213_tuneproc (ide_drive_t *drive, u8 pio) | ||
82 | { | ||
83 | ide_hwif_t *hwif = HWIF(drive); | ||
84 | struct pci_dev *dev = hwif->pci_dev; | ||
85 | int is_slave = drive->dn & 1; | ||
86 | int master_port = 0x40; | ||
87 | int slave_port = 0x44; | ||
88 | unsigned long flags; | ||
89 | u16 master_data; | ||
90 | u8 slave_data; | ||
91 | static DEFINE_SPINLOCK(tune_lock); | ||
92 | int control = 0; | ||
93 | |||
94 | static const u8 timings[][2]= { | ||
95 | { 0, 0 }, | ||
96 | { 0, 0 }, | ||
97 | { 1, 0 }, | ||
98 | { 2, 1 }, | ||
99 | { 2, 3 }, }; | ||
100 | |||
101 | pio = ide_get_best_pio_mode(drive, pio, 4, NULL); | ||
102 | |||
103 | spin_lock_irqsave(&tune_lock, flags); | ||
104 | pci_read_config_word(dev, master_port, &master_data); | ||
105 | |||
106 | if (pio > 1) | ||
107 | control |= 1; /* Programmable timing on */ | ||
108 | if (drive->media != ide_disk) | ||
109 | control |= 4; /* ATAPI */ | ||
110 | if (pio > 2) | ||
111 | control |= 2; /* IORDY */ | ||
112 | if (is_slave) { | ||
113 | master_data |= 0x4000; | ||
114 | master_data &= ~0x0070; | ||
115 | if (pio > 1) | ||
116 | master_data = master_data | (control << 4); | ||
117 | pci_read_config_byte(dev, slave_port, &slave_data); | ||
118 | slave_data = slave_data & 0xf0; | ||
119 | slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1]; | ||
120 | } else { | ||
121 | master_data &= ~0x3307; | ||
122 | if (pio > 1) | ||
123 | master_data = master_data | control; | ||
124 | master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8); | ||
125 | } | ||
126 | pci_write_config_word(dev, master_port, master_data); | ||
127 | if (is_slave) | ||
128 | pci_write_config_byte(dev, slave_port, slave_data); | ||
129 | spin_unlock_irqrestore(&tune_lock, flags); | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * it8213_tune_chipset - set controller timings | ||
134 | * @drive: Drive to set up | ||
135 | * @xferspeed: speed we want to achieve | ||
136 | * | ||
137 | * Tune the ITE chipset for the desired mode. If we can't achieve | ||
138 | * the desired mode then tune for a lower one, but ultimately | ||
139 | * make the thing work. | ||
140 | */ | ||
141 | |||
142 | static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed) | ||
143 | { | ||
144 | |||
145 | ide_hwif_t *hwif = HWIF(drive); | ||
146 | struct pci_dev *dev = hwif->pci_dev; | ||
147 | u8 maslave = 0x40; | ||
148 | u8 speed = ide_rate_filter(it8213_ratemask(drive), xferspeed); | ||
149 | int a_speed = 3 << (drive->dn * 4); | ||
150 | int u_flag = 1 << drive->dn; | ||
151 | int v_flag = 0x01 << drive->dn; | ||
152 | int w_flag = 0x10 << drive->dn; | ||
153 | int u_speed = 0; | ||
154 | u16 reg4042, reg4a; | ||
155 | u8 reg48, reg54, reg55; | ||
156 | |||
157 | pci_read_config_word(dev, maslave, ®4042); | ||
158 | pci_read_config_byte(dev, 0x48, ®48); | ||
159 | pci_read_config_word(dev, 0x4a, ®4a); | ||
160 | pci_read_config_byte(dev, 0x54, ®54); | ||
161 | pci_read_config_byte(dev, 0x55, ®55); | ||
162 | |||
163 | switch(speed) { | ||
164 | case XFER_UDMA_6: | ||
165 | case XFER_UDMA_4: | ||
166 | case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break; | ||
167 | case XFER_UDMA_5: | ||
168 | case XFER_UDMA_3: | ||
169 | case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break; | ||
170 | case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break; | ||
171 | break; | ||
172 | case XFER_MW_DMA_2: | ||
173 | case XFER_MW_DMA_1: | ||
174 | case XFER_SW_DMA_2: | ||
175 | break; | ||
176 | case XFER_PIO_4: | ||
177 | case XFER_PIO_3: | ||
178 | case XFER_PIO_2: | ||
179 | case XFER_PIO_1: | ||
180 | case XFER_PIO_0: | ||
181 | break; | ||
182 | default: | ||
183 | return -1; | ||
184 | } | ||
185 | |||
186 | if (speed >= XFER_UDMA_0) { | ||
187 | if (!(reg48 & u_flag)) | ||
188 | pci_write_config_byte(dev, 0x48, reg48 | u_flag); | ||
189 | if (speed >= XFER_UDMA_5) { | ||
190 | pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag); | ||
191 | } else { | ||
192 | pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag); | ||
193 | } | ||
194 | |||
195 | if ((reg4a & a_speed) != u_speed) | ||
196 | pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed); | ||
197 | if (speed > XFER_UDMA_2) { | ||
198 | if (!(reg54 & v_flag)) | ||
199 | pci_write_config_byte(dev, 0x54, reg54 | v_flag); | ||
200 | } else | ||
201 | pci_write_config_byte(dev, 0x54, reg54 & ~v_flag); | ||
202 | } else { | ||
203 | if (reg48 & u_flag) | ||
204 | pci_write_config_byte(dev, 0x48, reg48 & ~u_flag); | ||
205 | if (reg4a & a_speed) | ||
206 | pci_write_config_word(dev, 0x4a, reg4a & ~a_speed); | ||
207 | if (reg54 & v_flag) | ||
208 | pci_write_config_byte(dev, 0x54, reg54 & ~v_flag); | ||
209 | if (reg55 & w_flag) | ||
210 | pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag); | ||
211 | } | ||
212 | it8213_tuneproc(drive, it8213_dma_2_pio(speed)); | ||
213 | return ide_config_drive_speed(drive, speed); | ||
214 | } | ||
215 | |||
216 | /* | ||
217 | * config_chipset_for_dma - configure for DMA | ||
218 | * @drive: drive to configure | ||
219 | * | ||
220 | * Called by the IDE layer when it wants the timings set up. | ||
221 | */ | ||
222 | |||
223 | static int config_chipset_for_dma (ide_drive_t *drive) | ||
224 | { | ||
225 | u8 speed = ide_dma_speed(drive, it8213_ratemask(drive)); | ||
226 | |||
227 | if (!speed) | ||
228 | return 0; | ||
229 | |||
230 | it8213_tune_chipset(drive, speed); | ||
231 | |||
232 | return ide_dma_enable(drive); | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * it8213_configure_drive_for_dma - set up for DMA transfers | ||
237 | * @drive: drive we are going to set up | ||
238 | * | ||
239 | * Set up the drive for DMA, tune the controller and drive as | ||
240 | * required. If the drive isn't suitable for DMA or we hit | ||
241 | * other problems then we will drop down to PIO and set up | ||
242 | * PIO appropriately | ||
243 | */ | ||
244 | |||
245 | static int it8213_config_drive_for_dma (ide_drive_t *drive) | ||
246 | { | ||
247 | ide_hwif_t *hwif = drive->hwif; | ||
248 | |||
249 | if (ide_use_dma(drive)) { | ||
250 | if (config_chipset_for_dma(drive)) | ||
251 | return hwif->ide_dma_on(drive); | ||
252 | } | ||
253 | |||
254 | hwif->speedproc(drive, XFER_PIO_0 | ||
255 | + ide_get_best_pio_mode(drive, 255, 4, NULL)); | ||
256 | |||
257 | return hwif->ide_dma_off_quietly(drive); | ||
258 | } | ||
259 | |||
260 | /** | ||
261 | * init_hwif_it8213 - set up hwif structs | ||
262 | * @hwif: interface to set up | ||
263 | * | ||
264 | * We do the basic set up of the interface structure. The IT8212 | ||
265 | * requires several custom handlers so we override the default | ||
266 | * ide DMA handlers appropriately | ||
267 | */ | ||
268 | |||
269 | static void __devinit init_hwif_it8213(ide_hwif_t *hwif) | ||
270 | { | ||
271 | u8 reg42h = 0, ata66 = 0; | ||
272 | |||
273 | hwif->speedproc = &it8213_tune_chipset; | ||
274 | hwif->tuneproc = &it8213_tuneproc; | ||
275 | |||
276 | hwif->autodma = 0; | ||
277 | |||
278 | hwif->drives[0].autotune = 1; | ||
279 | hwif->drives[1].autotune = 1; | ||
280 | |||
281 | if (!hwif->dma_base) | ||
282 | return; | ||
283 | |||
284 | hwif->atapi_dma = 1; | ||
285 | hwif->ultra_mask = 0x7f; | ||
286 | hwif->mwdma_mask = 0x06; | ||
287 | hwif->swdma_mask = 0x04; | ||
288 | |||
289 | pci_read_config_byte(hwif->pci_dev, 0x42, ®42h); | ||
290 | ata66 = (reg42h & 0x02) ? 0 : 1; | ||
291 | |||
292 | hwif->ide_dma_check = &it8213_config_drive_for_dma; | ||
293 | if (!(hwif->udma_four)) | ||
294 | hwif->udma_four = ata66; | ||
295 | |||
296 | /* | ||
297 | * The BIOS often doesn't set up DMA on this controller | ||
298 | * so we always do it. | ||
299 | */ | ||
300 | if (!noautodma) | ||
301 | hwif->autodma = 1; | ||
302 | |||
303 | hwif->drives[0].autodma = hwif->autodma; | ||
304 | hwif->drives[1].autodma = hwif->autodma; | ||
305 | } | ||
306 | |||
307 | |||
308 | #define DECLARE_ITE_DEV(name_str) \ | ||
309 | { \ | ||
310 | .name = name_str, \ | ||
311 | .init_hwif = init_hwif_it8213, \ | ||
312 | .channels = 1, \ | ||
313 | .autodma = AUTODMA, \ | ||
314 | .enablebits = {{0x41,0x80,0x80}}, \ | ||
315 | .bootable = ON_BOARD, \ | ||
316 | } | ||
317 | |||
318 | static ide_pci_device_t it8213_chipsets[] __devinitdata = { | ||
319 | /* 0 */ DECLARE_ITE_DEV("IT8213"), | ||
320 | }; | ||
321 | |||
322 | |||
323 | /** | ||
324 | * it8213_init_one - pci layer discovery entry | ||
325 | * @dev: PCI device | ||
326 | * @id: ident table entry | ||
327 | * | ||
328 | * Called by the PCI code when it finds an ITE8213 controller. As | ||
329 | * this device follows the standard interfaces we can use the | ||
330 | * standard helper functions to do almost all the work for us. | ||
331 | */ | ||
332 | |||
333 | static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id) | ||
334 | { | ||
335 | ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]); | ||
336 | return 0; | ||
337 | } | ||
338 | |||
339 | |||
340 | static struct pci_device_id it8213_pci_tbl[] = { | ||
341 | { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, | ||
342 | { 0, }, | ||
343 | }; | ||
344 | |||
345 | MODULE_DEVICE_TABLE(pci, it8213_pci_tbl); | ||
346 | |||
347 | static struct pci_driver driver = { | ||
348 | .name = "ITE8213_IDE", | ||
349 | .id_table = it8213_pci_tbl, | ||
350 | .probe = it8213_init_one, | ||
351 | }; | ||
352 | |||
353 | static int __init it8213_ide_init(void) | ||
354 | { | ||
355 | return ide_pci_register_driver(&driver); | ||
356 | } | ||
357 | |||
358 | module_init(it8213_ide_init); | ||
359 | |||
360 | MODULE_AUTHOR("Jack Lee, Alan Cox"); | ||
361 | MODULE_DESCRIPTION("PCI driver module for the ITE 8213"); | ||
362 | MODULE_LICENSE("GPL"); | ||