1c69e88c5a1385565b909dc721ca117cde21a8aa
[muen/linux.git] / drivers / ata / ahci_imx.c
1 /*
2  * copyright (c) 2013 Freescale Semiconductor, Inc.
3  * Freescale IMX AHCI SATA platform driver
4  *
5  * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19
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>
32 #include "ahci.h"
33
34 #define DRV_NAME "ahci-imx"
35
36 enum {
37         /* Timer 1-ms Register */
38         IMX_TIMER1MS                            = 0x00e0,
39         /* Port0 PHY Control Register */
40         IMX_P0PHYCR                             = 0x0178,
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 */
47         IMX_P0PHYSR                             = 0x017c,
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,
56 };
57
58 enum ahci_imx_type {
59         AHCI_IMX53,
60         AHCI_IMX6Q,
61         AHCI_IMX6QP,
62 };
63
64 struct imx_ahci_priv {
65         struct platform_device *ahci_pdev;
66         enum ahci_imx_type type;
67         struct clk *sata_clk;
68         struct clk *sata_ref_clk;
69         struct clk *ahb_clk;
70         struct regmap *gpr;
71         bool no_device;
72         bool first_time;
73         u32 phy_params;
74 };
75
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)");
79
80 static void ahci_imx_host_stop(struct ata_host *host);
81
82 static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
83 {
84         int timeout = 10;
85         u32 crval;
86         u32 srval;
87
88         /* Assert or deassert the bit */
89         crval = readl(mmio + IMX_P0PHYCR);
90         if (assert)
91                 crval |= bit;
92         else
93                 crval &= ~bit;
94         writel(crval, mmio + IMX_P0PHYCR);
95
96         /* Wait for the cr_ack signal */
97         do {
98                 srval = readl(mmio + IMX_P0PHYSR);
99                 if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
100                         break;
101                 usleep_range(100, 200);
102         } while (--timeout);
103
104         return timeout ? 0 : -ETIMEDOUT;
105 }
106
107 static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
108 {
109         u32 crval = addr;
110         int ret;
111
112         /* Supply the address on cr_data_in */
113         writel(crval, mmio + IMX_P0PHYCR);
114
115         /* Assert the cr_cap_addr signal */
116         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
117         if (ret)
118                 return ret;
119
120         /* Deassert cr_cap_addr */
121         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
122         if (ret)
123                 return ret;
124
125         return 0;
126 }
127
128 static int imx_phy_reg_write(u16 val, void __iomem *mmio)
129 {
130         u32 crval = val;
131         int ret;
132
133         /* Supply the data on cr_data_in */
134         writel(crval, mmio + IMX_P0PHYCR);
135
136         /* Assert the cr_cap_data signal */
137         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
138         if (ret)
139                 return ret;
140
141         /* Deassert cr_cap_data */
142         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
143         if (ret)
144                 return ret;
145
146         if (val & IMX_CLOCK_RESET_RESET) {
147                 /*
148                  * In case we're resetting the phy, it's unable to acknowledge,
149                  * so we return immediately here.
150                  */
151                 crval |= IMX_P0PHYCR_CR_WRITE;
152                 writel(crval, mmio + IMX_P0PHYCR);
153                 goto out;
154         }
155
156         /* Assert the cr_write signal */
157         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
158         if (ret)
159                 return ret;
160
161         /* Deassert cr_write */
162         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
163         if (ret)
164                 return ret;
165
166 out:
167         return 0;
168 }
169
170 static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
171 {
172         int ret;
173
174         /* Assert the cr_read signal */
175         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
176         if (ret)
177                 return ret;
178
179         /* Capture the data from cr_data_out[] */
180         *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
181
182         /* Deassert cr_read */
183         ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
184         if (ret)
185                 return ret;
186
187         return 0;
188 }
189
190 static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
191 {
192         struct imx_ahci_priv *imxpriv = hpriv->plat_data;
193         void __iomem *mmio = hpriv->mmio;
194         int timeout = 10;
195         u16 val;
196         int ret;
197
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);
202
203                 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
204                                    IMX6Q_GPR5_SATA_SW_RST, 0);
205                 udelay(50);
206                 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
207                                    IMX6Q_GPR5_SATA_SW_RST,
208                                    IMX6Q_GPR5_SATA_SW_RST);
209                 return 0;
210         }
211
212         /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
213         ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
214         if (ret)
215                 return ret;
216         ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
217         if (ret)
218                 return ret;
219
220         /* Wait for PHY RX_PLL to be stable */
221         do {
222                 usleep_range(100, 200);
223                 ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
224                 if (ret)
225                         return ret;
226                 ret = imx_phy_reg_read(&val, mmio);
227                 if (ret)
228                         return ret;
229                 if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
230                         break;
231         } while (--timeout);
232
233         return timeout ? 0 : -ETIMEDOUT;
234 }
235
236 enum {
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,
243 };
244
245 static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
246 {
247         u16 adc_out_reg, read_sum;
248         u32 index, read_attempt;
249         const u32 attempt_limit = 200;
250
251         imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
252         imx_phy_reg_write(rtune_ctl_reg, mmio);
253
254         /* two dummy read */
255         index = 0;
256         read_attempt = 0;
257         adc_out_reg = 0;
258         imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
259         while (index < 2) {
260                 imx_phy_reg_read(&adc_out_reg, mmio);
261                 /* check if valid */
262                 if (adc_out_reg & 0x400)
263                         index++;
264
265                 read_attempt++;
266                 if (read_attempt > attempt_limit) {
267                         dev_err(dev, "Read REG more than %d times!\n",
268                                 attempt_limit);
269                         break;
270                 }
271         }
272
273         index = 0;
274         read_attempt = 0;
275         read_sum = 0;
276         while (index < 80) {
277                 imx_phy_reg_read(&adc_out_reg, mmio);
278                 if (adc_out_reg & 0x400) {
279                         read_sum = read_sum + (adc_out_reg & 0x3FF);
280                         index++;
281                 }
282                 read_attempt++;
283                 if (read_attempt > attempt_limit) {
284                         dev_err(dev, "Read REG more than %d times!\n",
285                                 attempt_limit);
286                         break;
287                 }
288         }
289
290         /* Use the U32 to make 1000 precision */
291         return (read_sum * 1000) / 80;
292 }
293
294 /* SATA AHCI temperature monitor */
295 static int sata_ahci_read_temperature(void *dev, int *temp)
296 {
297         u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
298         u32 str1, str2, str3, str4;
299         int m1, m2, a;
300         struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
301         void __iomem *mmio = hpriv->mmio;
302
303         /* check rd-wr to reg */
304         read_sum = 0;
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);
310
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);
315
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);
320
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);
328
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);
337
338         /* Calculate the m1 */
339         /* mpll_tst.meas_iv */
340         mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
341         /* rtune_ctl.mode */
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);
352
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);
357
358         /* restore the status  */
359         /* mpll_tst.meas_iv */
360         mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
361         /* rtune_ctl.mode */
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;
367
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);
374
375         /* Compute temperature */
376         if (!(m2 / 1000))
377                 m2 = 1000;
378         a = (m2 - m1) / (m2/1000);
379         *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
380
381         return 0;
382 }
383
384 static ssize_t sata_ahci_show_temp(struct device *dev,
385                                    struct device_attribute *da,
386                                    char *buf)
387 {
388         unsigned int temp = 0;
389         int err;
390
391         err = sata_ahci_read_temperature(dev, &temp);
392         if (err < 0)
393                 return err;
394
395         return sprintf(buf, "%u\n", temp);
396 }
397
398 static const struct thermal_zone_of_device_ops fsl_sata_ahci_of_thermal_ops = {
399         .get_temp = sata_ahci_read_temperature,
400 };
401
402 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
403
404 static struct attribute *fsl_sata_ahci_attrs[] = {
405         &sensor_dev_attr_temp1_input.dev_attr.attr,
406         NULL
407 };
408 ATTRIBUTE_GROUPS(fsl_sata_ahci);
409
410 static int imx_sata_enable(struct ahci_host_priv *hpriv)
411 {
412         struct imx_ahci_priv *imxpriv = hpriv->plat_data;
413         struct device *dev = &imxpriv->ahci_pdev->dev;
414         int ret;
415
416         if (imxpriv->no_device)
417                 return 0;
418
419         ret = ahci_platform_enable_regulators(hpriv);
420         if (ret)
421                 return ret;
422
423         ret = clk_prepare_enable(imxpriv->sata_ref_clk);
424         if (ret < 0)
425                 goto disable_regulator;
426
427         if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
428                 /*
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
432                  * the mpll_clk_en.
433                  */
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);
449
450                 usleep_range(100, 200);
451
452                 ret = imx_sata_phy_reset(hpriv);
453                 if (ret) {
454                         dev_err(dev, "failed to reset phy: %d\n", ret);
455                         goto disable_clk;
456                 }
457         }
458
459         usleep_range(1000, 2000);
460
461         return 0;
462
463 disable_clk:
464         clk_disable_unprepare(imxpriv->sata_ref_clk);
465 disable_regulator:
466         ahci_platform_disable_regulators(hpriv);
467
468         return ret;
469 }
470
471 static void imx_sata_disable(struct ahci_host_priv *hpriv)
472 {
473         struct imx_ahci_priv *imxpriv = hpriv->plat_data;
474
475         if (imxpriv->no_device)
476                 return;
477
478         switch (imxpriv->type) {
479         case AHCI_IMX6QP:
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);
486                 break;
487
488         case AHCI_IMX6Q:
489                 regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
490                                    IMX6Q_GPR13_SATA_MPLL_CLK_EN,
491                                    !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
492                 break;
493
494         default:
495                 break;
496         }
497
498         clk_disable_unprepare(imxpriv->sata_ref_clk);
499
500         ahci_platform_disable_regulators(hpriv);
501 }
502
503 static void ahci_imx_error_handler(struct ata_port *ap)
504 {
505         u32 reg_val;
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;
511
512         ahci_error_handler(ap);
513
514         if (!(imxpriv->first_time) || ahci_imx_hotplug)
515                 return;
516
517         imxpriv->first_time = false;
518
519         ata_for_each_dev(dev, &ap->link, ENABLED)
520                 return;
521         /*
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.
525          */
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;
530
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");
533 }
534
535 static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
536                        unsigned long deadline)
537 {
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;
542         int ret = -EIO;
543
544         if (imxpriv->type == AHCI_IMX53)
545                 ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
546         else
547                 ret = ahci_ops.softreset(link, class, deadline);
548
549         return ret;
550 }
551
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,
557 };
558
559 static const struct ata_port_info ahci_imx_port_info = {
560         .flags          = AHCI_FLAG_COMMON,
561         .pio_mask       = ATA_PIO4,
562         .udma_mask      = ATA_UDMA6,
563         .port_ops       = &ahci_imx_ops,
564 };
565
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 },
570         {},
571 };
572 MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
573
574 struct reg_value {
575         u32 of_value;
576         u32 reg_value;
577 };
578
579 struct reg_property {
580         const char *name;
581         const struct reg_value *values;
582         size_t num_values;
583         u32 def_value;
584         u32 set_value;
585 };
586
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 }
620 };
621
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 }
639 };
640
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 },
648 };
649
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 },
659 };
660
661 static const struct reg_property gpr13_props[] = {
662         {
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,
667         }, {
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,
672         }, {
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,
677         }, {
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,
682         }, {
683                 .name = "fsl,no-spread-spectrum",
684                 .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
685                 .set_value = 0,
686         },
687 };
688
689 static u32 imx_ahci_parse_props(struct device *dev,
690                                 const struct reg_property *prop, size_t num)
691 {
692         struct device_node *np = dev->of_node;
693         u32 reg_value = 0;
694         int i, j;
695
696         for (i = 0; i < num; i++, prop++) {
697                 u32 of_val;
698
699                 if (prop->num_values == 0) {
700                         if (of_property_read_bool(np, prop->name))
701                                 reg_value |= prop->set_value;
702                         else
703                                 reg_value |= prop->def_value;
704                         continue;
705                 }
706
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;
711                         continue;
712                 }
713
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;
719                                 break;
720                         }
721                 }
722
723                 if (j == prop->num_values) {
724                         dev_err(dev, "DT property %s is not a valid value\n",
725                                 prop->name);
726                         reg_value |= prop->def_value;
727                 }
728         }
729
730         return reg_value;
731 }
732
733 static struct scsi_host_template ahci_platform_sht = {
734         AHCI_SHT(DRV_NAME),
735 };
736
737 static int imx_ahci_probe(struct platform_device *pdev)
738 {
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;
744         int ret;
745
746         of_id = of_match_device(imx_ahci_of_match, dev);
747         if (!of_id)
748                 return -EINVAL;
749
750         imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
751         if (!imxpriv)
752                 return -ENOMEM;
753
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;
758
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);
763         }
764
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);
769         }
770
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);
775         }
776
777         if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
778                 u32 reg_value;
779
780                 imxpriv->gpr = syscon_regmap_lookup_by_compatible(
781                                                         "fsl,imx6q-iomuxc-gpr");
782                 if (IS_ERR(imxpriv->gpr)) {
783                         dev_err(dev,
784                                 "failed to find fsl,imx6q-iomux-gpr regmap\n");
785                         return PTR_ERR(imxpriv->gpr);
786                 }
787
788                 reg_value = imx_ahci_parse_props(dev, gpr13_props,
789                                                  ARRAY_SIZE(gpr13_props));
790
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 |
795                                    reg_value;
796         }
797
798         hpriv = ahci_platform_get_resources(pdev);
799         if (IS_ERR(hpriv))
800                 return PTR_ERR(hpriv);
801
802         hpriv->plat_data = imxpriv;
803
804         ret = clk_prepare_enable(imxpriv->sata_clk);
805         if (ret)
806                 return ret;
807
808         if (imxpriv->type == AHCI_IMX53 &&
809             IS_ENABLED(CONFIG_HWMON)) {
810                 /* Add the temperature monitor */
811                 struct device *hwmon_dev;
812
813                 hwmon_dev =
814                         devm_hwmon_device_register_with_groups(dev,
815                                                         "sata_ahci",
816                                                         hpriv,
817                                                         fsl_sata_ahci_groups);
818                 if (IS_ERR(hwmon_dev)) {
819                         ret = PTR_ERR(hwmon_dev);
820                         goto disable_clk;
821                 }
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));
825         }
826
827         ret = imx_sata_enable(hpriv);
828         if (ret)
829                 goto disable_clk;
830
831         /*
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.
837          */
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);
842         }
843         reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
844         if (!(reg_val & 0x1)) {
845                 reg_val |= 0x1;
846                 writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
847         }
848
849         reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
850         writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
851
852         ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
853                                       &ahci_platform_sht);
854         if (ret)
855                 goto disable_sata;
856
857         return 0;
858
859 disable_sata:
860         imx_sata_disable(hpriv);
861 disable_clk:
862         clk_disable_unprepare(imxpriv->sata_clk);
863         return ret;
864 }
865
866 static void ahci_imx_host_stop(struct ata_host *host)
867 {
868         struct ahci_host_priv *hpriv = host->private_data;
869         struct imx_ahci_priv *imxpriv = hpriv->plat_data;
870
871         imx_sata_disable(hpriv);
872         clk_disable_unprepare(imxpriv->sata_clk);
873 }
874
875 #ifdef CONFIG_PM_SLEEP
876 static int imx_ahci_suspend(struct device *dev)
877 {
878         struct ata_host *host = dev_get_drvdata(dev);
879         struct ahci_host_priv *hpriv = host->private_data;
880         int ret;
881
882         ret = ahci_platform_suspend_host(dev);
883         if (ret)
884                 return ret;
885
886         imx_sata_disable(hpriv);
887
888         return 0;
889 }
890
891 static int imx_ahci_resume(struct device *dev)
892 {
893         struct ata_host *host = dev_get_drvdata(dev);
894         struct ahci_host_priv *hpriv = host->private_data;
895         int ret;
896
897         ret = imx_sata_enable(hpriv);
898         if (ret)
899                 return ret;
900
901         return ahci_platform_resume_host(dev);
902 }
903 #endif
904
905 static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
906
907 static struct platform_driver imx_ahci_driver = {
908         .probe = imx_ahci_probe,
909         .remove = ata_platform_remove_one,
910         .driver = {
911                 .name = DRV_NAME,
912                 .of_match_table = imx_ahci_of_match,
913                 .pm = &ahci_imx_pm_ops,
914         },
915 };
916 module_platform_driver(imx_ahci_driver);
917
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");