blob: 2d059c0469782d4b538dbcf0b126da57cdd9cdf5 [file] [log] [blame]
Shawn Linc474a942016-02-03 15:22:22 +08001/*
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 */
Brian Norris675f65c2016-06-20 10:56:43 -070034#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
Shawn Linc474a942016-02-03 15:22:22 +080042
Brian Norris675f65c2016-06-20 10:56:43 -070043#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
Brian Norris36b5d462016-06-20 10:56:42 -070072#define PHYCTRL_OTAPDLYENA 0x1
73#define PHYCTRL_OTAPDLYENA_MASK 0x1
74#define PHYCTRL_OTAPDLYENA_SHIFT 0xb
75#define PHYCTRL_OTAPDLYSEL_MASK 0xf
76#define PHYCTRL_OTAPDLYSEL_SHIFT 0x7
Shawn Linc474a942016-02-03 15:22:22 +080077
78struct rockchip_emmc_phy {
79 unsigned int reg_offset;
80 struct regmap *reg_base;
81};
82
83static int rockchip_emmc_phy_power(struct rockchip_emmc_phy *rk_phy,
84 bool on_off)
85{
86 unsigned int caldone;
87 unsigned int dllrdy;
Douglas Anderson49f9ccd2016-06-20 10:56:44 -070088 unsigned long timeout;
Shawn Linc474a942016-02-03 15:22:22 +080089
90 /*
91 * Keep phyctrl_pdb and phyctrl_endll low to allow
92 * initialization of CALIO state M/C DFFs
93 */
94 regmap_write(rk_phy->reg_base,
95 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
96 HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF,
97 PHYCTRL_PDB_MASK,
98 PHYCTRL_PDB_SHIFT));
99 regmap_write(rk_phy->reg_base,
100 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
101 HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE,
102 PHYCTRL_ENDLL_MASK,
103 PHYCTRL_ENDLL_SHIFT));
104
105 /* Already finish power_off above */
106 if (on_off == PHYCTRL_PDB_PWR_OFF)
107 return 0;
108
109 /*
110 * According to the user manual, calpad calibration
111 * cycle takes more than 2us without the minimal recommended
112 * value, so we may need a little margin here
113 */
114 udelay(3);
115 regmap_write(rk_phy->reg_base,
116 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
117 HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON,
118 PHYCTRL_PDB_MASK,
119 PHYCTRL_PDB_SHIFT));
120
121 /*
122 * According to the user manual, it asks driver to
123 * wait 5us for calpad busy trimming
124 */
125 udelay(5);
126 regmap_read(rk_phy->reg_base,
127 rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
128 &caldone);
129 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
130 if (caldone != PHYCTRL_CALDONE_DONE) {
131 pr_err("rockchip_emmc_phy_power: caldone timeout.\n");
132 return -ETIMEDOUT;
133 }
134
135 regmap_write(rk_phy->reg_base,
136 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
137 HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE,
138 PHYCTRL_ENDLL_MASK,
139 PHYCTRL_ENDLL_SHIFT));
140 /*
Douglas Anderson49f9ccd2016-06-20 10:56:44 -0700141 * After enabling analog DLL circuits docs say that we need 10.2 us if
142 * our source clock is at 50 MHz and that lock time scales linearly
143 * with clock speed. If we are powering on the PHY and the card clock
144 * is super slow (like 100 kHZ) this could take as long as 5.1 ms as
145 * per the math: 10.2 us * (50000000 Hz / 100000 Hz) => 5.1 ms
146 * Hopefully we won't be running at 100 kHz, but we should still make
147 * sure we wait long enough.
Shawn Linc474a942016-02-03 15:22:22 +0800148 */
Douglas Anderson49f9ccd2016-06-20 10:56:44 -0700149 timeout = jiffies + msecs_to_jiffies(10);
150 do {
151 udelay(1);
152
153 regmap_read(rk_phy->reg_base,
154 rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
155 &dllrdy);
156 dllrdy = (dllrdy >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK;
157 if (dllrdy == PHYCTRL_DLLRDY_DONE)
158 break;
159 } while (!time_after(jiffies, timeout));
160
Shawn Linc474a942016-02-03 15:22:22 +0800161 if (dllrdy != PHYCTRL_DLLRDY_DONE) {
162 pr_err("rockchip_emmc_phy_power: dllrdy timeout.\n");
163 return -ETIMEDOUT;
164 }
165
166 return 0;
167}
168
169static int rockchip_emmc_phy_power_off(struct phy *phy)
170{
171 struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
172 int ret = 0;
173
174 /* Power down emmc phy analog blocks */
175 ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_OFF);
176 if (ret)
177 return ret;
178
179 return 0;
180}
181
182static int rockchip_emmc_phy_power_on(struct phy *phy)
183{
184 struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
185 int ret = 0;
186
Shawn Lind7485772016-06-20 10:56:41 -0700187 /* DLL operation: 200 MHz */
188 regmap_write(rk_phy->reg_base,
189 rk_phy->reg_offset + GRF_EMMCPHY_CON0,
190 HIWORD_UPDATE(PHYCTRL_FREQSEL_200M,
191 PHYCTRL_FREQSEL_MASK,
192 PHYCTRL_FREQSEL_SHIFT));
193
194 /* Drive impedance: 50 Ohm */
195 regmap_write(rk_phy->reg_base,
196 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
197 HIWORD_UPDATE(PHYCTRL_DR_50OHM,
198 PHYCTRL_DR_MASK,
199 PHYCTRL_DR_SHIFT));
200
Brian Norris36b5d462016-06-20 10:56:42 -0700201 /* Output tap delay: enable */
202 regmap_write(rk_phy->reg_base,
203 rk_phy->reg_offset + GRF_EMMCPHY_CON0,
204 HIWORD_UPDATE(PHYCTRL_OTAPDLYENA,
205 PHYCTRL_OTAPDLYENA_MASK,
206 PHYCTRL_OTAPDLYENA_SHIFT));
207
208 /* Output tap delay */
209 regmap_write(rk_phy->reg_base,
210 rk_phy->reg_offset + GRF_EMMCPHY_CON0,
211 HIWORD_UPDATE(4,
212 PHYCTRL_OTAPDLYSEL_MASK,
213 PHYCTRL_OTAPDLYSEL_SHIFT));
214
Shawn Linc474a942016-02-03 15:22:22 +0800215 /* Power up emmc phy analog blocks */
216 ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_ON);
217 if (ret)
218 return ret;
219
220 return 0;
221}
222
223static const struct phy_ops ops = {
224 .power_on = rockchip_emmc_phy_power_on,
225 .power_off = rockchip_emmc_phy_power_off,
226 .owner = THIS_MODULE,
227};
228
229static int rockchip_emmc_phy_probe(struct platform_device *pdev)
230{
231 struct device *dev = &pdev->dev;
232 struct rockchip_emmc_phy *rk_phy;
233 struct phy *generic_phy;
234 struct phy_provider *phy_provider;
235 struct regmap *grf;
236 unsigned int reg_offset;
237
Heiko Stuebner332184a2016-03-24 22:29:02 +0100238 if (!dev->parent || !dev->parent->of_node)
239 return -ENODEV;
240
241 grf = syscon_node_to_regmap(dev->parent->of_node);
Shawn Linc474a942016-02-03 15:22:22 +0800242 if (IS_ERR(grf)) {
243 dev_err(dev, "Missing rockchip,grf property\n");
244 return PTR_ERR(grf);
245 }
246
247 rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
248 if (!rk_phy)
249 return -ENOMEM;
250
251 if (of_property_read_u32(dev->of_node, "reg", &reg_offset)) {
252 dev_err(dev, "missing reg property in node %s\n",
253 dev->of_node->name);
254 return -EINVAL;
255 }
256
257 rk_phy->reg_offset = reg_offset;
258 rk_phy->reg_base = grf;
259
260 generic_phy = devm_phy_create(dev, dev->of_node, &ops);
261 if (IS_ERR(generic_phy)) {
262 dev_err(dev, "failed to create PHY\n");
263 return PTR_ERR(generic_phy);
264 }
265
266 phy_set_drvdata(generic_phy, rk_phy);
267 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
268
269 return PTR_ERR_OR_ZERO(phy_provider);
270}
271
272static const struct of_device_id rockchip_emmc_phy_dt_ids[] = {
273 { .compatible = "rockchip,rk3399-emmc-phy" },
274 {}
275};
276
277MODULE_DEVICE_TABLE(of, rockchip_emmc_phy_dt_ids);
278
279static struct platform_driver rockchip_emmc_driver = {
280 .probe = rockchip_emmc_phy_probe,
281 .driver = {
282 .name = "rockchip-emmc-phy",
283 .of_match_table = rockchip_emmc_phy_dt_ids,
284 },
285};
286
287module_platform_driver(rockchip_emmc_driver);
288
289MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
290MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
291MODULE_LICENSE("GPL v2");