Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 2 | /* |
Kumar Gala | eb453df | 2010-04-20 10:21:25 -0500 | [diff] [blame] | 3 | * Copyright (C) 2008,2010 Freescale Semiconductor, Inc. |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 4 | * Copyright 2019 NXP |
| 5 | * Author: Dave Liu <daveliu@freescale.com> |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Tom Rini | eee91aa | 2022-06-10 22:59:26 -0400 | [diff] [blame] | 8 | #include <ahci.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Tom Rini | eee91aa | 2022-06-10 22:59:26 -0400 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <dm/device-internal.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 16 | #include <asm/io.h> |
Dave Liu | b9674d0 | 2010-04-12 14:23:25 +0800 | [diff] [blame] | 17 | #include <asm/processor.h> |
Kumar Gala | eb453df | 2010-04-20 10:21:25 -0500 | [diff] [blame] | 18 | #include <asm/fsl_serdes.h> |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | #include <libata.h> |
| 21 | #include <fis.h> |
Pavel Herrmann | 9e9f628 | 2012-09-27 23:18:04 +0000 | [diff] [blame] | 22 | #include <sata.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 24 | #include "fsl_sata.h" |
| 25 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 26 | static inline void sdelay(unsigned long sec) |
| 27 | { |
| 28 | unsigned long i; |
| 29 | for (i = 0; i < sec; i++) |
| 30 | mdelay(1000); |
| 31 | } |
| 32 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 33 | static void fsl_sata_dump_sfis(struct sata_fis_d2h *s) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 34 | { |
| 35 | printf("Status FIS dump:\n\r"); |
| 36 | printf("fis_type: %02x\n\r", s->fis_type); |
| 37 | printf("pm_port_i: %02x\n\r", s->pm_port_i); |
| 38 | printf("status: %02x\n\r", s->status); |
| 39 | printf("error: %02x\n\r", s->error); |
| 40 | printf("lba_low: %02x\n\r", s->lba_low); |
| 41 | printf("lba_mid: %02x\n\r", s->lba_mid); |
| 42 | printf("lba_high: %02x\n\r", s->lba_high); |
| 43 | printf("device: %02x\n\r", s->device); |
| 44 | printf("lba_low_exp: %02x\n\r", s->lba_low_exp); |
| 45 | printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp); |
| 46 | printf("lba_high_exp: %02x\n\r", s->lba_high_exp); |
| 47 | printf("res1: %02x\n\r", s->res1); |
| 48 | printf("sector_count: %02x\n\r", s->sector_count); |
| 49 | printf("sector_count_exp: %02x\n\r", s->sector_count_exp); |
| 50 | } |
| 51 | |
Kim Phillips | 0348748 | 2012-10-29 13:34:40 +0000 | [diff] [blame] | 52 | static int ata_wait_register(unsigned __iomem *addr, u32 mask, |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 53 | u32 val, u32 timeout_msec) |
| 54 | { |
| 55 | int i; |
| 56 | u32 temp; |
| 57 | |
| 58 | for (i = 0; (((temp = in_le32(addr)) & mask) != val) |
| 59 | && i < timeout_msec; i++) |
| 60 | mdelay(1); |
| 61 | return (i < timeout_msec) ? 0 : -1; |
| 62 | } |
| 63 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 64 | static int init_sata(struct fsl_ata_priv *priv, int dev) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 65 | { |
| 66 | u32 length, align; |
| 67 | cmd_hdr_tbl_t *cmd_hdr; |
| 68 | u32 cda; |
| 69 | u32 val32; |
Kim Phillips | 0348748 | 2012-10-29 13:34:40 +0000 | [diff] [blame] | 70 | fsl_sata_reg_t __iomem *reg; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 71 | u32 sig; |
| 72 | int i; |
| 73 | fsl_sata_t *sata; |
| 74 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) { |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 76 | printf("the sata index %d is out of ranges\n\r", dev); |
| 77 | return -1; |
| 78 | } |
| 79 | |
Kumar Gala | eb453df | 2010-04-20 10:21:25 -0500 | [diff] [blame] | 80 | #ifdef CONFIG_MPC85xx |
| 81 | if ((dev == 0) && (!is_serdes_configured(SATA1))) { |
| 82 | printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev); |
| 83 | return -1; |
| 84 | } |
| 85 | if ((dev == 1) && (!is_serdes_configured(SATA2))) { |
| 86 | printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev); |
| 87 | return -1; |
| 88 | } |
| 89 | #endif |
| 90 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 91 | /* Allocate SATA device driver struct */ |
| 92 | sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t)); |
| 93 | if (!sata) { |
| 94 | printf("alloc the sata device struct failed\n\r"); |
| 95 | return -1; |
| 96 | } |
| 97 | /* Zero all of the device driver struct */ |
| 98 | memset((void *)sata, 0, sizeof(fsl_sata_t)); |
| 99 | |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 100 | snprintf(sata->name, 12, "SATA%d:", dev); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 101 | |
| 102 | /* Set the controller register base address to device struct */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 103 | reg = (fsl_sata_reg_t *)(priv->base + priv->offset * dev); |
| 104 | sata->dma_flag = priv->flag; |
| 105 | priv->fsl_sata = sata; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 106 | sata->reg_base = reg; |
| 107 | |
| 108 | /* Allocate the command header table, 4 bytes aligned */ |
| 109 | length = sizeof(struct cmd_hdr_tbl); |
| 110 | align = SATA_HC_CMD_HDR_TBL_ALIGN; |
| 111 | sata->cmd_hdr_tbl_offset = (void *)malloc(length + align); |
xypron.glpk@gmx.de | 222d242 | 2017-04-15 15:31:53 +0200 | [diff] [blame] | 112 | if (!sata->cmd_hdr_tbl_offset) { |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 113 | printf("alloc the command header failed\n\r"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align) |
| 118 | & ~(align - 1)); |
| 119 | sata->cmd_hdr = cmd_hdr; |
| 120 | |
| 121 | /* Zero all of the command header table */ |
| 122 | memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align); |
| 123 | |
| 124 | /* Allocate command descriptor for all command */ |
| 125 | length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD; |
| 126 | align = SATA_HC_CMD_DESC_ALIGN; |
| 127 | sata->cmd_desc_offset = (void *)malloc(length + align); |
| 128 | if (!sata->cmd_desc_offset) { |
| 129 | printf("alloc the command descriptor failed\n\r"); |
| 130 | return -1; |
| 131 | } |
| 132 | sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align) |
| 133 | & ~(align - 1)); |
| 134 | /* Zero all of command descriptor */ |
| 135 | memset((void *)sata->cmd_desc_offset, 0, length + align); |
| 136 | |
| 137 | /* Link the command descriptor to command header */ |
| 138 | for (i = 0; i < SATA_HC_MAX_CMD; i++) { |
| 139 | cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i) |
| 140 | & ~(CMD_HDR_CDA_ALIGN - 1); |
| 141 | cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda); |
| 142 | } |
| 143 | |
| 144 | /* To have safe state, force the controller offline */ |
| 145 | val32 = in_le32(®->hcontrol); |
| 146 | val32 &= ~HCONTROL_ONOFF; |
| 147 | val32 |= HCONTROL_FORCE_OFFLINE; |
| 148 | out_le32(®->hcontrol, val32); |
| 149 | |
| 150 | /* Wait the controller offline */ |
| 151 | ata_wait_register(®->hstatus, HSTATUS_ONOFF, 0, 1000); |
| 152 | |
| 153 | /* Set the command header base address to CHBA register to tell DMA */ |
| 154 | out_le32(®->chba, (u32)cmd_hdr & ~0x3); |
| 155 | |
| 156 | /* Snoop for the command header */ |
| 157 | val32 = in_le32(®->hcontrol); |
| 158 | val32 |= HCONTROL_HDR_SNOOP; |
| 159 | out_le32(®->hcontrol, val32); |
| 160 | |
| 161 | /* Disable all of interrupts */ |
| 162 | val32 = in_le32(®->hcontrol); |
| 163 | val32 &= ~HCONTROL_INT_EN_ALL; |
| 164 | out_le32(®->hcontrol, val32); |
| 165 | |
| 166 | /* Clear all of interrupts */ |
| 167 | val32 = in_le32(®->hstatus); |
| 168 | out_le32(®->hstatus, val32); |
| 169 | |
| 170 | /* Set the ICC, no interrupt coalescing */ |
| 171 | out_le32(®->icc, 0x01000000); |
| 172 | |
| 173 | /* No PM attatched, the SATA device direct connect */ |
| 174 | out_le32(®->cqpmp, 0); |
| 175 | |
| 176 | /* Clear SError register */ |
| 177 | val32 = in_le32(®->serror); |
| 178 | out_le32(®->serror, val32); |
| 179 | |
| 180 | /* Clear CER register */ |
| 181 | val32 = in_le32(®->cer); |
| 182 | out_le32(®->cer, val32); |
| 183 | |
| 184 | /* Clear DER register */ |
| 185 | val32 = in_le32(®->der); |
| 186 | out_le32(®->der, val32); |
| 187 | |
| 188 | /* No device detection or initialization action requested */ |
| 189 | out_le32(®->scontrol, 0x00000300); |
| 190 | |
| 191 | /* Configure the transport layer, default value */ |
| 192 | out_le32(®->transcfg, 0x08000016); |
| 193 | |
| 194 | /* Configure the link layer, default value */ |
| 195 | out_le32(®->linkcfg, 0x0000ff34); |
| 196 | |
| 197 | /* Bring the controller online */ |
| 198 | val32 = in_le32(®->hcontrol); |
| 199 | val32 |= HCONTROL_ONOFF; |
| 200 | out_le32(®->hcontrol, val32); |
| 201 | |
| 202 | mdelay(100); |
| 203 | |
| 204 | /* print sata device name */ |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 205 | printf("%s ", sata->name); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 206 | |
Dave Liu | 4d9c188 | 2008-06-03 17:38:19 +0800 | [diff] [blame] | 207 | /* Wait PHY RDY signal changed for 500ms */ |
| 208 | ata_wait_register(®->hstatus, HSTATUS_PHY_RDY, |
| 209 | HSTATUS_PHY_RDY, 500); |
| 210 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 211 | /* Check PHYRDY */ |
| 212 | val32 = in_le32(®->hstatus); |
| 213 | if (val32 & HSTATUS_PHY_RDY) { |
| 214 | sata->link = 1; |
| 215 | } else { |
| 216 | sata->link = 0; |
| 217 | printf("(No RDY)\n\r"); |
| 218 | return -1; |
| 219 | } |
| 220 | |
Dave Liu | 4d9c188 | 2008-06-03 17:38:19 +0800 | [diff] [blame] | 221 | /* Wait for signature updated, which is 1st D2H */ |
| 222 | ata_wait_register(®->hstatus, HSTATUS_SIGNATURE, |
| 223 | HSTATUS_SIGNATURE, 10000); |
| 224 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 225 | if (val32 & HSTATUS_SIGNATURE) { |
| 226 | sig = in_le32(®->sig); |
| 227 | debug("Signature updated, the sig =%08x\n\r", sig); |
| 228 | sata->ata_device_type = ata_dev_classify(sig); |
| 229 | } |
| 230 | |
| 231 | /* Check the speed */ |
| 232 | val32 = in_le32(®->sstatus); |
| 233 | if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1) |
| 234 | printf("(1.5 Gbps)\n\r"); |
| 235 | else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2) |
| 236 | printf("(3 Gbps)\n\r"); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Nikita Kiryanov | b9666d6 | 2014-11-21 12:47:23 +0200 | [diff] [blame] | 241 | int reset_sata(int dev) |
| 242 | { |
| 243 | return 0; |
| 244 | } |
| 245 | |
Kim Phillips | 0348748 | 2012-10-29 13:34:40 +0000 | [diff] [blame] | 246 | static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 247 | { |
| 248 | printf("\n\rSATA: %08x\n\r", (u32)reg); |
| 249 | printf("CQR: %08x\n\r", in_le32(®->cqr)); |
| 250 | printf("CAR: %08x\n\r", in_le32(®->car)); |
| 251 | printf("CCR: %08x\n\r", in_le32(®->ccr)); |
| 252 | printf("CER: %08x\n\r", in_le32(®->cer)); |
| 253 | printf("CQR: %08x\n\r", in_le32(®->cqr)); |
| 254 | printf("DER: %08x\n\r", in_le32(®->der)); |
| 255 | printf("CHBA: %08x\n\r", in_le32(®->chba)); |
| 256 | printf("HStatus: %08x\n\r", in_le32(®->hstatus)); |
| 257 | printf("HControl: %08x\n\r", in_le32(®->hcontrol)); |
| 258 | printf("CQPMP: %08x\n\r", in_le32(®->cqpmp)); |
| 259 | printf("SIG: %08x\n\r", in_le32(®->sig)); |
| 260 | printf("ICC: %08x\n\r", in_le32(®->icc)); |
| 261 | printf("SStatus: %08x\n\r", in_le32(®->sstatus)); |
| 262 | printf("SError: %08x\n\r", in_le32(®->serror)); |
| 263 | printf("SControl: %08x\n\r", in_le32(®->scontrol)); |
| 264 | printf("SNotification: %08x\n\r", in_le32(®->snotification)); |
| 265 | printf("TransCfg: %08x\n\r", in_le32(®->transcfg)); |
| 266 | printf("TransStatus: %08x\n\r", in_le32(®->transstatus)); |
| 267 | printf("LinkCfg: %08x\n\r", in_le32(®->linkcfg)); |
| 268 | printf("LinkCfg1: %08x\n\r", in_le32(®->linkcfg1)); |
| 269 | printf("LinkCfg2: %08x\n\r", in_le32(®->linkcfg2)); |
| 270 | printf("LinkStatus: %08x\n\r", in_le32(®->linkstatus)); |
| 271 | printf("LinkStatus1: %08x\n\r", in_le32(®->linkstatus1)); |
| 272 | printf("PhyCtrlCfg: %08x\n\r", in_le32(®->phyctrlcfg)); |
| 273 | printf("SYSPR: %08x\n\r", in_be32(®->syspr)); |
| 274 | } |
| 275 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 276 | static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis, |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 277 | int is_ncq, int tag, u8 *buffer, u32 len) |
| 278 | { |
| 279 | cmd_hdr_entry_t *cmd_hdr; |
| 280 | cmd_desc_t *cmd_desc; |
| 281 | sata_fis_h2d_t *h2d; |
| 282 | prd_entry_t *prde; |
| 283 | u32 ext_c_ddc; |
| 284 | u32 prde_count; |
| 285 | u32 val32; |
| 286 | u32 ttl; |
Kim Phillips | 0348748 | 2012-10-29 13:34:40 +0000 | [diff] [blame] | 287 | fsl_sata_reg_t __iomem *reg = sata->reg_base; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 288 | int i; |
| 289 | |
| 290 | /* Check xfer length */ |
| 291 | if (len > SATA_HC_MAX_XFER_LEN) { |
| 292 | printf("max transfer length is 64MB\n\r"); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Setup the command descriptor */ |
| 297 | cmd_desc = sata->cmd_desc + tag; |
| 298 | |
| 299 | /* Get the pointer cfis of command descriptor */ |
| 300 | h2d = (sata_fis_h2d_t *)cmd_desc->cfis; |
| 301 | |
| 302 | /* Zero the cfis of command descriptor */ |
| 303 | memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE); |
| 304 | |
| 305 | /* Copy the cfis from user to command descriptor */ |
| 306 | h2d->fis_type = cfis->fis_type; |
| 307 | h2d->pm_port_c = cfis->pm_port_c; |
| 308 | h2d->command = cfis->command; |
| 309 | |
| 310 | h2d->features = cfis->features; |
| 311 | h2d->features_exp = cfis->features_exp; |
| 312 | |
| 313 | h2d->lba_low = cfis->lba_low; |
| 314 | h2d->lba_mid = cfis->lba_mid; |
| 315 | h2d->lba_high = cfis->lba_high; |
| 316 | h2d->lba_low_exp = cfis->lba_low_exp; |
| 317 | h2d->lba_mid_exp = cfis->lba_mid_exp; |
| 318 | h2d->lba_high_exp = cfis->lba_high_exp; |
| 319 | |
| 320 | if (!is_ncq) { |
| 321 | h2d->sector_count = cfis->sector_count; |
| 322 | h2d->sector_count_exp = cfis->sector_count_exp; |
| 323 | } else { /* NCQ */ |
| 324 | h2d->sector_count = (u8)(tag << 3); |
| 325 | } |
| 326 | |
| 327 | h2d->device = cfis->device; |
| 328 | h2d->control = cfis->control; |
| 329 | |
| 330 | /* Setup the PRD table */ |
| 331 | prde = (prd_entry_t *)cmd_desc->prdt; |
| 332 | memset((void *)prde, 0, sizeof(struct prdt)); |
| 333 | |
| 334 | prde_count = 0; |
| 335 | ttl = len; |
| 336 | for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) { |
| 337 | if (!len) |
| 338 | break; |
| 339 | prde->dba = cpu_to_le32((u32)buffer & ~0x3); |
| 340 | debug("dba = %08x\n\r", (u32)buffer); |
| 341 | |
| 342 | if (len < PRD_ENTRY_MAX_XFER_SZ) { |
| 343 | ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len; |
| 344 | debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len); |
| 345 | prde->ext_c_ddc = cpu_to_le32(ext_c_ddc); |
| 346 | prde_count++; |
| 347 | prde++; |
| 348 | break; |
| 349 | } else { |
| 350 | ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */ |
| 351 | debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len); |
| 352 | prde->ext_c_ddc = cpu_to_le32(ext_c_ddc); |
| 353 | buffer += PRD_ENTRY_MAX_XFER_SZ; |
| 354 | len -= PRD_ENTRY_MAX_XFER_SZ; |
| 355 | prde_count++; |
| 356 | prde++; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Setup the command slot of cmd hdr */ |
| 361 | cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag]; |
| 362 | |
| 363 | cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3); |
| 364 | |
| 365 | val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT; |
| 366 | val32 |= sizeof(sata_fis_h2d_t); |
| 367 | cmd_hdr->prde_fis_len = cpu_to_le32(val32); |
| 368 | |
| 369 | cmd_hdr->ttl = cpu_to_le32(ttl); |
| 370 | |
| 371 | if (!is_ncq) { |
| 372 | val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP; |
| 373 | } else { |
| 374 | val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA; |
| 375 | } |
| 376 | |
| 377 | tag &= CMD_HDR_ATTR_TAG; |
| 378 | val32 |= tag; |
| 379 | |
| 380 | debug("attribute = %08x\n\r", val32); |
| 381 | cmd_hdr->attribute = cpu_to_le32(val32); |
| 382 | |
Vagrant Cascadian | d65d684 | 2015-11-24 14:45:02 -0800 | [diff] [blame] | 383 | /* Make sure cmd desc and cmd slot valid before command issue */ |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 384 | sync(); |
| 385 | |
| 386 | /* PMP*/ |
| 387 | val32 = (u32)(h2d->pm_port_c & 0x0f); |
| 388 | out_le32(®->cqpmp, val32); |
| 389 | |
| 390 | /* Wait no active */ |
| 391 | if (ata_wait_register(®->car, (1 << tag), 0, 10000)) |
| 392 | printf("Wait no active time out\n\r"); |
| 393 | |
| 394 | /* Issue command */ |
| 395 | if (!(in_le32(®->cqr) & (1 << tag))) { |
| 396 | val32 = 1 << tag; |
| 397 | out_le32(®->cqr, val32); |
| 398 | } |
| 399 | |
| 400 | /* Wait command completed for 10s */ |
| 401 | if (ata_wait_register(®->ccr, (1 << tag), (1 << tag), 10000)) { |
| 402 | if (!is_ncq) |
| 403 | printf("Non-NCQ command time out\n\r"); |
| 404 | else |
| 405 | printf("NCQ command time out\n\r"); |
| 406 | } |
| 407 | |
| 408 | val32 = in_le32(®->cer); |
| 409 | |
| 410 | if (val32) { |
| 411 | u32 der; |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 412 | fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 413 | printf("CE at device\n\r"); |
| 414 | fsl_sata_dump_regs(reg); |
| 415 | der = in_le32(®->der); |
| 416 | out_le32(®->cer, val32); |
| 417 | out_le32(®->der, der); |
| 418 | } |
| 419 | |
| 420 | /* Clear complete flags */ |
| 421 | val32 = in_le32(®->ccr); |
| 422 | out_le32(®->ccr, val32); |
| 423 | |
| 424 | return len; |
| 425 | } |
| 426 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 427 | static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis, |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 428 | int tag, u8 *buffer, u32 len) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 433 | static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis, |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 434 | enum cmd_type command_type, int tag, u8 *buffer, u32 len) |
| 435 | { |
| 436 | int rc; |
| 437 | |
| 438 | if (tag > SATA_HC_MAX_CMD || tag < 0) { |
Kim Phillips | f91aa8d | 2008-07-10 14:00:15 -0500 | [diff] [blame] | 439 | printf("tag is out of range, tag=%d\n\r", tag); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | switch (command_type) { |
| 444 | case CMD_ATA: |
| 445 | rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len); |
| 446 | return rc; |
| 447 | case CMD_RESET: |
| 448 | rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len); |
| 449 | return rc; |
| 450 | case CMD_NCQ: |
| 451 | rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len); |
| 452 | return rc; |
| 453 | case CMD_ATAPI: |
| 454 | case CMD_VENDOR_BIST: |
| 455 | case CMD_BIST: |
| 456 | printf("not support now\n\r"); |
| 457 | return -1; |
| 458 | default: |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | return -1; |
| 463 | } |
| 464 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 465 | static void fsl_sata_xfer_mode(fsl_sata_t *sata, u16 *id) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 466 | { |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 467 | sata->pio = id[ATA_ID_PIO_MODES]; |
| 468 | sata->mwdma = id[ATA_ID_MWDMA_MODES]; |
| 469 | sata->udma = id[ATA_ID_UDMA_MODES]; |
| 470 | debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma); |
| 471 | } |
| 472 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 473 | static void fsl_sata_set_features(fsl_sata_t *sata) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 474 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 475 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 476 | u8 udma_cap; |
| 477 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 478 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 479 | |
| 480 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 481 | cfis->pm_port_c = 0x80; /* is command */ |
| 482 | cfis->command = ATA_CMD_SET_FEATURES; |
| 483 | cfis->features = SETFEATURES_XFER; |
| 484 | |
| 485 | /* First check the device capablity */ |
| 486 | udma_cap = (u8)(sata->udma & 0xff); |
| 487 | debug("udma_cap %02x\n\r", udma_cap); |
| 488 | |
| 489 | if (udma_cap == ATA_UDMA6) |
| 490 | cfis->sector_count = XFER_UDMA_6; |
| 491 | if (udma_cap == ATA_UDMA5) |
| 492 | cfis->sector_count = XFER_UDMA_5; |
| 493 | if (udma_cap == ATA_UDMA4) |
| 494 | cfis->sector_count = XFER_UDMA_4; |
| 495 | if (udma_cap == ATA_UDMA3) |
| 496 | cfis->sector_count = XFER_UDMA_3; |
| 497 | |
| 498 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0); |
| 499 | } |
| 500 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 501 | static u32 fsl_sata_rw_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt, u8 *buffer, |
| 502 | int is_write) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 503 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 504 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 505 | u32 block; |
| 506 | |
| 507 | block = start; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 508 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 509 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 510 | |
| 511 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 512 | cfis->pm_port_c = 0x80; /* is command */ |
Dave Liu | b9342a0 | 2008-04-01 15:22:11 +0800 | [diff] [blame] | 513 | cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 514 | cfis->device = ATA_LBA; |
| 515 | |
| 516 | cfis->device |= (block >> 24) & 0xf; |
| 517 | cfis->lba_high = (block >> 16) & 0xff; |
| 518 | cfis->lba_mid = (block >> 8) & 0xff; |
| 519 | cfis->lba_low = block & 0xff; |
| 520 | cfis->sector_count = (u8)(blkcnt & 0xff); |
| 521 | |
| 522 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt); |
| 523 | return blkcnt; |
| 524 | } |
| 525 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 526 | static void fsl_sata_flush_cache(fsl_sata_t *sata) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 527 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 528 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 529 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 530 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 531 | |
| 532 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 533 | cfis->pm_port_c = 0x80; /* is command */ |
Dave Liu | b9342a0 | 2008-04-01 15:22:11 +0800 | [diff] [blame] | 534 | cfis->command = ATA_CMD_FLUSH; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 535 | |
| 536 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0); |
| 537 | } |
| 538 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 539 | static u32 fsl_sata_rw_cmd_ext(fsl_sata_t *sata, u32 start, u32 blkcnt, |
| 540 | u8 *buffer, int is_write) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 541 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 542 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 543 | u64 block; |
| 544 | |
| 545 | block = (u64)start; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 546 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 547 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 548 | |
| 549 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 550 | cfis->pm_port_c = 0x80; /* is command */ |
| 551 | |
Dave Liu | b9342a0 | 2008-04-01 15:22:11 +0800 | [diff] [blame] | 552 | cfis->command = (is_write) ? ATA_CMD_WRITE_EXT |
| 553 | : ATA_CMD_READ_EXT; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 554 | |
| 555 | cfis->lba_high_exp = (block >> 40) & 0xff; |
| 556 | cfis->lba_mid_exp = (block >> 32) & 0xff; |
| 557 | cfis->lba_low_exp = (block >> 24) & 0xff; |
| 558 | cfis->lba_high = (block >> 16) & 0xff; |
| 559 | cfis->lba_mid = (block >> 8) & 0xff; |
| 560 | cfis->lba_low = block & 0xff; |
| 561 | cfis->device = ATA_LBA; |
| 562 | cfis->sector_count_exp = (blkcnt >> 8) & 0xff; |
| 563 | cfis->sector_count = blkcnt & 0xff; |
| 564 | |
| 565 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt); |
| 566 | return blkcnt; |
| 567 | } |
| 568 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 569 | static u32 fsl_sata_rw_ncq_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt, |
| 570 | u8 *buffer, int is_write) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 571 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 572 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 573 | int ncq_channel; |
| 574 | u64 block; |
| 575 | |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 576 | if (sata->lba48 != 1) { |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 577 | printf("execute FPDMA command on non-LBA48 hard disk\n\r"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | block = (u64)start; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 582 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 583 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 584 | |
| 585 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 586 | cfis->pm_port_c = 0x80; /* is command */ |
| 587 | |
Dave Liu | b9342a0 | 2008-04-01 15:22:11 +0800 | [diff] [blame] | 588 | cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE |
| 589 | : ATA_CMD_FPDMA_READ; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 590 | |
| 591 | cfis->lba_high_exp = (block >> 40) & 0xff; |
| 592 | cfis->lba_mid_exp = (block >> 32) & 0xff; |
| 593 | cfis->lba_low_exp = (block >> 24) & 0xff; |
| 594 | cfis->lba_high = (block >> 16) & 0xff; |
| 595 | cfis->lba_mid = (block >> 8) & 0xff; |
| 596 | cfis->lba_low = block & 0xff; |
| 597 | |
| 598 | cfis->device = ATA_LBA; |
| 599 | cfis->features_exp = (blkcnt >> 8) & 0xff; |
| 600 | cfis->features = blkcnt & 0xff; |
| 601 | |
| 602 | if (sata->queue_depth >= SATA_HC_MAX_CMD) |
| 603 | ncq_channel = SATA_HC_MAX_CMD - 1; |
| 604 | else |
| 605 | ncq_channel = sata->queue_depth - 1; |
| 606 | |
| 607 | /* Use the latest queue */ |
| 608 | fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt); |
| 609 | return blkcnt; |
| 610 | } |
| 611 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 612 | static void fsl_sata_flush_cache_ext(fsl_sata_t *sata) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 613 | { |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 614 | struct sata_fis_h2d h2d, *cfis = &h2d; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 615 | |
galak | 18cf84a | 2009-07-07 15:53:21 -0500 | [diff] [blame] | 616 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 617 | |
| 618 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 619 | cfis->pm_port_c = 0x80; /* is command */ |
Dave Liu | b9342a0 | 2008-04-01 15:22:11 +0800 | [diff] [blame] | 620 | cfis->command = ATA_CMD_FLUSH_EXT; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 621 | |
| 622 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0); |
| 623 | } |
| 624 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 625 | static void fsl_sata_init_wcache(fsl_sata_t *sata, u16 *id) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 626 | { |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 627 | if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id)) |
| 628 | sata->wcache = 1; |
| 629 | if (ata_id_has_flush(id)) |
| 630 | sata->flush = 1; |
| 631 | if (ata_id_has_flush_ext(id)) |
| 632 | sata->flush_ext = 1; |
| 633 | } |
| 634 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 635 | static u32 ata_low_level_rw_lba48(fsl_sata_t *sata, u32 blknr, lbaint_t blkcnt, |
| 636 | const void *buffer, int is_write) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 637 | { |
| 638 | u32 start, blks; |
| 639 | u8 *addr; |
| 640 | int max_blks; |
| 641 | |
| 642 | start = blknr; |
| 643 | blks = blkcnt; |
| 644 | addr = (u8 *)buffer; |
| 645 | |
| 646 | max_blks = ATA_MAX_SECTORS_LBA48; |
| 647 | do { |
| 648 | if (blks > max_blks) { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 649 | if (sata->dma_flag != FLAGS_FPDMA) |
| 650 | fsl_sata_rw_cmd_ext(sata, start, max_blks, addr, |
| 651 | is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 652 | else |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 653 | fsl_sata_rw_ncq_cmd(sata, start, max_blks, addr, |
| 654 | is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 655 | start += max_blks; |
| 656 | blks -= max_blks; |
| 657 | addr += ATA_SECT_SIZE * max_blks; |
| 658 | } else { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 659 | if (sata->dma_flag != FLAGS_FPDMA) |
| 660 | fsl_sata_rw_cmd_ext(sata, start, blks, addr, |
| 661 | is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 662 | else |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 663 | fsl_sata_rw_ncq_cmd(sata, start, blks, addr, |
| 664 | is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 665 | start += blks; |
| 666 | blks = 0; |
| 667 | addr += ATA_SECT_SIZE * blks; |
| 668 | } |
| 669 | } while (blks != 0); |
| 670 | |
| 671 | return blkcnt; |
| 672 | } |
| 673 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 674 | static u32 ata_low_level_rw_lba28(fsl_sata_t *sata, u32 blknr, u32 blkcnt, |
Kim Phillips | 0348748 | 2012-10-29 13:34:40 +0000 | [diff] [blame] | 675 | const void *buffer, int is_write) |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 676 | { |
| 677 | u32 start, blks; |
| 678 | u8 *addr; |
| 679 | int max_blks; |
| 680 | |
| 681 | start = blknr; |
| 682 | blks = blkcnt; |
| 683 | addr = (u8 *)buffer; |
| 684 | |
| 685 | max_blks = ATA_MAX_SECTORS; |
| 686 | do { |
| 687 | if (blks > max_blks) { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 688 | fsl_sata_rw_cmd(sata, start, max_blks, addr, is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 689 | start += max_blks; |
| 690 | blks -= max_blks; |
| 691 | addr += ATA_SECT_SIZE * max_blks; |
| 692 | } else { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 693 | fsl_sata_rw_cmd(sata, start, blks, addr, is_write); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 694 | start += blks; |
| 695 | blks = 0; |
| 696 | addr += ATA_SECT_SIZE * blks; |
| 697 | } |
| 698 | } while (blks != 0); |
| 699 | |
| 700 | return blkcnt; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * SATA interface between low level driver and command layer |
| 705 | */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 706 | static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 707 | void *buffer) |
| 708 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 709 | struct fsl_ata_priv *priv = dev_get_plat(dev); |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 710 | fsl_sata_t *sata = priv->fsl_sata; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 711 | u32 rc; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 712 | |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 713 | if (sata->lba48) |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 714 | rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer, |
| 715 | READ_CMD); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 716 | else |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 717 | rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer, |
| 718 | READ_CMD); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 719 | return rc; |
| 720 | } |
| 721 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 722 | static ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 723 | const void *buffer) |
| 724 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 725 | struct fsl_ata_priv *priv = dev_get_plat(dev); |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 726 | fsl_sata_t *sata = priv->fsl_sata; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 727 | u32 rc; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 728 | |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 729 | if (sata->lba48) { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 730 | rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer, |
| 731 | WRITE_CMD); |
| 732 | if (sata->wcache && sata->flush_ext) |
| 733 | fsl_sata_flush_cache_ext(sata); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 734 | } else { |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 735 | rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer, |
| 736 | WRITE_CMD); |
| 737 | if (sata->wcache && sata->flush) |
| 738 | fsl_sata_flush_cache(sata); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 739 | } |
| 740 | return rc; |
| 741 | } |
| 742 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 743 | static void fsl_sata_identify(fsl_sata_t *sata, u16 *id) |
| 744 | { |
| 745 | struct sata_fis_h2d h2d, *cfis = &h2d; |
| 746 | |
| 747 | memset(cfis, 0, sizeof(struct sata_fis_h2d)); |
| 748 | |
| 749 | cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D; |
| 750 | cfis->pm_port_c = 0x80; /* is command */ |
| 751 | cfis->command = ATA_CMD_ID_ATA; |
| 752 | |
| 753 | fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2); |
| 754 | ata_swap_buf_le16(id, ATA_ID_WORDS); |
| 755 | } |
| 756 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 757 | static int scan_sata(struct udevice *dev) |
| 758 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 759 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 760 | struct fsl_ata_priv *priv = dev_get_plat(dev); |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 761 | fsl_sata_t *sata = priv->fsl_sata; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 762 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 763 | unsigned char serial[ATA_ID_SERNO_LEN + 1]; |
| 764 | unsigned char firmware[ATA_ID_FW_REV_LEN + 1]; |
| 765 | unsigned char product[ATA_ID_PROD_LEN + 1]; |
| 766 | u16 *id; |
| 767 | u64 n_sectors; |
| 768 | |
| 769 | /* if no detected link */ |
| 770 | if (!sata->link) |
| 771 | return -1; |
| 772 | |
| 773 | id = (u16 *)malloc(ATA_ID_WORDS * 2); |
| 774 | if (!id) { |
| 775 | printf("id malloc failed\n\r"); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | /* Identify device to get information */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 780 | fsl_sata_identify(sata, id); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 781 | |
| 782 | /* Serial number */ |
| 783 | ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 784 | |
| 785 | /* Firmware version */ |
| 786 | ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 787 | |
| 788 | /* Product model */ |
| 789 | ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product)); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 790 | |
| 791 | /* Totoal sectors */ |
| 792 | n_sectors = ata_id_n_sectors(id); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 793 | |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 794 | #ifdef CONFIG_LBA48 |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 795 | /* Check if support LBA48 */ |
| 796 | if (ata_id_has_lba48(id)) { |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 797 | sata->lba48 = 1; |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 798 | debug("Device support LBA48\n\r"); |
Tang Yuantian | a89db42 | 2011-10-03 12:18:41 -0700 | [diff] [blame] | 799 | } else |
| 800 | debug("Device supports LBA28\n\r"); |
| 801 | #endif |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 802 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 803 | memcpy(desc->product, serial, sizeof(serial)); |
| 804 | memcpy(desc->revision, firmware, sizeof(firmware)); |
| 805 | memcpy(desc->vendor, product, sizeof(product)); |
| 806 | desc->lba = n_sectors; |
| 807 | #ifdef CONFIG_LBA48 |
| 808 | desc->lba48 = sata->lba48; |
| 809 | #endif |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 810 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 811 | /* Get the NCQ queue depth from device */ |
| 812 | sata->queue_depth = ata_id_queue_depth(id); |
| 813 | |
| 814 | /* Get the xfer mode from device */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 815 | fsl_sata_xfer_mode(sata, id); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 816 | |
| 817 | /* Get the write cache status from device */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 818 | fsl_sata_init_wcache(sata, id); |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 819 | |
| 820 | /* Set the xfer mode to highest speed */ |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 821 | fsl_sata_set_features(sata); |
| 822 | |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 823 | #ifdef DEBUG |
Dave Liu | 6f1a8a2 | 2008-03-26 22:55:32 +0800 | [diff] [blame] | 824 | ata_dump_id(id); |
| 825 | #endif |
| 826 | free((void *)id); |
| 827 | return 0; |
| 828 | } |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 829 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 830 | static const struct blk_ops sata_fsl_blk_ops = { |
| 831 | .read = sata_read, |
| 832 | .write = sata_write, |
| 833 | }; |
| 834 | |
| 835 | U_BOOT_DRIVER(sata_fsl_driver) = { |
| 836 | .name = "sata_fsl_blk", |
| 837 | .id = UCLASS_BLK, |
| 838 | .ops = &sata_fsl_blk_ops, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 839 | .plat_auto = sizeof(struct fsl_ata_priv), |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 840 | }; |
| 841 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 842 | static int fsl_ata_of_to_plat(struct udevice *dev) |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 843 | { |
| 844 | struct fsl_ata_priv *priv = dev_get_priv(dev); |
| 845 | |
| 846 | priv->number = dev_read_u32_default(dev, "sata-number", -1); |
| 847 | priv->flag = dev_read_u32_default(dev, "sata-fpdma", -1); |
| 848 | priv->offset = dev_read_u32_default(dev, "sata-offset", -1); |
| 849 | |
| 850 | priv->base = dev_read_addr(dev); |
| 851 | if (priv->base == FDT_ADDR_T_NONE) |
| 852 | return -EINVAL; |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 857 | static int fsl_unbind_device(struct udevice *dev) |
| 858 | { |
| 859 | int ret; |
| 860 | |
| 861 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 862 | if (ret) |
| 863 | return ret; |
| 864 | |
| 865 | ret = device_unbind(dev); |
| 866 | if (ret) |
| 867 | return ret; |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 872 | static int fsl_ata_probe(struct udevice *dev) |
| 873 | { |
| 874 | struct fsl_ata_priv *blk_priv, *priv; |
| 875 | struct udevice *blk; |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 876 | int failed_number; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 877 | char sata_name[10]; |
| 878 | int nr_ports; |
| 879 | int ret; |
| 880 | int i; |
| 881 | |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 882 | failed_number = 0; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 883 | priv = dev_get_priv(dev); |
| 884 | nr_ports = priv->number; |
| 885 | nr_ports = min(nr_ports, CONFIG_SYS_SATA_MAX_DEVICE); |
| 886 | |
| 887 | for (i = 0; i < nr_ports; i++) { |
| 888 | snprintf(sata_name, sizeof(sata_name), "fsl_sata%d", i); |
| 889 | ret = blk_create_devicef(dev, "sata_fsl_blk", sata_name, |
Bin Meng | 2294ecb | 2023-09-26 16:43:31 +0800 | [diff] [blame] | 890 | UCLASS_AHCI, -1, DEFAULT_BLKSZ, |
| 891 | 0, &blk); |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 892 | if (ret) { |
| 893 | debug("Can't create device\n"); |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | /* Init SATA port */ |
| 898 | ret = init_sata(priv, i); |
| 899 | if (ret) { |
| 900 | debug("%s: Failed to init sata\n", __func__); |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 901 | ret = fsl_unbind_device(blk); |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | |
| 905 | failed_number++; |
| 906 | continue; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 909 | blk_priv = dev_get_plat(blk); |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 910 | blk_priv->fsl_sata = priv->fsl_sata; |
| 911 | /* Scan SATA port */ |
| 912 | ret = scan_sata(blk); |
| 913 | if (ret) { |
| 914 | debug("%s: Failed to scan bus\n", __func__); |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 915 | ret = fsl_unbind_device(blk); |
| 916 | if (ret) |
| 917 | return ret; |
| 918 | |
| 919 | failed_number++; |
| 920 | continue; |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 921 | } |
AKASHI Takahiro | 927a7a5 | 2022-03-08 20:36:43 +0900 | [diff] [blame] | 922 | |
| 923 | ret = device_probe(dev); |
| 924 | if (ret < 0) { |
| 925 | debug("Probing %s failed (%d)\n", dev->name, ret); |
| 926 | ret = fsl_unbind_device(blk); |
| 927 | if (ret) |
| 928 | return ret; |
| 929 | |
| 930 | failed_number++; |
| 931 | continue; |
| 932 | } |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 935 | if (failed_number == nr_ports) |
| 936 | return -ENODEV; |
| 937 | else |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | static int fsl_ata_remove(struct udevice *dev) |
| 942 | { |
| 943 | fsl_sata_t *sata; |
| 944 | struct fsl_ata_priv *priv; |
| 945 | |
| 946 | priv = dev_get_priv(dev); |
| 947 | sata = priv->fsl_sata; |
| 948 | |
| 949 | free(sata->cmd_hdr_tbl_offset); |
| 950 | free(sata->cmd_desc_offset); |
| 951 | free(sata); |
| 952 | |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int sata_fsl_scan(struct udevice *dev) |
| 957 | { |
| 958 | /* Nothing to do here */ |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | struct ahci_ops sata_fsl_ahci_ops = { |
| 964 | .scan = sata_fsl_scan, |
| 965 | }; |
| 966 | |
| 967 | static const struct udevice_id fsl_ata_ids[] = { |
| 968 | { .compatible = "fsl,pq-sata-v2" }, |
| 969 | { } |
| 970 | }; |
| 971 | |
| 972 | U_BOOT_DRIVER(fsl_ahci) = { |
| 973 | .name = "fsl_ahci", |
| 974 | .id = UCLASS_AHCI, |
| 975 | .of_match = fsl_ata_ids, |
| 976 | .ops = &sata_fsl_ahci_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 977 | .of_to_plat = fsl_ata_of_to_plat, |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 978 | .probe = fsl_ata_probe, |
Peng Ma | c15f177 | 2019-12-04 10:36:45 +0000 | [diff] [blame] | 979 | .remove = fsl_ata_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 980 | .priv_auto = sizeof(struct fsl_ata_priv), |
Peng Ma | 64a6ec3 | 2019-11-19 06:17:36 +0000 | [diff] [blame] | 981 | }; |