dwc_ahsata.c 22 KB

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