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 <linux/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 int reset_sata(int dev
)
431 static u8
ata_check_altstatus(struct ata_port
*ap
)
434 val
= readb(ap
->ioaddr
.altstatus_addr
);
438 static int sata_dwc_softreset(struct ata_port
*ap
)
442 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
444 in_le32((void *)ap
->ioaddr
.scr_addr
+ (SCR_ERROR
* 4));
446 writeb(0x55, ioaddr
->nsect_addr
);
447 writeb(0xaa, ioaddr
->lbal_addr
);
448 writeb(0xaa, ioaddr
->nsect_addr
);
449 writeb(0x55, ioaddr
->lbal_addr
);
450 writeb(0x55, ioaddr
->nsect_addr
);
451 writeb(0xaa, ioaddr
->lbal_addr
);
453 nsect
= readb(ioaddr
->nsect_addr
);
454 lbal
= readb(ioaddr
->lbal_addr
);
456 if ((nsect
== 0x55) && (lbal
== 0xaa)) {
457 printf("Device found\n");
459 printf("No device found\n");
460 dev_state
= SATA_NODEVICE
;
464 tmp
= ATA_DEVICE_OBS
;
465 writeb(tmp
, ioaddr
->device_addr
);
466 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
470 writeb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
473 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
476 ata_check_status(ap
);
479 ata_check_status(ap
);
482 u8 status
= ata_check_status(ap
);
484 if (!(status
& ATA_BUSY
))
487 printf("Hard Disk status is BUSY.\n");
491 tmp
= ATA_DEVICE_OBS
;
492 writeb(tmp
, ioaddr
->device_addr
);
494 nsect
= readb(ioaddr
->nsect_addr
);
495 lbal
= readb(ioaddr
->lbal_addr
);
500 static u8
ata_check_status(struct ata_port
*ap
)
503 val
= readb(ap
->ioaddr
.status_addr
);
507 static int ata_id_has_hipm(const u16
*id
)
511 if (val
== 0 || val
== 0xffff)
514 return val
& (1 << 9);
517 static int ata_id_has_dipm(const u16
*id
)
521 if (val
== 0 || val
== 0xffff)
524 return val
& (1 << 3);
527 int scan_sata(int dev
)
533 struct ata_device
*ata_dev
= &ata_device
;
534 unsigned long pio_mask
, mwdma_mask
;
536 u16 iobuf
[ATA_SECTOR_WORDS
];
538 memset(iobuf
, 0, sizeof(iobuf
));
540 if (dev_state
== SATA_NODEVICE
)
543 printf("Waiting for device...");
548 status
= ata_check_altstatus(&ap
);
550 if ((status
& ATA_BUSY
) == 0) {
556 if (i
> (ATA_RESET_TIME
* 100)) {
557 printf("** TimeOUT **\n");
559 dev_state
= SATA_NODEVICE
;
562 if ((i
>= 100) && ((i
% 100) == 0))
568 rc
= ata_dev_read_id(ata_dev
, &ata_dev
->class,
569 ATA_READID_POSTRESET
,ata_dev
->id
);
571 printf("sata_dwc : error. failed sata scan\n");
575 /* SATA drives indicate we have a bridge. We don't know which
576 * end of the link the bridge is which is a problem
578 if (ata_id_is_sata(ata_dev
->id
))
579 ap
.cbl
= ATA_CBL_SATA
;
583 ata_dev
->flags
&= ~ATA_DFLAG_CFG_MASK
;
584 ata_dev
->max_sectors
= 0;
585 ata_dev
->cdb_len
= 0;
586 ata_dev
->n_sectors
= 0;
587 ata_dev
->cylinders
= 0;
589 ata_dev
->sectors
= 0;
591 if (id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
592 pio_mask
= id
[ATA_ID_PIO_MODES
] & 0x03;
596 /* If word 64 isn't valid then Word 51 high byte holds
597 * the PIO timing number for the maximum. Turn it into
600 u8 mode
= (id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF;
602 pio_mask
= (2 << mode
) - 1;
608 mwdma_mask
= id
[ATA_ID_MWDMA_MODES
] & 0x07;
610 if (ata_id_is_cfa(id
)) {
611 int pio
= id
[163] & 0x7;
612 int dma
= (id
[163] >> 3) & 7;
615 pio_mask
|= (1 << 5);
617 pio_mask
|= (1 << 6);
619 mwdma_mask
|= (1 << 3);
621 mwdma_mask
|= (1 << 4);
624 if (ata_dev
->class == ATA_DEV_ATA
) {
625 if (ata_id_is_cfa(id
)) {
627 printf("supports DRM functions and may "
628 "not be fully accessable.\n");
629 strcpy(revbuf
, "CFA");
631 if (ata_id_has_tpm(id
))
632 printf("supports DRM functions and may "
633 "not be fully accessable.\n");
636 ata_dev
->n_sectors
= ata_id_n_sectors((u16
*)id
);
638 if (ata_dev
->id
[59] & 0x100)
639 ata_dev
->multi_count
= ata_dev
->id
[59] & 0xff;
641 if (ata_id_has_lba(id
)) {
644 ata_dev
->flags
|= ATA_DFLAG_LBA
;
645 if (ata_id_has_lba48(id
)) {
646 ata_dev
->flags
|= ATA_DFLAG_LBA48
;
648 if (ata_dev
->n_sectors
>= (1UL << 28) &&
649 ata_id_has_flush_ext(id
))
650 ata_dev
->flags
|= ATA_DFLAG_FLUSH_EXT
;
652 if (!ata_id_has_ncq(ata_dev
->id
))
655 if (ata_dev
->horkage
& ATA_HORKAGE_NONCQ
)
656 strcpy(ncq_desc
, "NCQ (not used)");
658 if (ap
.flags
& ATA_FLAG_NCQ
)
659 ata_dev
->flags
|= ATA_DFLAG_NCQ
;
661 ata_dev
->cdb_len
= 16;
663 ata_dev
->max_sectors
= ATA_MAX_SECTORS
;
664 if (ata_dev
->flags
& ATA_DFLAG_LBA48
)
665 ata_dev
->max_sectors
= ATA_MAX_SECTORS_LBA48
;
667 if (!(ata_dev
->horkage
& ATA_HORKAGE_IPM
)) {
668 if (ata_id_has_hipm(ata_dev
->id
))
669 ata_dev
->flags
|= ATA_DFLAG_HIPM
;
670 if (ata_id_has_dipm(ata_dev
->id
))
671 ata_dev
->flags
|= ATA_DFLAG_DIPM
;
674 if ((ap
.cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ata_dev
->id
))) {
675 ata_dev
->udma_mask
&= ATA_UDMA5
;
676 ata_dev
->max_sectors
= ATA_MAX_SECTORS
;
679 if (ata_dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
) {
680 printf("Drive reports diagnostics failure."
681 "This may indicate a drive\n");
682 printf("fault or invalid emulation."
683 "Contact drive vendor for information.\n");
686 rc
= check_sata_dev_state();
688 ata_id_c_string(ata_dev
->id
,
689 (unsigned char *)sata_dev_desc
[dev
].revision
,
690 ATA_ID_FW_REV
, sizeof(sata_dev_desc
[dev
].revision
));
691 ata_id_c_string(ata_dev
->id
,
692 (unsigned char *)sata_dev_desc
[dev
].vendor
,
693 ATA_ID_PROD
, sizeof(sata_dev_desc
[dev
].vendor
));
694 ata_id_c_string(ata_dev
->id
,
695 (unsigned char *)sata_dev_desc
[dev
].product
,
696 ATA_ID_SERNO
, sizeof(sata_dev_desc
[dev
].product
));
698 sata_dev_desc
[dev
].lba
= (u32
) ata_dev
->n_sectors
;
701 if (ata_dev
->id
[83] & (1 << 10)) {
702 sata_dev_desc
[dev
].lba48
= 1;
704 sata_dev_desc
[dev
].lba48
= 0;
711 static u8
ata_busy_wait(struct ata_port
*ap
,
712 unsigned int bits
,unsigned int max
)
718 status
= ata_check_status(ap
);
720 } while (status
!= 0xff && (status
& bits
) && (max
> 0));
725 static int ata_dev_read_id(struct ata_device
*dev
, unsigned int *p_class
,
726 unsigned int flags
, u16
*id
)
728 struct ata_port
*ap
= pap
;
729 unsigned int class = *p_class
;
730 struct ata_taskfile tf
;
731 unsigned int err_mask
= 0;
733 int may_fallback
= 1, tried_spinup
= 0;
737 status
= ata_busy_wait(ap
, ATA_BUSY
, 30000);
738 if (status
& ATA_BUSY
) {
739 printf("BSY = 0 check. timeout.\n");
744 ata_dev_select(ap
, dev
->devno
, 1, 1);
747 memset(&tf
, 0, sizeof(tf
));
749 ap
->flags
&= ~ATA_FLAG_DISABLED
;
751 tf
.device
= ATA_DEVICE_OBS
;
752 tf
.command
= ATA_CMD_ID_ATA
;
753 tf
.protocol
= ATA_PROT_PIO
;
755 /* Some devices choke if TF registers contain garbage. Make
756 * sure those are properly initialized.
758 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
760 /* Device presence detection is unreliable on some
761 * controllers. Always poll IDENTIFY if available.
763 tf
.flags
|= ATA_TFLAG_POLLING
;
767 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
768 sizeof(id
[0]) * ATA_ID_WORDS
, 0);
771 if (err_mask
& AC_ERR_NODEV_HINT
) {
772 printf("NODEV after polling detection\n");
776 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
777 /* Device or controller might have reported
778 * the wrong device class. Give a shot at the
779 * other IDENTIFY if the current one is
780 * aborted by the device.
785 if (class == ATA_DEV_ATA
) {
786 class = ATA_DEV_ATAPI
;
792 /* Control reaches here iff the device aborted
793 * both flavors of IDENTIFYs which happens
794 * sometimes with phantom devices.
796 printf("both IDENTIFYs aborted, assuming NODEV\n");
800 reason
= "I/O error";
804 /* Falling back doesn't make sense if ID data was read
805 * successfully at least once.
811 for (id_cnt
= 0; id_cnt
< ATA_ID_WORDS
; id_cnt
++)
812 id
[id_cnt
] = le16_to_cpu(id
[id_cnt
]);
816 reason
= "device reports invalid type";
818 if (class == ATA_DEV_ATA
) {
819 if (!ata_id_is_ata(id
) && !ata_id_is_cfa(id
))
822 if (ata_id_is_ata(id
))
825 if (!tried_spinup
&& (id
[2] == 0x37c8 || id
[2] == 0x738c)) {
828 * Drive powered-up in standby mode, and requires a specific
829 * SET_FEATURES spin-up subcommand before it will accept
830 * anything other than the original IDENTIFY command.
832 err_mask
= ata_dev_set_feature(dev
, SETFEATURES_SPINUP
, 0);
833 if (err_mask
&& id
[2] != 0x738c) {
835 reason
= "SPINUP failed";
839 * If the drive initially returned incomplete IDENTIFY info,
840 * we now must reissue the IDENTIFY command.
846 if ((flags
& ATA_READID_POSTRESET
) && class == ATA_DEV_ATA
) {
848 * The exact sequence expected by certain pre-ATA4 drives is:
850 * IDENTIFY (optional in early ATA)
851 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
853 * Some drives were very specific about that exact sequence.
855 * Note that ATA4 says lba is mandatory so the second check
856 * shoud never trigger.
858 if (ata_id_major_version(id
) < 4 || !ata_id_has_lba(id
)) {
859 err_mask
= ata_dev_init_params(dev
, id
[3], id
[6]);
862 reason
= "INIT_DEV_PARAMS failed";
866 /* current CHS translation info (id[53-58]) might be
867 * changed. reread the identify device info.
869 flags
&= ~ATA_READID_POSTRESET
;
878 printf("failed to READ ID (%s, err_mask=0x%x)\n", reason
, err_mask
);
882 static u8
ata_wait_idle(struct ata_port
*ap
)
884 u8 status
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 1000);
888 static void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
889 unsigned int wait
, unsigned int can_sleep
)
894 ata_std_dev_select(ap
, device
);
900 static void ata_std_dev_select(struct ata_port
*ap
, unsigned int device
)
905 tmp
= ATA_DEVICE_OBS
;
907 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
910 writeb(tmp
, ap
->ioaddr
.device_addr
);
912 readb(ap
->ioaddr
.altstatus_addr
);
917 static int waiting_for_reg_state(volatile u8
*offset
,
924 for (i
= 0; i
< timeout_msec
; i
++) {
925 status
= readl(offset
);
926 if ((status
& sign
) != 0)
931 return (i
< timeout_msec
) ? 0 : -1;
934 static void ata_qc_reinit(struct ata_queued_cmd
*qc
)
936 qc
->dma_dir
= DMA_NONE
;
938 qc
->nbytes
= qc
->extrabytes
= qc
->curbytes
= 0;
941 qc
->sect_size
= ATA_SECT_SIZE
;
942 qc
->nbytes
= ATA_SECT_SIZE
* temp_n_block
;
944 memset(&qc
->tf
, 0, sizeof(qc
->tf
));
946 qc
->tf
.device
= ATA_DEVICE_OBS
;
948 qc
->result_tf
.command
= ATA_DRDY
;
949 qc
->result_tf
.feature
= 0;
952 struct ata_queued_cmd
*__ata_qc_from_tag(struct ata_port
*ap
,
955 if (tag
< ATA_MAX_QUEUE
)
956 return &ap
->qcmd
[tag
];
960 static void __ata_port_freeze(struct ata_port
*ap
)
962 printf("set port freeze.\n");
963 ap
->pflags
|= ATA_PFLAG_FROZEN
;
966 static int ata_port_freeze(struct ata_port
*ap
)
968 __ata_port_freeze(ap
);
972 unsigned ata_exec_internal(struct ata_device
*dev
,
973 struct ata_taskfile
*tf
, const u8
*cdb
,
974 int dma_dir
, unsigned int buflen
,
975 unsigned long timeout
)
977 struct ata_link
*link
= dev
->link
;
978 struct ata_port
*ap
= pap
;
979 struct ata_queued_cmd
*qc
;
980 unsigned int tag
, preempted_tag
;
981 u32 preempted_sactive
, preempted_qc_active
;
982 int preempted_nr_active_links
;
983 unsigned int err_mask
;
987 status
= ata_busy_wait(ap
, ATA_BUSY
, 300000);
988 if (status
& ATA_BUSY
) {
989 printf("BSY = 0 check. timeout.\n");
994 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
995 return AC_ERR_SYSTEM
;
997 tag
= ATA_TAG_INTERNAL
;
999 if (test_and_set_bit(tag
, &ap
->qc_allocated
)) {
1004 qc
= __ata_qc_from_tag(ap
, tag
);
1011 preempted_tag
= link
->active_tag
;
1012 preempted_sactive
= link
->sactive
;
1013 preempted_qc_active
= ap
->qc_active
;
1014 preempted_nr_active_links
= ap
->nr_active_links
;
1015 link
->active_tag
= ATA_TAG_POISON
;
1018 ap
->nr_active_links
= 0;
1022 memcpy(qc
->cdb
, cdb
, ATAPI_CDB_LEN
);
1023 qc
->flags
|= ATA_QCFLAG_RESULT_TF
;
1024 qc
->dma_dir
= dma_dir
;
1025 qc
->private_data
= 0;
1030 timeout
= ata_probe_timeout
* 1000 / HZ
;
1032 status
= ata_busy_wait(ap
, ATA_BUSY
, 30000);
1033 if (status
& ATA_BUSY
) {
1034 printf("BSY = 0 check. timeout.\n");
1035 printf("altstatus = 0x%x.\n", status
);
1036 qc
->err_mask
|= AC_ERR_OTHER
;
1037 return qc
->err_mask
;
1040 if (waiting_for_reg_state(ap
->ioaddr
.altstatus_addr
, 1000, 0x8)) {
1044 status
= readb(ap
->ioaddr
.altstatus_addr
);
1045 if ((status
& 0x01) != 0) {
1046 errorStatus
= readb(ap
->ioaddr
.feature_addr
);
1047 if (errorStatus
== 0x04 &&
1048 qc
->tf
.command
== ATA_CMD_PIO_READ_EXT
){
1049 printf("Hard Disk doesn't support LBA48\n");
1050 dev_state
= SATA_ERROR
;
1051 qc
->err_mask
|= AC_ERR_OTHER
;
1052 return qc
->err_mask
;
1055 qc
->err_mask
|= AC_ERR_OTHER
;
1056 return qc
->err_mask
;
1059 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
1060 if (status
& ATA_BUSY
) {
1061 printf("BSY = 0 check. timeout.\n");
1062 qc
->err_mask
|= AC_ERR_OTHER
;
1063 return qc
->err_mask
;
1069 if (qc
->flags
& ATA_QCFLAG_ACTIVE
) {
1070 qc
->err_mask
|= AC_ERR_TIMEOUT
;
1071 ata_port_freeze(ap
);
1075 if (qc
->flags
& ATA_QCFLAG_FAILED
) {
1076 if (qc
->result_tf
.command
& (ATA_ERR
| ATA_DF
))
1077 qc
->err_mask
|= AC_ERR_DEV
;
1080 qc
->err_mask
|= AC_ERR_OTHER
;
1082 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1083 qc
->err_mask
&= ~AC_ERR_OTHER
;
1086 *tf
= qc
->result_tf
;
1087 err_mask
= qc
->err_mask
;
1089 link
->active_tag
= preempted_tag
;
1090 link
->sactive
= preempted_sactive
;
1091 ap
->qc_active
= preempted_qc_active
;
1092 ap
->nr_active_links
= preempted_nr_active_links
;
1094 if (ap
->flags
& ATA_FLAG_DISABLED
) {
1095 err_mask
|= AC_ERR_SYSTEM
;
1096 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1102 static void ata_qc_issue(struct ata_queued_cmd
*qc
)
1104 struct ata_port
*ap
= qc
->ap
;
1105 struct ata_link
*link
= qc
->dev
->link
;
1106 u8 prot
= qc
->tf
.protocol
;
1108 if (ata_is_ncq(prot
)) {
1110 ap
->nr_active_links
++;
1111 link
->sactive
|= 1 << qc
->tag
;
1113 ap
->nr_active_links
++;
1114 link
->active_tag
= qc
->tag
;
1117 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
1118 ap
->qc_active
|= 1 << qc
->tag
;
1120 if (qc
->dev
->flags
& ATA_DFLAG_SLEEPING
) {
1125 qc
->err_mask
|= ata_qc_issue_prot(qc
);
1131 ata_qc_complete(qc
);
1134 static unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
1136 struct ata_port
*ap
= qc
->ap
;
1138 if (ap
->flags
& ATA_FLAG_PIO_POLLING
) {
1139 switch (qc
->tf
.protocol
) {
1141 case ATA_PROT_NODATA
:
1142 case ATAPI_PROT_PIO
:
1143 case ATAPI_PROT_NODATA
:
1144 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
1151 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
1153 switch (qc
->tf
.protocol
) {
1155 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1156 qc
->tf
.ctl
|= ATA_NIEN
;
1158 ata_tf_to_host(ap
, &qc
->tf
);
1160 ap
->hsm_task_state
= HSM_ST
;
1162 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1163 ata_pio_queue_task(ap
, qc
, 0);
1168 return AC_ERR_SYSTEM
;
1174 static void ata_tf_to_host(struct ata_port
*ap
,
1175 const struct ata_taskfile
*tf
)
1177 ata_tf_load(ap
, tf
);
1178 ata_exec_command(ap
, tf
);
1181 static void ata_tf_load(struct ata_port
*ap
,
1182 const struct ata_taskfile
*tf
)
1184 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1185 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
1187 if (tf
->ctl
!= ap
->last_ctl
) {
1188 if (ioaddr
->ctl_addr
)
1189 writeb(tf
->ctl
, ioaddr
->ctl_addr
);
1190 ap
->last_ctl
= tf
->ctl
;
1194 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
1195 writeb(tf
->hob_feature
, ioaddr
->feature_addr
);
1196 writeb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
1197 writeb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
1198 writeb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
1199 writeb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
1203 writeb(tf
->feature
, ioaddr
->feature_addr
);
1204 writeb(tf
->nsect
, ioaddr
->nsect_addr
);
1205 writeb(tf
->lbal
, ioaddr
->lbal_addr
);
1206 writeb(tf
->lbam
, ioaddr
->lbam_addr
);
1207 writeb(tf
->lbah
, ioaddr
->lbah_addr
);
1210 if (tf
->flags
& ATA_TFLAG_DEVICE
)
1211 writeb(tf
->device
, ioaddr
->device_addr
);
1216 static void ata_exec_command(struct ata_port
*ap
,
1217 const struct ata_taskfile
*tf
)
1219 writeb(tf
->command
, ap
->ioaddr
.command_addr
);
1221 readb(ap
->ioaddr
.altstatus_addr
);
1226 static void ata_pio_queue_task(struct ata_port
*ap
,
1227 void *data
,unsigned long delay
)
1229 ap
->port_task_data
= data
;
1232 static unsigned int ac_err_mask(u8 status
)
1234 if (status
& (ATA_BUSY
| ATA_DRQ
))
1236 if (status
& (ATA_ERR
| ATA_DF
))
1241 static unsigned int __ac_err_mask(u8 status
)
1243 unsigned int mask
= ac_err_mask(status
);
1245 return AC_ERR_OTHER
;
1249 static void ata_pio_task(struct ata_port
*arg_ap
)
1251 struct ata_port
*ap
= arg_ap
;
1252 struct ata_queued_cmd
*qc
= ap
->port_task_data
;
1258 * This is purely heuristic. This is a fast path.
1259 * Sometimes when we enter, BSY will be cleared in
1260 * a chk-status or two. If not, the drive is probably seeking
1261 * or something. Snooze for a couple msecs, then
1262 * chk-status again. If still busy, queue delayed work.
1264 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
1265 if (status
& ATA_BUSY
) {
1267 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
1268 if (status
& ATA_BUSY
) {
1269 ata_pio_queue_task(ap
, qc
, ATA_SHORT_PAUSE
);
1274 poll_next
= ata_hsm_move(ap
, qc
, status
, 1);
1276 /* another command or interrupt handler
1277 * may be running at this point.
1283 static int ata_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
1284 u8 status
, int in_wq
)
1289 switch (ap
->hsm_task_state
) {
1291 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1293 if ((status
& ATA_DRQ
) == 0) {
1294 if (status
& (ATA_ERR
| ATA_DF
)) {
1295 qc
->err_mask
|= AC_ERR_DEV
;
1297 qc
->err_mask
|= AC_ERR_HSM
;
1299 ap
->hsm_task_state
= HSM_ST_ERR
;
1303 /* Device should not ask for data transfer (DRQ=1)
1304 * when it finds something wrong.
1305 * We ignore DRQ here and stop the HSM by
1306 * changing hsm_task_state to HSM_ST_ERR and
1307 * let the EH abort the command or reset the device.
1309 if (status
& (ATA_ERR
| ATA_DF
)) {
1310 if (!(qc
->dev
->horkage
& ATA_HORKAGE_STUCK_ERR
)) {
1311 printf("DRQ=1 with device error, "
1312 "dev_stat 0x%X\n", status
);
1313 qc
->err_mask
|= AC_ERR_HSM
;
1314 ap
->hsm_task_state
= HSM_ST_ERR
;
1319 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
1320 /* PIO data out protocol.
1321 * send first data block.
1323 /* ata_pio_sectors() might change the state
1324 * to HSM_ST_LAST. so, the state is changed here
1325 * before ata_pio_sectors().
1327 ap
->hsm_task_state
= HSM_ST
;
1328 ata_pio_sectors(qc
);
1330 printf("protocol is not ATA_PROT_PIO \n");
1335 if ((status
& ATA_DRQ
) == 0) {
1336 if (status
& (ATA_ERR
| ATA_DF
)) {
1337 qc
->err_mask
|= AC_ERR_DEV
;
1339 /* HSM violation. Let EH handle this.
1340 * Phantom devices also trigger this
1341 * condition. Mark hint.
1343 qc
->err_mask
|= AC_ERR_HSM
| AC_ERR_NODEV_HINT
;
1346 ap
->hsm_task_state
= HSM_ST_ERR
;
1349 /* For PIO reads, some devices may ask for
1350 * data transfer (DRQ=1) alone with ERR=1.
1351 * We respect DRQ here and transfer one
1352 * block of junk data before changing the
1353 * hsm_task_state to HSM_ST_ERR.
1355 * For PIO writes, ERR=1 DRQ=1 doesn't make
1356 * sense since the data block has been
1357 * transferred to the device.
1359 if (status
& (ATA_ERR
| ATA_DF
)) {
1360 qc
->err_mask
|= AC_ERR_DEV
;
1362 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
1363 ata_pio_sectors(qc
);
1364 status
= ata_wait_idle(ap
);
1367 if (status
& (ATA_BUSY
| ATA_DRQ
))
1368 qc
->err_mask
|= AC_ERR_HSM
;
1370 /* ata_pio_sectors() might change the
1371 * state to HSM_ST_LAST. so, the state
1372 * is changed after ata_pio_sectors().
1374 ap
->hsm_task_state
= HSM_ST_ERR
;
1378 ata_pio_sectors(qc
);
1379 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
1380 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
1381 status
= ata_wait_idle(ap
);
1389 if (!ata_ok(status
)) {
1390 qc
->err_mask
|= __ac_err_mask(status
);
1391 ap
->hsm_task_state
= HSM_ST_ERR
;
1395 ap
->hsm_task_state
= HSM_ST_IDLE
;
1397 ata_hsm_qc_complete(qc
, in_wq
);
1403 /* make sure qc->err_mask is available to
1404 * know what's wrong and recover
1406 ap
->hsm_task_state
= HSM_ST_IDLE
;
1408 ata_hsm_qc_complete(qc
, in_wq
);
1419 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
1421 struct ata_port
*ap
;
1423 qc
->pdata
= ap
->pdata
;
1427 readb(qc
->ap
->ioaddr
.altstatus_addr
);
1431 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
1433 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
1434 struct ata_port
*ap
= qc
->ap
;
1435 unsigned int offset
;
1437 char temp_data_buf
[512];
1439 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
1440 ap
->hsm_task_state
= HSM_ST_LAST
;
1442 offset
= qc
->curbytes
;
1444 switch (qc
->tf
.command
) {
1445 case ATA_CMD_ID_ATA
:
1446 buf
= (unsigned char *)&ata_device
.id
[0];
1448 case ATA_CMD_PIO_READ_EXT
:
1449 case ATA_CMD_PIO_READ
:
1450 case ATA_CMD_PIO_WRITE_EXT
:
1451 case ATA_CMD_PIO_WRITE
:
1452 buf
= qc
->pdata
+ offset
;
1455 buf
= (unsigned char *)&temp_data_buf
[0];
1458 ata_mmio_data_xfer(qc
->dev
, buf
, qc
->sect_size
, do_write
);
1460 qc
->curbytes
+= qc
->sect_size
;
1464 static void ata_mmio_data_xfer(struct ata_device
*dev
, unsigned char *buf
,
1465 unsigned int buflen
, int do_write
)
1467 struct ata_port
*ap
= pap
;
1468 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
1469 unsigned int words
= buflen
>> 1;
1470 u16
*buf16
= (u16
*)buf
;
1475 for (i
= 0; i
< words
; i
++)
1476 writew(le16_to_cpu(buf16
[i
]), data_addr
);
1478 for (i
= 0; i
< words
; i
++)
1479 buf16
[i
] = cpu_to_le16(readw(data_addr
));
1482 if (buflen
& 0x01) {
1483 __le16 align_buf
[1] = { 0 };
1484 unsigned char *trailing_buf
= buf
+ buflen
- 1;
1487 memcpy(align_buf
, trailing_buf
, 1);
1488 writew(le16_to_cpu(align_buf
[0]), data_addr
);
1490 align_buf
[0] = cpu_to_le16(readw(data_addr
));
1491 memcpy(trailing_buf
, align_buf
, 1);
1496 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
1498 struct ata_port
*ap
= qc
->ap
;
1501 /* EH might have kicked in while host lock is
1504 qc
= &ap
->qcmd
[qc
->tag
];
1506 if (!(qc
->err_mask
& AC_ERR_HSM
)) {
1508 ata_qc_complete(qc
);
1510 ata_port_freeze(ap
);
1514 if (!(qc
->err_mask
& AC_ERR_HSM
)) {
1515 ata_qc_complete(qc
);
1517 ata_port_freeze(ap
);
1522 static u8
ata_irq_on(struct ata_port
*ap
)
1524 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1527 ap
->ctl
&= ~ATA_NIEN
;
1528 ap
->last_ctl
= ap
->ctl
;
1530 if (ioaddr
->ctl_addr
)
1531 writeb(ap
->ctl
, ioaddr
->ctl_addr
);
1533 tmp
= ata_wait_idle(ap
);
1538 static unsigned int ata_tag_internal(unsigned int tag
)
1540 return tag
== ATA_MAX_QUEUE
- 1;
1543 static void ata_qc_complete(struct ata_queued_cmd
*qc
)
1545 struct ata_device
*dev
= qc
->dev
;
1547 qc
->flags
|= ATA_QCFLAG_FAILED
;
1549 if (qc
->flags
& ATA_QCFLAG_FAILED
) {
1550 if (!ata_tag_internal(qc
->tag
)) {
1555 if (qc
->flags
& ATA_QCFLAG_RESULT_TF
)
1558 /* Some commands need post-processing after successful
1561 switch (qc
->tf
.command
) {
1562 case ATA_CMD_SET_FEATURES
:
1563 if (qc
->tf
.feature
!= SETFEATURES_WC_ON
&&
1564 qc
->tf
.feature
!= SETFEATURES_WC_OFF
)
1566 case ATA_CMD_INIT_DEV_PARAMS
:
1567 case ATA_CMD_SET_MULTI
:
1571 dev
->flags
|= ATA_DFLAG_SLEEPING
;
1575 __ata_qc_complete(qc
);
1578 static void fill_result_tf(struct ata_queued_cmd
*qc
)
1580 struct ata_port
*ap
= qc
->ap
;
1582 qc
->result_tf
.flags
= qc
->tf
.flags
;
1583 ata_tf_read(ap
, &qc
->result_tf
);
1586 static void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
1588 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1590 tf
->command
= ata_check_status(ap
);
1591 tf
->feature
= readb(ioaddr
->error_addr
);
1592 tf
->nsect
= readb(ioaddr
->nsect_addr
);
1593 tf
->lbal
= readb(ioaddr
->lbal_addr
);
1594 tf
->lbam
= readb(ioaddr
->lbam_addr
);
1595 tf
->lbah
= readb(ioaddr
->lbah_addr
);
1596 tf
->device
= readb(ioaddr
->device_addr
);
1598 if (tf
->flags
& ATA_TFLAG_LBA48
) {
1599 if (ioaddr
->ctl_addr
) {
1600 writeb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
1602 tf
->hob_feature
= readb(ioaddr
->error_addr
);
1603 tf
->hob_nsect
= readb(ioaddr
->nsect_addr
);
1604 tf
->hob_lbal
= readb(ioaddr
->lbal_addr
);
1605 tf
->hob_lbam
= readb(ioaddr
->lbam_addr
);
1606 tf
->hob_lbah
= readb(ioaddr
->lbah_addr
);
1608 writeb(tf
->ctl
, ioaddr
->ctl_addr
);
1609 ap
->last_ctl
= tf
->ctl
;
1611 printf("sata_dwc warnning register read.\n");
1616 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
1618 struct ata_port
*ap
= qc
->ap
;
1619 struct ata_link
*link
= qc
->dev
->link
;
1621 link
->active_tag
= ATA_TAG_POISON
;
1622 ap
->nr_active_links
--;
1624 if (qc
->flags
& ATA_QCFLAG_CLEAR_EXCL
&& ap
->excl_link
== link
)
1625 ap
->excl_link
= NULL
;
1627 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
1628 ap
->qc_active
&= ~(1 << qc
->tag
);
1631 static void ata_qc_free(struct ata_queued_cmd
*qc
)
1633 struct ata_port
*ap
= qc
->ap
;
1637 if (tag
< ATA_MAX_QUEUE
) {
1638 qc
->tag
= ATA_TAG_POISON
;
1639 clear_bit(tag
, &ap
->qc_allocated
);
1643 static int check_sata_dev_state(void)
1645 unsigned long datalen
;
1646 unsigned char *pdata
;
1649 char temp_data_buf
[512];
1654 pdata
= (unsigned char*)&temp_data_buf
[0];
1657 ret
= ata_dev_read_sectors(pdata
, datalen
, 0, 1);
1663 if (i
> (ATA_RESET_TIME
* 100)) {
1664 printf("** TimeOUT **\n");
1665 dev_state
= SATA_NODEVICE
;
1669 if ((i
>= 100) && ((i
% 100) == 0))
1673 dev_state
= SATA_READY
;
1678 static unsigned int ata_dev_set_feature(struct ata_device
*dev
,
1679 u8 enable
, u8 feature
)
1681 struct ata_taskfile tf
;
1682 struct ata_port
*ap
;
1684 unsigned int err_mask
;
1686 memset(&tf
, 0, sizeof(tf
));
1689 tf
.device
= ATA_DEVICE_OBS
;
1690 tf
.command
= ATA_CMD_SET_FEATURES
;
1691 tf
.feature
= enable
;
1692 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1693 tf
.protocol
= ATA_PROT_NODATA
;
1696 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, 0, 0);
1701 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
1702 u16 heads
, u16 sectors
)
1704 struct ata_taskfile tf
;
1705 struct ata_port
*ap
;
1707 unsigned int err_mask
;
1709 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
1710 return AC_ERR_INVALID
;
1712 memset(&tf
, 0, sizeof(tf
));
1714 tf
.device
= ATA_DEVICE_OBS
;
1715 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
1716 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1717 tf
.protocol
= ATA_PROT_NODATA
;
1719 tf
.device
|= (heads
- 1) & 0x0f;
1721 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, 0, 0);
1723 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
1729 #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
1730 #define SATA_MAX_READ_BLK 0xFF
1732 #define SATA_MAX_READ_BLK 0xFFFF
1735 ulong
sata_read(int device
, ulong blknr
, lbaint_t blkcnt
, void *buffer
)
1737 ulong start
,blks
, buf_addr
;
1738 unsigned short smallblks
;
1739 unsigned long datalen
;
1740 unsigned char *pdata
;
1746 if (dev_state
!= SATA_READY
)
1749 buf_addr
= (unsigned long)buffer
;
1753 pdata
= (unsigned char *)buf_addr
;
1754 if (blks
> SATA_MAX_READ_BLK
) {
1755 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_READ_BLK
;
1756 smallblks
= SATA_MAX_READ_BLK
;
1759 n_block
= (u32
)smallblks
;
1761 start
+= SATA_MAX_READ_BLK
;
1762 blks
-= SATA_MAX_READ_BLK
;
1764 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_READ_BLK
;
1765 datalen
= sata_dev_desc
[device
].blksz
* blks
;
1766 smallblks
= (unsigned short)blks
;
1769 n_block
= (u32
)smallblks
;
1775 if (ata_dev_read_sectors(pdata
, datalen
, block
, n_block
) != true) {
1776 printf("sata_dwc : Hard disk read error.\n");
1780 buf_addr
+= datalen
;
1781 } while (blks
!= 0);
1786 static int ata_dev_read_sectors(unsigned char *pdata
, unsigned long datalen
,
1787 u32 block
, u32 n_block
)
1789 struct ata_port
*ap
= pap
;
1790 struct ata_device
*dev
= &ata_device
;
1791 struct ata_taskfile tf
;
1792 unsigned int class = ATA_DEV_ATA
;
1793 unsigned int err_mask
= 0;
1795 int may_fallback
= 1;
1797 if (dev_state
== SATA_ERROR
)
1800 ata_dev_select(ap
, dev
->devno
, 1, 1);
1803 memset(&tf
, 0, sizeof(tf
));
1806 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1810 tf
.device
= ATA_DEVICE_OBS
;
1812 temp_n_block
= n_block
;
1815 tf
.command
= ATA_CMD_PIO_READ_EXT
;
1816 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
;
1818 tf
.hob_feature
= 31;
1820 tf
.hob_nsect
= (n_block
>> 8) & 0xff;
1821 tf
.nsect
= n_block
& 0xff;
1825 tf
.hob_lbal
= (block
>> 24) & 0xff;
1826 tf
.lbah
= (block
>> 16) & 0xff;
1827 tf
.lbam
= (block
>> 8) & 0xff;
1828 tf
.lbal
= block
& 0xff;
1831 if (tf
.flags
& ATA_TFLAG_FUA
)
1832 tf
.device
|= 1 << 7;
1834 tf
.command
= ATA_CMD_PIO_READ
;
1835 tf
.flags
|= ATA_TFLAG_LBA
;
1838 tf
.nsect
= n_block
& 0xff;
1840 tf
.lbah
= (block
>> 16) & 0xff;
1841 tf
.lbam
= (block
>> 8) & 0xff;
1842 tf
.lbal
= block
& 0xff;
1844 tf
.device
= (block
>> 24) & 0xf;
1846 tf
.device
|= 1 << 6;
1847 if (tf
.flags
& ATA_TFLAG_FUA
)
1848 tf
.device
|= 1 << 7;
1852 tf
.protocol
= ATA_PROT_PIO
;
1854 /* Some devices choke if TF registers contain garbage. Make
1855 * sure those are properly initialized.
1857 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1858 tf
.flags
|= ATA_TFLAG_POLLING
;
1860 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
, 0, 0);
1863 if (err_mask
& AC_ERR_NODEV_HINT
) {
1864 printf("READ_SECTORS NODEV after polling detection\n");
1868 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
1869 /* Device or controller might have reported
1870 * the wrong device class. Give a shot at the
1871 * other IDENTIFY if the current one is
1872 * aborted by the device.
1877 if (class == ATA_DEV_ATA
) {
1878 class = ATA_DEV_ATAPI
;
1880 class = ATA_DEV_ATA
;
1884 /* Control reaches here iff the device aborted
1885 * both flavors of IDENTIFYs which happens
1886 * sometimes with phantom devices.
1888 printf("both IDENTIFYs aborted, assuming NODEV\n");
1892 reason
= "I/O error";
1899 printf("failed to READ SECTORS (%s, err_mask=0x%x)\n", reason
, err_mask
);
1903 #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
1904 #define SATA_MAX_WRITE_BLK 0xFF
1906 #define SATA_MAX_WRITE_BLK 0xFFFF
1909 ulong
sata_write(int device
, ulong blknr
, lbaint_t blkcnt
, const void *buffer
)
1911 ulong start
,blks
, buf_addr
;
1912 unsigned short smallblks
;
1913 unsigned long datalen
;
1914 unsigned char *pdata
;
1921 if (dev_state
!= SATA_READY
)
1924 buf_addr
= (unsigned long)buffer
;
1928 pdata
= (unsigned char *)buf_addr
;
1929 if (blks
> SATA_MAX_WRITE_BLK
) {
1930 datalen
= sata_dev_desc
[device
].blksz
* SATA_MAX_WRITE_BLK
;
1931 smallblks
= SATA_MAX_WRITE_BLK
;
1934 n_block
= (u32
)smallblks
;
1936 start
+= SATA_MAX_WRITE_BLK
;
1937 blks
-= SATA_MAX_WRITE_BLK
;
1939 datalen
= sata_dev_desc
[device
].blksz
* blks
;
1940 smallblks
= (unsigned short)blks
;
1943 n_block
= (u32
)smallblks
;
1949 if (ata_dev_write_sectors(pdata
, datalen
, block
, n_block
) != true) {
1950 printf("sata_dwc : Hard disk read error.\n");
1954 buf_addr
+= datalen
;
1955 } while (blks
!= 0);
1960 static int ata_dev_write_sectors(unsigned char* pdata
, unsigned long datalen
,
1961 u32 block
, u32 n_block
)
1963 struct ata_port
*ap
= pap
;
1964 struct ata_device
*dev
= &ata_device
;
1965 struct ata_taskfile tf
;
1966 unsigned int class = ATA_DEV_ATA
;
1967 unsigned int err_mask
= 0;
1969 int may_fallback
= 1;
1971 if (dev_state
== SATA_ERROR
)
1974 ata_dev_select(ap
, dev
->devno
, 1, 1);
1977 memset(&tf
, 0, sizeof(tf
));
1980 ap
->flags
&= ~ATA_FLAG_DISABLED
;
1984 tf
.device
= ATA_DEVICE_OBS
;
1986 temp_n_block
= n_block
;
1990 tf
.command
= ATA_CMD_PIO_WRITE_EXT
;
1991 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
| ATA_TFLAG_WRITE
;
1993 tf
.hob_feature
= 31;
1995 tf
.hob_nsect
= (n_block
>> 8) & 0xff;
1996 tf
.nsect
= n_block
& 0xff;
2000 tf
.hob_lbal
= (block
>> 24) & 0xff;
2001 tf
.lbah
= (block
>> 16) & 0xff;
2002 tf
.lbam
= (block
>> 8) & 0xff;
2003 tf
.lbal
= block
& 0xff;
2006 if (tf
.flags
& ATA_TFLAG_FUA
)
2007 tf
.device
|= 1 << 7;
2009 tf
.command
= ATA_CMD_PIO_WRITE
;
2010 tf
.flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_WRITE
;
2013 tf
.nsect
= n_block
& 0xff;
2015 tf
.lbah
= (block
>> 16) & 0xff;
2016 tf
.lbam
= (block
>> 8) & 0xff;
2017 tf
.lbal
= block
& 0xff;
2019 tf
.device
= (block
>> 24) & 0xf;
2021 tf
.device
|= 1 << 6;
2022 if (tf
.flags
& ATA_TFLAG_FUA
)
2023 tf
.device
|= 1 << 7;
2027 tf
.protocol
= ATA_PROT_PIO
;
2029 /* Some devices choke if TF registers contain garbage. Make
2030 * sure those are properly initialized.
2032 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2033 tf
.flags
|= ATA_TFLAG_POLLING
;
2035 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
, 0, 0);
2038 if (err_mask
& AC_ERR_NODEV_HINT
) {
2039 printf("READ_SECTORS NODEV after polling detection\n");
2043 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
2044 /* Device or controller might have reported
2045 * the wrong device class. Give a shot at the
2046 * other IDENTIFY if the current one is
2047 * aborted by the device.
2052 if (class == ATA_DEV_ATA
) {
2053 class = ATA_DEV_ATAPI
;
2055 class = ATA_DEV_ATA
;
2059 /* Control reaches here iff the device aborted
2060 * both flavors of IDENTIFYs which happens
2061 * sometimes with phantom devices.
2063 printf("both IDENTIFYs aborted, assuming NODEV\n");
2067 reason
= "I/O error";
2074 printf("failed to WRITE SECTORS (%s, err_mask=0x%x)\n", reason
, err_mask
);