2 * libata-sff.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 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
60 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
63 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
64 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
65 tmp
= ata_wait_idle(ap
);
67 ap
->ops
->irq_clear(ap
);
73 * ata_tf_load - send taskfile registers to host controller
74 * @ap: Port to which output is sent
75 * @tf: ATA taskfile register set
77 * Outputs ATA taskfile to standard ATA host controller.
80 * Inherited from caller.
83 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
85 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
86 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
88 if (tf
->ctl
!= ap
->last_ctl
) {
89 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
90 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
93 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
94 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
95 ap
->last_ctl
= tf
->ctl
;
99 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
100 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
102 WARN_ON(!ioaddr
->ctl_addr
);
103 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
104 iowrite8(tf
->hob_feature
, ioaddr
->feature_addr
);
105 iowrite8(tf
->hob_nsect
, ioaddr
->nsect_addr
);
106 iowrite8(tf
->hob_lbal
, ioaddr
->lbal_addr
);
107 iowrite8(tf
->hob_lbam
, ioaddr
->lbam_addr
);
108 iowrite8(tf
->hob_lbah
, ioaddr
->lbah_addr
);
109 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
118 iowrite8(tf
->feature
, ioaddr
->feature_addr
);
119 iowrite8(tf
->nsect
, ioaddr
->nsect_addr
);
120 iowrite8(tf
->lbal
, ioaddr
->lbal_addr
);
121 iowrite8(tf
->lbam
, ioaddr
->lbam_addr
);
122 iowrite8(tf
->lbah
, ioaddr
->lbah_addr
);
123 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
131 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
132 iowrite8(tf
->device
, ioaddr
->device_addr
);
133 VPRINTK("device 0x%X\n", tf
->device
);
140 * ata_exec_command - issue ATA command to host controller
141 * @ap: port to which command is being issued
142 * @tf: ATA taskfile register set
144 * Issues ATA command, with proper synchronization with interrupt
145 * handler / other threads.
148 * spin_lock_irqsave(host lock)
150 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
152 DPRINTK("ata%u: cmd 0x%X\n", ap
->print_id
, tf
->command
);
154 iowrite8(tf
->command
, ap
->ioaddr
.command_addr
);
159 * ata_tf_read - input device's ATA taskfile shadow registers
160 * @ap: Port from which input is read
161 * @tf: ATA taskfile register set for storing input
163 * Reads ATA taskfile registers for currently-selected device
164 * into @tf. Assumes the device has a fully SFF compliant task file
165 * layout and behaviour. If you device does not (eg has a different
166 * status method) then you will need to provide a replacement tf_read
169 * Inherited from caller.
171 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
173 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
175 tf
->command
= ata_check_status(ap
);
176 tf
->feature
= ioread8(ioaddr
->error_addr
);
177 tf
->nsect
= ioread8(ioaddr
->nsect_addr
);
178 tf
->lbal
= ioread8(ioaddr
->lbal_addr
);
179 tf
->lbam
= ioread8(ioaddr
->lbam_addr
);
180 tf
->lbah
= ioread8(ioaddr
->lbah_addr
);
181 tf
->device
= ioread8(ioaddr
->device_addr
);
183 if (tf
->flags
& ATA_TFLAG_LBA48
) {
184 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
185 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
186 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
187 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
188 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
189 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
190 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
191 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
192 ap
->last_ctl
= tf
->ctl
;
194 if (likely(ioaddr
->ctl_addr
)) {
195 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
196 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
197 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
198 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
199 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
200 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
201 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
202 ap
->last_ctl
= tf
->ctl
;
205 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
210 * ata_check_status - Read device status reg & clear interrupt
211 * @ap: port where the device is
213 * Reads ATA taskfile status register for currently-selected device
214 * and return its value. This also clears pending interrupts
218 * Inherited from caller.
220 u8
ata_check_status(struct ata_port
*ap
)
222 return ioread8(ap
->ioaddr
.status_addr
);
226 * ata_altstatus - Read device alternate status reg
227 * @ap: port where the device is
229 * Reads ATA taskfile alternate status register for
230 * currently-selected device and return its value.
232 * Note: may NOT be used as the check_altstatus() entry in
233 * ata_port_operations.
236 * Inherited from caller.
238 u8
ata_altstatus(struct ata_port
*ap
)
240 if (ap
->ops
->check_altstatus
)
241 return ap
->ops
->check_altstatus(ap
);
243 return ioread8(ap
->ioaddr
.altstatus_addr
);
247 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
248 * @qc: Info associated with this ATA transaction.
251 * spin_lock_irqsave(host lock)
253 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
255 struct ata_port
*ap
= qc
->ap
;
256 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
259 /* load PRD table addr. */
260 mb(); /* make sure PRD table writes are visible to controller */
261 iowrite32(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
263 /* specify data direction, triple-check start bit is clear */
264 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
265 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
267 dmactl
|= ATA_DMA_WR
;
268 iowrite8(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
270 /* issue r/w command */
271 ap
->ops
->exec_command(ap
, &qc
->tf
);
275 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
276 * @qc: Info associated with this ATA transaction.
279 * spin_lock_irqsave(host lock)
281 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
283 struct ata_port
*ap
= qc
->ap
;
286 /* start host DMA transaction */
287 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
288 iowrite8(dmactl
| ATA_DMA_START
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
290 /* Strictly, one may wish to issue an ioread8() here, to
291 * flush the mmio write. However, control also passes
292 * to the hardware at this point, and it will interrupt
293 * us when we are to resume control. So, in effect,
294 * we don't care when the mmio write flushes.
295 * Further, a read of the DMA status register _immediately_
296 * following the write may not be what certain flaky hardware
297 * is expected, so I think it is best to not add a readb()
298 * without first all the MMIO ATA cards/mobos.
299 * Or maybe I'm just being paranoid.
301 * FIXME: The posting of this write means I/O starts are
302 * unneccessarily delayed for MMIO
307 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
308 * @ap: Port associated with this ATA transaction.
310 * Clear interrupt and error flags in DMA status register.
312 * May be used as the irq_clear() entry in ata_port_operations.
315 * spin_lock_irqsave(host lock)
317 void ata_bmdma_irq_clear(struct ata_port
*ap
)
319 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
324 iowrite8(ioread8(mmio
+ ATA_DMA_STATUS
), mmio
+ ATA_DMA_STATUS
);
328 * ata_bmdma_status - Read PCI IDE BMDMA status
329 * @ap: Port associated with this ATA transaction.
331 * Read and return BMDMA status register.
333 * May be used as the bmdma_status() entry in ata_port_operations.
336 * spin_lock_irqsave(host lock)
338 u8
ata_bmdma_status(struct ata_port
*ap
)
340 return ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
344 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
345 * @qc: Command we are ending DMA for
347 * Clears the ATA_DMA_START flag in the dma control register
349 * May be used as the bmdma_stop() entry in ata_port_operations.
352 * spin_lock_irqsave(host lock)
354 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
356 struct ata_port
*ap
= qc
->ap
;
357 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
359 /* clear start/stop bit */
360 iowrite8(ioread8(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
363 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
364 ata_altstatus(ap
); /* dummy read */
368 * ata_bmdma_freeze - Freeze BMDMA controller port
369 * @ap: port to freeze
371 * Freeze BMDMA controller port.
374 * Inherited from caller.
376 void ata_bmdma_freeze(struct ata_port
*ap
)
378 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
381 ap
->last_ctl
= ap
->ctl
;
383 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
384 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
386 if (ioaddr
->ctl_addr
)
387 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
388 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
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
->link
.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
== ATAPI_PROT_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 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
496 ata_reset_fn_t hardreset
;
498 ata_reset_fn_t softreset
= NULL
, hardreset
= NULL
;
499 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
501 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
504 if (ap
->ioaddr
.ctl_addr
)
505 softreset
= ata_std_softreset
;
506 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
507 if (sata_scr_valid(&ap
->link
))
508 hardreset
= sata_std_hardreset
;
510 <<<<<<< HEAD
:drivers
/ata
/libata
-sff
.c
511 ata_bmdma_drive_eh(ap
, ata_std_prereset
, ata_std_softreset
, hardreset
,
513 ata_bmdma_drive_eh(ap
, ata_std_prereset
, softreset
, hardreset
,
514 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/ata
/libata
-sff
.c
519 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
521 * @qc: internal command to clean up
524 * Kernel thread context (may sleep)
526 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd
*qc
)
528 if (qc
->ap
->ioaddr
.bmdma_addr
)
533 * ata_sff_port_start - Set port up for dma.
534 * @ap: Port to initialize
536 * Called just after data structures for each port are
537 * initialized. Allocates space for PRD table if the device
538 * is DMA capable SFF.
540 * May be used as the port_start() entry in ata_port_operations.
543 * Inherited from caller.
546 int ata_sff_port_start(struct ata_port
*ap
)
548 if (ap
->ioaddr
.bmdma_addr
)
549 return ata_port_start(ap
);
555 static int ata_resources_present(struct pci_dev
*pdev
, int port
)
559 /* Check the PCI resources for this channel are enabled */
561 for (i
= 0; i
< 2; i
++) {
562 if (pci_resource_start(pdev
, port
+ i
) == 0 ||
563 pci_resource_len(pdev
, port
+ i
) == 0)
570 * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
571 * @host: target ATA host
573 * Acquire PCI BMDMA resources and initialize @host accordingly.
576 * Inherited from calling layer (may sleep).
579 * 0 on success, -errno otherwise.
581 int ata_pci_init_bmdma(struct ata_host
*host
)
583 struct device
*gdev
= host
->dev
;
584 struct pci_dev
*pdev
= to_pci_dev(gdev
);
587 /* No BAR4 allocation: No DMA */
588 if (pci_resource_start(pdev
, 4) == 0)
591 /* TODO: If we get no DMA mask we should fall back to PIO */
592 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
595 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
599 /* request and iomap DMA region */
600 rc
= pcim_iomap_regions(pdev
, 1 << 4, dev_driver_string(gdev
));
602 dev_printk(KERN_ERR
, gdev
, "failed to request/iomap BAR4\n");
605 host
->iomap
= pcim_iomap_table(pdev
);
607 for (i
= 0; i
< 2; i
++) {
608 struct ata_port
*ap
= host
->ports
[i
];
609 void __iomem
*bmdma
= host
->iomap
[4] + 8 * i
;
611 if (ata_port_is_dummy(ap
))
614 ap
->ioaddr
.bmdma_addr
= bmdma
;
615 if ((!(ap
->flags
& ATA_FLAG_IGN_SIMPLEX
)) &&
616 (ioread8(bmdma
+ 2) & 0x80))
617 host
->flags
|= ATA_HOST_SIMPLEX
;
619 ata_port_desc(ap
, "bmdma 0x%llx",
620 (unsigned long long)pci_resource_start(pdev
, 4) + 8 * i
);
627 * ata_pci_init_sff_host - acquire native PCI ATA resources and init host
628 * @host: target ATA host
630 * Acquire native PCI ATA resources for @host and initialize the
631 * first two ports of @host accordingly. Ports marked dummy are
632 * skipped and allocation failure makes the port dummy.
634 * Note that native PCI resources are valid even for legacy hosts
635 * as we fix up pdev resources array early in boot, so this
636 * function can be used for both native and legacy SFF hosts.
639 * Inherited from calling layer (may sleep).
642 * 0 if at least one port is initialized, -ENODEV if no port is
645 int ata_pci_init_sff_host(struct ata_host
*host
)
647 struct device
*gdev
= host
->dev
;
648 struct pci_dev
*pdev
= to_pci_dev(gdev
);
649 unsigned int mask
= 0;
652 /* request, iomap BARs and init port addresses accordingly */
653 for (i
= 0; i
< 2; i
++) {
654 struct ata_port
*ap
= host
->ports
[i
];
656 void __iomem
* const *iomap
;
658 if (ata_port_is_dummy(ap
))
661 /* Discard disabled ports. Some controllers show
662 * their unused channels this way. Disabled ports are
665 if (!ata_resources_present(pdev
, i
)) {
666 ap
->ops
= &ata_dummy_port_ops
;
670 rc
= pcim_iomap_regions(pdev
, 0x3 << base
,
671 dev_driver_string(gdev
));
673 dev_printk(KERN_WARNING
, gdev
,
674 "failed to request/iomap BARs for port %d "
675 "(errno=%d)\n", i
, rc
);
677 pcim_pin_device(pdev
);
678 ap
->ops
= &ata_dummy_port_ops
;
681 host
->iomap
= iomap
= pcim_iomap_table(pdev
);
683 ap
->ioaddr
.cmd_addr
= iomap
[base
];
684 ap
->ioaddr
.altstatus_addr
=
685 ap
->ioaddr
.ctl_addr
= (void __iomem
*)
686 ((unsigned long)iomap
[base
+ 1] | ATA_PCI_CTL_OFS
);
687 ata_std_ports(&ap
->ioaddr
);
689 ata_port_desc(ap
, "cmd 0x%llx ctl 0x%llx",
690 (unsigned long long)pci_resource_start(pdev
, base
),
691 (unsigned long long)pci_resource_start(pdev
, base
+ 1));
697 dev_printk(KERN_ERR
, gdev
, "no available native port\n");
705 * ata_pci_prepare_sff_host - helper to prepare native PCI ATA host
706 * @pdev: target PCI device
707 * @ppi: array of port_info, must be enough for two ports
708 * @r_host: out argument for the initialized ATA host
710 * Helper to allocate ATA host for @pdev, acquire all native PCI
711 * resources and initialize it accordingly in one go.
714 * Inherited from calling layer (may sleep).
717 * 0 on success, -errno otherwise.
719 int ata_pci_prepare_sff_host(struct pci_dev
*pdev
,
720 const struct ata_port_info
* const * ppi
,
721 struct ata_host
**r_host
)
723 struct ata_host
*host
;
726 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
))
729 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, 2);
731 dev_printk(KERN_ERR
, &pdev
->dev
,
732 "failed to allocate ATA host\n");
737 rc
= ata_pci_init_sff_host(host
);
741 /* init DMA related stuff */
742 rc
= ata_pci_init_bmdma(host
);
746 devres_remove_group(&pdev
->dev
, NULL
);
751 /* This is necessary because PCI and iomap resources are
752 * merged and releasing the top group won't release the
753 * acquired resources if some of those have been acquired
754 * before entering this function.
756 pcim_iounmap_regions(pdev
, 0xf);
758 devres_release_group(&pdev
->dev
, NULL
);
763 * ata_pci_activate_sff_host - start SFF host, request IRQ and register it
764 * @host: target SFF ATA host
765 * @irq_handler: irq_handler used when requesting IRQ(s)
766 * @sht: scsi_host_template to use when registering the host
768 * This is the counterpart of ata_host_activate() for SFF ATA
769 * hosts. This separate helper is necessary because SFF hosts
770 * use two separate interrupts in legacy mode.
773 * Inherited from calling layer (may sleep).
776 * 0 on success, -errno otherwise.
778 int ata_pci_activate_sff_host(struct ata_host
*host
,
779 irq_handler_t irq_handler
,
780 struct scsi_host_template
*sht
)
782 struct device
*dev
= host
->dev
;
783 struct pci_dev
*pdev
= to_pci_dev(dev
);
784 const char *drv_name
= dev_driver_string(host
->dev
);
785 int legacy_mode
= 0, rc
;
787 rc
= ata_host_start(host
);
791 if ((pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
794 /* TODO: What if one channel is in native mode ... */
795 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
796 mask
= (1 << 2) | (1 << 0);
797 if ((tmp8
& mask
) != mask
)
799 #if defined(CONFIG_NO_ATA_LEGACY)
800 /* Some platforms with PCI limits cannot address compat
801 port space. In that case we punt if their firmware has
802 left a device in compatibility mode */
804 printk(KERN_ERR
"ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
810 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
813 if (!legacy_mode
&& pdev
->irq
) {
814 rc
= devm_request_irq(dev
, pdev
->irq
, irq_handler
,
815 IRQF_SHARED
, drv_name
, host
);
819 ata_port_desc(host
->ports
[0], "irq %d", pdev
->irq
);
820 ata_port_desc(host
->ports
[1], "irq %d", pdev
->irq
);
821 } else if (legacy_mode
) {
822 if (!ata_port_is_dummy(host
->ports
[0])) {
823 rc
= devm_request_irq(dev
, ATA_PRIMARY_IRQ(pdev
),
824 irq_handler
, IRQF_SHARED
,
829 ata_port_desc(host
->ports
[0], "irq %d",
830 ATA_PRIMARY_IRQ(pdev
));
833 if (!ata_port_is_dummy(host
->ports
[1])) {
834 rc
= devm_request_irq(dev
, ATA_SECONDARY_IRQ(pdev
),
835 irq_handler
, IRQF_SHARED
,
840 ata_port_desc(host
->ports
[1], "irq %d",
841 ATA_SECONDARY_IRQ(pdev
));
845 rc
= ata_host_register(host
, sht
);
848 devres_remove_group(dev
, NULL
);
850 devres_release_group(dev
, NULL
);
856 * ata_pci_init_one - Initialize/register PCI IDE host controller
857 * @pdev: Controller to be initialized
858 * @ppi: array of port_info, must be enough for two ports
860 * This is a helper function which can be called from a driver's
861 * xxx_init_one() probe function if the hardware uses traditional
862 * IDE taskfile registers.
864 * This function calls pci_enable_device(), reserves its register
865 * regions, sets the dma mask, enables bus master mode, and calls
869 * Nobody makes a single channel controller that appears solely as
870 * the secondary legacy port on PCI.
873 * Inherited from PCI layer (may sleep).
876 * Zero on success, negative on errno-based value on error.
878 int ata_pci_init_one(struct pci_dev
*pdev
,
879 const struct ata_port_info
* const * ppi
)
881 struct device
*dev
= &pdev
->dev
;
882 const struct ata_port_info
*pi
= NULL
;
883 struct ata_host
*host
= NULL
;
888 /* look up the first valid port_info */
889 for (i
= 0; i
< 2 && ppi
[i
]; i
++) {
890 if (ppi
[i
]->port_ops
!= &ata_dummy_port_ops
) {
897 dev_printk(KERN_ERR
, &pdev
->dev
,
898 "no valid port_info specified\n");
902 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
905 rc
= pcim_enable_device(pdev
);
909 /* prepare and activate SFF host */
910 rc
= ata_pci_prepare_sff_host(pdev
, ppi
, &host
);
914 pci_set_master(pdev
);
915 rc
= ata_pci_activate_sff_host(host
, pi
->port_ops
->irq_handler
,
919 devres_remove_group(&pdev
->dev
, NULL
);
921 devres_release_group(&pdev
->dev
, NULL
);
927 * ata_pci_clear_simplex - attempt to kick device out of simplex
930 * Some PCI ATA devices report simplex mode but in fact can be told to
931 * enter non simplex mode. This implements the necessary logic to
932 * perform the task on such devices. Calling it on other devices will
933 * have -undefined- behaviour.
936 int ata_pci_clear_simplex(struct pci_dev
*pdev
)
938 unsigned long bmdma
= pci_resource_start(pdev
, 4);
944 simplex
= inb(bmdma
+ 0x02);
945 outb(simplex
& 0x60, bmdma
+ 0x02);
946 simplex
= inb(bmdma
+ 0x02);
952 unsigned long ata_pci_default_filter(struct ata_device
*adev
, unsigned long xfer_mask
)
954 /* Filter out DMA modes if the device has been configured by
955 the BIOS as PIO only */
957 if (adev
->link
->ap
->ioaddr
.bmdma_addr
== NULL
)
958 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
962 #endif /* CONFIG_PCI */