1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/kernel.h>
4 #include <linux/export.h>
7 #if defined(CONFIG_ARM) || defined(CONFIG_M68K) || defined(CONFIG_MIPS) || \
8 defined(CONFIG_PARISC) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
11 #include <asm-generic/ide_iops.h>
15 * Conventional PIO operations for ATA devices
18 static u8 ide_inb(unsigned long port)
20 return (u8) inb(port);
23 static void ide_outb(u8 val, unsigned long port)
29 * MMIO operations, typically used for SATA controllers
32 static u8 ide_mm_inb(unsigned long port)
34 return (u8) readb((void __iomem *) port);
37 static void ide_mm_outb(u8 value, unsigned long port)
39 writeb(value, (void __iomem *) port);
42 void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
44 if (hwif->host_flags & IDE_HFLAG_MMIO)
45 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
47 outb(cmd, hwif->io_ports.command_addr);
49 EXPORT_SYMBOL_GPL(ide_exec_command);
51 u8 ide_read_status(ide_hwif_t *hwif)
53 if (hwif->host_flags & IDE_HFLAG_MMIO)
54 return readb((void __iomem *)hwif->io_ports.status_addr);
56 return inb(hwif->io_ports.status_addr);
58 EXPORT_SYMBOL_GPL(ide_read_status);
60 u8 ide_read_altstatus(ide_hwif_t *hwif)
62 if (hwif->host_flags & IDE_HFLAG_MMIO)
63 return readb((void __iomem *)hwif->io_ports.ctl_addr);
65 return inb(hwif->io_ports.ctl_addr);
67 EXPORT_SYMBOL_GPL(ide_read_altstatus);
69 void ide_write_devctl(ide_hwif_t *hwif, u8 ctl)
71 if (hwif->host_flags & IDE_HFLAG_MMIO)
72 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
74 outb(ctl, hwif->io_ports.ctl_addr);
76 EXPORT_SYMBOL_GPL(ide_write_devctl);
78 void ide_dev_select(ide_drive_t *drive)
80 ide_hwif_t *hwif = drive->hwif;
81 u8 select = drive->select | ATA_DEVICE_OBS;
83 if (hwif->host_flags & IDE_HFLAG_MMIO)
84 writeb(select, (void __iomem *)hwif->io_ports.device_addr);
86 outb(select, hwif->io_ports.device_addr);
88 EXPORT_SYMBOL_GPL(ide_dev_select);
90 void ide_tf_load(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
92 ide_hwif_t *hwif = drive->hwif;
93 struct ide_io_ports *io_ports = &hwif->io_ports;
94 void (*tf_outb)(u8 addr, unsigned long port);
95 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
98 tf_outb = ide_mm_outb;
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 if (valid & IDE_VALID_DEVICE)
113 tf_outb(tf->device, io_ports->device_addr);
115 EXPORT_SYMBOL_GPL(ide_tf_load);
117 void ide_tf_read(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
119 ide_hwif_t *hwif = drive->hwif;
120 struct ide_io_ports *io_ports = &hwif->io_ports;
121 u8 (*tf_inb)(unsigned long port);
122 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
129 if (valid & IDE_VALID_ERROR)
130 tf->error = tf_inb(io_ports->feature_addr);
131 if (valid & IDE_VALID_NSECT)
132 tf->nsect = tf_inb(io_ports->nsect_addr);
133 if (valid & IDE_VALID_LBAL)
134 tf->lbal = tf_inb(io_ports->lbal_addr);
135 if (valid & IDE_VALID_LBAM)
136 tf->lbam = tf_inb(io_ports->lbam_addr);
137 if (valid & IDE_VALID_LBAH)
138 tf->lbah = tf_inb(io_ports->lbah_addr);
139 if (valid & IDE_VALID_DEVICE)
140 tf->device = tf_inb(io_ports->device_addr);
142 EXPORT_SYMBOL_GPL(ide_tf_read);
145 * Some localbus EIDE interfaces require a special access sequence
146 * when using 32-bit I/O instructions to transfer data. We call this
147 * the "vlb_sync" sequence, which consists of three successive reads
148 * of the sector count register location, with interrupts disabled
149 * to ensure that the reads all happen together.
151 static void ata_vlb_sync(unsigned long port)
159 * This is used for most PIO data transfers *from* the IDE interface
161 * These routines will round up any request for an odd number of bytes,
162 * so if an odd len is specified, be sure that there's at least one
163 * extra byte allocated for the buffer.
165 void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
168 ide_hwif_t *hwif = drive->hwif;
169 struct ide_io_ports *io_ports = &hwif->io_ports;
170 unsigned long data_addr = io_ports->data_addr;
171 unsigned int words = (len + 1) >> 1;
172 u8 io_32bit = drive->io_32bit;
173 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
178 if ((io_32bit & 2) && !mmio) {
179 local_irq_save(flags);
180 ata_vlb_sync(io_ports->nsect_addr);
185 __ide_mm_insl((void __iomem *)data_addr, buf, words);
187 insl(data_addr, buf, words);
189 if ((io_32bit & 2) && !mmio)
190 local_irq_restore(flags);
192 if (((len + 1) & 3) < 2)
200 __ide_mm_insw((void __iomem *)data_addr, buf, words);
202 insw(data_addr, buf, words);
204 EXPORT_SYMBOL_GPL(ide_input_data);
207 * This is used for most PIO data transfers *to* the IDE interface
209 void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
212 ide_hwif_t *hwif = drive->hwif;
213 struct ide_io_ports *io_ports = &hwif->io_ports;
214 unsigned long data_addr = io_ports->data_addr;
215 unsigned int words = (len + 1) >> 1;
216 u8 io_32bit = drive->io_32bit;
217 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
222 if ((io_32bit & 2) && !mmio) {
223 local_irq_save(flags);
224 ata_vlb_sync(io_ports->nsect_addr);
229 __ide_mm_outsl((void __iomem *)data_addr, buf, words);
231 outsl(data_addr, buf, words);
233 if ((io_32bit & 2) && !mmio)
234 local_irq_restore(flags);
236 if (((len + 1) & 3) < 2)
244 __ide_mm_outsw((void __iomem *)data_addr, buf, words);
246 outsw(data_addr, buf, words);
248 EXPORT_SYMBOL_GPL(ide_output_data);
250 const struct ide_tp_ops default_tp_ops = {
251 .exec_command = ide_exec_command,
252 .read_status = ide_read_status,
253 .read_altstatus = ide_read_altstatus,
254 .write_devctl = ide_write_devctl,
256 .dev_select = ide_dev_select,
257 .tf_load = ide_tf_load,
258 .tf_read = ide_tf_read,
260 .input_data = ide_input_data,
261 .output_data = ide_output_data,