ahci_qoriq.c 9.3 KB

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