blob: 6b3517369ddd4c13941207f280e5e494797167f4 [file] [log] [blame]
Dave Liu6f1a8a22008-03-26 22:55:32 +08001/*
Kumar Galaeb453df2010-04-20 10:21:25 -05002 * Copyright (C) 2008,2010 Freescale Semiconductor, Inc.
Dave Liu6f1a8a22008-03-26 22:55:32 +08003 * Dave Liu <daveliu@freescale.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20
21#include <common.h>
22#include <command.h>
23#include <asm/io.h>
Dave Liub9674d02010-04-12 14:23:25 +080024#include <asm/processor.h>
Kumar Galaeb453df2010-04-20 10:21:25 -050025#include <asm/fsl_serdes.h>
Dave Liu6f1a8a22008-03-26 22:55:32 +080026#include <malloc.h>
27#include <libata.h>
28#include <fis.h>
29#include "fsl_sata.h"
30
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020031extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
Dave Liu6f1a8a22008-03-26 22:55:32 +080032
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020033#ifndef CONFIG_SYS_SATA1_FLAGS
34 #define CONFIG_SYS_SATA1_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080035#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036#ifndef CONFIG_SYS_SATA2_FLAGS
37 #define CONFIG_SYS_SATA2_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080038#endif
39
40static struct fsl_sata_info fsl_sata_info[] = {
41#ifdef CONFIG_SATA1
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042 {CONFIG_SYS_SATA1, CONFIG_SYS_SATA1_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080043#else
44 {0, 0},
45#endif
46#ifdef CONFIG_SATA2
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020047 {CONFIG_SYS_SATA2, CONFIG_SYS_SATA2_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080048#else
49 {0, 0},
50#endif
51};
52
Dave Liu6f1a8a22008-03-26 22:55:32 +080053static inline void sdelay(unsigned long sec)
54{
55 unsigned long i;
56 for (i = 0; i < sec; i++)
57 mdelay(1000);
58}
59
60void dprint_buffer(unsigned char *buf, int len)
61{
62 int i, j;
63
64 i = 0;
65 j = 0;
66 printf("\n\r");
67
68 for (i = 0; i < len; i++) {
69 printf("%02x ", *buf++);
70 j++;
71 if (j == 16) {
72 printf("\n\r");
73 j = 0;
74 }
75 }
76 printf("\n\r");
77}
78
galak18cf84a2009-07-07 15:53:21 -050079static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
Dave Liu6f1a8a22008-03-26 22:55:32 +080080{
81 printf("Status FIS dump:\n\r");
82 printf("fis_type: %02x\n\r", s->fis_type);
83 printf("pm_port_i: %02x\n\r", s->pm_port_i);
84 printf("status: %02x\n\r", s->status);
85 printf("error: %02x\n\r", s->error);
86 printf("lba_low: %02x\n\r", s->lba_low);
87 printf("lba_mid: %02x\n\r", s->lba_mid);
88 printf("lba_high: %02x\n\r", s->lba_high);
89 printf("device: %02x\n\r", s->device);
90 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
91 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
92 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
93 printf("res1: %02x\n\r", s->res1);
94 printf("sector_count: %02x\n\r", s->sector_count);
95 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
96}
97
98static int ata_wait_register(volatile unsigned *addr, u32 mask,
99 u32 val, u32 timeout_msec)
100{
101 int i;
102 u32 temp;
103
104 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
105 && i < timeout_msec; i++)
106 mdelay(1);
107 return (i < timeout_msec) ? 0 : -1;
108}
109
110int init_sata(int dev)
111{
112 u32 length, align;
113 cmd_hdr_tbl_t *cmd_hdr;
114 u32 cda;
115 u32 val32;
116 fsl_sata_reg_t *reg;
117 u32 sig;
118 int i;
119 fsl_sata_t *sata;
120
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800122 printf("the sata index %d is out of ranges\n\r", dev);
123 return -1;
124 }
125
Kumar Galaeb453df2010-04-20 10:21:25 -0500126#ifdef CONFIG_MPC85xx
127 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
128 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
129 return -1;
130 }
131 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
132 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
133 return -1;
134 }
135#endif
136
Dave Liu6f1a8a22008-03-26 22:55:32 +0800137 /* Allocate SATA device driver struct */
138 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
139 if (!sata) {
140 printf("alloc the sata device struct failed\n\r");
141 return -1;
142 }
143 /* Zero all of the device driver struct */
144 memset((void *)sata, 0, sizeof(fsl_sata_t));
145
146 /* Save the private struct to block device struct */
147 sata_dev_desc[dev].priv = (void *)sata;
148
149 sprintf(sata->name, "SATA%d", dev);
150
151 /* Set the controller register base address to device struct */
152 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
153 sata->reg_base = reg;
154
155 /* Allocate the command header table, 4 bytes aligned */
156 length = sizeof(struct cmd_hdr_tbl);
157 align = SATA_HC_CMD_HDR_TBL_ALIGN;
158 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
159 if (!sata) {
160 printf("alloc the command header failed\n\r");
161 return -1;
162 }
163
164 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
165 & ~(align - 1));
166 sata->cmd_hdr = cmd_hdr;
167
168 /* Zero all of the command header table */
169 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
170
171 /* Allocate command descriptor for all command */
172 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
173 align = SATA_HC_CMD_DESC_ALIGN;
174 sata->cmd_desc_offset = (void *)malloc(length + align);
175 if (!sata->cmd_desc_offset) {
176 printf("alloc the command descriptor failed\n\r");
177 return -1;
178 }
179 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
180 & ~(align - 1));
181 /* Zero all of command descriptor */
182 memset((void *)sata->cmd_desc_offset, 0, length + align);
183
184 /* Link the command descriptor to command header */
185 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
186 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
187 & ~(CMD_HDR_CDA_ALIGN - 1);
188 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
189 }
190
191 /* To have safe state, force the controller offline */
192 val32 = in_le32(&reg->hcontrol);
193 val32 &= ~HCONTROL_ONOFF;
194 val32 |= HCONTROL_FORCE_OFFLINE;
195 out_le32(&reg->hcontrol, val32);
196
197 /* Wait the controller offline */
198 ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
199
Dave Liub9674d02010-04-12 14:23:25 +0800200#if defined(CONFIG_FSL_SATA_V2) && defined(CONFIG_FSL_SATA_ERRATUM_A001)
201 /*
202 * For P1022/1013 Rev1.0 silicon, after power on SATA host
203 * controller is configured in legacy mode instead of the
204 * expected enterprise mode. software needs to clear bit[28]
205 * of HControl register to change to enterprise mode from
206 * legacy mode.
207 */
208 {
209 u32 svr = get_svr();
210 if (IS_SVR_REV(svr, 1, 0) &&
211 ((SVR_SOC_VER(svr) == SVR_P1022) ||
212 (SVR_SOC_VER(svr) == SVR_P1022_E) ||
213 (SVR_SOC_VER(svr) == SVR_P1013) ||
214 (SVR_SOC_VER(svr) == SVR_P1013_E))) {
215 out_le32(&reg->hstatus, 0x20000000);
216 out_le32(&reg->hcontrol, 0x00000100);
217 }
218 }
219#endif
220
Dave Liu6f1a8a22008-03-26 22:55:32 +0800221 /* Set the command header base address to CHBA register to tell DMA */
222 out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
223
224 /* Snoop for the command header */
225 val32 = in_le32(&reg->hcontrol);
226 val32 |= HCONTROL_HDR_SNOOP;
227 out_le32(&reg->hcontrol, val32);
228
229 /* Disable all of interrupts */
230 val32 = in_le32(&reg->hcontrol);
231 val32 &= ~HCONTROL_INT_EN_ALL;
232 out_le32(&reg->hcontrol, val32);
233
234 /* Clear all of interrupts */
235 val32 = in_le32(&reg->hstatus);
236 out_le32(&reg->hstatus, val32);
237
238 /* Set the ICC, no interrupt coalescing */
239 out_le32(&reg->icc, 0x01000000);
240
241 /* No PM attatched, the SATA device direct connect */
242 out_le32(&reg->cqpmp, 0);
243
244 /* Clear SError register */
245 val32 = in_le32(&reg->serror);
246 out_le32(&reg->serror, val32);
247
248 /* Clear CER register */
249 val32 = in_le32(&reg->cer);
250 out_le32(&reg->cer, val32);
251
252 /* Clear DER register */
253 val32 = in_le32(&reg->der);
254 out_le32(&reg->der, val32);
255
256 /* No device detection or initialization action requested */
257 out_le32(&reg->scontrol, 0x00000300);
258
259 /* Configure the transport layer, default value */
260 out_le32(&reg->transcfg, 0x08000016);
261
262 /* Configure the link layer, default value */
263 out_le32(&reg->linkcfg, 0x0000ff34);
264
265 /* Bring the controller online */
266 val32 = in_le32(&reg->hcontrol);
267 val32 |= HCONTROL_ONOFF;
268 out_le32(&reg->hcontrol, val32);
269
270 mdelay(100);
271
272 /* print sata device name */
273 if (!dev)
274 printf("%s ", sata->name);
275 else
276 printf(" %s ", sata->name);
277
Dave Liu4d9c1882008-06-03 17:38:19 +0800278 /* Wait PHY RDY signal changed for 500ms */
279 ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
280 HSTATUS_PHY_RDY, 500);
281
Dave Liu6f1a8a22008-03-26 22:55:32 +0800282 /* Check PHYRDY */
283 val32 = in_le32(&reg->hstatus);
284 if (val32 & HSTATUS_PHY_RDY) {
285 sata->link = 1;
286 } else {
287 sata->link = 0;
288 printf("(No RDY)\n\r");
289 return -1;
290 }
291
Dave Liu4d9c1882008-06-03 17:38:19 +0800292 /* Wait for signature updated, which is 1st D2H */
293 ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
294 HSTATUS_SIGNATURE, 10000);
295
Dave Liu6f1a8a22008-03-26 22:55:32 +0800296 if (val32 & HSTATUS_SIGNATURE) {
297 sig = in_le32(&reg->sig);
298 debug("Signature updated, the sig =%08x\n\r", sig);
299 sata->ata_device_type = ata_dev_classify(sig);
300 }
301
302 /* Check the speed */
303 val32 = in_le32(&reg->sstatus);
304 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
305 printf("(1.5 Gbps)\n\r");
306 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
307 printf("(3 Gbps)\n\r");
308
309 return 0;
310}
311
312/* Hardware reset, like Power-on and COMRESET */
313void fsl_sata_hardware_reset(u32 reg_base)
314{
315 fsl_sata_reg_t *reg = (fsl_sata_reg_t *)reg_base;
316 u32 scontrol;
317
318 /* Disable the SATA interface and put PHY offline */
319 scontrol = in_le32(&reg->scontrol);
320 scontrol = (scontrol & 0x0f0) | 0x304;
321 out_le32(&reg->scontrol, scontrol);
322
323 /* No speed strict */
324 scontrol = in_le32(&reg->scontrol);
325 scontrol = scontrol & ~0x0f0;
326 out_le32(&reg->scontrol, scontrol);
327
328 /* Issue PHY wake/reset, Hardware_reset_asserted */
329 scontrol = in_le32(&reg->scontrol);
330 scontrol = (scontrol & 0x0f0) | 0x301;
331 out_le32(&reg->scontrol, scontrol);
332
333 mdelay(100);
334
335 /* Resume PHY, COMRESET negated, the device initialize hardware
336 * and execute diagnostics, send good status-signature to host,
337 * which is D2H register FIS, and then the device enter idle state.
338 */
339 scontrol = in_le32(&reg->scontrol);
340 scontrol = (scontrol & 0x0f0) | 0x300;
341 out_le32(&reg->scontrol, scontrol);
342
343 mdelay(100);
344 return;
345}
346
347static void fsl_sata_dump_regs(fsl_sata_reg_t *reg)
348{
349 printf("\n\rSATA: %08x\n\r", (u32)reg);
350 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
351 printf("CAR: %08x\n\r", in_le32(&reg->car));
352 printf("CCR: %08x\n\r", in_le32(&reg->ccr));
353 printf("CER: %08x\n\r", in_le32(&reg->cer));
354 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
355 printf("DER: %08x\n\r", in_le32(&reg->der));
356 printf("CHBA: %08x\n\r", in_le32(&reg->chba));
357 printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
358 printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
359 printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
360 printf("SIG: %08x\n\r", in_le32(&reg->sig));
361 printf("ICC: %08x\n\r", in_le32(&reg->icc));
362 printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
363 printf("SError: %08x\n\r", in_le32(&reg->serror));
364 printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
365 printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
366 printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
367 printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
368 printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
369 printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
370 printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
371 printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
372 printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
373 printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
374 printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
375}
376
galak18cf84a2009-07-07 15:53:21 -0500377static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800378 int is_ncq, int tag, u8 *buffer, u32 len)
379{
380 cmd_hdr_entry_t *cmd_hdr;
381 cmd_desc_t *cmd_desc;
382 sata_fis_h2d_t *h2d;
383 prd_entry_t *prde;
384 u32 ext_c_ddc;
385 u32 prde_count;
386 u32 val32;
387 u32 ttl;
388 fsl_sata_reg_t *reg = sata->reg_base;
389 int i;
390
391 /* Check xfer length */
392 if (len > SATA_HC_MAX_XFER_LEN) {
393 printf("max transfer length is 64MB\n\r");
394 return 0;
395 }
396
397 /* Setup the command descriptor */
398 cmd_desc = sata->cmd_desc + tag;
399
400 /* Get the pointer cfis of command descriptor */
401 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
402
403 /* Zero the cfis of command descriptor */
404 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
405
406 /* Copy the cfis from user to command descriptor */
407 h2d->fis_type = cfis->fis_type;
408 h2d->pm_port_c = cfis->pm_port_c;
409 h2d->command = cfis->command;
410
411 h2d->features = cfis->features;
412 h2d->features_exp = cfis->features_exp;
413
414 h2d->lba_low = cfis->lba_low;
415 h2d->lba_mid = cfis->lba_mid;
416 h2d->lba_high = cfis->lba_high;
417 h2d->lba_low_exp = cfis->lba_low_exp;
418 h2d->lba_mid_exp = cfis->lba_mid_exp;
419 h2d->lba_high_exp = cfis->lba_high_exp;
420
421 if (!is_ncq) {
422 h2d->sector_count = cfis->sector_count;
423 h2d->sector_count_exp = cfis->sector_count_exp;
424 } else { /* NCQ */
425 h2d->sector_count = (u8)(tag << 3);
426 }
427
428 h2d->device = cfis->device;
429 h2d->control = cfis->control;
430
431 /* Setup the PRD table */
432 prde = (prd_entry_t *)cmd_desc->prdt;
433 memset((void *)prde, 0, sizeof(struct prdt));
434
435 prde_count = 0;
436 ttl = len;
437 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
438 if (!len)
439 break;
440 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
441 debug("dba = %08x\n\r", (u32)buffer);
442
443 if (len < PRD_ENTRY_MAX_XFER_SZ) {
444 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
445 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
446 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
447 prde_count++;
448 prde++;
449 break;
450 } else {
451 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
452 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
453 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
454 buffer += PRD_ENTRY_MAX_XFER_SZ;
455 len -= PRD_ENTRY_MAX_XFER_SZ;
456 prde_count++;
457 prde++;
458 }
459 }
460
461 /* Setup the command slot of cmd hdr */
462 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
463
464 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
465
466 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
467 val32 |= sizeof(sata_fis_h2d_t);
468 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
469
470 cmd_hdr->ttl = cpu_to_le32(ttl);
471
472 if (!is_ncq) {
473 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
474 } else {
475 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
476 }
477
478 tag &= CMD_HDR_ATTR_TAG;
479 val32 |= tag;
480
481 debug("attribute = %08x\n\r", val32);
482 cmd_hdr->attribute = cpu_to_le32(val32);
483
484 /* Make sure cmd desc and cmd slot valid before commmand issue */
485 sync();
486
487 /* PMP*/
488 val32 = (u32)(h2d->pm_port_c & 0x0f);
489 out_le32(&reg->cqpmp, val32);
490
491 /* Wait no active */
492 if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
493 printf("Wait no active time out\n\r");
494
495 /* Issue command */
496 if (!(in_le32(&reg->cqr) & (1 << tag))) {
497 val32 = 1 << tag;
498 out_le32(&reg->cqr, val32);
499 }
500
501 /* Wait command completed for 10s */
502 if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
503 if (!is_ncq)
504 printf("Non-NCQ command time out\n\r");
505 else
506 printf("NCQ command time out\n\r");
507 }
508
509 val32 = in_le32(&reg->cer);
510
511 if (val32) {
512 u32 der;
galak18cf84a2009-07-07 15:53:21 -0500513 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800514 printf("CE at device\n\r");
515 fsl_sata_dump_regs(reg);
516 der = in_le32(&reg->der);
517 out_le32(&reg->cer, val32);
518 out_le32(&reg->der, der);
519 }
520
521 /* Clear complete flags */
522 val32 = in_le32(&reg->ccr);
523 out_le32(&reg->ccr, val32);
524
525 return len;
526}
527
galak18cf84a2009-07-07 15:53:21 -0500528static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800529 int tag, u8 *buffer, u32 len)
530{
531 return 0;
532}
533
galak18cf84a2009-07-07 15:53:21 -0500534static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800535 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
536{
537 int rc;
538
539 if (tag > SATA_HC_MAX_CMD || tag < 0) {
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500540 printf("tag is out of range, tag=%d\n\r", tag);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800541 return -1;
542 }
543
544 switch (command_type) {
545 case CMD_ATA:
546 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
547 return rc;
548 case CMD_RESET:
549 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
550 return rc;
551 case CMD_NCQ:
552 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
553 return rc;
554 case CMD_ATAPI:
555 case CMD_VENDOR_BIST:
556 case CMD_BIST:
557 printf("not support now\n\r");
558 return -1;
559 default:
560 break;
561 }
562
563 return -1;
564}
565
566static void fsl_sata_identify(int dev, u16 *id)
567{
568 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500569 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800570
galak18cf84a2009-07-07 15:53:21 -0500571 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800572
573 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
574 cfis->pm_port_c = 0x80; /* is command */
575 cfis->command = ATA_CMD_ID_ATA;
576
577 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
578 ata_swap_buf_le16(id, ATA_ID_WORDS);
579}
580
581static void fsl_sata_xfer_mode(int dev, u16 *id)
582{
583 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
584
585 sata->pio = id[ATA_ID_PIO_MODES];
586 sata->mwdma = id[ATA_ID_MWDMA_MODES];
587 sata->udma = id[ATA_ID_UDMA_MODES];
588 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
589}
590
591static void fsl_sata_set_features(int dev)
592{
593 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500594 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800595 u8 udma_cap;
596
galak18cf84a2009-07-07 15:53:21 -0500597 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800598
599 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
600 cfis->pm_port_c = 0x80; /* is command */
601 cfis->command = ATA_CMD_SET_FEATURES;
602 cfis->features = SETFEATURES_XFER;
603
604 /* First check the device capablity */
605 udma_cap = (u8)(sata->udma & 0xff);
606 debug("udma_cap %02x\n\r", udma_cap);
607
608 if (udma_cap == ATA_UDMA6)
609 cfis->sector_count = XFER_UDMA_6;
610 if (udma_cap == ATA_UDMA5)
611 cfis->sector_count = XFER_UDMA_5;
612 if (udma_cap == ATA_UDMA4)
613 cfis->sector_count = XFER_UDMA_4;
614 if (udma_cap == ATA_UDMA3)
615 cfis->sector_count = XFER_UDMA_3;
616
617 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
618}
619
620static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
621{
622 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500623 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800624 u32 block;
625
626 block = start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800627
galak18cf84a2009-07-07 15:53:21 -0500628 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800629
630 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
631 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800632 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800633 cfis->device = ATA_LBA;
634
635 cfis->device |= (block >> 24) & 0xf;
636 cfis->lba_high = (block >> 16) & 0xff;
637 cfis->lba_mid = (block >> 8) & 0xff;
638 cfis->lba_low = block & 0xff;
639 cfis->sector_count = (u8)(blkcnt & 0xff);
640
641 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
642 return blkcnt;
643}
644
645void fsl_sata_flush_cache(int dev)
646{
647 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500648 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800649
galak18cf84a2009-07-07 15:53:21 -0500650 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800651
652 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
653 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800654 cfis->command = ATA_CMD_FLUSH;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800655
656 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
657}
658
659static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
660{
661 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500662 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800663 u64 block;
664
665 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800666
galak18cf84a2009-07-07 15:53:21 -0500667 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800668
669 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
670 cfis->pm_port_c = 0x80; /* is command */
671
Dave Liub9342a02008-04-01 15:22:11 +0800672 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
673 : ATA_CMD_READ_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800674
675 cfis->lba_high_exp = (block >> 40) & 0xff;
676 cfis->lba_mid_exp = (block >> 32) & 0xff;
677 cfis->lba_low_exp = (block >> 24) & 0xff;
678 cfis->lba_high = (block >> 16) & 0xff;
679 cfis->lba_mid = (block >> 8) & 0xff;
680 cfis->lba_low = block & 0xff;
681 cfis->device = ATA_LBA;
682 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
683 cfis->sector_count = blkcnt & 0xff;
684
685 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
686 return blkcnt;
687}
688
689u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
690{
691 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500692 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800693 int ncq_channel;
694 u64 block;
695
Tang Yuantiana89db422011-10-03 12:18:41 -0700696 if (sata->lba48 != 1) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800697 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
698 return -1;
699 }
700
701 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800702
galak18cf84a2009-07-07 15:53:21 -0500703 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800704
705 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
706 cfis->pm_port_c = 0x80; /* is command */
707
Dave Liub9342a02008-04-01 15:22:11 +0800708 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
709 : ATA_CMD_FPDMA_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800710
711 cfis->lba_high_exp = (block >> 40) & 0xff;
712 cfis->lba_mid_exp = (block >> 32) & 0xff;
713 cfis->lba_low_exp = (block >> 24) & 0xff;
714 cfis->lba_high = (block >> 16) & 0xff;
715 cfis->lba_mid = (block >> 8) & 0xff;
716 cfis->lba_low = block & 0xff;
717
718 cfis->device = ATA_LBA;
719 cfis->features_exp = (blkcnt >> 8) & 0xff;
720 cfis->features = blkcnt & 0xff;
721
722 if (sata->queue_depth >= SATA_HC_MAX_CMD)
723 ncq_channel = SATA_HC_MAX_CMD - 1;
724 else
725 ncq_channel = sata->queue_depth - 1;
726
727 /* Use the latest queue */
728 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
729 return blkcnt;
730}
731
732void fsl_sata_flush_cache_ext(int dev)
733{
734 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500735 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800736
galak18cf84a2009-07-07 15:53:21 -0500737 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800738
739 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
740 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800741 cfis->command = ATA_CMD_FLUSH_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800742
743 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
744}
745
746/* Software reset, set SRST of the Device Control register */
747void fsl_sata_software_reset(int dev)
748{
749 return;
750}
751
752static void fsl_sata_init_wcache(int dev, u16 *id)
753{
754 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
755
756 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
757 sata->wcache = 1;
758 if (ata_id_has_flush(id))
759 sata->flush = 1;
760 if (ata_id_has_flush_ext(id))
761 sata->flush_ext = 1;
762}
763
764static int fsl_sata_get_wcache(int dev)
765{
766 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
767 return sata->wcache;
768}
769
770static int fsl_sata_get_flush(int dev)
771{
772 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
773 return sata->flush;
774}
775
776static int fsl_sata_get_flush_ext(int dev)
777{
778 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
779 return sata->flush_ext;
780}
781
782u32 ata_low_level_rw_lba48(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
783{
784 u32 start, blks;
785 u8 *addr;
786 int max_blks;
787
788 start = blknr;
789 blks = blkcnt;
790 addr = (u8 *)buffer;
791
792 max_blks = ATA_MAX_SECTORS_LBA48;
793 do {
794 if (blks > max_blks) {
795 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
796 fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
797 else
798 fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
799 start += max_blks;
800 blks -= max_blks;
801 addr += ATA_SECT_SIZE * max_blks;
802 } else {
803 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
804 fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
805 else
806 fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
807 start += blks;
808 blks = 0;
809 addr += ATA_SECT_SIZE * blks;
810 }
811 } while (blks != 0);
812
813 return blkcnt;
814}
815
816u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
817{
818 u32 start, blks;
819 u8 *addr;
820 int max_blks;
821
822 start = blknr;
823 blks = blkcnt;
824 addr = (u8 *)buffer;
825
826 max_blks = ATA_MAX_SECTORS;
827 do {
828 if (blks > max_blks) {
829 fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
830 start += max_blks;
831 blks -= max_blks;
832 addr += ATA_SECT_SIZE * max_blks;
833 } else {
834 fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
835 start += blks;
836 blks = 0;
837 addr += ATA_SECT_SIZE * blks;
838 }
839 } while (blks != 0);
840
841 return blkcnt;
842}
843
844/*
845 * SATA interface between low level driver and command layer
846 */
847ulong sata_read(int dev, u32 blknr, u32 blkcnt, void *buffer)
848{
849 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700850 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800851
Tang Yuantiana89db422011-10-03 12:18:41 -0700852 if (sata->lba48)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800853 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
854 else
855 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
856 return rc;
857}
858
859ulong sata_write(int dev, u32 blknr, u32 blkcnt, void *buffer)
860{
861 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700862 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800863
Tang Yuantiana89db422011-10-03 12:18:41 -0700864 if (sata->lba48) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800865 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
866 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
867 fsl_sata_flush_cache_ext(dev);
868 } else {
869 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
870 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
871 fsl_sata_flush_cache(dev);
872 }
873 return rc;
874}
875
876int scan_sata(int dev)
877{
878 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
879 unsigned char serial[ATA_ID_SERNO_LEN + 1];
880 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
881 unsigned char product[ATA_ID_PROD_LEN + 1];
882 u16 *id;
883 u64 n_sectors;
884
885 /* if no detected link */
886 if (!sata->link)
887 return -1;
888
889 id = (u16 *)malloc(ATA_ID_WORDS * 2);
890 if (!id) {
891 printf("id malloc failed\n\r");
892 return -1;
893 }
894
895 /* Identify device to get information */
896 fsl_sata_identify(dev, id);
897
898 /* Serial number */
899 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
900 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
901
902 /* Firmware version */
903 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
904 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
905
906 /* Product model */
907 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
908 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
909
910 /* Totoal sectors */
911 n_sectors = ata_id_n_sectors(id);
912 sata_dev_desc[dev].lba = (u32)n_sectors;
913
Tang Yuantiana89db422011-10-03 12:18:41 -0700914#ifdef CONFIG_LBA48
Dave Liu6f1a8a22008-03-26 22:55:32 +0800915 /* Check if support LBA48 */
916 if (ata_id_has_lba48(id)) {
Tang Yuantiana89db422011-10-03 12:18:41 -0700917 sata->lba48 = 1;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800918 debug("Device support LBA48\n\r");
Tang Yuantiana89db422011-10-03 12:18:41 -0700919 } else
920 debug("Device supports LBA28\n\r");
921#endif
Dave Liu6f1a8a22008-03-26 22:55:32 +0800922
923 /* Get the NCQ queue depth from device */
924 sata->queue_depth = ata_id_queue_depth(id);
925
926 /* Get the xfer mode from device */
927 fsl_sata_xfer_mode(dev, id);
928
929 /* Get the write cache status from device */
930 fsl_sata_init_wcache(dev, id);
931
932 /* Set the xfer mode to highest speed */
933 fsl_sata_set_features(dev);
934#ifdef DEBUG
935 fsl_sata_identify(dev, id);
936 ata_dump_id(id);
937#endif
938 free((void *)id);
939 return 0;
940}