^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Vitaly Bordug <vbordug@ru.mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Anton Vorontsov <avorontsov@ru.mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2006-2007 MontaVista Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mii.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/phy_fixed.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/linkmode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "swphy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct fixed_mdio_bus {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct mii_bus *mii_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct list_head phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct fixed_phy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct phy_device *phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct fixed_phy_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) bool no_carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int (*link_update)(struct net_device *, struct fixed_phy_status *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct gpio_desc *link_gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static struct fixed_mdio_bus platform_fmb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .phys = LIST_HEAD_INIT(platform_fmb.phys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct phy_device *phydev = dev->phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct fixed_phy *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!phydev || !phydev->mdio.bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) list_for_each_entry(fp, &fmb->phys, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (fp->addr == phydev->mdio.addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) fp->no_carrier = !new_carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static void fixed_phy_update(struct fixed_phy *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!fp->no_carrier && fp->link_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct fixed_mdio_bus *fmb = bus->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct fixed_phy *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) list_for_each_entry(fp, &fmb->phys, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (fp->addr == phy_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct fixed_phy_status state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) fp->status.link = !fp->no_carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Issue callback if user registered it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (fp->link_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fp->link_update(fp->phydev->attached_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) &fp->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Check the GPIO for change in status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fixed_phy_update(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) state = fp->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return swphy_read_reg(reg_num, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * If something weird is required to be done with link/speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * network driver is able to assign a function to implement this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * May be useful for PHY's that need to be software-driven.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int fixed_phy_set_link_update(struct phy_device *phydev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int (*link_update)(struct net_device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct fixed_phy_status *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct fixed_phy *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!phydev || !phydev->mdio.bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) list_for_each_entry(fp, &fmb->phys, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (fp->addr == phydev->mdio.addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fp->link_update = link_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fp->phydev = phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct fixed_phy_status *status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct gpio_desc *gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct fixed_phy *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ret = swphy_validate_state(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) fp = kzalloc(sizeof(*fp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (irq != PHY_POLL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) fmb->mii_bus->irq[phy_addr] = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fp->addr = phy_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fp->status = *status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) fp->link_gpiod = gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) fixed_phy_update(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) list_add_tail(&fp->node, &fmb->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int fixed_phy_add(unsigned int irq, int phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct fixed_phy_status *status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) EXPORT_SYMBOL_GPL(fixed_phy_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static DEFINE_IDA(phy_fixed_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void fixed_phy_del(int phy_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct fixed_phy *fp, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (fp->addr == phy_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) list_del(&fp->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (fp->link_gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) gpiod_put(fp->link_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) kfree(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ida_simple_remove(&phy_fixed_ida, phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #ifdef CONFIG_OF_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct device_node *fixed_link_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) fixed_link_node = of_get_child_by_name(np, "fixed-link");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!fixed_link_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * As the fixed link is just a device tree node without any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * Linux device associated with it, we simply have obtain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * the GPIO descriptor from the device tree like this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) "link", 0, GPIOD_IN, "mdio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (PTR_ERR(gpiod) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) fixed_link_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) gpiod = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) of_node_put(fixed_link_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static struct phy_device *__fixed_phy_register(unsigned int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct fixed_phy_status *status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct gpio_desc *gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct phy_device *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int phy_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* Check if we have a GPIO associated with this fixed phy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!gpiod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) gpiod = fixed_phy_get_gpiod(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (IS_ERR(gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return ERR_CAST(gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* Get the next available PHY address, up to PHY_MAX_ADDR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (phy_addr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return ERR_PTR(phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ida_simple_remove(&phy_fixed_ida, phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) phy = get_phy_device(fmb->mii_bus, phy_addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (IS_ERR(phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) fixed_phy_del(phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* propagate the fixed link values to struct phy_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) phy->link = status->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (status->link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) phy->speed = status->speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) phy->duplex = status->duplex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) phy->pause = status->pause;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) phy->asym_pause = status->asym_pause;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) phy->mdio.dev.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) phy->is_pseudo_fixed_link = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) switch (status->speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case SPEED_1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) case SPEED_100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) case SPEED_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) phy->supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) phy_advertise_supported(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ret = phy_device_register(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) phy_device_free(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) fixed_phy_del(phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct phy_device *fixed_phy_register(unsigned int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct fixed_phy_status *status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return __fixed_phy_register(irq, status, np, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) EXPORT_SYMBOL_GPL(fixed_phy_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct phy_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) fixed_phy_register_with_gpiod(unsigned int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct fixed_phy_status *status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct gpio_desc *gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return __fixed_phy_register(irq, status, NULL, gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void fixed_phy_unregister(struct phy_device *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) phy_device_remove(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) of_node_put(phy->mdio.dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) fixed_phy_del(phy->mdio.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) EXPORT_SYMBOL_GPL(fixed_phy_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static int __init fixed_mdio_bus_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (IS_ERR(pdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return PTR_ERR(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) fmb->mii_bus = mdiobus_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (fmb->mii_bus == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto err_mdiobus_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) fmb->mii_bus->name = "Fixed MDIO Bus";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) fmb->mii_bus->priv = fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) fmb->mii_bus->parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) fmb->mii_bus->read = &fixed_mdio_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) fmb->mii_bus->write = &fixed_mdio_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = mdiobus_register(fmb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto err_mdiobus_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) err_mdiobus_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) mdiobus_free(fmb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) err_mdiobus_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) platform_device_unregister(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) module_init(fixed_mdio_bus_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static void __exit fixed_mdio_bus_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct fixed_mdio_bus *fmb = &platform_fmb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct fixed_phy *fp, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) mdiobus_unregister(fmb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) mdiobus_free(fmb->mii_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) platform_device_unregister(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) list_del(&fp->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) kfree(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ida_destroy(&phy_fixed_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) module_exit(fixed_mdio_bus_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) MODULE_AUTHOR("Vitaly Bordug");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) MODULE_LICENSE("GPL");