diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2018-01-05 12:08:19 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-01-05 12:08:19 +0100 |
commit | cdd3ad8795af8d2b9384b5c2919cef9365749520 (patch) | |
tree | 08c26c8881a24b297aee41a0fc2911c4a0c3b849 /drivers | |
parent | 10b25c12c2b2afa6fea0959596f7341d30b196cf (diff) | |
parent | 9e34d2ee095eef72abda98af486b2c26020a9622 (diff) | |
download | barebox-cdd3ad8795af8d2b9384b5c2919cef9365749520.tar.gz barebox-cdd3ad8795af8d2b9384b5c2919cef9365749520.tar.xz |
Merge branch 'for-next/mii'
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/net/phy/Kconfig | 18 | ||||
-rw-r--r-- | drivers/net/phy/Makefile | 3 | ||||
-rw-r--r-- | drivers/net/phy/mdio-mux-gpio.c | 147 | ||||
-rw-r--r-- | drivers/net/phy/mdio-mux.c | 143 | ||||
-rw-r--r-- | drivers/net/phy/mdio_bus.c | 30 |
5 files changed, 341 insertions, 0 deletions
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig index ea2e062656..cda752b659 100644 --- a/drivers/net/phy/Kconfig +++ b/drivers/net/phy/Kconfig @@ -66,6 +66,24 @@ config MDIO_GPIO ---help--- Supports GPIO lib-based MDIO busses. +config MDIO_BUS_MUX + bool + help + This module provides a driver framework for MDIO bus + multiplexers which connect one of several child MDIO busses + to a parent bus. Switching between child busses is done by + device specific drivers. + +config MDIO_BUS_MUX_GPIO + bool "GPIO controlled MDIO bus multiplexers" + depends on OF_GPIO + select MDIO_BUS_MUX + help + This module provides a driver for MDIO bus multiplexers that + are controlled via GPIO lines. The multiplexer connects one of + several child MDIO busses to a parent bus. Child bus + selection is under the control of GPIO lines. + endif endmenu diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile index 13b8f6545d..30b20f8ee6 100644 --- a/drivers/net/phy/Makefile +++ b/drivers/net/phy/Makefile @@ -10,3 +10,6 @@ obj-$(CONFIG_SMSC_PHY) += smsc.o obj-$(CONFIG_MDIO_MVEBU) += mdio-mvebu.o obj-$(CONFIG_MDIO_BITBANG) += mdio-bitbang.o obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o +obj-$(CONFIG_MDIO_BUS_MUX) += mdio-mux.o +obj-$(CONFIG_MDIO_BUS_MUX_GPIO) += mdio-mux-gpio.o + diff --git a/drivers/net/phy/mdio-mux-gpio.c b/drivers/net/phy/mdio-mux-gpio.c new file mode 100644 index 0000000000..221353cbcb --- /dev/null +++ b/drivers/net/phy/mdio-mux-gpio.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2017 Zodiac Inflight Innovation + * Author: Andrey Smirnov <andrew.smirnov@gmail.com> + * + * Based on analogous code from Linux kernel + * + * Copyright (C) 2011, 2012 Cavium, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * This file is subject to the terms and conditions of the GNU General Public + * License. See the file "COPYING" in the main directory of this archive + * for more details. + * + */ +#include <common.h> +#include <driver.h> +#include <init.h> +#include <linux/mdio-mux.h> +#include <gpio.h> +#include <of_gpio.h> + + +struct mdio_mux_gpio_state { + struct gpio *gpios; + int gpios_num; +}; + +static void mdio_mux_gpio_set_active(struct mdio_mux_gpio_state *s, + unsigned int mask, int value) +{ + while (mask) { + const int n = __ffs(mask); + mask >>= n + 1; + + if (WARN_ON(n >= s->gpios_num)) + break; + + gpio_set_active(s->gpios[n].gpio, value); + } +} + +static int mdio_mux_gpio_switch_fn(int current_child, int desired_child, + void *data) +{ + if (current_child < 0) + current_child = 0; + + if (current_child != desired_child) { + struct mdio_mux_gpio_state *s = data; + /* + * GPIOs that are set in both current and desired + * states can be left untouched. So we calculate them + * and clear corresponding bits in both states. + */ + const int unchanged = current_child & desired_child; + + current_child &= ~unchanged; + desired_child &= ~unchanged; + + /* + * First step: disable all of the currently selected + * bits that are no loger needed + */ + mdio_mux_gpio_set_active(s, current_child, 0); + /* + * Second step: enable all of the GPIOs that are + * desired to be selected + */ + mdio_mux_gpio_set_active(s, desired_child, 1); + } + + return 0; +} + +static int mdio_mux_gpio_probe(struct device_d *dev) +{ + struct mdio_mux_gpio_state *s; + int i, r; + + s = xzalloc(sizeof(*s)); + + s->gpios_num = of_gpio_count(dev->device_node); + if (s->gpios_num <= 0) { + dev_err(dev, "No GPIOs specified\n"); + r = -EINVAL; + goto free_mem; + } + + s->gpios = xzalloc(s->gpios_num * sizeof(s->gpios[0])); + + for (i = 0; i < s->gpios_num; i++) { + enum of_gpio_flags flags; + + r = of_get_gpio_flags(dev->device_node, i, &flags); + if (!gpio_is_valid(r)) { + r = (r < 0) ? r : -EINVAL; + goto free_mem; + } + + s->gpios[i].gpio = r; + s->gpios[i].label = dev_name(dev); + s->gpios[i].flags = GPIOF_OUT_INIT_INACTIVE; + + if (flags & OF_GPIO_ACTIVE_LOW) + s->gpios[i].flags |= GPIOF_ACTIVE_LOW; + } + + r = gpio_request_array(s->gpios, s->gpios_num); + if (r < 0) + goto free_gpios; + + + r = mdio_mux_init(dev, dev->device_node, + mdio_mux_gpio_switch_fn, s, NULL); + if (r < 0) + goto free_gpios; + + return 0; + +free_gpios: + gpio_free_array(s->gpios, s->gpios_num); +free_mem: + free(s->gpios); + free(s); + return r; +} + +static const struct of_device_id mdio_mux_gpio_match[] = { + { + .compatible = "mdio-mux-gpio", + }, + {}, +}; + +static struct driver_d mdio_mux_gpio_driver = { + .name = "mdio-mux-gpio", + .probe = mdio_mux_gpio_probe, + .of_compatible = mdio_mux_gpio_match, +}; +device_platform_driver(mdio_mux_gpio_driver); diff --git a/drivers/net/phy/mdio-mux.c b/drivers/net/phy/mdio-mux.c new file mode 100644 index 0000000000..1f57d86c6f --- /dev/null +++ b/drivers/net/phy/mdio-mux.c @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2017 Zodiac Inflight Innovation + * Author: Andrey Smirnov <andrew.smirnov@gmail.com> + * + * Based on analogous code from Linux kernel + * + * Copyright (C) 2011, 2012 Cavium, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ +#include <common.h> +#include <linux/mdio-mux.h> +#include <linux/phy.h> + +struct mdio_mux_parent_bus { + struct mii_bus *mii_bus; + int current_child; + int parent_id; + void *switch_data; + int (*switch_fn)(int current_child, int desired_child, void *data); +}; + +struct mdio_mux_child_bus { + struct mii_bus mii_bus; + struct mdio_mux_parent_bus *parent; + struct mdio_mux_child_bus *next; + int bus_number; +}; + +static int mdio_mux_read_or_write(struct mii_bus *bus, int phy_id, + int regnum, u16 *val) +{ + struct mdio_mux_child_bus *cb = bus->priv; + struct mdio_mux_parent_bus *pb = cb->parent; + int r; + + r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data); + if (!r) { + pb->current_child = cb->bus_number; + if (val) + r = pb->mii_bus->write(pb->mii_bus, phy_id, + regnum, *val); + else + r = pb->mii_bus->read(pb->mii_bus, phy_id, + regnum); + } + return r; +} + +static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum) +{ + return mdio_mux_read_or_write(bus, phy_id, regnum, NULL); +} + +static int mdio_mux_write(struct mii_bus *bus, int phy_id, + int regnum, u16 val) +{ + return mdio_mux_read_or_write(bus, phy_id, regnum, &val); +} + +int mdio_mux_init(struct device_d *dev, + struct device_node *mux_node, + int (*switch_fn)(int cur, int desired, void *data), + void *data, + struct mii_bus *mux_bus) +{ + static int parent_count = 0; + + struct device_node *parent_bus_node; + struct device_node *child_bus_node; + struct mdio_mux_parent_bus *pb; + struct mdio_mux_child_bus *cb; + struct mii_bus *parent_bus; + int r; + + if (!mux_node) + return -ENODEV; + + if (!mux_bus) { + parent_bus_node = of_parse_phandle(mux_node, + "mdio-parent-bus", 0); + + if (!parent_bus_node) + return -ENODEV; + + parent_bus = of_mdio_find_bus(parent_bus_node); + + if (!parent_bus) + return -EPROBE_DEFER; + } else { + parent_bus_node = NULL; + parent_bus = mux_bus; + } + + pb = xzalloc(sizeof(*pb)); + + pb->switch_data = data; + pb->switch_fn = switch_fn; + pb->current_child = -1; + pb->parent_id = parent_count++; + pb->mii_bus = parent_bus; + + for_each_available_child_of_node(mux_node, child_bus_node) { + int v; + + r = of_property_read_u32(child_bus_node, "reg", &v); + if (r) { + dev_err(dev, + "Error: Failed to find reg for child %pOF\n", + child_bus_node); + continue; + } + + cb = xzalloc(sizeof(*cb)); + cb->bus_number = v; + cb->parent = pb; + + cb->mii_bus.priv = cb; + cb->mii_bus.parent = dev; + cb->mii_bus.read = mdio_mux_read; + cb->mii_bus.write = mdio_mux_write; + cb->mii_bus.dev.device_node = child_bus_node; + + r = mdiobus_register(&cb->mii_bus); + if (r) { + dev_err(dev, + "Error: Failed to register MDIO bus for child %pOF\n", + child_bus_node); + } + } + + parent_bus->is_multiplexed = true; + return 0; +} +EXPORT_SYMBOL_GPL(mdio_mux_init); diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c index 012b90e834..d209716a14 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -32,6 +32,9 @@ int mdiobus_detect(struct device_d *dev) struct mii_bus *mii = to_mii_bus(dev); int i, ret; + if (mii->is_multiplexed) + return 0; + for (i = 0; i < PHY_MAX_ADDR; i++) { struct phy_device *phydev; @@ -207,6 +210,33 @@ struct mii_bus *mdiobus_get_bus(int busnum) } /** + * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. + * @mdio_bus_np: Pointer to the mii_bus. + * + * Returns a reference to the mii_bus, or NULL if none found. + * + * Because the association of a device_node and mii_bus is made via + * mdiobus_register(), the mii_bus cannot be found before it is + * registered with mdiobus_register(). + * + */ +struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) +{ + struct mii_bus *mii; + + if (!mdio_bus_np) + return NULL; + + for_each_mii_bus(mii) + if (mii->dev.device_node == mdio_bus_np) + return mii; + + return NULL; +} +EXPORT_SYMBOL(of_mdio_find_bus); + + +/** * mdio_bus_match - determine if given PHY driver supports the given PHY device * @dev: target PHY device * @drv: given PHY driver |