Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 1 | /* |
Dave Jones | f3a03b0 | 2007-07-16 11:23:03 -0400 | [diff] [blame] | 2 | * libata-sff.c - helper library for PCI IDE BMDMA |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 3 | * |
| 4 | * Maintained by: Jeff Garzik <jgarzik@pobox.com> |
| 5 | * Please ALWAYS copy linux-ide@vger.kernel.org |
| 6 | * on emails. |
| 7 | * |
| 8 | * Copyright 2003-2006 Red Hat, Inc. All rights reserved. |
| 9 | * Copyright 2003-2006 Jeff Garzik |
| 10 | * |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; see the file COPYING. If not, write to |
| 24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | * |
| 26 | * |
| 27 | * libata documentation is available via 'make {ps|pdf}docs', |
| 28 | * as Documentation/DocBook/libata.* |
| 29 | * |
| 30 | * Hardware documentation available from http://www.t13.org/ and |
| 31 | * http://www.sata-io.org/ |
| 32 | * |
| 33 | */ |
| 34 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 35 | #include <linux/kernel.h> |
| 36 | #include <linux/pci.h> |
| 37 | #include <linux/libata.h> |
| 38 | |
| 39 | #include "libata.h" |
| 40 | |
| 41 | /** |
Tejun Heo | 90088bb | 2006-10-09 11:10:26 +0900 | [diff] [blame] | 42 | * ata_irq_on - Enable interrupts on a port. |
| 43 | * @ap: Port on which interrupts are enabled. |
| 44 | * |
| 45 | * Enable interrupts on a legacy IDE device using MMIO or PIO, |
| 46 | * wait for idle, clear any pending interrupts. |
| 47 | * |
| 48 | * LOCKING: |
| 49 | * Inherited from caller. |
| 50 | */ |
| 51 | u8 ata_irq_on(struct ata_port *ap) |
| 52 | { |
| 53 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 54 | u8 tmp; |
| 55 | |
| 56 | ap->ctl &= ~ATA_NIEN; |
| 57 | ap->last_ctl = ap->ctl; |
| 58 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 59 | iowrite8(ap->ctl, ioaddr->ctl_addr); |
Tejun Heo | 90088bb | 2006-10-09 11:10:26 +0900 | [diff] [blame] | 60 | tmp = ata_wait_idle(ap); |
| 61 | |
| 62 | ap->ops->irq_clear(ap); |
| 63 | |
| 64 | return tmp; |
| 65 | } |
| 66 | |
| 67 | /** |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 68 | * ata_tf_load - send taskfile registers to host controller |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 69 | * @ap: Port to which output is sent |
| 70 | * @tf: ATA taskfile register set |
| 71 | * |
| 72 | * Outputs ATA taskfile to standard ATA host controller. |
| 73 | * |
| 74 | * LOCKING: |
| 75 | * Inherited from caller. |
| 76 | */ |
| 77 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 78 | void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) |
| 79 | { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 80 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 81 | unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; |
| 82 | |
| 83 | if (tf->ctl != ap->last_ctl) { |
| 84 | iowrite8(tf->ctl, ioaddr->ctl_addr); |
| 85 | ap->last_ctl = tf->ctl; |
| 86 | ata_wait_idle(ap); |
| 87 | } |
| 88 | |
| 89 | if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { |
| 90 | iowrite8(tf->hob_feature, ioaddr->feature_addr); |
| 91 | iowrite8(tf->hob_nsect, ioaddr->nsect_addr); |
| 92 | iowrite8(tf->hob_lbal, ioaddr->lbal_addr); |
| 93 | iowrite8(tf->hob_lbam, ioaddr->lbam_addr); |
| 94 | iowrite8(tf->hob_lbah, ioaddr->lbah_addr); |
| 95 | VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n", |
| 96 | tf->hob_feature, |
| 97 | tf->hob_nsect, |
| 98 | tf->hob_lbal, |
| 99 | tf->hob_lbam, |
| 100 | tf->hob_lbah); |
| 101 | } |
| 102 | |
| 103 | if (is_addr) { |
| 104 | iowrite8(tf->feature, ioaddr->feature_addr); |
| 105 | iowrite8(tf->nsect, ioaddr->nsect_addr); |
| 106 | iowrite8(tf->lbal, ioaddr->lbal_addr); |
| 107 | iowrite8(tf->lbam, ioaddr->lbam_addr); |
| 108 | iowrite8(tf->lbah, ioaddr->lbah_addr); |
| 109 | VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n", |
| 110 | tf->feature, |
| 111 | tf->nsect, |
| 112 | tf->lbal, |
| 113 | tf->lbam, |
| 114 | tf->lbah); |
| 115 | } |
| 116 | |
| 117 | if (tf->flags & ATA_TFLAG_DEVICE) { |
| 118 | iowrite8(tf->device, ioaddr->device_addr); |
| 119 | VPRINTK("device 0x%X\n", tf->device); |
| 120 | } |
| 121 | |
| 122 | ata_wait_idle(ap); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 126 | * ata_exec_command - issue ATA command to host controller |
| 127 | * @ap: port to which command is being issued |
| 128 | * @tf: ATA taskfile register set |
| 129 | * |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 130 | * Issues ATA command, with proper synchronization with interrupt |
| 131 | * handler / other threads. |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 132 | * |
| 133 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 134 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 135 | */ |
| 136 | void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf) |
| 137 | { |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 138 | DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command); |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 139 | |
| 140 | iowrite8(tf->command, ap->ioaddr.command_addr); |
| 141 | ata_pause(ap); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 145 | * ata_tf_read - input device's ATA taskfile shadow registers |
| 146 | * @ap: Port from which input is read |
| 147 | * @tf: ATA taskfile register set for storing input |
| 148 | * |
| 149 | * Reads ATA taskfile registers for currently-selected device |
Alan Cox | 76548ed | 2007-11-19 14:34:56 +0000 | [diff] [blame] | 150 | * into @tf. Assumes the device has a fully SFF compliant task file |
| 151 | * layout and behaviour. If you device does not (eg has a different |
| 152 | * status method) then you will need to provide a replacement tf_read |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 153 | * |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 154 | * LOCKING: |
| 155 | * Inherited from caller. |
| 156 | */ |
| 157 | void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf) |
| 158 | { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 159 | struct ata_ioports *ioaddr = &ap->ioaddr; |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 160 | |
Alan Cox | 76548ed | 2007-11-19 14:34:56 +0000 | [diff] [blame] | 161 | tf->command = ata_check_status(ap); |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 162 | tf->feature = ioread8(ioaddr->error_addr); |
| 163 | tf->nsect = ioread8(ioaddr->nsect_addr); |
| 164 | tf->lbal = ioread8(ioaddr->lbal_addr); |
| 165 | tf->lbam = ioread8(ioaddr->lbam_addr); |
| 166 | tf->lbah = ioread8(ioaddr->lbah_addr); |
| 167 | tf->device = ioread8(ioaddr->device_addr); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 168 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 169 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 170 | iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr); |
| 171 | tf->hob_feature = ioread8(ioaddr->error_addr); |
| 172 | tf->hob_nsect = ioread8(ioaddr->nsect_addr); |
| 173 | tf->hob_lbal = ioread8(ioaddr->lbal_addr); |
| 174 | tf->hob_lbam = ioread8(ioaddr->lbam_addr); |
| 175 | tf->hob_lbah = ioread8(ioaddr->lbah_addr); |
Petr Vandrovec | fe36cb5 | 2007-07-20 07:44:44 -0400 | [diff] [blame] | 176 | iowrite8(tf->ctl, ioaddr->ctl_addr); |
| 177 | ap->last_ctl = tf->ctl; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 178 | } |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 181 | /** |
| 182 | * ata_check_status - Read device status reg & clear interrupt |
| 183 | * @ap: port where the device is |
| 184 | * |
| 185 | * Reads ATA taskfile status register for currently-selected device |
| 186 | * and return its value. This also clears pending interrupts |
| 187 | * from this device |
| 188 | * |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 189 | * LOCKING: |
| 190 | * Inherited from caller. |
| 191 | */ |
| 192 | u8 ata_check_status(struct ata_port *ap) |
| 193 | { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 194 | return ioread8(ap->ioaddr.status_addr); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 197 | /** |
| 198 | * ata_altstatus - Read device alternate status reg |
| 199 | * @ap: port where the device is |
| 200 | * |
| 201 | * Reads ATA taskfile alternate status register for |
| 202 | * currently-selected device and return its value. |
| 203 | * |
| 204 | * Note: may NOT be used as the check_altstatus() entry in |
| 205 | * ata_port_operations. |
| 206 | * |
| 207 | * LOCKING: |
| 208 | * Inherited from caller. |
| 209 | */ |
| 210 | u8 ata_altstatus(struct ata_port *ap) |
| 211 | { |
| 212 | if (ap->ops->check_altstatus) |
| 213 | return ap->ops->check_altstatus(ap); |
| 214 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 215 | return ioread8(ap->ioaddr.altstatus_addr); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 218 | /** |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 219 | * ata_bmdma_setup - Set up PCI IDE BMDMA transaction |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 220 | * @qc: Info associated with this ATA transaction. |
| 221 | * |
| 222 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 223 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 224 | */ |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 225 | void ata_bmdma_setup(struct ata_queued_cmd *qc) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 226 | { |
| 227 | struct ata_port *ap = qc->ap; |
| 228 | unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); |
| 229 | u8 dmactl; |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 230 | |
| 231 | /* load PRD table addr. */ |
| 232 | mb(); /* make sure PRD table writes are visible to controller */ |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 233 | iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 234 | |
| 235 | /* specify data direction, triple-check start bit is clear */ |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 236 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 237 | dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); |
| 238 | if (!rw) |
| 239 | dmactl |= ATA_DMA_WR; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 240 | iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 241 | |
| 242 | /* issue r/w command */ |
| 243 | ap->ops->exec_command(ap, &qc->tf); |
| 244 | } |
| 245 | |
| 246 | /** |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 247 | * ata_bmdma_start - Start a PCI IDE BMDMA transaction |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 248 | * @qc: Info associated with this ATA transaction. |
| 249 | * |
| 250 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 251 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 252 | */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 253 | void ata_bmdma_start(struct ata_queued_cmd *qc) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 254 | { |
| 255 | struct ata_port *ap = qc->ap; |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 256 | u8 dmactl; |
| 257 | |
| 258 | /* start host DMA transaction */ |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 259 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
| 260 | iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 261 | |
Alan Cox | e1cc9de | 2007-09-20 15:03:07 +0100 | [diff] [blame] | 262 | /* Strictly, one may wish to issue an ioread8() here, to |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 263 | * flush the mmio write. However, control also passes |
| 264 | * to the hardware at this point, and it will interrupt |
| 265 | * us when we are to resume control. So, in effect, |
| 266 | * we don't care when the mmio write flushes. |
| 267 | * Further, a read of the DMA status register _immediately_ |
| 268 | * following the write may not be what certain flaky hardware |
| 269 | * is expected, so I think it is best to not add a readb() |
| 270 | * without first all the MMIO ATA cards/mobos. |
| 271 | * Or maybe I'm just being paranoid. |
Alan Cox | e1cc9de | 2007-09-20 15:03:07 +0100 | [diff] [blame] | 272 | * |
| 273 | * FIXME: The posting of this write means I/O starts are |
| 274 | * unneccessarily delayed for MMIO |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 275 | */ |
| 276 | } |
| 277 | |
| 278 | /** |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 279 | * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt. |
| 280 | * @ap: Port associated with this ATA transaction. |
| 281 | * |
| 282 | * Clear interrupt and error flags in DMA status register. |
| 283 | * |
| 284 | * May be used as the irq_clear() entry in ata_port_operations. |
| 285 | * |
| 286 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 287 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 288 | */ |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 289 | void ata_bmdma_irq_clear(struct ata_port *ap) |
| 290 | { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 291 | void __iomem *mmio = ap->ioaddr.bmdma_addr; |
| 292 | |
| 293 | if (!mmio) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 294 | return; |
| 295 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 296 | iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 299 | /** |
| 300 | * ata_bmdma_status - Read PCI IDE BMDMA status |
| 301 | * @ap: Port associated with this ATA transaction. |
| 302 | * |
| 303 | * Read and return BMDMA status register. |
| 304 | * |
| 305 | * May be used as the bmdma_status() entry in ata_port_operations. |
| 306 | * |
| 307 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 308 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 309 | */ |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 310 | u8 ata_bmdma_status(struct ata_port *ap) |
| 311 | { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 312 | return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 315 | /** |
| 316 | * ata_bmdma_stop - Stop PCI IDE BMDMA transfer |
| 317 | * @qc: Command we are ending DMA for |
| 318 | * |
| 319 | * Clears the ATA_DMA_START flag in the dma control register |
| 320 | * |
| 321 | * May be used as the bmdma_stop() entry in ata_port_operations. |
| 322 | * |
| 323 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 324 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 325 | */ |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 326 | void ata_bmdma_stop(struct ata_queued_cmd *qc) |
| 327 | { |
| 328 | struct ata_port *ap = qc->ap; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 329 | void __iomem *mmio = ap->ioaddr.bmdma_addr; |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 330 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 331 | /* clear start/stop bit */ |
| 332 | iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START, |
| 333 | mmio + ATA_DMA_CMD); |
Jeff Garzik | 2cc432e | 2006-03-23 00:32:03 -0500 | [diff] [blame] | 334 | |
| 335 | /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ |
| 336 | ata_altstatus(ap); /* dummy read */ |
| 337 | } |
| 338 | |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 339 | /** |
| 340 | * ata_bmdma_freeze - Freeze BMDMA controller port |
| 341 | * @ap: port to freeze |
| 342 | * |
| 343 | * Freeze BMDMA controller port. |
| 344 | * |
| 345 | * LOCKING: |
| 346 | * Inherited from caller. |
| 347 | */ |
| 348 | void ata_bmdma_freeze(struct ata_port *ap) |
| 349 | { |
| 350 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 351 | |
| 352 | ap->ctl |= ATA_NIEN; |
| 353 | ap->last_ctl = ap->ctl; |
| 354 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 355 | iowrite8(ap->ctl, ioaddr->ctl_addr); |
Tejun Heo | 0f0a3ad | 2006-11-17 12:24:22 +0900 | [diff] [blame] | 356 | |
| 357 | /* Under certain circumstances, some controllers raise IRQ on |
| 358 | * ATA_NIEN manipulation. Also, many controllers fail to mask |
| 359 | * previously pending IRQ on ATA_NIEN assertion. Clear it. |
| 360 | */ |
| 361 | ata_chk_status(ap); |
| 362 | |
| 363 | ap->ops->irq_clear(ap); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * ata_bmdma_thaw - Thaw BMDMA controller port |
| 368 | * @ap: port to thaw |
| 369 | * |
| 370 | * Thaw BMDMA controller port. |
| 371 | * |
| 372 | * LOCKING: |
| 373 | * Inherited from caller. |
| 374 | */ |
| 375 | void ata_bmdma_thaw(struct ata_port *ap) |
| 376 | { |
| 377 | /* clear & re-enable interrupts */ |
| 378 | ata_chk_status(ap); |
| 379 | ap->ops->irq_clear(ap); |
Akira Iguchi | 8362500 | 2007-01-26 16:27:32 +0900 | [diff] [blame] | 380 | ap->ops->irq_on(ap); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller |
| 385 | * @ap: port to handle error for |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 386 | * @prereset: prereset method (can be NULL) |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 387 | * @softreset: softreset method (can be NULL) |
| 388 | * @hardreset: hardreset method (can be NULL) |
| 389 | * @postreset: postreset method (can be NULL) |
| 390 | * |
| 391 | * Handle error for ATA BMDMA controller. It can handle both |
| 392 | * PATA and SATA controllers. Many controllers should be able to |
| 393 | * use this EH as-is or with some added handling before and |
| 394 | * after. |
| 395 | * |
| 396 | * This function is intended to be used for constructing |
| 397 | * ->error_handler callback by low level drivers. |
| 398 | * |
| 399 | * LOCKING: |
| 400 | * Kernel thread context (may sleep) |
| 401 | */ |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 402 | void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset, |
| 403 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, |
| 404 | ata_postreset_fn_t postreset) |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 405 | { |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 406 | struct ata_queued_cmd *qc; |
| 407 | unsigned long flags; |
| 408 | int thaw = 0; |
| 409 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 410 | qc = __ata_qc_from_tag(ap, ap->link.active_tag); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 411 | if (qc && !(qc->flags & ATA_QCFLAG_FAILED)) |
| 412 | qc = NULL; |
| 413 | |
| 414 | /* reset PIO HSM and stop DMA engine */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 415 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 416 | |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 417 | ap->hsm_task_state = HSM_ST_IDLE; |
| 418 | |
| 419 | if (qc && (qc->tf.protocol == ATA_PROT_DMA || |
Tejun Heo | 0dc3688 | 2007-12-18 16:34:43 -0500 | [diff] [blame^] | 420 | qc->tf.protocol == ATAPI_PROT_DMA)) { |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 421 | u8 host_stat; |
| 422 | |
Robert Hancock | fbbb262 | 2006-10-27 19:08:41 -0700 | [diff] [blame] | 423 | host_stat = ap->ops->bmdma_status(ap); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 424 | |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 425 | /* BMDMA controllers indicate host bus error by |
| 426 | * setting DMA_ERR bit and timing out. As it wasn't |
| 427 | * really a timeout event, adjust error mask and |
| 428 | * cancel frozen state. |
| 429 | */ |
Alan | 18d90de | 2007-01-24 11:42:38 +0000 | [diff] [blame] | 430 | if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) { |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 431 | qc->err_mask = AC_ERR_HOST_BUS; |
| 432 | thaw = 1; |
| 433 | } |
| 434 | |
| 435 | ap->ops->bmdma_stop(qc); |
| 436 | } |
| 437 | |
| 438 | ata_altstatus(ap); |
| 439 | ata_chk_status(ap); |
| 440 | ap->ops->irq_clear(ap); |
| 441 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 442 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 443 | |
| 444 | if (thaw) |
| 445 | ata_eh_thaw_port(ap); |
| 446 | |
| 447 | /* PIO and DMA engines have been stopped, perform recovery */ |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 448 | ata_do_eh(ap, prereset, softreset, hardreset, postreset); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /** |
| 452 | * ata_bmdma_error_handler - Stock error handler for BMDMA controller |
| 453 | * @ap: port to handle error for |
| 454 | * |
| 455 | * Stock error handler for BMDMA controller. |
| 456 | * |
| 457 | * LOCKING: |
| 458 | * Kernel thread context (may sleep) |
| 459 | */ |
| 460 | void ata_bmdma_error_handler(struct ata_port *ap) |
| 461 | { |
| 462 | ata_reset_fn_t hardreset; |
| 463 | |
| 464 | hardreset = NULL; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 465 | if (sata_scr_valid(&ap->link)) |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 466 | hardreset = sata_std_hardreset; |
| 467 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 468 | ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset, |
| 469 | ata_std_postreset); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
| 473 | * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for |
| 474 | * BMDMA controller |
| 475 | * @qc: internal command to clean up |
| 476 | * |
| 477 | * LOCKING: |
| 478 | * Kernel thread context (may sleep) |
| 479 | */ |
| 480 | void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc) |
| 481 | { |
Alan | 61dd08c | 2007-01-25 15:09:05 +0000 | [diff] [blame] | 482 | if (qc->ap->ioaddr.bmdma_addr) |
| 483 | ata_bmdma_stop(qc); |
Tejun Heo | 6d97dbd | 2006-05-15 20:58:24 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Alan Cox | d92e74d | 2007-06-07 16:19:15 +0100 | [diff] [blame] | 486 | /** |
| 487 | * ata_sff_port_start - Set port up for dma. |
| 488 | * @ap: Port to initialize |
| 489 | * |
| 490 | * Called just after data structures for each port are |
| 491 | * initialized. Allocates space for PRD table if the device |
| 492 | * is DMA capable SFF. |
| 493 | * |
| 494 | * May be used as the port_start() entry in ata_port_operations. |
| 495 | * |
| 496 | * LOCKING: |
| 497 | * Inherited from caller. |
| 498 | */ |
| 499 | |
| 500 | int ata_sff_port_start(struct ata_port *ap) |
| 501 | { |
| 502 | if (ap->ioaddr.bmdma_addr) |
| 503 | return ata_port_start(ap); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 507 | #ifdef CONFIG_PCI |
Alan | 4112e16 | 2007-01-08 12:10:05 +0000 | [diff] [blame] | 508 | |
| 509 | static int ata_resources_present(struct pci_dev *pdev, int port) |
| 510 | { |
| 511 | int i; |
Jeff Garzik | a84471f | 2007-02-26 05:51:33 -0500 | [diff] [blame] | 512 | |
Alan | 4112e16 | 2007-01-08 12:10:05 +0000 | [diff] [blame] | 513 | /* Check the PCI resources for this channel are enabled */ |
| 514 | port = port * 2; |
| 515 | for (i = 0; i < 2; i ++) { |
| 516 | if (pci_resource_start(pdev, port + i) == 0 || |
Tejun Heo | 55a6ade | 2007-03-09 19:43:35 +0900 | [diff] [blame] | 517 | pci_resource_len(pdev, port + i) == 0) |
| 518 | return 0; |
Alan | 4112e16 | 2007-01-08 12:10:05 +0000 | [diff] [blame] | 519 | } |
| 520 | return 1; |
| 521 | } |
Jeff Garzik | a84471f | 2007-02-26 05:51:33 -0500 | [diff] [blame] | 522 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 523 | /** |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 524 | * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host |
| 525 | * @host: target ATA host |
| 526 | * |
| 527 | * Acquire PCI BMDMA resources and initialize @host accordingly. |
| 528 | * |
| 529 | * LOCKING: |
| 530 | * Inherited from calling layer (may sleep). |
| 531 | * |
| 532 | * RETURNS: |
| 533 | * 0 on success, -errno otherwise. |
| 534 | */ |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 535 | int ata_pci_init_bmdma(struct ata_host *host) |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 536 | { |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 537 | struct device *gdev = host->dev; |
| 538 | struct pci_dev *pdev = to_pci_dev(gdev); |
| 539 | int i, rc; |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 540 | |
Alan Cox | 6fdc99a | 2007-07-26 18:41:30 +0100 | [diff] [blame] | 541 | /* No BAR4 allocation: No DMA */ |
| 542 | if (pci_resource_start(pdev, 4) == 0) |
| 543 | return 0; |
| 544 | |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 545 | /* TODO: If we get no DMA mask we should fall back to PIO */ |
| 546 | rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); |
| 547 | if (rc) |
| 548 | return rc; |
| 549 | rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); |
| 550 | if (rc) |
| 551 | return rc; |
| 552 | |
| 553 | /* request and iomap DMA region */ |
| 554 | rc = pcim_iomap_regions(pdev, 1 << 4, DRV_NAME); |
| 555 | if (rc) { |
| 556 | dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n"); |
| 557 | return -ENOMEM; |
| 558 | } |
| 559 | host->iomap = pcim_iomap_table(pdev); |
| 560 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 561 | for (i = 0; i < 2; i++) { |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 562 | struct ata_port *ap = host->ports[i]; |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 563 | void __iomem *bmdma = host->iomap[4] + 8 * i; |
| 564 | |
| 565 | if (ata_port_is_dummy(ap)) |
| 566 | continue; |
| 567 | |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 568 | ap->ioaddr.bmdma_addr = bmdma; |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 569 | if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) && |
| 570 | (ioread8(bmdma + 2) & 0x80)) |
| 571 | host->flags |= ATA_HOST_SIMPLEX; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 572 | |
| 573 | ata_port_desc(ap, "bmdma 0x%llx", |
| 574 | (unsigned long long)pci_resource_start(pdev, 4) + 8 * i); |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 575 | } |
| 576 | |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 577 | return 0; |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 580 | /** |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 581 | * ata_pci_init_sff_host - acquire native PCI ATA resources and init host |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 582 | * @host: target ATA host |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 583 | * |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 584 | * Acquire native PCI ATA resources for @host and initialize the |
| 585 | * first two ports of @host accordingly. Ports marked dummy are |
| 586 | * skipped and allocation failure makes the port dummy. |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 587 | * |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 588 | * Note that native PCI resources are valid even for legacy hosts |
| 589 | * as we fix up pdev resources array early in boot, so this |
| 590 | * function can be used for both native and legacy SFF hosts. |
| 591 | * |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 592 | * LOCKING: |
| 593 | * Inherited from calling layer (may sleep). |
| 594 | * |
| 595 | * RETURNS: |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 596 | * 0 if at least one port is initialized, -ENODEV if no port is |
| 597 | * available. |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 598 | */ |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 599 | int ata_pci_init_sff_host(struct ata_host *host) |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 600 | { |
| 601 | struct device *gdev = host->dev; |
| 602 | struct pci_dev *pdev = to_pci_dev(gdev); |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 603 | unsigned int mask = 0; |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 604 | int i, rc; |
| 605 | |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 606 | /* request, iomap BARs and init port addresses accordingly */ |
| 607 | for (i = 0; i < 2; i++) { |
| 608 | struct ata_port *ap = host->ports[i]; |
| 609 | int base = i * 2; |
| 610 | void __iomem * const *iomap; |
| 611 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 612 | if (ata_port_is_dummy(ap)) |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 613 | continue; |
| 614 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 615 | /* Discard disabled ports. Some controllers show |
| 616 | * their unused channels this way. Disabled ports are |
| 617 | * made dummy. |
| 618 | */ |
| 619 | if (!ata_resources_present(pdev, i)) { |
| 620 | ap->ops = &ata_dummy_port_ops; |
| 621 | continue; |
| 622 | } |
| 623 | |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 624 | rc = pcim_iomap_regions(pdev, 0x3 << base, DRV_NAME); |
| 625 | if (rc) { |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 626 | dev_printk(KERN_WARNING, gdev, |
| 627 | "failed to request/iomap BARs for port %d " |
| 628 | "(errno=%d)\n", i, rc); |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 629 | if (rc == -EBUSY) |
| 630 | pcim_pin_device(pdev); |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 631 | ap->ops = &ata_dummy_port_ops; |
| 632 | continue; |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 633 | } |
| 634 | host->iomap = iomap = pcim_iomap_table(pdev); |
| 635 | |
| 636 | ap->ioaddr.cmd_addr = iomap[base]; |
| 637 | ap->ioaddr.altstatus_addr = |
| 638 | ap->ioaddr.ctl_addr = (void __iomem *) |
| 639 | ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS); |
| 640 | ata_std_ports(&ap->ioaddr); |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 641 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 642 | ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx", |
| 643 | (unsigned long long)pci_resource_start(pdev, base), |
| 644 | (unsigned long long)pci_resource_start(pdev, base + 1)); |
| 645 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 646 | mask |= 1 << i; |
| 647 | } |
| 648 | |
| 649 | if (!mask) { |
| 650 | dev_printk(KERN_ERR, gdev, "no available native port\n"); |
| 651 | return -ENODEV; |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 657 | /** |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 658 | * ata_pci_prepare_sff_host - helper to prepare native PCI ATA host |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 659 | * @pdev: target PCI device |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 660 | * @ppi: array of port_info, must be enough for two ports |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 661 | * @r_host: out argument for the initialized ATA host |
| 662 | * |
| 663 | * Helper to allocate ATA host for @pdev, acquire all native PCI |
| 664 | * resources and initialize it accordingly in one go. |
| 665 | * |
| 666 | * LOCKING: |
| 667 | * Inherited from calling layer (may sleep). |
| 668 | * |
| 669 | * RETURNS: |
| 670 | * 0 on success, -errno otherwise. |
| 671 | */ |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 672 | int ata_pci_prepare_sff_host(struct pci_dev *pdev, |
| 673 | const struct ata_port_info * const * ppi, |
| 674 | struct ata_host **r_host) |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 675 | { |
| 676 | struct ata_host *host; |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 677 | int rc; |
| 678 | |
| 679 | if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) |
| 680 | return -ENOMEM; |
| 681 | |
| 682 | host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2); |
| 683 | if (!host) { |
| 684 | dev_printk(KERN_ERR, &pdev->dev, |
| 685 | "failed to allocate ATA host\n"); |
| 686 | rc = -ENOMEM; |
| 687 | goto err_out; |
| 688 | } |
| 689 | |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 690 | rc = ata_pci_init_sff_host(host); |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 691 | if (rc) |
| 692 | goto err_out; |
| 693 | |
| 694 | /* init DMA related stuff */ |
| 695 | rc = ata_pci_init_bmdma(host); |
| 696 | if (rc) |
| 697 | goto err_bmdma; |
| 698 | |
| 699 | devres_remove_group(&pdev->dev, NULL); |
| 700 | *r_host = host; |
| 701 | return 0; |
| 702 | |
| 703 | err_bmdma: |
| 704 | /* This is necessary because PCI and iomap resources are |
| 705 | * merged and releasing the top group won't release the |
| 706 | * acquired resources if some of those have been acquired |
| 707 | * before entering this function. |
| 708 | */ |
| 709 | pcim_iounmap_regions(pdev, 0xf); |
| 710 | err_out: |
| 711 | devres_release_group(&pdev->dev, NULL); |
| 712 | return rc; |
| 713 | } |
| 714 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 715 | /** |
| 716 | * ata_pci_init_one - Initialize/register PCI IDE host controller |
| 717 | * @pdev: Controller to be initialized |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 718 | * @ppi: array of port_info, must be enough for two ports |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 719 | * |
| 720 | * This is a helper function which can be called from a driver's |
| 721 | * xxx_init_one() probe function if the hardware uses traditional |
| 722 | * IDE taskfile registers. |
| 723 | * |
| 724 | * This function calls pci_enable_device(), reserves its register |
| 725 | * regions, sets the dma mask, enables bus master mode, and calls |
| 726 | * ata_device_add() |
| 727 | * |
Alan Cox | 2ec7df0 | 2006-08-10 16:59:10 +0900 | [diff] [blame] | 728 | * ASSUMPTION: |
| 729 | * Nobody makes a single channel controller that appears solely as |
| 730 | * the secondary legacy port on PCI. |
| 731 | * |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 732 | * LOCKING: |
| 733 | * Inherited from PCI layer (may sleep). |
| 734 | * |
| 735 | * RETURNS: |
| 736 | * Zero on success, negative on errno-based value on error. |
| 737 | */ |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 738 | int ata_pci_init_one(struct pci_dev *pdev, |
| 739 | const struct ata_port_info * const * ppi) |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 740 | { |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 741 | struct device *dev = &pdev->dev; |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 742 | const struct ata_port_info *pi = NULL; |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 743 | struct ata_host *host = NULL; |
Jeff Garzik | c791c30 | 2006-09-28 03:40:11 -0400 | [diff] [blame] | 744 | u8 mask; |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 745 | int legacy_mode = 0; |
| 746 | int i, rc; |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 747 | |
| 748 | DPRINTK("ENTER\n"); |
| 749 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 750 | /* look up the first valid port_info */ |
| 751 | for (i = 0; i < 2 && ppi[i]; i++) { |
| 752 | if (ppi[i]->port_ops != &ata_dummy_port_ops) { |
| 753 | pi = ppi[i]; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | if (!pi) { |
| 759 | dev_printk(KERN_ERR, &pdev->dev, |
| 760 | "no valid port_info specified\n"); |
| 761 | return -EINVAL; |
| 762 | } |
| 763 | |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 764 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
| 765 | return -ENOMEM; |
| 766 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 767 | /* FIXME: Really for ATA it isn't safe because the device may be |
| 768 | multi-purpose and we want to leave it alone if it was already |
| 769 | enabled. Secondly for shared use as Arjan says we want refcounting |
| 770 | |
| 771 | Checking dev->is_enabled is insufficient as this is not set at |
| 772 | boot for the primary video which is BIOS enabled |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 773 | */ |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 774 | |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 775 | rc = pcim_enable_device(pdev); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 776 | if (rc) |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 777 | goto err_out; |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 778 | |
Jeff Garzik | c791c30 | 2006-09-28 03:40:11 -0400 | [diff] [blame] | 779 | if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) { |
| 780 | u8 tmp8; |
| 781 | |
| 782 | /* TODO: What if one channel is in native mode ... */ |
| 783 | pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8); |
| 784 | mask = (1 << 2) | (1 << 0); |
| 785 | if ((tmp8 & mask) != mask) |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 786 | legacy_mode = 1; |
Alan Cox | 8eb166b | 2006-10-16 16:24:50 +0100 | [diff] [blame] | 787 | #if defined(CONFIG_NO_ATA_LEGACY) |
| 788 | /* Some platforms with PCI limits cannot address compat |
| 789 | port space. In that case we punt if their firmware has |
| 790 | left a device in compatibility mode */ |
| 791 | if (legacy_mode) { |
| 792 | printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n"); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 793 | rc = -EOPNOTSUPP; |
| 794 | goto err_out; |
Alan Cox | 8eb166b | 2006-10-16 16:24:50 +0100 | [diff] [blame] | 795 | } |
| 796 | #endif |
Jeff Garzik | c791c30 | 2006-09-28 03:40:11 -0400 | [diff] [blame] | 797 | } |
| 798 | |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 799 | /* prepare host */ |
| 800 | rc = ata_pci_prepare_sff_host(pdev, ppi, &host); |
| 801 | if (rc) |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 802 | goto err_out; |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 803 | |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 804 | pci_set_master(pdev); |
| 805 | |
| 806 | /* start host and request IRQ */ |
| 807 | rc = ata_host_start(host); |
| 808 | if (rc) |
| 809 | goto err_out; |
| 810 | |
Alan Cox | 277d72a | 2008-01-03 17:22:28 +0000 | [diff] [blame] | 811 | if (!legacy_mode && pdev->irq) { |
| 812 | /* We may have no IRQ assigned in which case we can poll. This |
| 813 | shouldn't happen on a sane system but robustness is cheap |
| 814 | in this case */ |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 815 | rc = devm_request_irq(dev, pdev->irq, pi->port_ops->irq_handler, |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 816 | IRQF_SHARED, DRV_NAME, host); |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 817 | if (rc) |
| 818 | goto err_out; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 819 | |
| 820 | ata_port_desc(host->ports[0], "irq %d", pdev->irq); |
| 821 | ata_port_desc(host->ports[1], "irq %d", pdev->irq); |
Alan Cox | 277d72a | 2008-01-03 17:22:28 +0000 | [diff] [blame] | 822 | } else if (legacy_mode) { |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 823 | if (!ata_port_is_dummy(host->ports[0])) { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 824 | rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev), |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 825 | pi->port_ops->irq_handler, |
| 826 | IRQF_SHARED, DRV_NAME, host); |
| 827 | if (rc) |
| 828 | goto err_out; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 829 | |
| 830 | ata_port_desc(host->ports[0], "irq %d", |
| 831 | ATA_PRIMARY_IRQ(pdev)); |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 832 | } |
Tejun Heo | 0f834de | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 833 | |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 834 | if (!ata_port_is_dummy(host->ports[1])) { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 835 | rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev), |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 836 | pi->port_ops->irq_handler, |
| 837 | IRQF_SHARED, DRV_NAME, host); |
| 838 | if (rc) |
| 839 | goto err_out; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 840 | |
| 841 | ata_port_desc(host->ports[1], "irq %d", |
| 842 | ATA_SECONDARY_IRQ(pdev)); |
Tejun Heo | d583bc1 | 2007-07-04 18:02:07 +0900 | [diff] [blame] | 843 | } |
Jeff Garzik | c791c30 | 2006-09-28 03:40:11 -0400 | [diff] [blame] | 844 | } |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 845 | |
| 846 | /* register */ |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 847 | rc = ata_host_register(host, pi->sht); |
Tejun Heo | d491b27 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 848 | if (rc) |
| 849 | goto err_out; |
| 850 | |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 851 | devres_remove_group(dev, NULL); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 852 | return 0; |
| 853 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 854 | err_out: |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 855 | devres_release_group(dev, NULL); |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 856 | return rc; |
| 857 | } |
| 858 | |
Alan Cox | d33d44f | 2006-03-21 15:59:57 +0000 | [diff] [blame] | 859 | /** |
| 860 | * ata_pci_clear_simplex - attempt to kick device out of simplex |
| 861 | * @pdev: PCI device |
| 862 | * |
| 863 | * Some PCI ATA devices report simplex mode but in fact can be told to |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 864 | * enter non simplex mode. This implements the necessary logic to |
Alan Cox | d33d44f | 2006-03-21 15:59:57 +0000 | [diff] [blame] | 865 | * perform the task on such devices. Calling it on other devices will |
| 866 | * have -undefined- behaviour. |
| 867 | */ |
| 868 | |
| 869 | int ata_pci_clear_simplex(struct pci_dev *pdev) |
| 870 | { |
| 871 | unsigned long bmdma = pci_resource_start(pdev, 4); |
| 872 | u8 simplex; |
| 873 | |
| 874 | if (bmdma == 0) |
| 875 | return -ENOENT; |
| 876 | |
| 877 | simplex = inb(bmdma + 0x02); |
| 878 | outb(simplex & 0x60, bmdma + 0x02); |
| 879 | simplex = inb(bmdma + 0x02); |
| 880 | if (simplex & 0x80) |
| 881 | return -EOPNOTSUPP; |
| 882 | return 0; |
| 883 | } |
| 884 | |
Alan Cox | a76b62c | 2007-03-09 09:34:07 -0500 | [diff] [blame] | 885 | unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask) |
Alan Cox | d33d44f | 2006-03-21 15:59:57 +0000 | [diff] [blame] | 886 | { |
| 887 | /* Filter out DMA modes if the device has been configured by |
| 888 | the BIOS as PIO only */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 889 | |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 890 | if (adev->link->ap->ioaddr.bmdma_addr == NULL) |
Alan Cox | d33d44f | 2006-03-21 15:59:57 +0000 | [diff] [blame] | 891 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
| 892 | return xfer_mask; |
| 893 | } |
| 894 | |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 895 | #endif /* CONFIG_PCI */ |
| 896 | |