2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Alstom Group.
5 * Copyright (c) 2021 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
35 #include <machine/bus.h>
36 #include <machine/resource.h>
38 #include <dev/enetc/enetc_hw.h>
39 #include <dev/enetc/enetc_mdio.h>
41 #define ENETC_MDIO_RD4(regs, base, off) \
42 bus_read_4((regs), (base) + (off))
43 #define ENETC_MDIO_WR4(regs, base, off, value) \
44 bus_write_4((regs), (base) + (off), (value))
47 enetc_mdio_wait(struct resource *regs, int mdio_base)
55 val = ENETC_MDIO_RD4(regs, mdio_base, ENETC_MDIO_CFG);
56 if ((val & MDIO_CFG_BSY) == 0)
58 } while (i++ < ENETC_TIMEOUT);
64 enetc_mdio_read(struct resource *regs, int mdio_base, int phy, int reg)
66 uint32_t mdio_cfg, mdio_ctl;
69 mdio_cfg = MDIO_CFG_CLKDIV(ENETC_MDC_DIV) | MDIO_CFG_NEG;
70 if (reg & MII_ADDR_C45) {
72 dev_addr = (reg >> 16) & 0x1f;
73 mdio_cfg |= MDIO_CFG_ENC45;
76 dev_addr = reg & 0x1f;
77 mdio_cfg &= ~MDIO_CFG_ENC45;
80 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_CFG, mdio_cfg);
82 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)
85 /* Set port and device addr. */
86 mdio_ctl = MDIO_CTL_PORT_ADDR(phy) | MDIO_CTL_DEV_ADDR(dev_addr);
87 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_CTL, mdio_ctl);
89 /* Set the register address. */
90 if (reg & MII_ADDR_C45) {
91 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_ADDR, reg & 0xffff);
93 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)
97 /* Initiate the read. */
98 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_CTL, mdio_ctl | MDIO_CTL_READ);
100 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)
103 /* Check if any error occurred while reading PHY register. */
104 if (ENETC_MDIO_RD4(regs, mdio_base, ENETC_MDIO_CFG) & MDIO_CFG_RD_ER)
107 return (MDIO_DATA(ENETC_MDIO_RD4(regs, mdio_base, ENETC_MDIO_DATA)));
111 enetc_mdio_write(struct resource *regs, int mdio_base, int phy, int reg,
114 uint32_t mdio_cfg, mdio_ctl;
117 mdio_cfg = MDIO_CFG_CLKDIV(ENETC_MDC_DIV) | MDIO_CFG_NEG;
118 if (reg & MII_ADDR_C45) {
120 dev_addr = (reg >> 16) & 0x1f;
121 mdio_cfg |= MDIO_CFG_ENC45;
124 dev_addr = reg & 0x1f;
125 mdio_cfg &= ~MDIO_CFG_ENC45;
128 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_CFG, mdio_cfg);
130 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)
133 /* Set port and device addr. */
134 mdio_ctl = MDIO_CTL_PORT_ADDR(phy) | MDIO_CTL_DEV_ADDR(dev_addr);
135 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_CTL, mdio_ctl);
137 /* Set the register address. */
138 if (reg & MII_ADDR_C45) {
139 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_ADDR, reg & 0xffff);
141 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)
145 /* Write the value. */
146 ENETC_MDIO_WR4(regs, mdio_base, ENETC_MDIO_DATA, MDIO_DATA(data));
148 if (enetc_mdio_wait(regs, mdio_base) == ETIMEDOUT)