blob: fda3389e8bf69a1ad6a321f7d499384e3c7b81b7 [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>
Pavel Herrmann9e9f6282012-09-27 23:18:04 +000029#include <sata.h>
Dave Liu6f1a8a22008-03-26 22:55:32 +080030#include "fsl_sata.h"
31
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020032#ifndef CONFIG_SYS_SATA1_FLAGS
33 #define CONFIG_SYS_SATA1_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080034#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035#ifndef CONFIG_SYS_SATA2_FLAGS
36 #define CONFIG_SYS_SATA2_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080037#endif
38
39static struct fsl_sata_info fsl_sata_info[] = {
40#ifdef CONFIG_SATA1
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020041 {CONFIG_SYS_SATA1, CONFIG_SYS_SATA1_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080042#else
43 {0, 0},
44#endif
45#ifdef CONFIG_SATA2
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046 {CONFIG_SYS_SATA2, CONFIG_SYS_SATA2_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080047#else
48 {0, 0},
49#endif
50};
51
Dave Liu6f1a8a22008-03-26 22:55:32 +080052static inline void sdelay(unsigned long sec)
53{
54 unsigned long i;
55 for (i = 0; i < sec; i++)
56 mdelay(1000);
57}
58
59void dprint_buffer(unsigned char *buf, int len)
60{
61 int i, j;
62
63 i = 0;
64 j = 0;
65 printf("\n\r");
66
67 for (i = 0; i < len; i++) {
68 printf("%02x ", *buf++);
69 j++;
70 if (j == 16) {
71 printf("\n\r");
72 j = 0;
73 }
74 }
75 printf("\n\r");
76}
77
galak18cf84a2009-07-07 15:53:21 -050078static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
Dave Liu6f1a8a22008-03-26 22:55:32 +080079{
80 printf("Status FIS dump:\n\r");
81 printf("fis_type: %02x\n\r", s->fis_type);
82 printf("pm_port_i: %02x\n\r", s->pm_port_i);
83 printf("status: %02x\n\r", s->status);
84 printf("error: %02x\n\r", s->error);
85 printf("lba_low: %02x\n\r", s->lba_low);
86 printf("lba_mid: %02x\n\r", s->lba_mid);
87 printf("lba_high: %02x\n\r", s->lba_high);
88 printf("device: %02x\n\r", s->device);
89 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
90 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
91 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
92 printf("res1: %02x\n\r", s->res1);
93 printf("sector_count: %02x\n\r", s->sector_count);
94 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
95}
96
97static int ata_wait_register(volatile unsigned *addr, u32 mask,
98 u32 val, u32 timeout_msec)
99{
100 int i;
101 u32 temp;
102
103 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
104 && i < timeout_msec; i++)
105 mdelay(1);
106 return (i < timeout_msec) ? 0 : -1;
107}
108
109int init_sata(int dev)
110{
111 u32 length, align;
112 cmd_hdr_tbl_t *cmd_hdr;
113 u32 cda;
114 u32 val32;
115 fsl_sata_reg_t *reg;
116 u32 sig;
117 int i;
118 fsl_sata_t *sata;
119
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200120 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800121 printf("the sata index %d is out of ranges\n\r", dev);
122 return -1;
123 }
124
Kumar Galaeb453df2010-04-20 10:21:25 -0500125#ifdef CONFIG_MPC85xx
126 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
127 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
128 return -1;
129 }
130 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
131 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
132 return -1;
133 }
134#endif
135
Dave Liu6f1a8a22008-03-26 22:55:32 +0800136 /* Allocate SATA device driver struct */
137 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
138 if (!sata) {
139 printf("alloc the sata device struct failed\n\r");
140 return -1;
141 }
142 /* Zero all of the device driver struct */
143 memset((void *)sata, 0, sizeof(fsl_sata_t));
144
145 /* Save the private struct to block device struct */
146 sata_dev_desc[dev].priv = (void *)sata;
147
148 sprintf(sata->name, "SATA%d", dev);
149
150 /* Set the controller register base address to device struct */
151 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
152 sata->reg_base = reg;
153
154 /* Allocate the command header table, 4 bytes aligned */
155 length = sizeof(struct cmd_hdr_tbl);
156 align = SATA_HC_CMD_HDR_TBL_ALIGN;
157 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
158 if (!sata) {
159 printf("alloc the command header failed\n\r");
160 return -1;
161 }
162
163 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
164 & ~(align - 1));
165 sata->cmd_hdr = cmd_hdr;
166
167 /* Zero all of the command header table */
168 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
169
170 /* Allocate command descriptor for all command */
171 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
172 align = SATA_HC_CMD_DESC_ALIGN;
173 sata->cmd_desc_offset = (void *)malloc(length + align);
174 if (!sata->cmd_desc_offset) {
175 printf("alloc the command descriptor failed\n\r");
176 return -1;
177 }
178 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
179 & ~(align - 1));
180 /* Zero all of command descriptor */
181 memset((void *)sata->cmd_desc_offset, 0, length + align);
182
183 /* Link the command descriptor to command header */
184 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
185 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
186 & ~(CMD_HDR_CDA_ALIGN - 1);
187 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
188 }
189
190 /* To have safe state, force the controller offline */
191 val32 = in_le32(&reg->hcontrol);
192 val32 &= ~HCONTROL_ONOFF;
193 val32 |= HCONTROL_FORCE_OFFLINE;
194 out_le32(&reg->hcontrol, val32);
195
196 /* Wait the controller offline */
197 ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
198
199 /* Set the command header base address to CHBA register to tell DMA */
200 out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
201
202 /* Snoop for the command header */
203 val32 = in_le32(&reg->hcontrol);
204 val32 |= HCONTROL_HDR_SNOOP;
205 out_le32(&reg->hcontrol, val32);
206
207 /* Disable all of interrupts */
208 val32 = in_le32(&reg->hcontrol);
209 val32 &= ~HCONTROL_INT_EN_ALL;
210 out_le32(&reg->hcontrol, val32);
211
212 /* Clear all of interrupts */
213 val32 = in_le32(&reg->hstatus);
214 out_le32(&reg->hstatus, val32);
215
216 /* Set the ICC, no interrupt coalescing */
217 out_le32(&reg->icc, 0x01000000);
218
219 /* No PM attatched, the SATA device direct connect */
220 out_le32(&reg->cqpmp, 0);
221
222 /* Clear SError register */
223 val32 = in_le32(&reg->serror);
224 out_le32(&reg->serror, val32);
225
226 /* Clear CER register */
227 val32 = in_le32(&reg->cer);
228 out_le32(&reg->cer, val32);
229
230 /* Clear DER register */
231 val32 = in_le32(&reg->der);
232 out_le32(&reg->der, val32);
233
234 /* No device detection or initialization action requested */
235 out_le32(&reg->scontrol, 0x00000300);
236
237 /* Configure the transport layer, default value */
238 out_le32(&reg->transcfg, 0x08000016);
239
240 /* Configure the link layer, default value */
241 out_le32(&reg->linkcfg, 0x0000ff34);
242
243 /* Bring the controller online */
244 val32 = in_le32(&reg->hcontrol);
245 val32 |= HCONTROL_ONOFF;
246 out_le32(&reg->hcontrol, val32);
247
248 mdelay(100);
249
250 /* print sata device name */
251 if (!dev)
252 printf("%s ", sata->name);
253 else
254 printf(" %s ", sata->name);
255
Dave Liu4d9c1882008-06-03 17:38:19 +0800256 /* Wait PHY RDY signal changed for 500ms */
257 ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
258 HSTATUS_PHY_RDY, 500);
259
Dave Liu6f1a8a22008-03-26 22:55:32 +0800260 /* Check PHYRDY */
261 val32 = in_le32(&reg->hstatus);
262 if (val32 & HSTATUS_PHY_RDY) {
263 sata->link = 1;
264 } else {
265 sata->link = 0;
266 printf("(No RDY)\n\r");
267 return -1;
268 }
269
Dave Liu4d9c1882008-06-03 17:38:19 +0800270 /* Wait for signature updated, which is 1st D2H */
271 ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
272 HSTATUS_SIGNATURE, 10000);
273
Dave Liu6f1a8a22008-03-26 22:55:32 +0800274 if (val32 & HSTATUS_SIGNATURE) {
275 sig = in_le32(&reg->sig);
276 debug("Signature updated, the sig =%08x\n\r", sig);
277 sata->ata_device_type = ata_dev_classify(sig);
278 }
279
280 /* Check the speed */
281 val32 = in_le32(&reg->sstatus);
282 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
283 printf("(1.5 Gbps)\n\r");
284 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
285 printf("(3 Gbps)\n\r");
286
287 return 0;
288}
289
290/* Hardware reset, like Power-on and COMRESET */
291void fsl_sata_hardware_reset(u32 reg_base)
292{
293 fsl_sata_reg_t *reg = (fsl_sata_reg_t *)reg_base;
294 u32 scontrol;
295
296 /* Disable the SATA interface and put PHY offline */
297 scontrol = in_le32(&reg->scontrol);
298 scontrol = (scontrol & 0x0f0) | 0x304;
299 out_le32(&reg->scontrol, scontrol);
300
301 /* No speed strict */
302 scontrol = in_le32(&reg->scontrol);
303 scontrol = scontrol & ~0x0f0;
304 out_le32(&reg->scontrol, scontrol);
305
306 /* Issue PHY wake/reset, Hardware_reset_asserted */
307 scontrol = in_le32(&reg->scontrol);
308 scontrol = (scontrol & 0x0f0) | 0x301;
309 out_le32(&reg->scontrol, scontrol);
310
311 mdelay(100);
312
313 /* Resume PHY, COMRESET negated, the device initialize hardware
314 * and execute diagnostics, send good status-signature to host,
315 * which is D2H register FIS, and then the device enter idle state.
316 */
317 scontrol = in_le32(&reg->scontrol);
318 scontrol = (scontrol & 0x0f0) | 0x300;
319 out_le32(&reg->scontrol, scontrol);
320
321 mdelay(100);
322 return;
323}
324
325static void fsl_sata_dump_regs(fsl_sata_reg_t *reg)
326{
327 printf("\n\rSATA: %08x\n\r", (u32)reg);
328 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
329 printf("CAR: %08x\n\r", in_le32(&reg->car));
330 printf("CCR: %08x\n\r", in_le32(&reg->ccr));
331 printf("CER: %08x\n\r", in_le32(&reg->cer));
332 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
333 printf("DER: %08x\n\r", in_le32(&reg->der));
334 printf("CHBA: %08x\n\r", in_le32(&reg->chba));
335 printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
336 printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
337 printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
338 printf("SIG: %08x\n\r", in_le32(&reg->sig));
339 printf("ICC: %08x\n\r", in_le32(&reg->icc));
340 printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
341 printf("SError: %08x\n\r", in_le32(&reg->serror));
342 printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
343 printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
344 printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
345 printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
346 printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
347 printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
348 printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
349 printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
350 printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
351 printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
352 printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
353}
354
galak18cf84a2009-07-07 15:53:21 -0500355static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800356 int is_ncq, int tag, u8 *buffer, u32 len)
357{
358 cmd_hdr_entry_t *cmd_hdr;
359 cmd_desc_t *cmd_desc;
360 sata_fis_h2d_t *h2d;
361 prd_entry_t *prde;
362 u32 ext_c_ddc;
363 u32 prde_count;
364 u32 val32;
365 u32 ttl;
366 fsl_sata_reg_t *reg = sata->reg_base;
367 int i;
368
369 /* Check xfer length */
370 if (len > SATA_HC_MAX_XFER_LEN) {
371 printf("max transfer length is 64MB\n\r");
372 return 0;
373 }
374
375 /* Setup the command descriptor */
376 cmd_desc = sata->cmd_desc + tag;
377
378 /* Get the pointer cfis of command descriptor */
379 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
380
381 /* Zero the cfis of command descriptor */
382 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
383
384 /* Copy the cfis from user to command descriptor */
385 h2d->fis_type = cfis->fis_type;
386 h2d->pm_port_c = cfis->pm_port_c;
387 h2d->command = cfis->command;
388
389 h2d->features = cfis->features;
390 h2d->features_exp = cfis->features_exp;
391
392 h2d->lba_low = cfis->lba_low;
393 h2d->lba_mid = cfis->lba_mid;
394 h2d->lba_high = cfis->lba_high;
395 h2d->lba_low_exp = cfis->lba_low_exp;
396 h2d->lba_mid_exp = cfis->lba_mid_exp;
397 h2d->lba_high_exp = cfis->lba_high_exp;
398
399 if (!is_ncq) {
400 h2d->sector_count = cfis->sector_count;
401 h2d->sector_count_exp = cfis->sector_count_exp;
402 } else { /* NCQ */
403 h2d->sector_count = (u8)(tag << 3);
404 }
405
406 h2d->device = cfis->device;
407 h2d->control = cfis->control;
408
409 /* Setup the PRD table */
410 prde = (prd_entry_t *)cmd_desc->prdt;
411 memset((void *)prde, 0, sizeof(struct prdt));
412
413 prde_count = 0;
414 ttl = len;
415 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
416 if (!len)
417 break;
418 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
419 debug("dba = %08x\n\r", (u32)buffer);
420
421 if (len < PRD_ENTRY_MAX_XFER_SZ) {
422 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
423 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
424 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
425 prde_count++;
426 prde++;
427 break;
428 } else {
429 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
430 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
431 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
432 buffer += PRD_ENTRY_MAX_XFER_SZ;
433 len -= PRD_ENTRY_MAX_XFER_SZ;
434 prde_count++;
435 prde++;
436 }
437 }
438
439 /* Setup the command slot of cmd hdr */
440 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
441
442 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
443
444 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
445 val32 |= sizeof(sata_fis_h2d_t);
446 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
447
448 cmd_hdr->ttl = cpu_to_le32(ttl);
449
450 if (!is_ncq) {
451 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
452 } else {
453 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
454 }
455
456 tag &= CMD_HDR_ATTR_TAG;
457 val32 |= tag;
458
459 debug("attribute = %08x\n\r", val32);
460 cmd_hdr->attribute = cpu_to_le32(val32);
461
462 /* Make sure cmd desc and cmd slot valid before commmand issue */
463 sync();
464
465 /* PMP*/
466 val32 = (u32)(h2d->pm_port_c & 0x0f);
467 out_le32(&reg->cqpmp, val32);
468
469 /* Wait no active */
470 if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
471 printf("Wait no active time out\n\r");
472
473 /* Issue command */
474 if (!(in_le32(&reg->cqr) & (1 << tag))) {
475 val32 = 1 << tag;
476 out_le32(&reg->cqr, val32);
477 }
478
479 /* Wait command completed for 10s */
480 if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
481 if (!is_ncq)
482 printf("Non-NCQ command time out\n\r");
483 else
484 printf("NCQ command time out\n\r");
485 }
486
487 val32 = in_le32(&reg->cer);
488
489 if (val32) {
490 u32 der;
galak18cf84a2009-07-07 15:53:21 -0500491 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800492 printf("CE at device\n\r");
493 fsl_sata_dump_regs(reg);
494 der = in_le32(&reg->der);
495 out_le32(&reg->cer, val32);
496 out_le32(&reg->der, der);
497 }
498
499 /* Clear complete flags */
500 val32 = in_le32(&reg->ccr);
501 out_le32(&reg->ccr, val32);
502
503 return len;
504}
505
galak18cf84a2009-07-07 15:53:21 -0500506static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800507 int tag, u8 *buffer, u32 len)
508{
509 return 0;
510}
511
galak18cf84a2009-07-07 15:53:21 -0500512static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800513 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
514{
515 int rc;
516
517 if (tag > SATA_HC_MAX_CMD || tag < 0) {
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500518 printf("tag is out of range, tag=%d\n\r", tag);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800519 return -1;
520 }
521
522 switch (command_type) {
523 case CMD_ATA:
524 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
525 return rc;
526 case CMD_RESET:
527 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
528 return rc;
529 case CMD_NCQ:
530 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
531 return rc;
532 case CMD_ATAPI:
533 case CMD_VENDOR_BIST:
534 case CMD_BIST:
535 printf("not support now\n\r");
536 return -1;
537 default:
538 break;
539 }
540
541 return -1;
542}
543
544static void fsl_sata_identify(int dev, u16 *id)
545{
546 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500547 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800548
galak18cf84a2009-07-07 15:53:21 -0500549 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800550
551 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
552 cfis->pm_port_c = 0x80; /* is command */
553 cfis->command = ATA_CMD_ID_ATA;
554
555 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
556 ata_swap_buf_le16(id, ATA_ID_WORDS);
557}
558
559static void fsl_sata_xfer_mode(int dev, u16 *id)
560{
561 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
562
563 sata->pio = id[ATA_ID_PIO_MODES];
564 sata->mwdma = id[ATA_ID_MWDMA_MODES];
565 sata->udma = id[ATA_ID_UDMA_MODES];
566 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
567}
568
569static void fsl_sata_set_features(int dev)
570{
571 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500572 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800573 u8 udma_cap;
574
galak18cf84a2009-07-07 15:53:21 -0500575 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800576
577 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
578 cfis->pm_port_c = 0x80; /* is command */
579 cfis->command = ATA_CMD_SET_FEATURES;
580 cfis->features = SETFEATURES_XFER;
581
582 /* First check the device capablity */
583 udma_cap = (u8)(sata->udma & 0xff);
584 debug("udma_cap %02x\n\r", udma_cap);
585
586 if (udma_cap == ATA_UDMA6)
587 cfis->sector_count = XFER_UDMA_6;
588 if (udma_cap == ATA_UDMA5)
589 cfis->sector_count = XFER_UDMA_5;
590 if (udma_cap == ATA_UDMA4)
591 cfis->sector_count = XFER_UDMA_4;
592 if (udma_cap == ATA_UDMA3)
593 cfis->sector_count = XFER_UDMA_3;
594
595 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
596}
597
598static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
599{
600 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500601 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800602 u32 block;
603
604 block = start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800605
galak18cf84a2009-07-07 15:53:21 -0500606 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800607
608 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
609 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800610 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800611 cfis->device = ATA_LBA;
612
613 cfis->device |= (block >> 24) & 0xf;
614 cfis->lba_high = (block >> 16) & 0xff;
615 cfis->lba_mid = (block >> 8) & 0xff;
616 cfis->lba_low = block & 0xff;
617 cfis->sector_count = (u8)(blkcnt & 0xff);
618
619 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
620 return blkcnt;
621}
622
623void fsl_sata_flush_cache(int dev)
624{
625 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500626 struct sata_fis_h2d h2d, *cfis = &h2d;
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 = ATA_CMD_FLUSH;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800633
634 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
635}
636
637static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
638{
639 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500640 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800641 u64 block;
642
643 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800644
galak18cf84a2009-07-07 15:53:21 -0500645 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800646
647 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
648 cfis->pm_port_c = 0x80; /* is command */
649
Dave Liub9342a02008-04-01 15:22:11 +0800650 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
651 : ATA_CMD_READ_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800652
653 cfis->lba_high_exp = (block >> 40) & 0xff;
654 cfis->lba_mid_exp = (block >> 32) & 0xff;
655 cfis->lba_low_exp = (block >> 24) & 0xff;
656 cfis->lba_high = (block >> 16) & 0xff;
657 cfis->lba_mid = (block >> 8) & 0xff;
658 cfis->lba_low = block & 0xff;
659 cfis->device = ATA_LBA;
660 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
661 cfis->sector_count = blkcnt & 0xff;
662
663 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
664 return blkcnt;
665}
666
667u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
668{
669 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500670 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800671 int ncq_channel;
672 u64 block;
673
Tang Yuantiana89db422011-10-03 12:18:41 -0700674 if (sata->lba48 != 1) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800675 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
676 return -1;
677 }
678
679 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800680
galak18cf84a2009-07-07 15:53:21 -0500681 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800682
683 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
684 cfis->pm_port_c = 0x80; /* is command */
685
Dave Liub9342a02008-04-01 15:22:11 +0800686 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
687 : ATA_CMD_FPDMA_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800688
689 cfis->lba_high_exp = (block >> 40) & 0xff;
690 cfis->lba_mid_exp = (block >> 32) & 0xff;
691 cfis->lba_low_exp = (block >> 24) & 0xff;
692 cfis->lba_high = (block >> 16) & 0xff;
693 cfis->lba_mid = (block >> 8) & 0xff;
694 cfis->lba_low = block & 0xff;
695
696 cfis->device = ATA_LBA;
697 cfis->features_exp = (blkcnt >> 8) & 0xff;
698 cfis->features = blkcnt & 0xff;
699
700 if (sata->queue_depth >= SATA_HC_MAX_CMD)
701 ncq_channel = SATA_HC_MAX_CMD - 1;
702 else
703 ncq_channel = sata->queue_depth - 1;
704
705 /* Use the latest queue */
706 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
707 return blkcnt;
708}
709
710void fsl_sata_flush_cache_ext(int dev)
711{
712 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500713 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800714
galak18cf84a2009-07-07 15:53:21 -0500715 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800716
717 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
718 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800719 cfis->command = ATA_CMD_FLUSH_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800720
721 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
722}
723
724/* Software reset, set SRST of the Device Control register */
725void fsl_sata_software_reset(int dev)
726{
727 return;
728}
729
730static void fsl_sata_init_wcache(int dev, u16 *id)
731{
732 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
733
734 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
735 sata->wcache = 1;
736 if (ata_id_has_flush(id))
737 sata->flush = 1;
738 if (ata_id_has_flush_ext(id))
739 sata->flush_ext = 1;
740}
741
742static int fsl_sata_get_wcache(int dev)
743{
744 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
745 return sata->wcache;
746}
747
748static int fsl_sata_get_flush(int dev)
749{
750 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
751 return sata->flush;
752}
753
754static int fsl_sata_get_flush_ext(int dev)
755{
756 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
757 return sata->flush_ext;
758}
759
Tom Rini47e68fb2012-09-29 07:55:11 -0700760u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
761 const void *buffer, int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800762{
763 u32 start, blks;
764 u8 *addr;
765 int max_blks;
766
767 start = blknr;
768 blks = blkcnt;
769 addr = (u8 *)buffer;
770
771 max_blks = ATA_MAX_SECTORS_LBA48;
772 do {
773 if (blks > max_blks) {
774 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
775 fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
776 else
777 fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
778 start += max_blks;
779 blks -= max_blks;
780 addr += ATA_SECT_SIZE * max_blks;
781 } else {
782 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
783 fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
784 else
785 fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
786 start += blks;
787 blks = 0;
788 addr += ATA_SECT_SIZE * blks;
789 }
790 } while (blks != 0);
791
792 return blkcnt;
793}
794
Tom Rini47e68fb2012-09-29 07:55:11 -0700795u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt, const void *buffer,
796 int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800797{
798 u32 start, blks;
799 u8 *addr;
800 int max_blks;
801
802 start = blknr;
803 blks = blkcnt;
804 addr = (u8 *)buffer;
805
806 max_blks = ATA_MAX_SECTORS;
807 do {
808 if (blks > max_blks) {
809 fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
810 start += max_blks;
811 blks -= max_blks;
812 addr += ATA_SECT_SIZE * max_blks;
813 } else {
814 fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
815 start += blks;
816 blks = 0;
817 addr += ATA_SECT_SIZE * blks;
818 }
819 } while (blks != 0);
820
821 return blkcnt;
822}
823
824/*
825 * SATA interface between low level driver and command layer
826 */
Tom Rini47e68fb2012-09-29 07:55:11 -0700827ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800828{
829 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700830 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800831
Tang Yuantiana89db422011-10-03 12:18:41 -0700832 if (sata->lba48)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800833 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
834 else
835 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
836 return rc;
837}
838
Tom Rini47e68fb2012-09-29 07:55:11 -0700839ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800840{
841 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700842 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800843
Tang Yuantiana89db422011-10-03 12:18:41 -0700844 if (sata->lba48) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800845 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
846 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
847 fsl_sata_flush_cache_ext(dev);
848 } else {
849 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
850 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
851 fsl_sata_flush_cache(dev);
852 }
853 return rc;
854}
855
856int scan_sata(int dev)
857{
858 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
859 unsigned char serial[ATA_ID_SERNO_LEN + 1];
860 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
861 unsigned char product[ATA_ID_PROD_LEN + 1];
862 u16 *id;
863 u64 n_sectors;
864
865 /* if no detected link */
866 if (!sata->link)
867 return -1;
868
869 id = (u16 *)malloc(ATA_ID_WORDS * 2);
870 if (!id) {
871 printf("id malloc failed\n\r");
872 return -1;
873 }
874
875 /* Identify device to get information */
876 fsl_sata_identify(dev, id);
877
878 /* Serial number */
879 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
880 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
881
882 /* Firmware version */
883 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
884 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
885
886 /* Product model */
887 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
888 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
889
890 /* Totoal sectors */
891 n_sectors = ata_id_n_sectors(id);
892 sata_dev_desc[dev].lba = (u32)n_sectors;
893
Tang Yuantiana89db422011-10-03 12:18:41 -0700894#ifdef CONFIG_LBA48
Dave Liu6f1a8a22008-03-26 22:55:32 +0800895 /* Check if support LBA48 */
896 if (ata_id_has_lba48(id)) {
Tang Yuantiana89db422011-10-03 12:18:41 -0700897 sata->lba48 = 1;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800898 debug("Device support LBA48\n\r");
Tang Yuantiana89db422011-10-03 12:18:41 -0700899 } else
900 debug("Device supports LBA28\n\r");
901#endif
Dave Liu6f1a8a22008-03-26 22:55:32 +0800902
903 /* Get the NCQ queue depth from device */
904 sata->queue_depth = ata_id_queue_depth(id);
905
906 /* Get the xfer mode from device */
907 fsl_sata_xfer_mode(dev, id);
908
909 /* Get the write cache status from device */
910 fsl_sata_init_wcache(dev, id);
911
912 /* Set the xfer mode to highest speed */
913 fsl_sata_set_features(dev);
914#ifdef DEBUG
915 fsl_sata_identify(dev, id);
916 ata_dump_id(id);
917#endif
918 free((void *)id);
919 return 0;
920}