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