More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / device.c
blobaaec151c4d27bcf60bd307c643395ab9c76e5931
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
12 * more details.
16 * Common low leved device access code. This is the lowest layer of hardware
17 * access.
19 * This is the place where register set access portability will be handled in
20 * the future.
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.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>
36 #include <asm/io.h>
37 #include <asm/bitops.h>
38 #include <asm/uaccess.h>
41 * Select a device for operation with possible busy waiting for the operation
42 * to complete.
44 void ata_select(struct ata_device *drive, unsigned long delay)
46 struct ata_channel *ch = drive->channel;
48 if (!ch)
49 return;
51 if (ch->selectproc)
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
56 * it.
58 if (delay) {
59 if (delay >= 1000)
60 mdelay(delay / 1000);
61 else
62 udelay(delay);
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;
75 if (!ch)
76 return;
78 if (ch->maskproc)
79 ch->maskproc(drive);
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]);
119 else
120 #endif
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 */
125 else
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)
140 int i;
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"
149 udelay(1);
150 if (!ata_status(drive, 0, BUSY_STAT)) {
151 unsigned long flags;
153 local_save_flags(flags);
154 local_irq_enable();
155 timeout += jiffies;
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++) {
172 udelay(1);
173 if (ata_status(drive, good, bad))
174 return ATA_OP_READY;
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])
192 return 0;
194 /* 0x08 is for legacy ATA-1 devices */
195 if (on)
196 OUT_BYTE(0x08 | 0x00, ch->io_ports[IDE_CONTROL_OFFSET]);
197 else {
198 if (!ch->intrproc)
199 OUT_BYTE(0x08 | 0x02, ch->io_ports[IDE_CONTROL_OFFSET]);
200 else
201 ch->intrproc(drive);
204 return 1;
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;
215 u8 stat;
217 if (!ch->io_ports[IDE_CONTROL_OFFSET])
218 return;
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]);
223 udelay(10);
224 /* 0x08 is for legacy ATA-1 devices */
225 OUT_BYTE(0x08 | 0x00, ch->io_ports[IDE_CONTROL_OFFSET]);
226 do {
227 mdelay(50);
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");