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