ahci_imx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. /* Timer 1-ms Register */
  31. IMX_TIMER1MS = 0x00e0,
  32. /* Port0 PHY Control Register */
  33. IMX_P0PHYCR = 0x0178,
  34. IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
  35. IMX_P0PHYCR_CR_READ = 1 << 19,
  36. IMX_P0PHYCR_CR_WRITE = 1 << 18,
  37. IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
  38. IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
  39. /* Port0 PHY Status Register */
  40. IMX_P0PHYSR = 0x017c,
  41. IMX_P0PHYSR_CR_ACK = 1 << 18,
  42. IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
  43. /* Lane0 Output Status Register */
  44. IMX_LANE0_OUT_STAT = 0x2003,
  45. IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
  46. /* Clock Reset Register */
  47. IMX_CLOCK_RESET = 0x7f3f,
  48. IMX_CLOCK_RESET_RESET = 1 << 0,
  49. };
  50. enum ahci_imx_type {
  51. AHCI_IMX53,
  52. AHCI_IMX6Q,
  53. };
  54. struct imx_ahci_priv {
  55. struct platform_device *ahci_pdev;
  56. enum ahci_imx_type type;
  57. struct clk *sata_clk;
  58. struct clk *sata_ref_clk;
  59. struct clk *ahb_clk;
  60. struct regmap *gpr;
  61. bool no_device;
  62. bool first_time;
  63. };
  64. static int ahci_imx_hotplug;
  65. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  66. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  67. static void ahci_imx_host_stop(struct ata_host *host);
  68. static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
  69. {
  70. int timeout = 10;
  71. u32 crval;
  72. u32 srval;
  73. /* Assert or deassert the bit */
  74. crval = readl(mmio + IMX_P0PHYCR);
  75. if (assert)
  76. crval |= bit;
  77. else
  78. crval &= ~bit;
  79. writel(crval, mmio + IMX_P0PHYCR);
  80. /* Wait for the cr_ack signal */
  81. do {
  82. srval = readl(mmio + IMX_P0PHYSR);
  83. if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
  84. break;
  85. usleep_range(100, 200);
  86. } while (--timeout);
  87. return timeout ? 0 : -ETIMEDOUT;
  88. }
  89. static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
  90. {
  91. u32 crval = addr;
  92. int ret;
  93. /* Supply the address on cr_data_in */
  94. writel(crval, mmio + IMX_P0PHYCR);
  95. /* Assert the cr_cap_addr signal */
  96. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
  97. if (ret)
  98. return ret;
  99. /* Deassert cr_cap_addr */
  100. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
  101. if (ret)
  102. return ret;
  103. return 0;
  104. }
  105. static int imx_phy_reg_write(u16 val, void __iomem *mmio)
  106. {
  107. u32 crval = val;
  108. int ret;
  109. /* Supply the data on cr_data_in */
  110. writel(crval, mmio + IMX_P0PHYCR);
  111. /* Assert the cr_cap_data signal */
  112. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
  113. if (ret)
  114. return ret;
  115. /* Deassert cr_cap_data */
  116. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
  117. if (ret)
  118. return ret;
  119. if (val & IMX_CLOCK_RESET_RESET) {
  120. /*
  121. * In case we're resetting the phy, it's unable to acknowledge,
  122. * so we return immediately here.
  123. */
  124. crval |= IMX_P0PHYCR_CR_WRITE;
  125. writel(crval, mmio + IMX_P0PHYCR);
  126. goto out;
  127. }
  128. /* Assert the cr_write signal */
  129. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
  130. if (ret)
  131. return ret;
  132. /* Deassert cr_write */
  133. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
  134. if (ret)
  135. return ret;
  136. out:
  137. return 0;
  138. }
  139. static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
  140. {
  141. int ret;
  142. /* Assert the cr_read signal */
  143. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
  144. if (ret)
  145. return ret;
  146. /* Capture the data from cr_data_out[] */
  147. *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
  148. /* Deassert cr_read */
  149. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
  150. if (ret)
  151. return ret;
  152. return 0;
  153. }
  154. static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
  155. {
  156. void __iomem *mmio = hpriv->mmio;
  157. int timeout = 10;
  158. u16 val;
  159. int ret;
  160. /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
  161. ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
  162. if (ret)
  163. return ret;
  164. ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
  165. if (ret)
  166. return ret;
  167. /* Wait for PHY RX_PLL to be stable */
  168. do {
  169. usleep_range(100, 200);
  170. ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
  171. if (ret)
  172. return ret;
  173. ret = imx_phy_reg_read(&val, mmio);
  174. if (ret)
  175. return ret;
  176. if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
  177. break;
  178. } while (--timeout);
  179. return timeout ? 0 : -ETIMEDOUT;
  180. }
  181. static int imx_sata_enable(struct ahci_host_priv *hpriv)
  182. {
  183. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  184. struct device *dev = &imxpriv->ahci_pdev->dev;
  185. int ret;
  186. if (imxpriv->no_device)
  187. return 0;
  188. if (hpriv->target_pwr) {
  189. ret = regulator_enable(hpriv->target_pwr);
  190. if (ret)
  191. return ret;
  192. }
  193. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  194. if (ret < 0)
  195. goto disable_regulator;
  196. if (imxpriv->type == AHCI_IMX6Q) {
  197. /*
  198. * set PHY Paremeters, two steps to configure the GPR13,
  199. * one write for rest of parameters, mask of first write
  200. * is 0x07ffffff, and the other one write for setting
  201. * the mpll_clk_en.
  202. */
  203. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  204. IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
  205. IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
  206. IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
  207. IMX6Q_GPR13_SATA_SPD_MODE_MASK |
  208. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  209. IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
  210. IMX6Q_GPR13_SATA_TX_BOOST_MASK |
  211. IMX6Q_GPR13_SATA_TX_LVL_MASK |
  212. IMX6Q_GPR13_SATA_MPLL_CLK_EN |
  213. IMX6Q_GPR13_SATA_TX_EDGE_RATE,
  214. IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB |
  215. IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
  216. IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
  217. IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
  218. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  219. IMX6Q_GPR13_SATA_TX_ATTEN_9_16 |
  220. IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB |
  221. IMX6Q_GPR13_SATA_TX_LVL_1_025_V);
  222. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  223. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  224. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  225. usleep_range(100, 200);
  226. ret = imx_sata_phy_reset(hpriv);
  227. if (ret) {
  228. dev_err(dev, "failed to reset phy: %d\n", ret);
  229. goto disable_regulator;
  230. }
  231. }
  232. usleep_range(1000, 2000);
  233. return 0;
  234. disable_regulator:
  235. if (hpriv->target_pwr)
  236. regulator_disable(hpriv->target_pwr);
  237. return ret;
  238. }
  239. static void imx_sata_disable(struct ahci_host_priv *hpriv)
  240. {
  241. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  242. if (imxpriv->no_device)
  243. return;
  244. if (imxpriv->type == AHCI_IMX6Q) {
  245. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  246. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  247. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  248. }
  249. clk_disable_unprepare(imxpriv->sata_ref_clk);
  250. if (hpriv->target_pwr)
  251. regulator_disable(hpriv->target_pwr);
  252. }
  253. static void ahci_imx_error_handler(struct ata_port *ap)
  254. {
  255. u32 reg_val;
  256. struct ata_device *dev;
  257. struct ata_host *host = dev_get_drvdata(ap->dev);
  258. struct ahci_host_priv *hpriv = host->private_data;
  259. void __iomem *mmio = hpriv->mmio;
  260. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  261. ahci_error_handler(ap);
  262. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  263. return;
  264. imxpriv->first_time = false;
  265. ata_for_each_dev(dev, &ap->link, ENABLED)
  266. return;
  267. /*
  268. * Disable link to save power. An imx ahci port can't be recovered
  269. * without full reset once the pddq mode is enabled making it
  270. * impossible to use as part of libata LPM.
  271. */
  272. reg_val = readl(mmio + IMX_P0PHYCR);
  273. writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
  274. imx_sata_disable(hpriv);
  275. imxpriv->no_device = true;
  276. dev_info(ap->dev, "no device found, disabling link.\n");
  277. dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
  278. }
  279. static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
  280. unsigned long deadline)
  281. {
  282. struct ata_port *ap = link->ap;
  283. struct ata_host *host = dev_get_drvdata(ap->dev);
  284. struct ahci_host_priv *hpriv = host->private_data;
  285. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  286. int ret = -EIO;
  287. if (imxpriv->type == AHCI_IMX53)
  288. ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
  289. else if (imxpriv->type == AHCI_IMX6Q)
  290. ret = ahci_ops.softreset(link, class, deadline);
  291. return ret;
  292. }
  293. static struct ata_port_operations ahci_imx_ops = {
  294. .inherits = &ahci_ops,
  295. .host_stop = ahci_imx_host_stop,
  296. .error_handler = ahci_imx_error_handler,
  297. .softreset = ahci_imx_softreset,
  298. };
  299. static const struct ata_port_info ahci_imx_port_info = {
  300. .flags = AHCI_FLAG_COMMON,
  301. .pio_mask = ATA_PIO4,
  302. .udma_mask = ATA_UDMA6,
  303. .port_ops = &ahci_imx_ops,
  304. };
  305. static const struct of_device_id imx_ahci_of_match[] = {
  306. { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
  307. { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
  308. {},
  309. };
  310. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  311. static int imx_ahci_probe(struct platform_device *pdev)
  312. {
  313. struct device *dev = &pdev->dev;
  314. const struct of_device_id *of_id;
  315. struct ahci_host_priv *hpriv;
  316. struct imx_ahci_priv *imxpriv;
  317. unsigned int reg_val;
  318. int ret;
  319. of_id = of_match_device(imx_ahci_of_match, dev);
  320. if (!of_id)
  321. return -EINVAL;
  322. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  323. if (!imxpriv)
  324. return -ENOMEM;
  325. imxpriv->ahci_pdev = pdev;
  326. imxpriv->no_device = false;
  327. imxpriv->first_time = true;
  328. imxpriv->type = (enum ahci_imx_type)of_id->data;
  329. imxpriv->sata_clk = devm_clk_get(dev, "sata");
  330. if (IS_ERR(imxpriv->sata_clk)) {
  331. dev_err(dev, "can't get sata clock.\n");
  332. return PTR_ERR(imxpriv->sata_clk);
  333. }
  334. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  335. if (IS_ERR(imxpriv->sata_ref_clk)) {
  336. dev_err(dev, "can't get sata_ref clock.\n");
  337. return PTR_ERR(imxpriv->sata_ref_clk);
  338. }
  339. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  340. if (IS_ERR(imxpriv->ahb_clk)) {
  341. dev_err(dev, "can't get ahb clock.\n");
  342. return PTR_ERR(imxpriv->ahb_clk);
  343. }
  344. if (imxpriv->type == AHCI_IMX6Q) {
  345. imxpriv->gpr = syscon_regmap_lookup_by_compatible(
  346. "fsl,imx6q-iomuxc-gpr");
  347. if (IS_ERR(imxpriv->gpr)) {
  348. dev_err(dev,
  349. "failed to find fsl,imx6q-iomux-gpr regmap\n");
  350. return PTR_ERR(imxpriv->gpr);
  351. }
  352. }
  353. hpriv = ahci_platform_get_resources(pdev);
  354. if (IS_ERR(hpriv))
  355. return PTR_ERR(hpriv);
  356. hpriv->plat_data = imxpriv;
  357. ret = clk_prepare_enable(imxpriv->sata_clk);
  358. if (ret)
  359. return ret;
  360. ret = imx_sata_enable(hpriv);
  361. if (ret)
  362. goto disable_clk;
  363. /*
  364. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  365. * and IP vendor specific register IMX_TIMER1MS.
  366. * Configure CAP_SSS (support stagered spin up).
  367. * Implement the port0.
  368. * Get the ahb clock rate, and configure the TIMER1MS register.
  369. */
  370. reg_val = readl(hpriv->mmio + HOST_CAP);
  371. if (!(reg_val & HOST_CAP_SSS)) {
  372. reg_val |= HOST_CAP_SSS;
  373. writel(reg_val, hpriv->mmio + HOST_CAP);
  374. }
  375. reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
  376. if (!(reg_val & 0x1)) {
  377. reg_val |= 0x1;
  378. writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
  379. }
  380. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  381. writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
  382. ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
  383. 0, 0, 0);
  384. if (ret)
  385. goto disable_sata;
  386. return 0;
  387. disable_sata:
  388. imx_sata_disable(hpriv);
  389. disable_clk:
  390. clk_disable_unprepare(imxpriv->sata_clk);
  391. return ret;
  392. }
  393. static void ahci_imx_host_stop(struct ata_host *host)
  394. {
  395. struct ahci_host_priv *hpriv = host->private_data;
  396. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  397. imx_sata_disable(hpriv);
  398. clk_disable_unprepare(imxpriv->sata_clk);
  399. }
  400. #ifdef CONFIG_PM_SLEEP
  401. static int imx_ahci_suspend(struct device *dev)
  402. {
  403. struct ata_host *host = dev_get_drvdata(dev);
  404. struct ahci_host_priv *hpriv = host->private_data;
  405. int ret;
  406. ret = ahci_platform_suspend_host(dev);
  407. if (ret)
  408. return ret;
  409. imx_sata_disable(hpriv);
  410. return 0;
  411. }
  412. static int imx_ahci_resume(struct device *dev)
  413. {
  414. struct ata_host *host = dev_get_drvdata(dev);
  415. struct ahci_host_priv *hpriv = host->private_data;
  416. int ret;
  417. ret = imx_sata_enable(hpriv);
  418. if (ret)
  419. return ret;
  420. return ahci_platform_resume_host(dev);
  421. }
  422. #endif
  423. static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
  424. static struct platform_driver imx_ahci_driver = {
  425. .probe = imx_ahci_probe,
  426. .remove = ata_platform_remove_one,
  427. .driver = {
  428. .name = "ahci-imx",
  429. .owner = THIS_MODULE,
  430. .of_match_table = imx_ahci_of_match,
  431. .pm = &ahci_imx_pm_ops,
  432. },
  433. };
  434. module_platform_driver(imx_ahci_driver);
  435. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  436. MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
  437. MODULE_LICENSE("GPL");
  438. MODULE_ALIAS("ahci:imx");