dwc_ahsata.c 23 KB

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