Path: blob/main/sys/arm64/broadcom/brcmmdio/mdio_nexus_iproc.c
39482 views
/*-1* Copyright (c) 2019 Juniper Networks, Inc.2* Copyright (c) 2019 Semihalf.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,18* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR20* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,22* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN23* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24* POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/bus.h>29#include <sys/kernel.h>30#include <sys/module.h>31#include <sys/rman.h>32#include <sys/systm.h>3334#include <dev/fdt/simplebus.h>35#include <dev/ofw/ofw_bus_subr.h>36#include <dev/ofw/ofw_bus.h>3738#include <machine/bus.h>39#include <machine/resource.h>4041#include "mdio_if.h"4243MALLOC_DEFINE(M_BRCM_IPROC_NEXUS, "Broadcom IPROC MDIO NEXUS",44"Broadcom IPROC MDIO NEXUS dynamic memory");4546struct brcm_mdionexus_softc {47struct simplebus_softc simplebus_sc;48uint32_t mux_id;49};5051/* OFW bus interface */52struct brcm_mdionexus_ofw_devinfo {53struct ofw_bus_devinfo di_dinfo;54struct resource_list di_rl;55};5657static device_probe_t brcm_mdionexus_fdt_probe;58static device_attach_t brcm_mdionexus_fdt_attach;5960static const struct ofw_bus_devinfo * brcm_mdionexus_ofw_get_devinfo(device_t,61device_t);62static int brcm_mdionexus_mdio_readreg(device_t, int, int);63static int brcm_mdionexus_mdio_writereg(device_t, int, int, int);6465static device_method_t brcm_mdionexus_fdt_methods[] = {66/* Device interface */67DEVMETHOD(device_probe, brcm_mdionexus_fdt_probe),68DEVMETHOD(device_attach, brcm_mdionexus_fdt_attach),6970/* Bus interface */71DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),72DEVMETHOD(bus_release_resource, bus_generic_release_resource),73DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),7475/* ofw_bus interface */76DEVMETHOD(ofw_bus_get_devinfo, brcm_mdionexus_ofw_get_devinfo),77DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),78DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),79DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),80DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),81DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),8283/* MDIO interface */84/* MDIO interface */85DEVMETHOD(mdio_readreg, brcm_mdionexus_mdio_readreg),86DEVMETHOD(mdio_writereg, brcm_mdionexus_mdio_writereg),8788DEVMETHOD_END89};9091DEFINE_CLASS_0(brcm_mdionexus, brcm_mdionexus_fdt_driver, brcm_mdionexus_fdt_methods,92sizeof(struct brcm_mdionexus_softc));9394static driver_t brcm_mdionexus_driver = {95"brcm_mdionexus",96brcm_mdionexus_fdt_methods,97sizeof(struct brcm_mdionexus_softc)98};99100EARLY_DRIVER_MODULE(brcm_mdionexus, brcm_iproc_mdio, brcm_mdionexus_driver,101NULL, NULL, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);102103static int brcm_mdionexus_ofw_bus_attach(device_t);104105static int106brcm_mdionexus_mdio_readreg(device_t dev, int phy, int reg)107{108struct brcm_mdionexus_softc *sc;109110sc = device_get_softc(dev);111112return (MDIO_READREG_MUX(device_get_parent(dev),113sc->mux_id, phy, reg));114}115116static int117brcm_mdionexus_mdio_writereg(device_t dev, int phy, int reg, int val)118{119struct brcm_mdionexus_softc *sc;120121sc = device_get_softc(dev);122123return (MDIO_WRITEREG_MUX(device_get_parent(dev),124sc->mux_id, phy, reg, val));125}126127static __inline void128get_addr_size_cells(phandle_t node, pcell_t *addr_cells, pcell_t *size_cells)129{130131*addr_cells = 2;132/* Find address cells if present */133OF_getencprop(node, "#address-cells", addr_cells, sizeof(*addr_cells));134135*size_cells = 2;136/* Find size cells if present */137OF_getencprop(node, "#size-cells", size_cells, sizeof(*size_cells));138}139140static int141brcm_mdionexus_fdt_probe(device_t dev)142{143144if (!ofw_bus_status_okay(dev))145return (ENXIO);146device_set_desc(dev, "Broadcom MDIO nexus");147return (BUS_PROBE_SPECIFIC);148}149150static int151brcm_mdionexus_fdt_attach(device_t dev)152{153struct brcm_mdionexus_softc *sc;154int err;155pcell_t addr_cells, size_cells, buf[2];156phandle_t node;157158sc = device_get_softc(dev);159160node = ofw_bus_get_node(dev);161get_addr_size_cells(node, &addr_cells, &size_cells);162if ((addr_cells != 1) || (size_cells != 0)) {163device_printf(dev, "Only addr_cells=1 and size_cells=0 are supported\n");164return (EINVAL);165}166167if (OF_getencprop(node, "reg", buf, sizeof(pcell_t)) < 0)168return (ENXIO);169170sc->mux_id = buf[0];171172err = brcm_mdionexus_ofw_bus_attach(dev);173if (err != 0)174return (err);175176bus_attach_children(dev);177return (0);178}179180static const struct ofw_bus_devinfo *181brcm_mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child)182{183struct brcm_mdionexus_ofw_devinfo *di;184185di = device_get_ivars(child);186return (&di->di_dinfo);187}188189static int190brcm_mdionexus_ofw_bus_attach(device_t dev)191{192struct simplebus_softc *sc;193struct brcm_mdionexus_ofw_devinfo *di;194device_t child;195phandle_t parent, node;196197parent = ofw_bus_get_node(dev);198simplebus_init(dev, parent);199200sc = (struct simplebus_softc *)device_get_softc(dev);201202/* Iterate through all bus subordinates */203for (node = OF_child(parent); node > 0; node = OF_peer(node)) {204/* Allocate and populate devinfo. */205di = malloc(sizeof(*di), M_BRCM_IPROC_NEXUS, M_WAITOK | M_ZERO);206if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {207free(di, M_BRCM_IPROC_NEXUS);208continue;209}210211/* Initialize and populate resource list. */212resource_list_init(&di->di_rl);213ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,214&di->di_rl);215ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);216217/* Add newbus device for this FDT node */218child = device_add_child(dev, NULL, DEVICE_UNIT_ANY);219if (child == NULL) {220resource_list_free(&di->di_rl);221ofw_bus_gen_destroy_devinfo(&di->di_dinfo);222free(di, M_BRCM_IPROC_NEXUS);223continue;224}225226device_set_ivars(child, di);227}228229return (0);230}231232233