ide: move common code out of tf_load() method
[pandora-kernel.git] / drivers / ide / ide-io-std.c
1
2 #include <linux/kernel.h>
3 #include <linux/ide.h>
4
5 #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
12 /*
13  *      Conventional PIO operations for ATA devices
14  */
15
16 static u8 ide_inb(unsigned long port)
17 {
18         return (u8) inb(port);
19 }
20
21 static 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
30 static u8 ide_mm_inb(unsigned long port)
31 {
32         return (u8) readb((void __iomem *) port);
33 }
34
35 static void ide_mm_outb(u8 value, unsigned long port)
36 {
37         writeb(value, (void __iomem *) port);
38 }
39
40 void 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 }
47 EXPORT_SYMBOL_GPL(ide_exec_command);
48
49 u8 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 }
56 EXPORT_SYMBOL_GPL(ide_read_status);
57
58 u8 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 }
65 EXPORT_SYMBOL_GPL(ide_read_altstatus);
66
67 void ide_write_devctl(ide_hwif_t *hwif, u8 ctl)
68 {
69         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 }
74 EXPORT_SYMBOL_GPL(ide_write_devctl);
75
76 void 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 }
86 EXPORT_SYMBOL_GPL(ide_dev_select);
87
88 void ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
89 {
90         ide_hwif_t *hwif = drive->hwif;
91         struct ide_io_ports *io_ports = &hwif->io_ports;
92         struct ide_taskfile *tf = &cmd->hob;
93         void (*tf_outb)(u8 addr, unsigned long port);
94         u8 valid = cmd->valid.out.hob;
95         u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
96
97         if (mmio)
98                 tf_outb = ide_mm_outb;
99         else
100                 tf_outb = ide_outb;
101
102         if (valid & IDE_VALID_FEATURE)
103                 tf_outb(tf->feature, io_ports->feature_addr);
104         if (valid & IDE_VALID_NSECT)
105                 tf_outb(tf->nsect, io_ports->nsect_addr);
106         if (valid & IDE_VALID_LBAL)
107                 tf_outb(tf->lbal, io_ports->lbal_addr);
108         if (valid & IDE_VALID_LBAM)
109                 tf_outb(tf->lbam, io_ports->lbam_addr);
110         if (valid & IDE_VALID_LBAH)
111                 tf_outb(tf->lbah, io_ports->lbah_addr);
112
113         tf = &cmd->tf;
114         valid = cmd->valid.out.tf;
115
116         if (valid & IDE_VALID_FEATURE)
117                 tf_outb(tf->feature, io_ports->feature_addr);
118         if (valid & IDE_VALID_NSECT)
119                 tf_outb(tf->nsect, io_ports->nsect_addr);
120         if (valid & IDE_VALID_LBAL)
121                 tf_outb(tf->lbal, io_ports->lbal_addr);
122         if (valid & IDE_VALID_LBAM)
123                 tf_outb(tf->lbam, io_ports->lbam_addr);
124         if (valid & IDE_VALID_LBAH)
125                 tf_outb(tf->lbah, io_ports->lbah_addr);
126         if (valid & IDE_VALID_DEVICE)
127                 tf_outb(tf->device, io_ports->device_addr);
128 }
129 EXPORT_SYMBOL_GPL(ide_tf_load);
130
131 void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
132 {
133         ide_hwif_t *hwif = drive->hwif;
134         struct ide_io_ports *io_ports = &hwif->io_ports;
135         struct ide_taskfile *tf = &cmd->tf;
136         void (*tf_outb)(u8 addr, unsigned long port);
137         u8 (*tf_inb)(unsigned long port);
138         u8 valid = cmd->valid.in.tf;
139         u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
140
141         if (mmio) {
142                 tf_outb = ide_mm_outb;
143                 tf_inb  = ide_mm_inb;
144         } else {
145                 tf_outb = ide_outb;
146                 tf_inb  = ide_inb;
147         }
148
149         /* be sure we're looking at the low order bits */
150         tf_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
151
152         if (valid & IDE_VALID_ERROR)
153                 tf->error  = tf_inb(io_ports->feature_addr);
154         if (valid & IDE_VALID_NSECT)
155                 tf->nsect  = tf_inb(io_ports->nsect_addr);
156         if (valid & IDE_VALID_LBAL)
157                 tf->lbal   = tf_inb(io_ports->lbal_addr);
158         if (valid & IDE_VALID_LBAM)
159                 tf->lbam   = tf_inb(io_ports->lbam_addr);
160         if (valid & IDE_VALID_LBAH)
161                 tf->lbah   = tf_inb(io_ports->lbah_addr);
162         if (valid & IDE_VALID_DEVICE)
163                 tf->device = tf_inb(io_ports->device_addr);
164
165         if (cmd->tf_flags & IDE_TFLAG_LBA48) {
166                 tf_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
167
168                 tf = &cmd->hob;
169                 valid = cmd->valid.in.hob;
170
171                 if (valid & IDE_VALID_ERROR)
172                         tf->error = tf_inb(io_ports->feature_addr);
173                 if (valid & IDE_VALID_NSECT)
174                         tf->nsect = tf_inb(io_ports->nsect_addr);
175                 if (valid & IDE_VALID_LBAL)
176                         tf->lbal  = tf_inb(io_ports->lbal_addr);
177                 if (valid & IDE_VALID_LBAM)
178                         tf->lbam  = tf_inb(io_ports->lbam_addr);
179                 if (valid & IDE_VALID_LBAH)
180                         tf->lbah  = tf_inb(io_ports->lbah_addr);
181         }
182 }
183 EXPORT_SYMBOL_GPL(ide_tf_read);
184
185 /*
186  * Some localbus EIDE interfaces require a special access sequence
187  * when using 32-bit I/O instructions to transfer data.  We call this
188  * the "vlb_sync" sequence, which consists of three successive reads
189  * of the sector count register location, with interrupts disabled
190  * to ensure that the reads all happen together.
191  */
192 static void ata_vlb_sync(unsigned long port)
193 {
194         (void)inb(port);
195         (void)inb(port);
196         (void)inb(port);
197 }
198
199 /*
200  * This is used for most PIO data transfers *from* the IDE interface
201  *
202  * These routines will round up any request for an odd number of bytes,
203  * so if an odd len is specified, be sure that there's at least one
204  * extra byte allocated for the buffer.
205  */
206 void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
207                     unsigned int len)
208 {
209         ide_hwif_t *hwif = drive->hwif;
210         struct ide_io_ports *io_ports = &hwif->io_ports;
211         unsigned long data_addr = io_ports->data_addr;
212         unsigned int words = (len + 1) >> 1;
213         u8 io_32bit = drive->io_32bit;
214         u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
215
216         if (io_32bit) {
217                 unsigned long uninitialized_var(flags);
218
219                 if ((io_32bit & 2) && !mmio) {
220                         local_irq_save(flags);
221                         ata_vlb_sync(io_ports->nsect_addr);
222                 }
223
224                 words >>= 1;
225                 if (mmio)
226                         __ide_mm_insl((void __iomem *)data_addr, buf, words);
227                 else
228                         insl(data_addr, buf, words);
229
230                 if ((io_32bit & 2) && !mmio)
231                         local_irq_restore(flags);
232
233                 if (((len + 1) & 3) < 2)
234                         return;
235
236                 buf += len & ~3;
237                 words = 1;
238         }
239
240         if (mmio)
241                 __ide_mm_insw((void __iomem *)data_addr, buf, words);
242         else
243                 insw(data_addr, buf, words);
244 }
245 EXPORT_SYMBOL_GPL(ide_input_data);
246
247 /*
248  * This is used for most PIO data transfers *to* the IDE interface
249  */
250 void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
251                      unsigned int len)
252 {
253         ide_hwif_t *hwif = drive->hwif;
254         struct ide_io_ports *io_ports = &hwif->io_ports;
255         unsigned long data_addr = io_ports->data_addr;
256         unsigned int words = (len + 1) >> 1;
257         u8 io_32bit = drive->io_32bit;
258         u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
259
260         if (io_32bit) {
261                 unsigned long uninitialized_var(flags);
262
263                 if ((io_32bit & 2) && !mmio) {
264                         local_irq_save(flags);
265                         ata_vlb_sync(io_ports->nsect_addr);
266                 }
267
268                 words >>= 1;
269                 if (mmio)
270                         __ide_mm_outsl((void __iomem *)data_addr, buf, words);
271                 else
272                         outsl(data_addr, buf, words);
273
274                 if ((io_32bit & 2) && !mmio)
275                         local_irq_restore(flags);
276
277                 if (((len + 1) & 3) < 2)
278                         return;
279
280                 buf += len & ~3;
281                 words = 1;
282         }
283
284         if (mmio)
285                 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
286         else
287                 outsw(data_addr, buf, words);
288 }
289 EXPORT_SYMBOL_GPL(ide_output_data);
290
291 const struct ide_tp_ops default_tp_ops = {
292         .exec_command           = ide_exec_command,
293         .read_status            = ide_read_status,
294         .read_altstatus         = ide_read_altstatus,
295         .write_devctl           = ide_write_devctl,
296
297         .dev_select             = ide_dev_select,
298         .tf_load                = ide_tf_load,
299         .tf_read                = ide_tf_read,
300
301         .input_data             = ide_input_data,
302         .output_data            = ide_output_data,
303 };