dwc_ahsata.c 24 KB

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