ahci_imx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * copyright (c) 2013 Freescale Semiconductor, Inc.
  3. * Freescale IMX 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/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/ahci_platform.h>
  24. #include <linux/of_device.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  27. #include <linux/libata.h>
  28. #include "ahci.h"
  29. enum {
  30. PORT_PHY_CTL = 0x178, /* Port0 PHY Control */
  31. PORT_PHY_CTL_PDDQ_LOC = 0x100000, /* PORT_PHY_CTL bits */
  32. HOST_TIMER1MS = 0xe0, /* Timer 1-ms */
  33. };
  34. enum ahci_imx_type {
  35. AHCI_IMX53,
  36. AHCI_IMX6Q,
  37. };
  38. struct imx_ahci_priv {
  39. struct platform_device *ahci_pdev;
  40. enum ahci_imx_type type;
  41. /* i.MX53 clock */
  42. struct clk *sata_gate_clk;
  43. /* Common clock */
  44. struct clk *sata_ref_clk;
  45. struct clk *ahb_clk;
  46. struct regmap *gpr;
  47. bool no_device;
  48. bool first_time;
  49. };
  50. static int ahci_imx_hotplug;
  51. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  52. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  53. static int imx_sata_clock_enable(struct device *dev)
  54. {
  55. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  56. int ret;
  57. if (imxpriv->type == AHCI_IMX53) {
  58. ret = clk_prepare_enable(imxpriv->sata_gate_clk);
  59. if (ret < 0) {
  60. dev_err(dev, "prepare-enable sata_gate clock err:%d\n",
  61. ret);
  62. return ret;
  63. }
  64. }
  65. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  66. if (ret < 0) {
  67. dev_err(dev, "prepare-enable sata_ref clock err:%d\n",
  68. ret);
  69. goto clk_err;
  70. }
  71. if (imxpriv->type == AHCI_IMX6Q) {
  72. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  73. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  74. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  75. }
  76. usleep_range(1000, 2000);
  77. return 0;
  78. clk_err:
  79. if (imxpriv->type == AHCI_IMX53)
  80. clk_disable_unprepare(imxpriv->sata_gate_clk);
  81. return ret;
  82. }
  83. static void imx_sata_clock_disable(struct device *dev)
  84. {
  85. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  86. if (imxpriv->type == AHCI_IMX6Q) {
  87. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  88. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  89. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  90. }
  91. clk_disable_unprepare(imxpriv->sata_ref_clk);
  92. if (imxpriv->type == AHCI_IMX53)
  93. clk_disable_unprepare(imxpriv->sata_gate_clk);
  94. }
  95. static void ahci_imx_error_handler(struct ata_port *ap)
  96. {
  97. u32 reg_val;
  98. struct ata_device *dev;
  99. struct ata_host *host = dev_get_drvdata(ap->dev);
  100. struct ahci_host_priv *hpriv = host->private_data;
  101. void __iomem *mmio = hpriv->mmio;
  102. struct imx_ahci_priv *imxpriv = dev_get_drvdata(ap->dev->parent);
  103. ahci_error_handler(ap);
  104. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  105. return;
  106. imxpriv->first_time = false;
  107. ata_for_each_dev(dev, &ap->link, ENABLED)
  108. return;
  109. /*
  110. * Disable link to save power. An imx ahci port can't be recovered
  111. * without full reset once the pddq mode is enabled making it
  112. * impossible to use as part of libata LPM.
  113. */
  114. reg_val = readl(mmio + PORT_PHY_CTL);
  115. writel(reg_val | PORT_PHY_CTL_PDDQ_LOC, mmio + PORT_PHY_CTL);
  116. imx_sata_clock_disable(ap->dev);
  117. imxpriv->no_device = true;
  118. }
  119. static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
  120. unsigned long deadline)
  121. {
  122. struct ata_port *ap = link->ap;
  123. struct imx_ahci_priv *imxpriv = dev_get_drvdata(ap->dev->parent);
  124. int ret = -EIO;
  125. if (imxpriv->type == AHCI_IMX53)
  126. ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
  127. else if (imxpriv->type == AHCI_IMX6Q)
  128. ret = ahci_ops.softreset(link, class, deadline);
  129. return ret;
  130. }
  131. static struct ata_port_operations ahci_imx_ops = {
  132. .inherits = &ahci_platform_ops,
  133. .error_handler = ahci_imx_error_handler,
  134. .softreset = ahci_imx_softreset,
  135. };
  136. static const struct ata_port_info ahci_imx_port_info = {
  137. .flags = AHCI_FLAG_COMMON,
  138. .pio_mask = ATA_PIO4,
  139. .udma_mask = ATA_UDMA6,
  140. .port_ops = &ahci_imx_ops,
  141. };
  142. static int imx_sata_init(struct device *dev, void __iomem *mmio)
  143. {
  144. int ret = 0;
  145. unsigned int reg_val;
  146. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  147. ret = imx_sata_clock_enable(dev);
  148. if (ret < 0)
  149. return ret;
  150. /*
  151. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  152. * and IP vendor specific register HOST_TIMER1MS.
  153. * Configure CAP_SSS (support stagered spin up).
  154. * Implement the port0.
  155. * Get the ahb clock rate, and configure the TIMER1MS register.
  156. */
  157. reg_val = readl(mmio + HOST_CAP);
  158. if (!(reg_val & HOST_CAP_SSS)) {
  159. reg_val |= HOST_CAP_SSS;
  160. writel(reg_val, mmio + HOST_CAP);
  161. }
  162. reg_val = readl(mmio + HOST_PORTS_IMPL);
  163. if (!(reg_val & 0x1)) {
  164. reg_val |= 0x1;
  165. writel(reg_val, mmio + HOST_PORTS_IMPL);
  166. }
  167. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  168. writel(reg_val, mmio + HOST_TIMER1MS);
  169. return 0;
  170. }
  171. static void imx_sata_exit(struct device *dev)
  172. {
  173. imx_sata_clock_disable(dev);
  174. }
  175. static int imx_ahci_suspend(struct device *dev)
  176. {
  177. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  178. /*
  179. * If no_device is set, The CLKs had been gated off in the
  180. * initialization so don't do it again here.
  181. */
  182. if (!imxpriv->no_device)
  183. imx_sata_clock_disable(dev);
  184. return 0;
  185. }
  186. static int imx_ahci_resume(struct device *dev)
  187. {
  188. struct imx_ahci_priv *imxpriv = dev_get_drvdata(dev->parent);
  189. int ret = 0;
  190. if (!imxpriv->no_device)
  191. ret = imx_sata_clock_enable(dev);
  192. return ret;
  193. }
  194. static struct ahci_platform_data imx_sata_pdata = {
  195. .init = imx_sata_init,
  196. .exit = imx_sata_exit,
  197. .ata_port_info = &ahci_imx_port_info,
  198. .suspend = imx_ahci_suspend,
  199. .resume = imx_ahci_resume,
  200. };
  201. static const struct of_device_id imx_ahci_of_match[] = {
  202. { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
  203. { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
  204. {},
  205. };
  206. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  207. static int imx_ahci_probe(struct platform_device *pdev)
  208. {
  209. struct device *dev = &pdev->dev;
  210. struct resource *mem, *irq, res[2];
  211. const struct of_device_id *of_id;
  212. enum ahci_imx_type type;
  213. const struct ahci_platform_data *pdata = NULL;
  214. struct imx_ahci_priv *imxpriv;
  215. struct device *ahci_dev;
  216. struct platform_device *ahci_pdev;
  217. int ret;
  218. of_id = of_match_device(imx_ahci_of_match, dev);
  219. if (!of_id)
  220. return -EINVAL;
  221. type = (enum ahci_imx_type)of_id->data;
  222. pdata = &imx_sata_pdata;
  223. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  224. if (!imxpriv) {
  225. dev_err(dev, "can't alloc ahci_host_priv\n");
  226. return -ENOMEM;
  227. }
  228. ahci_pdev = platform_device_alloc("ahci", -1);
  229. if (!ahci_pdev)
  230. return -ENODEV;
  231. ahci_dev = &ahci_pdev->dev;
  232. ahci_dev->parent = dev;
  233. imxpriv->no_device = false;
  234. imxpriv->first_time = true;
  235. imxpriv->type = type;
  236. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  237. if (IS_ERR(imxpriv->ahb_clk)) {
  238. dev_err(dev, "can't get ahb clock.\n");
  239. ret = PTR_ERR(imxpriv->ahb_clk);
  240. goto err_out;
  241. }
  242. if (type == AHCI_IMX53) {
  243. imxpriv->sata_gate_clk = devm_clk_get(dev, "sata_gate");
  244. if (IS_ERR(imxpriv->sata_gate_clk)) {
  245. dev_err(dev, "can't get sata_gate clock.\n");
  246. ret = PTR_ERR(imxpriv->sata_gate_clk);
  247. goto err_out;
  248. }
  249. }
  250. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  251. if (IS_ERR(imxpriv->sata_ref_clk)) {
  252. dev_err(dev, "can't get sata_ref clock.\n");
  253. ret = PTR_ERR(imxpriv->sata_ref_clk);
  254. goto err_out;
  255. }
  256. imxpriv->ahci_pdev = ahci_pdev;
  257. platform_set_drvdata(pdev, imxpriv);
  258. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  259. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  260. if (!mem || !irq) {
  261. dev_err(dev, "no mmio/irq resource\n");
  262. ret = -ENOMEM;
  263. goto err_out;
  264. }
  265. res[0] = *mem;
  266. res[1] = *irq;
  267. ahci_dev->coherent_dma_mask = DMA_BIT_MASK(32);
  268. ahci_dev->dma_mask = &ahci_dev->coherent_dma_mask;
  269. ahci_dev->of_node = dev->of_node;
  270. if (type == AHCI_IMX6Q) {
  271. imxpriv->gpr = syscon_regmap_lookup_by_compatible(
  272. "fsl,imx6q-iomuxc-gpr");
  273. if (IS_ERR(imxpriv->gpr)) {
  274. dev_err(dev,
  275. "failed to find fsl,imx6q-iomux-gpr regmap\n");
  276. ret = PTR_ERR(imxpriv->gpr);
  277. goto err_out;
  278. }
  279. /*
  280. * Set PHY Paremeters, two steps to configure the GPR13,
  281. * one write for rest of parameters, mask of first write
  282. * is 0x07fffffe, and the other one write for setting
  283. * the mpll_clk_en happens in imx_sata_clock_enable().
  284. */
  285. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  286. IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
  287. IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
  288. IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
  289. IMX6Q_GPR13_SATA_SPD_MODE_MASK |
  290. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  291. IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
  292. IMX6Q_GPR13_SATA_TX_BOOST_MASK |
  293. IMX6Q_GPR13_SATA_TX_LVL_MASK |
  294. IMX6Q_GPR13_SATA_MPLL_CLK_EN |
  295. IMX6Q_GPR13_SATA_TX_EDGE_RATE,
  296. IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB |
  297. IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
  298. IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
  299. IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
  300. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  301. IMX6Q_GPR13_SATA_TX_ATTEN_9_16 |
  302. IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB |
  303. IMX6Q_GPR13_SATA_TX_LVL_1_025_V);
  304. }
  305. ret = platform_device_add_resources(ahci_pdev, res, 2);
  306. if (ret)
  307. goto err_out;
  308. ret = platform_device_add_data(ahci_pdev, pdata, sizeof(*pdata));
  309. if (ret)
  310. goto err_out;
  311. ret = platform_device_add(ahci_pdev);
  312. if (ret) {
  313. err_out:
  314. platform_device_put(ahci_pdev);
  315. return ret;
  316. }
  317. return 0;
  318. }
  319. static int imx_ahci_remove(struct platform_device *pdev)
  320. {
  321. struct imx_ahci_priv *imxpriv = platform_get_drvdata(pdev);
  322. struct platform_device *ahci_pdev = imxpriv->ahci_pdev;
  323. platform_device_unregister(ahci_pdev);
  324. return 0;
  325. }
  326. static struct platform_driver imx_ahci_driver = {
  327. .probe = imx_ahci_probe,
  328. .remove = imx_ahci_remove,
  329. .driver = {
  330. .name = "ahci-imx",
  331. .owner = THIS_MODULE,
  332. .of_match_table = imx_ahci_of_match,
  333. },
  334. };
  335. module_platform_driver(imx_ahci_driver);
  336. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  337. MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
  338. MODULE_LICENSE("GPL");
  339. MODULE_ALIAS("ahci:imx");