]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/block/ahci.c
ahci: flush / invalidate dcache around SATA commands
[karo-tx-uboot.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  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  *
24  * with the reference on libata and ahci drvier in kernel
25  *
26  */
27 #include <common.h>
28
29 #include <command.h>
30 #include <pci.h>
31 #include <asm/processor.h>
32 #include <asm/errno.h>
33 #include <asm/io.h>
34 #include <malloc.h>
35 #include <scsi.h>
36 #include <ata.h>
37 #include <linux/ctype.h>
38 #include <ahci.h>
39
40 struct ahci_probe_ent *probe_ent = NULL;
41 hd_driveid_t *ataid[AHCI_MAX_PORTS];
42
43 #define writel_with_flush(a,b)  do { writel(a,b); readl(b); } while (0)
44
45 /*
46  * Some controllers limit number of blocks they can read/write at once.
47  * Contemporary SSD devices work much faster if the read/write size is aligned
48  * to a power of 2.  Let's set default to 128 and allowing to be overwritten if
49  * needed.
50  */
51 #ifndef MAX_SATA_BLOCKS_READ_WRITE
52 #define MAX_SATA_BLOCKS_READ_WRITE      0x80
53 #endif
54
55 static inline u32 ahci_port_base(u32 base, u32 port)
56 {
57         return base + 0x100 + (port * 0x80);
58 }
59
60
61 static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
62                             unsigned int port_idx)
63 {
64         base = ahci_port_base(base, port_idx);
65
66         port->cmd_addr = base;
67         port->scr_addr = base + PORT_SCR;
68 }
69
70
71 #define msleep(a) udelay(a * 1000)
72
73 static void ahci_dcache_flush_range(unsigned begin, unsigned len)
74 {
75         const unsigned long start = begin;
76         const unsigned long end = start + len;
77
78         debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
79         flush_dcache_range(start, end);
80 }
81
82 /*
83  * SATA controller DMAs to physical RAM.  Ensure data from the
84  * controller is invalidated from dcache; next access comes from
85  * physical RAM.
86  */
87 static void ahci_dcache_invalidate_range(unsigned begin, unsigned len)
88 {
89         const unsigned long start = begin;
90         const unsigned long end = start + len;
91
92         debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
93         invalidate_dcache_range(start, end);
94 }
95
96 /*
97  * Ensure data for SATA controller is flushed out of dcache and
98  * written to physical memory.
99  */
100 static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
101 {
102         ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
103                                 AHCI_PORT_PRIV_DMA_SZ);
104 }
105
106 static int waiting_for_cmd_completed(volatile u8 *offset,
107                                      int timeout_msec,
108                                      u32 sign)
109 {
110         int i;
111         u32 status;
112
113         for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
114                 msleep(1);
115
116         return (i < timeout_msec) ? 0 : -1;
117 }
118
119
120 static int ahci_host_init(struct ahci_probe_ent *probe_ent)
121 {
122 #ifndef CONFIG_SCSI_AHCI_PLAT
123         pci_dev_t pdev = probe_ent->dev;
124         u16 tmp16;
125         unsigned short vendor;
126 #endif
127         volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
128         u32 tmp, cap_save;
129         int i, j;
130         volatile u8 *port_mmio;
131
132         debug("ahci_host_init: start\n");
133
134         cap_save = readl(mmio + HOST_CAP);
135         cap_save &= ((1 << 28) | (1 << 17));
136         cap_save |= (1 << 27);
137
138         /* global controller reset */
139         tmp = readl(mmio + HOST_CTL);
140         if ((tmp & HOST_RESET) == 0)
141                 writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
142
143         /* reset must complete within 1 second, or
144          * the hardware should be considered fried.
145          */
146         i = 1000;
147         do {
148                 udelay(1000);
149                 tmp = readl(mmio + HOST_CTL);
150                 if (!i--) {
151                         debug("controller reset failed (0x%x)\n", tmp);
152                         return -1;
153                 }
154         } while (tmp & HOST_RESET);
155
156         writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
157         writel(cap_save, mmio + HOST_CAP);
158         writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
159
160 #ifndef CONFIG_SCSI_AHCI_PLAT
161         pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
162
163         if (vendor == PCI_VENDOR_ID_INTEL) {
164                 u16 tmp16;
165                 pci_read_config_word(pdev, 0x92, &tmp16);
166                 tmp16 |= 0xf;
167                 pci_write_config_word(pdev, 0x92, tmp16);
168         }
169 #endif
170         probe_ent->cap = readl(mmio + HOST_CAP);
171         probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
172         probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
173
174         debug("cap 0x%x  port_map 0x%x  n_ports %d\n",
175               probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
176
177         if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
178                 probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
179
180         for (i = 0; i < probe_ent->n_ports; i++) {
181                 probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
182                 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
183                 ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
184
185                 /* make sure port is not active */
186                 tmp = readl(port_mmio + PORT_CMD);
187                 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
188                            PORT_CMD_FIS_RX | PORT_CMD_START)) {
189                         debug("Port %d is active. Deactivating.\n", i);
190                         tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
191                                  PORT_CMD_FIS_RX | PORT_CMD_START);
192                         writel_with_flush(tmp, port_mmio + PORT_CMD);
193
194                         /* spec says 500 msecs for each bit, so
195                          * this is slightly incorrect.
196                          */
197                         msleep(500);
198                 }
199
200                 debug("Spinning up port %d... ", i);
201                 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
202
203                 j = 0;
204                 while (j < 1000) {
205                         tmp = readl(port_mmio + PORT_SCR_STAT);
206                         if ((tmp & 0xf) == 0x3)
207                                 break;
208                         udelay(1000);
209                         j++;
210                 }
211                 if (j == 1000)
212                         debug("timeout.\n");
213                 else
214                         debug("ok.\n");
215
216                 tmp = readl(port_mmio + PORT_SCR_ERR);
217                 debug("PORT_SCR_ERR 0x%x\n", tmp);
218                 writel(tmp, port_mmio + PORT_SCR_ERR);
219
220                 /* ack any pending irq events for this port */
221                 tmp = readl(port_mmio + PORT_IRQ_STAT);
222                 debug("PORT_IRQ_STAT 0x%x\n", tmp);
223                 if (tmp)
224                         writel(tmp, port_mmio + PORT_IRQ_STAT);
225
226                 writel(1 << i, mmio + HOST_IRQ_STAT);
227
228                 /* set irq mask (enables interrupts) */
229                 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
230
231                 /* register linkup ports */
232                 tmp = readl(port_mmio + PORT_SCR_STAT);
233                 debug("Port %d status: 0x%x\n", i, tmp);
234                 if ((tmp & 0xf) == 0x03)
235                         probe_ent->link_port_map |= (0x01 << i);
236         }
237
238         tmp = readl(mmio + HOST_CTL);
239         debug("HOST_CTL 0x%x\n", tmp);
240         writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
241         tmp = readl(mmio + HOST_CTL);
242         debug("HOST_CTL 0x%x\n", tmp);
243 #ifndef CONFIG_SCSI_AHCI_PLAT
244         pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
245         tmp |= PCI_COMMAND_MASTER;
246         pci_write_config_word(pdev, PCI_COMMAND, tmp16);
247 #endif
248         return 0;
249 }
250
251
252 static void ahci_print_info(struct ahci_probe_ent *probe_ent)
253 {
254 #ifndef CONFIG_SCSI_AHCI_PLAT
255         pci_dev_t pdev = probe_ent->dev;
256         u16 cc;
257 #endif
258         volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
259         u32 vers, cap, cap2, impl, speed;
260         const char *speed_s;
261         const char *scc_s;
262
263         vers = readl(mmio + HOST_VERSION);
264         cap = probe_ent->cap;
265         cap2 = readl(mmio + HOST_CAP2);
266         impl = probe_ent->port_map;
267
268         speed = (cap >> 20) & 0xf;
269         if (speed == 1)
270                 speed_s = "1.5";
271         else if (speed == 2)
272                 speed_s = "3";
273         else if (speed == 3)
274                 speed_s = "6";
275         else
276                 speed_s = "?";
277
278 #ifdef CONFIG_SCSI_AHCI_PLAT
279         scc_s = "SATA";
280 #else
281         pci_read_config_word(pdev, 0x0a, &cc);
282         if (cc == 0x0101)
283                 scc_s = "IDE";
284         else if (cc == 0x0106)
285                 scc_s = "SATA";
286         else if (cc == 0x0104)
287                 scc_s = "RAID";
288         else
289                 scc_s = "unknown";
290 #endif
291         printf("AHCI %02x%02x.%02x%02x "
292                "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
293                (vers >> 24) & 0xff,
294                (vers >> 16) & 0xff,
295                (vers >> 8) & 0xff,
296                vers & 0xff,
297                ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
298
299         printf("flags: "
300                "%s%s%s%s%s%s%s"
301                "%s%s%s%s%s%s%s"
302                "%s%s%s%s%s%s\n",
303                cap & (1 << 31) ? "64bit " : "",
304                cap & (1 << 30) ? "ncq " : "",
305                cap & (1 << 28) ? "ilck " : "",
306                cap & (1 << 27) ? "stag " : "",
307                cap & (1 << 26) ? "pm " : "",
308                cap & (1 << 25) ? "led " : "",
309                cap & (1 << 24) ? "clo " : "",
310                cap & (1 << 19) ? "nz " : "",
311                cap & (1 << 18) ? "only " : "",
312                cap & (1 << 17) ? "pmp " : "",
313                cap & (1 << 16) ? "fbss " : "",
314                cap & (1 << 15) ? "pio " : "",
315                cap & (1 << 14) ? "slum " : "",
316                cap & (1 << 13) ? "part " : "",
317                cap & (1 << 7) ? "ccc " : "",
318                cap & (1 << 6) ? "ems " : "",
319                cap & (1 << 5) ? "sxs " : "",
320                cap2 & (1 << 2) ? "apst " : "",
321                cap2 & (1 << 1) ? "nvmp " : "",
322                cap2 & (1 << 0) ? "boh " : "");
323 }
324
325 #ifndef CONFIG_SCSI_AHCI_PLAT
326 static int ahci_init_one(pci_dev_t pdev)
327 {
328         u16 vendor;
329         int rc;
330
331         memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
332
333         probe_ent = malloc(sizeof(struct ahci_probe_ent));
334         memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
335         probe_ent->dev = pdev;
336
337         probe_ent->host_flags = ATA_FLAG_SATA
338                                 | ATA_FLAG_NO_LEGACY
339                                 | ATA_FLAG_MMIO
340                                 | ATA_FLAG_PIO_DMA
341                                 | ATA_FLAG_NO_ATAPI;
342         probe_ent->pio_mask = 0x1f;
343         probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
344
345         pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base);
346         debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base);
347
348         /* Take from kernel:
349          * JMicron-specific fixup:
350          * make sure we're in AHCI mode
351          */
352         pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
353         if (vendor == 0x197b)
354                 pci_write_config_byte(pdev, 0x41, 0xa1);
355
356         /* initialize adapter */
357         rc = ahci_host_init(probe_ent);
358         if (rc)
359                 goto err_out;
360
361         ahci_print_info(probe_ent);
362
363         return 0;
364
365       err_out:
366         return rc;
367 }
368 #endif
369
370 #define MAX_DATA_BYTE_COUNT  (4*1024*1024)
371
372 static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
373 {
374         struct ahci_ioports *pp = &(probe_ent->port[port]);
375         struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
376         u32 sg_count;
377         int i;
378
379         sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
380         if (sg_count > AHCI_MAX_SG) {
381                 printf("Error:Too much sg!\n");
382                 return -1;
383         }
384
385         for (i = 0; i < sg_count; i++) {
386                 ahci_sg->addr =
387                     cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
388                 ahci_sg->addr_hi = 0;
389                 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
390                                           (buf_len < MAX_DATA_BYTE_COUNT
391                                            ? (buf_len - 1)
392                                            : (MAX_DATA_BYTE_COUNT - 1)));
393                 ahci_sg++;
394                 buf_len -= MAX_DATA_BYTE_COUNT;
395         }
396
397         return sg_count;
398 }
399
400
401 static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
402 {
403         pp->cmd_slot->opts = cpu_to_le32(opts);
404         pp->cmd_slot->status = 0;
405         pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
406         pp->cmd_slot->tbl_addr_hi = 0;
407 }
408
409
410 #ifdef CONFIG_AHCI_SETFEATURES_XFER
411 static void ahci_set_feature(u8 port)
412 {
413         struct ahci_ioports *pp = &(probe_ent->port[port]);
414         volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
415         u32 cmd_fis_len = 5;    /* five dwords */
416         u8 fis[20];
417
418         /* set feature */
419         memset(fis, 0, sizeof(fis));
420         fis[0] = 0x27;
421         fis[1] = 1 << 7;
422         fis[2] = ATA_CMD_SETF;
423         fis[3] = SETFEATURES_XFER;
424         fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
425
426         memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis));
427         ahci_fill_cmd_slot(pp, cmd_fis_len);
428         ahci_dcache_flush_sata_cmd(pp);
429         writel(1, port_mmio + PORT_CMD_ISSUE);
430         readl(port_mmio + PORT_CMD_ISSUE);
431
432         if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
433                 printf("set feature error on port %d!\n", port);
434         }
435 }
436 #endif
437
438
439 static int ahci_port_start(u8 port)
440 {
441         struct ahci_ioports *pp = &(probe_ent->port[port]);
442         volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
443         u32 port_status;
444         u32 mem;
445
446         debug("Enter start port: %d\n", port);
447         port_status = readl(port_mmio + PORT_SCR_STAT);
448         debug("Port %d status: %x\n", port, port_status);
449         if ((port_status & 0xf) != 0x03) {
450                 printf("No Link on this port!\n");
451                 return -1;
452         }
453
454         mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
455         if (!mem) {
456                 free(pp);
457                 printf("No mem for table!\n");
458                 return -ENOMEM;
459         }
460
461         mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
462         memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
463
464         /*
465          * First item in chunk of DMA memory: 32-slot command table,
466          * 32 bytes each in size
467          */
468         pp->cmd_slot =
469                 (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
470         debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot);
471         mem += (AHCI_CMD_SLOT_SZ + 224);
472
473         /*
474          * Second item: Received-FIS area
475          */
476         pp->rx_fis = virt_to_phys((void *)mem);
477         mem += AHCI_RX_FIS_SZ;
478
479         /*
480          * Third item: data area for storing a single command
481          * and its scatter-gather table
482          */
483         pp->cmd_tbl = virt_to_phys((void *)mem);
484         debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
485
486         mem += AHCI_CMD_TBL_HDR;
487         pp->cmd_tbl_sg =
488                         (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
489
490         writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
491
492         writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
493
494         writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
495                           PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
496                           PORT_CMD_START, port_mmio + PORT_CMD);
497
498         debug("Exit start port %d\n", port);
499
500         return 0;
501 }
502
503
504 static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf,
505                                 int buf_len, u8 is_write)
506 {
507
508         struct ahci_ioports *pp = &(probe_ent->port[port]);
509         volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
510         u32 opts;
511         u32 port_status;
512         int sg_count;
513
514         debug("Enter %s: for port %d\n", __func__, port);
515
516         if (port > probe_ent->n_ports) {
517                 printf("Invalid port number %d\n", port);
518                 return -1;
519         }
520
521         port_status = readl(port_mmio + PORT_SCR_STAT);
522         if ((port_status & 0xf) != 0x03) {
523                 debug("No Link on port %d!\n", port);
524                 return -1;
525         }
526
527         memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
528
529         sg_count = ahci_fill_sg(port, buf, buf_len);
530         opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
531         ahci_fill_cmd_slot(pp, opts);
532
533         ahci_dcache_flush_sata_cmd(pp);
534         ahci_dcache_flush_range((unsigned)buf, (unsigned)buf_len);
535
536         writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
537
538         if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
539                 printf("timeout exit!\n");
540                 return -1;
541         }
542
543         ahci_dcache_invalidate_range((unsigned)buf, (unsigned)buf_len);
544         debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
545
546         return 0;
547 }
548
549
550 static char *ata_id_strcpy(u16 *target, u16 *src, int len)
551 {
552         int i;
553         for (i = 0; i < len / 2; i++)
554                 target[i] = swab16(src[i]);
555         return (char *)target;
556 }
557
558
559 static void dump_ataid(hd_driveid_t *ataid)
560 {
561         debug("(49)ataid->capability = 0x%x\n", ataid->capability);
562         debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
563         debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
564         debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
565         debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
566         debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
567         debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
568         debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
569         debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
570         debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
571         debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
572         debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
573         debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
574         debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
575         debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
576 }
577
578
579 /*
580  * SCSI INQUIRY command operation.
581  */
582 static int ata_scsiop_inquiry(ccb *pccb)
583 {
584         u8 hdr[] = {
585                 0,
586                 0,
587                 0x5,            /* claim SPC-3 version compatibility */
588                 2,
589                 95 - 4,
590         };
591         u8 fis[20];
592         u8 *tmpid;
593         u8 port;
594
595         /* Clean ccb data buffer */
596         memset(pccb->pdata, 0, pccb->datalen);
597
598         memcpy(pccb->pdata, hdr, sizeof(hdr));
599
600         if (pccb->datalen <= 35)
601                 return 0;
602
603         memset(fis, 0, sizeof(fis));
604         /* Construct the FIS */
605         fis[0] = 0x27;          /* Host to device FIS. */
606         fis[1] = 1 << 7;        /* Command FIS. */
607         fis[2] = ATA_CMD_IDENT; /* Command byte. */
608
609         /* Read id from sata */
610         port = pccb->target;
611         if (!(tmpid = malloc(sizeof(hd_driveid_t))))
612                 return -ENOMEM;
613
614         if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), tmpid,
615                                 sizeof(hd_driveid_t), 0)) {
616                 debug("scsi_ahci: SCSI inquiry command failure.\n");
617                 return -EIO;
618         }
619
620         if (ataid[port])
621                 free(ataid[port]);
622         ataid[port] = (hd_driveid_t *) tmpid;
623
624         memcpy(&pccb->pdata[8], "ATA     ", 8);
625         ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
626         ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
627
628         dump_ataid(ataid[port]);
629         return 0;
630 }
631
632
633 /*
634  * SCSI READ10/WRITE10 command operation.
635  */
636 static int ata_scsiop_read_write(ccb *pccb, u8 is_write)
637 {
638         u32 lba = 0;
639         u16 blocks = 0;
640         u8 fis[20];
641         u8 *user_buffer = pccb->pdata;
642         u32 user_buffer_size = pccb->datalen;
643
644         /* Retrieve the base LBA number from the ccb structure. */
645         memcpy(&lba, pccb->cmd + 2, sizeof(lba));
646         lba = be32_to_cpu(lba);
647
648         /*
649          * And the number of blocks.
650          *
651          * For 10-byte and 16-byte SCSI R/W commands, transfer
652          * length 0 means transfer 0 block of data.
653          * However, for ATA R/W commands, sector count 0 means
654          * 256 or 65536 sectors, not 0 sectors as in SCSI.
655          *
656          * WARNING: one or two older ATA drives treat 0 as 0...
657          */
658         blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
659
660         debug("scsi_ahci: %s %d blocks starting from lba 0x%x\n",
661               is_write ?  "write" : "read", (unsigned)lba, blocks);
662
663         /* Preset the FIS */
664         memset(fis, 0, sizeof(fis));
665         fis[0] = 0x27;           /* Host to device FIS. */
666         fis[1] = 1 << 7;         /* Command FIS. */
667         /* Command byte (read/write). */
668         fis[2] = is_write ? ATA_CMD_WR_DMA : ATA_CMD_RD_DMA;
669
670         while (blocks) {
671                 u16 now_blocks; /* number of blocks per iteration */
672                 u32 transfer_size; /* number of bytes per iteration */
673
674                 now_blocks = min(MAX_SATA_BLOCKS_READ_WRITE, blocks);
675
676                 transfer_size = ATA_BLOCKSIZE * now_blocks;
677                 if (transfer_size > user_buffer_size) {
678                         printf("scsi_ahci: Error: buffer too small.\n");
679                         return -EIO;
680                 }
681
682                 /* LBA address, only support LBA28 in this driver */
683                 fis[4] = (lba >> 0) & 0xff;
684                 fis[5] = (lba >> 8) & 0xff;
685                 fis[6] = (lba >> 16) & 0xff;
686                 fis[7] = ((lba >> 24) & 0xf) | 0xe0;
687
688                 /* Block (sector) count */
689                 fis[12] = (now_blocks >> 0) & 0xff;
690                 fis[13] = (now_blocks >> 8) & 0xff;
691
692                 /* Read/Write from ahci */
693                 if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis),
694                                         user_buffer, user_buffer_size,
695                                         is_write)) {
696                         debug("scsi_ahci: SCSI %s10 command failure.\n",
697                               is_write ? "WRITE" : "READ");
698                         return -EIO;
699                 }
700                 user_buffer += transfer_size;
701                 user_buffer_size -= transfer_size;
702                 blocks -= now_blocks;
703                 lba += now_blocks;
704         }
705
706         return 0;
707 }
708
709
710 /*
711  * SCSI READ CAPACITY10 command operation.
712  */
713 static int ata_scsiop_read_capacity10(ccb *pccb)
714 {
715         u32 cap;
716         u32 block_size;
717
718         if (!ataid[pccb->target]) {
719                 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
720                        "\tNo ATA info!\n"
721                        "\tPlease run SCSI commmand INQUIRY firstly!\n");
722                 return -EPERM;
723         }
724
725         cap = le32_to_cpu(ataid[pccb->target]->lba_capacity);
726         if (cap == 0xfffffff) {
727                 unsigned short *cap48 = ataid[pccb->target]->lba48_capacity;
728                 if (cap48[2] || cap48[3]) {
729                         cap = 0xffffffff;
730                 } else {
731                         cap = (le16_to_cpu(cap48[1]) << 16) |
732                               (le16_to_cpu(cap48[0]));
733                 }
734         }
735
736         cap = cpu_to_be32(cap);
737         memcpy(pccb->pdata, &cap, sizeof(cap));
738
739         block_size = cpu_to_be32((u32)512);
740         memcpy(&pccb->pdata[4], &block_size, 4);
741
742         return 0;
743 }
744
745
746 /*
747  * SCSI READ CAPACITY16 command operation.
748  */
749 static int ata_scsiop_read_capacity16(ccb *pccb)
750 {
751         u64 cap;
752         u64 block_size;
753
754         if (!ataid[pccb->target]) {
755                 printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
756                        "\tNo ATA info!\n"
757                        "\tPlease run SCSI commmand INQUIRY firstly!\n");
758                 return -EPERM;
759         }
760
761         cap = le32_to_cpu(ataid[pccb->target]->lba_capacity);
762         if (cap == 0xfffffff) {
763                 memcpy(&cap, ataid[pccb->target]->lba48_capacity, sizeof(cap));
764                 cap = le64_to_cpu(cap);
765         }
766
767         cap = cpu_to_be64(cap);
768         memcpy(pccb->pdata, &cap, sizeof(cap));
769
770         block_size = cpu_to_be64((u64)512);
771         memcpy(&pccb->pdata[8], &block_size, 8);
772
773         return 0;
774 }
775
776
777 /*
778  * SCSI TEST UNIT READY command operation.
779  */
780 static int ata_scsiop_test_unit_ready(ccb *pccb)
781 {
782         return (ataid[pccb->target]) ? 0 : -EPERM;
783 }
784
785
786 int scsi_exec(ccb *pccb)
787 {
788         int ret;
789
790         switch (pccb->cmd[0]) {
791         case SCSI_READ10:
792                 ret = ata_scsiop_read_write(pccb, 0);
793                 break;
794         case SCSI_WRITE10:
795                 ret = ata_scsiop_read_write(pccb, 1);
796                 break;
797         case SCSI_RD_CAPAC10:
798                 ret = ata_scsiop_read_capacity10(pccb);
799                 break;
800         case SCSI_RD_CAPAC16:
801                 ret = ata_scsiop_read_capacity16(pccb);
802                 break;
803         case SCSI_TST_U_RDY:
804                 ret = ata_scsiop_test_unit_ready(pccb);
805                 break;
806         case SCSI_INQUIRY:
807                 ret = ata_scsiop_inquiry(pccb);
808                 break;
809         default:
810                 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
811                 return FALSE;
812         }
813
814         if (ret) {
815                 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
816                 return FALSE;
817         }
818         return TRUE;
819
820 }
821
822
823 void scsi_low_level_init(int busdevfunc)
824 {
825         int i;
826         u32 linkmap;
827
828 #ifndef CONFIG_SCSI_AHCI_PLAT
829         ahci_init_one(busdevfunc);
830 #endif
831
832         linkmap = probe_ent->link_port_map;
833
834         for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
835                 if (((linkmap >> i) & 0x01)) {
836                         if (ahci_port_start((u8) i)) {
837                                 printf("Can not start port %d\n", i);
838                                 continue;
839                         }
840 #ifdef CONFIG_AHCI_SETFEATURES_XFER
841                         ahci_set_feature((u8) i);
842 #endif
843                 }
844         }
845 }
846
847 #ifdef CONFIG_SCSI_AHCI_PLAT
848 int ahci_init(u32 base)
849 {
850         int i, rc = 0;
851         u32 linkmap;
852
853         memset(ataid, 0, sizeof(ataid));
854
855         probe_ent = malloc(sizeof(struct ahci_probe_ent));
856         memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
857
858         probe_ent->host_flags = ATA_FLAG_SATA
859                                 | ATA_FLAG_NO_LEGACY
860                                 | ATA_FLAG_MMIO
861                                 | ATA_FLAG_PIO_DMA
862                                 | ATA_FLAG_NO_ATAPI;
863         probe_ent->pio_mask = 0x1f;
864         probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
865
866         probe_ent->mmio_base = base;
867
868         /* initialize adapter */
869         rc = ahci_host_init(probe_ent);
870         if (rc)
871                 goto err_out;
872
873         ahci_print_info(probe_ent);
874
875         linkmap = probe_ent->link_port_map;
876
877         for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
878                 if (((linkmap >> i) & 0x01)) {
879                         if (ahci_port_start((u8) i)) {
880                                 printf("Can not start port %d\n", i);
881                                 continue;
882                         }
883 #ifdef CONFIG_AHCI_SETFEATURES_XFER
884                         ahci_set_feature((u8) i);
885 #endif
886                 }
887         }
888 err_out:
889         return rc;
890 }
891 #endif
892
893 void scsi_bus_reset(void)
894 {
895         /*Not implement*/
896 }
897
898
899 void scsi_print_error(ccb * pccb)
900 {
901         /*The ahci error info can be read in the ahci driver*/
902 }