fsl_sata.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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[CFG_SATA_MAX_DEVICE];
  28. #ifndef CFG_SATA1_FLAGS
  29. #define CFG_SATA1_FLAGS FLAGS_DMA
  30. #endif
  31. #ifndef CFG_SATA2_FLAGS
  32. #define CFG_SATA2_FLAGS FLAGS_DMA
  33. #endif
  34. static struct fsl_sata_info fsl_sata_info[] = {
  35. #ifdef CONFIG_SATA1
  36. {CFG_SATA1, CFG_SATA1_FLAGS},
  37. #else
  38. {0, 0},
  39. #endif
  40. #ifdef CONFIG_SATA2
  41. {CFG_SATA2, CFG_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 > (CFG_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. /* Check PHYRDY */
  211. val32 = in_le32(&reg->hstatus);
  212. if (val32 & HSTATUS_PHY_RDY) {
  213. sata->link = 1;
  214. } else {
  215. sata->link = 0;
  216. printf("(No RDY)\n\r");
  217. return -1;
  218. }
  219. if (val32 & HSTATUS_SIGNATURE) {
  220. sig = in_le32(&reg->sig);
  221. debug("Signature updated, the sig =%08x\n\r", sig);
  222. sata->ata_device_type = ata_dev_classify(sig);
  223. }
  224. /* Check the speed */
  225. val32 = in_le32(&reg->sstatus);
  226. if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN1)
  227. printf("(1.5 Gbps)\n\r");
  228. else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
  229. printf("(3 Gbps)\n\r");
  230. return 0;
  231. }
  232. /* Hardware reset, like Power-on and COMRESET */
  233. void fsl_sata_hardware_reset(u32 reg_base)
  234. {
  235. fsl_sata_reg_t *reg = (fsl_sata_reg_t *)reg_base;
  236. u32 scontrol;
  237. /* Disable the SATA interface and put PHY offline */
  238. scontrol = in_le32(&reg->scontrol);
  239. scontrol = (scontrol & 0x0f0) | 0x304;
  240. out_le32(&reg->scontrol, scontrol);
  241. /* No speed strict */
  242. scontrol = in_le32(&reg->scontrol);
  243. scontrol = scontrol & ~0x0f0;
  244. out_le32(&reg->scontrol, scontrol);
  245. /* Issue PHY wake/reset, Hardware_reset_asserted */
  246. scontrol = in_le32(&reg->scontrol);
  247. scontrol = (scontrol & 0x0f0) | 0x301;
  248. out_le32(&reg->scontrol, scontrol);
  249. mdelay(100);
  250. /* Resume PHY, COMRESET negated, the device initialize hardware
  251. * and execute diagnostics, send good status-signature to host,
  252. * which is D2H register FIS, and then the device enter idle state.
  253. */
  254. scontrol = in_le32(&reg->scontrol);
  255. scontrol = (scontrol & 0x0f0) | 0x300;
  256. out_le32(&reg->scontrol, scontrol);
  257. mdelay(100);
  258. return;
  259. }
  260. static void fsl_sata_dump_regs(fsl_sata_reg_t *reg)
  261. {
  262. printf("\n\rSATA: %08x\n\r", (u32)reg);
  263. printf("CQR: %08x\n\r", in_le32(&reg->cqr));
  264. printf("CAR: %08x\n\r", in_le32(&reg->car));
  265. printf("CCR: %08x\n\r", in_le32(&reg->ccr));
  266. printf("CER: %08x\n\r", in_le32(&reg->cer));
  267. printf("CQR: %08x\n\r", in_le32(&reg->cqr));
  268. printf("DER: %08x\n\r", in_le32(&reg->der));
  269. printf("CHBA: %08x\n\r", in_le32(&reg->chba));
  270. printf("HStatus: %08x\n\r", in_le32(&reg->hstatus));
  271. printf("HControl: %08x\n\r", in_le32(&reg->hcontrol));
  272. printf("CQPMP: %08x\n\r", in_le32(&reg->cqpmp));
  273. printf("SIG: %08x\n\r", in_le32(&reg->sig));
  274. printf("ICC: %08x\n\r", in_le32(&reg->icc));
  275. printf("SStatus: %08x\n\r", in_le32(&reg->sstatus));
  276. printf("SError: %08x\n\r", in_le32(&reg->serror));
  277. printf("SControl: %08x\n\r", in_le32(&reg->scontrol));
  278. printf("SNotification: %08x\n\r", in_le32(&reg->snotification));
  279. printf("TransCfg: %08x\n\r", in_le32(&reg->transcfg));
  280. printf("TransStatus: %08x\n\r", in_le32(&reg->transstatus));
  281. printf("LinkCfg: %08x\n\r", in_le32(&reg->linkcfg));
  282. printf("LinkCfg1: %08x\n\r", in_le32(&reg->linkcfg1));
  283. printf("LinkCfg2: %08x\n\r", in_le32(&reg->linkcfg2));
  284. printf("LinkStatus: %08x\n\r", in_le32(&reg->linkstatus));
  285. printf("LinkStatus1: %08x\n\r", in_le32(&reg->linkstatus1));
  286. printf("PhyCtrlCfg: %08x\n\r", in_le32(&reg->phyctrlcfg));
  287. printf("SYSPR: %08x\n\r", in_be32(&reg->syspr));
  288. }
  289. static int fsl_ata_exec_ata_cmd(struct fsl_sata *sata, struct cfis *cfis,
  290. int is_ncq, int tag, u8 *buffer, u32 len)
  291. {
  292. cmd_hdr_entry_t *cmd_hdr;
  293. cmd_desc_t *cmd_desc;
  294. sata_fis_h2d_t *h2d;
  295. prd_entry_t *prde;
  296. u32 ext_c_ddc;
  297. u32 prde_count;
  298. u32 val32;
  299. u32 ttl;
  300. fsl_sata_reg_t *reg = sata->reg_base;
  301. int i;
  302. /* Check xfer length */
  303. if (len > SATA_HC_MAX_XFER_LEN) {
  304. printf("max transfer length is 64MB\n\r");
  305. return 0;
  306. }
  307. /* Setup the command descriptor */
  308. cmd_desc = sata->cmd_desc + tag;
  309. /* Get the pointer cfis of command descriptor */
  310. h2d = (sata_fis_h2d_t *)cmd_desc->cfis;
  311. /* Zero the cfis of command descriptor */
  312. memset((void *)h2d, 0, SATA_HC_CMD_DESC_CFIS_SIZE);
  313. /* Copy the cfis from user to command descriptor */
  314. h2d->fis_type = cfis->fis_type;
  315. h2d->pm_port_c = cfis->pm_port_c;
  316. h2d->command = cfis->command;
  317. h2d->features = cfis->features;
  318. h2d->features_exp = cfis->features_exp;
  319. h2d->lba_low = cfis->lba_low;
  320. h2d->lba_mid = cfis->lba_mid;
  321. h2d->lba_high = cfis->lba_high;
  322. h2d->lba_low_exp = cfis->lba_low_exp;
  323. h2d->lba_mid_exp = cfis->lba_mid_exp;
  324. h2d->lba_high_exp = cfis->lba_high_exp;
  325. if (!is_ncq) {
  326. h2d->sector_count = cfis->sector_count;
  327. h2d->sector_count_exp = cfis->sector_count_exp;
  328. } else { /* NCQ */
  329. h2d->sector_count = (u8)(tag << 3);
  330. }
  331. h2d->device = cfis->device;
  332. h2d->control = cfis->control;
  333. /* Setup the PRD table */
  334. prde = (prd_entry_t *)cmd_desc->prdt;
  335. memset((void *)prde, 0, sizeof(struct prdt));
  336. prde_count = 0;
  337. ttl = len;
  338. for (i = 0; i < SATA_HC_MAX_PRD_DIRECT; i++) {
  339. if (!len)
  340. break;
  341. prde->dba = cpu_to_le32((u32)buffer & ~0x3);
  342. debug("dba = %08x\n\r", (u32)buffer);
  343. if (len < PRD_ENTRY_MAX_XFER_SZ) {
  344. ext_c_ddc = PRD_ENTRY_DATA_SNOOP | len;
  345. debug("ext_c_ddc1 = %08x, len = %08x\n\r", ext_c_ddc, len);
  346. prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
  347. prde_count++;
  348. prde++;
  349. break;
  350. } else {
  351. ext_c_ddc = PRD_ENTRY_DATA_SNOOP; /* 4M bytes */
  352. debug("ext_c_ddc2 = %08x, len = %08x\n\r", ext_c_ddc, len);
  353. prde->ext_c_ddc = cpu_to_le32(ext_c_ddc);
  354. buffer += PRD_ENTRY_MAX_XFER_SZ;
  355. len -= PRD_ENTRY_MAX_XFER_SZ;
  356. prde_count++;
  357. prde++;
  358. }
  359. }
  360. /* Setup the command slot of cmd hdr */
  361. cmd_hdr = (cmd_hdr_entry_t *)&sata->cmd_hdr->cmd_slot[tag];
  362. cmd_hdr->cda = cpu_to_le32((u32)cmd_desc & ~0x3);
  363. val32 = prde_count << CMD_HDR_PRD_ENTRY_SHIFT;
  364. val32 |= sizeof(sata_fis_h2d_t);
  365. cmd_hdr->prde_fis_len = cpu_to_le32(val32);
  366. cmd_hdr->ttl = cpu_to_le32(ttl);
  367. if (!is_ncq) {
  368. val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP;
  369. } else {
  370. val32 = CMD_HDR_ATTR_RES | CMD_HDR_ATTR_SNOOP | CMD_HDR_ATTR_FPDMA;
  371. }
  372. tag &= CMD_HDR_ATTR_TAG;
  373. val32 |= tag;
  374. debug("attribute = %08x\n\r", val32);
  375. cmd_hdr->attribute = cpu_to_le32(val32);
  376. /* Make sure cmd desc and cmd slot valid before commmand issue */
  377. sync();
  378. /* PMP*/
  379. val32 = (u32)(h2d->pm_port_c & 0x0f);
  380. out_le32(&reg->cqpmp, val32);
  381. /* Wait no active */
  382. if (ata_wait_register(&reg->car, (1 << tag), 0, 10000))
  383. printf("Wait no active time out\n\r");
  384. /* Issue command */
  385. if (!(in_le32(&reg->cqr) & (1 << tag))) {
  386. val32 = 1 << tag;
  387. out_le32(&reg->cqr, val32);
  388. }
  389. /* Wait command completed for 10s */
  390. if (ata_wait_register(&reg->ccr, (1 << tag), (1 << tag), 10000)) {
  391. if (!is_ncq)
  392. printf("Non-NCQ command time out\n\r");
  393. else
  394. printf("NCQ command time out\n\r");
  395. }
  396. val32 = in_le32(&reg->cer);
  397. if (val32) {
  398. u32 der;
  399. fsl_sata_dump_sfis((struct sfis *)cmd_desc->sfis);
  400. printf("CE at device\n\r");
  401. fsl_sata_dump_regs(reg);
  402. der = in_le32(&reg->der);
  403. out_le32(&reg->cer, val32);
  404. out_le32(&reg->der, der);
  405. }
  406. /* Clear complete flags */
  407. val32 = in_le32(&reg->ccr);
  408. out_le32(&reg->ccr, val32);
  409. return len;
  410. }
  411. static int fsl_ata_exec_reset_cmd(struct fsl_sata *sata, struct cfis *cfis,
  412. int tag, u8 *buffer, u32 len)
  413. {
  414. return 0;
  415. }
  416. static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct cfis *cfis,
  417. enum cmd_type command_type, int tag, u8 *buffer, u32 len)
  418. {
  419. int rc;
  420. if (tag > SATA_HC_MAX_CMD || tag < 0) {
  421. printf("tag is out of range, tag=\n\r", tag);
  422. return -1;
  423. }
  424. switch (command_type) {
  425. case CMD_ATA:
  426. rc = fsl_ata_exec_ata_cmd(sata, cfis, 0, tag, buffer, len);
  427. return rc;
  428. case CMD_RESET:
  429. rc = fsl_ata_exec_reset_cmd(sata, cfis, tag, buffer, len);
  430. return rc;
  431. case CMD_NCQ:
  432. rc = fsl_ata_exec_ata_cmd(sata, cfis, 1, tag, buffer, len);
  433. return rc;
  434. case CMD_ATAPI:
  435. case CMD_VENDOR_BIST:
  436. case CMD_BIST:
  437. printf("not support now\n\r");
  438. return -1;
  439. default:
  440. break;
  441. }
  442. return -1;
  443. }
  444. static void fsl_sata_identify(int dev, u16 *id)
  445. {
  446. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  447. struct sata_fis_h2d h2d;
  448. struct cfis *cfis;
  449. cfis = (struct cfis *)&h2d;
  450. memset((void *)cfis, 0, sizeof(struct cfis));
  451. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  452. cfis->pm_port_c = 0x80; /* is command */
  453. cfis->command = ATA_CMD_ID_ATA;
  454. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
  455. ata_swap_buf_le16(id, ATA_ID_WORDS);
  456. }
  457. static void fsl_sata_xfer_mode(int dev, u16 *id)
  458. {
  459. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  460. sata->pio = id[ATA_ID_PIO_MODES];
  461. sata->mwdma = id[ATA_ID_MWDMA_MODES];
  462. sata->udma = id[ATA_ID_UDMA_MODES];
  463. debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
  464. }
  465. static void fsl_sata_set_features(int dev)
  466. {
  467. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  468. struct sata_fis_h2d h2d;
  469. struct cfis *cfis;
  470. u8 udma_cap;
  471. cfis = (struct cfis *)&h2d;
  472. memset((void *)cfis, 0, sizeof(struct cfis));
  473. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  474. cfis->pm_port_c = 0x80; /* is command */
  475. cfis->command = ATA_CMD_SET_FEATURES;
  476. cfis->features = SETFEATURES_XFER;
  477. /* First check the device capablity */
  478. udma_cap = (u8)(sata->udma & 0xff);
  479. debug("udma_cap %02x\n\r", udma_cap);
  480. if (udma_cap == ATA_UDMA6)
  481. cfis->sector_count = XFER_UDMA_6;
  482. if (udma_cap == ATA_UDMA5)
  483. cfis->sector_count = XFER_UDMA_5;
  484. if (udma_cap == ATA_UDMA4)
  485. cfis->sector_count = XFER_UDMA_4;
  486. if (udma_cap == ATA_UDMA3)
  487. cfis->sector_count = XFER_UDMA_3;
  488. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  489. }
  490. static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
  491. {
  492. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  493. struct sata_fis_h2d h2d;
  494. struct cfis *cfis;
  495. u32 block;
  496. block = start;
  497. cfis = (struct cfis *)&h2d;
  498. memset((void *)cfis, 0, sizeof(struct cfis));
  499. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  500. cfis->pm_port_c = 0x80; /* is command */
  501. cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
  502. cfis->device = ATA_LBA;
  503. cfis->device |= (block >> 24) & 0xf;
  504. cfis->lba_high = (block >> 16) & 0xff;
  505. cfis->lba_mid = (block >> 8) & 0xff;
  506. cfis->lba_low = block & 0xff;
  507. cfis->sector_count = (u8)(blkcnt & 0xff);
  508. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
  509. return blkcnt;
  510. }
  511. void fsl_sata_flush_cache(int dev)
  512. {
  513. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  514. struct sata_fis_h2d h2d;
  515. struct cfis *cfis;
  516. cfis = (struct cfis *)&h2d;
  517. memset((void *)cfis, 0, sizeof(struct cfis));
  518. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  519. cfis->pm_port_c = 0x80; /* is command */
  520. cfis->command = ATA_CMD_FLUSH;
  521. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  522. }
  523. static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
  524. {
  525. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  526. struct sata_fis_h2d h2d;
  527. struct cfis *cfis;
  528. u64 block;
  529. block = (u64)start;
  530. cfis = (struct cfis *)&h2d;
  531. memset((void *)cfis, 0, sizeof(struct cfis));
  532. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  533. cfis->pm_port_c = 0x80; /* is command */
  534. cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
  535. : ATA_CMD_READ_EXT;
  536. cfis->lba_high_exp = (block >> 40) & 0xff;
  537. cfis->lba_mid_exp = (block >> 32) & 0xff;
  538. cfis->lba_low_exp = (block >> 24) & 0xff;
  539. cfis->lba_high = (block >> 16) & 0xff;
  540. cfis->lba_mid = (block >> 8) & 0xff;
  541. cfis->lba_low = block & 0xff;
  542. cfis->device = ATA_LBA;
  543. cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
  544. cfis->sector_count = blkcnt & 0xff;
  545. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, buffer, ATA_SECT_SIZE * blkcnt);
  546. return blkcnt;
  547. }
  548. u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
  549. {
  550. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  551. struct sata_fis_h2d h2d;
  552. struct cfis *cfis;
  553. int ncq_channel;
  554. u64 block;
  555. if (sata_dev_desc[dev].lba48 != 1) {
  556. printf("execute FPDMA command on non-LBA48 hard disk\n\r");
  557. return -1;
  558. }
  559. block = (u64)start;
  560. cfis = (struct cfis *)&h2d;
  561. memset((void *)cfis, 0, sizeof(struct cfis));
  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;
  587. struct cfis *cfis;
  588. cfis = (struct cfis *)&h2d;
  589. memset((void *)cfis, 0, sizeof(struct cfis));
  590. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  591. cfis->pm_port_c = 0x80; /* is command */
  592. cfis->command = ATA_CMD_FLUSH_EXT;
  593. fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
  594. }
  595. /* Software reset, set SRST of the Device Control register */
  596. void fsl_sata_software_reset(int dev)
  597. {
  598. return;
  599. }
  600. static void fsl_sata_init_wcache(int dev, u16 *id)
  601. {
  602. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  603. if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
  604. sata->wcache = 1;
  605. if (ata_id_has_flush(id))
  606. sata->flush = 1;
  607. if (ata_id_has_flush_ext(id))
  608. sata->flush_ext = 1;
  609. }
  610. static int fsl_sata_get_wcache(int dev)
  611. {
  612. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  613. return sata->wcache;
  614. }
  615. static int fsl_sata_get_flush(int dev)
  616. {
  617. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  618. return sata->flush;
  619. }
  620. static int fsl_sata_get_flush_ext(int dev)
  621. {
  622. fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
  623. return sata->flush_ext;
  624. }
  625. u32 ata_low_level_rw_lba48(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
  626. {
  627. u32 start, blks;
  628. u8 *addr;
  629. int max_blks;
  630. start = blknr;
  631. blks = blkcnt;
  632. addr = (u8 *)buffer;
  633. max_blks = ATA_MAX_SECTORS_LBA48;
  634. do {
  635. if (blks > max_blks) {
  636. if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
  637. fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
  638. else
  639. fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
  640. start += max_blks;
  641. blks -= max_blks;
  642. addr += ATA_SECT_SIZE * max_blks;
  643. } else {
  644. if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
  645. fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
  646. else
  647. fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
  648. start += blks;
  649. blks = 0;
  650. addr += ATA_SECT_SIZE * blks;
  651. }
  652. } while (blks != 0);
  653. return blkcnt;
  654. }
  655. u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt, void *buffer, int is_write)
  656. {
  657. u32 start, blks;
  658. u8 *addr;
  659. int max_blks;
  660. start = blknr;
  661. blks = blkcnt;
  662. addr = (u8 *)buffer;
  663. max_blks = ATA_MAX_SECTORS;
  664. do {
  665. if (blks > max_blks) {
  666. fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
  667. start += max_blks;
  668. blks -= max_blks;
  669. addr += ATA_SECT_SIZE * max_blks;
  670. } else {
  671. fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
  672. start += blks;
  673. blks = 0;
  674. addr += ATA_SECT_SIZE * blks;
  675. }
  676. } while (blks != 0);
  677. return blkcnt;
  678. }
  679. /*
  680. * SATA interface between low level driver and command layer
  681. */
  682. ulong sata_read(int dev, u32 blknr, u32 blkcnt, void *buffer)
  683. {
  684. u32 rc;
  685. if (sata_dev_desc[dev].lba48)
  686. rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
  687. else
  688. rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
  689. return rc;
  690. }
  691. ulong sata_write(int dev, u32 blknr, u32 blkcnt, void *buffer)
  692. {
  693. u32 rc;
  694. if (sata_dev_desc[dev].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. /* Check if support LBA48 */
  736. if (ata_id_has_lba48(id)) {
  737. sata_dev_desc[dev].lba48 = 1;
  738. debug("Device support LBA48\n\r");
  739. }
  740. /* Get the NCQ queue depth from device */
  741. sata->queue_depth = ata_id_queue_depth(id);
  742. /* Get the xfer mode from device */
  743. fsl_sata_xfer_mode(dev, id);
  744. /* Get the write cache status from device */
  745. fsl_sata_init_wcache(dev, id);
  746. /* Set the xfer mode to highest speed */
  747. fsl_sata_set_features(dev);
  748. #ifdef DEBUG
  749. fsl_sata_identify(dev, id);
  750. ata_dump_id(id);
  751. #endif
  752. free((void *)id);
  753. return 0;
  754. }