Loading...
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
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>
16#include <linux/of.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>
24#include "ahci.h"
25
26#define DRV_NAME "ahci-imx"
27
28enum {
29 /* Timer 1-ms Register */
30 IMX_TIMER1MS = 0x00e0,
31 /* Port0 PHY Control Register */
32 IMX_P0PHYCR = 0x0178,
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 */
39 IMX_P0PHYSR = 0x017c,
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,
52};
53
54enum ahci_imx_type {
55 AHCI_IMX53,
56 AHCI_IMX6Q,
57 AHCI_IMX6QP,
58 AHCI_IMX8QM,
59};
60
61struct imx_ahci_priv {
62 struct platform_device *ahci_pdev;
63 enum ahci_imx_type type;
64 struct clk *sata_clk;
65 struct clk *sata_ref_clk;
66 struct clk *ahb_clk;
67 struct regmap *gpr;
68 struct phy *sata_phy;
69 struct phy *cali_phy0;
70 struct phy *cali_phy1;
71 bool no_device;
72 bool first_time;
73 u32 phy_params;
74 u32 imped_ratio;
75};
76
77static int ahci_imx_hotplug;
78module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
79MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
80
81static void ahci_imx_host_stop(struct ata_host *host);
82
83static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
84{
85 int timeout = 10;
86 u32 crval;
87 u32 srval;
88
89 /* Assert or deassert the bit */
90 crval = readl(mmio + IMX_P0PHYCR);
91 if (assert)
92 crval |= bit;
93 else
94 crval &= ~bit;
95 writel(crval, mmio + IMX_P0PHYCR);
96
97 /* Wait for the cr_ack signal */
98 do {
99 srval = readl(mmio + IMX_P0PHYSR);
100 if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
101 break;
102 usleep_range(100, 200);
103 } while (--timeout);
104
105 return timeout ? 0 : -ETIMEDOUT;
106}
107
108static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
109{
110 u32 crval = addr;
111 int ret;
112
113 /* Supply the address on cr_data_in */
114 writel(crval, mmio + IMX_P0PHYCR);
115
116 /* Assert the cr_cap_addr signal */
117 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
118 if (ret)
119 return ret;
120
121 /* Deassert cr_cap_addr */
122 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
123 if (ret)
124 return ret;
125
126 return 0;
127}
128
129static int imx_phy_reg_write(u16 val, void __iomem *mmio)
130{
131 u32 crval = val;
132 int ret;
133
134 /* Supply the data on cr_data_in */
135 writel(crval, mmio + IMX_P0PHYCR);
136
137 /* Assert the cr_cap_data signal */
138 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
139 if (ret)
140 return ret;
141
142 /* Deassert cr_cap_data */
143 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
144 if (ret)
145 return ret;
146
147 if (val & IMX_CLOCK_RESET_RESET) {
148 /*
149 * In case we're resetting the phy, it's unable to acknowledge,
150 * so we return immediately here.
151 */
152 crval |= IMX_P0PHYCR_CR_WRITE;
153 writel(crval, mmio + IMX_P0PHYCR);
154 goto out;
155 }
156
157 /* Assert the cr_write signal */
158 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
159 if (ret)
160 return ret;
161
162 /* Deassert cr_write */
163 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
164 if (ret)
165 return ret;
166
167out:
168 return 0;
169}
170
171static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
172{
173 int ret;
174
175 /* Assert the cr_read signal */
176 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
177 if (ret)
178 return ret;
179
180 /* Capture the data from cr_data_out[] */
181 *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
182
183 /* Deassert cr_read */
184 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
185 if (ret)
186 return ret;
187
188 return 0;
189}
190
191static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
192{
193 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
194 void __iomem *mmio = hpriv->mmio;
195 int timeout = 10;
196 u16 val;
197 int ret;
198
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);
203
204 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
205 IMX6Q_GPR5_SATA_SW_RST, 0);
206 udelay(50);
207 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
208 IMX6Q_GPR5_SATA_SW_RST,
209 IMX6Q_GPR5_SATA_SW_RST);
210 return 0;
211 }
212
213 /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
214 ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
215 if (ret)
216 return ret;
217 ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
218 if (ret)
219 return ret;
220
221 /* Wait for PHY RX_PLL to be stable */
222 do {
223 usleep_range(100, 200);
224 ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
225 if (ret)
226 return ret;
227 ret = imx_phy_reg_read(&val, mmio);
228 if (ret)
229 return ret;
230 if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
231 break;
232 } while (--timeout);
233
234 return timeout ? 0 : -ETIMEDOUT;
235}
236
237enum {
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,
244};
245
246static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
247{
248 u16 adc_out_reg, read_sum;
249 u32 index, read_attempt;
250 const u32 attempt_limit = 200;
251
252 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
253 imx_phy_reg_write(rtune_ctl_reg, mmio);
254
255 /* two dummy read */
256 index = 0;
257 read_attempt = 0;
258 adc_out_reg = 0;
259 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
260 while (index < 2) {
261 imx_phy_reg_read(&adc_out_reg, mmio);
262 /* check if valid */
263 if (adc_out_reg & 0x400)
264 index++;
265
266 read_attempt++;
267 if (read_attempt > attempt_limit) {
268 dev_err(dev, "Read REG more than %d times!\n",
269 attempt_limit);
270 break;
271 }
272 }
273
274 index = 0;
275 read_attempt = 0;
276 read_sum = 0;
277 while (index < 80) {
278 imx_phy_reg_read(&adc_out_reg, mmio);
279 if (adc_out_reg & 0x400) {
280 read_sum = read_sum + (adc_out_reg & 0x3FF);
281 index++;
282 }
283 read_attempt++;
284 if (read_attempt > attempt_limit) {
285 dev_err(dev, "Read REG more than %d times!\n",
286 attempt_limit);
287 break;
288 }
289 }
290
291 /* Use the U32 to make 1000 precision */
292 return (read_sum * 1000) / 80;
293}
294
295/* SATA AHCI temperature monitor */
296static int __sata_ahci_read_temperature(void *dev, int *temp)
297{
298 u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
299 u32 str1, str2, str3, str4;
300 int m1, m2, a;
301 struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
302 void __iomem *mmio = hpriv->mmio;
303
304 /* check rd-wr to reg */
305 read_sum = 0;
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);
311
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);
316
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);
321
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);
329
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);
338
339 /* Calculate the m1 */
340 /* mpll_tst.meas_iv */
341 mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
342 /* rtune_ctl.mode */
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);
353
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);
358
359 /* restore the status */
360 /* mpll_tst.meas_iv */
361 mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
362 /* rtune_ctl.mode */
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;
368
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);
375
376 /* Compute temperature */
377 if (!(m2 / 1000))
378 m2 = 1000;
379 a = (m2 - m1) / (m2/1000);
380 *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
381
382 return 0;
383}
384
385static int sata_ahci_read_temperature(struct thermal_zone_device *tz, int *temp)
386{
387 return __sata_ahci_read_temperature(thermal_zone_device_priv(tz), temp);
388}
389
390static ssize_t sata_ahci_show_temp(struct device *dev,
391 struct device_attribute *da,
392 char *buf)
393{
394 unsigned int temp = 0;
395 int err;
396
397 err = __sata_ahci_read_temperature(dev, &temp);
398 if (err < 0)
399 return err;
400
401 return sprintf(buf, "%u\n", temp);
402}
403
404static const struct thermal_zone_device_ops fsl_sata_ahci_of_thermal_ops = {
405 .get_temp = sata_ahci_read_temperature,
406};
407
408static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
409
410static struct attribute *fsl_sata_ahci_attrs[] = {
411 &sensor_dev_attr_temp1_input.dev_attr.attr,
412 NULL
413};
414ATTRIBUTE_GROUPS(fsl_sata_ahci);
415
416static int imx8_sata_enable(struct ahci_host_priv *hpriv)
417{
418 u32 val;
419 int ret;
420 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
421 struct device *dev = &imxpriv->ahci_pdev->dev;
422
423 /*
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.
427 *
428 * Initialize the first two lane PHYs here, although only the
429 * third lane PHY is used by SATA.
430 */
431 ret = phy_init(imxpriv->cali_phy0);
432 if (ret) {
433 dev_err(dev, "cali PHY init failed\n");
434 return ret;
435 }
436 ret = phy_power_on(imxpriv->cali_phy0);
437 if (ret) {
438 dev_err(dev, "cali PHY power on failed\n");
439 goto err_cali_phy0_exit;
440 }
441 ret = phy_init(imxpriv->cali_phy1);
442 if (ret) {
443 dev_err(dev, "cali PHY1 init failed\n");
444 goto err_cali_phy0_off;
445 }
446 ret = phy_power_on(imxpriv->cali_phy1);
447 if (ret) {
448 dev_err(dev, "cali PHY1 power on failed\n");
449 goto err_cali_phy1_exit;
450 }
451 ret = phy_init(imxpriv->sata_phy);
452 if (ret) {
453 dev_err(dev, "sata PHY init failed\n");
454 goto err_cali_phy1_off;
455 }
456 ret = phy_set_mode(imxpriv->sata_phy, PHY_MODE_SATA);
457 if (ret) {
458 dev_err(dev, "unable to set SATA PHY mode\n");
459 goto err_sata_phy_exit;
460 }
461 ret = phy_power_on(imxpriv->sata_phy);
462 if (ret) {
463 dev_err(dev, "sata PHY power up failed\n");
464 goto err_sata_phy_exit;
465 }
466
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);
472
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);
478
479 return 0;
480
481err_sata_phy_exit:
482 phy_exit(imxpriv->sata_phy);
483err_cali_phy1_off:
484 phy_power_off(imxpriv->cali_phy1);
485err_cali_phy1_exit:
486 phy_exit(imxpriv->cali_phy1);
487err_cali_phy0_off:
488 phy_power_off(imxpriv->cali_phy0);
489err_cali_phy0_exit:
490 phy_exit(imxpriv->cali_phy0);
491
492 return ret;
493}
494
495static int imx_sata_enable(struct ahci_host_priv *hpriv)
496{
497 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
498 struct device *dev = &imxpriv->ahci_pdev->dev;
499 int ret;
500
501 if (imxpriv->no_device)
502 return 0;
503
504 ret = ahci_platform_enable_regulators(hpriv);
505 if (ret)
506 return ret;
507
508 ret = clk_prepare_enable(imxpriv->sata_ref_clk);
509 if (ret < 0)
510 goto disable_regulator;
511
512 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
513 /*
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
517 * the mpll_clk_en.
518 */
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);
534
535 usleep_range(100, 200);
536
537 ret = imx_sata_phy_reset(hpriv);
538 if (ret) {
539 dev_err(dev, "failed to reset phy: %d\n", ret);
540 goto disable_clk;
541 }
542 } else if (imxpriv->type == AHCI_IMX8QM) {
543 ret = imx8_sata_enable(hpriv);
544 if (ret)
545 goto disable_clk;
546
547 }
548
549 usleep_range(1000, 2000);
550
551 return 0;
552
553disable_clk:
554 clk_disable_unprepare(imxpriv->sata_ref_clk);
555disable_regulator:
556 ahci_platform_disable_regulators(hpriv);
557
558 return ret;
559}
560
561static void imx_sata_disable(struct ahci_host_priv *hpriv)
562{
563 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
564
565 if (imxpriv->no_device)
566 return;
567
568 switch (imxpriv->type) {
569 case AHCI_IMX6QP:
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);
576 break;
577
578 case AHCI_IMX6Q:
579 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
580 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
581 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
582 break;
583
584 case AHCI_IMX8QM:
585 if (imxpriv->sata_phy) {
586 phy_power_off(imxpriv->sata_phy);
587 phy_exit(imxpriv->sata_phy);
588 }
589 break;
590
591 default:
592 break;
593 }
594
595 clk_disable_unprepare(imxpriv->sata_ref_clk);
596
597 ahci_platform_disable_regulators(hpriv);
598}
599
600static void ahci_imx_error_handler(struct ata_port *ap)
601{
602 u32 reg_val;
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;
608
609 ahci_error_handler(ap);
610
611 if (imxpriv->type == AHCI_IMX8QM)
612 return;
613
614 if (!(imxpriv->first_time) || ahci_imx_hotplug)
615 return;
616
617 imxpriv->first_time = false;
618
619 ata_for_each_dev(dev, &ap->link, ENABLED)
620 return;
621 /*
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.
625 */
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;
630
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");
633}
634
635static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
636 unsigned long deadline)
637{
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;
642 int ret;
643
644 if (imxpriv->type == AHCI_IMX53)
645 ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
646 else
647 ret = ahci_ops.softreset(link, class, deadline);
648
649 return ret;
650}
651
652static 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,
657};
658
659static 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,
664};
665
666static 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 },
671 { /* sentinel */ }
672};
673MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
674
675struct reg_value {
676 u32 of_value;
677 u32 reg_value;
678};
679
680struct reg_property {
681 const char *name;
682 const struct reg_value *values;
683 size_t num_values;
684 u32 def_value;
685 u32 set_value;
686};
687
688static 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 }
721};
722
723static 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 }
740};
741
742static 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 },
749};
750
751static 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 },
760};
761
762static const struct reg_property gpr13_props[] = {
763 {
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,
768 }, {
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,
773 }, {
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,
778 }, {
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,
783 }, {
784 .name = "fsl,no-spread-spectrum",
785 .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
786 .set_value = 0,
787 },
788};
789
790static u32 imx_ahci_parse_props(struct device *dev,
791 const struct reg_property *prop, size_t num)
792{
793 struct device_node *np = dev->of_node;
794 u32 reg_value = 0;
795 int i, j;
796
797 for (i = 0; i < num; i++, prop++) {
798 u32 of_val;
799
800 if (prop->num_values == 0) {
801 if (of_property_read_bool(np, prop->name))
802 reg_value |= prop->set_value;
803 else
804 reg_value |= prop->def_value;
805 continue;
806 }
807
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;
812 continue;
813 }
814
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;
820 break;
821 }
822 }
823
824 if (j == prop->num_values) {
825 dev_err(dev, "DT property %s is not a valid value\n",
826 prop->name);
827 reg_value |= prop->def_value;
828 }
829 }
830
831 return reg_value;
832}
833
834static const struct scsi_host_template ahci_platform_sht = {
835 AHCI_SHT(DRV_NAME),
836};
837
838static int imx8_sata_probe(struct device *dev, struct imx_ahci_priv *imxpriv)
839{
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");
844
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");
853 return 0;
854}
855
856static int imx_ahci_probe(struct platform_device *pdev)
857{
858 struct device *dev = &pdev->dev;
859 struct ahci_host_priv *hpriv;
860 struct imx_ahci_priv *imxpriv;
861 unsigned int reg_val;
862 int ret;
863
864 imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
865 if (!imxpriv)
866 return -ENOMEM;
867
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);
872
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);
877 }
878
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);
883 }
884
885 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
886 u32 reg_value;
887
888 imxpriv->gpr = syscon_regmap_lookup_by_compatible(
889 "fsl,imx6q-iomuxc-gpr");
890 if (IS_ERR(imxpriv->gpr)) {
891 dev_err(dev,
892 "failed to find fsl,imx6q-iomux-gpr regmap\n");
893 return PTR_ERR(imxpriv->gpr);
894 }
895
896 reg_value = imx_ahci_parse_props(dev, gpr13_props,
897 ARRAY_SIZE(gpr13_props));
898
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 |
903 reg_value;
904 } else if (imxpriv->type == AHCI_IMX8QM) {
905 ret = imx8_sata_probe(dev, imxpriv);
906 if (ret)
907 return ret;
908 }
909
910 hpriv = ahci_platform_get_resources(pdev, 0);
911 if (IS_ERR(hpriv))
912 return PTR_ERR(hpriv);
913
914 hpriv->plat_data = imxpriv;
915
916 ret = clk_prepare_enable(imxpriv->sata_clk);
917 if (ret)
918 return ret;
919
920 if (imxpriv->type == AHCI_IMX53 &&
921 IS_ENABLED(CONFIG_HWMON)) {
922 /* Add the temperature monitor */
923 struct device *hwmon_dev;
924
925 hwmon_dev =
926 devm_hwmon_device_register_with_groups(dev,
927 "sata_ahci",
928 hpriv,
929 fsl_sata_ahci_groups);
930 if (IS_ERR(hwmon_dev)) {
931 ret = PTR_ERR(hwmon_dev);
932 goto disable_clk;
933 }
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));
937 }
938
939 ret = imx_sata_enable(hpriv);
940 if (ret)
941 goto disable_clk;
942
943 /*
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.
946 */
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);
951 }
952 reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
953 if (!(reg_val & 0x1)) {
954 reg_val |= 0x1;
955 writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
956 }
957
958 if (imxpriv->type != AHCI_IMX8QM) {
959 /*
960 * Get AHB clock rate and configure the vendor specified
961 * TIMER1MS register on i.MX53, i.MX6Q and i.MX6QP only.
962 */
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);
967 goto disable_sata;
968 }
969 reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
970 writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
971 }
972
973 ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
974 &ahci_platform_sht);
975 if (ret)
976 goto disable_sata;
977
978 return 0;
979
980disable_sata:
981 imx_sata_disable(hpriv);
982disable_clk:
983 clk_disable_unprepare(imxpriv->sata_clk);
984 return ret;
985}
986
987static void ahci_imx_host_stop(struct ata_host *host)
988{
989 struct ahci_host_priv *hpriv = host->private_data;
990 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
991
992 imx_sata_disable(hpriv);
993 clk_disable_unprepare(imxpriv->sata_clk);
994}
995
996#ifdef CONFIG_PM_SLEEP
997static int imx_ahci_suspend(struct device *dev)
998{
999 struct ata_host *host = dev_get_drvdata(dev);
1000 struct ahci_host_priv *hpriv = host->private_data;
1001 int ret;
1002
1003 ret = ahci_platform_suspend_host(dev);
1004 if (ret)
1005 return ret;
1006
1007 imx_sata_disable(hpriv);
1008
1009 return 0;
1010}
1011
1012static int imx_ahci_resume(struct device *dev)
1013{
1014 struct ata_host *host = dev_get_drvdata(dev);
1015 struct ahci_host_priv *hpriv = host->private_data;
1016 int ret;
1017
1018 ret = imx_sata_enable(hpriv);
1019 if (ret)
1020 return ret;
1021
1022 return ahci_platform_resume_host(dev);
1023}
1024#endif
1025
1026static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
1027
1028static struct platform_driver imx_ahci_driver = {
1029 .probe = imx_ahci_probe,
1030 .remove = ata_platform_remove_one,
1031 .driver = {
1032 .name = DRV_NAME,
1033 .of_match_table = imx_ahci_of_match,
1034 .pm = &ahci_imx_pm_ops,
1035 },
1036};
1037module_platform_driver(imx_ahci_driver);
1038
1039MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
1040MODULE_AUTHOR("Richard Zhu <hongxing.zhu@nxp.com>");
1041MODULE_LICENSE("GPL");
1042MODULE_ALIAS("platform:" DRV_NAME);
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
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/regmap.h>
13#include <linux/ahci_platform.h>
14#include <linux/of_device.h>
15#include <linux/of_gpio.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/thermal.h>
22#include "ahci.h"
23
24#define DRV_NAME "ahci-imx"
25
26enum {
27 /* Timer 1-ms Register */
28 IMX_TIMER1MS = 0x00e0,
29 /* Port0 PHY Control Register */
30 IMX_P0PHYCR = 0x0178,
31 IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
32 IMX_P0PHYCR_CR_READ = 1 << 19,
33 IMX_P0PHYCR_CR_WRITE = 1 << 18,
34 IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
35 IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
36 /* Port0 PHY Status Register */
37 IMX_P0PHYSR = 0x017c,
38 IMX_P0PHYSR_CR_ACK = 1 << 18,
39 IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
40 /* Lane0 Output Status Register */
41 IMX_LANE0_OUT_STAT = 0x2003,
42 IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
43 /* Clock Reset Register */
44 IMX_CLOCK_RESET = 0x7f3f,
45 IMX_CLOCK_RESET_RESET = 1 << 0,
46 /* IMX8QM HSIO AHCI definitions */
47 IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET = 0x03,
48 IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET = 0x09,
49 IMX8QM_SATA_PHY_IMPED_RATIO_85OHM = 0x6c,
50 IMX8QM_LPCG_PHYX2_OFFSET = 0x00000,
51 IMX8QM_CSR_PHYX2_OFFSET = 0x90000,
52 IMX8QM_CSR_PHYX1_OFFSET = 0xa0000,
53 IMX8QM_CSR_PHYX_STTS0_OFFSET = 0x4,
54 IMX8QM_CSR_PCIEA_OFFSET = 0xb0000,
55 IMX8QM_CSR_PCIEB_OFFSET = 0xc0000,
56 IMX8QM_CSR_SATA_OFFSET = 0xd0000,
57 IMX8QM_CSR_PCIE_CTRL2_OFFSET = 0x8,
58 IMX8QM_CSR_MISC_OFFSET = 0xe0000,
59
60 IMX8QM_LPCG_PHYX2_PCLK0_MASK = (0x3 << 16),
61 IMX8QM_LPCG_PHYX2_PCLK1_MASK = (0x3 << 20),
62 IMX8QM_PHY_APB_RSTN_0 = BIT(0),
63 IMX8QM_PHY_MODE_SATA = BIT(19),
64 IMX8QM_PHY_MODE_MASK = (0xf << 17),
65 IMX8QM_PHY_PIPE_RSTN_0 = BIT(24),
66 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0 = BIT(25),
67 IMX8QM_PHY_PIPE_RSTN_1 = BIT(26),
68 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1 = BIT(27),
69 IMX8QM_STTS0_LANE0_TX_PLL_LOCK = BIT(4),
70 IMX8QM_MISC_IOB_RXENA = BIT(0),
71 IMX8QM_MISC_IOB_TXENA = BIT(1),
72 IMX8QM_MISC_PHYX1_EPCS_SEL = BIT(12),
73 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 = BIT(24),
74 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 = BIT(25),
75 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 = BIT(28),
76 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0 = BIT(29),
77 IMX8QM_SATA_CTRL_RESET_N = BIT(12),
78 IMX8QM_SATA_CTRL_EPCS_PHYRESET_N = BIT(7),
79 IMX8QM_CTRL_BUTTON_RST_N = BIT(21),
80 IMX8QM_CTRL_POWER_UP_RST_N = BIT(23),
81 IMX8QM_CTRL_LTSSM_ENABLE = BIT(4),
82};
83
84enum ahci_imx_type {
85 AHCI_IMX53,
86 AHCI_IMX6Q,
87 AHCI_IMX6QP,
88 AHCI_IMX8QM,
89};
90
91struct imx_ahci_priv {
92 struct platform_device *ahci_pdev;
93 enum ahci_imx_type type;
94 struct clk *sata_clk;
95 struct clk *sata_ref_clk;
96 struct clk *ahb_clk;
97 struct clk *epcs_tx_clk;
98 struct clk *epcs_rx_clk;
99 struct clk *phy_apbclk;
100 struct clk *phy_pclk0;
101 struct clk *phy_pclk1;
102 void __iomem *phy_base;
103 int clkreq_gpio;
104 struct regmap *gpr;
105 bool no_device;
106 bool first_time;
107 u32 phy_params;
108 u32 imped_ratio;
109};
110
111static int ahci_imx_hotplug;
112module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
113MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
114
115static void ahci_imx_host_stop(struct ata_host *host);
116
117static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
118{
119 int timeout = 10;
120 u32 crval;
121 u32 srval;
122
123 /* Assert or deassert the bit */
124 crval = readl(mmio + IMX_P0PHYCR);
125 if (assert)
126 crval |= bit;
127 else
128 crval &= ~bit;
129 writel(crval, mmio + IMX_P0PHYCR);
130
131 /* Wait for the cr_ack signal */
132 do {
133 srval = readl(mmio + IMX_P0PHYSR);
134 if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
135 break;
136 usleep_range(100, 200);
137 } while (--timeout);
138
139 return timeout ? 0 : -ETIMEDOUT;
140}
141
142static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
143{
144 u32 crval = addr;
145 int ret;
146
147 /* Supply the address on cr_data_in */
148 writel(crval, mmio + IMX_P0PHYCR);
149
150 /* Assert the cr_cap_addr signal */
151 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
152 if (ret)
153 return ret;
154
155 /* Deassert cr_cap_addr */
156 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
157 if (ret)
158 return ret;
159
160 return 0;
161}
162
163static int imx_phy_reg_write(u16 val, void __iomem *mmio)
164{
165 u32 crval = val;
166 int ret;
167
168 /* Supply the data on cr_data_in */
169 writel(crval, mmio + IMX_P0PHYCR);
170
171 /* Assert the cr_cap_data signal */
172 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
173 if (ret)
174 return ret;
175
176 /* Deassert cr_cap_data */
177 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
178 if (ret)
179 return ret;
180
181 if (val & IMX_CLOCK_RESET_RESET) {
182 /*
183 * In case we're resetting the phy, it's unable to acknowledge,
184 * so we return immediately here.
185 */
186 crval |= IMX_P0PHYCR_CR_WRITE;
187 writel(crval, mmio + IMX_P0PHYCR);
188 goto out;
189 }
190
191 /* Assert the cr_write signal */
192 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
193 if (ret)
194 return ret;
195
196 /* Deassert cr_write */
197 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
198 if (ret)
199 return ret;
200
201out:
202 return 0;
203}
204
205static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
206{
207 int ret;
208
209 /* Assert the cr_read signal */
210 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
211 if (ret)
212 return ret;
213
214 /* Capture the data from cr_data_out[] */
215 *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
216
217 /* Deassert cr_read */
218 ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
219 if (ret)
220 return ret;
221
222 return 0;
223}
224
225static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
226{
227 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
228 void __iomem *mmio = hpriv->mmio;
229 int timeout = 10;
230 u16 val;
231 int ret;
232
233 if (imxpriv->type == AHCI_IMX6QP) {
234 /* 6qp adds the sata reset mechanism, use it for 6qp sata */
235 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
236 IMX6Q_GPR5_SATA_SW_PD, 0);
237
238 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
239 IMX6Q_GPR5_SATA_SW_RST, 0);
240 udelay(50);
241 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
242 IMX6Q_GPR5_SATA_SW_RST,
243 IMX6Q_GPR5_SATA_SW_RST);
244 return 0;
245 }
246
247 /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
248 ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
249 if (ret)
250 return ret;
251 ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
252 if (ret)
253 return ret;
254
255 /* Wait for PHY RX_PLL to be stable */
256 do {
257 usleep_range(100, 200);
258 ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
259 if (ret)
260 return ret;
261 ret = imx_phy_reg_read(&val, mmio);
262 if (ret)
263 return ret;
264 if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
265 break;
266 } while (--timeout);
267
268 return timeout ? 0 : -ETIMEDOUT;
269}
270
271enum {
272 /* SATA PHY Register */
273 SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT = 0x0001,
274 SATA_PHY_CR_CLOCK_DAC_CTL = 0x0008,
275 SATA_PHY_CR_CLOCK_RTUNE_CTL = 0x0009,
276 SATA_PHY_CR_CLOCK_ADC_OUT = 0x000A,
277 SATA_PHY_CR_CLOCK_MPLL_TST = 0x0017,
278};
279
280static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
281{
282 u16 adc_out_reg, read_sum;
283 u32 index, read_attempt;
284 const u32 attempt_limit = 200;
285
286 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
287 imx_phy_reg_write(rtune_ctl_reg, mmio);
288
289 /* two dummy read */
290 index = 0;
291 read_attempt = 0;
292 adc_out_reg = 0;
293 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
294 while (index < 2) {
295 imx_phy_reg_read(&adc_out_reg, mmio);
296 /* check if valid */
297 if (adc_out_reg & 0x400)
298 index++;
299
300 read_attempt++;
301 if (read_attempt > attempt_limit) {
302 dev_err(dev, "Read REG more than %d times!\n",
303 attempt_limit);
304 break;
305 }
306 }
307
308 index = 0;
309 read_attempt = 0;
310 read_sum = 0;
311 while (index < 80) {
312 imx_phy_reg_read(&adc_out_reg, mmio);
313 if (adc_out_reg & 0x400) {
314 read_sum = read_sum + (adc_out_reg & 0x3FF);
315 index++;
316 }
317 read_attempt++;
318 if (read_attempt > attempt_limit) {
319 dev_err(dev, "Read REG more than %d times!\n",
320 attempt_limit);
321 break;
322 }
323 }
324
325 /* Use the U32 to make 1000 precision */
326 return (read_sum * 1000) / 80;
327}
328
329/* SATA AHCI temperature monitor */
330static int sata_ahci_read_temperature(void *dev, int *temp)
331{
332 u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
333 u32 str1, str2, str3, str4;
334 int m1, m2, a;
335 struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
336 void __iomem *mmio = hpriv->mmio;
337
338 /* check rd-wr to reg */
339 read_sum = 0;
340 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT, mmio);
341 imx_phy_reg_write(read_sum, mmio);
342 imx_phy_reg_read(&read_sum, mmio);
343 if ((read_sum & 0xffff) != 0)
344 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
345
346 imx_phy_reg_write(0x5A5A, mmio);
347 imx_phy_reg_read(&read_sum, mmio);
348 if ((read_sum & 0xffff) != 0x5A5A)
349 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
350
351 imx_phy_reg_write(0x1234, mmio);
352 imx_phy_reg_read(&read_sum, mmio);
353 if ((read_sum & 0xffff) != 0x1234)
354 dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
355
356 /* start temperature test */
357 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
358 imx_phy_reg_read(&mpll_test_reg, mmio);
359 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
360 imx_phy_reg_read(&rtune_ctl_reg, mmio);
361 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
362 imx_phy_reg_read(&dac_ctl_reg, mmio);
363
364 /* mpll_tst.meas_iv ([12:2]) */
365 str1 = (mpll_test_reg >> 2) & 0x7FF;
366 /* rtune_ctl.mode ([1:0]) */
367 str2 = (rtune_ctl_reg) & 0x3;
368 /* dac_ctl.dac_mode ([14:12]) */
369 str3 = (dac_ctl_reg >> 12) & 0x7;
370 /* rtune_ctl.sel_atbp ([4]) */
371 str4 = (rtune_ctl_reg >> 4);
372
373 /* Calculate the m1 */
374 /* mpll_tst.meas_iv */
375 mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
376 /* rtune_ctl.mode */
377 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (1);
378 /* dac_ctl.dac_mode */
379 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (4) << 12;
380 /* rtune_ctl.sel_atbp */
381 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (0) << 4;
382 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
383 imx_phy_reg_write(mpll_test_reg, mmio);
384 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
385 imx_phy_reg_write(dac_ctl_reg, mmio);
386 m1 = read_adc_sum(dev, rtune_ctl_reg, mmio);
387
388 /* Calculate the m2 */
389 /* rtune_ctl.sel_atbp */
390 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (1) << 4;
391 m2 = read_adc_sum(dev, rtune_ctl_reg, mmio);
392
393 /* restore the status */
394 /* mpll_tst.meas_iv */
395 mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
396 /* rtune_ctl.mode */
397 rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (str2);
398 /* dac_ctl.dac_mode */
399 dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (str3) << 12;
400 /* rtune_ctl.sel_atbp */
401 rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (str4) << 4;
402
403 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
404 imx_phy_reg_write(mpll_test_reg, mmio);
405 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
406 imx_phy_reg_write(dac_ctl_reg, mmio);
407 imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
408 imx_phy_reg_write(rtune_ctl_reg, mmio);
409
410 /* Compute temperature */
411 if (!(m2 / 1000))
412 m2 = 1000;
413 a = (m2 - m1) / (m2/1000);
414 *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
415
416 return 0;
417}
418
419static ssize_t sata_ahci_show_temp(struct device *dev,
420 struct device_attribute *da,
421 char *buf)
422{
423 unsigned int temp = 0;
424 int err;
425
426 err = sata_ahci_read_temperature(dev, &temp);
427 if (err < 0)
428 return err;
429
430 return sprintf(buf, "%u\n", temp);
431}
432
433static const struct thermal_zone_of_device_ops fsl_sata_ahci_of_thermal_ops = {
434 .get_temp = sata_ahci_read_temperature,
435};
436
437static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
438
439static struct attribute *fsl_sata_ahci_attrs[] = {
440 &sensor_dev_attr_temp1_input.dev_attr.attr,
441 NULL
442};
443ATTRIBUTE_GROUPS(fsl_sata_ahci);
444
445static int imx8_sata_enable(struct ahci_host_priv *hpriv)
446{
447 u32 val, reg;
448 int i, ret;
449 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
450 struct device *dev = &imxpriv->ahci_pdev->dev;
451
452 /* configure the hsio for sata */
453 ret = clk_prepare_enable(imxpriv->phy_pclk0);
454 if (ret < 0) {
455 dev_err(dev, "can't enable phy_pclk0.\n");
456 return ret;
457 }
458 ret = clk_prepare_enable(imxpriv->phy_pclk1);
459 if (ret < 0) {
460 dev_err(dev, "can't enable phy_pclk1.\n");
461 goto disable_phy_pclk0;
462 }
463 ret = clk_prepare_enable(imxpriv->epcs_tx_clk);
464 if (ret < 0) {
465 dev_err(dev, "can't enable epcs_tx_clk.\n");
466 goto disable_phy_pclk1;
467 }
468 ret = clk_prepare_enable(imxpriv->epcs_rx_clk);
469 if (ret < 0) {
470 dev_err(dev, "can't enable epcs_rx_clk.\n");
471 goto disable_epcs_tx_clk;
472 }
473 ret = clk_prepare_enable(imxpriv->phy_apbclk);
474 if (ret < 0) {
475 dev_err(dev, "can't enable phy_apbclk.\n");
476 goto disable_epcs_rx_clk;
477 }
478 /* Configure PHYx2 PIPE_RSTN */
479 regmap_read(imxpriv->gpr, IMX8QM_CSR_PCIEA_OFFSET +
480 IMX8QM_CSR_PCIE_CTRL2_OFFSET, &val);
481 if ((val & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
482 /* The link of the PCIEA of HSIO is down */
483 regmap_update_bits(imxpriv->gpr,
484 IMX8QM_CSR_PHYX2_OFFSET,
485 IMX8QM_PHY_PIPE_RSTN_0 |
486 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0,
487 IMX8QM_PHY_PIPE_RSTN_0 |
488 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0);
489 }
490 regmap_read(imxpriv->gpr, IMX8QM_CSR_PCIEB_OFFSET +
491 IMX8QM_CSR_PCIE_CTRL2_OFFSET, ®);
492 if ((reg & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
493 /* The link of the PCIEB of HSIO is down */
494 regmap_update_bits(imxpriv->gpr,
495 IMX8QM_CSR_PHYX2_OFFSET,
496 IMX8QM_PHY_PIPE_RSTN_1 |
497 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1,
498 IMX8QM_PHY_PIPE_RSTN_1 |
499 IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1);
500 }
501 if (((reg | val) & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
502 /* The links of both PCIA and PCIEB of HSIO are down */
503 regmap_update_bits(imxpriv->gpr,
504 IMX8QM_LPCG_PHYX2_OFFSET,
505 IMX8QM_LPCG_PHYX2_PCLK0_MASK |
506 IMX8QM_LPCG_PHYX2_PCLK1_MASK,
507 0);
508 }
509
510 /* set PWR_RST and BT_RST of csr_pciea */
511 val = IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET;
512 regmap_update_bits(imxpriv->gpr,
513 val,
514 IMX8QM_CTRL_BUTTON_RST_N,
515 IMX8QM_CTRL_BUTTON_RST_N);
516 regmap_update_bits(imxpriv->gpr,
517 val,
518 IMX8QM_CTRL_POWER_UP_RST_N,
519 IMX8QM_CTRL_POWER_UP_RST_N);
520
521 /* PHYX1_MODE to SATA */
522 regmap_update_bits(imxpriv->gpr,
523 IMX8QM_CSR_PHYX1_OFFSET,
524 IMX8QM_PHY_MODE_MASK,
525 IMX8QM_PHY_MODE_SATA);
526
527 /*
528 * BIT0 RXENA 1, BIT1 TXENA 0
529 * BIT12 PHY_X1_EPCS_SEL 1.
530 */
531 regmap_update_bits(imxpriv->gpr,
532 IMX8QM_CSR_MISC_OFFSET,
533 IMX8QM_MISC_IOB_RXENA,
534 IMX8QM_MISC_IOB_RXENA);
535 regmap_update_bits(imxpriv->gpr,
536 IMX8QM_CSR_MISC_OFFSET,
537 IMX8QM_MISC_IOB_TXENA,
538 0);
539 regmap_update_bits(imxpriv->gpr,
540 IMX8QM_CSR_MISC_OFFSET,
541 IMX8QM_MISC_PHYX1_EPCS_SEL,
542 IMX8QM_MISC_PHYX1_EPCS_SEL);
543 /*
544 * It is possible, for PCIe and SATA are sharing
545 * the same clock source, HPLL or external oscillator.
546 * When PCIe is in low power modes (L1.X or L2 etc),
547 * the clock source can be turned off. In this case,
548 * if this clock source is required to be toggling by
549 * SATA, then SATA functions will be abnormal.
550 * Set the override here to avoid it.
551 */
552 regmap_update_bits(imxpriv->gpr,
553 IMX8QM_CSR_MISC_OFFSET,
554 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 |
555 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 |
556 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 |
557 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0,
558 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 |
559 IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 |
560 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 |
561 IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0);
562
563 /* clear PHY RST, then set it */
564 regmap_update_bits(imxpriv->gpr,
565 IMX8QM_CSR_SATA_OFFSET,
566 IMX8QM_SATA_CTRL_EPCS_PHYRESET_N,
567 0);
568
569 regmap_update_bits(imxpriv->gpr,
570 IMX8QM_CSR_SATA_OFFSET,
571 IMX8QM_SATA_CTRL_EPCS_PHYRESET_N,
572 IMX8QM_SATA_CTRL_EPCS_PHYRESET_N);
573
574 /* CTRL RST: SET -> delay 1 us -> CLEAR -> SET */
575 regmap_update_bits(imxpriv->gpr,
576 IMX8QM_CSR_SATA_OFFSET,
577 IMX8QM_SATA_CTRL_RESET_N,
578 IMX8QM_SATA_CTRL_RESET_N);
579 udelay(1);
580 regmap_update_bits(imxpriv->gpr,
581 IMX8QM_CSR_SATA_OFFSET,
582 IMX8QM_SATA_CTRL_RESET_N,
583 0);
584 regmap_update_bits(imxpriv->gpr,
585 IMX8QM_CSR_SATA_OFFSET,
586 IMX8QM_SATA_CTRL_RESET_N,
587 IMX8QM_SATA_CTRL_RESET_N);
588
589 /* APB reset */
590 regmap_update_bits(imxpriv->gpr,
591 IMX8QM_CSR_PHYX1_OFFSET,
592 IMX8QM_PHY_APB_RSTN_0,
593 IMX8QM_PHY_APB_RSTN_0);
594
595 for (i = 0; i < 100; i++) {
596 reg = IMX8QM_CSR_PHYX1_OFFSET +
597 IMX8QM_CSR_PHYX_STTS0_OFFSET;
598 regmap_read(imxpriv->gpr, reg, &val);
599 val &= IMX8QM_STTS0_LANE0_TX_PLL_LOCK;
600 if (val == IMX8QM_STTS0_LANE0_TX_PLL_LOCK)
601 break;
602 udelay(1);
603 }
604
605 if (val != IMX8QM_STTS0_LANE0_TX_PLL_LOCK) {
606 dev_err(dev, "TX PLL of the PHY is not locked\n");
607 ret = -ENODEV;
608 } else {
609 writeb(imxpriv->imped_ratio, imxpriv->phy_base +
610 IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET);
611 writeb(imxpriv->imped_ratio, imxpriv->phy_base +
612 IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET);
613 reg = readb(imxpriv->phy_base +
614 IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET);
615 if (unlikely(reg != imxpriv->imped_ratio))
616 dev_info(dev, "Can't set PHY RX impedance ratio.\n");
617 reg = readb(imxpriv->phy_base +
618 IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET);
619 if (unlikely(reg != imxpriv->imped_ratio))
620 dev_info(dev, "Can't set PHY TX impedance ratio.\n");
621 usleep_range(50, 100);
622
623 /*
624 * To reduce the power consumption, gate off
625 * the PHY clks
626 */
627 clk_disable_unprepare(imxpriv->phy_apbclk);
628 clk_disable_unprepare(imxpriv->phy_pclk1);
629 clk_disable_unprepare(imxpriv->phy_pclk0);
630 return ret;
631 }
632
633 clk_disable_unprepare(imxpriv->phy_apbclk);
634disable_epcs_rx_clk:
635 clk_disable_unprepare(imxpriv->epcs_rx_clk);
636disable_epcs_tx_clk:
637 clk_disable_unprepare(imxpriv->epcs_tx_clk);
638disable_phy_pclk1:
639 clk_disable_unprepare(imxpriv->phy_pclk1);
640disable_phy_pclk0:
641 clk_disable_unprepare(imxpriv->phy_pclk0);
642
643 return ret;
644}
645
646static int imx_sata_enable(struct ahci_host_priv *hpriv)
647{
648 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
649 struct device *dev = &imxpriv->ahci_pdev->dev;
650 int ret;
651
652 if (imxpriv->no_device)
653 return 0;
654
655 ret = ahci_platform_enable_regulators(hpriv);
656 if (ret)
657 return ret;
658
659 ret = clk_prepare_enable(imxpriv->sata_ref_clk);
660 if (ret < 0)
661 goto disable_regulator;
662
663 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
664 /*
665 * set PHY Paremeters, two steps to configure the GPR13,
666 * one write for rest of parameters, mask of first write
667 * is 0x07ffffff, and the other one write for setting
668 * the mpll_clk_en.
669 */
670 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
671 IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
672 IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
673 IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
674 IMX6Q_GPR13_SATA_SPD_MODE_MASK |
675 IMX6Q_GPR13_SATA_MPLL_SS_EN |
676 IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
677 IMX6Q_GPR13_SATA_TX_BOOST_MASK |
678 IMX6Q_GPR13_SATA_TX_LVL_MASK |
679 IMX6Q_GPR13_SATA_MPLL_CLK_EN |
680 IMX6Q_GPR13_SATA_TX_EDGE_RATE,
681 imxpriv->phy_params);
682 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
683 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
684 IMX6Q_GPR13_SATA_MPLL_CLK_EN);
685
686 usleep_range(100, 200);
687
688 ret = imx_sata_phy_reset(hpriv);
689 if (ret) {
690 dev_err(dev, "failed to reset phy: %d\n", ret);
691 goto disable_clk;
692 }
693 } else if (imxpriv->type == AHCI_IMX8QM) {
694 ret = imx8_sata_enable(hpriv);
695 }
696
697 usleep_range(1000, 2000);
698
699 return 0;
700
701disable_clk:
702 clk_disable_unprepare(imxpriv->sata_ref_clk);
703disable_regulator:
704 ahci_platform_disable_regulators(hpriv);
705
706 return ret;
707}
708
709static void imx_sata_disable(struct ahci_host_priv *hpriv)
710{
711 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
712
713 if (imxpriv->no_device)
714 return;
715
716 switch (imxpriv->type) {
717 case AHCI_IMX6QP:
718 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
719 IMX6Q_GPR5_SATA_SW_PD,
720 IMX6Q_GPR5_SATA_SW_PD);
721 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
722 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
723 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
724 break;
725
726 case AHCI_IMX6Q:
727 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
728 IMX6Q_GPR13_SATA_MPLL_CLK_EN,
729 !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
730 break;
731
732 case AHCI_IMX8QM:
733 clk_disable_unprepare(imxpriv->epcs_rx_clk);
734 clk_disable_unprepare(imxpriv->epcs_tx_clk);
735 break;
736
737 default:
738 break;
739 }
740
741 clk_disable_unprepare(imxpriv->sata_ref_clk);
742
743 ahci_platform_disable_regulators(hpriv);
744}
745
746static void ahci_imx_error_handler(struct ata_port *ap)
747{
748 u32 reg_val;
749 struct ata_device *dev;
750 struct ata_host *host = dev_get_drvdata(ap->dev);
751 struct ahci_host_priv *hpriv = host->private_data;
752 void __iomem *mmio = hpriv->mmio;
753 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
754
755 ahci_error_handler(ap);
756
757 if (!(imxpriv->first_time) || ahci_imx_hotplug)
758 return;
759
760 imxpriv->first_time = false;
761
762 ata_for_each_dev(dev, &ap->link, ENABLED)
763 return;
764 /*
765 * Disable link to save power. An imx ahci port can't be recovered
766 * without full reset once the pddq mode is enabled making it
767 * impossible to use as part of libata LPM.
768 */
769 reg_val = readl(mmio + IMX_P0PHYCR);
770 writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
771 imx_sata_disable(hpriv);
772 imxpriv->no_device = true;
773
774 dev_info(ap->dev, "no device found, disabling link.\n");
775 dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
776}
777
778static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
779 unsigned long deadline)
780{
781 struct ata_port *ap = link->ap;
782 struct ata_host *host = dev_get_drvdata(ap->dev);
783 struct ahci_host_priv *hpriv = host->private_data;
784 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
785 int ret = -EIO;
786
787 if (imxpriv->type == AHCI_IMX53)
788 ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
789 else
790 ret = ahci_ops.softreset(link, class, deadline);
791
792 return ret;
793}
794
795static struct ata_port_operations ahci_imx_ops = {
796 .inherits = &ahci_ops,
797 .host_stop = ahci_imx_host_stop,
798 .error_handler = ahci_imx_error_handler,
799 .softreset = ahci_imx_softreset,
800};
801
802static const struct ata_port_info ahci_imx_port_info = {
803 .flags = AHCI_FLAG_COMMON,
804 .pio_mask = ATA_PIO4,
805 .udma_mask = ATA_UDMA6,
806 .port_ops = &ahci_imx_ops,
807};
808
809static const struct of_device_id imx_ahci_of_match[] = {
810 { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
811 { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
812 { .compatible = "fsl,imx6qp-ahci", .data = (void *)AHCI_IMX6QP },
813 { .compatible = "fsl,imx8qm-ahci", .data = (void *)AHCI_IMX8QM },
814 {},
815};
816MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
817
818struct reg_value {
819 u32 of_value;
820 u32 reg_value;
821};
822
823struct reg_property {
824 const char *name;
825 const struct reg_value *values;
826 size_t num_values;
827 u32 def_value;
828 u32 set_value;
829};
830
831static const struct reg_value gpr13_tx_level[] = {
832 { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
833 { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
834 { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
835 { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
836 { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
837 { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
838 { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
839 { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
840 { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
841 { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
842 { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
843 { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
844 { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
845 { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
846 { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
847 { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
848 { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
849 { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
850 { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
851 { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
852 { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
853 { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
854 { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
855 { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
856 { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
857 { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
858 { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
859 { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
860 { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
861 { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
862 { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
863 { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
864};
865
866static const struct reg_value gpr13_tx_boost[] = {
867 { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
868 { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
869 { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
870 { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
871 { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
872 { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
873 { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
874 { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
875 { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
876 { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
877 { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
878 { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
879 { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
880 { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
881 { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
882 { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
883};
884
885static const struct reg_value gpr13_tx_atten[] = {
886 { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
887 { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
888 { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
889 { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
890 { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
891 { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
892};
893
894static const struct reg_value gpr13_rx_eq[] = {
895 { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
896 { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
897 { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
898 { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
899 { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
900 { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
901 { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
902 { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
903};
904
905static const struct reg_property gpr13_props[] = {
906 {
907 .name = "fsl,transmit-level-mV",
908 .values = gpr13_tx_level,
909 .num_values = ARRAY_SIZE(gpr13_tx_level),
910 .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
911 }, {
912 .name = "fsl,transmit-boost-mdB",
913 .values = gpr13_tx_boost,
914 .num_values = ARRAY_SIZE(gpr13_tx_boost),
915 .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
916 }, {
917 .name = "fsl,transmit-atten-16ths",
918 .values = gpr13_tx_atten,
919 .num_values = ARRAY_SIZE(gpr13_tx_atten),
920 .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
921 }, {
922 .name = "fsl,receive-eq-mdB",
923 .values = gpr13_rx_eq,
924 .num_values = ARRAY_SIZE(gpr13_rx_eq),
925 .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
926 }, {
927 .name = "fsl,no-spread-spectrum",
928 .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
929 .set_value = 0,
930 },
931};
932
933static u32 imx_ahci_parse_props(struct device *dev,
934 const struct reg_property *prop, size_t num)
935{
936 struct device_node *np = dev->of_node;
937 u32 reg_value = 0;
938 int i, j;
939
940 for (i = 0; i < num; i++, prop++) {
941 u32 of_val;
942
943 if (prop->num_values == 0) {
944 if (of_property_read_bool(np, prop->name))
945 reg_value |= prop->set_value;
946 else
947 reg_value |= prop->def_value;
948 continue;
949 }
950
951 if (of_property_read_u32(np, prop->name, &of_val)) {
952 dev_info(dev, "%s not specified, using %08x\n",
953 prop->name, prop->def_value);
954 reg_value |= prop->def_value;
955 continue;
956 }
957
958 for (j = 0; j < prop->num_values; j++) {
959 if (prop->values[j].of_value == of_val) {
960 dev_info(dev, "%s value %u, using %08x\n",
961 prop->name, of_val, prop->values[j].reg_value);
962 reg_value |= prop->values[j].reg_value;
963 break;
964 }
965 }
966
967 if (j == prop->num_values) {
968 dev_err(dev, "DT property %s is not a valid value\n",
969 prop->name);
970 reg_value |= prop->def_value;
971 }
972 }
973
974 return reg_value;
975}
976
977static struct scsi_host_template ahci_platform_sht = {
978 AHCI_SHT(DRV_NAME),
979};
980
981static int imx8_sata_probe(struct device *dev, struct imx_ahci_priv *imxpriv)
982{
983 int ret;
984 struct resource *phy_res;
985 struct platform_device *pdev = imxpriv->ahci_pdev;
986 struct device_node *np = dev->of_node;
987
988 if (of_property_read_u32(np, "fsl,phy-imp", &imxpriv->imped_ratio))
989 imxpriv->imped_ratio = IMX8QM_SATA_PHY_IMPED_RATIO_85OHM;
990 phy_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
991 if (phy_res) {
992 imxpriv->phy_base = devm_ioremap(dev, phy_res->start,
993 resource_size(phy_res));
994 if (!imxpriv->phy_base) {
995 dev_err(dev, "error with ioremap\n");
996 return -ENOMEM;
997 }
998 } else {
999 dev_err(dev, "missing *phy* reg region.\n");
1000 return -ENOMEM;
1001 }
1002 imxpriv->gpr =
1003 syscon_regmap_lookup_by_phandle(np, "hsio");
1004 if (IS_ERR(imxpriv->gpr)) {
1005 dev_err(dev, "unable to find gpr registers\n");
1006 return PTR_ERR(imxpriv->gpr);
1007 }
1008
1009 imxpriv->epcs_tx_clk = devm_clk_get(dev, "epcs_tx");
1010 if (IS_ERR(imxpriv->epcs_tx_clk)) {
1011 dev_err(dev, "can't get epcs_tx_clk clock.\n");
1012 return PTR_ERR(imxpriv->epcs_tx_clk);
1013 }
1014 imxpriv->epcs_rx_clk = devm_clk_get(dev, "epcs_rx");
1015 if (IS_ERR(imxpriv->epcs_rx_clk)) {
1016 dev_err(dev, "can't get epcs_rx_clk clock.\n");
1017 return PTR_ERR(imxpriv->epcs_rx_clk);
1018 }
1019 imxpriv->phy_pclk0 = devm_clk_get(dev, "phy_pclk0");
1020 if (IS_ERR(imxpriv->phy_pclk0)) {
1021 dev_err(dev, "can't get phy_pclk0 clock.\n");
1022 return PTR_ERR(imxpriv->phy_pclk0);
1023 }
1024 imxpriv->phy_pclk1 = devm_clk_get(dev, "phy_pclk1");
1025 if (IS_ERR(imxpriv->phy_pclk1)) {
1026 dev_err(dev, "can't get phy_pclk1 clock.\n");
1027 return PTR_ERR(imxpriv->phy_pclk1);
1028 }
1029 imxpriv->phy_apbclk = devm_clk_get(dev, "phy_apbclk");
1030 if (IS_ERR(imxpriv->phy_apbclk)) {
1031 dev_err(dev, "can't get phy_apbclk clock.\n");
1032 return PTR_ERR(imxpriv->phy_apbclk);
1033 }
1034
1035 /* Fetch GPIO, then enable the external OSC */
1036 imxpriv->clkreq_gpio = of_get_named_gpio(np, "clkreq-gpio", 0);
1037 if (gpio_is_valid(imxpriv->clkreq_gpio)) {
1038 ret = devm_gpio_request_one(dev, imxpriv->clkreq_gpio,
1039 GPIOF_OUT_INIT_LOW,
1040 "SATA CLKREQ");
1041 if (ret == -EBUSY) {
1042 dev_info(dev, "clkreq had been initialized.\n");
1043 } else if (ret) {
1044 dev_err(dev, "%d unable to get clkreq.\n", ret);
1045 return ret;
1046 }
1047 } else if (imxpriv->clkreq_gpio == -EPROBE_DEFER) {
1048 return imxpriv->clkreq_gpio;
1049 }
1050
1051 return 0;
1052}
1053
1054static int imx_ahci_probe(struct platform_device *pdev)
1055{
1056 struct device *dev = &pdev->dev;
1057 const struct of_device_id *of_id;
1058 struct ahci_host_priv *hpriv;
1059 struct imx_ahci_priv *imxpriv;
1060 unsigned int reg_val;
1061 int ret;
1062
1063 of_id = of_match_device(imx_ahci_of_match, dev);
1064 if (!of_id)
1065 return -EINVAL;
1066
1067 imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
1068 if (!imxpriv)
1069 return -ENOMEM;
1070
1071 imxpriv->ahci_pdev = pdev;
1072 imxpriv->no_device = false;
1073 imxpriv->first_time = true;
1074 imxpriv->type = (enum ahci_imx_type)of_id->data;
1075
1076 imxpriv->sata_clk = devm_clk_get(dev, "sata");
1077 if (IS_ERR(imxpriv->sata_clk)) {
1078 dev_err(dev, "can't get sata clock.\n");
1079 return PTR_ERR(imxpriv->sata_clk);
1080 }
1081
1082 imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
1083 if (IS_ERR(imxpriv->sata_ref_clk)) {
1084 dev_err(dev, "can't get sata_ref clock.\n");
1085 return PTR_ERR(imxpriv->sata_ref_clk);
1086 }
1087
1088 imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
1089 if (IS_ERR(imxpriv->ahb_clk)) {
1090 dev_err(dev, "can't get ahb clock.\n");
1091 return PTR_ERR(imxpriv->ahb_clk);
1092 }
1093
1094 if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
1095 u32 reg_value;
1096
1097 imxpriv->gpr = syscon_regmap_lookup_by_compatible(
1098 "fsl,imx6q-iomuxc-gpr");
1099 if (IS_ERR(imxpriv->gpr)) {
1100 dev_err(dev,
1101 "failed to find fsl,imx6q-iomux-gpr regmap\n");
1102 return PTR_ERR(imxpriv->gpr);
1103 }
1104
1105 reg_value = imx_ahci_parse_props(dev, gpr13_props,
1106 ARRAY_SIZE(gpr13_props));
1107
1108 imxpriv->phy_params =
1109 IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
1110 IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
1111 IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
1112 reg_value;
1113 } else if (imxpriv->type == AHCI_IMX8QM) {
1114 ret = imx8_sata_probe(dev, imxpriv);
1115 if (ret)
1116 return ret;
1117 }
1118
1119 hpriv = ahci_platform_get_resources(pdev, 0);
1120 if (IS_ERR(hpriv))
1121 return PTR_ERR(hpriv);
1122
1123 hpriv->plat_data = imxpriv;
1124
1125 ret = clk_prepare_enable(imxpriv->sata_clk);
1126 if (ret)
1127 return ret;
1128
1129 if (imxpriv->type == AHCI_IMX53 &&
1130 IS_ENABLED(CONFIG_HWMON)) {
1131 /* Add the temperature monitor */
1132 struct device *hwmon_dev;
1133
1134 hwmon_dev =
1135 devm_hwmon_device_register_with_groups(dev,
1136 "sata_ahci",
1137 hpriv,
1138 fsl_sata_ahci_groups);
1139 if (IS_ERR(hwmon_dev)) {
1140 ret = PTR_ERR(hwmon_dev);
1141 goto disable_clk;
1142 }
1143 devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
1144 &fsl_sata_ahci_of_thermal_ops);
1145 dev_info(dev, "%s: sensor 'sata_ahci'\n", dev_name(hwmon_dev));
1146 }
1147
1148 ret = imx_sata_enable(hpriv);
1149 if (ret)
1150 goto disable_clk;
1151
1152 /*
1153 * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
1154 * and IP vendor specific register IMX_TIMER1MS.
1155 * Configure CAP_SSS (support stagered spin up).
1156 * Implement the port0.
1157 * Get the ahb clock rate, and configure the TIMER1MS register.
1158 */
1159 reg_val = readl(hpriv->mmio + HOST_CAP);
1160 if (!(reg_val & HOST_CAP_SSS)) {
1161 reg_val |= HOST_CAP_SSS;
1162 writel(reg_val, hpriv->mmio + HOST_CAP);
1163 }
1164 reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
1165 if (!(reg_val & 0x1)) {
1166 reg_val |= 0x1;
1167 writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
1168 }
1169
1170 reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
1171 writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
1172
1173 ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
1174 &ahci_platform_sht);
1175 if (ret)
1176 goto disable_sata;
1177
1178 return 0;
1179
1180disable_sata:
1181 imx_sata_disable(hpriv);
1182disable_clk:
1183 clk_disable_unprepare(imxpriv->sata_clk);
1184 return ret;
1185}
1186
1187static void ahci_imx_host_stop(struct ata_host *host)
1188{
1189 struct ahci_host_priv *hpriv = host->private_data;
1190 struct imx_ahci_priv *imxpriv = hpriv->plat_data;
1191
1192 imx_sata_disable(hpriv);
1193 clk_disable_unprepare(imxpriv->sata_clk);
1194}
1195
1196#ifdef CONFIG_PM_SLEEP
1197static int imx_ahci_suspend(struct device *dev)
1198{
1199 struct ata_host *host = dev_get_drvdata(dev);
1200 struct ahci_host_priv *hpriv = host->private_data;
1201 int ret;
1202
1203 ret = ahci_platform_suspend_host(dev);
1204 if (ret)
1205 return ret;
1206
1207 imx_sata_disable(hpriv);
1208
1209 return 0;
1210}
1211
1212static int imx_ahci_resume(struct device *dev)
1213{
1214 struct ata_host *host = dev_get_drvdata(dev);
1215 struct ahci_host_priv *hpriv = host->private_data;
1216 int ret;
1217
1218 ret = imx_sata_enable(hpriv);
1219 if (ret)
1220 return ret;
1221
1222 return ahci_platform_resume_host(dev);
1223}
1224#endif
1225
1226static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
1227
1228static struct platform_driver imx_ahci_driver = {
1229 .probe = imx_ahci_probe,
1230 .remove = ata_platform_remove_one,
1231 .driver = {
1232 .name = DRV_NAME,
1233 .of_match_table = imx_ahci_of_match,
1234 .pm = &ahci_imx_pm_ops,
1235 },
1236};
1237module_platform_driver(imx_ahci_driver);
1238
1239MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
1240MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>");
1241MODULE_LICENSE("GPL");
1242MODULE_ALIAS("ahci:imx");