blob: 19763977568c5d6fd7ecc87c3f4e7bc47fc0d85e [file] [log] [blame]
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +01001
2#include <linux/kernel.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -04003#include <linux/export.h>
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +01004#include <linux/ide.h>
5
Bartlomiej Zolnierkiewicz15a453a2009-03-27 12:46:26 +01006#if defined(CONFIG_ARM) || defined(CONFIG_M68K) || defined(CONFIG_MIPS) || \
7 defined(CONFIG_PARISC) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
8#include <asm/ide.h>
9#else
10#include <asm-generic/ide_iops.h>
11#endif
12
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010013/*
14 * Conventional PIO operations for ATA devices
15 */
16
17static u8 ide_inb(unsigned long port)
18{
19 return (u8) inb(port);
20}
21
22static void ide_outb(u8 val, unsigned long port)
23{
24 outb(val, port);
25}
26
27/*
28 * MMIO operations, typically used for SATA controllers
29 */
30
31static u8 ide_mm_inb(unsigned long port)
32{
33 return (u8) readb((void __iomem *) port);
34}
35
36static void ide_mm_outb(u8 value, unsigned long port)
37{
38 writeb(value, (void __iomem *) port);
39}
40
41void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
42{
43 if (hwif->host_flags & IDE_HFLAG_MMIO)
44 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
45 else
46 outb(cmd, hwif->io_ports.command_addr);
47}
48EXPORT_SYMBOL_GPL(ide_exec_command);
49
50u8 ide_read_status(ide_hwif_t *hwif)
51{
52 if (hwif->host_flags & IDE_HFLAG_MMIO)
53 return readb((void __iomem *)hwif->io_ports.status_addr);
54 else
55 return inb(hwif->io_ports.status_addr);
56}
57EXPORT_SYMBOL_GPL(ide_read_status);
58
59u8 ide_read_altstatus(ide_hwif_t *hwif)
60{
61 if (hwif->host_flags & IDE_HFLAG_MMIO)
62 return readb((void __iomem *)hwif->io_ports.ctl_addr);
63 else
64 return inb(hwif->io_ports.ctl_addr);
65}
66EXPORT_SYMBOL_GPL(ide_read_altstatus);
67
Sergei Shtylyovecf3a312009-03-31 20:15:30 +020068void ide_write_devctl(ide_hwif_t *hwif, u8 ctl)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010069{
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010070 if (hwif->host_flags & IDE_HFLAG_MMIO)
71 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
72 else
73 outb(ctl, hwif->io_ports.ctl_addr);
74}
Sergei Shtylyovecf3a312009-03-31 20:15:30 +020075EXPORT_SYMBOL_GPL(ide_write_devctl);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010076
Sergei Shtylyovabb596b2009-03-31 20:15:32 +020077void ide_dev_select(ide_drive_t *drive)
78{
79 ide_hwif_t *hwif = drive->hwif;
80 u8 select = drive->select | ATA_DEVICE_OBS;
81
82 if (hwif->host_flags & IDE_HFLAG_MMIO)
83 writeb(select, (void __iomem *)hwif->io_ports.device_addr);
84 else
85 outb(select, hwif->io_ports.device_addr);
86}
87EXPORT_SYMBOL_GPL(ide_dev_select);
88
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +020089void ide_tf_load(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +010090{
91 ide_hwif_t *hwif = drive->hwif;
92 struct ide_io_ports *io_ports = &hwif->io_ports;
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 Zolnierkiewicz1574cf62009-03-24 23:22:46 +010095
96 if (mmio)
97 tf_outb = ide_mm_outb;
98 else
99 tf_outb = ide_outb;
100
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200101 if (valid & IDE_VALID_FEATURE)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200102 tf_outb(tf->feature, io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200103 if (valid & IDE_VALID_NSECT)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200104 tf_outb(tf->nsect, io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200105 if (valid & IDE_VALID_LBAL)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200106 tf_outb(tf->lbal, io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200107 if (valid & IDE_VALID_LBAM)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200108 tf_outb(tf->lbam, io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200109 if (valid & IDE_VALID_LBAH)
Sergei Shtylyov745483f2009-04-08 14:13:02 +0200110 tf_outb(tf->lbah, io_ports->lbah_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200111 if (valid & IDE_VALID_DEVICE)
Sergei Shtylyov4109d192009-04-08 14:13:02 +0200112 tf_outb(tf->device, io_ports->device_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100113}
114EXPORT_SYMBOL_GPL(ide_tf_load);
115
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200116void ide_tf_read(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100117{
118 ide_hwif_t *hwif = drive->hwif;
119 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100120 u8 (*tf_inb)(unsigned long port);
121 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
122
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200123 if (mmio)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100124 tf_inb = ide_mm_inb;
Sergei Shtylyov30881b92009-04-08 14:13:02 +0200125 else
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100126 tf_inb = ide_inb;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100127
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200128 if (valid & IDE_VALID_ERROR)
Sergei Shtylyov67625112009-03-31 20:15:30 +0200129 tf->error = tf_inb(io_ports->feature_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200130 if (valid & IDE_VALID_NSECT)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100131 tf->nsect = tf_inb(io_ports->nsect_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200132 if (valid & IDE_VALID_LBAL)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100133 tf->lbal = tf_inb(io_ports->lbal_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200134 if (valid & IDE_VALID_LBAM)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100135 tf->lbam = tf_inb(io_ports->lbam_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200136 if (valid & IDE_VALID_LBAH)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100137 tf->lbah = tf_inb(io_ports->lbah_addr);
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200138 if (valid & IDE_VALID_DEVICE)
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100139 tf->device = tf_inb(io_ports->device_addr);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100140}
141EXPORT_SYMBOL_GPL(ide_tf_read);
142
143/*
144 * Some localbus EIDE interfaces require a special access sequence
145 * when using 32-bit I/O instructions to transfer data. We call this
146 * the "vlb_sync" sequence, which consists of three successive reads
147 * of the sector count register location, with interrupts disabled
148 * to ensure that the reads all happen together.
149 */
150static void ata_vlb_sync(unsigned long port)
151{
152 (void)inb(port);
153 (void)inb(port);
154 (void)inb(port);
155}
156
157/*
158 * This is used for most PIO data transfers *from* the IDE interface
159 *
160 * These routines will round up any request for an odd number of bytes,
161 * so if an odd len is specified, be sure that there's at least one
162 * extra byte allocated for the buffer.
163 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100164void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100165 unsigned int len)
166{
167 ide_hwif_t *hwif = drive->hwif;
168 struct ide_io_ports *io_ports = &hwif->io_ports;
169 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200170 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100171 u8 io_32bit = drive->io_32bit;
172 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
173
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100174 if (io_32bit) {
175 unsigned long uninitialized_var(flags);
176
177 if ((io_32bit & 2) && !mmio) {
178 local_irq_save(flags);
179 ata_vlb_sync(io_ports->nsect_addr);
180 }
181
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200182 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100183 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200184 __ide_mm_insl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100185 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200186 insl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100187
188 if ((io_32bit & 2) && !mmio)
189 local_irq_restore(flags);
190
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200191 if (((len + 1) & 3) < 2)
192 return;
193
194 buf += len & ~3;
195 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100196 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200197
198 if (mmio)
199 __ide_mm_insw((void __iomem *)data_addr, buf, words);
200 else
201 insw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100202}
203EXPORT_SYMBOL_GPL(ide_input_data);
204
205/*
206 * This is used for most PIO data transfers *to* the IDE interface
207 */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100208void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100209 unsigned int len)
210{
211 ide_hwif_t *hwif = drive->hwif;
212 struct ide_io_ports *io_ports = &hwif->io_ports;
213 unsigned long data_addr = io_ports->data_addr;
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200214 unsigned int words = (len + 1) >> 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100215 u8 io_32bit = drive->io_32bit;
216 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
217
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100218 if (io_32bit) {
219 unsigned long uninitialized_var(flags);
220
221 if ((io_32bit & 2) && !mmio) {
222 local_irq_save(flags);
223 ata_vlb_sync(io_ports->nsect_addr);
224 }
225
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200226 words >>= 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100227 if (mmio)
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200228 __ide_mm_outsl((void __iomem *)data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100229 else
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200230 outsl(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100231
232 if ((io_32bit & 2) && !mmio)
233 local_irq_restore(flags);
234
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200235 if (((len + 1) & 3) < 2)
236 return;
237
238 buf += len & ~3;
239 words = 1;
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100240 }
Sergei Shtylyovdeae17f2009-03-31 20:15:31 +0200241
242 if (mmio)
243 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
244 else
245 outsw(data_addr, buf, words);
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100246}
247EXPORT_SYMBOL_GPL(ide_output_data);
248
249const struct ide_tp_ops default_tp_ops = {
250 .exec_command = ide_exec_command,
251 .read_status = ide_read_status,
252 .read_altstatus = ide_read_altstatus,
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200253 .write_devctl = ide_write_devctl,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100254
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200255 .dev_select = ide_dev_select,
Bartlomiej Zolnierkiewicz1574cf62009-03-24 23:22:46 +0100256 .tf_load = ide_tf_load,
257 .tf_read = ide_tf_read,
258
259 .input_data = ide_input_data,
260 .output_data = ide_output_data,
261};