dwc_ahsata.c 23 KB

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