blob: 7f77bb7db48857263c8040b7b19e6cf31c5261d8 [file] [log] [blame]
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +01001
2#include <linux/kernel.h>
3#include <linux/ide.h>
4
Bartlomiej Zolnierkiewicz15a453a2009-03-27 12:46:26 +01005#if defined(CONFIG_ARM) || defined(CONFIG_M68K) || defined(CONFIG_MIPS) || \
6 defined(CONFIG_PARISC) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
7#include <asm/ide.h>
8#else
9#include <asm-generic/ide_iops.h>
10#endif
11
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010012/*
13 * Conventional PIO operations for ATA devices
14 */
15
16static u8 ide_inb(unsigned long port)
17{
18 return (u8) inb(port);
19}
20
21static void ide_outb(u8 val, unsigned long port)
22{
23 outb(val, port);
24}
25
26/*
27 * MMIO operations, typically used for SATA controllers
28 */
29
30static u8 ide_mm_inb(unsigned long port)
31{
32 return (u8) readb((void __iomem *) port);
33}
34
35static void ide_mm_outb(u8 value, unsigned long port)
36{
37 writeb(value, (void __iomem *) port);
38}
39
40void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
41{
42 if (hwif->host_flags & IDE_HFLAG_MMIO)
43 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
44 else
45 outb(cmd, hwif->io_ports.command_addr);
46}
47EXPORT_SYMBOL_GPL(ide_exec_command);
48
49u8 ide_read_status(ide_hwif_t *hwif)
50{
51 if (hwif->host_flags & IDE_HFLAG_MMIO)
52 return readb((void __iomem *)hwif->io_ports.status_addr);
53 else
54 return inb(hwif->io_ports.status_addr);
55}
56EXPORT_SYMBOL_GPL(ide_read_status);
57
58u8 ide_read_altstatus(ide_hwif_t *hwif)
59{
60 if (hwif->host_flags & IDE_HFLAG_MMIO)
61 return readb((void __iomem *)hwif->io_ports.ctl_addr);
62 else
63 return inb(hwif->io_ports.ctl_addr);
64}
65EXPORT_SYMBOL_GPL(ide_read_altstatus);
66
Sergei Shtylyovecf3a312009-03-31 20:15:30 +020067void ide_write_devctl(ide_hwif_t *hwif, u8 ctl)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010068{
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010069 if (hwif->host_flags & IDE_HFLAG_MMIO)
70 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
71 else
72 outb(ctl, hwif->io_ports.ctl_addr);
73}
Sergei Shtylyovecf3a312009-03-31 20:15:30 +020074EXPORT_SYMBOL_GPL(ide_write_devctl);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010075
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010076void ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010077{
78 ide_hwif_t *hwif = drive->hwif;
79 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010080 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010081 void (*tf_outb)(u8 addr, unsigned long port);
82 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010083 u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010084
85 if (mmio)
86 tf_outb = ide_mm_outb;
87 else
88 tf_outb = ide_outb;
89
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010090 if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010091 HIHI = 0xFF;
92
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010093 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010094 tf_outb(tf->hob_feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010095 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010096 tf_outb(tf->hob_nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010097 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010098 tf_outb(tf->hob_lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010099 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100100 tf_outb(tf->hob_lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100101 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100102 tf_outb(tf->hob_lbah, io_ports->lbah_addr);
103
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100104 if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100105 tf_outb(tf->feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100106 if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100107 tf_outb(tf->nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100108 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100109 tf_outb(tf->lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100110 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100111 tf_outb(tf->lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100112 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100113 tf_outb(tf->lbah, io_ports->lbah_addr);
114
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100115 if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100116 tf_outb((tf->device & HIHI) | drive->select,
117 io_ports->device_addr);
118}
119EXPORT_SYMBOL_GPL(ide_tf_load);
120
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100121void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100122{
123 ide_hwif_t *hwif = drive->hwif;
124 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100125 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100126 void (*tf_outb)(u8 addr, unsigned long port);
127 u8 (*tf_inb)(unsigned long port);
128 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
129
130 if (mmio) {
131 tf_outb = ide_mm_outb;
132 tf_inb = ide_mm_inb;
133 } else {
134 tf_outb = ide_outb;
135 tf_inb = ide_inb;
136 }
137
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100138 /* be sure we're looking at the low order bits */
Sergei Shtylyov4d74c3f2009-03-31 20:15:29 +0200139 tf_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100140
Sergei Shtylyov67625112009-03-31 20:15:30 +0200141 if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
142 tf->error = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100143 if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100144 tf->nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100145 if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100146 tf->lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100147 if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100148 tf->lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100149 if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100150 tf->lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100151 if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100152 tf->device = tf_inb(io_ports->device_addr);
153
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100154 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
Sergei Shtylyov4d74c3f2009-03-31 20:15:29 +0200155 tf_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100156
Sergei Shtylyov67625112009-03-31 20:15:30 +0200157 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
158 tf->hob_error = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100159 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200160 tf->hob_nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100161 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200162 tf->hob_lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100163 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200164 tf->hob_lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100165 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200166 tf->hob_lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100167 }
168}
169EXPORT_SYMBOL_GPL(ide_tf_read);
170
171/*
172 * Some localbus EIDE interfaces require a special access sequence
173 * when using 32-bit I/O instructions to transfer data. We call this
174 * the "vlb_sync" sequence, which consists of three successive reads
175 * of the sector count register location, with interrupts disabled
176 * to ensure that the reads all happen together.
177 */
178static void ata_vlb_sync(unsigned long port)
179{
180 (void)inb(port);
181 (void)inb(port);
182 (void)inb(port);
183}
184
185/*
186 * This is used for most PIO data transfers *from* the IDE interface
187 *
188 * These routines will round up any request for an odd number of bytes,
189 * so if an odd len is specified, be sure that there's at least one
190 * extra byte allocated for the buffer.
191 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100192void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100193 unsigned int len)
194{
195 ide_hwif_t *hwif = drive->hwif;
196 struct ide_io_ports *io_ports = &hwif->io_ports;
197 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200198 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100199 u8 io_32bit = drive->io_32bit;
200 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
201
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100202 if (io_32bit) {
203 unsigned long uninitialized_var(flags);
204
205 if ((io_32bit & 2) && !mmio) {
206 local_irq_save(flags);
207 ata_vlb_sync(io_ports->nsect_addr);
208 }
209
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200210 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100211 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200212 __ide_mm_insl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100213 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200214 insl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100215
216 if ((io_32bit & 2) && !mmio)
217 local_irq_restore(flags);
218
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200219 if (((len + 1) & 3) < 2)
220 return;
221
222 buf += len & ~3;
223 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100224 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200225
226 if (mmio)
227 __ide_mm_insw((void __iomem *)data_addr, buf, words);
228 else
229 insw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100230}
231EXPORT_SYMBOL_GPL(ide_input_data);
232
233/*
234 * This is used for most PIO data transfers *to* the IDE interface
235 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100236void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100237 unsigned int len)
238{
239 ide_hwif_t *hwif = drive->hwif;
240 struct ide_io_ports *io_ports = &hwif->io_ports;
241 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200242 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100243 u8 io_32bit = drive->io_32bit;
244 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
245
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100246 if (io_32bit) {
247 unsigned long uninitialized_var(flags);
248
249 if ((io_32bit & 2) && !mmio) {
250 local_irq_save(flags);
251 ata_vlb_sync(io_ports->nsect_addr);
252 }
253
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200254 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100255 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200256 __ide_mm_outsl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100257 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200258 outsl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100259
260 if ((io_32bit & 2) && !mmio)
261 local_irq_restore(flags);
262
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200263 if (((len + 1) & 3) < 2)
264 return;
265
266 buf += len & ~3;
267 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100268 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200269
270 if (mmio)
271 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
272 else
273 outsw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100274}
275EXPORT_SYMBOL_GPL(ide_output_data);
276
277const struct ide_tp_ops default_tp_ops = {
278 .exec_command = ide_exec_command,
279 .read_status = ide_read_status,
280 .read_altstatus = ide_read_altstatus,
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200281 .write_devctl = ide_write_devctl,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100282
283 .tf_load = ide_tf_load,
284 .tf_read = ide_tf_read,
285
286 .input_data = ide_input_data,
287 .output_data = ide_output_data,
288};