Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org> |
| 3 | * Copyright (C) 2003 Red Hat <alan@redhat.com> |
| 4 | * |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/timer.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/major.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/genhd.h> |
| 17 | #include <linux/blkpg.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/hdreg.h> |
| 22 | #include <linux/ide.h> |
| 23 | #include <linux/bitops.h> |
Michal Schmidt | 1e86240 | 2006-07-30 03:03:29 -0700 | [diff] [blame] | 24 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/byteorder.h> |
| 27 | #include <asm/irq.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/io.h> |
| 30 | |
| 31 | /* |
| 32 | * Conventional PIO operations for ATA devices |
| 33 | */ |
| 34 | |
| 35 | static u8 ide_inb (unsigned long port) |
| 36 | { |
| 37 | return (u8) inb(port); |
| 38 | } |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static void ide_outb (u8 val, unsigned long port) |
| 41 | { |
| 42 | outb(val, port); |
| 43 | } |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * MMIO operations, typically used for SATA controllers |
| 47 | */ |
| 48 | |
| 49 | static u8 ide_mm_inb (unsigned long port) |
| 50 | { |
| 51 | return (u8) readb((void __iomem *) port); |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static void ide_mm_outb (u8 value, unsigned long port) |
| 55 | { |
| 56 | writeb(value, (void __iomem *) port); |
| 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | void SELECT_DRIVE (ide_drive_t *drive) |
| 60 | { |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 61 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 62 | const struct ide_port_ops *port_ops = hwif->port_ops; |
Bartlomiej Zolnierkiewicz | 40f095f | 2008-07-23 19:55:53 +0200 | [diff] [blame] | 63 | ide_task_t task; |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 64 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 65 | if (port_ops && port_ops->selectproc) |
| 66 | port_ops->selectproc(drive); |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 67 | |
Bartlomiej Zolnierkiewicz | 40f095f | 2008-07-23 19:55:53 +0200 | [diff] [blame] | 68 | memset(&task, 0, sizeof(task)); |
| 69 | task.tf_flags = IDE_TFLAG_OUT_DEVICE; |
| 70 | |
| 71 | drive->hwif->tf_load(drive, &task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Bartlomiej Zolnierkiewicz | ed4af48 | 2008-07-15 21:21:48 +0200 | [diff] [blame] | 74 | void SELECT_MASK(ide_drive_t *drive, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 76 | const struct ide_port_ops *port_ops = drive->hwif->port_ops; |
| 77 | |
| 78 | if (port_ops && port_ops->maskproc) |
| 79 | port_ops->maskproc(drive, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 82 | static void ide_exec_command(ide_hwif_t *hwif, u8 cmd) |
| 83 | { |
| 84 | if (hwif->host_flags & IDE_HFLAG_MMIO) |
| 85 | writeb(cmd, (void __iomem *)hwif->io_ports.command_addr); |
| 86 | else |
| 87 | outb(cmd, hwif->io_ports.command_addr); |
| 88 | } |
| 89 | |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 90 | static u8 ide_read_status(ide_hwif_t *hwif) |
| 91 | { |
| 92 | if (hwif->host_flags & IDE_HFLAG_MMIO) |
| 93 | return readb((void __iomem *)hwif->io_ports.status_addr); |
| 94 | else |
| 95 | return inb(hwif->io_ports.status_addr); |
| 96 | } |
| 97 | |
Bartlomiej Zolnierkiewicz | 1f6d8a0 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 98 | static u8 ide_read_altstatus(ide_hwif_t *hwif) |
| 99 | { |
| 100 | if (hwif->host_flags & IDE_HFLAG_MMIO) |
| 101 | return readb((void __iomem *)hwif->io_ports.ctl_addr); |
| 102 | else |
| 103 | return inb(hwif->io_ports.ctl_addr); |
| 104 | } |
| 105 | |
Bartlomiej Zolnierkiewicz | b2f951a | 2008-07-23 19:55:50 +0200 | [diff] [blame] | 106 | static u8 ide_read_sff_dma_status(ide_hwif_t *hwif) |
| 107 | { |
| 108 | if (hwif->host_flags & IDE_HFLAG_MMIO) |
Bartlomiej Zolnierkiewicz | cab7f8e | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 109 | return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS)); |
Bartlomiej Zolnierkiewicz | b2f951a | 2008-07-23 19:55:50 +0200 | [diff] [blame] | 110 | else |
Bartlomiej Zolnierkiewicz | cab7f8e | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 111 | return inb(hwif->dma_base + ATA_DMA_STATUS); |
Bartlomiej Zolnierkiewicz | b2f951a | 2008-07-23 19:55:50 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 114 | static void ide_set_irq(ide_hwif_t *hwif, int on) |
| 115 | { |
| 116 | u8 ctl = ATA_DEVCTL_OBS; |
| 117 | |
| 118 | if (on == 4) { /* hack for SRST */ |
| 119 | ctl |= 4; |
| 120 | on &= ~4; |
| 121 | } |
| 122 | |
| 123 | ctl |= on ? 0 : 2; |
| 124 | |
| 125 | if (hwif->host_flags & IDE_HFLAG_MMIO) |
| 126 | writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr); |
| 127 | else |
| 128 | outb(ctl, hwif->io_ports.ctl_addr); |
| 129 | } |
| 130 | |
Bartlomiej Zolnierkiewicz | 94cd5b6 | 2008-04-28 23:44:40 +0200 | [diff] [blame] | 131 | static void ide_tf_load(ide_drive_t *drive, ide_task_t *task) |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 132 | { |
| 133 | ide_hwif_t *hwif = drive->hwif; |
| 134 | struct ide_io_ports *io_ports = &hwif->io_ports; |
| 135 | struct ide_taskfile *tf = &task->tf; |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 136 | void (*tf_outb)(u8 addr, unsigned long port); |
| 137 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 138 | u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF; |
| 139 | |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 140 | if (mmio) |
| 141 | tf_outb = ide_mm_outb; |
| 142 | else |
| 143 | tf_outb = ide_outb; |
| 144 | |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 145 | if (task->tf_flags & IDE_TFLAG_FLAGGED) |
| 146 | HIHI = 0xFF; |
| 147 | |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 148 | if (task->tf_flags & IDE_TFLAG_OUT_DATA) { |
| 149 | u16 data = (tf->hob_data << 8) | tf->data; |
| 150 | |
| 151 | if (mmio) |
| 152 | writew(data, (void __iomem *)io_ports->data_addr); |
| 153 | else |
| 154 | outw(data, io_ports->data_addr); |
| 155 | } |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 156 | |
| 157 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 158 | tf_outb(tf->hob_feature, io_ports->feature_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 159 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 160 | tf_outb(tf->hob_nsect, io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 161 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 162 | tf_outb(tf->hob_lbal, io_ports->lbal_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 163 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 164 | tf_outb(tf->hob_lbam, io_ports->lbam_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 165 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 166 | tf_outb(tf->hob_lbah, io_ports->lbah_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 167 | |
| 168 | if (task->tf_flags & IDE_TFLAG_OUT_FEATURE) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 169 | tf_outb(tf->feature, io_ports->feature_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 170 | if (task->tf_flags & IDE_TFLAG_OUT_NSECT) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 171 | tf_outb(tf->nsect, io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 172 | if (task->tf_flags & IDE_TFLAG_OUT_LBAL) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 173 | tf_outb(tf->lbal, io_ports->lbal_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 174 | if (task->tf_flags & IDE_TFLAG_OUT_LBAM) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 175 | tf_outb(tf->lbam, io_ports->lbam_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 176 | if (task->tf_flags & IDE_TFLAG_OUT_LBAH) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 177 | tf_outb(tf->lbah, io_ports->lbah_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 178 | |
| 179 | if (task->tf_flags & IDE_TFLAG_OUT_DEVICE) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 180 | tf_outb((tf->device & HIHI) | drive->select.all, |
| 181 | io_ports->device_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Bartlomiej Zolnierkiewicz | 94cd5b6 | 2008-04-28 23:44:40 +0200 | [diff] [blame] | 184 | static void ide_tf_read(ide_drive_t *drive, ide_task_t *task) |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 185 | { |
| 186 | ide_hwif_t *hwif = drive->hwif; |
| 187 | struct ide_io_ports *io_ports = &hwif->io_ports; |
| 188 | struct ide_taskfile *tf = &task->tf; |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 189 | void (*tf_outb)(u8 addr, unsigned long port); |
| 190 | u8 (*tf_inb)(unsigned long port); |
| 191 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
| 192 | |
| 193 | if (mmio) { |
| 194 | tf_outb = ide_mm_outb; |
| 195 | tf_inb = ide_mm_inb; |
| 196 | } else { |
| 197 | tf_outb = ide_outb; |
| 198 | tf_inb = ide_inb; |
| 199 | } |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 200 | |
| 201 | if (task->tf_flags & IDE_TFLAG_IN_DATA) { |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 202 | u16 data; |
| 203 | |
| 204 | if (mmio) |
| 205 | data = readw((void __iomem *)io_ports->data_addr); |
| 206 | else |
| 207 | data = inw(io_ports->data_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 208 | |
| 209 | tf->data = data & 0xff; |
| 210 | tf->hob_data = (data >> 8) & 0xff; |
| 211 | } |
| 212 | |
| 213 | /* be sure we're looking at the low order bits */ |
Bartlomiej Zolnierkiewicz | ff07488 | 2008-07-15 21:21:50 +0200 | [diff] [blame] | 214 | tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 215 | |
Bartlomiej Zolnierkiewicz | 92eb438 | 2008-07-23 19:55:53 +0200 | [diff] [blame] | 216 | if (task->tf_flags & IDE_TFLAG_IN_FEATURE) |
| 217 | tf->feature = tf_inb(io_ports->feature_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 218 | if (task->tf_flags & IDE_TFLAG_IN_NSECT) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 219 | tf->nsect = tf_inb(io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 220 | if (task->tf_flags & IDE_TFLAG_IN_LBAL) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 221 | tf->lbal = tf_inb(io_ports->lbal_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 222 | if (task->tf_flags & IDE_TFLAG_IN_LBAM) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 223 | tf->lbam = tf_inb(io_ports->lbam_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 224 | if (task->tf_flags & IDE_TFLAG_IN_LBAH) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 225 | tf->lbah = tf_inb(io_ports->lbah_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 226 | if (task->tf_flags & IDE_TFLAG_IN_DEVICE) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 227 | tf->device = tf_inb(io_ports->device_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 228 | |
| 229 | if (task->tf_flags & IDE_TFLAG_LBA48) { |
Bartlomiej Zolnierkiewicz | ff07488 | 2008-07-15 21:21:50 +0200 | [diff] [blame] | 230 | tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 231 | |
| 232 | if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 233 | tf->hob_feature = tf_inb(io_ports->feature_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 234 | if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 235 | tf->hob_nsect = tf_inb(io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 236 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 237 | tf->hob_lbal = tf_inb(io_ports->lbal_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 238 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 239 | tf->hob_lbam = tf_inb(io_ports->lbam_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 240 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH) |
Bartlomiej Zolnierkiewicz | ca545c1 | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 241 | tf->hob_lbah = tf_inb(io_ports->lbah_addr); |
Bartlomiej Zolnierkiewicz | d309e0b | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* |
| 246 | * Some localbus EIDE interfaces require a special access sequence |
| 247 | * when using 32-bit I/O instructions to transfer data. We call this |
| 248 | * the "vlb_sync" sequence, which consists of three successive reads |
| 249 | * of the sector count register location, with interrupts disabled |
| 250 | * to ensure that the reads all happen together. |
| 251 | */ |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 252 | static void ata_vlb_sync(unsigned long port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 254 | (void)inb(port); |
| 255 | (void)inb(port); |
| 256 | (void)inb(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * This is used for most PIO data transfers *from* the IDE interface |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 261 | * |
| 262 | * These routines will round up any request for an odd number of bytes, |
| 263 | * so if an odd len is specified, be sure that there's at least one |
| 264 | * extra byte allocated for the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 266 | static void ata_input_data(ide_drive_t *drive, struct request *rq, |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 267 | void *buf, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 269 | ide_hwif_t *hwif = drive->hwif; |
| 270 | struct ide_io_ports *io_ports = &hwif->io_ports; |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 271 | unsigned long data_addr = io_ports->data_addr; |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 272 | u8 io_32bit = drive->io_32bit; |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 273 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 275 | len++; |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (io_32bit) { |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 278 | unsigned long uninitialized_var(flags); |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 279 | |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 280 | if ((io_32bit & 2) && !mmio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | local_irq_save(flags); |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 282 | ata_vlb_sync(io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 283 | } |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 284 | |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 285 | if (mmio) |
| 286 | __ide_mm_insl((void __iomem *)data_addr, buf, len / 4); |
| 287 | else |
| 288 | insl(data_addr, buf, len / 4); |
| 289 | |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 290 | if ((io_32bit & 2) && !mmio) |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 291 | local_irq_restore(flags); |
| 292 | |
| 293 | if ((len & 3) >= 2) { |
| 294 | if (mmio) |
| 295 | __ide_mm_insw((void __iomem *)data_addr, |
| 296 | (u8 *)buf + (len & ~3), 1); |
| 297 | else |
| 298 | insw(data_addr, (u8 *)buf + (len & ~3), 1); |
| 299 | } |
| 300 | } else { |
| 301 | if (mmio) |
| 302 | __ide_mm_insw((void __iomem *)data_addr, buf, len / 2); |
| 303 | else |
| 304 | insw(data_addr, buf, len / 2); |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * This is used for most PIO data transfers *to* the IDE interface |
| 310 | */ |
Bartlomiej Zolnierkiewicz | 92d3ab2 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 311 | static void ata_output_data(ide_drive_t *drive, struct request *rq, |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 312 | void *buf, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 314 | ide_hwif_t *hwif = drive->hwif; |
| 315 | struct ide_io_ports *io_ports = &hwif->io_ports; |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 316 | unsigned long data_addr = io_ports->data_addr; |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 317 | u8 io_32bit = drive->io_32bit; |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 318 | u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | if (io_32bit) { |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 321 | unsigned long uninitialized_var(flags); |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 322 | |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 323 | if ((io_32bit & 2) && !mmio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | local_irq_save(flags); |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 325 | ata_vlb_sync(io_ports->nsect_addr); |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 326 | } |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 327 | |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 328 | if (mmio) |
| 329 | __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4); |
| 330 | else |
| 331 | outsl(data_addr, buf, len / 4); |
| 332 | |
Bartlomiej Zolnierkiewicz | 22cdd6c | 2008-04-28 23:44:41 +0200 | [diff] [blame] | 333 | if ((io_32bit & 2) && !mmio) |
Bartlomiej Zolnierkiewicz | 16bb69c | 2008-04-28 23:44:37 +0200 | [diff] [blame] | 334 | local_irq_restore(flags); |
| 335 | |
| 336 | if ((len & 3) >= 2) { |
| 337 | if (mmio) |
| 338 | __ide_mm_outsw((void __iomem *)data_addr, |
| 339 | (u8 *)buf + (len & ~3), 1); |
| 340 | else |
| 341 | outsw(data_addr, (u8 *)buf + (len & ~3), 1); |
| 342 | } |
| 343 | } else { |
| 344 | if (mmio) |
| 345 | __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2); |
| 346 | else |
| 347 | outsw(data_addr, buf, len / 2); |
| 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void default_hwif_transport(ide_hwif_t *hwif) |
| 352 | { |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 353 | hwif->exec_command = ide_exec_command; |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 354 | hwif->read_status = ide_read_status; |
Bartlomiej Zolnierkiewicz | 1f6d8a0 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 355 | hwif->read_altstatus = ide_read_altstatus; |
Bartlomiej Zolnierkiewicz | b2f951a | 2008-07-23 19:55:50 +0200 | [diff] [blame] | 356 | hwif->read_sff_dma_status = ide_read_sff_dma_status; |
| 357 | |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 358 | hwif->set_irq = ide_set_irq; |
| 359 | |
Bartlomiej Zolnierkiewicz | 94cd5b6 | 2008-04-28 23:44:40 +0200 | [diff] [blame] | 360 | hwif->tf_load = ide_tf_load; |
| 361 | hwif->tf_read = ide_tf_read; |
| 362 | |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 363 | hwif->input_data = ata_input_data; |
| 364 | hwif->output_data = ata_output_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Bartlomiej Zolnierkiewicz | 92eb438 | 2008-07-23 19:55:53 +0200 | [diff] [blame] | 367 | u8 ide_read_error(ide_drive_t *drive) |
| 368 | { |
| 369 | ide_task_t task; |
| 370 | |
| 371 | memset(&task, 0, sizeof(task)); |
| 372 | task.tf_flags = IDE_TFLAG_IN_FEATURE; |
| 373 | |
| 374 | drive->hwif->tf_read(drive, &task); |
| 375 | |
| 376 | return task.tf.error; |
| 377 | } |
| 378 | EXPORT_SYMBOL_GPL(ide_read_error); |
| 379 | |
Bartlomiej Zolnierkiewicz | 1823649 | 2008-07-23 19:55:54 +0200 | [diff] [blame] | 380 | void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason) |
| 381 | { |
| 382 | ide_task_t task; |
| 383 | |
| 384 | memset(&task, 0, sizeof(task)); |
| 385 | task.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM | |
| 386 | IDE_TFLAG_IN_NSECT; |
| 387 | |
| 388 | drive->hwif->tf_read(drive, &task); |
| 389 | |
| 390 | *bcount = (task.tf.lbah << 8) | task.tf.lbam; |
| 391 | *ireason = task.tf.nsect & 3; |
| 392 | } |
| 393 | EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason); |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | void ide_fix_driveid (struct hd_driveid *id) |
| 396 | { |
| 397 | #ifndef __LITTLE_ENDIAN |
| 398 | # ifdef __BIG_ENDIAN |
| 399 | int i; |
| 400 | u16 *stringcast; |
| 401 | |
| 402 | id->config = __le16_to_cpu(id->config); |
| 403 | id->cyls = __le16_to_cpu(id->cyls); |
| 404 | id->reserved2 = __le16_to_cpu(id->reserved2); |
| 405 | id->heads = __le16_to_cpu(id->heads); |
| 406 | id->track_bytes = __le16_to_cpu(id->track_bytes); |
| 407 | id->sector_bytes = __le16_to_cpu(id->sector_bytes); |
| 408 | id->sectors = __le16_to_cpu(id->sectors); |
| 409 | id->vendor0 = __le16_to_cpu(id->vendor0); |
| 410 | id->vendor1 = __le16_to_cpu(id->vendor1); |
| 411 | id->vendor2 = __le16_to_cpu(id->vendor2); |
| 412 | stringcast = (u16 *)&id->serial_no[0]; |
| 413 | for (i = 0; i < (20/2); i++) |
| 414 | stringcast[i] = __le16_to_cpu(stringcast[i]); |
| 415 | id->buf_type = __le16_to_cpu(id->buf_type); |
| 416 | id->buf_size = __le16_to_cpu(id->buf_size); |
| 417 | id->ecc_bytes = __le16_to_cpu(id->ecc_bytes); |
| 418 | stringcast = (u16 *)&id->fw_rev[0]; |
| 419 | for (i = 0; i < (8/2); i++) |
| 420 | stringcast[i] = __le16_to_cpu(stringcast[i]); |
| 421 | stringcast = (u16 *)&id->model[0]; |
| 422 | for (i = 0; i < (40/2); i++) |
| 423 | stringcast[i] = __le16_to_cpu(stringcast[i]); |
| 424 | id->dword_io = __le16_to_cpu(id->dword_io); |
| 425 | id->reserved50 = __le16_to_cpu(id->reserved50); |
| 426 | id->field_valid = __le16_to_cpu(id->field_valid); |
| 427 | id->cur_cyls = __le16_to_cpu(id->cur_cyls); |
| 428 | id->cur_heads = __le16_to_cpu(id->cur_heads); |
| 429 | id->cur_sectors = __le16_to_cpu(id->cur_sectors); |
| 430 | id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0); |
| 431 | id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1); |
| 432 | id->lba_capacity = __le32_to_cpu(id->lba_capacity); |
| 433 | id->dma_1word = __le16_to_cpu(id->dma_1word); |
| 434 | id->dma_mword = __le16_to_cpu(id->dma_mword); |
| 435 | id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes); |
| 436 | id->eide_dma_min = __le16_to_cpu(id->eide_dma_min); |
| 437 | id->eide_dma_time = __le16_to_cpu(id->eide_dma_time); |
| 438 | id->eide_pio = __le16_to_cpu(id->eide_pio); |
| 439 | id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy); |
| 440 | for (i = 0; i < 2; ++i) |
| 441 | id->words69_70[i] = __le16_to_cpu(id->words69_70[i]); |
| 442 | for (i = 0; i < 4; ++i) |
| 443 | id->words71_74[i] = __le16_to_cpu(id->words71_74[i]); |
| 444 | id->queue_depth = __le16_to_cpu(id->queue_depth); |
| 445 | for (i = 0; i < 4; ++i) |
| 446 | id->words76_79[i] = __le16_to_cpu(id->words76_79[i]); |
| 447 | id->major_rev_num = __le16_to_cpu(id->major_rev_num); |
| 448 | id->minor_rev_num = __le16_to_cpu(id->minor_rev_num); |
| 449 | id->command_set_1 = __le16_to_cpu(id->command_set_1); |
| 450 | id->command_set_2 = __le16_to_cpu(id->command_set_2); |
| 451 | id->cfsse = __le16_to_cpu(id->cfsse); |
| 452 | id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1); |
| 453 | id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2); |
| 454 | id->csf_default = __le16_to_cpu(id->csf_default); |
| 455 | id->dma_ultra = __le16_to_cpu(id->dma_ultra); |
| 456 | id->trseuc = __le16_to_cpu(id->trseuc); |
| 457 | id->trsEuc = __le16_to_cpu(id->trsEuc); |
| 458 | id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues); |
| 459 | id->mprc = __le16_to_cpu(id->mprc); |
| 460 | id->hw_config = __le16_to_cpu(id->hw_config); |
| 461 | id->acoustic = __le16_to_cpu(id->acoustic); |
| 462 | id->msrqs = __le16_to_cpu(id->msrqs); |
| 463 | id->sxfert = __le16_to_cpu(id->sxfert); |
| 464 | id->sal = __le16_to_cpu(id->sal); |
| 465 | id->spg = __le32_to_cpu(id->spg); |
| 466 | id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2); |
| 467 | for (i = 0; i < 22; i++) |
| 468 | id->words104_125[i] = __le16_to_cpu(id->words104_125[i]); |
| 469 | id->last_lun = __le16_to_cpu(id->last_lun); |
| 470 | id->word127 = __le16_to_cpu(id->word127); |
| 471 | id->dlf = __le16_to_cpu(id->dlf); |
| 472 | id->csfo = __le16_to_cpu(id->csfo); |
| 473 | for (i = 0; i < 26; i++) |
| 474 | id->words130_155[i] = __le16_to_cpu(id->words130_155[i]); |
| 475 | id->word156 = __le16_to_cpu(id->word156); |
| 476 | for (i = 0; i < 3; i++) |
| 477 | id->words157_159[i] = __le16_to_cpu(id->words157_159[i]); |
| 478 | id->cfa_power = __le16_to_cpu(id->cfa_power); |
| 479 | for (i = 0; i < 14; i++) |
| 480 | id->words161_175[i] = __le16_to_cpu(id->words161_175[i]); |
| 481 | for (i = 0; i < 31; i++) |
| 482 | id->words176_205[i] = __le16_to_cpu(id->words176_205[i]); |
| 483 | for (i = 0; i < 48; i++) |
| 484 | id->words206_254[i] = __le16_to_cpu(id->words206_254[i]); |
| 485 | id->integrity_word = __le16_to_cpu(id->integrity_word); |
| 486 | # else |
| 487 | # error "Please fix <asm/byteorder.h>" |
| 488 | # endif |
| 489 | #endif |
| 490 | } |
| 491 | |
Bartlomiej Zolnierkiewicz | 0174511 | 2007-11-05 21:42:29 +0100 | [diff] [blame] | 492 | /* |
| 493 | * ide_fixstring() cleans up and (optionally) byte-swaps a text string, |
| 494 | * removing leading/trailing blanks and compressing internal blanks. |
| 495 | * It is primarily used to tidy up the model name/number fields as |
| 496 | * returned by the WIN_[P]IDENTIFY commands. |
| 497 | */ |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | void ide_fixstring (u8 *s, const int bytecount, const int byteswap) |
| 500 | { |
| 501 | u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */ |
| 502 | |
| 503 | if (byteswap) { |
| 504 | /* convert from big-endian to host byte order */ |
| 505 | for (p = end ; p != s;) { |
| 506 | unsigned short *pp = (unsigned short *) (p -= 2); |
| 507 | *pp = ntohs(*pp); |
| 508 | } |
| 509 | } |
| 510 | /* strip leading blanks */ |
| 511 | while (s != end && *s == ' ') |
| 512 | ++s; |
| 513 | /* compress internal blanks and strip trailing blanks */ |
| 514 | while (s != end && *s) { |
| 515 | if (*s++ != ' ' || (s != end && *s && *s != ' ')) |
| 516 | *p++ = *(s-1); |
| 517 | } |
| 518 | /* wipe out trailing garbage */ |
| 519 | while (p != end) |
| 520 | *p++ = '\0'; |
| 521 | } |
| 522 | |
| 523 | EXPORT_SYMBOL(ide_fixstring); |
| 524 | |
| 525 | /* |
| 526 | * Needed for PCI irq sharing |
| 527 | */ |
| 528 | int drive_is_ready (ide_drive_t *drive) |
| 529 | { |
| 530 | ide_hwif_t *hwif = HWIF(drive); |
| 531 | u8 stat = 0; |
| 532 | |
| 533 | if (drive->waiting_for_dma) |
Bartlomiej Zolnierkiewicz | 5e37bdc | 2008-04-26 22:25:24 +0200 | [diff] [blame] | 534 | return hwif->dma_ops->dma_test_irq(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | #if 0 |
| 537 | /* need to guarantee 400ns since last command was issued */ |
| 538 | udelay(1); |
| 539 | #endif |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* |
| 542 | * We do a passive status test under shared PCI interrupts on |
| 543 | * cards that truly share the ATA side interrupt, but may also share |
| 544 | * an interrupt with another pci card/device. We make no assumptions |
| 545 | * about possible isa-pnp and pci-pnp issues yet. |
| 546 | */ |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 547 | if (hwif->io_ports.ctl_addr) |
Bartlomiej Zolnierkiewicz | 1f6d8a0 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 548 | stat = hwif->read_altstatus(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* Note: this may clear a pending IRQ!! */ |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 551 | stat = hwif->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | if (stat & BUSY_STAT) |
| 554 | /* drive busy: definitely not interrupting */ |
| 555 | return 0; |
| 556 | |
| 557 | /* drive ready: *might* be interrupting */ |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | EXPORT_SYMBOL(drive_is_ready); |
| 562 | |
| 563 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | * This routine busy-waits for the drive status to be not "busy". |
| 565 | * It then checks the status for all of the "good" bits and none |
| 566 | * of the "bad" bits, and if all is okay it returns 0. All other |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 567 | * cases return error -- caller may then invoke ide_error(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * |
| 569 | * This routine should get fixed to not hog the cpu during extra long waits.. |
| 570 | * That could be done by busy-waiting for the first jiffy or two, and then |
| 571 | * setting a timer to wake up at half second intervals thereafter, |
| 572 | * until timeout is achieved, before timing out. |
| 573 | */ |
Bartlomiej Zolnierkiewicz | aedea59 | 2007-10-13 17:47:50 +0200 | [diff] [blame] | 574 | static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 576 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 578 | int i; |
| 579 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | udelay(1); /* spec allows drive 400ns to assert "BUSY" */ |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 582 | stat = hwif->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 583 | |
| 584 | if (stat & BUSY_STAT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | local_irq_set(flags); |
| 586 | timeout += jiffies; |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 587 | while ((stat = hwif->read_status(hwif)) & BUSY_STAT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (time_after(jiffies, timeout)) { |
| 589 | /* |
| 590 | * One last read after the timeout in case |
| 591 | * heavy interrupt load made us not make any |
| 592 | * progress during the timeout.. |
| 593 | */ |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 594 | stat = hwif->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (!(stat & BUSY_STAT)) |
| 596 | break; |
| 597 | |
| 598 | local_irq_restore(flags); |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 599 | *rstat = stat; |
| 600 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | local_irq_restore(flags); |
| 604 | } |
| 605 | /* |
| 606 | * Allow status to settle, then read it again. |
| 607 | * A few rare drives vastly violate the 400ns spec here, |
| 608 | * so we'll wait up to 10usec for a "good" status |
| 609 | * rather than expensively fail things immediately. |
| 610 | * This fix courtesy of Matthew Faupel & Niccolo Rigacci. |
| 611 | */ |
| 612 | for (i = 0; i < 10; i++) { |
| 613 | udelay(1); |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 614 | stat = hwif->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 615 | |
| 616 | if (OK_STAT(stat, good, bad)) { |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 617 | *rstat = stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return 0; |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 621 | *rstat = stat; |
| 622 | return -EFAULT; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * In case of error returns error value after doing "*startstop = ide_error()". |
| 627 | * The caller should return the updated value of "startstop" in this case, |
| 628 | * "startstop" is unchanged when the function returns 0. |
| 629 | */ |
| 630 | int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout) |
| 631 | { |
| 632 | int err; |
| 633 | u8 stat; |
| 634 | |
| 635 | /* bail early if we've exceeded max_failures */ |
| 636 | if (drive->max_failures && (drive->failures > drive->max_failures)) { |
| 637 | *startstop = ide_stopped; |
| 638 | return 1; |
| 639 | } |
| 640 | |
| 641 | err = __ide_wait_stat(drive, good, bad, timeout, &stat); |
| 642 | |
| 643 | if (err) { |
| 644 | char *s = (err == -EBUSY) ? "status timeout" : "status error"; |
| 645 | *startstop = ide_error(drive, s, stat); |
| 646 | } |
| 647 | |
| 648 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | EXPORT_SYMBOL(ide_wait_stat); |
| 652 | |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 653 | /** |
| 654 | * ide_in_drive_list - look for drive in black/white list |
| 655 | * @id: drive identifier |
| 656 | * @drive_table: list to inspect |
| 657 | * |
| 658 | * Look for a drive in the blacklist and the whitelist tables |
| 659 | * Returns 1 if the drive is found in the table. |
| 660 | */ |
| 661 | |
| 662 | int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table) |
| 663 | { |
| 664 | for ( ; drive_table->id_model; drive_table++) |
| 665 | if ((!strcmp(drive_table->id_model, id->model)) && |
| 666 | (!drive_table->id_firmware || |
| 667 | strstr(id->fw_rev, drive_table->id_firmware))) |
| 668 | return 1; |
| 669 | return 0; |
| 670 | } |
| 671 | |
Bartlomiej Zolnierkiewicz | b0244a0 | 2007-08-20 22:42:57 +0200 | [diff] [blame] | 672 | EXPORT_SYMBOL_GPL(ide_in_drive_list); |
| 673 | |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 674 | /* |
| 675 | * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid. |
| 676 | * We list them here and depend on the device side cable detection for them. |
Bartlomiej Zolnierkiewicz | 8588a2b7 | 2007-10-26 20:31:16 +0200 | [diff] [blame] | 677 | * |
| 678 | * Some optical devices with the buggy firmwares have the same problem. |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 679 | */ |
| 680 | static const struct drive_list_entry ivb_list[] = { |
| 681 | { "QUANTUM FIREBALLlct10 05" , "A03.0900" }, |
Bartlomiej Zolnierkiewicz | 8588a2b7 | 2007-10-26 20:31:16 +0200 | [diff] [blame] | 682 | { "TSSTcorp CDDVDW SH-S202J" , "SB00" }, |
Peter Missel | e97564f | 2007-11-27 21:35:57 +0100 | [diff] [blame] | 683 | { "TSSTcorp CDDVDW SH-S202J" , "SB01" }, |
| 684 | { "TSSTcorp CDDVDW SH-S202N" , "SB00" }, |
| 685 | { "TSSTcorp CDDVDW SH-S202N" , "SB01" }, |
Alexander Smal | 3ced5c4 | 2008-04-28 23:44:43 +0200 | [diff] [blame] | 686 | { "TSSTcorp CDDVDW SH-S202H" , "SB00" }, |
| 687 | { "TSSTcorp CDDVDW SH-S202H" , "SB01" }, |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 688 | { NULL , NULL } |
| 689 | }; |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* |
| 692 | * All hosts that use the 80c ribbon must use! |
| 693 | * The name is derived from upper byte of word 93 and the 80c ribbon. |
| 694 | */ |
| 695 | u8 eighty_ninty_three (ide_drive_t *drive) |
| 696 | { |
Bartlomiej Zolnierkiewicz | 7f8f48a | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 697 | ide_hwif_t *hwif = drive->hwif; |
| 698 | struct hd_driveid *id = drive->id; |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 699 | int ivb = ide_in_drive_list(id, ivb_list); |
Bartlomiej Zolnierkiewicz | 7f8f48a | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 700 | |
Bartlomiej Zolnierkiewicz | 49521f9 | 2007-07-09 23:17:58 +0200 | [diff] [blame] | 701 | if (hwif->cbl == ATA_CBL_PATA40_SHORT) |
| 702 | return 1; |
| 703 | |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 704 | if (ivb) |
| 705 | printk(KERN_DEBUG "%s: skipping word 93 validity check\n", |
| 706 | drive->name); |
| 707 | |
George Kibardin | b98f880 | 2008-01-10 23:03:42 +0100 | [diff] [blame] | 708 | if (ide_dev_is_sata(id) && !ivb) |
| 709 | return 1; |
| 710 | |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 711 | if (hwif->cbl != ATA_CBL_PATA80 && !ivb) |
Bartlomiej Zolnierkiewicz | 7f8f48a | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 712 | goto no_80w; |
Alan Cox | 1a1276e | 2006-06-28 04:26:58 -0700 | [diff] [blame] | 713 | |
Bartlomiej Zolnierkiewicz | f68d932 | 2007-03-26 23:03:18 +0200 | [diff] [blame] | 714 | /* |
| 715 | * FIXME: |
Bartlomiej Zolnierkiewicz | f367bed | 2008-03-29 19:48:21 +0100 | [diff] [blame] | 716 | * - change master/slave IDENTIFY order |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 717 | * - force bit13 (80c cable present) check also for !ivb devices |
Bartlomiej Zolnierkiewicz | f68d932 | 2007-03-26 23:03:18 +0200 | [diff] [blame] | 718 | * (unless the slave device is pre-ATA3) |
| 719 | */ |
Bartlomiej Zolnierkiewicz | a5b7e70 | 2007-08-20 22:42:56 +0200 | [diff] [blame] | 720 | if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000))) |
Bartlomiej Zolnierkiewicz | 7f8f48a | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 721 | return 1; |
| 722 | |
| 723 | no_80w: |
| 724 | if (drive->udma33_warned == 1) |
| 725 | return 0; |
| 726 | |
| 727 | printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, " |
| 728 | "limiting max speed to UDMA33\n", |
Bartlomiej Zolnierkiewicz | 49521f9 | 2007-07-09 23:17:58 +0200 | [diff] [blame] | 729 | drive->name, |
| 730 | hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host"); |
Bartlomiej Zolnierkiewicz | 7f8f48a | 2007-05-10 00:01:10 +0200 | [diff] [blame] | 731 | |
| 732 | drive->udma33_warned = 1; |
| 733 | |
| 734 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Bartlomiej Zolnierkiewicz | 8a45513 | 2007-10-20 00:32:36 +0200 | [diff] [blame] | 737 | int ide_driveid_update(ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
Bartlomiej Zolnierkiewicz | 8a45513 | 2007-10-20 00:32:36 +0200 | [diff] [blame] | 739 | ide_hwif_t *hwif = drive->hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | struct hd_driveid *id; |
Bartlomiej Zolnierkiewicz | 8a45513 | 2007-10-20 00:32:36 +0200 | [diff] [blame] | 741 | unsigned long timeout, flags; |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 742 | u8 stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | /* |
| 745 | * Re-read drive->id for possible DMA mode |
| 746 | * change (copied from ide-probe.c) |
| 747 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | SELECT_MASK(drive, 1); |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 750 | hwif->set_irq(hwif, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | msleep(50); |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 752 | hwif->exec_command(hwif, WIN_IDENTIFY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | timeout = jiffies + WAIT_WORSTCASE; |
| 754 | do { |
| 755 | if (time_after(jiffies, timeout)) { |
| 756 | SELECT_MASK(drive, 0); |
| 757 | return 0; /* drive timed-out */ |
| 758 | } |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | msleep(50); /* give drive a breather */ |
Bartlomiej Zolnierkiewicz | 1f6d8a0 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 761 | stat = hwif->read_altstatus(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 762 | } while (stat & BUSY_STAT); |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | msleep(50); /* wait for IRQ and DRQ_STAT */ |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 765 | stat = hwif->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 766 | |
| 767 | if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | SELECT_MASK(drive, 0); |
| 769 | printk("%s: CHECK for good STATUS\n", drive->name); |
| 770 | return 0; |
| 771 | } |
| 772 | local_irq_save(flags); |
| 773 | SELECT_MASK(drive, 0); |
| 774 | id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC); |
| 775 | if (!id) { |
| 776 | local_irq_restore(flags); |
| 777 | return 0; |
| 778 | } |
Bartlomiej Zolnierkiewicz | 9567b34 | 2008-04-28 23:44:36 +0200 | [diff] [blame] | 779 | hwif->input_data(drive, NULL, id, SECTOR_SIZE); |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 780 | (void)hwif->read_status(hwif); /* clear drive IRQ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | local_irq_enable(); |
| 782 | local_irq_restore(flags); |
| 783 | ide_fix_driveid(id); |
| 784 | if (id) { |
| 785 | drive->id->dma_ultra = id->dma_ultra; |
| 786 | drive->id->dma_mword = id->dma_mword; |
| 787 | drive->id->dma_1word = id->dma_1word; |
| 788 | /* anything more ? */ |
| 789 | kfree(id); |
Bartlomiej Zolnierkiewicz | 3ab7efe | 2007-12-12 23:31:58 +0100 | [diff] [blame] | 790 | |
| 791 | if (drive->using_dma && ide_id_dma_bug(drive)) |
| 792 | ide_dma_off(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 798 | int ide_config_drive_speed(ide_drive_t *drive, u8 speed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | { |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 800 | ide_hwif_t *hwif = drive->hwif; |
Sergei Shtylyov | 89613e6 | 2007-11-27 21:35:52 +0100 | [diff] [blame] | 801 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | u8 stat; |
Bartlomiej Zolnierkiewicz | 59be2c8 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 803 | ide_task_t task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | #ifdef CONFIG_BLK_DEV_IDEDMA |
Bartlomiej Zolnierkiewicz | 5e37bdc | 2008-04-26 22:25:24 +0200 | [diff] [blame] | 806 | if (hwif->dma_ops) /* check if host supports DMA */ |
| 807 | hwif->dma_ops->dma_host_set(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | #endif |
| 809 | |
Sergei Shtylyov | 89613e6 | 2007-11-27 21:35:52 +0100 | [diff] [blame] | 810 | /* Skip setting PIO flow-control modes on pre-EIDE drives */ |
| 811 | if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08)) |
| 812 | goto skip; |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | /* |
| 815 | * Don't use ide_wait_cmd here - it will |
| 816 | * attempt to set_geometry and recalibrate, |
| 817 | * but for some reason these don't work at |
| 818 | * this point (lost interrupt). |
| 819 | */ |
| 820 | /* |
| 821 | * Select the drive, and issue the SETFEATURES command |
| 822 | */ |
| 823 | disable_irq_nosync(hwif->irq); |
| 824 | |
| 825 | /* |
| 826 | * FIXME: we race against the running IRQ here if |
| 827 | * this is called from non IRQ context. If we use |
| 828 | * disable_irq() we hang on the error path. Work |
| 829 | * is needed. |
| 830 | */ |
| 831 | |
| 832 | udelay(1); |
| 833 | SELECT_DRIVE(drive); |
| 834 | SELECT_MASK(drive, 0); |
| 835 | udelay(1); |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 836 | hwif->set_irq(hwif, 0); |
Bartlomiej Zolnierkiewicz | 59be2c8 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 837 | |
| 838 | memset(&task, 0, sizeof(task)); |
| 839 | task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT; |
| 840 | task.tf.feature = SETFEATURES_XFER; |
| 841 | task.tf.nsect = speed; |
| 842 | |
| 843 | hwif->tf_load(drive, &task); |
| 844 | |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 845 | hwif->exec_command(hwif, WIN_SETFEATURES); |
Bartlomiej Zolnierkiewicz | 59be2c8 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 846 | |
Bartlomiej Zolnierkiewicz | 81ca691 | 2008-01-26 20:13:08 +0100 | [diff] [blame] | 847 | if (drive->quirk_list == 2) |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 848 | hwif->set_irq(hwif, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Bartlomiej Zolnierkiewicz | 74af21c | 2007-10-13 17:47:49 +0200 | [diff] [blame] | 850 | error = __ide_wait_stat(drive, drive->ready_stat, |
| 851 | BUSY_STAT|DRQ_STAT|ERR_STAT, |
| 852 | WAIT_CMD, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | SELECT_MASK(drive, 0); |
| 855 | |
| 856 | enable_irq(hwif->irq); |
| 857 | |
| 858 | if (error) { |
| 859 | (void) ide_dump_status(drive, "set_drive_speed_status", stat); |
| 860 | return error; |
| 861 | } |
| 862 | |
| 863 | drive->id->dma_ultra &= ~0xFF00; |
| 864 | drive->id->dma_mword &= ~0x0F00; |
| 865 | drive->id->dma_1word &= ~0x0F00; |
| 866 | |
Sergei Shtylyov | 89613e6 | 2007-11-27 21:35:52 +0100 | [diff] [blame] | 867 | skip: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | #ifdef CONFIG_BLK_DEV_IDEDMA |
Bartlomiej Zolnierkiewicz | f37aaf9 | 2008-01-26 20:13:02 +0100 | [diff] [blame] | 869 | if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) && |
| 870 | drive->using_dma) |
Bartlomiej Zolnierkiewicz | 5e37bdc | 2008-04-26 22:25:24 +0200 | [diff] [blame] | 871 | hwif->dma_ops->dma_host_set(drive, 1); |
| 872 | else if (hwif->dma_ops) /* check if host supports DMA */ |
Bartlomiej Zolnierkiewicz | 4a546e0 | 2008-01-26 20:13:01 +0100 | [diff] [blame] | 873 | ide_dma_off_quietly(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | #endif |
| 875 | |
| 876 | switch(speed) { |
| 877 | case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break; |
| 878 | case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break; |
| 879 | case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break; |
| 880 | case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break; |
| 881 | case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break; |
| 882 | case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break; |
| 883 | case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break; |
| 884 | case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break; |
| 885 | case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break; |
| 886 | case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break; |
| 887 | case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break; |
| 888 | case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break; |
| 889 | case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break; |
| 890 | case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break; |
| 891 | default: break; |
| 892 | } |
| 893 | if (!drive->init_speed) |
| 894 | drive->init_speed = speed; |
| 895 | drive->current_speed = speed; |
| 896 | return error; |
| 897 | } |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | /* |
| 900 | * This should get invoked any time we exit the driver to |
| 901 | * wait for an interrupt response from a drive. handler() points |
| 902 | * at the appropriate code to handle the next interrupt, and a |
| 903 | * timer is started to prevent us from waiting forever in case |
| 904 | * something goes wrong (see the ide_timer_expiry() handler later on). |
| 905 | * |
| 906 | * See also ide_execute_command |
| 907 | */ |
| 908 | static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, |
| 909 | unsigned int timeout, ide_expiry_t *expiry) |
| 910 | { |
| 911 | ide_hwgroup_t *hwgroup = HWGROUP(drive); |
| 912 | |
Sergei Shtylyov | d30a426 | 2008-02-11 00:32:12 +0100 | [diff] [blame] | 913 | BUG_ON(hwgroup->handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | hwgroup->handler = handler; |
| 915 | hwgroup->expiry = expiry; |
| 916 | hwgroup->timer.expires = jiffies + timeout; |
Sergei Shtylyov | d30a426 | 2008-02-11 00:32:12 +0100 | [diff] [blame] | 917 | hwgroup->req_gen_timer = hwgroup->req_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | add_timer(&hwgroup->timer); |
| 919 | } |
| 920 | |
| 921 | void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, |
| 922 | unsigned int timeout, ide_expiry_t *expiry) |
| 923 | { |
| 924 | unsigned long flags; |
| 925 | spin_lock_irqsave(&ide_lock, flags); |
| 926 | __ide_set_handler(drive, handler, timeout, expiry); |
| 927 | spin_unlock_irqrestore(&ide_lock, flags); |
| 928 | } |
| 929 | |
| 930 | EXPORT_SYMBOL(ide_set_handler); |
| 931 | |
| 932 | /** |
| 933 | * ide_execute_command - execute an IDE command |
| 934 | * @drive: IDE drive to issue the command against |
| 935 | * @command: command byte to write |
| 936 | * @handler: handler for next phase |
| 937 | * @timeout: timeout for command |
| 938 | * @expiry: handler to run on timeout |
| 939 | * |
| 940 | * Helper function to issue an IDE command. This handles the |
| 941 | * atomicity requirements, command timing and ensures that the |
| 942 | * handler and IRQ setup do not race. All IDE command kick off |
| 943 | * should go via this function or do equivalent locking. |
| 944 | */ |
Bartlomiej Zolnierkiewicz | cd2a2d9 | 2008-01-25 22:17:06 +0100 | [diff] [blame] | 945 | |
| 946 | void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler, |
| 947 | unsigned timeout, ide_expiry_t *expiry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
| 949 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | ide_hwif_t *hwif = HWIF(drive); |
Bartlomiej Zolnierkiewicz | 629f944 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | spin_lock_irqsave(&ide_lock, flags); |
Bartlomiej Zolnierkiewicz | 629f944 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 953 | __ide_set_handler(drive, handler, timeout, expiry); |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 954 | hwif->exec_command(hwif, cmd); |
Bartlomiej Zolnierkiewicz | 629f944 | 2008-02-02 19:56:46 +0100 | [diff] [blame] | 955 | /* |
| 956 | * Drive takes 400nS to respond, we must avoid the IRQ being |
| 957 | * serviced before that. |
| 958 | * |
| 959 | * FIXME: we could skip this delay with care on non shared devices |
| 960 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | ndelay(400); |
| 962 | spin_unlock_irqrestore(&ide_lock, flags); |
| 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | EXPORT_SYMBOL(ide_execute_command); |
| 965 | |
Bartlomiej Zolnierkiewicz | 1fc1425 | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 966 | void ide_execute_pkt_cmd(ide_drive_t *drive) |
| 967 | { |
| 968 | ide_hwif_t *hwif = drive->hwif; |
| 969 | unsigned long flags; |
| 970 | |
| 971 | spin_lock_irqsave(&ide_lock, flags); |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 972 | hwif->exec_command(hwif, WIN_PACKETCMD); |
Bartlomiej Zolnierkiewicz | 1fc1425 | 2008-04-28 23:44:39 +0200 | [diff] [blame] | 973 | ndelay(400); |
| 974 | spin_unlock_irqrestore(&ide_lock, flags); |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 978 | static inline void ide_complete_drive_reset(ide_drive_t *drive, int err) |
Elias Oltmanns | 79e36a9 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 979 | { |
| 980 | struct request *rq = drive->hwif->hwgroup->rq; |
| 981 | |
| 982 | if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET) |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 983 | ide_end_request(drive, err ? err : 1, 0); |
Elias Oltmanns | 79e36a9 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 984 | } |
| 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* needed below */ |
| 987 | static ide_startstop_t do_reset1 (ide_drive_t *, int); |
| 988 | |
| 989 | /* |
| 990 | * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms |
| 991 | * during an atapi drive reset operation. If the drive has not yet responded, |
| 992 | * and we have not yet hit our maximum waiting time, then the timer is restarted |
| 993 | * for another 50ms. |
| 994 | */ |
| 995 | static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive) |
| 996 | { |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 997 | ide_hwif_t *hwif = drive->hwif; |
| 998 | ide_hwgroup_t *hwgroup = hwif->hwgroup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | u8 stat; |
| 1000 | |
| 1001 | SELECT_DRIVE(drive); |
| 1002 | udelay (10); |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1003 | stat = hwif->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1005 | if (OK_STAT(stat, 0, BUSY_STAT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | printk("%s: ATAPI reset complete\n", drive->name); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1007 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (time_before(jiffies, hwgroup->poll_timeout)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL); |
| 1010 | /* continue polling */ |
| 1011 | return ide_started; |
| 1012 | } |
| 1013 | /* end of polling */ |
| 1014 | hwgroup->polling = 0; |
| 1015 | printk("%s: ATAPI reset timed-out, status=0x%02x\n", |
| 1016 | drive->name, stat); |
| 1017 | /* do it the old fashioned way */ |
| 1018 | return do_reset1(drive, 1); |
| 1019 | } |
| 1020 | /* done polling */ |
| 1021 | hwgroup->polling = 0; |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1022 | ide_complete_drive_reset(drive, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | return ide_stopped; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * reset_pollfunc() gets invoked to poll the interface for completion every 50ms |
| 1028 | * during an ide reset operation. If the drives have not yet responded, |
| 1029 | * and we have not yet hit our maximum waiting time, then the timer is restarted |
| 1030 | * for another 50ms. |
| 1031 | */ |
| 1032 | static ide_startstop_t reset_pollfunc (ide_drive_t *drive) |
| 1033 | { |
| 1034 | ide_hwgroup_t *hwgroup = HWGROUP(drive); |
| 1035 | ide_hwif_t *hwif = HWIF(drive); |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1036 | const struct ide_port_ops *port_ops = hwif->port_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | u8 tmp; |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1038 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1040 | if (port_ops && port_ops->reset_poll) { |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1041 | err = port_ops->reset_poll(drive); |
| 1042 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | printk(KERN_ERR "%s: host reset_poll failure for %s.\n", |
| 1044 | hwif->name, drive->name); |
Elias Oltmanns | 79e36a9 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1045 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1049 | tmp = hwif->read_status(hwif); |
Bartlomiej Zolnierkiewicz | c47137a | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1050 | |
| 1051 | if (!OK_STAT(tmp, 0, BUSY_STAT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (time_before(jiffies, hwgroup->poll_timeout)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL); |
| 1054 | /* continue polling */ |
| 1055 | return ide_started; |
| 1056 | } |
| 1057 | printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp); |
| 1058 | drive->failures++; |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1059 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | printk("%s: reset: ", hwif->name); |
Bartlomiej Zolnierkiewicz | 64a57fe | 2008-02-06 02:57:51 +0100 | [diff] [blame] | 1062 | tmp = ide_read_error(drive); |
| 1063 | |
| 1064 | if (tmp == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | printk("success\n"); |
| 1066 | drive->failures = 0; |
| 1067 | } else { |
| 1068 | drive->failures++; |
| 1069 | printk("master: "); |
| 1070 | switch (tmp & 0x7f) { |
| 1071 | case 1: printk("passed"); |
| 1072 | break; |
| 1073 | case 2: printk("formatter device error"); |
| 1074 | break; |
| 1075 | case 3: printk("sector buffer error"); |
| 1076 | break; |
| 1077 | case 4: printk("ECC circuitry error"); |
| 1078 | break; |
| 1079 | case 5: printk("controlling MPU error"); |
| 1080 | break; |
| 1081 | default:printk("error (0x%02x?)", tmp); |
| 1082 | } |
| 1083 | if (tmp & 0x80) |
| 1084 | printk("; slave: failed"); |
| 1085 | printk("\n"); |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1086 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
Elias Oltmanns | 79e36a9 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1089 | out: |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1090 | hwgroup->polling = 0; /* done polling */ |
| 1091 | ide_complete_drive_reset(drive, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | return ide_stopped; |
| 1093 | } |
| 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | static void ide_disk_pre_reset(ide_drive_t *drive) |
| 1096 | { |
| 1097 | int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1; |
| 1098 | |
| 1099 | drive->special.all = 0; |
| 1100 | drive->special.b.set_geometry = legacy; |
| 1101 | drive->special.b.recalibrate = legacy; |
Bartlomiej Zolnierkiewicz | 4ee06b7 | 2008-01-25 22:17:08 +0100 | [diff] [blame] | 1102 | drive->mult_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (!drive->keep_settings && !drive->using_dma) |
| 1104 | drive->mult_req = 0; |
| 1105 | if (drive->mult_req != drive->mult_count) |
| 1106 | drive->special.b.set_multmode = 1; |
| 1107 | } |
| 1108 | |
| 1109 | static void pre_reset(ide_drive_t *drive) |
| 1110 | { |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1111 | const struct ide_port_ops *port_ops = drive->hwif->port_ops; |
| 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (drive->media == ide_disk) |
| 1114 | ide_disk_pre_reset(drive); |
| 1115 | else |
| 1116 | drive->post_reset = 1; |
| 1117 | |
Bartlomiej Zolnierkiewicz | 99ffbe0 | 2008-02-02 19:56:47 +0100 | [diff] [blame] | 1118 | if (drive->using_dma) { |
| 1119 | if (drive->crc_count) |
Bartlomiej Zolnierkiewicz | 578cfa0 | 2008-02-02 19:56:47 +0100 | [diff] [blame] | 1120 | ide_check_dma_crc(drive); |
Bartlomiej Zolnierkiewicz | 99ffbe0 | 2008-02-02 19:56:47 +0100 | [diff] [blame] | 1121 | else |
| 1122 | ide_dma_off(drive); |
| 1123 | } |
| 1124 | |
| 1125 | if (!drive->keep_settings) { |
| 1126 | if (!drive->using_dma) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | drive->unmask = 0; |
| 1128 | drive->io_32bit = 0; |
| 1129 | } |
| 1130 | return; |
| 1131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1133 | if (port_ops && port_ops->pre_reset) |
| 1134 | port_ops->pre_reset(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Suleiman Souhlal | 513daad | 2007-03-26 23:03:20 +0200 | [diff] [blame] | 1136 | if (drive->current_speed != 0xff) |
| 1137 | drive->desired_speed = drive->current_speed; |
| 1138 | drive->current_speed = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * do_reset1() attempts to recover a confused drive by resetting it. |
| 1143 | * Unfortunately, resetting a disk drive actually resets all devices on |
| 1144 | * the same interface, so it can really be thought of as resetting the |
| 1145 | * interface rather than resetting the drive. |
| 1146 | * |
| 1147 | * ATAPI devices have their own reset mechanism which allows them to be |
| 1148 | * individually reset without clobbering other devices on the same interface. |
| 1149 | * |
| 1150 | * Unfortunately, the IDE interface does not generate an interrupt to let |
| 1151 | * us know when the reset operation has finished, so we must poll for this. |
| 1152 | * Equally poor, though, is the fact that this may a very long time to complete, |
| 1153 | * (up to 30 seconds worstcase). So, instead of busy-waiting here for it, |
| 1154 | * we set a timer to poll at 50ms intervals. |
| 1155 | */ |
| 1156 | static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi) |
| 1157 | { |
| 1158 | unsigned int unit; |
| 1159 | unsigned long flags; |
| 1160 | ide_hwif_t *hwif; |
| 1161 | ide_hwgroup_t *hwgroup; |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1162 | struct ide_io_ports *io_ports; |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1163 | const struct ide_port_ops *port_ops; |
Bartlomiej Zolnierkiewicz | 23579a2 | 2008-04-18 00:46:26 +0200 | [diff] [blame] | 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | spin_lock_irqsave(&ide_lock, flags); |
| 1166 | hwif = HWIF(drive); |
| 1167 | hwgroup = HWGROUP(drive); |
| 1168 | |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1169 | io_ports = &hwif->io_ports; |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* We must not reset with running handlers */ |
Eric Sesterhenn | 125e187 | 2006-06-23 02:06:06 -0700 | [diff] [blame] | 1172 | BUG_ON(hwgroup->handler != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
| 1174 | /* For an ATAPI device, first try an ATAPI SRST. */ |
| 1175 | if (drive->media != ide_disk && !do_not_try_atapi) { |
| 1176 | pre_reset(drive); |
| 1177 | SELECT_DRIVE(drive); |
| 1178 | udelay (20); |
Bartlomiej Zolnierkiewicz | c6dfa86 | 2008-07-23 19:55:51 +0200 | [diff] [blame] | 1179 | hwif->exec_command(hwif, WIN_SRST); |
Alan Cox | 68ad991 | 2005-06-27 15:24:25 -0700 | [diff] [blame] | 1180 | ndelay(400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE; |
| 1182 | hwgroup->polling = 1; |
| 1183 | __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL); |
| 1184 | spin_unlock_irqrestore(&ide_lock, flags); |
| 1185 | return ide_started; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * First, reset any device state data we were maintaining |
| 1190 | * for any of the drives on this interface. |
| 1191 | */ |
| 1192 | for (unit = 0; unit < MAX_DRIVES; ++unit) |
| 1193 | pre_reset(&hwif->drives[unit]); |
| 1194 | |
Bartlomiej Zolnierkiewicz | 4c3032d | 2008-04-27 15:38:32 +0200 | [diff] [blame] | 1195 | if (io_ports->ctl_addr == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | spin_unlock_irqrestore(&ide_lock, flags); |
Elias Oltmanns | 64a8f00 | 2008-07-16 20:33:48 +0200 | [diff] [blame] | 1197 | ide_complete_drive_reset(drive, -ENXIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | return ide_stopped; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * Note that we also set nIEN while resetting the device, |
| 1203 | * to mask unwanted interrupts from the interface during the reset. |
| 1204 | * However, due to the design of PC hardware, this will cause an |
| 1205 | * immediate interrupt due to the edge transition it produces. |
| 1206 | * This single interrupt gives us a "fast poll" for drives that |
| 1207 | * recover from reset very quickly, saving us the first 50ms wait time. |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1208 | * |
| 1209 | * TODO: add ->softreset method and stop abusing ->set_irq |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | */ |
| 1211 | /* set SRST and nIEN */ |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1212 | hwif->set_irq(hwif, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | /* more than enough time */ |
| 1214 | udelay(10); |
Bartlomiej Zolnierkiewicz | 6e6afb3 | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1215 | /* clear SRST, leave nIEN (unless device is on the quirk list) */ |
| 1216 | hwif->set_irq(hwif, drive->quirk_list == 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* more than enough time */ |
| 1218 | udelay(10); |
| 1219 | hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE; |
| 1220 | hwgroup->polling = 1; |
| 1221 | __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL); |
| 1222 | |
| 1223 | /* |
| 1224 | * Some weird controller like resetting themselves to a strange |
| 1225 | * state when the disks are reset this way. At least, the Winbond |
| 1226 | * 553 documentation says that |
| 1227 | */ |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 1228 | port_ops = hwif->port_ops; |
| 1229 | if (port_ops && port_ops->resetproc) |
| 1230 | port_ops->resetproc(drive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
| 1232 | spin_unlock_irqrestore(&ide_lock, flags); |
| 1233 | return ide_started; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * ide_do_reset() is the entry point to the drive/interface reset code. |
| 1238 | */ |
| 1239 | |
| 1240 | ide_startstop_t ide_do_reset (ide_drive_t *drive) |
| 1241 | { |
| 1242 | return do_reset1(drive, 0); |
| 1243 | } |
| 1244 | |
| 1245 | EXPORT_SYMBOL(ide_do_reset); |
| 1246 | |
| 1247 | /* |
| 1248 | * ide_wait_not_busy() waits for the currently selected device on the hwif |
Bartlomiej Zolnierkiewicz | 9d50152 | 2008-02-01 23:09:36 +0100 | [diff] [blame] | 1249 | * to report a non-busy status, see comments in ide_probe_port(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | */ |
| 1251 | int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout) |
| 1252 | { |
| 1253 | u8 stat = 0; |
| 1254 | |
| 1255 | while(timeout--) { |
| 1256 | /* |
| 1257 | * Turn this into a schedule() sleep once I'm sure |
| 1258 | * about locking issues (2.5 work ?). |
| 1259 | */ |
| 1260 | mdelay(1); |
Bartlomiej Zolnierkiewicz | b73c7ee | 2008-07-23 19:55:52 +0200 | [diff] [blame] | 1261 | stat = hwif->read_status(hwif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | if ((stat & BUSY_STAT) == 0) |
| 1263 | return 0; |
| 1264 | /* |
| 1265 | * Assume a value of 0xff means nothing is connected to |
| 1266 | * the interface and it doesn't implement the pull-down |
| 1267 | * resistor on D7. |
| 1268 | */ |
| 1269 | if (stat == 0xff) |
| 1270 | return -ENODEV; |
Ingo Molnar | 6842f8c | 2006-02-03 03:04:55 -0800 | [diff] [blame] | 1271 | touch_softlockup_watchdog(); |
Michal Schmidt | 1e86240 | 2006-07-30 03:03:29 -0700 | [diff] [blame] | 1272 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | return -EBUSY; |
| 1275 | } |
| 1276 | |
| 1277 | EXPORT_SYMBOL_GPL(ide_wait_not_busy); |
| 1278 | |