blob: 8a030a1d1166a86b890fbe745c94b45b5ce6adc5 [file] [log] [blame]
Stefano Babic771bfd12012-02-22 00:24:39 +00001/*
2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 * Terry Lv <r65388@freescale.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefano Babic771bfd12012-02-22 00:24:39 +00006 */
7
8#include <libata.h>
9#include <ahci.h>
10#include <fis.h>
Pavel Herrmann9e9f6282012-09-27 23:18:04 +000011#include <sata.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000012
13#include <common.h>
14#include <malloc.h>
15#include <linux/ctype.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000017#include <asm/io.h>
18#include <linux/bitops.h>
19#include <asm/arch/clock.h>
Tim Harveye9d13472014-05-07 22:23:35 -070020#include <asm/arch/sys_proto.h>
Stefano Babic771bfd12012-02-22 00:24:39 +000021#include "dwc_ahsata.h"
22
23struct sata_port_regs {
24 u32 clb;
25 u32 clbu;
26 u32 fb;
27 u32 fbu;
28 u32 is;
29 u32 ie;
30 u32 cmd;
31 u32 res1[1];
32 u32 tfd;
33 u32 sig;
34 u32 ssts;
35 u32 sctl;
36 u32 serr;
37 u32 sact;
38 u32 ci;
39 u32 sntf;
40 u32 res2[1];
41 u32 dmacr;
42 u32 res3[1];
43 u32 phycr;
44 u32 physr;
45};
46
47struct sata_host_regs {
48 u32 cap;
49 u32 ghc;
50 u32 is;
51 u32 pi;
52 u32 vs;
53 u32 ccc_ctl;
54 u32 ccc_ports;
55 u32 res1[2];
56 u32 cap2;
57 u32 res2[30];
58 u32 bistafr;
59 u32 bistcr;
60 u32 bistfctr;
61 u32 bistsr;
62 u32 bistdecr;
63 u32 res3[2];
64 u32 oobr;
65 u32 res4[8];
66 u32 timer1ms;
67 u32 res5[1];
68 u32 gparam1r;
69 u32 gparam2r;
70 u32 pparamr;
71 u32 testr;
72 u32 versionr;
73 u32 idr;
74};
75
76#define MAX_DATA_BYTES_PER_SG (4 * 1024 * 1024)
77#define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
78
79#define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
80
81static int is_ready;
82
Tang Yuantian3f262d02015-07-09 14:37:30 +080083static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
Stefano Babic771bfd12012-02-22 00:24:39 +000084{
85 return base + 0x100 + (port * 0x80);
86}
87
88static int waiting_for_cmd_completed(u8 *offset,
89 int timeout_msec,
90 u32 sign)
91{
92 int i;
93 u32 status;
94
95 for (i = 0;
96 ((status = readl(offset)) & sign) && i < timeout_msec;
97 ++i)
98 mdelay(1);
99
100 return (i < timeout_msec) ? 0 : -1;
101}
102
Simon Glass5ce59672017-06-14 21:28:32 -0600103static int ahci_setup_oobr(struct ahci_uc_priv *probe_ent,
Stefano Babic771bfd12012-02-22 00:24:39 +0000104 int clk)
105{
106 struct sata_host_regs *host_mmio =
107 (struct sata_host_regs *)probe_ent->mmio_base;
108
109 writel(SATA_HOST_OOBR_WE, &(host_mmio->oobr));
110 writel(0x02060b14, &(host_mmio->oobr));
111
112 return 0;
113}
114
Simon Glass5ce59672017-06-14 21:28:32 -0600115static int ahci_host_init(struct ahci_uc_priv *probe_ent)
Stefano Babic771bfd12012-02-22 00:24:39 +0000116{
117 u32 tmp, cap_save, num_ports;
118 int i, j, timeout = 1000;
119 struct sata_port_regs *port_mmio = NULL;
120 struct sata_host_regs *host_mmio =
121 (struct sata_host_regs *)probe_ent->mmio_base;
122 int clk = mxc_get_clock(MXC_SATA_CLK);
123
124 cap_save = readl(&(host_mmio->cap));
125 cap_save |= SATA_HOST_CAP_SSS;
126
127 /* global controller reset */
128 tmp = readl(&(host_mmio->ghc));
129 if ((tmp & SATA_HOST_GHC_HR) == 0)
130 writel_with_flush(tmp | SATA_HOST_GHC_HR, &(host_mmio->ghc));
131
132 while ((readl(&(host_mmio->ghc)) & SATA_HOST_GHC_HR)
133 && --timeout)
134 ;
135
136 if (timeout <= 0) {
137 debug("controller reset failed (0x%x)\n", tmp);
138 return -1;
139 }
140
141 /* Set timer 1ms */
142 writel(clk / 1000, &(host_mmio->timer1ms));
143
144 ahci_setup_oobr(probe_ent, 0);
145
146 writel_with_flush(SATA_HOST_GHC_AE, &(host_mmio->ghc));
147 writel(cap_save, &(host_mmio->cap));
148 num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
149 writel_with_flush((1 << num_ports) - 1,
150 &(host_mmio->pi));
151
152 /*
153 * Determine which Ports are implemented by the DWC_ahsata,
154 * by reading the PI register. This bit map value aids the
155 * software to determine how many Ports are available and
156 * which Port registers need to be initialized.
157 */
158 probe_ent->cap = readl(&(host_mmio->cap));
159 probe_ent->port_map = readl(&(host_mmio->pi));
160
161 /* Determine how many command slots the HBA supports */
162 probe_ent->n_ports =
163 (probe_ent->cap & SATA_HOST_CAP_NP_MASK) + 1;
164
165 debug("cap 0x%x port_map 0x%x n_ports %d\n",
166 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
167
168 for (i = 0; i < probe_ent->n_ports; i++) {
169 probe_ent->port[i].port_mmio =
Tang Yuantian3f262d02015-07-09 14:37:30 +0800170 ahci_port_base(host_mmio, i);
Stefano Babic771bfd12012-02-22 00:24:39 +0000171 port_mmio =
172 (struct sata_port_regs *)probe_ent->port[i].port_mmio;
173
174 /* Ensure that the DWC_ahsata is in idle state */
175 tmp = readl(&(port_mmio->cmd));
176
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;
190 writel_with_flush(tmp, &(port_mmio->cmd));
191
192 /*
193 * spec says 500 msecs for each bit, so
194 * this is slightly incorrect.
195 */
196 mdelay(500);
197
198 timeout = 1000;
199 while ((readl(&(port_mmio->cmd)) & SATA_PORT_CMD_CR)
200 && --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 */
210 tmp = readl(&(port_mmio->cmd));
211 writel((tmp | SATA_PORT_CMD_SUD), &(port_mmio->cmd));
212
213 /* Wait for spin-up to finish */
214 timeout = 1000;
215 while (!(readl(&(port_mmio->cmd)) | SATA_PORT_CMD_SUD)
216 && --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);
225 tmp = readl(&(port_mmio->ssts));
226 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;
233 while (!(readl(&(port_mmio->serr)) | SATA_PORT_SERR_DIAG_X)
234 && --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 */
246 tmp = readl(&(port_mmio->serr));
247 debug("P#SERR 0x%x\n",
248 tmp);
249 writel(tmp, &(port_mmio->serr));
250
251 /* Ack any pending irq events for this port */
252 tmp = readl(&(host_mmio->is));
253 debug("IS 0x%x\n", tmp);
254 if (tmp)
255 writel(tmp, &(host_mmio->is));
256
257 writel(1 << i, &(host_mmio->is));
258
259 /* set irq mask (enables interrupts) */
260 writel(DEF_PORT_IRQ, &(port_mmio->ie));
261
262 /* register linkup ports */
263 tmp = readl(&(port_mmio->ssts));
264 debug("Port %d status: 0x%x\n", i, tmp);
265 if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
266 probe_ent->link_port_map |= (0x01 << i);
267 }
268
269 tmp = readl(&(host_mmio->ghc));
270 debug("GHC 0x%x\n", tmp);
271 writel(tmp | SATA_HOST_GHC_IE, &(host_mmio->ghc));
272 tmp = readl(&(host_mmio->ghc));
273 debug("GHC 0x%x\n", tmp);
274
275 return 0;
276}
277
Simon Glass5ce59672017-06-14 21:28:32 -0600278static void ahci_print_info(struct ahci_uc_priv *probe_ent)
Stefano Babic771bfd12012-02-22 00:24:39 +0000279{
280 struct sata_host_regs *host_mmio =
281 (struct sata_host_regs *)probe_ent->mmio_base;
282 u32 vers, cap, impl, speed;
283 const char *speed_s;
284 const char *scc_s;
285
286 vers = readl(&(host_mmio->vs));
287 cap = probe_ent->cap;
288 impl = probe_ent->port_map;
289
290 speed = (cap & SATA_HOST_CAP_ISS_MASK)
291 >> SATA_HOST_CAP_ISS_OFFSET;
292 if (speed == 1)
293 speed_s = "1.5";
294 else if (speed == 2)
295 speed_s = "3";
296 else
297 speed_s = "?";
298
299 scc_s = "SATA";
300
301 printf("AHCI %02x%02x.%02x%02x "
302 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
303 (vers >> 24) & 0xff,
304 (vers >> 16) & 0xff,
305 (vers >> 8) & 0xff,
306 vers & 0xff,
307 ((cap >> 8) & 0x1f) + 1,
308 (cap & 0x1f) + 1,
309 speed_s,
310 impl,
311 scc_s);
312
313 printf("flags: "
314 "%s%s%s%s%s%s"
315 "%s%s%s%s%s%s%s\n",
316 cap & (1 << 31) ? "64bit " : "",
317 cap & (1 << 30) ? "ncq " : "",
318 cap & (1 << 28) ? "ilck " : "",
319 cap & (1 << 27) ? "stag " : "",
320 cap & (1 << 26) ? "pm " : "",
321 cap & (1 << 25) ? "led " : "",
322 cap & (1 << 24) ? "clo " : "",
323 cap & (1 << 19) ? "nz " : "",
324 cap & (1 << 18) ? "only " : "",
325 cap & (1 << 17) ? "pmp " : "",
326 cap & (1 << 15) ? "pio " : "",
327 cap & (1 << 14) ? "slum " : "",
328 cap & (1 << 13) ? "part " : "");
329}
330
331static int ahci_init_one(int pdev)
332{
333 int rc;
Simon Glass5ce59672017-06-14 21:28:32 -0600334 struct ahci_uc_priv *probe_ent = NULL;
Stefano Babic771bfd12012-02-22 00:24:39 +0000335
Simon Glass5ce59672017-06-14 21:28:32 -0600336 probe_ent = malloc(sizeof(struct ahci_uc_priv));
337 memset(probe_ent, 0, sizeof(struct ahci_uc_priv));
Stefano Babic771bfd12012-02-22 00:24:39 +0000338 probe_ent->dev = pdev;
339
340 probe_ent->host_flags = ATA_FLAG_SATA
341 | ATA_FLAG_NO_LEGACY
342 | ATA_FLAG_MMIO
343 | ATA_FLAG_PIO_DMA
344 | ATA_FLAG_NO_ATAPI;
345
Scott Wood16519a32015-04-17 09:19:01 -0500346 probe_ent->mmio_base = (void __iomem *)CONFIG_DWC_AHSATA_BASE_ADDR;
Stefano Babic771bfd12012-02-22 00:24:39 +0000347
348 /* initialize adapter */
349 rc = ahci_host_init(probe_ent);
350 if (rc)
351 goto err_out;
352
353 ahci_print_info(probe_ent);
354
355 /* Save the private struct to block device struct */
356 sata_dev_desc[pdev].priv = (void *)probe_ent;
357
358 return 0;
359
360err_out:
361 return rc;
362}
363
Simon Glass5ce59672017-06-14 21:28:32 -0600364static int ahci_fill_sg(struct ahci_uc_priv *probe_ent,
Stefano Babic771bfd12012-02-22 00:24:39 +0000365 u8 port, unsigned char *buf, int buf_len)
366{
367 struct ahci_ioports *pp = &(probe_ent->port[port]);
368 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
369 u32 sg_count, max_bytes;
370 int i;
371
372 max_bytes = MAX_DATA_BYTES_PER_SG;
373 sg_count = ((buf_len - 1) / max_bytes) + 1;
374 if (sg_count > AHCI_MAX_SG) {
375 printf("Error:Too much sg!\n");
376 return -1;
377 }
378
379 for (i = 0; i < sg_count; i++) {
380 ahci_sg->addr =
381 cpu_to_le32((u32)buf + i * max_bytes);
382 ahci_sg->addr_hi = 0;
383 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
384 (buf_len < max_bytes
385 ? (buf_len - 1)
386 : (max_bytes - 1)));
387 ahci_sg++;
388 buf_len -= max_bytes;
389 }
390
391 return sg_count;
392}
393
394static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
395{
396 struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
397 AHCI_CMD_SLOT_SZ * cmd_slot);
398
399 memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
400 cmd_hdr->opts = cpu_to_le32(opts);
401 cmd_hdr->status = 0;
Tang Yuantian3f262d02015-07-09 14:37:30 +0800402 pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
403#ifdef CONFIG_PHYS_64BIT
404 pp->cmd_slot->tbl_addr_hi =
405 cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
406#endif
Stefano Babic771bfd12012-02-22 00:24:39 +0000407}
408
409#define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
410
Simon Glass5ce59672017-06-14 21:28:32 -0600411static int ahci_exec_ata_cmd(struct ahci_uc_priv *probe_ent,
Stefano Babic771bfd12012-02-22 00:24:39 +0000412 u8 port, struct sata_fis_h2d *cfis,
413 u8 *buf, u32 buf_len, s32 is_write)
414{
415 struct ahci_ioports *pp = &(probe_ent->port[port]);
416 struct sata_port_regs *port_mmio =
417 (struct sata_port_regs *)pp->port_mmio;
418 u32 opts;
419 int sg_count = 0, cmd_slot = 0;
420
421 cmd_slot = AHCI_GET_CMD_SLOT(readl(&(port_mmio->ci)));
422 if (32 == cmd_slot) {
423 printf("Can't find empty command slot!\n");
424 return 0;
425 }
426
427 /* Check xfer length */
428 if (buf_len > MAX_BYTES_PER_TRANS) {
429 printf("Max transfer length is %dB\n\r",
430 MAX_BYTES_PER_TRANS);
431 return 0;
432 }
433
434 memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
435 if (buf && buf_len)
436 sg_count = ahci_fill_sg(probe_ent, port, buf, buf_len);
437 opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
Eric Nelson998816b2013-06-15 16:09:55 -0700438 if (is_write) {
Stefano Babic771bfd12012-02-22 00:24:39 +0000439 opts |= 0x40;
Eric Nelson998816b2013-06-15 16:09:55 -0700440 flush_cache((ulong)buf, buf_len);
441 }
Stefano Babic771bfd12012-02-22 00:24:39 +0000442 ahci_fill_cmd_slot(pp, cmd_slot, opts);
443
Eric Nelson998816b2013-06-15 16:09:55 -0700444 flush_cache((int)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
Stefano Babic771bfd12012-02-22 00:24:39 +0000445 writel_with_flush(1 << cmd_slot, &(port_mmio->ci));
446
447 if (waiting_for_cmd_completed((u8 *)&(port_mmio->ci),
448 10000, 0x1 << cmd_slot)) {
449 printf("timeout exit!\n");
450 return -1;
451 }
Eric Nelson998816b2013-06-15 16:09:55 -0700452 invalidate_dcache_range((int)(pp->cmd_slot),
453 (int)(pp->cmd_slot)+AHCI_PORT_PRIV_DMA_SZ);
Stefano Babic771bfd12012-02-22 00:24:39 +0000454 debug("ahci_exec_ata_cmd: %d byte transferred.\n",
455 pp->cmd_slot->status);
Eric Nelson998816b2013-06-15 16:09:55 -0700456 if (!is_write)
457 invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
Stefano Babic771bfd12012-02-22 00:24:39 +0000458
459 return buf_len;
460}
461
462static void ahci_set_feature(u8 dev, u8 port)
463{
Simon Glass5ce59672017-06-14 21:28:32 -0600464 struct ahci_uc_priv *probe_ent =
465 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700466 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
467 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000468
469 memset(cfis, 0, sizeof(struct sata_fis_h2d));
470 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
471 cfis->pm_port_c = 1 << 7;
472 cfis->command = ATA_CMD_SET_FEATURES;
473 cfis->features = SETFEATURES_XFER;
474 cfis->sector_count = ffs(probe_ent->udma_mask + 1) + 0x3e;
475
476 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, READ_CMD);
477}
478
Simon Glass5ce59672017-06-14 21:28:32 -0600479static int ahci_port_start(struct ahci_uc_priv *probe_ent,
Stefano Babic771bfd12012-02-22 00:24:39 +0000480 u8 port)
481{
482 struct ahci_ioports *pp = &(probe_ent->port[port]);
483 struct sata_port_regs *port_mmio =
484 (struct sata_port_regs *)pp->port_mmio;
485 u32 port_status;
486 u32 mem;
487 int timeout = 10000000;
488
489 debug("Enter start port: %d\n", port);
490 port_status = readl(&(port_mmio->ssts));
491 debug("Port %d status: %x\n", port, port_status);
492 if ((port_status & 0xf) != 0x03) {
493 printf("No Link on this port!\n");
494 return -1;
495 }
496
497 mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
498 if (!mem) {
499 free(pp);
500 printf("No mem for table!\n");
501 return -ENOMEM;
502 }
503
504 mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
505 memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
506
507 /*
508 * First item in chunk of DMA memory: 32-slot command table,
509 * 32 bytes each in size
510 */
511 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
512 debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
513 mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
514
515 /*
516 * Second item: Received-FIS area, 256-Byte aligned
517 */
518 pp->rx_fis = mem;
519 mem += AHCI_RX_FIS_SZ;
520
521 /*
522 * Third item: data area for storing a single command
523 * and its scatter-gather table
524 */
525 pp->cmd_tbl = mem;
Tang Yuantian3f262d02015-07-09 14:37:30 +0800526 debug("cmd_tbl_dma = 0x%lx\n", pp->cmd_tbl);
Stefano Babic771bfd12012-02-22 00:24:39 +0000527
528 mem += AHCI_CMD_TBL_HDR;
529
530 writel_with_flush(0x00004444, &(port_mmio->dmacr));
531 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
532 writel_with_flush((u32)pp->cmd_slot, &(port_mmio->clb));
533 writel_with_flush(pp->rx_fis, &(port_mmio->fb));
534
535 /* Enable FRE */
536 writel_with_flush((SATA_PORT_CMD_FRE | readl(&(port_mmio->cmd))),
537 &(port_mmio->cmd));
538
539 /* Wait device ready */
540 while ((readl(&(port_mmio->tfd)) & (SATA_PORT_TFD_STS_ERR |
541 SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
542 && --timeout)
543 ;
544 if (timeout <= 0) {
545 debug("Device not ready for BSY, DRQ and"
546 "ERR in TFD!\n");
547 return -1;
548 }
549
550 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
551 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
552 PORT_CMD_START, &(port_mmio->cmd));
553
554 debug("Exit start port %d\n", port);
555
556 return 0;
557}
558
Stefano Babic771bfd12012-02-22 00:24:39 +0000559static void dwc_ahsata_print_info(int dev)
560{
Simon Glasse3394752016-02-29 15:25:34 -0700561 struct blk_desc *pdev = &(sata_dev_desc[dev]);
Stefano Babic771bfd12012-02-22 00:24:39 +0000562
563 printf("SATA Device Info:\n\r");
564#ifdef CONFIG_SYS_64BIT_LBA
565 printf("S/N: %s\n\rProduct model number: %s\n\r"
566 "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
567 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
568#else
569 printf("S/N: %s\n\rProduct model number: %s\n\r"
570 "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
571 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
572#endif
573}
574
575static void dwc_ahsata_identify(int dev, u16 *id)
576{
Simon Glass5ce59672017-06-14 21:28:32 -0600577 struct ahci_uc_priv *probe_ent =
578 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700579 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
580 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000581 u8 port = probe_ent->hard_port_no;
582
583 memset(cfis, 0, sizeof(struct sata_fis_h2d));
584
585 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
586 cfis->pm_port_c = 0x80; /* is command */
587 cfis->command = ATA_CMD_ID_ATA;
588
589 ahci_exec_ata_cmd(probe_ent, port, cfis,
590 (u8 *)id, ATA_ID_WORDS * 2, READ_CMD);
591 ata_swap_buf_le16(id, ATA_ID_WORDS);
592}
593
594static void dwc_ahsata_xfer_mode(int dev, u16 *id)
595{
Simon Glass5ce59672017-06-14 21:28:32 -0600596 struct ahci_uc_priv *probe_ent =
597 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Stefano Babic771bfd12012-02-22 00:24:39 +0000598
599 probe_ent->pio_mask = id[ATA_ID_PIO_MODES];
600 probe_ent->udma_mask = id[ATA_ID_UDMA_MODES];
601 debug("pio %04x, udma %04x\n\r",
602 probe_ent->pio_mask, probe_ent->udma_mask);
603}
604
605static u32 dwc_ahsata_rw_cmd(int dev, u32 start, u32 blkcnt,
606 u8 *buffer, int is_write)
607{
Simon Glass5ce59672017-06-14 21:28:32 -0600608 struct ahci_uc_priv *probe_ent =
609 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700610 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
611 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000612 u8 port = probe_ent->hard_port_no;
613 u32 block;
614
615 block = start;
616
617 memset(cfis, 0, sizeof(struct sata_fis_h2d));
618
619 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
620 cfis->pm_port_c = 0x80; /* is command */
621 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
622 cfis->device = ATA_LBA;
623
624 cfis->device |= (block >> 24) & 0xf;
625 cfis->lba_high = (block >> 16) & 0xff;
626 cfis->lba_mid = (block >> 8) & 0xff;
627 cfis->lba_low = block & 0xff;
628 cfis->sector_count = (u8)(blkcnt & 0xff);
629
630 if (ahci_exec_ata_cmd(probe_ent, port, cfis,
631 buffer, ATA_SECT_SIZE * blkcnt, is_write) > 0)
632 return blkcnt;
633 else
634 return 0;
635}
636
Simon Glass89dc64f2017-07-29 11:35:01 -0600637static void dwc_ahsata_flush_cache(int dev)
Stefano Babic771bfd12012-02-22 00:24:39 +0000638{
Simon Glass5ce59672017-06-14 21:28:32 -0600639 struct ahci_uc_priv *probe_ent =
640 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700641 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
642 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000643 u8 port = probe_ent->hard_port_no;
644
645 memset(cfis, 0, sizeof(struct sata_fis_h2d));
646
647 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
648 cfis->pm_port_c = 0x80; /* is command */
649 cfis->command = ATA_CMD_FLUSH;
650
651 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
652}
653
654static u32 dwc_ahsata_rw_cmd_ext(int dev, u32 start, lbaint_t blkcnt,
655 u8 *buffer, int is_write)
656{
Simon Glass5ce59672017-06-14 21:28:32 -0600657 struct ahci_uc_priv *probe_ent =
658 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700659 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
660 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000661 u8 port = probe_ent->hard_port_no;
662 u64 block;
663
664 block = (u64)start;
665
666 memset(cfis, 0, sizeof(struct sata_fis_h2d));
667
668 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
669 cfis->pm_port_c = 0x80; /* is command */
670
671 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
672 : ATA_CMD_READ_EXT;
673
674 cfis->lba_high_exp = (block >> 40) & 0xff;
675 cfis->lba_mid_exp = (block >> 32) & 0xff;
676 cfis->lba_low_exp = (block >> 24) & 0xff;
677 cfis->lba_high = (block >> 16) & 0xff;
678 cfis->lba_mid = (block >> 8) & 0xff;
679 cfis->lba_low = block & 0xff;
680 cfis->device = ATA_LBA;
681 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
682 cfis->sector_count = blkcnt & 0xff;
683
684 if (ahci_exec_ata_cmd(probe_ent, port, cfis, buffer,
685 ATA_SECT_SIZE * blkcnt, is_write) > 0)
686 return blkcnt;
687 else
688 return 0;
689}
690
Simon Glass89dc64f2017-07-29 11:35:01 -0600691static void dwc_ahsata_flush_cache_ext(int dev)
Stefano Babic771bfd12012-02-22 00:24:39 +0000692{
Simon Glass5ce59672017-06-14 21:28:32 -0600693 struct ahci_uc_priv *probe_ent =
694 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Eric Nelson998816b2013-06-15 16:09:55 -0700695 struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
696 struct sata_fis_h2d *cfis = &h2d;
Stefano Babic771bfd12012-02-22 00:24:39 +0000697 u8 port = probe_ent->hard_port_no;
698
699 memset(cfis, 0, sizeof(struct sata_fis_h2d));
700
701 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
702 cfis->pm_port_c = 0x80; /* is command */
703 cfis->command = ATA_CMD_FLUSH_EXT;
704
705 ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
706}
707
708static void dwc_ahsata_init_wcache(int dev, u16 *id)
709{
Simon Glass5ce59672017-06-14 21:28:32 -0600710 struct ahci_uc_priv *probe_ent =
711 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Stefano Babic771bfd12012-02-22 00:24:39 +0000712
713 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
714 probe_ent->flags |= SATA_FLAG_WCACHE;
715 if (ata_id_has_flush(id))
716 probe_ent->flags |= SATA_FLAG_FLUSH;
717 if (ata_id_has_flush_ext(id))
718 probe_ent->flags |= SATA_FLAG_FLUSH_EXT;
719}
720
Simon Glass89dc64f2017-07-29 11:35:01 -0600721static u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
722 const void *buffer, int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000723{
724 u32 start, blks;
725 u8 *addr;
726 int max_blks;
727
728 start = blknr;
729 blks = blkcnt;
730 addr = (u8 *)buffer;
731
732 max_blks = ATA_MAX_SECTORS_LBA48;
733
734 do {
735 if (blks > max_blks) {
736 if (max_blks != dwc_ahsata_rw_cmd_ext(dev, start,
737 max_blks, addr, is_write))
738 return 0;
739 start += max_blks;
740 blks -= max_blks;
741 addr += ATA_SECT_SIZE * max_blks;
742 } else {
743 if (blks != dwc_ahsata_rw_cmd_ext(dev, start,
744 blks, addr, is_write))
745 return 0;
746 start += blks;
747 blks = 0;
748 addr += ATA_SECT_SIZE * blks;
749 }
750 } while (blks != 0);
751
752 return blkcnt;
753}
754
Simon Glass89dc64f2017-07-29 11:35:01 -0600755static u32 ata_low_level_rw_lba28(int dev, u32 blknr, lbaint_t blkcnt,
756 const void *buffer, int is_write)
Stefano Babic771bfd12012-02-22 00:24:39 +0000757{
758 u32 start, blks;
759 u8 *addr;
760 int max_blks;
761
762 start = blknr;
763 blks = blkcnt;
764 addr = (u8 *)buffer;
765
766 max_blks = ATA_MAX_SECTORS;
767 do {
768 if (blks > max_blks) {
769 if (max_blks != dwc_ahsata_rw_cmd(dev, start,
770 max_blks, addr, is_write))
771 return 0;
772 start += max_blks;
773 blks -= max_blks;
774 addr += ATA_SECT_SIZE * max_blks;
775 } else {
776 if (blks != dwc_ahsata_rw_cmd(dev, start,
777 blks, addr, is_write))
778 return 0;
779 start += blks;
780 blks = 0;
781 addr += ATA_SECT_SIZE * blks;
782 }
783 } while (blks != 0);
784
785 return blkcnt;
786}
787
Simon Glassed82fcc2017-07-29 11:35:03 -0600788int init_sata(int dev)
789{
790 int i;
791 u32 linkmap;
792 struct ahci_uc_priv *probe_ent = NULL;
793
794#if defined(CONFIG_MX6)
795 if (!is_mx6dq() && !is_mx6dqp())
796 return 1;
797#endif
798 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
799 printf("The sata index %d is out of ranges\n\r", dev);
800 return -1;
801 }
802
803 ahci_init_one(dev);
804
805 probe_ent = (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
806 linkmap = probe_ent->link_port_map;
807
808 if (0 == linkmap) {
809 printf("No port device detected!\n");
810 return 1;
811 }
812
813 for (i = 0; i < probe_ent->n_ports; i++) {
814 if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
815 if (ahci_port_start(probe_ent, (u8)i)) {
816 printf("Can not start port %d\n", i);
817 return 1;
818 }
819 probe_ent->hard_port_no = i;
820 break;
821 }
822 }
823
824 return 0;
825}
826
827int reset_sata(int dev)
828{
829 struct ahci_uc_priv *probe_ent;
830 struct sata_host_regs *host_mmio;
831
832 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
833 printf("The sata index %d is out of ranges\n\r", dev);
834 return -1;
835 }
836
837 probe_ent = (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
838 if (NULL == probe_ent)
839 /* not initialized, so nothing to reset */
840 return 0;
841
842 host_mmio = (struct sata_host_regs *)probe_ent->mmio_base;
843 setbits_le32(&host_mmio->ghc, SATA_HOST_GHC_HR);
844 while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
845 udelay(100);
846
847 return 0;
848}
849
Nikita Kiryanov66914042014-08-20 15:08:53 +0300850int sata_port_status(int dev, int port)
851{
852 struct sata_port_regs *port_mmio;
Simon Glass5ce59672017-06-14 21:28:32 -0600853 struct ahci_uc_priv *probe_ent = NULL;
Nikita Kiryanov66914042014-08-20 15:08:53 +0300854
855 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1))
856 return -EINVAL;
857
858 if (sata_dev_desc[dev].priv == NULL)
859 return -ENODEV;
860
Simon Glass5ce59672017-06-14 21:28:32 -0600861 probe_ent = (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Nikita Kiryanov66914042014-08-20 15:08:53 +0300862 port_mmio = (struct sata_port_regs *)probe_ent->port[port].port_mmio;
863
Nikita Kiryanov7653a732014-10-28 14:59:29 +0200864 return readl(&(port_mmio->ssts)) & SATA_PORT_SSTS_DET_MASK;
Nikita Kiryanov66914042014-08-20 15:08:53 +0300865}
866
Stefano Babic771bfd12012-02-22 00:24:39 +0000867/*
868 * SATA interface between low level driver and command layer
869 */
Tom Rini532e8672012-09-29 07:53:06 -0700870ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
Stefano Babic771bfd12012-02-22 00:24:39 +0000871{
872 u32 rc;
873
874 if (sata_dev_desc[dev].lba48)
875 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
876 buffer, READ_CMD);
877 else
878 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
879 buffer, READ_CMD);
880 return rc;
881}
882
Tom Rini532e8672012-09-29 07:53:06 -0700883ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Stefano Babic771bfd12012-02-22 00:24:39 +0000884{
885 u32 rc;
Simon Glass5ce59672017-06-14 21:28:32 -0600886 struct ahci_uc_priv *probe_ent =
887 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Stefano Babic771bfd12012-02-22 00:24:39 +0000888 u32 flags = probe_ent->flags;
889
890 if (sata_dev_desc[dev].lba48) {
891 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
892 buffer, WRITE_CMD);
893 if ((flags & SATA_FLAG_WCACHE) &&
894 (flags & SATA_FLAG_FLUSH_EXT))
895 dwc_ahsata_flush_cache_ext(dev);
896 } else {
897 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
898 buffer, WRITE_CMD);
899 if ((flags & SATA_FLAG_WCACHE) &&
900 (flags & SATA_FLAG_FLUSH))
901 dwc_ahsata_flush_cache(dev);
902 }
903 return rc;
904}
905
906int scan_sata(int dev)
907{
908 u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
909 u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
910 u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
911 u16 *id;
912 u64 n_sectors;
Simon Glass5ce59672017-06-14 21:28:32 -0600913 struct ahci_uc_priv *probe_ent =
914 (struct ahci_uc_priv *)sata_dev_desc[dev].priv;
Stefano Babic771bfd12012-02-22 00:24:39 +0000915 u8 port = probe_ent->hard_port_no;
Simon Glasse3394752016-02-29 15:25:34 -0700916 struct blk_desc *pdev = &(sata_dev_desc[dev]);
Stefano Babic771bfd12012-02-22 00:24:39 +0000917
Eric Nelson998816b2013-06-15 16:09:55 -0700918 id = (u16 *)memalign(ARCH_DMA_MINALIGN,
919 roundup(ARCH_DMA_MINALIGN,
920 (ATA_ID_WORDS * 2)));
Stefano Babic771bfd12012-02-22 00:24:39 +0000921 if (!id) {
922 printf("id malloc failed\n\r");
923 return -1;
924 }
925
926 /* Identify device to get information */
927 dwc_ahsata_identify(dev, id);
928
929 /* Serial number */
930 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
931 memcpy(pdev->product, serial, sizeof(serial));
932
933 /* Firmware version */
934 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
935 memcpy(pdev->revision, firmware, sizeof(firmware));
936
937 /* Product model */
938 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
939 memcpy(pdev->vendor, product, sizeof(product));
940
941 /* Totoal sectors */
942 n_sectors = ata_id_n_sectors(id);
943 pdev->lba = (u32)n_sectors;
944
945 pdev->type = DEV_TYPE_HARDDISK;
946 pdev->blksz = ATA_SECT_SIZE;
947 pdev->lun = 0 ;
948
949 /* Check if support LBA48 */
950 if (ata_id_has_lba48(id)) {
951 pdev->lba48 = 1;
952 debug("Device support LBA48\n\r");
953 }
954
955 /* Get the NCQ queue depth from device */
956 probe_ent->flags &= (~SATA_FLAG_Q_DEP_MASK);
957 probe_ent->flags |= ata_id_queue_depth(id);
958
959 /* Get the xfer mode from device */
960 dwc_ahsata_xfer_mode(dev, id);
961
962 /* Get the write cache status from device */
963 dwc_ahsata_init_wcache(dev, id);
964
965 /* Set the xfer mode to highest speed */
966 ahci_set_feature(dev, port);
967
968 free((void *)id);
969
970 dwc_ahsata_print_info(dev);
971
972 is_ready = 1;
973
974 return 0;
975}