]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/ahci.c
Files include/linux/byteorder/{big,little}_endian.h define
[people/ms/u-boot.git] / drivers / ahci.c
CommitLineData
4782ac80
JZ
1/*
2 * Copyright (C) Freescale Semiconductor, Inc. 2006. All rights reserved.
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#ifdef CONFIG_SCSI_AHCI
30
31#include <command.h>
32#include <pci.h>
33#include <asm/processor.h>
34#include <asm/errno.h>
35#include <asm/io.h>
36#include <malloc.h>
37#include <scsi.h>
38#include <ata.h>
39#include <linux/ctype.h>
40#include <ahci.h>
41
42struct ahci_probe_ent *probe_ent = NULL;
43hd_driveid_t *ataid[AHCI_MAX_PORTS];
44
4a7cc0f2
JL
45#define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
46
4782ac80
JZ
47
48static inline u32 ahci_port_base(u32 base, u32 port)
49{
50 return base + 0x100 + (port * 0x80);
51}
52
53
54static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
55 unsigned int port_idx)
56{
57 base = ahci_port_base(base, port_idx);
58
4a7cc0f2
JL
59 port->cmd_addr = base;
60 port->scr_addr = base + PORT_SCR;
4782ac80
JZ
61}
62
63
64#define msleep(a) udelay(a * 1000)
65#define ssleep(a) msleep(a * 1000)
4a7cc0f2
JL
66
67static int waiting_for_cmd_completed(volatile u8 *offset,
68 int timeout_msec,
69 u32 sign)
4782ac80
JZ
70{
71 int i;
72 u32 status;
4a7cc0f2
JL
73
74 for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
4782ac80
JZ
75 msleep(1);
76
4a7cc0f2 77 return (i < timeout_msec) ? 0 : -1;
4782ac80
JZ
78}
79
80
81static int ahci_host_init(struct ahci_probe_ent *probe_ent)
82{
83 pci_dev_t pdev = probe_ent->dev;
84 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
85 u32 tmp, cap_save;
86 u16 tmp16;
87 int i, j;
4a7cc0f2 88 volatile u8 *port_mmio;
4782ac80
JZ
89 unsigned short vendor;
90
91 cap_save = readl(mmio + HOST_CAP);
4a7cc0f2 92 cap_save &= ((1 << 28) | (1 << 17));
4782ac80
JZ
93 cap_save |= (1 << 27);
94
95 /* global controller reset */
96 tmp = readl(mmio + HOST_CTL);
97 if ((tmp & HOST_RESET) == 0)
98 writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
99
100 /* reset must complete within 1 second, or
101 * the hardware should be considered fried.
102 */
103 ssleep(1);
104
105 tmp = readl(mmio + HOST_CTL);
106 if (tmp & HOST_RESET) {
107 debug("controller reset failed (0x%x)\n", tmp);
108 return -1;
109 }
110
111 writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
112 writel(cap_save, mmio + HOST_CAP);
113 writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
114
115 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
116
117 if (vendor == PCI_VENDOR_ID_INTEL) {
118 u16 tmp16;
119 pci_read_config_word(pdev, 0x92, &tmp16);
120 tmp16 |= 0xf;
121 pci_write_config_word(pdev, 0x92, tmp16);
122 }
123
124 probe_ent->cap = readl(mmio + HOST_CAP);
125 probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
126 probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
127
128 debug("cap 0x%x port_map 0x%x n_ports %d\n",
4a7cc0f2 129 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
4782ac80
JZ
130
131 for (i = 0; i < probe_ent->n_ports; i++) {
4a7cc0f2
JL
132 probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
133 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
134 ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
4782ac80
JZ
135
136 /* make sure port is not active */
137 tmp = readl(port_mmio + PORT_CMD);
138 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
139 PORT_CMD_FIS_RX | PORT_CMD_START)) {
140 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
141 PORT_CMD_FIS_RX | PORT_CMD_START);
142 writel_with_flush(tmp, port_mmio + PORT_CMD);
143
144 /* spec says 500 msecs for each bit, so
145 * this is slightly incorrect.
146 */
147 msleep(500);
148 }
149
150 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
151
152 j = 0;
153 while (j < 100) {
154 msleep(10);
155 tmp = readl(port_mmio + PORT_SCR_STAT);
156 if ((tmp & 0xf) == 0x3)
157 break;
158 j++;
159 }
160
161 tmp = readl(port_mmio + PORT_SCR_ERR);
162 debug("PORT_SCR_ERR 0x%x\n", tmp);
163 writel(tmp, port_mmio + PORT_SCR_ERR);
164
165 /* ack any pending irq events for this port */
166 tmp = readl(port_mmio + PORT_IRQ_STAT);
167 debug("PORT_IRQ_STAT 0x%x\n", tmp);
168 if (tmp)
169 writel(tmp, port_mmio + PORT_IRQ_STAT);
170
171 writel(1 << i, mmio + HOST_IRQ_STAT);
172
173 /* set irq mask (enables interrupts) */
174 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
175
4a7cc0f2 176 /*register linkup ports */
4782ac80 177 tmp = readl(port_mmio + PORT_SCR_STAT);
4a7cc0f2
JL
178 debug("Port %d status: 0x%x\n", i, tmp);
179 if ((tmp & 0xf) == 0x03)
180 probe_ent->link_port_map |= (0x01 << i);
4782ac80
JZ
181 }
182
183 tmp = readl(mmio + HOST_CTL);
184 debug("HOST_CTL 0x%x\n", tmp);
185 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
186 tmp = readl(mmio + HOST_CTL);
187 debug("HOST_CTL 0x%x\n", tmp);
188
189 pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
190 tmp |= PCI_COMMAND_MASTER;
191 pci_write_config_word(pdev, PCI_COMMAND, tmp16);
192
193 return 0;
194}
195
196
197static void ahci_print_info(struct ahci_probe_ent *probe_ent)
198{
199 pci_dev_t pdev = probe_ent->dev;
4a7cc0f2 200 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
4782ac80
JZ
201 u32 vers, cap, impl, speed;
202 const char *speed_s;
203 u16 cc;
204 const char *scc_s;
205
206 vers = readl(mmio + HOST_VERSION);
207 cap = probe_ent->cap;
208 impl = probe_ent->port_map;
209
210 speed = (cap >> 20) & 0xf;
211 if (speed == 1)
212 speed_s = "1.5";
213 else if (speed == 2)
214 speed_s = "3";
215 else
216 speed_s = "?";
217
218 pci_read_config_word(pdev, 0x0a, &cc);
219 if (cc == 0x0101)
220 scc_s = "IDE";
221 else if (cc == 0x0106)
222 scc_s = "SATA";
223 else if (cc == 0x0104)
224 scc_s = "RAID";
225 else
226 scc_s = "unknown";
227
4a7cc0f2
JL
228 printf("AHCI %02x%02x.%02x%02x "
229 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
230 (vers >> 24) & 0xff,
231 (vers >> 16) & 0xff,
232 (vers >> 8) & 0xff,
233 vers & 0xff,
234 ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
4782ac80
JZ
235
236 printf("flags: "
4a7cc0f2
JL
237 "%s%s%s%s%s%s"
238 "%s%s%s%s%s%s%s\n",
239 cap & (1 << 31) ? "64bit " : "",
240 cap & (1 << 30) ? "ncq " : "",
241 cap & (1 << 28) ? "ilck " : "",
242 cap & (1 << 27) ? "stag " : "",
243 cap & (1 << 26) ? "pm " : "",
244 cap & (1 << 25) ? "led " : "",
245 cap & (1 << 24) ? "clo " : "",
246 cap & (1 << 19) ? "nz " : "",
247 cap & (1 << 18) ? "only " : "",
248 cap & (1 << 17) ? "pmp " : "",
249 cap & (1 << 15) ? "pio " : "",
250 cap & (1 << 14) ? "slum " : "",
251 cap & (1 << 13) ? "part " : "");
4782ac80
JZ
252}
253
4a7cc0f2 254static int ahci_init_one(pci_dev_t pdev)
4782ac80
JZ
255{
256 u32 iobase, vendor;
257 int rc;
258
4a7cc0f2 259 memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
4782ac80
JZ
260
261 probe_ent = malloc(sizeof(probe_ent));
262 memset(probe_ent, 0, sizeof(probe_ent));
263 probe_ent->dev = pdev;
264
265 pci_read_config_dword(pdev, AHCI_PCI_BAR, &iobase);
266 iobase &= ~0xf;
267
4a7cc0f2
JL
268 probe_ent->host_flags = ATA_FLAG_SATA
269 | ATA_FLAG_NO_LEGACY
270 | ATA_FLAG_MMIO
271 | ATA_FLAG_PIO_DMA
272 | ATA_FLAG_NO_ATAPI;
273 probe_ent->pio_mask = 0x1f;
274 probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
4782ac80 275
4a7cc0f2 276 probe_ent->mmio_base = iobase;
4782ac80
JZ
277
278 /* Take from kernel:
279 * JMicron-specific fixup:
280 * make sure we're in AHCI mode
281 */
282 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
4a7cc0f2 283 if (vendor == 0x197b)
4782ac80
JZ
284 pci_write_config_byte(pdev, 0x41, 0xa1);
285
286 /* initialize adapter */
287 rc = ahci_host_init(probe_ent);
288 if (rc)
289 goto err_out;
290
291 ahci_print_info(probe_ent);
292
293 return 0;
294
4a7cc0f2 295 err_out:
4782ac80
JZ
296 return rc;
297}
298
299
300#define MAX_DATA_BYTE_COUNT (4*1024*1024)
4a7cc0f2 301
4782ac80
JZ
302static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
303{
4782ac80
JZ
304 struct ahci_ioports *pp = &(probe_ent->port[port]);
305 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
306 u32 sg_count;
307 int i;
308
309 sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
4a7cc0f2 310 if (sg_count > AHCI_MAX_SG) {
4782ac80
JZ
311 printf("Error:Too much sg!\n");
312 return -1;
313 }
314
4a7cc0f2
JL
315 for (i = 0; i < sg_count; i++) {
316 ahci_sg->addr =
317 cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
4782ac80 318 ahci_sg->addr_hi = 0;
4a7cc0f2
JL
319 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
320 (buf_len < MAX_DATA_BYTE_COUNT
321 ? (buf_len - 1)
322 : (MAX_DATA_BYTE_COUNT - 1)));
4782ac80
JZ
323 ahci_sg++;
324 buf_len -= MAX_DATA_BYTE_COUNT;
325 }
326
327 return sg_count;
328}
329
330
331static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
332{
333 pp->cmd_slot->opts = cpu_to_le32(opts);
334 pp->cmd_slot->status = 0;
335 pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
336 pp->cmd_slot->tbl_addr_hi = 0;
337}
338
339
340static void ahci_set_feature(u8 port)
341{
4782ac80 342 struct ahci_ioports *pp = &(probe_ent->port[port]);
4a7cc0f2
JL
343 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
344 u32 cmd_fis_len = 5; /* five dwords */
4782ac80
JZ
345 u8 fis[20];
346
4a7cc0f2
JL
347 /*set feature */
348 memset(fis, 0, 20);
4782ac80
JZ
349 fis[0] = 0x27;
350 fis[1] = 1 << 7;
351 fis[2] = ATA_CMD_SETF;
352 fis[3] = SETFEATURES_XFER;
353 fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
354
4a7cc0f2 355 memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
4782ac80
JZ
356 ahci_fill_cmd_slot(pp, cmd_fis_len);
357 writel(1, port_mmio + PORT_CMD_ISSUE);
358 readl(port_mmio + PORT_CMD_ISSUE);
359
4a7cc0f2 360 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
4782ac80
JZ
361 printf("set feature error!\n");
362 }
363}
364
365
366static int ahci_port_start(u8 port)
367{
4782ac80 368 struct ahci_ioports *pp = &(probe_ent->port[port]);
4a7cc0f2 369 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
4782ac80
JZ
370 u32 port_status;
371 u32 mem;
372
4a7cc0f2 373 debug("Enter start port: %d\n", port);
4782ac80 374 port_status = readl(port_mmio + PORT_SCR_STAT);
4a7cc0f2
JL
375 debug("Port %d status: %x\n", port, port_status);
376 if ((port_status & 0xf) != 0x03) {
4782ac80
JZ
377 printf("No Link on this port!\n");
378 return -1;
379 }
380
4a7cc0f2 381 mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
4782ac80
JZ
382 if (!mem) {
383 free(pp);
384 printf("No mem for table!\n");
385 return -ENOMEM;
386 }
387
4a7cc0f2
JL
388 mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
389 memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
4782ac80 390
4782ac80
JZ
391 /*
392 * First item in chunk of DMA memory: 32-slot command table,
393 * 32 bytes each in size
394 */
395 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
4a7cc0f2 396 debug("cmd_slot = 0x%x\n", pp->cmd_slot);
4782ac80 397 mem += (AHCI_CMD_SLOT_SZ + 224);
4a7cc0f2 398
4782ac80
JZ
399 /*
400 * Second item: Received-FIS area
401 */
402 pp->rx_fis = mem;
4782ac80 403 mem += AHCI_RX_FIS_SZ;
4a7cc0f2 404
4782ac80
JZ
405 /*
406 * Third item: data area for storing a single command
407 * and its scatter-gather table
408 */
409 pp->cmd_tbl = mem;
4a7cc0f2 410 debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
4782ac80
JZ
411
412 mem += AHCI_CMD_TBL_HDR;
413 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
414
4a7cc0f2 415 writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
4782ac80
JZ
416
417 writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
418
419 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
4a7cc0f2
JL
420 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
421 PORT_CMD_START, port_mmio + PORT_CMD);
4782ac80 422
4a7cc0f2 423 debug("Exit start port %d\n", port);
4782ac80
JZ
424
425 return 0;
426}
427
428
4a7cc0f2
JL
429static int get_ahci_device_data(u8 port, u8 *fis, int fis_len, u8 *buf,
430 int buf_len)
4782ac80
JZ
431{
432
4a7cc0f2
JL
433 struct ahci_ioports *pp = &(probe_ent->port[port]);
434 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
4782ac80
JZ
435 u32 opts;
436 u32 port_status;
437 int sg_count;
438
4a7cc0f2 439 debug("Enter get_ahci_device_data: for port %d\n", port);
4782ac80 440
4a7cc0f2 441 if (port > probe_ent->n_ports) {
4782ac80
JZ
442 printf("Invaild port number %d\n", port);
443 return -1;
444 }
445
446 port_status = readl(port_mmio + PORT_SCR_STAT);
4a7cc0f2
JL
447 if ((port_status & 0xf) != 0x03) {
448 debug("No Link on port %d!\n", port);
4782ac80
JZ
449 return -1;
450 }
451
452 memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
453
4a7cc0f2
JL
454 sg_count = ahci_fill_sg(port, buf, buf_len);
455 opts = (fis_len >> 2) | (sg_count << 16);
4782ac80
JZ
456 ahci_fill_cmd_slot(pp, opts);
457
458 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
459
460 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
461 printf("timeout exit!\n");
462 return -1;
463 }
464 debug("get_ahci_device_data: %d byte transferred.\n",
4a7cc0f2 465 pp->cmd_slot->status);
4782ac80
JZ
466
467 return 0;
468}
469
470
471static char *ata_id_strcpy(u16 *target, u16 *src, int len)
472{
473 int i;
4a7cc0f2 474 for (i = 0; i < len / 2; i++)
4782ac80
JZ
475 target[i] = le16_to_cpu(src[i]);
476 return (char *)target;
477}
478
479
480static void dump_ataid(hd_driveid_t *ataid)
481{
482 debug("(49)ataid->capability = 0x%x\n", ataid->capability);
483 debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
484 debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
485 debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
486 debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
487 debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
488 debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
489 debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
490 debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
491 debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
492 debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
493 debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
494 debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
495 debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
496 debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
497}
498
4a7cc0f2 499
4782ac80
JZ
500/*
501 * SCSI INQUIRY command operation.
502 */
503static int ata_scsiop_inquiry(ccb *pccb)
504{
505 u8 hdr[] = {
506 0,
507 0,
4a7cc0f2 508 0x5, /* claim SPC-3 version compatibility */
4782ac80
JZ
509 2,
510 95 - 4,
511 };
512 u8 fis[20];
513 u8 *tmpid;
514 u8 port;
515
516 /* Clean ccb data buffer */
517 memset(pccb->pdata, 0, pccb->datalen);
518
519 memcpy(pccb->pdata, hdr, sizeof(hdr));
520
4a7cc0f2 521 if (pccb->datalen <= 35)
4782ac80
JZ
522 return 0;
523
524 memset(fis, 0, 20);
525 /* Construct the FIS */
4a7cc0f2
JL
526 fis[0] = 0x27; /* Host to device FIS. */
527 fis[1] = 1 << 7; /* Command FIS. */
528 fis[2] = ATA_CMD_IDENT; /* Command byte. */
4782ac80
JZ
529
530 /* Read id from sata */
531 port = pccb->target;
4a7cc0f2 532 if (!(tmpid = malloc(sizeof(hd_driveid_t))))
4782ac80
JZ
533 return -ENOMEM;
534
4a7cc0f2
JL
535 if (get_ahci_device_data(port, (u8 *) & fis, 20,
536 tmpid, sizeof(hd_driveid_t))) {
4782ac80
JZ
537 debug("scsi_ahci: SCSI inquiry command failure.\n");
538 return -EIO;
539 }
540
4a7cc0f2 541 if (ataid[port])
4782ac80 542 free(ataid[port]);
4a7cc0f2 543 ataid[port] = (hd_driveid_t *) tmpid;
4782ac80
JZ
544
545 memcpy(&pccb->pdata[8], "ATA ", 8);
4a7cc0f2
JL
546 ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
547 ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
4782ac80
JZ
548
549 dump_ataid(ataid[port]);
550 return 0;
551}
552
553
554/*
555 * SCSI READ10 command operation.
556 */
4a7cc0f2 557static int ata_scsiop_read10(ccb * pccb)
4782ac80
JZ
558{
559 u64 lba = 0;
560 u32 len = 0;
561 u8 fis[20];
562
4a7cc0f2
JL
563 lba = (((u64) pccb->cmd[2]) << 24) | (((u64) pccb->cmd[3]) << 16)
564 | (((u64) pccb->cmd[4]) << 8) | ((u64) pccb->cmd[5]);
565 len = (((u32) pccb->cmd[7]) << 8) | ((u32) pccb->cmd[8]);
4782ac80
JZ
566
567 /* For 10-byte and 16-byte SCSI R/W commands, transfer
568 * length 0 means transfer 0 block of data.
569 * However, for ATA R/W commands, sector count 0 means
570 * 256 or 65536 sectors, not 0 sectors as in SCSI.
571 *
572 * WARNING: one or two older ATA drives treat 0 as 0...
573 */
4a7cc0f2
JL
574 if (!len)
575 return 0;
4782ac80
JZ
576 memset(fis, 0, 20);
577
578 /* Construct the FIS */
4a7cc0f2
JL
579 fis[0] = 0x27; /* Host to device FIS. */
580 fis[1] = 1 << 7; /* Command FIS. */
4782ac80
JZ
581 fis[2] = ATA_CMD_RD_DMA; /* Command byte. */
582
4a7cc0f2 583 /* LBA address, only support LBA28 in this driver */
4782ac80
JZ
584 fis[4] = pccb->cmd[5];
585 fis[5] = pccb->cmd[4];
586 fis[6] = pccb->cmd[3];
587 fis[7] = (pccb->cmd[2] & 0x0f) | 0xe0;
588
589 /* Sector Count */
590 fis[12] = pccb->cmd[8];
591 fis[13] = pccb->cmd[7];
592
593 /* Read from ahci */
4a7cc0f2
JL
594 if (get_ahci_device_data(pccb->target, (u8 *) & fis, 20,
595 pccb->pdata, pccb->datalen)) {
4782ac80
JZ
596 debug("scsi_ahci: SCSI READ10 command failure.\n");
597 return -EIO;
598 }
599
600 return 0;
601}
602
603
604/*
605 * SCSI READ CAPACITY10 command operation.
606 */
607static int ata_scsiop_read_capacity10(ccb *pccb)
608{
609 u8 buf[8];
610
4a7cc0f2 611 if (!ataid[pccb->target]) {
4782ac80 612 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
4a7cc0f2
JL
613 "\tNo ATA info!\n"
614 "\tPlease run SCSI commmand INQUIRY firstly!\n");
4782ac80
JZ
615 return -EPERM;
616 }
617
618 memset(buf, 0, 8);
619
4a7cc0f2 620 *(u32 *) buf = le32_to_cpu(ataid[pccb->target]->lba_capacity);
4782ac80
JZ
621
622 buf[6] = 512 >> 8;
623 buf[7] = 512 & 0xff;
624
625 memcpy(pccb->pdata, buf, 8);
626
627 return 0;
628}
629
630
631/*
632 * SCSI TEST UNIT READY command operation.
633 */
634static int ata_scsiop_test_unit_ready(ccb *pccb)
635{
636 return (ataid[pccb->target]) ? 0 : -EPERM;
637}
638
4a7cc0f2 639
4782ac80
JZ
640int scsi_exec(ccb *pccb)
641{
642 int ret;
643
4a7cc0f2 644 switch (pccb->cmd[0]) {
4782ac80
JZ
645 case SCSI_READ10:
646 ret = ata_scsiop_read10(pccb);
647 break;
648 case SCSI_RD_CAPAC:
649 ret = ata_scsiop_read_capacity10(pccb);
650 break;
651 case SCSI_TST_U_RDY:
652 ret = ata_scsiop_test_unit_ready(pccb);
653 break;
654 case SCSI_INQUIRY:
655 ret = ata_scsiop_inquiry(pccb);
656 break;
657 default:
658 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
659 return FALSE;
660 }
661
4a7cc0f2
JL
662 if (ret) {
663 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
4782ac80
JZ
664 return FALSE;
665 }
666 return TRUE;
667
668}
669
670
671void scsi_low_level_init(int busdevfunc)
672{
673 int i;
674 u32 linkmap;
675
676 ahci_init_one(busdevfunc);
677
678 linkmap = probe_ent->link_port_map;
679
4a7cc0f2
JL
680 for (i = 0; i < CFG_SCSI_MAX_SCSI_ID; i++) {
681 if (((linkmap >> i) & 0x01)) {
682 if (ahci_port_start((u8) i)) {
683 printf("Can not start port %d\n", i);
4782ac80
JZ
684 continue;
685 }
4a7cc0f2 686 ahci_set_feature((u8) i);
4782ac80
JZ
687 }
688 }
689}
690
691
692void scsi_bus_reset(void)
693{
4a7cc0f2 694 /*Not implement*/
4782ac80
JZ
695}
696
697
4a7cc0f2 698void scsi_print_error(ccb * pccb)
4782ac80 699{
4a7cc0f2 700 /*The ahci error info can be read in the ahci driver*/
4782ac80
JZ
701}
702#endif