ahci_imx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. #define DRV_NAME "ahci-imx"
  30. enum {
  31. /* Timer 1-ms Register */
  32. IMX_TIMER1MS = 0x00e0,
  33. /* Port0 PHY Control Register */
  34. IMX_P0PHYCR = 0x0178,
  35. IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
  36. IMX_P0PHYCR_CR_READ = 1 << 19,
  37. IMX_P0PHYCR_CR_WRITE = 1 << 18,
  38. IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
  39. IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
  40. /* Port0 PHY Status Register */
  41. IMX_P0PHYSR = 0x017c,
  42. IMX_P0PHYSR_CR_ACK = 1 << 18,
  43. IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
  44. /* Lane0 Output Status Register */
  45. IMX_LANE0_OUT_STAT = 0x2003,
  46. IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
  47. /* Clock Reset Register */
  48. IMX_CLOCK_RESET = 0x7f3f,
  49. IMX_CLOCK_RESET_RESET = 1 << 0,
  50. };
  51. enum ahci_imx_type {
  52. AHCI_IMX53,
  53. AHCI_IMX6Q,
  54. };
  55. struct imx_ahci_priv {
  56. struct platform_device *ahci_pdev;
  57. enum ahci_imx_type type;
  58. struct clk *sata_clk;
  59. struct clk *sata_ref_clk;
  60. struct clk *ahb_clk;
  61. struct regmap *gpr;
  62. bool no_device;
  63. bool first_time;
  64. u32 phy_params;
  65. };
  66. static int ahci_imx_hotplug;
  67. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  68. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  69. static void ahci_imx_host_stop(struct ata_host *host);
  70. static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
  71. {
  72. int timeout = 10;
  73. u32 crval;
  74. u32 srval;
  75. /* Assert or deassert the bit */
  76. crval = readl(mmio + IMX_P0PHYCR);
  77. if (assert)
  78. crval |= bit;
  79. else
  80. crval &= ~bit;
  81. writel(crval, mmio + IMX_P0PHYCR);
  82. /* Wait for the cr_ack signal */
  83. do {
  84. srval = readl(mmio + IMX_P0PHYSR);
  85. if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
  86. break;
  87. usleep_range(100, 200);
  88. } while (--timeout);
  89. return timeout ? 0 : -ETIMEDOUT;
  90. }
  91. static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
  92. {
  93. u32 crval = addr;
  94. int ret;
  95. /* Supply the address on cr_data_in */
  96. writel(crval, mmio + IMX_P0PHYCR);
  97. /* Assert the cr_cap_addr signal */
  98. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
  99. if (ret)
  100. return ret;
  101. /* Deassert cr_cap_addr */
  102. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
  103. if (ret)
  104. return ret;
  105. return 0;
  106. }
  107. static int imx_phy_reg_write(u16 val, void __iomem *mmio)
  108. {
  109. u32 crval = val;
  110. int ret;
  111. /* Supply the data on cr_data_in */
  112. writel(crval, mmio + IMX_P0PHYCR);
  113. /* Assert the cr_cap_data signal */
  114. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
  115. if (ret)
  116. return ret;
  117. /* Deassert cr_cap_data */
  118. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
  119. if (ret)
  120. return ret;
  121. if (val & IMX_CLOCK_RESET_RESET) {
  122. /*
  123. * In case we're resetting the phy, it's unable to acknowledge,
  124. * so we return immediately here.
  125. */
  126. crval |= IMX_P0PHYCR_CR_WRITE;
  127. writel(crval, mmio + IMX_P0PHYCR);
  128. goto out;
  129. }
  130. /* Assert the cr_write signal */
  131. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
  132. if (ret)
  133. return ret;
  134. /* Deassert cr_write */
  135. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
  136. if (ret)
  137. return ret;
  138. out:
  139. return 0;
  140. }
  141. static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
  142. {
  143. int ret;
  144. /* Assert the cr_read signal */
  145. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
  146. if (ret)
  147. return ret;
  148. /* Capture the data from cr_data_out[] */
  149. *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
  150. /* Deassert cr_read */
  151. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
  152. if (ret)
  153. return ret;
  154. return 0;
  155. }
  156. static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
  157. {
  158. void __iomem *mmio = hpriv->mmio;
  159. int timeout = 10;
  160. u16 val;
  161. int ret;
  162. /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
  163. ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
  164. if (ret)
  165. return ret;
  166. ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
  167. if (ret)
  168. return ret;
  169. /* Wait for PHY RX_PLL to be stable */
  170. do {
  171. usleep_range(100, 200);
  172. ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
  173. if (ret)
  174. return ret;
  175. ret = imx_phy_reg_read(&val, mmio);
  176. if (ret)
  177. return ret;
  178. if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
  179. break;
  180. } while (--timeout);
  181. return timeout ? 0 : -ETIMEDOUT;
  182. }
  183. static int imx_sata_enable(struct ahci_host_priv *hpriv)
  184. {
  185. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  186. struct device *dev = &imxpriv->ahci_pdev->dev;
  187. int ret;
  188. if (imxpriv->no_device)
  189. return 0;
  190. ret = ahci_platform_enable_regulators(hpriv);
  191. if (ret)
  192. return ret;
  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. imxpriv->phy_params);
  215. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  216. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  217. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  218. usleep_range(100, 200);
  219. ret = imx_sata_phy_reset(hpriv);
  220. if (ret) {
  221. dev_err(dev, "failed to reset phy: %d\n", ret);
  222. goto disable_clk;
  223. }
  224. }
  225. usleep_range(1000, 2000);
  226. return 0;
  227. disable_clk:
  228. clk_disable_unprepare(imxpriv->sata_ref_clk);
  229. disable_regulator:
  230. ahci_platform_disable_regulators(hpriv);
  231. return ret;
  232. }
  233. static void imx_sata_disable(struct ahci_host_priv *hpriv)
  234. {
  235. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  236. if (imxpriv->no_device)
  237. return;
  238. if (imxpriv->type == AHCI_IMX6Q) {
  239. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  240. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  241. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  242. }
  243. clk_disable_unprepare(imxpriv->sata_ref_clk);
  244. ahci_platform_disable_regulators(hpriv);
  245. }
  246. static void ahci_imx_error_handler(struct ata_port *ap)
  247. {
  248. u32 reg_val;
  249. struct ata_device *dev;
  250. struct ata_host *host = dev_get_drvdata(ap->dev);
  251. struct ahci_host_priv *hpriv = host->private_data;
  252. void __iomem *mmio = hpriv->mmio;
  253. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  254. ahci_error_handler(ap);
  255. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  256. return;
  257. imxpriv->first_time = false;
  258. ata_for_each_dev(dev, &ap->link, ENABLED)
  259. return;
  260. /*
  261. * Disable link to save power. An imx ahci port can't be recovered
  262. * without full reset once the pddq mode is enabled making it
  263. * impossible to use as part of libata LPM.
  264. */
  265. reg_val = readl(mmio + IMX_P0PHYCR);
  266. writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
  267. imx_sata_disable(hpriv);
  268. imxpriv->no_device = true;
  269. dev_info(ap->dev, "no device found, disabling link.\n");
  270. dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
  271. }
  272. static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
  273. unsigned long deadline)
  274. {
  275. struct ata_port *ap = link->ap;
  276. struct ata_host *host = dev_get_drvdata(ap->dev);
  277. struct ahci_host_priv *hpriv = host->private_data;
  278. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  279. int ret = -EIO;
  280. if (imxpriv->type == AHCI_IMX53)
  281. ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
  282. else if (imxpriv->type == AHCI_IMX6Q)
  283. ret = ahci_ops.softreset(link, class, deadline);
  284. return ret;
  285. }
  286. static struct ata_port_operations ahci_imx_ops = {
  287. .inherits = &ahci_ops,
  288. .host_stop = ahci_imx_host_stop,
  289. .error_handler = ahci_imx_error_handler,
  290. .softreset = ahci_imx_softreset,
  291. };
  292. static const struct ata_port_info ahci_imx_port_info = {
  293. .flags = AHCI_FLAG_COMMON,
  294. .pio_mask = ATA_PIO4,
  295. .udma_mask = ATA_UDMA6,
  296. .port_ops = &ahci_imx_ops,
  297. };
  298. static const struct of_device_id imx_ahci_of_match[] = {
  299. { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
  300. { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
  301. {},
  302. };
  303. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  304. struct reg_value {
  305. u32 of_value;
  306. u32 reg_value;
  307. };
  308. struct reg_property {
  309. const char *name;
  310. const struct reg_value *values;
  311. size_t num_values;
  312. u32 def_value;
  313. u32 set_value;
  314. };
  315. static const struct reg_value gpr13_tx_level[] = {
  316. { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
  317. { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
  318. { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
  319. { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
  320. { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
  321. { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
  322. { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
  323. { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
  324. { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
  325. { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
  326. { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
  327. { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
  328. { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
  329. { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
  330. { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
  331. { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
  332. { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
  333. { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
  334. { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
  335. { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
  336. { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
  337. { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
  338. { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
  339. { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
  340. { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
  341. { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
  342. { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
  343. { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
  344. { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
  345. { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
  346. { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
  347. { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
  348. };
  349. static const struct reg_value gpr13_tx_boost[] = {
  350. { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
  351. { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
  352. { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
  353. { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
  354. { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
  355. { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
  356. { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
  357. { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
  358. { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
  359. { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
  360. { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
  361. { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
  362. { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
  363. { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
  364. { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
  365. { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
  366. };
  367. static const struct reg_value gpr13_tx_atten[] = {
  368. { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
  369. { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
  370. { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
  371. { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
  372. { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
  373. { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
  374. };
  375. static const struct reg_value gpr13_rx_eq[] = {
  376. { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
  377. { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
  378. { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
  379. { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
  380. { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
  381. { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
  382. { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
  383. { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
  384. };
  385. static const struct reg_property gpr13_props[] = {
  386. {
  387. .name = "fsl,transmit-level-mV",
  388. .values = gpr13_tx_level,
  389. .num_values = ARRAY_SIZE(gpr13_tx_level),
  390. .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
  391. }, {
  392. .name = "fsl,transmit-boost-mdB",
  393. .values = gpr13_tx_boost,
  394. .num_values = ARRAY_SIZE(gpr13_tx_boost),
  395. .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
  396. }, {
  397. .name = "fsl,transmit-atten-16ths",
  398. .values = gpr13_tx_atten,
  399. .num_values = ARRAY_SIZE(gpr13_tx_atten),
  400. .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
  401. }, {
  402. .name = "fsl,receive-eq-mdB",
  403. .values = gpr13_rx_eq,
  404. .num_values = ARRAY_SIZE(gpr13_rx_eq),
  405. .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
  406. }, {
  407. .name = "fsl,no-spread-spectrum",
  408. .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
  409. .set_value = 0,
  410. },
  411. };
  412. static u32 imx_ahci_parse_props(struct device *dev,
  413. const struct reg_property *prop, size_t num)
  414. {
  415. struct device_node *np = dev->of_node;
  416. u32 reg_value = 0;
  417. int i, j;
  418. for (i = 0; i < num; i++, prop++) {
  419. u32 of_val;
  420. if (prop->num_values == 0) {
  421. if (of_property_read_bool(np, prop->name))
  422. reg_value |= prop->set_value;
  423. else
  424. reg_value |= prop->def_value;
  425. continue;
  426. }
  427. if (of_property_read_u32(np, prop->name, &of_val)) {
  428. dev_info(dev, "%s not specified, using %08x\n",
  429. prop->name, prop->def_value);
  430. reg_value |= prop->def_value;
  431. continue;
  432. }
  433. for (j = 0; j < prop->num_values; j++) {
  434. if (prop->values[j].of_value == of_val) {
  435. dev_info(dev, "%s value %u, using %08x\n",
  436. prop->name, of_val, prop->values[j].reg_value);
  437. reg_value |= prop->values[j].reg_value;
  438. break;
  439. }
  440. }
  441. if (j == prop->num_values) {
  442. dev_err(dev, "DT property %s is not a valid value\n",
  443. prop->name);
  444. reg_value |= prop->def_value;
  445. }
  446. }
  447. return reg_value;
  448. }
  449. static struct scsi_host_template ahci_platform_sht = {
  450. AHCI_SHT(DRV_NAME),
  451. };
  452. static int imx_ahci_probe(struct platform_device *pdev)
  453. {
  454. struct device *dev = &pdev->dev;
  455. const struct of_device_id *of_id;
  456. struct ahci_host_priv *hpriv;
  457. struct imx_ahci_priv *imxpriv;
  458. unsigned int reg_val;
  459. int ret;
  460. of_id = of_match_device(imx_ahci_of_match, dev);
  461. if (!of_id)
  462. return -EINVAL;
  463. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  464. if (!imxpriv)
  465. return -ENOMEM;
  466. imxpriv->ahci_pdev = pdev;
  467. imxpriv->no_device = false;
  468. imxpriv->first_time = true;
  469. imxpriv->type = (enum ahci_imx_type)of_id->data;
  470. imxpriv->sata_clk = devm_clk_get(dev, "sata");
  471. if (IS_ERR(imxpriv->sata_clk)) {
  472. dev_err(dev, "can't get sata clock.\n");
  473. return PTR_ERR(imxpriv->sata_clk);
  474. }
  475. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  476. if (IS_ERR(imxpriv->sata_ref_clk)) {
  477. dev_err(dev, "can't get sata_ref clock.\n");
  478. return PTR_ERR(imxpriv->sata_ref_clk);
  479. }
  480. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  481. if (IS_ERR(imxpriv->ahb_clk)) {
  482. dev_err(dev, "can't get ahb clock.\n");
  483. return PTR_ERR(imxpriv->ahb_clk);
  484. }
  485. if (imxpriv->type == AHCI_IMX6Q) {
  486. u32 reg_value;
  487. imxpriv->gpr = syscon_regmap_lookup_by_compatible(
  488. "fsl,imx6q-iomuxc-gpr");
  489. if (IS_ERR(imxpriv->gpr)) {
  490. dev_err(dev,
  491. "failed to find fsl,imx6q-iomux-gpr regmap\n");
  492. return PTR_ERR(imxpriv->gpr);
  493. }
  494. reg_value = imx_ahci_parse_props(dev, gpr13_props,
  495. ARRAY_SIZE(gpr13_props));
  496. imxpriv->phy_params =
  497. IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
  498. IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
  499. IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
  500. reg_value;
  501. }
  502. hpriv = ahci_platform_get_resources(pdev);
  503. if (IS_ERR(hpriv))
  504. return PTR_ERR(hpriv);
  505. hpriv->plat_data = imxpriv;
  506. ret = clk_prepare_enable(imxpriv->sata_clk);
  507. if (ret)
  508. return ret;
  509. ret = imx_sata_enable(hpriv);
  510. if (ret)
  511. goto disable_clk;
  512. /*
  513. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  514. * and IP vendor specific register IMX_TIMER1MS.
  515. * Configure CAP_SSS (support stagered spin up).
  516. * Implement the port0.
  517. * Get the ahb clock rate, and configure the TIMER1MS register.
  518. */
  519. reg_val = readl(hpriv->mmio + HOST_CAP);
  520. if (!(reg_val & HOST_CAP_SSS)) {
  521. reg_val |= HOST_CAP_SSS;
  522. writel(reg_val, hpriv->mmio + HOST_CAP);
  523. }
  524. reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
  525. if (!(reg_val & 0x1)) {
  526. reg_val |= 0x1;
  527. writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
  528. }
  529. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  530. writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
  531. ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
  532. &ahci_platform_sht);
  533. if (ret)
  534. goto disable_sata;
  535. return 0;
  536. disable_sata:
  537. imx_sata_disable(hpriv);
  538. disable_clk:
  539. clk_disable_unprepare(imxpriv->sata_clk);
  540. return ret;
  541. }
  542. static void ahci_imx_host_stop(struct ata_host *host)
  543. {
  544. struct ahci_host_priv *hpriv = host->private_data;
  545. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  546. imx_sata_disable(hpriv);
  547. clk_disable_unprepare(imxpriv->sata_clk);
  548. }
  549. #ifdef CONFIG_PM_SLEEP
  550. static int imx_ahci_suspend(struct device *dev)
  551. {
  552. struct ata_host *host = dev_get_drvdata(dev);
  553. struct ahci_host_priv *hpriv = host->private_data;
  554. int ret;
  555. ret = ahci_platform_suspend_host(dev);
  556. if (ret)
  557. return ret;
  558. imx_sata_disable(hpriv);
  559. return 0;
  560. }
  561. static int imx_ahci_resume(struct device *dev)
  562. {
  563. struct ata_host *host = dev_get_drvdata(dev);
  564. struct ahci_host_priv *hpriv = host->private_data;
  565. int ret;
  566. ret = imx_sata_enable(hpriv);
  567. if (ret)
  568. return ret;
  569. return ahci_platform_resume_host(dev);
  570. }
  571. #endif
  572. static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
  573. static struct platform_driver imx_ahci_driver = {
  574. .probe = imx_ahci_probe,
  575. .remove = ata_platform_remove_one,
  576. .driver = {
  577. .name = DRV_NAME,
  578. .of_match_table = imx_ahci_of_match,
  579. .pm = &ahci_imx_pm_ops,
  580. },
  581. };
  582. module_platform_driver(imx_ahci_driver);
  583. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  584. MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
  585. MODULE_LICENSE("GPL");
  586. MODULE_ALIAS("ahci:imx");