blob: 66c27768e85be5d61817baf7125e9a700983f63b [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;
Sergei Shtylyov745483f2009-04-08 14:13:02 +020092 struct ide_taskfile *tf = &cmd->hob;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010093 void (*tf_outb)(u8 addr, unsigned long port);
Sergei Shtylyov60f85012009-04-08 14:13:01 +020094 u8 valid = cmd->valid.out.hob;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010095 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
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
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200102 if (valid & IDE_VALID_FEATURE)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200103 tf_outb(tf->feature, io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200104 if (valid & IDE_VALID_NSECT)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200105 tf_outb(tf->nsect, io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200106 if (valid & IDE_VALID_LBAL)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200107 tf_outb(tf->lbal, io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200108 if (valid & IDE_VALID_LBAM)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200109 tf_outb(tf->lbam, io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200110 if (valid & IDE_VALID_LBAH)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200111 tf_outb(tf->lbah, io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100112
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200113 tf = &cmd->tf;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200114 valid = cmd->valid.out.tf;
115
116 if (valid & IDE_VALID_FEATURE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100117 tf_outb(tf->feature, io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200118 if (valid & IDE_VALID_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100119 tf_outb(tf->nsect, io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200120 if (valid & IDE_VALID_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100121 tf_outb(tf->lbal, io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200122 if (valid & IDE_VALID_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100123 tf_outb(tf->lbam, io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200124 if (valid & IDE_VALID_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100125 tf_outb(tf->lbah, io_ports->lbah_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200126 if (valid & IDE_VALID_DEVICE)
Sergei Shtylyov4109d192009-04-08 14:13:02 +0200127 tf_outb(tf->device, io_ports->device_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100128}
129EXPORT_SYMBOL_GPL(ide_tf_load);
130
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100131void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100132{
133 ide_hwif_t *hwif = drive->hwif;
134 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100135 struct ide_taskfile *tf = &cmd->tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100136 u8 (*tf_inb)(unsigned long port);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200137 u8 valid = cmd->valid.in.tf;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100138 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
139
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200140 if (mmio)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100141 tf_inb = ide_mm_inb;
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200142 else
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100143 tf_inb = ide_inb;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100144
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100145 /* be sure we're looking at the low order bits */
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200146 hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100147
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200148 if (valid & IDE_VALID_ERROR)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200149 tf->error = tf_inb(io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200150 if (valid & IDE_VALID_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100151 tf->nsect = tf_inb(io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200152 if (valid & IDE_VALID_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100153 tf->lbal = tf_inb(io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200154 if (valid & IDE_VALID_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100155 tf->lbam = tf_inb(io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200156 if (valid & IDE_VALID_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100157 tf->lbah = tf_inb(io_ports->lbah_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200158 if (valid & IDE_VALID_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100159 tf->device = tf_inb(io_ports->device_addr);
160
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100161 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200162 hwif->tp_ops->write_devctl(hwif, ATA_HOB | ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100163
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200164 tf = &cmd->hob;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200165 valid = cmd->valid.in.hob;
166
167 if (valid & IDE_VALID_ERROR)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200168 tf->error = tf_inb(io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200169 if (valid & IDE_VALID_NSECT)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200170 tf->nsect = tf_inb(io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200171 if (valid & IDE_VALID_LBAL)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200172 tf->lbal = tf_inb(io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200173 if (valid & IDE_VALID_LBAM)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200174 tf->lbam = tf_inb(io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200175 if (valid & IDE_VALID_LBAH)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200176 tf->lbah = tf_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100177 }
178}
179EXPORT_SYMBOL_GPL(ide_tf_read);
180
181/*
182 * Some localbus EIDE interfaces require a special access sequence
183 * when using 32-bit I/O instructions to transfer data. We call this
184 * the "vlb_sync" sequence, which consists of three successive reads
185 * of the sector count register location, with interrupts disabled
186 * to ensure that the reads all happen together.
187 */
188static void ata_vlb_sync(unsigned long port)
189{
190 (void)inb(port);
191 (void)inb(port);
192 (void)inb(port);
193}
194
195/*
196 * This is used for most PIO data transfers *from* the IDE interface
197 *
198 * These routines will round up any request for an odd number of bytes,
199 * so if an odd len is specified, be sure that there's at least one
200 * extra byte allocated for the buffer.
201 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100202void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100203 unsigned int len)
204{
205 ide_hwif_t *hwif = drive->hwif;
206 struct ide_io_ports *io_ports = &hwif->io_ports;
207 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200208 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100209 u8 io_32bit = drive->io_32bit;
210 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
211
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100212 if (io_32bit) {
213 unsigned long uninitialized_var(flags);
214
215 if ((io_32bit & 2) && !mmio) {
216 local_irq_save(flags);
217 ata_vlb_sync(io_ports->nsect_addr);
218 }
219
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200220 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100221 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200222 __ide_mm_insl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100223 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200224 insl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100225
226 if ((io_32bit & 2) && !mmio)
227 local_irq_restore(flags);
228
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200229 if (((len + 1) & 3) < 2)
230 return;
231
232 buf += len & ~3;
233 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100234 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200235
236 if (mmio)
237 __ide_mm_insw((void __iomem *)data_addr, buf, words);
238 else
239 insw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100240}
241EXPORT_SYMBOL_GPL(ide_input_data);
242
243/*
244 * This is used for most PIO data transfers *to* the IDE interface
245 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100246void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100247 unsigned int len)
248{
249 ide_hwif_t *hwif = drive->hwif;
250 struct ide_io_ports *io_ports = &hwif->io_ports;
251 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200252 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100253 u8 io_32bit = drive->io_32bit;
254 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
255
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100256 if (io_32bit) {
257 unsigned long uninitialized_var(flags);
258
259 if ((io_32bit & 2) && !mmio) {
260 local_irq_save(flags);
261 ata_vlb_sync(io_ports->nsect_addr);
262 }
263
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200264 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100265 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200266 __ide_mm_outsl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100267 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200268 outsl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100269
270 if ((io_32bit & 2) && !mmio)
271 local_irq_restore(flags);
272
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200273 if (((len + 1) & 3) < 2)
274 return;
275
276 buf += len & ~3;
277 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100278 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200279
280 if (mmio)
281 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
282 else
283 outsw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100284}
285EXPORT_SYMBOL_GPL(ide_output_data);
286
287const struct ide_tp_ops default_tp_ops = {
288 .exec_command = ide_exec_command,
289 .read_status = ide_read_status,
290 .read_altstatus = ide_read_altstatus,
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200291 .write_devctl = ide_write_devctl,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100292
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200293 .dev_select = ide_dev_select,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100294 .tf_load = ide_tf_load,
295 .tf_read = ide_tf_read,
296
297 .input_data = ide_input_data,
298 .output_data = ide_output_data,
299};