1 /* $OpenBSD: octohci.c,v 1.4 2019/01/07 03:41:06 dlg Exp $ */
2
3 /*
4 * Copyright (c) 2015 Jonathan Matthew <jmatthew@openbsd.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22
23 #include <machine/intr.h>
24 #include <machine/bus.h>
25 #include <machine/octeonreg.h>
26 #include <machine/octeonvar.h>
27
28 #include <octeon/dev/octuctlreg.h>
29 #include <octeon/dev/octuctlvar.h>
30
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdi.h>
33 #include <dev/usb/usbdivar.h>
34
35 #include <dev/usb/ohcireg.h>
36 #include <dev/usb/ohcivar.h>
37
38 struct octohci_softc {
39 struct ohci_softc sc_ohci;
40
41 void *sc_ih;
42 uint64_t sc_reg_size;
43 };
44
45 int octohci_match(struct device *, void *, void *);
46 void octohci_attach(struct device *, struct device *, void *);
47 void octohci_attach_deferred(struct device *);
48
49 const struct cfattach octohci_ca = {
50 sizeof(struct octohci_softc), octohci_match, octohci_attach,
51 };
52
53 struct cfdriver octohci_cd = {
54 NULL, "ohci", DV_DULL
55 };
56
57 int
octohci_match(struct device * parent,void * match,void * aux)58 octohci_match(struct device *parent, void *match, void *aux)
59 {
60 struct octuctl_attach_args *aa = aux;
61 return (OF_is_compatible(aa->aa_node, "cavium,octeon-6335-ohci"));
62 }
63
64 void
octohci_attach(struct device * parent,struct device * self,void * aux)65 octohci_attach(struct device *parent, struct device *self, void *aux)
66 {
67 struct octohci_softc *sc = (struct octohci_softc *)self;
68 struct octuctl_attach_args *aa = aux;
69 uint64_t port_ctl;
70 int rc;
71 int s;
72
73 sc->sc_ohci.iot = aa->aa_bust;
74 sc->sc_ohci.sc_bus.pipe_size = sizeof(struct usbd_pipe);
75 sc->sc_ohci.sc_bus.dmatag = aa->aa_dmat;
76
77 rc = bus_space_map(sc->sc_ohci.iot, aa->aa_reg.addr, aa->aa_reg.size,
78 0, &sc->sc_ohci.ioh);
79 KASSERT(rc == 0);
80 sc->sc_reg_size = aa->aa_reg.size;
81
82 port_ctl = bus_space_read_8(aa->aa_octuctl_bust, aa->aa_ioh,
83 UCTL_OHCI_CTL);
84 port_ctl &= ~UCTL_OHCI_CTL_L2C_ADDR_MSB_MASK;
85 port_ctl |= (1 << UCTL_OHCI_CTL_L2C_DESC_EMOD_SHIFT);
86 port_ctl |= (1 << UCTL_OHCI_CTL_L2C_BUFF_EMOD_SHIFT);
87 bus_space_write_8(aa->aa_octuctl_bust, aa->aa_ioh, UCTL_OHCI_CTL,
88 port_ctl);
89
90 s = splusb();
91
92 sc->sc_ohci.sc_id_vendor = 0;
93 strlcpy(sc->sc_ohci.sc_vendor, "Octeon", sizeof(sc->sc_ohci.sc_vendor));
94
95 sc->sc_ih = octeon_intr_establish(CIU_INT_USB, IPL_USB, ohci_intr,
96 (void *)&sc->sc_ohci, sc->sc_ohci.sc_bus.bdev.dv_xname);
97 KASSERT(sc->sc_ih != NULL);
98
99 printf(", ");
100
101 if ((ohci_checkrev(&sc->sc_ohci) != USBD_NORMAL_COMPLETION) ||
102 (ohci_handover(&sc->sc_ohci) != USBD_NORMAL_COMPLETION))
103 goto failed;
104
105 /* ignore interrupts for now */
106 sc->sc_ohci.sc_bus.dying = 1;
107 config_defer(self, octohci_attach_deferred);
108
109 splx(s);
110 return;
111
112 failed:
113 octeon_intr_disestablish(sc->sc_ih);
114 bus_space_unmap(sc->sc_ohci.iot, sc->sc_ohci.ioh, sc->sc_reg_size);
115 splx(s);
116 return;
117 }
118
119 void
octohci_attach_deferred(struct device * self)120 octohci_attach_deferred(struct device *self)
121 {
122 struct octohci_softc *sc = (struct octohci_softc *)self;
123 usbd_status r;
124 int s;
125
126 s = splusb();
127 sc->sc_ohci.sc_bus.dying = 0;
128
129 r = ohci_init(&sc->sc_ohci);
130 splx(s);
131
132 if (r != USBD_NORMAL_COMPLETION) {
133 printf("%s: init failed, error=%d\n",
134 sc->sc_ohci.sc_bus.bdev.dv_xname, r);
135 octeon_intr_disestablish(sc->sc_ih);
136 bus_space_unmap(sc->sc_ohci.iot, sc->sc_ohci.ioh,
137 sc->sc_reg_size);
138 } else {
139 config_found(self, &sc->sc_ohci.sc_bus, usbctlprint);
140 }
141 }
142