a57f674c52c69971912a8fdce1e85c33535088f5
[oweals/u-boot.git] / drivers / block / ahci.c
1 /*
2  * Copyright (C) Freescale Semiconductor, Inc. 2006.
3  * Author: Jason Jin<Jason.jin@freescale.com>
4  *         Zhang Wei<wei.zhang@freescale.com>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  *
8  * with the reference on libata and ahci drvier in kernel
9  */
10 #include <common.h>
11
12 #include <command.h>
13 #include <pci.h>
14 #include <asm/processor.h>
15 #include <asm/errno.h>
16 #include <asm/io.h>
17 #include <malloc.h>
18 #include <scsi.h>
19 #include <libata.h>
20 #include <linux/ctype.h>
21 #include <ahci.h>
22
23 static int ata_io_flush(u8 port);
24
25 struct ahci_probe_ent *probe_ent = NULL;
26 u16 *ataid[AHCI_MAX_PORTS];
27
28 #define writel_with_flush(a,b)  do { writel(a,b); readl(b); } while (0)
29
30 /*
31  * Some controllers limit number of blocks they can read/write at once.
32  * Contemporary SSD devices work much faster if the read/write size is aligned
33  * to a power of 2.  Let's set default to 128 and allowing to be overwritten if
34  * needed.
35  */
36 #ifndef MAX_SATA_BLOCKS_READ_WRITE
37 #define MAX_SATA_BLOCKS_READ_WRITE      0x80
38 #endif
39
40 /* Maximum timeouts for each event */
41 #define WAIT_MS_SPINUP  20000
42 #define WAIT_MS_DATAIO  10000
43 #define WAIT_MS_FLUSH   5000
44 #define WAIT_MS_LINKUP  200
45
46 static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
47 {
48         return base + 0x100 + (port * 0x80);
49 }
50
51
52 static void ahci_setup_port(struct ahci_ioports *port, void __iomem *base,
53                             unsigned int port_idx)
54 {
55         base = ahci_port_base(base, port_idx);
56
57         port->cmd_addr = base;
58         port->scr_addr = base + PORT_SCR;
59 }
60
61
62 #define msleep(a) udelay(a * 1000)
63
64 static void ahci_dcache_flush_range(unsigned long begin, unsigned long len)
65 {
66         const unsigned long start = begin;
67         const unsigned long end = start + len;
68
69         debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
70         flush_dcache_range(start, end);
71 }
72
73 /*
74  * SATA controller DMAs to physical RAM.  Ensure data from the
75  * controller is invalidated from dcache; next access comes from
76  * physical RAM.
77  */
78 static void ahci_dcache_invalidate_range(unsigned long begin, unsigned long len)
79 {
80         const unsigned long start = begin;
81         const unsigned long end = start + len;
82
83         debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
84         invalidate_dcache_range(start, end);
85 }
86
87 /*
88  * Ensure data for SATA controller is flushed out of dcache and
89  * written to physical memory.
90  */
91 static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
92 {
93         ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
94                                 AHCI_PORT_PRIV_DMA_SZ);
95 }
96
97 static int waiting_for_cmd_completed(void __iomem *offset,
98                                      int timeout_msec,
99                                      u32 sign)
100 {
101         int i;
102         u32 status;
103
104         for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
105                 msleep(1);
106
107         return (i < timeout_msec) ? 0 : -1;
108 }
109
110 int __weak ahci_link_up(struct ahci_probe_ent *probe_ent, u8 port)
111 {
112         u32 tmp;
113         int j = 0;
114         void __iomem *port_mmio = probe_ent->port[port].port_mmio;
115
116         /*
117          * Bring up SATA link.
118          * SATA link bringup time is usually less than 1 ms; only very
119          * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
120          */
121         while (j < WAIT_MS_LINKUP) {
122                 tmp = readl(port_mmio + PORT_SCR_STAT);
123                 tmp &= PORT_SCR_STAT_DET_MASK;
124                 if (tmp == PORT_SCR_STAT_DET_PHYRDY)
125                         return 0;
126                 udelay(1000);
127                 j++;
128         }
129         return 1;
130 }
131
132 #ifdef CONFIG_SUNXI_AHCI
133 /* The sunxi AHCI controller requires this undocumented setup */
134 static void sunxi_dma_init(void __iomem *port_mmio)
135 {
136         clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
137 }
138 #endif
139
140 int ahci_reset(void __iomem *base)
141 {
142         int i = 1000;
143         u32 __iomem *host_ctl_reg = base + HOST_CTL;
144         u32 tmp = readl(host_ctl_reg); /* global controller reset */
145
146         if ((tmp & HOST_RESET) == 0)
147                 writel_with_flush(tmp | HOST_RESET, host_ctl_reg);
148
149         /*
150          * reset must complete within 1 second, or
151          * the hardware should be considered fried.
152          */
153         do {
154                 udelay(1000);
155                 tmp = readl(host_ctl_reg);
156                 i--;
157         } while ((i > 0) && (tmp & HOST_RESET));
158
159         if (i == 0) {
160                 printf("controller reset failed (0x%x)\n", tmp);
161                 return -1;
162         }
163
164         return 0;
165 }
166
167 static int ahci_host_init(struct ahci_probe_ent *probe_ent)
168 {
169 #ifndef CONFIG_SCSI_AHCI_PLAT
170         pci_dev_t pdev = probe_ent->dev;
171         u16 tmp16;
172         unsigned short vendor;
173 #endif
174         void __iomem *mmio = probe_ent->mmio_base;
175         u32 tmp, cap_save, cmd;
176         int i, j, ret;
177         void __iomem *port_mmio;
178         u32 port_map;
179
180         debug("ahci_host_init: start\n");
181
182         cap_save = readl(mmio + HOST_CAP);
183         cap_save &= ((1 << 28) | (1 << 17));
184         cap_save |= (1 << 27);  /* Staggered Spin-up. Not needed. */
185
186         ret = ahci_reset(probe_ent->mmio_base);
187         if (ret)
188                 return ret;
189
190         writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
191         writel(cap_save, mmio + HOST_CAP);
192         writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
193
194 #ifndef CONFIG_SCSI_AHCI_PLAT
195         pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
196
197         if (vendor == PCI_VENDOR_ID_INTEL) {
198                 u16 tmp16;
199                 pci_read_config_word(pdev, 0x92, &tmp16);
200                 tmp16 |= 0xf;
201                 pci_write_config_word(pdev, 0x92, tmp16);
202         }
203 #endif
204         probe_ent->cap = readl(mmio + HOST_CAP);
205         probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
206         port_map = probe_ent->port_map;
207         probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
208
209         debug("cap 0x%x  port_map 0x%x  n_ports %d\n",
210               probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
211
212         if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
213                 probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
214
215         for (i = 0; i < probe_ent->n_ports; i++) {
216                 if (!(port_map & (1 << i)))
217                         continue;
218                 probe_ent->port[i].port_mmio = ahci_port_base(mmio, i);
219                 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
220                 ahci_setup_port(&probe_ent->port[i], mmio, i);
221
222                 /* make sure port is not active */
223                 tmp = readl(port_mmio + PORT_CMD);
224                 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
225                            PORT_CMD_FIS_RX | PORT_CMD_START)) {
226                         debug("Port %d is active. Deactivating.\n", i);
227                         tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
228                                  PORT_CMD_FIS_RX | PORT_CMD_START);
229                         writel_with_flush(tmp, port_mmio + PORT_CMD);
230
231                         /* spec says 500 msecs for each bit, so
232                          * this is slightly incorrect.
233                          */
234                         msleep(500);
235                 }
236
237 #ifdef CONFIG_SUNXI_AHCI
238                 sunxi_dma_init(port_mmio);
239 #endif
240
241                 /* Add the spinup command to whatever mode bits may
242                  * already be on in the command register.
243                  */
244                 cmd = readl(port_mmio + PORT_CMD);
245                 cmd |= PORT_CMD_SPIN_UP;
246                 writel_with_flush(cmd, port_mmio + PORT_CMD);
247
248                 /* Bring up SATA link. */
249                 ret = ahci_link_up(probe_ent, i);
250                 if (ret) {
251                         printf("SATA link %d timeout.\n", i);
252                         continue;
253                 } else {
254                         debug("SATA link ok.\n");
255                 }
256
257                 /* Clear error status */
258                 tmp = readl(port_mmio + PORT_SCR_ERR);
259                 if (tmp)
260                         writel(tmp, port_mmio + PORT_SCR_ERR);
261
262                 debug("Spinning up device on SATA port %d... ", i);
263
264                 j = 0;
265                 while (j < WAIT_MS_SPINUP) {
266                         tmp = readl(port_mmio + PORT_TFDATA);
267                         if (!(tmp & (ATA_BUSY | ATA_DRQ)))
268                                 break;
269                         udelay(1000);
270                         tmp = readl(port_mmio + PORT_SCR_STAT);
271                         tmp &= PORT_SCR_STAT_DET_MASK;
272                         if (tmp == PORT_SCR_STAT_DET_PHYRDY)
273                                 break;
274                         j++;
275                 }
276
277                 tmp = readl(port_mmio + PORT_SCR_STAT) & PORT_SCR_STAT_DET_MASK;
278                 if (tmp == PORT_SCR_STAT_DET_COMINIT) {
279                         debug("SATA link %d down (COMINIT received), retrying...\n", i);
280                         i--;
281                         continue;
282                 }
283
284                 printf("Target spinup took %d ms.\n", j);
285                 if (j == WAIT_MS_SPINUP)
286                         debug("timeout.\n");
287                 else
288                         debug("ok.\n");
289
290                 tmp = readl(port_mmio + PORT_SCR_ERR);
291                 debug("PORT_SCR_ERR 0x%x\n", tmp);
292                 writel(tmp, port_mmio + PORT_SCR_ERR);
293
294                 /* ack any pending irq events for this port */
295                 tmp = readl(port_mmio + PORT_IRQ_STAT);
296                 debug("PORT_IRQ_STAT 0x%x\n", tmp);
297                 if (tmp)
298                         writel(tmp, port_mmio + PORT_IRQ_STAT);
299
300                 writel(1 << i, mmio + HOST_IRQ_STAT);
301
302                 /* set irq mask (enables interrupts) */
303                 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
304
305                 /* register linkup ports */
306                 tmp = readl(port_mmio + PORT_SCR_STAT);
307                 debug("SATA port %d status: 0x%x\n", i, tmp);
308                 if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
309                         probe_ent->link_port_map |= (0x01 << i);
310         }
311
312         tmp = readl(mmio + HOST_CTL);
313         debug("HOST_CTL 0x%x\n", tmp);
314         writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
315         tmp = readl(mmio + HOST_CTL);
316         debug("HOST_CTL 0x%x\n", tmp);
317 #ifndef CONFIG_SCSI_AHCI_PLAT
318         pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
319         tmp |= PCI_COMMAND_MASTER;
320         pci_write_config_word(pdev, PCI_COMMAND, tmp16);
321 #endif
322         return 0;
323 }
324
325
326 static void ahci_print_info(struct ahci_probe_ent *probe_ent)
327 {
328 #ifndef CONFIG_SCSI_AHCI_PLAT
329         pci_dev_t pdev = probe_ent->dev;
330         u16 cc;
331 #endif
332         void __iomem *mmio = probe_ent->mmio_base;
333         u32 vers, cap, cap2, impl, speed;
334         const char *speed_s;
335         const char *scc_s;
336
337         vers = readl(mmio + HOST_VERSION);
338         cap = probe_ent->cap;
339         cap2 = readl(mmio + HOST_CAP2);
340         impl = probe_ent->port_map;
341
342         speed = (cap >> 20) & 0xf;
343         if (speed == 1)
344                 speed_s = "1.5";
345         else if (speed == 2)
346                 speed_s = "3";
347         else if (speed == 3)
348                 speed_s = "6";
349         else
350                 speed_s = "?";
351
352 #ifdef CONFIG_SCSI_AHCI_PLAT
353         scc_s = "SATA";
354 #else
355         pci_read_config_word(pdev, 0x0a, &cc);
356         if (cc == 0x0101)
357                 scc_s = "IDE";
358         else if (cc == 0x0106)
359                 scc_s = "SATA";
360         else if (cc == 0x0104)
361                 scc_s = "RAID";
362         else
363                 scc_s = "unknown";
364 #endif
365         printf("AHCI %02x%02x.%02x%02x "
366                "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
367                (vers >> 24) & 0xff,
368                (vers >> 16) & 0xff,
369                (vers >> 8) & 0xff,
370                vers & 0xff,
371                ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
372
373         printf("flags: "
374                "%s%s%s%s%s%s%s"
375                "%s%s%s%s%s%s%s"
376                "%s%s%s%s%s%s\n",
377                cap & (1 << 31) ? "64bit " : "",
378                cap & (1 << 30) ? "ncq " : "",
379                cap & (1 << 28) ? "ilck " : "",
380                cap & (1 << 27) ? "stag " : "",
381                cap & (1 << 26) ? "pm " : "",
382                cap & (1 << 25) ? "led " : "",
383                cap & (1 << 24) ? "clo " : "",
384                cap & (1 << 19) ? "nz " : "",
385                cap & (1 << 18) ? "only " : "",
386                cap & (1 << 17) ? "pmp " : "",
387                cap & (1 << 16) ? "fbss " : "",
388                cap & (1 << 15) ? "pio " : "",
389                cap & (1 << 14) ? "slum " : "",
390                cap & (1 << 13) ? "part " : "",
391                cap & (1 << 7) ? "ccc " : "",
392                cap & (1 << 6) ? "ems " : "",
393                cap & (1 << 5) ? "sxs " : "",
394                cap2 & (1 << 2) ? "apst " : "",
395                cap2 & (1 << 1) ? "nvmp " : "",
396                cap2 & (1 << 0) ? "boh " : "");
397 }
398
399 #ifndef CONFIG_SCSI_AHCI_PLAT
400 static int ahci_init_one(pci_dev_t pdev)
401 {
402         u16 vendor;
403         int rc;
404
405         probe_ent = malloc(sizeof(struct ahci_probe_ent));
406         if (!probe_ent) {
407                 printf("%s: No memory for probe_ent\n", __func__);
408                 return -ENOMEM;
409         }
410
411         memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
412         probe_ent->dev = pdev;
413
414         probe_ent->host_flags = ATA_FLAG_SATA
415                                 | ATA_FLAG_NO_LEGACY
416                                 | ATA_FLAG_MMIO
417                                 | ATA_FLAG_PIO_DMA
418                                 | ATA_FLAG_NO_ATAPI;
419         probe_ent->pio_mask = 0x1f;
420         probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
421
422         probe_ent->mmio_base = pci_map_bar(pdev, PCI_BASE_ADDRESS_5,
423                                            PCI_REGION_MEM);
424         debug("ahci mmio_base=0x%p\n", probe_ent->mmio_base);
425
426         /* Take from kernel:
427          * JMicron-specific fixup:
428          * make sure we're in AHCI mode
429          */
430         pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
431         if (vendor == 0x197b)
432                 pci_write_config_byte(pdev, 0x41, 0xa1);
433
434         /* initialize adapter */
435         rc = ahci_host_init(probe_ent);
436         if (rc)
437                 goto err_out;
438
439         ahci_print_info(probe_ent);
440
441         return 0;
442
443       err_out:
444         return rc;
445 }
446 #endif
447
448 #define MAX_DATA_BYTE_COUNT  (4*1024*1024)
449
450 static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
451 {
452         struct ahci_ioports *pp = &(probe_ent->port[port]);
453         struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
454         u32 sg_count;
455         int i;
456
457         sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
458         if (sg_count > AHCI_MAX_SG) {
459                 printf("Error:Too much sg!\n");
460                 return -1;
461         }
462
463         for (i = 0; i < sg_count; i++) {
464                 ahci_sg->addr =
465                     cpu_to_le32((unsigned long) buf + i * MAX_DATA_BYTE_COUNT);
466                 ahci_sg->addr_hi = 0;
467                 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
468                                           (buf_len < MAX_DATA_BYTE_COUNT
469                                            ? (buf_len - 1)
470                                            : (MAX_DATA_BYTE_COUNT - 1)));
471                 ahci_sg++;
472                 buf_len -= MAX_DATA_BYTE_COUNT;
473         }
474
475         return sg_count;
476 }
477
478
479 static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
480 {
481         pp->cmd_slot->opts = cpu_to_le32(opts);
482         pp->cmd_slot->status = 0;
483         pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
484 #ifdef CONFIG_PHYS_64BIT
485         pp->cmd_slot->tbl_addr_hi =
486             cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
487 #endif
488 }
489
490
491 #ifdef CONFIG_AHCI_SETFEATURES_XFER
492 static void ahci_set_feature(u8 port)
493 {
494         struct ahci_ioports *pp = &(probe_ent->port[port]);
495         void __iomem *port_mmio = pp->port_mmio;
496         u32 cmd_fis_len = 5;    /* five dwords */
497         u8 fis[20];
498
499         /* set feature */
500         memset(fis, 0, sizeof(fis));
501         fis[0] = 0x27;
502         fis[1] = 1 << 7;
503         fis[2] = ATA_CMD_SET_FEATURES;
504         fis[3] = SETFEATURES_XFER;
505         fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
506
507         memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis));
508         ahci_fill_cmd_slot(pp, cmd_fis_len);
509         ahci_dcache_flush_sata_cmd(pp);
510         writel(1, port_mmio + PORT_CMD_ISSUE);
511         readl(port_mmio + PORT_CMD_ISSUE);
512
513         if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
514                                 WAIT_MS_DATAIO, 0x1)) {
515                 printf("set feature error on port %d!\n", port);
516         }
517 }
518 #endif
519
520 static int wait_spinup(void __iomem *port_mmio)
521 {
522         ulong start;
523         u32 tf_data;
524
525         start = get_timer(0);
526         do {
527                 tf_data = readl(port_mmio + PORT_TFDATA);
528                 if (!(tf_data & ATA_BUSY))
529                         return 0;
530         } while (get_timer(start) < WAIT_MS_SPINUP);
531
532         return -ETIMEDOUT;
533 }
534
535 static int ahci_port_start(u8 port)
536 {
537         struct ahci_ioports *pp = &(probe_ent->port[port]);
538         void __iomem *port_mmio = pp->port_mmio;
539         u32 port_status;
540         void __iomem *mem;
541
542         debug("Enter start port: %d\n", port);
543         port_status = readl(port_mmio + PORT_SCR_STAT);
544         debug("Port %d status: %x\n", port, port_status);
545         if ((port_status & 0xf) != 0x03) {
546                 printf("No Link on this port!\n");
547                 return -1;
548         }
549
550         mem = malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
551         if (!mem) {
552                 free(pp);
553                 printf("%s: No mem for table!\n", __func__);
554                 return -ENOMEM;
555         }
556
557         /* Aligned to 2048-bytes */
558         mem = memalign(2048, AHCI_PORT_PRIV_DMA_SZ);
559         memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
560
561         /*
562          * First item in chunk of DMA memory: 32-slot command table,
563          * 32 bytes each in size
564          */
565         pp->cmd_slot =
566                 (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
567         debug("cmd_slot = %p\n", pp->cmd_slot);
568         mem += (AHCI_CMD_SLOT_SZ + 224);
569
570         /*
571          * Second item: Received-FIS area
572          */
573         pp->rx_fis = virt_to_phys((void *)mem);
574         mem += AHCI_RX_FIS_SZ;
575
576         /*
577          * Third item: data area for storing a single command
578          * and its scatter-gather table
579          */
580         pp->cmd_tbl = virt_to_phys((void *)mem);
581         debug("cmd_tbl_dma = %lx\n", pp->cmd_tbl);
582
583         mem += AHCI_CMD_TBL_HDR;
584         pp->cmd_tbl_sg =
585                         (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
586
587         writel_with_flush((unsigned long)pp->cmd_slot,
588                           port_mmio + PORT_LST_ADDR);
589
590         writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
591
592 #ifdef CONFIG_SUNXI_AHCI
593         sunxi_dma_init(port_mmio);
594 #endif
595
596         writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
597                           PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
598                           PORT_CMD_START, port_mmio + PORT_CMD);
599
600         debug("Exit start port %d\n", port);
601
602         /*
603          * Make sure interface is not busy based on error and status
604          * information from task file data register before proceeding
605          */
606         return wait_spinup(port_mmio);
607 }
608
609
610 static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf,
611                                 int buf_len, u8 is_write)
612 {
613
614         struct ahci_ioports *pp = &(probe_ent->port[port]);
615         void __iomem *port_mmio = pp->port_mmio;
616         u32 opts;
617         u32 port_status;
618         int sg_count;
619
620         debug("Enter %s: for port %d\n", __func__, port);
621
622         if (port > probe_ent->n_ports) {
623                 printf("Invalid port number %d\n", port);
624                 return -1;
625         }
626
627         port_status = readl(port_mmio + PORT_SCR_STAT);
628         if ((port_status & 0xf) != 0x03) {
629                 debug("No Link on port %d!\n", port);
630                 return -1;
631         }
632
633         memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
634
635         sg_count = ahci_fill_sg(port, buf, buf_len);
636         opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
637         ahci_fill_cmd_slot(pp, opts);
638
639         ahci_dcache_flush_sata_cmd(pp);
640         ahci_dcache_flush_range((unsigned long)buf, (unsigned long)buf_len);
641
642         writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
643
644         if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
645                                 WAIT_MS_DATAIO, 0x1)) {
646                 printf("timeout exit!\n");
647                 return -1;
648         }
649
650         ahci_dcache_invalidate_range((unsigned long)buf,
651                                      (unsigned long)buf_len);
652         debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
653
654         return 0;
655 }
656
657
658 static char *ata_id_strcpy(u16 *target, u16 *src, int len)
659 {
660         int i;
661         for (i = 0; i < len / 2; i++)
662                 target[i] = swab16(src[i]);
663         return (char *)target;
664 }
665
666 /*
667  * SCSI INQUIRY command operation.
668  */
669 static int ata_scsiop_inquiry(ccb *pccb)
670 {
671         static const u8 hdr[] = {
672                 0,
673                 0,
674                 0x5,            /* claim SPC-3 version compatibility */
675                 2,
676                 95 - 4,
677         };
678         u8 fis[20];
679         u16 *idbuf;
680         ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
681         u8 port;
682
683         /* Clean ccb data buffer */
684         memset(pccb->pdata, 0, pccb->datalen);
685
686         memcpy(pccb->pdata, hdr, sizeof(hdr));
687
688         if (pccb->datalen <= 35)
689                 return 0;
690
691         memset(fis, 0, sizeof(fis));
692         /* Construct the FIS */
693         fis[0] = 0x27;          /* Host to device FIS. */
694         fis[1] = 1 << 7;        /* Command FIS. */
695         fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
696
697         /* Read id from sata */
698         port = pccb->target;
699
700         if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), (u8 *)tmpid,
701                                 ATA_ID_WORDS * 2, 0)) {
702                 debug("scsi_ahci: SCSI inquiry command failure.\n");
703                 return -EIO;
704         }
705
706         if (!ataid[port]) {
707                 ataid[port] = malloc(ATA_ID_WORDS * 2);
708                 if (!ataid[port]) {
709                         printf("%s: No memory for ataid[port]\n", __func__);
710                         return -ENOMEM;
711                 }
712         }
713
714         idbuf = ataid[port];
715
716         memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
717         ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
718
719         memcpy(&pccb->pdata[8], "ATA     ", 8);
720         ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
721         ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
722
723 #ifdef DEBUG
724         ata_dump_id(idbuf);
725 #endif
726         return 0;
727 }
728
729
730 /*
731  * SCSI READ10/WRITE10 command operation.
732  */
733 static int ata_scsiop_read_write(ccb *pccb, u8 is_write)
734 {
735         lbaint_t lba = 0;
736         u16 blocks = 0;
737         u8 fis[20];
738         u8 *user_buffer = pccb->pdata;
739         u32 user_buffer_size = pccb->datalen;
740
741         /* Retrieve the base LBA number from the ccb structure. */
742         if (pccb->cmd[0] == SCSI_READ16) {
743                 memcpy(&lba, pccb->cmd + 2, 8);
744                 lba = be64_to_cpu(lba);
745         } else {
746                 u32 temp;
747                 memcpy(&temp, pccb->cmd + 2, 4);
748                 lba = be32_to_cpu(temp);
749         }
750
751         /*
752          * Retrieve the base LBA number and the block count from
753          * the ccb structure.
754          *
755          * For 10-byte and 16-byte SCSI R/W commands, transfer
756          * length 0 means transfer 0 block of data.
757          * However, for ATA R/W commands, sector count 0 means
758          * 256 or 65536 sectors, not 0 sectors as in SCSI.
759          *
760          * WARNING: one or two older ATA drives treat 0 as 0...
761          */
762         if (pccb->cmd[0] == SCSI_READ16)
763                 blocks = (((u16)pccb->cmd[13]) << 8) | ((u16) pccb->cmd[14]);
764         else
765                 blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
766
767         debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "\n",
768               is_write ?  "write" : "read", blocks, lba);
769
770         /* Preset the FIS */
771         memset(fis, 0, sizeof(fis));
772         fis[0] = 0x27;           /* Host to device FIS. */
773         fis[1] = 1 << 7;         /* Command FIS. */
774         /* Command byte (read/write). */
775         fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
776
777         while (blocks) {
778                 u16 now_blocks; /* number of blocks per iteration */
779                 u32 transfer_size; /* number of bytes per iteration */
780
781                 now_blocks = min((u16)MAX_SATA_BLOCKS_READ_WRITE, blocks);
782
783                 transfer_size = ATA_SECT_SIZE * now_blocks;
784                 if (transfer_size > user_buffer_size) {
785                         printf("scsi_ahci: Error: buffer too small.\n");
786                         return -EIO;
787                 }
788
789                 /*
790                  * LBA48 SATA command but only use 32bit address range within
791                  * that (unless we've enabled 64bit LBA support). The next
792                  * smaller command range (28bit) is too small.
793                  */
794                 fis[4] = (lba >> 0) & 0xff;
795                 fis[5] = (lba >> 8) & 0xff;
796                 fis[6] = (lba >> 16) & 0xff;
797                 fis[7] = 1 << 6; /* device reg: set LBA mode */
798                 fis[8] = ((lba >> 24) & 0xff);
799 #ifdef CONFIG_SYS_64BIT_LBA
800                 if (pccb->cmd[0] == SCSI_READ16) {
801                         fis[9] = ((lba >> 32) & 0xff);
802                         fis[10] = ((lba >> 40) & 0xff);
803                 }
804 #endif
805
806                 fis[3] = 0xe0; /* features */
807
808                 /* Block (sector) count */
809                 fis[12] = (now_blocks >> 0) & 0xff;
810                 fis[13] = (now_blocks >> 8) & 0xff;
811
812                 /* Read/Write from ahci */
813                 if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis),
814                                         user_buffer, transfer_size,
815                                         is_write)) {
816                         debug("scsi_ahci: SCSI %s10 command failure.\n",
817                               is_write ? "WRITE" : "READ");
818                         return -EIO;
819                 }
820
821                 /* If this transaction is a write, do a following flush.
822                  * Writes in u-boot are so rare, and the logic to know when is
823                  * the last write and do a flush only there is sufficiently
824                  * difficult. Just do a flush after every write. This incurs,
825                  * usually, one extra flush when the rare writes do happen.
826                  */
827                 if (is_write) {
828                         if (-EIO == ata_io_flush(pccb->target))
829                                 return -EIO;
830                 }
831                 user_buffer += transfer_size;
832                 user_buffer_size -= transfer_size;
833                 blocks -= now_blocks;
834                 lba += now_blocks;
835         }
836
837         return 0;
838 }
839
840
841 /*
842  * SCSI READ CAPACITY10 command operation.
843  */
844 static int ata_scsiop_read_capacity10(ccb *pccb)
845 {
846         u32 cap;
847         u64 cap64;
848         u32 block_size;
849
850         if (!ataid[pccb->target]) {
851                 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
852                        "\tNo ATA info!\n"
853                        "\tPlease run SCSI commmand INQUIRY firstly!\n");
854                 return -EPERM;
855         }
856
857         cap64 = ata_id_n_sectors(ataid[pccb->target]);
858         if (cap64 > 0x100000000ULL)
859                 cap64 = 0xffffffff;
860
861         cap = cpu_to_be32(cap64);
862         memcpy(pccb->pdata, &cap, sizeof(cap));
863
864         block_size = cpu_to_be32((u32)512);
865         memcpy(&pccb->pdata[4], &block_size, 4);
866
867         return 0;
868 }
869
870
871 /*
872  * SCSI READ CAPACITY16 command operation.
873  */
874 static int ata_scsiop_read_capacity16(ccb *pccb)
875 {
876         u64 cap;
877         u64 block_size;
878
879         if (!ataid[pccb->target]) {
880                 printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
881                        "\tNo ATA info!\n"
882                        "\tPlease run SCSI commmand INQUIRY firstly!\n");
883                 return -EPERM;
884         }
885
886         cap = ata_id_n_sectors(ataid[pccb->target]);
887         cap = cpu_to_be64(cap);
888         memcpy(pccb->pdata, &cap, sizeof(cap));
889
890         block_size = cpu_to_be64((u64)512);
891         memcpy(&pccb->pdata[8], &block_size, 8);
892
893         return 0;
894 }
895
896
897 /*
898  * SCSI TEST UNIT READY command operation.
899  */
900 static int ata_scsiop_test_unit_ready(ccb *pccb)
901 {
902         return (ataid[pccb->target]) ? 0 : -EPERM;
903 }
904
905
906 int scsi_exec(ccb *pccb)
907 {
908         int ret;
909
910         switch (pccb->cmd[0]) {
911         case SCSI_READ16:
912         case SCSI_READ10:
913                 ret = ata_scsiop_read_write(pccb, 0);
914                 break;
915         case SCSI_WRITE10:
916                 ret = ata_scsiop_read_write(pccb, 1);
917                 break;
918         case SCSI_RD_CAPAC10:
919                 ret = ata_scsiop_read_capacity10(pccb);
920                 break;
921         case SCSI_RD_CAPAC16:
922                 ret = ata_scsiop_read_capacity16(pccb);
923                 break;
924         case SCSI_TST_U_RDY:
925                 ret = ata_scsiop_test_unit_ready(pccb);
926                 break;
927         case SCSI_INQUIRY:
928                 ret = ata_scsiop_inquiry(pccb);
929                 break;
930         default:
931                 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
932                 return false;
933         }
934
935         if (ret) {
936                 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
937                 return false;
938         }
939         return true;
940
941 }
942
943
944 void scsi_low_level_init(int busdevfunc)
945 {
946         int i;
947         u32 linkmap;
948
949 #ifndef CONFIG_SCSI_AHCI_PLAT
950         ahci_init_one(busdevfunc);
951 #endif
952
953         linkmap = probe_ent->link_port_map;
954
955         for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
956                 if (((linkmap >> i) & 0x01)) {
957                         if (ahci_port_start((u8) i)) {
958                                 printf("Can not start port %d\n", i);
959                                 continue;
960                         }
961 #ifdef CONFIG_AHCI_SETFEATURES_XFER
962                         ahci_set_feature((u8) i);
963 #endif
964                 }
965         }
966 }
967
968 #ifdef CONFIG_SCSI_AHCI_PLAT
969 int ahci_init(void __iomem *base)
970 {
971         int i, rc = 0;
972         u32 linkmap;
973
974         probe_ent = malloc(sizeof(struct ahci_probe_ent));
975         if (!probe_ent) {
976                 printf("%s: No memory for probe_ent\n", __func__);
977                 return -ENOMEM;
978         }
979
980         memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
981
982         probe_ent->host_flags = ATA_FLAG_SATA
983                                 | ATA_FLAG_NO_LEGACY
984                                 | ATA_FLAG_MMIO
985                                 | ATA_FLAG_PIO_DMA
986                                 | ATA_FLAG_NO_ATAPI;
987         probe_ent->pio_mask = 0x1f;
988         probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
989
990         probe_ent->mmio_base = base;
991
992         /* initialize adapter */
993         rc = ahci_host_init(probe_ent);
994         if (rc)
995                 goto err_out;
996
997         ahci_print_info(probe_ent);
998
999         linkmap = probe_ent->link_port_map;
1000
1001         for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
1002                 if (((linkmap >> i) & 0x01)) {
1003                         if (ahci_port_start((u8) i)) {
1004                                 printf("Can not start port %d\n", i);
1005                                 continue;
1006                         }
1007 #ifdef CONFIG_AHCI_SETFEATURES_XFER
1008                         ahci_set_feature((u8) i);
1009 #endif
1010                 }
1011         }
1012 err_out:
1013         return rc;
1014 }
1015
1016 void __weak scsi_init(void)
1017 {
1018 }
1019
1020 #endif
1021
1022 /*
1023  * In the general case of generic rotating media it makes sense to have a
1024  * flush capability. It probably even makes sense in the case of SSDs because
1025  * one cannot always know for sure what kind of internal cache/flush mechanism
1026  * is embodied therein. At first it was planned to invoke this after the last
1027  * write to disk and before rebooting. In practice, knowing, a priori, which
1028  * is the last write is difficult. Because writing to the disk in u-boot is
1029  * very rare, this flush command will be invoked after every block write.
1030  */
1031 static int ata_io_flush(u8 port)
1032 {
1033         u8 fis[20];
1034         struct ahci_ioports *pp = &(probe_ent->port[port]);
1035         void __iomem *port_mmio = pp->port_mmio;
1036         u32 cmd_fis_len = 5;    /* five dwords */
1037
1038         /* Preset the FIS */
1039         memset(fis, 0, 20);
1040         fis[0] = 0x27;           /* Host to device FIS. */
1041         fis[1] = 1 << 7;         /* Command FIS. */
1042         fis[2] = ATA_CMD_FLUSH_EXT;
1043
1044         memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
1045         ahci_fill_cmd_slot(pp, cmd_fis_len);
1046         writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
1047
1048         if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
1049                         WAIT_MS_FLUSH, 0x1)) {
1050                 debug("scsi_ahci: flush command timeout on port %d.\n", port);
1051                 return -EIO;
1052         }
1053
1054         return 0;
1055 }
1056
1057
1058 __weak void scsi_bus_reset(void)
1059 {
1060         /*Not implement*/
1061 }
1062
1063 void scsi_print_error(ccb * pccb)
1064 {
1065         /*The ahci error info can be read in the ahci driver*/
1066 }