1 // SPDX-License-Identifier: GPL-2.0-only
3 * copyright (c) 2013 Freescale Semiconductor, Inc.
4 * Freescale IMX AHCI SATA platform driver
6 * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/regmap.h>
14 #include <linux/ahci_platform.h>
15 #include <linux/gpio/consumer.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
19 #include <linux/libata.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/phy/phy.h>
23 #include <linux/thermal.h>
26 #define DRV_NAME "ahci-imx"
29 /* Timer 1-ms Register */
30 IMX_TIMER1MS = 0x00e0,
31 /* Port0 PHY Control Register */
33 IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
34 IMX_P0PHYCR_CR_READ = 1 << 19,
35 IMX_P0PHYCR_CR_WRITE = 1 << 18,
36 IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
37 IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
38 /* Port0 PHY Status Register */
40 IMX_P0PHYSR_CR_ACK = 1 << 18,
41 IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
42 /* Lane0 Output Status Register */
43 IMX_LANE0_OUT_STAT = 0x2003,
44 IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
45 /* Clock Reset Register */
46 IMX_CLOCK_RESET = 0x7f3f,
47 IMX_CLOCK_RESET_RESET = 1 << 0,
48 /* IMX8QM SATA specific control registers */
49 IMX8QM_SATA_AHCI_PTC = 0xc8,
50 IMX8QM_SATA_AHCI_PTC_RXWM_MASK = GENMASK(6, 0),
51 IMX8QM_SATA_AHCI_PTC_RXWM = 0x29,
61 struct imx_ahci_priv {
62 struct platform_device *ahci_pdev;
63 enum ahci_imx_type type;
65 struct clk *sata_ref_clk;
69 struct phy *cali_phy0;
70 struct phy *cali_phy1;
77 static int ahci_imx_hotplug;
78 module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
79 MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
81 static void ahci_imx_host_stop(struct ata_host *host);
83 static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
89 /* Assert or deassert the bit */
90 crval = readl(mmio + IMX_P0PHYCR);
95 writel(crval, mmio + IMX_P0PHYCR);
97 /* Wait for the cr_ack signal */
99 srval = readl(mmio + IMX_P0PHYSR);
100 if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
102 usleep_range(100, 200);
105 return timeout ? 0 : -ETIMEDOUT;
108 static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
113 /* Supply the address on cr_data_in */
114 writel(crval, mmio + IMX_P0PHYCR);
116 /* Assert the cr_cap_addr signal */
117 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
121 /* Deassert cr_cap_addr */
122 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
129 static int imx_phy_reg_write(u16 val, void __iomem *mmio)
134 /* Supply the data on cr_data_in */
135 writel(crval, mmio + IMX_P0PHYCR);
137 /* Assert the cr_cap_data signal */
138 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
142 /* Deassert cr_cap_data */
143 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
147 if (val & IMX_CLOCK_RESET_RESET) {
149 * In case we're resetting the phy, it's unable to acknowledge,
150 * so we return immediately here.
152 crval |= IMX_P0PHYCR_CR_WRITE;
153 writel(crval, mmio + IMX_P0PHYCR);
157 /* Assert the cr_write signal */
158 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
162 /* Deassert cr_write */
163 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
171 static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
175 /* Assert the cr_read signal */
176 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
180 /* Capture the data from cr_data_out[] */
181 *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
183 /* Deassert cr_read */
184 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
191 static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
193 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
194 void __iomem *mmio = hpriv->mmio;
199 if (imxpriv->type == AHCI_IMX6QP) {
200 /* 6qp adds the sata reset mechanism, use it for 6qp sata */
201 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
202 IMX6Q_GPR5_SATA_SW_PD, 0);
204 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
205 IMX6Q_GPR5_SATA_SW_RST, 0);
207 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
208 IMX6Q_GPR5_SATA_SW_RST,
209 IMX6Q_GPR5_SATA_SW_RST);
213 /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
214 ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
217 ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
221 /* Wait for PHY RX_PLL to be stable */
223 usleep_range(100, 200);
224 ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
227 ret = imx_phy_reg_read(&val, mmio);
230 if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
234 return timeout ? 0 : -ETIMEDOUT;
238 /* SATA PHY Register */
239 SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT = 0x0001,
240 SATA_PHY_CR_CLOCK_DAC_CTL = 0x0008,
241 SATA_PHY_CR_CLOCK_RTUNE_CTL = 0x0009,
242 SATA_PHY_CR_CLOCK_ADC_OUT = 0x000A,
243 SATA_PHY_CR_CLOCK_MPLL_TST = 0x0017,
246 static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
248 u16 adc_out_reg, read_sum;
249 u32 index, read_attempt;
250 const u32 attempt_limit = 200;
252 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
253 imx_phy_reg_write(rtune_ctl_reg, mmio);
259 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
261 imx_phy_reg_read(&adc_out_reg, mmio);
263 if (adc_out_reg & 0x400)
267 if (read_attempt > attempt_limit) {
268 dev_err(dev, "Read REG more than %d times!\n",
278 imx_phy_reg_read(&adc_out_reg, mmio);
279 if (adc_out_reg & 0x400) {
280 read_sum = read_sum + (adc_out_reg & 0x3FF);
284 if (read_attempt > attempt_limit) {
285 dev_err(dev, "Read REG more than %d times!\n",
291 /* Use the U32 to make 1000 precision */
292 return (read_sum * 1000) / 80;
295 /* SATA AHCI temperature monitor */
296 static int __sata_ahci_read_temperature(void *dev, int *temp)
298 u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
299 u32 str1, str2, str3, str4;
301 struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
302 void __iomem *mmio = hpriv->mmio;
304 /* check rd-wr to reg */
306 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT, mmio);
307 imx_phy_reg_write(read_sum, mmio);
308 imx_phy_reg_read(&read_sum, mmio);
309 if ((read_sum & 0xffff) != 0)
310 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
312 imx_phy_reg_write(0x5A5A, mmio);
313 imx_phy_reg_read(&read_sum, mmio);
314 if ((read_sum & 0xffff) != 0x5A5A)
315 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
317 imx_phy_reg_write(0x1234, mmio);
318 imx_phy_reg_read(&read_sum, mmio);
319 if ((read_sum & 0xffff) != 0x1234)
320 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
322 /* start temperature test */
323 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
324 imx_phy_reg_read(&mpll_test_reg, mmio);
325 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
326 imx_phy_reg_read(&rtune_ctl_reg, mmio);
327 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
328 imx_phy_reg_read(&dac_ctl_reg, mmio);
330 /* mpll_tst.meas_iv ([12:2]) */
331 str1 = (mpll_test_reg >> 2) & 0x7FF;
332 /* rtune_ctl.mode ([1:0]) */
333 str2 = (rtune_ctl_reg) & 0x3;
334 /* dac_ctl.dac_mode ([14:12]) */
335 str3 = (dac_ctl_reg >> 12) & 0x7;
336 /* rtune_ctl.sel_atbp ([4]) */
337 str4 = (rtune_ctl_reg >> 4);
339 /* Calculate the m1 */
340 /* mpll_tst.meas_iv */
341 mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
343 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (1);
344 /* dac_ctl.dac_mode */
345 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (4) << 12;
346 /* rtune_ctl.sel_atbp */
347 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (0) << 4;
348 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
349 imx_phy_reg_write(mpll_test_reg, mmio);
350 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
351 imx_phy_reg_write(dac_ctl_reg, mmio);
352 m1 = read_adc_sum(dev, rtune_ctl_reg, mmio);
354 /* Calculate the m2 */
355 /* rtune_ctl.sel_atbp */
356 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (1) << 4;
357 m2 = read_adc_sum(dev, rtune_ctl_reg, mmio);
359 /* restore the status */
360 /* mpll_tst.meas_iv */
361 mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
363 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (str2);
364 /* dac_ctl.dac_mode */
365 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (str3) << 12;
366 /* rtune_ctl.sel_atbp */
367 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (str4) << 4;
369 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
370 imx_phy_reg_write(mpll_test_reg, mmio);
371 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
372 imx_phy_reg_write(dac_ctl_reg, mmio);
373 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
374 imx_phy_reg_write(rtune_ctl_reg, mmio);
376 /* Compute temperature */
379 a = (m2 - m1) / (m2/1000);
380 *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
385 static int sata_ahci_read_temperature(struct thermal_zone_device *tz, int *temp)
387 return __sata_ahci_read_temperature(thermal_zone_device_priv(tz), temp);
390 static ssize_t sata_ahci_show_temp(struct device *dev,
391 struct device_attribute *da,
394 unsigned int temp = 0;
397 err = __sata_ahci_read_temperature(dev, &temp);
401 return sprintf(buf, "%u\n", temp);
404 static const struct thermal_zone_device_ops fsl_sata_ahci_of_thermal_ops = {
405 .get_temp = sata_ahci_read_temperature,
408 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
410 static struct attribute *fsl_sata_ahci_attrs[] = {
411 &sensor_dev_attr_temp1_input.dev_attr.attr,
414 ATTRIBUTE_GROUPS(fsl_sata_ahci);
416 static int imx8_sata_enable(struct ahci_host_priv *hpriv)
420 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
421 struct device *dev = &imxpriv->ahci_pdev->dev;
424 * Since "REXT" pin is only present for first lane of i.MX8QM
425 * PHY, its calibration results will be stored, passed through
426 * to the second lane PHY, and shared with all three lane PHYs.
428 * Initialize the first two lane PHYs here, although only the
429 * third lane PHY is used by SATA.
431 ret = phy_init(imxpriv->cali_phy0);
433 dev_err(dev, "cali PHY init failed\n");
436 ret = phy_power_on(imxpriv->cali_phy0);
438 dev_err(dev, "cali PHY power on failed\n");
439 goto err_cali_phy0_exit;
441 ret = phy_init(imxpriv->cali_phy1);
443 dev_err(dev, "cali PHY1 init failed\n");
444 goto err_cali_phy0_off;
446 ret = phy_power_on(imxpriv->cali_phy1);
448 dev_err(dev, "cali PHY1 power on failed\n");
449 goto err_cali_phy1_exit;
451 ret = phy_init(imxpriv->sata_phy);
453 dev_err(dev, "sata PHY init failed\n");
454 goto err_cali_phy1_off;
456 ret = phy_set_mode(imxpriv->sata_phy, PHY_MODE_SATA);
458 dev_err(dev, "unable to set SATA PHY mode\n");
459 goto err_sata_phy_exit;
461 ret = phy_power_on(imxpriv->sata_phy);
463 dev_err(dev, "sata PHY power up failed\n");
464 goto err_sata_phy_exit;
467 /* The cali_phy# can be turned off after SATA PHY is initialized. */
468 phy_power_off(imxpriv->cali_phy1);
469 phy_exit(imxpriv->cali_phy1);
470 phy_power_off(imxpriv->cali_phy0);
471 phy_exit(imxpriv->cali_phy0);
473 /* RxWaterMark setting */
474 val = readl(hpriv->mmio + IMX8QM_SATA_AHCI_PTC);
475 val &= ~IMX8QM_SATA_AHCI_PTC_RXWM_MASK;
476 val |= IMX8QM_SATA_AHCI_PTC_RXWM;
477 writel(val, hpriv->mmio + IMX8QM_SATA_AHCI_PTC);
482 phy_exit(imxpriv->sata_phy);
484 phy_power_off(imxpriv->cali_phy1);
486 phy_exit(imxpriv->cali_phy1);
488 phy_power_off(imxpriv->cali_phy0);
490 phy_exit(imxpriv->cali_phy0);
495 static int imx_sata_enable(struct ahci_host_priv *hpriv)
497 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
498 struct device *dev = &imxpriv->ahci_pdev->dev;
501 if (imxpriv->no_device)
504 ret = ahci_platform_enable_regulators(hpriv);
508 ret = clk_prepare_enable(imxpriv->sata_ref_clk);
510 goto disable_regulator;
512 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
514 * set PHY Parameters, two steps to configure the GPR13,
515 * one write for rest of parameters, mask of first write
516 * is 0x07ffffff, and the other one write for setting
519 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
520 IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
521 IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
522 IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
523 IMX6Q_GPR13_SATA_SPD_MODE_MASK |
524 IMX6Q_GPR13_SATA_MPLL_SS_EN |
525 IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
526 IMX6Q_GPR13_SATA_TX_BOOST_MASK |
527 IMX6Q_GPR13_SATA_TX_LVL_MASK |
528 IMX6Q_GPR13_SATA_MPLL_CLK_EN |
529 IMX6Q_GPR13_SATA_TX_EDGE_RATE,
530 imxpriv->phy_params);
531 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
532 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
533 IMX6Q_GPR13_SATA_MPLL_CLK_EN);
535 usleep_range(100, 200);
537 ret = imx_sata_phy_reset(hpriv);
539 dev_err(dev, "failed to reset phy: %d\n", ret);
542 } else if (imxpriv->type == AHCI_IMX8QM) {
543 ret = imx8_sata_enable(hpriv);
549 usleep_range(1000, 2000);
554 clk_disable_unprepare(imxpriv->sata_ref_clk);
556 ahci_platform_disable_regulators(hpriv);
561 static void imx_sata_disable(struct ahci_host_priv *hpriv)
563 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
565 if (imxpriv->no_device)
568 switch (imxpriv->type) {
570 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
571 IMX6Q_GPR5_SATA_SW_PD,
572 IMX6Q_GPR5_SATA_SW_PD);
573 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
574 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
575 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
579 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
580 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
581 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
585 if (imxpriv->sata_phy) {
586 phy_power_off(imxpriv->sata_phy);
587 phy_exit(imxpriv->sata_phy);
595 clk_disable_unprepare(imxpriv->sata_ref_clk);
597 ahci_platform_disable_regulators(hpriv);
600 static void ahci_imx_error_handler(struct ata_port *ap)
603 struct ata_device *dev;
604 struct ata_host *host = dev_get_drvdata(ap->dev);
605 struct ahci_host_priv *hpriv = host->private_data;
606 void __iomem *mmio = hpriv->mmio;
607 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
609 ahci_error_handler(ap);
611 if (imxpriv->type == AHCI_IMX8QM)
614 if (!(imxpriv->first_time) || ahci_imx_hotplug)
617 imxpriv->first_time = false;
619 ata_for_each_dev(dev, &ap->link, ENABLED)
622 * Disable link to save power. An imx ahci port can't be recovered
623 * without full reset once the pddq mode is enabled making it
624 * impossible to use as part of libata LPM.
626 reg_val = readl(mmio + IMX_P0PHYCR);
627 writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
628 imx_sata_disable(hpriv);
629 imxpriv->no_device = true;
631 dev_info(ap->dev, "no device found, disabling link.\n");
632 dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
635 static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
636 unsigned long deadline)
638 struct ata_port *ap = link->ap;
639 struct ata_host *host = dev_get_drvdata(ap->dev);
640 struct ahci_host_priv *hpriv = host->private_data;
641 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
644 if (imxpriv->type == AHCI_IMX53)
645 ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
647 ret = ahci_ops.softreset(link, class, deadline);
652 static struct ata_port_operations ahci_imx_ops = {
653 .inherits = &ahci_ops,
654 .host_stop = ahci_imx_host_stop,
655 .error_handler = ahci_imx_error_handler,
656 .softreset = ahci_imx_softreset,
659 static const struct ata_port_info ahci_imx_port_info = {
660 .flags = AHCI_FLAG_COMMON,
661 .pio_mask = ATA_PIO4,
662 .udma_mask = ATA_UDMA6,
663 .port_ops = &ahci_imx_ops,
666 static const struct of_device_id imx_ahci_of_match[] = {
667 { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
668 { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
669 { .compatible = "fsl,imx6qp-ahci", .data = (void *)AHCI_IMX6QP },
670 { .compatible = "fsl,imx8qm-ahci", .data = (void *)AHCI_IMX8QM },
673 MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
680 struct reg_property {
682 const struct reg_value *values;
688 static const struct reg_value gpr13_tx_level[] = {
689 { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
690 { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
691 { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
692 { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
693 { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
694 { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
695 { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
696 { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
697 { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
698 { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
699 { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
700 { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
701 { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
702 { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
703 { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
704 { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
705 { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
706 { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
707 { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
708 { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
709 { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
710 { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
711 { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
712 { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
713 { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
714 { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
715 { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
716 { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
717 { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
718 { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
719 { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
720 { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
723 static const struct reg_value gpr13_tx_boost[] = {
724 { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
725 { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
726 { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
727 { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
728 { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
729 { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
730 { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
731 { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
732 { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
733 { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
734 { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
735 { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
736 { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
737 { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
738 { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
739 { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
742 static const struct reg_value gpr13_tx_atten[] = {
743 { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
744 { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
745 { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
746 { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
747 { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
748 { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
751 static const struct reg_value gpr13_rx_eq[] = {
752 { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
753 { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
754 { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
755 { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
756 { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
757 { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
758 { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
759 { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
762 static const struct reg_property gpr13_props[] = {
764 .name = "fsl,transmit-level-mV",
765 .values = gpr13_tx_level,
766 .num_values = ARRAY_SIZE(gpr13_tx_level),
767 .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
769 .name = "fsl,transmit-boost-mdB",
770 .values = gpr13_tx_boost,
771 .num_values = ARRAY_SIZE(gpr13_tx_boost),
772 .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
774 .name = "fsl,transmit-atten-16ths",
775 .values = gpr13_tx_atten,
776 .num_values = ARRAY_SIZE(gpr13_tx_atten),
777 .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
779 .name = "fsl,receive-eq-mdB",
780 .values = gpr13_rx_eq,
781 .num_values = ARRAY_SIZE(gpr13_rx_eq),
782 .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
784 .name = "fsl,no-spread-spectrum",
785 .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
790 static u32 imx_ahci_parse_props(struct device *dev,
791 const struct reg_property *prop, size_t num)
793 struct device_node *np = dev->of_node;
797 for (i = 0; i < num; i++, prop++) {
800 if (prop->num_values == 0) {
801 if (of_property_read_bool(np, prop->name))
802 reg_value |= prop->set_value;
804 reg_value |= prop->def_value;
808 if (of_property_read_u32(np, prop->name, &of_val)) {
809 dev_info(dev, "%s not specified, using %08x\n",
810 prop->name, prop->def_value);
811 reg_value |= prop->def_value;
815 for (j = 0; j < prop->num_values; j++) {
816 if (prop->values[j].of_value == of_val) {
817 dev_info(dev, "%s value %u, using %08x\n",
818 prop->name, of_val, prop->values[j].reg_value);
819 reg_value |= prop->values[j].reg_value;
824 if (j == prop->num_values) {
825 dev_err(dev, "DT property %s is not a valid value\n",
827 reg_value |= prop->def_value;
834 static const struct scsi_host_template ahci_platform_sht = {
838 static int imx8_sata_probe(struct device *dev, struct imx_ahci_priv *imxpriv)
840 imxpriv->sata_phy = devm_phy_get(dev, "sata-phy");
841 if (IS_ERR(imxpriv->sata_phy))
842 return dev_err_probe(dev, PTR_ERR(imxpriv->sata_phy),
843 "Failed to get sata_phy\n");
845 imxpriv->cali_phy0 = devm_phy_get(dev, "cali-phy0");
846 if (IS_ERR(imxpriv->cali_phy0))
847 return dev_err_probe(dev, PTR_ERR(imxpriv->cali_phy0),
848 "Failed to get cali_phy0\n");
849 imxpriv->cali_phy1 = devm_phy_get(dev, "cali-phy1");
850 if (IS_ERR(imxpriv->cali_phy1))
851 return dev_err_probe(dev, PTR_ERR(imxpriv->cali_phy1),
852 "Failed to get cali_phy1\n");
856 static int imx_ahci_probe(struct platform_device *pdev)
858 struct device *dev = &pdev->dev;
859 struct ahci_host_priv *hpriv;
860 struct imx_ahci_priv *imxpriv;
861 unsigned int reg_val;
864 imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
868 imxpriv->ahci_pdev = pdev;
869 imxpriv->no_device = false;
870 imxpriv->first_time = true;
871 imxpriv->type = (enum ahci_imx_type)device_get_match_data(dev);
873 imxpriv->sata_clk = devm_clk_get(dev, "sata");
874 if (IS_ERR(imxpriv->sata_clk)) {
875 dev_err(dev, "can't get sata clock.\n");
876 return PTR_ERR(imxpriv->sata_clk);
879 imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
880 if (IS_ERR(imxpriv->sata_ref_clk)) {
881 dev_err(dev, "can't get sata_ref clock.\n");
882 return PTR_ERR(imxpriv->sata_ref_clk);
885 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
888 imxpriv->gpr = syscon_regmap_lookup_by_compatible(
889 "fsl,imx6q-iomuxc-gpr");
890 if (IS_ERR(imxpriv->gpr)) {
892 "failed to find fsl,imx6q-iomux-gpr regmap\n");
893 return PTR_ERR(imxpriv->gpr);
896 reg_value = imx_ahci_parse_props(dev, gpr13_props,
897 ARRAY_SIZE(gpr13_props));
899 imxpriv->phy_params =
900 IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
901 IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
902 IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
904 } else if (imxpriv->type == AHCI_IMX8QM) {
905 ret = imx8_sata_probe(dev, imxpriv);
910 hpriv = ahci_platform_get_resources(pdev, 0);
912 return PTR_ERR(hpriv);
914 hpriv->plat_data = imxpriv;
916 ret = clk_prepare_enable(imxpriv->sata_clk);
920 if (imxpriv->type == AHCI_IMX53 &&
921 IS_ENABLED(CONFIG_HWMON)) {
922 /* Add the temperature monitor */
923 struct device *hwmon_dev;
926 devm_hwmon_device_register_with_groups(dev,
929 fsl_sata_ahci_groups);
930 if (IS_ERR(hwmon_dev)) {
931 ret = PTR_ERR(hwmon_dev);
934 devm_thermal_of_zone_register(hwmon_dev, 0, hwmon_dev,
935 &fsl_sata_ahci_of_thermal_ops);
936 dev_info(dev, "%s: sensor 'sata_ahci'\n", dev_name(hwmon_dev));
939 ret = imx_sata_enable(hpriv);
944 * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL.
945 * Set CAP_SSS (support stagered spin up) and Implement the port0.
947 reg_val = readl(hpriv->mmio + HOST_CAP);
948 if (!(reg_val & HOST_CAP_SSS)) {
949 reg_val |= HOST_CAP_SSS;
950 writel(reg_val, hpriv->mmio + HOST_CAP);
952 reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
953 if (!(reg_val & 0x1)) {
955 writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
958 if (imxpriv->type != AHCI_IMX8QM) {
960 * Get AHB clock rate and configure the vendor specified
961 * TIMER1MS register on i.MX53, i.MX6Q and i.MX6QP only.
963 imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
964 if (IS_ERR(imxpriv->ahb_clk)) {
965 dev_err(dev, "Failed to get ahb clock\n");
966 ret = PTR_ERR(imxpriv->ahb_clk);
969 reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
970 writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
973 ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
981 imx_sata_disable(hpriv);
983 clk_disable_unprepare(imxpriv->sata_clk);
987 static void ahci_imx_host_stop(struct ata_host *host)
989 struct ahci_host_priv *hpriv = host->private_data;
990 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
992 imx_sata_disable(hpriv);
993 clk_disable_unprepare(imxpriv->sata_clk);
996 #ifdef CONFIG_PM_SLEEP
997 static int imx_ahci_suspend(struct device *dev)
999 struct ata_host *host = dev_get_drvdata(dev);
1000 struct ahci_host_priv *hpriv = host->private_data;
1003 ret = ahci_platform_suspend_host(dev);
1007 imx_sata_disable(hpriv);
1012 static int imx_ahci_resume(struct device *dev)
1014 struct ata_host *host = dev_get_drvdata(dev);
1015 struct ahci_host_priv *hpriv = host->private_data;
1018 ret = imx_sata_enable(hpriv);
1022 return ahci_platform_resume_host(dev);
1026 static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
1028 static struct platform_driver imx_ahci_driver = {
1029 .probe = imx_ahci_probe,
1030 .remove = ata_platform_remove_one,
1033 .of_match_table = imx_ahci_of_match,
1034 .pm = &ahci_imx_pm_ops,
1037 module_platform_driver(imx_ahci_driver);
1039 MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
1041 MODULE_LICENSE("GPL");
1042 MODULE_ALIAS("platform:" DRV_NAME);