[28ee86a] | 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 | |
---|
[cd089b9] | 46 | #include <linux/of_mdio.h> |
---|
| 47 | |
---|
[28ee86a] | 48 | #define MDIO_LOCK() mtx_lock(&mdio.mutex) |
---|
| 49 | #define MDIO_UNLOCK() mtx_unlock(&mdio.mutex) |
---|
| 50 | |
---|
| 51 | static struct { |
---|
[cd089b9] | 52 | SLIST_HEAD(, mdio_bus) instances; |
---|
[28ee86a] | 53 | struct mtx mutex; |
---|
| 54 | } mdio = { |
---|
| 55 | .instances = SLIST_HEAD_INITIALIZER(mdio.instances) |
---|
| 56 | }; |
---|
| 57 | |
---|
[9da83e7] | 58 | MTX_SYSINIT(mdio_mutex, &mdio.mutex, "MDIO", MTX_DEF); |
---|
| 59 | |
---|
| 60 | int |
---|
| 61 | phy_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 | |
---|
| 74 | int |
---|
| 75 | phy_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 | } |
---|
[28ee86a] | 87 | |
---|
| 88 | static uint64_t |
---|
| 89 | fdt_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 | |
---|
| 142 | struct 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 | |
---|
[cd089b9] | 158 | struct fman_mdio_bus { |
---|
| 159 | struct mdio_bus base; |
---|
[28ee86a] | 160 | volatile struct fman_mdio_regs *regs; |
---|
| 161 | }; |
---|
| 162 | |
---|
| 163 | static int |
---|
| 164 | fman_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 | |
---|
| 187 | static int |
---|
[9da83e7] | 188 | fman_mdio_setup(volatile struct fman_mdio_regs *regs, int addr, int reg, |
---|
| 189 | uint32_t *mdio_ctrl_p) |
---|
[28ee86a] | 190 | { |
---|
[9da83e7] | 191 | uint32_t mdio_cfg; |
---|
| 192 | uint32_t mdio_ctrl; |
---|
| 193 | uint32_t reg_addr; |
---|
[28ee86a] | 194 | int err; |
---|
| 195 | |
---|
| 196 | err = fman_mdio_wait(regs); |
---|
[9da83e7] | 197 | if (err != 0) { |
---|
| 198 | return (err); |
---|
| 199 | } |
---|
[28ee86a] | 200 | |
---|
[9da83e7] | 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; |
---|
[28ee86a] | 207 | mdio_cfg &= ~MDIO_CFG_ENC45; |
---|
[9da83e7] | 208 | } |
---|
| 209 | regs->mdio_cfg = mdio_cfg; |
---|
[28ee86a] | 210 | |
---|
[9da83e7] | 211 | mdio_ctrl = MDIO_CTRL_PHY_ADDR(addr) | MDIO_CTRL_REG_ADDR(reg_addr); |
---|
| 212 | regs->mdio_ctrl = mdio_ctrl; |
---|
[28ee86a] | 213 | |
---|
[9da83e7] | 214 | if ((reg & MII_ADDR_C45) != 0) { |
---|
| 215 | regs->mdio_addr = (uint32_t)(reg & 0xffff); |
---|
[28ee86a] | 216 | err = fman_mdio_wait(regs); |
---|
[9da83e7] | 217 | if (err != 0) { |
---|
| 218 | return (err); |
---|
[28ee86a] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[9da83e7] | 222 | *mdio_ctrl_p = mdio_ctrl; |
---|
| 223 | return (0); |
---|
[28ee86a] | 224 | } |
---|
| 225 | |
---|
| 226 | static int |
---|
[9da83e7] | 227 | fman_mdio_read(struct mdio_bus *base, int addr, int reg) |
---|
[28ee86a] | 228 | { |
---|
[cd089b9] | 229 | struct fman_mdio_bus *fm; |
---|
[28ee86a] | 230 | volatile struct fman_mdio_regs *regs; |
---|
[9da83e7] | 231 | uint32_t mdio_ctrl; |
---|
| 232 | int val; |
---|
[28ee86a] | 233 | int err; |
---|
| 234 | |
---|
[cd089b9] | 235 | fm = (struct fman_mdio_bus *)base; |
---|
[28ee86a] | 236 | regs = fm->regs; |
---|
| 237 | |
---|
[9da83e7] | 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; |
---|
[28ee86a] | 245 | |
---|
| 246 | err = fman_mdio_wait(regs); |
---|
[9da83e7] | 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 | } |
---|
[28ee86a] | 252 | |
---|
[9da83e7] | 253 | return (val); |
---|
| 254 | } |
---|
[28ee86a] | 255 | |
---|
[9da83e7] | 256 | static int |
---|
| 257 | fman_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; |
---|
[28ee86a] | 263 | |
---|
[9da83e7] | 264 | fm = (struct fman_mdio_bus *)base; |
---|
| 265 | regs = fm->regs; |
---|
[28ee86a] | 266 | |
---|
[9da83e7] | 267 | err = fman_mdio_setup(regs, addr, reg, &mdio_ctrl); |
---|
| 268 | if (err != 0) { |
---|
| 269 | return (0); |
---|
[28ee86a] | 270 | } |
---|
| 271 | |
---|
[9da83e7] | 272 | regs->mdio_data = (uint32_t)(val & 0xffff); |
---|
| 273 | fman_mdio_wait(regs); |
---|
[28ee86a] | 274 | return (0); |
---|
| 275 | } |
---|
| 276 | |
---|
[cd089b9] | 277 | static struct mdio_bus * |
---|
[28ee86a] | 278 | create_fman_mdio(const void *fdt, int mdio_node) |
---|
| 279 | { |
---|
[cd089b9] | 280 | struct fman_mdio_bus *fm = NULL; |
---|
[28ee86a] | 281 | |
---|
| 282 | fm = malloc(sizeof(*fm), M_TEMP, M_WAITOK | M_ZERO); |
---|
| 283 | if (fm == NULL) { |
---|
| 284 | return (NULL); |
---|
| 285 | } |
---|
| 286 | |
---|
[cd089b9] | 287 | fm->base.read = fman_mdio_read; |
---|
| 288 | fm->base.write = fman_mdio_write; |
---|
| 289 | fm->base.node = mdio_node; |
---|
[28ee86a] | 290 | fm->regs = (volatile struct fman_mdio_regs *)(uintptr_t) |
---|
| 291 | fdt_get_address(fdt, mdio_node); |
---|
| 292 | |
---|
| 293 | return (&fm->base); |
---|
| 294 | } |
---|
| 295 | |
---|
[cd089b9] | 296 | static struct mdio_bus * |
---|
| 297 | create_mdio_bus(const void *fdt, int mdio_node) |
---|
[28ee86a] | 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 | |
---|
| 310 | static int |
---|
[cd089b9] | 311 | find_mdio_bus(const void *fdt, int mdio_node, |
---|
| 312 | struct phy_device *phy_dev) |
---|
[28ee86a] | 313 | { |
---|
[cd089b9] | 314 | struct mdio_bus *mdio_bus = NULL; |
---|
[28ee86a] | 315 | |
---|
[cd089b9] | 316 | SLIST_FOREACH(mdio_bus, &mdio.instances, next) { |
---|
| 317 | if (mdio_bus->node == mdio_node) { |
---|
[28ee86a] | 318 | break; |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[cd089b9] | 322 | if (mdio_bus == NULL) { |
---|
| 323 | mdio_bus = create_mdio_bus(fdt, mdio_node); |
---|
[28ee86a] | 324 | } |
---|
| 325 | |
---|
[cd089b9] | 326 | if (mdio_bus == NULL) { |
---|
[28ee86a] | 327 | return (ENXIO); |
---|
| 328 | } |
---|
| 329 | |
---|
[cd089b9] | 330 | phy_dev->mdio.bus = mdio_bus; |
---|
[28ee86a] | 331 | return (0); |
---|
| 332 | } |
---|
| 333 | |
---|
[cd089b9] | 334 | static struct phy_device * |
---|
[9da83e7] | 335 | phy_obtain(const void *fdt, int is_c45, int mdio_node, int addr) |
---|
[28ee86a] | 336 | { |
---|
[cd089b9] | 337 | struct phy_device *phy_dev; |
---|
[28ee86a] | 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 | |
---|
[9da83e7] | 345 | phy_dev->mdio.addr = addr; |
---|
| 346 | phy_dev->mdio.is_c45 = is_c45; |
---|
[28ee86a] | 347 | MDIO_LOCK(); |
---|
[cd089b9] | 348 | err = find_mdio_bus(fdt, mdio_node, phy_dev); |
---|
[28ee86a] | 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 | |
---|
[cd089b9] | 359 | struct phy_device * |
---|
| 360 | of_phy_find_device(struct device_node *dn) |
---|
[28ee86a] | 361 | { |
---|
| 362 | const void *fdt; |
---|
[9da83e7] | 363 | const fdt32_t *addr; |
---|
[28ee86a] | 364 | int len; |
---|
[9da83e7] | 365 | int is_c45; |
---|
[cd089b9] | 366 | int mdio_node; |
---|
[28ee86a] | 367 | |
---|
| 368 | fdt = bsp_fdt_get(); |
---|
| 369 | |
---|
[9da83e7] | 370 | addr = fdt_getprop(fdt, dn->offset, "reg", &len); |
---|
| 371 | if (addr == NULL || len != sizeof(*addr)) { |
---|
[28ee86a] | 372 | return (NULL); |
---|
| 373 | } |
---|
| 374 | |
---|
[9da83e7] | 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 | |
---|
[cd089b9] | 381 | mdio_node = fdt_parent_offset(fdt, dn->offset); |
---|
| 382 | if (mdio_node < 0) { |
---|
[28ee86a] | 383 | return (NULL); |
---|
| 384 | } |
---|
| 385 | |
---|
[9da83e7] | 386 | return (phy_obtain(fdt, is_c45, mdio_node, (int)fdt32_to_cpu(*addr))); |
---|
[28ee86a] | 387 | } |
---|