1 /* $NetBSD: ohci_sbus.c,v 1.16 2021/08/07 16:19:02 thorpej 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.16 2021/08/07 16:19:02 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/kmem.h>
37
38 /* bus_dma */
39 #include <sys/mbuf.h>
40 #include <uvm/uvm_extern.h>
41
42 #define _PLAYSTATION2_BUS_DMA_PRIVATE
43 #include <machine/bus.h>
44 #include <machine/autoconf.h>
45
46 #include <mips/cpuregs.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdivar.h>
51 #include <dev/usb/usb_mem.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 void **, int);
79 STATIC void _ohci_sbus_mem_unmap(bus_dma_tag_t, void *, 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 CFATTACH_DECL_NEW(ohci_sbus, sizeof(struct ohci_sbus_softc),
110 ohci_sbus_match, ohci_sbus_attach, NULL, NULL);
111
112 int
ohci_sbus_match(struct device * parent,struct cfdata * cf,void * aux)113 ohci_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
114 {
115
116 return 1;
117 }
118
119 void
ohci_sbus_attach(struct device * parent,struct device * self,void * aux)120 ohci_sbus_attach(struct device *parent, struct device *self, void *aux)
121 {
122 struct ohci_sbus_softc *sc = device_private(self);
123
124 printf("\n");
125
126 sc->sc.sc_dev = self;
127 sc->sc.sc_bus.ub_hcpriv = sc;
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.ub_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 int err = ohci_init(&sc->sc);
146
147 if (err) {
148 printf(": init failed. error=%d\n", err);
149 return;
150 }
151
152 /* Attach usb device. */
153 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
154 CFARGS_NONE);
155 }
156
157 void
_ohci_sbus_map_sync(bus_dma_tag_t t,bus_dmamap_t map,bus_addr_t offset,bus_size_t len,int ops)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
_ohci_sbus_mem_alloc(bus_dma_tag_t t,bus_size_t size,bus_size_t alignment,bus_size_t boundary,bus_dma_segment_t * segs,int nsegs,int * rsegs,int flags)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 = kmem_intr_alloc(sizeof(struct ohci_dma_segment), KM_NOSLEEP);
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 kmem_intr_free(ds, sizeof(*ds));
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
_ohci_sbus_mem_free(bus_dma_tag_t t,bus_dma_segment_t * segs,int nsegs)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 kmem_intr_free(ds, sizeof(*ds));
215 return;
216 }
217 }
218
219 panic("_dmamem_free: can't find corresponding handle.");
220 /* NOTREACHED */
221 }
222
223 int
_ohci_sbus_mem_map(bus_dma_tag_t t,bus_dma_segment_t * segs,int nsegs,size_t size,void ** kvap,int flags)224 _ohci_sbus_mem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs, size_t size,
225 void **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 = (void *)ds->ds_iopdma_seg.ee_vaddr;
236
237 return 0;
238 }
239 }
240
241 return 1;
242 }
243
244 void
_ohci_sbus_mem_unmap(bus_dma_tag_t t,void * kva,size_t size)245 _ohci_sbus_mem_unmap(bus_dma_tag_t t, void *kva, size_t size)
246 {
247 /* nothing to do */
248 }
249