ahci_ceva.c 9.7 KB

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