blob: d225289fe6ea41e0e96bf9ea3b9365a93361fb18 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefano Babic771bfd12012-02-22 00:24:39 +00002/*
3 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
4 * Terry Lv <r65388@freescale.com>
Stefano Babic771bfd12012-02-22 00:24:39 +00005 */
6
Stefano Babic771bfd12012-02-22 00:24:39 +00007#include <ahci.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <blk.h>
Heinrich Schuchardt41b54472024-08-08 09:08:03 +02009#include <bootdev.h>
Jiaxun Yang0495a052024-05-17 19:14:53 +010010#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Simon Glass0067b872017-07-29 11:35:16 -060012#include <dm.h>
13#include <dwc_ahsata.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000014#include <fis.h>
Simon Glass602cedc2017-07-29 11:35:08 -060015#include <libata.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000017#include <malloc.h>
Simon Glassf89b2502017-07-29 11:35:12 -060018#include <memalign.h>
Simon Glass655306c2020-05-10 11:39:58 -060019#include <part.h>
Simon Glass602cedc2017-07-29 11:35:08 -060020#include <sata.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <asm/cache.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000022#include <asm/io.h>
Jiaxun Yang0495a052024-05-17 19:14:53 +010023#if IS_ENABLED(CONFIG_ARCH_MX5) || IS_ENABLED(CONFIG_ARCH_MX6)
Stefano Babic771bfd12012-02-22 00:24:39 +000024#include <asm/arch/clock.h>
Tim Harveye9d13472014-05-07 22:23:35 -070025#include <asm/arch/sys_proto.h>
Soeren Moch5569bbd2019-03-01 13:10:59 +010026#include <asm/mach-imx/sata.h>
Jiaxun Yang0495a052024-05-17 19:14:53 +010027#endif
Simon Glass602cedc2017-07-29 11:35:08 -060028#include <linux/bitops.h>
29#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Simon Glass602cedc2017-07-29 11:35:08 -060031#include <linux/errno.h>
Simon Glass7b2a6292017-07-29 11:35:09 -060032#include "dwc_ahsata_priv.h"
Stefano Babic771bfd12012-02-22 00:24:39 +000033
34struct sata_port_regs {
35 u32 clb;
36 u32 clbu;
37 u32 fb;
38 u32 fbu;
39 u32 is;
40 u32 ie;
41 u32 cmd;
42 u32 res1[1];
43 u32 tfd;
44 u32 sig;
45 u32 ssts;
46 u32 sctl;
47 u32 serr;
48 u32 sact;
49 u32 ci;
50 u32 sntf;
51 u32 res2[1];
52 u32 dmacr;
53 u32 res3[1];
54 u32 phycr;
55 u32 physr;
56};
57
58struct sata_host_regs {
59 u32 cap;
60 u32 ghc;
61 u32 is;
62 u32 pi;
63 u32 vs;
64 u32 ccc_ctl;
65 u32 ccc_ports;
66 u32 res1[2];
67 u32 cap2;
68 u32 res2[30];
69 u32 bistafr;
70 u32 bistcr;
71 u32 bistfctr;
72 u32 bistsr;
73 u32 bistdecr;
74 u32 res3[2];
75 u32 oobr;
76 u32 res4[8];
77 u32 timer1ms;
78 u32 res5[1];
79 u32 gparam1r;
80 u32 gparam2r;
81 u32 pparamr;
82 u32 testr;
83 u32 versionr;
84 u32 idr;
85};
86
87#define MAX_DATA_BYTES_PER_SG (4 * 1024 * 1024)
88#define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
89
90#define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
91
Tang Yuantian3f262d02015-07-09 14:37:30 +080092static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
Stefano Babic771bfd12012-02-22 00:24:39 +000093{
94 return base + 0x100 + (port * 0x80);
95}
96
97static int waiting_for_cmd_completed(u8 *offset,
98 int timeout_msec,
99 u32 sign)
100{
101 int i;
102 u32 status;
103
104 for (i = 0;
105 ((status = readl(offset)) & sign) && i < timeout_msec;
106 ++i)
107 mdelay(1);
108
109 return (i < timeout_msec) ? 0 : -1;
110}
111
Simon Glassb1f7f582017-07-29 11:35:04 -0600112static int ahci_setup_oobr(struct ahci_uc_priv *uc_priv, int clk)
Stefano Babic771bfd12012-02-22 00:24:39 +0000113{
Simon Glassd30e76c2017-07-29 11:35:05 -0600114 struct sata_host_regs *host_mmio = uc_priv->mmio_base;
Stefano Babic771bfd12012-02-22 00:24:39 +0000115
Simon Glass96f2af42017-07-29 11:35:07 -0600116 writel(SATA_HOST_OOBR_WE, &host_mmio->oobr);
117 writel(0x02060b14, &host_mmio->oobr);
Stefano Babic771bfd12012-02-22 00:24:39 +0000118
119 return 0;
120}
121
Jiaxun Yang0495a052024-05-17 19:14:53 +0100122static int ahci_host_init(struct ahci_uc_priv *uc_priv, int clk)
Stefano Babic771bfd12012-02-22 00:24:39 +0000123{
124 u32 tmp, cap_save, num_ports;
125 int i, j, timeout = 1000;
126 struct sata_port_regs *port_mmio = NULL;
Simon Glassd30e76c2017-07-29 11:35:05 -0600127 struct sata_host_regs *host_mmio = uc_priv->mmio_base;
Stefano Babic771bfd12012-02-22 00:24:39 +0000128
Simon Glass96f2af42017-07-29 11:35:07 -0600129 cap_save = readl(&host_mmio->cap);
Stefano Babic771bfd12012-02-22 00:24:39 +0000130 cap_save |= SATA_HOST_CAP_SSS;
131
132 /* global controller reset */
Simon Glass96f2af42017-07-29 11:35:07 -0600133 tmp = readl(&host_mmio->ghc);
Stefano Babic771bfd12012-02-22 00:24:39 +0000134 if ((tmp & SATA_HOST_GHC_HR) == 0)
Simon Glass96f2af42017-07-29 11:35:07 -0600135 writel_with_flush(tmp | SATA_HOST_GHC_HR, &host_mmio->ghc);
Stefano Babic771bfd12012-02-22 00:24:39 +0000136
Simon Glass96f2af42017-07-29 11:35:07 -0600137 while ((readl(&host_mmio->ghc) & SATA_HOST_GHC_HR) && --timeout)
Stefano Babic771bfd12012-02-22 00:24:39 +0000138 ;
139
140 if (timeout <= 0) {
141 debug("controller reset failed (0x%x)\n", tmp);
142 return -1;
143 }
144
145 /* Set timer 1ms */
Simon Glass96f2af42017-07-29 11:35:07 -0600146 writel(clk / 1000, &host_mmio->timer1ms);
Stefano Babic771bfd12012-02-22 00:24:39 +0000147
Simon Glassb1f7f582017-07-29 11:35:04 -0600148 ahci_setup_oobr(uc_priv, 0);
Stefano Babic771bfd12012-02-22 00:24:39 +0000149
Simon Glass96f2af42017-07-29 11:35:07 -0600150 writel_with_flush(SATA_HOST_GHC_AE, &host_mmio->ghc);
151 writel(cap_save, &host_mmio->cap);
Stefano Babic771bfd12012-02-22 00:24:39 +0000152 num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
Simon Glass96f2af42017-07-29 11:35:07 -0600153 writel_with_flush((1 << num_ports) - 1, &host_mmio->pi);
Stefano Babic771bfd12012-02-22 00:24:39 +0000154
155 /*
156 * Determine which Ports are implemented by the DWC_ahsata,
157 * by reading the PI register. This bit map value aids the
158 * software to determine how many Ports are available and
159 * which Port registers need to be initialized.
160 */
Simon Glass96f2af42017-07-29 11:35:07 -0600161 uc_priv->cap = readl(&host_mmio->cap);
162 uc_priv->port_map = readl(&host_mmio->pi);
Stefano Babic771bfd12012-02-22 00:24:39 +0000163
164 /* Determine how many command slots the HBA supports */
Simon Glassb1f7f582017-07-29 11:35:04 -0600165 uc_priv->n_ports = (uc_priv->cap & SATA_HOST_CAP_NP_MASK) + 1;
Stefano Babic771bfd12012-02-22 00:24:39 +0000166
167 debug("cap 0x%x port_map 0x%x n_ports %d\n",
Simon Glassb1f7f582017-07-29 11:35:04 -0600168 uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
Stefano Babic771bfd12012-02-22 00:24:39 +0000169
Simon Glassb1f7f582017-07-29 11:35:04 -0600170 for (i = 0; i < uc_priv->n_ports; i++) {
171 uc_priv->port[i].port_mmio = ahci_port_base(host_mmio, i);
Simon Glassd30e76c2017-07-29 11:35:05 -0600172 port_mmio = uc_priv->port[i].port_mmio;
Stefano Babic771bfd12012-02-22 00:24:39 +0000173
174 /* Ensure that the DWC_ahsata is in idle state */
Simon Glass96f2af42017-07-29 11:35:07 -0600175 tmp = readl(&port_mmio->cmd);
Stefano Babic771bfd12012-02-22 00:24:39 +0000176
177 /*
178 * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
179 * are all cleared, the Port is in an idle state.
180 */
181 if (tmp & (SATA_PORT_CMD_CR | SATA_PORT_CMD_FR |
182 SATA_PORT_CMD_FRE | SATA_PORT_CMD_ST)) {
183
184 /*
185 * System software places a Port into the idle state by
186 * clearing P#CMD.ST and waiting for P#CMD.CR to return
187 * 0 when read.
188 */
189 tmp &= ~SATA_PORT_CMD_ST;
Simon Glass96f2af42017-07-29 11:35:07 -0600190 writel_with_flush(tmp, &port_mmio->cmd);
Stefano Babic771bfd12012-02-22 00:24:39 +0000191
192 /*
193 * spec says 500 msecs for each bit, so
194 * this is slightly incorrect.
195 */
196 mdelay(500);
197
198 timeout = 1000;
Simon Glass96f2af42017-07-29 11:35:07 -0600199 while ((readl(&port_mmio->cmd) & SATA_PORT_CMD_CR)
Stefano Babic771bfd12012-02-22 00:24:39 +0000200 && --timeout)
201 ;
202
203 if (timeout <= 0) {
204 debug("port reset failed (0x%x)\n", tmp);
205 return -1;
206 }
207 }
208
209 /* Spin-up device */
Simon Glass96f2af42017-07-29 11:35:07 -0600210 tmp = readl(&port_mmio->cmd);
211 writel((tmp | SATA_PORT_CMD_SUD), &port_mmio->cmd);
Stefano Babic771bfd12012-02-22 00:24:39 +0000212
213 /* Wait for spin-up to finish */
214 timeout = 1000;
Simon Glass96f2af42017-07-29 11:35:07 -0600215 while (!(readl(&port_mmio->cmd) | SATA_PORT_CMD_SUD)
Stefano Babic771bfd12012-02-22 00:24:39 +0000216 && --timeout)
217 ;
218 if (timeout <= 0) {
219 debug("Spin-Up can't finish!\n");
220 return -1;
221 }
222
223 for (j = 0; j < 100; ++j) {
224 mdelay(10);
Simon Glass96f2af42017-07-29 11:35:07 -0600225 tmp = readl(&port_mmio->ssts);
Stefano Babic771bfd12012-02-22 00:24:39 +0000226 if (((tmp & SATA_PORT_SSTS_DET_MASK) == 0x3) ||
227 ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x1))
228 break;
229 }
230
231 /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
232 timeout = 1000;
Ye Lif1c562e2020-05-03 22:27:01 +0800233 while (!(readl(&port_mmio->serr) & SATA_PORT_SERR_DIAG_X)
Stefano Babic771bfd12012-02-22 00:24:39 +0000234 && --timeout)
235 ;
236 if (timeout <= 0) {
237 debug("Can't find DIAG_X set!\n");
238 return -1;
239 }
240
241 /*
242 * For each implemented Port, clear the P#SERR
243 * register, by writing ones to each implemented\
244 * bit location.
245 */
Simon Glass96f2af42017-07-29 11:35:07 -0600246 tmp = readl(&port_mmio->serr);
Stefano Babic771bfd12012-02-22 00:24:39 +0000247 debug("P#SERR 0x%x\n",
248 tmp);
Simon Glass96f2af42017-07-29 11:35:07 -0600249 writel(tmp, &port_mmio->serr);
Stefano Babic771bfd12012-02-22 00:24:39 +0000250
251 /* Ack any pending irq events for this port */
Simon Glass96f2af42017-07-29 11:35:07 -0600252 tmp = readl(&host_mmio->is);
Stefano Babic771bfd12012-02-22 00:24:39 +0000253 debug("IS 0x%x\n", tmp);
254 if (tmp)
Simon Glass96f2af42017-07-29 11:35:07 -0600255 writel(tmp, &host_mmio->is);
Stefano Babic771bfd12012-02-22 00:24:39 +0000256
Simon Glass96f2af42017-07-29 11:35:07 -0600257 writel(1 << i, &host_mmio->is);
Stefano Babic771bfd12012-02-22 00:24:39 +0000258
259 /* set irq mask (enables interrupts) */
Simon Glass96f2af42017-07-29 11:35:07 -0600260 writel(DEF_PORT_IRQ, &port_mmio->ie);
Stefano Babic771bfd12012-02-22 00:24:39 +0000261
262 /* register linkup ports */
Simon Glass96f2af42017-07-29 11:35:07 -0600263 tmp = readl(&port_mmio->ssts);
Stefano Babic771bfd12012-02-22 00:24:39 +0000264 debug("Port %d status: 0x%x\n", i, tmp);
265 if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
Simon Glassb1f7f582017-07-29 11:35:04 -0600266 uc_priv->link_port_map |= (0x01 << i);
Stefano Babic771bfd12012-02-22 00:24:39 +0000267 }
268
Simon Glass96f2af42017-07-29 11:35:07 -0600269 tmp = readl(&host_mmio->ghc);
Stefano Babic771bfd12012-02-22 00:24:39 +0000270 debug("GHC 0x%x\n", tmp);
Simon Glass96f2af42017-07-29 11:35:07 -0600271 writel(tmp | SATA_HOST_GHC_IE, &host_mmio->ghc);
272 tmp = readl(&host_mmio->ghc);
Stefano Babic771bfd12012-02-22 00:24:39 +0000273 debug("GHC 0x%x\n", tmp);
274
275 return 0;
276}
277
Simon Glassb1f7f582017-07-29 11:35:04 -0600278static void ahci_print_info(struct ahci_uc_priv *uc_priv)
Stefano Babic771bfd12012-02-22 00:24:39 +0000279{
Simon Glassd30e76c2017-07-29 11:35:05 -0600280 struct sata_host_regs *host_mmio = uc_priv->mmio_base;
Stefano Babic771bfd12012-02-22 00:24:39 +0000281 u32 vers, cap, impl, speed;
282 const char *speed_s;
283 const char *scc_s;
284
Simon Glass96f2af42017-07-29 11:35:07 -0600285 vers = readl(&host_mmio->vs);
Simon Glassb1f7f582017-07-29 11:35:04 -0600286 cap = uc_priv->cap;
287 impl = uc_priv->port_map;
Stefano Babic771bfd12012-02-22 00:24:39 +0000288
289 speed = (cap & SATA_HOST_CAP_ISS_MASK)
290 >> SATA_HOST_CAP_ISS_OFFSET;
291 if (speed == 1)
292 speed_s = "1.5";
293 else if (speed == 2)
294 speed_s = "3";
295 else
296 speed_s = "?";
297
298 scc_s = "SATA";
299
300 printf("AHCI %02x%02x.%02x%02x "
301 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
302 (vers >> 24) & 0xff,
303 (vers >> 16) & 0xff,
304 (vers >> 8) & 0xff,
305 vers & 0xff,
306 ((cap >> 8) & 0x1f) + 1,
307 (cap & 0x1f) + 1,
308 speed_s,
309 impl,
310 scc_s);
311
312 printf("flags: "
313 "%s%s%s%s%s%s"
314 "%s%s%s%s%s%s%s\n",
315 cap & (1 << 31) ? "64bit " : "",
316 cap & (1 << 30) ? "ncq " : "",
317 cap & (1 << 28) ? "ilck " : "",
318 cap & (1 << 27) ? "stag " : "",
319 cap & (1 << 26) ? "pm " : "",
320 cap & (1 << 25) ? "led " : "",
321 cap & (1 << 24) ? "clo " : "",
322 cap & (1 << 19) ? "nz " : "",
323 cap & (1 << 18) ? "only " : "",
324 cap & (1 << 17) ? "pmp " : "",
325 cap & (1 << 15) ? "pio " : "",
326 cap & (1 << 14) ? "slum " : "",
327 cap & (1 << 13) ? "part " : "");
328}
329
Simon Glassb1f7f582017-07-29 11:35:04 -0600330static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
331 unsigned char *buf, int buf_len)
Stefano Babic771bfd12012-02-22 00:24:39 +0000332{
Simon Glass96f2af42017-07-29 11:35:07 -0600333 struct ahci_ioports *pp = &uc_priv->port[port];
Stefano Babic771bfd12012-02-22 00:24:39 +0000334 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100335 phys_addr_t pa = virt_to_phys(buf);
Stefano Babic771bfd12012-02-22 00:24:39 +0000336 u32 sg_count, max_bytes;
337 int i;
338
339 max_bytes = MAX_DATA_BYTES_PER_SG;
340 sg_count = ((buf_len - 1) / max_bytes) + 1;
341 if (sg_count > AHCI_MAX_SG) {
342 printf("Error:Too much sg!\n");
343 return -1;
344 }
345
346 for (i = 0; i < sg_count; i++) {
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100347 ahci_sg->addr = cpu_to_le32(lower_32_bits(pa));
348 ahci_sg->addr_hi = cpu_to_le32(upper_32_bits(pa));
Stefano Babic771bfd12012-02-22 00:24:39 +0000349 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
350 (buf_len < max_bytes
351 ? (buf_len - 1)
352 : (max_bytes - 1)));
353 ahci_sg++;
354 buf_len -= max_bytes;
355 }
356
357 return sg_count;
358}
359
360static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
361{
362 struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
363 AHCI_CMD_SLOT_SZ * cmd_slot);
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100364 phys_addr_t pa = virt_to_phys(pp->cmd_tbl);
Stefano Babic771bfd12012-02-22 00:24:39 +0000365
366 memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
367 cmd_hdr->opts = cpu_to_le32(opts);
368 cmd_hdr->status = 0;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100369 pp->cmd_slot->tbl_addr = cpu_to_le32(lower_32_bits(pa));
Tang Yuantian3f262d02015-07-09 14:37:30 +0800370#ifdef CONFIG_PHYS_64BIT
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100371 pp->cmd_slot->tbl_addr_hi = cpu_to_le32(upper_32_bits(pa));
Tang Yuantian3f262d02015-07-09 14:37:30 +0800372#endif
Stefano Babic771bfd12012-02-22 00:24:39 +0000373}
374
375#define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
376
Simon Glassb1f7f582017-07-29 11:35:04 -0600377static int ahci_exec_ata_cmd(struct ahci_uc_priv *uc_priv, u8 port,
378 struct sata_fis_h2d *cfis, u8 *buf, u32 buf_len,
379 s32 is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000380{
Simon Glass96f2af42017-07-29 11:35:07 -0600381 struct ahci_ioports *pp = &uc_priv->port[port];
Simon Glassd30e76c2017-07-29 11:35:05 -0600382 struct sata_port_regs *port_mmio = pp->port_mmio;
Stefano Babic771bfd12012-02-22 00:24:39 +0000383 u32 opts;
384 int sg_count = 0, cmd_slot = 0;
385
Simon Glass96f2af42017-07-29 11:35:07 -0600386 cmd_slot = AHCI_GET_CMD_SLOT(readl(&port_mmio->ci));
Stefano Babic771bfd12012-02-22 00:24:39 +0000387 if (32 == cmd_slot) {
388 printf("Can't find empty command slot!\n");
389 return 0;
390 }
391
392 /* Check xfer length */
393 if (buf_len > MAX_BYTES_PER_TRANS) {
394 printf("Max transfer length is %dB\n\r",
395 MAX_BYTES_PER_TRANS);
396 return 0;
397 }
398
399 memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
400 if (buf && buf_len)
Simon Glassb1f7f582017-07-29 11:35:04 -0600401 sg_count = ahci_fill_sg(uc_priv, port, buf, buf_len);
Stefano Babic771bfd12012-02-22 00:24:39 +0000402 opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
Eric Nelson998816b2013-06-15 16:09:55 -0700403 if (is_write) {
Stefano Babic771bfd12012-02-22 00:24:39 +0000404 opts |= 0x40;
Eric Nelson998816b2013-06-15 16:09:55 -0700405 flush_cache((ulong)buf, buf_len);
406 }
Stefano Babic771bfd12012-02-22 00:24:39 +0000407 ahci_fill_cmd_slot(pp, cmd_slot, opts);
408
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100409 flush_cache((ulong)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
Simon Glass96f2af42017-07-29 11:35:07 -0600410 writel_with_flush(1 << cmd_slot, &port_mmio->ci);
Stefano Babic771bfd12012-02-22 00:24:39 +0000411
Simon Glass96f2af42017-07-29 11:35:07 -0600412 if (waiting_for_cmd_completed((u8 *)&port_mmio->ci, 10000,
413 0x1 << cmd_slot)) {
Stefano Babic771bfd12012-02-22 00:24:39 +0000414 printf("timeout exit!\n");
415 return -1;
416 }
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100417 invalidate_dcache_range((ulong)(pp->cmd_slot),
418 (ulong)(pp->cmd_slot) + AHCI_PORT_PRIV_DMA_SZ);
Stefano Babic771bfd12012-02-22 00:24:39 +0000419 debug("ahci_exec_ata_cmd: %d byte transferred.\n",
420 pp->cmd_slot->status);
Eric Nelson998816b2013-06-15 16:09:55 -0700421 if (!is_write)
422 invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
Stefano Babic771bfd12012-02-22 00:24:39 +0000423
424 return buf_len;
425}
426
Simon Glassc5fc2a32017-07-29 11:35:06 -0600427static void ahci_set_feature(struct ahci_uc_priv *uc_priv, u8 port)
Stefano Babic771bfd12012-02-22 00:24:39 +0000428{
Eric Nelson998816b2013-06-15 16:09:55 -0700429 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
430 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000431
432 memset(cfis, 0, sizeof(struct sata_fis_h2d));
433 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
434 cfis->pm_port_c = 1 << 7;
435 cfis->command = ATA_CMD_SET_FEATURES;
436 cfis->features = SETFEATURES_XFER;
Simon Glassb1f7f582017-07-29 11:35:04 -0600437 cfis->sector_count = ffs(uc_priv->udma_mask + 1) + 0x3e;
Stefano Babic771bfd12012-02-22 00:24:39 +0000438
Simon Glassb1f7f582017-07-29 11:35:04 -0600439 ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, READ_CMD);
Stefano Babic771bfd12012-02-22 00:24:39 +0000440}
441
Simon Glassb1f7f582017-07-29 11:35:04 -0600442static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
Stefano Babic771bfd12012-02-22 00:24:39 +0000443{
Simon Glass96f2af42017-07-29 11:35:07 -0600444 struct ahci_ioports *pp = &uc_priv->port[port];
Simon Glassd30e76c2017-07-29 11:35:05 -0600445 struct sata_port_regs *port_mmio = pp->port_mmio;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100446 phys_addr_t dma_addr;
Stefano Babic771bfd12012-02-22 00:24:39 +0000447 u32 port_status;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100448 void *mem;
Stefano Babic771bfd12012-02-22 00:24:39 +0000449 int timeout = 10000000;
450
451 debug("Enter start port: %d\n", port);
Simon Glass96f2af42017-07-29 11:35:07 -0600452 port_status = readl(&port_mmio->ssts);
Stefano Babic771bfd12012-02-22 00:24:39 +0000453 debug("Port %d status: %x\n", port, port_status);
454 if ((port_status & 0xf) != 0x03) {
455 printf("No Link on this port!\n");
456 return -1;
457 }
458
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100459 mem = memalign(2048, AHCI_PORT_PRIV_DMA_SZ);
Stefano Babic771bfd12012-02-22 00:24:39 +0000460 if (!mem) {
Stefano Babic771bfd12012-02-22 00:24:39 +0000461 printf("No mem for table!\n");
462 return -ENOMEM;
463 }
464
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100465 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
Stefano Babic771bfd12012-02-22 00:24:39 +0000466
467 /*
468 * First item in chunk of DMA memory: 32-slot command table,
469 * 32 bytes each in size
470 */
471 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100472 mem += AHCI_CMD_SLOT_SZ * AHCI_MAX_CMD_SLOT;
Stefano Babic771bfd12012-02-22 00:24:39 +0000473
474 /*
475 * Second item: Received-FIS area, 256-Byte aligned
476 */
477 pp->rx_fis = mem;
478 mem += AHCI_RX_FIS_SZ;
479
480 /*
481 * Third item: data area for storing a single command
482 * and its scatter-gather table
483 */
484 pp->cmd_tbl = mem;
Stefano Babic771bfd12012-02-22 00:24:39 +0000485 mem += AHCI_CMD_TBL_HDR;
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100486 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
Stefano Babic771bfd12012-02-22 00:24:39 +0000487
Simon Glass96f2af42017-07-29 11:35:07 -0600488 writel_with_flush(0x00004444, &port_mmio->dmacr);
Jiaxun Yangaf5fd392024-05-17 19:14:52 +0100489 dma_addr = virt_to_phys(pp->cmd_slot);
490 debug("cmd_slot_dma = 0x%08llx\n", (u64)dma_addr);
491 writel_with_flush(lower_32_bits(dma_addr), &port_mmio->clb);
492 writel_with_flush(upper_32_bits(dma_addr), &port_mmio->clbu);
493 dma_addr = virt_to_phys(pp->cmd_slot);
494 debug("rx_fis_slot_dma = 0x%08llx\n", (u64)dma_addr);
495 writel_with_flush(lower_32_bits(dma_addr), &port_mmio->fb);
496 writel_with_flush(upper_32_bits(dma_addr), &port_mmio->fbu);
497
Stefano Babic771bfd12012-02-22 00:24:39 +0000498
499 /* Enable FRE */
Simon Glass96f2af42017-07-29 11:35:07 -0600500 writel_with_flush((SATA_PORT_CMD_FRE | readl(&port_mmio->cmd)),
501 &port_mmio->cmd);
Stefano Babic771bfd12012-02-22 00:24:39 +0000502
503 /* Wait device ready */
Simon Glass96f2af42017-07-29 11:35:07 -0600504 while ((readl(&port_mmio->tfd) & (SATA_PORT_TFD_STS_ERR |
Stefano Babic771bfd12012-02-22 00:24:39 +0000505 SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
506 && --timeout)
507 ;
508 if (timeout <= 0) {
509 debug("Device not ready for BSY, DRQ and"
510 "ERR in TFD!\n");
511 return -1;
512 }
513
514 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
515 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
Simon Glass96f2af42017-07-29 11:35:07 -0600516 PORT_CMD_START, &port_mmio->cmd);
Stefano Babic771bfd12012-02-22 00:24:39 +0000517
518 debug("Exit start port %d\n", port);
519
520 return 0;
521}
522
Simon Glassc5fc2a32017-07-29 11:35:06 -0600523static void dwc_ahsata_print_info(struct blk_desc *pdev)
Stefano Babic771bfd12012-02-22 00:24:39 +0000524{
Stefano Babic771bfd12012-02-22 00:24:39 +0000525 printf("SATA Device Info:\n\r");
Stefano Babic771bfd12012-02-22 00:24:39 +0000526 printf("S/N: %s\n\rProduct model number: %s\n\r"
Soeren Moch71657f12019-03-01 13:10:58 +0100527 "Firmware version: %s\n\rCapacity: " LBAFU " sectors\n\r",
Stefano Babic771bfd12012-02-22 00:24:39 +0000528 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
Stefano Babic771bfd12012-02-22 00:24:39 +0000529}
530
Simon Glassc5fc2a32017-07-29 11:35:06 -0600531static void dwc_ahsata_identify(struct ahci_uc_priv *uc_priv, u16 *id)
Stefano Babic771bfd12012-02-22 00:24:39 +0000532{
Eric Nelson998816b2013-06-15 16:09:55 -0700533 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
534 struct sata_fis_h2d *cfis = &h2d;
Simon Glassb1f7f582017-07-29 11:35:04 -0600535 u8 port = uc_priv->hard_port_no;
Stefano Babic771bfd12012-02-22 00:24:39 +0000536
537 memset(cfis, 0, sizeof(struct sata_fis_h2d));
538
539 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
540 cfis->pm_port_c = 0x80; /* is command */
541 cfis->command = ATA_CMD_ID_ATA;
542
Simon Glassb1f7f582017-07-29 11:35:04 -0600543 ahci_exec_ata_cmd(uc_priv, port, cfis, (u8 *)id, ATA_ID_WORDS * 2,
544 READ_CMD);
Stefano Babic771bfd12012-02-22 00:24:39 +0000545 ata_swap_buf_le16(id, ATA_ID_WORDS);
546}
547
Simon Glassc5fc2a32017-07-29 11:35:06 -0600548static void dwc_ahsata_xfer_mode(struct ahci_uc_priv *uc_priv, u16 *id)
Stefano Babic771bfd12012-02-22 00:24:39 +0000549{
Simon Glassb1f7f582017-07-29 11:35:04 -0600550 uc_priv->pio_mask = id[ATA_ID_PIO_MODES];
551 uc_priv->udma_mask = id[ATA_ID_UDMA_MODES];
552 debug("pio %04x, udma %04x\n\r", uc_priv->pio_mask, uc_priv->udma_mask);
Stefano Babic771bfd12012-02-22 00:24:39 +0000553}
554
Simon Glassc5fc2a32017-07-29 11:35:06 -0600555static u32 dwc_ahsata_rw_cmd(struct ahci_uc_priv *uc_priv, u32 start,
556 u32 blkcnt, u8 *buffer, int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000557{
Eric Nelson998816b2013-06-15 16:09:55 -0700558 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
559 struct sata_fis_h2d *cfis = &h2d;
Simon Glassb1f7f582017-07-29 11:35:04 -0600560 u8 port = uc_priv->hard_port_no;
Stefano Babic771bfd12012-02-22 00:24:39 +0000561 u32 block;
562
563 block = start;
564
565 memset(cfis, 0, sizeof(struct sata_fis_h2d));
566
567 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
568 cfis->pm_port_c = 0x80; /* is command */
569 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
570 cfis->device = ATA_LBA;
571
572 cfis->device |= (block >> 24) & 0xf;
573 cfis->lba_high = (block >> 16) & 0xff;
574 cfis->lba_mid = (block >> 8) & 0xff;
575 cfis->lba_low = block & 0xff;
576 cfis->sector_count = (u8)(blkcnt & 0xff);
577
Simon Glassb1f7f582017-07-29 11:35:04 -0600578 if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
579 ATA_SECT_SIZE * blkcnt, is_write) > 0)
Stefano Babic771bfd12012-02-22 00:24:39 +0000580 return blkcnt;
581 else
582 return 0;
583}
584
Simon Glassc5fc2a32017-07-29 11:35:06 -0600585static void dwc_ahsata_flush_cache(struct ahci_uc_priv *uc_priv)
Stefano Babic771bfd12012-02-22 00:24:39 +0000586{
Eric Nelson998816b2013-06-15 16:09:55 -0700587 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
588 struct sata_fis_h2d *cfis = &h2d;
Simon Glassb1f7f582017-07-29 11:35:04 -0600589 u8 port = uc_priv->hard_port_no;
Stefano Babic771bfd12012-02-22 00:24:39 +0000590
591 memset(cfis, 0, sizeof(struct sata_fis_h2d));
592
593 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
594 cfis->pm_port_c = 0x80; /* is command */
595 cfis->command = ATA_CMD_FLUSH;
596
Simon Glassb1f7f582017-07-29 11:35:04 -0600597 ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
Stefano Babic771bfd12012-02-22 00:24:39 +0000598}
599
Simon Glassc5fc2a32017-07-29 11:35:06 -0600600static u32 dwc_ahsata_rw_cmd_ext(struct ahci_uc_priv *uc_priv, u32 start,
601 lbaint_t blkcnt, u8 *buffer, int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000602{
Eric Nelson998816b2013-06-15 16:09:55 -0700603 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
604 struct sata_fis_h2d *cfis = &h2d;
Simon Glassb1f7f582017-07-29 11:35:04 -0600605 u8 port = uc_priv->hard_port_no;
Stefano Babic771bfd12012-02-22 00:24:39 +0000606 u64 block;
607
608 block = (u64)start;
609
610 memset(cfis, 0, sizeof(struct sata_fis_h2d));
611
612 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
613 cfis->pm_port_c = 0x80; /* is command */
614
615 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
616 : ATA_CMD_READ_EXT;
617
618 cfis->lba_high_exp = (block >> 40) & 0xff;
619 cfis->lba_mid_exp = (block >> 32) & 0xff;
620 cfis->lba_low_exp = (block >> 24) & 0xff;
621 cfis->lba_high = (block >> 16) & 0xff;
622 cfis->lba_mid = (block >> 8) & 0xff;
623 cfis->lba_low = block & 0xff;
624 cfis->device = ATA_LBA;
625 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
626 cfis->sector_count = blkcnt & 0xff;
627
Simon Glassb1f7f582017-07-29 11:35:04 -0600628 if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
629 ATA_SECT_SIZE * blkcnt, is_write) > 0)
Stefano Babic771bfd12012-02-22 00:24:39 +0000630 return blkcnt;
631 else
632 return 0;
633}
634
Simon Glassc5fc2a32017-07-29 11:35:06 -0600635static void dwc_ahsata_flush_cache_ext(struct ahci_uc_priv *uc_priv)
Stefano Babic771bfd12012-02-22 00:24:39 +0000636{
Eric Nelson998816b2013-06-15 16:09:55 -0700637 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
638 struct sata_fis_h2d *cfis = &h2d;
Simon Glassb1f7f582017-07-29 11:35:04 -0600639 u8 port = uc_priv->hard_port_no;
Stefano Babic771bfd12012-02-22 00:24:39 +0000640
641 memset(cfis, 0, sizeof(struct sata_fis_h2d));
642
643 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
644 cfis->pm_port_c = 0x80; /* is command */
645 cfis->command = ATA_CMD_FLUSH_EXT;
646
Simon Glassb1f7f582017-07-29 11:35:04 -0600647 ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
Stefano Babic771bfd12012-02-22 00:24:39 +0000648}
649
Simon Glassc5fc2a32017-07-29 11:35:06 -0600650static void dwc_ahsata_init_wcache(struct ahci_uc_priv *uc_priv, u16 *id)
Stefano Babic771bfd12012-02-22 00:24:39 +0000651{
Stefano Babic771bfd12012-02-22 00:24:39 +0000652 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
Simon Glassb1f7f582017-07-29 11:35:04 -0600653 uc_priv->flags |= SATA_FLAG_WCACHE;
Stefano Babic771bfd12012-02-22 00:24:39 +0000654 if (ata_id_has_flush(id))
Simon Glassb1f7f582017-07-29 11:35:04 -0600655 uc_priv->flags |= SATA_FLAG_FLUSH;
Stefano Babic771bfd12012-02-22 00:24:39 +0000656 if (ata_id_has_flush_ext(id))
Simon Glassb1f7f582017-07-29 11:35:04 -0600657 uc_priv->flags |= SATA_FLAG_FLUSH_EXT;
Stefano Babic771bfd12012-02-22 00:24:39 +0000658}
659
Simon Glassc5fc2a32017-07-29 11:35:06 -0600660static u32 ata_low_level_rw_lba48(struct ahci_uc_priv *uc_priv, u32 blknr,
661 lbaint_t blkcnt, const void *buffer,
662 int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000663{
664 u32 start, blks;
665 u8 *addr;
666 int max_blks;
667
668 start = blknr;
669 blks = blkcnt;
670 addr = (u8 *)buffer;
671
672 max_blks = ATA_MAX_SECTORS_LBA48;
673
674 do {
675 if (blks > max_blks) {
Simon Glassc5fc2a32017-07-29 11:35:06 -0600676 if (max_blks != dwc_ahsata_rw_cmd_ext(uc_priv, start,
677 max_blks, addr,
678 is_write))
Stefano Babic771bfd12012-02-22 00:24:39 +0000679 return 0;
680 start += max_blks;
681 blks -= max_blks;
682 addr += ATA_SECT_SIZE * max_blks;
683 } else {
Simon Glassc5fc2a32017-07-29 11:35:06 -0600684 if (blks != dwc_ahsata_rw_cmd_ext(uc_priv, start, blks,
685 addr, is_write))
Stefano Babic771bfd12012-02-22 00:24:39 +0000686 return 0;
687 start += blks;
688 blks = 0;
689 addr += ATA_SECT_SIZE * blks;
690 }
691 } while (blks != 0);
692
693 return blkcnt;
694}
695
Simon Glassc5fc2a32017-07-29 11:35:06 -0600696static u32 ata_low_level_rw_lba28(struct ahci_uc_priv *uc_priv, u32 blknr,
697 lbaint_t blkcnt, const void *buffer,
698 int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000699{
700 u32 start, blks;
701 u8 *addr;
702 int max_blks;
703
704 start = blknr;
705 blks = blkcnt;
706 addr = (u8 *)buffer;
707
708 max_blks = ATA_MAX_SECTORS;
709 do {
710 if (blks > max_blks) {
Simon Glassc5fc2a32017-07-29 11:35:06 -0600711 if (max_blks != dwc_ahsata_rw_cmd(uc_priv, start,
712 max_blks, addr,
713 is_write))
Stefano Babic771bfd12012-02-22 00:24:39 +0000714 return 0;
715 start += max_blks;
716 blks -= max_blks;
717 addr += ATA_SECT_SIZE * max_blks;
718 } else {
Simon Glassc5fc2a32017-07-29 11:35:06 -0600719 if (blks != dwc_ahsata_rw_cmd(uc_priv, start, blks,
720 addr, is_write))
Stefano Babic771bfd12012-02-22 00:24:39 +0000721 return 0;
722 start += blks;
723 blks = 0;
724 addr += ATA_SECT_SIZE * blks;
725 }
726 } while (blks != 0);
727
728 return blkcnt;
729}
730
Simon Glassf89b2502017-07-29 11:35:12 -0600731static int dwc_ahci_start_ports(struct ahci_uc_priv *uc_priv)
732{
733 u32 linkmap;
734 int i;
735
736 linkmap = uc_priv->link_port_map;
737
738 if (0 == linkmap) {
739 printf("No port device detected!\n");
740 return -ENXIO;
741 }
742
743 for (i = 0; i < uc_priv->n_ports; i++) {
744 if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
745 if (ahci_port_start(uc_priv, (u8)i)) {
746 printf("Can not start port %d\n", i);
747 return 1;
748 }
749 uc_priv->hard_port_no = i;
750 break;
751 }
752 }
753
754 return 0;
755}
756
757static int dwc_ahsata_scan_common(struct ahci_uc_priv *uc_priv,
758 struct blk_desc *pdev)
759{
760 u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
761 u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
762 u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
Simon Glassf89b2502017-07-29 11:35:12 -0600763 u8 port = uc_priv->hard_port_no;
764 ALLOC_CACHE_ALIGN_BUFFER(u16, id, ATA_ID_WORDS);
765
766 /* Identify device to get information */
767 dwc_ahsata_identify(uc_priv, id);
768
769 /* Serial number */
770 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
771 memcpy(pdev->product, serial, sizeof(serial));
772
773 /* Firmware version */
774 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
775 memcpy(pdev->revision, firmware, sizeof(firmware));
776
777 /* Product model */
778 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
779 memcpy(pdev->vendor, product, sizeof(product));
780
Soeren Moch71657f12019-03-01 13:10:58 +0100781 /* Total sectors */
782 pdev->lba = ata_id_n_sectors(id);
Simon Glassf89b2502017-07-29 11:35:12 -0600783
784 pdev->type = DEV_TYPE_HARDDISK;
785 pdev->blksz = ATA_SECT_SIZE;
786 pdev->lun = 0;
787
788 /* Check if support LBA48 */
789 if (ata_id_has_lba48(id)) {
790 pdev->lba48 = 1;
791 debug("Device support LBA48\n\r");
792 }
793
794 /* Get the NCQ queue depth from device */
795 uc_priv->flags &= (~SATA_FLAG_Q_DEP_MASK);
796 uc_priv->flags |= ata_id_queue_depth(id);
797
798 /* Get the xfer mode from device */
799 dwc_ahsata_xfer_mode(uc_priv, id);
800
801 /* Get the write cache status from device */
802 dwc_ahsata_init_wcache(uc_priv, id);
803
804 /* Set the xfer mode to highest speed */
805 ahci_set_feature(uc_priv, port);
806
807 dwc_ahsata_print_info(pdev);
808
809 return 0;
810}
811
812/*
813 * SATA interface between low level driver and command layer
814 */
815static ulong sata_read_common(struct ahci_uc_priv *uc_priv,
816 struct blk_desc *desc, ulong blknr,
817 lbaint_t blkcnt, void *buffer)
818{
819 u32 rc;
820
821 if (desc->lba48)
822 rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt, buffer,
823 READ_CMD);
824 else
825 rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt, buffer,
826 READ_CMD);
827
828 return rc;
829}
830
831static ulong sata_write_common(struct ahci_uc_priv *uc_priv,
832 struct blk_desc *desc, ulong blknr,
833 lbaint_t blkcnt, const void *buffer)
834{
835 u32 rc;
836 u32 flags = uc_priv->flags;
837
838 if (desc->lba48) {
839 rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt, buffer,
840 WRITE_CMD);
841 if ((flags & SATA_FLAG_WCACHE) && (flags & SATA_FLAG_FLUSH_EXT))
842 dwc_ahsata_flush_cache_ext(uc_priv);
843 } else {
844 rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt, buffer,
845 WRITE_CMD);
846 if ((flags & SATA_FLAG_WCACHE) && (flags & SATA_FLAG_FLUSH))
847 dwc_ahsata_flush_cache(uc_priv);
848 }
849
850 return rc;
851}
852
Simon Glass0067b872017-07-29 11:35:16 -0600853int dwc_ahsata_port_status(struct udevice *dev, int port)
854{
855 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
856 struct sata_port_regs *port_mmio;
857
858 port_mmio = uc_priv->port[port].port_mmio;
859 return readl(&port_mmio->ssts) & SATA_PORT_SSTS_DET_MASK ? 0 : -ENXIO;
860}
861
862int dwc_ahsata_bus_reset(struct udevice *dev)
863{
864 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
865 struct sata_host_regs *host_mmio = uc_priv->mmio_base;
866
867 setbits_le32(&host_mmio->ghc, SATA_HOST_GHC_HR);
868 while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
869 udelay(100);
870
871 return 0;
872}
873
874int dwc_ahsata_scan(struct udevice *dev)
875{
876 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
877 struct blk_desc *desc;
878 struct udevice *blk;
879 int ret;
880
881 /*
882 * Create only one block device and do detection
883 * to make sure that there won't be a lot of
884 * block devices created
885 */
886 device_find_first_child(dev, &blk);
887 if (!blk) {
888 ret = blk_create_devicef(dev, "dwc_ahsata_blk", "blk",
Bin Meng2294ecb2023-09-26 16:43:31 +0800889 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
890 0, &blk);
Simon Glass0067b872017-07-29 11:35:16 -0600891 if (ret) {
892 debug("Can't create device\n");
893 return ret;
894 }
895 }
896
Simon Glass71fa5b42020-12-03 16:55:18 -0700897 desc = dev_get_uclass_plat(blk);
Simon Glass0067b872017-07-29 11:35:16 -0600898 ret = dwc_ahsata_scan_common(uc_priv, desc);
899 if (ret) {
900 debug("%s: Failed to scan bus\n", __func__);
901 return ret;
902 }
903
AKASHI Takahiro927a7a52022-03-08 20:36:43 +0900904 ret = blk_probe_or_unbind(dev);
905 if (ret < 0)
906 /* TODO: undo create */
Heinrich Schuchardt41b54472024-08-08 09:08:03 +0200907 return log_msg_ret("pro", ret);
908
909 ret = bootdev_setup_for_sibling_blk(blk, "sata_bootdev");
910 if (ret)
911 return log_msg_ret("bd", ret);
AKASHI Takahiro927a7a52022-03-08 20:36:43 +0900912
Simon Glass0067b872017-07-29 11:35:16 -0600913 return 0;
914}
915
916int dwc_ahsata_probe(struct udevice *dev)
917{
918 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Jiaxun Yang0495a052024-05-17 19:14:53 +0100919 struct clk_bulk clk_bulk __maybe_unused;
920 struct clk clk __maybe_unused;
921 int sataclk;
Simon Glass0067b872017-07-29 11:35:16 -0600922 int ret;
923
Jiaxun Yang0495a052024-05-17 19:14:53 +0100924#if IS_ENABLED(CONFIG_MX6)
Soeren Moch5569bbd2019-03-01 13:10:59 +0100925 setup_sata();
926#endif
Jiaxun Yang0495a052024-05-17 19:14:53 +0100927#if IS_ENABLED(CONFIG_MX5) || IS_ENABLED(CONFIG_MX6)
928 sataclk = mxc_get_clock(MXC_SATA_CLK);
929#else
930 ret = clk_get_bulk(dev, &clk_bulk);
931 if (ret)
932 return ret;
933
934 ret = clk_enable_bulk(&clk_bulk);
935 if (ret)
936 return ret;
937
938 ret = clk_get_by_name(dev, "sata", &clk);
939 if (ret)
940 return ret;
941
942 sataclk = clk_get_rate(&clk);
943#endif
944 if (IS_ERR_VALUE(sataclk)) {
945 log_err("Unable to get SATA clock rate\n");
946 return -EINVAL;
947 }
Simon Glass0067b872017-07-29 11:35:16 -0600948 uc_priv->host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
949 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NO_ATAPI;
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100950 uc_priv->mmio_base = dev_read_addr_ptr(dev);
Simon Glass0067b872017-07-29 11:35:16 -0600951
952 /* initialize adapter */
Jiaxun Yang0495a052024-05-17 19:14:53 +0100953 ret = ahci_host_init(uc_priv, sataclk);
Simon Glass0067b872017-07-29 11:35:16 -0600954 if (ret)
955 return ret;
956
957 ahci_print_info(uc_priv);
958
959 return dwc_ahci_start_ports(uc_priv);
960}
961
962static ulong dwc_ahsata_read(struct udevice *blk, lbaint_t blknr,
963 lbaint_t blkcnt, void *buffer)
964{
Simon Glass71fa5b42020-12-03 16:55:18 -0700965 struct blk_desc *desc = dev_get_uclass_plat(blk);
Simon Glass0067b872017-07-29 11:35:16 -0600966 struct udevice *dev = dev_get_parent(blk);
967 struct ahci_uc_priv *uc_priv;
968
969 uc_priv = dev_get_uclass_priv(dev);
970 return sata_read_common(uc_priv, desc, blknr, blkcnt, buffer);
971}
972
973static ulong dwc_ahsata_write(struct udevice *blk, lbaint_t blknr,
974 lbaint_t blkcnt, const void *buffer)
975{
Simon Glass71fa5b42020-12-03 16:55:18 -0700976 struct blk_desc *desc = dev_get_uclass_plat(blk);
Simon Glass0067b872017-07-29 11:35:16 -0600977 struct udevice *dev = dev_get_parent(blk);
978 struct ahci_uc_priv *uc_priv;
979
980 uc_priv = dev_get_uclass_priv(dev);
981 return sata_write_common(uc_priv, desc, blknr, blkcnt, buffer);
982}
983
984static const struct blk_ops dwc_ahsata_blk_ops = {
985 .read = dwc_ahsata_read,
986 .write = dwc_ahsata_write,
987};
988
989U_BOOT_DRIVER(dwc_ahsata_blk) = {
990 .name = "dwc_ahsata_blk",
991 .id = UCLASS_BLK,
992 .ops = &dwc_ahsata_blk_ops,
993};
994
Soeren Moch5569bbd2019-03-01 13:10:59 +0100995struct ahci_ops dwc_ahsata_ahci_ops = {
996 .port_status = dwc_ahsata_port_status,
997 .reset = dwc_ahsata_bus_reset,
998 .scan = dwc_ahsata_scan,
999};
1000
1001static const struct udevice_id dwc_ahsata_ahci_ids[] = {
Jiaxun Yang0495a052024-05-17 19:14:53 +01001002 { .compatible = "fsl,imx53-ahci" },
Soeren Moch5569bbd2019-03-01 13:10:59 +01001003 { .compatible = "fsl,imx6q-ahci" },
Jiaxun Yang0495a052024-05-17 19:14:53 +01001004 { .compatible = "fsl,imx6qp-ahci" },
Soeren Moch5569bbd2019-03-01 13:10:59 +01001005 { }
1006};
1007
1008U_BOOT_DRIVER(dwc_ahsata_ahci) = {
1009 .name = "dwc_ahsata_ahci",
1010 .id = UCLASS_AHCI,
1011 .of_match = dwc_ahsata_ahci_ids,
1012 .ops = &dwc_ahsata_ahci_ops,
1013 .probe = dwc_ahsata_probe,
1014};