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