falconide/q40ide: add ->atapi_*put_bytes and ->ata_*put_data methods (take 2)
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / ide / ide-iops.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
3 * Copyright (C) 2003 Red Hat <alan@redhat.com>
4 *
5 */
6
1da177e4
LT
7#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/string.h>
10#include <linux/kernel.h>
11#include <linux/timer.h>
12#include <linux/mm.h>
13#include <linux/interrupt.h>
14#include <linux/major.h>
15#include <linux/errno.h>
16#include <linux/genhd.h>
17#include <linux/blkpg.h>
18#include <linux/slab.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hdreg.h>
22#include <linux/ide.h>
23#include <linux/bitops.h>
1e86240f 24#include <linux/nmi.h>
1da177e4
LT
25
26#include <asm/byteorder.h>
27#include <asm/irq.h>
28#include <asm/uaccess.h>
29#include <asm/io.h>
30
31/*
32 * Conventional PIO operations for ATA devices
33 */
34
35static u8 ide_inb (unsigned long port)
36{
37 return (u8) inb(port);
38}
39
40static u16 ide_inw (unsigned long port)
41{
42 return (u16) inw(port);
43}
44
45static void ide_insw (unsigned long port, void *addr, u32 count)
46{
47 insw(port, addr, count);
48}
49
1da177e4
LT
50static void ide_insl (unsigned long port, void *addr, u32 count)
51{
52 insl(port, addr, count);
53}
54
55static void ide_outb (u8 val, unsigned long port)
56{
57 outb(val, port);
58}
59
60static void ide_outbsync (ide_drive_t *drive, u8 addr, unsigned long port)
61{
62 outb(addr, port);
63}
64
65static void ide_outw (u16 val, unsigned long port)
66{
67 outw(val, port);
68}
69
70static void ide_outsw (unsigned long port, void *addr, u32 count)
71{
72 outsw(port, addr, count);
73}
74
1da177e4
LT
75static void ide_outsl (unsigned long port, void *addr, u32 count)
76{
77 outsl(port, addr, count);
78}
79
80void default_hwif_iops (ide_hwif_t *hwif)
81{
82 hwif->OUTB = ide_outb;
83 hwif->OUTBSYNC = ide_outbsync;
84 hwif->OUTW = ide_outw;
1da177e4
LT
85 hwif->OUTSW = ide_outsw;
86 hwif->OUTSL = ide_outsl;
87 hwif->INB = ide_inb;
88 hwif->INW = ide_inw;
1da177e4
LT
89 hwif->INSW = ide_insw;
90 hwif->INSL = ide_insl;
91}
92
1da177e4
LT
93/*
94 * MMIO operations, typically used for SATA controllers
95 */
96
97static u8 ide_mm_inb (unsigned long port)
98{
99 return (u8) readb((void __iomem *) port);
100}
101
102static u16 ide_mm_inw (unsigned long port)
103{
104 return (u16) readw((void __iomem *) port);
105}
106
107static void ide_mm_insw (unsigned long port, void *addr, u32 count)
108{
109 __ide_mm_insw((void __iomem *) port, addr, count);
110}
111
1da177e4
LT
112static void ide_mm_insl (unsigned long port, void *addr, u32 count)
113{
114 __ide_mm_insl((void __iomem *) port, addr, count);
115}
116
117static void ide_mm_outb (u8 value, unsigned long port)
118{
119 writeb(value, (void __iomem *) port);
120}
121
122static void ide_mm_outbsync (ide_drive_t *drive, u8 value, unsigned long port)
123{
124 writeb(value, (void __iomem *) port);
125}
126
127static void ide_mm_outw (u16 value, unsigned long port)
128{
129 writew(value, (void __iomem *) port);
130}
131
132static void ide_mm_outsw (unsigned long port, void *addr, u32 count)
133{
134 __ide_mm_outsw((void __iomem *) port, addr, count);
135}
136
1da177e4
LT
137static void ide_mm_outsl (unsigned long port, void *addr, u32 count)
138{
139 __ide_mm_outsl((void __iomem *) port, addr, count);
140}
141
142void default_hwif_mmiops (ide_hwif_t *hwif)
143{
144 hwif->OUTB = ide_mm_outb;
145 /* Most systems will need to override OUTBSYNC, alas however
146 this one is controller specific! */
147 hwif->OUTBSYNC = ide_mm_outbsync;
148 hwif->OUTW = ide_mm_outw;
1da177e4
LT
149 hwif->OUTSW = ide_mm_outsw;
150 hwif->OUTSL = ide_mm_outsl;
151 hwif->INB = ide_mm_inb;
152 hwif->INW = ide_mm_inw;
1da177e4
LT
153 hwif->INSW = ide_mm_insw;
154 hwif->INSL = ide_mm_insl;
155}
156
157EXPORT_SYMBOL(default_hwif_mmiops);
158
1da177e4
LT
159void SELECT_DRIVE (ide_drive_t *drive)
160{
23579a2a 161 ide_hwif_t *hwif = drive->hwif;
ac95beed 162 const struct ide_port_ops *port_ops = hwif->port_ops;
23579a2a 163
ac95beed
BZ
164 if (port_ops && port_ops->selectproc)
165 port_ops->selectproc(drive);
23579a2a 166
4c3032d8 167 hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
1da177e4
LT
168}
169
1da177e4
LT
170void SELECT_MASK (ide_drive_t *drive, int mask)
171{
ac95beed
BZ
172 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
173
174 if (port_ops && port_ops->maskproc)
175 port_ops->maskproc(drive, mask);
1da177e4
LT
176}
177
1da177e4
LT
178/*
179 * Some localbus EIDE interfaces require a special access sequence
180 * when using 32-bit I/O instructions to transfer data. We call this
181 * the "vlb_sync" sequence, which consists of three successive reads
182 * of the sector count register location, with interrupts disabled
183 * to ensure that the reads all happen together.
184 */
185static void ata_vlb_sync(ide_drive_t *drive, unsigned long port)
186{
187 (void) HWIF(drive)->INB(port);
188 (void) HWIF(drive)->INB(port);
189 (void) HWIF(drive)->INB(port);
190}
191
192/*
193 * This is used for most PIO data transfers *from* the IDE interface
194 */
92d3ab27
BZ
195static void ata_input_data(ide_drive_t *drive, struct request *rq,
196 void *buffer, u32 wcount)
1da177e4 197{
4c3032d8
BZ
198 ide_hwif_t *hwif = drive->hwif;
199 struct ide_io_ports *io_ports = &hwif->io_ports;
200 u8 io_32bit = drive->io_32bit;
1da177e4
LT
201
202 if (io_32bit) {
203 if (io_32bit & 2) {
204 unsigned long flags;
23579a2a 205
1da177e4 206 local_irq_save(flags);
4c3032d8
BZ
207 ata_vlb_sync(drive, io_ports->nsect_addr);
208 hwif->INSL(io_ports->data_addr, buffer, wcount);
1da177e4
LT
209 local_irq_restore(flags);
210 } else
4c3032d8 211 hwif->INSL(io_ports->data_addr, buffer, wcount);
23579a2a 212 } else
4c3032d8 213 hwif->INSW(io_ports->data_addr, buffer, wcount << 1);
1da177e4
LT
214}
215
216/*
217 * This is used for most PIO data transfers *to* the IDE interface
218 */
92d3ab27
BZ
219static void ata_output_data(ide_drive_t *drive, struct request *rq,
220 void *buffer, u32 wcount)
1da177e4 221{
4c3032d8
BZ
222 ide_hwif_t *hwif = drive->hwif;
223 struct ide_io_ports *io_ports = &hwif->io_ports;
224 u8 io_32bit = drive->io_32bit;
1da177e4
LT
225
226 if (io_32bit) {
227 if (io_32bit & 2) {
228 unsigned long flags;
23579a2a 229
1da177e4 230 local_irq_save(flags);
4c3032d8
BZ
231 ata_vlb_sync(drive, io_ports->nsect_addr);
232 hwif->OUTSL(io_ports->data_addr, buffer, wcount);
1da177e4
LT
233 local_irq_restore(flags);
234 } else
4c3032d8 235 hwif->OUTSL(io_ports->data_addr, buffer, wcount);
23579a2a 236 } else
4c3032d8 237 hwif->OUTSW(io_ports->data_addr, buffer, wcount << 1);
1da177e4
LT
238}
239
240/*
241 * The following routines are mainly used by the ATAPI drivers.
242 *
243 * These routines will round up any request for an odd number of bytes,
244 * so if an odd bytecount is specified, be sure that there's at least one
245 * extra byte allocated for the buffer.
246 */
247
248static void atapi_input_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
249{
250 ide_hwif_t *hwif = HWIF(drive);
251
252 ++bytecount;
92d3ab27 253 hwif->ata_input_data(drive, NULL, buffer, bytecount / 4);
1da177e4 254 if ((bytecount & 0x03) >= 2)
4c3032d8 255 hwif->INSW(hwif->io_ports.data_addr,
23579a2a 256 (u8 *)buffer + (bytecount & ~0x03), 1);
1da177e4
LT
257}
258
259static void atapi_output_bytes(ide_drive_t *drive, void *buffer, u32 bytecount)
260{
261 ide_hwif_t *hwif = HWIF(drive);
262
263 ++bytecount;
92d3ab27 264 hwif->ata_output_data(drive, NULL, buffer, bytecount / 4);
1da177e4 265 if ((bytecount & 0x03) >= 2)
4c3032d8 266 hwif->OUTSW(hwif->io_ports.data_addr,
23579a2a 267 (u8 *)buffer + (bytecount & ~0x03), 1);
1da177e4
LT
268}
269
270void default_hwif_transport(ide_hwif_t *hwif)
271{
272 hwif->ata_input_data = ata_input_data;
273 hwif->ata_output_data = ata_output_data;
274 hwif->atapi_input_bytes = atapi_input_bytes;
275 hwif->atapi_output_bytes = atapi_output_bytes;
276}
277
1da177e4
LT
278void ide_fix_driveid (struct hd_driveid *id)
279{
280#ifndef __LITTLE_ENDIAN
281# ifdef __BIG_ENDIAN
282 int i;
283 u16 *stringcast;
284
285 id->config = __le16_to_cpu(id->config);
286 id->cyls = __le16_to_cpu(id->cyls);
287 id->reserved2 = __le16_to_cpu(id->reserved2);
288 id->heads = __le16_to_cpu(id->heads);
289 id->track_bytes = __le16_to_cpu(id->track_bytes);
290 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
291 id->sectors = __le16_to_cpu(id->sectors);
292 id->vendor0 = __le16_to_cpu(id->vendor0);
293 id->vendor1 = __le16_to_cpu(id->vendor1);
294 id->vendor2 = __le16_to_cpu(id->vendor2);
295 stringcast = (u16 *)&id->serial_no[0];
296 for (i = 0; i < (20/2); i++)
297 stringcast[i] = __le16_to_cpu(stringcast[i]);
298 id->buf_type = __le16_to_cpu(id->buf_type);
299 id->buf_size = __le16_to_cpu(id->buf_size);
300 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
301 stringcast = (u16 *)&id->fw_rev[0];
302 for (i = 0; i < (8/2); i++)
303 stringcast[i] = __le16_to_cpu(stringcast[i]);
304 stringcast = (u16 *)&id->model[0];
305 for (i = 0; i < (40/2); i++)
306 stringcast[i] = __le16_to_cpu(stringcast[i]);
307 id->dword_io = __le16_to_cpu(id->dword_io);
308 id->reserved50 = __le16_to_cpu(id->reserved50);
309 id->field_valid = __le16_to_cpu(id->field_valid);
310 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
311 id->cur_heads = __le16_to_cpu(id->cur_heads);
312 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
313 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
314 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
315 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
316 id->dma_1word = __le16_to_cpu(id->dma_1word);
317 id->dma_mword = __le16_to_cpu(id->dma_mword);
318 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
319 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
320 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
321 id->eide_pio = __le16_to_cpu(id->eide_pio);
322 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
323 for (i = 0; i < 2; ++i)
324 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
325 for (i = 0; i < 4; ++i)
326 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
327 id->queue_depth = __le16_to_cpu(id->queue_depth);
328 for (i = 0; i < 4; ++i)
329 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
330 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
331 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
332 id->command_set_1 = __le16_to_cpu(id->command_set_1);
333 id->command_set_2 = __le16_to_cpu(id->command_set_2);
334 id->cfsse = __le16_to_cpu(id->cfsse);
335 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
336 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
337 id->csf_default = __le16_to_cpu(id->csf_default);
338 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
339 id->trseuc = __le16_to_cpu(id->trseuc);
340 id->trsEuc = __le16_to_cpu(id->trsEuc);
341 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
342 id->mprc = __le16_to_cpu(id->mprc);
343 id->hw_config = __le16_to_cpu(id->hw_config);
344 id->acoustic = __le16_to_cpu(id->acoustic);
345 id->msrqs = __le16_to_cpu(id->msrqs);
346 id->sxfert = __le16_to_cpu(id->sxfert);
347 id->sal = __le16_to_cpu(id->sal);
348 id->spg = __le32_to_cpu(id->spg);
349 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
350 for (i = 0; i < 22; i++)
351 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
352 id->last_lun = __le16_to_cpu(id->last_lun);
353 id->word127 = __le16_to_cpu(id->word127);
354 id->dlf = __le16_to_cpu(id->dlf);
355 id->csfo = __le16_to_cpu(id->csfo);
356 for (i = 0; i < 26; i++)
357 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
358 id->word156 = __le16_to_cpu(id->word156);
359 for (i = 0; i < 3; i++)
360 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
361 id->cfa_power = __le16_to_cpu(id->cfa_power);
362 for (i = 0; i < 14; i++)
363 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
364 for (i = 0; i < 31; i++)
365 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
366 for (i = 0; i < 48; i++)
367 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
368 id->integrity_word = __le16_to_cpu(id->integrity_word);
369# else
370# error "Please fix <asm/byteorder.h>"
371# endif
372#endif
373}
374
01745112
BZ
375/*
376 * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
377 * removing leading/trailing blanks and compressing internal blanks.
378 * It is primarily used to tidy up the model name/number fields as
379 * returned by the WIN_[P]IDENTIFY commands.
380 */
381
1da177e4
LT
382void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
383{
384 u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
385
386 if (byteswap) {
387 /* convert from big-endian to host byte order */
388 for (p = end ; p != s;) {
389 unsigned short *pp = (unsigned short *) (p -= 2);
390 *pp = ntohs(*pp);
391 }
392 }
393 /* strip leading blanks */
394 while (s != end && *s == ' ')
395 ++s;
396 /* compress internal blanks and strip trailing blanks */
397 while (s != end && *s) {
398 if (*s++ != ' ' || (s != end && *s && *s != ' '))
399 *p++ = *(s-1);
400 }
401 /* wipe out trailing garbage */
402 while (p != end)
403 *p++ = '\0';
404}
405
406EXPORT_SYMBOL(ide_fixstring);
407
408/*
409 * Needed for PCI irq sharing
410 */
411int drive_is_ready (ide_drive_t *drive)
412{
413 ide_hwif_t *hwif = HWIF(drive);
414 u8 stat = 0;
415
416 if (drive->waiting_for_dma)
5e37bdc0 417 return hwif->dma_ops->dma_test_irq(drive);
1da177e4
LT
418
419#if 0
420 /* need to guarantee 400ns since last command was issued */
421 udelay(1);
422#endif
423
1da177e4
LT
424 /*
425 * We do a passive status test under shared PCI interrupts on
426 * cards that truly share the ATA side interrupt, but may also share
427 * an interrupt with another pci card/device. We make no assumptions
428 * about possible isa-pnp and pci-pnp issues yet.
429 */
4c3032d8 430 if (hwif->io_ports.ctl_addr)
c47137a9 431 stat = ide_read_altstatus(drive);
1da177e4 432 else
1da177e4 433 /* Note: this may clear a pending IRQ!! */
c47137a9 434 stat = ide_read_status(drive);
1da177e4
LT
435
436 if (stat & BUSY_STAT)
437 /* drive busy: definitely not interrupting */
438 return 0;
439
440 /* drive ready: *might* be interrupting */
441 return 1;
442}
443
444EXPORT_SYMBOL(drive_is_ready);
445
1da177e4
LT
446/*
447 * This routine busy-waits for the drive status to be not "busy".
448 * It then checks the status for all of the "good" bits and none
449 * of the "bad" bits, and if all is okay it returns 0. All other
74af21cf 450 * cases return error -- caller may then invoke ide_error().
1da177e4
LT
451 *
452 * This routine should get fixed to not hog the cpu during extra long waits..
453 * That could be done by busy-waiting for the first jiffy or two, and then
454 * setting a timer to wake up at half second intervals thereafter,
455 * until timeout is achieved, before timing out.
456 */
aedea591 457static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
1da177e4 458{
1da177e4 459 unsigned long flags;
74af21cf
BZ
460 int i;
461 u8 stat;
1da177e4
LT
462
463 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
c47137a9
BZ
464 stat = ide_read_status(drive);
465
466 if (stat & BUSY_STAT) {
1da177e4
LT
467 local_irq_set(flags);
468 timeout += jiffies;
c47137a9 469 while ((stat = ide_read_status(drive)) & BUSY_STAT) {
1da177e4
LT
470 if (time_after(jiffies, timeout)) {
471 /*
472 * One last read after the timeout in case
473 * heavy interrupt load made us not make any
474 * progress during the timeout..
475 */
c47137a9 476 stat = ide_read_status(drive);
1da177e4
LT
477 if (!(stat & BUSY_STAT))
478 break;
479
480 local_irq_restore(flags);
74af21cf
BZ
481 *rstat = stat;
482 return -EBUSY;
1da177e4
LT
483 }
484 }
485 local_irq_restore(flags);
486 }
487 /*
488 * Allow status to settle, then read it again.
489 * A few rare drives vastly violate the 400ns spec here,
490 * so we'll wait up to 10usec for a "good" status
491 * rather than expensively fail things immediately.
492 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
493 */
494 for (i = 0; i < 10; i++) {
495 udelay(1);
c47137a9
BZ
496 stat = ide_read_status(drive);
497
498 if (OK_STAT(stat, good, bad)) {
74af21cf 499 *rstat = stat;
1da177e4 500 return 0;
74af21cf 501 }
1da177e4 502 }
74af21cf
BZ
503 *rstat = stat;
504 return -EFAULT;
505}
506
507/*
508 * In case of error returns error value after doing "*startstop = ide_error()".
509 * The caller should return the updated value of "startstop" in this case,
510 * "startstop" is unchanged when the function returns 0.
511 */
512int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
513{
514 int err;
515 u8 stat;
516
517 /* bail early if we've exceeded max_failures */
518 if (drive->max_failures && (drive->failures > drive->max_failures)) {
519 *startstop = ide_stopped;
520 return 1;
521 }
522
523 err = __ide_wait_stat(drive, good, bad, timeout, &stat);
524
525 if (err) {
526 char *s = (err == -EBUSY) ? "status timeout" : "status error";
527 *startstop = ide_error(drive, s, stat);
528 }
529
530 return err;
1da177e4
LT
531}
532
533EXPORT_SYMBOL(ide_wait_stat);
534
a5b7e70d
BZ
535/**
536 * ide_in_drive_list - look for drive in black/white list
537 * @id: drive identifier
538 * @drive_table: list to inspect
539 *
540 * Look for a drive in the blacklist and the whitelist tables
541 * Returns 1 if the drive is found in the table.
542 */
543
544int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
545{
546 for ( ; drive_table->id_model; drive_table++)
547 if ((!strcmp(drive_table->id_model, id->model)) &&
548 (!drive_table->id_firmware ||
549 strstr(id->fw_rev, drive_table->id_firmware)))
550 return 1;
551 return 0;
552}
553
b0244a00
BZ
554EXPORT_SYMBOL_GPL(ide_in_drive_list);
555
a5b7e70d
BZ
556/*
557 * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
558 * We list them here and depend on the device side cable detection for them.
8588a2b7
BZ
559 *
560 * Some optical devices with the buggy firmwares have the same problem.
a5b7e70d
BZ
561 */
562static const struct drive_list_entry ivb_list[] = {
563 { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
8588a2b7 564 { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
e97564f3
PM
565 { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
566 { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
567 { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
a5b7e70d
BZ
568 { NULL , NULL }
569};
570
1da177e4
LT
571/*
572 * All hosts that use the 80c ribbon must use!
573 * The name is derived from upper byte of word 93 and the 80c ribbon.
574 */
575u8 eighty_ninty_three (ide_drive_t *drive)
576{
7f8f48af
BZ
577 ide_hwif_t *hwif = drive->hwif;
578 struct hd_driveid *id = drive->id;
a5b7e70d 579 int ivb = ide_in_drive_list(id, ivb_list);
7f8f48af 580
49521f97
BZ
581 if (hwif->cbl == ATA_CBL_PATA40_SHORT)
582 return 1;
583
a5b7e70d
BZ
584 if (ivb)
585 printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
586 drive->name);
587
b98f8803
GK
588 if (ide_dev_is_sata(id) && !ivb)
589 return 1;
590
a5b7e70d 591 if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
7f8f48af 592 goto no_80w;
1a1276e7 593
f68d9320
BZ
594 /*
595 * FIXME:
f367bed0 596 * - change master/slave IDENTIFY order
a5b7e70d 597 * - force bit13 (80c cable present) check also for !ivb devices
f68d9320
BZ
598 * (unless the slave device is pre-ATA3)
599 */
a5b7e70d 600 if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
7f8f48af
BZ
601 return 1;
602
603no_80w:
604 if (drive->udma33_warned == 1)
605 return 0;
606
607 printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
608 "limiting max speed to UDMA33\n",
49521f97
BZ
609 drive->name,
610 hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
7f8f48af
BZ
611
612 drive->udma33_warned = 1;
613
614 return 0;
1da177e4
LT
615}
616
8a455134 617int ide_driveid_update(ide_drive_t *drive)
1da177e4 618{
8a455134 619 ide_hwif_t *hwif = drive->hwif;
1da177e4 620 struct hd_driveid *id;
8a455134 621 unsigned long timeout, flags;
c47137a9 622 u8 stat;
1da177e4 623
1da177e4
LT
624 /*
625 * Re-read drive->id for possible DMA mode
626 * change (copied from ide-probe.c)
627 */
1da177e4
LT
628
629 SELECT_MASK(drive, 1);
81ca6919 630 ide_set_irq(drive, 1);
1da177e4 631 msleep(50);
4c3032d8 632 hwif->OUTB(WIN_IDENTIFY, hwif->io_ports.command_addr);
1da177e4
LT
633 timeout = jiffies + WAIT_WORSTCASE;
634 do {
635 if (time_after(jiffies, timeout)) {
636 SELECT_MASK(drive, 0);
637 return 0; /* drive timed-out */
638 }
c47137a9 639
1da177e4 640 msleep(50); /* give drive a breather */
c47137a9
BZ
641 stat = ide_read_altstatus(drive);
642 } while (stat & BUSY_STAT);
643
1da177e4 644 msleep(50); /* wait for IRQ and DRQ_STAT */
c47137a9
BZ
645 stat = ide_read_status(drive);
646
647 if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
1da177e4
LT
648 SELECT_MASK(drive, 0);
649 printk("%s: CHECK for good STATUS\n", drive->name);
650 return 0;
651 }
652 local_irq_save(flags);
653 SELECT_MASK(drive, 0);
654 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
655 if (!id) {
656 local_irq_restore(flags);
657 return 0;
658 }
92d3ab27 659 hwif->ata_input_data(drive, NULL, id, SECTOR_WORDS);
c47137a9 660 (void)ide_read_status(drive); /* clear drive IRQ */
1da177e4
LT
661 local_irq_enable();
662 local_irq_restore(flags);
663 ide_fix_driveid(id);
664 if (id) {
665 drive->id->dma_ultra = id->dma_ultra;
666 drive->id->dma_mword = id->dma_mword;
667 drive->id->dma_1word = id->dma_1word;
668 /* anything more ? */
669 kfree(id);
3ab7efe8
BZ
670
671 if (drive->using_dma && ide_id_dma_bug(drive))
672 ide_dma_off(drive);
1da177e4
LT
673 }
674
675 return 1;
1da177e4
LT
676}
677
74af21cf 678int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
1da177e4 679{
74af21cf 680 ide_hwif_t *hwif = drive->hwif;
4c3032d8 681 struct ide_io_ports *io_ports = &hwif->io_ports;
89613e66 682 int error = 0;
1da177e4
LT
683 u8 stat;
684
685// while (HWGROUP(drive)->busy)
686// msleep(50);
687
688#ifdef CONFIG_BLK_DEV_IDEDMA
5e37bdc0
BZ
689 if (hwif->dma_ops) /* check if host supports DMA */
690 hwif->dma_ops->dma_host_set(drive, 0);
1da177e4
LT
691#endif
692
89613e66
SS
693 /* Skip setting PIO flow-control modes on pre-EIDE drives */
694 if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
695 goto skip;
696
1da177e4
LT
697 /*
698 * Don't use ide_wait_cmd here - it will
699 * attempt to set_geometry and recalibrate,
700 * but for some reason these don't work at
701 * this point (lost interrupt).
702 */
703 /*
704 * Select the drive, and issue the SETFEATURES command
705 */
706 disable_irq_nosync(hwif->irq);
707
708 /*
709 * FIXME: we race against the running IRQ here if
710 * this is called from non IRQ context. If we use
711 * disable_irq() we hang on the error path. Work
712 * is needed.
713 */
714
715 udelay(1);
716 SELECT_DRIVE(drive);
717 SELECT_MASK(drive, 0);
718 udelay(1);
81ca6919 719 ide_set_irq(drive, 0);
4c3032d8
BZ
720 hwif->OUTB(speed, io_ports->nsect_addr);
721 hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
722 hwif->OUTBSYNC(drive, WIN_SETFEATURES, io_ports->command_addr);
81ca6919
BZ
723 if (drive->quirk_list == 2)
724 ide_set_irq(drive, 1);
1da177e4 725
74af21cf
BZ
726 error = __ide_wait_stat(drive, drive->ready_stat,
727 BUSY_STAT|DRQ_STAT|ERR_STAT,
728 WAIT_CMD, &stat);
1da177e4
LT
729
730 SELECT_MASK(drive, 0);
731
732 enable_irq(hwif->irq);
733
734 if (error) {
735 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
736 return error;
737 }
738
739 drive->id->dma_ultra &= ~0xFF00;
740 drive->id->dma_mword &= ~0x0F00;
741 drive->id->dma_1word &= ~0x0F00;
742
89613e66 743 skip:
1da177e4 744#ifdef CONFIG_BLK_DEV_IDEDMA
f37aaf9e
BZ
745 if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
746 drive->using_dma)
5e37bdc0
BZ
747 hwif->dma_ops->dma_host_set(drive, 1);
748 else if (hwif->dma_ops) /* check if host supports DMA */
4a546e04 749 ide_dma_off_quietly(drive);
1da177e4
LT
750#endif
751
752 switch(speed) {
753 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
754 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
755 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
756 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
757 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
758 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
759 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
760 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
761 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
762 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
763 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
764 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
765 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
766 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
767 default: break;
768 }
769 if (!drive->init_speed)
770 drive->init_speed = speed;
771 drive->current_speed = speed;
772 return error;
773}
774
1da177e4
LT
775/*
776 * This should get invoked any time we exit the driver to
777 * wait for an interrupt response from a drive. handler() points
778 * at the appropriate code to handle the next interrupt, and a
779 * timer is started to prevent us from waiting forever in case
780 * something goes wrong (see the ide_timer_expiry() handler later on).
781 *
782 * See also ide_execute_command
783 */
784static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
785 unsigned int timeout, ide_expiry_t *expiry)
786{
787 ide_hwgroup_t *hwgroup = HWGROUP(drive);
788
d30a426d 789 BUG_ON(hwgroup->handler);
1da177e4
LT
790 hwgroup->handler = handler;
791 hwgroup->expiry = expiry;
792 hwgroup->timer.expires = jiffies + timeout;
d30a426d 793 hwgroup->req_gen_timer = hwgroup->req_gen;
1da177e4
LT
794 add_timer(&hwgroup->timer);
795}
796
797void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
798 unsigned int timeout, ide_expiry_t *expiry)
799{
800 unsigned long flags;
801 spin_lock_irqsave(&ide_lock, flags);
802 __ide_set_handler(drive, handler, timeout, expiry);
803 spin_unlock_irqrestore(&ide_lock, flags);
804}
805
806EXPORT_SYMBOL(ide_set_handler);
807
808/**
809 * ide_execute_command - execute an IDE command
810 * @drive: IDE drive to issue the command against
811 * @command: command byte to write
812 * @handler: handler for next phase
813 * @timeout: timeout for command
814 * @expiry: handler to run on timeout
815 *
816 * Helper function to issue an IDE command. This handles the
817 * atomicity requirements, command timing and ensures that the
818 * handler and IRQ setup do not race. All IDE command kick off
819 * should go via this function or do equivalent locking.
820 */
cd2a2d96
BZ
821
822void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
823 unsigned timeout, ide_expiry_t *expiry)
1da177e4
LT
824{
825 unsigned long flags;
1da177e4 826 ide_hwif_t *hwif = HWIF(drive);
629f944b 827
1da177e4 828 spin_lock_irqsave(&ide_lock, flags);
629f944b 829 __ide_set_handler(drive, handler, timeout, expiry);
4c3032d8 830 hwif->OUTBSYNC(drive, cmd, hwif->io_ports.command_addr);
629f944b
BZ
831 /*
832 * Drive takes 400nS to respond, we must avoid the IRQ being
833 * serviced before that.
834 *
835 * FIXME: we could skip this delay with care on non shared devices
836 */
1da177e4
LT
837 ndelay(400);
838 spin_unlock_irqrestore(&ide_lock, flags);
839}
840
841EXPORT_SYMBOL(ide_execute_command);
842
843
844/* needed below */
845static ide_startstop_t do_reset1 (ide_drive_t *, int);
846
847/*
848 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
849 * during an atapi drive reset operation. If the drive has not yet responded,
850 * and we have not yet hit our maximum waiting time, then the timer is restarted
851 * for another 50ms.
852 */
853static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
854{
855 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1da177e4
LT
856 u8 stat;
857
858 SELECT_DRIVE(drive);
859 udelay (10);
c47137a9 860 stat = ide_read_status(drive);
1da177e4 861
c47137a9 862 if (OK_STAT(stat, 0, BUSY_STAT))
1da177e4 863 printk("%s: ATAPI reset complete\n", drive->name);
c47137a9 864 else {
1da177e4 865 if (time_before(jiffies, hwgroup->poll_timeout)) {
1da177e4
LT
866 ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
867 /* continue polling */
868 return ide_started;
869 }
870 /* end of polling */
871 hwgroup->polling = 0;
872 printk("%s: ATAPI reset timed-out, status=0x%02x\n",
873 drive->name, stat);
874 /* do it the old fashioned way */
875 return do_reset1(drive, 1);
876 }
877 /* done polling */
878 hwgroup->polling = 0;
913759ac 879 hwgroup->resetting = 0;
1da177e4
LT
880 return ide_stopped;
881}
882
883/*
884 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
885 * during an ide reset operation. If the drives have not yet responded,
886 * and we have not yet hit our maximum waiting time, then the timer is restarted
887 * for another 50ms.
888 */
889static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
890{
891 ide_hwgroup_t *hwgroup = HWGROUP(drive);
892 ide_hwif_t *hwif = HWIF(drive);
ac95beed 893 const struct ide_port_ops *port_ops = hwif->port_ops;
1da177e4
LT
894 u8 tmp;
895
ac95beed
BZ
896 if (port_ops && port_ops->reset_poll) {
897 if (port_ops->reset_poll(drive)) {
1da177e4
LT
898 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
899 hwif->name, drive->name);
900 return ide_stopped;
901 }
902 }
903
c47137a9
BZ
904 tmp = ide_read_status(drive);
905
906 if (!OK_STAT(tmp, 0, BUSY_STAT)) {
1da177e4 907 if (time_before(jiffies, hwgroup->poll_timeout)) {
1da177e4
LT
908 ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
909 /* continue polling */
910 return ide_started;
911 }
912 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
913 drive->failures++;
914 } else {
915 printk("%s: reset: ", hwif->name);
64a57fe4
BZ
916 tmp = ide_read_error(drive);
917
918 if (tmp == 1) {
1da177e4
LT
919 printk("success\n");
920 drive->failures = 0;
921 } else {
922 drive->failures++;
923 printk("master: ");
924 switch (tmp & 0x7f) {
925 case 1: printk("passed");
926 break;
927 case 2: printk("formatter device error");
928 break;
929 case 3: printk("sector buffer error");
930 break;
931 case 4: printk("ECC circuitry error");
932 break;
933 case 5: printk("controlling MPU error");
934 break;
935 default:printk("error (0x%02x?)", tmp);
936 }
937 if (tmp & 0x80)
938 printk("; slave: failed");
939 printk("\n");
940 }
941 }
942 hwgroup->polling = 0; /* done polling */
913759ac 943 hwgroup->resetting = 0; /* done reset attempt */
1da177e4
LT
944 return ide_stopped;
945}
946
1da177e4
LT
947static void ide_disk_pre_reset(ide_drive_t *drive)
948{
949 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
950
951 drive->special.all = 0;
952 drive->special.b.set_geometry = legacy;
953 drive->special.b.recalibrate = legacy;
4ee06b7e 954 drive->mult_count = 0;
1da177e4
LT
955 if (!drive->keep_settings && !drive->using_dma)
956 drive->mult_req = 0;
957 if (drive->mult_req != drive->mult_count)
958 drive->special.b.set_multmode = 1;
959}
960
961static void pre_reset(ide_drive_t *drive)
962{
ac95beed
BZ
963 const struct ide_port_ops *port_ops = drive->hwif->port_ops;
964
1da177e4
LT
965 if (drive->media == ide_disk)
966 ide_disk_pre_reset(drive);
967 else
968 drive->post_reset = 1;
969
99ffbe0e
BZ
970 if (drive->using_dma) {
971 if (drive->crc_count)
578cfa0d 972 ide_check_dma_crc(drive);
99ffbe0e
BZ
973 else
974 ide_dma_off(drive);
975 }
976
977 if (!drive->keep_settings) {
978 if (!drive->using_dma) {
1da177e4
LT
979 drive->unmask = 0;
980 drive->io_32bit = 0;
981 }
982 return;
983 }
1da177e4 984
ac95beed
BZ
985 if (port_ops && port_ops->pre_reset)
986 port_ops->pre_reset(drive);
1da177e4 987
513daadd
SS
988 if (drive->current_speed != 0xff)
989 drive->desired_speed = drive->current_speed;
990 drive->current_speed = 0xff;
1da177e4
LT
991}
992
993/*
994 * do_reset1() attempts to recover a confused drive by resetting it.
995 * Unfortunately, resetting a disk drive actually resets all devices on
996 * the same interface, so it can really be thought of as resetting the
997 * interface rather than resetting the drive.
998 *
999 * ATAPI devices have their own reset mechanism which allows them to be
1000 * individually reset without clobbering other devices on the same interface.
1001 *
1002 * Unfortunately, the IDE interface does not generate an interrupt to let
1003 * us know when the reset operation has finished, so we must poll for this.
1004 * Equally poor, though, is the fact that this may a very long time to complete,
1005 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
1006 * we set a timer to poll at 50ms intervals.
1007 */
1008static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1009{
1010 unsigned int unit;
1011 unsigned long flags;
1012 ide_hwif_t *hwif;
1013 ide_hwgroup_t *hwgroup;
4c3032d8 1014 struct ide_io_ports *io_ports;
ac95beed 1015 const struct ide_port_ops *port_ops;
23579a2a
BZ
1016 u8 ctl;
1017
1da177e4
LT
1018 spin_lock_irqsave(&ide_lock, flags);
1019 hwif = HWIF(drive);
1020 hwgroup = HWGROUP(drive);
1021
4c3032d8
BZ
1022 io_ports = &hwif->io_ports;
1023
1da177e4 1024 /* We must not reset with running handlers */
125e1874 1025 BUG_ON(hwgroup->handler != NULL);
1da177e4
LT
1026
1027 /* For an ATAPI device, first try an ATAPI SRST. */
1028 if (drive->media != ide_disk && !do_not_try_atapi) {
913759ac 1029 hwgroup->resetting = 1;
1da177e4
LT
1030 pre_reset(drive);
1031 SELECT_DRIVE(drive);
1032 udelay (20);
4c3032d8 1033 hwif->OUTBSYNC(drive, WIN_SRST, io_ports->command_addr);
68ad9910 1034 ndelay(400);
1da177e4
LT
1035 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1036 hwgroup->polling = 1;
1037 __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
1038 spin_unlock_irqrestore(&ide_lock, flags);
1039 return ide_started;
1040 }
1041
1042 /*
1043 * First, reset any device state data we were maintaining
1044 * for any of the drives on this interface.
1045 */
1046 for (unit = 0; unit < MAX_DRIVES; ++unit)
1047 pre_reset(&hwif->drives[unit]);
1048
4c3032d8 1049 if (io_ports->ctl_addr == 0) {
1da177e4
LT
1050 spin_unlock_irqrestore(&ide_lock, flags);
1051 return ide_stopped;
1052 }
1053
913759ac 1054 hwgroup->resetting = 1;
1da177e4
LT
1055 /*
1056 * Note that we also set nIEN while resetting the device,
1057 * to mask unwanted interrupts from the interface during the reset.
1058 * However, due to the design of PC hardware, this will cause an
1059 * immediate interrupt due to the edge transition it produces.
1060 * This single interrupt gives us a "fast poll" for drives that
1061 * recover from reset very quickly, saving us the first 50ms wait time.
1062 */
1063 /* set SRST and nIEN */
4c3032d8 1064 hwif->OUTBSYNC(drive, drive->ctl|6, io_ports->ctl_addr);
1da177e4
LT
1065 /* more than enough time */
1066 udelay(10);
23579a2a
BZ
1067 if (drive->quirk_list == 2)
1068 ctl = drive->ctl; /* clear SRST and nIEN */
1069 else
1070 ctl = drive->ctl | 2; /* clear SRST, leave nIEN */
4c3032d8 1071 hwif->OUTBSYNC(drive, ctl, io_ports->ctl_addr);
1da177e4
LT
1072 /* more than enough time */
1073 udelay(10);
1074 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
1075 hwgroup->polling = 1;
1076 __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
1077
1078 /*
1079 * Some weird controller like resetting themselves to a strange
1080 * state when the disks are reset this way. At least, the Winbond
1081 * 553 documentation says that
1082 */
ac95beed
BZ
1083 port_ops = hwif->port_ops;
1084 if (port_ops && port_ops->resetproc)
1085 port_ops->resetproc(drive);
1da177e4
LT
1086
1087 spin_unlock_irqrestore(&ide_lock, flags);
1088 return ide_started;
1089}
1090
1091/*
1092 * ide_do_reset() is the entry point to the drive/interface reset code.
1093 */
1094
1095ide_startstop_t ide_do_reset (ide_drive_t *drive)
1096{
1097 return do_reset1(drive, 0);
1098}
1099
1100EXPORT_SYMBOL(ide_do_reset);
1101
1102/*
1103 * ide_wait_not_busy() waits for the currently selected device on the hwif
9d501529 1104 * to report a non-busy status, see comments in ide_probe_port().
1da177e4
LT
1105 */
1106int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
1107{
1108 u8 stat = 0;
1109
1110 while(timeout--) {
1111 /*
1112 * Turn this into a schedule() sleep once I'm sure
1113 * about locking issues (2.5 work ?).
1114 */
1115 mdelay(1);
4c3032d8 1116 stat = hwif->INB(hwif->io_ports.status_addr);
1da177e4
LT
1117 if ((stat & BUSY_STAT) == 0)
1118 return 0;
1119 /*
1120 * Assume a value of 0xff means nothing is connected to
1121 * the interface and it doesn't implement the pull-down
1122 * resistor on D7.
1123 */
1124 if (stat == 0xff)
1125 return -ENODEV;
6842f8c8 1126 touch_softlockup_watchdog();
1e86240f 1127 touch_nmi_watchdog();
1da177e4
LT
1128 }
1129 return -EBUSY;
1130}
1131
1132EXPORT_SYMBOL_GPL(ide_wait_not_busy);
1133