1*fe5dbe47Sjmatthew /* $OpenBSD: octehci.c,v 1.3 2017/07/25 11:01:28 jmatthew Exp $ */
2494889e9Sjmatthew
3494889e9Sjmatthew /*
4494889e9Sjmatthew * Copyright (c) 2015 Jonathan Matthew <jmatthew@openbsd.org>
5494889e9Sjmatthew *
6494889e9Sjmatthew * Permission to use, copy, modify, and/or distribute this software for any
7494889e9Sjmatthew * purpose with or without fee is hereby granted, provided that the above
8494889e9Sjmatthew * copyright notice and this permission notice appear in all copies.
9494889e9Sjmatthew *
10494889e9Sjmatthew * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11494889e9Sjmatthew * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12494889e9Sjmatthew * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13494889e9Sjmatthew * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14494889e9Sjmatthew * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15494889e9Sjmatthew * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16494889e9Sjmatthew * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17494889e9Sjmatthew */
18494889e9Sjmatthew
19494889e9Sjmatthew #include <sys/param.h>
20494889e9Sjmatthew #include <sys/systm.h>
21494889e9Sjmatthew #include <sys/device.h>
22494889e9Sjmatthew
23494889e9Sjmatthew #include <machine/intr.h>
24494889e9Sjmatthew #include <machine/bus.h>
25494889e9Sjmatthew #include <machine/octeonreg.h>
26494889e9Sjmatthew #include <machine/octeonvar.h>
27494889e9Sjmatthew
28494889e9Sjmatthew #include <octeon/dev/octuctlreg.h>
29494889e9Sjmatthew #include <octeon/dev/octuctlvar.h>
30494889e9Sjmatthew
31494889e9Sjmatthew #include <dev/usb/usb.h>
32494889e9Sjmatthew #include <dev/usb/usbdi.h>
33494889e9Sjmatthew #include <dev/usb/usbdivar.h>
34494889e9Sjmatthew
35494889e9Sjmatthew #include <sys/rwlock.h>
36494889e9Sjmatthew #include <dev/usb/ehcireg.h>
37494889e9Sjmatthew #include <dev/usb/ehcivar.h>
38494889e9Sjmatthew
39494889e9Sjmatthew struct octehci_softc {
40494889e9Sjmatthew struct ehci_softc sc_ehci;
41494889e9Sjmatthew
42494889e9Sjmatthew void *sc_ih;
43494889e9Sjmatthew };
44494889e9Sjmatthew
45494889e9Sjmatthew int octehci_match(struct device *, void *, void *);
46494889e9Sjmatthew void octehci_attach(struct device *, struct device *, void *);
47494889e9Sjmatthew
48494889e9Sjmatthew const struct cfattach octehci_ca = {
49494889e9Sjmatthew sizeof(struct octehci_softc), octehci_match, octehci_attach,
50494889e9Sjmatthew };
51494889e9Sjmatthew
52494889e9Sjmatthew struct cfdriver octehci_cd = {
53494889e9Sjmatthew NULL, "ehci", DV_DULL
54494889e9Sjmatthew };
55494889e9Sjmatthew
56494889e9Sjmatthew int
octehci_match(struct device * parent,void * match,void * aux)57494889e9Sjmatthew octehci_match(struct device *parent, void *match, void *aux)
58494889e9Sjmatthew {
59494889e9Sjmatthew struct octuctl_attach_args *aa = aux;
60*fe5dbe47Sjmatthew return (OF_is_compatible(aa->aa_node, "cavium,octeon-6335-ehci"));
61494889e9Sjmatthew }
62494889e9Sjmatthew
63494889e9Sjmatthew void
octehci_attach(struct device * parent,struct device * self,void * aux)64494889e9Sjmatthew octehci_attach(struct device *parent, struct device *self, void *aux)
65494889e9Sjmatthew {
66494889e9Sjmatthew struct octehci_softc *sc = (struct octehci_softc *)self;
67494889e9Sjmatthew struct octuctl_attach_args *aa = aux;
68494889e9Sjmatthew uint64_t port_ctl;
69494889e9Sjmatthew int rc;
70494889e9Sjmatthew int s;
71494889e9Sjmatthew
72494889e9Sjmatthew sc->sc_ehci.iot = aa->aa_bust;
73494889e9Sjmatthew sc->sc_ehci.sc_bus.pipe_size = sizeof(struct usbd_pipe);
74494889e9Sjmatthew sc->sc_ehci.sc_bus.dmatag = aa->aa_dmat;
75494889e9Sjmatthew
76*fe5dbe47Sjmatthew rc = bus_space_map(sc->sc_ehci.iot, aa->aa_reg.addr, aa->aa_reg.size,
77494889e9Sjmatthew 0, &sc->sc_ehci.ioh);
78494889e9Sjmatthew KASSERT(rc == 0);
79494889e9Sjmatthew
80494889e9Sjmatthew port_ctl = bus_space_read_8(aa->aa_octuctl_bust, aa->aa_ioh,
81494889e9Sjmatthew UCTL_EHCI_CTL);
82494889e9Sjmatthew port_ctl &= ~UCTL_EHCI_CTL_L2C_ADDR_MSB_MASK;
83494889e9Sjmatthew port_ctl |= (1 << UCTL_EHCI_CTL_L2C_DESC_EMOD_SHIFT);
84494889e9Sjmatthew port_ctl |= (1 << UCTL_EHCI_CTL_L2C_BUFF_EMOD_SHIFT);
85494889e9Sjmatthew port_ctl |= UCTL_EHCI_CTL_EHCI_64B_ADDR_EN;
86494889e9Sjmatthew bus_space_write_8(aa->aa_octuctl_bust, aa->aa_ioh, UCTL_EHCI_CTL,
87494889e9Sjmatthew port_ctl);
88494889e9Sjmatthew
89494889e9Sjmatthew s = splhardusb();
90494889e9Sjmatthew sc->sc_ehci.sc_offs = EREAD1(&sc->sc_ehci, EHCI_CAPLENGTH);
91494889e9Sjmatthew EOWRITE2(&sc->sc_ehci, EHCI_USBINTR, 0);
92494889e9Sjmatthew
93494889e9Sjmatthew sc->sc_ehci.sc_id_vendor = 0;
94494889e9Sjmatthew strlcpy(sc->sc_ehci.sc_vendor, "Octeon", sizeof(sc->sc_ehci.sc_vendor));
95494889e9Sjmatthew
9628a985aaSvisa sc->sc_ih = octeon_intr_establish(CIU_INT_USB, IPL_USB | IPL_MPSAFE,
9728a985aaSvisa ehci_intr, (void *)&sc->sc_ehci, sc->sc_ehci.sc_bus.bdev.dv_xname);
98494889e9Sjmatthew KASSERT(sc->sc_ih != NULL);
99494889e9Sjmatthew
100494889e9Sjmatthew rc = ehci_init(&sc->sc_ehci);
101494889e9Sjmatthew if (rc != USBD_NORMAL_COMPLETION) {
102494889e9Sjmatthew printf(": init failed, error=%d\n", rc);
103494889e9Sjmatthew octeon_intr_disestablish(sc->sc_ih);
104494889e9Sjmatthew bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh,
105*fe5dbe47Sjmatthew aa->aa_reg.size);
106494889e9Sjmatthew splx(s);
107494889e9Sjmatthew return;
108494889e9Sjmatthew }
109494889e9Sjmatthew
110494889e9Sjmatthew printf("\n");
111494889e9Sjmatthew if (rc == 0)
112494889e9Sjmatthew config_found(self, &sc->sc_ehci.sc_bus, usbctlprint);
113494889e9Sjmatthew splx(s);
114494889e9Sjmatthew }
115