Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 1 | |
| 2 | #include <linux/kernel.h> |
Paul Gortmaker | 38789fd | 2011-07-17 15:33:58 -0400 | [diff] [blame^] | 3 | #include <linux/export.h> |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 4 | #include <linux/ide.h> |
| 5 | |
Bartlomiej Zolnierkiewicz | 15a453a | 2009-03-27 12:46:26 +0100 | [diff] [blame] | 6 | #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 Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 13 | /* |
| 14 | * Conventional PIO operations for ATA devices |
| 15 | */ |
| 16 | |
| 17 | static u8 ide_inb(unsigned long port) |
| 18 | { |
| 19 | return (u8) inb(port); |
| 20 | } |
| 21 | |
| 22 | static 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 | |
| 31 | static u8 ide_mm_inb(unsigned long port) |
| 32 | { |
| 33 | return (u8) readb((void __iomem *) port); |
| 34 | } |
| 35 | |
| 36 | static void ide_mm_outb(u8 value, unsigned long port) |
| 37 | { |
| 38 | writeb(value, (void __iomem *) port); |
| 39 | } |
| 40 | |
| 41 | void 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 | } |
| 48 | EXPORT_SYMBOL_GPL(ide_exec_command); |
| 49 | |
| 50 | u8 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 | } |
| 57 | EXPORT_SYMBOL_GPL(ide_read_status); |
| 58 | |
| 59 | u8 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 | } |
| 66 | EXPORT_SYMBOL_GPL(ide_read_altstatus); |
| 67 | |
Sergei Shtylyov | ecf3a31 | 2009-03-31 20:15:30 +0200 | [diff] [blame] | 68 | void ide_write_devctl(ide_hwif_t *hwif, u8 ctl) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 69 | { |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 70 | 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 Shtylyov | ecf3a31 | 2009-03-31 20:15:30 +0200 | [diff] [blame] | 75 | EXPORT_SYMBOL_GPL(ide_write_devctl); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 76 | |
Sergei Shtylyov | abb596b | 2009-03-31 20:15:32 +0200 | [diff] [blame] | 77 | void 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 | } |
| 87 | EXPORT_SYMBOL_GPL(ide_dev_select); |
| 88 | |
Sergei Shtylyov | c9ff9e7 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 89 | void ide_tf_load(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 90 | { |
| 91 | ide_hwif_t *hwif = drive->hwif; |
| 92 | struct ide_io_ports *io_ports = &hwif->io_ports; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 93 | void (*tf_outb)(u8 addr, unsigned long port); |
| 94 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 95 | |
| 96 | if (mmio) |
| 97 | tf_outb = ide_mm_outb; |
| 98 | else |
| 99 | tf_outb = ide_outb; |
| 100 | |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 101 | if (valid & IDE_VALID_FEATURE) |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 102 | tf_outb(tf->feature, io_ports->feature_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 103 | if (valid & IDE_VALID_NSECT) |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 104 | tf_outb(tf->nsect, io_ports->nsect_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 105 | if (valid & IDE_VALID_LBAL) |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 106 | tf_outb(tf->lbal, io_ports->lbal_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 107 | if (valid & IDE_VALID_LBAM) |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 108 | tf_outb(tf->lbam, io_ports->lbam_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 109 | if (valid & IDE_VALID_LBAH) |
Sergei Shtylyov | 745483f | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 110 | tf_outb(tf->lbah, io_ports->lbah_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 111 | if (valid & IDE_VALID_DEVICE) |
Sergei Shtylyov | 4109d19 | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 112 | tf_outb(tf->device, io_ports->device_addr); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL_GPL(ide_tf_load); |
| 115 | |
Sergei Shtylyov | 3153c26 | 2009-04-08 14:13:03 +0200 | [diff] [blame] | 116 | void ide_tf_read(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 117 | { |
| 118 | ide_hwif_t *hwif = drive->hwif; |
| 119 | struct ide_io_ports *io_ports = &hwif->io_ports; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 120 | u8 (*tf_inb)(unsigned long port); |
| 121 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
| 122 | |
Sergei Shtylyov | 30881b9 | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 123 | if (mmio) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 124 | tf_inb = ide_mm_inb; |
Sergei Shtylyov | 30881b9 | 2009-04-08 14:13:02 +0200 | [diff] [blame] | 125 | else |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 126 | tf_inb = ide_inb; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 127 | |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 128 | if (valid & IDE_VALID_ERROR) |
Sergei Shtylyov | 6762511 | 2009-03-31 20:15:30 +0200 | [diff] [blame] | 129 | tf->error = tf_inb(io_ports->feature_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 130 | if (valid & IDE_VALID_NSECT) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 131 | tf->nsect = tf_inb(io_ports->nsect_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 132 | if (valid & IDE_VALID_LBAL) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 133 | tf->lbal = tf_inb(io_ports->lbal_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 134 | if (valid & IDE_VALID_LBAM) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 135 | tf->lbam = tf_inb(io_ports->lbam_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 136 | if (valid & IDE_VALID_LBAH) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 137 | tf->lbah = tf_inb(io_ports->lbah_addr); |
Sergei Shtylyov | 60f8501 | 2009-04-08 14:13:01 +0200 | [diff] [blame] | 138 | if (valid & IDE_VALID_DEVICE) |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 139 | tf->device = tf_inb(io_ports->device_addr); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 140 | } |
| 141 | EXPORT_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 | */ |
| 150 | static 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 Zolnierkiewicz | adb1af9 | 2009-03-27 12:46:38 +0100 | [diff] [blame] | 164 | void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf, |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 165 | 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 Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 170 | unsigned int words = (len + 1) >> 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 171 | u8 io_32bit = drive->io_32bit; |
| 172 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
| 173 | |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 174 | 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 Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 182 | words >>= 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 183 | if (mmio) |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 184 | __ide_mm_insl((void __iomem *)data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 185 | else |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 186 | insl(data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 187 | |
| 188 | if ((io_32bit & 2) && !mmio) |
| 189 | local_irq_restore(flags); |
| 190 | |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 191 | if (((len + 1) & 3) < 2) |
| 192 | return; |
| 193 | |
| 194 | buf += len & ~3; |
| 195 | words = 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 196 | } |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 197 | |
| 198 | if (mmio) |
| 199 | __ide_mm_insw((void __iomem *)data_addr, buf, words); |
| 200 | else |
| 201 | insw(data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 202 | } |
| 203 | EXPORT_SYMBOL_GPL(ide_input_data); |
| 204 | |
| 205 | /* |
| 206 | * This is used for most PIO data transfers *to* the IDE interface |
| 207 | */ |
Bartlomiej Zolnierkiewicz | adb1af9 | 2009-03-27 12:46:38 +0100 | [diff] [blame] | 208 | void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf, |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 209 | 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 Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 214 | unsigned int words = (len + 1) >> 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 215 | u8 io_32bit = drive->io_32bit; |
| 216 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
| 217 | |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 218 | 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 Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 226 | words >>= 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 227 | if (mmio) |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 228 | __ide_mm_outsl((void __iomem *)data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 229 | else |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 230 | outsl(data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 231 | |
| 232 | if ((io_32bit & 2) && !mmio) |
| 233 | local_irq_restore(flags); |
| 234 | |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 235 | if (((len + 1) & 3) < 2) |
| 236 | return; |
| 237 | |
| 238 | buf += len & ~3; |
| 239 | words = 1; |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 240 | } |
Sergei Shtylyov | deae17f | 2009-03-31 20:15:31 +0200 | [diff] [blame] | 241 | |
| 242 | if (mmio) |
| 243 | __ide_mm_outsw((void __iomem *)data_addr, buf, words); |
| 244 | else |
| 245 | outsw(data_addr, buf, words); |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 246 | } |
| 247 | EXPORT_SYMBOL_GPL(ide_output_data); |
| 248 | |
| 249 | const 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 Shtylyov | ecf3a31 | 2009-03-31 20:15:30 +0200 | [diff] [blame] | 253 | .write_devctl = ide_write_devctl, |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 254 | |
Sergei Shtylyov | abb596b | 2009-03-31 20:15:32 +0200 | [diff] [blame] | 255 | .dev_select = ide_dev_select, |
Bartlomiej Zolnierkiewicz | 1574cf6 | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 256 | .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 | }; |