xref: /netbsd-src/sys/arch/playstation2/dev/ohci_sbus.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ohci_sbus.c,v 1.13 2016/07/18 22:17:09 maya 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.13 2016/07/18 22:17:09 maya 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 <mips/cpuregs.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdivar.h>
50 #include <dev/usb/usb_mem.h>
51 
52 #include <dev/usb/ohcireg.h>
53 #include <dev/usb/ohcivar.h>
54 
55 #include <playstation2/ee/sifvar.h>	/* DMA staff */
56 #include <playstation2/ee/dmacvar.h>
57 #include <playstation2/dev/sbusvar.h>
58 
59 #ifdef DEBUG
60 #define STATIC
61 #else
62 #define STATIC static
63 #endif
64 
65 #define	SBUS_OHCI_REGBASE	MIPS_PHYS_TO_KSEG1(0x1f801600)
66 #define SBUS_OHCI_REGSIZE	0x1000
67 
68 STATIC int ohci_sbus_match(struct device *, struct cfdata *, void *);
69 STATIC void ohci_sbus_attach(struct device *, struct device *, void *);
70 
71 STATIC void _ohci_sbus_map_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
72     bus_size_t, int);
73 STATIC int _ohci_sbus_mem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
74     bus_size_t, bus_dma_segment_t *, int, int *, int);
75 STATIC void _ohci_sbus_mem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
76 STATIC int _ohci_sbus_mem_map(bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
77     void **, int);
78 STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, void *, size_t);
79 
80 struct playstation2_bus_dma_tag ohci_bus_dma_tag = {
81 	_bus_dmamap_create,
82 	_bus_dmamap_destroy,
83 	_bus_dmamap_load,
84 	_bus_dmamap_load_mbuf,
85 	_bus_dmamap_load_uio,
86 	_bus_dmamap_load_raw,
87 	_bus_dmamap_unload,
88 	_ohci_sbus_map_sync,
89 	_ohci_sbus_mem_alloc,
90 	_ohci_sbus_mem_free,
91 	_ohci_sbus_mem_map,
92 	_ohci_sbus_mem_unmap,
93 	_bus_dmamem_mmap,
94 };
95 
96 struct ohci_dma_segment {
97 	struct iopdma_segment ds_iopdma_seg;
98 
99 	LIST_ENTRY(ohci_dma_segment) ds_link;
100 };
101 
102 struct ohci_sbus_softc {
103 	struct ohci_softc sc;
104 
105 	LIST_HEAD(, ohci_dma_segment) sc_dmaseg_head;
106 };
107 
108 CFATTACH_DECL_NEW(ohci_sbus, sizeof(struct ohci_sbus_softc),
109     ohci_sbus_match, ohci_sbus_attach, NULL, NULL);
110 
111 int
112 ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
113 {
114 
115 	return 1;
116 }
117 
118 void
119 ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
120 {
121 	struct ohci_sbus_softc *sc = device_private(self);
122 
123 	printf("\n");
124 
125 	sc->sc.sc_dev = self;
126 	sc->sc.sc_bus.ub_hcpriv = sc;
127 
128 	sc->sc.iot = bus_space_create(0, "OHCI I/O space", SBUS_OHCI_REGBASE,
129 	    SBUS_OHCI_REGSIZE);
130 	sc->sc.ioh = SBUS_OHCI_REGBASE;
131 
132 	ohci_bus_dma_tag._dmachip_cookie = sc;
133 	sc->sc.sc_bus.ub_dmatag = &ohci_bus_dma_tag;
134 
135 	/* Disable interrupts, so we don't can any spurious ones. */
136 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
137 	    OHCI_ALL_INTRS);
138 
139 	sbus_intr_establish(SBUS_IRQ_USB, ohci_intr, sc);
140 
141 	/* IOP/EE DMA relay segment list */
142 	LIST_INIT(&sc->sc_dmaseg_head);
143 
144 	int err = ohci_init(&sc->sc);
145 
146 	if (err) {
147 		printf(": init failed. error=%d\n", err);
148 		return;
149 	}
150 
151 	/* Attach usb device. */
152 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, 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     void **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 = (void *)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, void *kva, size_t size)
244 {
245 	/* nothing to do */
246 }
247