blob: 0b2b2410b27ed77d05c1b763a995e63da6ddaf45 [file] [log] [blame]
Sekhar Nori888e1ca2019-08-01 19:12:58 +05301// SPDX-License-Identifier: GPL-2.0+
2/**
3 * PCIe SERDES driver for AM654x SoC
4 *
5 * Copyright (C) 2018 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
8
9#include <common.h>
10#include <clk-uclass.h>
11#include <dm.h>
12#include <dm/device.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Sekhar Nori888e1ca2019-08-01 19:12:58 +053014#include <dm/lists.h>
15#include <dt-bindings/phy/phy.h>
16#include <generic-phy.h>
17#include <asm/io.h>
18#include <asm/arch/sys_proto.h>
19#include <power-domain.h>
20#include <regmap.h>
21#include <syscon.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <linux/err.h>
Sekhar Nori888e1ca2019-08-01 19:12:58 +053023
24#define CMU_R07C 0x7c
25#define CMU_MASTER_CDN_O BIT(24)
26
27#define COMLANE_R138 0xb38
28#define CONFIG_VERSION_REG_MASK GENMASK(23, 16)
29#define CONFIG_VERSION_REG_SHIFT 16
30#define VERSION 0x70
31
32#define COMLANE_R190 0xb90
33#define L1_MASTER_CDN_O BIT(9)
34
35#define COMLANE_R194 0xb94
36#define CMU_OK_I_0 BIT(19)
37
38#define SERDES_CTRL 0x1fd0
39#define POR_EN BIT(29)
40
41#define WIZ_LANEXCTL_STS 0x1fe0
42#define TX0_ENABLE_OVL BIT(31)
43#define TX0_ENABLE_MASK GENMASK(30, 29)
44#define TX0_ENABLE_SHIFT 29
45#define TX0_DISABLE_STATE 0x0
46#define TX0_SLEEP_STATE 0x1
47#define TX0_SNOOZE_STATE 0x2
48#define TX0_ENABLE_STATE 0x3
49#define RX0_ENABLE_OVL BIT(15)
50#define RX0_ENABLE_MASK GENMASK(14, 13)
51#define RX0_ENABLE_SHIFT 13
52#define RX0_DISABLE_STATE 0x0
53#define RX0_SLEEP_STATE 0x1
54#define RX0_SNOOZE_STATE 0x2
55#define RX0_ENABLE_STATE 0x3
56
57#define WIZ_PLL_CTRL 0x1ff4
58#define PLL_ENABLE_OVL BIT(31)
59#define PLL_ENABLE_MASK GENMASK(30, 29)
60#define PLL_ENABLE_SHIFT 29
61#define PLL_DISABLE_STATE 0x0
62#define PLL_SLEEP_STATE 0x1
63#define PLL_SNOOZE_STATE 0x2
64#define PLL_ENABLE_STATE 0x3
65#define PLL_OK BIT(28)
66
67#define PLL_LOCK_TIME 1000 /* in milliseconds */
68#define SLEEP_TIME 100 /* in microseconds */
69
70#define LANE_USB3 0x0
71#define LANE_PCIE0_LANE0 0x1
72
73#define LANE_PCIE1_LANE0 0x0
74#define LANE_PCIE0_LANE1 0x1
75
76#define SERDES_NUM_CLOCKS 3
77
78/* SERDES control MMR bit offsets */
79#define SERDES_CTL_LANE_FUNC_SEL_SHIFT 0
80#define SERDES_CTL_LANE_FUNC_SEL_MASK GENMASK(1, 0)
81#define SERDES_CTL_CLK_SEL_SHIFT 4
82#define SERDES_CTL_CLK_SEL_MASK GENMASK(7, 4)
83
84/**
85 * struct serdes_am654_mux_clk_data - clock controller information structure
86 */
87struct serdes_am654_mux_clk_data {
88 struct regmap *regmap;
89 struct clk_bulk parents;
90};
91
92static int serdes_am654_mux_clk_probe(struct udevice *dev)
93{
94 struct serdes_am654_mux_clk_data *data = dev_get_priv(dev);
95 struct udevice *syscon;
96 struct regmap *regmap;
97 int ret;
98
99 debug("%s(dev=%s)\n", __func__, dev->name);
100
101 if (!data)
102 return -ENOMEM;
103
104 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
105 "ti,serdes-clk", &syscon);
106 if (ret) {
107 dev_err(dev, "unable to find syscon device\n");
108 return ret;
109 }
110
111 regmap = syscon_get_regmap(syscon);
112 if (IS_ERR(regmap)) {
113 dev_err(dev, "Fail to get Syscon regmap\n");
114 return PTR_ERR(regmap);
115 }
116
117 data->regmap = regmap;
118
119 ret = clk_get_bulk(dev, &data->parents);
120 if (ret) {
121 dev_err(dev, "Failed to obtain parent clocks\n");
122 return ret;
123 }
124
125 return 0;
126}
127
128static int mux_table[SERDES_NUM_CLOCKS][3] = {
129 /*
130 * The entries represent values for selecting between
131 * {left input, external reference clock, right input}
132 * Only one of Left Output or Right Output should be used since
133 * both left and right output clock uses the same bits and modifying
134 * one clock will impact the other.
135 */
136 { BIT(2), 0, BIT(0) }, /* Mux of CMU refclk */
137 { -1, BIT(3), BIT(1) }, /* Mux of Left Output */
138 { BIT(1), BIT(3) | BIT(1), -1 }, /* Mux of Right Output */
139};
140
141static int serdes_am654_mux_clk_set_parent(struct clk *clk, struct clk *parent)
142{
143 struct serdes_am654_mux_clk_data *data = dev_get_priv(clk->dev);
144 u32 val;
145 int i;
146
147 debug("%s(clk=%s, parent=%s)\n", __func__, clk->dev->name,
148 parent->dev->name);
149
150 /*
151 * Since we have the same device-tree node represent both the
152 * clock and serdes device, we have two devices associated with
153 * the serdes node. assigned-clocks for this node is processed twice,
154 * once for the clock device and another time for the serdes
155 * device. When it is processed for the clock device, it is before
156 * the probe for clock device has been called. We ignore this case
157 * and rely on assigned-clocks to be processed correctly for the
158 * serdes case.
159 */
160 if (!data->regmap)
161 return 0;
162
163 for (i = 0; i < data->parents.count; i++) {
164 if (clk_is_match(&data->parents.clks[i], parent))
165 break;
166 }
167
168 if (i >= data->parents.count)
169 return -EINVAL;
170
171 val = mux_table[clk->id][i];
172 val <<= SERDES_CTL_CLK_SEL_SHIFT;
173
174 regmap_update_bits(data->regmap, 0, SERDES_CTL_CLK_SEL_MASK, val);
175
176 return 0;
177}
178
179static struct clk_ops serdes_am654_mux_clk_ops = {
180 .set_parent = serdes_am654_mux_clk_set_parent,
181};
182
183U_BOOT_DRIVER(serdes_am654_mux_clk) = {
184 .name = "ti-serdes-am654-mux-clk",
185 .id = UCLASS_CLK,
186 .probe = serdes_am654_mux_clk_probe,
187 .priv_auto_alloc_size = sizeof(struct serdes_am654_mux_clk_data),
188 .ops = &serdes_am654_mux_clk_ops,
189};
190
191struct serdes_am654 {
192 struct regmap *regmap;
193 struct regmap *serdes_ctl;
194};
195
196static int serdes_am654_enable_pll(struct serdes_am654 *phy)
197{
198 u32 mask = PLL_ENABLE_OVL | PLL_ENABLE_MASK;
199 u32 val = PLL_ENABLE_OVL | (PLL_ENABLE_STATE << PLL_ENABLE_SHIFT);
200
201 regmap_update_bits(phy->regmap, WIZ_PLL_CTRL, mask, val);
202
203 return regmap_read_poll_timeout(phy->regmap, WIZ_PLL_CTRL, val,
204 val & PLL_OK, 1000, PLL_LOCK_TIME);
205}
206
207static void serdes_am654_disable_pll(struct serdes_am654 *phy)
208{
209 u32 mask = PLL_ENABLE_OVL | PLL_ENABLE_MASK;
210
211 regmap_update_bits(phy->regmap, WIZ_PLL_CTRL, mask, 0);
212}
213
214static int serdes_am654_enable_txrx(struct serdes_am654 *phy)
215{
216 u32 mask;
217 u32 val;
218
219 /* Enable TX */
220 mask = TX0_ENABLE_OVL | TX0_ENABLE_MASK;
221 val = TX0_ENABLE_OVL | (TX0_ENABLE_STATE << TX0_ENABLE_SHIFT);
222 regmap_update_bits(phy->regmap, WIZ_LANEXCTL_STS, mask, val);
223
224 /* Enable RX */
225 mask = RX0_ENABLE_OVL | RX0_ENABLE_MASK;
226 val = RX0_ENABLE_OVL | (RX0_ENABLE_STATE << RX0_ENABLE_SHIFT);
227 regmap_update_bits(phy->regmap, WIZ_LANEXCTL_STS, mask, val);
228
229 return 0;
230}
231
232static int serdes_am654_disable_txrx(struct serdes_am654 *phy)
233{
234 u32 mask;
235
236 /* Disable TX */
237 mask = TX0_ENABLE_OVL | TX0_ENABLE_MASK;
238 regmap_update_bits(phy->regmap, WIZ_LANEXCTL_STS, mask, 0);
239
240 /* Disable RX */
241 mask = RX0_ENABLE_OVL | RX0_ENABLE_MASK;
242 regmap_update_bits(phy->regmap, WIZ_LANEXCTL_STS, mask, 0);
243
244 return 0;
245}
246
247static int serdes_am654_power_on(struct phy *x)
248{
249 struct serdes_am654 *phy = dev_get_priv(x->dev);
250 int ret;
251 u32 val;
252
253 ret = serdes_am654_enable_pll(phy);
254 if (ret) {
255 dev_err(x->dev, "Failed to enable PLL\n");
256 return ret;
257 }
258
259 ret = serdes_am654_enable_txrx(phy);
260 if (ret) {
261 dev_err(x->dev, "Failed to enable TX RX\n");
262 return ret;
263 }
264
265 return regmap_read_poll_timeout(phy->regmap, COMLANE_R194, val,
266 val & CMU_OK_I_0, SLEEP_TIME,
267 PLL_LOCK_TIME);
268}
269
270static int serdes_am654_power_off(struct phy *x)
271{
272 struct serdes_am654 *phy = dev_get_priv(x->dev);
273
274 serdes_am654_disable_txrx(phy);
275 serdes_am654_disable_pll(phy);
276
277 return 0;
278}
279
280static int serdes_am654_init(struct phy *x)
281{
282 struct serdes_am654 *phy = dev_get_priv(x->dev);
283 u32 mask;
284 u32 val;
285
286 mask = CONFIG_VERSION_REG_MASK;
287 val = VERSION << CONFIG_VERSION_REG_SHIFT;
288 regmap_update_bits(phy->regmap, COMLANE_R138, mask, val);
289
290 val = CMU_MASTER_CDN_O;
291 regmap_update_bits(phy->regmap, CMU_R07C, val, val);
292
293 val = L1_MASTER_CDN_O;
294 regmap_update_bits(phy->regmap, COMLANE_R190, val, val);
295
296 return 0;
297}
298
299static int serdes_am654_reset(struct phy *x)
300{
301 struct serdes_am654 *phy = dev_get_priv(x->dev);
302 u32 val;
303
304 val = POR_EN;
305 regmap_update_bits(phy->regmap, SERDES_CTRL, val, val);
306 mdelay(1);
307 regmap_update_bits(phy->regmap, SERDES_CTRL, val, 0);
308
309 return 0;
310}
311
312static int serdes_am654_of_xlate(struct phy *x,
313 struct ofnode_phandle_args *args)
314{
315 struct serdes_am654 *phy = dev_get_priv(x->dev);
316
317 if (args->args_count != 2) {
318 dev_err(phy->dev, "Invalid DT PHY argument count: %d\n",
319 args->args_count);
320 return -EINVAL;
321 }
322
323 if (args->args[0] != PHY_TYPE_PCIE) {
324 dev_err(phy->dev, "Unrecognized PHY type: %d\n",
325 args->args[0]);
326 return -EINVAL;
327 }
328
329 x->id = args->args[0] | (args->args[1] << 16);
330
331 /* Setup mux mode using second argument */
332 regmap_update_bits(phy->serdes_ctl, 0, SERDES_CTL_LANE_FUNC_SEL_MASK,
333 args->args[1]);
334
335 return 0;
336}
337
338static int serdes_am654_bind(struct udevice *dev)
339{
340 int ret;
341
342 ret = device_bind_driver_to_node(dev->parent,
343 "ti-serdes-am654-mux-clk",
344 dev_read_name(dev), dev->node,
345 NULL);
346 if (ret) {
347 dev_err(dev, "%s: not able to bind clock driver\n", __func__);
348 return ret;
349 }
350
351 return 0;
352}
353
354static int serdes_am654_probe(struct udevice *dev)
355{
356 struct serdes_am654 *phy = dev_get_priv(dev);
357 struct power_domain serdes_pwrdmn;
358 struct regmap *serdes_ctl;
359 struct regmap *map;
360 int ret;
361
362 ret = regmap_init_mem(dev_ofnode(dev), &map);
363 if (ret)
364 return ret;
365
366 phy->regmap = map;
367
368 serdes_ctl = syscon_regmap_lookup_by_phandle(dev, "ti,serdes-clk");
369 if (IS_ERR(serdes_ctl)) {
370 dev_err(dev, "unable to find syscon device\n");
371 return PTR_ERR(serdes_ctl);
372 }
373
374 phy->serdes_ctl = serdes_ctl;
375
376 ret = power_domain_get_by_index(dev, &serdes_pwrdmn, 0);
377 if (ret) {
378 dev_err(dev, "failed to get power domain\n");
379 return ret;
380 }
381
382 ret = power_domain_on(&serdes_pwrdmn);
383 if (ret) {
384 dev_err(dev, "Power domain on failed\n");
385 return ret;
386 }
387
388 return 0;
389}
390
391static const struct udevice_id serdes_am654_phy_ids[] = {
392 {
393 .compatible = "ti,phy-am654-serdes",
394 },
395};
396
397static const struct phy_ops serdes_am654_phy_ops = {
398 .reset = serdes_am654_reset,
399 .init = serdes_am654_init,
400 .power_on = serdes_am654_power_on,
401 .power_off = serdes_am654_power_off,
402 .of_xlate = serdes_am654_of_xlate,
403};
404
405U_BOOT_DRIVER(am654_serdes_phy) = {
406 .name = "am654_serdes_phy",
407 .id = UCLASS_PHY,
408 .of_match = serdes_am654_phy_ids,
409 .bind = serdes_am654_bind,
410 .ops = &serdes_am654_phy_ops,
411 .probe = serdes_am654_probe,
412 .priv_auto_alloc_size = sizeof(struct serdes_am654),
413};