2 * copyright (c) 2013 Freescale Semiconductor, Inc.
3 * Freescale IMX AHCI SATA platform driver
5 * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
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.
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
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/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/ahci_platform.h>
25 #include <linux/of_device.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
28 #include <linux/libata.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/thermal.h>
34 #define DRV_NAME "ahci-imx"
37 /* Timer 1-ms Register */
38 IMX_TIMER1MS = 0x00e0,
39 /* Port0 PHY Control Register */
41 IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
42 IMX_P0PHYCR_CR_READ = 1 << 19,
43 IMX_P0PHYCR_CR_WRITE = 1 << 18,
44 IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
45 IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
46 /* Port0 PHY Status Register */
48 IMX_P0PHYSR_CR_ACK = 1 << 18,
49 IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
50 /* Lane0 Output Status Register */
51 IMX_LANE0_OUT_STAT = 0x2003,
52 IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
53 /* Clock Reset Register */
54 IMX_CLOCK_RESET = 0x7f3f,
55 IMX_CLOCK_RESET_RESET = 1 << 0,
64 struct imx_ahci_priv {
65 struct platform_device *ahci_pdev;
66 enum ahci_imx_type type;
68 struct clk *sata_ref_clk;
76 static int ahci_imx_hotplug;
77 module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
78 MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
80 static void ahci_imx_host_stop(struct ata_host *host);
82 static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
88 /* Assert or deassert the bit */
89 crval = readl(mmio + IMX_P0PHYCR);
94 writel(crval, mmio + IMX_P0PHYCR);
96 /* Wait for the cr_ack signal */
98 srval = readl(mmio + IMX_P0PHYSR);
99 if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
101 usleep_range(100, 200);
104 return timeout ? 0 : -ETIMEDOUT;
107 static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
112 /* Supply the address on cr_data_in */
113 writel(crval, mmio + IMX_P0PHYCR);
115 /* Assert the cr_cap_addr signal */
116 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
120 /* Deassert cr_cap_addr */
121 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
128 static int imx_phy_reg_write(u16 val, void __iomem *mmio)
133 /* Supply the data on cr_data_in */
134 writel(crval, mmio + IMX_P0PHYCR);
136 /* Assert the cr_cap_data signal */
137 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
141 /* Deassert cr_cap_data */
142 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
146 if (val & IMX_CLOCK_RESET_RESET) {
148 * In case we're resetting the phy, it's unable to acknowledge,
149 * so we return immediately here.
151 crval |= IMX_P0PHYCR_CR_WRITE;
152 writel(crval, mmio + IMX_P0PHYCR);
156 /* Assert the cr_write signal */
157 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
161 /* Deassert cr_write */
162 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
170 static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
174 /* Assert the cr_read signal */
175 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
179 /* Capture the data from cr_data_out[] */
180 *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
182 /* Deassert cr_read */
183 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
190 static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
192 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
193 void __iomem *mmio = hpriv->mmio;
198 if (imxpriv->type == AHCI_IMX6QP) {
199 /* 6qp adds the sata reset mechanism, use it for 6qp sata */
200 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
201 IMX6Q_GPR5_SATA_SW_PD, 0);
203 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
204 IMX6Q_GPR5_SATA_SW_RST, 0);
206 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
207 IMX6Q_GPR5_SATA_SW_RST,
208 IMX6Q_GPR5_SATA_SW_RST);
212 /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
213 ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
216 ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
220 /* Wait for PHY RX_PLL to be stable */
222 usleep_range(100, 200);
223 ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
226 ret = imx_phy_reg_read(&val, mmio);
229 if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
233 return timeout ? 0 : -ETIMEDOUT;
237 /* SATA PHY Register */
238 SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT = 0x0001,
239 SATA_PHY_CR_CLOCK_DAC_CTL = 0x0008,
240 SATA_PHY_CR_CLOCK_RTUNE_CTL = 0x0009,
241 SATA_PHY_CR_CLOCK_ADC_OUT = 0x000A,
242 SATA_PHY_CR_CLOCK_MPLL_TST = 0x0017,
245 static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
247 u16 adc_out_reg, read_sum;
248 u32 index, read_attempt;
249 const u32 attempt_limit = 200;
251 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
252 imx_phy_reg_write(rtune_ctl_reg, mmio);
258 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
260 imx_phy_reg_read(&adc_out_reg, mmio);
262 if (adc_out_reg & 0x400)
266 if (read_attempt > attempt_limit) {
267 dev_err(dev, "Read REG more than %d times!\n",
277 imx_phy_reg_read(&adc_out_reg, mmio);
278 if (adc_out_reg & 0x400) {
279 read_sum = read_sum + (adc_out_reg & 0x3FF);
283 if (read_attempt > attempt_limit) {
284 dev_err(dev, "Read REG more than %d times!\n",
290 /* Use the U32 to make 1000 precision */
291 return (read_sum * 1000) / 80;
294 /* SATA AHCI temperature monitor */
295 static int sata_ahci_read_temperature(void *dev, int *temp)
297 u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
298 u32 str1, str2, str3, str4;
300 struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
301 void __iomem *mmio = hpriv->mmio;
303 /* check rd-wr to reg */
305 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT, mmio);
306 imx_phy_reg_write(read_sum, mmio);
307 imx_phy_reg_read(&read_sum, mmio);
308 if ((read_sum & 0xffff) != 0)
309 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
311 imx_phy_reg_write(0x5A5A, mmio);
312 imx_phy_reg_read(&read_sum, mmio);
313 if ((read_sum & 0xffff) != 0x5A5A)
314 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
316 imx_phy_reg_write(0x1234, mmio);
317 imx_phy_reg_read(&read_sum, mmio);
318 if ((read_sum & 0xffff) != 0x1234)
319 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
321 /* start temperature test */
322 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
323 imx_phy_reg_read(&mpll_test_reg, mmio);
324 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
325 imx_phy_reg_read(&rtune_ctl_reg, mmio);
326 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
327 imx_phy_reg_read(&dac_ctl_reg, mmio);
329 /* mpll_tst.meas_iv ([12:2]) */
330 str1 = (mpll_test_reg >> 2) & 0x7FF;
331 /* rtune_ctl.mode ([1:0]) */
332 str2 = (rtune_ctl_reg) & 0x3;
333 /* dac_ctl.dac_mode ([14:12]) */
334 str3 = (dac_ctl_reg >> 12) & 0x7;
335 /* rtune_ctl.sel_atbp ([4]) */
336 str4 = (rtune_ctl_reg >> 4);
338 /* Calculate the m1 */
339 /* mpll_tst.meas_iv */
340 mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
342 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (1);
343 /* dac_ctl.dac_mode */
344 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (4) << 12;
345 /* rtune_ctl.sel_atbp */
346 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (0) << 4;
347 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
348 imx_phy_reg_write(mpll_test_reg, mmio);
349 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
350 imx_phy_reg_write(dac_ctl_reg, mmio);
351 m1 = read_adc_sum(dev, rtune_ctl_reg, mmio);
353 /* Calculate the m2 */
354 /* rtune_ctl.sel_atbp */
355 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (1) << 4;
356 m2 = read_adc_sum(dev, rtune_ctl_reg, mmio);
358 /* restore the status */
359 /* mpll_tst.meas_iv */
360 mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
362 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (str2);
363 /* dac_ctl.dac_mode */
364 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (str3) << 12;
365 /* rtune_ctl.sel_atbp */
366 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (str4) << 4;
368 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
369 imx_phy_reg_write(mpll_test_reg, mmio);
370 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
371 imx_phy_reg_write(dac_ctl_reg, mmio);
372 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
373 imx_phy_reg_write(rtune_ctl_reg, mmio);
375 /* Compute temperature */
378 a = (m2 - m1) / (m2/1000);
379 *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
384 static ssize_t sata_ahci_show_temp(struct device *dev,
385 struct device_attribute *da,
388 unsigned int temp = 0;
391 err = sata_ahci_read_temperature(dev, &temp);
395 return sprintf(buf, "%u\n", temp);
398 static const struct thermal_zone_of_device_ops fsl_sata_ahci_of_thermal_ops = {
399 .get_temp = sata_ahci_read_temperature,
402 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
404 static struct attribute *fsl_sata_ahci_attrs[] = {
405 &sensor_dev_attr_temp1_input.dev_attr.attr,
408 ATTRIBUTE_GROUPS(fsl_sata_ahci);
410 static int imx_sata_enable(struct ahci_host_priv *hpriv)
412 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
413 struct device *dev = &imxpriv->ahci_pdev->dev;
416 if (imxpriv->no_device)
419 ret = ahci_platform_enable_regulators(hpriv);
423 ret = clk_prepare_enable(imxpriv->sata_ref_clk);
425 goto disable_regulator;
427 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
429 * set PHY Paremeters, two steps to configure the GPR13,
430 * one write for rest of parameters, mask of first write
431 * is 0x07ffffff, and the other one write for setting
434 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
435 IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
436 IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
437 IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
438 IMX6Q_GPR13_SATA_SPD_MODE_MASK |
439 IMX6Q_GPR13_SATA_MPLL_SS_EN |
440 IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
441 IMX6Q_GPR13_SATA_TX_BOOST_MASK |
442 IMX6Q_GPR13_SATA_TX_LVL_MASK |
443 IMX6Q_GPR13_SATA_MPLL_CLK_EN |
444 IMX6Q_GPR13_SATA_TX_EDGE_RATE,
445 imxpriv->phy_params);
446 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
447 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
448 IMX6Q_GPR13_SATA_MPLL_CLK_EN);
450 usleep_range(100, 200);
452 ret = imx_sata_phy_reset(hpriv);
454 dev_err(dev, "failed to reset phy: %d\n", ret);
459 usleep_range(1000, 2000);
464 clk_disable_unprepare(imxpriv->sata_ref_clk);
466 ahci_platform_disable_regulators(hpriv);
471 static void imx_sata_disable(struct ahci_host_priv *hpriv)
473 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
475 if (imxpriv->no_device)
478 switch (imxpriv->type) {
480 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
481 IMX6Q_GPR5_SATA_SW_PD,
482 IMX6Q_GPR5_SATA_SW_PD);
483 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
484 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
485 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
489 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
490 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
491 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
498 clk_disable_unprepare(imxpriv->sata_ref_clk);
500 ahci_platform_disable_regulators(hpriv);
503 static void ahci_imx_error_handler(struct ata_port *ap)
506 struct ata_device *dev;
507 struct ata_host *host = dev_get_drvdata(ap->dev);
508 struct ahci_host_priv *hpriv = host->private_data;
509 void __iomem *mmio = hpriv->mmio;
510 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
512 ahci_error_handler(ap);
514 if (!(imxpriv->first_time) || ahci_imx_hotplug)
517 imxpriv->first_time = false;
519 ata_for_each_dev(dev, &ap->link, ENABLED)
522 * Disable link to save power. An imx ahci port can't be recovered
523 * without full reset once the pddq mode is enabled making it
524 * impossible to use as part of libata LPM.
526 reg_val = readl(mmio + IMX_P0PHYCR);
527 writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
528 imx_sata_disable(hpriv);
529 imxpriv->no_device = true;
531 dev_info(ap->dev, "no device found, disabling link.\n");
532 dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
535 static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
536 unsigned long deadline)
538 struct ata_port *ap = link->ap;
539 struct ata_host *host = dev_get_drvdata(ap->dev);
540 struct ahci_host_priv *hpriv = host->private_data;
541 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
544 if (imxpriv->type == AHCI_IMX53)
545 ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
547 ret = ahci_ops.softreset(link, class, deadline);
552 static struct ata_port_operations ahci_imx_ops = {
553 .inherits = &ahci_ops,
554 .host_stop = ahci_imx_host_stop,
555 .error_handler = ahci_imx_error_handler,
556 .softreset = ahci_imx_softreset,
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,
566 static const struct of_device_id imx_ahci_of_match[] = {
567 { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
568 { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
569 { .compatible = "fsl,imx6qp-ahci", .data = (void *)AHCI_IMX6QP },
572 MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
579 struct reg_property {
581 const struct reg_value *values;
587 static const struct reg_value gpr13_tx_level[] = {
588 { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
589 { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
590 { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
591 { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
592 { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
593 { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
594 { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
595 { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
596 { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
597 { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
598 { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
599 { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
600 { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
601 { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
602 { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
603 { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
604 { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
605 { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
606 { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
607 { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
608 { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
609 { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
610 { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
611 { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
612 { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
613 { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
614 { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
615 { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
616 { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
617 { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
618 { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
619 { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
622 static const struct reg_value gpr13_tx_boost[] = {
623 { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
624 { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
625 { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
626 { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
627 { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
628 { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
629 { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
630 { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
631 { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
632 { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
633 { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
634 { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
635 { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
636 { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
637 { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
638 { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
641 static const struct reg_value gpr13_tx_atten[] = {
642 { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
643 { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
644 { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
645 { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
646 { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
647 { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
650 static const struct reg_value gpr13_rx_eq[] = {
651 { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
652 { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
653 { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
654 { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
655 { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
656 { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
657 { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
658 { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
661 static const struct reg_property gpr13_props[] = {
663 .name = "fsl,transmit-level-mV",
664 .values = gpr13_tx_level,
665 .num_values = ARRAY_SIZE(gpr13_tx_level),
666 .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
668 .name = "fsl,transmit-boost-mdB",
669 .values = gpr13_tx_boost,
670 .num_values = ARRAY_SIZE(gpr13_tx_boost),
671 .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
673 .name = "fsl,transmit-atten-16ths",
674 .values = gpr13_tx_atten,
675 .num_values = ARRAY_SIZE(gpr13_tx_atten),
676 .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
678 .name = "fsl,receive-eq-mdB",
679 .values = gpr13_rx_eq,
680 .num_values = ARRAY_SIZE(gpr13_rx_eq),
681 .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
683 .name = "fsl,no-spread-spectrum",
684 .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
689 static u32 imx_ahci_parse_props(struct device *dev,
690 const struct reg_property *prop, size_t num)
692 struct device_node *np = dev->of_node;
696 for (i = 0; i < num; i++, prop++) {
699 if (prop->num_values == 0) {
700 if (of_property_read_bool(np, prop->name))
701 reg_value |= prop->set_value;
703 reg_value |= prop->def_value;
707 if (of_property_read_u32(np, prop->name, &of_val)) {
708 dev_info(dev, "%s not specified, using %08x\n",
709 prop->name, prop->def_value);
710 reg_value |= prop->def_value;
714 for (j = 0; j < prop->num_values; j++) {
715 if (prop->values[j].of_value == of_val) {
716 dev_info(dev, "%s value %u, using %08x\n",
717 prop->name, of_val, prop->values[j].reg_value);
718 reg_value |= prop->values[j].reg_value;
723 if (j == prop->num_values) {
724 dev_err(dev, "DT property %s is not a valid value\n",
726 reg_value |= prop->def_value;
733 static struct scsi_host_template ahci_platform_sht = {
737 static int imx_ahci_probe(struct platform_device *pdev)
739 struct device *dev = &pdev->dev;
740 const struct of_device_id *of_id;
741 struct ahci_host_priv *hpriv;
742 struct imx_ahci_priv *imxpriv;
743 unsigned int reg_val;
746 of_id = of_match_device(imx_ahci_of_match, dev);
750 imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
754 imxpriv->ahci_pdev = pdev;
755 imxpriv->no_device = false;
756 imxpriv->first_time = true;
757 imxpriv->type = (enum ahci_imx_type)of_id->data;
759 imxpriv->sata_clk = devm_clk_get(dev, "sata");
760 if (IS_ERR(imxpriv->sata_clk)) {
761 dev_err(dev, "can't get sata clock.\n");
762 return PTR_ERR(imxpriv->sata_clk);
765 imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
766 if (IS_ERR(imxpriv->sata_ref_clk)) {
767 dev_err(dev, "can't get sata_ref clock.\n");
768 return PTR_ERR(imxpriv->sata_ref_clk);
771 imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
772 if (IS_ERR(imxpriv->ahb_clk)) {
773 dev_err(dev, "can't get ahb clock.\n");
774 return PTR_ERR(imxpriv->ahb_clk);
777 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
780 imxpriv->gpr = syscon_regmap_lookup_by_compatible(
781 "fsl,imx6q-iomuxc-gpr");
782 if (IS_ERR(imxpriv->gpr)) {
784 "failed to find fsl,imx6q-iomux-gpr regmap\n");
785 return PTR_ERR(imxpriv->gpr);
788 reg_value = imx_ahci_parse_props(dev, gpr13_props,
789 ARRAY_SIZE(gpr13_props));
791 imxpriv->phy_params =
792 IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
793 IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
794 IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
798 hpriv = ahci_platform_get_resources(pdev);
800 return PTR_ERR(hpriv);
802 hpriv->plat_data = imxpriv;
804 ret = clk_prepare_enable(imxpriv->sata_clk);
808 if (imxpriv->type == AHCI_IMX53 &&
809 IS_ENABLED(CONFIG_HWMON)) {
810 /* Add the temperature monitor */
811 struct device *hwmon_dev;
814 devm_hwmon_device_register_with_groups(dev,
817 fsl_sata_ahci_groups);
818 if (IS_ERR(hwmon_dev)) {
819 ret = PTR_ERR(hwmon_dev);
822 devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
823 &fsl_sata_ahci_of_thermal_ops);
824 dev_info(dev, "%s: sensor 'sata_ahci'\n", dev_name(hwmon_dev));
827 ret = imx_sata_enable(hpriv);
832 * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
833 * and IP vendor specific register IMX_TIMER1MS.
834 * Configure CAP_SSS (support stagered spin up).
835 * Implement the port0.
836 * Get the ahb clock rate, and configure the TIMER1MS register.
838 reg_val = readl(hpriv->mmio + HOST_CAP);
839 if (!(reg_val & HOST_CAP_SSS)) {
840 reg_val |= HOST_CAP_SSS;
841 writel(reg_val, hpriv->mmio + HOST_CAP);
843 reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
844 if (!(reg_val & 0x1)) {
846 writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
849 reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
850 writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
852 ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
860 imx_sata_disable(hpriv);
862 clk_disable_unprepare(imxpriv->sata_clk);
866 static void ahci_imx_host_stop(struct ata_host *host)
868 struct ahci_host_priv *hpriv = host->private_data;
869 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
871 imx_sata_disable(hpriv);
872 clk_disable_unprepare(imxpriv->sata_clk);
875 #ifdef CONFIG_PM_SLEEP
876 static int imx_ahci_suspend(struct device *dev)
878 struct ata_host *host = dev_get_drvdata(dev);
879 struct ahci_host_priv *hpriv = host->private_data;
882 ret = ahci_platform_suspend_host(dev);
886 imx_sata_disable(hpriv);
891 static int imx_ahci_resume(struct device *dev)
893 struct ata_host *host = dev_get_drvdata(dev);
894 struct ahci_host_priv *hpriv = host->private_data;
897 ret = imx_sata_enable(hpriv);
901 return ahci_platform_resume_host(dev);
905 static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
907 static struct platform_driver imx_ahci_driver = {
908 .probe = imx_ahci_probe,
909 .remove = ata_platform_remove_one,
912 .of_match_table = imx_ahci_of_match,
913 .pm = &ahci_imx_pm_ops,
916 module_platform_driver(imx_ahci_driver);
918 MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
919 MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
920 MODULE_LICENSE("GPL");
921 MODULE_ALIAS("ahci:imx");