summaryrefslogtreecommitdiffstats
path: root/sys/dev/fdt/amlpciephy.c
blob: 81c5b945899a097cf3c7e70dc0b3d4b98e08ccbf (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
/*	$OpenBSD: amlpciephy.c,v 1.4 2020/12/27 20:37:58 kettenis Exp $	*/
/*
 * Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/fdt.h>

#define PHY_R0		0x00
#define  PHY_R0_PCIE_POWER_MASK	(0x1f << 0)
#define  PHY_R0_PCIE_POWER_ON	(0x1c << 0)
#define  PHY_R0_PCIE_POWER_OFF	(0x1d << 0)
#define  PHY_R0_MODE_MASK	(0x3 << 5)
#define  PHY_R0_MODE_USB3	(0x3 << 5)
#define PHY_R4		0x10
#define  PHY_R4_PHY_CR_WRITE	(1 << 0)
#define  PHY_R4_PHY_CR_READ	(1 << 1)
#define  PHY_R4_PHY_CR_CAP_DATA	(1 << 18)
#define  PHY_R4_PHY_CR_CAP_ADDR	(1 << 19)
#define PHY_R5		0x14
#define  PHY_R5_PHY_CR_ACK	(1 << 16)

#define HREAD4(sc, reg)							\
	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val)						\
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))

struct amlpciephy_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	struct phy_device	sc_pd;
};

int amlpciephy_match(struct device *, void *, void *);
void amlpciephy_attach(struct device *, struct device *, void *);

struct cfattach	amlpciephy_ca = {
	sizeof (struct amlpciephy_softc), amlpciephy_match, amlpciephy_attach
};

struct cfdriver amlpciephy_cd = {
	NULL, "amlpciephy", DV_DULL
};

int	amlpciephy_enable(void *, uint32_t *);
uint16_t amlpciephy_read(struct amlpciephy_softc *, bus_addr_t);
void	amlpciephy_write(struct amlpciephy_softc *, bus_addr_t, uint16_t);

int
amlpciephy_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return OF_is_compatible(faa->fa_node, "amlogic,g12a-usb3-pcie-phy");
}

void
amlpciephy_attach(struct device *parent, struct device *self, void *aux)
{
	struct amlpciephy_softc *sc = (struct amlpciephy_softc *)self;
	struct fdt_attach_args *faa = aux;

	if (faa->fa_nreg < 1) {
		printf(": no registers\n");
		return;
	}

	sc->sc_iot = faa->fa_iot;
	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
		printf(": can't map registers\n");
		return;
	}

	printf("\n");

	sc->sc_pd.pd_node = faa->fa_node;
	sc->sc_pd.pd_cookie = sc;
	sc->sc_pd.pd_enable = amlpciephy_enable;
	phy_register(&sc->sc_pd);
}

int
amlpciephy_enable(void *cookie, uint32_t *cells)
{
	struct amlpciephy_softc *sc = cookie;
	int node = sc->sc_pd.pd_node;
	uint32_t type = cells[0];
	uint32_t reg;

	/* Hardware can be switched between PCIe 2.0 and USB 3.0 mode. */
	if (type != PHY_TYPE_PCIE && type != PHY_TYPE_USB3)
		return -1;

	clock_set_assigned(node);
	clock_enable_all(node);

	switch (type) {
	case PHY_TYPE_PCIE:
		/* Power on. */
		reg = HREAD4(sc, PHY_R0);
		reg &= ~PHY_R0_PCIE_POWER_MASK;
		reg |= PHY_R0_PCIE_POWER_ON;
		HWRITE4(sc, PHY_R0, reg);

		reset_assert_all(node);
		delay(500);
		reset_deassert_all(node);
		delay(500);

		break;
	case PHY_TYPE_USB3:
		reset_assert_all(node);
		delay(10);
		reset_deassert_all(node);

		/* Switch to USB 3.0 mode. */
		reg = HREAD4(sc, PHY_R0);
		reg &= ~PHY_R0_MODE_MASK;
		reg |= PHY_R0_MODE_USB3;
		HWRITE4(sc, PHY_R0, reg);

		/* Workaround for SuperSpeed PHY suspend bug. */
		reg = amlpciephy_read(sc, 0x102d);
		reg |= (1 << 7);
		amlpciephy_write(sc, 0x102d, reg);

		reg = amlpciephy_read(sc, 0x1010);
		reg &= ~0xff0;
		reg |= 0x10;
		amlpciephy_write(sc, 0x1010, reg);

		/* Rx equalization magic. */
		reg = amlpciephy_read(sc, 0x1006);
		reg &= (1 << 6);
		reg |= (1 << 7);
		reg &= ~(0x7 << 8);
		reg |= (0x3 << 8);
		reg |= (1 << 11);
		amlpciephy_write(sc, 0x1006, reg);

		/* Tx equalization magic. */
		reg = amlpciephy_read(sc, 0x1002);
		reg &= ~0x3f80;
		reg |= (0x16 << 7);
		reg &= ~0x7f;
		reg |= (0x7f | (1 << 14));
		amlpciephy_write(sc, 0x1002, reg);

		/* MPLL loop magic. */
		reg = amlpciephy_read(sc, 0x30);
		reg &= ~(0xf << 4);
		reg |= (8 << 4);
		amlpciephy_write(sc, 0x30, reg);

		break;
	}

	return 0;
}

void
amlpciephy_addr(struct amlpciephy_softc *sc, bus_addr_t addr)
{
	int timo;
	
	HWRITE4(sc, PHY_R4, addr << 2);
	HWRITE4(sc, PHY_R4, addr << 2);
	HWRITE4(sc, PHY_R4, (addr << 2) | PHY_R4_PHY_CR_CAP_ADDR);
	for (timo = 200; timo > 0; timo--) {
		if (HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}
	HWRITE4(sc, PHY_R4, addr << 2);
	for (timo = 200; timo > 0; timo--) {
		if ((HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK) == 0)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}
}

uint16_t
amlpciephy_read(struct amlpciephy_softc *sc, bus_addr_t addr)
{
	uint32_t reg;
	int timo;

	amlpciephy_addr(sc, addr);
	HWRITE4(sc, PHY_R4, 0);
	HWRITE4(sc, PHY_R4, PHY_R4_PHY_CR_READ);
	for (timo = 200; timo > 0; timo--) {
		if (HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return 0;
	}
	reg = HREAD4(sc, PHY_R5);
	HWRITE4(sc, PHY_R4, 0);
	for (timo = 200; timo > 0; timo--) {
		if ((HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK) == 0)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return 0;
	}
	return reg;
}

void
amlpciephy_write(struct amlpciephy_softc *sc, bus_addr_t addr, uint16_t data)
{
	int timo;

	amlpciephy_addr(sc, addr);
	HWRITE4(sc, PHY_R4, data << 2);
	HWRITE4(sc, PHY_R4, data << 2);
	HWRITE4(sc, PHY_R4, data << 2 | PHY_R4_PHY_CR_CAP_DATA);
	for (timo = 200; timo > 0; timo--) {
		if (HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}
	HWRITE4(sc, PHY_R4, data << 2);
	for (timo = 200; timo > 0; timo--) {
		if ((HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK) == 0)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}

	HWRITE4(sc, PHY_R4, data << 2);
	HWRITE4(sc, PHY_R4, data << 2 | PHY_R4_PHY_CR_WRITE);
	for (timo = 200; timo > 0; timo--) {
		if (HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}
	HWRITE4(sc, PHY_R4, data << 2);
	for (timo = 200; timo > 0; timo--) {
		if ((HREAD4(sc, PHY_R5) & PHY_R5_PHY_CR_ACK) == 0)
			break;
		delay(5);
	}
	if (timo == 0) {
		printf("%s: timeout\n", __func__);
		return;
	}
}