dwc_ahsata.c 26 KB

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