ahci_imx.c 23 KB

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