fsl_sata.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2008,2010 Freescale Semiconductor, Inc.
  4. * Copyright 2019 NXP
  5. * Author: Dave Liu <daveliu@freescale.com>
  6. */
  7. #include <common.h>
  8. #include <ahci.h>
  9. #include <blk.h>
  10. #include <command.h>
  11. #include <console.h>
  12. #include <cpu_func.h>
  13. #include <dm.h>
  14. #include <dm/device-internal.h>
  15. #include <log.h>
  16. #include <asm/io.h>
  17. #include <asm/processor.h>
  18. #include <asm/fsl_serdes.h>
  19. #include <malloc.h>
  20. #include <libata.h>
  21. #include <fis.h>
  22. #include <sata.h>
  23. #include <linux/delay.h>
  24. #include "fsl_sata.h"
  25. static inline void sdelay(unsigned long sec)
  26. {
  27. unsigned long i;
  28. for (i = 0; i < sec; i++)
  29. mdelay(1000);
  30. }
  31. static void fsl_sata_dump_sfis(struct sata_fis_d2h *s)
  32. {
  33. printf("Status FIS dump:\n\r");
  34. printf("fis_type: %02x\n\r", s->fis_type);
  35. printf("pm_port_i: %02x\n\r", s->pm_port_i);
  36. printf("status: %02x\n\r", s->status);
  37. printf("error: %02x\n\r", s->error);
  38. printf("lba_low: %02x\n\r", s->lba_low);
  39. printf("lba_mid: %02x\n\r", s->lba_mid);
  40. printf("lba_high: %02x\n\r", s->lba_high);
  41. printf("device: %02x\n\r", s->device);
  42. printf("lba_low_exp: %02x\n\r", s->lba_low_exp);
  43. printf("lba_mid_exp: %02x\n\r", s->lba_mid_exp);
  44. printf("lba_high_exp: %02x\n\r", s->lba_high_exp);
  45. printf("res1: %02x\n\r", s->res1);
  46. printf("sector_count: %02x\n\r", s->sector_count);
  47. printf("sector_count_exp: %02x\n\r", s->sector_count_exp);
  48. }
  49. static int ata_wait_register(unsigned __iomem *addr, u32 mask,
  50. u32 val, u32 timeout_msec)
  51. {
  52. int i;
  53. u32 temp;
  54. for (i = 0; (((temp = in_le32(addr)) & mask) != val)
  55. && i < timeout_msec; i++)
  56. mdelay(1);
  57. return (i < timeout_msec) ? 0 : -1;
  58. }
  59. static int init_sata(struct fsl_ata_priv *priv, int dev)
  60. {
  61. u32 length, align;
  62. cmd_hdr_tbl_t *cmd_hdr;
  63. u32 cda;
  64. u32 val32;
  65. fsl_sata_reg_t __iomem *reg;
  66. u32 sig;
  67. int i;
  68. fsl_sata_t *sata;
  69. if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
  70. printf("the sata index %d is out of ranges\n\r", dev);
  71. return -1;
  72. }
  73. #ifdef CONFIG_MPC85xx
  74. if ((dev == 0) && (!is_serdes_configured(SATA1))) {
  75. printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
  76. return -1;
  77. }
  78. if ((dev == 1) && (!is_serdes_configured(SATA2))) {
  79. printf("SATA%d [dev = %d] is not enabled\n", dev+1, dev);
  80. return -1;
  81. }
  82. #endif
  83. /* Allocate SATA device driver struct */
  84. sata = (fsl_sata_t *)malloc(sizeof(fsl_sata_t));
  85. if (!sata) {
  86. printf("alloc the sata device struct failed\n\r");
  87. return -1;
  88. }
  89. /* Zero all of the device driver struct */
  90. memset((void *)sata, 0, sizeof(fsl_sata_t));
  91. snprintf(sata->name, 12, "SATA%d:", dev);
  92. /* Set the controller register base address to device struct */
  93. reg = (fsl_sata_reg_t *)(priv->base + priv->offset * dev);
  94. sata->dma_flag = priv->flag;
  95. priv->fsl_sata = sata;
  96. sata->reg_base = reg;
  97. /* Allocate the command header table, 4 bytes aligned */
  98. length = sizeof(struct cmd_hdr_tbl);
  99. align = SATA_HC_CMD_HDR_TBL_ALIGN;
  100. sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
  101. if (!sata->cmd_hdr_tbl_offset) {
  102. printf("alloc the command header failed\n\r");
  103. return -1;
  104. }
  105. cmd_hdr = (cmd_hdr_tbl_t *)(((u32)sata->cmd_hdr_tbl_offset + align)
  106. & ~(align - 1));
  107. sata->cmd_hdr = cmd_hdr;
  108. /* Zero all of the command header table */
  109. memset((void *)sata->cmd_hdr_tbl_offset, 0, length + align);
  110. /* Allocate command descriptor for all command */
  111. length = sizeof(struct cmd_desc) * SATA_HC_MAX_CMD;
  112. align = SATA_HC_CMD_DESC_ALIGN;
  113. sata->cmd_desc_offset = (void *)malloc(length + align);
  114. if (!sata->cmd_desc_offset) {
  115. printf("alloc the command descriptor failed\n\r");
  116. return -1;
  117. }
  118. sata->cmd_desc = (cmd_desc_t *)(((u32)sata->cmd_desc_offset + align)
  119. & ~(align - 1));
  120. /* Zero all of command descriptor */
  121. memset((void *)sata->cmd_desc_offset, 0, length + align);
  122. /* Link the command descriptor to command header */
  123. for (i = 0; i < SATA_HC_MAX_CMD; i++) {
  124. cda = ((u32)sata->cmd_desc + SATA_HC_CMD_DESC_SIZE * i)
  125. & ~(CMD_HDR_CDA_ALIGN - 1);
  126. cmd_hdr->cmd_slot[i].cda = cpu_to_le32(cda);
  127. }
  128. /* To have safe state, force the controller offline */
  129. val32 = in_le32(&reg->hcontrol);
  130. val32 &= ~HCONTROL_ONOFF;
  131. val32 |= HCONTROL_FORCE_OFFLINE;
  132. out_le32(&reg->hcontrol, val32);
  133. /* Wait the controller offline */
  134. ata_wait_register(&reg->hstatus, HSTATUS_ONOFF, 0, 1000);
  135. /* Set the command header base address to CHBA register to tell DMA */
  136. out_le32(&reg->chba, (u32)cmd_hdr & ~0x3);
  137. /* Snoop for the command header */
  138. val32 = in_le32(&reg->hcontrol);
  139. val32 |= HCONTROL_HDR_SNOOP;
  140. out_le32(&reg->hcontrol, val32);
  141. /* Disable all of interrupts */
  142. val32 = in_le32(&reg->hcontrol);
  143. val32 &= ~HCONTROL_INT_EN_ALL;
  144. out_le32(&reg->hcontrol, val32);
  145. /* Clear all of interrupts */
  146. val32 = in_le32(&reg->hstatus);
  147. out_le32(&reg->hstatus, val32);
  148. /* Set the ICC, no interrupt coalescing */
  149. out_le32(&reg->icc, 0x01000000);
  150. /* No PM attatched, the SATA device direct connect */
  151. out_le32(&reg->cqpmp, 0);
  152. /* Clear SError register */
  153. val32 = in_le32(&reg->serror);
  154. out_le32(&reg->serror, val32);
  155. /* Clear CER register */
  156. val32 = in_le32(&reg->cer);
  157. out_le32(&reg->cer, val32);
  158. /* Clear DER register */
  159. val32 = in_le32(&reg->der);
  160. out_le32(&reg->der, val32);
  161. /* No device detection or initialization action requested */
  162. out_le32(&reg->scontrol, 0x00000300);
  163. /* Configure the transport layer, default value */
  164. out_le32(&reg->transcfg, 0x08000016);
  165. /* Configure the link layer, default value */
  166. out_le32(&reg->linkcfg, 0x0000ff34);
  167. /* Bring the controller online */
  168. val32 = in_le32(&reg->hcontrol);
  169. val32 |= HCONTROL_ONOFF;
  170. out_le32(&reg->hcontrol, val32);
  171. mdelay(100);
  172. /* print sata device name */
  173. printf("%s ", sata->name);
  174. /* Wait PHY RDY signal changed for 500ms */
  175. ata_wait_register(&reg->hstatus, HSTATUS_PHY_RDY,
  176. HSTATUS_PHY_RDY, 500);
  177. /* Check PHYRDY */
  178. val32 = in_le32(&reg->hstatus);
  179. if (val32 & HSTATUS_PHY_RDY) {
  180. sata->link = 1;
  181. } else {
  182. sata->link = 0;
  183. printf("(No RDY)\n\r");
  184. return -1;
  185. }
  186. /* Wait for signature updated, which is 1st D2H */
  187. ata_wait_register(&reg->hstatus, HSTATUS_SIGNATURE,
  188. HSTATUS_SIGNATURE, 10000);
  189. if (val32 & HSTATUS_SIGNATURE) {
  190. sig = in_le32(&reg->sig);
  191. debug("Signature updated, the sig =%08x\n\r", sig);
  192. sata->ata_device_type = ata_dev_classify(sig);
  193. }
  194. /* Check the speed */
  195. val32 = in_le32(&reg->sstatus);
  196. if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
  197. printf("(1.5 Gbps)\n\r");
  198. else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
  199. printf("(3 Gbps)\n\r");
  200. return 0;
  201. }
  202. int reset_sata(int dev)
  203. {
  204. return 0;
  205. }
  206. static void fsl_sata_dump_regs(fsl_sata_reg_t __iomem *reg)
  207. {
  208. printf("\n\rSATA: %08x\n\r", (u32)reg);
  209. printf("CQR: %08x\n\r", in_le32(&reg->cqr));
  210. printf("CAR: %08x\n\r", in_le32(&reg->car));
  211. printf("CCR: %08x\n\r", in_le32(&reg->ccr));
  212. printf("CER: %08x\n\r", in_le32(&reg->cer));
  213. printf("CQR: %08x\n\r", in_le32(&reg->cqr));
  214. printf("DER: %08x\n\r", in_le32(&reg->der));
  215. printf("CHBA: %08x\n\r", in_le32(&reg->chba));
  216. printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
  217. printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
  218. printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
  219. printf("SIG: %08x\n\r", in_le32(&reg->sig));
  220. printf("ICC: %08x\n\r", in_le32(&reg->icc));
  221. printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
  222. printf("SError: %08x\n\r", in_le32(&reg->serror));
  223. printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
  224. printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
  225. printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
  226. printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
  227. printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
  228. printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
  229. printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
  230. printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
  231. printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
  232. printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
  233. printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
  234. }
  235. static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
  236. int is_ncq, int tag, u8 *buffer, u32 len)
  237. {
  238. cmd_hdr_entry_t *cmd_hdr;
  239. cmd_desc_t *cmd_desc;
  240. sata_fis_h2d_t *h2d;
  241. prd_entry_t *prde;
  242. u32 ext_c_ddc;
  243. u32 prde_count;
  244. u32 val32;
  245. u32 ttl;
  246. fsl_sata_reg_t __iomem *reg = sata->reg_base;
  247. int i;
  248. /* Check xfer length */
  249. if (len > SATA_HC_MAX_XFER_LEN) {
  250. printf("max transfer length is 64MB\n\r");
  251. return 0;
  252. }
  253. /* Setup the command descriptor */
  254. cmd_desc = sata->cmd_desc + tag;
  255. /* Get the pointer cfis of command descriptor */
  256. h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
  257. /* Zero the cfis of command descriptor */
  258. memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
  259. /* Copy the cfis from user to command descriptor */
  260. h2d->fis_type = cfis->fis_type;
  261. h2d->pm_port_c = cfis->pm_port_c;
  262. h2d->command = cfis->command;
  263. h2d->features = cfis->features;
  264. h2d->features_exp = cfis->features_exp;
  265. h2d->lba_low = cfis->lba_low;
  266. h2d->lba_mid = cfis->lba_mid;
  267. h2d->lba_high = cfis->lba_high;
  268. h2d->lba_low_exp = cfis->lba_low_exp;
  269. h2d->lba_mid_exp = cfis->lba_mid_exp;
  270. h2d->lba_high_exp = cfis->lba_high_exp;
  271. if (!is_ncq) {
  272. h2d->sector_count = cfis->sector_count;
  273. h2d->sector_count_exp = cfis->sector_count_exp;
  274. } else { /* NCQ */
  275. h2d->sector_count = (u8)(tag << 3);
  276. }
  277. h2d->device = cfis->device;
  278. h2d->control = cfis->control;
  279. /* Setup the PRD table */
  280. prde = (prd_entry_t *)cmd_desc->prdt;
  281. memset((void *)prde, 0, sizeof(struct prdt));
  282. prde_count = 0;
  283. ttl = len;
  284. for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
  285. if (!len)
  286. break;
  287. prde->dba = cpu_to_le32((u32)buffer & ~0x3);
  288. debug("dba = %08x\n\r", (u32)buffer);
  289. if (len < PRD_ENTRY_MAX_XFER_SZ) {
  290. ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
  291. debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
  292. prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
  293. prde_count++;
  294. prde++;
  295. break;
  296. } else {
  297. ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
  298. debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
  299. prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
  300. buffer += PRD_ENTRY_MAX_XFER_SZ;
  301. len -= PRD_ENTRY_MAX_XFER_SZ;
  302. prde_count++;
  303. prde++;
  304. }
  305. }
  306. /* Setup the command slot of cmd hdr */
  307. cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
  308. cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
  309. val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
  310. val32 |= sizeof(sata_fis_h2d_t);
  311. cmd_hdr->prde_fis_len = cpu_to_le32(val32);
  312. cmd_hdr->ttl = cpu_to_le32(ttl);
  313. if (!is_ncq) {
  314. val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
  315. } else {
  316. val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
  317. }
  318. tag &= CMD_HDR_ATTR_TAG;
  319. val32 |= tag;
  320. debug("attribute = %08x\n\r", val32);
  321. cmd_hdr->attribute = cpu_to_le32(val32);
  322. /* Make sure cmd desc and cmd slot valid before command issue */
  323. sync();
  324. /* PMP*/
  325. val32 = (u32)(h2d->pm_port_c & 0x0f);
  326. out_le32(&reg->cqpmp, val32);
  327. /* Wait no active */
  328. if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
  329. printf("Wait no active time out\n\r");
  330. /* Issue command */
  331. if (!(in_le32(&reg->cqr) & (1 << tag))) {
  332. val32 = 1 << tag;
  333. out_le32(&reg->cqr, val32);
  334. }
  335. /* Wait command completed for 10s */
  336. if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
  337. if (!is_ncq)
  338. printf("Non-NCQ command time out\n\r");
  339. else
  340. printf("NCQ command time out\n\r");
  341. }
  342. val32 = in_le32(&reg->cer);
  343. if (val32) {
  344. u32 der;
  345. fsl_sata_dump_sfis((struct sata_fis_d2h *)cmd_desc->sfis);
  346. printf("CE at device\n\r");
  347. fsl_sata_dump_regs(reg);
  348. der = in_le32(&reg->der);
  349. out_le32(&reg->cer, val32);
  350. out_le32(&reg->der, der);
  351. }
  352. /* Clear complete flags */
  353. val32 = in_le32(&reg->ccr);
  354. out_le32(&reg->ccr, val32);
  355. return len;
  356. }
  357. static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
  358. int tag, u8 *buffer, u32 len)
  359. {
  360. return 0;
  361. }
  362. static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
  363. enum cmd_type command_type, int tag, u8 *buffer, u32 len)
  364. {
  365. int rc;
  366. if (tag > SATA_HC_MAX_CMD || tag < 0) {
  367. printf("tag is out of range, tag=%d\n\r", tag);
  368. return -1;
  369. }
  370. switch (command_type) {
  371. case CMD_ATA:
  372. rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
  373. return rc;
  374. case CMD_RESET:
  375. rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
  376. return rc;
  377. case CMD_NCQ:
  378. rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
  379. return rc;
  380. case CMD_ATAPI:
  381. case CMD_VENDOR_BIST:
  382. case CMD_BIST:
  383. printf("not support now\n\r");
  384. return -1;
  385. default:
  386. break;
  387. }
  388. return -1;
  389. }
  390. static void fsl_sata_xfer_mode(fsl_sata_t *sata, u16 *id)
  391. {
  392. sata->pio = id[ATA_ID_PIO_MODES];
  393. sata->mwdma = id[ATA_ID_MWDMA_MODES];
  394. sata->udma = id[ATA_ID_UDMA_MODES];
  395. debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
  396. }
  397. static void fsl_sata_set_features(fsl_sata_t *sata)
  398. {
  399. struct sata_fis_h2d h2d, *cfis = &h2d;
  400. u8 udma_cap;
  401. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  402. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  403. cfis->pm_port_c = 0x80; /* is command */
  404. cfis->command = ATA_CMD_SET_FEATURES;
  405. cfis->features = SETFEATURES_XFER;
  406. /* First check the device capablity */
  407. udma_cap = (u8)(sata->udma & 0xff);
  408. debug("udma_cap %02x\n\r", udma_cap);
  409. if (udma_cap == ATA_UDMA6)
  410. cfis->sector_count = XFER_UDMA_6;
  411. if (udma_cap == ATA_UDMA5)
  412. cfis->sector_count = XFER_UDMA_5;
  413. if (udma_cap == ATA_UDMA4)
  414. cfis->sector_count = XFER_UDMA_4;
  415. if (udma_cap == ATA_UDMA3)
  416. cfis->sector_count = XFER_UDMA_3;
  417. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  418. }
  419. static u32 fsl_sata_rw_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt, u8 *buffer,
  420. int is_write)
  421. {
  422. struct sata_fis_h2d h2d, *cfis = &h2d;
  423. u32 block;
  424. block = start;
  425. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  426. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  427. cfis->pm_port_c = 0x80; /* is command */
  428. cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
  429. cfis->device = ATA_LBA;
  430. cfis->device |= (block >> 24) & 0xf;
  431. cfis->lba_high = (block >> 16) & 0xff;
  432. cfis->lba_mid = (block >> 8) & 0xff;
  433. cfis->lba_low = block & 0xff;
  434. cfis->sector_count = (u8)(blkcnt & 0xff);
  435. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
  436. return blkcnt;
  437. }
  438. static void fsl_sata_flush_cache(fsl_sata_t *sata)
  439. {
  440. struct sata_fis_h2d h2d, *cfis = &h2d;
  441. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  442. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  443. cfis->pm_port_c = 0x80; /* is command */
  444. cfis->command = ATA_CMD_FLUSH;
  445. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  446. }
  447. static u32 fsl_sata_rw_cmd_ext(fsl_sata_t *sata, u32 start, u32 blkcnt,
  448. u8 *buffer, int is_write)
  449. {
  450. struct sata_fis_h2d h2d, *cfis = &h2d;
  451. u64 block;
  452. block = (u64)start;
  453. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  454. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  455. cfis->pm_port_c = 0x80; /* is command */
  456. cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
  457. : ATA_CMD_READ_EXT;
  458. cfis->lba_high_exp = (block >> 40) & 0xff;
  459. cfis->lba_mid_exp = (block >> 32) & 0xff;
  460. cfis->lba_low_exp = (block >> 24) & 0xff;
  461. cfis->lba_high = (block >> 16) & 0xff;
  462. cfis->lba_mid = (block >> 8) & 0xff;
  463. cfis->lba_low = block & 0xff;
  464. cfis->device = ATA_LBA;
  465. cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
  466. cfis->sector_count = blkcnt & 0xff;
  467. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
  468. return blkcnt;
  469. }
  470. static u32 fsl_sata_rw_ncq_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt,
  471. u8 *buffer, int is_write)
  472. {
  473. struct sata_fis_h2d h2d, *cfis = &h2d;
  474. int ncq_channel;
  475. u64 block;
  476. if (sata->lba48 != 1) {
  477. printf("execute FPDMA command on non-LBA48 hard disk\n\r");
  478. return -1;
  479. }
  480. block = (u64)start;
  481. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  482. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  483. cfis->pm_port_c = 0x80; /* is command */
  484. cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
  485. : ATA_CMD_FPDMA_READ;
  486. cfis->lba_high_exp = (block >> 40) & 0xff;
  487. cfis->lba_mid_exp = (block >> 32) & 0xff;
  488. cfis->lba_low_exp = (block >> 24) & 0xff;
  489. cfis->lba_high = (block >> 16) & 0xff;
  490. cfis->lba_mid = (block >> 8) & 0xff;
  491. cfis->lba_low = block & 0xff;
  492. cfis->device = ATA_LBA;
  493. cfis->features_exp = (blkcnt >> 8) & 0xff;
  494. cfis->features = blkcnt & 0xff;
  495. if (sata->queue_depth >= SATA_HC_MAX_CMD)
  496. ncq_channel = SATA_HC_MAX_CMD - 1;
  497. else
  498. ncq_channel = sata->queue_depth - 1;
  499. /* Use the latest queue */
  500. fsl_sata_exec_cmd(sata, cfis, CMD_NCQ, ncq_channel, buffer, ATA_SECT_SIZE * blkcnt);
  501. return blkcnt;
  502. }
  503. static void fsl_sata_flush_cache_ext(fsl_sata_t *sata)
  504. {
  505. struct sata_fis_h2d h2d, *cfis = &h2d;
  506. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  507. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  508. cfis->pm_port_c = 0x80; /* is command */
  509. cfis->command = ATA_CMD_FLUSH_EXT;
  510. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  511. }
  512. static void fsl_sata_init_wcache(fsl_sata_t *sata, u16 *id)
  513. {
  514. if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
  515. sata->wcache = 1;
  516. if (ata_id_has_flush(id))
  517. sata->flush = 1;
  518. if (ata_id_has_flush_ext(id))
  519. sata->flush_ext = 1;
  520. }
  521. static u32 ata_low_level_rw_lba48(fsl_sata_t *sata, u32 blknr, lbaint_t blkcnt,
  522. const void *buffer, int is_write)
  523. {
  524. u32 start, blks;
  525. u8 *addr;
  526. int max_blks;
  527. start = blknr;
  528. blks = blkcnt;
  529. addr = (u8 *)buffer;
  530. max_blks = ATA_MAX_SECTORS_LBA48;
  531. do {
  532. if (blks > max_blks) {
  533. if (sata->dma_flag != FLAGS_FPDMA)
  534. fsl_sata_rw_cmd_ext(sata, start, max_blks, addr,
  535. is_write);
  536. else
  537. fsl_sata_rw_ncq_cmd(sata, start, max_blks, addr,
  538. is_write);
  539. start += max_blks;
  540. blks -= max_blks;
  541. addr += ATA_SECT_SIZE * max_blks;
  542. } else {
  543. if (sata->dma_flag != FLAGS_FPDMA)
  544. fsl_sata_rw_cmd_ext(sata, start, blks, addr,
  545. is_write);
  546. else
  547. fsl_sata_rw_ncq_cmd(sata, start, blks, addr,
  548. is_write);
  549. start += blks;
  550. blks = 0;
  551. addr += ATA_SECT_SIZE * blks;
  552. }
  553. } while (blks != 0);
  554. return blkcnt;
  555. }
  556. static u32 ata_low_level_rw_lba28(fsl_sata_t *sata, u32 blknr, u32 blkcnt,
  557. const void *buffer, int is_write)
  558. {
  559. u32 start, blks;
  560. u8 *addr;
  561. int max_blks;
  562. start = blknr;
  563. blks = blkcnt;
  564. addr = (u8 *)buffer;
  565. max_blks = ATA_MAX_SECTORS;
  566. do {
  567. if (blks > max_blks) {
  568. fsl_sata_rw_cmd(sata, start, max_blks, addr, is_write);
  569. start += max_blks;
  570. blks -= max_blks;
  571. addr += ATA_SECT_SIZE * max_blks;
  572. } else {
  573. fsl_sata_rw_cmd(sata, start, blks, addr, is_write);
  574. start += blks;
  575. blks = 0;
  576. addr += ATA_SECT_SIZE * blks;
  577. }
  578. } while (blks != 0);
  579. return blkcnt;
  580. }
  581. /*
  582. * SATA interface between low level driver and command layer
  583. */
  584. static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  585. void *buffer)
  586. {
  587. struct fsl_ata_priv *priv = dev_get_plat(dev);
  588. fsl_sata_t *sata = priv->fsl_sata;
  589. u32 rc;
  590. if (sata->lba48)
  591. rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
  592. READ_CMD);
  593. else
  594. rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
  595. READ_CMD);
  596. return rc;
  597. }
  598. static ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  599. const void *buffer)
  600. {
  601. struct fsl_ata_priv *priv = dev_get_plat(dev);
  602. fsl_sata_t *sata = priv->fsl_sata;
  603. u32 rc;
  604. if (sata->lba48) {
  605. rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
  606. WRITE_CMD);
  607. if (sata->wcache && sata->flush_ext)
  608. fsl_sata_flush_cache_ext(sata);
  609. } else {
  610. rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
  611. WRITE_CMD);
  612. if (sata->wcache && sata->flush)
  613. fsl_sata_flush_cache(sata);
  614. }
  615. return rc;
  616. }
  617. static void fsl_sata_identify(fsl_sata_t *sata, u16 *id)
  618. {
  619. struct sata_fis_h2d h2d, *cfis = &h2d;
  620. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  621. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  622. cfis->pm_port_c = 0x80; /* is command */
  623. cfis->command = ATA_CMD_ID_ATA;
  624. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
  625. ata_swap_buf_le16(id, ATA_ID_WORDS);
  626. }
  627. static int scan_sata(struct udevice *dev)
  628. {
  629. struct blk_desc *desc = dev_get_uclass_plat(dev);
  630. struct fsl_ata_priv *priv = dev_get_plat(dev);
  631. fsl_sata_t *sata = priv->fsl_sata;
  632. unsigned char serial[ATA_ID_SERNO_LEN + 1];
  633. unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
  634. unsigned char product[ATA_ID_PROD_LEN + 1];
  635. u16 *id;
  636. u64 n_sectors;
  637. /* if no detected link */
  638. if (!sata->link)
  639. return -1;
  640. id = (u16 *)malloc(ATA_ID_WORDS * 2);
  641. if (!id) {
  642. printf("id malloc failed\n\r");
  643. return -1;
  644. }
  645. /* Identify device to get information */
  646. fsl_sata_identify(sata, id);
  647. /* Serial number */
  648. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  649. /* Firmware version */
  650. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  651. /* Product model */
  652. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  653. /* Totoal sectors */
  654. n_sectors = ata_id_n_sectors(id);
  655. #ifdef CONFIG_LBA48
  656. /* Check if support LBA48 */
  657. if (ata_id_has_lba48(id)) {
  658. sata->lba48 = 1;
  659. debug("Device support LBA48\n\r");
  660. } else
  661. debug("Device supports LBA28\n\r");
  662. #endif
  663. memcpy(desc->product, serial, sizeof(serial));
  664. memcpy(desc->revision, firmware, sizeof(firmware));
  665. memcpy(desc->vendor, product, sizeof(product));
  666. desc->lba = n_sectors;
  667. #ifdef CONFIG_LBA48
  668. desc->lba48 = sata->lba48;
  669. #endif
  670. /* Get the NCQ queue depth from device */
  671. sata->queue_depth = ata_id_queue_depth(id);
  672. /* Get the xfer mode from device */
  673. fsl_sata_xfer_mode(sata, id);
  674. /* Get the write cache status from device */
  675. fsl_sata_init_wcache(sata, id);
  676. /* Set the xfer mode to highest speed */
  677. fsl_sata_set_features(sata);
  678. #ifdef DEBUG
  679. ata_dump_id(id);
  680. #endif
  681. free((void *)id);
  682. return 0;
  683. }
  684. static const struct blk_ops sata_fsl_blk_ops = {
  685. .read = sata_read,
  686. .write = sata_write,
  687. };
  688. U_BOOT_DRIVER(sata_fsl_driver) = {
  689. .name = "sata_fsl_blk",
  690. .id = UCLASS_BLK,
  691. .ops = &sata_fsl_blk_ops,
  692. .plat_auto = sizeof(struct fsl_ata_priv),
  693. };
  694. static int fsl_ata_of_to_plat(struct udevice *dev)
  695. {
  696. struct fsl_ata_priv *priv = dev_get_priv(dev);
  697. priv->number = dev_read_u32_default(dev, "sata-number", -1);
  698. priv->flag = dev_read_u32_default(dev, "sata-fpdma", -1);
  699. priv->offset = dev_read_u32_default(dev, "sata-offset", -1);
  700. priv->base = dev_read_addr(dev);
  701. if (priv->base == FDT_ADDR_T_NONE)
  702. return -EINVAL;
  703. return 0;
  704. }
  705. static int fsl_unbind_device(struct udevice *dev)
  706. {
  707. int ret;
  708. ret = device_remove(dev, DM_REMOVE_NORMAL);
  709. if (ret)
  710. return ret;
  711. ret = device_unbind(dev);
  712. if (ret)
  713. return ret;
  714. return 0;
  715. }
  716. static int fsl_ata_probe(struct udevice *dev)
  717. {
  718. struct fsl_ata_priv *blk_priv, *priv;
  719. struct udevice *blk;
  720. int failed_number;
  721. char sata_name[10];
  722. int nr_ports;
  723. int ret;
  724. int i;
  725. failed_number = 0;
  726. priv = dev_get_priv(dev);
  727. nr_ports = priv->number;
  728. nr_ports = min(nr_ports, CONFIG_SYS_SATA_MAX_DEVICE);
  729. for (i = 0; i < nr_ports; i++) {
  730. snprintf(sata_name, sizeof(sata_name), "fsl_sata%d", i);
  731. ret = blk_create_devicef(dev, "sata_fsl_blk", sata_name,
  732. UCLASS_AHCI, -1, 512, 0, &blk);
  733. if (ret) {
  734. debug("Can't create device\n");
  735. return ret;
  736. }
  737. /* Init SATA port */
  738. ret = init_sata(priv, i);
  739. if (ret) {
  740. debug("%s: Failed to init sata\n", __func__);
  741. ret = fsl_unbind_device(blk);
  742. if (ret)
  743. return ret;
  744. failed_number++;
  745. continue;
  746. }
  747. blk_priv = dev_get_plat(blk);
  748. blk_priv->fsl_sata = priv->fsl_sata;
  749. /* Scan SATA port */
  750. ret = scan_sata(blk);
  751. if (ret) {
  752. debug("%s: Failed to scan bus\n", __func__);
  753. ret = fsl_unbind_device(blk);
  754. if (ret)
  755. return ret;
  756. failed_number++;
  757. continue;
  758. }
  759. ret = device_probe(dev);
  760. if (ret < 0) {
  761. debug("Probing %s failed (%d)\n", dev->name, ret);
  762. ret = fsl_unbind_device(blk);
  763. if (ret)
  764. return ret;
  765. failed_number++;
  766. continue;
  767. }
  768. }
  769. if (failed_number == nr_ports)
  770. return -ENODEV;
  771. else
  772. return 0;
  773. }
  774. static int fsl_ata_remove(struct udevice *dev)
  775. {
  776. fsl_sata_t *sata;
  777. struct fsl_ata_priv *priv;
  778. priv = dev_get_priv(dev);
  779. sata = priv->fsl_sata;
  780. free(sata->cmd_hdr_tbl_offset);
  781. free(sata->cmd_desc_offset);
  782. free(sata);
  783. return 0;
  784. }
  785. static int sata_fsl_scan(struct udevice *dev)
  786. {
  787. /* Nothing to do here */
  788. return 0;
  789. }
  790. struct ahci_ops sata_fsl_ahci_ops = {
  791. .scan = sata_fsl_scan,
  792. };
  793. static const struct udevice_id fsl_ata_ids[] = {
  794. { .compatible = "fsl,pq-sata-v2" },
  795. { }
  796. };
  797. U_BOOT_DRIVER(fsl_ahci) = {
  798. .name = "fsl_ahci",
  799. .id = UCLASS_AHCI,
  800. .of_match = fsl_ata_ids,
  801. .ops = &sata_fsl_ahci_ops,
  802. .of_to_plat = fsl_ata_of_to_plat,
  803. .probe = fsl_ata_probe,
  804. .remove = fsl_ata_remove,
  805. .priv_auto = sizeof(struct fsl_ata_priv),
  806. };