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