1*c7fb772bSthorpej /* $NetBSD: epohci.c,v 1.11 2021/08/07 16:18:43 thorpej Exp $ */
281551c0bSjoff
381551c0bSjoff /*-
481551c0bSjoff * Copyright (c) 2004 Jesse Off
581551c0bSjoff * All rights reserved.
681551c0bSjoff *
781551c0bSjoff * Redistribution and use in source and binary forms, with or without
881551c0bSjoff * modification, are permitted provided that the following conditions
981551c0bSjoff * are met:
1081551c0bSjoff * 1. Redistributions of source code must retain the above copyright
1181551c0bSjoff * notice, this list of conditions and the following disclaimer.
1281551c0bSjoff * 2. Redistributions in binary form must reproduce the above copyright
1381551c0bSjoff * notice, this list of conditions and the following disclaimer in the
1481551c0bSjoff * documentation and/or other materials provided with the distribution.
1581551c0bSjoff *
1681551c0bSjoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1781551c0bSjoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1881551c0bSjoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1981551c0bSjoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2081551c0bSjoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2181551c0bSjoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2281551c0bSjoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2381551c0bSjoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2481551c0bSjoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2581551c0bSjoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2681551c0bSjoff * SUCH DAMAGE.
2781551c0bSjoff */
2881551c0bSjoff
2981551c0bSjoff /*
3081551c0bSjoff * USB Open Host Controller driver.
3181551c0bSjoff *
3281551c0bSjoff * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
3381551c0bSjoff * USB spec: http://www.usb.org/developers/data/usb11.pdf
3481551c0bSjoff */
3581551c0bSjoff
3681551c0bSjoff #include <sys/cdefs.h>
37*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: epohci.c,v 1.11 2021/08/07 16:18:43 thorpej Exp $");
3881551c0bSjoff
3981551c0bSjoff #include <sys/param.h>
4081551c0bSjoff #include <sys/systm.h>
4181551c0bSjoff #include <sys/kernel.h>
4281551c0bSjoff #include <sys/device.h>
4381551c0bSjoff #include <sys/proc.h>
4481551c0bSjoff #include <sys/queue.h>
4581551c0bSjoff
4681551c0bSjoff /* busdma */
4781551c0bSjoff #include <sys/mbuf.h>
4881551c0bSjoff #include <uvm/uvm_extern.h>
4981551c0bSjoff
50cf10107dSdyoung #include <sys/bus.h>
5181551c0bSjoff
5281551c0bSjoff #include <dev/usb/usb.h>
5381551c0bSjoff #include <dev/usb/usbdi.h>
5481551c0bSjoff #include <dev/usb/usbdivar.h>
5581551c0bSjoff #include <dev/usb/usb_mem.h>
5681551c0bSjoff
5781551c0bSjoff #include <dev/usb/ohcireg.h>
5881551c0bSjoff #include <dev/usb/ohcivar.h>
5981551c0bSjoff
6081551c0bSjoff #include <arm/ep93xx/ep93xxreg.h>
6181551c0bSjoff #include <arm/ep93xx/ep93xxvar.h>
6281551c0bSjoff #include <arm/ep93xx/epsocvar.h>
6381551c0bSjoff
64cbab9cadSchs int epohci_match(device_t, cfdata_t, void *);
65cbab9cadSchs void epohci_attach(device_t, device_t, void *);
66cbab9cadSchs void epohci_callback(device_t);
6781551c0bSjoff
6881551c0bSjoff struct epohci_softc {
6981551c0bSjoff struct ohci_softc sc;
7081551c0bSjoff void *sc_ih;
7181551c0bSjoff int sc_intr;
7281551c0bSjoff };
7381551c0bSjoff
7446f8b823Sdrochner CFATTACH_DECL_NEW(epohci, sizeof(struct epohci_softc),
7581551c0bSjoff epohci_match, epohci_attach, NULL, NULL);
7681551c0bSjoff
7781551c0bSjoff int
epohci_match(device_t parent,cfdata_t match,void * aux)78cbab9cadSchs epohci_match(device_t parent, cfdata_t match, void *aux)
7981551c0bSjoff {
8081551c0bSjoff /* EP93xx builtin OHCI module */
8181551c0bSjoff
824e8e6643Sskrll return 1;
8381551c0bSjoff }
8481551c0bSjoff
8581551c0bSjoff void
epohci_attach(device_t parent,device_t self,void * aux)86cbab9cadSchs epohci_attach(device_t parent, device_t self, void *aux)
8781551c0bSjoff {
8846f8b823Sdrochner struct epohci_softc *sc = device_private(self);
8981551c0bSjoff struct epsoc_attach_args *sa = aux;
9008a4aba7Sskrll uint32_t i;
9181551c0bSjoff bus_space_handle_t syscon_ioh;
9281551c0bSjoff
9346f8b823Sdrochner sc->sc.sc_dev = self;
944e8e6643Sskrll sc->sc.sc_bus.ub_hcpriv = sc;
9546f8b823Sdrochner
9681551c0bSjoff sc->sc.iot = sa->sa_iot;
974e8e6643Sskrll sc->sc.sc_bus.ub_dmatag = sa->sa_dmat;
9881551c0bSjoff sc->sc_intr = sa->sa_intr;
9981551c0bSjoff
10081551c0bSjoff /* Map I/O space */
10181551c0bSjoff if (bus_space_map(sc->sc.iot, sa->sa_addr, sa->sa_size,
10281551c0bSjoff 0, &sc->sc.ioh)) {
10381551c0bSjoff printf(": cannot map mem space\n");
10481551c0bSjoff return;
10581551c0bSjoff }
10681551c0bSjoff
10781551c0bSjoff /* Enable hclk clock gating to the USB block. */
10881551c0bSjoff
10981551c0bSjoff bus_space_map(sc->sc.iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
11081551c0bSjoff EP93XX_APB_SYSCON_SIZE, 0, &syscon_ioh);
11181551c0bSjoff i = bus_space_read_4(sc->sc.iot, syscon_ioh, EP93XX_SYSCON_PwrCnt);
11281551c0bSjoff i |= 0x10000000;
11381551c0bSjoff bus_space_write_4(sc->sc.iot, syscon_ioh, EP93XX_SYSCON_PwrCnt, i);
11481551c0bSjoff
11581551c0bSjoff /*
11681551c0bSjoff * Not sure if I understand the datasheet here, but we must wait a
11781551c0bSjoff * few instructions and also make certain PLL2 is stable before
11881551c0bSjoff * continuing. Hopefully, the check for PLL2 is enough, as the
11981551c0bSjoff * datasheet is elusive to actually how many insns we need.
12081551c0bSjoff */
12181551c0bSjoff
12281551c0bSjoff do {
12381551c0bSjoff i = bus_space_read_4(sc->sc.iot, syscon_ioh,
12481551c0bSjoff EP93XX_SYSCON_PwrSts);
12581551c0bSjoff } while ((i & 0x100) == 0);
12681551c0bSjoff bus_space_unmap(sc->sc.iot, syscon_ioh, EP93XX_APB_SYSCON_SIZE);
12781551c0bSjoff
12881551c0bSjoff printf("\n");
12981551c0bSjoff
13081551c0bSjoff /* Defer the rest until later */
13181551c0bSjoff config_defer(self, epohci_callback);
13281551c0bSjoff }
13381551c0bSjoff
13481551c0bSjoff void
epohci_callback(device_t self)135cbab9cadSchs epohci_callback(device_t self)
13681551c0bSjoff {
13746f8b823Sdrochner struct epohci_softc *sc = device_private(self);
13881551c0bSjoff
13981551c0bSjoff /* Disable interrupts, so we don't get any spurious ones. */
14081551c0bSjoff bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
14181551c0bSjoff OHCI_ALL_INTRS);
14281551c0bSjoff
14381551c0bSjoff sc->sc_ih = ep93xx_intr_establish(sc->sc_intr, IPL_USB,
14481551c0bSjoff ohci_intr, sc);
1454e8e6643Sskrll int err = ohci_init(&sc->sc);
14681551c0bSjoff
1474e8e6643Sskrll if (err) {
1484e8e6643Sskrll printf("%s: init failed, error=%d\n", device_xname(self), err);
14981551c0bSjoff
15081551c0bSjoff ep93xx_intr_disestablish(sc->sc_ih);
15181551c0bSjoff return;
15281551c0bSjoff }
15381551c0bSjoff
15481551c0bSjoff /* Attach usb device. */
1552685996bSthorpej sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
156*c7fb772bSthorpej CFARGS_NONE);
15781551c0bSjoff
15881551c0bSjoff }
159