Home | History | Annotate | Line # | Download | only in dev
ohci_sbus.c revision 1.14
      1 /*	$NetBSD: ohci_sbus.c,v 1.14 2020/11/21 17:46:08 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ohci_sbus.c,v 1.14 2020/11/21 17:46:08 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/kmem.h>
     37 
     38 /* bus_dma */
     39 #include <sys/mbuf.h>
     40 #include <uvm/uvm_extern.h>
     41 
     42 #define _PLAYSTATION2_BUS_DMA_PRIVATE
     43 #include <machine/bus.h>
     44 #include <machine/autoconf.h>
     45 
     46 #include <mips/cpuregs.h>
     47 
     48 #include <dev/usb/usb.h>
     49 #include <dev/usb/usbdi.h>
     50 #include <dev/usb/usbdivar.h>
     51 #include <dev/usb/usb_mem.h>
     52 
     53 #include <dev/usb/ohcireg.h>
     54 #include <dev/usb/ohcivar.h>
     55 
     56 #include <playstation2/ee/sifvar.h>	/* DMA staff */
     57 #include <playstation2/ee/dmacvar.h>
     58 #include <playstation2/dev/sbusvar.h>
     59 
     60 #ifdef DEBUG
     61 #define STATIC
     62 #else
     63 #define STATIC static
     64 #endif
     65 
     66 #define	SBUS_OHCI_REGBASE	MIPS_PHYS_TO_KSEG1(0x1f801600)
     67 #define SBUS_OHCI_REGSIZE	0x1000
     68 
     69 STATIC int ohci_sbus_match(struct device *, struct cfdata *, void *);
     70 STATIC void ohci_sbus_attach(struct device *, struct device *, void *);
     71 
     72 STATIC void _ohci_sbus_map_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     73     bus_size_t, int);
     74 STATIC int _ohci_sbus_mem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
     75     bus_size_t, bus_dma_segment_t *, int, int *, int);
     76 STATIC void _ohci_sbus_mem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
     77 STATIC int _ohci_sbus_mem_map(bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
     78     void **, int);
     79 STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, void *, size_t);
     80 
     81 struct playstation2_bus_dma_tag ohci_bus_dma_tag = {
     82 	_bus_dmamap_create,
     83 	_bus_dmamap_destroy,
     84 	_bus_dmamap_load,
     85 	_bus_dmamap_load_mbuf,
     86 	_bus_dmamap_load_uio,
     87 	_bus_dmamap_load_raw,
     88 	_bus_dmamap_unload,
     89 	_ohci_sbus_map_sync,
     90 	_ohci_sbus_mem_alloc,
     91 	_ohci_sbus_mem_free,
     92 	_ohci_sbus_mem_map,
     93 	_ohci_sbus_mem_unmap,
     94 	_bus_dmamem_mmap,
     95 };
     96 
     97 struct ohci_dma_segment {
     98 	struct iopdma_segment ds_iopdma_seg;
     99 
    100 	LIST_ENTRY(ohci_dma_segment) ds_link;
    101 };
    102 
    103 struct ohci_sbus_softc {
    104 	struct ohci_softc sc;
    105 
    106 	LIST_HEAD(, ohci_dma_segment) sc_dmaseg_head;
    107 };
    108 
    109 CFATTACH_DECL_NEW(ohci_sbus, sizeof(struct ohci_sbus_softc),
    110     ohci_sbus_match, ohci_sbus_attach, NULL, NULL);
    111 
    112 int
    113 ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    114 {
    115 
    116 	return 1;
    117 }
    118 
    119 void
    120 ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
    121 {
    122 	struct ohci_sbus_softc *sc = device_private(self);
    123 
    124 	printf("\n");
    125 
    126 	sc->sc.sc_dev = self;
    127 	sc->sc.sc_bus.ub_hcpriv = sc;
    128 
    129 	sc->sc.iot = bus_space_create(0, "OHCI I/O space", SBUS_OHCI_REGBASE,
    130 	    SBUS_OHCI_REGSIZE);
    131 	sc->sc.ioh = SBUS_OHCI_REGBASE;
    132 
    133 	ohci_bus_dma_tag._dmachip_cookie = sc;
    134 	sc->sc.sc_bus.ub_dmatag = &ohci_bus_dma_tag;
    135 
    136 	/* Disable interrupts, so we don't can any spurious ones. */
    137 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    138 	    OHCI_ALL_INTRS);
    139 
    140 	sbus_intr_establish(SBUS_IRQ_USB, ohci_intr, sc);
    141 
    142 	/* IOP/EE DMA relay segment list */
    143 	LIST_INIT(&sc->sc_dmaseg_head);
    144 
    145 	int err = ohci_init(&sc->sc);
    146 
    147 	if (err) {
    148 		printf(": init failed. error=%d\n", err);
    149 		return;
    150 	}
    151 
    152 	/* Attach usb device. */
    153 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    154 }
    155 
    156 void
    157 _ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    158     bus_size_t len, int ops)
    159 {
    160 
    161 	dmac_sync_buffer(); /* XXX over flush */
    162 }
    163 
    164 int
    165 _ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    166     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    167     int flags)
    168 {
    169 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    170 	struct ohci_dma_segment *ds;
    171 	struct iopdma_segment *iopdma_seg;
    172 	int error;
    173 
    174 	KDASSERT(sc);
    175 	ds = kmem_intr_alloc(sizeof(struct ohci_dma_segment), KM_NOSLEEP);
    176 	if (ds == NULL)
    177 		return 1;
    178 	/*
    179 	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
    180 	 */
    181 	iopdma_seg = &ds->ds_iopdma_seg;
    182 	error = iopdma_allocate_buffer(iopdma_seg, size);
    183 
    184 	if (error) {
    185 		kmem_intr_free(ds, sizeof(*ds));
    186 		return 1;
    187 	}
    188 
    189 	segs[0].ds_len	  = iopdma_seg->size;
    190 	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
    191 	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;
    192 
    193 	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);
    194 
    195 	*rsegs = 1;
    196 
    197 	return 0;
    198 }
    199 
    200 void
    201 _ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    202 {
    203 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    204 	struct ohci_dma_segment *ds;
    205 	paddr_t addr = segs[0].ds_addr;
    206 
    207 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    208 	    ds = LIST_NEXT(ds, ds_link)) {
    209 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    210 			iopdma_free_buffer(&ds->ds_iopdma_seg);
    211 
    212 			LIST_REMOVE(ds, ds_link);
    213 			kmem_intr_free(ds, sizeof(*ds));
    214 			return;
    215 		}
    216 	}
    217 
    218 	panic("_dmamem_free: can't find corresponding handle.");
    219 	/* NOTREACHED */
    220 }
    221 
    222 int
    223 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
    224     void **kvap, int flags)
    225 {
    226 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    227 	struct ohci_dma_segment *ds;
    228 	paddr_t addr = segs[0].ds_addr;
    229 
    230 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    231 	    ds = LIST_NEXT(ds, ds_link)) {
    232 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    233 
    234 			*kvap = (void *)ds->ds_iopdma_seg.ee_vaddr;
    235 
    236 			return 0;
    237 		}
    238 	}
    239 
    240 	return 1;
    241 }
    242 
    243 void
    244 _ohci_sbus_mem_unmap(bus_dma_tag_t t, void *kva, size_t size)
    245 {
    246 	/* nothing to do */
    247 }
    248