blob: d04cff3ee7177a5b8031f9a2131cc2862d0ac21a [file] [log] [blame]
Peng Ma34228532019-03-27 09:23:23 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * NXP PPC SATA platform driver
4 *
5 * (C) Copyright 2019 NXP, Inc.
6 *
7 */
8#include <common.h>
9#include <asm/fsl_serdes.h>
10#include <dm/lists.h>
11#include <dm.h>
12#include <ahci.h>
13#include <scsi.h>
14#include <libata.h>
15#include <sata.h>
16#include <malloc.h>
17#include <memalign.h>
18#include <fis.h>
19
20#include "fsl_sata.h"
21
22struct fsl_ahci_priv {
23 u32 base;
24 u32 flag;
25 u32 number;
26 fsl_sata_t *fsl_sata;
27};
28
29static int fsl_ahci_bind(struct udevice *dev)
30{
31 return device_bind_driver(dev, "fsl_ahci_scsi", "fsl_ahci_scsi", NULL);
32}
33
34static int fsl_ahci_ofdata_to_platdata(struct udevice *dev)
35{
36 struct fsl_ahci_priv *priv = dev_get_priv(dev);
37
38 priv->number = dev_read_u32_default(dev, "sata-number", -1);
39 priv->flag = dev_read_u32_default(dev, "sata-fpdma", -1);
40
41 priv->base = dev_read_addr(dev);
42 if (priv->base == FDT_ADDR_T_NONE)
43 return -EINVAL;
44
45 return 0;
46}
47
48static int ata_wait_register(unsigned __iomem *addr, u32 mask,
49 u32 val, u32 timeout_msec)
50{
51 int i;
52
53 for (i = 0; ((in_le32(addr) & mask) != val) && i < timeout_msec; i++)
54 mdelay(1);
55
56 return (i < timeout_msec) ? 0 : -1;
57}
58
59static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
60{
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
78static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg)
79{
80 printf("\n\rSATA: %08x\n\r", (u32)reg);
81 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
82 printf("CAR: %08x\n\r", in_le32(&reg->car));
83 printf("CCR: %08x\n\r", in_le32(&reg->ccr));
84 printf("CER: %08x\n\r", in_le32(&reg->cer));
85 printf("CQR: %08x\n\r", in_le32(&reg->cqr));
86 printf("DER: %08x\n\r", in_le32(&reg->der));
87 printf("CHBA: %08x\n\r", in_le32(&reg->chba));
88 printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
89 printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
90 printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
91 printf("SIG: %08x\n\r", in_le32(&reg->sig));
92 printf("ICC: %08x\n\r", in_le32(&reg->icc));
93 printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
94 printf("SError: %08x\n\r", in_le32(&reg->serror));
95 printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
96 printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
97 printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
98 printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
99 printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
100 printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
101 printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
102 printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
103 printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
104 printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
105 printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
106}
107
108static int init_sata(struct fsl_ahci_priv *priv)
109{
110 int i;
111 u32 cda;
112 u32 val32;
113 u32 sig;
114 fsl_sata_t *sata;
115 u32 length, align;
116 cmd_hdr_tbl_t *cmd_hdr;
117 fsl_sata_reg_t __iomem *reg;
118
119 int dev = priv->number;
120
121 if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
122 printf("the sata index %d is out of ranges\n\r", dev);
123 return -EINVAL;
124 }
125
126#ifdef CONFIG_MPC85xx
127 if (dev == 0 && (!is_serdes_configured(SATA1))) {
128 printf("SATA%d [dev = %d] is not enabled\n", dev + 1, dev);
129 return -EINVAL;
130 }
131 if (dev == 1 && (!is_serdes_configured(SATA2))) {
132 printf("SATA%d [dev = %d] is not enabled\n", dev + 1, dev);
133 return -EINVAL;
134 }
135#endif
136
137 /* Allocate SATA device driver struct */
138 sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
139 if (!sata) {
140 printf("alloc the sata device struct failed\n\r");
141 return -ENOMEM;
142 }
143 /* Zero all of the device driver struct */
144 memset((void *)sata, 0, sizeof(fsl_sata_t));
145
146 sata->dma_flag = priv->flag;
147 snprintf(sata->name, 12, "SATA%d", dev);
148
149 /* Set the controller register base address to device struct */
150 reg = (fsl_sata_reg_t *)priv->base;
151 sata->reg_base = reg;
152
153 /* Allocate the command header table, 4 bytes aligned */
154 length = sizeof(struct cmd_hdr_tbl);
155 align = SATA_HC_CMD_HDR_TBL_ALIGN;
156 sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
157 if (!sata->cmd_hdr_tbl_offset) {
158 printf("alloc the command header failed\n\r");
159 return -ENOMEM;
160 }
161
162 cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
163 & ~(align - 1));
164 sata->cmd_hdr = cmd_hdr;
165
166 /* Zero all of the command header table */
167 memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
168
169 /* Allocate command descriptor for all command */
170 length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
171 align = SATA_HC_CMD_DESC_ALIGN;
172 sata->cmd_desc_offset = (void *)malloc(length + align);
173 if (!sata->cmd_desc_offset) {
174 printf("alloc the command descriptor failed\n\r");
175 return -ENOMEM;
176 }
177 sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
178 & ~(align - 1));
179 /* Zero all of command descriptor */
180 memset((void *)sata->cmd_desc_offset, 0, length + align);
181
182 /* Link the command descriptor to command header */
183 for (i = 0; i < SATA_HC_MAX_CMD; i++) {
184 cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
185 & ~(CMD_HDR_CDA_ALIGN - 1);
186 cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
187 }
188
189 /* To have safe state, force the controller offline */
190 val32 = in_le32(&reg->hcontrol);
191 val32 &= ~HCONTROL_ONOFF;
192 val32 |= HCONTROL_FORCE_OFFLINE;
193 out_le32(&reg->hcontrol, val32);
194
195 /* Wait the controller offline */
196 ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
197
198 /* Set the command header base address to CHBA register to tell DMA */
199 out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
200
201 /* Snoop for the command header */
202 val32 = in_le32(&reg->hcontrol);
203 val32 |= HCONTROL_HDR_SNOOP;
204 out_le32(&reg->hcontrol, val32);
205
206 /* Disable all of interrupts */
207 val32 = in_le32(&reg->hcontrol);
208 val32 &= ~HCONTROL_INT_EN_ALL;
209 out_le32(&reg->hcontrol, val32);
210
211 /* Clear all of interrupts */
212 val32 = in_le32(&reg->hstatus);
213 out_le32(&reg->hstatus, val32);
214
215 /* Set the ICC, no interrupt coalescing */
216 out_le32(&reg->icc, 0x01000000);
217
218 /* No PM attatched, the SATA device direct connect */
219 out_le32(&reg->cqpmp, 0);
220
221 /* Clear SError register */
222 val32 = in_le32(&reg->serror);
223 out_le32(&reg->serror, val32);
224
225 /* Clear CER register */
226 val32 = in_le32(&reg->cer);
227 out_le32(&reg->cer, val32);
228
229 /* Clear DER register */
230 val32 = in_le32(&reg->der);
231 out_le32(&reg->der, val32);
232
233 /* No device detection or initialization action requested */
234 out_le32(&reg->scontrol, 0x00000300);
235
236 /* Configure the transport layer, default value */
237 out_le32(&reg->transcfg, 0x08000016);
238
239 /* Configure the link layer, default value */
240 out_le32(&reg->linkcfg, 0x0000ff34);
241
242 /* Bring the controller online */
243 val32 = in_le32(&reg->hcontrol);
244 val32 |= HCONTROL_ONOFF;
245 out_le32(&reg->hcontrol, val32);
246
247 mdelay(100);
248
249 /* print sata device name */
250 printf("%s ", sata->name);
251
252 /* Wait PHY RDY signal changed for 500ms */
253 ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
254 HSTATUS_PHY_RDY, 500);
255
256 /* Check PHYRDY */
257 val32 = in_le32(&reg->hstatus);
258 if (val32 & HSTATUS_PHY_RDY) {
259 sata->link = 1;
260 } else {
261 sata->link = 0;
262 printf("(No RDY)\n\r");
263 return -EINVAL;
264 }
265
266 /* Wait for signature updated, which is 1st D2H */
267 ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
268 HSTATUS_SIGNATURE, 10000);
269
270 if (val32 & HSTATUS_SIGNATURE) {
271 sig = in_le32(&reg->sig);
272 debug("Signature updated, the sig =%08x\n\r", sig);
273 sata->ata_device_type = ata_dev_classify(sig);
274 }
275
276 /* Check the speed */
277 val32 = in_le32(&reg->sstatus);
278 if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
279 printf("(1.5 Gbps)\n\r");
280 else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
281 printf("(3 Gbps)\n\r");
282
283 priv->fsl_sata = sata;
284
285 return 0;
286}
287
288static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata,
289 struct sata_fis_h2d *cfis,
290 int is_ncq, int tag,
291 u8 *buffer, u32 len)
292{
293 cmd_hdr_entry_t *cmd_hdr;
294 cmd_desc_t *cmd_desc;
295 sata_fis_h2d_t *h2d;
296 prd_entry_t *prde;
297 u32 ext_c_ddc;
298 u32 prde_count;
299 u32 val32;
300 u32 ttl;
301 u32 der;
302 int i;
303
304 fsl_sata_reg_t *reg = sata->reg_base;
305
306 /* Check xfer length */
307 if (len > SATA_HC_MAX_XFER_LEN) {
308 printf("max transfer length is 64MB\n\r");
309 return 0;
310 }
311
312 /* Setup the command descriptor */
313 cmd_desc = sata->cmd_desc + tag;
314
315 /* Get the pointer cfis of command descriptor */
316 h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
317
318 /* Zero the cfis of command descriptor */
319 memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
320
321 /* Copy the cfis from user to command descriptor */
322 h2d->fis_type = cfis->fis_type;
323 h2d->pm_port_c = cfis->pm_port_c;
324 h2d->command = cfis->command;
325
326 h2d->features = cfis->features;
327 h2d->features_exp = cfis->features_exp;
328
329 h2d->lba_low = cfis->lba_low;
330 h2d->lba_mid = cfis->lba_mid;
331 h2d->lba_high = cfis->lba_high;
332 h2d->lba_low_exp = cfis->lba_low_exp;
333 h2d->lba_mid_exp = cfis->lba_mid_exp;
334 h2d->lba_high_exp = cfis->lba_high_exp;
335
336 if (!is_ncq) {
337 h2d->sector_count = cfis->sector_count;
338 h2d->sector_count_exp = cfis->sector_count_exp;
339 } else { /* NCQ */
340 h2d->sector_count = (u8)(tag << 3);
341 }
342
343 h2d->device = cfis->device;
344 h2d->control = cfis->control;
345
346 /* Setup the PRD table */
347 prde = (prd_entry_t *)cmd_desc->prdt;
348 memset((void *)prde, 0, sizeof(struct prdt));
349
350 prde_count = 0;
351 ttl = len;
352 for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
353 if (!len)
354 break;
355 prde->dba = cpu_to_le32((u32)buffer & ~0x3);
356 debug("dba = %08x\n\r", (u32)buffer);
357
358 if (len < PRD_ENTRY_MAX_XFER_SZ) {
359 ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
360 debug("ext_c_ddc1 = %08x, len = %08x\n\r",
361 ext_c_ddc, len);
362 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
363 prde_count++;
364 prde++;
365 } else {
366 ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
367 debug("ext_c_ddc2 = %08x, len = %08x\n\r",
368 ext_c_ddc, len);
369 prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
370 buffer += PRD_ENTRY_MAX_XFER_SZ;
371 len -= PRD_ENTRY_MAX_XFER_SZ;
372 prde_count++;
373 prde++;
374 }
375 }
376
377 /* Setup the command slot of cmd hdr */
378 cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
379
380 cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
381
382 val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
383 val32 |= sizeof(sata_fis_h2d_t);
384 cmd_hdr->prde_fis_len = cpu_to_le32(val32);
385
386 cmd_hdr->ttl = cpu_to_le32(ttl);
387
388 if (!is_ncq)
389 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
390 else
391 val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP |
392 CMD_HDR_ATTR_FPDMA;
393
394 tag &= CMD_HDR_ATTR_TAG;
395 val32 |= tag;
396
397 debug("attribute = %08x\n\r", val32);
398 cmd_hdr->attribute = cpu_to_le32(val32);
399
400 /* Make sure cmd desc and cmd slot valid before command issue */
401 sync();
402
403 /* PMP*/
404 val32 = (u32)(h2d->pm_port_c & 0x0f);
405 out_le32(&reg->cqpmp, val32);
406
407 /* Wait no active */
408 if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
409 printf("Wait no active time out\n\r");
410
411 /* Issue command */
412 if (!(in_le32(&reg->cqr) & (1 << tag))) {
413 val32 = 1 << tag;
414 out_le32(&reg->cqr, val32);
415 }
416
417 /* Wait command completed for 10s */
418 if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
419 if (!is_ncq)
420 printf("Non-NCQ command time out\n\r");
421 else
422 printf("NCQ command time out\n\r");
423 }
424
425 val32 = in_le32(&reg->cer);
426
427 if (val32) {
428 fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
429 printf("CE at device\n\r");
430 fsl_sata_dump_regs(reg);
431 der = in_le32(&reg->der);
432 out_le32(&reg->cer, val32);
433 out_le32(&reg->der, der);
434 }
435
436 /* Clear complete flags */
437 val32 = in_le32(&reg->ccr);
438 out_le32(&reg->ccr, val32);
439
440 return len;
441}
442
443static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
444 enum cmd_type command_type, int tag, u8 *buffer,
445 u32 len)
446{
447 int rc;
448
449 if (tag > SATA_HC_MAX_CMD || tag < 0) {
450 printf("tag is out of range, tag=%d\n\r", tag);
451 return -1;
452 }
453
454 switch (command_type) {
455 case CMD_ATA:
456 rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
457 return rc;
458 case CMD_NCQ:
459 rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
460 return rc;
461 case CMD_ATAPI:
462 case CMD_VENDOR_BIST:
463 case CMD_BIST:
464 printf("not support now\n\r");
465 return -1;
466 default:
467 break;
468 }
469
470 return -1;
471}
472
473static void fsl_sata_identify(fsl_sata_t *sata, u16 *id)
474{
475 struct sata_fis_h2d h2d, *cfis = &h2d;
476
477 memset(cfis, 0, sizeof(struct sata_fis_h2d));
478
479 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
480 cfis->pm_port_c = 0x80; /* is command */
481 cfis->command = ATA_CMD_ID_ATA;
482
483 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
484 ata_swap_buf_le16(id, ATA_ID_WORDS);
485}
486
487static void fsl_sata_xfer_mode(fsl_sata_t *sata, u16 *id)
488{
489 sata->pio = id[ATA_ID_PIO_MODES];
490 sata->mwdma = id[ATA_ID_MWDMA_MODES];
491 sata->udma = id[ATA_ID_UDMA_MODES];
492 debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio,
493 sata->mwdma, sata->udma);
494}
495
496static void fsl_sata_init_wcache(fsl_sata_t *sata, u16 *id)
497{
498 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
499 sata->wcache = 1;
500 if (ata_id_has_flush(id))
501 sata->flush = 1;
502 if (ata_id_has_flush_ext(id))
503 sata->flush_ext = 1;
504}
505
506static void fsl_sata_set_features(fsl_sata_t *sata)
507{
508 struct sata_fis_h2d h2d, *cfis = &h2d;
509 u8 udma_cap;
510
511 memset(cfis, 0, sizeof(struct sata_fis_h2d));
512
513 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
514 cfis->pm_port_c = 0x80; /* is command */
515 cfis->command = ATA_CMD_SET_FEATURES;
516 cfis->features = SETFEATURES_XFER;
517
518 /* First check the device capablity */
519 udma_cap = (u8)(sata->udma & 0xff);
520 debug("udma_cap %02x\n\r", udma_cap);
521
522 if (udma_cap == ATA_UDMA6)
523 cfis->sector_count = XFER_UDMA_6;
524 if (udma_cap == ATA_UDMA5)
525 cfis->sector_count = XFER_UDMA_5;
526 if (udma_cap == ATA_UDMA4)
527 cfis->sector_count = XFER_UDMA_4;
528 if (udma_cap == ATA_UDMA3)
529 cfis->sector_count = XFER_UDMA_3;
530
531 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
532}
533
534static u32 fsl_sata_rw_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt,
535 u8 *buffer, int is_write)
536{
537 struct sata_fis_h2d h2d, *cfis = &h2d;
538 u32 block;
539
540 block = start;
541
542 memset(cfis, 0, sizeof(struct sata_fis_h2d));
543
544 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
545 cfis->pm_port_c = 0x80; /* is command */
546 cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
547 cfis->device = ATA_LBA;
548
549 cfis->device |= (block >> 24) & 0xf;
550 cfis->lba_high = (block >> 16) & 0xff;
551 cfis->lba_mid = (block >> 8) & 0xff;
552 cfis->lba_low = block & 0xff;
553 cfis->sector_count = (u8)(blkcnt & 0xff);
554
555 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer,
556 ATA_SECT_SIZE * blkcnt);
557 return blkcnt;
558}
559
560static void fsl_sata_flush_cache(fsl_sata_t *sata)
561{
562 struct sata_fis_h2d h2d, *cfis = &h2d;
563
564 memset(cfis, 0, sizeof(struct sata_fis_h2d));
565
566 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
567 cfis->pm_port_c = 0x80; /* is command */
568 cfis->command = ATA_CMD_FLUSH;
569
570 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
571}
572
573static u32 fsl_sata_rw_cmd_ext(fsl_sata_t *sata, u32 start,
574 u32 blkcnt, u8 *buffer, int is_write)
575{
576 struct sata_fis_h2d h2d, *cfis = &h2d;
577 u64 block;
578
579 block = (u64)start;
580
581 memset(cfis, 0, sizeof(struct sata_fis_h2d));
582
583 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
584 cfis->pm_port_c = 0x80; /* is command */
585
586 cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
587 : ATA_CMD_READ_EXT;
588
589 cfis->lba_high_exp = (block >> 40) & 0xff;
590 cfis->lba_mid_exp = (block >> 32) & 0xff;
591 cfis->lba_low_exp = (block >> 24) & 0xff;
592 cfis->lba_high = (block >> 16) & 0xff;
593 cfis->lba_mid = (block >> 8) & 0xff;
594 cfis->lba_low = block & 0xff;
595 cfis->device = ATA_LBA;
596 cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
597 cfis->sector_count = blkcnt & 0xff;
598
599 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer,
600 ATA_SECT_SIZE * blkcnt);
601 return blkcnt;
602}
603
604static u32 fsl_sata_rw_ncq_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt,
605 u8 *buffer,
606 int is_write)
607{
608 struct sata_fis_h2d h2d, *cfis = &h2d;
609 int ncq_channel;
610 u64 block;
611
612 if (sata->lba48 != 1) {
613 printf("execute FPDMA command on non-LBA48 hard disk\n\r");
614 return -1;
615 }
616
617 block = (u64)start;
618
619 memset(cfis, 0, sizeof(struct sata_fis_h2d));
620
621 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
622 cfis->pm_port_c = 0x80; /* is command */
623
624 cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
625 : ATA_CMD_FPDMA_READ;
626
627 cfis->lba_high_exp = (block >> 40) & 0xff;
628 cfis->lba_mid_exp = (block >> 32) & 0xff;
629 cfis->lba_low_exp = (block >> 24) & 0xff;
630 cfis->lba_high = (block >> 16) & 0xff;
631 cfis->lba_mid = (block >> 8) & 0xff;
632 cfis->lba_low = block & 0xff;
633
634 cfis->device = ATA_LBA;
635 cfis->features_exp = (blkcnt >> 8) & 0xff;
636 cfis->features = blkcnt & 0xff;
637
638 if (sata->queue_depth >= SATA_HC_MAX_CMD)
639 ncq_channel = SATA_HC_MAX_CMD - 1;
640 else
641 ncq_channel = sata->queue_depth - 1;
642
643 /* Use the latest queue */
644 fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer,
645 ATA_SECT_SIZE * blkcnt);
646 return blkcnt;
647}
648
649static void fsl_sata_flush_cache_ext(fsl_sata_t *sata)
650{
651 struct sata_fis_h2d h2d, *cfis = &h2d;
652
653 memset(cfis, 0, sizeof(struct sata_fis_h2d));
654
655 cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
656 cfis->pm_port_c = 0x80; /* is command */
657 cfis->command = ATA_CMD_FLUSH_EXT;
658
659 fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
660}
661
662static u32 ata_low_level_rw_lba48(fsl_sata_t *sata, u32 blknr, lbaint_t blkcnt,
663 const void *buffer, int is_write)
664{
665 u32 start, blks;
666 u8 *addr;
667 int max_blks;
668
669 start = blknr;
670 blks = blkcnt;
671 addr = (u8 *)buffer;
672
673 max_blks = ATA_MAX_SECTORS_LBA48;
674 do {
675 if (blks > max_blks) {
676 if (sata->dma_flag != FLAGS_FPDMA)
677 fsl_sata_rw_cmd_ext(sata, start, max_blks,
678 addr, is_write);
679 else
680 fsl_sata_rw_ncq_cmd(sata, start, max_blks,
681 addr, is_write);
682 start += max_blks;
683 blks -= max_blks;
684 addr += ATA_SECT_SIZE * max_blks;
685 } else {
686 if (sata->dma_flag != FLAGS_FPDMA)
687 fsl_sata_rw_cmd_ext(sata, start, blks,
688 addr, is_write);
689 else
690 fsl_sata_rw_ncq_cmd(sata, start, blks,
691 addr, is_write);
692 start += blks;
693 blks = 0;
694 addr += ATA_SECT_SIZE * blks;
695 }
696 } while (blks != 0);
697
698 return blks;
699}
700
701static u32 ata_low_level_rw_lba28(fsl_sata_t *sata, u32 blknr, u32 blkcnt,
702 const void *buffer, int is_write)
703{
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;
713 do {
714 if (blks > max_blks) {
715 fsl_sata_rw_cmd(sata, start, max_blks, addr, is_write);
716 start += max_blks;
717 blks -= max_blks;
718 addr += ATA_SECT_SIZE * max_blks;
719 } else {
720 fsl_sata_rw_cmd(sata, start, blks, addr, is_write);
721 start += blks;
722 blks = 0;
723 addr += ATA_SECT_SIZE * blks;
724 }
725 } while (blks != 0);
726
727 return blks;
728}
729
730/*
731 * SATA interface between low level driver and command layer
732 */
733static int sata_read(fsl_sata_t *sata, ulong blknr, lbaint_t blkcnt,
734 void *buffer)
735{
736 u32 rc;
737
738 if (sata->lba48)
739 rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
740 READ_CMD);
741 else
742 rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
743 READ_CMD);
744 return rc;
745}
746
747static int sata_write(fsl_sata_t *sata, ulong blknr, lbaint_t blkcnt,
748 const void *buffer)
749{
750 u32 rc;
751
752 if (sata->lba48) {
753 rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
754 WRITE_CMD);
755 if (sata->wcache && sata->flush_ext)
756 fsl_sata_flush_cache_ext(sata);
757 } else {
758 rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
759 WRITE_CMD);
760 if (sata->wcache && sata->flush)
761 fsl_sata_flush_cache(sata);
762 }
763
764 return rc;
765}
766
767int sata_getinfo(fsl_sata_t *sata, u16 *id)
768{
769 /* if no detected link */
770 if (!sata->link)
771 return -EINVAL;
772
773#ifdef CONFIG_LBA48
774 /* Check if support LBA48 */
775 if (ata_id_has_lba48(id)) {
776 sata->lba48 = 1;
777 debug("Device support LBA48\n\r");
778 } else {
779 debug("Device supports LBA28\n\r");
780 }
781#endif
782
783 /* Get the NCQ queue depth from device */
784 sata->queue_depth = ata_id_queue_depth(id);
785
786 /* Get the xfer mode from device */
787 fsl_sata_xfer_mode(sata, id);
788
789 /* Get the write cache status from device */
790 fsl_sata_init_wcache(sata, id);
791
792 /* Set the xfer mode to highest speed */
793 fsl_sata_set_features(sata);
794
795 return 0;
796}
797
798static int fsl_scsi_exec(fsl_sata_t *sata, struct scsi_cmd *pccb,
799 bool is_write)
800{
801 int ret;
802 u32 temp;
803 u16 blocks = 0;
804 lbaint_t start = 0;
805 u8 *buffer = pccb->pdata;
806
807 /* Retrieve the base LBA number from the ccb structure. */
808 if (pccb->cmd[0] == SCSI_READ16) {
809 memcpy(&start, pccb->cmd + 2, 8);
810 start = be64_to_cpu(start);
811 } else {
812 memcpy(&temp, pccb->cmd + 2, 4);
813 start = be32_to_cpu(temp);
814 }
815
816 if (pccb->cmd[0] == SCSI_READ16)
817 blocks = (((u16)pccb->cmd[13]) << 8) | ((u16)pccb->cmd[14]);
818 else
819 blocks = (((u16)pccb->cmd[7]) << 8) | ((u16)pccb->cmd[8]);
820
821 debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "\n",
822 is_write ? "write" : "read", blocks, start);
823
824 if (is_write)
825 ret = sata_write(sata, start, blocks, buffer);
826 else
827 ret = sata_read(sata, start, blocks, buffer);
828
829 return ret;
830}
831
832static char *fsl_ata_id_strcpy(u16 *target, u16 *src, int len)
833{
834 int i;
835
836 for (i = 0; i < len / 2; i++)
837 target[i] = src[i];
838
839 return (char *)target;
840}
841
842static int fsl_ata_scsiop_inquiry(struct ahci_uc_priv *uc_priv,
843 struct scsi_cmd *pccb,
844 fsl_sata_t *sata)
845{
846 u8 port;
847 u16 *idbuf;
848
849 ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
850
851 /* Clean ccb data buffer */
852 memset(pccb->pdata, 0, pccb->datalen);
853
854 if (pccb->datalen <= 35)
855 return 0;
856
857 /* Read id from sata */
858 port = pccb->target;
859
860 fsl_sata_identify(sata, (u16 *)tmpid);
861
862 if (!uc_priv->ataid[port]) {
863 uc_priv->ataid[port] = malloc(ATA_ID_WORDS * 2);
864 if (!uc_priv->ataid[port]) {
865 printf("%s: No memory for ataid[port]\n", __func__);
866 return -ENOMEM;
867 }
868 }
869
870 idbuf = uc_priv->ataid[port];
871
872 memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
873
874 memcpy(&pccb->pdata[8], "ATA ", 8);
875 fsl_ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
876 fsl_ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
877
878 sata_getinfo(sata, (u16 *)idbuf);
879#ifdef DEBUG
880 ata_dump_id(idbuf);
881#endif
882 return 0;
883}
884
885/*
886 * SCSI READ CAPACITY10 command operation.
887 */
888static int fsl_ata_scsiop_read_capacity10(struct ahci_uc_priv *uc_priv,
889 struct scsi_cmd *pccb)
890{
891 u32 cap;
892 u64 cap64;
893 u32 block_size;
894
895 if (!uc_priv->ataid[pccb->target]) {
896 printf("scsi_ahci: SCSI READ CAPACITY10 command failure.");
897 printf("\tNo ATA info!\n");
898 printf("\tPlease run SCSI command INQUIRY first!\n");
899 return -EPERM;
900 }
901
902 cap64 = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
903 if (cap64 > 0x100000000ULL)
904 cap64 = 0xffffffff;
905
906 cap = cpu_to_be32(cap64);
907 memcpy(pccb->pdata, &cap, sizeof(cap));
908
909 block_size = cpu_to_be32((u32)512);
910 memcpy(&pccb->pdata[4], &block_size, 4);
911
912 return 0;
913}
914
915/*
916 * SCSI READ CAPACITY16 command operation.
917 */
918static int fsl_ata_scsiop_read_capacity16(struct ahci_uc_priv *uc_priv,
919 struct scsi_cmd *pccb)
920{
921 u64 cap;
922 u64 block_size;
923
924 if (!uc_priv->ataid[pccb->target]) {
925 printf("scsi_ahci: SCSI READ CAPACITY16 command failure.");
926 printf("\tNo ATA info!\n");
927 printf("\tPlease run SCSI command INQUIRY first!\n");
928 return -EPERM;
929 }
930
931 cap = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
932 cap = cpu_to_be64(cap);
933 memcpy(pccb->pdata, &cap, sizeof(cap));
934
935 block_size = cpu_to_be64((u64)512);
936 memcpy(&pccb->pdata[8], &block_size, 8);
937
938 return 0;
939}
940
941/*
942 * SCSI TEST UNIT READY command operation.
943 */
944static int fsl_ata_scsiop_test_unit_ready(struct ahci_uc_priv *uc_priv,
945 struct scsi_cmd *pccb)
946{
947 return (uc_priv->ataid[pccb->target]) ? 0 : -EPERM;
948}
949
950static int fsl_ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
951{
952 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev->parent);
953 struct fsl_ahci_priv *priv = dev_get_priv(dev->parent);
954 fsl_sata_t *sata = priv->fsl_sata;
955 int ret;
956
957 switch (pccb->cmd[0]) {
958 case SCSI_READ16:
959 case SCSI_READ10:
960 ret = fsl_scsi_exec(sata, pccb, 0);
961 break;
962 case SCSI_WRITE10:
963 ret = fsl_scsi_exec(sata, pccb, 1);
964 break;
965 case SCSI_RD_CAPAC10:
966 ret = fsl_ata_scsiop_read_capacity10(uc_priv, pccb);
967 break;
968 case SCSI_RD_CAPAC16:
969 ret = fsl_ata_scsiop_read_capacity16(uc_priv, pccb);
970 break;
971 case SCSI_TST_U_RDY:
972 ret = fsl_ata_scsiop_test_unit_ready(uc_priv, pccb);
973 break;
974 case SCSI_INQUIRY:
975 ret = fsl_ata_scsiop_inquiry(uc_priv, pccb, sata);
976 break;
977 default:
978 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
979 return -ENOTSUPP;
980 }
981
982 if (ret) {
983 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
984 return ret;
985 }
986
987 return 0;
988}
989
990static int fsl_ahci_probe(struct udevice *dev)
991{
992 struct fsl_ahci_priv *priv = dev_get_priv(dev);
993 struct udevice *child_dev;
994 struct scsi_platdata *uc_plat;
995
996 device_find_first_child(dev, &child_dev);
997 if (!child_dev)
998 return -ENODEV;
999 uc_plat = dev_get_uclass_platdata(child_dev);
1000 uc_plat->base = priv->base;
1001 uc_plat->max_lun = 1;
1002 uc_plat->max_id = 1;
1003
1004 return init_sata(priv);
1005}
1006
1007struct scsi_ops fsl_scsi_ops = {
1008 .exec = fsl_ahci_scsi_exec,
1009};
1010
1011static const struct udevice_id fsl_ahci_ids[] = {
1012 { .compatible = "fsl,pq-sata-v2" },
1013 { }
1014};
1015
1016U_BOOT_DRIVER(fsl_ahci_scsi) = {
1017 .name = "fsl_ahci_scsi",
1018 .id = UCLASS_SCSI,
1019 .ops = &fsl_scsi_ops,
1020};
1021
1022U_BOOT_DRIVER(fsl_ahci) = {
1023 .name = "fsl_ahci",
1024 .id = UCLASS_AHCI,
1025 .of_match = fsl_ahci_ids,
1026 .bind = fsl_ahci_bind,
1027 .ofdata_to_platdata = fsl_ahci_ofdata_to_platdata,
1028 .probe = fsl_ahci_probe,
1029 .priv_auto_alloc_size = sizeof(struct fsl_ahci_priv),
1030};