fsl_sata.c 22 KB

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