1/*	$OpenBSD: hidwusb.c,v 1.4 2023/09/22 01:10:44 jsg Exp $	*/
2/*
3 * Copyright (c) 2017, 2018 Mark Kettenis <kettenis@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/device.h>
21
22#include <machine/bus.h>
23#include <machine/fdt.h>
24#include <machine/simplebusvar.h>
25
26#include <dev/ofw/openfirm.h>
27#include <dev/ofw/ofw_clock.h>
28#include <dev/ofw/ofw_gpio.h>
29#include <dev/ofw/fdt.h>
30
31/*
32 * This driver is based on preliminary device tree bindings and will
33 * almost certainly need changes once the official bindings land in
34 * mainline Linux.  Support for these preliminary bindings will be
35 * dropped as soon as official bindings are available.
36 */
37
38struct hidwusb_softc {
39	struct simplebus_softc	sc_sbus;
40};
41
42int	hidwusb_match(struct device *, void *, void *);
43void	hidwusb_attach(struct device *, struct device *, void *);
44
45const struct cfattach hidwusb_ca = {
46	sizeof(struct hidwusb_softc), hidwusb_match, hidwusb_attach
47};
48
49struct cfdriver hidwusb_cd = {
50	NULL, "hidwusb", DV_DULL
51};
52
53int
54hidwusb_match(struct device *parent, void *match, void *aux)
55{
56	struct fdt_attach_args *faa = aux;
57
58	return OF_is_compatible(faa->fa_node, "hisilicon,kirin970-dwc3");
59}
60
61void
62hidwusb_attach(struct device *parent, struct device *self, void *aux)
63{
64	struct hidwusb_softc *sc = (struct hidwusb_softc *)self;
65	struct fdt_attach_args *faa = aux;
66	uint32_t gpio[3];
67	int node;
68
69	/*
70	 * The HiKey970 has a switch to select between the Type-C and
71	 * a hub with Type-A connectors.  Switch to the USB Type-C
72	 * connector as we can't power up the hub yet.
73	 */
74	node = OF_finddevice("/soc/hikey_usbhub");
75	if (node) {
76		if (OF_getpropintarray(node, "typc_vbus_int_gpio,typec-gpios",
77		    gpio, sizeof(gpio)) == sizeof(gpio)) {
78			gpio_controller_config_pin(gpio, GPIO_CONFIG_OUTPUT);
79			gpio_controller_set_pin(gpio, 1);
80		}
81	}
82
83	clock_enable_all(faa->fa_node);
84	reset_deassert_all(faa->fa_node);
85
86	simplebus_attach(parent, &sc->sc_sbus.sc_dev, faa);
87}
88