dwc_ahsata.c 24 KB

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