1*c7fb772bSthorpej /* $NetBSD: gcscehci.c,v 1.15 2021/08/07 16:18:55 thorpej Exp $ */
2424b422cSjmcneill
3424b422cSjmcneill /*
4424b422cSjmcneill * Copyright (c) 2001, 2002, 2007 The NetBSD Foundation, Inc.
5424b422cSjmcneill * All rights reserved.
6424b422cSjmcneill *
7424b422cSjmcneill * This code is derived from software contributed to The NetBSD Foundation
8424b422cSjmcneill * by Lennart Augustsson (lennart@augustsson.net)
9424b422cSjmcneill * and Jared D. McNeill (jmcneill@invisible.ca)
10424b422cSjmcneill *
11424b422cSjmcneill * Redistribution and use in source and binary forms, with or without
12424b422cSjmcneill * modification, are permitted provided that the following conditions
13424b422cSjmcneill * are met:
14424b422cSjmcneill * 1. Redistributions of source code must retain the above copyright
15424b422cSjmcneill * notice, this list of conditions and the following disclaimer.
16424b422cSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
17424b422cSjmcneill * notice, this list of conditions and the following disclaimer in the
18424b422cSjmcneill * documentation and/or other materials provided with the distribution.
19424b422cSjmcneill *
20424b422cSjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21424b422cSjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22424b422cSjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23424b422cSjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24424b422cSjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25424b422cSjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26424b422cSjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27424b422cSjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28424b422cSjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29424b422cSjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30424b422cSjmcneill * POSSIBILITY OF SUCH DAMAGE.
31424b422cSjmcneill */
32424b422cSjmcneill
33424b422cSjmcneill #include <sys/cdefs.h>
34*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: gcscehci.c,v 1.15 2021/08/07 16:18:55 thorpej Exp $");
35424b422cSjmcneill
36424b422cSjmcneill #include <sys/param.h>
37424b422cSjmcneill #include <sys/systm.h>
38424b422cSjmcneill #include <sys/kernel.h>
39424b422cSjmcneill #include <sys/device.h>
40424b422cSjmcneill #include <sys/proc.h>
41424b422cSjmcneill #include <sys/queue.h>
42424b422cSjmcneill
43f5b064eeSdyoung #include <sys/bus.h>
44424b422cSjmcneill #include <machine/cpufunc.h>
45424b422cSjmcneill
46424b422cSjmcneill #include <dev/pci/pcidevs.h>
47424b422cSjmcneill #include <dev/pci/pcivar.h>
48424b422cSjmcneill #include <dev/pci/usb_pci.h>
49424b422cSjmcneill
50424b422cSjmcneill #include <dev/usb/usb.h>
51424b422cSjmcneill #include <dev/usb/usbdi.h>
52424b422cSjmcneill #include <dev/usb/usbdivar.h>
53424b422cSjmcneill #include <dev/usb/usb_mem.h>
54424b422cSjmcneill
55424b422cSjmcneill #include <dev/usb/ehcireg.h>
56424b422cSjmcneill #include <dev/usb/ehcivar.h>
57424b422cSjmcneill
58424b422cSjmcneill #ifdef EHCI_DEBUG
59424b422cSjmcneill #define DPRINTF(x) if (ehcidebug) printf x
60424b422cSjmcneill extern int ehcidebug;
61424b422cSjmcneill #else
62424b422cSjmcneill #define DPRINTF(x)
63424b422cSjmcneill #endif
64424b422cSjmcneill
65424b422cSjmcneill #define GCSCUSB_MSR_BASE 0x51200000
66424b422cSjmcneill #define GCSCUSB_MSR_EHCB (GCSCUSB_MSR_BASE + 0x09)
67424b422cSjmcneill
68424b422cSjmcneill struct gcscehci_softc {
69424b422cSjmcneill ehci_softc_t sc;
70424b422cSjmcneill pci_chipset_tag_t sc_pc;
71424b422cSjmcneill pcitag_t sc_tag;
72424b422cSjmcneill void *sc_ih; /* interrupt vectoring */
73424b422cSjmcneill };
74424b422cSjmcneill
75424b422cSjmcneill static int
gcscehci_match(device_t parent,cfdata_t match,void * aux)76ad6eeaadScegger gcscehci_match(device_t parent, cfdata_t match, void *aux)
77424b422cSjmcneill {
78424b422cSjmcneill struct pci_attach_args *pa = (struct pci_attach_args *) aux;
79424b422cSjmcneill
80424b422cSjmcneill if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
81424b422cSjmcneill PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
82424b422cSjmcneill PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI &&
83424b422cSjmcneill PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
84424b422cSjmcneill PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_CS5536_EHCI)
854e8e6643Sskrll return 10; /* beat ehci_pci */
86424b422cSjmcneill
874e8e6643Sskrll return 0;
88424b422cSjmcneill }
89424b422cSjmcneill
90424b422cSjmcneill static void
gcscehci_attach(device_t parent,device_t self,void * aux)91ad6eeaadScegger gcscehci_attach(device_t parent, device_t self, void *aux)
92424b422cSjmcneill {
93fd0ded75Sdrochner struct gcscehci_softc *sc = device_private(self);
94424b422cSjmcneill struct pci_attach_args *pa = (struct pci_attach_args *)aux;
95424b422cSjmcneill pci_chipset_tag_t pc = pa->pa_pc;
96424b422cSjmcneill pcitag_t tag = pa->pa_tag;
97424b422cSjmcneill char const *intrstr;
98424b422cSjmcneill pci_intr_handle_t ih;
99fd0ded75Sdrochner const char *devname = device_xname(self);
100424b422cSjmcneill char devinfo[256];
101424b422cSjmcneill bus_addr_t ehcibase;
102424b422cSjmcneill int ncomp;
103424b422cSjmcneill struct usb_pci *up;
104e58a356cSchristos char buf[PCI_INTRSTR_LEN];
105424b422cSjmcneill
106fd0ded75Sdrochner sc->sc.sc_dev = self;
1074e8e6643Sskrll sc->sc.sc_bus.ub_hcpriv = sc;
108fd0ded75Sdrochner
109424b422cSjmcneill aprint_naive(": USB controller\n");
110424b422cSjmcneill
111424b422cSjmcneill pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
112424b422cSjmcneill aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
113424b422cSjmcneill PCI_REVISION(pa->pa_class));
114424b422cSjmcneill
115424b422cSjmcneill /* Map I/O registers */
116424b422cSjmcneill ehcibase = rdmsr(GCSCUSB_MSR_EHCB) & 0xffffff00;
117424b422cSjmcneill sc->sc.iot = pa->pa_memt;
118424b422cSjmcneill sc->sc.sc_size = 256;
119424b422cSjmcneill if (bus_space_map(sc->sc.iot, ehcibase, 256, 0, &sc->sc.ioh)) {
120424b422cSjmcneill aprint_error("%s: can't map memory space\n", devname);
121424b422cSjmcneill return;
122424b422cSjmcneill }
123424b422cSjmcneill
124424b422cSjmcneill sc->sc_pc = pc;
125424b422cSjmcneill sc->sc_tag = tag;
1264e8e6643Sskrll sc->sc.sc_bus.ub_dmatag = pa->pa_dmat;
127424b422cSjmcneill
128424b422cSjmcneill /* Disable interrupts, so we don't get any spurious ones. */
129424b422cSjmcneill sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
130424b422cSjmcneill DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
131424b422cSjmcneill EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
132424b422cSjmcneill
133424b422cSjmcneill /* Map and establish the interrupt. */
134424b422cSjmcneill if (pci_intr_map(pa, &ih)) {
135424b422cSjmcneill aprint_error("%s: couldn't map interrupt\n", devname);
136424b422cSjmcneill return;
137424b422cSjmcneill }
138e58a356cSchristos intrstr = pci_intr_string(pc, ih, buf, sizeof(buf));
139424b422cSjmcneill sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
140424b422cSjmcneill if (sc->sc_ih == NULL) {
141424b422cSjmcneill aprint_error("%s: couldn't establish interrupt", devname);
142424b422cSjmcneill if (intrstr != NULL)
14374341862Snjoly aprint_error(" at %s", intrstr);
14474341862Snjoly aprint_error("\n");
145424b422cSjmcneill return;
146424b422cSjmcneill }
147424b422cSjmcneill aprint_normal("%s: interrupting at %s\n", devname, intrstr);
148424b422cSjmcneill
1494e8e6643Sskrll sc->sc.sc_bus.ub_revision = USBREV_2_0;
150424b422cSjmcneill
151424b422cSjmcneill /*
152424b422cSjmcneill * Find companion controllers. According to the spec they always
153424b422cSjmcneill * have lower function numbers so they should be enumerated already.
154424b422cSjmcneill */
155424b422cSjmcneill ncomp = 0;
156424b422cSjmcneill TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
157424b422cSjmcneill if (up->bus == pa->pa_bus && up->device == pa->pa_device) {
158424b422cSjmcneill DPRINTF(("gcscehci_attach: companion %s\n",
159fd0ded75Sdrochner device_xname(up->usb)));
160424b422cSjmcneill sc->sc.sc_comps[ncomp++] = up->usb;
161424b422cSjmcneill if (ncomp >= EHCI_COMPANION_MAX)
162424b422cSjmcneill break;
163424b422cSjmcneill }
164424b422cSjmcneill }
165424b422cSjmcneill sc->sc.sc_ncomp = ncomp;
166424b422cSjmcneill
1674e8e6643Sskrll int err = ehci_init(&sc->sc);
1684e8e6643Sskrll if (err) {
1694e8e6643Sskrll aprint_error("%s: init failed, error=%d\n", devname, err);
170424b422cSjmcneill return;
171424b422cSjmcneill }
172424b422cSjmcneill
173424b422cSjmcneill /* Attach usb device. */
1742685996bSthorpej sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
175*c7fb772bSthorpej CFARGS_NONE);
176424b422cSjmcneill }
177424b422cSjmcneill
178fd0ded75Sdrochner CFATTACH_DECL_NEW(gcscehci, sizeof(struct gcscehci_softc),
179424b422cSjmcneill gcscehci_match, gcscehci_attach, NULL, ehci_activate);
180