1 /**** vi:set ts=8 sts=8 sw=8:************************************************
3 * Copyright (C) 2002 Marcin Dalecki <martin@dalecki.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * Common low leved device access code. This is the lowest layer of hardware
19 * This is the place where register set access portability will be handled in
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
28 #include <linux/ioport.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/hdreg.h>
33 #include <linux/ide.h>
35 #include <asm/byteorder.h>
37 #include <asm/bitops.h>
38 #include <asm/uaccess.h>
41 * Select a device for operation with possible busy waiting for the operation
44 void ata_select(struct ata_device
*drive
, unsigned long delay
)
46 struct ata_channel
*ch
= drive
->channel
;
52 ch
->selectproc(drive
);
53 OUT_BYTE(drive
->select
.all
, ch
->io_ports
[IDE_SELECT_OFFSET
]);
55 /* The delays during probing for drives can be georgeous. Deal with
66 EXPORT_SYMBOL(ata_select
);
69 * Handle quirky routing of interrupts.
71 void ata_mask(struct ata_device
*drive
)
73 struct ata_channel
*ch
= drive
->channel
;
82 EXPORT_SYMBOL(ata_mask
);
85 * Check the state of the status register.
87 int ata_status(struct ata_device
*drive
, u8 good
, u8 bad
)
89 struct ata_channel
*ch
= drive
->channel
;
91 drive
->status
= IN_BYTE(ch
->io_ports
[IDE_STATUS_OFFSET
]);
93 return (drive
->status
& (good
| bad
)) == good
;
96 EXPORT_SYMBOL(ata_status
);
99 * This is used to check for the drive status on the IRQ handling code path.
101 int ata_status_irq(struct ata_device
*drive
)
103 if (test_bit(IDE_DMA
, drive
->channel
->active
))
104 return udma_irq_status(drive
);
106 /* Need to guarantee 400ns since last command was issued?
108 #ifdef CONFIG_IDEPCI_SHARE_IRQ
111 * We do a passive status test under shared PCI interrupts on cards
112 * that truly share the ATA side interrupt, but may also share an
113 * interrupt with another pci card/device.
116 if (drive
->channel
->io_ports
[IDE_CONTROL_OFFSET
])
117 drive
->status
= IN_BYTE(drive
->channel
->io_ports
[IDE_CONTROL_OFFSET
]);
121 ata_status(drive
, 0, 0); /* Note: this may clear a pending IRQ! */
123 if (drive
->status
& BUSY_STAT
)
124 return 0; /* drive busy: definitely not interrupting */
126 return 1; /* drive ready: *might* be interrupting */
129 EXPORT_SYMBOL(ata_status_irq
);
132 * Busy-wait for the drive status to be not "busy". Check then the status for
133 * all of the "good" bits and none of the "bad" bits, and if all is okay it
134 * returns 0. All other cases return 1 after invoking error handler -- caller
135 * should just return.
137 int ata_status_poll(struct ata_device
*drive
, u8 good
, u8 bad
,
138 unsigned long timeout
, struct request
*rq
)
142 /* bail early if we've exceeded max_failures */
143 if (drive
->max_failures
&& (drive
->failures
> drive
->max_failures
))
144 return ATA_OP_FINISHED
;
146 * Spin until the drive is no longer busy.
147 * Spec allows drive 400ns to assert "BUSY"
150 if (!ata_status(drive
, 0, BUSY_STAT
)) {
153 local_save_flags(flags
);
156 while (!ata_status(drive
, 0, BUSY_STAT
)) {
157 if (time_after(jiffies
, timeout
)) {
158 local_irq_restore(flags
);
159 return ata_error(drive
, rq
, "status timeout");
162 local_irq_restore(flags
);
166 * Allow status to settle, then read it again. A few rare drives
167 * vastly violate the 400ns spec here, so we'll wait up to 10usec for a
168 * "good" status rather than expensively fail things immediately. This
169 * fix courtesy of Matthew Faupel & Niccolo Rigacci.
171 for (i
= 0; i
< 10; i
++) {
173 if (ata_status(drive
, good
, bad
))
177 return ata_error(drive
, rq
, "status error");
180 EXPORT_SYMBOL(ata_status_poll
);
183 * Handle the nIEN - negated Interrupt ENable of the drive.
184 * This is controlling whatever the drive will acnowlenge commands
185 * with interrupts or not.
187 int ata_irq_enable(struct ata_device
*drive
, int on
)
189 struct ata_channel
*ch
= drive
->channel
;
191 if (!ch
->io_ports
[IDE_CONTROL_OFFSET
])
194 /* 0x08 is for legacy ATA-1 devices */
196 OUT_BYTE(0x08 | 0x00, ch
->io_ports
[IDE_CONTROL_OFFSET
]);
199 OUT_BYTE(0x08 | 0x02, ch
->io_ports
[IDE_CONTROL_OFFSET
]);
207 EXPORT_SYMBOL(ata_irq_enable
);
210 * Perform a reset operation on the currently selected drive.
212 void ata_reset(struct ata_channel
*ch
)
214 unsigned long timeout
= jiffies
+ WAIT_WORSTCASE
;
217 if (!ch
->io_ports
[IDE_CONTROL_OFFSET
])
220 printk("%s: reset\n", ch
->name
);
221 /* 0x08 is for legacy ATA-1 devices */
222 OUT_BYTE(0x08 | 0x04, ch
->io_ports
[IDE_CONTROL_OFFSET
]);
224 /* 0x08 is for legacy ATA-1 devices */
225 OUT_BYTE(0x08 | 0x00, ch
->io_ports
[IDE_CONTROL_OFFSET
]);
228 stat
= IN_BYTE(ch
->io_ports
[IDE_STATUS_OFFSET
]);
229 } while ((stat
& BUSY_STAT
) && time_before(jiffies
, timeout
));
232 EXPORT_SYMBOL(ata_reset
);
235 * Output a complete register file.
237 void ata_out_regfile(struct ata_device
*drive
, struct hd_drive_task_hdr
*rf
)
239 struct ata_channel
*ch
= drive
->channel
;
241 OUT_BYTE(rf
->feature
, ch
->io_ports
[IDE_FEATURE_OFFSET
]);
242 OUT_BYTE(rf
->sector_count
, ch
->io_ports
[IDE_NSECTOR_OFFSET
]);
243 OUT_BYTE(rf
->sector_number
, ch
->io_ports
[IDE_SECTOR_OFFSET
]);
244 OUT_BYTE(rf
->low_cylinder
, ch
->io_ports
[IDE_LCYL_OFFSET
]);
245 OUT_BYTE(rf
->high_cylinder
, ch
->io_ports
[IDE_HCYL_OFFSET
]);
248 EXPORT_SYMBOL(ata_out_regfile
);
251 * Input a complete register file.
253 void ata_in_regfile(struct ata_device
*drive
, struct hd_drive_task_hdr
*rf
)
255 struct ata_channel
*ch
= drive
->channel
;
257 rf
->sector_count
= IN_BYTE(ch
->io_ports
[IDE_NSECTOR_OFFSET
]);
258 rf
->sector_number
= IN_BYTE(ch
->io_ports
[IDE_SECTOR_OFFSET
]);
259 rf
->low_cylinder
= IN_BYTE(ch
->io_ports
[IDE_LCYL_OFFSET
]);
260 rf
->high_cylinder
= IN_BYTE(ch
->io_ports
[IDE_HCYL_OFFSET
]);
263 MODULE_LICENSE("GPL");