fsl_sata.c 24 KB

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