ahci_qoriq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Freescale QorIQ AHCI SATA platform driver
  3. *
  4. * Copyright 2015 Freescale, Inc.
  5. * Tang Yuantian <Yuantian.Tang@freescale.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/pm.h>
  15. #include <linux/ahci_platform.h>
  16. #include <linux/device.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/libata.h>
  22. #include "ahci.h"
  23. #define DRV_NAME "ahci-qoriq"
  24. /* port register definition */
  25. #define PORT_PHY1 0xA8
  26. #define PORT_PHY2 0xAC
  27. #define PORT_PHY3 0xB0
  28. #define PORT_PHY4 0xB4
  29. #define PORT_PHY5 0xB8
  30. #define PORT_AXICC 0xBC
  31. #define PORT_TRANS 0xC8
  32. /* port register default value */
  33. #define AHCI_PORT_PHY_1_CFG 0xa003fffe
  34. #define AHCI_PORT_TRANS_CFG 0x08000029
  35. #define AHCI_PORT_AXICC_CFG 0x3fffffff
  36. /* for ls1021a */
  37. #define LS1021A_PORT_PHY2 0x28183414
  38. #define LS1021A_PORT_PHY3 0x0e080e06
  39. #define LS1021A_PORT_PHY4 0x064a080b
  40. #define LS1021A_PORT_PHY5 0x2aa86470
  41. #define LS1021A_AXICC_ADDR 0xC0
  42. #define SATA_ECC_DISABLE 0x00020000
  43. #define ECC_DIS_ARMV8_CH2 0x80000000
  44. #define ECC_DIS_LS1088A 0x40000000
  45. enum ahci_qoriq_type {
  46. AHCI_LS1021A,
  47. AHCI_LS1043A,
  48. AHCI_LS2080A,
  49. AHCI_LS1046A,
  50. AHCI_LS1088A,
  51. AHCI_LS2088A,
  52. };
  53. struct ahci_qoriq_priv {
  54. struct ccsr_ahci *reg_base;
  55. enum ahci_qoriq_type type;
  56. void __iomem *ecc_addr;
  57. bool is_dmacoherent;
  58. };
  59. static const struct of_device_id ahci_qoriq_of_match[] = {
  60. { .compatible = "fsl,ls1021a-ahci", .data = (void *)AHCI_LS1021A},
  61. { .compatible = "fsl,ls1043a-ahci", .data = (void *)AHCI_LS1043A},
  62. { .compatible = "fsl,ls2080a-ahci", .data = (void *)AHCI_LS2080A},
  63. { .compatible = "fsl,ls1046a-ahci", .data = (void *)AHCI_LS1046A},
  64. { .compatible = "fsl,ls1088a-ahci", .data = (void *)AHCI_LS1088A},
  65. { .compatible = "fsl,ls2088a-ahci", .data = (void *)AHCI_LS2088A},
  66. {},
  67. };
  68. MODULE_DEVICE_TABLE(of, ahci_qoriq_of_match);
  69. static int ahci_qoriq_hardreset(struct ata_link *link, unsigned int *class,
  70. unsigned long deadline)
  71. {
  72. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  73. void __iomem *port_mmio = ahci_port_base(link->ap);
  74. u32 px_cmd, px_is, px_val;
  75. struct ata_port *ap = link->ap;
  76. struct ahci_port_priv *pp = ap->private_data;
  77. struct ahci_host_priv *hpriv = ap->host->private_data;
  78. struct ahci_qoriq_priv *qoriq_priv = hpriv->plat_data;
  79. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  80. struct ata_taskfile tf;
  81. bool online;
  82. int rc;
  83. bool ls1021a_workaround = (qoriq_priv->type == AHCI_LS1021A);
  84. DPRINTK("ENTER\n");
  85. hpriv->stop_engine(ap);
  86. /*
  87. * There is a errata on ls1021a Rev1.0 and Rev2.0 which is:
  88. * A-009042: The device detection initialization sequence
  89. * mistakenly resets some registers.
  90. *
  91. * Workaround for this is:
  92. * The software should read and store PxCMD and PxIS values
  93. * before issuing the device detection initialization sequence.
  94. * After the sequence is complete, software should restore the
  95. * PxCMD and PxIS with the stored values.
  96. */
  97. if (ls1021a_workaround) {
  98. px_cmd = readl(port_mmio + PORT_CMD);
  99. px_is = readl(port_mmio + PORT_IRQ_STAT);
  100. }
  101. /* clear D2H reception area to properly wait for D2H FIS */
  102. ata_tf_init(link->device, &tf);
  103. tf.command = ATA_BUSY;
  104. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  105. rc = sata_link_hardreset(link, timing, deadline, &online,
  106. ahci_check_ready);
  107. /* restore the PxCMD and PxIS on ls1021 */
  108. if (ls1021a_workaround) {
  109. px_val = readl(port_mmio + PORT_CMD);
  110. if (px_val != px_cmd)
  111. writel(px_cmd, port_mmio + PORT_CMD);
  112. px_val = readl(port_mmio + PORT_IRQ_STAT);
  113. if (px_val != px_is)
  114. writel(px_is, port_mmio + PORT_IRQ_STAT);
  115. }
  116. hpriv->start_engine(ap);
  117. if (online)
  118. *class = ahci_dev_classify(ap);
  119. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  120. return rc;
  121. }
  122. static struct ata_port_operations ahci_qoriq_ops = {
  123. .inherits = &ahci_ops,
  124. .hardreset = ahci_qoriq_hardreset,
  125. };
  126. static const struct ata_port_info ahci_qoriq_port_info = {
  127. .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
  128. .pio_mask = ATA_PIO4,
  129. .udma_mask = ATA_UDMA6,
  130. .port_ops = &ahci_qoriq_ops,
  131. };
  132. static struct scsi_host_template ahci_qoriq_sht = {
  133. AHCI_SHT(DRV_NAME),
  134. };
  135. static int ahci_qoriq_phy_init(struct ahci_host_priv *hpriv)
  136. {
  137. struct ahci_qoriq_priv *qpriv = hpriv->plat_data;
  138. void __iomem *reg_base = hpriv->mmio;
  139. switch (qpriv->type) {
  140. case AHCI_LS1021A:
  141. if (!qpriv->ecc_addr)
  142. return -EINVAL;
  143. writel(SATA_ECC_DISABLE, qpriv->ecc_addr);
  144. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  145. writel(LS1021A_PORT_PHY2, reg_base + PORT_PHY2);
  146. writel(LS1021A_PORT_PHY3, reg_base + PORT_PHY3);
  147. writel(LS1021A_PORT_PHY4, reg_base + PORT_PHY4);
  148. writel(LS1021A_PORT_PHY5, reg_base + PORT_PHY5);
  149. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  150. if (qpriv->is_dmacoherent)
  151. writel(AHCI_PORT_AXICC_CFG,
  152. reg_base + LS1021A_AXICC_ADDR);
  153. break;
  154. case AHCI_LS1043A:
  155. if (!qpriv->ecc_addr)
  156. return -EINVAL;
  157. writel(readl(qpriv->ecc_addr) | ECC_DIS_ARMV8_CH2,
  158. qpriv->ecc_addr);
  159. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  160. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  161. if (qpriv->is_dmacoherent)
  162. writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
  163. break;
  164. case AHCI_LS2080A:
  165. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  166. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  167. if (qpriv->is_dmacoherent)
  168. writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
  169. break;
  170. case AHCI_LS1046A:
  171. if (!qpriv->ecc_addr)
  172. return -EINVAL;
  173. writel(readl(qpriv->ecc_addr) | ECC_DIS_ARMV8_CH2,
  174. qpriv->ecc_addr);
  175. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  176. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  177. if (qpriv->is_dmacoherent)
  178. writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
  179. break;
  180. case AHCI_LS1088A:
  181. if (!qpriv->ecc_addr)
  182. return -EINVAL;
  183. writel(readl(qpriv->ecc_addr) | ECC_DIS_LS1088A,
  184. qpriv->ecc_addr);
  185. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  186. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  187. if (qpriv->is_dmacoherent)
  188. writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
  189. break;
  190. case AHCI_LS2088A:
  191. writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
  192. writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
  193. if (qpriv->is_dmacoherent)
  194. writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
  195. break;
  196. }
  197. return 0;
  198. }
  199. static int ahci_qoriq_probe(struct platform_device *pdev)
  200. {
  201. struct device_node *np = pdev->dev.of_node;
  202. struct device *dev = &pdev->dev;
  203. struct ahci_host_priv *hpriv;
  204. struct ahci_qoriq_priv *qoriq_priv;
  205. const struct of_device_id *of_id;
  206. struct resource *res;
  207. int rc;
  208. hpriv = ahci_platform_get_resources(pdev);
  209. if (IS_ERR(hpriv))
  210. return PTR_ERR(hpriv);
  211. of_id = of_match_node(ahci_qoriq_of_match, np);
  212. if (!of_id)
  213. return -ENODEV;
  214. qoriq_priv = devm_kzalloc(dev, sizeof(*qoriq_priv), GFP_KERNEL);
  215. if (!qoriq_priv)
  216. return -ENOMEM;
  217. qoriq_priv->type = (enum ahci_qoriq_type)of_id->data;
  218. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  219. "sata-ecc");
  220. if (res) {
  221. qoriq_priv->ecc_addr = devm_ioremap_resource(dev, res);
  222. if (IS_ERR(qoriq_priv->ecc_addr))
  223. return PTR_ERR(qoriq_priv->ecc_addr);
  224. }
  225. qoriq_priv->is_dmacoherent = of_dma_is_coherent(np);
  226. rc = ahci_platform_enable_resources(hpriv);
  227. if (rc)
  228. return rc;
  229. hpriv->plat_data = qoriq_priv;
  230. rc = ahci_qoriq_phy_init(hpriv);
  231. if (rc)
  232. goto disable_resources;
  233. rc = ahci_platform_init_host(pdev, hpriv, &ahci_qoriq_port_info,
  234. &ahci_qoriq_sht);
  235. if (rc)
  236. goto disable_resources;
  237. return 0;
  238. disable_resources:
  239. ahci_platform_disable_resources(hpriv);
  240. return rc;
  241. }
  242. #ifdef CONFIG_PM_SLEEP
  243. static int ahci_qoriq_resume(struct device *dev)
  244. {
  245. struct ata_host *host = dev_get_drvdata(dev);
  246. struct ahci_host_priv *hpriv = host->private_data;
  247. int rc;
  248. rc = ahci_platform_enable_resources(hpriv);
  249. if (rc)
  250. return rc;
  251. rc = ahci_qoriq_phy_init(hpriv);
  252. if (rc)
  253. goto disable_resources;
  254. rc = ahci_platform_resume_host(dev);
  255. if (rc)
  256. goto disable_resources;
  257. /* We resumed so update PM runtime state */
  258. pm_runtime_disable(dev);
  259. pm_runtime_set_active(dev);
  260. pm_runtime_enable(dev);
  261. return 0;
  262. disable_resources:
  263. ahci_platform_disable_resources(hpriv);
  264. return rc;
  265. }
  266. #endif
  267. static SIMPLE_DEV_PM_OPS(ahci_qoriq_pm_ops, ahci_platform_suspend,
  268. ahci_qoriq_resume);
  269. static struct platform_driver ahci_qoriq_driver = {
  270. .probe = ahci_qoriq_probe,
  271. .remove = ata_platform_remove_one,
  272. .driver = {
  273. .name = DRV_NAME,
  274. .of_match_table = ahci_qoriq_of_match,
  275. .pm = &ahci_qoriq_pm_ops,
  276. },
  277. };
  278. module_platform_driver(ahci_qoriq_driver);
  279. MODULE_DESCRIPTION("Freescale QorIQ AHCI SATA platform driver");
  280. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  281. MODULE_LICENSE("GPL");