1f9f848faSopenharmony_ci/*-
2f9f848faSopenharmony_ci * SPDX-License-Identifier: BSD-4-Clause
3f9f848faSopenharmony_ci *
4f9f848faSopenharmony_ci * Copyright (c) 1997, 1998, 1999, 2000-2003
5f9f848faSopenharmony_ci *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6f9f848faSopenharmony_ci *
7f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without
8f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions
9f9f848faSopenharmony_ci * are met:
10f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
11f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
12f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
13f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
14f9f848faSopenharmony_ci *    documentation and/or other materials provided with the distribution.
15f9f848faSopenharmony_ci * 3. All advertising materials mentioning features or use of this software
16f9f848faSopenharmony_ci *    must display the following acknowledgement:
17f9f848faSopenharmony_ci *	This product includes software developed by Bill Paul.
18f9f848faSopenharmony_ci * 4. Neither the name of the author nor the names of any co-contributors
19f9f848faSopenharmony_ci *    may be used to endorse or promote products derived from this software
20f9f848faSopenharmony_ci *    without specific prior written permission.
21f9f848faSopenharmony_ci *
22f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23f9f848faSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f9f848faSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f9f848faSopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26f9f848faSopenharmony_ci * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f9f848faSopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f9f848faSopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f9f848faSopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f9f848faSopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f9f848faSopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f9f848faSopenharmony_ci * THE POSSIBILITY OF SUCH DAMAGE.
33f9f848faSopenharmony_ci */
34f9f848faSopenharmony_ci
35f9f848faSopenharmony_ci#include <sys/cdefs.h>
36f9f848faSopenharmony_ci/*
37f9f848faSopenharmony_ci * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
38f9f848faSopenharmony_ci * Used in the LinkSys USB200M and various other adapters.
39f9f848faSopenharmony_ci *
40f9f848faSopenharmony_ci * Manuals available from:
41f9f848faSopenharmony_ci * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
42f9f848faSopenharmony_ci * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
43f9f848faSopenharmony_ci * controller) to find the definitions for the RX control register.
44f9f848faSopenharmony_ci * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
45f9f848faSopenharmony_ci *
46f9f848faSopenharmony_ci * Written by Bill Paul <wpaul@windriver.com>
47f9f848faSopenharmony_ci * Senior Engineer
48f9f848faSopenharmony_ci * Wind River Systems
49f9f848faSopenharmony_ci */
50f9f848faSopenharmony_ci
51f9f848faSopenharmony_ci/*
52f9f848faSopenharmony_ci * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
53f9f848faSopenharmony_ci * It uses an external PHY (reference designs use a RealTek chip),
54f9f848faSopenharmony_ci * and has a 64-bit multicast hash filter. There is some information
55f9f848faSopenharmony_ci * missing from the manual which one needs to know in order to make
56f9f848faSopenharmony_ci * the chip function:
57f9f848faSopenharmony_ci *
58f9f848faSopenharmony_ci * - You must set bit 7 in the RX control register, otherwise the
59f9f848faSopenharmony_ci *   chip won't receive any packets.
60f9f848faSopenharmony_ci * - You must initialize all 3 IPG registers, or you won't be able
61f9f848faSopenharmony_ci *   to send any packets.
62f9f848faSopenharmony_ci *
63f9f848faSopenharmony_ci * Note that this device appears to only support loading the station
64f9f848faSopenharmony_ci * address via autload from the EEPROM (i.e. there's no way to manually
65f9f848faSopenharmony_ci * set it).
66f9f848faSopenharmony_ci *
67f9f848faSopenharmony_ci * (Adam Weinberger wanted me to name this driver if_gir.c.)
68f9f848faSopenharmony_ci */
69f9f848faSopenharmony_ci
70f9f848faSopenharmony_ci/*
71f9f848faSopenharmony_ci * Ax88178 and Ax88772 support backported from the OpenBSD driver.
72f9f848faSopenharmony_ci * 2007/02/12, J.R. Oldroyd, fbsd@opal.com
73f9f848faSopenharmony_ci *
74f9f848faSopenharmony_ci * Manual here:
75f9f848faSopenharmony_ci * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
76f9f848faSopenharmony_ci * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
77f9f848faSopenharmony_ci */
78f9f848faSopenharmony_ci
79f9f848faSopenharmony_ci#include <lwip/netif.h>
80f9f848faSopenharmony_ci#include <lwip/dhcp.h>
81f9f848faSopenharmony_ci#include <lwip/netifapi.h>
82f9f848faSopenharmony_ci#include <lwip/inet.h>
83f9f848faSopenharmony_ci
84f9f848faSopenharmony_ci#include "implementation/global_implementation.h"
85f9f848faSopenharmony_ci#include "usb_ethernet.h"
86f9f848faSopenharmony_ci#include "if_axereg.h"
87f9f848faSopenharmony_ci#include "mii.h"
88f9f848faSopenharmony_ci
89f9f848faSopenharmony_ci/*
90f9f848faSopenharmony_ci * AXE_178_MAX_FRAME_BURST
91f9f848faSopenharmony_ci * max frame burst size for Ax88178 and Ax88772
92f9f848faSopenharmony_ci * 0	2048 bytes
93f9f848faSopenharmony_ci * 1	4096 bytes
94f9f848faSopenharmony_ci * 2	8192 bytes
95f9f848faSopenharmony_ci * 3	16384 bytes
96f9f848faSopenharmony_ci * use the largest your system can handle without USB stalling.
97f9f848faSopenharmony_ci *
98f9f848faSopenharmony_ci * NB: 88772 parts appear to generate lots of input errors with
99f9f848faSopenharmony_ci * a 2K rx buffer and 8K is only slightly faster than 4K on an
100f9f848faSopenharmony_ci * EHCI port on a T42 so change at your own risk.
101f9f848faSopenharmony_ci */
102f9f848faSopenharmony_ci
103f9f848faSopenharmony_ci#define	AXE_178_MAX_FRAME_BURST	1
104f9f848faSopenharmony_ci#define	AXE_CSUM_FEATURES		(CSUM_IP | CSUM_TCP | CSUM_UDP)
105f9f848faSopenharmony_ci
106f9f848faSopenharmony_ci#undef USB_DEBUG_VAR
107f9f848faSopenharmony_ci#define	USB_DEBUG_VAR	axe_debug
108f9f848faSopenharmony_ci#ifdef LOSCFG_USB_DEBUG
109f9f848faSopenharmony_cistatic int axe_debug = 0;
110f9f848faSopenharmony_civoid
111f9f848faSopenharmony_ciusb_axe_debug_func(int level)
112f9f848faSopenharmony_ci{
113f9f848faSopenharmony_ci	axe_debug = level;
114f9f848faSopenharmony_ci	PRINTK("The level of usb axe debug is %d\n", level);
115f9f848faSopenharmony_ci}
116f9f848faSopenharmony_ciDEBUG_MODULE(axe, usb_axe_debug_func);
117f9f848faSopenharmony_ci#endif
118f9f848faSopenharmony_ci
119f9f848faSopenharmony_ci#define	IFF_DRV_OACTIVE  IFF_MASTER
120f9f848faSopenharmony_ci#define	IFF_SIMPLEX		IFF_SLAVE
121f9f848faSopenharmony_ci
122f9f848faSopenharmony_ci/*
123f9f848faSopenharmony_ci * Various supported device vendors/products.
124f9f848faSopenharmony_ci */
125f9f848faSopenharmony_cistatic const STRUCT_USB_HOST_ID axe_devs[] = {
126f9f848faSopenharmony_ci	{ USB_VPI(0x0B95, 0x772B, AXE_FLAG_772B) },
127f9f848faSopenharmony_ci	{ USB_VPI(0x0B95, 0x772A, AXE_FLAG_772A) },
128f9f848faSopenharmony_ci};
129f9f848faSopenharmony_ci
130f9f848faSopenharmony_cistatic device_probe_t axe_probe;
131f9f848faSopenharmony_cistatic device_attach_t axe_attach;
132f9f848faSopenharmony_cistatic device_detach_t axe_detach;
133f9f848faSopenharmony_ci
134f9f848faSopenharmony_cistatic usb_callback_t axe_bulk_read_callback;
135f9f848faSopenharmony_cistatic usb_callback_t axe_bulk_write_callback;
136f9f848faSopenharmony_ci
137f9f848faSopenharmony_cistatic int axe_miibus_writereg(struct axe_softc *sc, int reg, int val);
138f9f848faSopenharmony_cistatic uint16_t axe_miibus_readreg(struct axe_softc *sc, int reg);
139f9f848faSopenharmony_ci
140f9f848faSopenharmony_cistatic uether_fn_t axe_attach_post;
141f9f848faSopenharmony_cistatic uether_fn_t axe_init;
142f9f848faSopenharmony_cistatic uether_fn_t axe_stop;
143f9f848faSopenharmony_cistatic uether_fn_t axe_start;
144f9f848faSopenharmony_cistatic uether_fn_t axe_setmulti;
145f9f848faSopenharmony_cistatic uether_fn_t axe_setpromisc;
146f9f848faSopenharmony_cistatic uether_fn_t axe_tick;
147f9f848faSopenharmony_ci
148f9f848faSopenharmony_cistatic void axe_cmd(struct axe_softc *, int, int, int, void *);
149f9f848faSopenharmony_cistatic void axe_ax88178_init(struct axe_softc *);
150f9f848faSopenharmony_cistatic void axe_ax88772_init(struct axe_softc *);
151f9f848faSopenharmony_cistatic void axe_ax88772_phywake(struct axe_softc *);
152f9f848faSopenharmony_cistatic void axe_ax88772b_init(struct axe_softc *);
153f9f848faSopenharmony_cistatic int axe_get_phyno(struct axe_softc *, int);
154f9f848faSopenharmony_cistatic int axe_rx_frame(struct usb_ether *, struct usb_page_cache *, int);
155f9f848faSopenharmony_cistatic int axe_rxeof(struct usb_ether *, struct usb_page_cache *,
156f9f848faSopenharmony_ci				    unsigned int offset, unsigned int, struct axe_csum_hdr *);
157f9f848faSopenharmony_cistatic void axe_csum_cfg(struct usb_ether *);
158f9f848faSopenharmony_ci
159f9f848faSopenharmony_cistatic const struct usb_config axe_config[AXE_N_TRANSFER] = {
160f9f848faSopenharmony_ci	{ /* [AXE_BULK_DT_WR] = */
161f9f848faSopenharmony_ci		.type = UE_BULK,
162f9f848faSopenharmony_ci		.endpoint = UE_ADDR_ANY,
163f9f848faSopenharmony_ci		.direction = UE_DIR_OUT,
164f9f848faSopenharmony_ci		.frames = USB_AXE_MAX_FRAMES,
165f9f848faSopenharmony_ci		.bufsize = USB_AXE_MAX_FRAMES * MCLBYTES,
166f9f848faSopenharmony_ci		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
167f9f848faSopenharmony_ci		.callback = axe_bulk_write_callback,
168f9f848faSopenharmony_ci		.timeout = 10000,	/* 10 seconds */
169f9f848faSopenharmony_ci	},
170f9f848faSopenharmony_ci	{ /* [AXE_BULK_DT_RD] = */
171f9f848faSopenharmony_ci		.type = UE_BULK,
172f9f848faSopenharmony_ci		.endpoint = UE_ADDR_ANY,
173f9f848faSopenharmony_ci		.direction = UE_DIR_IN,
174f9f848faSopenharmony_ci		.bufsize = 16 * MCLBYTES,	/* bytes */
175f9f848faSopenharmony_ci		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
176f9f848faSopenharmony_ci		.callback = axe_bulk_read_callback,
177f9f848faSopenharmony_ci		.timeout = 0,	/* no timeout */
178f9f848faSopenharmony_ci	},
179f9f848faSopenharmony_ci};
180f9f848faSopenharmony_ci
181f9f848faSopenharmony_cistatic const struct ax88772b_mfb ax88772b_mfb_table[] = {
182f9f848faSopenharmony_ci	{ 0x8000, 0x8001, 2048 },
183f9f848faSopenharmony_ci	{ 0x8100, 0x8147, 4096 },
184f9f848faSopenharmony_ci	{ 0x8200, 0x81EB, 6144 },
185f9f848faSopenharmony_ci	{ 0x8300, 0x83D7, 8192 },
186f9f848faSopenharmony_ci	{ 0x84C0, 0x861E, 16384 },
187f9f848faSopenharmony_ci	{ 0x8500, 0x8666, 20480 },
188f9f848faSopenharmony_ci	{ 0x8600, 0x87AE, 24576 },
189f9f848faSopenharmony_ci	{ 0x8700, 0x851E, 32768 }
190f9f848faSopenharmony_ci};
191f9f848faSopenharmony_ci
192f9f848faSopenharmony_cistatic device_method_t axe_methods[] = {
193f9f848faSopenharmony_ci	/* Device interface */
194f9f848faSopenharmony_ci	DEVMETHOD(device_probe, axe_probe),
195f9f848faSopenharmony_ci	DEVMETHOD(device_attach, axe_attach),
196f9f848faSopenharmony_ci	DEVMETHOD(device_detach, axe_detach),
197f9f848faSopenharmony_ci	DEVMETHOD_END
198f9f848faSopenharmony_ci};
199f9f848faSopenharmony_ci
200f9f848faSopenharmony_cistatic driver_t axe_driver = {
201f9f848faSopenharmony_ci	.name = "USB_AXE",
202f9f848faSopenharmony_ci	.methods = axe_methods,
203f9f848faSopenharmony_ci	.size = sizeof(struct axe_softc),
204f9f848faSopenharmony_ci};
205f9f848faSopenharmony_ci
206f9f848faSopenharmony_cistatic devclass_t axe_devclass;
207f9f848faSopenharmony_ciDRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, 0, 0);
208f9f848faSopenharmony_ci
209f9f848faSopenharmony_cistatic const struct usb_ether_methods axe_ue_methods = {
210f9f848faSopenharmony_ci	.ue_attach_post = axe_attach_post,
211f9f848faSopenharmony_ci	.ue_start = axe_start,
212f9f848faSopenharmony_ci	.ue_init = axe_init,
213f9f848faSopenharmony_ci	.ue_stop = axe_stop,
214f9f848faSopenharmony_ci	.ue_setmulti = axe_setmulti,
215f9f848faSopenharmony_ci	.ue_setpromisc = axe_setpromisc,
216f9f848faSopenharmony_ci	.ue_tick = axe_tick,
217f9f848faSopenharmony_ci};
218f9f848faSopenharmony_ci
219f9f848faSopenharmony_cistatic void
220f9f848faSopenharmony_ciaxe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
221f9f848faSopenharmony_ci{
222f9f848faSopenharmony_ci	struct usb_device_request req;
223f9f848faSopenharmony_ci	usb_error_t err;
224f9f848faSopenharmony_ci
225f9f848faSopenharmony_ci	AXE_LOCK_ASSERT(sc, MA_OWNED);
226f9f848faSopenharmony_ci
227f9f848faSopenharmony_ci	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
228f9f848faSopenharmony_ci	    UT_WRITE_VENDOR_DEVICE :
229f9f848faSopenharmony_ci	    UT_READ_VENDOR_DEVICE);
230f9f848faSopenharmony_ci	req.bRequest = AXE_CMD_CMD(cmd);
231f9f848faSopenharmony_ci	USETW(req.wValue, val);
232f9f848faSopenharmony_ci	USETW(req.wIndex, index);
233f9f848faSopenharmony_ci	USETW(req.wLength, AXE_CMD_LEN(cmd));
234f9f848faSopenharmony_ci
235f9f848faSopenharmony_ci	err = uether_do_request(&sc->sc_ue, &req, buf, 10000);
236f9f848faSopenharmony_ci	if (err != USB_ERR_NORMAL_COMPLETION) {
237f9f848faSopenharmony_ci		dprintf("Fatal Error in function [%s]! err:%d\n", __FUNCTION__, err);
238f9f848faSopenharmony_ci	}
239f9f848faSopenharmony_ci}
240f9f848faSopenharmony_ci
241f9f848faSopenharmony_cistatic uint16_t
242f9f848faSopenharmony_ciaxe_miibus_readreg(struct axe_softc *sc, int reg)
243f9f848faSopenharmony_ci{
244f9f848faSopenharmony_ci	uint16_t val;
245f9f848faSopenharmony_ci
246f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
247f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, 0x10, &val);
248f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
249f9f848faSopenharmony_ci
250f9f848faSopenharmony_ci	val = le16toh(val);
251f9f848faSopenharmony_ci	if (AXE_IS_772(sc) && reg == MII_BMSR) {
252f9f848faSopenharmony_ci		/*
253f9f848faSopenharmony_ci		 * BMSR of AX88772 indicates that it supports extended
254f9f848faSopenharmony_ci		 * capability but the extended status register is
255f9f848faSopenharmony_ci		 * revered for embedded ethernet PHY. So clear the
256f9f848faSopenharmony_ci		 * extended capability bit of BMSR.
257f9f848faSopenharmony_ci		 */
258f9f848faSopenharmony_ci		val &= ~BMSR_EXTCAP;
259f9f848faSopenharmony_ci	}
260f9f848faSopenharmony_ci	return (val);
261f9f848faSopenharmony_ci}
262f9f848faSopenharmony_ci
263f9f848faSopenharmony_cistatic int
264f9f848faSopenharmony_ciaxe_miibus_writereg(struct axe_softc *sc, int reg, int val)
265f9f848faSopenharmony_ci{
266f9f848faSopenharmony_ci	val = htole32(val);
267f9f848faSopenharmony_ci
268f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
269f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, 0x10, &val);
270f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
271f9f848faSopenharmony_ci	return (0);
272f9f848faSopenharmony_ci}
273f9f848faSopenharmony_ci
274f9f848faSopenharmony_cistatic int
275f9f848faSopenharmony_ciaxe_setmedium(struct axe_softc *sc)
276f9f848faSopenharmony_ci{
277f9f848faSopenharmony_ci	uint16_t val = AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
278f9f848faSopenharmony_ci	uint16_t bmcr;
279f9f848faSopenharmony_ci	int b100 = 0;
280f9f848faSopenharmony_ci	int bfull = 0;
281f9f848faSopenharmony_ci
282f9f848faSopenharmony_ci	bmcr = axe_miibus_readreg(sc, 0);
283f9f848faSopenharmony_ci
284f9f848faSopenharmony_ci	if (bmcr & 0x2000) { /* 100Mbps */
285f9f848faSopenharmony_ci		val |= AXE_178_MEDIA_100TX;
286f9f848faSopenharmony_ci		b100 = 1;
287f9f848faSopenharmony_ci	}
288f9f848faSopenharmony_ci
289f9f848faSopenharmony_ci	if (bmcr & 0x100) { /* full-duplex */
290f9f848faSopenharmony_ci		val |= AXE_MEDIA_FULL_DUPLEX | AXE_178_MEDIA_TXFLOW_CONTROL_EN | AXE_178_MEDIA_RXFLOW_CONTROL_EN;
291f9f848faSopenharmony_ci		bfull = 1;
292f9f848faSopenharmony_ci	}
293f9f848faSopenharmony_ci
294f9f848faSopenharmony_ci	if (b100)
295f9f848faSopenharmony_ci		PRINTK("- 100Mbps/");
296f9f848faSopenharmony_ci	else
297f9f848faSopenharmony_ci		PRINTK("- 10Mbps/");
298f9f848faSopenharmony_ci
299f9f848faSopenharmony_ci	if (bfull)
300f9f848faSopenharmony_ci		PRINTK("Full\n");
301f9f848faSopenharmony_ci	else
302f9f848faSopenharmony_ci		PRINTK("Half\n");
303f9f848faSopenharmony_ci
304f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
305f9f848faSopenharmony_ci	return (0);
306f9f848faSopenharmony_ci}
307f9f848faSopenharmony_ci
308f9f848faSopenharmony_cistatic void
309f9f848faSopenharmony_ciaxe_setmulti(struct usb_ether *ue)
310f9f848faSopenharmony_ci{
311f9f848faSopenharmony_ci}
312f9f848faSopenharmony_ci
313f9f848faSopenharmony_cistatic int
314f9f848faSopenharmony_ciaxe_get_phyno(struct axe_softc *sc, int sel)
315f9f848faSopenharmony_ci{
316f9f848faSopenharmony_ci	int phyno;
317f9f848faSopenharmony_ci
318f9f848faSopenharmony_ci	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
319f9f848faSopenharmony_ci	case PHY_TYPE_100_HOME:
320f9f848faSopenharmony_ci	case PHY_TYPE_GIG:
321f9f848faSopenharmony_ci		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
322f9f848faSopenharmony_ci		break;
323f9f848faSopenharmony_ci	case PHY_TYPE_SPECIAL:
324f9f848faSopenharmony_ci		/* FALLTHROUGH */
325f9f848faSopenharmony_ci	case PHY_TYPE_RSVD:
326f9f848faSopenharmony_ci		/* FALLTHROUGH */
327f9f848faSopenharmony_ci	case PHY_TYPE_NON_SUP:
328f9f848faSopenharmony_ci		/* FALLTHROUGH */
329f9f848faSopenharmony_ci	default:
330f9f848faSopenharmony_ci		phyno = -1;
331f9f848faSopenharmony_ci		break;
332f9f848faSopenharmony_ci	}
333f9f848faSopenharmony_ci
334f9f848faSopenharmony_ci	return (phyno);
335f9f848faSopenharmony_ci}
336f9f848faSopenharmony_ci
337f9f848faSopenharmony_cistatic void
338f9f848faSopenharmony_ciaxe_uether_pause(struct usb_ether * usbe, unsigned int t_ick)
339f9f848faSopenharmony_ci{
340f9f848faSopenharmony_ci	(void) uether_pause(usbe, t_ick);
341f9f848faSopenharmony_ci}
342f9f848faSopenharmony_ci
343f9f848faSopenharmony_ci#define	AXE_GPIO_WRITE(x, y)	do { \
344f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL); \
345f9f848faSopenharmony_ci	axe_uether_pause(ue, (y)); \
346f9f848faSopenharmony_ci} while (0)
347f9f848faSopenharmony_ci
348f9f848faSopenharmony_cistatic void
349f9f848faSopenharmony_ciaxe_ax88178_init(struct axe_softc *sc)
350f9f848faSopenharmony_ci{
351f9f848faSopenharmony_ci	struct usb_ether *ue;
352f9f848faSopenharmony_ci	int gpio0, ledmode, phymode;
353f9f848faSopenharmony_ci	uint16_t eeprom, val;
354f9f848faSopenharmony_ci
355f9f848faSopenharmony_ci	ue = &sc->sc_ue;
356f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
357f9f848faSopenharmony_ci	/* XXX magic */
358f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
359f9f848faSopenharmony_ci	eeprom = le16toh(eeprom);
360f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
361f9f848faSopenharmony_ci
362f9f848faSopenharmony_ci	/* if EEPROM is invalid we have to use to GPIO0 */
363f9f848faSopenharmony_ci	if (eeprom == 0xffff) {
364f9f848faSopenharmony_ci		phymode = AXE_PHY_MODE_MARVELL;
365f9f848faSopenharmony_ci		gpio0 = 1;
366f9f848faSopenharmony_ci		ledmode = 0;
367f9f848faSopenharmony_ci	} else {
368f9f848faSopenharmony_ci		phymode = eeprom & 0x7f;
369f9f848faSopenharmony_ci		gpio0 = (eeprom & 0x80) ? 0 : 1;
370f9f848faSopenharmony_ci		ledmode = eeprom >> 8;
371f9f848faSopenharmony_ci	}
372f9f848faSopenharmony_ci
373f9f848faSopenharmony_ci	/* Program GPIOs depending on PHY hardware. */
374f9f848faSopenharmony_ci	switch (phymode) {
375f9f848faSopenharmony_ci	case AXE_PHY_MODE_MARVELL:
376f9f848faSopenharmony_ci		if (gpio0 == 1) {
377f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
378f9f848faSopenharmony_ci			    hz / 32);
379f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
380f9f848faSopenharmony_ci			    hz / 32);
381f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
382f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
383f9f848faSopenharmony_ci			    hz / 32);
384f9f848faSopenharmony_ci		} else {
385f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
386f9f848faSopenharmony_ci			    AXE_GPIO1_EN, hz / 3);
387f9f848faSopenharmony_ci			if (ledmode == 1) {
388f9f848faSopenharmony_ci				AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
389f9f848faSopenharmony_ci				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
390f9f848faSopenharmony_ci				    hz / 3);
391f9f848faSopenharmony_ci			} else {
392f9f848faSopenharmony_ci				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
393f9f848faSopenharmony_ci				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
394f9f848faSopenharmony_ci				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
395f9f848faSopenharmony_ci				    AXE_GPIO2_EN, hz / 4);
396f9f848faSopenharmony_ci				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
397f9f848faSopenharmony_ci				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
398f9f848faSopenharmony_ci			}
399f9f848faSopenharmony_ci		}
400f9f848faSopenharmony_ci		break;
401f9f848faSopenharmony_ci	case AXE_PHY_MODE_CICADA:
402f9f848faSopenharmony_ci	case AXE_PHY_MODE_CICADA_V2:
403f9f848faSopenharmony_ci	case AXE_PHY_MODE_CICADA_V2_ASIX:
404f9f848faSopenharmony_ci		if (gpio0 == 1)
405f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
406f9f848faSopenharmony_ci			    AXE_GPIO0_EN, hz / 32);
407f9f848faSopenharmony_ci		else
408f9f848faSopenharmony_ci			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
409f9f848faSopenharmony_ci			    AXE_GPIO1_EN, hz / 32);
410f9f848faSopenharmony_ci		break;
411f9f848faSopenharmony_ci	case AXE_PHY_MODE_AGERE:
412f9f848faSopenharmony_ci		AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
413f9f848faSopenharmony_ci		    AXE_GPIO1_EN, hz / 32);
414f9f848faSopenharmony_ci		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
415f9f848faSopenharmony_ci		    AXE_GPIO2_EN, hz / 32);
416f9f848faSopenharmony_ci		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
417f9f848faSopenharmony_ci		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
418f9f848faSopenharmony_ci		    AXE_GPIO2_EN, hz / 32);
419f9f848faSopenharmony_ci		break;
420f9f848faSopenharmony_ci	case AXE_PHY_MODE_REALTEK_8211CL:
421f9f848faSopenharmony_ci	case AXE_PHY_MODE_REALTEK_8211BN:
422f9f848faSopenharmony_ci	case AXE_PHY_MODE_REALTEK_8251CL:
423f9f848faSopenharmony_ci		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
424f9f848faSopenharmony_ci		    AXE_GPIO1 | AXE_GPIO1_EN;
425f9f848faSopenharmony_ci		AXE_GPIO_WRITE(val, hz / 32);
426f9f848faSopenharmony_ci		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
427f9f848faSopenharmony_ci		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
428f9f848faSopenharmony_ci		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
429f9f848faSopenharmony_ci		if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
430f9f848faSopenharmony_ci			(void) axe_miibus_writereg(sc, 0x1F, 0x0005);
431f9f848faSopenharmony_ci			(void) axe_miibus_writereg(sc, 0x0C, 0x0000);
432f9f848faSopenharmony_ci			val = axe_miibus_readreg(sc, 0x0001);
433f9f848faSopenharmony_ci			(void) axe_miibus_writereg(sc, 0x01, val | 0x0080);
434f9f848faSopenharmony_ci			(void) axe_miibus_writereg(sc, 0x1F, 0x0000);
435f9f848faSopenharmony_ci		}
436f9f848faSopenharmony_ci		break;
437f9f848faSopenharmony_ci	default:
438f9f848faSopenharmony_ci		/* Unknown PHY model or no need to program GPIOs. */
439f9f848faSopenharmony_ci		break;
440f9f848faSopenharmony_ci	}
441f9f848faSopenharmony_ci
442f9f848faSopenharmony_ci	/* soft reset */
443f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
444f9f848faSopenharmony_ci	axe_uether_pause(ue, hz / 4);
445f9f848faSopenharmony_ci
446f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
447f9f848faSopenharmony_ci		AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
448f9f848faSopenharmony_ci	axe_uether_pause(ue, hz / 4);
449f9f848faSopenharmony_ci	/* Enable MII/GMII/RGMII interface to work with external PHY. */
450f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
451f9f848faSopenharmony_ci	axe_uether_pause(ue, hz / 4);
452f9f848faSopenharmony_ci
453f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
454f9f848faSopenharmony_ci}
455f9f848faSopenharmony_ci
456f9f848faSopenharmony_cistatic void
457f9f848faSopenharmony_ciaxe_ax88772_init(struct axe_softc *sc)
458f9f848faSopenharmony_ci{
459f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
460f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 16);
461f9f848faSopenharmony_ci
462f9f848faSopenharmony_ci	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
463f9f848faSopenharmony_ci		/* ask for the embedded PHY */
464f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
465f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 64);
466f9f848faSopenharmony_ci
467f9f848faSopenharmony_ci		/* power down and reset state, pin reset state */
468f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
469f9f848faSopenharmony_ci		    AXE_SW_RESET_CLEAR, NULL);
470f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 16);
471f9f848faSopenharmony_ci
472f9f848faSopenharmony_ci		/* power down/reset state, pin operating state */
473f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
474f9f848faSopenharmony_ci		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
475f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 4);
476f9f848faSopenharmony_ci
477f9f848faSopenharmony_ci		/* power up, reset */
478f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
479f9f848faSopenharmony_ci
480f9f848faSopenharmony_ci		/* power up, operating */
481f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
482f9f848faSopenharmony_ci		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
483f9f848faSopenharmony_ci	} else {
484f9f848faSopenharmony_ci		/* ask for external PHY */
485f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
486f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 64);
487f9f848faSopenharmony_ci
488f9f848faSopenharmony_ci		/* power down internal PHY */
489f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
490f9f848faSopenharmony_ci		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
491f9f848faSopenharmony_ci	}
492f9f848faSopenharmony_ci
493f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 4);
494f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
495f9f848faSopenharmony_ci}
496f9f848faSopenharmony_ci
497f9f848faSopenharmony_cistatic void
498f9f848faSopenharmony_ciaxe_ax88772_phywake(struct axe_softc *sc)
499f9f848faSopenharmony_ci{
500f9f848faSopenharmony_ci	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
501f9f848faSopenharmony_ci		/* Manually select internal(embedded) PHY - MAC mode. */
502f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
503f9f848faSopenharmony_ci		    AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII,
504f9f848faSopenharmony_ci		    NULL);
505f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 32);
506f9f848faSopenharmony_ci	} else {
507f9f848faSopenharmony_ci		/*
508f9f848faSopenharmony_ci		 * Manually select external PHY - MAC mode.
509f9f848faSopenharmony_ci		 * Reverse MII/RMII is for AX88772A PHY mode.
510f9f848faSopenharmony_ci		 */
511f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
512f9f848faSopenharmony_ci		    AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
513f9f848faSopenharmony_ci		axe_uether_pause(&sc->sc_ue, hz / 32);
514f9f848faSopenharmony_ci	}
515f9f848faSopenharmony_ci	/* Take PHY out of power down. */
516f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
517f9f848faSopenharmony_ci	    AXE_SW_RESET_IPRL, NULL);
518f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 4);
519f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
520f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz);
521f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
522f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 32);
523f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
524f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 32);
525f9f848faSopenharmony_ci}
526f9f848faSopenharmony_ci
527f9f848faSopenharmony_cistatic void
528f9f848faSopenharmony_ciaxe_ax88772b_init(struct axe_softc *sc)
529f9f848faSopenharmony_ci{
530f9f848faSopenharmony_ci	struct usb_ether *ue = &sc->sc_ue;
531f9f848faSopenharmony_ci	uint16_t eeprom;
532f9f848faSopenharmony_ci	uint8_t *eaddr;
533f9f848faSopenharmony_ci	uint8_t *tmp;
534f9f848faSopenharmony_ci	uint8_t i;
535f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
536f9f848faSopenharmony_ci
537f9f848faSopenharmony_ci	/* Reload EEPROM. */
538f9f848faSopenharmony_ci	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 2);
539f9f848faSopenharmony_ci	/*
540f9f848faSopenharmony_ci	 * Save PHY power saving configuration(high byte) and
541f9f848faSopenharmony_ci	 * clear EEPROM checksum value(low byte).
542f9f848faSopenharmony_ci	 */
543f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom);
544f9f848faSopenharmony_ci	sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
545f9f848faSopenharmony_ci
546f9f848faSopenharmony_ci	/*
547f9f848faSopenharmony_ci	 * Auto-loaded default station address from internal ROM is
548f9f848faSopenharmony_ci	 * 00:00:00:00:00:00 such that an explicit access to EEPROM
549f9f848faSopenharmony_ci	 * is required to get real station address.
550f9f848faSopenharmony_ci	 */
551f9f848faSopenharmony_ci	eaddr = ue->ue_eaddr;
552f9f848faSopenharmony_ci	ifp->ac_if.hwaddr_len = NETIF_MAX_HWADDR_LEN;
553f9f848faSopenharmony_ci	tmp = (uint8_t *) ifp->ac_if.hwaddr;
554f9f848faSopenharmony_ci	for (i = 0; i < NETIF_MAX_HWADDR_LEN / 2; i++) {
555f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i, &eeprom);
556f9f848faSopenharmony_ci		eeprom = le16toh(eeprom);
557f9f848faSopenharmony_ci		*eaddr++ = (uint8_t)(eeprom & 0xFF);
558f9f848faSopenharmony_ci		*eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
559f9f848faSopenharmony_ci		*tmp++ = (uint8_t)(eeprom & 0xFF);
560f9f848faSopenharmony_ci		*tmp++ = (uint8_t)((eeprom >> 8) & 0xFF);
561f9f848faSopenharmony_ci	}
562f9f848faSopenharmony_ci	axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, ue->ue_eaddr);
563f9f848faSopenharmony_ci
564f9f848faSopenharmony_ci	axe_attach_post(ue);
565f9f848faSopenharmony_ci
566f9f848faSopenharmony_ci	/* Wakeup PHY. */
567f9f848faSopenharmony_ci	axe_ax88772_phywake(sc);
568f9f848faSopenharmony_ci	/* Stop MAC. */
569f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
570f9f848faSopenharmony_ci
571f9f848faSopenharmony_ci	ifp = ue->ue_drv_sc;
572f9f848faSopenharmony_ci	ifp->ac_if.flags |= NETIF_FLAG_UP | NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
573f9f848faSopenharmony_ci	ifp->ac_if.flags &= ~NETIF_FLAG_LINK_UP;
574f9f848faSopenharmony_ci	sc->sc_flags &= ~AXE_FLAG_LINK;
575f9f848faSopenharmony_ci
576f9f848faSopenharmony_ci	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
577f9f848faSopenharmony_ci}
578f9f848faSopenharmony_ci
579f9f848faSopenharmony_ci#undef	AXE_GPIO_WRITE
580f9f848faSopenharmony_ci
581f9f848faSopenharmony_cistatic void
582f9f848faSopenharmony_ciaxe_reset(struct axe_softc *sc)
583f9f848faSopenharmony_ci{
584f9f848faSopenharmony_ci	struct usb_config_descriptor *cd;
585f9f848faSopenharmony_ci	usb_error_t err;
586f9f848faSopenharmony_ci
587f9f848faSopenharmony_ci	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
588f9f848faSopenharmony_ci	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
589f9f848faSopenharmony_ci							    cd->bConfigurationValue);
590f9f848faSopenharmony_ci	if (err)
591f9f848faSopenharmony_ci		DPRINTF("reset failed (ignored)\n");
592f9f848faSopenharmony_ci
593f9f848faSopenharmony_ci	/* Wait a little while for the chip to get its brains in order. */
594f9f848faSopenharmony_ci	axe_uether_pause(&sc->sc_ue, hz / 100);
595f9f848faSopenharmony_ci	/* Reinitialize controller to achieve full reset. */
596f9f848faSopenharmony_ci	if (sc->sc_flags & AXE_FLAG_178)
597f9f848faSopenharmony_ci		axe_ax88178_init(sc);
598f9f848faSopenharmony_ci	else if (sc->sc_flags & AXE_FLAG_772)
599f9f848faSopenharmony_ci		axe_ax88772_init(sc);
600f9f848faSopenharmony_ci	else if (sc->sc_flags & AXE_FLAG_772A)
601f9f848faSopenharmony_ci		axe_ax88772b_init(sc);
602f9f848faSopenharmony_ci	else if (sc->sc_flags & AXE_FLAG_772B)
603f9f848faSopenharmony_ci		axe_ax88772b_init(sc);
604f9f848faSopenharmony_ci
605f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0,
606f9f848faSopenharmony_ci		AXE_MEDIA_FULL_DUPLEX | AXE_178_MEDIA_TXFLOW_CONTROL_EN | AXE_178_MEDIA_RXFLOW_CONTROL_EN
607f9f848faSopenharmony_ci		| AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC, NULL);
608f9f848faSopenharmony_ci}
609f9f848faSopenharmony_ci
610f9f848faSopenharmony_cistatic void
611f9f848faSopenharmony_ciaxe_attach_post(struct usb_ether *ue)
612f9f848faSopenharmony_ci{
613f9f848faSopenharmony_ci	struct axe_softc *sc = uether_getsc(ue);
614f9f848faSopenharmony_ci	/*
615f9f848faSopenharmony_ci	 * Load PHY indexes first. Needed by axe_xxx_init().
616f9f848faSopenharmony_ci	 */
617f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
618f9f848faSopenharmony_ci	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
619f9f848faSopenharmony_ci	if (sc->sc_phyno == -1)
620f9f848faSopenharmony_ci		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
621f9f848faSopenharmony_ci	if (sc->sc_phyno == -1) {
622f9f848faSopenharmony_ci		device_printf(sc->sc_ue.ue_dev,
623f9f848faSopenharmony_ci					    "no valid PHY address found, assuming PHY address 0\n");
624f9f848faSopenharmony_ci		sc->sc_phyno = 0;
625f9f848faSopenharmony_ci	}
626f9f848faSopenharmony_ci	/*
627f9f848faSopenharmony_ci	 * Fetch IPG values.
628f9f848faSopenharmony_ci	 */
629f9f848faSopenharmony_ci	if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B)) {
630f9f848faSopenharmony_ci		/* Set IPG values. */
631f9f848faSopenharmony_ci		sc->sc_ipgs[0] = 0x15;
632f9f848faSopenharmony_ci		sc->sc_ipgs[1] = 0x16;
633f9f848faSopenharmony_ci		sc->sc_ipgs[2] = 0x1A;
634f9f848faSopenharmony_ci	} else
635f9f848faSopenharmony_ci		axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
636f9f848faSopenharmony_ci}
637f9f848faSopenharmony_ci/*
638f9f848faSopenharmony_ci * Probe for a AX88172 chip.
639f9f848faSopenharmony_ci */
640f9f848faSopenharmony_cistatic int
641f9f848faSopenharmony_ciaxe_probe(device_t dev)
642f9f848faSopenharmony_ci{
643f9f848faSopenharmony_ci	struct usb_attach_arg *uaa = device_get_ivars(dev);
644f9f848faSopenharmony_ci
645f9f848faSopenharmony_ci	if (uaa->usb_mode != USB_MODE_HOST)
646f9f848faSopenharmony_ci		return (ENXIO);
647f9f848faSopenharmony_ci	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
648f9f848faSopenharmony_ci		return (ENXIO);
649f9f848faSopenharmony_ci	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
650f9f848faSopenharmony_ci		return (ENXIO);
651f9f848faSopenharmony_ci	return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
652f9f848faSopenharmony_ci}
653f9f848faSopenharmony_ci
654f9f848faSopenharmony_cistatic void
655f9f848faSopenharmony_ciaxe_miibus_statchg(struct axe_softc *sc, uint16_t link_status)
656f9f848faSopenharmony_ci{
657f9f848faSopenharmony_ci	struct usb_ether *ue = &sc->sc_ue;
658f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
659f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc *)ifp->driver_context;
660f9f848faSopenharmony_ci
661f9f848faSopenharmony_ci	if (drv_sc->state & IFF_DRV_RUNNING) {
662f9f848faSopenharmony_ci		if (link_status) {
663f9f848faSopenharmony_ci			if (sc->sc_flags & AXE_FLAG_772A) {
664f9f848faSopenharmony_ci				PRINTK("\nAX88772A Link Up ");
665f9f848faSopenharmony_ci			}
666f9f848faSopenharmony_ci			else if (sc->sc_flags & AXE_FLAG_772B) {
667f9f848faSopenharmony_ci				PRINTK("\nAX88772B Link Up ");
668f9f848faSopenharmony_ci			}
669f9f848faSopenharmony_ci
670f9f848faSopenharmony_ci			(void) axe_setmedium(sc);
671f9f848faSopenharmony_ci			axe_start(ue);
672f9f848faSopenharmony_ci
673f9f848faSopenharmony_ci			ifp->ac_if.flags |= NETIF_FLAG_LINK_UP;
674f9f848faSopenharmony_ci			(void)netifapi_netif_set_up(&ifp->ac_if);
675f9f848faSopenharmony_ci		} else {
676f9f848faSopenharmony_ci			if (sc->sc_flags & AXE_FLAG_772A)
677f9f848faSopenharmony_ci				PRINTK("\nAX88772A Link Down\n");
678f9f848faSopenharmony_ci			else if (sc->sc_flags & AXE_FLAG_772B)
679f9f848faSopenharmony_ci				PRINTK("\nAX88772B Link Down\n");
680f9f848faSopenharmony_ci
681f9f848faSopenharmony_ci			ifp->ac_if.flags &= ~NETIF_FLAG_LINK_UP;
682f9f848faSopenharmony_ci		}
683f9f848faSopenharmony_ci	}
684f9f848faSopenharmony_ci}
685f9f848faSopenharmony_ci
686f9f848faSopenharmony_ci/*
687f9f848faSopenharmony_ci * Attach the interface. Allocate softc structures, do ifmedia
688f9f848faSopenharmony_ci * setup and ethernet/BPF attach.
689f9f848faSopenharmony_ci */
690f9f848faSopenharmony_cistatic int
691f9f848faSopenharmony_ciaxe_attach(device_t dev)
692f9f848faSopenharmony_ci{
693f9f848faSopenharmony_ci	struct usb_attach_arg *uaa = device_get_ivars(dev);
694f9f848faSopenharmony_ci	struct axe_softc *sc = device_get_softc(dev);
695f9f848faSopenharmony_ci	struct usb_ether *ue = &sc->sc_ue;
696f9f848faSopenharmony_ci	uint8_t iface_index;
697f9f848faSopenharmony_ci	int error;
698f9f848faSopenharmony_ci	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
699f9f848faSopenharmony_ci	sc->sc_link_status = AXE_LINK_MASK;
700f9f848faSopenharmony_ci
701f9f848faSopenharmony_ci	device_set_usb_desc(dev);
702f9f848faSopenharmony_ci
703f9f848faSopenharmony_ci	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_RECURSE);
704f9f848faSopenharmony_ci
705f9f848faSopenharmony_ci	iface_index = AXE_IFACE_IDX;
706f9f848faSopenharmony_ci	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
707f9f848faSopenharmony_ci		axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
708f9f848faSopenharmony_ci	if (error) {
709f9f848faSopenharmony_ci		device_printf(dev, "allocating USB transfers failed\n");
710f9f848faSopenharmony_ci		goto detach;
711f9f848faSopenharmony_ci	}
712f9f848faSopenharmony_ci
713f9f848faSopenharmony_ci	ue->ue_sc = sc;
714f9f848faSopenharmony_ci	ue->ue_dev = dev;
715f9f848faSopenharmony_ci	ue->ue_udev = uaa->device;
716f9f848faSopenharmony_ci	ue->ue_mtx = &sc->sc_mtx;
717f9f848faSopenharmony_ci	ue->ue_methods = &axe_ue_methods;
718f9f848faSopenharmony_ci	error = uether_ifattach(ue);
719f9f848faSopenharmony_ci	if (error) {
720f9f848faSopenharmony_ci		device_printf(dev, "could not attach interface\n");
721f9f848faSopenharmony_ci		goto detach;
722f9f848faSopenharmony_ci	}
723f9f848faSopenharmony_ci	return (0);			/* success */
724f9f848faSopenharmony_ci
725f9f848faSopenharmony_cidetach:
726f9f848faSopenharmony_ci	(void) axe_detach(dev);
727f9f848faSopenharmony_ci	return (ENXIO);			/* failure */
728f9f848faSopenharmony_ci}
729f9f848faSopenharmony_ci
730f9f848faSopenharmony_cistatic int
731f9f848faSopenharmony_ciaxe_detach(device_t dev)
732f9f848faSopenharmony_ci{
733f9f848faSopenharmony_ci	struct axe_softc *sc = device_get_softc(dev);
734f9f848faSopenharmony_ci	struct usb_ether *ue = &sc->sc_ue;
735f9f848faSopenharmony_ci
736f9f848faSopenharmony_ci	usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
737f9f848faSopenharmony_ci	uether_ifdetach(ue);
738f9f848faSopenharmony_ci	mtx_destroy(&sc->sc_mtx);
739f9f848faSopenharmony_ci
740f9f848faSopenharmony_ci	return (0);
741f9f848faSopenharmony_ci}
742f9f848faSopenharmony_ci
743f9f848faSopenharmony_ci#if (AXE_BULK_BUF_SIZE >= 0x10000)
744f9f848faSopenharmony_ci#error "Please update axe_bulk_read_callback()!"
745f9f848faSopenharmony_ci#endif
746f9f848faSopenharmony_ci
747f9f848faSopenharmony_cistatic void
748f9f848faSopenharmony_ciaxe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
749f9f848faSopenharmony_ci{
750f9f848faSopenharmony_ci	struct axe_softc *sc = usbd_xfer_softc(xfer);
751f9f848faSopenharmony_ci	struct usb_ether *ue = &sc->sc_ue;
752f9f848faSopenharmony_ci	struct usb_page_cache *pc;
753f9f848faSopenharmony_ci	int actlen;
754f9f848faSopenharmony_ci
755f9f848faSopenharmony_ci	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
756f9f848faSopenharmony_ci	switch (USB_GET_STATE(xfer)) {
757f9f848faSopenharmony_ci	case USB_ST_TRANSFERRED:
758f9f848faSopenharmony_ci		pc = usbd_xfer_get_frame(xfer, 0);
759f9f848faSopenharmony_ci		(void) axe_rx_frame(ue, pc, actlen);
760f9f848faSopenharmony_ci		/* FALLTHROUGH */
761f9f848faSopenharmony_ci	case USB_ST_SETUP:
762f9f848faSopenharmony_citr_setup:
763f9f848faSopenharmony_ci		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
764f9f848faSopenharmony_ci		usbd_transfer_submit(xfer);
765f9f848faSopenharmony_ci		uether_rxflush(ue);
766f9f848faSopenharmony_ci		return;
767f9f848faSopenharmony_ci
768f9f848faSopenharmony_ci	default:			/* Error */
769f9f848faSopenharmony_ci		DPRINTF("bulk read error, %s\n", usbd_errstr(error));
770f9f848faSopenharmony_ci
771f9f848faSopenharmony_ci		if (error != USB_ERR_CANCELLED) {
772f9f848faSopenharmony_ci			/* try to clear stall first */
773f9f848faSopenharmony_ci			usbd_xfer_set_stall(xfer);
774f9f848faSopenharmony_ci			goto tr_setup;
775f9f848faSopenharmony_ci		}
776f9f848faSopenharmony_ci		return;
777f9f848faSopenharmony_ci	}
778f9f848faSopenharmony_ci}
779f9f848faSopenharmony_ci
780f9f848faSopenharmony_cistatic int
781f9f848faSopenharmony_ciaxe_rx_frame(struct usb_ether *ue, struct usb_page_cache *pc, int actlen)
782f9f848faSopenharmony_ci{
783f9f848faSopenharmony_ci	struct axe_softc *sc = ue->ue_sc;
784f9f848faSopenharmony_ci	struct axe_sframe_hdr hdr;
785f9f848faSopenharmony_ci	struct axe_csum_hdr csum_hdr;
786f9f848faSopenharmony_ci	int error, len, pos;
787f9f848faSopenharmony_ci
788f9f848faSopenharmony_ci	pos = 0;
789f9f848faSopenharmony_ci	len = 0;
790f9f848faSopenharmony_ci	error = 0;
791f9f848faSopenharmony_ci
792f9f848faSopenharmony_ci	if ((sc->sc_flags & AXE_FLAG_STD_FRAME) != 0) {
793f9f848faSopenharmony_ci		while (pos < actlen) {
794f9f848faSopenharmony_ci			if ((int)(pos + sizeof(hdr)) > actlen) {
795f9f848faSopenharmony_ci				/* too little data */
796f9f848faSopenharmony_ci				error = EINVAL;
797f9f848faSopenharmony_ci				break;
798f9f848faSopenharmony_ci			}
799f9f848faSopenharmony_ci
800f9f848faSopenharmony_ci			usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
801f9f848faSopenharmony_ci			if ((hdr.len ^ hdr.ilen) != sc->sc_lenmask) {
802f9f848faSopenharmony_ci				/* we lost sync */
803f9f848faSopenharmony_ci				error = EINVAL;
804f9f848faSopenharmony_ci				break;
805f9f848faSopenharmony_ci			}
806f9f848faSopenharmony_ci			pos += sizeof(hdr);
807f9f848faSopenharmony_ci			len = le16toh(hdr.len);
808f9f848faSopenharmony_ci			if (pos + len > actlen) {
809f9f848faSopenharmony_ci				/* invalid length */
810f9f848faSopenharmony_ci				error = EINVAL;
811f9f848faSopenharmony_ci				break;
812f9f848faSopenharmony_ci			}
813f9f848faSopenharmony_ci			(void) axe_rxeof(ue, pc, pos, len, NULL);
814f9f848faSopenharmony_ci			pos += len + (len % 2);
815f9f848faSopenharmony_ci		}
816f9f848faSopenharmony_ci	} else if ((sc->sc_flags & AXE_FLAG_CSUM_FRAME) != 0) {
817f9f848faSopenharmony_ci		while (pos < actlen) {
818f9f848faSopenharmony_ci			if ((int)(pos + sizeof(csum_hdr)) > actlen) {
819f9f848faSopenharmony_ci				/* too little data */
820f9f848faSopenharmony_ci				error = EINVAL;
821f9f848faSopenharmony_ci				break;
822f9f848faSopenharmony_ci			}
823f9f848faSopenharmony_ci			usbd_copy_out(pc, pos, &csum_hdr, sizeof(csum_hdr));
824f9f848faSopenharmony_ci
825f9f848faSopenharmony_ci			csum_hdr.len = le16toh(csum_hdr.len);
826f9f848faSopenharmony_ci			csum_hdr.ilen = le16toh(csum_hdr.ilen);
827f9f848faSopenharmony_ci			csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
828f9f848faSopenharmony_ci			if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
829f9f848faSopenharmony_ci			    AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
830f9f848faSopenharmony_ci			    sc->sc_lenmask) {
831f9f848faSopenharmony_ci				/* we lost sync */
832f9f848faSopenharmony_ci				error = EINVAL;
833f9f848faSopenharmony_ci				break;
834f9f848faSopenharmony_ci			}
835f9f848faSopenharmony_ci			/*
836f9f848faSopenharmony_ci			 * Get total transferred frame length including
837f9f848faSopenharmony_ci			 * checksum header.  The length should be multiple
838f9f848faSopenharmony_ci			 * of 4.
839f9f848faSopenharmony_ci			 */
840f9f848faSopenharmony_ci			len = sizeof(csum_hdr) + AXE_CSUM_RXBYTES(csum_hdr.len);
841f9f848faSopenharmony_ci			len = (len + 3) & ~3;
842f9f848faSopenharmony_ci			if (pos + len > actlen) {
843f9f848faSopenharmony_ci				/* invalid length */
844f9f848faSopenharmony_ci				error = EINVAL;
845f9f848faSopenharmony_ci				break;
846f9f848faSopenharmony_ci			}
847f9f848faSopenharmony_ci			(void) axe_rxeof(ue, pc, pos + sizeof(csum_hdr),
848f9f848faSopenharmony_ci				AXE_CSUM_RXBYTES(csum_hdr.len), &csum_hdr);
849f9f848faSopenharmony_ci			pos += len;
850f9f848faSopenharmony_ci		}
851f9f848faSopenharmony_ci	} else {
852f9f848faSopenharmony_ci		(void)axe_rxeof(ue, pc, 0, actlen, NULL);
853f9f848faSopenharmony_ci	}
854f9f848faSopenharmony_ci	return (error);
855f9f848faSopenharmony_ci}
856f9f848faSopenharmony_ci
857f9f848faSopenharmony_cistatic int
858f9f848faSopenharmony_ciaxe_rxeof(struct usb_ether *ue, struct usb_page_cache *pc, unsigned int offset,
859f9f848faSopenharmony_ci		    unsigned int len, struct axe_csum_hdr *csum_hdr)
860f9f848faSopenharmony_ci{
861f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
862f9f848faSopenharmony_ci
863f9f848faSopenharmony_ci	struct pbuf *m = pbuf_alloc(PBUF_RAW, len+ETH_PAD_SIZE, PBUF_RAM);
864f9f848faSopenharmony_ci	struct pbuf *p;
865f9f848faSopenharmony_ci
866f9f848faSopenharmony_ci	if (len < ETHER_HDR_LEN) {
867f9f848faSopenharmony_ci		(void)pbuf_free(m);
868f9f848faSopenharmony_ci		return (EINVAL);
869f9f848faSopenharmony_ci	}
870f9f848faSopenharmony_ci
871f9f848faSopenharmony_ci#if ETH_PAD_SIZE
872f9f848faSopenharmony_ci	/* drop the padding word */
873f9f848faSopenharmony_ci	if (pbuf_header(m, -ETH_PAD_SIZE)) {
874f9f848faSopenharmony_ci		PRINTK("[AXE_ERROR]axe_rxeof : pbuf_header drop failed\n");
875f9f848faSopenharmony_ci		(void)pbuf_free(m);
876f9f848faSopenharmony_ci		return (EINVAL);
877f9f848faSopenharmony_ci	}
878f9f848faSopenharmony_ci#endif
879f9f848faSopenharmony_ci
880f9f848faSopenharmony_ci	for (p = m; p != NULL; p = p->next)
881f9f848faSopenharmony_ci		usbd_copy_out(pc, offset, p->payload, p->len);
882f9f848faSopenharmony_ci
883f9f848faSopenharmony_ci#if ETH_PAD_SIZE
884f9f848faSopenharmony_ci	/* reclaim the padding word */
885f9f848faSopenharmony_ci	if (pbuf_header(m, ETH_PAD_SIZE)) {
886f9f848faSopenharmony_ci		PRINTK("[AXE_ERROR]axe_rxeof : pbuf_header drop failed\n");
887f9f848faSopenharmony_ci		(void)pbuf_free(m);
888f9f848faSopenharmony_ci		return (EINVAL);
889f9f848faSopenharmony_ci	}
890f9f848faSopenharmony_ci#endif
891f9f848faSopenharmony_ci
892f9f848faSopenharmony_ci	driverif_input(&ifp->ac_if, m);
893f9f848faSopenharmony_ci	return (0);
894f9f848faSopenharmony_ci}
895f9f848faSopenharmony_ci
896f9f848faSopenharmony_ci#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
897f9f848faSopenharmony_ci#error "Please update axe_bulk_write_callback()!"
898f9f848faSopenharmony_ci#endif
899f9f848faSopenharmony_ci
900f9f848faSopenharmony_cistatic void
901f9f848faSopenharmony_ciaxe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
902f9f848faSopenharmony_ci{
903f9f848faSopenharmony_ci	struct axe_softc *sc = usbd_xfer_softc(xfer);
904f9f848faSopenharmony_ci	struct axe_sframe_hdr hdr;
905f9f848faSopenharmony_ci	struct usb_ether *ue = &(sc->sc_ue);
906f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
907f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc *)ifp->driver_context;
908f9f848faSopenharmony_ci	struct usb_page_cache *pc;
909f9f848faSopenharmony_ci	uint16_t txlen;
910f9f848faSopenharmony_ci	int nframes, pos;
911f9f848faSopenharmony_ci	struct pbuf *p;
912f9f848faSopenharmony_ci	uint8_t ustat;
913f9f848faSopenharmony_ci
914f9f848faSopenharmony_ci	ustat = USB_GET_STATE(xfer);
915f9f848faSopenharmony_citr_setup:
916f9f848faSopenharmony_ci	switch (ustat) {
917f9f848faSopenharmony_ci	case USB_ST_TRANSFERRED:
918f9f848faSopenharmony_ci		DPRINTFN(11, "transfer complete\n");
919f9f848faSopenharmony_ci		drv_sc->state &= ~IFF_DRV_OACTIVE;
920f9f848faSopenharmony_ci		/* FALLTHROUGH */
921f9f848faSopenharmony_ci
922f9f848faSopenharmony_ci	case USB_ST_SETUP:
923f9f848faSopenharmony_ci		if (drv_sc->state & IFF_DRV_OACTIVE)
924f9f848faSopenharmony_ci			return;
925f9f848faSopenharmony_ci
926f9f848faSopenharmony_ci		UE_LOCK(ue);
927f9f848faSopenharmony_ci		IF_DEQUEUE(&(ue->ue_txq), p);
928f9f848faSopenharmony_ci		UE_UNLOCK(ue);
929f9f848faSopenharmony_ci
930f9f848faSopenharmony_ci		nframes = 0;
931f9f848faSopenharmony_ci		while (p) {
932f9f848faSopenharmony_ci			txlen = p->len;
933f9f848faSopenharmony_ci			if (txlen <= 0)
934f9f848faSopenharmony_ci				break;
935f9f848faSopenharmony_ci
936f9f848faSopenharmony_ci			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES, nframes);
937f9f848faSopenharmony_ci			pos = 0;
938f9f848faSopenharmony_ci			pc = usbd_xfer_get_frame(xfer, nframes);
939f9f848faSopenharmony_ci			if (AXE_IS_178_FAMILY(sc)) {
940f9f848faSopenharmony_ci				hdr.len = htole16(txlen);
941f9f848faSopenharmony_ci				hdr.ilen = ~hdr.len;
942f9f848faSopenharmony_ci				usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
943f9f848faSopenharmony_ci				pos += sizeof(hdr);
944f9f848faSopenharmony_ci				usbd_copy_in(pc, pos, p->payload, txlen);
945f9f848faSopenharmony_ci				pos += txlen;
946f9f848faSopenharmony_ci			} else {
947f9f848faSopenharmony_ci				usbd_copy_in(pc, pos, p->payload, txlen);
948f9f848faSopenharmony_ci				pos += txlen;
949f9f848faSopenharmony_ci			}
950f9f848faSopenharmony_ci
951f9f848faSopenharmony_ci			/* Set frame length. */
952f9f848faSopenharmony_ci			usbd_xfer_set_frame_len(xfer, nframes, pos);
953f9f848faSopenharmony_ci
954f9f848faSopenharmony_ci			uether_freebuf(p);
955f9f848faSopenharmony_ci			nframes++;
956f9f848faSopenharmony_ci			if (nframes >= USB_AXE_MAX_FRAMES)
957f9f848faSopenharmony_ci				break;
958f9f848faSopenharmony_ci
959f9f848faSopenharmony_ci			UE_LOCK(ue);
960f9f848faSopenharmony_ci			IF_DEQUEUE(&(ue->ue_txq), p);
961f9f848faSopenharmony_ci			UE_UNLOCK(ue);
962f9f848faSopenharmony_ci		}
963f9f848faSopenharmony_ci		if (nframes != 0) {
964f9f848faSopenharmony_ci			usbd_xfer_set_frames(xfer, nframes);
965f9f848faSopenharmony_ci			usbd_transfer_submit(xfer);
966f9f848faSopenharmony_ci			drv_sc->state |= IFF_DRV_OACTIVE;
967f9f848faSopenharmony_ci		}
968f9f848faSopenharmony_ci		break;
969f9f848faSopenharmony_ci
970f9f848faSopenharmony_ci	default:			/* Error */
971f9f848faSopenharmony_ci		DPRINTFN(11, "transfer error, %s\n",
972f9f848faSopenharmony_ci			    usbd_errstr(error));
973f9f848faSopenharmony_ci		drv_sc->state &= ~IFF_DRV_OACTIVE;
974f9f848faSopenharmony_ci		if (error != USB_ERR_CANCELLED) {
975f9f848faSopenharmony_ci			/* try to clear stall first */
976f9f848faSopenharmony_ci			usbd_xfer_set_stall(xfer);
977f9f848faSopenharmony_ci			ustat = USB_ST_SETUP;
978f9f848faSopenharmony_ci			goto tr_setup;
979f9f848faSopenharmony_ci		}
980f9f848faSopenharmony_ci		break;
981f9f848faSopenharmony_ci	}
982f9f848faSopenharmony_ci}
983f9f848faSopenharmony_ci
984f9f848faSopenharmony_cistatic void
985f9f848faSopenharmony_ciaxe_start(struct usb_ether *ue)
986f9f848faSopenharmony_ci{
987f9f848faSopenharmony_ci	struct axe_softc *sc = ue->ue_sc;
988f9f848faSopenharmony_ci
989f9f848faSopenharmony_ci	/*
990f9f848faSopenharmony_ci	 * start the USB transfers, if not already started:
991f9f848faSopenharmony_ci	 */
992f9f848faSopenharmony_ci	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
993f9f848faSopenharmony_ci	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
994f9f848faSopenharmony_ci}
995f9f848faSopenharmony_ci
996f9f848faSopenharmony_cistatic void
997f9f848faSopenharmony_ciaxe_csum_cfg(struct usb_ether *ue)
998f9f848faSopenharmony_ci{
999f9f848faSopenharmony_ci	(void)ue;
1000f9f848faSopenharmony_ci}
1001f9f848faSopenharmony_ci
1002f9f848faSopenharmony_cistatic void
1003f9f848faSopenharmony_ciaxe_init(struct usb_ether *ue)
1004f9f848faSopenharmony_ci{
1005f9f848faSopenharmony_ci	struct axe_softc *sc = uether_getsc(ue);
1006f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
1007f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc *)ifp->driver_context;
1008f9f848faSopenharmony_ci	uint16_t rxmode;
1009f9f848faSopenharmony_ci
1010f9f848faSopenharmony_ci	drv_sc->state = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1011f9f848faSopenharmony_ci
1012f9f848faSopenharmony_ci	AXE_LOCK_ASSERT(sc, MA_OWNED);
1013f9f848faSopenharmony_ci	if ((drv_sc->state & IFF_DRV_RUNNING) != 0)
1014f9f848faSopenharmony_ci		return;
1015f9f848faSopenharmony_ci
1016f9f848faSopenharmony_ci	/* Cancel pending I/O */
1017f9f848faSopenharmony_ci	axe_stop(ue);
1018f9f848faSopenharmony_ci	axe_reset(sc);
1019f9f848faSopenharmony_ci
1020f9f848faSopenharmony_ci	/* Set MAC address and transmitter IPG values. */
1021f9f848faSopenharmony_ci	if (AXE_IS_178_FAMILY(sc)) {
1022f9f848faSopenharmony_ci		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
1023f9f848faSopenharmony_ci		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
1024f9f848faSopenharmony_ci			    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
1025f9f848faSopenharmony_ci	} else {
1026f9f848faSopenharmony_ci		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
1027f9f848faSopenharmony_ci		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
1028f9f848faSopenharmony_ci		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
1029f9f848faSopenharmony_ci		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
1030f9f848faSopenharmony_ci	}
1031f9f848faSopenharmony_ci	if (AXE_IS_178_FAMILY(sc)) {
1032f9f848faSopenharmony_ci		sc->sc_flags &= ~(AXE_FLAG_STD_FRAME | AXE_FLAG_CSUM_FRAME);
1033f9f848faSopenharmony_ci		sc->sc_lenmask = AXE_HDR_LEN_MASK;
1034f9f848faSopenharmony_ci		sc->sc_flags |= AXE_FLAG_STD_FRAME;
1035f9f848faSopenharmony_ci	}
1036f9f848faSopenharmony_ci	/* Configure TX/RX checksum offloading. */
1037f9f848faSopenharmony_ci	axe_csum_cfg(ue);
1038f9f848faSopenharmony_ci	if (sc->sc_flags & AXE_FLAG_772B) {
1039f9f848faSopenharmony_ci		/* AX88772B uses different maximum frame burst configuration. */
1040f9f848faSopenharmony_ci		axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1041f9f848faSopenharmony_ci			    ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1042f9f848faSopenharmony_ci			    ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1043f9f848faSopenharmony_ci	}
1044f9f848faSopenharmony_ci
1045f9f848faSopenharmony_ci	/* Enable receiver, set RX mode. */
1046f9f848faSopenharmony_ci	rxmode = (AXE_RXCMD_ALLMULTI | AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1047f9f848faSopenharmony_ci	if (AXE_IS_178_FAMILY(sc)) {
1048f9f848faSopenharmony_ci		if (sc->sc_flags & AXE_FLAG_772B) {
1049f9f848faSopenharmony_ci			/*
1050f9f848faSopenharmony_ci			 * Select RX header format type 1.  Aligning IP
1051f9f848faSopenharmony_ci			 * header on 4 byte boundary is not needed when
1052f9f848faSopenharmony_ci			 * checksum offloading feature is not used
1053f9f848faSopenharmony_ci			 * because we always copy the received frame in
1054f9f848faSopenharmony_ci			 * RX handler.  When RX checksum offloading is
1055f9f848faSopenharmony_ci			 * active, aligning IP header is required to
1056f9f848faSopenharmony_ci			 * reflect actual frame length including RX
1057f9f848faSopenharmony_ci			 * header size.
1058f9f848faSopenharmony_ci			 */
1059f9f848faSopenharmony_ci			rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1060f9f848faSopenharmony_ci		} else {
1061f9f848faSopenharmony_ci			/*
1062f9f848faSopenharmony_ci			 * Default Rx buffer size is too small to get
1063f9f848faSopenharmony_ci			 * maximum performance.
1064f9f848faSopenharmony_ci			 */
1065f9f848faSopenharmony_ci			if (sc->sc_flags & AXE_FLAG_772A)
1066f9f848faSopenharmony_ci				rxmode |= AXE_178_RXCMD_MFB_16384;
1067f9f848faSopenharmony_ci		}
1068f9f848faSopenharmony_ci	} else {
1069f9f848faSopenharmony_ci		rxmode |= AXE_172_RXCMD_UNICAST;
1070f9f848faSopenharmony_ci	}
1071f9f848faSopenharmony_ci
1072f9f848faSopenharmony_ci	/* If we want promiscuous mode, set the allframes bit. */
1073f9f848faSopenharmony_ci	if (drv_sc->state & IFF_PROMISC)
1074f9f848faSopenharmony_ci		rxmode |= AXE_RXCMD_PROMISC;
1075f9f848faSopenharmony_ci
1076f9f848faSopenharmony_ci	if (drv_sc->state & IFF_BROADCAST)
1077f9f848faSopenharmony_ci		rxmode |= AXE_RXCMD_BROADCAST;
1078f9f848faSopenharmony_ci
1079f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1080f9f848faSopenharmony_ci
1081f9f848faSopenharmony_ci	/* Load the multicast filter. */
1082f9f848faSopenharmony_ci	axe_setmulti(ue);
1083f9f848faSopenharmony_ci
1084f9f848faSopenharmony_ci	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
1085f9f848faSopenharmony_ci	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_RD]);
1086f9f848faSopenharmony_ci
1087f9f848faSopenharmony_ci	drv_sc->state |= IFF_DRV_RUNNING;
1088f9f848faSopenharmony_ci	ifp->ac_if.link_layer_type = ETHERNET_DRIVER_IF;
1089f9f848faSopenharmony_ci}
1090f9f848faSopenharmony_ci
1091f9f848faSopenharmony_cistatic void
1092f9f848faSopenharmony_ciaxe_setpromisc(struct usb_ether *ue)
1093f9f848faSopenharmony_ci{
1094f9f848faSopenharmony_ci	struct axe_softc *sc = uether_getsc(ue);
1095f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
1096f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc *)ifp->driver_context;
1097f9f848faSopenharmony_ci	uint16_t rxmode;
1098f9f848faSopenharmony_ci
1099f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1100f9f848faSopenharmony_ci
1101f9f848faSopenharmony_ci	rxmode = le16toh(rxmode);
1102f9f848faSopenharmony_ci
1103f9f848faSopenharmony_ci	if (drv_sc->state & IFF_PROMISC) {
1104f9f848faSopenharmony_ci		rxmode |= AXE_RXCMD_PROMISC;
1105f9f848faSopenharmony_ci	} else {
1106f9f848faSopenharmony_ci		rxmode &= ~AXE_RXCMD_PROMISC;
1107f9f848faSopenharmony_ci	}
1108f9f848faSopenharmony_ci
1109f9f848faSopenharmony_ci	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1110f9f848faSopenharmony_ci	axe_setmulti(ue);
1111f9f848faSopenharmony_ci}
1112f9f848faSopenharmony_ci
1113f9f848faSopenharmony_cistatic void
1114f9f848faSopenharmony_ciaxe_tick(struct usb_ether *ue)
1115f9f848faSopenharmony_ci{
1116f9f848faSopenharmony_ci	struct axe_softc *sc = uether_getsc(ue);
1117f9f848faSopenharmony_ci	uint16_t link_status;
1118f9f848faSopenharmony_ci
1119f9f848faSopenharmony_ci	AXE_LOCK_ASSERT(sc, MA_OWNED);
1120f9f848faSopenharmony_ci
1121f9f848faSopenharmony_ci	link_status = axe_miibus_readreg(sc, MII_BMSR) & AXE_LINK_MASK;
1122f9f848faSopenharmony_ci	if (sc->sc_link_status != link_status) {
1123f9f848faSopenharmony_ci		axe_miibus_statchg(sc, link_status);
1124f9f848faSopenharmony_ci		sc->sc_link_status = link_status;
1125f9f848faSopenharmony_ci	}
1126f9f848faSopenharmony_ci}
1127f9f848faSopenharmony_ci
1128f9f848faSopenharmony_cistatic void
1129f9f848faSopenharmony_ciaxe_stop(struct usb_ether *ue)
1130f9f848faSopenharmony_ci{
1131f9f848faSopenharmony_ci	struct axe_softc *sc = uether_getsc(ue);
1132f9f848faSopenharmony_ci	struct los_eth_driver *ifp = ue->ue_drv_sc;
1133f9f848faSopenharmony_ci	struct eth_drv_sc *drv_sc = (struct eth_drv_sc *)ifp->driver_context;
1134f9f848faSopenharmony_ci
1135f9f848faSopenharmony_ci	AXE_LOCK_ASSERT(sc, MA_OWNED);
1136f9f848faSopenharmony_ci	drv_sc->state &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1137f9f848faSopenharmony_ci	sc->sc_flags &= ~AXE_FLAG_LINK;
1138f9f848faSopenharmony_ci	/*
1139f9f848faSopenharmony_ci	 * stop all the transfers, if not already stopped:
1140f9f848faSopenharmony_ci	 */
1141f9f848faSopenharmony_ci	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1142f9f848faSopenharmony_ci	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1143f9f848faSopenharmony_ci}
1144f9f848faSopenharmony_ci
1145f9f848faSopenharmony_ci#undef USB_DEBUG_VAR
1146