aboutsummaryrefslogtreecommitdiff
path: root/sys/arm64/rockchip/rk_pcie_phy.c
blob: eb4e499ffb78192774a4fb1ac46da892cd25e1c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Rockchip PHY TYPEC
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/gpio.h>
#include <machine/bus.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/ofw_subr.h>

#include <dev/extres/clk/clk.h>
#include <dev/extres/phy/phy.h>
#include <dev/extres/phy/phy_internal.h>
#include <dev/extres/syscon/syscon.h>
#include <dev/extres/hwreset/hwreset.h>

#include "syscon_if.h"

#define GRF_HIWORD_SHIFT	16
#define	GRF_SOC_CON_5_PCIE	0xE214
#define	 CON_5_PCIE_IDLE_OFF(x)	(1 <<(((x) & 0x3) + 3))
#define	GRF_SOC_CON8		0xE220
#define	GRF_SOC_STATUS1 	0xE2A4

/* PHY config registers  - write */
#define	PHY_CFG_CLK_TEST	0x10
#define	 CLK_TEST_SEPE_RATE		(1 << 3)
#define	PHY_CFG_CLK_SCC		0x12
#define	 CLK_SCC_PLL_100M		(1 << 3)

/* PHY config registers  - read */
#define	PHY_CFG_PLL_LOCK	0x10
#define	 CLK_PLL_LOCKED			(1 << 1)
#define	PHY_CFG_SCC_LOCK	0x12
#define	 CLK_SCC_100M_GATE		(1 << 2)


#define	 STATUS1_PLL_LOCKED		(1 << 9)

static struct ofw_compat_data compat_data[] = {
	{"rockchip,rk3399-pcie-phy",	1},
	{NULL,				0}
};


struct rk_pcie_phy_softc {
	device_t		dev;
	struct syscon		*syscon;
	struct mtx		mtx;
	clk_t			clk_ref;
	hwreset_t		hwreset_phy;
	int			enable_count;
};

#define	PHY_LOCK(_sc)		mtx_lock(&(_sc)->mtx)
#define	PHY_UNLOCK(_sc)		mtx_unlock(&(_sc)->mtx)
#define	PHY_LOCK_INIT(_sc)	mtx_init(&(_sc)->mtx, 			\
	    device_get_nameunit(_sc->dev), "rk_pcie_phyc", MTX_DEF)
#define	PHY_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->mtx);
#define	PHY_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->mtx, MA_OWNED);
#define	PHY_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED);


#define	RD4(sc, reg)		SYSCON_READ_4((sc)->syscon, (reg))
#define	WR4(sc, reg, mask, val)						\
    SYSCON_WRITE_4((sc)->syscon, (reg), ((mask) << GRF_HIWORD_SHIFT) | (val))

#define	MAX_LANE	4


static void
cfg_write(struct rk_pcie_phy_softc *sc, uint32_t reg, uint32_t data)
{
	/* setup register address and data first */
	WR4(sc, GRF_SOC_CON8, 0x7FF,
	    (reg & 0x3F) << 1 | (data & 0x0F) << 7);
	/* dummy readback for sync */
	RD4(sc, GRF_SOC_CON8);

	/* Do write pulse */
	WR4(sc, GRF_SOC_CON8, 1, 1);
	RD4(sc, GRF_SOC_CON8);
	DELAY(10);
	WR4(sc, GRF_SOC_CON8, 1, 0);
	RD4(sc, GRF_SOC_CON8);
	DELAY(10);
}

static uint32_t
cfg_read(struct rk_pcie_phy_softc *sc, uint32_t reg)
{
	uint32_t val;

	WR4(sc, GRF_SOC_CON8, 0x3FF, reg << 1);
	RD4(sc, GRF_SOC_CON8);
	DELAY(10);
	val = RD4(sc, GRF_SOC_STATUS1);
	return ((val >> 8) & 0x0f);
}

static int
rk_pcie_phy_up(struct rk_pcie_phy_softc *sc, int id)
{
	uint32_t val;
	int i, rv;

	PHY_LOCK(sc);

	sc->enable_count++;
	if (sc->enable_count != 1) {
		PHY_UNLOCK(sc);
		return (0);
	}

	rv = hwreset_deassert(sc->hwreset_phy);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot deassert 'phy' reset\n");
		PHY_UNLOCK(sc);
		return (rv);
	}
	/* Un-idle all lanes */
	for (i = 0; i < MAX_LANE; i++)
		WR4(sc, GRF_SOC_CON_5_PCIE, CON_5_PCIE_IDLE_OFF(i), 0);

	/* Wait for PLL lock */
	for (i = 100; i > 0; i--) {
		val = cfg_read(sc, PHY_CFG_PLL_LOCK);
		if (val & CLK_PLL_LOCKED)
			break;
		DELAY(1000);
	}
	if (i <= 0) {
		device_printf(sc->dev, "PLL lock timeouted, 0x%02X\n", val);
		PHY_UNLOCK(sc);
		return (ETIMEDOUT);
	}
	/* Switch PLL to stable 5GHz, rate adjustment is done by divider */
	cfg_write(sc, PHY_CFG_CLK_TEST, CLK_TEST_SEPE_RATE);
	/* Enable 100MHz output for PCIe ref clock */
	cfg_write(sc, PHY_CFG_CLK_SCC, CLK_SCC_PLL_100M);

	/* Wait for ungating of ref clock */
	for (i = 100; i > 0; i--) {
		val = cfg_read(sc, PHY_CFG_SCC_LOCK);
		if ((val & CLK_SCC_100M_GATE) == 0)
			break;
		DELAY(1000);
	}
	if (i <= 0) {
		device_printf(sc->dev, "PLL output enable timeouted\n");
		PHY_UNLOCK(sc);
		return (ETIMEDOUT);
	}

	/* Wait for PLL relock (to 5GHz) */
	for (i = 100; i > 0; i--) {
		val = cfg_read(sc, PHY_CFG_PLL_LOCK);
		if (val & CLK_PLL_LOCKED)
			break;
		DELAY(1000);
	}
	if (i <= 0) {
		device_printf(sc->dev, "PLL relock timeouted\n");
		PHY_UNLOCK(sc);
		return (ETIMEDOUT);
	}

	PHY_UNLOCK(sc);
	return (rv);
}

static int
rk_pcie_phy_down(struct rk_pcie_phy_softc *sc, int id)
{
	int rv;

	PHY_LOCK(sc);

	rv = 0;
	if (sc->enable_count <= 0)
		panic("unpaired enable/disable");

	sc->enable_count--;

	/* Idle given lane */
	WR4(sc, GRF_SOC_CON_5_PCIE,
	    CON_5_PCIE_IDLE_OFF(id),
	    CON_5_PCIE_IDLE_OFF(id));

	if (sc->enable_count == 0) {
		rv = hwreset_assert(sc->hwreset_phy);
		if (rv != 0)
			device_printf(sc->dev, "Cannot assert 'phy' reset\n");
	}
	PHY_UNLOCK(sc);
	return (rv);
}

static int
rk_pcie_phy_enable(struct phynode *phynode, bool enable)
{
	struct rk_pcie_phy_softc *sc;
	device_t dev;
	intptr_t phy;
	int rv;

	dev = phynode_get_device(phynode);
	phy = phynode_get_id(phynode);
	sc = device_get_softc(dev);

	if (enable)
		rv = rk_pcie_phy_up(sc, (int)phy);
	 else
		rv = rk_pcie_phy_down(sc, (int) phy);

	return (rv);
}


/* Phy class and methods. */
static int rk_pcie_phy_enable(struct phynode *phynode, bool enable);
static phynode_method_t rk_pcie_phy_phynode_methods[] = {
	PHYNODEMETHOD(phynode_enable,		 rk_pcie_phy_enable),

	PHYNODEMETHOD_END
};

DEFINE_CLASS_1( rk_pcie_phy_phynode, rk_pcie_phy_phynode_class,
    rk_pcie_phy_phynode_methods, 0, phynode_class);

static int
 rk_pcie_phy_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
		return (ENXIO);

	device_set_desc(dev, "Rockchip RK3399 PCIe PHY");
	return (BUS_PROBE_DEFAULT);
}

static int
 rk_pcie_phy_attach(device_t dev)
{
	struct rk_pcie_phy_softc *sc;
	struct phynode_init_def phy_init;
	struct phynode *phynode;
	phandle_t node;
	int i, rv;

	sc = device_get_softc(dev);
	sc->dev = dev;
	node = ofw_bus_get_node(dev);
	PHY_LOCK_INIT(sc);

	if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
	    sc->syscon == NULL) {
		device_printf(dev, "cannot get syscon for device\n");
		rv = ENXIO;
		goto fail;
	}

	rv = clk_get_by_ofw_name(sc->dev, 0, "refclk", &sc->clk_ref);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot get 'refclk' clock\n");
		rv = ENXIO;
		goto fail;
	}
	rv = hwreset_get_by_ofw_name(sc->dev, 0, "phy", &sc->hwreset_phy);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot get 'phy' reset\n");
		rv = ENXIO;
		goto fail;
	}

	rv = hwreset_assert(sc->hwreset_phy);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot assert 'phy' reset\n");
		rv = ENXIO;
		goto fail;
	}

	rv = clk_enable(sc->clk_ref);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot enable 'ref' clock\n");
		rv = ENXIO;
		goto fail;
	}

	for (i = 0; i < MAX_LANE; i++) {
		phy_init.id = i;
		phy_init.ofw_node = node;
		phynode = phynode_create(dev, &rk_pcie_phy_phynode_class,
		&phy_init);
		if (phynode == NULL) {
			device_printf(dev, "Cannot create phy[%d]\n", i);
			rv = ENXIO;
			goto fail;
		}
		if (phynode_register(phynode) == NULL) {
			device_printf(dev, "Cannot register phy[%d]\n", i);
			rv = ENXIO;
			goto fail;
		}
	}

	return (0);

fail:
	return (rv);
}

static device_method_t rk_pcie_phy_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		 rk_pcie_phy_probe),
	DEVMETHOD(device_attach,	 rk_pcie_phy_attach),

	DEVMETHOD_END
};

DEFINE_CLASS_0(rk_pcie_phy, rk_pcie_phy_driver, rk_pcie_phy_methods,
    sizeof(struct rk_pcie_phy_softc));

static devclass_t rk_pcie_phy_devclass;
EARLY_DRIVER_MODULE(rk_pcie_phy, simplebus, rk_pcie_phy_driver,
    rk_pcie_phy_devclass, NULL, NULL,
    BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);