2 * Copyright (c) 2015 The FreeBSD Foundation
5 * This software was developed by Semihalf under
6 * the sponsorship of the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bitset.h>
36 #include <sys/bitstring.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <sys/pciio.h>
46 #include <sys/socket.h>
47 #include <sys/cpuset.h>
49 #include <sys/mutex.h>
51 #include <net/ethernet.h>
53 #include <net/if_media.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/mii/miivar.h>
58 #include "thunder_bgx.h"
59 #include "thunder_bgx_var.h"
61 #define CONN_TYPE_MAXLEN 16
62 #define CONN_TYPE_OFFSET 2
64 int bgx_fdt_init_phy(struct bgx *);
67 bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
69 uint8_t addr[ETHER_ADDR_LEN];
71 if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) {
72 /* Missing MAC address should be marked by clearing it */
73 memset(hwaddr, 0, ETHER_ADDR_LEN);
75 memcpy(hwaddr, addr, ETHER_ADDR_LEN);
79 bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, size_t size)
82 size -= CONN_TYPE_OFFSET;
84 switch (bgx->qlm_mode) {
86 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "sgmii", size) == 0)
89 case QLM_MODE_XAUI_1X4:
90 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xaui", size) == 0)
92 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "dxaui", size) == 0)
95 case QLM_MODE_RXAUI_2X2:
96 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "raui", size) == 0)
99 case QLM_MODE_XFI_4X1:
100 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi", size) == 0)
103 case QLM_MODE_XLAUI_1X4:
104 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui", size) == 0)
107 case QLM_MODE_10G_KR_4X1:
108 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi-10g-kr", size) == 0)
111 case QLM_MODE_40G_KR4_1X4:
112 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui-40g-kr", size) == 0)
123 bgx_fdt_init_phy(struct bgx *bgx)
125 phandle_t node, child;
129 char qlm_mode[CONN_TYPE_MAXLEN];
135 /* Get BGX node from DT */
136 snprintf(bgx_sel, 6, "/bgx%d", bgx->bgx_id);
137 node = OF_finddevice(bgx_sel);
138 if (node == 0 || node == -1) {
139 device_printf(bgx->dev,
140 "Could not find %s node in FDT\n", bgx_sel);
144 for (child = OF_child(node); child > 0; child = OF_peer(child)) {
145 if (OF_getprop(child, "qlm-mode", qlm_mode,
146 sizeof(qlm_mode)) <= 0) {
147 /* Missing qlm-mode, skipping */
151 if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, sizeof(qlm_mode))) {
153 * Connection type not match with BGX mode.
158 if (OF_getencprop(child, "phy-handle", &phy,
161 device_printf(bgx->dev,
162 "No phy-handle in PHY node. Skipping...\n");
167 /* Acquire PHY address */
168 phy = OF_node_from_xref(phy);
169 if (OF_getencprop(phy, "reg", &bgx->lmac[lmac].phyaddr,
170 sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
172 device_printf(bgx->dev,
173 "Could not retrieve PHY address\n");
175 bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
179 * Get PHY interface (MDIO bus) device.
180 * Driver must be already attached.
182 mdio = OF_parent(phy);
183 bgx->lmac[lmac].phy_if_dev =
184 OF_device_from_xref(OF_xref_from_node(mdio));
185 if (bgx->lmac[lmac].phy_if_dev == NULL) {
187 device_printf(bgx->dev,
188 "Could not find interface to PHY\n");
193 /* Get mac address from FDT */
194 bgx_fdt_get_macaddr(phy, bgx->lmac[lmac].mac);
196 bgx->lmac[lmac].lmacid = lmac;
198 if (lmac == MAX_LMAC_PER_BGX)
202 device_printf(bgx->dev, "Could not find matching PHY\n");