2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 * Terry Lv <r65388@freescale.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
31 #include <linux/ctype.h>
32 #include <asm/errno.h>
34 #include <linux/bitops.h>
35 #include <asm/arch/clock.h>
36 #include "dwc_ahsata.h"
38 struct sata_port_regs {
62 struct sata_host_regs {
91 #define MAX_DATA_BYTES_PER_SG (4 * 1024 * 1024)
92 #define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
94 #define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
98 static inline u32 ahci_port_base(u32 base, u32 port)
100 return base + 0x100 + (port * 0x80);
103 static int waiting_for_cmd_completed(u8 *offset,
111 ((status = readl(offset)) & sign) && i < timeout_msec;
115 return (i < timeout_msec) ? 0 : -1;
118 static int ahci_setup_oobr(struct ahci_probe_ent *probe_ent,
121 struct sata_host_regs *host_mmio =
122 (struct sata_host_regs *)probe_ent->mmio_base;
124 writel(SATA_HOST_OOBR_WE, &(host_mmio->oobr));
125 writel(0x02060b14, &(host_mmio->oobr));
130 static int ahci_host_init(struct ahci_probe_ent *probe_ent)
132 u32 tmp, cap_save, num_ports;
133 int i, j, timeout = 1000;
134 struct sata_port_regs *port_mmio = NULL;
135 struct sata_host_regs *host_mmio =
136 (struct sata_host_regs *)probe_ent->mmio_base;
137 int clk = mxc_get_clock(MXC_SATA_CLK);
139 cap_save = readl(&(host_mmio->cap));
140 cap_save |= SATA_HOST_CAP_SSS;
142 /* global controller reset */
143 tmp = readl(&(host_mmio->ghc));
144 if ((tmp & SATA_HOST_GHC_HR) == 0)
145 writel_with_flush(tmp | SATA_HOST_GHC_HR, &(host_mmio->ghc));
147 while ((readl(&(host_mmio->ghc)) & SATA_HOST_GHC_HR)
152 debug("controller reset failed (0x%x)\n", tmp);
157 writel(clk / 1000, &(host_mmio->timer1ms));
159 ahci_setup_oobr(probe_ent, 0);
161 writel_with_flush(SATA_HOST_GHC_AE, &(host_mmio->ghc));
162 writel(cap_save, &(host_mmio->cap));
163 num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
164 writel_with_flush((1 << num_ports) - 1,
168 * Determine which Ports are implemented by the DWC_ahsata,
169 * by reading the PI register. This bit map value aids the
170 * software to determine how many Ports are available and
171 * which Port registers need to be initialized.
173 probe_ent->cap = readl(&(host_mmio->cap));
174 probe_ent->port_map = readl(&(host_mmio->pi));
176 /* Determine how many command slots the HBA supports */
178 (probe_ent->cap & SATA_HOST_CAP_NP_MASK) + 1;
180 debug("cap 0x%x port_map 0x%x n_ports %d\n",
181 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
183 for (i = 0; i < probe_ent->n_ports; i++) {
184 probe_ent->port[i].port_mmio =
185 ahci_port_base((u32)host_mmio, i);
187 (struct sata_port_regs *)probe_ent->port[i].port_mmio;
189 /* Ensure that the DWC_ahsata is in idle state */
190 tmp = readl(&(port_mmio->cmd));
193 * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
194 * are all cleared, the Port is in an idle state.
196 if (tmp & (SATA_PORT_CMD_CR | SATA_PORT_CMD_FR |
197 SATA_PORT_CMD_FRE | SATA_PORT_CMD_ST)) {
200 * System software places a Port into the idle state by
201 * clearing P#CMD.ST and waiting for P#CMD.CR to return
204 tmp &= ~SATA_PORT_CMD_ST;
205 writel_with_flush(tmp, &(port_mmio->cmd));
208 * spec says 500 msecs for each bit, so
209 * this is slightly incorrect.
214 while ((readl(&(port_mmio->cmd)) & SATA_PORT_CMD_CR)
219 debug("port reset failed (0x%x)\n", tmp);
225 tmp = readl(&(port_mmio->cmd));
226 writel((tmp | SATA_PORT_CMD_SUD), &(port_mmio->cmd));
228 /* Wait for spin-up to finish */
230 while (!(readl(&(port_mmio->cmd)) | SATA_PORT_CMD_SUD)
234 debug("Spin-Up can't finish!\n");
238 for (j = 0; j < 100; ++j) {
240 tmp = readl(&(port_mmio->ssts));
241 if (((tmp & SATA_PORT_SSTS_DET_MASK) == 0x3) ||
242 ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x1))
246 /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
248 while (!(readl(&(port_mmio->serr)) | SATA_PORT_SERR_DIAG_X)
252 debug("Can't find DIAG_X set!\n");
257 * For each implemented Port, clear the P#SERR
258 * register, by writing ones to each implemented\
261 tmp = readl(&(port_mmio->serr));
262 debug("P#SERR 0x%x\n",
264 writel(tmp, &(port_mmio->serr));
266 /* Ack any pending irq events for this port */
267 tmp = readl(&(host_mmio->is));
268 debug("IS 0x%x\n", tmp);
270 writel(tmp, &(host_mmio->is));
272 writel(1 << i, &(host_mmio->is));
274 /* set irq mask (enables interrupts) */
275 writel(DEF_PORT_IRQ, &(port_mmio->ie));
277 /* register linkup ports */
278 tmp = readl(&(port_mmio->ssts));
279 debug("Port %d status: 0x%x\n", i, tmp);
280 if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
281 probe_ent->link_port_map |= (0x01 << i);
284 tmp = readl(&(host_mmio->ghc));
285 debug("GHC 0x%x\n", tmp);
286 writel(tmp | SATA_HOST_GHC_IE, &(host_mmio->ghc));
287 tmp = readl(&(host_mmio->ghc));
288 debug("GHC 0x%x\n", tmp);
293 static void ahci_print_info(struct ahci_probe_ent *probe_ent)
295 struct sata_host_regs *host_mmio =
296 (struct sata_host_regs *)probe_ent->mmio_base;
297 u32 vers, cap, impl, speed;
301 vers = readl(&(host_mmio->vs));
302 cap = probe_ent->cap;
303 impl = probe_ent->port_map;
305 speed = (cap & SATA_HOST_CAP_ISS_MASK)
306 >> SATA_HOST_CAP_ISS_OFFSET;
316 printf("AHCI %02x%02x.%02x%02x "
317 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
322 ((cap >> 8) & 0x1f) + 1,
331 cap & (1 << 31) ? "64bit " : "",
332 cap & (1 << 30) ? "ncq " : "",
333 cap & (1 << 28) ? "ilck " : "",
334 cap & (1 << 27) ? "stag " : "",
335 cap & (1 << 26) ? "pm " : "",
336 cap & (1 << 25) ? "led " : "",
337 cap & (1 << 24) ? "clo " : "",
338 cap & (1 << 19) ? "nz " : "",
339 cap & (1 << 18) ? "only " : "",
340 cap & (1 << 17) ? "pmp " : "",
341 cap & (1 << 15) ? "pio " : "",
342 cap & (1 << 14) ? "slum " : "",
343 cap & (1 << 13) ? "part " : "");
346 static int ahci_init_one(int pdev)
349 struct ahci_probe_ent *probe_ent = NULL;
351 probe_ent = malloc(sizeof(struct ahci_probe_ent));
352 memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
353 probe_ent->dev = pdev;
355 probe_ent->host_flags = ATA_FLAG_SATA
361 probe_ent->mmio_base = CONFIG_DWC_AHSATA_BASE_ADDR;
363 /* initialize adapter */
364 rc = ahci_host_init(probe_ent);
368 ahci_print_info(probe_ent);
370 /* Save the private struct to block device struct */
371 sata_dev_desc[pdev].priv = (void *)probe_ent;
379 static int ahci_fill_sg(struct ahci_probe_ent *probe_ent,
380 u8 port, unsigned char *buf, int buf_len)
382 struct ahci_ioports *pp = &(probe_ent->port[port]);
383 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
384 u32 sg_count, max_bytes;
387 max_bytes = MAX_DATA_BYTES_PER_SG;
388 sg_count = ((buf_len - 1) / max_bytes) + 1;
389 if (sg_count > AHCI_MAX_SG) {
390 printf("Error:Too much sg!\n");
394 for (i = 0; i < sg_count; i++) {
396 cpu_to_le32((u32)buf + i * max_bytes);
397 ahci_sg->addr_hi = 0;
398 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
403 buf_len -= max_bytes;
409 static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
411 struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
412 AHCI_CMD_SLOT_SZ * cmd_slot);
414 memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
415 cmd_hdr->opts = cpu_to_le32(opts);
417 cmd_hdr->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
418 cmd_hdr->tbl_addr_hi = 0;
421 #define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
423 static int ahci_exec_ata_cmd(struct ahci_probe_ent *probe_ent,
424 u8 port, struct sata_fis_h2d *cfis,
425 u8 *buf, u32 buf_len, s32 is_write)
427 struct ahci_ioports *pp = &(probe_ent->port[port]);
428 struct sata_port_regs *port_mmio =
429 (struct sata_port_regs *)pp->port_mmio;
431 int sg_count = 0, cmd_slot = 0;
433 cmd_slot = AHCI_GET_CMD_SLOT(readl(&(port_mmio->ci)));
434 if (32 == cmd_slot) {
435 printf("Can't find empty command slot!\n");
439 /* Check xfer length */
440 if (buf_len > MAX_BYTES_PER_TRANS) {
441 printf("Max transfer length is %dB\n\r",
442 MAX_BYTES_PER_TRANS);
446 memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
448 sg_count = ahci_fill_sg(probe_ent, port, buf, buf_len);
449 opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
452 flush_cache((ulong)buf, buf_len);
454 ahci_fill_cmd_slot(pp, cmd_slot, opts);
456 flush_cache((int)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
457 writel_with_flush(1 << cmd_slot, &(port_mmio->ci));
459 if (waiting_for_cmd_completed((u8 *)&(port_mmio->ci),
460 10000, 0x1 << cmd_slot)) {
461 printf("timeout exit!\n");
464 invalidate_dcache_range((int)(pp->cmd_slot),
465 (int)(pp->cmd_slot)+AHCI_PORT_PRIV_DMA_SZ);
466 debug("ahci_exec_ata_cmd: %d byte transferred.\n",
467 pp->cmd_slot->status);
469 invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
474 static void ahci_set_feature(u8 dev, u8 port)
476 struct ahci_probe_ent *probe_ent =
477 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
478 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
479 struct sata_fis_h2d *cfis = &h2d;
481 memset(cfis, 0, sizeof(struct sata_fis_h2d));
482 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
483 cfis->pm_port_c = 1 << 7;
484 cfis->command = ATA_CMD_SET_FEATURES;
485 cfis->features = SETFEATURES_XFER;
486 cfis->sector_count = ffs(probe_ent->udma_mask + 1) + 0x3e;
488 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, READ_CMD);
491 static int ahci_port_start(struct ahci_probe_ent *probe_ent,
494 struct ahci_ioports *pp = &(probe_ent->port[port]);
495 struct sata_port_regs *port_mmio =
496 (struct sata_port_regs *)pp->port_mmio;
499 int timeout = 10000000;
501 debug("Enter start port: %d\n", port);
502 port_status = readl(&(port_mmio->ssts));
503 debug("Port %d status: %x\n", port, port_status);
504 if ((port_status & 0xf) != 0x03) {
505 printf("No Link on this port!\n");
509 mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
512 printf("No mem for table!\n");
516 mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
517 memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
520 * First item in chunk of DMA memory: 32-slot command table,
521 * 32 bytes each in size
523 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
524 debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
525 mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
528 * Second item: Received-FIS area, 256-Byte aligned
531 mem += AHCI_RX_FIS_SZ;
534 * Third item: data area for storing a single command
535 * and its scatter-gather table
538 debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
540 mem += AHCI_CMD_TBL_HDR;
542 writel_with_flush(0x00004444, &(port_mmio->dmacr));
543 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
544 writel_with_flush((u32)pp->cmd_slot, &(port_mmio->clb));
545 writel_with_flush(pp->rx_fis, &(port_mmio->fb));
548 writel_with_flush((SATA_PORT_CMD_FRE | readl(&(port_mmio->cmd))),
551 /* Wait device ready */
552 while ((readl(&(port_mmio->tfd)) & (SATA_PORT_TFD_STS_ERR |
553 SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
557 debug("Device not ready for BSY, DRQ and"
562 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
563 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
564 PORT_CMD_START, &(port_mmio->cmd));
566 debug("Exit start port %d\n", port);
571 int init_sata(int dev)
575 struct ahci_probe_ent *probe_ent = NULL;
577 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
578 printf("The sata index %d is out of ranges\n\r", dev);
584 probe_ent = (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
585 linkmap = probe_ent->link_port_map;
588 printf("No port device detected!\n");
592 for (i = 0; i < probe_ent->n_ports; i++) {
593 if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
594 if (ahci_port_start(probe_ent, (u8)i)) {
595 printf("Can not start port %d\n", i);
598 probe_ent->hard_port_no = i;
606 static void dwc_ahsata_print_info(int dev)
608 block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
610 printf("SATA Device Info:\n\r");
611 #ifdef CONFIG_SYS_64BIT_LBA
612 printf("S/N: %s\n\rProduct model number: %s\n\r"
613 "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
614 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
616 printf("S/N: %s\n\rProduct model number: %s\n\r"
617 "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
618 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
622 static void dwc_ahsata_identify(int dev, u16 *id)
624 struct ahci_probe_ent *probe_ent =
625 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
626 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
627 struct sata_fis_h2d *cfis = &h2d;
628 u8 port = probe_ent->hard_port_no;
630 memset(cfis, 0, sizeof(struct sata_fis_h2d));
632 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
633 cfis->pm_port_c = 0x80; /* is command */
634 cfis->command = ATA_CMD_ID_ATA;
636 ahci_exec_ata_cmd(probe_ent, port, cfis,
637 (u8 *)id, ATA_ID_WORDS * 2, READ_CMD);
638 ata_swap_buf_le16(id, ATA_ID_WORDS);
641 static void dwc_ahsata_xfer_mode(int dev, u16 *id)
643 struct ahci_probe_ent *probe_ent =
644 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
646 probe_ent->pio_mask = id[ATA_ID_PIO_MODES];
647 probe_ent->udma_mask = id[ATA_ID_UDMA_MODES];
648 debug("pio %04x, udma %04x\n\r",
649 probe_ent->pio_mask, probe_ent->udma_mask);
652 static u32 dwc_ahsata_rw_cmd(int dev, u32 start, u32 blkcnt,
653 u8 *buffer, int is_write)
655 struct ahci_probe_ent *probe_ent =
656 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
657 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
658 struct sata_fis_h2d *cfis = &h2d;
659 u8 port = probe_ent->hard_port_no;
664 memset(cfis, 0, sizeof(struct sata_fis_h2d));
666 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
667 cfis->pm_port_c = 0x80; /* is command */
668 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
669 cfis->device = ATA_LBA;
671 cfis->device |= (block >> 24) & 0xf;
672 cfis->lba_high = (block >> 16) & 0xff;
673 cfis->lba_mid = (block >> 8) & 0xff;
674 cfis->lba_low = block & 0xff;
675 cfis->sector_count = (u8)(blkcnt & 0xff);
677 if (ahci_exec_ata_cmd(probe_ent, port, cfis,
678 buffer, ATA_SECT_SIZE * blkcnt, is_write) > 0)
684 void dwc_ahsata_flush_cache(int dev)
686 struct ahci_probe_ent *probe_ent =
687 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
688 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
689 struct sata_fis_h2d *cfis = &h2d;
690 u8 port = probe_ent->hard_port_no;
692 memset(cfis, 0, sizeof(struct sata_fis_h2d));
694 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
695 cfis->pm_port_c = 0x80; /* is command */
696 cfis->command = ATA_CMD_FLUSH;
698 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
701 static u32 dwc_ahsata_rw_cmd_ext(int dev, u32 start, lbaint_t blkcnt,
702 u8 *buffer, int is_write)
704 struct ahci_probe_ent *probe_ent =
705 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
706 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
707 struct sata_fis_h2d *cfis = &h2d;
708 u8 port = probe_ent->hard_port_no;
713 memset(cfis, 0, sizeof(struct sata_fis_h2d));
715 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
716 cfis->pm_port_c = 0x80; /* is command */
718 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
721 cfis->lba_high_exp = (block >> 40) & 0xff;
722 cfis->lba_mid_exp = (block >> 32) & 0xff;
723 cfis->lba_low_exp = (block >> 24) & 0xff;
724 cfis->lba_high = (block >> 16) & 0xff;
725 cfis->lba_mid = (block >> 8) & 0xff;
726 cfis->lba_low = block & 0xff;
727 cfis->device = ATA_LBA;
728 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
729 cfis->sector_count = blkcnt & 0xff;
731 if (ahci_exec_ata_cmd(probe_ent, port, cfis, buffer,
732 ATA_SECT_SIZE * blkcnt, is_write) > 0)
738 u32 dwc_ahsata_rw_ncq_cmd(int dev, u32 start, lbaint_t blkcnt,
739 u8 *buffer, int is_write)
741 struct ahci_probe_ent *probe_ent =
742 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
743 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
744 struct sata_fis_h2d *cfis = &h2d;
745 u8 port = probe_ent->hard_port_no;
748 if (sata_dev_desc[dev].lba48 != 1) {
749 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
755 memset(cfis, 0, sizeof(struct sata_fis_h2d));
757 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
758 cfis->pm_port_c = 0x80; /* is command */
760 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
761 : ATA_CMD_FPDMA_READ;
763 cfis->lba_high_exp = (block >> 40) & 0xff;
764 cfis->lba_mid_exp = (block >> 32) & 0xff;
765 cfis->lba_low_exp = (block >> 24) & 0xff;
766 cfis->lba_high = (block >> 16) & 0xff;
767 cfis->lba_mid = (block >> 8) & 0xff;
768 cfis->lba_low = block & 0xff;
770 cfis->device = ATA_LBA;
771 cfis->features_exp = (blkcnt >> 8) & 0xff;
772 cfis->features = blkcnt & 0xff;
774 /* Use the latest queue */
775 ahci_exec_ata_cmd(probe_ent, port, cfis,
776 buffer, ATA_SECT_SIZE * blkcnt, is_write);
781 void dwc_ahsata_flush_cache_ext(int dev)
783 struct ahci_probe_ent *probe_ent =
784 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
785 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
786 struct sata_fis_h2d *cfis = &h2d;
787 u8 port = probe_ent->hard_port_no;
789 memset(cfis, 0, sizeof(struct sata_fis_h2d));
791 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
792 cfis->pm_port_c = 0x80; /* is command */
793 cfis->command = ATA_CMD_FLUSH_EXT;
795 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
798 static void dwc_ahsata_init_wcache(int dev, u16 *id)
800 struct ahci_probe_ent *probe_ent =
801 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
803 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
804 probe_ent->flags |= SATA_FLAG_WCACHE;
805 if (ata_id_has_flush(id))
806 probe_ent->flags |= SATA_FLAG_FLUSH;
807 if (ata_id_has_flush_ext(id))
808 probe_ent->flags |= SATA_FLAG_FLUSH_EXT;
811 u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
812 const void *buffer, int is_write)
822 max_blks = ATA_MAX_SECTORS_LBA48;
825 if (blks > max_blks) {
826 if (max_blks != dwc_ahsata_rw_cmd_ext(dev, start,
827 max_blks, addr, is_write))
831 addr += ATA_SECT_SIZE * max_blks;
833 if (blks != dwc_ahsata_rw_cmd_ext(dev, start,
834 blks, addr, is_write))
838 addr += ATA_SECT_SIZE * blks;
845 u32 ata_low_level_rw_lba28(int dev, u32 blknr, lbaint_t blkcnt,
846 const void *buffer, int is_write)
856 max_blks = ATA_MAX_SECTORS;
858 if (blks > max_blks) {
859 if (max_blks != dwc_ahsata_rw_cmd(dev, start,
860 max_blks, addr, is_write))
864 addr += ATA_SECT_SIZE * max_blks;
866 if (blks != dwc_ahsata_rw_cmd(dev, start,
867 blks, addr, is_write))
871 addr += ATA_SECT_SIZE * blks;
879 * SATA interface between low level driver and command layer
881 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
885 if (sata_dev_desc[dev].lba48)
886 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
889 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
894 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
897 struct ahci_probe_ent *probe_ent =
898 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
899 u32 flags = probe_ent->flags;
901 if (sata_dev_desc[dev].lba48) {
902 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
904 if ((flags & SATA_FLAG_WCACHE) &&
905 (flags & SATA_FLAG_FLUSH_EXT))
906 dwc_ahsata_flush_cache_ext(dev);
908 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
910 if ((flags & SATA_FLAG_WCACHE) &&
911 (flags & SATA_FLAG_FLUSH))
912 dwc_ahsata_flush_cache(dev);
917 int scan_sata(int dev)
919 u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
920 u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
921 u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
924 struct ahci_probe_ent *probe_ent =
925 (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
926 u8 port = probe_ent->hard_port_no;
927 block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
929 id = (u16 *)memalign(ARCH_DMA_MINALIGN,
930 roundup(ARCH_DMA_MINALIGN,
931 (ATA_ID_WORDS * 2)));
933 printf("id malloc failed\n\r");
937 /* Identify device to get information */
938 dwc_ahsata_identify(dev, id);
941 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
942 memcpy(pdev->product, serial, sizeof(serial));
944 /* Firmware version */
945 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
946 memcpy(pdev->revision, firmware, sizeof(firmware));
949 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
950 memcpy(pdev->vendor, product, sizeof(product));
953 n_sectors = ata_id_n_sectors(id);
954 pdev->lba = (u32)n_sectors;
956 pdev->type = DEV_TYPE_HARDDISK;
957 pdev->blksz = ATA_SECT_SIZE;
960 /* Check if support LBA48 */
961 if (ata_id_has_lba48(id)) {
963 debug("Device support LBA48\n\r");
966 /* Get the NCQ queue depth from device */
967 probe_ent->flags &= (~SATA_FLAG_Q_DEP_MASK);
968 probe_ent->flags |= ata_id_queue_depth(id);
970 /* Get the xfer mode from device */
971 dwc_ahsata_xfer_mode(dev, id);
973 /* Get the write cache status from device */
974 dwc_ahsata_init_wcache(dev, id);
976 /* Set the xfer mode to highest speed */
977 ahci_set_feature(dev, port);
981 dwc_ahsata_print_info(dev);