dwc_ahsata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  3. * Terry Lv <r65388@freescale.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <libata.h>
  8. #include <ahci.h>
  9. #include <fis.h>
  10. #include <sata.h>
  11. #include <common.h>
  12. #include <malloc.h>
  13. #include <linux/ctype.h>
  14. #include <linux/errno.h>
  15. #include <asm/io.h>
  16. #include <linux/bitops.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/sys_proto.h>
  19. #include "dwc_ahsata.h"
  20. struct sata_port_regs {
  21. u32 clb;
  22. u32 clbu;
  23. u32 fb;
  24. u32 fbu;
  25. u32 is;
  26. u32 ie;
  27. u32 cmd;
  28. u32 res1[1];
  29. u32 tfd;
  30. u32 sig;
  31. u32 ssts;
  32. u32 sctl;
  33. u32 serr;
  34. u32 sact;
  35. u32 ci;
  36. u32 sntf;
  37. u32 res2[1];
  38. u32 dmacr;
  39. u32 res3[1];
  40. u32 phycr;
  41. u32 physr;
  42. };
  43. struct sata_host_regs {
  44. u32 cap;
  45. u32 ghc;
  46. u32 is;
  47. u32 pi;
  48. u32 vs;
  49. u32 ccc_ctl;
  50. u32 ccc_ports;
  51. u32 res1[2];
  52. u32 cap2;
  53. u32 res2[30];
  54. u32 bistafr;
  55. u32 bistcr;
  56. u32 bistfctr;
  57. u32 bistsr;
  58. u32 bistdecr;
  59. u32 res3[2];
  60. u32 oobr;
  61. u32 res4[8];
  62. u32 timer1ms;
  63. u32 res5[1];
  64. u32 gparam1r;
  65. u32 gparam2r;
  66. u32 pparamr;
  67. u32 testr;
  68. u32 versionr;
  69. u32 idr;
  70. };
  71. #define MAX_DATA_BYTES_PER_SG (4 * 1024 * 1024)
  72. #define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
  73. #define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
  74. static int is_ready;
  75. static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
  76. {
  77. return base + 0x100 + (port * 0x80);
  78. }
  79. static int waiting_for_cmd_completed(u8 *offset,
  80. int timeout_msec,
  81. u32 sign)
  82. {
  83. int i;
  84. u32 status;
  85. for (i = 0;
  86. ((status = readl(offset)) & sign) && i < timeout_msec;
  87. ++i)
  88. mdelay(1);
  89. return (i < timeout_msec) ? 0 : -1;
  90. }
  91. static int ahci_setup_oobr(struct ahci_uc_priv *uc_priv, int clk)
  92. {
  93. struct sata_host_regs *host_mmio = uc_priv->mmio_base;
  94. writel(SATA_HOST_OOBR_WE, &(host_mmio->oobr));
  95. writel(0x02060b14, &(host_mmio->oobr));
  96. return 0;
  97. }
  98. static int ahci_host_init(struct ahci_uc_priv *uc_priv)
  99. {
  100. u32 tmp, cap_save, num_ports;
  101. int i, j, timeout = 1000;
  102. struct sata_port_regs *port_mmio = NULL;
  103. struct sata_host_regs *host_mmio = uc_priv->mmio_base;
  104. int clk = mxc_get_clock(MXC_SATA_CLK);
  105. cap_save = readl(&(host_mmio->cap));
  106. cap_save |= SATA_HOST_CAP_SSS;
  107. /* global controller reset */
  108. tmp = readl(&(host_mmio->ghc));
  109. if ((tmp & SATA_HOST_GHC_HR) == 0)
  110. writel_with_flush(tmp | SATA_HOST_GHC_HR, &(host_mmio->ghc));
  111. while ((readl(&(host_mmio->ghc)) & SATA_HOST_GHC_HR)
  112. && --timeout)
  113. ;
  114. if (timeout <= 0) {
  115. debug("controller reset failed (0x%x)\n", tmp);
  116. return -1;
  117. }
  118. /* Set timer 1ms */
  119. writel(clk / 1000, &(host_mmio->timer1ms));
  120. ahci_setup_oobr(uc_priv, 0);
  121. writel_with_flush(SATA_HOST_GHC_AE, &(host_mmio->ghc));
  122. writel(cap_save, &(host_mmio->cap));
  123. num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
  124. writel_with_flush((1 << num_ports) - 1,
  125. &(host_mmio->pi));
  126. /*
  127. * Determine which Ports are implemented by the DWC_ahsata,
  128. * by reading the PI register. This bit map value aids the
  129. * software to determine how many Ports are available and
  130. * which Port registers need to be initialized.
  131. */
  132. uc_priv->cap = readl(&(host_mmio->cap));
  133. uc_priv->port_map = readl(&(host_mmio->pi));
  134. /* Determine how many command slots the HBA supports */
  135. uc_priv->n_ports = (uc_priv->cap & SATA_HOST_CAP_NP_MASK) + 1;
  136. debug("cap 0x%x port_map 0x%x n_ports %d\n",
  137. uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
  138. for (i = 0; i < uc_priv->n_ports; i++) {
  139. uc_priv->port[i].port_mmio = ahci_port_base(host_mmio, i);
  140. port_mmio = uc_priv->port[i].port_mmio;
  141. /* Ensure that the DWC_ahsata is in idle state */
  142. tmp = readl(&(port_mmio->cmd));
  143. /*
  144. * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
  145. * are all cleared, the Port is in an idle state.
  146. */
  147. if (tmp & (SATA_PORT_CMD_CR | SATA_PORT_CMD_FR |
  148. SATA_PORT_CMD_FRE | SATA_PORT_CMD_ST)) {
  149. /*
  150. * System software places a Port into the idle state by
  151. * clearing P#CMD.ST and waiting for P#CMD.CR to return
  152. * 0 when read.
  153. */
  154. tmp &= ~SATA_PORT_CMD_ST;
  155. writel_with_flush(tmp, &(port_mmio->cmd));
  156. /*
  157. * spec says 500 msecs for each bit, so
  158. * this is slightly incorrect.
  159. */
  160. mdelay(500);
  161. timeout = 1000;
  162. while ((readl(&(port_mmio->cmd)) & SATA_PORT_CMD_CR)
  163. && --timeout)
  164. ;
  165. if (timeout <= 0) {
  166. debug("port reset failed (0x%x)\n", tmp);
  167. return -1;
  168. }
  169. }
  170. /* Spin-up device */
  171. tmp = readl(&(port_mmio->cmd));
  172. writel((tmp | SATA_PORT_CMD_SUD), &(port_mmio->cmd));
  173. /* Wait for spin-up to finish */
  174. timeout = 1000;
  175. while (!(readl(&(port_mmio->cmd)) | SATA_PORT_CMD_SUD)
  176. && --timeout)
  177. ;
  178. if (timeout <= 0) {
  179. debug("Spin-Up can't finish!\n");
  180. return -1;
  181. }
  182. for (j = 0; j < 100; ++j) {
  183. mdelay(10);
  184. tmp = readl(&(port_mmio->ssts));
  185. if (((tmp & SATA_PORT_SSTS_DET_MASK) == 0x3) ||
  186. ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x1))
  187. break;
  188. }
  189. /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
  190. timeout = 1000;
  191. while (!(readl(&(port_mmio->serr)) | SATA_PORT_SERR_DIAG_X)
  192. && --timeout)
  193. ;
  194. if (timeout <= 0) {
  195. debug("Can't find DIAG_X set!\n");
  196. return -1;
  197. }
  198. /*
  199. * For each implemented Port, clear the P#SERR
  200. * register, by writing ones to each implemented\
  201. * bit location.
  202. */
  203. tmp = readl(&(port_mmio->serr));
  204. debug("P#SERR 0x%x\n",
  205. tmp);
  206. writel(tmp, &(port_mmio->serr));
  207. /* Ack any pending irq events for this port */
  208. tmp = readl(&(host_mmio->is));
  209. debug("IS 0x%x\n", tmp);
  210. if (tmp)
  211. writel(tmp, &(host_mmio->is));
  212. writel(1 << i, &(host_mmio->is));
  213. /* set irq mask (enables interrupts) */
  214. writel(DEF_PORT_IRQ, &(port_mmio->ie));
  215. /* register linkup ports */
  216. tmp = readl(&(port_mmio->ssts));
  217. debug("Port %d status: 0x%x\n", i, tmp);
  218. if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
  219. uc_priv->link_port_map |= (0x01 << i);
  220. }
  221. tmp = readl(&(host_mmio->ghc));
  222. debug("GHC 0x%x\n", tmp);
  223. writel(tmp | SATA_HOST_GHC_IE, &(host_mmio->ghc));
  224. tmp = readl(&(host_mmio->ghc));
  225. debug("GHC 0x%x\n", tmp);
  226. return 0;
  227. }
  228. static void ahci_print_info(struct ahci_uc_priv *uc_priv)
  229. {
  230. struct sata_host_regs *host_mmio = uc_priv->mmio_base;
  231. u32 vers, cap, impl, speed;
  232. const char *speed_s;
  233. const char *scc_s;
  234. vers = readl(&(host_mmio->vs));
  235. cap = uc_priv->cap;
  236. impl = uc_priv->port_map;
  237. speed = (cap & SATA_HOST_CAP_ISS_MASK)
  238. >> SATA_HOST_CAP_ISS_OFFSET;
  239. if (speed == 1)
  240. speed_s = "1.5";
  241. else if (speed == 2)
  242. speed_s = "3";
  243. else
  244. speed_s = "?";
  245. scc_s = "SATA";
  246. printf("AHCI %02x%02x.%02x%02x "
  247. "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
  248. (vers >> 24) & 0xff,
  249. (vers >> 16) & 0xff,
  250. (vers >> 8) & 0xff,
  251. vers & 0xff,
  252. ((cap >> 8) & 0x1f) + 1,
  253. (cap & 0x1f) + 1,
  254. speed_s,
  255. impl,
  256. scc_s);
  257. printf("flags: "
  258. "%s%s%s%s%s%s"
  259. "%s%s%s%s%s%s%s\n",
  260. cap & (1 << 31) ? "64bit " : "",
  261. cap & (1 << 30) ? "ncq " : "",
  262. cap & (1 << 28) ? "ilck " : "",
  263. cap & (1 << 27) ? "stag " : "",
  264. cap & (1 << 26) ? "pm " : "",
  265. cap & (1 << 25) ? "led " : "",
  266. cap & (1 << 24) ? "clo " : "",
  267. cap & (1 << 19) ? "nz " : "",
  268. cap & (1 << 18) ? "only " : "",
  269. cap & (1 << 17) ? "pmp " : "",
  270. cap & (1 << 15) ? "pio " : "",
  271. cap & (1 << 14) ? "slum " : "",
  272. cap & (1 << 13) ? "part " : "");
  273. }
  274. static int ahci_init_one(int pdev)
  275. {
  276. int rc;
  277. struct ahci_uc_priv *uc_priv = NULL;
  278. uc_priv = malloc(sizeof(struct ahci_uc_priv));
  279. memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
  280. uc_priv->dev = pdev;
  281. uc_priv->host_flags = ATA_FLAG_SATA
  282. | ATA_FLAG_NO_LEGACY
  283. | ATA_FLAG_MMIO
  284. | ATA_FLAG_PIO_DMA
  285. | ATA_FLAG_NO_ATAPI;
  286. uc_priv->mmio_base = (void __iomem *)CONFIG_DWC_AHSATA_BASE_ADDR;
  287. /* initialize adapter */
  288. rc = ahci_host_init(uc_priv);
  289. if (rc)
  290. goto err_out;
  291. ahci_print_info(uc_priv);
  292. /* Save the uc_private struct to block device struct */
  293. sata_dev_desc[pdev].priv = uc_priv;
  294. return 0;
  295. err_out:
  296. return rc;
  297. }
  298. static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
  299. unsigned char *buf, int buf_len)
  300. {
  301. struct ahci_ioports *pp = &(uc_priv->port[port]);
  302. struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
  303. u32 sg_count, max_bytes;
  304. int i;
  305. max_bytes = MAX_DATA_BYTES_PER_SG;
  306. sg_count = ((buf_len - 1) / max_bytes) + 1;
  307. if (sg_count > AHCI_MAX_SG) {
  308. printf("Error:Too much sg!\n");
  309. return -1;
  310. }
  311. for (i = 0; i < sg_count; i++) {
  312. ahci_sg->addr =
  313. cpu_to_le32((u32)buf + i * max_bytes);
  314. ahci_sg->addr_hi = 0;
  315. ahci_sg->flags_size = cpu_to_le32(0x3fffff &
  316. (buf_len < max_bytes
  317. ? (buf_len - 1)
  318. : (max_bytes - 1)));
  319. ahci_sg++;
  320. buf_len -= max_bytes;
  321. }
  322. return sg_count;
  323. }
  324. static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
  325. {
  326. struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
  327. AHCI_CMD_SLOT_SZ * cmd_slot);
  328. memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
  329. cmd_hdr->opts = cpu_to_le32(opts);
  330. cmd_hdr->status = 0;
  331. pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
  332. #ifdef CONFIG_PHYS_64BIT
  333. pp->cmd_slot->tbl_addr_hi =
  334. cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
  335. #endif
  336. }
  337. #define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
  338. static int ahci_exec_ata_cmd(struct ahci_uc_priv *uc_priv, u8 port,
  339. struct sata_fis_h2d *cfis, u8 *buf, u32 buf_len,
  340. s32 is_write)
  341. {
  342. struct ahci_ioports *pp = &(uc_priv->port[port]);
  343. struct sata_port_regs *port_mmio = pp->port_mmio;
  344. u32 opts;
  345. int sg_count = 0, cmd_slot = 0;
  346. cmd_slot = AHCI_GET_CMD_SLOT(readl(&(port_mmio->ci)));
  347. if (32 == cmd_slot) {
  348. printf("Can't find empty command slot!\n");
  349. return 0;
  350. }
  351. /* Check xfer length */
  352. if (buf_len > MAX_BYTES_PER_TRANS) {
  353. printf("Max transfer length is %dB\n\r",
  354. MAX_BYTES_PER_TRANS);
  355. return 0;
  356. }
  357. memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
  358. if (buf && buf_len)
  359. sg_count = ahci_fill_sg(uc_priv, port, buf, buf_len);
  360. opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
  361. if (is_write) {
  362. opts |= 0x40;
  363. flush_cache((ulong)buf, buf_len);
  364. }
  365. ahci_fill_cmd_slot(pp, cmd_slot, opts);
  366. flush_cache((int)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
  367. writel_with_flush(1 << cmd_slot, &(port_mmio->ci));
  368. if (waiting_for_cmd_completed((u8 *)&(port_mmio->ci),
  369. 10000, 0x1 << cmd_slot)) {
  370. printf("timeout exit!\n");
  371. return -1;
  372. }
  373. invalidate_dcache_range((int)(pp->cmd_slot),
  374. (int)(pp->cmd_slot)+AHCI_PORT_PRIV_DMA_SZ);
  375. debug("ahci_exec_ata_cmd: %d byte transferred.\n",
  376. pp->cmd_slot->status);
  377. if (!is_write)
  378. invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
  379. return buf_len;
  380. }
  381. static void ahci_set_feature(struct ahci_uc_priv *uc_priv, u8 port)
  382. {
  383. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  384. struct sata_fis_h2d *cfis = &h2d;
  385. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  386. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  387. cfis->pm_port_c = 1 << 7;
  388. cfis->command = ATA_CMD_SET_FEATURES;
  389. cfis->features = SETFEATURES_XFER;
  390. cfis->sector_count = ffs(uc_priv->udma_mask + 1) + 0x3e;
  391. ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, READ_CMD);
  392. }
  393. static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
  394. {
  395. struct ahci_ioports *pp = &(uc_priv->port[port]);
  396. struct sata_port_regs *port_mmio = pp->port_mmio;
  397. u32 port_status;
  398. u32 mem;
  399. int timeout = 10000000;
  400. debug("Enter start port: %d\n", port);
  401. port_status = readl(&(port_mmio->ssts));
  402. debug("Port %d status: %x\n", port, port_status);
  403. if ((port_status & 0xf) != 0x03) {
  404. printf("No Link on this port!\n");
  405. return -1;
  406. }
  407. mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
  408. if (!mem) {
  409. free(pp);
  410. printf("No mem for table!\n");
  411. return -ENOMEM;
  412. }
  413. mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
  414. memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  415. /*
  416. * First item in chunk of DMA memory: 32-slot command table,
  417. * 32 bytes each in size
  418. */
  419. pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
  420. debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
  421. mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
  422. /*
  423. * Second item: Received-FIS area, 256-Byte aligned
  424. */
  425. pp->rx_fis = mem;
  426. mem += AHCI_RX_FIS_SZ;
  427. /*
  428. * Third item: data area for storing a single command
  429. * and its scatter-gather table
  430. */
  431. pp->cmd_tbl = mem;
  432. debug("cmd_tbl_dma = 0x%lx\n", pp->cmd_tbl);
  433. mem += AHCI_CMD_TBL_HDR;
  434. writel_with_flush(0x00004444, &(port_mmio->dmacr));
  435. pp->cmd_tbl_sg = (struct ahci_sg *)mem;
  436. writel_with_flush((u32)pp->cmd_slot, &(port_mmio->clb));
  437. writel_with_flush(pp->rx_fis, &(port_mmio->fb));
  438. /* Enable FRE */
  439. writel_with_flush((SATA_PORT_CMD_FRE | readl(&(port_mmio->cmd))),
  440. &(port_mmio->cmd));
  441. /* Wait device ready */
  442. while ((readl(&(port_mmio->tfd)) & (SATA_PORT_TFD_STS_ERR |
  443. SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
  444. && --timeout)
  445. ;
  446. if (timeout <= 0) {
  447. debug("Device not ready for BSY, DRQ and"
  448. "ERR in TFD!\n");
  449. return -1;
  450. }
  451. writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  452. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  453. PORT_CMD_START, &(port_mmio->cmd));
  454. debug("Exit start port %d\n", port);
  455. return 0;
  456. }
  457. static void dwc_ahsata_print_info(struct blk_desc *pdev)
  458. {
  459. printf("SATA Device Info:\n\r");
  460. #ifdef CONFIG_SYS_64BIT_LBA
  461. printf("S/N: %s\n\rProduct model number: %s\n\r"
  462. "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
  463. pdev->product, pdev->vendor, pdev->revision, pdev->lba);
  464. #else
  465. printf("S/N: %s\n\rProduct model number: %s\n\r"
  466. "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
  467. pdev->product, pdev->vendor, pdev->revision, pdev->lba);
  468. #endif
  469. }
  470. static void dwc_ahsata_identify(struct ahci_uc_priv *uc_priv, u16 *id)
  471. {
  472. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  473. struct sata_fis_h2d *cfis = &h2d;
  474. u8 port = uc_priv->hard_port_no;
  475. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  476. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  477. cfis->pm_port_c = 0x80; /* is command */
  478. cfis->command = ATA_CMD_ID_ATA;
  479. ahci_exec_ata_cmd(uc_priv, port, cfis, (u8 *)id, ATA_ID_WORDS * 2,
  480. READ_CMD);
  481. ata_swap_buf_le16(id, ATA_ID_WORDS);
  482. }
  483. static void dwc_ahsata_xfer_mode(struct ahci_uc_priv *uc_priv, u16 *id)
  484. {
  485. uc_priv->pio_mask = id[ATA_ID_PIO_MODES];
  486. uc_priv->udma_mask = id[ATA_ID_UDMA_MODES];
  487. debug("pio %04x, udma %04x\n\r", uc_priv->pio_mask, uc_priv->udma_mask);
  488. }
  489. static u32 dwc_ahsata_rw_cmd(struct ahci_uc_priv *uc_priv, u32 start,
  490. u32 blkcnt, u8 *buffer, int is_write)
  491. {
  492. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  493. struct sata_fis_h2d *cfis = &h2d;
  494. u8 port = uc_priv->hard_port_no;
  495. u32 block;
  496. block = start;
  497. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  498. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  499. cfis->pm_port_c = 0x80; /* is command */
  500. cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
  501. cfis->device = ATA_LBA;
  502. cfis->device |= (block >> 24) & 0xf;
  503. cfis->lba_high = (block >> 16) & 0xff;
  504. cfis->lba_mid = (block >> 8) & 0xff;
  505. cfis->lba_low = block & 0xff;
  506. cfis->sector_count = (u8)(blkcnt & 0xff);
  507. if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
  508. ATA_SECT_SIZE * blkcnt, is_write) > 0)
  509. return blkcnt;
  510. else
  511. return 0;
  512. }
  513. static void dwc_ahsata_flush_cache(struct ahci_uc_priv *uc_priv)
  514. {
  515. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  516. struct sata_fis_h2d *cfis = &h2d;
  517. u8 port = uc_priv->hard_port_no;
  518. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  519. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  520. cfis->pm_port_c = 0x80; /* is command */
  521. cfis->command = ATA_CMD_FLUSH;
  522. ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
  523. }
  524. static u32 dwc_ahsata_rw_cmd_ext(struct ahci_uc_priv *uc_priv, u32 start,
  525. lbaint_t blkcnt, u8 *buffer, int is_write)
  526. {
  527. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  528. struct sata_fis_h2d *cfis = &h2d;
  529. u8 port = uc_priv->hard_port_no;
  530. u64 block;
  531. block = (u64)start;
  532. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  533. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  534. cfis->pm_port_c = 0x80; /* is command */
  535. cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
  536. : ATA_CMD_READ_EXT;
  537. cfis->lba_high_exp = (block >> 40) & 0xff;
  538. cfis->lba_mid_exp = (block >> 32) & 0xff;
  539. cfis->lba_low_exp = (block >> 24) & 0xff;
  540. cfis->lba_high = (block >> 16) & 0xff;
  541. cfis->lba_mid = (block >> 8) & 0xff;
  542. cfis->lba_low = block & 0xff;
  543. cfis->device = ATA_LBA;
  544. cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
  545. cfis->sector_count = blkcnt & 0xff;
  546. if (ahci_exec_ata_cmd(uc_priv, port, cfis, buffer,
  547. ATA_SECT_SIZE * blkcnt, is_write) > 0)
  548. return blkcnt;
  549. else
  550. return 0;
  551. }
  552. static void dwc_ahsata_flush_cache_ext(struct ahci_uc_priv *uc_priv)
  553. {
  554. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  555. struct sata_fis_h2d *cfis = &h2d;
  556. u8 port = uc_priv->hard_port_no;
  557. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  558. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  559. cfis->pm_port_c = 0x80; /* is command */
  560. cfis->command = ATA_CMD_FLUSH_EXT;
  561. ahci_exec_ata_cmd(uc_priv, port, cfis, NULL, 0, 0);
  562. }
  563. static void dwc_ahsata_init_wcache(struct ahci_uc_priv *uc_priv, u16 *id)
  564. {
  565. if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
  566. uc_priv->flags |= SATA_FLAG_WCACHE;
  567. if (ata_id_has_flush(id))
  568. uc_priv->flags |= SATA_FLAG_FLUSH;
  569. if (ata_id_has_flush_ext(id))
  570. uc_priv->flags |= SATA_FLAG_FLUSH_EXT;
  571. }
  572. static u32 ata_low_level_rw_lba48(struct ahci_uc_priv *uc_priv, u32 blknr,
  573. lbaint_t blkcnt, const void *buffer,
  574. int is_write)
  575. {
  576. u32 start, blks;
  577. u8 *addr;
  578. int max_blks;
  579. start = blknr;
  580. blks = blkcnt;
  581. addr = (u8 *)buffer;
  582. max_blks = ATA_MAX_SECTORS_LBA48;
  583. do {
  584. if (blks > max_blks) {
  585. if (max_blks != dwc_ahsata_rw_cmd_ext(uc_priv, start,
  586. max_blks, addr,
  587. is_write))
  588. return 0;
  589. start += max_blks;
  590. blks -= max_blks;
  591. addr += ATA_SECT_SIZE * max_blks;
  592. } else {
  593. if (blks != dwc_ahsata_rw_cmd_ext(uc_priv, start, blks,
  594. addr, is_write))
  595. return 0;
  596. start += blks;
  597. blks = 0;
  598. addr += ATA_SECT_SIZE * blks;
  599. }
  600. } while (blks != 0);
  601. return blkcnt;
  602. }
  603. static u32 ata_low_level_rw_lba28(struct ahci_uc_priv *uc_priv, u32 blknr,
  604. lbaint_t blkcnt, const void *buffer,
  605. int is_write)
  606. {
  607. u32 start, blks;
  608. u8 *addr;
  609. int max_blks;
  610. start = blknr;
  611. blks = blkcnt;
  612. addr = (u8 *)buffer;
  613. max_blks = ATA_MAX_SECTORS;
  614. do {
  615. if (blks > max_blks) {
  616. if (max_blks != dwc_ahsata_rw_cmd(uc_priv, start,
  617. max_blks, addr,
  618. is_write))
  619. return 0;
  620. start += max_blks;
  621. blks -= max_blks;
  622. addr += ATA_SECT_SIZE * max_blks;
  623. } else {
  624. if (blks != dwc_ahsata_rw_cmd(uc_priv, start, blks,
  625. addr, is_write))
  626. return 0;
  627. start += blks;
  628. blks = 0;
  629. addr += ATA_SECT_SIZE * blks;
  630. }
  631. } while (blks != 0);
  632. return blkcnt;
  633. }
  634. int init_sata(int dev)
  635. {
  636. int i;
  637. u32 linkmap;
  638. struct ahci_uc_priv *uc_priv = NULL;
  639. #if defined(CONFIG_MX6)
  640. if (!is_mx6dq() && !is_mx6dqp())
  641. return 1;
  642. #endif
  643. if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
  644. printf("The sata index %d is out of ranges\n\r", dev);
  645. return -1;
  646. }
  647. ahci_init_one(dev);
  648. uc_priv = sata_dev_desc[dev].priv;
  649. linkmap = uc_priv->link_port_map;
  650. if (0 == linkmap) {
  651. printf("No port device detected!\n");
  652. return 1;
  653. }
  654. for (i = 0; i < uc_priv->n_ports; i++) {
  655. if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
  656. if (ahci_port_start(uc_priv, (u8)i)) {
  657. printf("Can not start port %d\n", i);
  658. return 1;
  659. }
  660. uc_priv->hard_port_no = i;
  661. break;
  662. }
  663. }
  664. return 0;
  665. }
  666. int reset_sata(int dev)
  667. {
  668. struct ahci_uc_priv *uc_priv;
  669. struct sata_host_regs *host_mmio;
  670. if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
  671. printf("The sata index %d is out of ranges\n\r", dev);
  672. return -1;
  673. }
  674. uc_priv = sata_dev_desc[dev].priv;
  675. if (NULL == uc_priv)
  676. /* not initialized, so nothing to reset */
  677. return 0;
  678. host_mmio = uc_priv->mmio_base;
  679. setbits_le32(&host_mmio->ghc, SATA_HOST_GHC_HR);
  680. while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
  681. udelay(100);
  682. return 0;
  683. }
  684. int sata_port_status(int dev, int port)
  685. {
  686. struct sata_port_regs *port_mmio;
  687. struct ahci_uc_priv *uc_priv = NULL;
  688. if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1))
  689. return -EINVAL;
  690. if (sata_dev_desc[dev].priv == NULL)
  691. return -ENODEV;
  692. uc_priv = sata_dev_desc[dev].priv;
  693. port_mmio = uc_priv->port[port].port_mmio;
  694. return readl(&(port_mmio->ssts)) & SATA_PORT_SSTS_DET_MASK;
  695. }
  696. /*
  697. * SATA interface between low level driver and command layer
  698. */
  699. ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
  700. {
  701. struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
  702. u32 rc;
  703. if (sata_dev_desc[dev].lba48)
  704. rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt,
  705. buffer, READ_CMD);
  706. else
  707. rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt,
  708. buffer, READ_CMD);
  709. return rc;
  710. }
  711. ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
  712. {
  713. u32 rc;
  714. struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
  715. u32 flags = uc_priv->flags;
  716. if (sata_dev_desc[dev].lba48) {
  717. rc = ata_low_level_rw_lba48(uc_priv, blknr, blkcnt, buffer,
  718. WRITE_CMD);
  719. if ((flags & SATA_FLAG_WCACHE) &&
  720. (flags & SATA_FLAG_FLUSH_EXT))
  721. dwc_ahsata_flush_cache_ext(uc_priv);
  722. } else {
  723. rc = ata_low_level_rw_lba28(uc_priv, blknr, blkcnt, buffer,
  724. WRITE_CMD);
  725. if ((flags & SATA_FLAG_WCACHE) &&
  726. (flags & SATA_FLAG_FLUSH))
  727. dwc_ahsata_flush_cache(uc_priv);
  728. }
  729. return rc;
  730. }
  731. int scan_sata(int dev)
  732. {
  733. u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
  734. u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
  735. u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
  736. u16 *id;
  737. u64 n_sectors;
  738. struct ahci_uc_priv *uc_priv = sata_dev_desc[dev].priv;
  739. u8 port = uc_priv->hard_port_no;
  740. struct blk_desc *pdev = &(sata_dev_desc[dev]);
  741. id = (u16 *)memalign(ARCH_DMA_MINALIGN,
  742. roundup(ARCH_DMA_MINALIGN,
  743. (ATA_ID_WORDS * 2)));
  744. if (!id) {
  745. printf("id malloc failed\n\r");
  746. return -1;
  747. }
  748. /* Identify device to get information */
  749. dwc_ahsata_identify(uc_priv, id);
  750. /* Serial number */
  751. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  752. memcpy(pdev->product, serial, sizeof(serial));
  753. /* Firmware version */
  754. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  755. memcpy(pdev->revision, firmware, sizeof(firmware));
  756. /* Product model */
  757. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  758. memcpy(pdev->vendor, product, sizeof(product));
  759. /* Totoal sectors */
  760. n_sectors = ata_id_n_sectors(id);
  761. pdev->lba = (u32)n_sectors;
  762. pdev->type = DEV_TYPE_HARDDISK;
  763. pdev->blksz = ATA_SECT_SIZE;
  764. pdev->lun = 0 ;
  765. /* Check if support LBA48 */
  766. if (ata_id_has_lba48(id)) {
  767. pdev->lba48 = 1;
  768. debug("Device support LBA48\n\r");
  769. }
  770. /* Get the NCQ queue depth from device */
  771. uc_priv->flags &= (~SATA_FLAG_Q_DEP_MASK);
  772. uc_priv->flags |= ata_id_queue_depth(id);
  773. /* Get the xfer mode from device */
  774. dwc_ahsata_xfer_mode(uc_priv, id);
  775. /* Get the write cache status from device */
  776. dwc_ahsata_init_wcache(uc_priv, id);
  777. /* Set the xfer mode to highest speed */
  778. ahci_set_feature(uc_priv, port);
  779. free((void *)id);
  780. dwc_ahsata_print_info(&sata_dev_desc[dev]);
  781. is_ready = 1;
  782. return 0;
  783. }