1 /*- 2 * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> 3 * All rights reserved. 4 * 5 * Development sponsored by Microsemi, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * Utility functions for PHY drivers on systems configured using FDT data. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/socket.h> 39 #include <sys/bus.h> 40 #include <sys/malloc.h> 41 42 #include <net/if.h> 43 #include <net/if_media.h> 44 45 #include <dev/ofw/openfirm.h> 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include <dev/mii/mii.h> 50 #include <dev/mii/miivar.h> 51 #include <dev/mii/mii_fdt.h> 52 53 /* 54 * Table to translate MII_CONTYPE_xxxx constants to/from devicetree strings. 55 * We explicitly associate the enum values with the strings in a table to avoid 56 * relying on this list being sorted in the same order as the enum in miivar.h, 57 * and to avoid problems if the enum gains new types that aren't in the FDT 58 * data. However, the "unknown" entry must be first because it is referenced 59 * using subscript 0 in mii_fdt_contype_to_name(). 60 */ 61 static struct contype_names { 62 mii_contype_t type; 63 const char *name; 64 } fdt_contype_names[] = { 65 {MII_CONTYPE_UNKNOWN, "unknown"}, 66 {MII_CONTYPE_MII, "mii"}, 67 {MII_CONTYPE_GMII, "gmii"}, 68 {MII_CONTYPE_SGMII, "sgmii"}, 69 {MII_CONTYPE_QSGMII, "qsgmii"}, 70 {MII_CONTYPE_TBI, "tbi"}, 71 {MII_CONTYPE_REVMII, "rev-mii"}, 72 {MII_CONTYPE_RMII, "rmii"}, 73 {MII_CONTYPE_RGMII, "rgmii"}, 74 {MII_CONTYPE_RGMII_ID, "rgmii-id"}, 75 {MII_CONTYPE_RGMII_RXID, "rgmii-rxid"}, 76 {MII_CONTYPE_RGMII_TXID, "rgmii-txid"}, 77 {MII_CONTYPE_RTBI, "rtbi"}, 78 {MII_CONTYPE_SMII, "smii"}, 79 {MII_CONTYPE_XGMII, "xgmii"}, 80 {MII_CONTYPE_TRGMII, "trgmii"}, 81 {MII_CONTYPE_2000BX, "2000base-x"}, 82 {MII_CONTYPE_2500BX, "2500base-x"}, 83 {MII_CONTYPE_RXAUI, "rxaui"}, 84 }; 85 86 static phandle_t 87 mii_fdt_get_phynode(phandle_t macnode) 88 { 89 static const char *props[] = { 90 "phy-handle", "phy", "phy-device" 91 }; 92 pcell_t xref; 93 u_int i; 94 95 for (i = 0; i < nitems(props); ++i) { 96 if (OF_getencprop(macnode, props[i], &xref, sizeof(xref)) > 0) 97 return (OF_node_from_xref(xref)); 98 } 99 return (-1); 100 } 101 102 mii_contype_t 103 mii_fdt_contype_from_name(const char *name) 104 { 105 u_int i; 106 107 for (i = 0; i < nitems(fdt_contype_names); ++i) { 108 if (strcmp(name, fdt_contype_names[i].name) == 0) 109 return (fdt_contype_names[i].type); 110 } 111 return (MII_CONTYPE_UNKNOWN); 112 } 113 114 const char * 115 mii_fdt_contype_to_name(mii_contype_t contype) 116 { 117 u_int i; 118 119 for (i = 0; i < nitems(fdt_contype_names); ++i) { 120 if (contype == fdt_contype_names[i].type) 121 return (fdt_contype_names[i].name); 122 } 123 return (fdt_contype_names[0].name); 124 } 125 126 mii_contype_t 127 mii_fdt_get_contype(phandle_t macnode) 128 { 129 char val[32]; 130 131 if (OF_getprop(macnode, "phy-mode", val, sizeof(val)) <= 0 && 132 OF_getprop(macnode, "phy-connection-type", val, sizeof(val)) <= 0) { 133 return (MII_CONTYPE_UNKNOWN); 134 } 135 return (mii_fdt_contype_from_name(val)); 136 } 137 138 void 139 mii_fdt_free_config(struct mii_fdt_phy_config *cfg) 140 { 141 142 free(cfg, M_OFWPROP); 143 } 144 145 mii_fdt_phy_config_t * 146 mii_fdt_get_config(device_t phydev) 147 { 148 mii_fdt_phy_config_t *cfg; 149 device_t miibus, macdev; 150 pcell_t val; 151 152 miibus = device_get_parent(phydev); 153 macdev = device_get_parent(miibus); 154 155 cfg = malloc(sizeof(*cfg), M_OFWPROP, M_ZERO | M_WAITOK); 156 157 /* 158 * If we can't find our parent MAC's node, there's nothing more we can 159 * fill in; cfg is already full of zero/default values, return it. 160 */ 161 if ((cfg->macnode = ofw_bus_get_node(macdev)) == -1) 162 return (cfg); 163 164 cfg->con_type = mii_fdt_get_contype(cfg->macnode); 165 166 /* 167 * If we can't find our own PHY node, there's nothing more we can fill 168 * in, just return what we've got. 169 */ 170 if ((cfg->phynode = mii_fdt_get_phynode(cfg->macnode)) == -1) 171 return (cfg); 172 173 if (OF_getencprop(cfg->phynode, "max-speed", &val, sizeof(val)) > 0) 174 cfg->max_speed = val; 175 176 if (ofw_bus_node_is_compatible(cfg->phynode, 177 "ethernet-phy-ieee802.3-c45")) 178 cfg->flags |= MIIF_FDT_COMPAT_CLAUSE45; 179 180 if (OF_hasprop(cfg->phynode, "broken-turn-around")) 181 cfg->flags |= MIIF_FDT_BROKEN_TURNAROUND; 182 if (OF_hasprop(cfg->phynode, "enet-phy-lane-swap")) 183 cfg->flags |= MIIF_FDT_LANE_SWAP; 184 if (OF_hasprop(cfg->phynode, "enet-phy-lane-no-swap")) 185 cfg->flags |= MIIF_FDT_NO_LANE_SWAP; 186 if (OF_hasprop(cfg->phynode, "eee-broken-100tx")) 187 cfg->flags |= MIIF_FDT_EEE_BROKEN_100TX; 188 if (OF_hasprop(cfg->phynode, "eee-broken-1000t")) 189 cfg->flags |= MIIF_FDT_EEE_BROKEN_1000T; 190 if (OF_hasprop(cfg->phynode, "eee-broken-10gt")) 191 cfg->flags |= MIIF_FDT_EEE_BROKEN_10GT; 192 if (OF_hasprop(cfg->phynode, "eee-broken-1000kx")) 193 cfg->flags |= MIIF_FDT_EEE_BROKEN_1000KX; 194 if (OF_hasprop(cfg->phynode, "eee-broken-10gkx4")) 195 cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKX4; 196 if (OF_hasprop(cfg->phynode, "eee-broken-10gkr")) 197 cfg->flags |= MIIF_FDT_EEE_BROKEN_10GKR; 198 199 return (cfg); 200 } 201