fsl_sata.c 24 KB

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