ahci_ceva.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Xilinx, Inc.
  4. * CEVA AHCI SATA platform driver
  5. *
  6. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  7. */
  8. #include <linux/ahci_platform.h>
  9. #include <linux/kernel.h>
  10. #include <linux/libata.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #include "ahci.h"
  16. /* Vendor Specific Register Offsets */
  17. #define AHCI_VEND_PCFG 0xA4
  18. #define AHCI_VEND_PPCFG 0xA8
  19. #define AHCI_VEND_PP2C 0xAC
  20. #define AHCI_VEND_PP3C 0xB0
  21. #define AHCI_VEND_PP4C 0xB4
  22. #define AHCI_VEND_PP5C 0xB8
  23. #define AHCI_VEND_AXICC 0xBC
  24. #define AHCI_VEND_PAXIC 0xC0
  25. #define AHCI_VEND_PTC 0xC8
  26. /* Vendor Specific Register bit definitions */
  27. #define PAXIC_ADBW_BW64 0x1
  28. #define PAXIC_MAWID(i) (((i) * 2) << 4)
  29. #define PAXIC_MARID(i) (((i) * 2) << 12)
  30. #define PAXIC_MARIDD(i) ((((i) * 2) + 1) << 16)
  31. #define PAXIC_MAWIDD(i) ((((i) * 2) + 1) << 8)
  32. #define PAXIC_OTL (0x4 << 20)
  33. /* Register bit definitions for cache control */
  34. #define AXICC_ARCA_VAL (0xF << 0)
  35. #define AXICC_ARCF_VAL (0xF << 4)
  36. #define AXICC_ARCH_VAL (0xF << 8)
  37. #define AXICC_ARCP_VAL (0xF << 12)
  38. #define AXICC_AWCFD_VAL (0xF << 16)
  39. #define AXICC_AWCD_VAL (0xF << 20)
  40. #define AXICC_AWCF_VAL (0xF << 24)
  41. #define PCFG_TPSS_VAL (0x32 << 16)
  42. #define PCFG_TPRS_VAL (0x2 << 12)
  43. #define PCFG_PAD_VAL 0x2
  44. #define PPCFG_TTA 0x1FFFE
  45. #define PPCFG_PSSO_EN (1 << 28)
  46. #define PPCFG_PSS_EN (1 << 29)
  47. #define PPCFG_ESDF_EN (1 << 31)
  48. #define PP5C_RIT 0x60216
  49. #define PP5C_RCT (0x7f0 << 20)
  50. #define PTC_RX_WM_VAL 0x40
  51. #define PTC_RSVD (1 << 27)
  52. #define PORT0_BASE 0x100
  53. #define PORT1_BASE 0x180
  54. /* Port Control Register Bit Definitions */
  55. #define PORT_SCTL_SPD_GEN3 (0x3 << 4)
  56. #define PORT_SCTL_SPD_GEN2 (0x2 << 4)
  57. #define PORT_SCTL_SPD_GEN1 (0x1 << 4)
  58. #define PORT_SCTL_IPM (0x3 << 8)
  59. #define PORT_BASE 0x100
  60. #define PORT_OFFSET 0x80
  61. #define NR_PORTS 2
  62. #define DRV_NAME "ahci-ceva"
  63. #define CEVA_FLAG_BROKEN_GEN2 1
  64. static unsigned int rx_watermark = PTC_RX_WM_VAL;
  65. module_param(rx_watermark, uint, 0644);
  66. MODULE_PARM_DESC(rx_watermark, "RxWaterMark value (0 - 0x80)");
  67. struct ceva_ahci_priv {
  68. struct platform_device *ahci_pdev;
  69. /* Port Phy2Cfg Register */
  70. u32 pp2c[NR_PORTS];
  71. u32 pp3c[NR_PORTS];
  72. u32 pp4c[NR_PORTS];
  73. u32 pp5c[NR_PORTS];
  74. /* Axi Cache Control Register */
  75. u32 axicc;
  76. bool is_cci_enabled;
  77. int flags;
  78. struct reset_control *rst;
  79. };
  80. static unsigned int ceva_ahci_read_id(struct ata_device *dev,
  81. struct ata_taskfile *tf, __le16 *id)
  82. {
  83. u32 err_mask;
  84. err_mask = ata_do_dev_read_id(dev, tf, id);
  85. if (err_mask)
  86. return err_mask;
  87. /*
  88. * Since CEVA controller does not support device sleep feature, we
  89. * need to clear DEVSLP (bit 8) in word78 of the IDENTIFY DEVICE data.
  90. */
  91. id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
  92. return 0;
  93. }
  94. static struct ata_port_operations ahci_ceva_ops = {
  95. .inherits = &ahci_platform_ops,
  96. .read_id = ceva_ahci_read_id,
  97. };
  98. static const struct ata_port_info ahci_ceva_port_info = {
  99. .flags = AHCI_FLAG_COMMON,
  100. .pio_mask = ATA_PIO4,
  101. .udma_mask = ATA_UDMA6,
  102. .port_ops = &ahci_ceva_ops,
  103. };
  104. static void ahci_ceva_setup(struct ahci_host_priv *hpriv)
  105. {
  106. void __iomem *mmio = hpriv->mmio;
  107. struct ceva_ahci_priv *cevapriv = hpriv->plat_data;
  108. u32 tmp;
  109. int i;
  110. /* Set AHCI Enable */
  111. tmp = readl(mmio + HOST_CTL);
  112. tmp |= HOST_AHCI_EN;
  113. writel(tmp, mmio + HOST_CTL);
  114. for (i = 0; i < NR_PORTS; i++) {
  115. /* TPSS TPRS scalars, CISE and Port Addr */
  116. tmp = PCFG_TPSS_VAL | PCFG_TPRS_VAL | (PCFG_PAD_VAL + i);
  117. writel(tmp, mmio + AHCI_VEND_PCFG);
  118. /*
  119. * AXI Data bus width to 64
  120. * Set Mem Addr Read, Write ID for data transfers
  121. * Set Mem Addr Read ID, Write ID for non-data transfers
  122. * Transfer limit to 72 DWord
  123. */
  124. tmp = PAXIC_ADBW_BW64 | PAXIC_MAWIDD(i) | PAXIC_MARIDD(i) |
  125. PAXIC_MAWID(i) | PAXIC_MARID(i) | PAXIC_OTL;
  126. writel(tmp, mmio + AHCI_VEND_PAXIC);
  127. /* Set AXI cache control register if CCi is enabled */
  128. if (cevapriv->is_cci_enabled) {
  129. tmp = readl(mmio + AHCI_VEND_AXICC);
  130. tmp |= AXICC_ARCA_VAL | AXICC_ARCF_VAL |
  131. AXICC_ARCH_VAL | AXICC_ARCP_VAL |
  132. AXICC_AWCFD_VAL | AXICC_AWCD_VAL |
  133. AXICC_AWCF_VAL;
  134. writel(tmp, mmio + AHCI_VEND_AXICC);
  135. }
  136. /* Port Phy Cfg register enables */
  137. tmp = PPCFG_TTA | PPCFG_PSS_EN | PPCFG_ESDF_EN;
  138. writel(tmp, mmio + AHCI_VEND_PPCFG);
  139. /* Phy Control OOB timing parameters COMINIT */
  140. writel(cevapriv->pp2c[i], mmio + AHCI_VEND_PP2C);
  141. /* Phy Control OOB timing parameters COMWAKE */
  142. writel(cevapriv->pp3c[i], mmio + AHCI_VEND_PP3C);
  143. /* Phy Control Burst timing setting */
  144. writel(cevapriv->pp4c[i], mmio + AHCI_VEND_PP4C);
  145. /* Rate Change Timer and Retry Interval Timer setting */
  146. writel(cevapriv->pp5c[i], mmio + AHCI_VEND_PP5C);
  147. /* Rx Watermark setting */
  148. tmp = rx_watermark | PTC_RSVD;
  149. writel(tmp, mmio + AHCI_VEND_PTC);
  150. /* Default to Gen 3 Speed and Gen 1 if Gen2 is broken */
  151. tmp = PORT_SCTL_SPD_GEN3 | PORT_SCTL_IPM;
  152. if (cevapriv->flags & CEVA_FLAG_BROKEN_GEN2)
  153. tmp = PORT_SCTL_SPD_GEN1 | PORT_SCTL_IPM;
  154. writel(tmp, mmio + PORT_SCR_CTL + PORT_BASE + PORT_OFFSET * i);
  155. }
  156. }
  157. static struct scsi_host_template ahci_platform_sht = {
  158. AHCI_SHT(DRV_NAME),
  159. };
  160. static int ceva_ahci_probe(struct platform_device *pdev)
  161. {
  162. struct device_node *np = pdev->dev.of_node;
  163. struct device *dev = &pdev->dev;
  164. struct ahci_host_priv *hpriv;
  165. struct ceva_ahci_priv *cevapriv;
  166. enum dev_dma_attr attr;
  167. int rc;
  168. cevapriv = devm_kzalloc(dev, sizeof(*cevapriv), GFP_KERNEL);
  169. if (!cevapriv)
  170. return -ENOMEM;
  171. cevapriv->ahci_pdev = pdev;
  172. cevapriv->rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
  173. NULL);
  174. if (IS_ERR(cevapriv->rst))
  175. dev_err_probe(&pdev->dev, PTR_ERR(cevapriv->rst),
  176. "failed to get reset\n");
  177. hpriv = ahci_platform_get_resources(pdev, 0);
  178. if (IS_ERR(hpriv))
  179. return PTR_ERR(hpriv);
  180. if (!cevapriv->rst) {
  181. rc = ahci_platform_enable_resources(hpriv);
  182. if (rc)
  183. return rc;
  184. } else {
  185. int i;
  186. rc = ahci_platform_enable_clks(hpriv);
  187. if (rc)
  188. return rc;
  189. /* Assert the controller reset */
  190. reset_control_assert(cevapriv->rst);
  191. for (i = 0; i < hpriv->nports; i++) {
  192. rc = phy_init(hpriv->phys[i]);
  193. if (rc)
  194. return rc;
  195. }
  196. /* De-assert the controller reset */
  197. reset_control_deassert(cevapriv->rst);
  198. for (i = 0; i < hpriv->nports; i++) {
  199. rc = phy_power_on(hpriv->phys[i]);
  200. if (rc) {
  201. phy_exit(hpriv->phys[i]);
  202. return rc;
  203. }
  204. }
  205. }
  206. if (of_property_read_bool(np, "ceva,broken-gen2"))
  207. cevapriv->flags = CEVA_FLAG_BROKEN_GEN2;
  208. /* Read OOB timing value for COMINIT from device-tree */
  209. if (of_property_read_u8_array(np, "ceva,p0-cominit-params",
  210. (u8 *)&cevapriv->pp2c[0], 4) < 0) {
  211. dev_warn(dev, "ceva,p0-cominit-params property not defined\n");
  212. return -EINVAL;
  213. }
  214. if (of_property_read_u8_array(np, "ceva,p1-cominit-params",
  215. (u8 *)&cevapriv->pp2c[1], 4) < 0) {
  216. dev_warn(dev, "ceva,p1-cominit-params property not defined\n");
  217. return -EINVAL;
  218. }
  219. /* Read OOB timing value for COMWAKE from device-tree*/
  220. if (of_property_read_u8_array(np, "ceva,p0-comwake-params",
  221. (u8 *)&cevapriv->pp3c[0], 4) < 0) {
  222. dev_warn(dev, "ceva,p0-comwake-params property not defined\n");
  223. return -EINVAL;
  224. }
  225. if (of_property_read_u8_array(np, "ceva,p1-comwake-params",
  226. (u8 *)&cevapriv->pp3c[1], 4) < 0) {
  227. dev_warn(dev, "ceva,p1-comwake-params property not defined\n");
  228. return -EINVAL;
  229. }
  230. /* Read phy BURST timing value from device-tree */
  231. if (of_property_read_u8_array(np, "ceva,p0-burst-params",
  232. (u8 *)&cevapriv->pp4c[0], 4) < 0) {
  233. dev_warn(dev, "ceva,p0-burst-params property not defined\n");
  234. return -EINVAL;
  235. }
  236. if (of_property_read_u8_array(np, "ceva,p1-burst-params",
  237. (u8 *)&cevapriv->pp4c[1], 4) < 0) {
  238. dev_warn(dev, "ceva,p1-burst-params property not defined\n");
  239. return -EINVAL;
  240. }
  241. /* Read phy RETRY interval timing value from device-tree */
  242. if (of_property_read_u16_array(np, "ceva,p0-retry-params",
  243. (u16 *)&cevapriv->pp5c[0], 2) < 0) {
  244. dev_warn(dev, "ceva,p0-retry-params property not defined\n");
  245. return -EINVAL;
  246. }
  247. if (of_property_read_u16_array(np, "ceva,p1-retry-params",
  248. (u16 *)&cevapriv->pp5c[1], 2) < 0) {
  249. dev_warn(dev, "ceva,p1-retry-params property not defined\n");
  250. return -EINVAL;
  251. }
  252. /*
  253. * Check if CCI is enabled for SATA. The DEV_DMA_COHERENT is returned
  254. * if CCI is enabled, so check for DEV_DMA_COHERENT.
  255. */
  256. attr = device_get_dma_attr(dev);
  257. cevapriv->is_cci_enabled = (attr == DEV_DMA_COHERENT);
  258. hpriv->plat_data = cevapriv;
  259. /* CEVA specific initialization */
  260. ahci_ceva_setup(hpriv);
  261. rc = ahci_platform_init_host(pdev, hpriv, &ahci_ceva_port_info,
  262. &ahci_platform_sht);
  263. if (rc)
  264. goto disable_resources;
  265. return 0;
  266. disable_resources:
  267. ahci_platform_disable_resources(hpriv);
  268. return rc;
  269. }
  270. static int __maybe_unused ceva_ahci_suspend(struct device *dev)
  271. {
  272. return ahci_platform_suspend(dev);
  273. }
  274. static int __maybe_unused ceva_ahci_resume(struct device *dev)
  275. {
  276. struct ata_host *host = dev_get_drvdata(dev);
  277. struct ahci_host_priv *hpriv = host->private_data;
  278. int rc;
  279. rc = ahci_platform_enable_resources(hpriv);
  280. if (rc)
  281. return rc;
  282. /* Configure CEVA specific config before resuming HBA */
  283. ahci_ceva_setup(hpriv);
  284. rc = ahci_platform_resume_host(dev);
  285. if (rc)
  286. goto disable_resources;
  287. /* We resumed so update PM runtime state */
  288. pm_runtime_disable(dev);
  289. pm_runtime_set_active(dev);
  290. pm_runtime_enable(dev);
  291. return 0;
  292. disable_resources:
  293. ahci_platform_disable_resources(hpriv);
  294. return rc;
  295. }
  296. static SIMPLE_DEV_PM_OPS(ahci_ceva_pm_ops, ceva_ahci_suspend, ceva_ahci_resume);
  297. static const struct of_device_id ceva_ahci_of_match[] = {
  298. { .compatible = "ceva,ahci-1v84" },
  299. { /* sentinel */ }
  300. };
  301. MODULE_DEVICE_TABLE(of, ceva_ahci_of_match);
  302. static struct platform_driver ceva_ahci_driver = {
  303. .probe = ceva_ahci_probe,
  304. .remove = ata_platform_remove_one,
  305. .driver = {
  306. .name = DRV_NAME,
  307. .of_match_table = ceva_ahci_of_match,
  308. .pm = &ahci_ceva_pm_ops,
  309. },
  310. };
  311. module_platform_driver(ceva_ahci_driver);
  312. MODULE_DESCRIPTION("CEVA AHCI SATA platform driver");
  313. MODULE_AUTHOR("Xilinx Inc.");
  314. MODULE_LICENSE("GPL v2");