source: rtems-libbsd/rtemsbsd/sys/powerpc/fdt_phy.c @ 9da83e7

55-freebsd-126-freebsd-12
Last change on this file since 9da83e7 was 9da83e7, checked in by Sebastian Huber <sebastian.huber@…>, on 05/24/17 at 06:32:09

dpaa: Support c45 phys

  • Property mode set to 100644
File size: 8.1 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2#include <rtems/bsd/local/opt_dpaa.h>
3
4/*
5 * Copyright (c) 2016 embedded brains GmbH
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <machine/rtems-bsd-kernel-space.h>
31
32#include <sys/param.h>
33#include <sys/lock.h>
34#include <sys/time.h>
35#include <sys/queue.h>
36#include <sys/mutex.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39
40#include <libfdt.h>
41
42#include <rtems/bsd.h>
43
44#include <bsp/fdt.h>
45
46#include <linux/of_mdio.h>
47
48#define MDIO_LOCK()     mtx_lock(&mdio.mutex)
49#define MDIO_UNLOCK()   mtx_unlock(&mdio.mutex)
50
51static struct {
52        SLIST_HEAD(, mdio_bus) instances;
53        struct mtx mutex;
54} mdio = {
55        .instances = SLIST_HEAD_INITIALIZER(mdio.instances)
56};
57
58MTX_SYSINIT(mdio_mutex, &mdio.mutex, "MDIO", MTX_DEF);
59
60int
61phy_read(struct phy_device *phy_dev, int reg)
62{
63        struct mdio_bus *mdio_dev;
64        int val;
65
66        mdio_dev = phy_dev->mdio.bus;
67        MDIO_LOCK();
68        val = (*mdio_dev->read)(mdio_dev, phy_dev->mdio.addr,
69            reg | phy_dev->mdio.is_c45);
70        MDIO_UNLOCK();
71        return (val);
72}
73
74int
75phy_write(struct phy_device *phy_dev, int reg, int val)
76{
77        struct mdio_bus *mdio_dev;
78        int err;
79
80        mdio_dev = phy_dev->mdio.bus;
81        MDIO_LOCK();
82        err = (*mdio_dev->write)(mdio_dev, phy_dev->mdio.addr,
83            reg | phy_dev->mdio.is_c45, val);
84        MDIO_UNLOCK();
85        return (err);
86}
87
88static uint64_t
89fdt_get_address(const void *fdt, int node)
90{
91        uint64_t addr;
92        int nodes[16];
93        size_t i;
94        int ac;
95
96        i = 0;
97        do {
98                nodes[i] = node;
99                ++i;
100                node = fdt_parent_offset(fdt, node);
101        } while (node >= 0 && i < nitems(nodes));
102
103        if (node >= 0) {
104                return (0);
105        }
106
107        ac = 1;
108        addr = 0;
109        while (i > 0) {
110                const fdt32_t *p;
111                int len;
112
113                p = fdt_getprop(fdt, nodes[i - 1], "reg", &len);
114                if (p != NULL) {
115                        if (ac == 1 && len >= 4) {
116                                addr += fdt32_to_cpu(p[0]);
117                        } else if (ac == 2 && len >= 8) {
118                                addr += fdt32_to_cpu(p[1]);
119                                addr += (uint64_t)fdt32_to_cpu(p[0]) << 32;
120                        } else {
121                                return (0);
122                        }
123                }
124
125                p = fdt_getprop(fdt, nodes[i - 1], "#address-cells", &len);
126                if (p != NULL) {
127                        if (len != 4) {
128                                return (0);
129                        }
130                        ac = (int)fdt32_to_cpu(p[0]);
131                        if (ac != 1 && ac != 2) {
132                                return (0);
133                        }
134                }
135
136                --i;
137        }
138
139        return (addr);
140}
141
142struct fman_mdio_regs {
143        uint32_t reserved[12];
144        uint32_t mdio_cfg;
145        uint32_t mdio_ctrl;
146        uint32_t mdio_data;
147        uint32_t mdio_addr;
148};
149
150#define MDIO_CFG_BSY            (1U << 31)
151#define MDIO_CFG_ENC45          (1U << 6)
152#define MDIO_CFG_RD_ERR         (1U << 1)
153
154#define MDIO_CTRL_READ          (1U << 15)
155#define MDIO_CTRL_REG_ADDR(x)   ((x) & 0x1fU)
156#define MDIO_CTRL_PHY_ADDR(x)   (((x) & 0x1fU) << 5)
157
158struct fman_mdio_bus {
159        struct mdio_bus base;
160        volatile struct fman_mdio_regs *regs;
161};
162
163static int
164fman_mdio_wait(volatile struct fman_mdio_regs *regs)
165{
166        struct bintime start;
167
168        rtems_bsd_binuptime(&start);
169
170        while ((regs->mdio_cfg & MDIO_CFG_BSY) != 0) {
171                struct bintime now;
172
173                rtems_bsd_binuptime(&now);
174                if (bttosbt(now) - bttosbt(start) > 100 * SBT_1US) {
175                        break;
176                }
177        }
178
179        /* Check again, to take thread pre-emption into account */
180        if ((regs->mdio_cfg & MDIO_CFG_BSY) != 0) {
181                return (EIO);
182        }
183
184        return (0);
185}
186
187static int
188fman_mdio_setup(volatile struct fman_mdio_regs *regs, int addr, int reg,
189    uint32_t *mdio_ctrl_p)
190{
191        uint32_t mdio_cfg;
192        uint32_t mdio_ctrl;
193        uint32_t reg_addr;
194        int err;
195
196        err = fman_mdio_wait(regs);
197        if (err != 0) {
198                return (err);
199        }
200
201        mdio_cfg = regs->mdio_cfg;
202        if ((reg & MII_ADDR_C45) != 0) {
203                reg_addr = (uint32_t)(reg >> 16);
204                mdio_cfg |= MDIO_CFG_ENC45;
205        } else {
206                reg_addr = (uint32_t)reg;
207                mdio_cfg &= ~MDIO_CFG_ENC45;
208        }
209        regs->mdio_cfg = mdio_cfg;
210
211        mdio_ctrl = MDIO_CTRL_PHY_ADDR(addr) | MDIO_CTRL_REG_ADDR(reg_addr);
212        regs->mdio_ctrl = mdio_ctrl;
213
214        if ((reg & MII_ADDR_C45) != 0) {
215                regs->mdio_addr = (uint32_t)(reg & 0xffff);
216                err = fman_mdio_wait(regs);
217                if (err != 0) {
218                        return (err);
219                }
220        }
221
222        *mdio_ctrl_p = mdio_ctrl;
223        return (0);
224}
225
226static int
227fman_mdio_read(struct mdio_bus *base, int addr, int reg)
228{
229        struct fman_mdio_bus *fm;
230        volatile struct fman_mdio_regs *regs;
231        uint32_t mdio_ctrl;
232        int val;
233        int err;
234
235        fm = (struct fman_mdio_bus *)base;
236        regs = fm->regs;
237
238        err = fman_mdio_setup(regs, addr, reg, &mdio_ctrl);
239        if (err != 0) {
240                return (-1);
241        }
242
243        mdio_ctrl |= MDIO_CTRL_READ;
244        regs->mdio_ctrl = mdio_ctrl;
245
246        err = fman_mdio_wait(regs);
247        if (err == 0 && (regs->mdio_cfg & MDIO_CFG_RD_ERR) == 0) {
248                val = (int)(regs->mdio_data & 0xffff);
249        } else {
250                val = -1;
251        }
252
253        return (val);
254}
255
256static int
257fman_mdio_write(struct mdio_bus *base, int addr, int reg, int val)
258{
259        struct fman_mdio_bus *fm;
260        volatile struct fman_mdio_regs *regs;
261        uint32_t mdio_ctrl;
262        int err;
263
264        fm = (struct fman_mdio_bus *)base;
265        regs = fm->regs;
266
267        err = fman_mdio_setup(regs, addr, reg, &mdio_ctrl);
268        if (err != 0) {
269                return (0);
270        }
271
272        regs->mdio_data = (uint32_t)(val & 0xffff);
273        fman_mdio_wait(regs);
274        return (0);
275}
276
277static struct mdio_bus *
278create_fman_mdio(const void *fdt, int mdio_node)
279{
280        struct fman_mdio_bus *fm = NULL;
281
282        fm = malloc(sizeof(*fm), M_TEMP, M_WAITOK | M_ZERO);
283        if (fm == NULL) {
284                return (NULL);
285        }
286
287        fm->base.read = fman_mdio_read;
288        fm->base.write = fman_mdio_write;
289        fm->base.node = mdio_node;
290        fm->regs = (volatile struct fman_mdio_regs *)(uintptr_t)
291            fdt_get_address(fdt, mdio_node);
292
293        return (&fm->base);
294}
295
296static struct mdio_bus *
297create_mdio_bus(const void *fdt, int mdio_node)
298{
299
300        if (fdt_node_check_compatible(fdt, mdio_node,
301            "fsl,fman-memac-mdio") == 0 ||
302            fdt_node_check_compatible(fdt, mdio_node,
303            "fsl,fman-xmdio") == 0) {
304                return (create_fman_mdio(fdt, mdio_node));
305        } else {
306                return (NULL);
307        }
308}
309
310static int
311find_mdio_bus(const void *fdt, int mdio_node,
312    struct phy_device *phy_dev)
313{
314        struct mdio_bus *mdio_bus = NULL;
315
316        SLIST_FOREACH(mdio_bus, &mdio.instances, next) {
317                if (mdio_bus->node == mdio_node) {
318                        break;
319                }
320        }
321
322        if (mdio_bus == NULL) {
323                mdio_bus = create_mdio_bus(fdt, mdio_node);
324        }
325
326        if (mdio_bus == NULL) {
327                return (ENXIO);
328        }
329
330        phy_dev->mdio.bus = mdio_bus;
331        return (0);
332}
333
334static struct phy_device *
335phy_obtain(const void *fdt, int is_c45, int mdio_node, int addr)
336{
337        struct phy_device *phy_dev;
338        int err;
339
340        phy_dev = malloc(sizeof(*phy_dev), M_TEMP, M_WAITOK | M_ZERO);
341        if (phy_dev == NULL) {
342                return (NULL);
343        }
344
345        phy_dev->mdio.addr = addr;
346        phy_dev->mdio.is_c45 = is_c45;
347        MDIO_LOCK();
348        err = find_mdio_bus(fdt, mdio_node, phy_dev);
349        MDIO_UNLOCK();
350
351        if (err != 0) {
352                free(phy_dev, M_TEMP);
353                return (NULL);
354        }
355
356        return (phy_dev);
357}
358
359struct phy_device *
360of_phy_find_device(struct device_node *dn)
361{
362        const void *fdt;
363        const fdt32_t *addr;
364        int len;
365        int is_c45;
366        int mdio_node;
367
368        fdt = bsp_fdt_get();
369
370        addr = fdt_getprop(fdt, dn->offset, "reg", &len);
371        if (addr == NULL || len != sizeof(*addr)) {
372                return (NULL);
373        }
374
375        if (of_device_is_compatible(dn, "ethernet-phy-ieee802.3-c45")) {
376                is_c45 = MII_ADDR_C45;
377        } else {
378                is_c45 = 0;
379        }
380
381        mdio_node = fdt_parent_offset(fdt, dn->offset);
382        if (mdio_node < 0) {
383                return (NULL);
384        }
385
386        return (phy_obtain(fdt, is_c45, mdio_node, (int)fdt32_to_cpu(*addr)));
387}
Note: See TracBrowser for help on using the repository browser.