ahci_dwc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DWC AHCI SATA Platform driver
  4. *
  5. * Copyright (C) 2021 BAIKAL ELECTRONICS, JSC
  6. */
  7. #include <linux/ahci_platform.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include <linux/clk.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/libata.h>
  14. #include <linux/log2.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm.h>
  20. #include <linux/regmap.h>
  21. #include "ahci.h"
  22. #define DRV_NAME "ahci-dwc"
  23. #define AHCI_DWC_FBS_PMPN_MAX 15
  24. /* DWC AHCI SATA controller specific registers */
  25. #define AHCI_DWC_HOST_OOBR 0xbc
  26. #define AHCI_DWC_HOST_OOB_WE BIT(31)
  27. #define AHCI_DWC_HOST_CWMIN_MASK GENMASK(30, 24)
  28. #define AHCI_DWC_HOST_CWMAX_MASK GENMASK(23, 16)
  29. #define AHCI_DWC_HOST_CIMIN_MASK GENMASK(15, 8)
  30. #define AHCI_DWC_HOST_CIMAX_MASK GENMASK(7, 0)
  31. #define AHCI_DWC_HOST_GPCR 0xd0
  32. #define AHCI_DWC_HOST_GPSR 0xd4
  33. #define AHCI_DWC_HOST_TIMER1MS 0xe0
  34. #define AHCI_DWC_HOST_TIMV_MASK GENMASK(19, 0)
  35. #define AHCI_DWC_HOST_GPARAM1R 0xe8
  36. #define AHCI_DWC_HOST_ALIGN_M BIT(31)
  37. #define AHCI_DWC_HOST_RX_BUFFER BIT(30)
  38. #define AHCI_DWC_HOST_PHY_DATA_MASK GENMASK(29, 28)
  39. #define AHCI_DWC_HOST_PHY_RST BIT(27)
  40. #define AHCI_DWC_HOST_PHY_CTRL_MASK GENMASK(26, 21)
  41. #define AHCI_DWC_HOST_PHY_STAT_MASK GENMASK(20, 15)
  42. #define AHCI_DWC_HOST_LATCH_M BIT(14)
  43. #define AHCI_DWC_HOST_PHY_TYPE_MASK GENMASK(13, 11)
  44. #define AHCI_DWC_HOST_RET_ERR BIT(10)
  45. #define AHCI_DWC_HOST_AHB_ENDIAN_MASK GENMASK(9, 8)
  46. #define AHCI_DWC_HOST_S_HADDR BIT(7)
  47. #define AHCI_DWC_HOST_M_HADDR BIT(6)
  48. #define AHCI_DWC_HOST_S_HDATA_MASK GENMASK(5, 3)
  49. #define AHCI_DWC_HOST_M_HDATA_MASK GENMASK(2, 0)
  50. #define AHCI_DWC_HOST_GPARAM2R 0xec
  51. #define AHCI_DWC_HOST_FBS_MEM_S BIT(19)
  52. #define AHCI_DWC_HOST_FBS_PMPN_MASK GENMASK(17, 16)
  53. #define AHCI_DWC_HOST_FBS_SUP BIT(15)
  54. #define AHCI_DWC_HOST_DEV_CP BIT(14)
  55. #define AHCI_DWC_HOST_DEV_MP BIT(13)
  56. #define AHCI_DWC_HOST_ENCODE_M BIT(12)
  57. #define AHCI_DWC_HOST_RXOOB_CLK_M BIT(11)
  58. #define AHCI_DWC_HOST_RXOOB_M BIT(10)
  59. #define AHCI_DWC_HOST_TXOOB_M BIT(9)
  60. #define AHCI_DWC_HOST_RXOOB_M BIT(10)
  61. #define AHCI_DWC_HOST_RXOOB_CLK_MASK GENMASK(8, 0)
  62. #define AHCI_DWC_HOST_PPARAMR 0xf0
  63. #define AHCI_DWC_HOST_TX_MEM_M BIT(11)
  64. #define AHCI_DWC_HOST_TX_MEM_S BIT(10)
  65. #define AHCI_DWC_HOST_RX_MEM_M BIT(9)
  66. #define AHCI_DWC_HOST_RX_MEM_S BIT(8)
  67. #define AHCI_DWC_HOST_TXFIFO_DEPTH GENMASK(7, 4)
  68. #define AHCI_DWC_HOST_RXFIFO_DEPTH GENMASK(3, 0)
  69. #define AHCI_DWC_HOST_TESTR 0xf4
  70. #define AHCI_DWC_HOST_PSEL_MASK GENMASK(18, 16)
  71. #define AHCI_DWC_HOST_TEST_IF BIT(0)
  72. #define AHCI_DWC_HOST_VERSIONR 0xf8
  73. #define AHCI_DWC_HOST_IDR 0xfc
  74. #define AHCI_DWC_PORT_DMACR 0x70
  75. #define AHCI_DWC_PORT_RXABL_MASK GENMASK(15, 12)
  76. #define AHCI_DWC_PORT_TXABL_MASK GENMASK(11, 8)
  77. #define AHCI_DWC_PORT_RXTS_MASK GENMASK(7, 4)
  78. #define AHCI_DWC_PORT_TXTS_MASK GENMASK(3, 0)
  79. #define AHCI_DWC_PORT_PHYCR 0x74
  80. #define AHCI_DWC_PORT_PHYSR 0x78
  81. /* Baikal-T1 AHCI SATA specific registers */
  82. #define AHCI_BT1_HOST_PHYCR AHCI_DWC_HOST_GPCR
  83. #define AHCI_BT1_HOST_MPLM_MASK GENMASK(29, 23)
  84. #define AHCI_BT1_HOST_LOSDT_MASK GENMASK(22, 20)
  85. #define AHCI_BT1_HOST_CRR BIT(19)
  86. #define AHCI_BT1_HOST_CRW BIT(18)
  87. #define AHCI_BT1_HOST_CRCD BIT(17)
  88. #define AHCI_BT1_HOST_CRCA BIT(16)
  89. #define AHCI_BT1_HOST_CRDI_MASK GENMASK(15, 0)
  90. #define AHCI_BT1_HOST_PHYSR AHCI_DWC_HOST_GPSR
  91. #define AHCI_BT1_HOST_CRA BIT(16)
  92. #define AHCI_BT1_HOST_CRDO_MASK GENMASK(15, 0)
  93. struct ahci_dwc_plat_data {
  94. unsigned int pflags;
  95. unsigned int hflags;
  96. int (*init)(struct ahci_host_priv *hpriv);
  97. int (*reinit)(struct ahci_host_priv *hpriv);
  98. void (*clear)(struct ahci_host_priv *hpriv);
  99. };
  100. struct ahci_dwc_host_priv {
  101. const struct ahci_dwc_plat_data *pdata;
  102. struct platform_device *pdev;
  103. u32 timv;
  104. u32 dmacr[AHCI_MAX_PORTS];
  105. };
  106. static int ahci_bt1_init(struct ahci_host_priv *hpriv)
  107. {
  108. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  109. int ret;
  110. /* APB, application and reference clocks are required */
  111. if (!ahci_platform_find_clk(hpriv, "pclk") ||
  112. !ahci_platform_find_clk(hpriv, "aclk") ||
  113. !ahci_platform_find_clk(hpriv, "ref")) {
  114. dev_err(&dpriv->pdev->dev, "No system clocks specified\n");
  115. return -EINVAL;
  116. }
  117. /*
  118. * Fully reset the SATA AXI and ref clocks domain to ensure the state
  119. * machine is working from scratch especially if the reference clocks
  120. * source has been changed.
  121. */
  122. ret = ahci_platform_assert_rsts(hpriv);
  123. if (ret) {
  124. dev_err(&dpriv->pdev->dev, "Couldn't assert the resets\n");
  125. return ret;
  126. }
  127. ret = ahci_platform_deassert_rsts(hpriv);
  128. if (ret) {
  129. dev_err(&dpriv->pdev->dev, "Couldn't de-assert the resets\n");
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static struct ahci_host_priv *ahci_dwc_get_resources(struct platform_device *pdev)
  135. {
  136. struct ahci_dwc_host_priv *dpriv;
  137. struct ahci_host_priv *hpriv;
  138. dpriv = devm_kzalloc(&pdev->dev, sizeof(*dpriv), GFP_KERNEL);
  139. if (!dpriv)
  140. return ERR_PTR(-ENOMEM);
  141. dpriv->pdev = pdev;
  142. dpriv->pdata = device_get_match_data(&pdev->dev);
  143. if (!dpriv->pdata)
  144. return ERR_PTR(-EINVAL);
  145. hpriv = ahci_platform_get_resources(pdev, dpriv->pdata->pflags);
  146. if (IS_ERR(hpriv))
  147. return hpriv;
  148. hpriv->flags |= dpriv->pdata->hflags;
  149. hpriv->plat_data = (void *)dpriv;
  150. return hpriv;
  151. }
  152. static void ahci_dwc_check_cap(struct ahci_host_priv *hpriv)
  153. {
  154. unsigned long port_map = hpriv->saved_port_map | hpriv->mask_port_map;
  155. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  156. bool dev_mp, dev_cp, fbs_sup;
  157. unsigned int fbs_pmp;
  158. u32 param;
  159. int i;
  160. param = readl(hpriv->mmio + AHCI_DWC_HOST_GPARAM2R);
  161. dev_mp = !!(param & AHCI_DWC_HOST_DEV_MP);
  162. dev_cp = !!(param & AHCI_DWC_HOST_DEV_CP);
  163. fbs_sup = !!(param & AHCI_DWC_HOST_FBS_SUP);
  164. fbs_pmp = 5 * FIELD_GET(AHCI_DWC_HOST_FBS_PMPN_MASK, param);
  165. if (!dev_mp && hpriv->saved_cap & HOST_CAP_MPS) {
  166. dev_warn(&dpriv->pdev->dev, "MPS is unsupported\n");
  167. hpriv->saved_cap &= ~HOST_CAP_MPS;
  168. }
  169. if (fbs_sup && fbs_pmp < AHCI_DWC_FBS_PMPN_MAX) {
  170. dev_warn(&dpriv->pdev->dev, "PMPn is limited up to %u ports\n",
  171. fbs_pmp);
  172. }
  173. for_each_set_bit(i, &port_map, AHCI_MAX_PORTS) {
  174. if (!dev_mp && hpriv->saved_port_cap[i] & PORT_CMD_MPSP) {
  175. dev_warn(&dpriv->pdev->dev, "MPS incapable port %d\n", i);
  176. hpriv->saved_port_cap[i] &= ~PORT_CMD_MPSP;
  177. }
  178. if (!dev_cp && hpriv->saved_port_cap[i] & PORT_CMD_CPD) {
  179. dev_warn(&dpriv->pdev->dev, "CPD incapable port %d\n", i);
  180. hpriv->saved_port_cap[i] &= ~PORT_CMD_CPD;
  181. }
  182. if (!fbs_sup && hpriv->saved_port_cap[i] & PORT_CMD_FBSCP) {
  183. dev_warn(&dpriv->pdev->dev, "FBS incapable port %d\n", i);
  184. hpriv->saved_port_cap[i] &= ~PORT_CMD_FBSCP;
  185. }
  186. }
  187. }
  188. static void ahci_dwc_init_timer(struct ahci_host_priv *hpriv)
  189. {
  190. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  191. unsigned long rate;
  192. struct clk *aclk;
  193. u32 cap, cap2;
  194. /* 1ms tick is generated only for the CCC or DevSleep features */
  195. cap = readl(hpriv->mmio + HOST_CAP);
  196. cap2 = readl(hpriv->mmio + HOST_CAP2);
  197. if (!(cap & HOST_CAP_CCC) && !(cap2 & HOST_CAP2_SDS))
  198. return;
  199. /*
  200. * Tick is generated based on the AXI/AHB application clocks signal
  201. * so we need to be sure in the clock we are going to use.
  202. */
  203. aclk = ahci_platform_find_clk(hpriv, "aclk");
  204. if (!aclk)
  205. return;
  206. /* 1ms timer interval is set as TIMV = AMBA_FREQ[MHZ] * 1000 */
  207. dpriv->timv = readl(hpriv->mmio + AHCI_DWC_HOST_TIMER1MS);
  208. dpriv->timv = FIELD_GET(AHCI_DWC_HOST_TIMV_MASK, dpriv->timv);
  209. rate = clk_get_rate(aclk) / 1000UL;
  210. if (rate == dpriv->timv)
  211. return;
  212. dev_info(&dpriv->pdev->dev, "Update CCC/DevSlp timer for Fapp %lu MHz\n",
  213. rate / 1000UL);
  214. dpriv->timv = FIELD_PREP(AHCI_DWC_HOST_TIMV_MASK, rate);
  215. writel(dpriv->timv, hpriv->mmio + AHCI_DWC_HOST_TIMER1MS);
  216. }
  217. static int ahci_dwc_init_dmacr(struct ahci_host_priv *hpriv)
  218. {
  219. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  220. struct device_node *child;
  221. void __iomem *port_mmio;
  222. u32 port, dmacr, ts;
  223. /*
  224. * Update the DMA Tx/Rx transaction sizes in accordance with the
  225. * platform setup. Note values exceeding maximal or minimal limits will
  226. * be automatically clamped. Also note the register isn't affected by
  227. * the HBA global reset so we can freely initialize it once until the
  228. * next system reset.
  229. */
  230. for_each_child_of_node(dpriv->pdev->dev.of_node, child) {
  231. if (!of_device_is_available(child))
  232. continue;
  233. if (of_property_read_u32(child, "reg", &port)) {
  234. of_node_put(child);
  235. return -EINVAL;
  236. }
  237. port_mmio = __ahci_port_base(hpriv, port);
  238. dmacr = readl(port_mmio + AHCI_DWC_PORT_DMACR);
  239. if (!of_property_read_u32(child, "snps,tx-ts-max", &ts)) {
  240. ts = ilog2(ts);
  241. dmacr &= ~AHCI_DWC_PORT_TXTS_MASK;
  242. dmacr |= FIELD_PREP(AHCI_DWC_PORT_TXTS_MASK, ts);
  243. }
  244. if (!of_property_read_u32(child, "snps,rx-ts-max", &ts)) {
  245. ts = ilog2(ts);
  246. dmacr &= ~AHCI_DWC_PORT_RXTS_MASK;
  247. dmacr |= FIELD_PREP(AHCI_DWC_PORT_RXTS_MASK, ts);
  248. }
  249. writel(dmacr, port_mmio + AHCI_DWC_PORT_DMACR);
  250. dpriv->dmacr[port] = dmacr;
  251. }
  252. return 0;
  253. }
  254. static int ahci_dwc_init_host(struct ahci_host_priv *hpriv)
  255. {
  256. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  257. int rc;
  258. rc = ahci_platform_enable_resources(hpriv);
  259. if (rc)
  260. return rc;
  261. if (dpriv->pdata->init) {
  262. rc = dpriv->pdata->init(hpriv);
  263. if (rc)
  264. goto err_disable_resources;
  265. }
  266. ahci_dwc_check_cap(hpriv);
  267. ahci_dwc_init_timer(hpriv);
  268. rc = ahci_dwc_init_dmacr(hpriv);
  269. if (rc)
  270. goto err_clear_platform;
  271. return 0;
  272. err_clear_platform:
  273. if (dpriv->pdata->clear)
  274. dpriv->pdata->clear(hpriv);
  275. err_disable_resources:
  276. ahci_platform_disable_resources(hpriv);
  277. return rc;
  278. }
  279. static int ahci_dwc_reinit_host(struct ahci_host_priv *hpriv)
  280. {
  281. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  282. unsigned long port_map = hpriv->port_map;
  283. void __iomem *port_mmio;
  284. int i, rc;
  285. rc = ahci_platform_enable_resources(hpriv);
  286. if (rc)
  287. return rc;
  288. if (dpriv->pdata->reinit) {
  289. rc = dpriv->pdata->reinit(hpriv);
  290. if (rc)
  291. goto err_disable_resources;
  292. }
  293. writel(dpriv->timv, hpriv->mmio + AHCI_DWC_HOST_TIMER1MS);
  294. for_each_set_bit(i, &port_map, AHCI_MAX_PORTS) {
  295. port_mmio = __ahci_port_base(hpriv, i);
  296. writel(dpriv->dmacr[i], port_mmio + AHCI_DWC_PORT_DMACR);
  297. }
  298. return 0;
  299. err_disable_resources:
  300. ahci_platform_disable_resources(hpriv);
  301. return rc;
  302. }
  303. static void ahci_dwc_clear_host(struct ahci_host_priv *hpriv)
  304. {
  305. struct ahci_dwc_host_priv *dpriv = hpriv->plat_data;
  306. if (dpriv->pdata->clear)
  307. dpriv->pdata->clear(hpriv);
  308. ahci_platform_disable_resources(hpriv);
  309. }
  310. static void ahci_dwc_stop_host(struct ata_host *host)
  311. {
  312. struct ahci_host_priv *hpriv = host->private_data;
  313. ahci_dwc_clear_host(hpriv);
  314. }
  315. static struct ata_port_operations ahci_dwc_port_ops = {
  316. .inherits = &ahci_platform_ops,
  317. .host_stop = ahci_dwc_stop_host,
  318. };
  319. static const struct ata_port_info ahci_dwc_port_info = {
  320. .flags = AHCI_FLAG_COMMON,
  321. .pio_mask = ATA_PIO4,
  322. .udma_mask = ATA_UDMA6,
  323. .port_ops = &ahci_dwc_port_ops,
  324. };
  325. static struct scsi_host_template ahci_dwc_scsi_info = {
  326. AHCI_SHT(DRV_NAME),
  327. };
  328. static int ahci_dwc_probe(struct platform_device *pdev)
  329. {
  330. struct ahci_host_priv *hpriv;
  331. int rc;
  332. hpriv = ahci_dwc_get_resources(pdev);
  333. if (IS_ERR(hpriv))
  334. return PTR_ERR(hpriv);
  335. rc = ahci_dwc_init_host(hpriv);
  336. if (rc)
  337. return rc;
  338. rc = ahci_platform_init_host(pdev, hpriv, &ahci_dwc_port_info,
  339. &ahci_dwc_scsi_info);
  340. if (rc)
  341. goto err_clear_host;
  342. return 0;
  343. err_clear_host:
  344. ahci_dwc_clear_host(hpriv);
  345. return rc;
  346. }
  347. static int ahci_dwc_suspend(struct device *dev)
  348. {
  349. struct ata_host *host = dev_get_drvdata(dev);
  350. struct ahci_host_priv *hpriv = host->private_data;
  351. int rc;
  352. rc = ahci_platform_suspend_host(dev);
  353. if (rc)
  354. return rc;
  355. ahci_dwc_clear_host(hpriv);
  356. return 0;
  357. }
  358. static int ahci_dwc_resume(struct device *dev)
  359. {
  360. struct ata_host *host = dev_get_drvdata(dev);
  361. struct ahci_host_priv *hpriv = host->private_data;
  362. int rc;
  363. rc = ahci_dwc_reinit_host(hpriv);
  364. if (rc)
  365. return rc;
  366. return ahci_platform_resume_host(dev);
  367. }
  368. static DEFINE_SIMPLE_DEV_PM_OPS(ahci_dwc_pm_ops, ahci_dwc_suspend,
  369. ahci_dwc_resume);
  370. static struct ahci_dwc_plat_data ahci_dwc_plat = {
  371. .pflags = AHCI_PLATFORM_GET_RESETS,
  372. };
  373. static struct ahci_dwc_plat_data ahci_bt1_plat = {
  374. .pflags = AHCI_PLATFORM_GET_RESETS | AHCI_PLATFORM_RST_TRIGGER,
  375. .init = ahci_bt1_init,
  376. };
  377. static const struct of_device_id ahci_dwc_of_match[] = {
  378. { .compatible = "snps,dwc-ahci", &ahci_dwc_plat },
  379. { .compatible = "snps,spear-ahci", &ahci_dwc_plat },
  380. { .compatible = "baikal,bt1-ahci", &ahci_bt1_plat },
  381. {},
  382. };
  383. MODULE_DEVICE_TABLE(of, ahci_dwc_of_match);
  384. static struct platform_driver ahci_dwc_driver = {
  385. .probe = ahci_dwc_probe,
  386. .remove = ata_platform_remove_one,
  387. .shutdown = ahci_platform_shutdown,
  388. .driver = {
  389. .name = DRV_NAME,
  390. .of_match_table = ahci_dwc_of_match,
  391. .pm = &ahci_dwc_pm_ops,
  392. },
  393. };
  394. module_platform_driver(ahci_dwc_driver);
  395. MODULE_DESCRIPTION("DWC AHCI SATA platform driver");
  396. MODULE_AUTHOR("Serge Semin <[email protected]>");
  397. MODULE_LICENSE("GPL");