18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Vitaly Bordug <vbordug@ru.mvista.com>
68c2ecf20Sopenharmony_ci *         Anton Vorontsov <avorontsov@ru.mvista.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (c) 2006-2007 MontaVista Software, Inc.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/list.h>
158c2ecf20Sopenharmony_ci#include <linux/mii.h>
168c2ecf20Sopenharmony_ci#include <linux/phy.h>
178c2ecf20Sopenharmony_ci#include <linux/phy_fixed.h>
188c2ecf20Sopenharmony_ci#include <linux/err.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
228c2ecf20Sopenharmony_ci#include <linux/idr.h>
238c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
248c2ecf20Sopenharmony_ci#include <linux/linkmode.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "swphy.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct fixed_mdio_bus {
298c2ecf20Sopenharmony_ci	struct mii_bus *mii_bus;
308c2ecf20Sopenharmony_ci	struct list_head phys;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct fixed_phy {
348c2ecf20Sopenharmony_ci	int addr;
358c2ecf20Sopenharmony_ci	struct phy_device *phydev;
368c2ecf20Sopenharmony_ci	struct fixed_phy_status status;
378c2ecf20Sopenharmony_ci	bool no_carrier;
388c2ecf20Sopenharmony_ci	int (*link_update)(struct net_device *, struct fixed_phy_status *);
398c2ecf20Sopenharmony_ci	struct list_head node;
408c2ecf20Sopenharmony_ci	struct gpio_desc *link_gpiod;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic struct platform_device *pdev;
448c2ecf20Sopenharmony_cistatic struct fixed_mdio_bus platform_fmb = {
458c2ecf20Sopenharmony_ci	.phys = LIST_HEAD_INIT(platform_fmb.phys),
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciint fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
518c2ecf20Sopenharmony_ci	struct phy_device *phydev = dev->phydev;
528c2ecf20Sopenharmony_ci	struct fixed_phy *fp;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (!phydev || !phydev->mdio.bus)
558c2ecf20Sopenharmony_ci		return -EINVAL;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	list_for_each_entry(fp, &fmb->phys, node) {
588c2ecf20Sopenharmony_ci		if (fp->addr == phydev->mdio.addr) {
598c2ecf20Sopenharmony_ci			fp->no_carrier = !new_carrier;
608c2ecf20Sopenharmony_ci			return 0;
618c2ecf20Sopenharmony_ci		}
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci	return -EINVAL;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void fixed_phy_update(struct fixed_phy *fp)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	if (!fp->no_carrier && fp->link_gpiod)
708c2ecf20Sopenharmony_ci		fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = bus->priv;
768c2ecf20Sopenharmony_ci	struct fixed_phy *fp;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	list_for_each_entry(fp, &fmb->phys, node) {
798c2ecf20Sopenharmony_ci		if (fp->addr == phy_addr) {
808c2ecf20Sopenharmony_ci			struct fixed_phy_status state;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci			fp->status.link = !fp->no_carrier;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci			/* Issue callback if user registered it. */
858c2ecf20Sopenharmony_ci			if (fp->link_update)
868c2ecf20Sopenharmony_ci				fp->link_update(fp->phydev->attached_dev,
878c2ecf20Sopenharmony_ci						&fp->status);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci			/* Check the GPIO for change in status */
908c2ecf20Sopenharmony_ci			fixed_phy_update(fp);
918c2ecf20Sopenharmony_ci			state = fp->status;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci			return swphy_read_reg(reg_num, &state);
948c2ecf20Sopenharmony_ci		}
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return 0xFFFF;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
1018c2ecf20Sopenharmony_ci			    u16 val)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/*
1078c2ecf20Sopenharmony_ci * If something weird is required to be done with link/speed,
1088c2ecf20Sopenharmony_ci * network driver is able to assign a function to implement this.
1098c2ecf20Sopenharmony_ci * May be useful for PHY's that need to be software-driven.
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_ciint fixed_phy_set_link_update(struct phy_device *phydev,
1128c2ecf20Sopenharmony_ci			      int (*link_update)(struct net_device *,
1138c2ecf20Sopenharmony_ci						 struct fixed_phy_status *))
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
1168c2ecf20Sopenharmony_ci	struct fixed_phy *fp;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!phydev || !phydev->mdio.bus)
1198c2ecf20Sopenharmony_ci		return -EINVAL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	list_for_each_entry(fp, &fmb->phys, node) {
1228c2ecf20Sopenharmony_ci		if (fp->addr == phydev->mdio.addr) {
1238c2ecf20Sopenharmony_ci			fp->link_update = link_update;
1248c2ecf20Sopenharmony_ci			fp->phydev = phydev;
1258c2ecf20Sopenharmony_ci			return 0;
1268c2ecf20Sopenharmony_ci		}
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return -ENOENT;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
1348c2ecf20Sopenharmony_ci			       struct fixed_phy_status *status,
1358c2ecf20Sopenharmony_ci			       struct gpio_desc *gpiod)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	int ret;
1388c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
1398c2ecf20Sopenharmony_ci	struct fixed_phy *fp;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	ret = swphy_validate_state(status);
1428c2ecf20Sopenharmony_ci	if (ret < 0)
1438c2ecf20Sopenharmony_ci		return ret;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	fp = kzalloc(sizeof(*fp), GFP_KERNEL);
1468c2ecf20Sopenharmony_ci	if (!fp)
1478c2ecf20Sopenharmony_ci		return -ENOMEM;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (irq != PHY_POLL)
1508c2ecf20Sopenharmony_ci		fmb->mii_bus->irq[phy_addr] = irq;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	fp->addr = phy_addr;
1538c2ecf20Sopenharmony_ci	fp->status = *status;
1548c2ecf20Sopenharmony_ci	fp->link_gpiod = gpiod;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	fixed_phy_update(fp);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	list_add_tail(&fp->node, &fmb->phys);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciint fixed_phy_add(unsigned int irq, int phy_addr,
1648c2ecf20Sopenharmony_ci		  struct fixed_phy_status *status) {
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_add);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic DEFINE_IDA(phy_fixed_ida);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void fixed_phy_del(int phy_addr)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
1758c2ecf20Sopenharmony_ci	struct fixed_phy *fp, *tmp;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
1788c2ecf20Sopenharmony_ci		if (fp->addr == phy_addr) {
1798c2ecf20Sopenharmony_ci			list_del(&fp->node);
1808c2ecf20Sopenharmony_ci			if (fp->link_gpiod)
1818c2ecf20Sopenharmony_ci				gpiod_put(fp->link_gpiod);
1828c2ecf20Sopenharmony_ci			kfree(fp);
1838c2ecf20Sopenharmony_ci			ida_simple_remove(&phy_fixed_ida, phy_addr);
1848c2ecf20Sopenharmony_ci			return;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#ifdef CONFIG_OF_GPIO
1908c2ecf20Sopenharmony_cistatic struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct device_node *fixed_link_node;
1938c2ecf20Sopenharmony_ci	struct gpio_desc *gpiod;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (!np)
1968c2ecf20Sopenharmony_ci		return NULL;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	fixed_link_node = of_get_child_by_name(np, "fixed-link");
1998c2ecf20Sopenharmony_ci	if (!fixed_link_node)
2008c2ecf20Sopenharmony_ci		return NULL;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/*
2038c2ecf20Sopenharmony_ci	 * As the fixed link is just a device tree node without any
2048c2ecf20Sopenharmony_ci	 * Linux device associated with it, we simply have obtain
2058c2ecf20Sopenharmony_ci	 * the GPIO descriptor from the device tree like this.
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci	gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
2088c2ecf20Sopenharmony_ci				       "link", 0, GPIOD_IN, "mdio");
2098c2ecf20Sopenharmony_ci	if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
2108c2ecf20Sopenharmony_ci		if (PTR_ERR(gpiod) != -ENOENT)
2118c2ecf20Sopenharmony_ci			pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
2128c2ecf20Sopenharmony_ci			       fixed_link_node);
2138c2ecf20Sopenharmony_ci		gpiod = NULL;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci	of_node_put(fixed_link_node);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	return gpiod;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci#else
2208c2ecf20Sopenharmony_cistatic struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return NULL;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci#endif
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic struct phy_device *__fixed_phy_register(unsigned int irq,
2278c2ecf20Sopenharmony_ci					       struct fixed_phy_status *status,
2288c2ecf20Sopenharmony_ci					       struct device_node *np,
2298c2ecf20Sopenharmony_ci					       struct gpio_desc *gpiod)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
2328c2ecf20Sopenharmony_ci	struct phy_device *phy;
2338c2ecf20Sopenharmony_ci	int phy_addr;
2348c2ecf20Sopenharmony_ci	int ret;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
2378c2ecf20Sopenharmony_ci		return ERR_PTR(-EPROBE_DEFER);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Check if we have a GPIO associated with this fixed phy */
2408c2ecf20Sopenharmony_ci	if (!gpiod) {
2418c2ecf20Sopenharmony_ci		gpiod = fixed_phy_get_gpiod(np);
2428c2ecf20Sopenharmony_ci		if (IS_ERR(gpiod))
2438c2ecf20Sopenharmony_ci			return ERR_CAST(gpiod);
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* Get the next available PHY address, up to PHY_MAX_ADDR */
2478c2ecf20Sopenharmony_ci	phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
2488c2ecf20Sopenharmony_ci	if (phy_addr < 0)
2498c2ecf20Sopenharmony_ci		return ERR_PTR(phy_addr);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
2528c2ecf20Sopenharmony_ci	if (ret < 0) {
2538c2ecf20Sopenharmony_ci		ida_simple_remove(&phy_fixed_ida, phy_addr);
2548c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	phy = get_phy_device(fmb->mii_bus, phy_addr, false);
2588c2ecf20Sopenharmony_ci	if (IS_ERR(phy)) {
2598c2ecf20Sopenharmony_ci		fixed_phy_del(phy_addr);
2608c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* propagate the fixed link values to struct phy_device */
2648c2ecf20Sopenharmony_ci	phy->link = status->link;
2658c2ecf20Sopenharmony_ci	if (status->link) {
2668c2ecf20Sopenharmony_ci		phy->speed = status->speed;
2678c2ecf20Sopenharmony_ci		phy->duplex = status->duplex;
2688c2ecf20Sopenharmony_ci		phy->pause = status->pause;
2698c2ecf20Sopenharmony_ci		phy->asym_pause = status->asym_pause;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	of_node_get(np);
2738c2ecf20Sopenharmony_ci	phy->mdio.dev.of_node = np;
2748c2ecf20Sopenharmony_ci	phy->is_pseudo_fixed_link = true;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	switch (status->speed) {
2778c2ecf20Sopenharmony_ci	case SPEED_1000:
2788c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2798c2ecf20Sopenharmony_ci				 phy->supported);
2808c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2818c2ecf20Sopenharmony_ci				 phy->supported);
2828c2ecf20Sopenharmony_ci		fallthrough;
2838c2ecf20Sopenharmony_ci	case SPEED_100:
2848c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
2858c2ecf20Sopenharmony_ci				 phy->supported);
2868c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2878c2ecf20Sopenharmony_ci				 phy->supported);
2888c2ecf20Sopenharmony_ci		fallthrough;
2898c2ecf20Sopenharmony_ci	case SPEED_10:
2908c2ecf20Sopenharmony_ci	default:
2918c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
2928c2ecf20Sopenharmony_ci				 phy->supported);
2938c2ecf20Sopenharmony_ci		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
2948c2ecf20Sopenharmony_ci				 phy->supported);
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	phy_advertise_supported(phy);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	ret = phy_device_register(phy);
3008c2ecf20Sopenharmony_ci	if (ret) {
3018c2ecf20Sopenharmony_ci		phy_device_free(phy);
3028c2ecf20Sopenharmony_ci		of_node_put(np);
3038c2ecf20Sopenharmony_ci		fixed_phy_del(phy_addr);
3048c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return phy;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistruct phy_device *fixed_phy_register(unsigned int irq,
3118c2ecf20Sopenharmony_ci				      struct fixed_phy_status *status,
3128c2ecf20Sopenharmony_ci				      struct device_node *np)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	return __fixed_phy_register(irq, status, np, NULL);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_register);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistruct phy_device *
3198c2ecf20Sopenharmony_cifixed_phy_register_with_gpiod(unsigned int irq,
3208c2ecf20Sopenharmony_ci			      struct fixed_phy_status *status,
3218c2ecf20Sopenharmony_ci			      struct gpio_desc *gpiod)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	return __fixed_phy_register(irq, status, NULL, gpiod);
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_civoid fixed_phy_unregister(struct phy_device *phy)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	phy_device_remove(phy);
3308c2ecf20Sopenharmony_ci	of_node_put(phy->mdio.dev.of_node);
3318c2ecf20Sopenharmony_ci	fixed_phy_del(phy->mdio.addr);
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fixed_phy_unregister);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic int __init fixed_mdio_bus_init(void)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
3388c2ecf20Sopenharmony_ci	int ret;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
3418c2ecf20Sopenharmony_ci	if (IS_ERR(pdev))
3428c2ecf20Sopenharmony_ci		return PTR_ERR(pdev);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	fmb->mii_bus = mdiobus_alloc();
3458c2ecf20Sopenharmony_ci	if (fmb->mii_bus == NULL) {
3468c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3478c2ecf20Sopenharmony_ci		goto err_mdiobus_reg;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
3518c2ecf20Sopenharmony_ci	fmb->mii_bus->name = "Fixed MDIO Bus";
3528c2ecf20Sopenharmony_ci	fmb->mii_bus->priv = fmb;
3538c2ecf20Sopenharmony_ci	fmb->mii_bus->parent = &pdev->dev;
3548c2ecf20Sopenharmony_ci	fmb->mii_bus->read = &fixed_mdio_read;
3558c2ecf20Sopenharmony_ci	fmb->mii_bus->write = &fixed_mdio_write;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	ret = mdiobus_register(fmb->mii_bus);
3588c2ecf20Sopenharmony_ci	if (ret)
3598c2ecf20Sopenharmony_ci		goto err_mdiobus_alloc;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cierr_mdiobus_alloc:
3648c2ecf20Sopenharmony_ci	mdiobus_free(fmb->mii_bus);
3658c2ecf20Sopenharmony_cierr_mdiobus_reg:
3668c2ecf20Sopenharmony_ci	platform_device_unregister(pdev);
3678c2ecf20Sopenharmony_ci	return ret;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_cimodule_init(fixed_mdio_bus_init);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void __exit fixed_mdio_bus_exit(void)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct fixed_mdio_bus *fmb = &platform_fmb;
3748c2ecf20Sopenharmony_ci	struct fixed_phy *fp, *tmp;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	mdiobus_unregister(fmb->mii_bus);
3778c2ecf20Sopenharmony_ci	mdiobus_free(fmb->mii_bus);
3788c2ecf20Sopenharmony_ci	platform_device_unregister(pdev);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
3818c2ecf20Sopenharmony_ci		list_del(&fp->node);
3828c2ecf20Sopenharmony_ci		kfree(fp);
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci	ida_destroy(&phy_fixed_ida);
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_cimodule_exit(fixed_mdio_bus_exit);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
3898c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vitaly Bordug");
3908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
391