blob: 208a0ae889089e11bc658a9b7508e6c10f9e9fc7 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Dave Liu6f1a8a22008-03-26 22:55:32 +08006 */
7
8#include <common.h>
9#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070010#include <console.h>
Dave Liu6f1a8a22008-03-26 22:55:32 +080011#include <asm/io.h>
Dave Liub9674d02010-04-12 14:23:25 +080012#include <asm/processor.h>
Kumar Galaeb453df2010-04-20 10:21:25 -050013#include <asm/fsl_serdes.h>
Dave Liu6f1a8a22008-03-26 22:55:32 +080014#include <malloc.h>
15#include <libata.h>
16#include <fis.h>
Pavel Herrmann9e9f6282012-09-27 23:18:04 +000017#include <sata.h>
Dave Liu6f1a8a22008-03-26 22:55:32 +080018#include "fsl_sata.h"
19
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020020#ifndef CONFIG_SYS_SATA1_FLAGS
21 #define CONFIG_SYS_SATA1_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080022#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023#ifndef CONFIG_SYS_SATA2_FLAGS
24 #define CONFIG_SYS_SATA2_FLAGS FLAGS_DMA
Dave Liu6f1a8a22008-03-26 22:55:32 +080025#endif
26
27static struct fsl_sata_info fsl_sata_info[] = {
28#ifdef CONFIG_SATA1
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020029 {CONFIG_SYS_SATA1, CONFIG_SYS_SATA1_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080030#else
31 {0, 0},
32#endif
33#ifdef CONFIG_SATA2
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020034 {CONFIG_SYS_SATA2, CONFIG_SYS_SATA2_FLAGS},
Dave Liu6f1a8a22008-03-26 22:55:32 +080035#else
36 {0, 0},
37#endif
38};
39
Dave Liu6f1a8a22008-03-26 22:55:32 +080040static inline void sdelay(unsigned long sec)
41{
42 unsigned long i;
43 for (i = 0; i < sec; i++)
44 mdelay(1000);
45}
46
galak18cf84a2009-07-07 15:53:21 -050047static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
Dave Liu6f1a8a22008-03-26 22:55:32 +080048{
49 printf("Status FIS dump:\n\r");
50 printf("fis_type: %02x\n\r", s->fis_type);
51 printf("pm_port_i: %02x\n\r", s->pm_port_i);
52 printf("status: %02x\n\r", s->status);
53 printf("error: %02x\n\r", s->error);
54 printf("lba_low: %02x\n\r", s->lba_low);
55 printf("lba_mid: %02x\n\r", s->lba_mid);
56 printf("lba_high: %02x\n\r", s->lba_high);
57 printf("device: %02x\n\r", s->device);
58 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
59 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
60 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
61 printf("res1: %02x\n\r", s->res1);
62 printf("sector_count: %02x\n\r", s->sector_count);
63 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
64}
65
Kim Phillips03487482012-10-29 13:34:40 +000066static int ata_wait_register(unsigned __iomem *addr, u32 mask,
Dave Liu6f1a8a22008-03-26 22:55:32 +080067 u32 val, u32 timeout_msec)
68{
69 int i;
70 u32 temp;
71
72 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
73 && i < timeout_msec; i++)
74 mdelay(1);
75 return (i < timeout_msec) ? 0 : -1;
76}
77
78int init_sata(int dev)
79{
80 u32 length, align;
81 cmd_hdr_tbl_t *cmd_hdr;
82 u32 cda;
83 u32 val32;
Kim Phillips03487482012-10-29 13:34:40 +000084 fsl_sata_reg_t __iomem *reg;
Dave Liu6f1a8a22008-03-26 22:55:32 +080085 u32 sig;
86 int i;
87 fsl_sata_t *sata;
88
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020089 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
Dave Liu6f1a8a22008-03-26 22:55:32 +080090 printf("the sata index %d is out of ranges\n\r", dev);
91 return -1;
92 }
93
Kumar Galaeb453df2010-04-20 10:21:25 -050094#ifdef CONFIG_MPC85xx
95 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
96 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
97 return -1;
98 }
99 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
100 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
101 return -1;
102 }
103#endif
104
Dave Liu6f1a8a22008-03-26 22:55:32 +0800105 /* Allocate SATA device driver struct */
106 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
107 if (!sata) {
108 printf("alloc the sata device struct failed\n\r");
109 return -1;
110 }
111 /* Zero all of the device driver struct */
112 memset((void *)sata, 0, sizeof(fsl_sata_t));
113
114 /* Save the private struct to block device struct */
115 sata_dev_desc[dev].priv = (void *)sata;
116
Tang Yuantian42e78942015-05-06 11:21:33 +0800117 snprintf(sata->name, 12, "SATA%d", dev);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800118
119 /* Set the controller register base address to device struct */
120 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
121 sata->reg_base = reg;
122
123 /* Allocate the command header table, 4 bytes aligned */
124 length = sizeof(struct cmd_hdr_tbl);
125 align = SATA_HC_CMD_HDR_TBL_ALIGN;
126 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
127 if (!sata) {
128 printf("alloc the command header failed\n\r");
129 return -1;
130 }
131
132 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
133 & ~(align - 1));
134 sata->cmd_hdr = cmd_hdr;
135
136 /* Zero all of the command header table */
137 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
138
139 /* Allocate command descriptor for all command */
140 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
141 align = SATA_HC_CMD_DESC_ALIGN;
142 sata->cmd_desc_offset = (void *)malloc(length + align);
143 if (!sata->cmd_desc_offset) {
144 printf("alloc the command descriptor failed\n\r");
145 return -1;
146 }
147 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
148 & ~(align - 1));
149 /* Zero all of command descriptor */
150 memset((void *)sata->cmd_desc_offset, 0, length + align);
151
152 /* Link the command descriptor to command header */
153 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
154 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
155 & ~(CMD_HDR_CDA_ALIGN - 1);
156 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
157 }
158
159 /* To have safe state, force the controller offline */
160 val32 = in_le32(&reg->hcontrol);
161 val32 &= ~HCONTROL_ONOFF;
162 val32 |= HCONTROL_FORCE_OFFLINE;
163 out_le32(&reg->hcontrol, val32);
164
165 /* Wait the controller offline */
166 ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
167
168 /* Set the command header base address to CHBA register to tell DMA */
169 out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
170
171 /* Snoop for the command header */
172 val32 = in_le32(&reg->hcontrol);
173 val32 |= HCONTROL_HDR_SNOOP;
174 out_le32(&reg->hcontrol, val32);
175
176 /* Disable all of interrupts */
177 val32 = in_le32(&reg->hcontrol);
178 val32 &= ~HCONTROL_INT_EN_ALL;
179 out_le32(&reg->hcontrol, val32);
180
181 /* Clear all of interrupts */
182 val32 = in_le32(&reg->hstatus);
183 out_le32(&reg->hstatus, val32);
184
185 /* Set the ICC, no interrupt coalescing */
186 out_le32(&reg->icc, 0x01000000);
187
188 /* No PM attatched, the SATA device direct connect */
189 out_le32(&reg->cqpmp, 0);
190
191 /* Clear SError register */
192 val32 = in_le32(&reg->serror);
193 out_le32(&reg->serror, val32);
194
195 /* Clear CER register */
196 val32 = in_le32(&reg->cer);
197 out_le32(&reg->cer, val32);
198
199 /* Clear DER register */
200 val32 = in_le32(&reg->der);
201 out_le32(&reg->der, val32);
202
203 /* No device detection or initialization action requested */
204 out_le32(&reg->scontrol, 0x00000300);
205
206 /* Configure the transport layer, default value */
207 out_le32(&reg->transcfg, 0x08000016);
208
209 /* Configure the link layer, default value */
210 out_le32(&reg->linkcfg, 0x0000ff34);
211
212 /* Bring the controller online */
213 val32 = in_le32(&reg->hcontrol);
214 val32 |= HCONTROL_ONOFF;
215 out_le32(&reg->hcontrol, val32);
216
217 mdelay(100);
218
219 /* print sata device name */
220 if (!dev)
221 printf("%s ", sata->name);
222 else
223 printf(" %s ", sata->name);
224
Dave Liu4d9c1882008-06-03 17:38:19 +0800225 /* Wait PHY RDY signal changed for 500ms */
226 ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
227 HSTATUS_PHY_RDY, 500);
228
Dave Liu6f1a8a22008-03-26 22:55:32 +0800229 /* Check PHYRDY */
230 val32 = in_le32(&reg->hstatus);
231 if (val32 & HSTATUS_PHY_RDY) {
232 sata->link = 1;
233 } else {
234 sata->link = 0;
235 printf("(No RDY)\n\r");
236 return -1;
237 }
238
Dave Liu4d9c1882008-06-03 17:38:19 +0800239 /* Wait for signature updated, which is 1st D2H */
240 ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
241 HSTATUS_SIGNATURE, 10000);
242
Dave Liu6f1a8a22008-03-26 22:55:32 +0800243 if (val32 & HSTATUS_SIGNATURE) {
244 sig = in_le32(&reg->sig);
245 debug("Signature updated, the sig =%08x\n\r", sig);
246 sata->ata_device_type = ata_dev_classify(sig);
247 }
248
249 /* Check the speed */
250 val32 = in_le32(&reg->sstatus);
251 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
252 printf("(1.5 Gbps)\n\r");
253 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
254 printf("(3 Gbps)\n\r");
255
256 return 0;
257}
258
Nikita Kiryanovb9666d62014-11-21 12:47:23 +0200259int reset_sata(int dev)
260{
261 return 0;
262}
263
Kim Phillips03487482012-10-29 13:34:40 +0000264static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800265{
266 printf("\n\rSATA: %08x\n\r", (u32)reg);
267 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
268 printf("CAR: %08x\n\r", in_le32(&reg->car));
269 printf("CCR: %08x\n\r", in_le32(&reg->ccr));
270 printf("CER: %08x\n\r", in_le32(&reg->cer));
271 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
272 printf("DER: %08x\n\r", in_le32(&reg->der));
273 printf("CHBA: %08x\n\r", in_le32(&reg->chba));
274 printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
275 printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
276 printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
277 printf("SIG: %08x\n\r", in_le32(&reg->sig));
278 printf("ICC: %08x\n\r", in_le32(&reg->icc));
279 printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
280 printf("SError: %08x\n\r", in_le32(&reg->serror));
281 printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
282 printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
283 printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
284 printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
285 printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
286 printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
287 printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
288 printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
289 printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
290 printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
291 printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
292}
293
galak18cf84a2009-07-07 15:53:21 -0500294static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800295 int is_ncq, int tag, u8 *buffer, u32 len)
296{
297 cmd_hdr_entry_t *cmd_hdr;
298 cmd_desc_t *cmd_desc;
299 sata_fis_h2d_t *h2d;
300 prd_entry_t *prde;
301 u32 ext_c_ddc;
302 u32 prde_count;
303 u32 val32;
304 u32 ttl;
Kim Phillips03487482012-10-29 13:34:40 +0000305 fsl_sata_reg_t __iomem *reg = sata->reg_base;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800306 int i;
307
308 /* Check xfer length */
309 if (len > SATA_HC_MAX_XFER_LEN) {
310 printf("max transfer length is 64MB\n\r");
311 return 0;
312 }
313
314 /* Setup the command descriptor */
315 cmd_desc = sata->cmd_desc + tag;
316
317 /* Get the pointer cfis of command descriptor */
318 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
319
320 /* Zero the cfis of command descriptor */
321 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
322
323 /* Copy the cfis from user to command descriptor */
324 h2d->fis_type = cfis->fis_type;
325 h2d->pm_port_c = cfis->pm_port_c;
326 h2d->command = cfis->command;
327
328 h2d->features = cfis->features;
329 h2d->features_exp = cfis->features_exp;
330
331 h2d->lba_low = cfis->lba_low;
332 h2d->lba_mid = cfis->lba_mid;
333 h2d->lba_high = cfis->lba_high;
334 h2d->lba_low_exp = cfis->lba_low_exp;
335 h2d->lba_mid_exp = cfis->lba_mid_exp;
336 h2d->lba_high_exp = cfis->lba_high_exp;
337
338 if (!is_ncq) {
339 h2d->sector_count = cfis->sector_count;
340 h2d->sector_count_exp = cfis->sector_count_exp;
341 } else { /* NCQ */
342 h2d->sector_count = (u8)(tag << 3);
343 }
344
345 h2d->device = cfis->device;
346 h2d->control = cfis->control;
347
348 /* Setup the PRD table */
349 prde = (prd_entry_t *)cmd_desc->prdt;
350 memset((void *)prde, 0, sizeof(struct prdt));
351
352 prde_count = 0;
353 ttl = len;
354 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
355 if (!len)
356 break;
357 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
358 debug("dba = %08x\n\r", (u32)buffer);
359
360 if (len < PRD_ENTRY_MAX_XFER_SZ) {
361 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
362 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
363 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
364 prde_count++;
365 prde++;
366 break;
367 } else {
368 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
369 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
370 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
371 buffer += PRD_ENTRY_MAX_XFER_SZ;
372 len -= PRD_ENTRY_MAX_XFER_SZ;
373 prde_count++;
374 prde++;
375 }
376 }
377
378 /* Setup the command slot of cmd hdr */
379 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
380
381 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
382
383 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
384 val32 |= sizeof(sata_fis_h2d_t);
385 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
386
387 cmd_hdr->ttl = cpu_to_le32(ttl);
388
389 if (!is_ncq) {
390 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
391 } else {
392 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
393 }
394
395 tag &= CMD_HDR_ATTR_TAG;
396 val32 |= tag;
397
398 debug("attribute = %08x\n\r", val32);
399 cmd_hdr->attribute = cpu_to_le32(val32);
400
401 /* Make sure cmd desc and cmd slot valid before commmand issue */
402 sync();
403
404 /* PMP*/
405 val32 = (u32)(h2d->pm_port_c & 0x0f);
406 out_le32(&reg->cqpmp, val32);
407
408 /* Wait no active */
409 if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
410 printf("Wait no active time out\n\r");
411
412 /* Issue command */
413 if (!(in_le32(&reg->cqr) & (1 << tag))) {
414 val32 = 1 << tag;
415 out_le32(&reg->cqr, val32);
416 }
417
418 /* Wait command completed for 10s */
419 if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
420 if (!is_ncq)
421 printf("Non-NCQ command time out\n\r");
422 else
423 printf("NCQ command time out\n\r");
424 }
425
426 val32 = in_le32(&reg->cer);
427
428 if (val32) {
429 u32 der;
galak18cf84a2009-07-07 15:53:21 -0500430 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800431 printf("CE at device\n\r");
432 fsl_sata_dump_regs(reg);
433 der = in_le32(&reg->der);
434 out_le32(&reg->cer, val32);
435 out_le32(&reg->der, der);
436 }
437
438 /* Clear complete flags */
439 val32 = in_le32(&reg->ccr);
440 out_le32(&reg->ccr, val32);
441
442 return len;
443}
444
galak18cf84a2009-07-07 15:53:21 -0500445static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800446 int tag, u8 *buffer, u32 len)
447{
448 return 0;
449}
450
galak18cf84a2009-07-07 15:53:21 -0500451static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800452 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
453{
454 int rc;
455
456 if (tag > SATA_HC_MAX_CMD || tag < 0) {
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500457 printf("tag is out of range, tag=%d\n\r", tag);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800458 return -1;
459 }
460
461 switch (command_type) {
462 case CMD_ATA:
463 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
464 return rc;
465 case CMD_RESET:
466 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
467 return rc;
468 case CMD_NCQ:
469 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
470 return rc;
471 case CMD_ATAPI:
472 case CMD_VENDOR_BIST:
473 case CMD_BIST:
474 printf("not support now\n\r");
475 return -1;
476 default:
477 break;
478 }
479
480 return -1;
481}
482
483static void fsl_sata_identify(int dev, u16 *id)
484{
485 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500486 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800487
galak18cf84a2009-07-07 15:53:21 -0500488 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800489
490 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
491 cfis->pm_port_c = 0x80; /* is command */
492 cfis->command = ATA_CMD_ID_ATA;
493
494 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
495 ata_swap_buf_le16(id, ATA_ID_WORDS);
496}
497
498static void fsl_sata_xfer_mode(int dev, u16 *id)
499{
500 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
501
502 sata->pio = id[ATA_ID_PIO_MODES];
503 sata->mwdma = id[ATA_ID_MWDMA_MODES];
504 sata->udma = id[ATA_ID_UDMA_MODES];
505 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
506}
507
508static void fsl_sata_set_features(int dev)
509{
510 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500511 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800512 u8 udma_cap;
513
galak18cf84a2009-07-07 15:53:21 -0500514 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800515
516 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
517 cfis->pm_port_c = 0x80; /* is command */
518 cfis->command = ATA_CMD_SET_FEATURES;
519 cfis->features = SETFEATURES_XFER;
520
521 /* First check the device capablity */
522 udma_cap = (u8)(sata->udma & 0xff);
523 debug("udma_cap %02x\n\r", udma_cap);
524
525 if (udma_cap == ATA_UDMA6)
526 cfis->sector_count = XFER_UDMA_6;
527 if (udma_cap == ATA_UDMA5)
528 cfis->sector_count = XFER_UDMA_5;
529 if (udma_cap == ATA_UDMA4)
530 cfis->sector_count = XFER_UDMA_4;
531 if (udma_cap == ATA_UDMA3)
532 cfis->sector_count = XFER_UDMA_3;
533
534 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
535}
536
537static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
538{
539 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500540 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800541 u32 block;
542
543 block = start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800544
galak18cf84a2009-07-07 15:53:21 -0500545 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800546
547 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
548 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800549 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800550 cfis->device = ATA_LBA;
551
552 cfis->device |= (block >> 24) & 0xf;
553 cfis->lba_high = (block >> 16) & 0xff;
554 cfis->lba_mid = (block >> 8) & 0xff;
555 cfis->lba_low = block & 0xff;
556 cfis->sector_count = (u8)(blkcnt & 0xff);
557
558 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
559 return blkcnt;
560}
561
Kim Phillips03487482012-10-29 13:34:40 +0000562static void fsl_sata_flush_cache(int dev)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800563{
564 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500565 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800566
galak18cf84a2009-07-07 15:53:21 -0500567 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800568
569 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
570 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800571 cfis->command = ATA_CMD_FLUSH;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800572
573 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
574}
575
576static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
577{
578 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500579 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800580 u64 block;
581
582 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800583
galak18cf84a2009-07-07 15:53:21 -0500584 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800585
586 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
587 cfis->pm_port_c = 0x80; /* is command */
588
Dave Liub9342a02008-04-01 15:22:11 +0800589 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
590 : ATA_CMD_READ_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800591
592 cfis->lba_high_exp = (block >> 40) & 0xff;
593 cfis->lba_mid_exp = (block >> 32) & 0xff;
594 cfis->lba_low_exp = (block >> 24) & 0xff;
595 cfis->lba_high = (block >> 16) & 0xff;
596 cfis->lba_mid = (block >> 8) & 0xff;
597 cfis->lba_low = block & 0xff;
598 cfis->device = ATA_LBA;
599 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
600 cfis->sector_count = blkcnt & 0xff;
601
602 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
603 return blkcnt;
604}
605
Kim Phillips03487482012-10-29 13:34:40 +0000606static u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer,
607 int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800608{
609 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500610 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800611 int ncq_channel;
612 u64 block;
613
Tang Yuantiana89db422011-10-03 12:18:41 -0700614 if (sata->lba48 != 1) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800615 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
616 return -1;
617 }
618
619 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800620
galak18cf84a2009-07-07 15:53:21 -0500621 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800622
623 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
624 cfis->pm_port_c = 0x80; /* is command */
625
Dave Liub9342a02008-04-01 15:22:11 +0800626 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
627 : ATA_CMD_FPDMA_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800628
629 cfis->lba_high_exp = (block >> 40) & 0xff;
630 cfis->lba_mid_exp = (block >> 32) & 0xff;
631 cfis->lba_low_exp = (block >> 24) & 0xff;
632 cfis->lba_high = (block >> 16) & 0xff;
633 cfis->lba_mid = (block >> 8) & 0xff;
634 cfis->lba_low = block & 0xff;
635
636 cfis->device = ATA_LBA;
637 cfis->features_exp = (blkcnt >> 8) & 0xff;
638 cfis->features = blkcnt & 0xff;
639
640 if (sata->queue_depth >= SATA_HC_MAX_CMD)
641 ncq_channel = SATA_HC_MAX_CMD - 1;
642 else
643 ncq_channel = sata->queue_depth - 1;
644
645 /* Use the latest queue */
646 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
647 return blkcnt;
648}
649
Kim Phillips03487482012-10-29 13:34:40 +0000650static void fsl_sata_flush_cache_ext(int dev)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800651{
652 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500653 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800654
galak18cf84a2009-07-07 15:53:21 -0500655 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800656
657 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
658 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800659 cfis->command = ATA_CMD_FLUSH_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800660
661 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
662}
663
Dave Liu6f1a8a22008-03-26 22:55:32 +0800664static void fsl_sata_init_wcache(int dev, u16 *id)
665{
666 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
667
668 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
669 sata->wcache = 1;
670 if (ata_id_has_flush(id))
671 sata->flush = 1;
672 if (ata_id_has_flush_ext(id))
673 sata->flush_ext = 1;
674}
675
676static int fsl_sata_get_wcache(int dev)
677{
678 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
679 return sata->wcache;
680}
681
682static int fsl_sata_get_flush(int dev)
683{
684 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
685 return sata->flush;
686}
687
688static int fsl_sata_get_flush_ext(int dev)
689{
690 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
691 return sata->flush_ext;
692}
693
Kim Phillips03487482012-10-29 13:34:40 +0000694static u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
Tom Rini47e68fb2012-09-29 07:55:11 -0700695 const void *buffer, int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800696{
697 u32 start, blks;
698 u8 *addr;
699 int max_blks;
700
701 start = blknr;
702 blks = blkcnt;
703 addr = (u8 *)buffer;
704
705 max_blks = ATA_MAX_SECTORS_LBA48;
706 do {
707 if (blks > max_blks) {
708 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
709 fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
710 else
711 fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
712 start += max_blks;
713 blks -= max_blks;
714 addr += ATA_SECT_SIZE * max_blks;
715 } else {
716 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
717 fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
718 else
719 fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
720 start += blks;
721 blks = 0;
722 addr += ATA_SECT_SIZE * blks;
723 }
724 } while (blks != 0);
725
726 return blkcnt;
727}
728
Kim Phillips03487482012-10-29 13:34:40 +0000729static u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt,
730 const void *buffer, int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800731{
732 u32 start, blks;
733 u8 *addr;
734 int max_blks;
735
736 start = blknr;
737 blks = blkcnt;
738 addr = (u8 *)buffer;
739
740 max_blks = ATA_MAX_SECTORS;
741 do {
742 if (blks > max_blks) {
743 fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
744 start += max_blks;
745 blks -= max_blks;
746 addr += ATA_SECT_SIZE * max_blks;
747 } else {
748 fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
749 start += blks;
750 blks = 0;
751 addr += ATA_SECT_SIZE * blks;
752 }
753 } while (blks != 0);
754
755 return blkcnt;
756}
757
758/*
759 * SATA interface between low level driver and command layer
760 */
Tom Rini47e68fb2012-09-29 07:55:11 -0700761ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800762{
763 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700764 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800765
Tang Yuantiana89db422011-10-03 12:18:41 -0700766 if (sata->lba48)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800767 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
768 else
769 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
770 return rc;
771}
772
Tom Rini47e68fb2012-09-29 07:55:11 -0700773ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800774{
775 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700776 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800777
Tang Yuantiana89db422011-10-03 12:18:41 -0700778 if (sata->lba48) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800779 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
780 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
781 fsl_sata_flush_cache_ext(dev);
782 } else {
783 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
784 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
785 fsl_sata_flush_cache(dev);
786 }
787 return rc;
788}
789
790int scan_sata(int dev)
791{
792 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
793 unsigned char serial[ATA_ID_SERNO_LEN + 1];
794 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
795 unsigned char product[ATA_ID_PROD_LEN + 1];
796 u16 *id;
797 u64 n_sectors;
798
799 /* if no detected link */
800 if (!sata->link)
801 return -1;
802
803 id = (u16 *)malloc(ATA_ID_WORDS * 2);
804 if (!id) {
805 printf("id malloc failed\n\r");
806 return -1;
807 }
808
809 /* Identify device to get information */
810 fsl_sata_identify(dev, id);
811
812 /* Serial number */
813 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
814 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
815
816 /* Firmware version */
817 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
818 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
819
820 /* Product model */
821 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
822 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
823
824 /* Totoal sectors */
825 n_sectors = ata_id_n_sectors(id);
826 sata_dev_desc[dev].lba = (u32)n_sectors;
827
Tang Yuantiana89db422011-10-03 12:18:41 -0700828#ifdef CONFIG_LBA48
Dave Liu6f1a8a22008-03-26 22:55:32 +0800829 /* Check if support LBA48 */
830 if (ata_id_has_lba48(id)) {
Tang Yuantiana89db422011-10-03 12:18:41 -0700831 sata->lba48 = 1;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800832 debug("Device support LBA48\n\r");
Tang Yuantiana89db422011-10-03 12:18:41 -0700833 } else
834 debug("Device supports LBA28\n\r");
835#endif
Dave Liu6f1a8a22008-03-26 22:55:32 +0800836
837 /* Get the NCQ queue depth from device */
838 sata->queue_depth = ata_id_queue_depth(id);
839
840 /* Get the xfer mode from device */
841 fsl_sata_xfer_mode(dev, id);
842
843 /* Get the write cache status from device */
844 fsl_sata_init_wcache(dev, id);
845
846 /* Set the xfer mode to highest speed */
847 fsl_sata_set_features(dev);
848#ifdef DEBUG
849 fsl_sata_identify(dev, id);
850 ata_dump_id(id);
851#endif
852 free((void *)id);
853 return 0;
854}