xref: /netbsd-src/sys/arch/playstation2/dev/ohci_sbus.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ohci_sbus.c,v 1.11 2014/03/31 11:25:49 martin 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.11 2014/03/31 11:25:49 martin 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 	usbd_status result;
121 
122 	printf("\n");
123 
124 	sc->sc.sc_dev = self;
125 	sc->sc.sc_bus.hci_private = sc;
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(self, &sc->sc.sc_bus, usbctlprint);
152 }
153 
154 void
155 _ohci_sbus_map_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
156     bus_size_t len, int ops)
157 {
158 
159 	dmac_sync_buffer(); /* XXX over flush */
160 }
161 
162 int
163 _ohci_sbus_mem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
164     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
165     int flags)
166 {
167 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
168 	struct ohci_dma_segment *ds;
169 	struct iopdma_segment *iopdma_seg;
170 	int error;
171 
172 	KDASSERT(sc);
173 	ds = malloc(sizeof(struct ohci_dma_segment), M_DEVBUF, M_NOWAIT);
174 	if (ds == NULL)
175 		return (1);
176 	/*
177 	 * Allocate DMA Area (IOP DMA Area <-> SIF DMA <-> EE DMA Area)
178 	 */
179 	iopdma_seg = &ds->ds_iopdma_seg;
180 	error = iopdma_allocate_buffer(iopdma_seg, size);
181 
182 	if (error) {
183 		free(ds, M_DEVBUF);
184 		return (1);
185 	}
186 
187 	segs[0].ds_len	  = iopdma_seg->size;
188 	segs[0].ds_addr	  = iopdma_seg->iop_paddr;
189 	segs[0]._ds_vaddr = iopdma_seg->ee_vaddr;
190 
191 	LIST_INSERT_HEAD(&sc->sc_dmaseg_head, ds, ds_link);
192 
193 	*rsegs = 1;
194 
195 	return (0);
196 }
197 
198 void
199 _ohci_sbus_mem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
200 {
201 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
202 	struct ohci_dma_segment *ds;
203 	paddr_t addr = segs[0].ds_addr;
204 
205 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
206 	    ds = LIST_NEXT(ds, ds_link)) {
207 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
208 			iopdma_free_buffer(&ds->ds_iopdma_seg);
209 
210 			LIST_REMOVE(ds, ds_link);
211 			free(ds, M_DEVBUF);
212 			return;
213 		}
214 	}
215 
216 	panic("_dmamem_free: can't find corresponding handle.");
217 	/* NOTREACHED */
218 }
219 
220 int
221 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
222     void **kvap, int flags)
223 {
224 	struct ohci_sbus_softc *sc = t->_dmachip_cookie;
225 	struct ohci_dma_segment *ds;
226 	paddr_t addr = segs[0].ds_addr;
227 
228 	for (ds = LIST_FIRST(&sc->sc_dmaseg_head); ds != NULL;
229 	    ds = LIST_NEXT(ds, ds_link)) {
230 		if (ds->ds_iopdma_seg.iop_paddr == addr) {
231 
232 			*kvap = (void *)ds->ds_iopdma_seg.ee_vaddr;
233 
234 			return (0);
235 		}
236 	}
237 
238 	return (1);
239 }
240 
241 void
242 _ohci_sbus_mem_unmap(bus_dma_tag_t t, void *kva, size_t size)
243 {
244 	/* nothing to do */
245 }
246