dwc_ahsata.c 32 KB

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