4 * Synopsys DesignWare Cores (DWC) SATA host driver
6 * Author: Mark Miesfeld <mmiesfeld@amcc.com>
8 * Ported from 2.6.19.2 to 2.6.25/26 by Stefan Roese <sr@denx.de>
9 * Copyright 2008 DENX Software Engineering
11 * Based on versions provided by AMCC and Synopsys which are:
12 * Copyright 2006 Applied Micro Circuits Corporation
13 * COPYRIGHT (C) 2005 SYNOPSYS, INC. ALL RIGHTS RESERVED
15 * SPDX-License-Identifier: GPL-2.0+
18 * SATA support based on the chip canyonlands.
21 * The local version of this driver for the canyonlands board
22 * does not use interrupts but polls the chip instead.
28 #include <asm/processor.h>
29 #include <asm/errno.h>
34 #include <linux/ctype.h>
38 #define DMA_NUM_CHANS 1
39 #define DMA_NUM_CHAN_REGS 8
41 #define AHB_DMA_BRST_DFLT 16
48 struct dma_chan_regs
{
55 struct dmareg sstatar
;
56 struct dmareg dstatar
;
62 struct dma_interrupt_regs
{
65 struct dmareg srctran
;
66 struct dmareg dsttran
;
71 struct dma_chan_regs chan_regs
[DMA_NUM_CHAN_REGS
];
72 struct dma_interrupt_regs interrupt_raw
;
73 struct dma_interrupt_regs interrupt_status
;
74 struct dma_interrupt_regs interrupt_mask
;
75 struct dma_interrupt_regs interrupt_clear
;
76 struct dmareg statusInt
;
77 struct dmareg rq_srcreg
;
78 struct dmareg rq_dstreg
;
79 struct dmareg rq_sgl_srcreg
;
80 struct dmareg rq_sgl_dstreg
;
81 struct dmareg rq_lst_srcreg
;
82 struct dmareg rq_lst_dstreg
;
83 struct dmareg dma_cfg
;
84 struct dmareg dma_chan_en
;
86 struct dmareg dma_test
;
90 * Param 6 = dma_param[0], Param 5 = dma_param[1],
91 * Param 4 = dma_param[2] ...
93 struct dmareg dma_params
[6];
96 #define DMA_EN 0x00000001
97 #define DMA_DI 0x00000000
98 #define DMA_CHANNEL(ch) (0x00000001 << (ch))
99 #define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
100 ((0x000000001 << (ch)) << 8))
101 #define DMA_DISABLE_CHAN(ch) (0x00000000 | \
102 ((0x000000001 << (ch)) << 8))
104 #define SATA_DWC_MAX_PORTS 1
105 #define SATA_DWC_SCR_OFFSET 0x24
106 #define SATA_DWC_REG_OFFSET 0x64
108 struct sata_dwc_regs
{
138 #define SATA_DWC_TXFIFO_DEPTH 0x01FF
139 #define SATA_DWC_RXFIFO_DEPTH 0x01FF
141 #define SATA_DWC_DBTSR_MWR(size) ((size / 4) & SATA_DWC_TXFIFO_DEPTH)
142 #define SATA_DWC_DBTSR_MRD(size) (((size / 4) & \
143 SATA_DWC_RXFIFO_DEPTH) << 16)
144 #define SATA_DWC_INTPR_DMAT 0x00000001
145 #define SATA_DWC_INTPR_NEWFP 0x00000002
146 #define SATA_DWC_INTPR_PMABRT 0x00000004
147 #define SATA_DWC_INTPR_ERR 0x00000008
148 #define SATA_DWC_INTPR_NEWBIST 0x00000010
149 #define SATA_DWC_INTPR_IPF 0x10000000
150 #define SATA_DWC_INTMR_DMATM 0x00000001
151 #define SATA_DWC_INTMR_NEWFPM 0x00000002
152 #define SATA_DWC_INTMR_PMABRTM 0x00000004
153 #define SATA_DWC_INTMR_ERRM 0x00000008
154 #define SATA_DWC_INTMR_NEWBISTM 0x00000010
156 #define SATA_DWC_DMACR_TMOD_TXCHEN 0x00000004
157 #define SATA_DWC_DMACR_TXRXCH_CLEAR SATA_DWC_DMACR_TMOD_TXCHEN
159 #define SATA_DWC_QCMD_MAX 32
161 #define SATA_DWC_SERROR_ERR_BITS 0x0FFF0F03
163 #define HSDEVP_FROM_AP(ap) (struct sata_dwc_device_port*) \
166 struct sata_dwc_device
{
168 struct ata_probe_ent
*pe
;
169 struct ata_host
*host
;
171 struct sata_dwc_regs
*sata_dwc_regs
;
175 struct sata_dwc_device_port
{
176 struct sata_dwc_device
*hsdev
;
177 int cmd_issued
[SATA_DWC_QCMD_MAX
];
178 u32 dma_chan
[SATA_DWC_QCMD_MAX
];
179 int dma_pending
[SATA_DWC_QCMD_MAX
];
183 SATA_DWC_CMD_ISSUED_NOT
= 0,
184 SATA_DWC_CMD_ISSUED_PEND
= 1,
185 SATA_DWC_CMD_ISSUED_EXEC
= 2,
186 SATA_DWC_CMD_ISSUED_NODATA
= 3,
188 SATA_DWC_DMA_PENDING_NONE
= 0,
189 SATA_DWC_DMA_PENDING_TX
= 1,
190 SATA_DWC_DMA_PENDING_RX
= 2,
193 #define msleep(a) udelay(a * 1000)
194 #define ssleep(a) msleep(a * 1000)
196 static int ata_probe_timeout
= (ATA_TMOUT_INTERNAL
/ 100);
198 enum sata_dev_state
{
204 enum sata_dev_state dev_state
= SATA_INIT
;
206 static struct ahb_dma_regs
*sata_dma_regs
= 0;
207 static struct ata_host
*phost
;
208 static struct ata_port ap
;
209 static struct ata_port
*pap
= &ap
;
210 static struct ata_device ata_device
;
211 static struct sata_dwc_device_port dwc_devp
;
213 static void *scr_addr_sstatus
;
214 static u32 temp_n_block
= 0;
216 static unsigned ata_exec_internal(struct ata_device
*dev
,
217 struct ata_taskfile
*tf
, const u8
*cdb
,
218 int dma_dir
, unsigned int buflen
,
219 unsigned long timeout
);
220 static unsigned int ata_dev_set_feature(struct ata_device
*dev
,
221 u8 enable
,u8 feature
);
222 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
223 u16 heads
, u16 sectors
);
224 static u8
ata_irq_on(struct ata_port
*ap
);
225 static struct ata_queued_cmd
*__ata_qc_from_tag(struct ata_port
*ap
,
227 static int ata_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
228 u8 status
, int in_wq
);
229 static void ata_tf_to_host(struct ata_port
*ap
,
230 const struct ata_taskfile
*tf
);
231 static void ata_exec_command(struct ata_port
*ap
,
232 const struct ata_taskfile
*tf
);
233 static unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
);
234 static u8
ata_check_altstatus(struct ata_port
*ap
);
235 static u8
ata_check_status(struct ata_port
*ap
);
236 static void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
237 unsigned int wait
, unsigned int can_sleep
);
238 static void ata_qc_issue(struct ata_queued_cmd
*qc
);
239 static void ata_tf_load(struct ata_port
*ap
,
240 const struct ata_taskfile
*tf
);
241 static int ata_dev_read_sectors(unsigned char* pdata
,
242 unsigned long datalen
, u32 block
, u32 n_block
);
243 static int ata_dev_write_sectors(unsigned char* pdata
,
244 unsigned long datalen
, u32 block
, u32 n_block
);
245 static void ata_std_dev_select(struct ata_port
*ap
, unsigned int device
);
246 static void ata_qc_complete(struct ata_queued_cmd
*qc
);
247 static void __ata_qc_complete(struct ata_queued_cmd
*qc
);
248 static void fill_result_tf(struct ata_queued_cmd
*qc
);
249 static void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
);
250 static void ata_mmio_data_xfer(struct ata_device
*dev
,
252 unsigned int buflen
,int do_write
);
253 static void ata_pio_task(struct ata_port
*arg_ap
);
254 static void __ata_port_freeze(struct ata_port
*ap
);
255 static int ata_port_freeze(struct ata_port
*ap
);
256 static void ata_qc_free(struct ata_queued_cmd
*qc
);
257 static void ata_pio_sectors(struct ata_queued_cmd
*qc
);
258 static void ata_pio_sector(struct ata_queued_cmd
*qc
);
259 static void ata_pio_queue_task(struct ata_port
*ap
,
260 void *data
,unsigned long delay
);
261 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
);
262 static int sata_dwc_softreset(struct ata_port
*ap
);
263 static int ata_dev_read_id(struct ata_device
*dev
, unsigned int *p_class
,
264 unsigned int flags
, u16
*id
);
265 static int check_sata_dev_state(void);
267 static const struct ata_port_info sata_dwc_port_info
[] = {
269 .flags
= ATA_FLAG_SATA
| ATA_FLAG_NO_LEGACY
|
270 ATA_FLAG_MMIO
| ATA_FLAG_PIO_POLLING
|
271 ATA_FLAG_SRST
| ATA_FLAG_NCQ
,
278 int init_sata(int dev
)
280 struct sata_dwc_device hsdev
;
281 struct ata_host host
;
282 struct ata_port_info pi
= sata_dwc_port_info
[0];
283 struct ata_link
*link
;
284 struct sata_dwc_device_port hsdevp
= dwc_devp
;
286 u8
*sata_dma_regs_addr
= 0;
288 unsigned long base_addr
= 0;
295 base
= (u8
*)SATA_BASE_ADDR
;
297 hsdev
.sata_dwc_regs
= (void *__iomem
)(base
+ SATA_DWC_REG_OFFSET
);
299 host
.n_ports
= SATA_DWC_MAX_PORTS
;
301 for (i
= 0; i
< SATA_DWC_MAX_PORTS
; i
++) {
302 ap
.pflags
|= ATA_PFLAG_INITIALIZING
;
303 ap
.flags
= ATA_FLAG_DISABLED
;
305 ap
.ctl
= ATA_DEVCTL_OBS
;
312 link
->active_tag
= ATA_TAG_POISON
;
313 link
->hw_sata_spd_limit
= 0;
319 ap
.pio_mask
= pi
.pio_mask
;
320 ap
.mwdma_mask
= pi
.mwdma_mask
;
321 ap
.udma_mask
= pi
.udma_mask
;
322 ap
.flags
|= pi
.flags
;
323 ap
.link
.flags
|= pi
.link_flags
;
325 host
.ports
[0]->ioaddr
.cmd_addr
= base
;
326 host
.ports
[0]->ioaddr
.scr_addr
= base
+ SATA_DWC_SCR_OFFSET
;
327 scr_addr_sstatus
= base
+ SATA_DWC_SCR_OFFSET
;
329 base_addr
= (unsigned long)base
;
331 host
.ports
[0]->ioaddr
.cmd_addr
= (void *)base_addr
+ 0x00;
332 host
.ports
[0]->ioaddr
.data_addr
= (void *)base_addr
+ 0x00;
334 host
.ports
[0]->ioaddr
.error_addr
= (void *)base_addr
+ 0x04;
335 host
.ports
[0]->ioaddr
.feature_addr
= (void *)base_addr
+ 0x04;
337 host
.ports
[0]->ioaddr
.nsect_addr
= (void *)base_addr
+ 0x08;
339 host
.ports
[0]->ioaddr
.lbal_addr
= (void *)base_addr
+ 0x0c;
340 host
.ports
[0]->ioaddr
.lbam_addr
= (void *)base_addr
+ 0x10;
341 host
.ports
[0]->ioaddr
.lbah_addr
= (void *)base_addr
+ 0x14;
343 host
.ports
[0]->ioaddr
.device_addr
= (void *)base_addr
+ 0x18;
344 host
.ports
[0]->ioaddr
.command_addr
= (void *)base_addr
+ 0x1c;
345 host
.ports
[0]->ioaddr
.status_addr
= (void *)base_addr
+ 0x1c;
347 host
.ports
[0]->ioaddr
.altstatus_addr
= (void *)base_addr
+ 0x20;
348 host
.ports
[0]->ioaddr
.ctl_addr
= (void *)base_addr
+ 0x20;
350 sata_dma_regs_addr
= (u8
*)SATA_DMA_REG_ADDR
;
351 sata_dma_regs
= (void *__iomem
)sata_dma_regs_addr
;
353 status
= ata_check_altstatus(&ap
);
355 if (status
== 0x7f) {
356 printf("Hard Disk not found.\n");
357 dev_state
= SATA_NODEVICE
;
362 printf("Waiting for device...");
367 status
= ata_check_altstatus(&ap
);
369 if ((status
& ATA_BUSY
) == 0) {
375 if (i
> (ATA_RESET_TIME
* 100)) {
376 printf("** TimeOUT **\n");
378 dev_state
= SATA_NODEVICE
;
382 if ((i
>= 100) && ((i
% 100) == 0))
386 rc
= sata_dwc_softreset(&ap
);
389 printf("sata_dwc : error. soft reset failed\n");
393 for (chan
= 0; chan
< DMA_NUM_CHANS
; chan
++) {
394 out_le32(&(sata_dma_regs
->interrupt_mask
.error
.low
),
395 DMA_DISABLE_CHAN(chan
));
397 out_le32(&(sata_dma_regs
->interrupt_mask
.tfr
.low
),
398 DMA_DISABLE_CHAN(chan
));
401 out_le32(&(sata_dma_regs
->dma_cfg
.low
), DMA_DI
);
403 out_le32(&hsdev
.sata_dwc_regs
->intmr
,
404 SATA_DWC_INTMR_ERRM
|
405 SATA_DWC_INTMR_PMABRTM
);
407 /* Unmask the error bits that should trigger
408 * an error interrupt by setting the error mask register.
410 out_le32(&hsdev
.sata_dwc_regs
->errmr
, SATA_DWC_SERROR_ERR_BITS
);
412 hsdev
.host
= ap
.host
;
413 memset(&hsdevp
, 0, sizeof(hsdevp
));
414 hsdevp
.hsdev
= &hsdev
;
416 for (i
= 0; i
< SATA_DWC_QCMD_MAX
; i
++)
417 hsdevp
.cmd_issued
[i
] = SATA_DWC_CMD_ISSUED_NOT
;
419 out_le32((void __iomem
*)scr_addr_sstatus
+ 4,
420 in_le32((void __iomem
*)scr_addr_sstatus
+ 4));
426 static u8
ata_check_altstatus(struct ata_port
*ap
)
429 val
= readb(ap
->ioaddr
.altstatus_addr
);
433 static int sata_dwc_softreset(struct ata_port
*ap
)
437 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
439 in_le32((void *)ap
->ioaddr
.scr_addr
+ (SCR_ERROR
* 4));
441 writeb(0x55, ioaddr
->nsect_addr
);
442 writeb(0xaa, ioaddr
->lbal_addr
);
443 writeb(0xaa, ioaddr
->nsect_addr
);
444 writeb(0x55, ioaddr
->lbal_addr
);
445 writeb(0x55, ioaddr
->nsect_addr
);
446 writeb(0xaa, ioaddr
->lbal_addr
);
448 nsect
= readb(ioaddr
->nsect_addr
);
449 lbal
= readb(ioaddr
->lbal_addr
);
451 if ((nsect
== 0x55) && (lbal
== 0xaa)) {
452 printf("Device found\n");
454 printf("No device found\n");
455 dev_state
= SATA_NODEVICE
;
459 tmp
= ATA_DEVICE_OBS
;
460 writeb(tmp
, ioaddr
->device_addr
);
461 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
465 writeb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
468 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
471 ata_check_status(ap
);
474 ata_check_status(ap
);
477 u8 status
= ata_check_status(ap
);
479 if (!(status
& ATA_BUSY
))
482 printf("Hard Disk status is BUSY.\n");
486 tmp
= ATA_DEVICE_OBS
;
487 writeb(tmp
, ioaddr
->device_addr
);
489 nsect
= readb(ioaddr
->nsect_addr
);
490 lbal
= readb(ioaddr
->lbal_addr
);
495 static u8
ata_check_status(struct ata_port
*ap
)
498 val
= readb(ap
->ioaddr
.status_addr
);
502 static int ata_id_has_hipm(const u16
*id
)
506 if (val
== 0 || val
== 0xffff)
509 return val
& (1 << 9);
512 static int ata_id_has_dipm(const u16
*id
)
516 if (val
== 0 || val
== 0xffff)
519 return val
& (1 << 3);
522 int scan_sata(int dev
)
528 struct ata_device
*ata_dev
= &ata_device
;
529 unsigned long pio_mask
, mwdma_mask
;
531 u16 iobuf
[ATA_SECTOR_WORDS
];
533 memset(iobuf
, 0, sizeof(iobuf
));
535 if (dev_state
== SATA_NODEVICE
)
538 printf("Waiting for device...");
543 status
= ata_check_altstatus(&ap
);
545 if ((status
& ATA_BUSY
) == 0) {
551 if (i
> (ATA_RESET_TIME
* 100)) {
552 printf("** TimeOUT **\n");
554 dev_state
= SATA_NODEVICE
;
557 if ((i
>= 100) && ((i
% 100) == 0))
563 rc
= ata_dev_read_id(ata_dev
, &ata_dev
->class,
564 ATA_READID_POSTRESET
,ata_dev
->id
);
566 printf("sata_dwc : error. failed sata scan\n");
570 /* SATA drives indicate we have a bridge. We don't know which
571 * end of the link the bridge is which is a problem
573 if (ata_id_is_sata(ata_dev
->id
))
574 ap
.cbl
= ATA_CBL_SATA
;
578 ata_dev
->flags
&= ~ATA_DFLAG_CFG_MASK
;
579 ata_dev
->max_sectors
= 0;
580 ata_dev
->cdb_len
= 0;
581 ata_dev
->n_sectors
= 0;
582 ata_dev
->cylinders
= 0;
584 ata_dev
->sectors
= 0;
586 if (id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
587 pio_mask
= id
[ATA_ID_PIO_MODES
] & 0x03;
591 /* If word 64 isn't valid then Word 51 high byte holds
592 * the PIO timing number for the maximum. Turn it into
595 u8 mode
= (id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF;
597 pio_mask
= (2 << mode
) - 1;
603 mwdma_mask
= id
[ATA_ID_MWDMA_MODES
] & 0x07;
605 if (ata_id_is_cfa(id
)) {
606 int pio
= id
[163] & 0x7;
607 int dma
= (id
[163] >> 3) & 7;
610 pio_mask
|= (1 << 5);
612 pio_mask
|= (1 << 6);
614 mwdma_mask
|= (1 << 3);
616 mwdma_mask
|= (1 << 4);
619 if (ata_dev
->class == ATA_DEV_ATA
) {
620 if (ata_id_is_cfa(id
)) {
622 printf("supports DRM functions and may "
623 "not be fully accessable.\n");
624 sprintf(revbuf
, "%s", "CFA");
626 if (ata_id_has_tpm(id
))
627 printf("supports DRM functions and may "
628 "not be fully accessable.\n");
631 ata_dev
->n_sectors
= ata_id_n_sectors((u16
*)id
);
633 if (ata_dev
->id
[59] & 0x100)
634 ata_dev
->multi_count
= ata_dev
->id
[59] & 0xff;
636 if (ata_id_has_lba(id
)) {
639 ata_dev
->flags
|= ATA_DFLAG_LBA
;
640 if (ata_id_has_lba48(id
)) {
641 ata_dev
->flags
|= ATA_DFLAG_LBA48
;
643 if (ata_dev
->n_sectors
>= (1UL << 28) &&
644 ata_id_has_flush_ext(id
))
645 ata_dev
->flags
|= ATA_DFLAG_FLUSH_EXT
;
647 if (!ata_id_has_ncq(ata_dev
->id
))
650 if (ata_dev
->horkage
& ATA_HORKAGE_NONCQ
)
651 sprintf(ncq_desc
, "%s", "NCQ (not used)");
653 if (ap
.flags
& ATA_FLAG_NCQ
)
654 ata_dev
->flags
|= ATA_DFLAG_NCQ
;
656 ata_dev
->cdb_len
= 16;
658 ata_dev
->max_sectors
= ATA_MAX_SECTORS
;
659 if (ata_dev
->flags
& ATA_DFLAG_LBA48
)
660 ata_dev
->max_sectors
= ATA_MAX_SECTORS_LBA48
;
662 if (!(ata_dev
->horkage
& ATA_HORKAGE_IPM
)) {
663 if (ata_id_has_hipm(ata_dev
->id
))
664 ata_dev
->flags
|= ATA_DFLAG_HIPM
;
665 if (ata_id_has_dipm(ata_dev
->id
))
666 ata_dev
->flags
|= ATA_DFLAG_DIPM
;
669 if ((ap
.cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ata_dev
->id
))) {
670 ata_dev
->udma_mask
&= ATA_UDMA5
;
671 ata_dev
->max_sectors
= ATA_MAX_SECTORS
;
674 if (ata_dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
) {
675 printf("Drive reports diagnostics failure."
676 "This may indicate a drive\n");
677 printf("fault or invalid emulation."
678 "Contact drive vendor for information.\n");
681 rc
= check_sata_dev_state();
683 ata_id_c_string(ata_dev
->id
,
684 (unsigned char *)sata_dev_desc
[dev
].revision
,
685 ATA_ID_FW_REV
, sizeof(sata_dev_desc
[dev
].revision
));
686 ata_id_c_string(ata_dev
->id
,
687 (unsigned char *)sata_dev_desc
[dev
].vendor
,
688 ATA_ID_PROD
, sizeof(sata_dev_desc
[dev
].vendor
));
689 ata_id_c_string(ata_dev
->id
,
690 (unsigned char *)sata_dev_desc
[dev
].product
,
691 ATA_ID_SERNO
, sizeof(sata_dev_desc
[dev
].product
));
693 sata_dev_desc
[dev
].lba
= (u32
) ata_dev
->n_sectors
;
696 if (ata_dev
->id
[83] & (1 << 10)) {
697 sata_dev_desc
[dev
].lba48
= 1;
699 sata_dev_desc
[dev
].lba48
= 0;
706 static u8
ata_busy_wait(struct ata_port
*ap
,
707 unsigned int bits
,unsigned int max
)
713 status
= ata_check_status(ap
);
715 } while (status
!= 0xff && (status
& bits
) && (max
> 0));
720 static int ata_dev_read_id(struct ata_device
*dev
, unsigned int *p_class
,
721 unsigned int flags
, u16
*id
)
723 struct ata_port
*ap
= pap
;
724 unsigned int class = *p_class
;
725 struct ata_taskfile tf
;
726 unsigned int err_mask
= 0;
728 int may_fallback
= 1, tried_spinup
= 0;
732 status
= ata_busy_wait(ap
, ATA_BUSY
, 30000);
733 if (status
& ATA_BUSY
) {
734 printf("BSY = 0 check. timeout.\n");
739 ata_dev_select(ap
, dev
->devno
, 1, 1);
742 memset(&tf
, 0, sizeof(tf
));
744 ap
->flags
&= ~ATA_FLAG_DISABLED
;
746 tf
.device
= ATA_DEVICE_OBS
;
747 tf
.command
= ATA_CMD_ID_ATA
;
748 tf
.protocol
= ATA_PROT_PIO
;
750 /* Some devices choke if TF registers contain garbage. Make
751 * sure those are properly initialized.
753 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
755 /* Device presence detection is unreliable on some
756 * controllers. Always poll IDENTIFY if available.
758 tf
.flags
|= ATA_TFLAG_POLLING
;
762 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
763 sizeof(id
[0]) * ATA_ID_WORDS
, 0);
766 if (err_mask
& AC_ERR_NODEV_HINT
) {
767 printf("NODEV after polling detection\n");
771 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
772 /* Device or controller might have reported
773 * the wrong device class. Give a shot at the
774 * other IDENTIFY if the current one is
775 * aborted by the device.
780 if (class == ATA_DEV_ATA
) {
781 class = ATA_DEV_ATAPI
;
787 /* Control reaches here iff the device aborted
788 * both flavors of IDENTIFYs which happens
789 * sometimes with phantom devices.
791 printf("both IDENTIFYs aborted, assuming NODEV\n");
795 reason
= "I/O error";
799 /* Falling back doesn't make sense if ID data was read
800 * successfully at least once.
806 for (id_cnt
= 0; id_cnt
< ATA_ID_WORDS
; id_cnt
++)
807 id
[id_cnt
] = le16_to_cpu(id
[id_cnt
]);
811 reason
= "device reports invalid type";
813 if (class == ATA_DEV_ATA
) {
814 if (!ata_id_is_ata(id
) && !ata_id_is_cfa(id
))
817 if (ata_id_is_ata(id
))
820 if (!tried_spinup
&& (id
[2] == 0x37c8 || id
[2] == 0x738c)) {
823 * Drive powered-up in standby mode, and requires a specific
824 * SET_FEATURES spin-up subcommand before it will accept
825 * anything other than the original IDENTIFY command.
827 err_mask
= ata_dev_set_feature(dev
, SETFEATURES_SPINUP
, 0);
828 if (err_mask
&& id
[2] != 0x738c) {
830 reason
= "SPINUP failed";
834 * If the drive initially returned incomplete IDENTIFY info,
835 * we now must reissue the IDENTIFY command.
841 if ((flags
& ATA_READID_POSTRESET
) && class == ATA_DEV_ATA
) {
843 * The exact sequence expected by certain pre-ATA4 drives is:
845 * IDENTIFY (optional in early ATA)
846 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
848 * Some drives were very specific about that exact sequence.
850 * Note that ATA4 says lba is mandatory so the second check
851 * shoud never trigger.
853 if (ata_id_major_version(id
) < 4 || !ata_id_has_lba(id
)) {
854 err_mask
= ata_dev_init_params(dev
, id
[3], id
[6]);
857 reason
= "INIT_DEV_PARAMS failed";
861 /* current CHS translation info (id[53-58]) might be
862 * changed. reread the identify device info.
864 flags
&= ~ATA_READID_POSTRESET
;
873 printf("failed to READ ID (%s, err_mask=0x%x)\n", reason
, err_mask
);
877 static u8
ata_wait_idle(struct ata_port
*ap
)
879 u8 status
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 1000);
883 static void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
884 unsigned int wait
, unsigned int can_sleep
)
889 ata_std_dev_select(ap
, device
);
895 static void ata_std_dev_select(struct ata_port
*ap
, unsigned int device
)
900 tmp
= ATA_DEVICE_OBS
;
902 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
905 writeb(tmp
, ap
->ioaddr
.device_addr
);
907 readb(ap
->ioaddr
.altstatus_addr
);
912 static int waiting_for_reg_state(volatile u8
*offset
,
919 for (i
= 0; i
< timeout_msec
; i
++) {
920 status
= readl(offset
);
921 if ((status
& sign
) != 0)
926 return (i
< timeout_msec
) ? 0 : -1;
929 static void ata_qc_reinit(struct ata_queued_cmd
*qc
)
931 qc
->dma_dir
= DMA_NONE
;
933 qc
->nbytes
= qc
->extrabytes
= qc
->curbytes
= 0;
936 qc
->sect_size
= ATA_SECT_SIZE
;
937 qc
->nbytes
= ATA_SECT_SIZE
* temp_n_block
;
939 memset(&qc
->tf
, 0, sizeof(qc
->tf
));
941 qc
->tf
.device
= ATA_DEVICE_OBS
;
943 qc
->result_tf
.command
= ATA_DRDY
;
944 qc
->result_tf
.feature
= 0;
947 struct ata_queued_cmd
*__ata_qc_from_tag(struct ata_port
*ap
,
950 if (tag
< ATA_MAX_QUEUE
)
951 return &ap
->qcmd
[tag
];
955 static void __ata_port_freeze(struct ata_port
*ap
)
957 printf("set port freeze.\n");
958 ap
->pflags
|= ATA_PFLAG_FROZEN
;
961 static int ata_port_freeze(struct ata_port
*ap
)
963 __ata_port_freeze(ap
);
967 unsigned ata_exec_internal(struct ata_device
*dev
,
968 struct ata_taskfile
*tf
, const u8
*cdb
,
969 int dma_dir
, unsigned int buflen
,
970 unsigned long timeout
)
972 struct ata_link
*link
= dev
->link
;
973 struct ata_port
*ap
= pap
;
974 struct ata_queued_cmd
*qc
;
975 unsigned int tag
, preempted_tag
;
976 u32 preempted_sactive
, preempted_qc_active
;
977 int preempted_nr_active_links
;
978 unsigned int err_mask
;
982 status
= ata_busy_wait(ap
, ATA_BUSY
, 300000);
983 if (status
& ATA_BUSY
) {
984 printf("BSY = 0 check. timeout.\n");
989 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
990 return AC_ERR_SYSTEM
;
992 tag
= ATA_TAG_INTERNAL
;
994 if (test_and_set_bit(tag
, &ap
->qc_allocated
)) {
999 qc
= __ata_qc_from_tag(ap
, tag
);
1006 preempted_tag
= link
->active_tag
;
1007 preempted_sactive
= link
->sactive
;
1008 preempted_qc_active
= ap
->qc_active
;
1009 preempted_nr_active_links
= ap
->nr_active_links
;
1010 link
->active_tag
= ATA_TAG_POISON
;
1013 ap
->nr_active_links
= 0;
1017 memcpy(qc
->cdb
, cdb
, ATAPI_CDB_LEN
);
1018 qc
->flags
|= ATA_QCFLAG_RESULT_TF
;
1019 qc
->dma_dir
= dma_dir
;
1020 qc
->private_data
= 0;
1025 timeout
= ata_probe_timeout
* 1000 / HZ
;
1027 status
= ata_busy_wait(ap
, ATA_BUSY
, 30000);
1028 if (status
& ATA_BUSY
) {
1029 printf("BSY = 0 check. timeout.\n");
1030 printf("altstatus = 0x%x.\n", status
);
1031 qc
->err_mask
|= AC_ERR_OTHER
;
1032 return qc
->err_mask
;
1035 if (waiting_for_reg_state(ap
->ioaddr
.altstatus_addr
, 1000, 0x8)) {
1039 status
= readb(ap
->ioaddr
.altstatus_addr
);
1040 if ((status
& 0x01) != 0) {
1041 errorStatus
= readb(ap
->ioaddr
.feature_addr
);
1042 if (errorStatus
== 0x04 &&
1043 qc
->tf
.command
== ATA_CMD_PIO_READ_EXT
){
1044 printf("Hard Disk doesn't support LBA48\n");
1045 dev_state
= SATA_ERROR
;
1046 qc
->err_mask
|= AC_ERR_OTHER
;
1047 return qc
->err_mask
;
1050 qc
->err_mask
|= AC_ERR_OTHER
;
1051 return qc
->err_mask
;
1054 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
1055 if (status
& ATA_BUSY
) {
1056 printf("BSY = 0 check. timeout.\n");
1057 qc
->err_mask
|= AC_ERR_OTHER
;
1058 return qc
->err_mask
;
1064 if (qc
->flags
& ATA_QCFLAG_ACTIVE
) {
1065 qc
->err_mask
|= AC_ERR_TIMEOUT
;
1066 ata_port_freeze(ap
);
1070 if (qc
->flags
& ATA_QCFLAG_FAILED
) {
1071 if (qc
->result_tf
.command
& (ATA_ERR
| ATA_DF
))
1072 qc
->err_mask
|= AC_ERR_DEV
;
1075 qc
->err_mask
|= AC_ERR_OTHER
;
1077 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1078 qc
->err_mask
&= ~AC_ERR_OTHER
;
1081 *tf
= qc
->result_tf
;
1082 err_mask
= qc
->err_mask
;
1084 link
->active_tag
= preempted_tag
;
1085 link
->sactive
= preempted_sactive
;
1086 ap
->qc_active
= preempted_qc_active
;
1087 ap
->nr_active_links
= preempted_nr_active_links
;
1089 if (ap
->flags
& ATA_FLAG_DISABLED
) {
1090 err_mask
|= AC_ERR_SYSTEM
;
1091 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1097 static void ata_qc_issue(struct ata_queued_cmd
*qc
)
1099 struct ata_port
*ap
= qc
->ap
;
1100 struct ata_link
*link
= qc
->dev
->link
;
1101 u8 prot
= qc
->tf
.protocol
;
1103 if (ata_is_ncq(prot
)) {
1105 ap
->nr_active_links
++;
1106 link
->sactive
|= 1 << qc
->tag
;
1108 ap
->nr_active_links
++;
1109 link
->active_tag
= qc
->tag
;
1112 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
1113 ap
->qc_active
|= 1 << qc
->tag
;
1115 if (qc
->dev
->flags
& ATA_DFLAG_SLEEPING
) {
1120 qc
->err_mask
|= ata_qc_issue_prot(qc
);
1126 ata_qc_complete(qc
);
1129 static unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
1131 struct ata_port
*ap
= qc
->ap
;
1133 if (ap
->flags
& ATA_FLAG_PIO_POLLING
) {
1134 switch (qc
->tf
.protocol
) {
1136 case ATA_PROT_NODATA
:
1137 case ATAPI_PROT_PIO
:
1138 case ATAPI_PROT_NODATA
:
1139 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
1146 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
1148 switch (qc
->tf
.protocol
) {
1150 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1151 qc
->tf
.ctl
|= ATA_NIEN
;
1153 ata_tf_to_host(ap
, &qc
->tf
);
1155 ap
->hsm_task_state
= HSM_ST
;
1157 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1158 ata_pio_queue_task(ap
, qc
, 0);
1163 return AC_ERR_SYSTEM
;
1169 static void ata_tf_to_host(struct ata_port
*ap
,
1170 const struct ata_taskfile
*tf
)
1172 ata_tf_load(ap
, tf
);
1173 ata_exec_command(ap
, tf
);
1176 static void ata_tf_load(struct ata_port
*ap
,
1177 const struct ata_taskfile
*tf
)
1179 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1180 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
1182 if (tf
->ctl
!= ap
->last_ctl
) {
1183 if (ioaddr
->ctl_addr
)
1184 writeb(tf
->ctl
, ioaddr
->ctl_addr
);
1185 ap
->last_ctl
= tf
->ctl
;
1189 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
1190 writeb(tf
->hob_feature
, ioaddr
->feature_addr
);
1191 writeb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
1192 writeb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
1193 writeb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
1194 writeb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
1198 writeb(tf
->feature
, ioaddr
->feature_addr
);
1199 writeb(tf
->nsect
, ioaddr
->nsect_addr
);
1200 writeb(tf
->lbal
, ioaddr
->lbal_addr
);
1201 writeb(tf
->lbam
, ioaddr
->lbam_addr
);
1202 writeb(tf
->lbah
, ioaddr
->lbah_addr
);
1205 if (tf
->flags
& ATA_TFLAG_DEVICE
)
1206 writeb(tf
->device
, ioaddr
->device_addr
);
1211 static void ata_exec_command(struct ata_port
*ap
,
1212 const struct ata_taskfile
*tf
)
1214 writeb(tf
->command
, ap
->ioaddr
.command_addr
);
1216 readb(ap
->ioaddr
.altstatus_addr
);
1221 static void ata_pio_queue_task(struct ata_port
*ap
,
1222 void *data
,unsigned long delay
)
1224 ap
->port_task_data
= data
;
1227 static unsigned int ac_err_mask(u8 status
)
1229 if (status
& (ATA_BUSY
| ATA_DRQ
))
1231 if (status
& (ATA_ERR
| ATA_DF
))
1236 static unsigned int __ac_err_mask(u8 status
)
1238 unsigned int mask
= ac_err_mask(status
);
1240 return AC_ERR_OTHER
;
1244 static void ata_pio_task(struct ata_port
*arg_ap
)
1246 struct ata_port
*ap
= arg_ap
;
1247 struct ata_queued_cmd
*qc
= ap
->port_task_data
;
1253 * This is purely heuristic. This is a fast path.
1254 * Sometimes when we enter, BSY will be cleared in
1255 * a chk-status or two. If not, the drive is probably seeking
1256 * or something. Snooze for a couple msecs, then
1257 * chk-status again. If still busy, queue delayed work.
1259 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
1260 if (status
& ATA_BUSY
) {
1262 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
1263 if (status
& ATA_BUSY
) {
1264 ata_pio_queue_task(ap
, qc
, ATA_SHORT_PAUSE
);
1269 poll_next
= ata_hsm_move(ap
, qc
, status
, 1);
1271 /* another command or interrupt handler
1272 * may be running at this point.
1278 static int ata_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
1279 u8 status
, int in_wq
)
1284 switch (ap
->hsm_task_state
) {
1286 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1288 if ((status
& ATA_DRQ
) == 0) {
1289 if (status
& (ATA_ERR
| ATA_DF
)) {
1290 qc
->err_mask
|= AC_ERR_DEV
;
1292 qc
->err_mask
|= AC_ERR_HSM
;
1294 ap
->hsm_task_state
= HSM_ST_ERR
;
1298 /* Device should not ask for data transfer (DRQ=1)
1299 * when it finds something wrong.
1300 * We ignore DRQ here and stop the HSM by
1301 * changing hsm_task_state to HSM_ST_ERR and
1302 * let the EH abort the command or reset the device.
1304 if (status
& (ATA_ERR
| ATA_DF
)) {
1305 if (!(qc
->dev
->horkage
& ATA_HORKAGE_STUCK_ERR
)) {
1306 printf("DRQ=1 with device error, "
1307 "dev_stat 0x%X\n", status
);
1308 qc
->err_mask
|= AC_ERR_HSM
;
1309 ap
->hsm_task_state
= HSM_ST_ERR
;
1314 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
1315 /* PIO data out protocol.
1316 * send first data block.
1318 /* ata_pio_sectors() might change the state
1319 * to HSM_ST_LAST. so, the state is changed here
1320 * before ata_pio_sectors().
1322 ap
->hsm_task_state
= HSM_ST
;
1323 ata_pio_sectors(qc
);
1325 printf("protocol is not ATA_PROT_PIO \n");
1330 if ((status
& ATA_DRQ
) == 0) {
1331 if (status
& (ATA_ERR
| ATA_DF
)) {
1332 qc
->err_mask
|= AC_ERR_DEV
;
1334 /* HSM violation. Let EH handle this.
1335 * Phantom devices also trigger this
1336 * condition. Mark hint.
1338 qc
->err_mask
|= AC_ERR_HSM
| AC_ERR_NODEV_HINT
;
1341 ap
->hsm_task_state
= HSM_ST_ERR
;
1344 /* For PIO reads, some devices may ask for
1345 * data transfer (DRQ=1) alone with ERR=1.
1346 * We respect DRQ here and transfer one
1347 * block of junk data before changing the
1348 * hsm_task_state to HSM_ST_ERR.
1350 * For PIO writes, ERR=1 DRQ=1 doesn't make
1351 * sense since the data block has been
1352 * transferred to the device.
1354 if (status
& (ATA_ERR
| ATA_DF
)) {
1355 qc
->err_mask
|= AC_ERR_DEV
;
1357 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
1358 ata_pio_sectors(qc
);
1359 status
= ata_wait_idle(ap
);
1362 if (status
& (ATA_BUSY
| ATA_DRQ
))
1363 qc
->err_mask
|= AC_ERR_HSM
;
1365 /* ata_pio_sectors() might change the
1366 * state to HSM_ST_LAST. so, the state
1367 * is changed after ata_pio_sectors().
1369 ap
->hsm_task_state
= HSM_ST_ERR
;
1373 ata_pio_sectors(qc
);
1374 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
1375 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
1376 status
= ata_wait_idle(ap
);
1384 if (!ata_ok(status
)) {
1385 qc
->err_mask
|= __ac_err_mask(status
);
1386 ap
->hsm_task_state
= HSM_ST_ERR
;
1390 ap
->hsm_task_state
= HSM_ST_IDLE
;
1392 ata_hsm_qc_complete(qc
, in_wq
);
1398 /* make sure qc->err_mask is available to
1399 * know what's wrong and recover
1401 ap
->hsm_task_state
= HSM_ST_IDLE
;
1403 ata_hsm_qc_complete(qc
, in_wq
);
1414 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
1416 struct ata_port
*ap
;
1418 qc
->pdata
= ap
->pdata
;
1422 readb(qc
->ap
->ioaddr
.altstatus_addr
);
1426 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
1428 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
1429 struct ata_port
*ap
= qc
->ap
;
1430 unsigned int offset
;
1432 char temp_data_buf
[512];
1434 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
1435 ap
->hsm_task_state
= HSM_ST_LAST
;
1437 offset
= qc
->curbytes
;
1439 switch (qc
->tf
.command
) {
1440 case ATA_CMD_ID_ATA
:
1441 buf
= (unsigned char *)&ata_device
.id
[0];
1443 case ATA_CMD_PIO_READ_EXT
:
1444 case ATA_CMD_PIO_READ
:
1445 case ATA_CMD_PIO_WRITE_EXT
:
1446 case ATA_CMD_PIO_WRITE
:
1447 buf
= qc
->pdata
+ offset
;
1450 buf
= (unsigned char *)&temp_data_buf
[0];
1453 ata_mmio_data_xfer(qc
->dev
, buf
, qc
->sect_size
, do_write
);
1455 qc
->curbytes
+= qc
->sect_size
;
1459 static void ata_mmio_data_xfer(struct ata_device
*dev
, unsigned char *buf
,
1460 unsigned int buflen
, int do_write
)
1462 struct ata_port
*ap
= pap
;
1463 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
1464 unsigned int words
= buflen
>> 1;
1465 u16
*buf16
= (u16
*)buf
;
1470 for (i
= 0; i
< words
; i
++)
1471 writew(le16_to_cpu(buf16
[i
]), data_addr
);
1473 for (i
= 0; i
< words
; i
++)
1474 buf16
[i
] = cpu_to_le16(readw(data_addr
));
1477 if (buflen
& 0x01) {
1478 __le16 align_buf
[1] = { 0 };
1479 unsigned char *trailing_buf
= buf
+ buflen
- 1;
1482 memcpy(align_buf
, trailing_buf
, 1);
1483 writew(le16_to_cpu(align_buf
[0]), data_addr
);
1485 align_buf
[0] = cpu_to_le16(readw(data_addr
));
1486 memcpy(trailing_buf
, align_buf
, 1);
1491 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
1493 struct ata_port
*ap
= qc
->ap
;
1496 /* EH might have kicked in while host lock is
1499 qc
= &ap
->qcmd
[qc
->tag
];
1501 if (!(qc
->err_mask
& AC_ERR_HSM
)) {
1503 ata_qc_complete(qc
);
1505 ata_port_freeze(ap
);
1509 if (!(qc
->err_mask
& AC_ERR_HSM
)) {
1510 ata_qc_complete(qc
);
1512 ata_port_freeze(ap
);
1517 static u8
ata_irq_on(struct ata_port
*ap
)
1519 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1522 ap
->ctl
&= ~ATA_NIEN
;
1523 ap
->last_ctl
= ap
->ctl
;
1525 if (ioaddr
->ctl_addr
)
1526 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
1528 tmp
= ata_wait_idle(ap
);
1533 static unsigned int ata_tag_internal(unsigned int tag
)
1535 return tag
== ATA_MAX_QUEUE
- 1;
1538 static void ata_qc_complete(struct ata_queued_cmd
*qc
)
1540 struct ata_device
*dev
= qc
->dev
;
1542 qc
->flags
|= ATA_QCFLAG_FAILED
;
1544 if (qc
->flags
& ATA_QCFLAG_FAILED
) {
1545 if (!ata_tag_internal(qc
->tag
)) {
1550 if (qc
->flags
& ATA_QCFLAG_RESULT_TF
)
1553 /* Some commands need post-processing after successful
1556 switch (qc
->tf
.command
) {
1557 case ATA_CMD_SET_FEATURES
:
1558 if (qc
->tf
.feature
!= SETFEATURES_WC_ON
&&
1559 qc
->tf
.feature
!= SETFEATURES_WC_OFF
)
1561 case ATA_CMD_INIT_DEV_PARAMS
:
1562 case ATA_CMD_SET_MULTI
:
1566 dev
->flags
|= ATA_DFLAG_SLEEPING
;
1570 __ata_qc_complete(qc
);
1573 static void fill_result_tf(struct ata_queued_cmd
*qc
)
1575 struct ata_port
*ap
= qc
->ap
;
1577 qc
->result_tf
.flags
= qc
->tf
.flags
;
1578 ata_tf_read(ap
, &qc
->result_tf
);
1581 static void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
1583 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1585 tf
->command
= ata_check_status(ap
);
1586 tf
->feature
= readb(ioaddr
->error_addr
);
1587 tf
->nsect
= readb(ioaddr
->nsect_addr
);
1588 tf
->lbal
= readb(ioaddr
->lbal_addr
);
1589 tf
->lbam
= readb(ioaddr
->lbam_addr
);
1590 tf
->lbah
= readb(ioaddr
->lbah_addr
);
1591 tf
->device
= readb(ioaddr
->device_addr
);
1593 if (tf
->flags
& ATA_TFLAG_LBA48
) {
1594 if (ioaddr
->ctl_addr
) {
1595 writeb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
1597 tf
->hob_feature
= readb(ioaddr
->error_addr
);
1598 tf
->hob_nsect
= readb(ioaddr
->nsect_addr
);
1599 tf
->hob_lbal
= readb(ioaddr
->lbal_addr
);
1600 tf
->hob_lbam
= readb(ioaddr
->lbam_addr
);
1601 tf
->hob_lbah
= readb(ioaddr
->lbah_addr
);
1603 writeb(tf
->ctl
, ioaddr
->ctl_addr
);
1604 ap
->last_ctl
= tf
->ctl
;
1606 printf("sata_dwc warnning register read.\n");
1611 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
1613 struct ata_port
*ap
= qc
->ap
;
1614 struct ata_link
*link
= qc
->dev
->link
;
1616 link
->active_tag
= ATA_TAG_POISON
;
1617 ap
->nr_active_links
--;
1619 if (qc
->flags
& ATA_QCFLAG_CLEAR_EXCL
&& ap
->excl_link
== link
)
1620 ap
->excl_link
= NULL
;
1622 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
1623 ap
->qc_active
&= ~(1 << qc
->tag
);
1626 static void ata_qc_free(struct ata_queued_cmd
*qc
)
1628 struct ata_port
*ap
= qc
->ap
;
1632 if (tag
< ATA_MAX_QUEUE
) {
1633 qc
->tag
= ATA_TAG_POISON
;
1634 clear_bit(tag
, &ap
->qc_allocated
);
1638 static int check_sata_dev_state(void)
1640 unsigned long datalen
;
1641 unsigned char *pdata
;
1644 char temp_data_buf
[512];
1649 pdata
= (unsigned char*)&temp_data_buf
[0];
1652 ret
= ata_dev_read_sectors(pdata
, datalen
, 0, 1);
1658 if (i
> (ATA_RESET_TIME
* 100)) {
1659 printf("** TimeOUT **\n");
1660 dev_state
= SATA_NODEVICE
;
1664 if ((i
>= 100) && ((i
% 100) == 0))
1668 dev_state
= SATA_READY
;
1673 static unsigned int ata_dev_set_feature(struct ata_device
*dev
,
1674 u8 enable
, u8 feature
)
1676 struct ata_taskfile tf
;
1677 struct ata_port
*ap
;
1679 unsigned int err_mask
;
1681 memset(&tf
, 0, sizeof(tf
));
1684 tf
.device
= ATA_DEVICE_OBS
;
1685 tf
.command
= ATA_CMD_SET_FEATURES
;
1686 tf
.feature
= enable
;
1687 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1688 tf
.protocol
= ATA_PROT_NODATA
;
1691 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, 0, 0);
1696 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
1697 u16 heads
, u16 sectors
)
1699 struct ata_taskfile tf
;
1700 struct ata_port
*ap
;
1702 unsigned int err_mask
;
1704 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
1705 return AC_ERR_INVALID
;
1707 memset(&tf
, 0, sizeof(tf
));
1709 tf
.device
= ATA_DEVICE_OBS
;
1710 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
1711 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1712 tf
.protocol
= ATA_PROT_NODATA
;
1714 tf
.device
|= (heads
- 1) & 0x0f;
1716 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, 0, 0);
1718 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
1724 #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
1725 #define SATA_MAX_READ_BLK 0xFF
1727 #define SATA_MAX_READ_BLK 0xFFFF
1730 ulong
sata_read(int device
, ulong blknr
, lbaint_t blkcnt
, void *buffer
)
1732 ulong start
,blks
, buf_addr
;
1733 unsigned short smallblks
;
1734 unsigned long datalen
;
1735 unsigned char *pdata
;
1741 if (dev_state
!= SATA_READY
)
1744 buf_addr
= (unsigned long)buffer
;
1748 pdata
= (unsigned char *)buf_addr
;
1749 if (blks
> SATA_MAX_READ_BLK
) {
1750 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_READ_BLK
;
1751 smallblks
= SATA_MAX_READ_BLK
;
1754 n_block
= (u32
)smallblks
;
1756 start
+= SATA_MAX_READ_BLK
;
1757 blks
-= SATA_MAX_READ_BLK
;
1759 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_READ_BLK
;
1760 datalen
= sata_dev_desc
[device
].blksz
* blks
;
1761 smallblks
= (unsigned short)blks
;
1764 n_block
= (u32
)smallblks
;
1770 if (ata_dev_read_sectors(pdata
, datalen
, block
, n_block
) != true) {
1771 printf("sata_dwc : Hard disk read error.\n");
1775 buf_addr
+= datalen
;
1776 } while (blks
!= 0);
1781 static int ata_dev_read_sectors(unsigned char *pdata
, unsigned long datalen
,
1782 u32 block
, u32 n_block
)
1784 struct ata_port
*ap
= pap
;
1785 struct ata_device
*dev
= &ata_device
;
1786 struct ata_taskfile tf
;
1787 unsigned int class = ATA_DEV_ATA
;
1788 unsigned int err_mask
= 0;
1790 int may_fallback
= 1;
1792 if (dev_state
== SATA_ERROR
)
1795 ata_dev_select(ap
, dev
->devno
, 1, 1);
1798 memset(&tf
, 0, sizeof(tf
));
1801 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1805 tf
.device
= ATA_DEVICE_OBS
;
1807 temp_n_block
= n_block
;
1810 tf
.command
= ATA_CMD_PIO_READ_EXT
;
1811 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
;
1813 tf
.hob_feature
= 31;
1815 tf
.hob_nsect
= (n_block
>> 8) & 0xff;
1816 tf
.nsect
= n_block
& 0xff;
1820 tf
.hob_lbal
= (block
>> 24) & 0xff;
1821 tf
.lbah
= (block
>> 16) & 0xff;
1822 tf
.lbam
= (block
>> 8) & 0xff;
1823 tf
.lbal
= block
& 0xff;
1826 if (tf
.flags
& ATA_TFLAG_FUA
)
1827 tf
.device
|= 1 << 7;
1829 tf
.command
= ATA_CMD_PIO_READ
;
1830 tf
.flags
|= ATA_TFLAG_LBA
;
1833 tf
.nsect
= n_block
& 0xff;
1835 tf
.lbah
= (block
>> 16) & 0xff;
1836 tf
.lbam
= (block
>> 8) & 0xff;
1837 tf
.lbal
= block
& 0xff;
1839 tf
.device
= (block
>> 24) & 0xf;
1841 tf
.device
|= 1 << 6;
1842 if (tf
.flags
& ATA_TFLAG_FUA
)
1843 tf
.device
|= 1 << 7;
1847 tf
.protocol
= ATA_PROT_PIO
;
1849 /* Some devices choke if TF registers contain garbage. Make
1850 * sure those are properly initialized.
1852 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1853 tf
.flags
|= ATA_TFLAG_POLLING
;
1855 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
, 0, 0);
1858 if (err_mask
& AC_ERR_NODEV_HINT
) {
1859 printf("READ_SECTORS NODEV after polling detection\n");
1863 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
1864 /* Device or controller might have reported
1865 * the wrong device class. Give a shot at the
1866 * other IDENTIFY if the current one is
1867 * aborted by the device.
1872 if (class == ATA_DEV_ATA
) {
1873 class = ATA_DEV_ATAPI
;
1875 class = ATA_DEV_ATA
;
1879 /* Control reaches here iff the device aborted
1880 * both flavors of IDENTIFYs which happens
1881 * sometimes with phantom devices.
1883 printf("both IDENTIFYs aborted, assuming NODEV\n");
1887 reason
= "I/O error";
1894 printf("failed to READ SECTORS (%s, err_mask=0x%x)\n", reason
, err_mask
);
1898 #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
1899 #define SATA_MAX_WRITE_BLK 0xFF
1901 #define SATA_MAX_WRITE_BLK 0xFFFF
1904 ulong
sata_write(int device
, ulong blknr
, lbaint_t blkcnt
, const void *buffer
)
1906 ulong start
,blks
, buf_addr
;
1907 unsigned short smallblks
;
1908 unsigned long datalen
;
1909 unsigned char *pdata
;
1916 if (dev_state
!= SATA_READY
)
1919 buf_addr
= (unsigned long)buffer
;
1923 pdata
= (unsigned char *)buf_addr
;
1924 if (blks
> SATA_MAX_WRITE_BLK
) {
1925 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_WRITE_BLK
;
1926 smallblks
= SATA_MAX_WRITE_BLK
;
1929 n_block
= (u32
)smallblks
;
1931 start
+= SATA_MAX_WRITE_BLK
;
1932 blks
-= SATA_MAX_WRITE_BLK
;
1934 datalen
= sata_dev_desc
[device
].blksz
* blks
;
1935 smallblks
= (unsigned short)blks
;
1938 n_block
= (u32
)smallblks
;
1944 if (ata_dev_write_sectors(pdata
, datalen
, block
, n_block
) != true) {
1945 printf("sata_dwc : Hard disk read error.\n");
1949 buf_addr
+= datalen
;
1950 } while (blks
!= 0);
1955 static int ata_dev_write_sectors(unsigned char* pdata
, unsigned long datalen
,
1956 u32 block
, u32 n_block
)
1958 struct ata_port
*ap
= pap
;
1959 struct ata_device
*dev
= &ata_device
;
1960 struct ata_taskfile tf
;
1961 unsigned int class = ATA_DEV_ATA
;
1962 unsigned int err_mask
= 0;
1964 int may_fallback
= 1;
1966 if (dev_state
== SATA_ERROR
)
1969 ata_dev_select(ap
, dev
->devno
, 1, 1);
1972 memset(&tf
, 0, sizeof(tf
));
1975 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1979 tf
.device
= ATA_DEVICE_OBS
;
1981 temp_n_block
= n_block
;
1985 tf
.command
= ATA_CMD_PIO_WRITE_EXT
;
1986 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
| ATA_TFLAG_WRITE
;
1988 tf
.hob_feature
= 31;
1990 tf
.hob_nsect
= (n_block
>> 8) & 0xff;
1991 tf
.nsect
= n_block
& 0xff;
1995 tf
.hob_lbal
= (block
>> 24) & 0xff;
1996 tf
.lbah
= (block
>> 16) & 0xff;
1997 tf
.lbam
= (block
>> 8) & 0xff;
1998 tf
.lbal
= block
& 0xff;
2001 if (tf
.flags
& ATA_TFLAG_FUA
)
2002 tf
.device
|= 1 << 7;
2004 tf
.command
= ATA_CMD_PIO_WRITE
;
2005 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_WRITE
;
2008 tf
.nsect
= n_block
& 0xff;
2010 tf
.lbah
= (block
>> 16) & 0xff;
2011 tf
.lbam
= (block
>> 8) & 0xff;
2012 tf
.lbal
= block
& 0xff;
2014 tf
.device
= (block
>> 24) & 0xf;
2016 tf
.device
|= 1 << 6;
2017 if (tf
.flags
& ATA_TFLAG_FUA
)
2018 tf
.device
|= 1 << 7;
2022 tf
.protocol
= ATA_PROT_PIO
;
2024 /* Some devices choke if TF registers contain garbage. Make
2025 * sure those are properly initialized.
2027 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2028 tf
.flags
|= ATA_TFLAG_POLLING
;
2030 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
, 0, 0);
2033 if (err_mask
& AC_ERR_NODEV_HINT
) {
2034 printf("READ_SECTORS NODEV after polling detection\n");
2038 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
2039 /* Device or controller might have reported
2040 * the wrong device class. Give a shot at the
2041 * other IDENTIFY if the current one is
2042 * aborted by the device.
2047 if (class == ATA_DEV_ATA
) {
2048 class = ATA_DEV_ATAPI
;
2050 class = ATA_DEV_ATA
;
2054 /* Control reaches here iff the device aborted
2055 * both flavors of IDENTIFYs which happens
2056 * sometimes with phantom devices.
2058 printf("both IDENTIFYs aborted, assuming NODEV\n");
2062 reason
= "I/O error";
2069 printf("failed to WRITE SECTORS (%s, err_mask=0x%x)\n", reason
, err_mask
);