Home | History | Annotate | Line # | Download | only in dev
ohci_sbus.c revision 1.3
      1 /*	$NetBSD: ohci_sbus.c,v 1.3 2002/10/02 04:17:21 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 
     41 /* bus_dma */
     42 #include <sys/mbuf.h>
     43 #include <uvm/uvm_extern.h>
     44 
     45 #define _PLAYSTATION2_BUS_DMA_PRIVATE
     46 #include <machine/bus.h>
     47 #include <machine/autoconf.h>
     48 
     49 #include <dev/usb/usb.h>
     50 #include <dev/usb/usbdi.h>
     51 #include <dev/usb/usbdivar.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     caddr_t *, int);
     79 STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, caddr_t, 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(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 = (void *)self;
    123 	usbd_status result;
    124 
    125 	printf("\n");
    126 
    127 	sc->sc.iot = bus_space_create(0, "OHCI I/O space", SBUS_OHCI_REGBASE,
    128 	    SBUS_OHCI_REGSIZE);
    129 	sc->sc.ioh = SBUS_OHCI_REGBASE;
    130 
    131 	ohci_bus_dma_tag._dmachip_cookie = sc;
    132 	sc->sc.sc_bus.dmatag = &ohci_bus_dma_tag;
    133 
    134 	/* Disable interrupts, so we don't can any spurious ones. */
    135 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    136 	    OHCI_ALL_INTRS);
    137 
    138 	sbus_intr_establish(SBUS_IRQ_USB, ohci_intr, sc);
    139 
    140 	/* IOP/EE DMA relay segment list */
    141 	LIST_INIT(&sc->sc_dmaseg_head);
    142 
    143 	result = ohci_init(&sc->sc);
    144 
    145 	if (result != USBD_NORMAL_COMPLETION) {
    146 		printf(": init failed. error=%d\n", result);
    147 		return;
    148 	}
    149 
    150 	/* Attach usb device. */
    151 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    152 	    usbctlprint);
    153 }
    154 
    155 void
    156 _ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    157     bus_size_t len, int ops)
    158 {
    159 
    160 	dmac_sync_buffer(); /* XXX over flush */
    161 }
    162 
    163 int
    164 _ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    165     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    166     int flags)
    167 {
    168 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    169 	struct ohci_dma_segment *ds;
    170 	struct iopdma_segment *iopdma_seg;
    171 	int error;
    172 
    173 	KDASSERT(sc);
    174 	ds = malloc(sizeof(struct ohci_dma_segment), M_DEVBUF, M_NOWAIT);
    175 	if (ds == NULL)
    176 		return (1);
    177 	/*
    178 	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
    179 	 */
    180 	iopdma_seg = &ds->ds_iopdma_seg;
    181 	error = iopdma_allocate_buffer(iopdma_seg, size);
    182 
    183 	if (error) {
    184 		free(ds, M_DEVBUF);
    185 		return (1);
    186 	}
    187 
    188 	segs[0].ds_len	  = iopdma_seg->size;
    189 	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
    190 	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;
    191 
    192 	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);
    193 
    194 	*rsegs = 1;
    195 
    196 	return (0);
    197 }
    198 
    199 void
    200 _ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    201 {
    202 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    203 	struct ohci_dma_segment *ds;
    204 	paddr_t addr = segs[0].ds_addr;
    205 
    206 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    207 	    ds = LIST_NEXT(ds, ds_link)) {
    208 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    209 			iopdma_free_buffer(&ds->ds_iopdma_seg);
    210 
    211 			LIST_REMOVE(ds, ds_link);
    212 			free(ds, M_DEVBUF);
    213 			return;
    214 		}
    215 	}
    216 
    217 	panic("_dmamem_free: can't find corresponding handle.");
    218 	/* NOTREACHED */
    219 }
    220 
    221 int
    222 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
    223     caddr_t *kvap, int flags)
    224 {
    225 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    226 	struct ohci_dma_segment *ds;
    227 	paddr_t addr = segs[0].ds_addr;
    228 
    229 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    230 	    ds = LIST_NEXT(ds, ds_link)) {
    231 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    232 
    233 			*kvap = (caddr_t)ds->ds_iopdma_seg.ee_vaddr;
    234 
    235 			return (0);
    236 		}
    237 	}
    238 
    239 	return (1);
    240 }
    241 
    242 void
    243 _ohci_sbus_mem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    244 {
    245 	/* nothing to do */
    246 }
    247