Merge branch '2020-05-08-assorted-fixes'
[oweals/u-boot.git] / drivers / ata / dwc_ahsata.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
4  * Terry Lv <r65388@freescale.com>
5  */
6
7 #include <common.h>
8 #include <ahci.h>
9 #include <cpu_func.h>
10 #include <dm.h>
11 #include <dwc_ahsata.h>
12 #include <fis.h>
13 #include <libata.h>
14 #include <malloc.h>
15 #include <memalign.h>
16 #include <sata.h>
17 #include <asm/io.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/sys_proto.h>
20 #include <asm/mach-imx/sata.h>
21 #include <linux/bitops.h>
22 #include <linux/ctype.h>
23 #include <linux/errno.h>
24 #include "dwc_ahsata_priv.h"
25
26 struct sata_port_regs {
27         u32 clb;
28         u32 clbu;
29         u32 fb;
30         u32 fbu;
31         u32 is;
32         u32 ie;
33         u32 cmd;
34         u32 res1[1];
35         u32 tfd;
36         u32 sig;
37         u32 ssts;
38         u32 sctl;
39         u32 serr;
40         u32 sact;
41         u32 ci;
42         u32 sntf;
43         u32 res2[1];
44         u32 dmacr;
45         u32 res3[1];
46         u32 phycr;
47         u32 physr;
48 };
49
50 struct sata_host_regs {
51         u32 cap;
52         u32 ghc;
53         u32 is;
54         u32 pi;
55         u32 vs;
56         u32 ccc_ctl;
57         u32 ccc_ports;
58         u32 res1[2];
59         u32 cap2;
60         u32 res2[30];
61         u32 bistafr;
62         u32 bistcr;
63         u32 bistfctr;
64         u32 bistsr;
65         u32 bistdecr;
66         u32 res3[2];
67         u32 oobr;
68         u32 res4[8];
69         u32 timer1ms;
70         u32 res5[1];
71         u32 gparam1r;
72         u32 gparam2r;
73         u32 pparamr;
74         u32 testr;
75         u32 versionr;
76         u32 idr;
77 };
78
79 #define MAX_DATA_BYTES_PER_SG  (4 * 1024 * 1024)
80 #define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
81
82 #define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
83
84 static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
85 {
86         return base + 0x100 + (port * 0x80);
87 }
88
89 static int waiting_for_cmd_completed(u8 *offset,
90                                         int timeout_msec,
91                                         u32 sign)
92 {
93         int i;
94         u32 status;
95
96         for (i = 0;
97                 ((status = readl(offset)) & sign) && i < timeout_msec;
98                 ++i)
99                 mdelay(1);
100
101         return (i < timeout_msec) ? 0 : -1;
102 }
103
104 static int ahci_setup_oobr(struct ahci_uc_priv *uc_priv, int clk)
105 {
106         struct sata_host_regs *host_mmio = uc_priv->mmio_base;
107
108         writel(SATA_HOST_OOBR_WE, &host_mmio->oobr);
109         writel(0x02060b14, &host_mmio->oobr);
110
111         return 0;
112 }
113
114 static int ahci_host_init(struct ahci_uc_priv *uc_priv)
115 {
116         u32 tmp, cap_save, num_ports;
117         int i, j, timeout = 1000;
118         struct sata_port_regs *port_mmio = NULL;
119         struct sata_host_regs *host_mmio = uc_priv->mmio_base;
120         int clk = mxc_get_clock(MXC_SATA_CLK);
121
122         cap_save = readl(&host_mmio->cap);
123         cap_save |= SATA_HOST_CAP_SSS;
124
125         /* global controller reset */
126         tmp = readl(&host_mmio->ghc);
127         if ((tmp & SATA_HOST_GHC_HR) == 0)
128                 writel_with_flush(tmp | SATA_HOST_GHC_HR, &host_mmio->ghc);
129
130         while ((readl(&host_mmio->ghc) & SATA_HOST_GHC_HR) && --timeout)
131                 ;
132
133         if (timeout <= 0) {
134                 debug("controller reset failed (0x%x)\n", tmp);
135                 return -1;
136         }
137
138         /* Set timer 1ms */
139         writel(clk / 1000, &host_mmio->timer1ms);
140
141         ahci_setup_oobr(uc_priv, 0);
142
143         writel_with_flush(SATA_HOST_GHC_AE, &host_mmio->ghc);
144         writel(cap_save, &host_mmio->cap);
145         num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
146         writel_with_flush((1 << num_ports) - 1, &host_mmio->pi);
147
148         /*
149          * Determine which Ports are implemented by the DWC_ahsata,
150          * by reading the PI register. This bit map value aids the
151          * software to determine how many Ports are available and
152          * which Port registers need to be initialized.
153          */
154         uc_priv->cap = readl(&host_mmio->cap);
155         uc_priv->port_map = readl(&host_mmio->pi);
156
157         /* Determine how many command slots the HBA supports */
158         uc_priv->n_ports = (uc_priv->cap & SATA_HOST_CAP_NP_MASK) + 1;
159
160         debug("cap 0x%x  port_map 0x%x  n_ports %d\n",
161                 uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
162
163         for (i = 0; i < uc_priv->n_ports; i++) {
164                 uc_priv->port[i].port_mmio = ahci_port_base(host_mmio, i);
165                 port_mmio = uc_priv->port[i].port_mmio;
166
167                 /* Ensure that the DWC_ahsata is in idle state */
168                 tmp = readl(&port_mmio->cmd);
169
170                 /*
171                  * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
172                  * are all cleared, the Port is in an idle state.
173                  */
174                 if (tmp & (SATA_PORT_CMD_CR | SATA_PORT_CMD_FR |
175                         SATA_PORT_CMD_FRE | SATA_PORT_CMD_ST)) {
176
177                         /*
178                          * System software places a Port into the idle state by
179                          * clearing P#CMD.ST and waiting for P#CMD.CR to return
180                          * 0 when read.
181                          */
182                         tmp &= ~SATA_PORT_CMD_ST;
183                         writel_with_flush(tmp, &port_mmio->cmd);
184
185                         /*
186                          * spec says 500 msecs for each bit, so
187                          * this is slightly incorrect.
188                          */
189                         mdelay(500);
190
191                         timeout = 1000;
192                         while ((readl(&port_mmio->cmd) & SATA_PORT_CMD_CR)
193                                 && --timeout)
194                                 ;
195
196                         if (timeout <= 0) {
197                                 debug("port reset failed (0x%x)\n", tmp);
198                                 return -1;
199                         }
200                 }
201
202                 /* Spin-up device */
203                 tmp = readl(&port_mmio->cmd);
204                 writel((tmp | SATA_PORT_CMD_SUD), &port_mmio->cmd);
205
206                 /* Wait for spin-up to finish */
207                 timeout = 1000;
208                 while (!(readl(&port_mmio->cmd) | SATA_PORT_CMD_SUD)
209                         && --timeout)
210                         ;
211                 if (timeout <= 0) {
212                         debug("Spin-Up can't finish!\n");
213                         return -1;
214                 }
215
216                 for (j = 0; j < 100; ++j) {
217                         mdelay(10);
218                         tmp = readl(&port_mmio->ssts);
219                         if (((tmp & SATA_PORT_SSTS_DET_MASK) == 0x3) ||
220                                 ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x1))
221                                 break;
222                 }
223
224                 /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
225                 timeout = 1000;
226                 while (!(readl(&port_mmio->serr) & SATA_PORT_SERR_DIAG_X)
227                         && --timeout)
228                         ;
229                 if (timeout <= 0) {
230                         debug("Can't find DIAG_X set!\n");
231                         return -1;
232                 }
233
234                 /*
235                  * For each implemented Port, clear the P#SERR
236                  * register, by writing ones to each implemented\
237                  * bit location.
238                  */
239                 tmp = readl(&port_mmio->serr);
240                 debug("P#SERR 0x%x\n",
241                                 tmp);
242                 writel(tmp, &port_mmio->serr);
243
244                 /* Ack any pending irq events for this port */
245                 tmp = readl(&host_mmio->is);
246                 debug("IS 0x%x\n", tmp);
247                 if (tmp)
248                         writel(tmp, &host_mmio->is);
249
250                 writel(1 << i, &host_mmio->is);
251
252                 /* set irq mask (enables interrupts) */
253                 writel(DEF_PORT_IRQ, &port_mmio->ie);
254
255                 /* register linkup ports */
256                 tmp = readl(&port_mmio->ssts);
257                 debug("Port %d status: 0x%x\n", i, tmp);
258                 if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
259                         uc_priv->link_port_map |= (0x01 << i);
260         }
261
262         tmp = readl(&host_mmio->ghc);
263         debug("GHC 0x%x\n", tmp);
264         writel(tmp | SATA_HOST_GHC_IE, &host_mmio->ghc);
265         tmp = readl(&host_mmio->ghc);
266         debug("GHC 0x%x\n", tmp);
267
268         return 0;
269 }
270
271 static void ahci_print_info(struct ahci_uc_priv *uc_priv)
272 {
273         struct sata_host_regs *host_mmio = uc_priv->mmio_base;
274         u32 vers, cap, impl, speed;
275         const char *speed_s;
276         const char *scc_s;
277
278         vers = readl(&host_mmio->vs);
279         cap = uc_priv->cap;
280         impl = uc_priv->port_map;
281
282         speed = (cap & SATA_HOST_CAP_ISS_MASK)
283                 >> SATA_HOST_CAP_ISS_OFFSET;
284         if (speed == 1)
285                 speed_s = "1.5";
286         else if (speed == 2)
287                 speed_s = "3";
288         else
289                 speed_s = "?";
290
291         scc_s = "SATA";
292
293         printf("AHCI %02x%02x.%02x%02x "
294                 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
295                 (vers >> 24) & 0xff,
296                 (vers >> 16) & 0xff,
297                 (vers >> 8) & 0xff,
298                 vers & 0xff,
299                 ((cap >> 8) & 0x1f) + 1,
300                 (cap & 0x1f) + 1,
301                 speed_s,
302                 impl,
303                 scc_s);
304
305         printf("flags: "
306                 "%s%s%s%s%s%s"
307                 "%s%s%s%s%s%s%s\n",
308                 cap & (1 << 31) ? "64bit " : "",
309                 cap & (1 << 30) ? "ncq " : "",
310                 cap & (1 << 28) ? "ilck " : "",
311                 cap & (1 << 27) ? "stag " : "",
312                 cap & (1 << 26) ? "pm " : "",
313                 cap & (1 << 25) ? "led " : "",
314                 cap & (1 << 24) ? "clo " : "",
315                 cap & (1 << 19) ? "nz " : "",
316                 cap & (1 << 18) ? "only " : "",
317                 cap & (1 << 17) ? "pmp " : "",
318                 cap & (1 << 15) ? "pio " : "",
319                 cap & (1 << 14) ? "slum " : "",
320                 cap & (1 << 13) ? "part " : "");
321 }
322
323 static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
324                         unsigned char *buf, int buf_len)
325 {
326         struct ahci_ioports *pp = &uc_priv->port[port];
327         struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
328         u32 sg_count, max_bytes;
329         int i;
330
331         max_bytes = MAX_DATA_BYTES_PER_SG;
332         sg_count = ((buf_len - 1) / max_bytes) + 1;
333         if (sg_count > AHCI_MAX_SG) {
334                 printf("Error:Too much sg!\n");
335                 return -1;
336         }
337
338         for (i = 0; i < sg_count; i++) {
339                 ahci_sg->addr =
340                         cpu_to_le32((u32)buf + i * max_bytes);
341                 ahci_sg->addr_hi = 0;
342                 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
343                                         (buf_len < max_bytes
344                                         ? (buf_len - 1)
345                                         : (max_bytes - 1)));
346                 ahci_sg++;
347                 buf_len -= max_bytes;
348         }
349
350         return sg_count;
351 }
352
353 static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
354 {
355         struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
356                                         AHCI_CMD_SLOT_SZ * cmd_slot);
357
358         memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
359         cmd_hdr->opts = cpu_to_le32(opts);
360         cmd_hdr->status = 0;
361         pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
362 #ifdef CONFIG_PHYS_64BIT
363         pp->cmd_slot->tbl_addr_hi =
364             cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
365 #endif
366 }
367
368 #define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
369
370 static int ahci_exec_ata_cmd(struct ahci_uc_priv *uc_priv, u8 port,
371                              struct sata_fis_h2d *cfis, u8 *buf, u32 buf_len,
372                              s32 is_write)
373 {
374         struct ahci_ioports *pp = &uc_priv->port[port];
375         struct sata_port_regs *port_mmio = pp->port_mmio;
376         u32 opts;
377         int sg_count = 0, cmd_slot = 0;
378
379         cmd_slot = AHCI_GET_CMD_SLOT(readl(&port_mmio->ci));
380         if (32 == cmd_slot) {
381                 printf("Can't find empty command slot!\n");
382                 return 0;
383         }
384
385         /* Check xfer length */
386         if (buf_len > MAX_BYTES_PER_TRANS) {
387                 printf("Max transfer length is %dB\n\r",
388                         MAX_BYTES_PER_TRANS);
389                 return 0;
390         }
391
392         memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
393         if (buf && buf_len)
394                 sg_count = ahci_fill_sg(uc_priv, port, buf, buf_len);
395         opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
396         if (is_write) {
397                 opts |= 0x40;
398                 flush_cache((ulong)buf, buf_len);
399         }
400         ahci_fill_cmd_slot(pp, cmd_slot, opts);
401
402         flush_cache((int)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
403         writel_with_flush(1 << cmd_slot, &port_mmio->ci);
404
405         if (waiting_for_cmd_completed((u8 *)&port_mmio->ci, 10000,
406                                       0x1 << cmd_slot)) {
407                 printf("timeout exit!\n");
408                 return -1;
409         }
410         invalidate_dcache_range((int)(pp->cmd_slot),
411                                 (int)(pp->cmd_slot)+AHCI_PORT_PRIV_DMA_SZ);
412         debug("ahci_exec_ata_cmd: %d byte transferred.\n",
413               pp->cmd_slot->status);
414         if (!is_write)
415                 invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
416
417         return buf_len;
418 }
419
420 static void ahci_set_feature(struct ahci_uc_priv *uc_priv, u8 port)
421 {
422         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
423         struct sata_fis_h2d *cfis = &h2d;
424
425         memset(cfis, 0, sizeof(struct sata_fis_h2d));
426         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
427         cfis->pm_port_c = 1 << 7;
428         cfis->command = ATA_CMD_SET_FEATURES;
429         cfis->features = SETFEATURES_XFER;
430         cfis->sector_count = ffs(uc_priv->udma_mask + 1) + 0x3e;
431
432         ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, READ_CMD);
433 }
434
435 static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
436 {
437         struct ahci_ioports *pp = &uc_priv->port[port];
438         struct sata_port_regs *port_mmio = pp->port_mmio;
439         u32 port_status;
440         u32 mem;
441         int timeout = 10000000;
442
443         debug("Enter start port: %d\n", port);
444         port_status = readl(&port_mmio->ssts);
445         debug("Port %d status: %x\n", port, port_status);
446         if ((port_status & 0xf) != 0x03) {
447                 printf("No Link on this port!\n");
448                 return -1;
449         }
450
451         mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
452         if (!mem) {
453                 printf("No mem for table!\n");
454                 return -ENOMEM;
455         }
456
457         mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
458         memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
459
460         /*
461          * First item in chunk of DMA memory: 32-slot command table,
462          * 32 bytes each in size
463          */
464         pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
465         debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
466         mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
467
468         /*
469          * Second item: Received-FIS area, 256-Byte aligned
470          */
471         pp->rx_fis = mem;
472         mem += AHCI_RX_FIS_SZ;
473
474         /*
475          * Third item: data area for storing a single command
476          * and its scatter-gather table
477          */
478         pp->cmd_tbl = mem;
479         debug("cmd_tbl_dma = 0x%lx\n", pp->cmd_tbl);
480
481         mem += AHCI_CMD_TBL_HDR;
482
483         writel_with_flush(0x00004444, &port_mmio->dmacr);
484         pp->cmd_tbl_sg = (struct ahci_sg *)mem;
485         writel_with_flush((u32)pp->cmd_slot, &port_mmio->clb);
486         writel_with_flush(pp->rx_fis, &port_mmio->fb);
487
488         /* Enable FRE */
489         writel_with_flush((SATA_PORT_CMD_FRE | readl(&port_mmio->cmd)),
490                           &port_mmio->cmd);
491
492         /* Wait device ready */
493         while ((readl(&port_mmio->tfd) & (SATA_PORT_TFD_STS_ERR |
494                 SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
495                 && --timeout)
496                 ;
497         if (timeout <= 0) {
498                 debug("Device not ready for BSY, DRQ and"
499                         "ERR in TFD!\n");
500                 return -1;
501         }
502
503         writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
504                           PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
505                           PORT_CMD_START, &port_mmio->cmd);
506
507         debug("Exit start port %d\n", port);
508
509         return 0;
510 }
511
512 static void dwc_ahsata_print_info(struct blk_desc *pdev)
513 {
514         printf("SATA Device Info:\n\r");
515         printf("S/N: %s\n\rProduct model number: %s\n\r"
516                 "Firmware version: %s\n\rCapacity: " LBAFU " sectors\n\r",
517                 pdev->product, pdev->vendor, pdev->revision, pdev->lba);
518 }
519
520 static void dwc_ahsata_identify(struct ahci_uc_priv *uc_priv, u16 *id)
521 {
522         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
523         struct sata_fis_h2d *cfis = &h2d;
524         u8 port = uc_priv->hard_port_no;
525
526         memset(cfis, 0, sizeof(struct sata_fis_h2d));
527
528         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
529         cfis->pm_port_c = 0x80; /* is command */
530         cfis->command = ATA_CMD_ID_ATA;
531
532         ahci_exec_ata_cmd(uc_priv, port, cfis, (u8 *)id, ATA_ID_WORDS * 2,
533                           READ_CMD);
534         ata_swap_buf_le16(id, ATA_ID_WORDS);
535 }
536
537 static void dwc_ahsata_xfer_mode(struct ahci_uc_priv *uc_priv, u16 *id)
538 {
539         uc_priv->pio_mask = id[ATA_ID_PIO_MODES];
540         uc_priv->udma_mask = id[ATA_ID_UDMA_MODES];
541         debug("pio %04x, udma %04x\n\r", uc_priv->pio_mask, uc_priv->udma_mask);
542 }
543
544 static u32 dwc_ahsata_rw_cmd(struct ahci_uc_priv *uc_priv, u32 start,
545                              u32 blkcnt, u8 *buffer, int is_write)
546 {
547         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
548         struct sata_fis_h2d *cfis = &h2d;
549         u8 port = uc_priv->hard_port_no;
550         u32 block;
551
552         block = start;
553
554         memset(cfis, 0, sizeof(struct sata_fis_h2d));
555
556         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
557         cfis->pm_port_c = 0x80; /* is command */
558         cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
559         cfis->device = ATA_LBA;
560
561         cfis->device |= (block >> 24) & 0xf;
562         cfis->lba_high = (block >> 16) & 0xff;
563         cfis->lba_mid = (block >> 8) & 0xff;
564         cfis->lba_low = block & 0xff;
565         cfis->sector_count = (u8)(blkcnt & 0xff);
566
567         if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
568                               ATA_SECT_SIZE * blkcnt, is_write) > 0)
569                 return blkcnt;
570         else
571                 return 0;
572 }
573
574 static void dwc_ahsata_flush_cache(struct ahci_uc_priv *uc_priv)
575 {
576         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
577         struct sata_fis_h2d *cfis = &h2d;
578         u8 port = uc_priv->hard_port_no;
579
580         memset(cfis, 0, sizeof(struct sata_fis_h2d));
581
582         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
583         cfis->pm_port_c = 0x80; /* is command */
584         cfis->command = ATA_CMD_FLUSH;
585
586         ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
587 }
588
589 static u32 dwc_ahsata_rw_cmd_ext(struct ahci_uc_priv *uc_priv, u32 start,
590                                  lbaint_t blkcnt, u8 *buffer, int is_write)
591 {
592         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
593         struct sata_fis_h2d *cfis = &h2d;
594         u8 port = uc_priv->hard_port_no;
595         u64 block;
596
597         block = (u64)start;
598
599         memset(cfis, 0, sizeof(struct sata_fis_h2d));
600
601         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
602         cfis->pm_port_c = 0x80; /* is command */
603
604         cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
605                                  : ATA_CMD_READ_EXT;
606
607         cfis->lba_high_exp = (block >> 40) & 0xff;
608         cfis->lba_mid_exp = (block >> 32) & 0xff;
609         cfis->lba_low_exp = (block >> 24) & 0xff;
610         cfis->lba_high = (block >> 16) & 0xff;
611         cfis->lba_mid = (block >> 8) & 0xff;
612         cfis->lba_low = block & 0xff;
613         cfis->device = ATA_LBA;
614         cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
615         cfis->sector_count = blkcnt & 0xff;
616
617         if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
618                               ATA_SECT_SIZE * blkcnt, is_write) > 0)
619                 return blkcnt;
620         else
621                 return 0;
622 }
623
624 static void dwc_ahsata_flush_cache_ext(struct ahci_uc_priv *uc_priv)
625 {
626         struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
627         struct sata_fis_h2d *cfis = &h2d;
628         u8 port = uc_priv->hard_port_no;
629
630         memset(cfis, 0, sizeof(struct sata_fis_h2d));
631
632         cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
633         cfis->pm_port_c = 0x80; /* is command */
634         cfis->command = ATA_CMD_FLUSH_EXT;
635
636         ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
637 }
638
639 static void dwc_ahsata_init_wcache(struct ahci_uc_priv *uc_priv, u16 *id)
640 {
641         if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
642                 uc_priv->flags |= SATA_FLAG_WCACHE;
643         if (ata_id_has_flush(id))
644                 uc_priv->flags |= SATA_FLAG_FLUSH;
645         if (ata_id_has_flush_ext(id))
646                 uc_priv->flags |= SATA_FLAG_FLUSH_EXT;
647 }
648
649 static u32 ata_low_level_rw_lba48(struct ahci_uc_priv *uc_priv, u32 blknr,
650                                   lbaint_t blkcnt, const void *buffer,
651                                   int is_write)
652 {
653         u32 start, blks;
654         u8 *addr;
655         int max_blks;
656
657         start = blknr;
658         blks = blkcnt;
659         addr = (u8 *)buffer;
660
661         max_blks = ATA_MAX_SECTORS_LBA48;
662
663         do {
664                 if (blks > max_blks) {
665                         if (max_blks != dwc_ahsata_rw_cmd_ext(uc_priv, start,
666                                                               max_blks, addr,
667                                                               is_write))
668                                 return 0;
669                         start += max_blks;
670                         blks -= max_blks;
671                         addr += ATA_SECT_SIZE * max_blks;
672                 } else {
673                         if (blks != dwc_ahsata_rw_cmd_ext(uc_priv, start, blks,
674                                                           addr, is_write))
675                                 return 0;
676                         start += blks;
677                         blks = 0;
678                         addr += ATA_SECT_SIZE * blks;
679                 }
680         } while (blks != 0);
681
682         return blkcnt;
683 }
684
685 static u32 ata_low_level_rw_lba28(struct ahci_uc_priv *uc_priv, u32 blknr,
686                                   lbaint_t blkcnt, const void *buffer,
687                                   int is_write)
688 {
689         u32 start, blks;
690         u8 *addr;
691         int max_blks;
692
693         start = blknr;
694         blks = blkcnt;
695         addr = (u8 *)buffer;
696
697         max_blks = ATA_MAX_SECTORS;
698         do {
699                 if (blks > max_blks) {
700                         if (max_blks != dwc_ahsata_rw_cmd(uc_priv, start,
701                                                           max_blks, addr,
702                                                           is_write))
703                                 return 0;
704                         start += max_blks;
705                         blks -= max_blks;
706                         addr += ATA_SECT_SIZE * max_blks;
707                 } else {
708                         if (blks != dwc_ahsata_rw_cmd(uc_priv, start, blks,
709                                                       addr, is_write))
710                                 return 0;
711                         start += blks;
712                         blks = 0;
713                         addr += ATA_SECT_SIZE * blks;
714                 }
715         } while (blks != 0);
716
717         return blkcnt;
718 }
719
720 static int dwc_ahci_start_ports(struct ahci_uc_priv *uc_priv)
721 {
722         u32 linkmap;
723         int i;
724
725         linkmap = uc_priv->link_port_map;
726
727         if (0 == linkmap) {
728                 printf("No port device detected!\n");
729                 return -ENXIO;
730         }
731
732         for (i = 0; i < uc_priv->n_ports; i++) {
733                 if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
734                         if (ahci_port_start(uc_priv, (u8)i)) {
735                                 printf("Can not start port %d\n", i);
736                                 return 1;
737                         }
738                         uc_priv->hard_port_no = i;
739                         break;
740                 }
741         }
742
743         return 0;
744 }
745
746 static int dwc_ahsata_scan_common(struct ahci_uc_priv *uc_priv,
747                                   struct blk_desc *pdev)
748 {
749         u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
750         u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
751         u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
752         u8 port = uc_priv->hard_port_no;
753         ALLOC_CACHE_ALIGN_BUFFER(u16, id, ATA_ID_WORDS);
754
755         /* Identify device to get information */
756         dwc_ahsata_identify(uc_priv, id);
757
758         /* Serial number */
759         ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
760         memcpy(pdev->product, serial, sizeof(serial));
761
762         /* Firmware version */
763         ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
764         memcpy(pdev->revision, firmware, sizeof(firmware));
765
766         /* Product model */
767         ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
768         memcpy(pdev->vendor, product, sizeof(product));
769
770         /* Total sectors */
771         pdev->lba = ata_id_n_sectors(id);
772
773         pdev->type = DEV_TYPE_HARDDISK;
774         pdev->blksz = ATA_SECT_SIZE;
775         pdev->lun = 0;
776
777         /* Check if support LBA48 */
778         if (ata_id_has_lba48(id)) {
779                 pdev->lba48 = 1;
780                 debug("Device support LBA48\n\r");
781         }
782
783         /* Get the NCQ queue depth from device */
784         uc_priv->flags &= (~SATA_FLAG_Q_DEP_MASK);
785         uc_priv->flags |= ata_id_queue_depth(id);
786
787         /* Get the xfer mode from device */
788         dwc_ahsata_xfer_mode(uc_priv, id);
789
790         /* Get the write cache status from device */
791         dwc_ahsata_init_wcache(uc_priv, id);
792
793         /* Set the xfer mode to highest speed */
794         ahci_set_feature(uc_priv, port);
795
796         dwc_ahsata_print_info(pdev);
797
798         return 0;
799 }
800
801 /*
802  * SATA interface between low level driver and command layer
803  */
804 static ulong sata_read_common(struct ahci_uc_priv *uc_priv,
805                               struct blk_desc *desc, ulong blknr,
806                               lbaint_t blkcnt, void *buffer)
807 {
808         u32 rc;
809
810         if (desc->lba48)
811                 rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt, buffer,
812                                             READ_CMD);
813         else
814                 rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt, buffer,
815                                             READ_CMD);
816
817         return rc;
818 }
819
820 static ulong sata_write_common(struct ahci_uc_priv *uc_priv,
821                                struct blk_desc *desc, ulong blknr,
822                                lbaint_t blkcnt, const void *buffer)
823 {
824         u32 rc;
825         u32 flags = uc_priv->flags;
826
827         if (desc->lba48) {
828                 rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt, buffer,
829                                             WRITE_CMD);
830                 if ((flags & SATA_FLAG_WCACHE) && (flags & SATA_FLAG_FLUSH_EXT))
831                         dwc_ahsata_flush_cache_ext(uc_priv);
832         } else {
833                 rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt, buffer,
834                                             WRITE_CMD);
835                 if ((flags & SATA_FLAG_WCACHE) && (flags & SATA_FLAG_FLUSH))
836                         dwc_ahsata_flush_cache(uc_priv);
837         }
838
839         return rc;
840 }
841
842 #if !CONFIG_IS_ENABLED(AHCI)
843 static int ahci_init_one(int pdev)
844 {
845         int rc;
846         struct ahci_uc_priv *uc_priv = NULL;
847
848         uc_priv = malloc(sizeof(struct ahci_uc_priv));
849         if (!uc_priv)
850                 return -ENOMEM;
851
852         memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
853         uc_priv->dev = pdev;
854
855         uc_priv->host_flags = ATA_FLAG_SATA
856                                 | ATA_FLAG_NO_LEGACY
857                                 | ATA_FLAG_MMIO
858                                 | ATA_FLAG_PIO_DMA
859                                 | ATA_FLAG_NO_ATAPI;
860
861         uc_priv->mmio_base = (void __iomem *)CONFIG_DWC_AHSATA_BASE_ADDR;
862
863         /* initialize adapter */
864         rc = ahci_host_init(uc_priv);
865         if (rc)
866                 goto err_out;
867
868         ahci_print_info(uc_priv);
869
870         /* Save the uc_private struct to block device struct */
871         sata_dev_desc[pdev].priv = uc_priv;
872
873         return 0;
874
875 err_out:
876         if (uc_priv)
877                 free(uc_priv);
878         return rc;
879 }
880
881 int init_sata(int dev)
882 {
883         struct ahci_uc_priv *uc_priv = NULL;
884
885 #if defined(CONFIG_MX6)
886         if (!is_mx6dq() && !is_mx6dqp())
887                 return 1;
888 #endif
889         if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
890                 printf("The sata index %d is out of ranges\n\r", dev);
891                 return -1;
892         }
893
894         ahci_init_one(dev);
895
896         uc_priv = sata_dev_desc[dev].priv;
897
898         return dwc_ahci_start_ports(uc_priv) ? 1 : 0;
899 }
900
901 int reset_sata(int dev)
902 {
903         struct ahci_uc_priv *uc_priv;
904         struct sata_host_regs *host_mmio;
905
906         if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
907                 printf("The sata index %d is out of ranges\n\r", dev);
908                 return -1;
909         }
910
911         uc_priv = sata_dev_desc[dev].priv;
912         if (NULL == uc_priv)
913                 /* not initialized, so nothing to reset */
914                 return 0;
915
916         host_mmio = uc_priv->mmio_base;
917         setbits_le32(&host_mmio->ghc, SATA_HOST_GHC_HR);
918         while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
919                 udelay(100);
920
921         free(uc_priv);
922         memset(&sata_dev_desc[dev], 0, sizeof(struct blk_desc));
923
924         return 0;
925 }
926
927 int sata_port_status(int dev, int port)
928 {
929         struct sata_port_regs *port_mmio;
930         struct ahci_uc_priv *uc_priv = NULL;
931
932         if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1))
933                 return -EINVAL;
934
935         if (sata_dev_desc[dev].priv == NULL)
936                 return -ENODEV;
937
938         uc_priv = sata_dev_desc[dev].priv;
939         port_mmio = uc_priv->port[port].port_mmio;
940
941         return readl(&port_mmio->ssts) & SATA_PORT_SSTS_DET_MASK;
942 }
943
944 /*
945  * SATA interface between low level driver and command layer
946  */
947 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
948 {
949         struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
950
951         return sata_read_common(uc_priv, &sata_dev_desc[dev], blknr, blkcnt,
952                                 buffer);
953 }
954
955 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
956 {
957         struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
958
959         return sata_write_common(uc_priv, &sata_dev_desc[dev], blknr, blkcnt,
960                                  buffer);
961 }
962
963 int scan_sata(int dev)
964 {
965         struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
966         struct blk_desc *pdev = &sata_dev_desc[dev];
967
968         return dwc_ahsata_scan_common(uc_priv, pdev);
969 }
970 #endif /* CONFIG_IS_ENABLED(AHCI) */
971
972 #if CONFIG_IS_ENABLED(AHCI)
973
974 int dwc_ahsata_port_status(struct udevice *dev, int port)
975 {
976         struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
977         struct sata_port_regs *port_mmio;
978
979         port_mmio = uc_priv->port[port].port_mmio;
980         return readl(&port_mmio->ssts) & SATA_PORT_SSTS_DET_MASK ? 0 : -ENXIO;
981 }
982
983 int dwc_ahsata_bus_reset(struct udevice *dev)
984 {
985         struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
986         struct sata_host_regs *host_mmio = uc_priv->mmio_base;
987
988         setbits_le32(&host_mmio->ghc, SATA_HOST_GHC_HR);
989         while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
990                 udelay(100);
991
992         return 0;
993 }
994
995 int dwc_ahsata_scan(struct udevice *dev)
996 {
997         struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
998         struct blk_desc *desc;
999         struct udevice *blk;
1000         int ret;
1001
1002         /*
1003         * Create only one block device and do detection
1004         * to make sure that there won't be a lot of
1005         * block devices created
1006         */
1007         device_find_first_child(dev, &blk);
1008         if (!blk) {
1009                 ret = blk_create_devicef(dev, "dwc_ahsata_blk", "blk",
1010                                          IF_TYPE_SATA, -1, 512, 0, &blk);
1011                 if (ret) {
1012                         debug("Can't create device\n");
1013                         return ret;
1014                 }
1015         }
1016
1017         desc = dev_get_uclass_platdata(blk);
1018         ret = dwc_ahsata_scan_common(uc_priv, desc);
1019         if (ret) {
1020                 debug("%s: Failed to scan bus\n", __func__);
1021                 return ret;
1022         }
1023
1024         return 0;
1025 }
1026
1027 int dwc_ahsata_probe(struct udevice *dev)
1028 {
1029         struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
1030         int ret;
1031
1032 #if defined(CONFIG_MX6)
1033         setup_sata();
1034 #endif
1035         uc_priv->host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
1036                         ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NO_ATAPI;
1037         uc_priv->mmio_base = (void __iomem *)dev_read_addr(dev);
1038
1039         /* initialize adapter */
1040         ret = ahci_host_init(uc_priv);
1041         if (ret)
1042                 return ret;
1043
1044         ahci_print_info(uc_priv);
1045
1046         return dwc_ahci_start_ports(uc_priv);
1047 }
1048
1049 static ulong dwc_ahsata_read(struct udevice *blk, lbaint_t blknr,
1050                              lbaint_t blkcnt, void *buffer)
1051 {
1052         struct blk_desc *desc = dev_get_uclass_platdata(blk);
1053         struct udevice *dev = dev_get_parent(blk);
1054         struct ahci_uc_priv *uc_priv;
1055
1056         uc_priv = dev_get_uclass_priv(dev);
1057         return sata_read_common(uc_priv, desc, blknr, blkcnt, buffer);
1058 }
1059
1060 static ulong dwc_ahsata_write(struct udevice *blk, lbaint_t blknr,
1061                               lbaint_t blkcnt, const void *buffer)
1062 {
1063         struct blk_desc *desc = dev_get_uclass_platdata(blk);
1064         struct udevice *dev = dev_get_parent(blk);
1065         struct ahci_uc_priv *uc_priv;
1066
1067         uc_priv = dev_get_uclass_priv(dev);
1068         return sata_write_common(uc_priv, desc, blknr, blkcnt, buffer);
1069 }
1070
1071 static const struct blk_ops dwc_ahsata_blk_ops = {
1072         .read   = dwc_ahsata_read,
1073         .write  = dwc_ahsata_write,
1074 };
1075
1076 U_BOOT_DRIVER(dwc_ahsata_blk) = {
1077         .name           = "dwc_ahsata_blk",
1078         .id             = UCLASS_BLK,
1079         .ops            = &dwc_ahsata_blk_ops,
1080 };
1081
1082 #if CONFIG_IS_ENABLED(DWC_AHSATA_AHCI)
1083 struct ahci_ops dwc_ahsata_ahci_ops = {
1084         .port_status = dwc_ahsata_port_status,
1085         .reset       = dwc_ahsata_bus_reset,
1086         .scan        = dwc_ahsata_scan,
1087 };
1088
1089 static const struct udevice_id dwc_ahsata_ahci_ids[] = {
1090         { .compatible = "fsl,imx6q-ahci" },
1091         { }
1092 };
1093
1094 U_BOOT_DRIVER(dwc_ahsata_ahci) = {
1095         .name     = "dwc_ahsata_ahci",
1096         .id       = UCLASS_AHCI,
1097         .of_match = dwc_ahsata_ahci_ids,
1098         .ops      = &dwc_ahsata_ahci_ops,
1099         .probe    = dwc_ahsata_probe,
1100 };
1101 #endif
1102 #endif