1319815Sian/*- 2319815Sian * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> 3319815Sian * All rights reserved. 4319815Sian * 5319815Sian * Development sponsored by Microsemi, Inc. 6319815Sian * 7319815Sian * Redistribution and use in source and binary forms, with or without 8319815Sian * modification, are permitted provided that the following conditions 9319815Sian * are met: 10319815Sian * 1. Redistributions of source code must retain the above copyright 11319815Sian * notice, this list of conditions and the following disclaimer. 12319815Sian * 2. Redistributions in binary form must reproduce the above copyright 13319815Sian * notice, this list of conditions and the following disclaimer in the 14319815Sian * documentation and/or other materials provided with the distribution. 15319815Sian * 16319815Sian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17319815Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18319815Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19319815Sian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20319815Sian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21319815Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22319815Sian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23319815Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24319815Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25319815Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26319815Sian * SUCH DAMAGE. 27319815Sian */ 28319815Sian 29319815Sian#include <sys/cdefs.h> 30319815Sian__FBSDID("$FreeBSD: stable/11/sys/dev/mii/mii_fdt.c 323414 2017-09-11 01:59:24Z ian $"); 31319815Sian 32319815Sian/* 33319815Sian * Utility functions for PHY drivers on systems configured using FDT data. 34319815Sian */ 35319815Sian 36319815Sian#include <sys/param.h> 37319815Sian#include <sys/systm.h> 38319815Sian#include <sys/socket.h> 39319815Sian#include <sys/bus.h> 40319815Sian#include <sys/malloc.h> 41319815Sian 42319815Sian#include <net/if.h> 43319815Sian#include <net/if_media.h> 44319815Sian 45319815Sian#include <dev/ofw/openfirm.h> 46319815Sian#include <dev/ofw/ofw_bus.h> 47319815Sian#include <dev/ofw/ofw_bus_subr.h> 48319815Sian 49319815Sian#include <dev/mii/mii.h> 50319815Sian#include <dev/mii/miivar.h> 51319815Sian#include <dev/mii/mii_fdt.h> 52319815Sian 53319815Sian/* 54319815Sian * Table to translate MII_CONTYPE_xxxx constants to/from devicetree strings. 55319815Sian * We explicitly associate the enum values with the strings in a table to avoid 56319815Sian * relying on this list being sorted in the same order as the enum in miivar.h, 57319815Sian * and to avoid problems if the enum gains new types that aren't in the FDT 58319815Sian * data. However, the "unknown" entry must be first because it is referenced 59319815Sian * using subscript 0 in mii_fdt_contype_to_name(). 60319815Sian */ 61319815Sianstatic struct contype_names { 62319815Sian mii_contype_t type; 63319815Sian const char *name; 64319815Sian} fdt_contype_names[] = { 65319815Sian {MII_CONTYPE_UNKNOWN, "unknown"}, 66319815Sian {MII_CONTYPE_MII, "mii"}, 67319815Sian {MII_CONTYPE_GMII, "gmii"}, 68319815Sian {MII_CONTYPE_SGMII, "sgmii"}, 69319815Sian {MII_CONTYPE_QSGMII, "qsgmii"}, 70319815Sian {MII_CONTYPE_TBI, "tbi"}, 71319815Sian {MII_CONTYPE_REVMII, "rev-mii"}, 72319815Sian {MII_CONTYPE_RMII, "rmii"}, 73319815Sian {MII_CONTYPE_RGMII, "rgmii"}, 74319815Sian {MII_CONTYPE_RGMII_ID, "rgmii-id"}, 75319815Sian {MII_CONTYPE_RGMII_RXID, "rgmii-rxid"}, 76319815Sian {MII_CONTYPE_RGMII_TXID, "rgmii-txid"}, 77319815Sian {MII_CONTYPE_RTBI, "rtbi"}, 78319815Sian {MII_CONTYPE_SMII, "smii"}, 79319815Sian {MII_CONTYPE_XGMII, "xgmii"}, 80319815Sian {MII_CONTYPE_TRGMII, "trgmii"}, 81319815Sian {MII_CONTYPE_2000BX, "2000base-x"}, 82319815Sian {MII_CONTYPE_2500BX, "2500base-x"}, 83319815Sian {MII_CONTYPE_RXAUI, "rxaui"}, 84319815Sian}; 85319815Sian 86319815Sianstatic phandle_t 87319815Sianmii_fdt_get_phynode(phandle_t macnode) 88319815Sian{ 89319815Sian static const char *props[] = { 90319815Sian "phy-handle", "phy", "phy-device" 91319815Sian }; 92319815Sian pcell_t xref; 93319815Sian u_int i; 94319815Sian 95319815Sian for (i = 0; i < nitems(props); ++i) { 96319815Sian if (OF_getencprop(macnode, props[i], &xref, sizeof(xref)) > 0) 97319815Sian return (OF_node_from_xref(xref)); 98319815Sian } 99319815Sian return (-1); 100319815Sian} 101319815Sian 102319815Sianmii_contype_t 103319815Sianmii_fdt_contype_from_name(const char *name) 104319815Sian{ 105319815Sian u_int i; 106319815Sian 107319815Sian for (i = 0; i < nitems(fdt_contype_names); ++i) { 108319815Sian if (strcmp(name, fdt_contype_names[i].name) == 0) 109319815Sian return (fdt_contype_names[i].type); 110319815Sian } 111319815Sian return (MII_CONTYPE_UNKNOWN); 112319815Sian} 113319815Sian 114319815Sianconst char * 115319815Sianmii_fdt_contype_to_name(mii_contype_t contype) 116319815Sian{ 117319815Sian u_int i; 118319815Sian 119319815Sian for (i = 0; i < nitems(fdt_contype_names); ++i) { 120319815Sian if (contype == fdt_contype_names[i].type) 121319815Sian return (fdt_contype_names[i].name); 122319815Sian } 123319815Sian return (fdt_contype_names[0].name); 124319815Sian} 125319815Sian 126319815Sianmii_contype_t 127319815Sianmii_fdt_get_contype(phandle_t macnode) 128319815Sian{ 129319815Sian char val[32]; 130319815Sian 131319815Sian if (OF_getprop(macnode, "phy-mode", val, sizeof(val)) <= 0 && 132319815Sian OF_getprop(macnode, "phy-connection-type", val, sizeof(val)) <= 0) { 133319815Sian return (MII_CONTYPE_UNKNOWN); 134319815Sian } 135319815Sian return (mii_fdt_contype_from_name(val)); 136319815Sian} 137319815Sian 138319815Sianvoid 139319815Sianmii_fdt_free_config(struct mii_fdt_phy_config *cfg) 140319815Sian{ 141319815Sian 142319815Sian free(cfg, M_OFWPROP); 143319815Sian} 144319815Sian 145319815Sianmii_fdt_phy_config_t * 146319815Sianmii_fdt_get_config(device_t phydev) 147319815Sian{ 148319815Sian mii_fdt_phy_config_t *cfg; 149319815Sian device_t miibus, macdev; 150319815Sian pcell_t val; 151319815Sian 152319815Sian miibus = device_get_parent(phydev); 153319815Sian macdev = device_get_parent(miibus); 154319815Sian 155319815Sian cfg = malloc(sizeof(*cfg), M_OFWPROP, M_ZERO | M_WAITOK); 156319815Sian 157319815Sian /* 158319815Sian * If we can't find our parent MAC's node, there's nothing more we can 159319815Sian * fill in; cfg is already full of zero/default values, return it. 160319815Sian */ 161319815Sian if ((cfg->macnode = ofw_bus_get_node(macdev)) == -1) 162319815Sian return (cfg); 163319815Sian 164319815Sian cfg->con_type = mii_fdt_get_contype(cfg->macnode); 165319815Sian 166319815Sian /* 167319815Sian * If we can't find our own PHY node, there's nothing more we can fill 168319815Sian * in, just return what we've got. 169319815Sian */ 170319815Sian if ((cfg->phynode = mii_fdt_get_phynode(cfg->macnode)) == -1) 171319815Sian return (cfg); 172319815Sian 173319815Sian if (OF_getencprop(cfg->phynode, "max-speed", &val, sizeof(val)) > 0) 174319815Sian cfg->max_speed = val; 175319815Sian 176319815Sian if (ofw_bus_node_is_compatible(cfg->phynode, 177319815Sian "ethernet-phy-ieee802.3-c45")) 178319815Sian cfg->flags |= MIIF_FDT_COMPAT_CLAUSE45; 179319815Sian 180319815Sian if (OF_hasprop(cfg->phynode, "broken-turn-around")) 181319815Sian cfg->flags |= MIIF_FDT_BROKEN_TURNAROUND; 182319815Sian if (OF_hasprop(cfg->phynode, "enet-phy-lane-swap")) 183319815Sian cfg->flags |= MIIF_FDT_LANE_SWAP; 184319815Sian if (OF_hasprop(cfg->phynode, "enet-phy-lane-no-swap")) 185319815Sian cfg->flags |= MIIF_FDT_NO_LANE_SWAP; 186319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-100tx")) 187319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_100TX; 188319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-1000t")) 189319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_1000T; 190319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-10gt")) 191319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_10GT; 192319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-1000kx")) 193319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_1000KX; 194319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-10gkx4")) 195319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKX4; 196319815Sian if (OF_hasprop(cfg->phynode, "eee-broken-10gkr")) 197319815Sian cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKR; 198319815Sian 199319815Sian return (cfg); 200319815Sian} 201