1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2008,2010 Freescale Semiconductor, Inc.
15 #include <asm/processor.h>
16 #include <asm/fsl_serdes.h>
23 #if CONFIG_IS_ENABLED(BLK)
27 #include <dm/device-internal.h>
29 #ifndef CONFIG_SYS_SATA1_FLAGS
30 #define CONFIG_SYS_SATA1_FLAGS FLAGS_DMA
32 #ifndef CONFIG_SYS_SATA2_FLAGS
33 #define CONFIG_SYS_SATA2_FLAGS FLAGS_DMA
36 static struct fsl_sata_info fsl_sata_info[] = {
38 {CONFIG_SYS_SATA1, CONFIG_SYS_SATA1_FLAGS},
43 {CONFIG_SYS_SATA2, CONFIG_SYS_SATA2_FLAGS},
50 static inline void sdelay(unsigned long sec)
53 for (i = 0; i < sec; i++)
57 static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
59 printf("Status FIS dump:\n\r");
60 printf("fis_type: %02x\n\r", s->fis_type);
61 printf("pm_port_i: %02x\n\r", s->pm_port_i);
62 printf("status: %02x\n\r", s->status);
63 printf("error: %02x\n\r", s->error);
64 printf("lba_low: %02x\n\r", s->lba_low);
65 printf("lba_mid: %02x\n\r", s->lba_mid);
66 printf("lba_high: %02x\n\r", s->lba_high);
67 printf("device: %02x\n\r", s->device);
68 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
69 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
70 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
71 printf("res1: %02x\n\r", s->res1);
72 printf("sector_count: %02x\n\r", s->sector_count);
73 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
76 static int ata_wait_register(unsigned __iomem *addr, u32 mask,
77 u32 val, u32 timeout_msec)
82 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
83 && i < timeout_msec; i++)
85 return (i < timeout_msec) ? 0 : -1;
88 #if !CONFIG_IS_ENABLED(BLK)
89 int init_sata(int dev)
91 static int init_sata(struct fsl_ata_priv *priv, int dev)
95 cmd_hdr_tbl_t *cmd_hdr;
98 fsl_sata_reg_t __iomem *reg;
103 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
104 printf("the sata index %d is out of ranges\n\r", dev);
108 #ifdef CONFIG_MPC85xx
109 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
110 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
113 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
114 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
119 /* Allocate SATA device driver struct */
120 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
122 printf("alloc the sata device struct failed\n\r");
125 /* Zero all of the device driver struct */
126 memset((void *)sata, 0, sizeof(fsl_sata_t));
128 snprintf(sata->name, 12, "SATA%d:", dev);
130 /* Set the controller register base address to device struct */
131 #if !CONFIG_IS_ENABLED(BLK)
132 sata_dev_desc[dev].priv = (void *)sata;
133 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
134 sata->dma_flag = fsl_sata_info[dev].flags;
136 reg = (fsl_sata_reg_t *)(priv->base + priv->offset * dev);
137 sata->dma_flag = priv->flag;
138 priv->fsl_sata = sata;
140 sata->reg_base = reg;
142 /* Allocate the command header table, 4 bytes aligned */
143 length = sizeof(struct cmd_hdr_tbl);
144 align = SATA_HC_CMD_HDR_TBL_ALIGN;
145 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
146 if (!sata->cmd_hdr_tbl_offset) {
147 printf("alloc the command header failed\n\r");
151 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
153 sata->cmd_hdr = cmd_hdr;
155 /* Zero all of the command header table */
156 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
158 /* Allocate command descriptor for all command */
159 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
160 align = SATA_HC_CMD_DESC_ALIGN;
161 sata->cmd_desc_offset = (void *)malloc(length + align);
162 if (!sata->cmd_desc_offset) {
163 printf("alloc the command descriptor failed\n\r");
166 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
168 /* Zero all of command descriptor */
169 memset((void *)sata->cmd_desc_offset, 0, length + align);
171 /* Link the command descriptor to command header */
172 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
173 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
174 & ~(CMD_HDR_CDA_ALIGN - 1);
175 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
178 /* To have safe state, force the controller offline */
179 val32 = in_le32(®->hcontrol);
180 val32 &= ~HCONTROL_ONOFF;
181 val32 |= HCONTROL_FORCE_OFFLINE;
182 out_le32(®->hcontrol, val32);
184 /* Wait the controller offline */
185 ata_wait_register(®->hstatus, HSTATUS_ONOFF, 0, 1000);
187 /* Set the command header base address to CHBA register to tell DMA */
188 out_le32(®->chba, (u32)cmd_hdr & ~0x3);
190 /* Snoop for the command header */
191 val32 = in_le32(®->hcontrol);
192 val32 |= HCONTROL_HDR_SNOOP;
193 out_le32(®->hcontrol, val32);
195 /* Disable all of interrupts */
196 val32 = in_le32(®->hcontrol);
197 val32 &= ~HCONTROL_INT_EN_ALL;
198 out_le32(®->hcontrol, val32);
200 /* Clear all of interrupts */
201 val32 = in_le32(®->hstatus);
202 out_le32(®->hstatus, val32);
204 /* Set the ICC, no interrupt coalescing */
205 out_le32(®->icc, 0x01000000);
207 /* No PM attatched, the SATA device direct connect */
208 out_le32(®->cqpmp, 0);
210 /* Clear SError register */
211 val32 = in_le32(®->serror);
212 out_le32(®->serror, val32);
214 /* Clear CER register */
215 val32 = in_le32(®->cer);
216 out_le32(®->cer, val32);
218 /* Clear DER register */
219 val32 = in_le32(®->der);
220 out_le32(®->der, val32);
222 /* No device detection or initialization action requested */
223 out_le32(®->scontrol, 0x00000300);
225 /* Configure the transport layer, default value */
226 out_le32(®->transcfg, 0x08000016);
228 /* Configure the link layer, default value */
229 out_le32(®->linkcfg, 0x0000ff34);
231 /* Bring the controller online */
232 val32 = in_le32(®->hcontrol);
233 val32 |= HCONTROL_ONOFF;
234 out_le32(®->hcontrol, val32);
238 /* print sata device name */
239 printf("%s ", sata->name);
241 /* Wait PHY RDY signal changed for 500ms */
242 ata_wait_register(®->hstatus, HSTATUS_PHY_RDY,
243 HSTATUS_PHY_RDY, 500);
246 val32 = in_le32(®->hstatus);
247 if (val32 & HSTATUS_PHY_RDY) {
251 printf("(No RDY)\n\r");
255 /* Wait for signature updated, which is 1st D2H */
256 ata_wait_register(®->hstatus, HSTATUS_SIGNATURE,
257 HSTATUS_SIGNATURE, 10000);
259 if (val32 & HSTATUS_SIGNATURE) {
260 sig = in_le32(®->sig);
261 debug("Signature updated, the sig =%08x\n\r", sig);
262 sata->ata_device_type = ata_dev_classify(sig);
265 /* Check the speed */
266 val32 = in_le32(®->sstatus);
267 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
268 printf("(1.5 Gbps)\n\r");
269 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
270 printf("(3 Gbps)\n\r");
275 int reset_sata(int dev)
280 static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg)
282 printf("\n\rSATA: %08x\n\r", (u32)reg);
283 printf("CQR: %08x\n\r", in_le32(®->cqr));
284 printf("CAR: %08x\n\r", in_le32(®->car));
285 printf("CCR: %08x\n\r", in_le32(®->ccr));
286 printf("CER: %08x\n\r", in_le32(®->cer));
287 printf("CQR: %08x\n\r", in_le32(®->cqr));
288 printf("DER: %08x\n\r", in_le32(®->der));
289 printf("CHBA: %08x\n\r", in_le32(®->chba));
290 printf("HStatus: %08x\n\r", in_le32(®->hstatus));
291 printf("HControl: %08x\n\r", in_le32(®->hcontrol));
292 printf("CQPMP: %08x\n\r", in_le32(®->cqpmp));
293 printf("SIG: %08x\n\r", in_le32(®->sig));
294 printf("ICC: %08x\n\r", in_le32(®->icc));
295 printf("SStatus: %08x\n\r", in_le32(®->sstatus));
296 printf("SError: %08x\n\r", in_le32(®->serror));
297 printf("SControl: %08x\n\r", in_le32(®->scontrol));
298 printf("SNotification: %08x\n\r", in_le32(®->snotification));
299 printf("TransCfg: %08x\n\r", in_le32(®->transcfg));
300 printf("TransStatus: %08x\n\r", in_le32(®->transstatus));
301 printf("LinkCfg: %08x\n\r", in_le32(®->linkcfg));
302 printf("LinkCfg1: %08x\n\r", in_le32(®->linkcfg1));
303 printf("LinkCfg2: %08x\n\r", in_le32(®->linkcfg2));
304 printf("LinkStatus: %08x\n\r", in_le32(®->linkstatus));
305 printf("LinkStatus1: %08x\n\r", in_le32(®->linkstatus1));
306 printf("PhyCtrlCfg: %08x\n\r", in_le32(®->phyctrlcfg));
307 printf("SYSPR: %08x\n\r", in_be32(®->syspr));
310 static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
311 int is_ncq, int tag, u8 *buffer, u32 len)
313 cmd_hdr_entry_t *cmd_hdr;
314 cmd_desc_t *cmd_desc;
321 fsl_sata_reg_t __iomem *reg = sata->reg_base;
324 /* Check xfer length */
325 if (len > SATA_HC_MAX_XFER_LEN) {
326 printf("max transfer length is 64MB\n\r");
330 /* Setup the command descriptor */
331 cmd_desc = sata->cmd_desc + tag;
333 /* Get the pointer cfis of command descriptor */
334 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
336 /* Zero the cfis of command descriptor */
337 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
339 /* Copy the cfis from user to command descriptor */
340 h2d->fis_type = cfis->fis_type;
341 h2d->pm_port_c = cfis->pm_port_c;
342 h2d->command = cfis->command;
344 h2d->features = cfis->features;
345 h2d->features_exp = cfis->features_exp;
347 h2d->lba_low = cfis->lba_low;
348 h2d->lba_mid = cfis->lba_mid;
349 h2d->lba_high = cfis->lba_high;
350 h2d->lba_low_exp = cfis->lba_low_exp;
351 h2d->lba_mid_exp = cfis->lba_mid_exp;
352 h2d->lba_high_exp = cfis->lba_high_exp;
355 h2d->sector_count = cfis->sector_count;
356 h2d->sector_count_exp = cfis->sector_count_exp;
358 h2d->sector_count = (u8)(tag << 3);
361 h2d->device = cfis->device;
362 h2d->control = cfis->control;
364 /* Setup the PRD table */
365 prde = (prd_entry_t *)cmd_desc->prdt;
366 memset((void *)prde, 0, sizeof(struct prdt));
370 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
373 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
374 debug("dba = %08x\n\r", (u32)buffer);
376 if (len < PRD_ENTRY_MAX_XFER_SZ) {
377 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
378 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
379 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
384 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
385 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
386 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
387 buffer += PRD_ENTRY_MAX_XFER_SZ;
388 len -= PRD_ENTRY_MAX_XFER_SZ;
394 /* Setup the command slot of cmd hdr */
395 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
397 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
399 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
400 val32 |= sizeof(sata_fis_h2d_t);
401 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
403 cmd_hdr->ttl = cpu_to_le32(ttl);
406 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
408 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
411 tag &= CMD_HDR_ATTR_TAG;
414 debug("attribute = %08x\n\r", val32);
415 cmd_hdr->attribute = cpu_to_le32(val32);
417 /* Make sure cmd desc and cmd slot valid before command issue */
421 val32 = (u32)(h2d->pm_port_c & 0x0f);
422 out_le32(®->cqpmp, val32);
425 if (ata_wait_register(®->car, (1 << tag), 0, 10000))
426 printf("Wait no active time out\n\r");
429 if (!(in_le32(®->cqr) & (1 << tag))) {
431 out_le32(®->cqr, val32);
434 /* Wait command completed for 10s */
435 if (ata_wait_register(®->ccr, (1 << tag), (1 << tag), 10000)) {
437 printf("Non-NCQ command time out\n\r");
439 printf("NCQ command time out\n\r");
442 val32 = in_le32(®->cer);
446 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
447 printf("CE at device\n\r");
448 fsl_sata_dump_regs(reg);
449 der = in_le32(®->der);
450 out_le32(®->cer, val32);
451 out_le32(®->der, der);
454 /* Clear complete flags */
455 val32 = in_le32(®->ccr);
456 out_le32(®->ccr, val32);
461 static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
462 int tag, u8 *buffer, u32 len)
467 static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
468 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
472 if (tag > SATA_HC_MAX_CMD || tag < 0) {
473 printf("tag is out of range, tag=%d\n\r", tag);
477 switch (command_type) {
479 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
482 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
485 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
488 case CMD_VENDOR_BIST:
490 printf("not support now\n\r");
499 static void fsl_sata_xfer_mode(fsl_sata_t *sata, u16 *id)
501 sata->pio = id[ATA_ID_PIO_MODES];
502 sata->mwdma = id[ATA_ID_MWDMA_MODES];
503 sata->udma = id[ATA_ID_UDMA_MODES];
504 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
507 static void fsl_sata_set_features(fsl_sata_t *sata)
509 struct sata_fis_h2d h2d, *cfis = &h2d;
512 memset(cfis, 0, sizeof(struct sata_fis_h2d));
514 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
515 cfis->pm_port_c = 0x80; /* is command */
516 cfis->command = ATA_CMD_SET_FEATURES;
517 cfis->features = SETFEATURES_XFER;
519 /* First check the device capablity */
520 udma_cap = (u8)(sata->udma & 0xff);
521 debug("udma_cap %02x\n\r", udma_cap);
523 if (udma_cap == ATA_UDMA6)
524 cfis->sector_count = XFER_UDMA_6;
525 if (udma_cap == ATA_UDMA5)
526 cfis->sector_count = XFER_UDMA_5;
527 if (udma_cap == ATA_UDMA4)
528 cfis->sector_count = XFER_UDMA_4;
529 if (udma_cap == ATA_UDMA3)
530 cfis->sector_count = XFER_UDMA_3;
532 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
535 static u32 fsl_sata_rw_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt, u8 *buffer,
538 struct sata_fis_h2d h2d, *cfis = &h2d;
543 memset(cfis, 0, sizeof(struct sata_fis_h2d));
545 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
546 cfis->pm_port_c = 0x80; /* is command */
547 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
548 cfis->device = ATA_LBA;
550 cfis->device |= (block >> 24) & 0xf;
551 cfis->lba_high = (block >> 16) & 0xff;
552 cfis->lba_mid = (block >> 8) & 0xff;
553 cfis->lba_low = block & 0xff;
554 cfis->sector_count = (u8)(blkcnt & 0xff);
556 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
560 static void fsl_sata_flush_cache(fsl_sata_t *sata)
562 struct sata_fis_h2d h2d, *cfis = &h2d;
564 memset(cfis, 0, sizeof(struct sata_fis_h2d));
566 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
567 cfis->pm_port_c = 0x80; /* is command */
568 cfis->command = ATA_CMD_FLUSH;
570 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
573 static u32 fsl_sata_rw_cmd_ext(fsl_sata_t *sata, u32 start, u32 blkcnt,
574 u8 *buffer, int is_write)
576 struct sata_fis_h2d h2d, *cfis = &h2d;
581 memset(cfis, 0, sizeof(struct sata_fis_h2d));
583 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
584 cfis->pm_port_c = 0x80; /* is command */
586 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
589 cfis->lba_high_exp = (block >> 40) & 0xff;
590 cfis->lba_mid_exp = (block >> 32) & 0xff;
591 cfis->lba_low_exp = (block >> 24) & 0xff;
592 cfis->lba_high = (block >> 16) & 0xff;
593 cfis->lba_mid = (block >> 8) & 0xff;
594 cfis->lba_low = block & 0xff;
595 cfis->device = ATA_LBA;
596 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
597 cfis->sector_count = blkcnt & 0xff;
599 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
603 static u32 fsl_sata_rw_ncq_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt,
604 u8 *buffer, int is_write)
606 struct sata_fis_h2d h2d, *cfis = &h2d;
610 if (sata->lba48 != 1) {
611 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
617 memset(cfis, 0, sizeof(struct sata_fis_h2d));
619 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
620 cfis->pm_port_c = 0x80; /* is command */
622 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
623 : ATA_CMD_FPDMA_READ;
625 cfis->lba_high_exp = (block >> 40) & 0xff;
626 cfis->lba_mid_exp = (block >> 32) & 0xff;
627 cfis->lba_low_exp = (block >> 24) & 0xff;
628 cfis->lba_high = (block >> 16) & 0xff;
629 cfis->lba_mid = (block >> 8) & 0xff;
630 cfis->lba_low = block & 0xff;
632 cfis->device = ATA_LBA;
633 cfis->features_exp = (blkcnt >> 8) & 0xff;
634 cfis->features = blkcnt & 0xff;
636 if (sata->queue_depth >= SATA_HC_MAX_CMD)
637 ncq_channel = SATA_HC_MAX_CMD - 1;
639 ncq_channel = sata->queue_depth - 1;
641 /* Use the latest queue */
642 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
646 static void fsl_sata_flush_cache_ext(fsl_sata_t *sata)
648 struct sata_fis_h2d h2d, *cfis = &h2d;
650 memset(cfis, 0, sizeof(struct sata_fis_h2d));
652 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
653 cfis->pm_port_c = 0x80; /* is command */
654 cfis->command = ATA_CMD_FLUSH_EXT;
656 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
659 static void fsl_sata_init_wcache(fsl_sata_t *sata, u16 *id)
661 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
663 if (ata_id_has_flush(id))
665 if (ata_id_has_flush_ext(id))
669 static u32 ata_low_level_rw_lba48(fsl_sata_t *sata, u32 blknr, lbaint_t blkcnt,
670 const void *buffer, int is_write)
680 max_blks = ATA_MAX_SECTORS_LBA48;
682 if (blks > max_blks) {
683 if (sata->dma_flag != FLAGS_FPDMA)
684 fsl_sata_rw_cmd_ext(sata, start, max_blks, addr,
687 fsl_sata_rw_ncq_cmd(sata, start, max_blks, addr,
691 addr += ATA_SECT_SIZE * max_blks;
693 if (sata->dma_flag != FLAGS_FPDMA)
694 fsl_sata_rw_cmd_ext(sata, start, blks, addr,
697 fsl_sata_rw_ncq_cmd(sata, start, blks, addr,
701 addr += ATA_SECT_SIZE * blks;
708 static u32 ata_low_level_rw_lba28(fsl_sata_t *sata, u32 blknr, u32 blkcnt,
709 const void *buffer, int is_write)
719 max_blks = ATA_MAX_SECTORS;
721 if (blks > max_blks) {
722 fsl_sata_rw_cmd(sata, start, max_blks, addr, is_write);
725 addr += ATA_SECT_SIZE * max_blks;
727 fsl_sata_rw_cmd(sata, start, blks, addr, is_write);
730 addr += ATA_SECT_SIZE * blks;
738 * SATA interface between low level driver and command layer
740 #if !CONFIG_IS_ENABLED(BLK)
741 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
743 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
745 static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
748 struct fsl_ata_priv *priv = dev_get_platdata(dev);
749 fsl_sata_t *sata = priv->fsl_sata;
754 rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
757 rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
762 #if !CONFIG_IS_ENABLED(BLK)
763 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
765 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
767 static ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
770 struct fsl_ata_priv *priv = dev_get_platdata(dev);
771 fsl_sata_t *sata = priv->fsl_sata;
776 rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
778 if (sata->wcache && sata->flush_ext)
779 fsl_sata_flush_cache_ext(sata);
781 rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
783 if (sata->wcache && sata->flush)
784 fsl_sata_flush_cache(sata);
789 static void fsl_sata_identify(fsl_sata_t *sata, u16 *id)
791 struct sata_fis_h2d h2d, *cfis = &h2d;
793 memset(cfis, 0, sizeof(struct sata_fis_h2d));
795 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
796 cfis->pm_port_c = 0x80; /* is command */
797 cfis->command = ATA_CMD_ID_ATA;
799 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
800 ata_swap_buf_le16(id, ATA_ID_WORDS);
803 #if !CONFIG_IS_ENABLED(BLK)
804 int scan_sata(int dev)
806 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
808 static int scan_sata(struct udevice *dev)
810 struct blk_desc *desc = dev_get_uclass_platdata(dev);
811 struct fsl_ata_priv *priv = dev_get_platdata(dev);
812 fsl_sata_t *sata = priv->fsl_sata;
815 unsigned char serial[ATA_ID_SERNO_LEN + 1];
816 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
817 unsigned char product[ATA_ID_PROD_LEN + 1];
821 /* if no detected link */
825 id = (u16 *)malloc(ATA_ID_WORDS * 2);
827 printf("id malloc failed\n\r");
831 /* Identify device to get information */
832 fsl_sata_identify(sata, id);
835 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
837 /* Firmware version */
838 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
841 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
844 n_sectors = ata_id_n_sectors(id);
847 /* Check if support LBA48 */
848 if (ata_id_has_lba48(id)) {
850 debug("Device support LBA48\n\r");
852 debug("Device supports LBA28\n\r");
855 #if !CONFIG_IS_ENABLED(BLK)
856 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
857 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
858 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
859 sata_dev_desc[dev].lba = (u32)n_sectors;
861 sata_dev_desc[dev].lba48 = sata->lba48;
864 memcpy(desc->product, serial, sizeof(serial));
865 memcpy(desc->revision, firmware, sizeof(firmware));
866 memcpy(desc->vendor, product, sizeof(product));
867 desc->lba = n_sectors;
869 desc->lba48 = sata->lba48;
873 /* Get the NCQ queue depth from device */
874 sata->queue_depth = ata_id_queue_depth(id);
876 /* Get the xfer mode from device */
877 fsl_sata_xfer_mode(sata, id);
879 /* Get the write cache status from device */
880 fsl_sata_init_wcache(sata, id);
882 /* Set the xfer mode to highest speed */
883 fsl_sata_set_features(sata);
892 #if CONFIG_IS_ENABLED(BLK)
893 static const struct blk_ops sata_fsl_blk_ops = {
898 U_BOOT_DRIVER(sata_fsl_driver) = {
899 .name = "sata_fsl_blk",
901 .ops = &sata_fsl_blk_ops,
902 .platdata_auto_alloc_size = sizeof(struct fsl_ata_priv),
905 static int fsl_ata_ofdata_to_platdata(struct udevice *dev)
907 struct fsl_ata_priv *priv = dev_get_priv(dev);
909 priv->number = dev_read_u32_default(dev, "sata-number", -1);
910 priv->flag = dev_read_u32_default(dev, "sata-fpdma", -1);
911 priv->offset = dev_read_u32_default(dev, "sata-offset", -1);
913 priv->base = dev_read_addr(dev);
914 if (priv->base == FDT_ADDR_T_NONE)
920 static int fsl_unbind_device(struct udevice *dev)
924 ret = device_remove(dev, DM_REMOVE_NORMAL);
928 ret = device_unbind(dev);
935 static int fsl_ata_probe(struct udevice *dev)
937 struct fsl_ata_priv *blk_priv, *priv;
946 priv = dev_get_priv(dev);
947 nr_ports = priv->number;
948 nr_ports = min(nr_ports, CONFIG_SYS_SATA_MAX_DEVICE);
950 for (i = 0; i < nr_ports; i++) {
951 snprintf(sata_name, sizeof(sata_name), "fsl_sata%d", i);
952 ret = blk_create_devicef(dev, "sata_fsl_blk", sata_name,
953 IF_TYPE_SATA, -1, 512, 0, &blk);
955 debug("Can't create device\n");
960 ret = init_sata(priv, i);
962 debug("%s: Failed to init sata\n", __func__);
963 ret = fsl_unbind_device(blk);
971 blk_priv = dev_get_platdata(blk);
972 blk_priv->fsl_sata = priv->fsl_sata;
974 ret = scan_sata(blk);
976 debug("%s: Failed to scan bus\n", __func__);
977 ret = fsl_unbind_device(blk);
986 if (failed_number == nr_ports)
992 static int fsl_ata_remove(struct udevice *dev)
995 struct fsl_ata_priv *priv;
997 priv = dev_get_priv(dev);
998 sata = priv->fsl_sata;
1000 free(sata->cmd_hdr_tbl_offset);
1001 free(sata->cmd_desc_offset);
1007 static int sata_fsl_scan(struct udevice *dev)
1009 /* Nothing to do here */
1014 struct ahci_ops sata_fsl_ahci_ops = {
1015 .scan = sata_fsl_scan,
1018 static const struct udevice_id fsl_ata_ids[] = {
1019 { .compatible = "fsl,pq-sata-v2" },
1023 U_BOOT_DRIVER(fsl_ahci) = {
1026 .of_match = fsl_ata_ids,
1027 .ops = &sata_fsl_ahci_ops,
1028 .ofdata_to_platdata = fsl_ata_ofdata_to_platdata,
1029 .probe = fsl_ata_probe,
1030 .remove = fsl_ata_remove,
1031 .priv_auto_alloc_size = sizeof(struct fsl_ata_priv),