fsl_sata.c 25 KB

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