dwc_ahsata.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 <asm/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 u32 ahci_port_base(u32 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((u32)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 = 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. cmd_hdr->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
  339. cmd_hdr->tbl_addr_hi = 0;
  340. }
  341. #define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
  342. static int ahci_exec_ata_cmd(struct ahci_probe_ent *probe_ent,
  343. u8 port, struct sata_fis_h2d *cfis,
  344. u8 *buf, u32 buf_len, s32 is_write)
  345. {
  346. struct ahci_ioports *pp = &(probe_ent->port[port]);
  347. struct sata_port_regs *port_mmio =
  348. (struct sata_port_regs *)pp->port_mmio;
  349. u32 opts;
  350. int sg_count = 0, cmd_slot = 0;
  351. cmd_slot = AHCI_GET_CMD_SLOT(readl(&(port_mmio->ci)));
  352. if (32 == cmd_slot) {
  353. printf("Can't find empty command slot!\n");
  354. return 0;
  355. }
  356. /* Check xfer length */
  357. if (buf_len > MAX_BYTES_PER_TRANS) {
  358. printf("Max transfer length is %dB\n\r",
  359. MAX_BYTES_PER_TRANS);
  360. return 0;
  361. }
  362. memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
  363. if (buf && buf_len)
  364. sg_count = ahci_fill_sg(probe_ent, port, buf, buf_len);
  365. opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
  366. if (is_write) {
  367. opts |= 0x40;
  368. flush_cache((ulong)buf, buf_len);
  369. }
  370. ahci_fill_cmd_slot(pp, cmd_slot, opts);
  371. flush_cache((int)(pp->cmd_slot), AHCI_PORT_PRIV_DMA_SZ);
  372. writel_with_flush(1 << cmd_slot, &(port_mmio->ci));
  373. if (waiting_for_cmd_completed((u8 *)&(port_mmio->ci),
  374. 10000, 0x1 << cmd_slot)) {
  375. printf("timeout exit!\n");
  376. return -1;
  377. }
  378. invalidate_dcache_range((int)(pp->cmd_slot),
  379. (int)(pp->cmd_slot)+AHCI_PORT_PRIV_DMA_SZ);
  380. debug("ahci_exec_ata_cmd: %d byte transferred.\n",
  381. pp->cmd_slot->status);
  382. if (!is_write)
  383. invalidate_dcache_range((ulong)buf, (ulong)buf+buf_len);
  384. return buf_len;
  385. }
  386. static void ahci_set_feature(u8 dev, u8 port)
  387. {
  388. struct ahci_probe_ent *probe_ent =
  389. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  390. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  391. struct sata_fis_h2d *cfis = &h2d;
  392. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  393. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  394. cfis->pm_port_c = 1 << 7;
  395. cfis->command = ATA_CMD_SET_FEATURES;
  396. cfis->features = SETFEATURES_XFER;
  397. cfis->sector_count = ffs(probe_ent->udma_mask + 1) + 0x3e;
  398. ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, READ_CMD);
  399. }
  400. static int ahci_port_start(struct ahci_probe_ent *probe_ent,
  401. u8 port)
  402. {
  403. struct ahci_ioports *pp = &(probe_ent->port[port]);
  404. struct sata_port_regs *port_mmio =
  405. (struct sata_port_regs *)pp->port_mmio;
  406. u32 port_status;
  407. u32 mem;
  408. int timeout = 10000000;
  409. debug("Enter start port: %d\n", port);
  410. port_status = readl(&(port_mmio->ssts));
  411. debug("Port %d status: %x\n", port, port_status);
  412. if ((port_status & 0xf) != 0x03) {
  413. printf("No Link on this port!\n");
  414. return -1;
  415. }
  416. mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
  417. if (!mem) {
  418. free(pp);
  419. printf("No mem for table!\n");
  420. return -ENOMEM;
  421. }
  422. mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
  423. memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  424. /*
  425. * First item in chunk of DMA memory: 32-slot command table,
  426. * 32 bytes each in size
  427. */
  428. pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
  429. debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
  430. mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
  431. /*
  432. * Second item: Received-FIS area, 256-Byte aligned
  433. */
  434. pp->rx_fis = mem;
  435. mem += AHCI_RX_FIS_SZ;
  436. /*
  437. * Third item: data area for storing a single command
  438. * and its scatter-gather table
  439. */
  440. pp->cmd_tbl = mem;
  441. debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
  442. mem += AHCI_CMD_TBL_HDR;
  443. writel_with_flush(0x00004444, &(port_mmio->dmacr));
  444. pp->cmd_tbl_sg = (struct ahci_sg *)mem;
  445. writel_with_flush((u32)pp->cmd_slot, &(port_mmio->clb));
  446. writel_with_flush(pp->rx_fis, &(port_mmio->fb));
  447. /* Enable FRE */
  448. writel_with_flush((SATA_PORT_CMD_FRE | readl(&(port_mmio->cmd))),
  449. &(port_mmio->cmd));
  450. /* Wait device ready */
  451. while ((readl(&(port_mmio->tfd)) & (SATA_PORT_TFD_STS_ERR |
  452. SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
  453. && --timeout)
  454. ;
  455. if (timeout <= 0) {
  456. debug("Device not ready for BSY, DRQ and"
  457. "ERR in TFD!\n");
  458. return -1;
  459. }
  460. writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  461. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  462. PORT_CMD_START, &(port_mmio->cmd));
  463. debug("Exit start port %d\n", port);
  464. return 0;
  465. }
  466. int init_sata(int dev)
  467. {
  468. int i;
  469. u32 linkmap;
  470. struct ahci_probe_ent *probe_ent = NULL;
  471. #if defined(CONFIG_MX6)
  472. if (!is_cpu_type(MXC_CPU_MX6Q) && !is_cpu_type(MXC_CPU_MX6D))
  473. return 1;
  474. #endif
  475. if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
  476. printf("The sata index %d is out of ranges\n\r", dev);
  477. return -1;
  478. }
  479. ahci_init_one(dev);
  480. probe_ent = (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  481. linkmap = probe_ent->link_port_map;
  482. if (0 == linkmap) {
  483. printf("No port device detected!\n");
  484. return 1;
  485. }
  486. for (i = 0; i < probe_ent->n_ports; i++) {
  487. if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
  488. if (ahci_port_start(probe_ent, (u8)i)) {
  489. printf("Can not start port %d\n", i);
  490. return 1;
  491. }
  492. probe_ent->hard_port_no = i;
  493. break;
  494. }
  495. }
  496. return 0;
  497. }
  498. static void dwc_ahsata_print_info(int dev)
  499. {
  500. block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
  501. printf("SATA Device Info:\n\r");
  502. #ifdef CONFIG_SYS_64BIT_LBA
  503. printf("S/N: %s\n\rProduct model number: %s\n\r"
  504. "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
  505. pdev->product, pdev->vendor, pdev->revision, pdev->lba);
  506. #else
  507. printf("S/N: %s\n\rProduct model number: %s\n\r"
  508. "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
  509. pdev->product, pdev->vendor, pdev->revision, pdev->lba);
  510. #endif
  511. }
  512. static void dwc_ahsata_identify(int dev, u16 *id)
  513. {
  514. struct ahci_probe_ent *probe_ent =
  515. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  516. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  517. struct sata_fis_h2d *cfis = &h2d;
  518. u8 port = probe_ent->hard_port_no;
  519. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  520. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  521. cfis->pm_port_c = 0x80; /* is command */
  522. cfis->command = ATA_CMD_ID_ATA;
  523. ahci_exec_ata_cmd(probe_ent, port, cfis,
  524. (u8 *)id, ATA_ID_WORDS * 2, READ_CMD);
  525. ata_swap_buf_le16(id, ATA_ID_WORDS);
  526. }
  527. static void dwc_ahsata_xfer_mode(int dev, u16 *id)
  528. {
  529. struct ahci_probe_ent *probe_ent =
  530. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  531. probe_ent->pio_mask = id[ATA_ID_PIO_MODES];
  532. probe_ent->udma_mask = id[ATA_ID_UDMA_MODES];
  533. debug("pio %04x, udma %04x\n\r",
  534. probe_ent->pio_mask, probe_ent->udma_mask);
  535. }
  536. static u32 dwc_ahsata_rw_cmd(int dev, u32 start, u32 blkcnt,
  537. u8 *buffer, int is_write)
  538. {
  539. struct ahci_probe_ent *probe_ent =
  540. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  541. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  542. struct sata_fis_h2d *cfis = &h2d;
  543. u8 port = probe_ent->hard_port_no;
  544. u32 block;
  545. block = start;
  546. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  547. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  548. cfis->pm_port_c = 0x80; /* is command */
  549. cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
  550. cfis->device = ATA_LBA;
  551. cfis->device |= (block >> 24) & 0xf;
  552. cfis->lba_high = (block >> 16) & 0xff;
  553. cfis->lba_mid = (block >> 8) & 0xff;
  554. cfis->lba_low = block & 0xff;
  555. cfis->sector_count = (u8)(blkcnt & 0xff);
  556. if (ahci_exec_ata_cmd(probe_ent, port, cfis,
  557. buffer, ATA_SECT_SIZE * blkcnt, is_write) > 0)
  558. return blkcnt;
  559. else
  560. return 0;
  561. }
  562. void dwc_ahsata_flush_cache(int dev)
  563. {
  564. struct ahci_probe_ent *probe_ent =
  565. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  566. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  567. struct sata_fis_h2d *cfis = &h2d;
  568. u8 port = probe_ent->hard_port_no;
  569. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  570. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  571. cfis->pm_port_c = 0x80; /* is command */
  572. cfis->command = ATA_CMD_FLUSH;
  573. ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
  574. }
  575. static u32 dwc_ahsata_rw_cmd_ext(int dev, u32 start, lbaint_t blkcnt,
  576. u8 *buffer, int is_write)
  577. {
  578. struct ahci_probe_ent *probe_ent =
  579. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  580. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  581. struct sata_fis_h2d *cfis = &h2d;
  582. u8 port = probe_ent->hard_port_no;
  583. u64 block;
  584. block = (u64)start;
  585. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  586. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  587. cfis->pm_port_c = 0x80; /* is command */
  588. cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
  589. : ATA_CMD_READ_EXT;
  590. cfis->lba_high_exp = (block >> 40) & 0xff;
  591. cfis->lba_mid_exp = (block >> 32) & 0xff;
  592. cfis->lba_low_exp = (block >> 24) & 0xff;
  593. cfis->lba_high = (block >> 16) & 0xff;
  594. cfis->lba_mid = (block >> 8) & 0xff;
  595. cfis->lba_low = block & 0xff;
  596. cfis->device = ATA_LBA;
  597. cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
  598. cfis->sector_count = blkcnt & 0xff;
  599. if (ahci_exec_ata_cmd(probe_ent, port, cfis, buffer,
  600. ATA_SECT_SIZE * blkcnt, is_write) > 0)
  601. return blkcnt;
  602. else
  603. return 0;
  604. }
  605. u32 dwc_ahsata_rw_ncq_cmd(int dev, u32 start, lbaint_t blkcnt,
  606. u8 *buffer, int is_write)
  607. {
  608. struct ahci_probe_ent *probe_ent =
  609. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  610. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  611. struct sata_fis_h2d *cfis = &h2d;
  612. u8 port = probe_ent->hard_port_no;
  613. u64 block;
  614. if (sata_dev_desc[dev].lba48 != 1) {
  615. printf("execute FPDMA command on non-LBA48 hard disk\n\r");
  616. return -1;
  617. }
  618. block = (u64)start;
  619. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  620. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  621. cfis->pm_port_c = 0x80; /* is command */
  622. cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
  623. : ATA_CMD_FPDMA_READ;
  624. cfis->lba_high_exp = (block >> 40) & 0xff;
  625. cfis->lba_mid_exp = (block >> 32) & 0xff;
  626. cfis->lba_low_exp = (block >> 24) & 0xff;
  627. cfis->lba_high = (block >> 16) & 0xff;
  628. cfis->lba_mid = (block >> 8) & 0xff;
  629. cfis->lba_low = block & 0xff;
  630. cfis->device = ATA_LBA;
  631. cfis->features_exp = (blkcnt >> 8) & 0xff;
  632. cfis->features = blkcnt & 0xff;
  633. /* Use the latest queue */
  634. ahci_exec_ata_cmd(probe_ent, port, cfis,
  635. buffer, ATA_SECT_SIZE * blkcnt, is_write);
  636. return blkcnt;
  637. }
  638. void dwc_ahsata_flush_cache_ext(int dev)
  639. {
  640. struct ahci_probe_ent *probe_ent =
  641. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  642. struct sata_fis_h2d h2d __aligned(ARCH_DMA_MINALIGN);
  643. struct sata_fis_h2d *cfis = &h2d;
  644. u8 port = probe_ent->hard_port_no;
  645. memset(cfis, 0, sizeof(struct sata_fis_h2d));
  646. cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  647. cfis->pm_port_c = 0x80; /* is command */
  648. cfis->command = ATA_CMD_FLUSH_EXT;
  649. ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
  650. }
  651. static void dwc_ahsata_init_wcache(int dev, u16 *id)
  652. {
  653. struct ahci_probe_ent *probe_ent =
  654. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  655. if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
  656. probe_ent->flags |= SATA_FLAG_WCACHE;
  657. if (ata_id_has_flush(id))
  658. probe_ent->flags |= SATA_FLAG_FLUSH;
  659. if (ata_id_has_flush_ext(id))
  660. probe_ent->flags |= SATA_FLAG_FLUSH_EXT;
  661. }
  662. u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
  663. const void *buffer, int is_write)
  664. {
  665. u32 start, blks;
  666. u8 *addr;
  667. int max_blks;
  668. start = blknr;
  669. blks = blkcnt;
  670. addr = (u8 *)buffer;
  671. max_blks = ATA_MAX_SECTORS_LBA48;
  672. do {
  673. if (blks > max_blks) {
  674. if (max_blks != dwc_ahsata_rw_cmd_ext(dev, start,
  675. max_blks, addr, is_write))
  676. return 0;
  677. start += max_blks;
  678. blks -= max_blks;
  679. addr += ATA_SECT_SIZE * max_blks;
  680. } else {
  681. if (blks != dwc_ahsata_rw_cmd_ext(dev, start,
  682. blks, addr, is_write))
  683. return 0;
  684. start += blks;
  685. blks = 0;
  686. addr += ATA_SECT_SIZE * blks;
  687. }
  688. } while (blks != 0);
  689. return blkcnt;
  690. }
  691. u32 ata_low_level_rw_lba28(int dev, u32 blknr, lbaint_t blkcnt,
  692. const void *buffer, int is_write)
  693. {
  694. u32 start, blks;
  695. u8 *addr;
  696. int max_blks;
  697. start = blknr;
  698. blks = blkcnt;
  699. addr = (u8 *)buffer;
  700. max_blks = ATA_MAX_SECTORS;
  701. do {
  702. if (blks > max_blks) {
  703. if (max_blks != dwc_ahsata_rw_cmd(dev, start,
  704. max_blks, addr, is_write))
  705. return 0;
  706. start += max_blks;
  707. blks -= max_blks;
  708. addr += ATA_SECT_SIZE * max_blks;
  709. } else {
  710. if (blks != dwc_ahsata_rw_cmd(dev, start,
  711. blks, addr, is_write))
  712. return 0;
  713. start += blks;
  714. blks = 0;
  715. addr += ATA_SECT_SIZE * blks;
  716. }
  717. } while (blks != 0);
  718. return blkcnt;
  719. }
  720. /*
  721. * SATA interface between low level driver and command layer
  722. */
  723. ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
  724. {
  725. u32 rc;
  726. if (sata_dev_desc[dev].lba48)
  727. rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
  728. buffer, READ_CMD);
  729. else
  730. rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
  731. buffer, READ_CMD);
  732. return rc;
  733. }
  734. ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
  735. {
  736. u32 rc;
  737. struct ahci_probe_ent *probe_ent =
  738. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  739. u32 flags = probe_ent->flags;
  740. if (sata_dev_desc[dev].lba48) {
  741. rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
  742. buffer, WRITE_CMD);
  743. if ((flags & SATA_FLAG_WCACHE) &&
  744. (flags & SATA_FLAG_FLUSH_EXT))
  745. dwc_ahsata_flush_cache_ext(dev);
  746. } else {
  747. rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
  748. buffer, WRITE_CMD);
  749. if ((flags & SATA_FLAG_WCACHE) &&
  750. (flags & SATA_FLAG_FLUSH))
  751. dwc_ahsata_flush_cache(dev);
  752. }
  753. return rc;
  754. }
  755. int scan_sata(int dev)
  756. {
  757. u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
  758. u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
  759. u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
  760. u16 *id;
  761. u64 n_sectors;
  762. struct ahci_probe_ent *probe_ent =
  763. (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
  764. u8 port = probe_ent->hard_port_no;
  765. block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
  766. id = (u16 *)memalign(ARCH_DMA_MINALIGN,
  767. roundup(ARCH_DMA_MINALIGN,
  768. (ATA_ID_WORDS * 2)));
  769. if (!id) {
  770. printf("id malloc failed\n\r");
  771. return -1;
  772. }
  773. /* Identify device to get information */
  774. dwc_ahsata_identify(dev, id);
  775. /* Serial number */
  776. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  777. memcpy(pdev->product, serial, sizeof(serial));
  778. /* Firmware version */
  779. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  780. memcpy(pdev->revision, firmware, sizeof(firmware));
  781. /* Product model */
  782. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  783. memcpy(pdev->vendor, product, sizeof(product));
  784. /* Totoal sectors */
  785. n_sectors = ata_id_n_sectors(id);
  786. pdev->lba = (u32)n_sectors;
  787. pdev->type = DEV_TYPE_HARDDISK;
  788. pdev->blksz = ATA_SECT_SIZE;
  789. pdev->lun = 0 ;
  790. /* Check if support LBA48 */
  791. if (ata_id_has_lba48(id)) {
  792. pdev->lba48 = 1;
  793. debug("Device support LBA48\n\r");
  794. }
  795. /* Get the NCQ queue depth from device */
  796. probe_ent->flags &= (~SATA_FLAG_Q_DEP_MASK);
  797. probe_ent->flags |= ata_id_queue_depth(id);
  798. /* Get the xfer mode from device */
  799. dwc_ahsata_xfer_mode(dev, id);
  800. /* Get the write cache status from device */
  801. dwc_ahsata_init_wcache(dev, id);
  802. /* Set the xfer mode to highest speed */
  803. ahci_set_feature(dev, port);
  804. free((void *)id);
  805. dwc_ahsata_print_info(dev);
  806. is_ready = 1;
  807. return 0;
  808. }