Home | History | Annotate | Line # | Download | only in dev
plumohci.c revision 1.15
      1 /*	$NetBSD: plumohci.c,v 1.15 2016/04/23 10:15:29 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 UCHIYAMA Yasushi
      5  * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * USB Open Host Controller driver.
     32  *
     33  * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
     34  * USB spec: http://www.usb.org/developers/data/usb11.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: plumohci.c,v 1.15 2016/04/23 10:15:29 skrll Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/proc.h>
     45 #include <sys/queue.h>
     46 
     47 /* busdma */
     48 #include <sys/mbuf.h>
     49 #include <uvm/uvm_extern.h>
     50 
     51 #include <machine/bus.h>
     52 #include <machine/bus_dma_hpcmips.h>
     53 
     54 #include <dev/usb/usb.h>
     55 #include <dev/usb/usbdi.h>
     56 #include <dev/usb/usbdivar.h>
     57 #include <dev/usb/usb_mem.h>
     58 
     59 #include <dev/usb/ohcireg.h>
     60 #include <dev/usb/ohcivar.h>
     61 
     62 #include <hpcmips/tx/tx39var.h>
     63 #include <hpcmips/dev/plumvar.h>
     64 #include <hpcmips/dev/plumicuvar.h>
     65 #include <hpcmips/dev/plumpowervar.h>
     66 #include <hpcmips/dev/plumohcireg.h>
     67 
     68 int plumohci_match(device_t, cfdata_t, void *);
     69 void plumohci_attach(device_t, device_t, void *);
     70 int plumohci_intr(void *);
     71 
     72 void __plumohci_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
     73     bus_addr_t, bus_size_t, int);
     74 int __plumohci_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
     75     bus_size_t, bus_dma_segment_t *, int, int *, int);
     76 void __plumohci_dmamem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
     77 int __plumohci_dmamem_map(bus_dma_tag_t, bus_dma_segment_t *,
     78     int, size_t, void **, int);
     79 void __plumohci_dmamem_unmap(bus_dma_tag_t, void *, size_t);
     80 
     81 struct bus_dma_tag_hpcmips plumohci_bus_dma_tag = {
     82 	{
     83 		NULL,
     84 		{
     85 			_hpcmips_bd_map_create,
     86 			_hpcmips_bd_map_destroy,
     87 			_hpcmips_bd_map_load,
     88 			_hpcmips_bd_map_load_mbuf,
     89 			_hpcmips_bd_map_load_uio,
     90 			_hpcmips_bd_map_load_raw,
     91 			_hpcmips_bd_map_unload,
     92 			__plumohci_dmamap_sync,
     93 			__plumohci_dmamem_alloc,
     94 			__plumohci_dmamem_free,
     95 			__plumohci_dmamem_map,
     96 			__plumohci_dmamem_unmap,
     97 			_hpcmips_bd_mem_mmap,
     98 		},
     99 	},
    100 	NULL,
    101 };
    102 
    103 struct plumohci_shm {
    104 	bus_space_handle_t ps_bsh;
    105 	paddr_t	ps_paddr;
    106 	void *	ps_caddr;
    107 	size_t	ps_size;
    108 	LIST_ENTRY(plumohci_shm) ps_link;
    109 };
    110 
    111 struct plumohci_softc {
    112 	struct ohci_softc sc;
    113 	void *sc_ih;
    114 	void *sc_wakeih;
    115 
    116 	LIST_HEAD(, plumohci_shm) sc_shm_head;
    117 };
    118 
    119 CFATTACH_DECL_NEW(plumohci, sizeof(struct plumohci_softc),
    120     plumohci_match, plumohci_attach, NULL, NULL);
    121 
    122 int
    123 plumohci_match(device_t parent, cfdata_t match, void *aux)
    124 {
    125 	/* PLUM2 builtin OHCI module */
    126 
    127 	return 1;
    128 }
    129 
    130 void
    131 plumohci_attach(device_t parent, device_t self, void *aux)
    132 {
    133 	struct plumohci_softc *sc = device_private(self);
    134 	struct plum_attach_args *pa = aux;
    135 
    136 	sc->sc.sc_dev = self;
    137 	sc->sc.sc_bus.ub_hcpriv = sc;
    138 
    139 	sc->sc.iot = pa->pa_iot;
    140 	sc->sc.sc_bus.ub_dmatag = &plumohci_bus_dma_tag.bdt;
    141 	plumohci_bus_dma_tag._dmamap_chipset_v = sc;
    142 
    143 	/* Map I/O space */
    144 	if (bus_space_map(sc->sc.iot, PLUM_OHCI_REGBASE, OHCI_PAGE_SIZE,
    145 	    0, &sc->sc.ioh)) {
    146 		printf(": cannot map mem space\n");
    147 		return;
    148 	}
    149 
    150 	/* power up */
    151 	/*
    152 	 * in the case of PLUM2, UHOSTC uses the VRAM as the shared RAM
    153 	 * so establish power/clock of Video contoroller
    154 	 */
    155 	plum_power_establish(pa->pa_pc, PLUM_PWR_EXTPW1);
    156 	plum_power_establish(pa->pa_pc, PLUM_PWR_USB);
    157 
    158 	/* Disable interrupts, so we don't can any spurious ones. */
    159 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    160 	    OHCI_ALL_INTRS);
    161 
    162 	/* master enable */
    163 	sc->sc_ih = plum_intr_establish(pa->pa_pc, PLUM_INT_USB, IST_EDGE,
    164 	    IPL_USB, ohci_intr, sc);
    165 #if 0
    166 	/*
    167 	 *  enable the clock restart request interrupt
    168 	 *  (for USBSUSPEND state)
    169 	 */
    170 	sc->sc_wakeih = plum_intr_establish(pa->pa_pc, PLUM_INT_USBWAKE,
    171 	    IST_EDGE, IPL_USB,
    172 	    plumohci_intr, sc);
    173 #endif
    174 	/*
    175 	 * Shared memory list.
    176 	 */
    177 	LIST_INIT(&sc->sc_shm_head);
    178 
    179 	printf("\n");
    180 
    181 	int err = ohci_init(&sc->sc);
    182 
    183 	if (err) {
    184 		printf(": init failed, error=%d\n", err);
    185 
    186 		plum_intr_disestablish(pa->pa_pc, sc->sc_ih);
    187 		plum_intr_disestablish(pa->pa_pc, sc->sc_wakeih);
    188 
    189 		return;
    190 	}
    191 
    192 	/* Attach usb device. */
    193 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    194 }
    195 
    196 int
    197 plumohci_intr(void *arg)
    198 {
    199 	printf("Plum2 OHCI: wakeup intr\n");
    200 	return 0;
    201 }
    202 
    203 /*
    204  * Plum2 OHCI specific busdma routines.
    205  *	Plum2 OHCI shared buffer can't allocate on memory
    206  *	but V-RAM (busspace).
    207  */
    208 
    209 void
    210 __plumohci_dmamap_sync(bus_dma_tag_t tx, bus_dmamap_t map, bus_addr_t offset,
    211     bus_size_t len, int ops)
    212 {
    213 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
    214 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
    215 
    216 	/*
    217 	 * Flush the write buffer allocated on the V-RAM.
    218 	 * Accessing any host controller register flushs write buffer
    219 	 */
    220 	(void)bus_space_read_4(sc->sc.iot, sc->sc.ioh, OHCI_REVISION);
    221 }
    222 
    223 int
    224 __plumohci_dmamem_alloc(bus_dma_tag_t tx, bus_size_t size,
    225     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
    226     int nsegs, int *rsegs, int flags)
    227 {
    228 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
    229 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
    230 	struct plumohci_shm *ps;
    231 	bus_space_handle_t bsh;
    232 	paddr_t paddr;
    233 	void *caddr;
    234 	int error;
    235 
    236 	size = round_page(size);
    237 
    238 	/*
    239 	 * Allocate buffer from V-RAM area.
    240 	 */
    241 	error = bus_space_alloc(sc->sc.iot, PLUM_OHCI_SHMEMBASE,
    242 	    PLUM_OHCI_SHMEMBASE + PLUM_OHCI_SHMEMSIZE - 1,
    243 	    size, OHCI_PAGE_SIZE, 0, 0,
    244 	    (bus_addr_t *)(void *)&caddr, &bsh);
    245 	if (error)
    246 		return 1;
    247 
    248 	pmap_extract(pmap_kernel(), (vaddr_t)caddr, &paddr);
    249 
    250 	ps = malloc(sizeof(struct plumohci_shm), M_DEVBUF, M_NOWAIT);
    251 	if (ps == 0)
    252 		return 1;
    253 
    254 	ps->ps_bsh = bsh;
    255 	ps->ps_size = segs[0].ds_len = size;
    256 	ps->ps_paddr = segs[0].ds_addr = paddr;
    257 	ps->ps_caddr = caddr;
    258 
    259 	LIST_INSERT_HEAD(&sc->sc_shm_head, ps, ps_link);
    260 
    261 	*rsegs = 1;
    262 
    263 	return 0;
    264 }
    265 
    266 void
    267 __plumohci_dmamem_free(bus_dma_tag_t tx, bus_dma_segment_t *segs, int nsegs)
    268 {
    269 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
    270 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
    271 	struct plumohci_shm *ps;
    272 
    273 	for (ps = LIST_FIRST(&sc->sc_shm_head); ps;
    274 	    ps = LIST_NEXT(ps, ps_link)) {
    275 
    276 		if (ps->ps_paddr == segs[0].ds_addr) {
    277 			bus_space_free(sc->sc.iot, ps->ps_bsh, ps->ps_size);
    278 			LIST_REMOVE(ps, ps_link);
    279 			free(ps, M_DEVBUF);
    280 
    281 			return;
    282 		}
    283 	}
    284 
    285 	panic("__plumohci_dmamem_free: can't find corresponding handle.");
    286 	/* NOTREACHED */
    287 }
    288 
    289 int
    290 __plumohci_dmamem_map(bus_dma_tag_t tx, bus_dma_segment_t *segs, int nsegs,
    291     size_t size, void **kvap, int flags)
    292 {
    293 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
    294 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
    295 	struct plumohci_shm *ps;
    296 
    297 	for (ps = LIST_FIRST(&sc->sc_shm_head); ps;
    298 	    ps = LIST_NEXT(ps, ps_link)) {
    299 		if (ps->ps_paddr == segs[0].ds_addr) {
    300 
    301 			*kvap = ps->ps_caddr;
    302 
    303 			return 0;
    304 		}
    305 	}
    306 
    307 	return 1;
    308 }
    309 
    310 void
    311 __plumohci_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
    312 {
    313 	/* nothing to do */
    314 }
    315