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