fsl_sata.c 24 KB

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