blob: 9cac281d82c485857a6f62d9d346c0d51de6de1e [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
Sergei Shtylyovabb596b2009-03-31 20:15:32 +020076void ide_dev_select(ide_drive_t *drive)
77{
78 ide_hwif_t *hwif = drive->hwif;
79 u8 select = drive->select | ATA_DEVICE_OBS;
80
81 if (hwif->host_flags & IDE_HFLAG_MMIO)
82 writeb(select, (void __iomem *)hwif->io_ports.device_addr);
83 else
84 outb(select, hwif->io_ports.device_addr);
85}
86EXPORT_SYMBOL_GPL(ide_dev_select);
87
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010088void ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010089{
90 ide_hwif_t *hwif = drive->hwif;
91 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010092 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010093 void (*tf_outb)(u8 addr, unsigned long port);
94 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010095 u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010096
97 if (mmio)
98 tf_outb = ide_mm_outb;
99 else
100 tf_outb = ide_outb;
101
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100102 if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100103 HIHI = 0xFF;
104
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100105 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100106 tf_outb(tf->hob_feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100107 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100108 tf_outb(tf->hob_nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100109 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100110 tf_outb(tf->hob_lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100111 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100112 tf_outb(tf->hob_lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100113 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100114 tf_outb(tf->hob_lbah, io_ports->lbah_addr);
115
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100116 if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100117 tf_outb(tf->feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100118 if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100119 tf_outb(tf->nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100120 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100121 tf_outb(tf->lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100122 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100123 tf_outb(tf->lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100124 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100125 tf_outb(tf->lbah, io_ports->lbah_addr);
126
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100127 if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100128 tf_outb((tf->device & HIHI) | drive->select,
129 io_ports->device_addr);
130}
131EXPORT_SYMBOL_GPL(ide_tf_load);
132
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100133void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100134{
135 ide_hwif_t *hwif = drive->hwif;
136 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100137 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100138 void (*tf_outb)(u8 addr, unsigned long port);
139 u8 (*tf_inb)(unsigned long port);
140 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
141
142 if (mmio) {
143 tf_outb = ide_mm_outb;
144 tf_inb = ide_mm_inb;
145 } else {
146 tf_outb = ide_outb;
147 tf_inb = ide_inb;
148 }
149
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100150 /* be sure we're looking at the low order bits */
Sergei Shtylyov4d74c3f2009-03-31 20:15:29 +0200151 tf_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100152
Sergei Shtylyov67625112009-03-31 20:15:30 +0200153 if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
154 tf->error = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100155 if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100156 tf->nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100157 if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100158 tf->lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100159 if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100160 tf->lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100161 if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100162 tf->lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100163 if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100164 tf->device = tf_inb(io_ports->device_addr);
165
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100166 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
Sergei Shtylyov4d74c3f2009-03-31 20:15:29 +0200167 tf_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100168
Sergei Shtylyov67625112009-03-31 20:15:30 +0200169 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
170 tf->hob_error = tf_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100171 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200172 tf->hob_nsect = tf_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100173 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200174 tf->hob_lbal = tf_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100175 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200176 tf->hob_lbam = tf_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100177 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200178 tf->hob_lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100179 }
180}
181EXPORT_SYMBOL_GPL(ide_tf_read);
182
183/*
184 * Some localbus EIDE interfaces require a special access sequence
185 * when using 32-bit I/O instructions to transfer data. We call this
186 * the "vlb_sync" sequence, which consists of three successive reads
187 * of the sector count register location, with interrupts disabled
188 * to ensure that the reads all happen together.
189 */
190static void ata_vlb_sync(unsigned long port)
191{
192 (void)inb(port);
193 (void)inb(port);
194 (void)inb(port);
195}
196
197/*
198 * This is used for most PIO data transfers *from* the IDE interface
199 *
200 * These routines will round up any request for an odd number of bytes,
201 * so if an odd len is specified, be sure that there's at least one
202 * extra byte allocated for the buffer.
203 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100204void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100205 unsigned int len)
206{
207 ide_hwif_t *hwif = drive->hwif;
208 struct ide_io_ports *io_ports = &hwif->io_ports;
209 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200210 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100211 u8 io_32bit = drive->io_32bit;
212 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
213
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100214 if (io_32bit) {
215 unsigned long uninitialized_var(flags);
216
217 if ((io_32bit & 2) && !mmio) {
218 local_irq_save(flags);
219 ata_vlb_sync(io_ports->nsect_addr);
220 }
221
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200222 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100223 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200224 __ide_mm_insl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100225 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200226 insl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100227
228 if ((io_32bit & 2) && !mmio)
229 local_irq_restore(flags);
230
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200231 if (((len + 1) & 3) < 2)
232 return;
233
234 buf += len & ~3;
235 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100236 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200237
238 if (mmio)
239 __ide_mm_insw((void __iomem *)data_addr, buf, words);
240 else
241 insw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100242}
243EXPORT_SYMBOL_GPL(ide_input_data);
244
245/*
246 * This is used for most PIO data transfers *to* the IDE interface
247 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100248void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100249 unsigned int len)
250{
251 ide_hwif_t *hwif = drive->hwif;
252 struct ide_io_ports *io_ports = &hwif->io_ports;
253 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200254 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100255 u8 io_32bit = drive->io_32bit;
256 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
257
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100258 if (io_32bit) {
259 unsigned long uninitialized_var(flags);
260
261 if ((io_32bit & 2) && !mmio) {
262 local_irq_save(flags);
263 ata_vlb_sync(io_ports->nsect_addr);
264 }
265
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200266 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100267 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200268 __ide_mm_outsl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100269 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200270 outsl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100271
272 if ((io_32bit & 2) && !mmio)
273 local_irq_restore(flags);
274
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200275 if (((len + 1) & 3) < 2)
276 return;
277
278 buf += len & ~3;
279 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100280 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200281
282 if (mmio)
283 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
284 else
285 outsw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100286}
287EXPORT_SYMBOL_GPL(ide_output_data);
288
289const struct ide_tp_ops default_tp_ops = {
290 .exec_command = ide_exec_command,
291 .read_status = ide_read_status,
292 .read_altstatus = ide_read_altstatus,
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200293 .write_devctl = ide_write_devctl,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100294
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200295 .dev_select = ide_dev_select,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100296 .tf_load = ide_tf_load,
297 .tf_read = ide_tf_read,
298
299 .input_data = ide_input_data,
300 .output_data = ide_output_data,
301};