dwc_ahsata.c 22 KB

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