2 * libata-bmdma.c - helper library for PCI IDE BMDMA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
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)
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <linux/libata.h>
42 * ata_irq_on - Enable interrupts on a port.
43 * @ap: Port on which interrupts are enabled.
45 * Enable interrupts on a legacy IDE device using MMIO or PIO,
46 * wait for idle, clear any pending interrupts.
49 * Inherited from caller.
51 u8
ata_irq_on(struct ata_port
*ap
)
53 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
57 ap
->last_ctl
= ap
->ctl
;
59 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
60 tmp
= ata_wait_idle(ap
);
62 ap
->ops
->irq_clear(ap
);
67 u8
ata_dummy_irq_on (struct ata_port
*ap
) { return 0; }
70 * ata_irq_ack - Acknowledge a device interrupt.
71 * @ap: Port on which interrupts are enabled.
73 * Wait up to 10 ms for legacy IDE device to become idle (BUSY
74 * or BUSY+DRQ clear). Obtain dma status and port status from
75 * device. Clear the interrupt. Return port status.
80 u8
ata_irq_ack(struct ata_port
*ap
, unsigned int chk_drq
)
82 unsigned int bits
= chk_drq
? ATA_BUSY
| ATA_DRQ
: ATA_BUSY
;
83 u8 host_stat
= 0, post_stat
= 0, status
;
85 status
= ata_busy_wait(ap
, bits
, 1000);
88 printk(KERN_ERR
"abnormal status 0x%X\n", status
);
90 if (ap
->ioaddr
.bmdma_addr
) {
91 /* get controller status; clear intr, err bits */
92 host_stat
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
93 iowrite8(host_stat
| ATA_DMA_INTR
| ATA_DMA_ERR
,
94 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
96 post_stat
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
99 printk(KERN_INFO
"%s: irq ack: host_stat 0x%X, new host_stat 0x%X, drv_stat 0x%X\n",
101 host_stat
, post_stat
, status
);
105 u8
ata_dummy_irq_ack(struct ata_port
*ap
, unsigned int chk_drq
) { return 0; }
108 * ata_tf_load - send taskfile registers to host controller
109 * @ap: Port to which output is sent
110 * @tf: ATA taskfile register set
112 * Outputs ATA taskfile to standard ATA host controller.
115 * Inherited from caller.
118 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
120 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
121 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
123 if (tf
->ctl
!= ap
->last_ctl
) {
124 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
125 ap
->last_ctl
= tf
->ctl
;
129 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
130 iowrite8(tf
->hob_feature
, ioaddr
->feature_addr
);
131 iowrite8(tf
->hob_nsect
, ioaddr
->nsect_addr
);
132 iowrite8(tf
->hob_lbal
, ioaddr
->lbal_addr
);
133 iowrite8(tf
->hob_lbam
, ioaddr
->lbam_addr
);
134 iowrite8(tf
->hob_lbah
, ioaddr
->lbah_addr
);
135 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
144 iowrite8(tf
->feature
, ioaddr
->feature_addr
);
145 iowrite8(tf
->nsect
, ioaddr
->nsect_addr
);
146 iowrite8(tf
->lbal
, ioaddr
->lbal_addr
);
147 iowrite8(tf
->lbam
, ioaddr
->lbam_addr
);
148 iowrite8(tf
->lbah
, ioaddr
->lbah_addr
);
149 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
157 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
158 iowrite8(tf
->device
, ioaddr
->device_addr
);
159 VPRINTK("device 0x%X\n", tf
->device
);
166 * ata_exec_command - issue ATA command to host controller
167 * @ap: port to which command is being issued
168 * @tf: ATA taskfile register set
170 * Issues ATA command, with proper synchronization with interrupt
171 * handler / other threads.
174 * spin_lock_irqsave(host lock)
176 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
178 DPRINTK("ata%u: cmd 0x%X\n", ap
->print_id
, tf
->command
);
180 iowrite8(tf
->command
, ap
->ioaddr
.command_addr
);
185 * ata_tf_read - input device's ATA taskfile shadow registers
186 * @ap: Port from which input is read
187 * @tf: ATA taskfile register set for storing input
189 * Reads ATA taskfile registers for currently-selected device
193 * Inherited from caller.
195 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
197 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
199 tf
->command
= ata_check_status(ap
);
200 tf
->feature
= ioread8(ioaddr
->error_addr
);
201 tf
->nsect
= ioread8(ioaddr
->nsect_addr
);
202 tf
->lbal
= ioread8(ioaddr
->lbal_addr
);
203 tf
->lbam
= ioread8(ioaddr
->lbam_addr
);
204 tf
->lbah
= ioread8(ioaddr
->lbah_addr
);
205 tf
->device
= ioread8(ioaddr
->device_addr
);
207 if (tf
->flags
& ATA_TFLAG_LBA48
) {
208 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
209 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
210 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
211 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
212 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
213 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
218 * ata_check_status - Read device status reg & clear interrupt
219 * @ap: port where the device is
221 * Reads ATA taskfile status register for currently-selected device
222 * and return its value. This also clears pending interrupts
226 * Inherited from caller.
228 u8
ata_check_status(struct ata_port
*ap
)
230 return ioread8(ap
->ioaddr
.status_addr
);
234 * ata_altstatus - Read device alternate status reg
235 * @ap: port where the device is
237 * Reads ATA taskfile alternate status register for
238 * currently-selected device and return its value.
240 * Note: may NOT be used as the check_altstatus() entry in
241 * ata_port_operations.
244 * Inherited from caller.
246 u8
ata_altstatus(struct ata_port
*ap
)
248 if (ap
->ops
->check_altstatus
)
249 return ap
->ops
->check_altstatus(ap
);
251 return ioread8(ap
->ioaddr
.altstatus_addr
);
255 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
256 * @qc: Info associated with this ATA transaction.
259 * spin_lock_irqsave(host lock)
261 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
263 struct ata_port
*ap
= qc
->ap
;
264 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
267 /* load PRD table addr. */
268 mb(); /* make sure PRD table writes are visible to controller */
269 iowrite32(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
271 /* specify data direction, triple-check start bit is clear */
272 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
273 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
275 dmactl
|= ATA_DMA_WR
;
276 iowrite8(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
278 /* issue r/w command */
279 ap
->ops
->exec_command(ap
, &qc
->tf
);
283 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
284 * @qc: Info associated with this ATA transaction.
287 * spin_lock_irqsave(host lock)
289 void ata_bmdma_start (struct ata_queued_cmd
*qc
)
291 struct ata_port
*ap
= qc
->ap
;
294 /* start host DMA transaction */
295 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
296 iowrite8(dmactl
| ATA_DMA_START
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
298 /* Strictly, one may wish to issue a readb() here, to
299 * flush the mmio write. However, control also passes
300 * to the hardware at this point, and it will interrupt
301 * us when we are to resume control. So, in effect,
302 * we don't care when the mmio write flushes.
303 * Further, a read of the DMA status register _immediately_
304 * following the write may not be what certain flaky hardware
305 * is expected, so I think it is best to not add a readb()
306 * without first all the MMIO ATA cards/mobos.
307 * Or maybe I'm just being paranoid.
312 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
313 * @ap: Port associated with this ATA transaction.
315 * Clear interrupt and error flags in DMA status register.
317 * May be used as the irq_clear() entry in ata_port_operations.
320 * spin_lock_irqsave(host lock)
322 void ata_bmdma_irq_clear(struct ata_port
*ap
)
324 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
329 iowrite8(ioread8(mmio
+ ATA_DMA_STATUS
), mmio
+ ATA_DMA_STATUS
);
333 * ata_bmdma_status - Read PCI IDE BMDMA status
334 * @ap: Port associated with this ATA transaction.
336 * Read and return BMDMA status register.
338 * May be used as the bmdma_status() entry in ata_port_operations.
341 * spin_lock_irqsave(host lock)
343 u8
ata_bmdma_status(struct ata_port
*ap
)
345 return ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
349 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
350 * @qc: Command we are ending DMA for
352 * Clears the ATA_DMA_START flag in the dma control register
354 * May be used as the bmdma_stop() entry in ata_port_operations.
357 * spin_lock_irqsave(host lock)
359 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
361 struct ata_port
*ap
= qc
->ap
;
362 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
364 /* clear start/stop bit */
365 iowrite8(ioread8(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
368 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
369 ata_altstatus(ap
); /* dummy read */
373 * ata_bmdma_freeze - Freeze BMDMA controller port
374 * @ap: port to freeze
376 * Freeze BMDMA controller port.
379 * Inherited from caller.
381 void ata_bmdma_freeze(struct ata_port
*ap
)
383 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
386 ap
->last_ctl
= ap
->ctl
;
388 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
390 /* Under certain circumstances, some controllers raise IRQ on
391 * ATA_NIEN manipulation. Also, many controllers fail to mask
392 * previously pending IRQ on ATA_NIEN assertion. Clear it.
396 ap
->ops
->irq_clear(ap
);
400 * ata_bmdma_thaw - Thaw BMDMA controller port
403 * Thaw BMDMA controller port.
406 * Inherited from caller.
408 void ata_bmdma_thaw(struct ata_port
*ap
)
410 /* clear & re-enable interrupts */
412 ap
->ops
->irq_clear(ap
);
417 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
418 * @ap: port to handle error for
419 * @prereset: prereset method (can be NULL)
420 * @softreset: softreset method (can be NULL)
421 * @hardreset: hardreset method (can be NULL)
422 * @postreset: postreset method (can be NULL)
424 * Handle error for ATA BMDMA controller. It can handle both
425 * PATA and SATA controllers. Many controllers should be able to
426 * use this EH as-is or with some added handling before and
429 * This function is intended to be used for constructing
430 * ->error_handler callback by low level drivers.
433 * Kernel thread context (may sleep)
435 void ata_bmdma_drive_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
436 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
437 ata_postreset_fn_t postreset
)
439 struct ata_queued_cmd
*qc
;
443 qc
= __ata_qc_from_tag(ap
, ap
->active_tag
);
444 if (qc
&& !(qc
->flags
& ATA_QCFLAG_FAILED
))
447 /* reset PIO HSM and stop DMA engine */
448 spin_lock_irqsave(ap
->lock
, flags
);
450 ap
->hsm_task_state
= HSM_ST_IDLE
;
452 if (qc
&& (qc
->tf
.protocol
== ATA_PROT_DMA
||
453 qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)) {
456 host_stat
= ap
->ops
->bmdma_status(ap
);
458 /* BMDMA controllers indicate host bus error by
459 * setting DMA_ERR bit and timing out. As it wasn't
460 * really a timeout event, adjust error mask and
461 * cancel frozen state.
463 if (qc
->err_mask
== AC_ERR_TIMEOUT
&& (host_stat
& ATA_DMA_ERR
)) {
464 qc
->err_mask
= AC_ERR_HOST_BUS
;
468 ap
->ops
->bmdma_stop(qc
);
473 ap
->ops
->irq_clear(ap
);
475 spin_unlock_irqrestore(ap
->lock
, flags
);
478 ata_eh_thaw_port(ap
);
480 /* PIO and DMA engines have been stopped, perform recovery */
481 ata_do_eh(ap
, prereset
, softreset
, hardreset
, postreset
);
485 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
486 * @ap: port to handle error for
488 * Stock error handler for BMDMA controller.
491 * Kernel thread context (may sleep)
493 void ata_bmdma_error_handler(struct ata_port
*ap
)
495 ata_reset_fn_t hardreset
;
498 if (sata_scr_valid(ap
))
499 hardreset
= sata_std_hardreset
;
501 ata_bmdma_drive_eh(ap
, ata_std_prereset
, ata_std_softreset
, hardreset
,
506 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
508 * @qc: internal command to clean up
511 * Kernel thread context (may sleep)
513 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd
*qc
)
515 if (qc
->ap
->ioaddr
.bmdma_addr
)
520 * ata_sff_port_start - Set port up for dma.
521 * @ap: Port to initialize
523 * Called just after data structures for each port are
524 * initialized. Allocates space for PRD table if the device
525 * is DMA capable SFF.
527 * May be used as the port_start() entry in ata_port_operations.
530 * Inherited from caller.
533 int ata_sff_port_start(struct ata_port
*ap
)
535 if (ap
->ioaddr
.bmdma_addr
)
536 return ata_port_start(ap
);
542 static int ata_resources_present(struct pci_dev
*pdev
, int port
)
546 /* Check the PCI resources for this channel are enabled */
548 for (i
= 0; i
< 2; i
++) {
549 if (pci_resource_start(pdev
, port
+ i
) == 0 ||
550 pci_resource_len(pdev
, port
+ i
) == 0)
557 * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
558 * @host: target ATA host
560 * Acquire PCI BMDMA resources and initialize @host accordingly.
563 * Inherited from calling layer (may sleep).
566 * 0 on success, -errno otherwise.
568 int ata_pci_init_bmdma(struct ata_host
*host
)
570 struct device
*gdev
= host
->dev
;
571 struct pci_dev
*pdev
= to_pci_dev(gdev
);
574 /* TODO: If we get no DMA mask we should fall back to PIO */
575 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
578 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
582 /* request and iomap DMA region */
583 rc
= pcim_iomap_regions(pdev
, 1 << 4, DRV_NAME
);
585 dev_printk(KERN_ERR
, gdev
, "failed to request/iomap BAR4\n");
588 host
->iomap
= pcim_iomap_table(pdev
);
590 for (i
= 0; i
< 2; i
++) {
591 struct ata_port
*ap
= host
->ports
[i
];
592 void __iomem
*bmdma
= host
->iomap
[4] + 8 * i
;
594 if (ata_port_is_dummy(ap
))
597 ap
->ioaddr
.bmdma_addr
= bmdma
;
598 if ((!(ap
->flags
& ATA_FLAG_IGN_SIMPLEX
)) &&
599 (ioread8(bmdma
+ 2) & 0x80))
600 host
->flags
|= ATA_HOST_SIMPLEX
;
607 * ata_pci_init_native_host - acquire native ATA resources and init host
608 * @host: target ATA host
610 * Acquire native PCI ATA resources for @host and initialize the
611 * first two ports of @host accordingly. Ports marked dummy are
612 * skipped and allocation failure makes the port dummy.
615 * Inherited from calling layer (may sleep).
618 * 0 if at least one port is initialized, -ENODEV if no port is
621 int ata_pci_init_native_host(struct ata_host
*host
)
623 struct device
*gdev
= host
->dev
;
624 struct pci_dev
*pdev
= to_pci_dev(gdev
);
625 unsigned int mask
= 0;
628 /* request, iomap BARs and init port addresses accordingly */
629 for (i
= 0; i
< 2; i
++) {
630 struct ata_port
*ap
= host
->ports
[i
];
632 void __iomem
* const *iomap
;
634 if (ata_port_is_dummy(ap
))
637 /* Discard disabled ports. Some controllers show
638 * their unused channels this way. Disabled ports are
641 if (!ata_resources_present(pdev
, i
)) {
642 ap
->ops
= &ata_dummy_port_ops
;
646 rc
= pcim_iomap_regions(pdev
, 0x3 << base
, DRV_NAME
);
648 dev_printk(KERN_WARNING
, gdev
,
649 "failed to request/iomap BARs for port %d "
650 "(errno=%d)\n", i
, rc
);
652 pcim_pin_device(pdev
);
653 ap
->ops
= &ata_dummy_port_ops
;
656 host
->iomap
= iomap
= pcim_iomap_table(pdev
);
658 ap
->ioaddr
.cmd_addr
= iomap
[base
];
659 ap
->ioaddr
.altstatus_addr
=
660 ap
->ioaddr
.ctl_addr
= (void __iomem
*)
661 ((unsigned long)iomap
[base
+ 1] | ATA_PCI_CTL_OFS
);
662 ata_std_ports(&ap
->ioaddr
);
668 dev_printk(KERN_ERR
, gdev
, "no available native port\n");
676 * ata_pci_prepare_native_host - helper to prepare native PCI ATA host
677 * @pdev: target PCI device
678 * @ppi: array of port_info, must be enough for two ports
679 * @r_host: out argument for the initialized ATA host
681 * Helper to allocate ATA host for @pdev, acquire all native PCI
682 * resources and initialize it accordingly in one go.
685 * Inherited from calling layer (may sleep).
688 * 0 on success, -errno otherwise.
690 int ata_pci_prepare_native_host(struct pci_dev
*pdev
,
691 const struct ata_port_info
* const * ppi
,
692 struct ata_host
**r_host
)
694 struct ata_host
*host
;
697 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
))
700 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, 2);
702 dev_printk(KERN_ERR
, &pdev
->dev
,
703 "failed to allocate ATA host\n");
708 rc
= ata_pci_init_native_host(host
);
712 /* init DMA related stuff */
713 rc
= ata_pci_init_bmdma(host
);
717 devres_remove_group(&pdev
->dev
, NULL
);
722 /* This is necessary because PCI and iomap resources are
723 * merged and releasing the top group won't release the
724 * acquired resources if some of those have been acquired
725 * before entering this function.
727 pcim_iounmap_regions(pdev
, 0xf);
729 devres_release_group(&pdev
->dev
, NULL
);
733 struct ata_legacy_devres
{
735 unsigned long cmd_port
[2];
736 void __iomem
* cmd_addr
[2];
737 void __iomem
* ctl_addr
[2];
739 void * irq_dev_id
[2];
742 static void ata_legacy_free_irqs(struct ata_legacy_devres
*legacy_dr
)
746 for (i
= 0; i
< 2; i
++) {
747 if (!legacy_dr
->irq
[i
])
750 free_irq(legacy_dr
->irq
[i
], legacy_dr
->irq_dev_id
[i
]);
751 legacy_dr
->irq
[i
] = 0;
752 legacy_dr
->irq_dev_id
[i
] = NULL
;
756 static void ata_legacy_release(struct device
*gdev
, void *res
)
758 struct ata_legacy_devres
*this = res
;
761 ata_legacy_free_irqs(this);
763 for (i
= 0; i
< 2; i
++) {
764 if (this->cmd_addr
[i
])
765 ioport_unmap(this->cmd_addr
[i
]);
766 if (this->ctl_addr
[i
])
767 ioport_unmap(this->ctl_addr
[i
]);
768 if (this->cmd_port
[i
])
769 release_region(this->cmd_port
[i
], 8);
773 static int ata_init_legacy_port(struct ata_port
*ap
,
774 struct ata_legacy_devres
*legacy_dr
)
776 struct ata_host
*host
= ap
->host
;
777 int port_no
= ap
->port_no
;
778 unsigned long cmd_port
, ctl_port
;
781 cmd_port
= ATA_PRIMARY_CMD
;
782 ctl_port
= ATA_PRIMARY_CTL
;
784 cmd_port
= ATA_SECONDARY_CMD
;
785 ctl_port
= ATA_SECONDARY_CTL
;
788 /* request cmd_port */
789 if (request_region(cmd_port
, 8, "libata"))
790 legacy_dr
->cmd_port
[port_no
] = cmd_port
;
792 dev_printk(KERN_WARNING
, host
->dev
,
793 "0x%0lX IDE port busy\n", cmd_port
);
797 /* iomap cmd and ctl ports */
798 legacy_dr
->cmd_addr
[port_no
] = ioport_map(cmd_port
, 8);
799 legacy_dr
->ctl_addr
[port_no
] = ioport_map(ctl_port
, 1);
800 if (!legacy_dr
->cmd_addr
[port_no
] || !legacy_dr
->ctl_addr
[port_no
]) {
801 dev_printk(KERN_WARNING
, host
->dev
,
802 "failed to map cmd/ctl ports\n");
806 /* init IO addresses */
807 ap
->ioaddr
.cmd_addr
= legacy_dr
->cmd_addr
[port_no
];
808 ap
->ioaddr
.altstatus_addr
= legacy_dr
->ctl_addr
[port_no
];
809 ap
->ioaddr
.ctl_addr
= legacy_dr
->ctl_addr
[port_no
];
810 ata_std_ports(&ap
->ioaddr
);
816 * ata_init_legacy_host - acquire legacy ATA resources and init ATA host
817 * @host: target ATA host
818 * @was_busy: out parameter, indicates whether any port was busy
820 * Acquire legacy ATA resources for the first two ports of @host
821 * and initialize it accordingly. Ports marked dummy are skipped
822 * and resource acquistion failure makes the port dummy.
825 * Inherited from calling layer (may sleep).
828 * 0 if at least one port is initialized, -ENODEV if no port is
831 static int ata_init_legacy_host(struct ata_host
*host
, int *was_busy
)
833 struct device
*gdev
= host
->dev
;
834 struct ata_legacy_devres
*legacy_dr
;
837 if (!devres_open_group(gdev
, NULL
, GFP_KERNEL
))
841 legacy_dr
= devres_alloc(ata_legacy_release
, sizeof(*legacy_dr
),
845 devres_add(gdev
, legacy_dr
);
847 for (i
= 0; i
< 2; i
++) {
848 if (ata_port_is_dummy(host
->ports
[i
]))
851 rc
= ata_init_legacy_port(host
->ports
[i
], legacy_dr
);
853 legacy_dr
->mask
|= 1 << i
;
857 host
->ports
[i
]->ops
= &ata_dummy_port_ops
;
861 if (!legacy_dr
->mask
) {
862 dev_printk(KERN_ERR
, gdev
, "no available legacy port\n");
866 devres_remove_group(gdev
, NULL
);
870 devres_release_group(gdev
, NULL
);
875 * ata_request_legacy_irqs - request legacy ATA IRQs
876 * @host: target ATA host
877 * @handler: array of IRQ handlers
878 * @irq_flags: array of IRQ flags
879 * @dev_id: array of IRQ dev_ids
881 * Request legacy IRQs for non-dummy legacy ports in @host. All
882 * IRQ parameters are passed as array to allow ports to have
883 * separate IRQ handlers.
886 * Inherited from calling layer (may sleep).
889 * 0 on success, -errno otherwise.
891 static int ata_request_legacy_irqs(struct ata_host
*host
,
892 irq_handler_t
const *handler
,
893 const unsigned int *irq_flags
,
894 void * const *dev_id
)
896 struct device
*gdev
= host
->dev
;
897 struct ata_legacy_devres
*legacy_dr
;
900 legacy_dr
= devres_find(host
->dev
, ata_legacy_release
, NULL
, NULL
);
903 for (i
= 0; i
< 2; i
++) {
906 /* FIXME: ATA_*_IRQ() should take generic device not pci_dev */
908 irq
= ATA_PRIMARY_IRQ(to_pci_dev(gdev
));
910 irq
= ATA_SECONDARY_IRQ(to_pci_dev(gdev
));
912 if (!(legacy_dr
->mask
& (1 << i
)))
916 dev_printk(KERN_ERR
, gdev
,
917 "NULL handler specified for port %d\n", i
);
922 rc
= request_irq(irq
, handler
[i
], irq_flags
[i
], DRV_NAME
,
925 dev_printk(KERN_ERR
, gdev
,
926 "irq %u request failed (errno=%d)\n", irq
, rc
);
930 /* record irq allocation in legacy_dr */
931 legacy_dr
->irq
[i
] = irq
;
932 legacy_dr
->irq_dev_id
[i
] = dev_id
[i
];
934 /* only used to print info */
944 ata_legacy_free_irqs(legacy_dr
);
949 * ata_pci_init_one - Initialize/register PCI IDE host controller
950 * @pdev: Controller to be initialized
951 * @ppi: array of port_info, must be enough for two ports
953 * This is a helper function which can be called from a driver's
954 * xxx_init_one() probe function if the hardware uses traditional
955 * IDE taskfile registers.
957 * This function calls pci_enable_device(), reserves its register
958 * regions, sets the dma mask, enables bus master mode, and calls
962 * Nobody makes a single channel controller that appears solely as
963 * the secondary legacy port on PCI.
966 * Inherited from PCI layer (may sleep).
969 * Zero on success, negative on errno-based value on error.
971 int ata_pci_init_one(struct pci_dev
*pdev
,
972 const struct ata_port_info
* const * ppi
)
974 struct device
*dev
= &pdev
->dev
;
975 const struct ata_port_info
*pi
= NULL
;
976 struct ata_host
*host
= NULL
;
983 /* look up the first valid port_info */
984 for (i
= 0; i
< 2 && ppi
[i
]; i
++) {
985 if (ppi
[i
]->port_ops
!= &ata_dummy_port_ops
) {
992 dev_printk(KERN_ERR
, &pdev
->dev
,
993 "no valid port_info specified\n");
997 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
1000 /* FIXME: Really for ATA it isn't safe because the device may be
1001 multi-purpose and we want to leave it alone if it was already
1002 enabled. Secondly for shared use as Arjan says we want refcounting
1004 Checking dev->is_enabled is insufficient as this is not set at
1005 boot for the primary video which is BIOS enabled
1008 rc
= pcim_enable_device(pdev
);
1012 if ((pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
1015 /* TODO: What if one channel is in native mode ... */
1016 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
1017 mask
= (1 << 2) | (1 << 0);
1018 if ((tmp8
& mask
) != mask
)
1020 #if defined(CONFIG_NO_ATA_LEGACY)
1021 /* Some platforms with PCI limits cannot address compat
1022 port space. In that case we punt if their firmware has
1023 left a device in compatibility mode */
1025 printk(KERN_ERR
"ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
1032 /* alloc and init host */
1033 host
= ata_host_alloc_pinfo(dev
, ppi
, 2);
1035 dev_printk(KERN_ERR
, &pdev
->dev
,
1036 "failed to allocate ATA host\n");
1042 rc
= ata_pci_init_native_host(host
);
1048 rc
= ata_init_legacy_host(host
, &was_busy
);
1050 pcim_pin_device(pdev
);
1054 /* request respective PCI regions, may fail */
1055 rc
= pci_request_region(pdev
, 1, DRV_NAME
);
1056 rc
= pci_request_region(pdev
, 3, DRV_NAME
);
1059 /* init BMDMA, may fail */
1060 ata_pci_init_bmdma(host
);
1061 pci_set_master(pdev
);
1063 /* start host and request IRQ */
1064 rc
= ata_host_start(host
);
1069 rc
= devm_request_irq(dev
, pdev
->irq
, pi
->port_ops
->irq_handler
,
1070 IRQF_SHARED
, DRV_NAME
, host
);
1072 irq_handler_t handler
[2] = { host
->ops
->irq_handler
,
1073 host
->ops
->irq_handler
};
1074 unsigned int irq_flags
[2] = { IRQF_SHARED
, IRQF_SHARED
};
1075 void *dev_id
[2] = { host
, host
};
1077 rc
= ata_request_legacy_irqs(host
, handler
, irq_flags
, dev_id
);
1083 rc
= ata_host_register(host
, pi
->sht
);
1087 devres_remove_group(dev
, NULL
);
1091 devres_release_group(dev
, NULL
);
1096 * ata_pci_clear_simplex - attempt to kick device out of simplex
1099 * Some PCI ATA devices report simplex mode but in fact can be told to
1100 * enter non simplex mode. This implements the neccessary logic to
1101 * perform the task on such devices. Calling it on other devices will
1102 * have -undefined- behaviour.
1105 int ata_pci_clear_simplex(struct pci_dev
*pdev
)
1107 unsigned long bmdma
= pci_resource_start(pdev
, 4);
1113 simplex
= inb(bmdma
+ 0x02);
1114 outb(simplex
& 0x60, bmdma
+ 0x02);
1115 simplex
= inb(bmdma
+ 0x02);
1121 unsigned long ata_pci_default_filter(struct ata_device
*adev
, unsigned long xfer_mask
)
1123 /* Filter out DMA modes if the device has been configured by
1124 the BIOS as PIO only */
1126 if (adev
->ap
->ioaddr
.bmdma_addr
== 0)
1127 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
1131 #endif /* CONFIG_PCI */