3a2bbb7032539c9c207f7320f41589fd612da30b
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-rockchip-emmc.c
1 /*
2  * Rockchip emmc PHY driver
3  *
4  * Copyright (C) 2016 Shawn Lin <shawn.lin@rock-chips.com>
5  * Copyright (C) 2016 ROCKCHIP, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25
26 /*
27  * The higher 16-bit of this register is used for write protection
28  * only if BIT(x + 16) set to 1 the BIT(x) can be written.
29  */
30 #define HIWORD_UPDATE(val, mask, shift) \
31                 ((val) << (shift) | (mask) << ((shift) + 16))
32
33 /* Register definition */
34 #define GRF_EMMCPHY_CON0                0x0
35 #define GRF_EMMCPHY_CON1                0x4
36 #define GRF_EMMCPHY_CON2                0x8
37 #define GRF_EMMCPHY_CON3                0xc
38 #define GRF_EMMCPHY_CON4                0x10
39 #define GRF_EMMCPHY_CON5                0x14
40 #define GRF_EMMCPHY_CON6                0x18
41 #define GRF_EMMCPHY_STATUS              0x20
42
43 #define PHYCTRL_PDB_MASK                0x1
44 #define PHYCTRL_PDB_SHIFT               0x0
45 #define PHYCTRL_PDB_PWR_ON              0x1
46 #define PHYCTRL_PDB_PWR_OFF             0x0
47 #define PHYCTRL_ENDLL_MASK              0x1
48 #define PHYCTRL_ENDLL_SHIFT             0x1
49 #define PHYCTRL_ENDLL_ENABLE            0x1
50 #define PHYCTRL_ENDLL_DISABLE           0x0
51 #define PHYCTRL_CALDONE_MASK            0x1
52 #define PHYCTRL_CALDONE_SHIFT           0x6
53 #define PHYCTRL_CALDONE_DONE            0x1
54 #define PHYCTRL_CALDONE_GOING           0x0
55 #define PHYCTRL_DLLRDY_MASK             0x1
56 #define PHYCTRL_DLLRDY_SHIFT            0x5
57 #define PHYCTRL_DLLRDY_DONE             0x1
58 #define PHYCTRL_DLLRDY_GOING            0x0
59 #define PHYCTRL_FREQSEL_200M            0x0
60 #define PHYCTRL_FREQSEL_50M             0x1
61 #define PHYCTRL_FREQSEL_100M            0x2
62 #define PHYCTRL_FREQSEL_150M            0x3
63 #define PHYCTRL_FREQSEL_MASK            0x3
64 #define PHYCTRL_FREQSEL_SHIFT           0xc
65 #define PHYCTRL_DR_MASK                 0x7
66 #define PHYCTRL_DR_SHIFT                0x4
67 #define PHYCTRL_DR_50OHM                0x0
68 #define PHYCTRL_DR_33OHM                0x1
69 #define PHYCTRL_DR_66OHM                0x2
70 #define PHYCTRL_DR_100OHM               0x3
71 #define PHYCTRL_DR_40OHM                0x4
72 #define PHYCTRL_OTAPDLYENA              0x1
73 #define PHYCTRL_OTAPDLYENA_MASK         0x1
74 #define PHYCTRL_OTAPDLYENA_SHIFT        11
75 #define PHYCTRL_OTAPDLYSEL_MASK         0xf
76 #define PHYCTRL_OTAPDLYSEL_SHIFT        7
77
78 struct rockchip_emmc_phy {
79         unsigned int    reg_offset;
80         struct regmap   *reg_base;
81         u32     freq_sel;
82         u32     dr_sel;
83         u32     opdelay;
84 };
85
86 static int rockchip_emmc_phy_power(struct rockchip_emmc_phy *rk_phy,
87                                    bool on_off)
88 {
89         unsigned int caldone;
90         unsigned int dllrdy;
91
92         /*
93          * Keep phyctrl_pdb and phyctrl_endll low to allow
94          * initialization of CALIO state M/C DFFs
95          */
96         regmap_write(rk_phy->reg_base,
97                      rk_phy->reg_offset + GRF_EMMCPHY_CON6,
98                      HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF,
99                                    PHYCTRL_PDB_MASK,
100                                    PHYCTRL_PDB_SHIFT));
101         regmap_write(rk_phy->reg_base,
102                      rk_phy->reg_offset + GRF_EMMCPHY_CON6,
103                      HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE,
104                                    PHYCTRL_ENDLL_MASK,
105                                    PHYCTRL_ENDLL_SHIFT));
106
107         /* Already finish power_off above */
108         if (on_off == PHYCTRL_PDB_PWR_OFF)
109                 return 0;
110
111         /*
112          * According to the user manual, calpad calibration
113          * cycle takes more than 2us without the minimal recommended
114          * value, so we may need a little margin here
115          */
116         udelay(3);
117         regmap_write(rk_phy->reg_base,
118                      rk_phy->reg_offset + GRF_EMMCPHY_CON6,
119                      HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON,
120                                    PHYCTRL_PDB_MASK,
121                                    PHYCTRL_PDB_SHIFT));
122
123         /*
124          * According to the user manual, it asks driver to
125          * wait 5us for calpad busy trimming
126          */
127         udelay(5);
128         regmap_read(rk_phy->reg_base,
129                     rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
130                     &caldone);
131         caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
132         if (caldone != PHYCTRL_CALDONE_DONE) {
133                 pr_err("rockchip_emmc_phy_power: caldone timeout.\n");
134                 return -ETIMEDOUT;
135         }
136
137         regmap_write(rk_phy->reg_base,
138                      rk_phy->reg_offset + GRF_EMMCPHY_CON6,
139                      HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE,
140                                    PHYCTRL_ENDLL_MASK,
141                                    PHYCTRL_ENDLL_SHIFT));
142         /*
143          * After enable analog DLL circuits, we need extra 10.2us
144          * for dll to be ready for work. But according to the test, we
145          * find some chips need more than 25us.
146          */
147         udelay(30);
148         regmap_read(rk_phy->reg_base,
149                     rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
150                     &dllrdy);
151         dllrdy = (dllrdy >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK;
152         if (dllrdy != PHYCTRL_DLLRDY_DONE) {
153                 pr_err("rockchip_emmc_phy_power: dllrdy timeout.\n");
154                 return -ETIMEDOUT;
155         }
156
157         return 0;
158 }
159
160 static int rockchip_emmc_phy_init(struct phy *phy)
161 {
162         struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
163
164         regmap_write(rk_phy->reg_base,
165                      rk_phy->reg_offset + GRF_EMMCPHY_CON0,
166                      HIWORD_UPDATE(rk_phy->freq_sel,
167                                    PHYCTRL_FREQSEL_MASK,
168                                    PHYCTRL_FREQSEL_SHIFT));
169
170         regmap_write(rk_phy->reg_base,
171                      rk_phy->reg_offset + GRF_EMMCPHY_CON6,
172                      HIWORD_UPDATE(rk_phy->dr_sel,
173                                    PHYCTRL_DR_MASK,
174                                    PHYCTRL_DR_SHIFT));
175
176         regmap_write(rk_phy->reg_base,
177                      rk_phy->reg_offset + GRF_EMMCPHY_CON0,
178                      HIWORD_UPDATE(PHYCTRL_OTAPDLYENA,
179                                    PHYCTRL_OTAPDLYENA_MASK,
180                                    PHYCTRL_OTAPDLYENA_SHIFT));
181
182         regmap_write(rk_phy->reg_base,
183                      rk_phy->reg_offset + GRF_EMMCPHY_CON0,
184                      HIWORD_UPDATE(rk_phy->opdelay,
185                                    PHYCTRL_OTAPDLYSEL_MASK,
186                                    PHYCTRL_OTAPDLYSEL_SHIFT));
187
188         return 0;
189 }
190
191 static int rockchip_emmc_phy_power_off(struct phy *phy)
192 {
193         struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
194         int ret = 0;
195
196         /* Power down emmc phy analog blocks */
197         ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_OFF);
198         if (ret)
199                 return ret;
200
201         return 0;
202 }
203
204 static int rockchip_emmc_phy_power_on(struct phy *phy)
205 {
206         struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
207         int ret = 0;
208
209         /* Power up emmc phy analog blocks */
210         ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_ON);
211         if (ret)
212                 return ret;
213
214         return 0;
215 }
216
217 static const struct phy_ops ops = {
218         .init           = rockchip_emmc_phy_init,
219         .power_on       = rockchip_emmc_phy_power_on,
220         .power_off      = rockchip_emmc_phy_power_off,
221         .owner          = THIS_MODULE,
222 };
223
224 static int rockchip_emmc_phy_probe(struct platform_device *pdev)
225 {
226         struct device *dev = &pdev->dev;
227         struct rockchip_emmc_phy *rk_phy;
228         struct phy *generic_phy;
229         struct phy_provider *phy_provider;
230         struct regmap *grf;
231         unsigned int reg_offset;
232         u32 freq_sel;
233         u32 dr_sel;
234         u32 opdelay;
235
236         grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
237         if (IS_ERR(grf)) {
238                 dev_err(dev, "Missing rockchip,grf property\n");
239                 return PTR_ERR(grf);
240         }
241
242         rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
243         if (!rk_phy)
244                 return -ENOMEM;
245
246         if (of_property_read_u32(dev->of_node, "reg-offset", &reg_offset)) {
247                 dev_err(dev, "missing reg property in node %s\n",
248                         dev->of_node->name);
249                 return -EINVAL;
250         }
251
252         rk_phy->freq_sel = 0x0;
253         if (!of_property_read_u32(dev->of_node, "freq-sel", &freq_sel)) {
254                 switch (freq_sel) {
255                 case 50000000:
256                         rk_phy->freq_sel = PHYCTRL_FREQSEL_50M;
257                         break;
258                 case 100000000:
259                         rk_phy->freq_sel = PHYCTRL_FREQSEL_100M;
260                         break;
261                 case 150000000:
262                         rk_phy->freq_sel = PHYCTRL_FREQSEL_150M;
263                         break;
264                 case 200000000:
265                         rk_phy->freq_sel = PHYCTRL_FREQSEL_200M;
266                         break;
267                 default:
268                         dev_info(dev, "Not support freq_sel, default 200M\n");
269                         break;
270                 }
271         }
272
273         rk_phy->dr_sel = 0x0;
274         if (!of_property_read_u32(dev->of_node, "dr-sel", &dr_sel)) {
275                 switch (dr_sel) {
276                 case 50:
277                         rk_phy->dr_sel = PHYCTRL_DR_50OHM;
278                         break;
279                 case 33:
280                         rk_phy->dr_sel = PHYCTRL_DR_33OHM;
281                         break;
282                 case 66:
283                         rk_phy->dr_sel = PHYCTRL_DR_66OHM;
284                         break;
285                 case 100:
286                         rk_phy->dr_sel = PHYCTRL_DR_100OHM;
287                         break;
288                 case 40:
289                         rk_phy->dr_sel = PHYCTRL_DR_40OHM;
290                         break;
291                 default:
292                         dev_info(dev, "Not support dr_sel, default 50OHM\n");
293                         break;
294                 }
295         }
296
297         rk_phy->opdelay = 0x4;
298         if (!of_property_read_u32(dev->of_node, "opdelay", &opdelay)) {
299                 if (opdelay > 15)
300                         dev_info(dev, "opdelay shouldn't larger than 15\n");
301                 else
302                         rk_phy->opdelay = opdelay;
303         }
304
305         rk_phy->reg_offset = reg_offset;
306         rk_phy->reg_base = grf;
307
308         generic_phy = devm_phy_create(dev, dev->of_node, &ops);
309         if (IS_ERR(generic_phy)) {
310                 dev_err(dev, "failed to create PHY\n");
311                 return PTR_ERR(generic_phy);
312         }
313
314         phy_set_drvdata(generic_phy, rk_phy);
315         phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
316
317         return PTR_ERR_OR_ZERO(phy_provider);
318 }
319
320 static const struct of_device_id rockchip_emmc_phy_dt_ids[] = {
321         { .compatible = "rockchip,rk3399-emmc-phy" },
322         {}
323 };
324
325 MODULE_DEVICE_TABLE(of, rockchip_emmc_phy_dt_ids);
326
327 static struct platform_driver rockchip_emmc_driver = {
328         .probe          = rockchip_emmc_phy_probe,
329         .driver         = {
330                 .name   = "rockchip-emmc-phy",
331                 .of_match_table = rockchip_emmc_phy_dt_ids,
332         },
333 };
334
335 module_platform_driver(rockchip_emmc_driver);
336
337 MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
338 MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
339 MODULE_LICENSE("GPL v2");