Home | History | Annotate | Line # | Download | only in dev
ohci_sbus.c revision 1.1
      1 /*	$NetBSD: ohci_sbus.c,v 1.1 2001/10/16 15:38:34 uch 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 struct cfattach ohci_sbus_ca = {
    110 	sizeof(struct ohci_sbus_softc), ohci_sbus_match,
    111 	ohci_sbus_attach,
    112 };
    113 
    114 int
    115 ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    116 {
    117 
    118 	return (1);
    119 }
    120 
    121 void
    122 ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
    123 {
    124 	struct ohci_sbus_softc *sc = (void *)self;
    125 	usbd_status result;
    126 
    127 	printf("\n");
    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.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 	result = ohci_init(&sc->sc);
    146 
    147 	if (result != USBD_NORMAL_COMPLETION) {
    148 		printf(": init failed. error=%d\n", result);
    149 		return;
    150 	}
    151 
    152 	/* Attach usb device. */
    153 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    154 	    usbctlprint);
    155 }
    156 
    157 void
    158 _ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    159     bus_size_t len, int ops)
    160 {
    161 
    162 	dmac_sync_buffer(); /* XXX over flush */
    163 }
    164 
    165 int
    166 _ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    167     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    168     int flags)
    169 {
    170 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    171 	struct ohci_dma_segment *ds;
    172 	struct iopdma_segment *iopdma_seg;
    173 	int error;
    174 
    175 	KDASSERT(sc);
    176 	ds = malloc(sizeof(struct ohci_dma_segment), M_DEVBUF, M_NOWAIT);
    177 	if (ds == NULL)
    178 		return (1);
    179 	/*
    180 	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
    181 	 */
    182 	iopdma_seg = &ds->ds_iopdma_seg;
    183 	error = iopdma_allocate_buffer(iopdma_seg, size);
    184 
    185 	if (error) {
    186 		free(ds, M_DEVBUF);
    187 		return (1);
    188 	}
    189 
    190 	segs[0].ds_len	  = iopdma_seg->size;
    191 	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
    192 	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;
    193 
    194 	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);
    195 
    196 	*rsegs = 1;
    197 
    198 	return (0);
    199 }
    200 
    201 void
    202 _ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    203 {
    204 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    205 	struct ohci_dma_segment *ds;
    206 	paddr_t addr = segs[0].ds_addr;
    207 
    208 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    209 	    ds = LIST_NEXT(ds, ds_link)) {
    210 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    211 			iopdma_free_buffer(&ds->ds_iopdma_seg);
    212 
    213 			LIST_REMOVE(ds, ds_link);
    214 			free(ds, M_DEVBUF);
    215 			return;
    216 		}
    217 	}
    218 
    219 	panic("_dmamem_free: can't find corresponding handle.");
    220 	/* NOTREACHED */
    221 }
    222 
    223 int
    224 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
    225     caddr_t *kvap, int flags)
    226 {
    227 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
    228 	struct ohci_dma_segment *ds;
    229 	paddr_t addr = segs[0].ds_addr;
    230 
    231 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
    232 	    ds = LIST_NEXT(ds, ds_link)) {
    233 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
    234 
    235 			*kvap = (caddr_t)ds->ds_iopdma_seg.ee_vaddr;
    236 
    237 			return (0);
    238 		}
    239 	}
    240 
    241 	return (1);
    242 }
    243 
    244 void
    245 _ohci_sbus_mem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    246 {
    247 	/* nothing to do */
    248 }
    249