Home | History | Annotate | Line # | Download | only in dev
ohci_sbus.c revision 1.12
      1 /*	$NetBSD: ohci_sbus.c,v 1.12 2016/04/23 10:15:30 skrll 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.12 2016/04/23 10:15:30 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 /* bus_dma */
     38 #include <sys/mbuf.h>
     39 #include <uvm/uvm_extern.h>
     40 
     41 #define _PLAYSTATION2_BUS_DMA_PRIVATE
     42 #include <machine/bus.h>
     43 #include <machine/autoconf.h>
     44 
     45 #include <dev/usb/usb.h>
     46 #include <dev/usb/usbdi.h>
     47 #include <dev/usb/usbdivar.h>
     48 #include <dev/usb/usb_mem.h>
     49 
     50 #include <dev/usb/ohcireg.h>
     51 #include <dev/usb/ohcivar.h>
     52 
     53 #include <playstation2/ee/sifvar.h>	/* DMA staff */
     54 #include <playstation2/ee/dmacvar.h>
     55 #include <playstation2/dev/sbusvar.h>
     56 
     57 #ifdef DEBUG
     58 #define STATIC
     59 #else
     60 #define STATIC static
     61 #endif
     62 
     63 #define	SBUS_OHCI_REGBASE	MIPS_PHYS_TO_KSEG1(0x1f801600)
     64 #define SBUS_OHCI_REGSIZE	0x1000
     65 
     66 STATIC int ohci_sbus_match(struct device *, struct cfdata *, void *);
     67 STATIC void ohci_sbus_attach(struct device *, struct device *, void *);
     68 
     69 STATIC void _ohci_sbus_map_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     70     bus_size_t, int);
     71 STATIC int _ohci_sbus_mem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
     72     bus_size_t, bus_dma_segment_t *, int, int *, int);
     73 STATIC void _ohci_sbus_mem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
     74 STATIC int _ohci_sbus_mem_map(bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
     75     void **, int);
     76 STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, void *, size_t);
     77 
     78 struct playstation2_bus_dma_tag ohci_bus_dma_tag = {
     79 	_bus_dmamap_create,
     80 	_bus_dmamap_destroy,
     81 	_bus_dmamap_load,
     82 	_bus_dmamap_load_mbuf,
     83 	_bus_dmamap_load_uio,
     84 	_bus_dmamap_load_raw,
     85 	_bus_dmamap_unload,
     86 	_ohci_sbus_map_sync,
     87 	_ohci_sbus_mem_alloc,
     88 	_ohci_sbus_mem_free,
     89 	_ohci_sbus_mem_map,
     90 	_ohci_sbus_mem_unmap,
     91 	_bus_dmamem_mmap,
     92 };
     93 
     94 struct ohci_dma_segment {
     95 	struct iopdma_segment ds_iopdma_seg;
     96 
     97 	LIST_ENTRY(ohci_dma_segment) ds_link;
     98 };
     99 
    100 struct ohci_sbus_softc {
    101 	struct ohci_softc sc;
    102 
    103 	LIST_HEAD(, ohci_dma_segment) sc_dmaseg_head;
    104 };
    105 
    106 CFATTACH_DECL_NEW(ohci_sbus, sizeof(struct ohci_sbus_softc),
    107     ohci_sbus_match, ohci_sbus_attach, NULL, NULL);
    108 
    109 int
    110 ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    111 {
    112 
    113 	return 1;
    114 }
    115 
    116 void
    117 ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
    118 {
    119 	struct ohci_sbus_softc *sc = device_private(self);
    120 
    121 	printf("\n");
    122 
    123 	sc->sc.sc_dev = self;
    124 	sc->sc.sc_bus.ub_hcpriv = sc;
    125 
    126 	sc->sc.iot = bus_space_create(0, "OHCI I/O space", SBUS_OHCI_REGBASE,
    127 	    SBUS_OHCI_REGSIZE);
    128 	sc->sc.ioh = SBUS_OHCI_REGBASE;
    129 
    130 	ohci_bus_dma_tag._dmachip_cookie = sc;
    131 	sc->sc.sc_bus.ub_dmatag = &ohci_bus_dma_tag;
    132 
    133 	/* Disable interrupts, so we don't can any spurious ones. */
    134 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    135 	    OHCI_ALL_INTRS);
    136 
    137 	sbus_intr_establish(SBUS_IRQ_USB, ohci_intr, sc);
    138 
    139 	/* IOP/EE DMA relay segment list */
    140 	LIST_INIT(&sc->sc_dmaseg_head);
    141 
    142 	int err = ohci_init(&sc->sc);
    143 
    144 	if (err) {
    145 		printf(": init failed. error=%d\n", err);
    146 		return;
    147 	}
    148 
    149 	/* Attach usb device. */
    150 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    151 }
    152 
    153 void
    154 _ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    155     bus_size_t len, int ops)
    156 {
    157 
    158 	dmac_sync_buffer(); /* XXX over flush */
    159 }
    160 
    161 int
    162 _ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    163     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    164     int flags)
    165 {
    166 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    167 	struct ohci_dma_segment *ds;
    168 	struct iopdma_segment *iopdma_seg;
    169 	int error;
    170 
    171 	KDASSERT(sc);
    172 	ds = malloc(sizeof(struct ohci_dma_segment), M_DEVBUF, M_NOWAIT);
    173 	if (ds == NULL)
    174 		return 1;
    175 	/*
    176 	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
    177 	 */
    178 	iopdma_seg = &ds->ds_iopdma_seg;
    179 	error = iopdma_allocate_buffer(iopdma_seg, size);
    180 
    181 	if (error) {
    182 		free(ds, M_DEVBUF);
    183 		return 1;
    184 	}
    185 
    186 	segs[0].ds_len	  = iopdma_seg->size;
    187 	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
    188 	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;
    189 
    190 	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);
    191 
    192 	*rsegs = 1;
    193 
    194 	return 0;
    195 }
    196 
    197 void
    198 _ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    199 {
    200 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    201 	struct ohci_dma_segment *ds;
    202 	paddr_t addr = segs[0].ds_addr;
    203 
    204 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    205 	    ds = LIST_NEXT(ds, ds_link)) {
    206 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    207 			iopdma_free_buffer(&ds->ds_iopdma_seg);
    208 
    209 			LIST_REMOVE(ds, ds_link);
    210 			free(ds, M_DEVBUF);
    211 			return;
    212 		}
    213 	}
    214 
    215 	panic("_dmamem_free: can't find corresponding handle.");
    216 	/* NOTREACHED */
    217 }
    218 
    219 int
    220 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
    221     void **kvap, int flags)
    222 {
    223 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    224 	struct ohci_dma_segment *ds;
    225 	paddr_t addr = segs[0].ds_addr;
    226 
    227 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    228 	    ds = LIST_NEXT(ds, ds_link)) {
    229 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    230 
    231 			*kvap = (void *)ds->ds_iopdma_seg.ee_vaddr;
    232 
    233 			return 0;
    234 		}
    235 	}
    236 
    237 	return 1;
    238 }
    239 
    240 void
    241 _ohci_sbus_mem_unmap(bus_dma_tag_t t, void *kva, size_t size)
    242 {
    243 	/* nothing to do */
    244 }
    245