blob: 1f9d7b097c6a0f8737da8fa24fd6222344d3703c [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
galak18cf84a2009-07-07 15:53:21 -050059static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
Dave Liu6f1a8a22008-03-26 22:55:32 +080060{
61 printf("Status FIS dump:\n\r");
62 printf("fis_type: %02x\n\r", s->fis_type);
63 printf("pm_port_i: %02x\n\r", s->pm_port_i);
64 printf("status: %02x\n\r", s->status);
65 printf("error: %02x\n\r", s->error);
66 printf("lba_low: %02x\n\r", s->lba_low);
67 printf("lba_mid: %02x\n\r", s->lba_mid);
68 printf("lba_high: %02x\n\r", s->lba_high);
69 printf("device: %02x\n\r", s->device);
70 printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
71 printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
72 printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
73 printf("res1: %02x\n\r", s->res1);
74 printf("sector_count: %02x\n\r", s->sector_count);
75 printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
76}
77
Kim Phillips03487482012-10-29 13:34:40 +000078static int ata_wait_register(unsigned __iomem *addr, u32 mask,
Dave Liu6f1a8a22008-03-26 22:55:32 +080079 u32 val, u32 timeout_msec)
80{
81 int i;
82 u32 temp;
83
84 for (i = 0; (((temp = in_le32(addr)) & mask) != val)
85 && i < timeout_msec; i++)
86 mdelay(1);
87 return (i < timeout_msec) ? 0 : -1;
88}
89
90int init_sata(int dev)
91{
92 u32 length, align;
93 cmd_hdr_tbl_t *cmd_hdr;
94 u32 cda;
95 u32 val32;
Kim Phillips03487482012-10-29 13:34:40 +000096 fsl_sata_reg_t __iomem *reg;
Dave Liu6f1a8a22008-03-26 22:55:32 +080097 u32 sig;
98 int i;
99 fsl_sata_t *sata;
100
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200101 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800102 printf("the sata index %d is out of ranges\n\r", dev);
103 return -1;
104 }
105
Kumar Galaeb453df2010-04-20 10:21:25 -0500106#ifdef CONFIG_MPC85xx
107 if ((dev == 0) && (!is_serdes_configured(SATA1))) {
108 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
109 return -1;
110 }
111 if ((dev == 1) && (!is_serdes_configured(SATA2))) {
112 printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
113 return -1;
114 }
115#endif
116
Dave Liu6f1a8a22008-03-26 22:55:32 +0800117 /* Allocate SATA device driver struct */
118 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
119 if (!sata) {
120 printf("alloc the sata device struct failed\n\r");
121 return -1;
122 }
123 /* Zero all of the device driver struct */
124 memset((void *)sata, 0, sizeof(fsl_sata_t));
125
126 /* Save the private struct to block device struct */
127 sata_dev_desc[dev].priv = (void *)sata;
128
129 sprintf(sata->name, "SATA%d", dev);
130
131 /* Set the controller register base address to device struct */
132 reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
133 sata->reg_base = reg;
134
135 /* Allocate the command header table, 4 bytes aligned */
136 length = sizeof(struct cmd_hdr_tbl);
137 align = SATA_HC_CMD_HDR_TBL_ALIGN;
138 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
139 if (!sata) {
140 printf("alloc the command header failed\n\r");
141 return -1;
142 }
143
144 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
145 & ~(align - 1));
146 sata->cmd_hdr = cmd_hdr;
147
148 /* Zero all of the command header table */
149 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
150
151 /* Allocate command descriptor for all command */
152 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
153 align = SATA_HC_CMD_DESC_ALIGN;
154 sata->cmd_desc_offset = (void *)malloc(length + align);
155 if (!sata->cmd_desc_offset) {
156 printf("alloc the command descriptor failed\n\r");
157 return -1;
158 }
159 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
160 & ~(align - 1));
161 /* Zero all of command descriptor */
162 memset((void *)sata->cmd_desc_offset, 0, length + align);
163
164 /* Link the command descriptor to command header */
165 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
166 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
167 & ~(CMD_HDR_CDA_ALIGN - 1);
168 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
169 }
170
171 /* To have safe state, force the controller offline */
172 val32 = in_le32(&reg->hcontrol);
173 val32 &= ~HCONTROL_ONOFF;
174 val32 |= HCONTROL_FORCE_OFFLINE;
175 out_le32(&reg->hcontrol, val32);
176
177 /* Wait the controller offline */
178 ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
179
180 /* Set the command header base address to CHBA register to tell DMA */
181 out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
182
183 /* Snoop for the command header */
184 val32 = in_le32(&reg->hcontrol);
185 val32 |= HCONTROL_HDR_SNOOP;
186 out_le32(&reg->hcontrol, val32);
187
188 /* Disable all of interrupts */
189 val32 = in_le32(&reg->hcontrol);
190 val32 &= ~HCONTROL_INT_EN_ALL;
191 out_le32(&reg->hcontrol, val32);
192
193 /* Clear all of interrupts */
194 val32 = in_le32(&reg->hstatus);
195 out_le32(&reg->hstatus, val32);
196
197 /* Set the ICC, no interrupt coalescing */
198 out_le32(&reg->icc, 0x01000000);
199
200 /* No PM attatched, the SATA device direct connect */
201 out_le32(&reg->cqpmp, 0);
202
203 /* Clear SError register */
204 val32 = in_le32(&reg->serror);
205 out_le32(&reg->serror, val32);
206
207 /* Clear CER register */
208 val32 = in_le32(&reg->cer);
209 out_le32(&reg->cer, val32);
210
211 /* Clear DER register */
212 val32 = in_le32(&reg->der);
213 out_le32(&reg->der, val32);
214
215 /* No device detection or initialization action requested */
216 out_le32(&reg->scontrol, 0x00000300);
217
218 /* Configure the transport layer, default value */
219 out_le32(&reg->transcfg, 0x08000016);
220
221 /* Configure the link layer, default value */
222 out_le32(&reg->linkcfg, 0x0000ff34);
223
224 /* Bring the controller online */
225 val32 = in_le32(&reg->hcontrol);
226 val32 |= HCONTROL_ONOFF;
227 out_le32(&reg->hcontrol, val32);
228
229 mdelay(100);
230
231 /* print sata device name */
232 if (!dev)
233 printf("%s ", sata->name);
234 else
235 printf(" %s ", sata->name);
236
Dave Liu4d9c1882008-06-03 17:38:19 +0800237 /* Wait PHY RDY signal changed for 500ms */
238 ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
239 HSTATUS_PHY_RDY, 500);
240
Dave Liu6f1a8a22008-03-26 22:55:32 +0800241 /* Check PHYRDY */
242 val32 = in_le32(&reg->hstatus);
243 if (val32 & HSTATUS_PHY_RDY) {
244 sata->link = 1;
245 } else {
246 sata->link = 0;
247 printf("(No RDY)\n\r");
248 return -1;
249 }
250
Dave Liu4d9c1882008-06-03 17:38:19 +0800251 /* Wait for signature updated, which is 1st D2H */
252 ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
253 HSTATUS_SIGNATURE, 10000);
254
Dave Liu6f1a8a22008-03-26 22:55:32 +0800255 if (val32 & HSTATUS_SIGNATURE) {
256 sig = in_le32(&reg->sig);
257 debug("Signature updated, the sig =%08x\n\r", sig);
258 sata->ata_device_type = ata_dev_classify(sig);
259 }
260
261 /* Check the speed */
262 val32 = in_le32(&reg->sstatus);
263 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
264 printf("(1.5 Gbps)\n\r");
265 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
266 printf("(3 Gbps)\n\r");
267
268 return 0;
269}
270
Kim Phillips03487482012-10-29 13:34:40 +0000271static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800272{
273 printf("\n\rSATA: %08x\n\r", (u32)reg);
274 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
275 printf("CAR: %08x\n\r", in_le32(&reg->car));
276 printf("CCR: %08x\n\r", in_le32(&reg->ccr));
277 printf("CER: %08x\n\r", in_le32(&reg->cer));
278 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
279 printf("DER: %08x\n\r", in_le32(&reg->der));
280 printf("CHBA: %08x\n\r", in_le32(&reg->chba));
281 printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
282 printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
283 printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
284 printf("SIG: %08x\n\r", in_le32(&reg->sig));
285 printf("ICC: %08x\n\r", in_le32(&reg->icc));
286 printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
287 printf("SError: %08x\n\r", in_le32(&reg->serror));
288 printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
289 printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
290 printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
291 printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
292 printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
293 printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
294 printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
295 printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
296 printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
297 printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
298 printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
299}
300
galak18cf84a2009-07-07 15:53:21 -0500301static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800302 int is_ncq, int tag, u8 *buffer, u32 len)
303{
304 cmd_hdr_entry_t *cmd_hdr;
305 cmd_desc_t *cmd_desc;
306 sata_fis_h2d_t *h2d;
307 prd_entry_t *prde;
308 u32 ext_c_ddc;
309 u32 prde_count;
310 u32 val32;
311 u32 ttl;
Kim Phillips03487482012-10-29 13:34:40 +0000312 fsl_sata_reg_t __iomem *reg = sata->reg_base;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800313 int i;
314
315 /* Check xfer length */
316 if (len > SATA_HC_MAX_XFER_LEN) {
317 printf("max transfer length is 64MB\n\r");
318 return 0;
319 }
320
321 /* Setup the command descriptor */
322 cmd_desc = sata->cmd_desc + tag;
323
324 /* Get the pointer cfis of command descriptor */
325 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
326
327 /* Zero the cfis of command descriptor */
328 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
329
330 /* Copy the cfis from user to command descriptor */
331 h2d->fis_type = cfis->fis_type;
332 h2d->pm_port_c = cfis->pm_port_c;
333 h2d->command = cfis->command;
334
335 h2d->features = cfis->features;
336 h2d->features_exp = cfis->features_exp;
337
338 h2d->lba_low = cfis->lba_low;
339 h2d->lba_mid = cfis->lba_mid;
340 h2d->lba_high = cfis->lba_high;
341 h2d->lba_low_exp = cfis->lba_low_exp;
342 h2d->lba_mid_exp = cfis->lba_mid_exp;
343 h2d->lba_high_exp = cfis->lba_high_exp;
344
345 if (!is_ncq) {
346 h2d->sector_count = cfis->sector_count;
347 h2d->sector_count_exp = cfis->sector_count_exp;
348 } else { /* NCQ */
349 h2d->sector_count = (u8)(tag << 3);
350 }
351
352 h2d->device = cfis->device;
353 h2d->control = cfis->control;
354
355 /* Setup the PRD table */
356 prde = (prd_entry_t *)cmd_desc->prdt;
357 memset((void *)prde, 0, sizeof(struct prdt));
358
359 prde_count = 0;
360 ttl = len;
361 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
362 if (!len)
363 break;
364 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
365 debug("dba = %08x\n\r", (u32)buffer);
366
367 if (len < PRD_ENTRY_MAX_XFER_SZ) {
368 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
369 debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
370 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
371 prde_count++;
372 prde++;
373 break;
374 } else {
375 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
376 debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
377 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
378 buffer += PRD_ENTRY_MAX_XFER_SZ;
379 len -= PRD_ENTRY_MAX_XFER_SZ;
380 prde_count++;
381 prde++;
382 }
383 }
384
385 /* Setup the command slot of cmd hdr */
386 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
387
388 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
389
390 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
391 val32 |= sizeof(sata_fis_h2d_t);
392 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
393
394 cmd_hdr->ttl = cpu_to_le32(ttl);
395
396 if (!is_ncq) {
397 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
398 } else {
399 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
400 }
401
402 tag &= CMD_HDR_ATTR_TAG;
403 val32 |= tag;
404
405 debug("attribute = %08x\n\r", val32);
406 cmd_hdr->attribute = cpu_to_le32(val32);
407
408 /* Make sure cmd desc and cmd slot valid before commmand issue */
409 sync();
410
411 /* PMP*/
412 val32 = (u32)(h2d->pm_port_c & 0x0f);
413 out_le32(&reg->cqpmp, val32);
414
415 /* Wait no active */
416 if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
417 printf("Wait no active time out\n\r");
418
419 /* Issue command */
420 if (!(in_le32(&reg->cqr) & (1 << tag))) {
421 val32 = 1 << tag;
422 out_le32(&reg->cqr, val32);
423 }
424
425 /* Wait command completed for 10s */
426 if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
427 if (!is_ncq)
428 printf("Non-NCQ command time out\n\r");
429 else
430 printf("NCQ command time out\n\r");
431 }
432
433 val32 = in_le32(&reg->cer);
434
435 if (val32) {
436 u32 der;
galak18cf84a2009-07-07 15:53:21 -0500437 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800438 printf("CE at device\n\r");
439 fsl_sata_dump_regs(reg);
440 der = in_le32(&reg->der);
441 out_le32(&reg->cer, val32);
442 out_le32(&reg->der, der);
443 }
444
445 /* Clear complete flags */
446 val32 = in_le32(&reg->ccr);
447 out_le32(&reg->ccr, val32);
448
449 return len;
450}
451
galak18cf84a2009-07-07 15:53:21 -0500452static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800453 int tag, u8 *buffer, u32 len)
454{
455 return 0;
456}
457
galak18cf84a2009-07-07 15:53:21 -0500458static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
Dave Liu6f1a8a22008-03-26 22:55:32 +0800459 enum cmd_type command_type, int tag, u8 *buffer, u32 len)
460{
461 int rc;
462
463 if (tag > SATA_HC_MAX_CMD || tag < 0) {
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500464 printf("tag is out of range, tag=%d\n\r", tag);
Dave Liu6f1a8a22008-03-26 22:55:32 +0800465 return -1;
466 }
467
468 switch (command_type) {
469 case CMD_ATA:
470 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
471 return rc;
472 case CMD_RESET:
473 rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
474 return rc;
475 case CMD_NCQ:
476 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
477 return rc;
478 case CMD_ATAPI:
479 case CMD_VENDOR_BIST:
480 case CMD_BIST:
481 printf("not support now\n\r");
482 return -1;
483 default:
484 break;
485 }
486
487 return -1;
488}
489
490static void fsl_sata_identify(int dev, u16 *id)
491{
492 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500493 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800494
galak18cf84a2009-07-07 15:53:21 -0500495 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800496
497 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
498 cfis->pm_port_c = 0x80; /* is command */
499 cfis->command = ATA_CMD_ID_ATA;
500
501 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
502 ata_swap_buf_le16(id, ATA_ID_WORDS);
503}
504
505static void fsl_sata_xfer_mode(int dev, u16 *id)
506{
507 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
508
509 sata->pio = id[ATA_ID_PIO_MODES];
510 sata->mwdma = id[ATA_ID_MWDMA_MODES];
511 sata->udma = id[ATA_ID_UDMA_MODES];
512 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
513}
514
515static void fsl_sata_set_features(int dev)
516{
517 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500518 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800519 u8 udma_cap;
520
galak18cf84a2009-07-07 15:53:21 -0500521 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800522
523 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
524 cfis->pm_port_c = 0x80; /* is command */
525 cfis->command = ATA_CMD_SET_FEATURES;
526 cfis->features = SETFEATURES_XFER;
527
528 /* First check the device capablity */
529 udma_cap = (u8)(sata->udma & 0xff);
530 debug("udma_cap %02x\n\r", udma_cap);
531
532 if (udma_cap == ATA_UDMA6)
533 cfis->sector_count = XFER_UDMA_6;
534 if (udma_cap == ATA_UDMA5)
535 cfis->sector_count = XFER_UDMA_5;
536 if (udma_cap == ATA_UDMA4)
537 cfis->sector_count = XFER_UDMA_4;
538 if (udma_cap == ATA_UDMA3)
539 cfis->sector_count = XFER_UDMA_3;
540
541 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
542}
543
544static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
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 u32 block;
549
550 block = start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800551
galak18cf84a2009-07-07 15:53:21 -0500552 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800553
554 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
555 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800556 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800557 cfis->device = ATA_LBA;
558
559 cfis->device |= (block >> 24) & 0xf;
560 cfis->lba_high = (block >> 16) & 0xff;
561 cfis->lba_mid = (block >> 8) & 0xff;
562 cfis->lba_low = block & 0xff;
563 cfis->sector_count = (u8)(blkcnt & 0xff);
564
565 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
566 return blkcnt;
567}
568
Kim Phillips03487482012-10-29 13:34:40 +0000569static void fsl_sata_flush_cache(int dev)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800570{
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
galak18cf84a2009-07-07 15:53:21 -0500574 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800575
576 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
577 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800578 cfis->command = ATA_CMD_FLUSH;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800579
580 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
581}
582
583static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
584{
585 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500586 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800587 u64 block;
588
589 block = (u64)start;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800590
galak18cf84a2009-07-07 15:53:21 -0500591 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800592
593 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
594 cfis->pm_port_c = 0x80; /* is command */
595
Dave Liub9342a02008-04-01 15:22:11 +0800596 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
597 : ATA_CMD_READ_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800598
599 cfis->lba_high_exp = (block >> 40) & 0xff;
600 cfis->lba_mid_exp = (block >> 32) & 0xff;
601 cfis->lba_low_exp = (block >> 24) & 0xff;
602 cfis->lba_high = (block >> 16) & 0xff;
603 cfis->lba_mid = (block >> 8) & 0xff;
604 cfis->lba_low = block & 0xff;
605 cfis->device = ATA_LBA;
606 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
607 cfis->sector_count = blkcnt & 0xff;
608
609 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
610 return blkcnt;
611}
612
Kim Phillips03487482012-10-29 13:34:40 +0000613static u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer,
614 int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800615{
616 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500617 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800618 int ncq_channel;
619 u64 block;
620
Tang Yuantiana89db422011-10-03 12:18:41 -0700621 if (sata->lba48 != 1) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800622 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
623 return -1;
624 }
625
626 block = (u64)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 */
632
Dave Liub9342a02008-04-01 15:22:11 +0800633 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
634 : ATA_CMD_FPDMA_READ;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800635
636 cfis->lba_high_exp = (block >> 40) & 0xff;
637 cfis->lba_mid_exp = (block >> 32) & 0xff;
638 cfis->lba_low_exp = (block >> 24) & 0xff;
639 cfis->lba_high = (block >> 16) & 0xff;
640 cfis->lba_mid = (block >> 8) & 0xff;
641 cfis->lba_low = block & 0xff;
642
643 cfis->device = ATA_LBA;
644 cfis->features_exp = (blkcnt >> 8) & 0xff;
645 cfis->features = blkcnt & 0xff;
646
647 if (sata->queue_depth >= SATA_HC_MAX_CMD)
648 ncq_channel = SATA_HC_MAX_CMD - 1;
649 else
650 ncq_channel = sata->queue_depth - 1;
651
652 /* Use the latest queue */
653 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
654 return blkcnt;
655}
656
Kim Phillips03487482012-10-29 13:34:40 +0000657static void fsl_sata_flush_cache_ext(int dev)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800658{
659 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
galak18cf84a2009-07-07 15:53:21 -0500660 struct sata_fis_h2d h2d, *cfis = &h2d;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800661
galak18cf84a2009-07-07 15:53:21 -0500662 memset(cfis, 0, sizeof(struct sata_fis_h2d));
Dave Liu6f1a8a22008-03-26 22:55:32 +0800663
664 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
665 cfis->pm_port_c = 0x80; /* is command */
Dave Liub9342a02008-04-01 15:22:11 +0800666 cfis->command = ATA_CMD_FLUSH_EXT;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800667
668 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
669}
670
Dave Liu6f1a8a22008-03-26 22:55:32 +0800671static void fsl_sata_init_wcache(int dev, u16 *id)
672{
673 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
674
675 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
676 sata->wcache = 1;
677 if (ata_id_has_flush(id))
678 sata->flush = 1;
679 if (ata_id_has_flush_ext(id))
680 sata->flush_ext = 1;
681}
682
683static int fsl_sata_get_wcache(int dev)
684{
685 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
686 return sata->wcache;
687}
688
689static int fsl_sata_get_flush(int dev)
690{
691 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
692 return sata->flush;
693}
694
695static int fsl_sata_get_flush_ext(int dev)
696{
697 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
698 return sata->flush_ext;
699}
700
Kim Phillips03487482012-10-29 13:34:40 +0000701static u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
Tom Rini47e68fb2012-09-29 07:55:11 -0700702 const void *buffer, int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800703{
704 u32 start, blks;
705 u8 *addr;
706 int max_blks;
707
708 start = blknr;
709 blks = blkcnt;
710 addr = (u8 *)buffer;
711
712 max_blks = ATA_MAX_SECTORS_LBA48;
713 do {
714 if (blks > max_blks) {
715 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
716 fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
717 else
718 fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
719 start += max_blks;
720 blks -= max_blks;
721 addr += ATA_SECT_SIZE * max_blks;
722 } else {
723 if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
724 fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
725 else
726 fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
727 start += blks;
728 blks = 0;
729 addr += ATA_SECT_SIZE * blks;
730 }
731 } while (blks != 0);
732
733 return blkcnt;
734}
735
Kim Phillips03487482012-10-29 13:34:40 +0000736static u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt,
737 const void *buffer, int is_write)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800738{
739 u32 start, blks;
740 u8 *addr;
741 int max_blks;
742
743 start = blknr;
744 blks = blkcnt;
745 addr = (u8 *)buffer;
746
747 max_blks = ATA_MAX_SECTORS;
748 do {
749 if (blks > max_blks) {
750 fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
751 start += max_blks;
752 blks -= max_blks;
753 addr += ATA_SECT_SIZE * max_blks;
754 } else {
755 fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
756 start += blks;
757 blks = 0;
758 addr += ATA_SECT_SIZE * blks;
759 }
760 } while (blks != 0);
761
762 return blkcnt;
763}
764
765/*
766 * SATA interface between low level driver and command layer
767 */
Tom Rini47e68fb2012-09-29 07:55:11 -0700768ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800769{
770 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700771 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800772
Tang Yuantiana89db422011-10-03 12:18:41 -0700773 if (sata->lba48)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800774 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
775 else
776 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
777 return rc;
778}
779
Tom Rini47e68fb2012-09-29 07:55:11 -0700780ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Dave Liu6f1a8a22008-03-26 22:55:32 +0800781{
782 u32 rc;
Tang Yuantiana89db422011-10-03 12:18:41 -0700783 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800784
Tang Yuantiana89db422011-10-03 12:18:41 -0700785 if (sata->lba48) {
Dave Liu6f1a8a22008-03-26 22:55:32 +0800786 rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
787 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
788 fsl_sata_flush_cache_ext(dev);
789 } else {
790 rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
791 if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
792 fsl_sata_flush_cache(dev);
793 }
794 return rc;
795}
796
797int scan_sata(int dev)
798{
799 fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
800 unsigned char serial[ATA_ID_SERNO_LEN + 1];
801 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
802 unsigned char product[ATA_ID_PROD_LEN + 1];
803 u16 *id;
804 u64 n_sectors;
805
806 /* if no detected link */
807 if (!sata->link)
808 return -1;
809
810 id = (u16 *)malloc(ATA_ID_WORDS * 2);
811 if (!id) {
812 printf("id malloc failed\n\r");
813 return -1;
814 }
815
816 /* Identify device to get information */
817 fsl_sata_identify(dev, id);
818
819 /* Serial number */
820 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
821 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
822
823 /* Firmware version */
824 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
825 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
826
827 /* Product model */
828 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
829 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
830
831 /* Totoal sectors */
832 n_sectors = ata_id_n_sectors(id);
833 sata_dev_desc[dev].lba = (u32)n_sectors;
834
Tang Yuantiana89db422011-10-03 12:18:41 -0700835#ifdef CONFIG_LBA48
Dave Liu6f1a8a22008-03-26 22:55:32 +0800836 /* Check if support LBA48 */
837 if (ata_id_has_lba48(id)) {
Tang Yuantiana89db422011-10-03 12:18:41 -0700838 sata->lba48 = 1;
Dave Liu6f1a8a22008-03-26 22:55:32 +0800839 debug("Device support LBA48\n\r");
Tang Yuantiana89db422011-10-03 12:18:41 -0700840 } else
841 debug("Device supports LBA28\n\r");
842#endif
Dave Liu6f1a8a22008-03-26 22:55:32 +0800843
844 /* Get the NCQ queue depth from device */
845 sata->queue_depth = ata_id_queue_depth(id);
846
847 /* Get the xfer mode from device */
848 fsl_sata_xfer_mode(dev, id);
849
850 /* Get the write cache status from device */
851 fsl_sata_init_wcache(dev, id);
852
853 /* Set the xfer mode to highest speed */
854 fsl_sata_set_features(dev);
855#ifdef DEBUG
856 fsl_sata_identify(dev, id);
857 ata_dump_id(id);
858#endif
859 free((void *)id);
860 return 0;
861}