dwc_ahsata.c 26 KB

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