2 * Copyright (C) 2008,2010 Freescale Semiconductor, Inc.
3 * Dave Liu <daveliu@freescale.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/processor.h>
25 #include <asm/fsl_serdes.h>
31 extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
33 #ifndef CONFIG_SYS_SATA1_FLAGS
34 #define CONFIG_SYS_SATA1_FLAGS FLAGS_DMA
36 #ifndef CONFIG_SYS_SATA2_FLAGS
37 #define CONFIG_SYS_SATA2_FLAGS FLAGS_DMA
40 static struct fsl_sata_info fsl_sata_info[] = {
42 {CONFIG_SYS_SATA1, CONFIG_SYS_SATA1_FLAGS},
47 {CONFIG_SYS_SATA2, CONFIG_SYS_SATA2_FLAGS},
53 static inline void sdelay(unsigned long sec)
56 for (i = 0; i < sec; i++)
60 void dprint_buffer(unsigned char *buf, int len)
68 for (i = 0; i < len; i++) {
69 printf("%02x ", *buf++);
79 static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
81 printf("Status FIS dump:\n\r");
82 printf("fis_type: %02x\n\r", s->fis_type);
83 printf("pm_port_i: %02x\n\r", s->pm_port_i);
84 printf("status: %02x\n\r", s->status);
85 printf("error: %02x\n\r", s->error);
86 printf("lba_low: %02x\n\r", s->lba_low);
87 printf("lba_mid: %02x\n\r", s->lba_mid);
88 printf("lba_high: %02x\n\r", s->lba_high);
89 printf("device: %02x\n\r", s->device);
90 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
91 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
92 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
93 printf("res1: %02x\n\r", s->res1);
94 printf("sector_count: %02x\n\r", s->sector_count);
95 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
98 static int ata_wait_register(volatile unsigned *addr, u32 mask,
99 u32 val, u32 timeout_msec)
104 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
105 && i < timeout_msec; i++)
107 return (i < timeout_msec) ? 0 : -1;
110 int init_sata(int dev)
113 cmd_hdr_tbl_t *cmd_hdr;
121 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
122 printf("the sata index %d is out of ranges\n\r", dev);
126 #ifdef CONFIG_MPC85xx
127 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
128 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
131 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
132 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
137 /* Allocate SATA device driver struct */
138 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
140 printf("alloc the sata device struct failed\n\r");
143 /* Zero all of the device driver struct */
144 memset((void *)sata, 0, sizeof(fsl_sata_t));
146 /* Save the private struct to block device struct */
147 sata_dev_desc[dev].priv = (void *)sata;
149 sprintf(sata->name, "SATA%d", dev);
151 /* Set the controller register base address to device struct */
152 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
153 sata->reg_base = reg;
155 /* Allocate the command header table, 4 bytes aligned */
156 length = sizeof(struct cmd_hdr_tbl);
157 align = SATA_HC_CMD_HDR_TBL_ALIGN;
158 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
160 printf("alloc the command header failed\n\r");
164 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
166 sata->cmd_hdr = cmd_hdr;
168 /* Zero all of the command header table */
169 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
171 /* Allocate command descriptor for all command */
172 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
173 align = SATA_HC_CMD_DESC_ALIGN;
174 sata->cmd_desc_offset = (void *)malloc(length + align);
175 if (!sata->cmd_desc_offset) {
176 printf("alloc the command descriptor failed\n\r");
179 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
181 /* Zero all of command descriptor */
182 memset((void *)sata->cmd_desc_offset, 0, length + align);
184 /* Link the command descriptor to command header */
185 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
186 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
187 & ~(CMD_HDR_CDA_ALIGN - 1);
188 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
191 /* To have safe state, force the controller offline */
192 val32 = in_le32(®->hcontrol);
193 val32 &= ~HCONTROL_ONOFF;
194 val32 |= HCONTROL_FORCE_OFFLINE;
195 out_le32(®->hcontrol, val32);
197 /* Wait the controller offline */
198 ata_wait_register(®->hstatus, HSTATUS_ONOFF, 0, 1000);
200 /* Set the command header base address to CHBA register to tell DMA */
201 out_le32(®->chba, (u32)cmd_hdr & ~0x3);
203 /* Snoop for the command header */
204 val32 = in_le32(®->hcontrol);
205 val32 |= HCONTROL_HDR_SNOOP;
206 out_le32(®->hcontrol, val32);
208 /* Disable all of interrupts */
209 val32 = in_le32(®->hcontrol);
210 val32 &= ~HCONTROL_INT_EN_ALL;
211 out_le32(®->hcontrol, val32);
213 /* Clear all of interrupts */
214 val32 = in_le32(®->hstatus);
215 out_le32(®->hstatus, val32);
217 /* Set the ICC, no interrupt coalescing */
218 out_le32(®->icc, 0x01000000);
220 /* No PM attatched, the SATA device direct connect */
221 out_le32(®->cqpmp, 0);
223 /* Clear SError register */
224 val32 = in_le32(®->serror);
225 out_le32(®->serror, val32);
227 /* Clear CER register */
228 val32 = in_le32(®->cer);
229 out_le32(®->cer, val32);
231 /* Clear DER register */
232 val32 = in_le32(®->der);
233 out_le32(®->der, val32);
235 /* No device detection or initialization action requested */
236 out_le32(®->scontrol, 0x00000300);
238 /* Configure the transport layer, default value */
239 out_le32(®->transcfg, 0x08000016);
241 /* Configure the link layer, default value */
242 out_le32(®->linkcfg, 0x0000ff34);
244 /* Bring the controller online */
245 val32 = in_le32(®->hcontrol);
246 val32 |= HCONTROL_ONOFF;
247 out_le32(®->hcontrol, val32);
251 /* print sata device name */
253 printf("%s ", sata->name);
255 printf(" %s ", sata->name);
257 /* Wait PHY RDY signal changed for 500ms */
258 ata_wait_register(®->hstatus, HSTATUS_PHY_RDY,
259 HSTATUS_PHY_RDY, 500);
262 val32 = in_le32(®->hstatus);
263 if (val32 & HSTATUS_PHY_RDY) {
267 printf("(No RDY)\n\r");
271 /* Wait for signature updated, which is 1st D2H */
272 ata_wait_register(®->hstatus, HSTATUS_SIGNATURE,
273 HSTATUS_SIGNATURE, 10000);
275 if (val32 & HSTATUS_SIGNATURE) {
276 sig = in_le32(®->sig);
277 debug("Signature updated, the sig =%08x\n\r", sig);
278 sata->ata_device_type = ata_dev_classify(sig);
281 /* Check the speed */
282 val32 = in_le32(®->sstatus);
283 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
284 printf("(1.5 Gbps)\n\r");
285 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
286 printf("(3 Gbps)\n\r");
291 /* Hardware reset, like Power-on and COMRESET */
292 void fsl_sata_hardware_reset(u32 reg_base)
294 fsl_sata_reg_t *reg = (fsl_sata_reg_t *)reg_base;
297 /* Disable the SATA interface and put PHY offline */
298 scontrol = in_le32(®->scontrol);
299 scontrol = (scontrol & 0x0f0) | 0x304;
300 out_le32(®->scontrol, scontrol);
302 /* No speed strict */
303 scontrol = in_le32(®->scontrol);
304 scontrol = scontrol & ~0x0f0;
305 out_le32(®->scontrol, scontrol);
307 /* Issue PHY wake/reset, Hardware_reset_asserted */
308 scontrol = in_le32(®->scontrol);
309 scontrol = (scontrol & 0x0f0) | 0x301;
310 out_le32(®->scontrol, scontrol);
314 /* Resume PHY, COMRESET negated, the device initialize hardware
315 * and execute diagnostics, send good status-signature to host,
316 * which is D2H register FIS, and then the device enter idle state.
318 scontrol = in_le32(®->scontrol);
319 scontrol = (scontrol & 0x0f0) | 0x300;
320 out_le32(®->scontrol, scontrol);
326 static void fsl_sata_dump_regs(fsl_sata_reg_t *reg)
328 printf("\n\rSATA: %08x\n\r", (u32)reg);
329 printf("CQR: %08x\n\r", in_le32(®->cqr));
330 printf("CAR: %08x\n\r", in_le32(®->car));
331 printf("CCR: %08x\n\r", in_le32(®->ccr));
332 printf("CER: %08x\n\r", in_le32(®->cer));
333 printf("CQR: %08x\n\r", in_le32(®->cqr));
334 printf("DER: %08x\n\r", in_le32(®->der));
335 printf("CHBA: %08x\n\r", in_le32(®->chba));
336 printf("HStatus: %08x\n\r", in_le32(®->hstatus));
337 printf("HControl: %08x\n\r", in_le32(®->hcontrol));
338 printf("CQPMP: %08x\n\r", in_le32(®->cqpmp));
339 printf("SIG: %08x\n\r", in_le32(®->sig));
340 printf("ICC: %08x\n\r", in_le32(®->icc));
341 printf("SStatus: %08x\n\r", in_le32(®->sstatus));
342 printf("SError: %08x\n\r", in_le32(®->serror));
343 printf("SControl: %08x\n\r", in_le32(®->scontrol));
344 printf("SNotification: %08x\n\r", in_le32(®->snotification));
345 printf("TransCfg: %08x\n\r", in_le32(®->transcfg));
346 printf("TransStatus: %08x\n\r", in_le32(®->transstatus));
347 printf("LinkCfg: %08x\n\r", in_le32(®->linkcfg));
348 printf("LinkCfg1: %08x\n\r", in_le32(®->linkcfg1));
349 printf("LinkCfg2: %08x\n\r", in_le32(®->linkcfg2));
350 printf("LinkStatus: %08x\n\r", in_le32(®->linkstatus));
351 printf("LinkStatus1: %08x\n\r", in_le32(®->linkstatus1));
352 printf("PhyCtrlCfg: %08x\n\r", in_le32(®->phyctrlcfg));
353 printf("SYSPR: %08x\n\r", in_be32(®->syspr));
356 static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
357 int is_ncq, int tag, u8 *buffer, u32 len)
359 cmd_hdr_entry_t *cmd_hdr;
360 cmd_desc_t *cmd_desc;
367 fsl_sata_reg_t *reg = sata->reg_base;
370 /* Check xfer length */
371 if (len > SATA_HC_MAX_XFER_LEN) {
372 printf("max transfer length is 64MB\n\r");
376 /* Setup the command descriptor */
377 cmd_desc = sata->cmd_desc + tag;
379 /* Get the pointer cfis of command descriptor */
380 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
382 /* Zero the cfis of command descriptor */
383 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
385 /* Copy the cfis from user to command descriptor */
386 h2d->fis_type = cfis->fis_type;
387 h2d->pm_port_c = cfis->pm_port_c;
388 h2d->command = cfis->command;
390 h2d->features = cfis->features;
391 h2d->features_exp = cfis->features_exp;
393 h2d->lba_low = cfis->lba_low;
394 h2d->lba_mid = cfis->lba_mid;
395 h2d->lba_high = cfis->lba_high;
396 h2d->lba_low_exp = cfis->lba_low_exp;
397 h2d->lba_mid_exp = cfis->lba_mid_exp;
398 h2d->lba_high_exp = cfis->lba_high_exp;
401 h2d->sector_count = cfis->sector_count;
402 h2d->sector_count_exp = cfis->sector_count_exp;
404 h2d->sector_count = (u8)(tag << 3);
407 h2d->device = cfis->device;
408 h2d->control = cfis->control;
410 /* Setup the PRD table */
411 prde = (prd_entry_t *)cmd_desc->prdt;
412 memset((void *)prde, 0, sizeof(struct prdt));
416 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
419 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
420 debug("dba = %08x\n\r", (u32)buffer);
422 if (len < PRD_ENTRY_MAX_XFER_SZ) {
423 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
424 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
425 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
430 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
431 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
432 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
433 buffer += PRD_ENTRY_MAX_XFER_SZ;
434 len -= PRD_ENTRY_MAX_XFER_SZ;
440 /* Setup the command slot of cmd hdr */
441 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
443 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
445 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
446 val32 |= sizeof(sata_fis_h2d_t);
447 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
449 cmd_hdr->ttl = cpu_to_le32(ttl);
452 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
454 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
457 tag &= CMD_HDR_ATTR_TAG;
460 debug("attribute = %08x\n\r", val32);
461 cmd_hdr->attribute = cpu_to_le32(val32);
463 /* Make sure cmd desc and cmd slot valid before commmand issue */
467 val32 = (u32)(h2d->pm_port_c & 0x0f);
468 out_le32(®->cqpmp, val32);
471 if (ata_wait_register(®->car, (1 << tag), 0, 10000))
472 printf("Wait no active time out\n\r");
475 if (!(in_le32(®->cqr) & (1 << tag))) {
477 out_le32(®->cqr, val32);
480 /* Wait command completed for 10s */
481 if (ata_wait_register(®->ccr, (1 << tag), (1 << tag), 10000)) {
483 printf("Non-NCQ command time out\n\r");
485 printf("NCQ command time out\n\r");
488 val32 = in_le32(®->cer);
492 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
493 printf("CE at device\n\r");
494 fsl_sata_dump_regs(reg);
495 der = in_le32(®->der);
496 out_le32(®->cer, val32);
497 out_le32(®->der, der);
500 /* Clear complete flags */
501 val32 = in_le32(®->ccr);
502 out_le32(®->ccr, val32);
507 static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
508 int tag, u8 *buffer, u32 len)
513 static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
514 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
518 if (tag > SATA_HC_MAX_CMD || tag < 0) {
519 printf("tag is out of range, tag=%d\n\r", tag);
523 switch (command_type) {
525 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
528 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
531 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
534 case CMD_VENDOR_BIST:
536 printf("not support now\n\r");
545 static void fsl_sata_identify(int dev, u16 *id)
547 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
548 struct sata_fis_h2d h2d, *cfis = &h2d;
550 memset(cfis, 0, sizeof(struct sata_fis_h2d));
552 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
553 cfis->pm_port_c = 0x80; /* is command */
554 cfis->command = ATA_CMD_ID_ATA;
556 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
557 ata_swap_buf_le16(id, ATA_ID_WORDS);
560 static void fsl_sata_xfer_mode(int dev, u16 *id)
562 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
564 sata->pio = id[ATA_ID_PIO_MODES];
565 sata->mwdma = id[ATA_ID_MWDMA_MODES];
566 sata->udma = id[ATA_ID_UDMA_MODES];
567 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
570 static void fsl_sata_set_features(int dev)
572 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
573 struct sata_fis_h2d h2d, *cfis = &h2d;
576 memset(cfis, 0, sizeof(struct sata_fis_h2d));
578 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
579 cfis->pm_port_c = 0x80; /* is command */
580 cfis->command = ATA_CMD_SET_FEATURES;
581 cfis->features = SETFEATURES_XFER;
583 /* First check the device capablity */
584 udma_cap = (u8)(sata->udma & 0xff);
585 debug("udma_cap %02x\n\r", udma_cap);
587 if (udma_cap == ATA_UDMA6)
588 cfis->sector_count = XFER_UDMA_6;
589 if (udma_cap == ATA_UDMA5)
590 cfis->sector_count = XFER_UDMA_5;
591 if (udma_cap == ATA_UDMA4)
592 cfis->sector_count = XFER_UDMA_4;
593 if (udma_cap == ATA_UDMA3)
594 cfis->sector_count = XFER_UDMA_3;
596 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
599 static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
601 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
602 struct sata_fis_h2d h2d, *cfis = &h2d;
607 memset(cfis, 0, sizeof(struct sata_fis_h2d));
609 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
610 cfis->pm_port_c = 0x80; /* is command */
611 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
612 cfis->device = ATA_LBA;
614 cfis->device |= (block >> 24) & 0xf;
615 cfis->lba_high = (block >> 16) & 0xff;
616 cfis->lba_mid = (block >> 8) & 0xff;
617 cfis->lba_low = block & 0xff;
618 cfis->sector_count = (u8)(blkcnt & 0xff);
620 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
624 void fsl_sata_flush_cache(int dev)
626 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
627 struct sata_fis_h2d h2d, *cfis = &h2d;
629 memset(cfis, 0, sizeof(struct sata_fis_h2d));
631 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
632 cfis->pm_port_c = 0x80; /* is command */
633 cfis->command = ATA_CMD_FLUSH;
635 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
638 static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
640 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
641 struct sata_fis_h2d h2d, *cfis = &h2d;
646 memset(cfis, 0, sizeof(struct sata_fis_h2d));
648 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
649 cfis->pm_port_c = 0x80; /* is command */
651 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
654 cfis->lba_high_exp = (block >> 40) & 0xff;
655 cfis->lba_mid_exp = (block >> 32) & 0xff;
656 cfis->lba_low_exp = (block >> 24) & 0xff;
657 cfis->lba_high = (block >> 16) & 0xff;
658 cfis->lba_mid = (block >> 8) & 0xff;
659 cfis->lba_low = block & 0xff;
660 cfis->device = ATA_LBA;
661 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
662 cfis->sector_count = blkcnt & 0xff;
664 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
668 u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
670 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
671 struct sata_fis_h2d h2d, *cfis = &h2d;
675 if (sata->lba48 != 1) {
676 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
682 memset(cfis, 0, sizeof(struct sata_fis_h2d));
684 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
685 cfis->pm_port_c = 0x80; /* is command */
687 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
688 : ATA_CMD_FPDMA_READ;
690 cfis->lba_high_exp = (block >> 40) & 0xff;
691 cfis->lba_mid_exp = (block >> 32) & 0xff;
692 cfis->lba_low_exp = (block >> 24) & 0xff;
693 cfis->lba_high = (block >> 16) & 0xff;
694 cfis->lba_mid = (block >> 8) & 0xff;
695 cfis->lba_low = block & 0xff;
697 cfis->device = ATA_LBA;
698 cfis->features_exp = (blkcnt >> 8) & 0xff;
699 cfis->features = blkcnt & 0xff;
701 if (sata->queue_depth >= SATA_HC_MAX_CMD)
702 ncq_channel = SATA_HC_MAX_CMD - 1;
704 ncq_channel = sata->queue_depth - 1;
706 /* Use the latest queue */
707 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
711 void fsl_sata_flush_cache_ext(int dev)
713 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
714 struct sata_fis_h2d h2d, *cfis = &h2d;
716 memset(cfis, 0, sizeof(struct sata_fis_h2d));
718 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
719 cfis->pm_port_c = 0x80; /* is command */
720 cfis->command = ATA_CMD_FLUSH_EXT;
722 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
725 /* Software reset, set SRST of the Device Control register */
726 void fsl_sata_software_reset(int dev)
731 static void fsl_sata_init_wcache(int dev, u16 *id)
733 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
735 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
737 if (ata_id_has_flush(id))
739 if (ata_id_has_flush_ext(id))
743 static int fsl_sata_get_wcache(int dev)
745 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
749 static int fsl_sata_get_flush(int dev)
751 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
755 static int fsl_sata_get_flush_ext(int dev)
757 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
758 return sata->flush_ext;
761 u32 ata_low_level_rw_lba48(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
771 max_blks = ATA_MAX_SECTORS_LBA48;
773 if (blks > max_blks) {
774 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
775 fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
777 fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
780 addr += ATA_SECT_SIZE * max_blks;
782 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
783 fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
785 fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
788 addr += ATA_SECT_SIZE * blks;
795 u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
805 max_blks = ATA_MAX_SECTORS;
807 if (blks > max_blks) {
808 fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
811 addr += ATA_SECT_SIZE * max_blks;
813 fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
816 addr += ATA_SECT_SIZE * blks;
824 * SATA interface between low level driver and command layer
826 ulong sata_read(int dev, u32 blknr, u32 blkcnt, void *buffer)
829 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
832 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
834 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
838 ulong sata_write(int dev, u32 blknr, u32 blkcnt, void *buffer)
841 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
844 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
845 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
846 fsl_sata_flush_cache_ext(dev);
848 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
849 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
850 fsl_sata_flush_cache(dev);
855 int scan_sata(int dev)
857 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
858 unsigned char serial[ATA_ID_SERNO_LEN + 1];
859 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
860 unsigned char product[ATA_ID_PROD_LEN + 1];
864 /* if no detected link */
868 id = (u16 *)malloc(ATA_ID_WORDS * 2);
870 printf("id malloc failed\n\r");
874 /* Identify device to get information */
875 fsl_sata_identify(dev, id);
878 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
879 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
881 /* Firmware version */
882 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
883 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
886 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
887 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
890 n_sectors = ata_id_n_sectors(id);
891 sata_dev_desc[dev].lba = (u32)n_sectors;
894 /* Check if support LBA48 */
895 if (ata_id_has_lba48(id)) {
897 debug("Device support LBA48\n\r");
899 debug("Device supports LBA28\n\r");
902 /* Get the NCQ queue depth from device */
903 sata->queue_depth = ata_id_queue_depth(id);
905 /* Get the xfer mode from device */
906 fsl_sata_xfer_mode(dev, id);
908 /* Get the write cache status from device */
909 fsl_sata_init_wcache(dev, id);
911 /* Set the xfer mode to highest speed */
912 fsl_sata_set_features(dev);
914 fsl_sata_identify(dev, id);