summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/sys/powerpc/fdt_phy.c
blob: b6f87f90cdd6a762a9c586dd912c6db749c5d8ef (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
#include <machine/rtems-bsd-kernel-space.h>
#include <rtems/bsd/local/opt_dpaa.h>

/*
 * Copyright (c) 2016 embedded brains GmbH
 * All rights reserved.
 *
 * 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 COPYRIGHT HOLDERS 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 COPYRIGHT
 * OWNER 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.
 */

#include <machine/rtems-bsd-kernel-space.h>

#include <fdt_phy.h>

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/malloc.h>

#include <libfdt.h>

#include <rtems/bsd.h>

#include <bsp/fdt.h>

#define	MDIO_LOCK()	mtx_lock(&mdio.mutex)
#define	MDIO_UNLOCK()	mtx_unlock(&mdio.mutex)

struct mdio_device {
	struct fdt_mdio_device base;
	SLIST_ENTRY(mdio_device) next;
	int node;
};

static struct {
	SLIST_HEAD(, mdio_device) instances;
	struct mtx mutex;
} mdio = {
	.instances = SLIST_HEAD_INITIALIZER(mdio.instances)
};

MTX_SYSINIT(mdio_mutex, &mdio.mutex, "FDT MDIO", MTX_DEF);

static uint64_t
fdt_get_address(const void *fdt, int node)
{
	uint64_t addr;
	int nodes[16];
	size_t i;
	int ac;

	i = 0;
	do {
		nodes[i] = node;
		++i;
		node = fdt_parent_offset(fdt, node);
	} while (node >= 0 && i < nitems(nodes));

	if (node >= 0) {
		return (0);
	}

	ac = 1;
	addr = 0;
	while (i > 0) {
		const fdt32_t *p;
		int len;

		p = fdt_getprop(fdt, nodes[i - 1], "reg", &len);
		if (p != NULL) {
			if (ac == 1 && len >= 4) {
				addr += fdt32_to_cpu(p[0]);
			} else if (ac == 2 && len >= 8) {
				addr += fdt32_to_cpu(p[1]);
				addr += (uint64_t)fdt32_to_cpu(p[0]) << 32;
			} else {
				return (0);
			}
		}

		p = fdt_getprop(fdt, nodes[i - 1], "#address-cells", &len);
		if (p != NULL) {
			if (len != 4) {
				return (0);
			}
			ac = (int)fdt32_to_cpu(p[0]);
			if (ac != 1 && ac != 2) {
				return (0);
			}
		}

		--i;
	}

	return (addr);
}

struct fman_mdio_regs {
	uint32_t reserved[12];
	uint32_t mdio_cfg;
	uint32_t mdio_ctrl;
	uint32_t mdio_data;
	uint32_t mdio_addr;
};

#define	MDIO_CFG_BSY		(1U << 31)
#define	MDIO_CFG_ENC45		(1U << 6)
#define	MDIO_CFG_RD_ERR		(1U << 1)

#define	MDIO_CTRL_READ		(1U << 15)
#define	MDIO_CTRL_REG_ADDR(x)	((x) & 0x1fU)
#define	MDIO_CTRL_PHY_ADDR(x)	(((x) & 0x1fU) << 5)

struct fman_mdio_device {
	struct mdio_device base;
	volatile struct fman_mdio_regs *regs;
};

static int
fman_mdio_wait(volatile struct fman_mdio_regs *regs)
{
	struct bintime start;

	rtems_bsd_binuptime(&start);

	while ((regs->mdio_cfg & MDIO_CFG_BSY) != 0) {
		struct bintime now;

		rtems_bsd_binuptime(&now);
		if (bttosbt(now) - bttosbt(start) > 100 * SBT_1US) {
			break;
		}
	}

	/* Check again, to take thread pre-emption into account */
	if ((regs->mdio_cfg & MDIO_CFG_BSY) != 0) {
		return (EIO);
	}

	return (0);
}

static int
fman_mdio_read(struct fdt_mdio_device *base, int phy, int reg)
{
	struct fman_mdio_device *fm;
	volatile struct fman_mdio_regs *regs;
	int val;
	int err;

	fm = (struct fman_mdio_device *)base;
	regs = fm->regs;

	MDIO_LOCK();

	err = fman_mdio_wait(regs);
	if (err == 0) {
		uint32_t mdio_cfg;
		uint32_t mdio_ctrl;

		mdio_cfg = regs->mdio_cfg;
		mdio_cfg &= ~MDIO_CFG_ENC45;
		regs->mdio_cfg = mdio_cfg;

		mdio_ctrl = MDIO_CTRL_PHY_ADDR(phy) | MDIO_CTRL_REG_ADDR(reg);
		regs->mdio_ctrl = mdio_ctrl;
		mdio_ctrl |= MDIO_CTRL_READ;
		regs->mdio_ctrl = mdio_ctrl;

		err = fman_mdio_wait(regs);
		if (err == 0 && (regs->mdio_cfg & MDIO_CFG_RD_ERR) == 0) {
			val = (int)(regs->mdio_data & 0xffff);
		} else {
			val = 0xffff;
		}
	} else {
		val = 0xffff;
	}

	MDIO_UNLOCK();

	return (val);
}

static int
fman_mdio_write(struct fdt_mdio_device *base, int phy, int reg, int val)
{
	struct fman_mdio_device *fm;
	volatile struct fman_mdio_regs *regs;
	int err;

	fm = (struct fman_mdio_device *)base;
	regs = fm->regs;

	MDIO_LOCK();

	err = fman_mdio_wait(regs);
	if (err == 0) {
		uint32_t mdio_cfg;
		uint32_t mdio_ctrl;

		mdio_cfg = regs->mdio_cfg;
		mdio_cfg &= ~MDIO_CFG_ENC45;
		regs->mdio_cfg = mdio_cfg;

		mdio_ctrl = MDIO_CTRL_PHY_ADDR(phy) | MDIO_CTRL_REG_ADDR(reg);
		regs->mdio_ctrl = mdio_ctrl;

		regs->mdio_data = (uint32_t)(val & 0xffff);

		fman_mdio_wait(regs);
	}

	MDIO_UNLOCK();

	return (0);
}

static struct mdio_device *
create_fman_mdio(const void *fdt, int mdio_node)
{
	struct fman_mdio_device *fm = NULL;

	fm = malloc(sizeof(*fm), M_TEMP, M_WAITOK | M_ZERO);
	if (fm == NULL) {
		return (NULL);
	}

	fm->regs = (volatile struct fman_mdio_regs *)(uintptr_t)
	    fdt_get_address(fdt, mdio_node);
	fm->base.base.read = fman_mdio_read;
	fm->base.base.write = fman_mdio_write;

	return (&fm->base);
}

static struct mdio_device *
create_mdio_device(const void *fdt, int mdio_node)
{

	if (fdt_node_check_compatible(fdt, mdio_node,
	    "fsl,fman-memac-mdio") == 0 ||
	    fdt_node_check_compatible(fdt, mdio_node,
	    "fsl,fman-xmdio") == 0) {
		return (create_fman_mdio(fdt, mdio_node));
	} else {
		return (NULL);
	}
}

static int
find_mdio_device(const void *fdt, int mdio_node,
    struct fdt_phy_device *phy_dev)
{
	struct mdio_device *mdio_dev = NULL;

	SLIST_FOREACH(mdio_dev, &mdio.instances, next) {
		if (mdio_dev->node == mdio_node) {
			break;
		}
	}

	if (mdio_dev == NULL) {
		mdio_dev = create_mdio_device(fdt, mdio_node);
	}

	if (mdio_dev == NULL) {
		return (ENXIO);
	}

	phy_dev->mdio_dev = &mdio_dev->base;
	return (0);
}

static struct fdt_phy_device *
phy_obtain(const void *fdt, int mdio_node, int phy)
{
	struct fdt_phy_device *phy_dev;
	int err;

	phy_dev = malloc(sizeof(*phy_dev), M_TEMP, M_WAITOK | M_ZERO);
	if (phy_dev == NULL) {
		return (NULL);
	}

	phy_dev->phy = phy;
	MDIO_LOCK();
	err = find_mdio_device(fdt, mdio_node, phy_dev);
	MDIO_UNLOCK();

	if (err != 0) {
		free(phy_dev, M_TEMP);
		return (NULL);
	}

	return (phy_dev);
}

struct fdt_phy_device *
fdt_phy_obtain(int device_node)
{
	const void *fdt;
	const fdt32_t *phandle;
	const fdt32_t *phy;
	int len;
	int node;

	fdt = bsp_fdt_get();

	phandle = fdt_getprop(fdt, device_node, "phy-handle", &len);
	if (phandle == NULL || len != sizeof(*phandle)) {
		return (NULL);
	}

	node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*phandle));
	if (node < 0) {
		return (NULL);
	}

	phy = fdt_getprop(fdt, node, "reg", &len);
	if (phy == NULL || len != sizeof(*phy)) {
		return (NULL);
	}

	node = fdt_parent_offset(fdt, node);
	if (node < 0) {
		return (NULL);
	}

	return (phy_obtain(fdt, node, (int)fdt32_to_cpu(*phy)));
}

void
fdt_phy_release(struct fdt_phy_device *phy_dev)
{

	free(phy_dev, M_TEMP);
}