bcm2835_dwctwo.c revision 1.2
1/*	$OpenBSD: bcm2835_dwctwo.c,v 1.2 2021/02/05 00:42:25 patrick Exp $	*/
2/*
3 * Copyright (c) 2015 Masao Uebayashi <uebayasi@tombiinc.com>
4 *
5 * Permission to use, copy, modify, and/or 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#include <sys/malloc.h>
22#include <sys/pool.h>
23#include <sys/kthread.h>
24
25#include <machine/intr.h>
26#include <machine/bus.h>
27#include <machine/fdt.h>
28
29#include <dev/ofw/openfirm.h>
30#include <dev/ofw/fdt.h>
31
32#include <dev/usb/usb.h>
33#include <dev/usb/usbdi.h>
34#include <dev/usb/usbdivar.h>
35#include <dev/usb/usb_mem.h>
36#include <dev/usb/usb_quirks.h>
37
38#include <dev/usb/dwc2/dwc2var.h>
39#include <dev/usb/dwc2/dwc2.h>
40#include <dev/usb/dwc2/dwc2_core.h>
41
42struct bcm_dwctwo_softc {
43	struct dwc2_softc	sc_dwc2;
44	void			*sc_ih;
45};
46
47int	bcm_dwctwo_match(struct device *, void *, void *);
48void	bcm_dwctwo_attach(struct device *, struct device *, void *);
49void	bcm_dwctwo_deferred(void *);
50
51const struct cfattach bcmdwctwo_ca = {
52	sizeof(struct bcm_dwctwo_softc), bcm_dwctwo_match, bcm_dwctwo_attach,
53};
54
55struct cfdriver dwctwo_cd = {
56	NULL, "dwctwo", DV_DULL
57};
58
59static struct dwc2_core_params bcm_dwctwo_params = {
60	.otg_cap			= 0,	/* HNP/SRP capable */
61	.otg_ver			= 0,	/* 1.3 */
62	.dma_enable			= 1,
63	.dma_desc_enable		= 0,
64	.speed				= 0,	/* High Speed */
65	.enable_dynamic_fifo		= 1,
66	.en_multiple_tx_fifo		= 1,
67	.host_rx_fifo_size		= 774,	/* 774 DWORDs */
68	.host_nperio_tx_fifo_size	= 256,	/* 256 DWORDs */
69	.host_perio_tx_fifo_size	= 512,	/* 512 DWORDs */
70	.max_transfer_size		= 65535,
71	.max_packet_count		= 511,
72	.host_channels			= 8,
73	.phy_type			= 1,	/* UTMI */
74	.phy_utmi_width			= 8,	/* 8 bits */
75	.phy_ulpi_ddr			= 0,	/* Single */
76	.phy_ulpi_ext_vbus		= 0,
77	.i2c_enable			= 0,
78	.ulpi_fs_ls			= 0,
79	.host_support_fs_ls_low_power	= 0,
80	.host_ls_low_power_phy_clk	= 0,	/* 48 MHz */
81	.ts_dline			= 0,
82	.reload_ctl			= 0,
83	.ahbcfg				= 0x10,
84	.uframe_sched			= 1,
85};
86
87int
88bcm_dwctwo_match(struct device *parent, void *match, void *aux)
89{
90	struct fdt_attach_args *faa = (struct fdt_attach_args *)aux;
91
92	return (OF_is_compatible(faa->fa_node, "brcm,bcm2708-usb") ||
93	    OF_is_compatible(faa->fa_node, "brcm,bcm2835-usb"));
94}
95
96void
97bcm_dwctwo_attach(struct device *parent, struct device *self, void *aux)
98{
99	struct bcm_dwctwo_softc *sc = (struct bcm_dwctwo_softc *)self;
100	struct fdt_attach_args *faa = aux;
101	int idx;
102
103	printf("\n");
104
105	sc->sc_dwc2.sc_iot = faa->fa_iot;
106	sc->sc_dwc2.sc_bus.pipe_size = sizeof(struct usbd_pipe);
107	sc->sc_dwc2.sc_bus.dmatag = faa->fa_dmat;
108	sc->sc_dwc2.sc_params = &bcm_dwctwo_params;
109
110	if (bus_space_map(faa->fa_iot, faa->fa_reg[0].addr,
111	    faa->fa_reg[0].size, 0, &sc->sc_dwc2.sc_ioh))
112		panic("%s: bus_space_map failed!", __func__);
113
114	idx = OF_getindex(faa->fa_node, "usb", "interrupt-names");
115	if (idx == -1)
116		idx = 1;
117
118	sc->sc_ih = fdt_intr_establish_idx(faa->fa_node, idx, IPL_USB,
119	    dwc2_intr, (void *)&sc->sc_dwc2, sc->sc_dwc2.sc_bus.bdev.dv_xname);
120	if (sc->sc_ih == NULL)
121		panic("%s: intr_establish failed!", __func__);
122
123	kthread_create_deferred(bcm_dwctwo_deferred, sc);
124}
125
126void
127bcm_dwctwo_deferred(void *self)
128{
129	struct bcm_dwctwo_softc *sc = (struct bcm_dwctwo_softc *)self;
130	int rc;
131
132	strlcpy(sc->sc_dwc2.sc_vendor, "Broadcom",
133	    sizeof(sc->sc_dwc2.sc_vendor));
134
135	rc = dwc2_init(&sc->sc_dwc2);
136	if (rc != 0)
137		return;
138
139	sc->sc_dwc2.sc_child = config_found(&sc->sc_dwc2.sc_bus.bdev,
140	    &sc->sc_dwc2.sc_bus, usbctlprint);
141}
142