blob: f2f75cf69af13404e1320028e6a38e1a97f7f289 [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 */
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
Shawn Lind7485772016-06-20 10:56:41 -070059#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
Shawn Linc474a942016-02-03 15:22:22 +080072
73struct rockchip_emmc_phy {
74 unsigned int reg_offset;
75 struct regmap *reg_base;
76};
77
78static int rockchip_emmc_phy_power(struct rockchip_emmc_phy *rk_phy,
79 bool on_off)
80{
81 unsigned int caldone;
82 unsigned int dllrdy;
83
84 /*
85 * Keep phyctrl_pdb and phyctrl_endll low to allow
86 * initialization of CALIO state M/C DFFs
87 */
88 regmap_write(rk_phy->reg_base,
89 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
90 HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF,
91 PHYCTRL_PDB_MASK,
92 PHYCTRL_PDB_SHIFT));
93 regmap_write(rk_phy->reg_base,
94 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
95 HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE,
96 PHYCTRL_ENDLL_MASK,
97 PHYCTRL_ENDLL_SHIFT));
98
99 /* Already finish power_off above */
100 if (on_off == PHYCTRL_PDB_PWR_OFF)
101 return 0;
102
103 /*
104 * According to the user manual, calpad calibration
105 * cycle takes more than 2us without the minimal recommended
106 * value, so we may need a little margin here
107 */
108 udelay(3);
109 regmap_write(rk_phy->reg_base,
110 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
111 HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON,
112 PHYCTRL_PDB_MASK,
113 PHYCTRL_PDB_SHIFT));
114
115 /*
116 * According to the user manual, it asks driver to
117 * wait 5us for calpad busy trimming
118 */
119 udelay(5);
120 regmap_read(rk_phy->reg_base,
121 rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
122 &caldone);
123 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
124 if (caldone != PHYCTRL_CALDONE_DONE) {
125 pr_err("rockchip_emmc_phy_power: caldone timeout.\n");
126 return -ETIMEDOUT;
127 }
128
129 regmap_write(rk_phy->reg_base,
130 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
131 HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE,
132 PHYCTRL_ENDLL_MASK,
133 PHYCTRL_ENDLL_SHIFT));
134 /*
Shawn Lin4d54a252016-06-20 10:56:40 -0700135 * After enable analog DLL circuits, we need an extra 10.2us
136 * for dll to be ready for work. But according to testing, we
137 * find some chips need more than 25us.
Shawn Linc474a942016-02-03 15:22:22 +0800138 */
Shawn Lin4d54a252016-06-20 10:56:40 -0700139 udelay(30);
Shawn Linc474a942016-02-03 15:22:22 +0800140 regmap_read(rk_phy->reg_base,
141 rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
142 &dllrdy);
143 dllrdy = (dllrdy >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK;
144 if (dllrdy != PHYCTRL_DLLRDY_DONE) {
145 pr_err("rockchip_emmc_phy_power: dllrdy timeout.\n");
146 return -ETIMEDOUT;
147 }
148
149 return 0;
150}
151
152static int rockchip_emmc_phy_power_off(struct phy *phy)
153{
154 struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
155 int ret = 0;
156
157 /* Power down emmc phy analog blocks */
158 ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_OFF);
159 if (ret)
160 return ret;
161
162 return 0;
163}
164
165static int rockchip_emmc_phy_power_on(struct phy *phy)
166{
167 struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
168 int ret = 0;
169
Shawn Lind7485772016-06-20 10:56:41 -0700170 /* DLL operation: 200 MHz */
171 regmap_write(rk_phy->reg_base,
172 rk_phy->reg_offset + GRF_EMMCPHY_CON0,
173 HIWORD_UPDATE(PHYCTRL_FREQSEL_200M,
174 PHYCTRL_FREQSEL_MASK,
175 PHYCTRL_FREQSEL_SHIFT));
176
177 /* Drive impedance: 50 Ohm */
178 regmap_write(rk_phy->reg_base,
179 rk_phy->reg_offset + GRF_EMMCPHY_CON6,
180 HIWORD_UPDATE(PHYCTRL_DR_50OHM,
181 PHYCTRL_DR_MASK,
182 PHYCTRL_DR_SHIFT));
183
Shawn Linc474a942016-02-03 15:22:22 +0800184 /* Power up emmc phy analog blocks */
185 ret = rockchip_emmc_phy_power(rk_phy, PHYCTRL_PDB_PWR_ON);
186 if (ret)
187 return ret;
188
189 return 0;
190}
191
192static const struct phy_ops ops = {
193 .power_on = rockchip_emmc_phy_power_on,
194 .power_off = rockchip_emmc_phy_power_off,
195 .owner = THIS_MODULE,
196};
197
198static int rockchip_emmc_phy_probe(struct platform_device *pdev)
199{
200 struct device *dev = &pdev->dev;
201 struct rockchip_emmc_phy *rk_phy;
202 struct phy *generic_phy;
203 struct phy_provider *phy_provider;
204 struct regmap *grf;
205 unsigned int reg_offset;
206
Heiko Stuebner332184a2016-03-24 22:29:02 +0100207 if (!dev->parent || !dev->parent->of_node)
208 return -ENODEV;
209
210 grf = syscon_node_to_regmap(dev->parent->of_node);
Shawn Linc474a942016-02-03 15:22:22 +0800211 if (IS_ERR(grf)) {
212 dev_err(dev, "Missing rockchip,grf property\n");
213 return PTR_ERR(grf);
214 }
215
216 rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
217 if (!rk_phy)
218 return -ENOMEM;
219
220 if (of_property_read_u32(dev->of_node, "reg", &reg_offset)) {
221 dev_err(dev, "missing reg property in node %s\n",
222 dev->of_node->name);
223 return -EINVAL;
224 }
225
226 rk_phy->reg_offset = reg_offset;
227 rk_phy->reg_base = grf;
228
229 generic_phy = devm_phy_create(dev, dev->of_node, &ops);
230 if (IS_ERR(generic_phy)) {
231 dev_err(dev, "failed to create PHY\n");
232 return PTR_ERR(generic_phy);
233 }
234
235 phy_set_drvdata(generic_phy, rk_phy);
236 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
237
238 return PTR_ERR_OR_ZERO(phy_provider);
239}
240
241static const struct of_device_id rockchip_emmc_phy_dt_ids[] = {
242 { .compatible = "rockchip,rk3399-emmc-phy" },
243 {}
244};
245
246MODULE_DEVICE_TABLE(of, rockchip_emmc_phy_dt_ids);
247
248static struct platform_driver rockchip_emmc_driver = {
249 .probe = rockchip_emmc_phy_probe,
250 .driver = {
251 .name = "rockchip-emmc-phy",
252 .of_match_table = rockchip_emmc_phy_dt_ids,
253 },
254};
255
256module_platform_driver(rockchip_emmc_driver);
257
258MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
259MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
260MODULE_LICENSE("GPL v2");