1/*	$NetBSD: usbnode.c,v 1.3 2026/01/17 05:45:17 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: usbnode.c,v 1.3 2026/01/17 05:45:17 skrll Exp $");
34
35#include <sys/param.h>
36#include <sys/device.h>
37
38#include <dev/fdt/fdtvar.h>
39
40struct usbnode_softc {
41	device_t			sc_dev;
42	int				sc_phandle;
43
44	struct fdtbus_gpio_pin *	sc_pin_reset;
45	u_int				sc_resetdelay;
46};
47
48struct usbnode_data {
49	const char *	und_desc;
50	u_int		und_resetdelay;
51};
52
53/* Genesys Logic GL852G usb hub */
54static struct usbnode_data genesys_gl852g_data = {
55	.und_desc = "Genesys Logic GL852G usb hub",
56	.und_resetdelay = 50,
57};
58
59static const struct device_compatible_entry compat_data[] = {
60	{ .compat = "usb5e3,610", .data = &genesys_gl852g_data },
61	DEVICE_COMPAT_EOL
62};
63
64static void
65usbnode_enable(struct usbnode_softc *sc,bool enable)
66{
67
68	if (enable) {
69		fdtbus_gpio_write(sc->sc_pin_reset, 1);
70		delay(sc->sc_resetdelay);
71	}
72
73	fdtbus_gpio_write(sc->sc_pin_reset, enable ? 0 : 1);
74}
75
76static int
77usbnode_match(device_t parent, cfdata_t cf, void *aux)
78{
79	struct fdt_attach_args * const faa = aux;
80
81	return of_compatible_match(faa->faa_phandle, compat_data);
82}
83
84static int
85usbnode_detach(device_t self, int flags)
86{
87	struct usbnode_softc * const sc = device_private(self);
88
89	usbnode_enable(sc, false);
90
91	return 0;
92}
93
94static void
95usbnode_attach(device_t parent, device_t self, void *aux)
96{
97	struct usbnode_softc * const sc = device_private(self);
98	struct fdt_attach_args * const faa = aux;
99	const int phandle = faa->faa_phandle;
100
101	sc->sc_dev = self;
102
103	const struct device_compatible_entry * const dce =
104	    of_compatible_lookup(phandle, compat_data);
105
106	const struct usbnode_data *und = dce->data;
107
108	/* reset gpio is ... */
109	sc->sc_pin_reset =
110	    fdtbus_gpio_acquire(phandle, "reset-gpio", GPIO_PIN_OUTPUT);
111	if (sc->sc_pin_reset == NULL) {
112		aprint_error(": couldn't acquire reset gpio\n");
113	}
114
115	usbnode_enable(sc, true);
116
117	aprint_naive("\n");
118	aprint_normal(": USB node '%s'\n", und->und_desc);
119}
120
121CFATTACH_DECL_NEW(usbnode, sizeof(struct usbnode_softc),
122	usbnode_match, usbnode_attach, usbnode_detach, NULL);
123