ahci_imx.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * copyright (c) 2013 Freescale Semiconductor, Inc.
  4. * Freescale IMX AHCI SATA platform driver
  5. *
  6. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/property.h>
  12. #include <linux/regmap.h>
  13. #include <linux/ahci_platform.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/of.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  18. #include <linux/libata.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/thermal.h>
  23. #include "ahci.h"
  24. #define DRV_NAME "ahci-imx"
  25. enum {
  26. /* Timer 1-ms Register */
  27. IMX_TIMER1MS = 0x00e0,
  28. /* Port0 PHY Control Register */
  29. IMX_P0PHYCR = 0x0178,
  30. IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
  31. IMX_P0PHYCR_CR_READ = 1 << 19,
  32. IMX_P0PHYCR_CR_WRITE = 1 << 18,
  33. IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
  34. IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
  35. /* Port0 PHY Status Register */
  36. IMX_P0PHYSR = 0x017c,
  37. IMX_P0PHYSR_CR_ACK = 1 << 18,
  38. IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
  39. /* Lane0 Output Status Register */
  40. IMX_LANE0_OUT_STAT = 0x2003,
  41. IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
  42. /* Clock Reset Register */
  43. IMX_CLOCK_RESET = 0x7f3f,
  44. IMX_CLOCK_RESET_RESET = 1 << 0,
  45. /* IMX8QM SATA specific control registers */
  46. IMX8QM_SATA_AHCI_PTC = 0xc8,
  47. IMX8QM_SATA_AHCI_PTC_RXWM_MASK = GENMASK(6, 0),
  48. IMX8QM_SATA_AHCI_PTC_RXWM = 0x29,
  49. };
  50. enum ahci_imx_type {
  51. AHCI_IMX53,
  52. AHCI_IMX6Q,
  53. AHCI_IMX6QP,
  54. AHCI_IMX8QM,
  55. };
  56. struct imx_ahci_priv {
  57. struct platform_device *ahci_pdev;
  58. enum ahci_imx_type type;
  59. struct clk *sata_clk;
  60. struct clk *sata_ref_clk;
  61. struct clk *ahb_clk;
  62. struct regmap *gpr;
  63. struct phy *sata_phy;
  64. struct phy *cali_phy0;
  65. struct phy *cali_phy1;
  66. bool no_device;
  67. bool first_time;
  68. u32 phy_params;
  69. u32 imped_ratio;
  70. };
  71. static int ahci_imx_hotplug;
  72. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  73. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  74. static void ahci_imx_host_stop(struct ata_host *host);
  75. static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
  76. {
  77. int timeout = 10;
  78. u32 crval;
  79. u32 srval;
  80. /* Assert or deassert the bit */
  81. crval = readl(mmio + IMX_P0PHYCR);
  82. if (assert)
  83. crval |= bit;
  84. else
  85. crval &= ~bit;
  86. writel(crval, mmio + IMX_P0PHYCR);
  87. /* Wait for the cr_ack signal */
  88. do {
  89. srval = readl(mmio + IMX_P0PHYSR);
  90. if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
  91. break;
  92. usleep_range(100, 200);
  93. } while (--timeout);
  94. return timeout ? 0 : -ETIMEDOUT;
  95. }
  96. static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
  97. {
  98. u32 crval = addr;
  99. int ret;
  100. /* Supply the address on cr_data_in */
  101. writel(crval, mmio + IMX_P0PHYCR);
  102. /* Assert the cr_cap_addr signal */
  103. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
  104. if (ret)
  105. return ret;
  106. /* Deassert cr_cap_addr */
  107. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
  108. if (ret)
  109. return ret;
  110. return 0;
  111. }
  112. static int imx_phy_reg_write(u16 val, void __iomem *mmio)
  113. {
  114. u32 crval = val;
  115. int ret;
  116. /* Supply the data on cr_data_in */
  117. writel(crval, mmio + IMX_P0PHYCR);
  118. /* Assert the cr_cap_data signal */
  119. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
  120. if (ret)
  121. return ret;
  122. /* Deassert cr_cap_data */
  123. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
  124. if (ret)
  125. return ret;
  126. if (val & IMX_CLOCK_RESET_RESET) {
  127. /*
  128. * In case we're resetting the phy, it's unable to acknowledge,
  129. * so we return immediately here.
  130. */
  131. crval |= IMX_P0PHYCR_CR_WRITE;
  132. writel(crval, mmio + IMX_P0PHYCR);
  133. goto out;
  134. }
  135. /* Assert the cr_write signal */
  136. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
  137. if (ret)
  138. return ret;
  139. /* Deassert cr_write */
  140. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
  141. if (ret)
  142. return ret;
  143. out:
  144. return 0;
  145. }
  146. static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
  147. {
  148. int ret;
  149. /* Assert the cr_read signal */
  150. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
  151. if (ret)
  152. return ret;
  153. /* Capture the data from cr_data_out[] */
  154. *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
  155. /* Deassert cr_read */
  156. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
  157. if (ret)
  158. return ret;
  159. return 0;
  160. }
  161. static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
  162. {
  163. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  164. void __iomem *mmio = hpriv->mmio;
  165. int timeout = 10;
  166. u16 val;
  167. int ret;
  168. if (imxpriv->type == AHCI_IMX6QP) {
  169. /* 6qp adds the sata reset mechanism, use it for 6qp sata */
  170. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  171. IMX6Q_GPR5_SATA_SW_PD, 0);
  172. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  173. IMX6Q_GPR5_SATA_SW_RST, 0);
  174. udelay(50);
  175. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  176. IMX6Q_GPR5_SATA_SW_RST,
  177. IMX6Q_GPR5_SATA_SW_RST);
  178. return 0;
  179. }
  180. /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
  181. ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
  182. if (ret)
  183. return ret;
  184. ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
  185. if (ret)
  186. return ret;
  187. /* Wait for PHY RX_PLL to be stable */
  188. do {
  189. usleep_range(100, 200);
  190. ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
  191. if (ret)
  192. return ret;
  193. ret = imx_phy_reg_read(&val, mmio);
  194. if (ret)
  195. return ret;
  196. if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
  197. break;
  198. } while (--timeout);
  199. return timeout ? 0 : -ETIMEDOUT;
  200. }
  201. enum {
  202. /* SATA PHY Register */
  203. SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT = 0x0001,
  204. SATA_PHY_CR_CLOCK_DAC_CTL = 0x0008,
  205. SATA_PHY_CR_CLOCK_RTUNE_CTL = 0x0009,
  206. SATA_PHY_CR_CLOCK_ADC_OUT = 0x000A,
  207. SATA_PHY_CR_CLOCK_MPLL_TST = 0x0017,
  208. };
  209. static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
  210. {
  211. u16 adc_out_reg, read_sum;
  212. u32 index, read_attempt;
  213. const u32 attempt_limit = 200;
  214. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  215. imx_phy_reg_write(rtune_ctl_reg, mmio);
  216. /* two dummy read */
  217. index = 0;
  218. read_attempt = 0;
  219. adc_out_reg = 0;
  220. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
  221. while (index < 2) {
  222. imx_phy_reg_read(&adc_out_reg, mmio);
  223. /* check if valid */
  224. if (adc_out_reg & 0x400)
  225. index++;
  226. read_attempt++;
  227. if (read_attempt > attempt_limit) {
  228. dev_err(dev, "Read REG more than %d times!\n",
  229. attempt_limit);
  230. break;
  231. }
  232. }
  233. index = 0;
  234. read_attempt = 0;
  235. read_sum = 0;
  236. while (index < 80) {
  237. imx_phy_reg_read(&adc_out_reg, mmio);
  238. if (adc_out_reg & 0x400) {
  239. read_sum = read_sum + (adc_out_reg & 0x3FF);
  240. index++;
  241. }
  242. read_attempt++;
  243. if (read_attempt > attempt_limit) {
  244. dev_err(dev, "Read REG more than %d times!\n",
  245. attempt_limit);
  246. break;
  247. }
  248. }
  249. /* Use the U32 to make 1000 precision */
  250. return (read_sum * 1000) / 80;
  251. }
  252. /* SATA AHCI temperature monitor */
  253. static int __sata_ahci_read_temperature(void *dev, int *temp)
  254. {
  255. u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
  256. u32 str1, str2, str3, str4;
  257. int m1, m2, a;
  258. struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
  259. void __iomem *mmio = hpriv->mmio;
  260. /* check rd-wr to reg */
  261. read_sum = 0;
  262. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT, mmio);
  263. imx_phy_reg_write(read_sum, mmio);
  264. imx_phy_reg_read(&read_sum, mmio);
  265. if ((read_sum & 0xffff) != 0)
  266. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  267. imx_phy_reg_write(0x5A5A, mmio);
  268. imx_phy_reg_read(&read_sum, mmio);
  269. if ((read_sum & 0xffff) != 0x5A5A)
  270. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  271. imx_phy_reg_write(0x1234, mmio);
  272. imx_phy_reg_read(&read_sum, mmio);
  273. if ((read_sum & 0xffff) != 0x1234)
  274. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  275. /* start temperature test */
  276. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  277. imx_phy_reg_read(&mpll_test_reg, mmio);
  278. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  279. imx_phy_reg_read(&rtune_ctl_reg, mmio);
  280. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  281. imx_phy_reg_read(&dac_ctl_reg, mmio);
  282. /* mpll_tst.meas_iv ([12:2]) */
  283. str1 = (mpll_test_reg >> 2) & 0x7FF;
  284. /* rtune_ctl.mode ([1:0]) */
  285. str2 = (rtune_ctl_reg) & 0x3;
  286. /* dac_ctl.dac_mode ([14:12]) */
  287. str3 = (dac_ctl_reg >> 12) & 0x7;
  288. /* rtune_ctl.sel_atbp ([4]) */
  289. str4 = (rtune_ctl_reg >> 4);
  290. /* Calculate the m1 */
  291. /* mpll_tst.meas_iv */
  292. mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
  293. /* rtune_ctl.mode */
  294. rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (1);
  295. /* dac_ctl.dac_mode */
  296. dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (4) << 12;
  297. /* rtune_ctl.sel_atbp */
  298. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (0) << 4;
  299. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  300. imx_phy_reg_write(mpll_test_reg, mmio);
  301. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  302. imx_phy_reg_write(dac_ctl_reg, mmio);
  303. m1 = read_adc_sum(dev, rtune_ctl_reg, mmio);
  304. /* Calculate the m2 */
  305. /* rtune_ctl.sel_atbp */
  306. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (1) << 4;
  307. m2 = read_adc_sum(dev, rtune_ctl_reg, mmio);
  308. /* restore the status */
  309. /* mpll_tst.meas_iv */
  310. mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
  311. /* rtune_ctl.mode */
  312. rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (str2);
  313. /* dac_ctl.dac_mode */
  314. dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (str3) << 12;
  315. /* rtune_ctl.sel_atbp */
  316. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (str4) << 4;
  317. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  318. imx_phy_reg_write(mpll_test_reg, mmio);
  319. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  320. imx_phy_reg_write(dac_ctl_reg, mmio);
  321. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  322. imx_phy_reg_write(rtune_ctl_reg, mmio);
  323. /* Compute temperature */
  324. if (!(m2 / 1000))
  325. m2 = 1000;
  326. a = (m2 - m1) / (m2/1000);
  327. *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
  328. return 0;
  329. }
  330. static int sata_ahci_read_temperature(struct thermal_zone_device *tz, int *temp)
  331. {
  332. return __sata_ahci_read_temperature(thermal_zone_device_priv(tz), temp);
  333. }
  334. static ssize_t sata_ahci_show_temp(struct device *dev,
  335. struct device_attribute *da,
  336. char *buf)
  337. {
  338. unsigned int temp = 0;
  339. int err;
  340. err = __sata_ahci_read_temperature(dev, &temp);
  341. if (err < 0)
  342. return err;
  343. return sprintf(buf, "%u\n", temp);
  344. }
  345. static const struct thermal_zone_device_ops fsl_sata_ahci_of_thermal_ops = {
  346. .get_temp = sata_ahci_read_temperature,
  347. };
  348. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
  349. static struct attribute *fsl_sata_ahci_attrs[] = {
  350. &sensor_dev_attr_temp1_input.dev_attr.attr,
  351. NULL
  352. };
  353. ATTRIBUTE_GROUPS(fsl_sata_ahci);
  354. static int imx8_sata_enable(struct ahci_host_priv *hpriv)
  355. {
  356. u32 val;
  357. int ret;
  358. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  359. struct device *dev = &imxpriv->ahci_pdev->dev;
  360. /*
  361. * Since "REXT" pin is only present for first lane of i.MX8QM
  362. * PHY, its calibration results will be stored, passed through
  363. * to the second lane PHY, and shared with all three lane PHYs.
  364. *
  365. * Initialize the first two lane PHYs here, although only the
  366. * third lane PHY is used by SATA.
  367. */
  368. ret = phy_init(imxpriv->cali_phy0);
  369. if (ret) {
  370. dev_err(dev, "cali PHY init failed\n");
  371. return ret;
  372. }
  373. ret = phy_power_on(imxpriv->cali_phy0);
  374. if (ret) {
  375. dev_err(dev, "cali PHY power on failed\n");
  376. goto err_cali_phy0_exit;
  377. }
  378. ret = phy_init(imxpriv->cali_phy1);
  379. if (ret) {
  380. dev_err(dev, "cali PHY1 init failed\n");
  381. goto err_cali_phy0_off;
  382. }
  383. ret = phy_power_on(imxpriv->cali_phy1);
  384. if (ret) {
  385. dev_err(dev, "cali PHY1 power on failed\n");
  386. goto err_cali_phy1_exit;
  387. }
  388. ret = phy_init(imxpriv->sata_phy);
  389. if (ret) {
  390. dev_err(dev, "sata PHY init failed\n");
  391. goto err_cali_phy1_off;
  392. }
  393. ret = phy_set_mode(imxpriv->sata_phy, PHY_MODE_SATA);
  394. if (ret) {
  395. dev_err(dev, "unable to set SATA PHY mode\n");
  396. goto err_sata_phy_exit;
  397. }
  398. ret = phy_power_on(imxpriv->sata_phy);
  399. if (ret) {
  400. dev_err(dev, "sata PHY power up failed\n");
  401. goto err_sata_phy_exit;
  402. }
  403. /* The cali_phy# can be turned off after SATA PHY is initialized. */
  404. phy_power_off(imxpriv->cali_phy1);
  405. phy_exit(imxpriv->cali_phy1);
  406. phy_power_off(imxpriv->cali_phy0);
  407. phy_exit(imxpriv->cali_phy0);
  408. /* RxWaterMark setting */
  409. val = readl(hpriv->mmio + IMX8QM_SATA_AHCI_PTC);
  410. val &= ~IMX8QM_SATA_AHCI_PTC_RXWM_MASK;
  411. val |= IMX8QM_SATA_AHCI_PTC_RXWM;
  412. writel(val, hpriv->mmio + IMX8QM_SATA_AHCI_PTC);
  413. return 0;
  414. err_sata_phy_exit:
  415. phy_exit(imxpriv->sata_phy);
  416. err_cali_phy1_off:
  417. phy_power_off(imxpriv->cali_phy1);
  418. err_cali_phy1_exit:
  419. phy_exit(imxpriv->cali_phy1);
  420. err_cali_phy0_off:
  421. phy_power_off(imxpriv->cali_phy0);
  422. err_cali_phy0_exit:
  423. phy_exit(imxpriv->cali_phy0);
  424. return ret;
  425. }
  426. static int imx_sata_enable(struct ahci_host_priv *hpriv)
  427. {
  428. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  429. struct device *dev = &imxpriv->ahci_pdev->dev;
  430. int ret;
  431. if (imxpriv->no_device)
  432. return 0;
  433. ret = ahci_platform_enable_regulators(hpriv);
  434. if (ret)
  435. return ret;
  436. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  437. if (ret < 0)
  438. goto disable_regulator;
  439. if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
  440. /*
  441. * set PHY Paremeters, two steps to configure the GPR13,
  442. * one write for rest of parameters, mask of first write
  443. * is 0x07ffffff, and the other one write for setting
  444. * the mpll_clk_en.
  445. */
  446. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  447. IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
  448. IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
  449. IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
  450. IMX6Q_GPR13_SATA_SPD_MODE_MASK |
  451. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  452. IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
  453. IMX6Q_GPR13_SATA_TX_BOOST_MASK |
  454. IMX6Q_GPR13_SATA_TX_LVL_MASK |
  455. IMX6Q_GPR13_SATA_MPLL_CLK_EN |
  456. IMX6Q_GPR13_SATA_TX_EDGE_RATE,
  457. imxpriv->phy_params);
  458. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  459. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  460. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  461. usleep_range(100, 200);
  462. ret = imx_sata_phy_reset(hpriv);
  463. if (ret) {
  464. dev_err(dev, "failed to reset phy: %d\n", ret);
  465. goto disable_clk;
  466. }
  467. } else if (imxpriv->type == AHCI_IMX8QM) {
  468. ret = imx8_sata_enable(hpriv);
  469. if (ret)
  470. goto disable_clk;
  471. }
  472. usleep_range(1000, 2000);
  473. return 0;
  474. disable_clk:
  475. clk_disable_unprepare(imxpriv->sata_ref_clk);
  476. disable_regulator:
  477. ahci_platform_disable_regulators(hpriv);
  478. return ret;
  479. }
  480. static void imx_sata_disable(struct ahci_host_priv *hpriv)
  481. {
  482. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  483. if (imxpriv->no_device)
  484. return;
  485. switch (imxpriv->type) {
  486. case AHCI_IMX6QP:
  487. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  488. IMX6Q_GPR5_SATA_SW_PD,
  489. IMX6Q_GPR5_SATA_SW_PD);
  490. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  491. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  492. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  493. break;
  494. case AHCI_IMX6Q:
  495. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  496. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  497. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  498. break;
  499. case AHCI_IMX8QM:
  500. if (imxpriv->sata_phy) {
  501. phy_power_off(imxpriv->sata_phy);
  502. phy_exit(imxpriv->sata_phy);
  503. }
  504. break;
  505. default:
  506. break;
  507. }
  508. clk_disable_unprepare(imxpriv->sata_ref_clk);
  509. ahci_platform_disable_regulators(hpriv);
  510. }
  511. static void ahci_imx_error_handler(struct ata_port *ap)
  512. {
  513. u32 reg_val;
  514. struct ata_device *dev;
  515. struct ata_host *host = dev_get_drvdata(ap->dev);
  516. struct ahci_host_priv *hpriv = host->private_data;
  517. void __iomem *mmio = hpriv->mmio;
  518. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  519. ahci_error_handler(ap);
  520. if (imxpriv->type == AHCI_IMX8QM)
  521. return;
  522. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  523. return;
  524. imxpriv->first_time = false;
  525. ata_for_each_dev(dev, &ap->link, ENABLED)
  526. return;
  527. /*
  528. * Disable link to save power. An imx ahci port can't be recovered
  529. * without full reset once the pddq mode is enabled making it
  530. * impossible to use as part of libata LPM.
  531. */
  532. reg_val = readl(mmio + IMX_P0PHYCR);
  533. writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
  534. imx_sata_disable(hpriv);
  535. imxpriv->no_device = true;
  536. dev_info(ap->dev, "no device found, disabling link.\n");
  537. dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
  538. }
  539. static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
  540. unsigned long deadline)
  541. {
  542. struct ata_port *ap = link->ap;
  543. struct ata_host *host = dev_get_drvdata(ap->dev);
  544. struct ahci_host_priv *hpriv = host->private_data;
  545. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  546. int ret;
  547. if (imxpriv->type == AHCI_IMX53)
  548. ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
  549. else
  550. ret = ahci_ops.softreset(link, class, deadline);
  551. return ret;
  552. }
  553. static struct ata_port_operations ahci_imx_ops = {
  554. .inherits = &ahci_ops,
  555. .host_stop = ahci_imx_host_stop,
  556. .error_handler = ahci_imx_error_handler,
  557. .softreset = ahci_imx_softreset,
  558. };
  559. static const struct ata_port_info ahci_imx_port_info = {
  560. .flags = AHCI_FLAG_COMMON,
  561. .pio_mask = ATA_PIO4,
  562. .udma_mask = ATA_UDMA6,
  563. .port_ops = &ahci_imx_ops,
  564. };
  565. static const struct of_device_id imx_ahci_of_match[] = {
  566. { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
  567. { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
  568. { .compatible = "fsl,imx6qp-ahci", .data = (void *)AHCI_IMX6QP },
  569. { .compatible = "fsl,imx8qm-ahci", .data = (void *)AHCI_IMX8QM },
  570. { /* sentinel */ }
  571. };
  572. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  573. struct reg_value {
  574. u32 of_value;
  575. u32 reg_value;
  576. };
  577. struct reg_property {
  578. const char *name;
  579. const struct reg_value *values;
  580. size_t num_values;
  581. u32 def_value;
  582. u32 set_value;
  583. };
  584. static const struct reg_value gpr13_tx_level[] = {
  585. { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
  586. { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
  587. { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
  588. { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
  589. { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
  590. { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
  591. { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
  592. { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
  593. { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
  594. { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
  595. { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
  596. { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
  597. { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
  598. { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
  599. { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
  600. { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
  601. { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
  602. { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
  603. { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
  604. { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
  605. { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
  606. { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
  607. { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
  608. { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
  609. { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
  610. { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
  611. { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
  612. { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
  613. { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
  614. { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
  615. { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
  616. { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
  617. };
  618. static const struct reg_value gpr13_tx_boost[] = {
  619. { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
  620. { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
  621. { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
  622. { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
  623. { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
  624. { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
  625. { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
  626. { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
  627. { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
  628. { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
  629. { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
  630. { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
  631. { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
  632. { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
  633. { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
  634. { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
  635. };
  636. static const struct reg_value gpr13_tx_atten[] = {
  637. { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
  638. { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
  639. { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
  640. { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
  641. { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
  642. { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
  643. };
  644. static const struct reg_value gpr13_rx_eq[] = {
  645. { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
  646. { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
  647. { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
  648. { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
  649. { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
  650. { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
  651. { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
  652. { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
  653. };
  654. static const struct reg_property gpr13_props[] = {
  655. {
  656. .name = "fsl,transmit-level-mV",
  657. .values = gpr13_tx_level,
  658. .num_values = ARRAY_SIZE(gpr13_tx_level),
  659. .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
  660. }, {
  661. .name = "fsl,transmit-boost-mdB",
  662. .values = gpr13_tx_boost,
  663. .num_values = ARRAY_SIZE(gpr13_tx_boost),
  664. .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
  665. }, {
  666. .name = "fsl,transmit-atten-16ths",
  667. .values = gpr13_tx_atten,
  668. .num_values = ARRAY_SIZE(gpr13_tx_atten),
  669. .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
  670. }, {
  671. .name = "fsl,receive-eq-mdB",
  672. .values = gpr13_rx_eq,
  673. .num_values = ARRAY_SIZE(gpr13_rx_eq),
  674. .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
  675. }, {
  676. .name = "fsl,no-spread-spectrum",
  677. .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
  678. .set_value = 0,
  679. },
  680. };
  681. static u32 imx_ahci_parse_props(struct device *dev,
  682. const struct reg_property *prop, size_t num)
  683. {
  684. struct device_node *np = dev->of_node;
  685. u32 reg_value = 0;
  686. int i, j;
  687. for (i = 0; i < num; i++, prop++) {
  688. u32 of_val;
  689. if (prop->num_values == 0) {
  690. if (of_property_read_bool(np, prop->name))
  691. reg_value |= prop->set_value;
  692. else
  693. reg_value |= prop->def_value;
  694. continue;
  695. }
  696. if (of_property_read_u32(np, prop->name, &of_val)) {
  697. dev_info(dev, "%s not specified, using %08x\n",
  698. prop->name, prop->def_value);
  699. reg_value |= prop->def_value;
  700. continue;
  701. }
  702. for (j = 0; j < prop->num_values; j++) {
  703. if (prop->values[j].of_value == of_val) {
  704. dev_info(dev, "%s value %u, using %08x\n",
  705. prop->name, of_val, prop->values[j].reg_value);
  706. reg_value |= prop->values[j].reg_value;
  707. break;
  708. }
  709. }
  710. if (j == prop->num_values) {
  711. dev_err(dev, "DT property %s is not a valid value\n",
  712. prop->name);
  713. reg_value |= prop->def_value;
  714. }
  715. }
  716. return reg_value;
  717. }
  718. static const struct scsi_host_template ahci_platform_sht = {
  719. AHCI_SHT(DRV_NAME),
  720. };
  721. static int imx8_sata_probe(struct device *dev, struct imx_ahci_priv *imxpriv)
  722. {
  723. imxpriv->sata_phy = devm_phy_get(dev, "sata-phy");
  724. if (IS_ERR(imxpriv->sata_phy))
  725. return dev_err_probe(dev, PTR_ERR(imxpriv->sata_phy),
  726. "Failed to get sata_phy\n");
  727. imxpriv->cali_phy0 = devm_phy_get(dev, "cali-phy0");
  728. if (IS_ERR(imxpriv->cali_phy0))
  729. return dev_err_probe(dev, PTR_ERR(imxpriv->cali_phy0),
  730. "Failed to get cali_phy0\n");
  731. imxpriv->cali_phy1 = devm_phy_get(dev, "cali-phy1");
  732. if (IS_ERR(imxpriv->cali_phy1))
  733. return dev_err_probe(dev, PTR_ERR(imxpriv->cali_phy1),
  734. "Failed to get cali_phy1\n");
  735. return 0;
  736. }
  737. static int imx_ahci_probe(struct platform_device *pdev)
  738. {
  739. struct device *dev = &pdev->dev;
  740. struct ahci_host_priv *hpriv;
  741. struct imx_ahci_priv *imxpriv;
  742. unsigned int reg_val;
  743. int ret;
  744. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  745. if (!imxpriv)
  746. return -ENOMEM;
  747. imxpriv->ahci_pdev = pdev;
  748. imxpriv->no_device = false;
  749. imxpriv->first_time = true;
  750. imxpriv->type = (enum ahci_imx_type)device_get_match_data(dev);
  751. imxpriv->sata_clk = devm_clk_get(dev, "sata");
  752. if (IS_ERR(imxpriv->sata_clk)) {
  753. dev_err(dev, "can't get sata clock.\n");
  754. return PTR_ERR(imxpriv->sata_clk);
  755. }
  756. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  757. if (IS_ERR(imxpriv->sata_ref_clk)) {
  758. dev_err(dev, "can't get sata_ref clock.\n");
  759. return PTR_ERR(imxpriv->sata_ref_clk);
  760. }
  761. if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
  762. u32 reg_value;
  763. imxpriv->gpr = syscon_regmap_lookup_by_compatible(
  764. "fsl,imx6q-iomuxc-gpr");
  765. if (IS_ERR(imxpriv->gpr)) {
  766. dev_err(dev,
  767. "failed to find fsl,imx6q-iomux-gpr regmap\n");
  768. return PTR_ERR(imxpriv->gpr);
  769. }
  770. reg_value = imx_ahci_parse_props(dev, gpr13_props,
  771. ARRAY_SIZE(gpr13_props));
  772. imxpriv->phy_params =
  773. IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
  774. IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
  775. IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
  776. reg_value;
  777. } else if (imxpriv->type == AHCI_IMX8QM) {
  778. ret = imx8_sata_probe(dev, imxpriv);
  779. if (ret)
  780. return ret;
  781. }
  782. hpriv = ahci_platform_get_resources(pdev, 0);
  783. if (IS_ERR(hpriv))
  784. return PTR_ERR(hpriv);
  785. hpriv->plat_data = imxpriv;
  786. ret = clk_prepare_enable(imxpriv->sata_clk);
  787. if (ret)
  788. return ret;
  789. if (imxpriv->type == AHCI_IMX53 &&
  790. IS_ENABLED(CONFIG_HWMON)) {
  791. /* Add the temperature monitor */
  792. struct device *hwmon_dev;
  793. hwmon_dev =
  794. devm_hwmon_device_register_with_groups(dev,
  795. "sata_ahci",
  796. hpriv,
  797. fsl_sata_ahci_groups);
  798. if (IS_ERR(hwmon_dev)) {
  799. ret = PTR_ERR(hwmon_dev);
  800. goto disable_clk;
  801. }
  802. devm_thermal_of_zone_register(hwmon_dev, 0, hwmon_dev,
  803. &fsl_sata_ahci_of_thermal_ops);
  804. dev_info(dev, "%s: sensor 'sata_ahci'\n", dev_name(hwmon_dev));
  805. }
  806. ret = imx_sata_enable(hpriv);
  807. if (ret)
  808. goto disable_clk;
  809. /*
  810. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL.
  811. * Set CAP_SSS (support stagered spin up) and Implement the port0.
  812. */
  813. reg_val = readl(hpriv->mmio + HOST_CAP);
  814. if (!(reg_val & HOST_CAP_SSS)) {
  815. reg_val |= HOST_CAP_SSS;
  816. writel(reg_val, hpriv->mmio + HOST_CAP);
  817. }
  818. reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
  819. if (!(reg_val & 0x1)) {
  820. reg_val |= 0x1;
  821. writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
  822. }
  823. if (imxpriv->type != AHCI_IMX8QM) {
  824. /*
  825. * Get AHB clock rate and configure the vendor specified
  826. * TIMER1MS register on i.MX53, i.MX6Q and i.MX6QP only.
  827. */
  828. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  829. if (IS_ERR(imxpriv->ahb_clk)) {
  830. dev_err(dev, "Failed to get ahb clock\n");
  831. ret = PTR_ERR(imxpriv->ahb_clk);
  832. goto disable_sata;
  833. }
  834. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  835. writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
  836. }
  837. ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
  838. &ahci_platform_sht);
  839. if (ret)
  840. goto disable_sata;
  841. return 0;
  842. disable_sata:
  843. imx_sata_disable(hpriv);
  844. disable_clk:
  845. clk_disable_unprepare(imxpriv->sata_clk);
  846. return ret;
  847. }
  848. static void ahci_imx_host_stop(struct ata_host *host)
  849. {
  850. struct ahci_host_priv *hpriv = host->private_data;
  851. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  852. imx_sata_disable(hpriv);
  853. clk_disable_unprepare(imxpriv->sata_clk);
  854. }
  855. #ifdef CONFIG_PM_SLEEP
  856. static int imx_ahci_suspend(struct device *dev)
  857. {
  858. struct ata_host *host = dev_get_drvdata(dev);
  859. struct ahci_host_priv *hpriv = host->private_data;
  860. int ret;
  861. ret = ahci_platform_suspend_host(dev);
  862. if (ret)
  863. return ret;
  864. imx_sata_disable(hpriv);
  865. return 0;
  866. }
  867. static int imx_ahci_resume(struct device *dev)
  868. {
  869. struct ata_host *host = dev_get_drvdata(dev);
  870. struct ahci_host_priv *hpriv = host->private_data;
  871. int ret;
  872. ret = imx_sata_enable(hpriv);
  873. if (ret)
  874. return ret;
  875. return ahci_platform_resume_host(dev);
  876. }
  877. #endif
  878. static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
  879. static struct platform_driver imx_ahci_driver = {
  880. .probe = imx_ahci_probe,
  881. .remove_new = ata_platform_remove_one,
  882. .driver = {
  883. .name = DRV_NAME,
  884. .of_match_table = imx_ahci_of_match,
  885. .pm = &ahci_imx_pm_ops,
  886. },
  887. };
  888. module_platform_driver(imx_ahci_driver);
  889. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  890. MODULE_AUTHOR("Richard Zhu <hongxing.zhu@nxp.com>");
  891. MODULE_LICENSE("GPL");
  892. MODULE_ALIAS("platform:" DRV_NAME);