2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 * Terry Lv <r65388@freescale.com>
5 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/ctype.h>
16 #include <asm/errno.h>
18 #include <linux/bitops.h>
19 #include <asm/arch/clock.h>
20 #include <asm/arch/sys_proto.h>
21 #include "dwc_ahsata.h"
23 struct sata_port_regs
{
47 struct sata_host_regs
{
76 #define MAX_DATA_BYTES_PER_SG (4 * 1024 * 1024)
77 #define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
79 #define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
83 static inline void __iomem
*ahci_port_base(void __iomem
*base
, u32 port
)
85 return base
+ 0x100 + (port
* 0x80);
88 static int waiting_for_cmd_completed(u8
*offset
,
96 ((status
= readl(offset
)) & sign
) && i
< timeout_msec
;
100 return (i
< timeout_msec
) ? 0 : -1;
103 static int ahci_setup_oobr(struct ahci_probe_ent
*probe_ent
,
106 struct sata_host_regs
*host_mmio
=
107 (struct sata_host_regs
*)probe_ent
->mmio_base
;
109 writel(SATA_HOST_OOBR_WE
, &(host_mmio
->oobr
));
110 writel(0x02060b14, &(host_mmio
->oobr
));
115 static int ahci_host_init(struct ahci_probe_ent
*probe_ent
)
117 u32 tmp
, cap_save
, num_ports
;
118 int i
, j
, timeout
= 1000;
119 struct sata_port_regs
*port_mmio
= NULL
;
120 struct sata_host_regs
*host_mmio
=
121 (struct sata_host_regs
*)probe_ent
->mmio_base
;
122 int clk
= mxc_get_clock(MXC_SATA_CLK
);
124 cap_save
= readl(&(host_mmio
->cap
));
125 cap_save
|= SATA_HOST_CAP_SSS
;
127 /* global controller reset */
128 tmp
= readl(&(host_mmio
->ghc
));
129 if ((tmp
& SATA_HOST_GHC_HR
) == 0)
130 writel_with_flush(tmp
| SATA_HOST_GHC_HR
, &(host_mmio
->ghc
));
132 while ((readl(&(host_mmio
->ghc
)) & SATA_HOST_GHC_HR
)
137 debug("controller reset failed (0x%x)\n", tmp
);
142 writel(clk
/ 1000, &(host_mmio
->timer1ms
));
144 ahci_setup_oobr(probe_ent
, 0);
146 writel_with_flush(SATA_HOST_GHC_AE
, &(host_mmio
->ghc
));
147 writel(cap_save
, &(host_mmio
->cap
));
148 num_ports
= (cap_save
& SATA_HOST_CAP_NP_MASK
) + 1;
149 writel_with_flush((1 << num_ports
) - 1,
153 * Determine which Ports are implemented by the DWC_ahsata,
154 * by reading the PI register. This bit map value aids the
155 * software to determine how many Ports are available and
156 * which Port registers need to be initialized.
158 probe_ent
->cap
= readl(&(host_mmio
->cap
));
159 probe_ent
->port_map
= readl(&(host_mmio
->pi
));
161 /* Determine how many command slots the HBA supports */
163 (probe_ent
->cap
& SATA_HOST_CAP_NP_MASK
) + 1;
165 debug("cap 0x%x port_map 0x%x n_ports %d\n",
166 probe_ent
->cap
, probe_ent
->port_map
, probe_ent
->n_ports
);
168 for (i
= 0; i
< probe_ent
->n_ports
; i
++) {
169 probe_ent
->port
[i
].port_mmio
=
170 ahci_port_base(host_mmio
, i
);
172 (struct sata_port_regs
*)probe_ent
->port
[i
].port_mmio
;
174 /* Ensure that the DWC_ahsata is in idle state */
175 tmp
= readl(&(port_mmio
->cmd
));
178 * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
179 * are all cleared, the Port is in an idle state.
181 if (tmp
& (SATA_PORT_CMD_CR
| SATA_PORT_CMD_FR
|
182 SATA_PORT_CMD_FRE
| SATA_PORT_CMD_ST
)) {
185 * System software places a Port into the idle state by
186 * clearing P#CMD.ST and waiting for P#CMD.CR to return
189 tmp
&= ~SATA_PORT_CMD_ST
;
190 writel_with_flush(tmp
, &(port_mmio
->cmd
));
193 * spec says 500 msecs for each bit, so
194 * this is slightly incorrect.
199 while ((readl(&(port_mmio
->cmd
)) & SATA_PORT_CMD_CR
)
204 debug("port reset failed (0x%x)\n", tmp
);
210 tmp
= readl(&(port_mmio
->cmd
));
211 writel((tmp
| SATA_PORT_CMD_SUD
), &(port_mmio
->cmd
));
213 /* Wait for spin-up to finish */
215 while (!(readl(&(port_mmio
->cmd
)) | SATA_PORT_CMD_SUD
)
219 debug("Spin-Up can't finish!\n");
223 for (j
= 0; j
< 100; ++j
) {
225 tmp
= readl(&(port_mmio
->ssts
));
226 if (((tmp
& SATA_PORT_SSTS_DET_MASK
) == 0x3) ||
227 ((tmp
& SATA_PORT_SSTS_DET_MASK
) == 0x1))
231 /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
233 while (!(readl(&(port_mmio
->serr
)) | SATA_PORT_SERR_DIAG_X
)
237 debug("Can't find DIAG_X set!\n");
242 * For each implemented Port, clear the P#SERR
243 * register, by writing ones to each implemented\
246 tmp
= readl(&(port_mmio
->serr
));
247 debug("P#SERR 0x%x\n",
249 writel(tmp
, &(port_mmio
->serr
));
251 /* Ack any pending irq events for this port */
252 tmp
= readl(&(host_mmio
->is
));
253 debug("IS 0x%x\n", tmp
);
255 writel(tmp
, &(host_mmio
->is
));
257 writel(1 << i
, &(host_mmio
->is
));
259 /* set irq mask (enables interrupts) */
260 writel(DEF_PORT_IRQ
, &(port_mmio
->ie
));
262 /* register linkup ports */
263 tmp
= readl(&(port_mmio
->ssts
));
264 debug("Port %d status: 0x%x\n", i
, tmp
);
265 if ((tmp
& SATA_PORT_SSTS_DET_MASK
) == 0x03)
266 probe_ent
->link_port_map
|= (0x01 << i
);
269 tmp
= readl(&(host_mmio
->ghc
));
270 debug("GHC 0x%x\n", tmp
);
271 writel(tmp
| SATA_HOST_GHC_IE
, &(host_mmio
->ghc
));
272 tmp
= readl(&(host_mmio
->ghc
));
273 debug("GHC 0x%x\n", tmp
);
278 static void ahci_print_info(struct ahci_probe_ent
*probe_ent
)
280 struct sata_host_regs
*host_mmio
=
281 (struct sata_host_regs
*)probe_ent
->mmio_base
;
282 u32 vers
, cap
, impl
, speed
;
286 vers
= readl(&(host_mmio
->vs
));
287 cap
= probe_ent
->cap
;
288 impl
= probe_ent
->port_map
;
290 speed
= (cap
& SATA_HOST_CAP_ISS_MASK
)
291 >> SATA_HOST_CAP_ISS_OFFSET
;
301 printf("AHCI %02x%02x.%02x%02x "
302 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
307 ((cap
>> 8) & 0x1f) + 1,
316 cap
& (1 << 31) ? "64bit " : "",
317 cap
& (1 << 30) ? "ncq " : "",
318 cap
& (1 << 28) ? "ilck " : "",
319 cap
& (1 << 27) ? "stag " : "",
320 cap
& (1 << 26) ? "pm " : "",
321 cap
& (1 << 25) ? "led " : "",
322 cap
& (1 << 24) ? "clo " : "",
323 cap
& (1 << 19) ? "nz " : "",
324 cap
& (1 << 18) ? "only " : "",
325 cap
& (1 << 17) ? "pmp " : "",
326 cap
& (1 << 15) ? "pio " : "",
327 cap
& (1 << 14) ? "slum " : "",
328 cap
& (1 << 13) ? "part " : "");
331 static int ahci_init_one(int pdev
)
334 struct ahci_probe_ent
*probe_ent
= NULL
;
336 probe_ent
= malloc(sizeof(struct ahci_probe_ent
));
337 memset(probe_ent
, 0, sizeof(struct ahci_probe_ent
));
338 probe_ent
->dev
= pdev
;
340 probe_ent
->host_flags
= ATA_FLAG_SATA
346 probe_ent
->mmio_base
= (void __iomem
*)CONFIG_DWC_AHSATA_BASE_ADDR
;
348 /* initialize adapter */
349 rc
= ahci_host_init(probe_ent
);
353 ahci_print_info(probe_ent
);
355 /* Save the private struct to block device struct */
356 sata_dev_desc
[pdev
].priv
= (void *)probe_ent
;
364 static int ahci_fill_sg(struct ahci_probe_ent
*probe_ent
,
365 u8 port
, unsigned char *buf
, int buf_len
)
367 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
368 struct ahci_sg
*ahci_sg
= pp
->cmd_tbl_sg
;
369 u32 sg_count
, max_bytes
;
372 max_bytes
= MAX_DATA_BYTES_PER_SG
;
373 sg_count
= ((buf_len
- 1) / max_bytes
) + 1;
374 if (sg_count
> AHCI_MAX_SG
) {
375 printf("Error:Too much sg!\n");
379 for (i
= 0; i
< sg_count
; i
++) {
381 cpu_to_le32((u32
)buf
+ i
* max_bytes
);
382 ahci_sg
->addr_hi
= 0;
383 ahci_sg
->flags_size
= cpu_to_le32(0x3fffff &
388 buf_len
-= max_bytes
;
394 static void ahci_fill_cmd_slot(struct ahci_ioports
*pp
, u32 cmd_slot
, u32 opts
)
396 struct ahci_cmd_hdr
*cmd_hdr
= (struct ahci_cmd_hdr
*)(pp
->cmd_slot
+
397 AHCI_CMD_SLOT_SZ
* cmd_slot
);
399 memset(cmd_hdr
, 0, AHCI_CMD_SLOT_SZ
);
400 cmd_hdr
->opts
= cpu_to_le32(opts
);
402 pp
->cmd_slot
->tbl_addr
= cpu_to_le32((u32
)pp
->cmd_tbl
& 0xffffffff);
403 #ifdef CONFIG_PHYS_64BIT
404 pp
->cmd_slot
->tbl_addr_hi
=
405 cpu_to_le32((u32
)(((pp
->cmd_tbl
) >> 16) >> 16));
409 #define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
411 static int ahci_exec_ata_cmd(struct ahci_probe_ent
*probe_ent
,
412 u8 port
, struct sata_fis_h2d
*cfis
,
413 u8
*buf
, u32 buf_len
, s32 is_write
)
415 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
416 struct sata_port_regs
*port_mmio
=
417 (struct sata_port_regs
*)pp
->port_mmio
;
419 int sg_count
= 0, cmd_slot
= 0;
421 cmd_slot
= AHCI_GET_CMD_SLOT(readl(&(port_mmio
->ci
)));
422 if (32 == cmd_slot
) {
423 printf("Can't find empty command slot!\n");
427 /* Check xfer length */
428 if (buf_len
> MAX_BYTES_PER_TRANS
) {
429 printf("Max transfer length is %dB\n\r",
430 MAX_BYTES_PER_TRANS
);
434 memcpy((u8
*)(pp
->cmd_tbl
), cfis
, sizeof(struct sata_fis_h2d
));
436 sg_count
= ahci_fill_sg(probe_ent
, port
, buf
, buf_len
);
437 opts
= (sizeof(struct sata_fis_h2d
) >> 2) | (sg_count
<< 16);
440 flush_cache((ulong
)buf
, buf_len
);
442 ahci_fill_cmd_slot(pp
, cmd_slot
, opts
);
444 flush_cache((int)(pp
->cmd_slot
), AHCI_PORT_PRIV_DMA_SZ
);
445 writel_with_flush(1 << cmd_slot
, &(port_mmio
->ci
));
447 if (waiting_for_cmd_completed((u8
*)&(port_mmio
->ci
),
448 10000, 0x1 << cmd_slot
)) {
449 printf("timeout exit!\n");
452 invalidate_dcache_range((int)(pp
->cmd_slot
),
453 (int)(pp
->cmd_slot
)+AHCI_PORT_PRIV_DMA_SZ
);
454 debug("ahci_exec_ata_cmd: %d byte transferred.\n",
455 pp
->cmd_slot
->status
);
457 invalidate_dcache_range((ulong
)buf
, (ulong
)buf
+buf_len
);
462 static void ahci_set_feature(u8 dev
, u8 port
)
464 struct ahci_probe_ent
*probe_ent
=
465 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
466 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
467 struct sata_fis_h2d
*cfis
= &h2d
;
469 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
470 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
471 cfis
->pm_port_c
= 1 << 7;
472 cfis
->command
= ATA_CMD_SET_FEATURES
;
473 cfis
->features
= SETFEATURES_XFER
;
474 cfis
->sector_count
= ffs(probe_ent
->udma_mask
+ 1) + 0x3e;
476 ahci_exec_ata_cmd(probe_ent
, port
, cfis
, NULL
, 0, READ_CMD
);
479 static int ahci_port_start(struct ahci_probe_ent
*probe_ent
,
482 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
483 struct sata_port_regs
*port_mmio
=
484 (struct sata_port_regs
*)pp
->port_mmio
;
487 int timeout
= 10000000;
489 debug("Enter start port: %d\n", port
);
490 port_status
= readl(&(port_mmio
->ssts
));
491 debug("Port %d status: %x\n", port
, port_status
);
492 if ((port_status
& 0xf) != 0x03) {
493 printf("No Link on this port!\n");
497 mem
= (u32
)malloc(AHCI_PORT_PRIV_DMA_SZ
+ 1024);
500 printf("No mem for table!\n");
504 mem
= (mem
+ 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
505 memset((u8
*)mem
, 0, AHCI_PORT_PRIV_DMA_SZ
);
508 * First item in chunk of DMA memory: 32-slot command table,
509 * 32 bytes each in size
511 pp
->cmd_slot
= (struct ahci_cmd_hdr
*)mem
;
512 debug("cmd_slot = 0x%x\n", (unsigned int) pp
->cmd_slot
);
513 mem
+= (AHCI_CMD_SLOT_SZ
* DWC_AHSATA_MAX_CMD_SLOTS
);
516 * Second item: Received-FIS area, 256-Byte aligned
519 mem
+= AHCI_RX_FIS_SZ
;
522 * Third item: data area for storing a single command
523 * and its scatter-gather table
526 debug("cmd_tbl_dma = 0x%lx\n", pp
->cmd_tbl
);
528 mem
+= AHCI_CMD_TBL_HDR
;
530 writel_with_flush(0x00004444, &(port_mmio
->dmacr
));
531 pp
->cmd_tbl_sg
= (struct ahci_sg
*)mem
;
532 writel_with_flush((u32
)pp
->cmd_slot
, &(port_mmio
->clb
));
533 writel_with_flush(pp
->rx_fis
, &(port_mmio
->fb
));
536 writel_with_flush((SATA_PORT_CMD_FRE
| readl(&(port_mmio
->cmd
))),
539 /* Wait device ready */
540 while ((readl(&(port_mmio
->tfd
)) & (SATA_PORT_TFD_STS_ERR
|
541 SATA_PORT_TFD_STS_DRQ
| SATA_PORT_TFD_STS_BSY
))
545 debug("Device not ready for BSY, DRQ and"
550 writel_with_flush(PORT_CMD_ICC_ACTIVE
| PORT_CMD_FIS_RX
|
551 PORT_CMD_POWER_ON
| PORT_CMD_SPIN_UP
|
552 PORT_CMD_START
, &(port_mmio
->cmd
));
554 debug("Exit start port %d\n", port
);
559 int init_sata(int dev
)
563 struct ahci_probe_ent
*probe_ent
= NULL
;
565 #if defined(CONFIG_MX6)
566 if (!is_cpu_type(MXC_CPU_MX6Q
) && !is_cpu_type(MXC_CPU_MX6D
))
569 if (dev
< 0 || dev
> (CONFIG_SYS_SATA_MAX_DEVICE
- 1)) {
570 printf("The sata index %d is out of ranges\n\r", dev
);
576 probe_ent
= (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
577 linkmap
= probe_ent
->link_port_map
;
580 printf("No port device detected!\n");
584 for (i
= 0; i
< probe_ent
->n_ports
; i
++) {
585 if ((linkmap
>> i
) && ((linkmap
>> i
) & 0x01)) {
586 if (ahci_port_start(probe_ent
, (u8
)i
)) {
587 printf("Can not start port %d\n", i
);
590 probe_ent
->hard_port_no
= i
;
598 int reset_sata(int dev
)
600 struct ahci_probe_ent
*probe_ent
;
601 struct sata_host_regs
*host_mmio
;
603 if (dev
< 0 || dev
> (CONFIG_SYS_SATA_MAX_DEVICE
- 1)) {
604 printf("The sata index %d is out of ranges\n\r", dev
);
608 probe_ent
= (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
609 if (NULL
== probe_ent
)
610 /* not initialized, so nothing to reset */
613 host_mmio
= (struct sata_host_regs
*)probe_ent
->mmio_base
;
614 setbits_le32(&host_mmio
->ghc
, SATA_HOST_GHC_HR
);
615 while (readl(&host_mmio
->ghc
) & SATA_HOST_GHC_HR
)
621 static void dwc_ahsata_print_info(int dev
)
623 struct blk_desc
*pdev
= &(sata_dev_desc
[dev
]);
625 printf("SATA Device Info:\n\r");
626 #ifdef CONFIG_SYS_64BIT_LBA
627 printf("S/N: %s\n\rProduct model number: %s\n\r"
628 "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
629 pdev
->product
, pdev
->vendor
, pdev
->revision
, pdev
->lba
);
631 printf("S/N: %s\n\rProduct model number: %s\n\r"
632 "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
633 pdev
->product
, pdev
->vendor
, pdev
->revision
, pdev
->lba
);
637 static void dwc_ahsata_identify(int dev
, u16
*id
)
639 struct ahci_probe_ent
*probe_ent
=
640 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
641 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
642 struct sata_fis_h2d
*cfis
= &h2d
;
643 u8 port
= probe_ent
->hard_port_no
;
645 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
647 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
648 cfis
->pm_port_c
= 0x80; /* is command */
649 cfis
->command
= ATA_CMD_ID_ATA
;
651 ahci_exec_ata_cmd(probe_ent
, port
, cfis
,
652 (u8
*)id
, ATA_ID_WORDS
* 2, READ_CMD
);
653 ata_swap_buf_le16(id
, ATA_ID_WORDS
);
656 static void dwc_ahsata_xfer_mode(int dev
, u16
*id
)
658 struct ahci_probe_ent
*probe_ent
=
659 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
661 probe_ent
->pio_mask
= id
[ATA_ID_PIO_MODES
];
662 probe_ent
->udma_mask
= id
[ATA_ID_UDMA_MODES
];
663 debug("pio %04x, udma %04x\n\r",
664 probe_ent
->pio_mask
, probe_ent
->udma_mask
);
667 static u32
dwc_ahsata_rw_cmd(int dev
, u32 start
, u32 blkcnt
,
668 u8
*buffer
, int is_write
)
670 struct ahci_probe_ent
*probe_ent
=
671 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
672 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
673 struct sata_fis_h2d
*cfis
= &h2d
;
674 u8 port
= probe_ent
->hard_port_no
;
679 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
681 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
682 cfis
->pm_port_c
= 0x80; /* is command */
683 cfis
->command
= (is_write
) ? ATA_CMD_WRITE
: ATA_CMD_READ
;
684 cfis
->device
= ATA_LBA
;
686 cfis
->device
|= (block
>> 24) & 0xf;
687 cfis
->lba_high
= (block
>> 16) & 0xff;
688 cfis
->lba_mid
= (block
>> 8) & 0xff;
689 cfis
->lba_low
= block
& 0xff;
690 cfis
->sector_count
= (u8
)(blkcnt
& 0xff);
692 if (ahci_exec_ata_cmd(probe_ent
, port
, cfis
,
693 buffer
, ATA_SECT_SIZE
* blkcnt
, is_write
) > 0)
699 void dwc_ahsata_flush_cache(int dev
)
701 struct ahci_probe_ent
*probe_ent
=
702 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
703 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
704 struct sata_fis_h2d
*cfis
= &h2d
;
705 u8 port
= probe_ent
->hard_port_no
;
707 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
709 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
710 cfis
->pm_port_c
= 0x80; /* is command */
711 cfis
->command
= ATA_CMD_FLUSH
;
713 ahci_exec_ata_cmd(probe_ent
, port
, cfis
, NULL
, 0, 0);
716 static u32
dwc_ahsata_rw_cmd_ext(int dev
, u32 start
, lbaint_t blkcnt
,
717 u8
*buffer
, int is_write
)
719 struct ahci_probe_ent
*probe_ent
=
720 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
721 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
722 struct sata_fis_h2d
*cfis
= &h2d
;
723 u8 port
= probe_ent
->hard_port_no
;
728 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
730 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
731 cfis
->pm_port_c
= 0x80; /* is command */
733 cfis
->command
= (is_write
) ? ATA_CMD_WRITE_EXT
736 cfis
->lba_high_exp
= (block
>> 40) & 0xff;
737 cfis
->lba_mid_exp
= (block
>> 32) & 0xff;
738 cfis
->lba_low_exp
= (block
>> 24) & 0xff;
739 cfis
->lba_high
= (block
>> 16) & 0xff;
740 cfis
->lba_mid
= (block
>> 8) & 0xff;
741 cfis
->lba_low
= block
& 0xff;
742 cfis
->device
= ATA_LBA
;
743 cfis
->sector_count_exp
= (blkcnt
>> 8) & 0xff;
744 cfis
->sector_count
= blkcnt
& 0xff;
746 if (ahci_exec_ata_cmd(probe_ent
, port
, cfis
, buffer
,
747 ATA_SECT_SIZE
* blkcnt
, is_write
) > 0)
753 u32
dwc_ahsata_rw_ncq_cmd(int dev
, u32 start
, lbaint_t blkcnt
,
754 u8
*buffer
, int is_write
)
756 struct ahci_probe_ent
*probe_ent
=
757 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
758 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
759 struct sata_fis_h2d
*cfis
= &h2d
;
760 u8 port
= probe_ent
->hard_port_no
;
763 if (sata_dev_desc
[dev
].lba48
!= 1) {
764 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
770 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
772 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
773 cfis
->pm_port_c
= 0x80; /* is command */
775 cfis
->command
= (is_write
) ? ATA_CMD_FPDMA_WRITE
776 : ATA_CMD_FPDMA_READ
;
778 cfis
->lba_high_exp
= (block
>> 40) & 0xff;
779 cfis
->lba_mid_exp
= (block
>> 32) & 0xff;
780 cfis
->lba_low_exp
= (block
>> 24) & 0xff;
781 cfis
->lba_high
= (block
>> 16) & 0xff;
782 cfis
->lba_mid
= (block
>> 8) & 0xff;
783 cfis
->lba_low
= block
& 0xff;
785 cfis
->device
= ATA_LBA
;
786 cfis
->features_exp
= (blkcnt
>> 8) & 0xff;
787 cfis
->features
= blkcnt
& 0xff;
789 /* Use the latest queue */
790 ahci_exec_ata_cmd(probe_ent
, port
, cfis
,
791 buffer
, ATA_SECT_SIZE
* blkcnt
, is_write
);
796 void dwc_ahsata_flush_cache_ext(int dev
)
798 struct ahci_probe_ent
*probe_ent
=
799 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
800 struct sata_fis_h2d h2d
__aligned(ARCH_DMA_MINALIGN
);
801 struct sata_fis_h2d
*cfis
= &h2d
;
802 u8 port
= probe_ent
->hard_port_no
;
804 memset(cfis
, 0, sizeof(struct sata_fis_h2d
));
806 cfis
->fis_type
= SATA_FIS_TYPE_REGISTER_H2D
;
807 cfis
->pm_port_c
= 0x80; /* is command */
808 cfis
->command
= ATA_CMD_FLUSH_EXT
;
810 ahci_exec_ata_cmd(probe_ent
, port
, cfis
, NULL
, 0, 0);
813 static void dwc_ahsata_init_wcache(int dev
, u16
*id
)
815 struct ahci_probe_ent
*probe_ent
=
816 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
818 if (ata_id_has_wcache(id
) && ata_id_wcache_enabled(id
))
819 probe_ent
->flags
|= SATA_FLAG_WCACHE
;
820 if (ata_id_has_flush(id
))
821 probe_ent
->flags
|= SATA_FLAG_FLUSH
;
822 if (ata_id_has_flush_ext(id
))
823 probe_ent
->flags
|= SATA_FLAG_FLUSH_EXT
;
826 u32
ata_low_level_rw_lba48(int dev
, u32 blknr
, lbaint_t blkcnt
,
827 const void *buffer
, int is_write
)
837 max_blks
= ATA_MAX_SECTORS_LBA48
;
840 if (blks
> max_blks
) {
841 if (max_blks
!= dwc_ahsata_rw_cmd_ext(dev
, start
,
842 max_blks
, addr
, is_write
))
846 addr
+= ATA_SECT_SIZE
* max_blks
;
848 if (blks
!= dwc_ahsata_rw_cmd_ext(dev
, start
,
849 blks
, addr
, is_write
))
853 addr
+= ATA_SECT_SIZE
* blks
;
860 u32
ata_low_level_rw_lba28(int dev
, u32 blknr
, lbaint_t blkcnt
,
861 const void *buffer
, int is_write
)
871 max_blks
= ATA_MAX_SECTORS
;
873 if (blks
> max_blks
) {
874 if (max_blks
!= dwc_ahsata_rw_cmd(dev
, start
,
875 max_blks
, addr
, is_write
))
879 addr
+= ATA_SECT_SIZE
* max_blks
;
881 if (blks
!= dwc_ahsata_rw_cmd(dev
, start
,
882 blks
, addr
, is_write
))
886 addr
+= ATA_SECT_SIZE
* blks
;
893 int sata_port_status(int dev
, int port
)
895 struct sata_port_regs
*port_mmio
;
896 struct ahci_probe_ent
*probe_ent
= NULL
;
898 if (dev
< 0 || dev
> (CONFIG_SYS_SATA_MAX_DEVICE
- 1))
901 if (sata_dev_desc
[dev
].priv
== NULL
)
904 probe_ent
= (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
905 port_mmio
= (struct sata_port_regs
*)probe_ent
->port
[port
].port_mmio
;
907 return readl(&(port_mmio
->ssts
)) & SATA_PORT_SSTS_DET_MASK
;
911 * SATA interface between low level driver and command layer
913 ulong
sata_read(int dev
, ulong blknr
, lbaint_t blkcnt
, void *buffer
)
917 if (sata_dev_desc
[dev
].lba48
)
918 rc
= ata_low_level_rw_lba48(dev
, blknr
, blkcnt
,
921 rc
= ata_low_level_rw_lba28(dev
, blknr
, blkcnt
,
926 ulong
sata_write(int dev
, ulong blknr
, lbaint_t blkcnt
, const void *buffer
)
929 struct ahci_probe_ent
*probe_ent
=
930 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
931 u32 flags
= probe_ent
->flags
;
933 if (sata_dev_desc
[dev
].lba48
) {
934 rc
= ata_low_level_rw_lba48(dev
, blknr
, blkcnt
,
936 if ((flags
& SATA_FLAG_WCACHE
) &&
937 (flags
& SATA_FLAG_FLUSH_EXT
))
938 dwc_ahsata_flush_cache_ext(dev
);
940 rc
= ata_low_level_rw_lba28(dev
, blknr
, blkcnt
,
942 if ((flags
& SATA_FLAG_WCACHE
) &&
943 (flags
& SATA_FLAG_FLUSH
))
944 dwc_ahsata_flush_cache(dev
);
949 int scan_sata(int dev
)
951 u8 serial
[ATA_ID_SERNO_LEN
+ 1] = { 0 };
952 u8 firmware
[ATA_ID_FW_REV_LEN
+ 1] = { 0 };
953 u8 product
[ATA_ID_PROD_LEN
+ 1] = { 0 };
956 struct ahci_probe_ent
*probe_ent
=
957 (struct ahci_probe_ent
*)sata_dev_desc
[dev
].priv
;
958 u8 port
= probe_ent
->hard_port_no
;
959 struct blk_desc
*pdev
= &(sata_dev_desc
[dev
]);
961 id
= (u16
*)memalign(ARCH_DMA_MINALIGN
,
962 roundup(ARCH_DMA_MINALIGN
,
963 (ATA_ID_WORDS
* 2)));
965 printf("id malloc failed\n\r");
969 /* Identify device to get information */
970 dwc_ahsata_identify(dev
, id
);
973 ata_id_c_string(id
, serial
, ATA_ID_SERNO
, sizeof(serial
));
974 memcpy(pdev
->product
, serial
, sizeof(serial
));
976 /* Firmware version */
977 ata_id_c_string(id
, firmware
, ATA_ID_FW_REV
, sizeof(firmware
));
978 memcpy(pdev
->revision
, firmware
, sizeof(firmware
));
981 ata_id_c_string(id
, product
, ATA_ID_PROD
, sizeof(product
));
982 memcpy(pdev
->vendor
, product
, sizeof(product
));
985 n_sectors
= ata_id_n_sectors(id
);
986 pdev
->lba
= (u32
)n_sectors
;
988 pdev
->type
= DEV_TYPE_HARDDISK
;
989 pdev
->blksz
= ATA_SECT_SIZE
;
992 /* Check if support LBA48 */
993 if (ata_id_has_lba48(id
)) {
995 debug("Device support LBA48\n\r");
998 /* Get the NCQ queue depth from device */
999 probe_ent
->flags
&= (~SATA_FLAG_Q_DEP_MASK
);
1000 probe_ent
->flags
|= ata_id_queue_depth(id
);
1002 /* Get the xfer mode from device */
1003 dwc_ahsata_xfer_mode(dev
, id
);
1005 /* Get the write cache status from device */
1006 dwc_ahsata_init_wcache(dev
, id
);
1008 /* Set the xfer mode to highest speed */
1009 ahci_set_feature(dev
, port
);
1013 dwc_ahsata_print_info(dev
);