ahci_msm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. /*
  14. * SATA init module.
  15. * To be used with SATA interface on MSM targets.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/io.h>
  21. #include <linux/err.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/delay.h>
  26. #include <linux/clk.h>
  27. #include <linux/iopoll.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/ahci_platform.h>
  30. #include <mach/clk.h>
  31. /* PHY registers */
  32. #define UNIPHY_PLL_REFCLK_CFG 0x000
  33. #define UNIPHY_PLL_POSTDIV1_CFG 0x004
  34. #define UNIPHY_PLL_CHGPUMP_CFG 0x008
  35. #define UNIPHY_PLL_VCOLPF_CFG 0x00C
  36. #define UNIPHY_PLL_VREG_CFG 0x010
  37. #define UNIPHY_PLL_PWRGEN_CFG 0x014
  38. #define UNIPHY_PLL_DMUX_CFG 0x018
  39. #define UNIPHY_PLL_AMUX_CFG 0x01C
  40. #define UNIPHY_PLL_GLB_CFG 0x020
  41. #define UNIPHY_PLL_POSTDIV2_CFG 0x024
  42. #define UNIPHY_PLL_POSTDIV3_CFG 0x028
  43. #define UNIPHY_PLL_LPFR_CFG 0x02C
  44. #define UNIPHY_PLL_LPFC1_CFG 0x030
  45. #define UNIPHY_PLL_LPFC2_CFG 0x034
  46. #define UNIPHY_PLL_SDM_CFG0 0x038
  47. #define UNIPHY_PLL_SDM_CFG1 0x03C
  48. #define UNIPHY_PLL_SDM_CFG2 0x040
  49. #define UNIPHY_PLL_SDM_CFG3 0x044
  50. #define UNIPHY_PLL_SDM_CFG4 0x048
  51. #define UNIPHY_PLL_SSC_CFG0 0x04C
  52. #define UNIPHY_PLL_SSC_CFG1 0x050
  53. #define UNIPHY_PLL_SSC_CFG2 0x054
  54. #define UNIPHY_PLL_SSC_CFG3 0x058
  55. #define UNIPHY_PLL_LKDET_CFG0 0x05C
  56. #define UNIPHY_PLL_LKDET_CFG1 0x060
  57. #define UNIPHY_PLL_LKDET_CFG2 0x064
  58. #define UNIPHY_PLL_TEST_CFG 0x068
  59. #define UNIPHY_PLL_CAL_CFG0 0x06C
  60. #define UNIPHY_PLL_CAL_CFG1 0x070
  61. #define UNIPHY_PLL_CAL_CFG2 0x074
  62. #define UNIPHY_PLL_CAL_CFG3 0x078
  63. #define UNIPHY_PLL_CAL_CFG4 0x07C
  64. #define UNIPHY_PLL_CAL_CFG5 0x080
  65. #define UNIPHY_PLL_CAL_CFG6 0x084
  66. #define UNIPHY_PLL_CAL_CFG7 0x088
  67. #define UNIPHY_PLL_CAL_CFG8 0x08C
  68. #define UNIPHY_PLL_CAL_CFG9 0x090
  69. #define UNIPHY_PLL_CAL_CFG10 0x094
  70. #define UNIPHY_PLL_CAL_CFG11 0x098
  71. #define UNIPHY_PLL_EFUSE_CFG 0x09C
  72. #define UNIPHY_PLL_DEBUG_BUS_SEL 0x0A0
  73. #define UNIPHY_PLL_CTRL_42 0x0A4
  74. #define UNIPHY_PLL_CTRL_43 0x0A8
  75. #define UNIPHY_PLL_CTRL_44 0x0AC
  76. #define UNIPHY_PLL_CTRL_45 0x0B0
  77. #define UNIPHY_PLL_CTRL_46 0x0B4
  78. #define UNIPHY_PLL_CTRL_47 0x0B8
  79. #define UNIPHY_PLL_CTRL_48 0x0BC
  80. #define UNIPHY_PLL_STATUS 0x0C0
  81. #define UNIPHY_PLL_DEBUG_BUS0 0x0C4
  82. #define UNIPHY_PLL_DEBUG_BUS1 0x0C8
  83. #define UNIPHY_PLL_DEBUG_BUS2 0x0CC
  84. #define UNIPHY_PLL_DEBUG_BUS3 0x0D0
  85. #define UNIPHY_PLL_CTRL_54 0x0D4
  86. #define SATA_PHY_SER_CTRL 0x100
  87. #define SATA_PHY_TX_DRIV_CTRL0 0x104
  88. #define SATA_PHY_TX_DRIV_CTRL1 0x108
  89. #define SATA_PHY_TX_DRIV_CTRL2 0x10C
  90. #define SATA_PHY_TX_DRIV_CTRL3 0x110
  91. #define SATA_PHY_TX_RESV0 0x114
  92. #define SATA_PHY_TX_RESV1 0x118
  93. #define SATA_PHY_TX_IMCAL0 0x11C
  94. #define SATA_PHY_TX_IMCAL1 0x120
  95. #define SATA_PHY_TX_IMCAL2 0x124
  96. #define SATA_PHY_RX_IMCAL0 0x128
  97. #define SATA_PHY_RX_IMCAL1 0x12C
  98. #define SATA_PHY_RX_IMCAL2 0x130
  99. #define SATA_PHY_RX_TERM 0x134
  100. #define SATA_PHY_RX_TERM_RESV 0x138
  101. #define SATA_PHY_EQUAL 0x13C
  102. #define SATA_PHY_EQUAL_RESV 0x140
  103. #define SATA_PHY_OOB_TERM 0x144
  104. #define SATA_PHY_CDR_CTRL0 0x148
  105. #define SATA_PHY_CDR_CTRL1 0x14C
  106. #define SATA_PHY_CDR_CTRL2 0x150
  107. #define SATA_PHY_CDR_CTRL3 0x154
  108. #define SATA_PHY_CDR_CTRL4 0x158
  109. #define SATA_PHY_FA_LOAD0 0x15C
  110. #define SATA_PHY_FA_LOAD1 0x160
  111. #define SATA_PHY_CDR_CTRL_RESV 0x164
  112. #define SATA_PHY_PI_CTRL0 0x168
  113. #define SATA_PHY_PI_CTRL1 0x16C
  114. #define SATA_PHY_DESER_RESV 0x170
  115. #define SATA_PHY_RX_RESV0 0x174
  116. #define SATA_PHY_AD_TPA_CTRL 0x178
  117. #define SATA_PHY_REFCLK_CTRL 0x17C
  118. #define SATA_PHY_POW_DWN_CTRL0 0x180
  119. #define SATA_PHY_POW_DWN_CTRL1 0x184
  120. #define SATA_PHY_TX_DATA_CTRL 0x188
  121. #define SATA_PHY_BIST_GEN0 0x18C
  122. #define SATA_PHY_BIST_GEN1 0x190
  123. #define SATA_PHY_BIST_GEN2 0x194
  124. #define SATA_PHY_BIST_GEN3 0x198
  125. #define SATA_PHY_LBK_CTRL 0x19C
  126. #define SATA_PHY_TEST_DEBUG_CTRL 0x1A0
  127. #define SATA_PHY_ALIGNP 0x1A4
  128. #define SATA_PHY_PRBS_CFG0 0x1A8
  129. #define SATA_PHY_PRBS_CFG1 0x1AC
  130. #define SATA_PHY_PRBS_CFG2 0x1B0
  131. #define SATA_PHY_PRBS_CFG3 0x1B4
  132. #define SATA_PHY_CHAN_COMP_CHK_CNT 0x1B8
  133. #define SATA_PHY_RESET_CTRL 0x1BC
  134. #define SATA_PHY_RX_CLR 0x1C0
  135. #define SATA_PHY_RX_EBUF_CTRL 0x1C4
  136. #define SATA_PHY_ID0 0x1C8
  137. #define SATA_PHY_ID1 0x1CC
  138. #define SATA_PHY_ID2 0x1D0
  139. #define SATA_PHY_ID3 0x1D4
  140. #define SATA_PHY_RX_CHK_ERR_CNT0 0x1D8
  141. #define SATA_PHY_RX_CHK_ERR_CNT1 0x1DC
  142. #define SATA_PHY_RX_CHK_STAT 0x1E0
  143. #define SATA_PHY_TX_IMCAL_STAT 0x1E4
  144. #define SATA_PHY_RX_IMCAL_STAT 0x1E8
  145. #define SATA_PHY_RX_EBUF_STAT 0x1EC
  146. #define SATA_PHY_DEBUG_BUS_STAT0 0x1F0
  147. #define SATA_PHY_DEBUG_BUS_STAT1 0x1F4
  148. #define SATA_PHY_DEBUG_BUS_STAT2 0x1F8
  149. #define SATA_PHY_DEBUG_BUS_STAT3 0x1FC
  150. #define AHCI_HOST_CAP 0x00
  151. #define AHCI_HOST_CAP_MASK 0x1F
  152. #define AHCI_HOST_CAP_PMP (1 << 17)
  153. struct msm_sata_hba {
  154. struct platform_device *ahci_pdev;
  155. struct clk *slave_iface_clk;
  156. struct clk *bus_clk;
  157. struct clk *iface_clk;
  158. struct clk *src_clk;
  159. struct clk *rxoob_clk;
  160. struct clk *pmalive_clk;
  161. struct clk *cfg_clk;
  162. struct regulator *clk_pwr;
  163. struct regulator *pmp_pwr;
  164. void __iomem *phy_base;
  165. void __iomem *ahci_base;
  166. };
  167. static inline void msm_sata_delay_us(unsigned int delay)
  168. {
  169. /* sleep for max. 50us more to combine processor wakeups */
  170. usleep_range(delay, delay + 50);
  171. }
  172. static int msm_sata_clk_get_prepare_enable_set_rate(struct device *dev,
  173. const char *name, struct clk **out_clk, int rate)
  174. {
  175. int ret = 0;
  176. struct clk *clk;
  177. clk = devm_clk_get(dev, name);
  178. if (IS_ERR(clk)) {
  179. ret = PTR_ERR(clk);
  180. dev_err(dev, "failed to get clk: %s err = %d\n", name, ret);
  181. goto out;
  182. }
  183. if (rate >= 0) {
  184. ret = clk_set_rate(clk, rate);
  185. if (ret) {
  186. dev_err(dev, "failed to set rate: %d clk: %s err = %d\n",
  187. rate, name, ret);
  188. goto out;
  189. }
  190. }
  191. ret = clk_prepare_enable(clk);
  192. if (ret)
  193. dev_err(dev, "failed to enable clk: %s err = %d\n", name, ret);
  194. out:
  195. if (!ret)
  196. *out_clk = clk;
  197. return ret;
  198. }
  199. static int msm_sata_clk_get_prepare_enable(struct device *dev,
  200. const char *name, struct clk **out_clk)
  201. {
  202. return msm_sata_clk_get_prepare_enable_set_rate(dev, name, out_clk, -1);
  203. }
  204. static void msm_sata_clk_put_unprepare_disable(struct clk **clk)
  205. {
  206. if (*clk) {
  207. clk_disable_unprepare(*clk);
  208. clk_put(*clk);
  209. *clk = NULL;
  210. }
  211. }
  212. static int msm_sata_hard_reset(struct device *dev)
  213. {
  214. int ret;
  215. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  216. ret = clk_reset(hba->iface_clk, CLK_RESET_ASSERT);
  217. if (ret) {
  218. dev_err(dev, "iface_clk assert failed %d\n", ret);
  219. goto out;
  220. }
  221. ret = clk_reset(hba->iface_clk, CLK_RESET_DEASSERT);
  222. if (ret) {
  223. dev_err(dev, "iface_clk de-assert failed %d\n", ret);
  224. goto out;
  225. }
  226. out:
  227. return ret;
  228. }
  229. static int msm_sata_clk_init(struct device *dev)
  230. {
  231. int ret = 0;
  232. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  233. /* Enable AHB clock for system fabric slave port connected to SATA */
  234. ret = msm_sata_clk_get_prepare_enable(dev,
  235. "slave_iface_clk", &hba->slave_iface_clk);
  236. if (ret)
  237. goto out;
  238. /* Enable AHB clock for system fabric and SATA core interface */
  239. ret = msm_sata_clk_get_prepare_enable(dev,
  240. "iface_clk", &hba->iface_clk);
  241. if (ret)
  242. goto put_dis_slave_iface_clk;
  243. /* Enable AXI clock for SATA AXI master and slave interfaces */
  244. ret = msm_sata_clk_get_prepare_enable(dev,
  245. "bus_clk", &hba->bus_clk);
  246. if (ret)
  247. goto put_dis_iface_clk;
  248. /* Enable the source clock for pmalive, rxoob and phy ref clocks */
  249. ret = msm_sata_clk_get_prepare_enable_set_rate(dev,
  250. "src_clk", &hba->src_clk, 100000000);
  251. if (ret)
  252. goto put_dis_bus_clk;
  253. /*
  254. * Enable RX OOB detection clock. The clock rate is
  255. * same as PHY reference clock (100MHz).
  256. */
  257. ret = msm_sata_clk_get_prepare_enable(dev,
  258. "core_rxoob_clk", &hba->rxoob_clk);
  259. if (ret)
  260. goto put_dis_src_clk;
  261. /*
  262. * Enable power management always-on clock. The clock rate
  263. * is same as PHY reference clock (100MHz).
  264. */
  265. ret = msm_sata_clk_get_prepare_enable(dev,
  266. "core_pmalive_clk", &hba->pmalive_clk);
  267. if (ret)
  268. goto put_dis_rxoob_clk;
  269. /* Enable PHY configuration AHB clock, fixed 64MHz clock */
  270. ret = msm_sata_clk_get_prepare_enable(dev,
  271. "cfg_clk", &hba->cfg_clk);
  272. if (ret)
  273. goto put_dis_pmalive_clk;
  274. return ret;
  275. put_dis_pmalive_clk:
  276. msm_sata_clk_put_unprepare_disable(&hba->pmalive_clk);
  277. put_dis_rxoob_clk:
  278. msm_sata_clk_put_unprepare_disable(&hba->rxoob_clk);
  279. put_dis_src_clk:
  280. msm_sata_clk_put_unprepare_disable(&hba->src_clk);
  281. put_dis_bus_clk:
  282. msm_sata_clk_put_unprepare_disable(&hba->bus_clk);
  283. put_dis_iface_clk:
  284. msm_sata_clk_put_unprepare_disable(&hba->iface_clk);
  285. put_dis_slave_iface_clk:
  286. msm_sata_clk_put_unprepare_disable(&hba->slave_iface_clk);
  287. out:
  288. return ret;
  289. }
  290. static void msm_sata_clk_deinit(struct device *dev)
  291. {
  292. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  293. msm_sata_clk_put_unprepare_disable(&hba->cfg_clk);
  294. msm_sata_clk_put_unprepare_disable(&hba->pmalive_clk);
  295. msm_sata_clk_put_unprepare_disable(&hba->rxoob_clk);
  296. msm_sata_clk_put_unprepare_disable(&hba->src_clk);
  297. msm_sata_clk_put_unprepare_disable(&hba->bus_clk);
  298. msm_sata_clk_put_unprepare_disable(&hba->iface_clk);
  299. msm_sata_clk_put_unprepare_disable(&hba->slave_iface_clk);
  300. }
  301. static int msm_sata_vreg_get_enable_set_vdd(struct device *dev,
  302. const char *name, struct regulator **out_vreg,
  303. int min_uV, int max_uV, int hpm_uA)
  304. {
  305. int ret = 0;
  306. struct regulator *vreg;
  307. vreg = devm_regulator_get(dev, name);
  308. if (IS_ERR(vreg)) {
  309. ret = PTR_ERR(vreg);
  310. dev_err(dev, "Regulator: %s get failed, err=%d\n", name, ret);
  311. goto out;
  312. }
  313. if (regulator_count_voltages(vreg) > 0) {
  314. ret = regulator_set_voltage(vreg, min_uV, max_uV);
  315. if (ret) {
  316. dev_err(dev, "Regulator: %s set voltage failed, err=%d\n",
  317. name, ret);
  318. goto err;
  319. }
  320. ret = regulator_set_optimum_mode(vreg, hpm_uA);
  321. if (ret < 0) {
  322. dev_err(dev, "Regulator: %s set optimum mode(uA_load=%d) failed, err=%d\n",
  323. name, hpm_uA, ret);
  324. goto err;
  325. } else {
  326. /*
  327. * regulator_set_optimum_mode() can return non zero
  328. * value even for success case.
  329. */
  330. ret = 0;
  331. }
  332. }
  333. ret = regulator_enable(vreg);
  334. if (ret)
  335. dev_err(dev, "Regulator: %s enable failed, err=%d\n",
  336. name, ret);
  337. err:
  338. if (!ret)
  339. *out_vreg = vreg;
  340. else
  341. devm_regulator_put(vreg);
  342. out:
  343. return ret;
  344. }
  345. static int msm_sata_vreg_put_disable(struct device *dev,
  346. struct regulator *reg, const char *name, int max_uV)
  347. {
  348. int ret;
  349. if (!reg)
  350. return 0;
  351. ret = regulator_disable(reg);
  352. if (ret) {
  353. dev_err(dev, "Regulator: %s disable failed err=%d\n",
  354. name, ret);
  355. goto err;
  356. }
  357. if (regulator_count_voltages(reg) > 0) {
  358. ret = regulator_set_voltage(reg, 0, max_uV);
  359. if (ret < 0) {
  360. dev_err(dev, "Regulator: %s set voltage to 0 failed, err=%d\n",
  361. name, ret);
  362. goto err;
  363. }
  364. ret = regulator_set_optimum_mode(reg, 0);
  365. if (ret < 0) {
  366. dev_err(dev, "Regulator: %s set optimum mode(uA_load = 0) failed, err=%d\n",
  367. name, ret);
  368. goto err;
  369. } else {
  370. /*
  371. * regulator_set_optimum_mode() can return non zero
  372. * value even for success case.
  373. */
  374. ret = 0;
  375. }
  376. }
  377. err:
  378. devm_regulator_put(reg);
  379. return ret;
  380. }
  381. static int msm_sata_vreg_init(struct device *dev)
  382. {
  383. int ret = 0;
  384. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  385. /*
  386. * The SATA clock generator needs 3.3V supply and can consume
  387. * max. 850mA during functional mode.
  388. */
  389. ret = msm_sata_vreg_get_enable_set_vdd(dev, "sata_ext_3p3v",
  390. &hba->clk_pwr, 3300000, 3300000, 850000);
  391. if (ret)
  392. goto out;
  393. /* Add 1ms regulator ramp-up delay */
  394. msm_sata_delay_us(1000);
  395. /* Read AHCI capability register to check if PMP is supported.*/
  396. if (readl_relaxed(hba->ahci_base +
  397. AHCI_HOST_CAP) & AHCI_HOST_CAP_PMP) {
  398. /* Power up port-multiplier */
  399. ret = msm_sata_vreg_get_enable_set_vdd(dev, "sata_pmp_pwr",
  400. &hba->pmp_pwr, 1800000, 1800000, 200000);
  401. if (ret) {
  402. msm_sata_vreg_put_disable(dev, hba->clk_pwr,
  403. "sata_ext_3p3v", 3300000);
  404. goto out;
  405. }
  406. /* Add 1ms regulator ramp-up delay */
  407. msm_sata_delay_us(1000);
  408. }
  409. out:
  410. return ret;
  411. }
  412. static void msm_sata_vreg_deinit(struct device *dev)
  413. {
  414. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  415. msm_sata_vreg_put_disable(dev, hba->clk_pwr,
  416. "sata_ext_3p3v", 3300000);
  417. if (hba->pmp_pwr)
  418. msm_sata_vreg_put_disable(dev, hba->pmp_pwr,
  419. "sata_pmp_pwr", 1800000);
  420. }
  421. static void msm_sata_phy_deinit(struct device *dev)
  422. {
  423. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  424. /* Power down PHY */
  425. writel_relaxed(0xF8, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
  426. writel_relaxed(0xFE, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
  427. /* Power down PLL block */
  428. writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_GLB_CFG);
  429. mb();
  430. devm_iounmap(dev, hba->phy_base);
  431. }
  432. static int msm_sata_phy_init(struct device *dev)
  433. {
  434. int ret = 0;
  435. u32 reg = 0;
  436. struct platform_device *pdev = to_platform_device(dev);
  437. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  438. struct resource *mem;
  439. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_mem");
  440. if (!mem) {
  441. dev_err(dev, "no mmio space\n");
  442. return -EINVAL;
  443. }
  444. hba->phy_base = devm_ioremap(dev, mem->start, resource_size(mem));
  445. if (!hba->phy_base) {
  446. dev_err(dev, "failed to allocate memory for SATA PHY\n");
  447. return -ENOMEM;
  448. }
  449. /* SATA phy initialization */
  450. writel_relaxed(0x01, hba->phy_base + SATA_PHY_SER_CTRL);
  451. writel_relaxed(0xB1, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
  452. mb();
  453. msm_sata_delay_us(10);
  454. writel_relaxed(0x01, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
  455. writel_relaxed(0x3E, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
  456. writel_relaxed(0x01, hba->phy_base + SATA_PHY_RX_IMCAL0);
  457. writel_relaxed(0x01, hba->phy_base + SATA_PHY_TX_IMCAL0);
  458. writel_relaxed(0x02, hba->phy_base + SATA_PHY_TX_IMCAL2);
  459. /* Write UNIPHYPLL registers to configure PLL */
  460. writel_relaxed(0x04, hba->phy_base + UNIPHY_PLL_REFCLK_CFG);
  461. writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_PWRGEN_CFG);
  462. writel_relaxed(0x0A, hba->phy_base + UNIPHY_PLL_CAL_CFG0);
  463. writel_relaxed(0xF3, hba->phy_base + UNIPHY_PLL_CAL_CFG8);
  464. writel_relaxed(0x01, hba->phy_base + UNIPHY_PLL_CAL_CFG9);
  465. writel_relaxed(0xED, hba->phy_base + UNIPHY_PLL_CAL_CFG10);
  466. writel_relaxed(0x02, hba->phy_base + UNIPHY_PLL_CAL_CFG11);
  467. writel_relaxed(0x36, hba->phy_base + UNIPHY_PLL_SDM_CFG0);
  468. writel_relaxed(0x0D, hba->phy_base + UNIPHY_PLL_SDM_CFG1);
  469. writel_relaxed(0xA3, hba->phy_base + UNIPHY_PLL_SDM_CFG2);
  470. writel_relaxed(0xF0, hba->phy_base + UNIPHY_PLL_SDM_CFG3);
  471. writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_SDM_CFG4);
  472. writel_relaxed(0x19, hba->phy_base + UNIPHY_PLL_SSC_CFG0);
  473. writel_relaxed(0xE1, hba->phy_base + UNIPHY_PLL_SSC_CFG1);
  474. writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_SSC_CFG2);
  475. writel_relaxed(0x11, hba->phy_base + UNIPHY_PLL_SSC_CFG3);
  476. writel_relaxed(0x04, hba->phy_base + UNIPHY_PLL_LKDET_CFG0);
  477. writel_relaxed(0xFF, hba->phy_base + UNIPHY_PLL_LKDET_CFG1);
  478. writel_relaxed(0x02, hba->phy_base + UNIPHY_PLL_GLB_CFG);
  479. mb();
  480. msm_sata_delay_us(40);
  481. writel_relaxed(0x03, hba->phy_base + UNIPHY_PLL_GLB_CFG);
  482. mb();
  483. msm_sata_delay_us(400);
  484. writel_relaxed(0x05, hba->phy_base + UNIPHY_PLL_LKDET_CFG2);
  485. mb();
  486. /* poll for ready status, timeout after 1 sec */
  487. ret = readl_poll_timeout(hba->phy_base + UNIPHY_PLL_STATUS, reg,
  488. (reg & 1 << 0), 100, 1000000);
  489. if (ret) {
  490. dev_err(dev, "poll timeout UNIPHY_PLL_STATUS\n");
  491. goto out;
  492. }
  493. ret = readl_poll_timeout(hba->phy_base + SATA_PHY_TX_IMCAL_STAT, reg,
  494. (reg & 1 << 0), 100, 1000000);
  495. if (ret) {
  496. dev_err(dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n");
  497. goto out;
  498. }
  499. ret = readl_poll_timeout(hba->phy_base + SATA_PHY_RX_IMCAL_STAT, reg,
  500. (reg & 1 << 0), 100, 1000000);
  501. if (ret) {
  502. dev_err(dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n");
  503. goto out;
  504. }
  505. /* SATA phy calibrated succesfully, power up to functional mode */
  506. writel_relaxed(0x3E, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
  507. writel_relaxed(0x01, hba->phy_base + SATA_PHY_RX_IMCAL0);
  508. writel_relaxed(0x01, hba->phy_base + SATA_PHY_TX_IMCAL0);
  509. writel_relaxed(0x00, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
  510. writel_relaxed(0x59, hba->phy_base + SATA_PHY_CDR_CTRL0);
  511. writel_relaxed(0x04, hba->phy_base + SATA_PHY_CDR_CTRL1);
  512. writel_relaxed(0x00, hba->phy_base + SATA_PHY_CDR_CTRL2);
  513. writel_relaxed(0x00, hba->phy_base + SATA_PHY_PI_CTRL0);
  514. writel_relaxed(0x00, hba->phy_base + SATA_PHY_CDR_CTRL3);
  515. writel_relaxed(0x01, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
  516. writel_relaxed(0x11, hba->phy_base + SATA_PHY_TX_DATA_CTRL);
  517. writel_relaxed(0x43, hba->phy_base + SATA_PHY_ALIGNP);
  518. writel_relaxed(0x04, hba->phy_base + SATA_PHY_OOB_TERM);
  519. writel_relaxed(0x01, hba->phy_base + SATA_PHY_EQUAL);
  520. writel_relaxed(0x09, hba->phy_base + SATA_PHY_TX_DRIV_CTRL0);
  521. writel_relaxed(0x09, hba->phy_base + SATA_PHY_TX_DRIV_CTRL1);
  522. mb();
  523. dev_dbg(dev, "SATA PHY powered up in functional mode\n");
  524. out:
  525. /* power down PHY in case of failure */
  526. if (ret)
  527. msm_sata_phy_deinit(dev);
  528. return ret;
  529. }
  530. int msm_sata_init(struct device *ahci_dev, void __iomem *mmio)
  531. {
  532. int ret;
  533. struct device *dev = ahci_dev->parent;
  534. struct msm_sata_hba *hba = dev_get_drvdata(dev);
  535. /* Save ahci mmio to access vendor specific registers */
  536. hba->ahci_base = mmio;
  537. ret = msm_sata_clk_init(dev);
  538. if (ret) {
  539. dev_err(dev, "SATA clk init failed with err=%d\n", ret);
  540. goto out;
  541. }
  542. ret = msm_sata_vreg_init(dev);
  543. if (ret) {
  544. dev_err(dev, "SATA vreg init failed with err=%d\n", ret);
  545. msm_sata_clk_deinit(dev);
  546. goto out;
  547. }
  548. ret = msm_sata_phy_init(dev);
  549. if (ret) {
  550. dev_err(dev, "SATA PHY init failed with err=%d\n", ret);
  551. msm_sata_vreg_deinit(dev);
  552. msm_sata_clk_deinit(dev);
  553. goto out;
  554. }
  555. out:
  556. return ret;
  557. }
  558. void msm_sata_deinit(struct device *ahci_dev)
  559. {
  560. struct device *dev = ahci_dev->parent;
  561. msm_sata_phy_deinit(dev);
  562. msm_sata_vreg_deinit(dev);
  563. msm_sata_clk_deinit(dev);
  564. }
  565. static int msm_sata_suspend(struct device *ahci_dev)
  566. {
  567. msm_sata_deinit(ahci_dev);
  568. return 0;
  569. }
  570. static int msm_sata_resume(struct device *ahci_dev)
  571. {
  572. int ret;
  573. struct device *dev = ahci_dev->parent;
  574. ret = msm_sata_clk_init(dev);
  575. if (ret) {
  576. dev_err(dev, "SATA clk init failed with err=%d\n", ret);
  577. /*
  578. * If clock initialization failed, that means ahci driver
  579. * cannot access any register going further. Since there is
  580. * no check within ahci driver to check for clock failures,
  581. * panic here instead of making an unclocked register access.
  582. */
  583. BUG();
  584. }
  585. /* Issue asynchronous reset to reset PHY */
  586. ret = msm_sata_hard_reset(dev);
  587. if (ret)
  588. goto out;
  589. ret = msm_sata_vreg_init(dev);
  590. if (ret) {
  591. dev_err(dev, "SATA vreg init failed with err=%d\n", ret);
  592. /* Do not turn off clks, AHCI driver might do register access */
  593. goto out;
  594. }
  595. ret = msm_sata_phy_init(dev);
  596. if (ret) {
  597. dev_err(dev, "SATA PHY init failed with err=%d\n", ret);
  598. /* Do not turn off clks, AHCI driver might do register access */
  599. msm_sata_vreg_deinit(dev);
  600. goto out;
  601. }
  602. out:
  603. return ret;
  604. }
  605. static struct ahci_platform_data msm_ahci_pdata = {
  606. .init = msm_sata_init,
  607. .exit = msm_sata_deinit,
  608. .suspend = msm_sata_suspend,
  609. .resume = msm_sata_resume,
  610. };
  611. static int __devinit msm_sata_probe(struct platform_device *pdev)
  612. {
  613. struct platform_device *ahci;
  614. struct msm_sata_hba *hba;
  615. int ret = 0;
  616. hba = devm_kzalloc(&pdev->dev, sizeof(struct msm_sata_hba), GFP_KERNEL);
  617. if (!hba) {
  618. dev_err(&pdev->dev, "no memory\n");
  619. ret = -ENOMEM;
  620. goto err;
  621. }
  622. platform_set_drvdata(pdev, hba);
  623. ahci = platform_device_alloc("ahci", pdev->id);
  624. if (!ahci) {
  625. dev_err(&pdev->dev, "couldn't allocate ahci device\n");
  626. ret = -ENOMEM;
  627. goto err_free;
  628. }
  629. dma_set_coherent_mask(&ahci->dev, pdev->dev.coherent_dma_mask);
  630. ahci->dev.parent = &pdev->dev;
  631. ahci->dev.dma_mask = pdev->dev.dma_mask;
  632. ahci->dev.dma_parms = pdev->dev.dma_parms;
  633. hba->ahci_pdev = ahci;
  634. ret = platform_device_add_resources(ahci, pdev->resource,
  635. pdev->num_resources);
  636. if (ret) {
  637. dev_err(&pdev->dev, "couldn't add resources to ahci device\n");
  638. goto err_put_device;
  639. }
  640. ahci->dev.platform_data = &msm_ahci_pdata;
  641. ret = platform_device_add(ahci);
  642. if (ret) {
  643. dev_err(&pdev->dev, "failed to register ahci device\n");
  644. goto err_put_device;
  645. }
  646. return 0;
  647. err_put_device:
  648. platform_device_put(ahci);
  649. err_free:
  650. devm_kfree(&pdev->dev, hba);
  651. err:
  652. return ret;
  653. }
  654. static int __devexit msm_sata_remove(struct platform_device *pdev)
  655. {
  656. struct msm_sata_hba *hba = platform_get_drvdata(pdev);
  657. platform_device_unregister(hba->ahci_pdev);
  658. return 0;
  659. }
  660. static struct platform_driver msm_sata_driver = {
  661. .probe = msm_sata_probe,
  662. .remove = __devexit_p(msm_sata_remove),
  663. .driver = {
  664. .name = "msm_sata",
  665. },
  666. };
  667. module_platform_driver(msm_sata_driver);
  668. MODULE_LICENSE("GPL v2");
  669. MODULE_DESCRIPTION("AHCI platform MSM Glue Layer");