blob: ff767755e98b4703fc578b6401e10abf2aee06e7 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * Libata driver for the highpoint 37x and 30x UDMA66 ATA controllers.
3 *
4 * This driver is heavily based upon:
5 *
6 * linux/drivers/ide/pci/hpt366.c Version 0.36 April 25, 2003
7 *
8 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
9 * Portions Copyright (C) 2001 Sun Microsystems, Inc.
10 * Portions Copyright (C) 2003 Red Hat Inc
11 *
12 * TODO
13 * PLL mode
14 * Look into engine reset on timeout errors. Should not be
15 * required.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/init.h>
22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <scsi/scsi_host.h>
25#include <linux/libata.h>
26
27#define DRV_NAME "pata_hpt37x"
Alan6929da42007-01-05 16:37:01 -080028#define DRV_VERSION "0.5.2"
Jeff Garzik669a5db2006-08-29 18:12:40 -040029
30struct hpt_clock {
31 u8 xfer_speed;
32 u32 timing;
33};
34
35struct hpt_chip {
36 const char *name;
37 unsigned int base;
38 struct hpt_clock const *clocks[4];
39};
40
41/* key for bus clock timings
42 * bit
43 * 0:3 data_high_time. inactive time of DIOW_/DIOR_ for PIO and MW
44 * DMA. cycles = value + 1
45 * 4:8 data_low_time. active time of DIOW_/DIOR_ for PIO and MW
46 * DMA. cycles = value + 1
47 * 9:12 cmd_high_time. inactive time of DIOW_/DIOR_ during task file
48 * register access.
49 * 13:17 cmd_low_time. active time of DIOW_/DIOR_ during task file
50 * register access.
51 * 18:21 udma_cycle_time. clock freq and clock cycles for UDMA xfer.
52 * during task file register access.
53 * 22:24 pre_high_time. time to initialize 1st cycle for PIO and MW DMA
54 * xfer.
55 * 25:27 cmd_pre_high_time. time to initialize 1st PIO cycle for task
56 * register access.
57 * 28 UDMA enable
58 * 29 DMA enable
59 * 30 PIO_MST enable. if set, the chip is in bus master mode during
60 * PIO.
61 * 31 FIFO enable.
62 */
63
64/* from highpoint documentation. these are old values */
65static const struct hpt_clock hpt370_timings_33[] = {
66/* { XFER_UDMA_5, 0x1A85F442, 0x16454e31 }, */
67 { XFER_UDMA_5, 0x16454e31 },
68 { XFER_UDMA_4, 0x16454e31 },
69 { XFER_UDMA_3, 0x166d4e31 },
70 { XFER_UDMA_2, 0x16494e31 },
71 { XFER_UDMA_1, 0x164d4e31 },
72 { XFER_UDMA_0, 0x16514e31 },
73
74 { XFER_MW_DMA_2, 0x26514e21 },
75 { XFER_MW_DMA_1, 0x26514e33 },
76 { XFER_MW_DMA_0, 0x26514e97 },
77
78 { XFER_PIO_4, 0x06514e21 },
79 { XFER_PIO_3, 0x06514e22 },
80 { XFER_PIO_2, 0x06514e33 },
81 { XFER_PIO_1, 0x06914e43 },
82 { XFER_PIO_0, 0x06914e57 },
83 { 0, 0x06514e57 }
84};
85
86static const struct hpt_clock hpt370_timings_66[] = {
87 { XFER_UDMA_5, 0x14846231 },
88 { XFER_UDMA_4, 0x14886231 },
89 { XFER_UDMA_3, 0x148c6231 },
90 { XFER_UDMA_2, 0x148c6231 },
91 { XFER_UDMA_1, 0x14906231 },
92 { XFER_UDMA_0, 0x14986231 },
93
94 { XFER_MW_DMA_2, 0x26514e21 },
95 { XFER_MW_DMA_1, 0x26514e33 },
96 { XFER_MW_DMA_0, 0x26514e97 },
97
98 { XFER_PIO_4, 0x06514e21 },
99 { XFER_PIO_3, 0x06514e22 },
100 { XFER_PIO_2, 0x06514e33 },
101 { XFER_PIO_1, 0x06914e43 },
102 { XFER_PIO_0, 0x06914e57 },
103 { 0, 0x06514e57 }
104};
105
106/* these are the current (4 sep 2001) timings from highpoint */
107static const struct hpt_clock hpt370a_timings_33[] = {
108 { XFER_UDMA_5, 0x12446231 },
109 { XFER_UDMA_4, 0x12446231 },
110 { XFER_UDMA_3, 0x126c6231 },
111 { XFER_UDMA_2, 0x12486231 },
112 { XFER_UDMA_1, 0x124c6233 },
113 { XFER_UDMA_0, 0x12506297 },
114
115 { XFER_MW_DMA_2, 0x22406c31 },
116 { XFER_MW_DMA_1, 0x22406c33 },
117 { XFER_MW_DMA_0, 0x22406c97 },
118
119 { XFER_PIO_4, 0x06414e31 },
120 { XFER_PIO_3, 0x06414e42 },
121 { XFER_PIO_2, 0x06414e53 },
122 { XFER_PIO_1, 0x06814e93 },
123 { XFER_PIO_0, 0x06814ea7 },
124 { 0, 0x06814ea7 }
125};
126
127/* 2x 33MHz timings */
128static const struct hpt_clock hpt370a_timings_66[] = {
129 { XFER_UDMA_5, 0x1488e673 },
130 { XFER_UDMA_4, 0x1488e673 },
131 { XFER_UDMA_3, 0x1498e673 },
132 { XFER_UDMA_2, 0x1490e673 },
133 { XFER_UDMA_1, 0x1498e677 },
134 { XFER_UDMA_0, 0x14a0e73f },
135
136 { XFER_MW_DMA_2, 0x2480fa73 },
Jeff Garzik85cd7252006-08-31 00:03:49 -0400137 { XFER_MW_DMA_1, 0x2480fa77 },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400138 { XFER_MW_DMA_0, 0x2480fb3f },
139
140 { XFER_PIO_4, 0x0c82be73 },
141 { XFER_PIO_3, 0x0c82be95 },
142 { XFER_PIO_2, 0x0c82beb7 },
143 { XFER_PIO_1, 0x0d02bf37 },
144 { XFER_PIO_0, 0x0d02bf5f },
145 { 0, 0x0d02bf5f }
146};
147
148static const struct hpt_clock hpt370a_timings_50[] = {
149 { XFER_UDMA_5, 0x12848242 },
150 { XFER_UDMA_4, 0x12ac8242 },
151 { XFER_UDMA_3, 0x128c8242 },
152 { XFER_UDMA_2, 0x120c8242 },
153 { XFER_UDMA_1, 0x12148254 },
154 { XFER_UDMA_0, 0x121882ea },
155
156 { XFER_MW_DMA_2, 0x22808242 },
157 { XFER_MW_DMA_1, 0x22808254 },
158 { XFER_MW_DMA_0, 0x228082ea },
159
160 { XFER_PIO_4, 0x0a81f442 },
161 { XFER_PIO_3, 0x0a81f443 },
162 { XFER_PIO_2, 0x0a81f454 },
163 { XFER_PIO_1, 0x0ac1f465 },
164 { XFER_PIO_0, 0x0ac1f48a },
165 { 0, 0x0ac1f48a }
166};
167
168static const struct hpt_clock hpt372_timings_33[] = {
169 { XFER_UDMA_6, 0x1c81dc62 },
170 { XFER_UDMA_5, 0x1c6ddc62 },
171 { XFER_UDMA_4, 0x1c8ddc62 },
172 { XFER_UDMA_3, 0x1c8edc62 }, /* checkme */
173 { XFER_UDMA_2, 0x1c91dc62 },
174 { XFER_UDMA_1, 0x1c9adc62 }, /* checkme */
175 { XFER_UDMA_0, 0x1c82dc62 }, /* checkme */
176
177 { XFER_MW_DMA_2, 0x2c829262 },
178 { XFER_MW_DMA_1, 0x2c829266 }, /* checkme */
179 { XFER_MW_DMA_0, 0x2c82922e }, /* checkme */
180
181 { XFER_PIO_4, 0x0c829c62 },
182 { XFER_PIO_3, 0x0c829c84 },
183 { XFER_PIO_2, 0x0c829ca6 },
184 { XFER_PIO_1, 0x0d029d26 },
185 { XFER_PIO_0, 0x0d029d5e },
186 { 0, 0x0d029d5e }
187};
188
189static const struct hpt_clock hpt372_timings_50[] = {
190 { XFER_UDMA_5, 0x12848242 },
191 { XFER_UDMA_4, 0x12ac8242 },
192 { XFER_UDMA_3, 0x128c8242 },
193 { XFER_UDMA_2, 0x120c8242 },
194 { XFER_UDMA_1, 0x12148254 },
195 { XFER_UDMA_0, 0x121882ea },
196
197 { XFER_MW_DMA_2, 0x22808242 },
198 { XFER_MW_DMA_1, 0x22808254 },
199 { XFER_MW_DMA_0, 0x228082ea },
200
201 { XFER_PIO_4, 0x0a81f442 },
202 { XFER_PIO_3, 0x0a81f443 },
203 { XFER_PIO_2, 0x0a81f454 },
204 { XFER_PIO_1, 0x0ac1f465 },
205 { XFER_PIO_0, 0x0ac1f48a },
206 { 0, 0x0a81f443 }
207};
208
209static const struct hpt_clock hpt372_timings_66[] = {
210 { XFER_UDMA_6, 0x1c869c62 },
211 { XFER_UDMA_5, 0x1cae9c62 },
212 { XFER_UDMA_4, 0x1c8a9c62 },
213 { XFER_UDMA_3, 0x1c8e9c62 },
214 { XFER_UDMA_2, 0x1c929c62 },
215 { XFER_UDMA_1, 0x1c9a9c62 },
216 { XFER_UDMA_0, 0x1c829c62 },
217
218 { XFER_MW_DMA_2, 0x2c829c62 },
219 { XFER_MW_DMA_1, 0x2c829c66 },
220 { XFER_MW_DMA_0, 0x2c829d2e },
221
222 { XFER_PIO_4, 0x0c829c62 },
223 { XFER_PIO_3, 0x0c829c84 },
224 { XFER_PIO_2, 0x0c829ca6 },
225 { XFER_PIO_1, 0x0d029d26 },
226 { XFER_PIO_0, 0x0d029d5e },
227 { 0, 0x0d029d26 }
228};
229
230static const struct hpt_clock hpt374_timings_33[] = {
231 { XFER_UDMA_6, 0x12808242 },
232 { XFER_UDMA_5, 0x12848242 },
233 { XFER_UDMA_4, 0x12ac8242 },
234 { XFER_UDMA_3, 0x128c8242 },
235 { XFER_UDMA_2, 0x120c8242 },
236 { XFER_UDMA_1, 0x12148254 },
237 { XFER_UDMA_0, 0x121882ea },
238
239 { XFER_MW_DMA_2, 0x22808242 },
240 { XFER_MW_DMA_1, 0x22808254 },
241 { XFER_MW_DMA_0, 0x228082ea },
242
243 { XFER_PIO_4, 0x0a81f442 },
244 { XFER_PIO_3, 0x0a81f443 },
245 { XFER_PIO_2, 0x0a81f454 },
246 { XFER_PIO_1, 0x0ac1f465 },
247 { XFER_PIO_0, 0x0ac1f48a },
248 { 0, 0x06814e93 }
249};
250
251static const struct hpt_chip hpt370 = {
252 "HPT370",
253 48,
254 {
255 hpt370_timings_33,
256 NULL,
257 NULL,
258 hpt370_timings_66
259 }
260};
261
262static const struct hpt_chip hpt370a = {
263 "HPT370A",
264 48,
265 {
266 hpt370a_timings_33,
267 NULL,
268 hpt370a_timings_50,
269 hpt370a_timings_66
270 }
271};
272
273static const struct hpt_chip hpt372 = {
274 "HPT372",
275 55,
276 {
277 hpt372_timings_33,
278 NULL,
279 hpt372_timings_50,
280 hpt372_timings_66
281 }
282};
283
284static const struct hpt_chip hpt302 = {
285 "HPT302",
286 66,
287 {
288 hpt372_timings_33,
289 NULL,
290 hpt372_timings_50,
291 hpt372_timings_66
292 }
293};
294
295static const struct hpt_chip hpt371 = {
296 "HPT371",
297 66,
298 {
299 hpt372_timings_33,
300 NULL,
301 hpt372_timings_50,
302 hpt372_timings_66
303 }
304};
305
306static const struct hpt_chip hpt372a = {
307 "HPT372A",
308 66,
309 {
310 hpt372_timings_33,
311 NULL,
312 hpt372_timings_50,
313 hpt372_timings_66
314 }
315};
316
317static const struct hpt_chip hpt374 = {
318 "HPT374",
319 48,
320 {
321 hpt374_timings_33,
322 NULL,
323 NULL,
324 NULL
325 }
326};
327
328/**
329 * hpt37x_find_mode - reset the hpt37x bus
330 * @ap: ATA port
331 * @speed: transfer mode
332 *
333 * Return the 32bit register programming information for this channel
334 * that matches the speed provided.
335 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400336
Jeff Garzik669a5db2006-08-29 18:12:40 -0400337static u32 hpt37x_find_mode(struct ata_port *ap, int speed)
338{
339 struct hpt_clock *clocks = ap->host->private_data;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400340
Jeff Garzik669a5db2006-08-29 18:12:40 -0400341 while(clocks->xfer_speed) {
342 if (clocks->xfer_speed == speed)
343 return clocks->timing;
344 clocks++;
345 }
346 BUG();
347 return 0xffffffffU; /* silence compiler warning */
348}
349
350static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr, const char *list[])
351{
Tejun Heoa0cf7332007-01-02 20:18:49 +0900352 unsigned char model_num[ATA_ID_PROD_LEN];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400353 char *s;
354 unsigned int len;
355 int i = 0;
356
Tejun Heoa0cf7332007-01-02 20:18:49 +0900357 ata_id_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400358 s = &model_num[0];
359 len = strnlen(s, sizeof(model_num));
360
361 /* ATAPI specifies that empty space is blank-filled; remove blanks */
362 while ((len > 0) && (s[len - 1] == ' ')) {
363 len--;
364 s[len] = 0;
365 }
366
367 while(list[i] != NULL) {
368 if (!strncmp(list[i], s, len)) {
Jeff Garzik85cd7252006-08-31 00:03:49 -0400369 printk(KERN_WARNING DRV_NAME ": %s is not supported for %s.\n",
Jeff Garzik669a5db2006-08-29 18:12:40 -0400370 modestr, list[i]);
371 return 1;
372 }
373 i++;
374 }
375 return 0;
376}
377
378static const char *bad_ata33[] = {
379 "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3", "Maxtor 90845U3", "Maxtor 90650U2",
380 "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5", "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
381 "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6", "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
382 "Maxtor 90510D4",
383 "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
384 "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7", "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
385 "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5", "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
386 NULL
387};
388
389static const char *bad_ata100_5[] = {
390 "IBM-DTLA-307075",
391 "IBM-DTLA-307060",
392 "IBM-DTLA-307045",
393 "IBM-DTLA-307030",
394 "IBM-DTLA-307020",
395 "IBM-DTLA-307015",
396 "IBM-DTLA-305040",
397 "IBM-DTLA-305030",
398 "IBM-DTLA-305020",
399 "IC35L010AVER07-0",
400 "IC35L020AVER07-0",
401 "IC35L030AVER07-0",
402 "IC35L040AVER07-0",
403 "IC35L060AVER07-0",
404 "WDC AC310200R",
405 NULL
406};
407
408/**
409 * hpt370_filter - mode selection filter
410 * @ap: ATA interface
411 * @adev: ATA device
412 *
413 * Block UDMA on devices that cause trouble with this controller.
414 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400415
Jeff Garzik669a5db2006-08-29 18:12:40 -0400416static unsigned long hpt370_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long mask)
417{
Alan6929da42007-01-05 16:37:01 -0800418 if (adev->class == ATA_DEV_ATA) {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400419 if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
420 mask &= ~ATA_MASK_UDMA;
421 if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
422 mask &= ~(0x1F << ATA_SHIFT_UDMA);
423 }
424 return ata_pci_default_filter(ap, adev, mask);
425}
426
427/**
428 * hpt370a_filter - mode selection filter
429 * @ap: ATA interface
430 * @adev: ATA device
431 *
432 * Block UDMA on devices that cause trouble with this controller.
433 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400434
Jeff Garzik669a5db2006-08-29 18:12:40 -0400435static unsigned long hpt370a_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long mask)
436{
437 if (adev->class != ATA_DEV_ATA) {
438 if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
439 mask &= ~ (0x1F << ATA_SHIFT_UDMA);
440 }
441 return ata_pci_default_filter(ap, adev, mask);
442}
Jeff Garzik85cd7252006-08-31 00:03:49 -0400443
Jeff Garzik669a5db2006-08-29 18:12:40 -0400444/**
445 * hpt37x_pre_reset - reset the hpt37x bus
446 * @ap: ATA port to reset
447 *
448 * Perform the initial reset handling for the 370/372 and 374 func 0
449 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400450
Jeff Garzik669a5db2006-08-29 18:12:40 -0400451static int hpt37x_pre_reset(struct ata_port *ap)
452{
453 u8 scr2, ata66;
454 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxb5bf24b2006-11-08 16:18:26 +0000455 static const struct pci_bits hpt37x_enable_bits[] = {
456 { 0x50, 1, 0x04, 0x04 },
457 { 0x54, 1, 0x04, 0x04 }
458 };
459 if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
460 return -ENOENT;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500461
Jeff Garzik669a5db2006-08-29 18:12:40 -0400462 pci_read_config_byte(pdev, 0x5B, &scr2);
463 pci_write_config_byte(pdev, 0x5B, scr2 & ~0x01);
464 /* Cable register now active */
465 pci_read_config_byte(pdev, 0x5A, &ata66);
466 /* Restore state */
467 pci_write_config_byte(pdev, 0x5B, scr2);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400468
Jeff Garzik669a5db2006-08-29 18:12:40 -0400469 if (ata66 & (1 << ap->port_no))
470 ap->cbl = ATA_CBL_PATA40;
471 else
472 ap->cbl = ATA_CBL_PATA80;
473
474 /* Reset the state machine */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400475 pci_write_config_byte(pdev, 0x50, 0x37);
476 pci_write_config_byte(pdev, 0x54, 0x37);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400477 udelay(100);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400478
Jeff Garzik669a5db2006-08-29 18:12:40 -0400479 return ata_std_prereset(ap);
480}
481
482/**
483 * hpt37x_error_handler - reset the hpt374
484 * @ap: ATA port to reset
485 *
486 * Perform probe for HPT37x, except for HPT374 channel 2
487 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400488
Jeff Garzik669a5db2006-08-29 18:12:40 -0400489static void hpt37x_error_handler(struct ata_port *ap)
490{
491 ata_bmdma_drive_eh(ap, hpt37x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
492}
493
494static int hpt374_pre_reset(struct ata_port *ap)
495{
Alan Coxb5bf24b2006-11-08 16:18:26 +0000496 static const struct pci_bits hpt37x_enable_bits[] = {
497 { 0x50, 1, 0x04, 0x04 },
498 { 0x54, 1, 0x04, 0x04 }
499 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400500 u16 mcr3, mcr6;
501 u8 ata66;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400502 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxb5bf24b2006-11-08 16:18:26 +0000503
504 if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
505 return -ENOENT;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500506
Jeff Garzik669a5db2006-08-29 18:12:40 -0400507 /* Do the extra channel work */
508 pci_read_config_word(pdev, 0x52, &mcr3);
509 pci_read_config_word(pdev, 0x56, &mcr6);
510 /* Set bit 15 of 0x52 to enable TCBLID as input
511 Set bit 15 of 0x56 to enable FCBLID as input
512 */
513 pci_write_config_word(pdev, 0x52, mcr3 | 0x8000);
514 pci_write_config_word(pdev, 0x56, mcr6 | 0x8000);
515 pci_read_config_byte(pdev, 0x5A, &ata66);
516 /* Reset TCBLID/FCBLID to output */
517 pci_write_config_word(pdev, 0x52, mcr3);
518 pci_write_config_word(pdev, 0x56, mcr6);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400519
Jeff Garzik669a5db2006-08-29 18:12:40 -0400520 if (ata66 & (1 << ap->port_no))
521 ap->cbl = ATA_CBL_PATA40;
522 else
523 ap->cbl = ATA_CBL_PATA80;
524
525 /* Reset the state machine */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400526 pci_write_config_byte(pdev, 0x50, 0x37);
527 pci_write_config_byte(pdev, 0x54, 0x37);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400528 udelay(100);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400529
Jeff Garzik669a5db2006-08-29 18:12:40 -0400530 return ata_std_prereset(ap);
531}
532
533/**
534 * hpt374_error_handler - reset the hpt374
535 * @classes:
536 *
537 * The 374 cable detect is a little different due to the extra
538 * channels. The function 0 channels work like usual but function 1
539 * is special
540 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400541
Jeff Garzik669a5db2006-08-29 18:12:40 -0400542static void hpt374_error_handler(struct ata_port *ap)
543{
544 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400545
Jeff Garzik669a5db2006-08-29 18:12:40 -0400546 if (!(PCI_FUNC(pdev->devfn) & 1))
547 hpt37x_error_handler(ap);
548 else
549 ata_bmdma_drive_eh(ap, hpt374_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
550}
551
552/**
553 * hpt370_set_piomode - PIO setup
554 * @ap: ATA interface
555 * @adev: device on the interface
556 *
Jeff Garzik85cd7252006-08-31 00:03:49 -0400557 * Perform PIO mode setup.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400558 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400559
Jeff Garzik669a5db2006-08-29 18:12:40 -0400560static void hpt370_set_piomode(struct ata_port *ap, struct ata_device *adev)
561{
562 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
563 u32 addr1, addr2;
564 u32 reg;
565 u32 mode;
566 u8 fast;
567
568 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
569 addr2 = 0x51 + 4 * ap->port_no;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400570
Jeff Garzik669a5db2006-08-29 18:12:40 -0400571 /* Fast interrupt prediction disable, hold off interrupt disable */
572 pci_read_config_byte(pdev, addr2, &fast);
573 fast &= ~0x02;
574 fast |= 0x01;
575 pci_write_config_byte(pdev, addr2, fast);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400576
Jeff Garzik669a5db2006-08-29 18:12:40 -0400577 pci_read_config_dword(pdev, addr1, &reg);
578 mode = hpt37x_find_mode(ap, adev->pio_mode);
579 mode &= ~0x8000000; /* No FIFO in PIO */
580 mode &= ~0x30070000; /* Leave config bits alone */
581 reg &= 0x30070000; /* Strip timing bits */
582 pci_write_config_dword(pdev, addr1, reg | mode);
583}
584
585/**
586 * hpt370_set_dmamode - DMA timing setup
587 * @ap: ATA interface
588 * @adev: Device being configured
589 *
590 * Set up the channel for MWDMA or UDMA modes. Much the same as with
591 * PIO, load the mode number and then set MWDMA or UDMA flag.
592 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400593
Jeff Garzik669a5db2006-08-29 18:12:40 -0400594static void hpt370_set_dmamode(struct ata_port *ap, struct ata_device *adev)
595{
596 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
597 u32 addr1, addr2;
598 u32 reg;
599 u32 mode;
600 u8 fast;
601
602 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
603 addr2 = 0x51 + 4 * ap->port_no;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400604
Jeff Garzik669a5db2006-08-29 18:12:40 -0400605 /* Fast interrupt prediction disable, hold off interrupt disable */
606 pci_read_config_byte(pdev, addr2, &fast);
607 fast &= ~0x02;
608 fast |= 0x01;
609 pci_write_config_byte(pdev, addr2, fast);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400610
Jeff Garzik669a5db2006-08-29 18:12:40 -0400611 pci_read_config_dword(pdev, addr1, &reg);
612 mode = hpt37x_find_mode(ap, adev->dma_mode);
613 mode |= 0x8000000; /* FIFO in MWDMA or UDMA */
614 mode &= ~0xC0000000; /* Leave config bits alone */
615 reg &= 0xC0000000; /* Strip timing bits */
616 pci_write_config_dword(pdev, addr1, reg | mode);
617}
618
619/**
620 * hpt370_bmdma_start - DMA engine begin
621 * @qc: ATA command
622 *
623 * The 370 and 370A want us to reset the DMA engine each time we
624 * use it. The 372 and later are fine.
625 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400626
Jeff Garzik669a5db2006-08-29 18:12:40 -0400627static void hpt370_bmdma_start(struct ata_queued_cmd *qc)
628{
629 struct ata_port *ap = qc->ap;
630 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
631 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
632 udelay(10);
633 ata_bmdma_start(qc);
634}
635
636/**
637 * hpt370_bmdma_end - DMA engine stop
638 * @qc: ATA command
639 *
640 * Work around the HPT370 DMA engine.
641 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400642
Jeff Garzik669a5db2006-08-29 18:12:40 -0400643static void hpt370_bmdma_stop(struct ata_queued_cmd *qc)
644{
645 struct ata_port *ap = qc->ap;
646 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
647 u8 dma_stat = inb(ap->ioaddr.bmdma_addr + 2);
648 u8 dma_cmd;
649 unsigned long bmdma = ap->ioaddr.bmdma_addr;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400650
Jeff Garzik669a5db2006-08-29 18:12:40 -0400651 if (dma_stat & 0x01) {
652 udelay(20);
653 dma_stat = inb(bmdma + 2);
654 }
655 if (dma_stat & 0x01) {
656 /* Clear the engine */
657 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
658 udelay(10);
659 /* Stop DMA */
660 dma_cmd = inb(bmdma );
661 outb(dma_cmd & 0xFE, bmdma);
662 /* Clear Error */
663 dma_stat = inb(bmdma + 2);
664 outb(dma_stat | 0x06 , bmdma + 2);
665 /* Clear the engine */
666 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
667 udelay(10);
668 }
669 ata_bmdma_stop(qc);
670}
671
672/**
673 * hpt372_set_piomode - PIO setup
674 * @ap: ATA interface
675 * @adev: device on the interface
676 *
Jeff Garzik85cd7252006-08-31 00:03:49 -0400677 * Perform PIO mode setup.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400678 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400679
Jeff Garzik669a5db2006-08-29 18:12:40 -0400680static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
681{
682 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
683 u32 addr1, addr2;
684 u32 reg;
685 u32 mode;
686 u8 fast;
687
688 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
689 addr2 = 0x51 + 4 * ap->port_no;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400690
Jeff Garzik669a5db2006-08-29 18:12:40 -0400691 /* Fast interrupt prediction disable, hold off interrupt disable */
692 pci_read_config_byte(pdev, addr2, &fast);
693 fast &= ~0x07;
694 pci_write_config_byte(pdev, addr2, fast);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400695
Jeff Garzik669a5db2006-08-29 18:12:40 -0400696 pci_read_config_dword(pdev, addr1, &reg);
697 mode = hpt37x_find_mode(ap, adev->pio_mode);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400698
Jeff Garzik669a5db2006-08-29 18:12:40 -0400699 printk("Find mode for %d reports %X\n", adev->pio_mode, mode);
700 mode &= ~0x80000000; /* No FIFO in PIO */
701 mode &= ~0x30070000; /* Leave config bits alone */
702 reg &= 0x30070000; /* Strip timing bits */
703 pci_write_config_dword(pdev, addr1, reg | mode);
704}
705
706/**
707 * hpt372_set_dmamode - DMA timing setup
708 * @ap: ATA interface
709 * @adev: Device being configured
710 *
711 * Set up the channel for MWDMA or UDMA modes. Much the same as with
712 * PIO, load the mode number and then set MWDMA or UDMA flag.
713 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400714
Jeff Garzik669a5db2006-08-29 18:12:40 -0400715static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
716{
717 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
718 u32 addr1, addr2;
719 u32 reg;
720 u32 mode;
721 u8 fast;
722
723 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
724 addr2 = 0x51 + 4 * ap->port_no;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400725
Jeff Garzik669a5db2006-08-29 18:12:40 -0400726 /* Fast interrupt prediction disable, hold off interrupt disable */
727 pci_read_config_byte(pdev, addr2, &fast);
728 fast &= ~0x07;
729 pci_write_config_byte(pdev, addr2, fast);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400730
Jeff Garzik669a5db2006-08-29 18:12:40 -0400731 pci_read_config_dword(pdev, addr1, &reg);
732 mode = hpt37x_find_mode(ap, adev->dma_mode);
733 printk("Find mode for DMA %d reports %X\n", adev->dma_mode, mode);
734 mode &= ~0xC0000000; /* Leave config bits alone */
735 mode |= 0x80000000; /* FIFO in MWDMA or UDMA */
736 reg &= 0xC0000000; /* Strip timing bits */
737 pci_write_config_dword(pdev, addr1, reg | mode);
738}
739
740/**
741 * hpt37x_bmdma_end - DMA engine stop
742 * @qc: ATA command
743 *
744 * Clean up after the HPT372 and later DMA engine
745 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400746
Jeff Garzik669a5db2006-08-29 18:12:40 -0400747static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
748{
749 struct ata_port *ap = qc->ap;
750 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan6929da42007-01-05 16:37:01 -0800751 int mscreg = 0x50 + 4 * ap->port_no;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400752 u8 bwsr_stat, msc_stat;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400753
Jeff Garzik669a5db2006-08-29 18:12:40 -0400754 pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
755 pci_read_config_byte(pdev, mscreg, &msc_stat);
756 if (bwsr_stat & (1 << ap->port_no))
757 pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
758 ata_bmdma_stop(qc);
759}
760
761
762static struct scsi_host_template hpt37x_sht = {
763 .module = THIS_MODULE,
764 .name = DRV_NAME,
765 .ioctl = ata_scsi_ioctl,
766 .queuecommand = ata_scsi_queuecmd,
767 .can_queue = ATA_DEF_QUEUE,
768 .this_id = ATA_SHT_THIS_ID,
769 .sg_tablesize = LIBATA_MAX_PRD,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400770 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
771 .emulated = ATA_SHT_EMULATED,
772 .use_clustering = ATA_SHT_USE_CLUSTERING,
773 .proc_name = DRV_NAME,
774 .dma_boundary = ATA_DMA_BOUNDARY,
775 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900776 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400777 .bios_param = ata_std_bios_param,
778};
779
780/*
781 * Configuration for HPT370
782 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400783
Jeff Garzik669a5db2006-08-29 18:12:40 -0400784static struct ata_port_operations hpt370_port_ops = {
785 .port_disable = ata_port_disable,
786 .set_piomode = hpt370_set_piomode,
787 .set_dmamode = hpt370_set_dmamode,
788 .mode_filter = hpt370_filter,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400789
Jeff Garzik669a5db2006-08-29 18:12:40 -0400790 .tf_load = ata_tf_load,
791 .tf_read = ata_tf_read,
792 .check_status = ata_check_status,
793 .exec_command = ata_exec_command,
794 .dev_select = ata_std_dev_select,
795
796 .freeze = ata_bmdma_freeze,
797 .thaw = ata_bmdma_thaw,
798 .error_handler = hpt37x_error_handler,
799 .post_internal_cmd = ata_bmdma_post_internal_cmd,
800
801 .bmdma_setup = ata_bmdma_setup,
802 .bmdma_start = hpt370_bmdma_start,
803 .bmdma_stop = hpt370_bmdma_stop,
804 .bmdma_status = ata_bmdma_status,
805
806 .qc_prep = ata_qc_prep,
807 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400808
Jeff Garzik669a5db2006-08-29 18:12:40 -0400809 .data_xfer = ata_pio_data_xfer,
810
811 .irq_handler = ata_interrupt,
812 .irq_clear = ata_bmdma_irq_clear,
813
814 .port_start = ata_port_start,
815 .port_stop = ata_port_stop,
816 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400817};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400818
819/*
820 * Configuration for HPT370A. Close to 370 but less filters
821 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400822
Jeff Garzik669a5db2006-08-29 18:12:40 -0400823static struct ata_port_operations hpt370a_port_ops = {
824 .port_disable = ata_port_disable,
825 .set_piomode = hpt370_set_piomode,
826 .set_dmamode = hpt370_set_dmamode,
827 .mode_filter = hpt370a_filter,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400828
Jeff Garzik669a5db2006-08-29 18:12:40 -0400829 .tf_load = ata_tf_load,
830 .tf_read = ata_tf_read,
831 .check_status = ata_check_status,
832 .exec_command = ata_exec_command,
833 .dev_select = ata_std_dev_select,
834
835 .freeze = ata_bmdma_freeze,
836 .thaw = ata_bmdma_thaw,
837 .error_handler = hpt37x_error_handler,
838 .post_internal_cmd = ata_bmdma_post_internal_cmd,
839
840 .bmdma_setup = ata_bmdma_setup,
841 .bmdma_start = hpt370_bmdma_start,
842 .bmdma_stop = hpt370_bmdma_stop,
843 .bmdma_status = ata_bmdma_status,
844
845 .qc_prep = ata_qc_prep,
846 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400847
Jeff Garzik669a5db2006-08-29 18:12:40 -0400848 .data_xfer = ata_pio_data_xfer,
849
850 .irq_handler = ata_interrupt,
851 .irq_clear = ata_bmdma_irq_clear,
852
853 .port_start = ata_port_start,
854 .port_stop = ata_port_stop,
855 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400856};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400857
858/*
859 * Configuration for HPT372, HPT371, HPT302. Slightly different PIO
860 * and DMA mode setting functionality.
861 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400862
Jeff Garzik669a5db2006-08-29 18:12:40 -0400863static struct ata_port_operations hpt372_port_ops = {
864 .port_disable = ata_port_disable,
865 .set_piomode = hpt372_set_piomode,
866 .set_dmamode = hpt372_set_dmamode,
867 .mode_filter = ata_pci_default_filter,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400868
Jeff Garzik669a5db2006-08-29 18:12:40 -0400869 .tf_load = ata_tf_load,
870 .tf_read = ata_tf_read,
871 .check_status = ata_check_status,
872 .exec_command = ata_exec_command,
873 .dev_select = ata_std_dev_select,
874
875 .freeze = ata_bmdma_freeze,
876 .thaw = ata_bmdma_thaw,
877 .error_handler = hpt37x_error_handler,
878 .post_internal_cmd = ata_bmdma_post_internal_cmd,
879
880 .bmdma_setup = ata_bmdma_setup,
881 .bmdma_start = ata_bmdma_start,
882 .bmdma_stop = hpt37x_bmdma_stop,
883 .bmdma_status = ata_bmdma_status,
884
885 .qc_prep = ata_qc_prep,
886 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400887
Jeff Garzik669a5db2006-08-29 18:12:40 -0400888 .data_xfer = ata_pio_data_xfer,
889
890 .irq_handler = ata_interrupt,
891 .irq_clear = ata_bmdma_irq_clear,
892
893 .port_start = ata_port_start,
894 .port_stop = ata_port_stop,
895 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400896};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400897
898/*
899 * Configuration for HPT374. Mode setting works like 372 and friends
900 * but we have a different cable detection procedure.
901 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400902
Jeff Garzik669a5db2006-08-29 18:12:40 -0400903static struct ata_port_operations hpt374_port_ops = {
904 .port_disable = ata_port_disable,
905 .set_piomode = hpt372_set_piomode,
906 .set_dmamode = hpt372_set_dmamode,
907 .mode_filter = ata_pci_default_filter,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400908
Jeff Garzik669a5db2006-08-29 18:12:40 -0400909 .tf_load = ata_tf_load,
910 .tf_read = ata_tf_read,
911 .check_status = ata_check_status,
912 .exec_command = ata_exec_command,
913 .dev_select = ata_std_dev_select,
914
915 .freeze = ata_bmdma_freeze,
916 .thaw = ata_bmdma_thaw,
917 .error_handler = hpt374_error_handler,
918 .post_internal_cmd = ata_bmdma_post_internal_cmd,
919
920 .bmdma_setup = ata_bmdma_setup,
921 .bmdma_start = ata_bmdma_start,
922 .bmdma_stop = hpt37x_bmdma_stop,
923 .bmdma_status = ata_bmdma_status,
924
925 .qc_prep = ata_qc_prep,
926 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400927
Jeff Garzik669a5db2006-08-29 18:12:40 -0400928 .data_xfer = ata_pio_data_xfer,
929
930 .irq_handler = ata_interrupt,
931 .irq_clear = ata_bmdma_irq_clear,
932
933 .port_start = ata_port_start,
934 .port_stop = ata_port_stop,
935 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400936};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400937
938/**
939 * htp37x_clock_slot - Turn timing to PC clock entry
940 * @freq: Reported frequency timing
941 * @base: Base timing
942 *
943 * Turn the timing data intoa clock slot (0 for 33, 1 for 40, 2 for 50
944 * and 3 for 66Mhz)
945 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400946
Jeff Garzik669a5db2006-08-29 18:12:40 -0400947static int hpt37x_clock_slot(unsigned int freq, unsigned int base)
948{
949 unsigned int f = (base * freq) / 192; /* Mhz */
950 if (f < 40)
951 return 0; /* 33Mhz slot */
952 if (f < 45)
953 return 1; /* 40Mhz slot */
954 if (f < 55)
955 return 2; /* 50Mhz slot */
956 return 3; /* 60Mhz slot */
957}
958
959/**
960 * hpt37x_calibrate_dpll - Calibrate the DPLL loop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400961 * @dev: PCI device
Jeff Garzik669a5db2006-08-29 18:12:40 -0400962 *
963 * Perform a calibration cycle on the HPT37x DPLL. Returns 1 if this
964 * succeeds
965 */
966
967static int hpt37x_calibrate_dpll(struct pci_dev *dev)
968{
969 u8 reg5b;
970 u32 reg5c;
971 int tries;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400972
Jeff Garzik669a5db2006-08-29 18:12:40 -0400973 for(tries = 0; tries < 0x5000; tries++) {
974 udelay(50);
975 pci_read_config_byte(dev, 0x5b, &reg5b);
976 if (reg5b & 0x80) {
977 /* See if it stays set */
978 for(tries = 0; tries < 0x1000; tries ++) {
979 pci_read_config_byte(dev, 0x5b, &reg5b);
980 /* Failed ? */
981 if ((reg5b & 0x80) == 0)
982 return 0;
983 }
984 /* Turn off tuning, we have the DPLL set */
985 pci_read_config_dword(dev, 0x5c, &reg5c);
986 pci_write_config_dword(dev, 0x5c, reg5c & ~ 0x100);
987 return 1;
988 }
989 }
990 /* Never went stable */
991 return 0;
992}
993/**
994 * hpt37x_init_one - Initialise an HPT37X/302
995 * @dev: PCI device
996 * @id: Entry in match table
997 *
998 * Initialise an HPT37x device. There are some interesting complications
999 * here. Firstly the chip may report 366 and be one of several variants.
1000 * Secondly all the timings depend on the clock for the chip which we must
1001 * detect and look up
1002 *
1003 * This is the known chip mappings. It may be missing a couple of later
1004 * releases.
1005 *
1006 * Chip version PCI Rev Notes
1007 * HPT366 4 (HPT366) 0 Other driver
1008 * HPT366 4 (HPT366) 1 Other driver
1009 * HPT368 4 (HPT366) 2 Other driver
1010 * HPT370 4 (HPT366) 3 UDMA100
1011 * HPT370A 4 (HPT366) 4 UDMA100
1012 * HPT372 4 (HPT366) 5 UDMA133 (1)
1013 * HPT372N 4 (HPT366) 6 Other driver
1014 * HPT372A 5 (HPT372) 1 UDMA133 (1)
1015 * HPT372N 5 (HPT372) 2 Other driver
1016 * HPT302 6 (HPT302) 1 UDMA133
1017 * HPT302N 6 (HPT302) 2 Other driver
1018 * HPT371 7 (HPT371) * UDMA133
1019 * HPT374 8 (HPT374) * UDMA133 4 channel
1020 * HPT372N 9 (HPT372N) * Other driver
1021 *
1022 * (1) UDMA133 support depends on the bus clock
1023 */
Jeff Garzik85cd7252006-08-31 00:03:49 -04001024
Jeff Garzik669a5db2006-08-29 18:12:40 -04001025static int hpt37x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1026{
1027 /* HPT370 - UDMA100 */
1028 static struct ata_port_info info_hpt370 = {
1029 .sht = &hpt37x_sht,
1030 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
1031 .pio_mask = 0x1f,
1032 .mwdma_mask = 0x07,
1033 .udma_mask = 0x3f,
1034 .port_ops = &hpt370_port_ops
1035 };
1036 /* HPT370A - UDMA100 */
1037 static struct ata_port_info info_hpt370a = {
1038 .sht = &hpt37x_sht,
1039 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
1040 .pio_mask = 0x1f,
1041 .mwdma_mask = 0x07,
1042 .udma_mask = 0x3f,
1043 .port_ops = &hpt370a_port_ops
1044 };
1045 /* HPT371, 372 and friends - UDMA133 */
1046 static struct ata_port_info info_hpt372 = {
1047 .sht = &hpt37x_sht,
1048 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
1049 .pio_mask = 0x1f,
1050 .mwdma_mask = 0x07,
1051 .udma_mask = 0x7f,
1052 .port_ops = &hpt372_port_ops
1053 };
1054 /* HPT371, 372 and friends - UDMA100 at 50MHz clock */
1055 static struct ata_port_info info_hpt372_50 = {
1056 .sht = &hpt37x_sht,
1057 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
1058 .pio_mask = 0x1f,
1059 .mwdma_mask = 0x07,
1060 .udma_mask = 0x3f,
1061 .port_ops = &hpt372_port_ops
1062 };
1063 /* HPT374 - UDMA133 */
1064 static struct ata_port_info info_hpt374 = {
1065 .sht = &hpt37x_sht,
1066 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
1067 .pio_mask = 0x1f,
1068 .mwdma_mask = 0x07,
1069 .udma_mask = 0x7f,
1070 .port_ops = &hpt374_port_ops
1071 };
1072
1073 static const int MHz[4] = { 33, 40, 50, 66 };
1074
1075 struct ata_port_info *port_info[2];
1076 struct ata_port_info *port;
1077
1078 u8 irqmask;
1079 u32 class_rev;
1080 u32 freq;
1081
1082 const struct hpt_chip *chip_table;
1083 int clock_slot;
1084
1085 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1086 class_rev &= 0xFF;
Jeff Garzik85cd7252006-08-31 00:03:49 -04001087
Jeff Garzik669a5db2006-08-29 18:12:40 -04001088 if (dev->device == PCI_DEVICE_ID_TTI_HPT366) {
1089 /* May be a later chip in disguise. Check */
1090 /* Older chips are in the HPT366 driver. Ignore them */
1091 if (class_rev < 3)
1092 return -ENODEV;
1093 /* N series chips have their own driver. Ignore */
1094 if (class_rev == 6)
1095 return -ENODEV;
1096
Jeff Garzik85cd7252006-08-31 00:03:49 -04001097 switch(class_rev) {
Jeff Garzik669a5db2006-08-29 18:12:40 -04001098 case 3:
1099 port = &info_hpt370;
1100 chip_table = &hpt370;
1101 break;
1102 case 4:
1103 port = &info_hpt370a;
1104 chip_table = &hpt370a;
1105 break;
1106 case 5:
1107 port = &info_hpt372;
1108 chip_table = &hpt372;
1109 break;
1110 default:
1111 printk(KERN_ERR "pata_hpt37x: Unknown HPT366 subtype please report (%d).\n", class_rev);
1112 return -ENODEV;
1113 }
1114 } else {
1115 switch(dev->device) {
1116 case PCI_DEVICE_ID_TTI_HPT372:
1117 /* 372N if rev >= 2*/
1118 if (class_rev >= 2)
1119 return -ENODEV;
1120 port = &info_hpt372;
1121 chip_table = &hpt372a;
1122 break;
1123 case PCI_DEVICE_ID_TTI_HPT302:
1124 /* 302N if rev > 1 */
1125 if (class_rev > 1)
1126 return -ENODEV;
1127 port = &info_hpt372;
1128 /* Check this */
1129 chip_table = &hpt302;
1130 break;
1131 case PCI_DEVICE_ID_TTI_HPT371:
1132 port = &info_hpt372;
1133 chip_table = &hpt371;
1134 break;
1135 case PCI_DEVICE_ID_TTI_HPT374:
1136 chip_table = &hpt374;
1137 port = &info_hpt374;
1138 break;
1139 default:
1140 printk(KERN_ERR "pata_hpt37x: PCI table is bogus please report (%d).\n", dev->device);
1141 return -ENODEV;
1142 }
1143 }
1144 /* Ok so this is a chip we support */
1145
1146 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
1147 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
1148 pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
1149 pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
1150
1151 pci_read_config_byte(dev, 0x5A, &irqmask);
1152 irqmask &= ~0x10;
1153 pci_write_config_byte(dev, 0x5a, irqmask);
1154
1155 /*
1156 * default to pci clock. make sure MA15/16 are set to output
1157 * to prevent drives having problems with 40-pin cables. Needed
1158 * for some drives such as IBM-DTLA which will not enter ready
1159 * state on reset when PDIAG is a input.
1160 */
1161
Jeff Garzik85cd7252006-08-31 00:03:49 -04001162 pci_write_config_byte(dev, 0x5b, 0x23);
1163
Jeff Garzik669a5db2006-08-29 18:12:40 -04001164 pci_read_config_dword(dev, 0x70, &freq);
1165 if ((freq >> 12) != 0xABCDE) {
1166 int i;
1167 u8 sr;
1168 u32 total = 0;
Jeff Garzik85cd7252006-08-31 00:03:49 -04001169
Jeff Garzik669a5db2006-08-29 18:12:40 -04001170 printk(KERN_WARNING "pata_hpt37x: BIOS has not set timing clocks.\n");
Jeff Garzik85cd7252006-08-31 00:03:49 -04001171
Jeff Garzik669a5db2006-08-29 18:12:40 -04001172 /* This is the process the HPT371 BIOS is reported to use */
1173 for(i = 0; i < 128; i++) {
1174 pci_read_config_byte(dev, 0x78, &sr);
1175 total += sr;
1176 udelay(15);
1177 }
1178 freq = total / 128;
1179 }
1180 freq &= 0x1FF;
Jeff Garzik85cd7252006-08-31 00:03:49 -04001181
Jeff Garzik669a5db2006-08-29 18:12:40 -04001182 /*
1183 * Turn the frequency check into a band and then find a timing
1184 * table to match it.
1185 */
Jeff Garzik85cd7252006-08-31 00:03:49 -04001186
Jeff Garzik669a5db2006-08-29 18:12:40 -04001187 clock_slot = hpt37x_clock_slot(freq, chip_table->base);
1188 if (chip_table->clocks[clock_slot] == NULL) {
1189 /*
1190 * We need to try PLL mode instead
1191 */
1192 unsigned int f_low = (MHz[clock_slot] * chip_table->base) / 192;
1193 unsigned int f_high = f_low + 2;
1194 int adjust;
Jeff Garzik85cd7252006-08-31 00:03:49 -04001195
Jeff Garzik669a5db2006-08-29 18:12:40 -04001196 for(adjust = 0; adjust < 8; adjust++) {
1197 if (hpt37x_calibrate_dpll(dev))
1198 break;
1199 /* See if it'll settle at a fractionally different clock */
1200 if ((adjust & 3) == 3) {
1201 f_low --;
1202 f_high ++;
1203 }
1204 pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low);
1205 }
1206 if (adjust == 8) {
1207 printk(KERN_WARNING "hpt37x: DPLL did not stabilize.\n");
1208 return -ENODEV;
1209 }
1210 /* Check if this works for all cases */
1211 port->private_data = (void *)hpt370_timings_66;
Jeff Garzik85cd7252006-08-31 00:03:49 -04001212
Jeff Garzik669a5db2006-08-29 18:12:40 -04001213 printk(KERN_INFO "hpt37x: Bus clock %dMHz, using DPLL.\n", MHz[clock_slot]);
1214 } else {
1215 port->private_data = (void *)chip_table->clocks[clock_slot];
1216 /*
1217 * Perform a final fixup. The 371 and 372 clock determines
1218 * if UDMA133 is available.
1219 */
Jeff Garzik85cd7252006-08-31 00:03:49 -04001220
Jeff Garzik669a5db2006-08-29 18:12:40 -04001221 if (clock_slot == 2 && chip_table == &hpt372) { /* 50Mhz */
1222 printk(KERN_WARNING "pata_hpt37x: No UDMA133 support available with 50MHz bus clock.\n");
1223 if (port == &info_hpt372)
1224 port = &info_hpt372_50;
1225 else BUG();
1226 }
1227 printk(KERN_INFO "hpt37x: %s: Bus clock %dMHz.\n", chip_table->name, MHz[clock_slot]);
1228 }
Jeff Garzik85cd7252006-08-31 00:03:49 -04001229 port_info[0] = port_info[1] = port;
Jeff Garzik669a5db2006-08-29 18:12:40 -04001230 /* Now kick off ATA set up */
1231 return ata_pci_init_one(dev, port_info, 2);
1232}
1233
Jeff Garzik2d2744f2006-09-28 20:21:59 -04001234static const struct pci_device_id hpt37x[] = {
1235 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
1236 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
1237 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
1238 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
1239 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
1240
1241 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -04001242};
1243
1244static struct pci_driver hpt37x_pci_driver = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -04001245 .name = DRV_NAME,
Jeff Garzik669a5db2006-08-29 18:12:40 -04001246 .id_table = hpt37x,
1247 .probe = hpt37x_init_one,
1248 .remove = ata_pci_remove_one
1249};
1250
1251static int __init hpt37x_init(void)
1252{
1253 return pci_register_driver(&hpt37x_pci_driver);
1254}
1255
Jeff Garzik669a5db2006-08-29 18:12:40 -04001256static void __exit hpt37x_exit(void)
1257{
1258 pci_unregister_driver(&hpt37x_pci_driver);
1259}
1260
Jeff Garzik669a5db2006-08-29 18:12:40 -04001261MODULE_AUTHOR("Alan Cox");
1262MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
1263MODULE_LICENSE("GPL");
1264MODULE_DEVICE_TABLE(pci, hpt37x);
1265MODULE_VERSION(DRV_VERSION);
1266
1267module_init(hpt37x_init);
1268module_exit(hpt37x_exit);