xref: /netbsd-src/sys/arch/hpcmips/dev/plumohci.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: plumohci.c,v 1.5 2001/11/18 08:19:39 takemura Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 UCHIYAMA Yasushi
5  * Copyright (c) 1999 MAEKAWA Masahide <bishop@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/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 
44 /* busdma */
45 #include <sys/mbuf.h>
46 #include <uvm/uvm_extern.h>
47 
48 #include <machine/bus.h>
49 #include <machine/bus_dma_hpcmips.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdivar.h>
54 #include <dev/usb/usb_mem.h>
55 
56 #include <dev/usb/ohcireg.h>
57 #include <dev/usb/ohcivar.h>
58 
59 #include <hpcmips/tx/tx39var.h>
60 #include <hpcmips/dev/plumvar.h>
61 #include <hpcmips/dev/plumicuvar.h>
62 #include <hpcmips/dev/plumpowervar.h>
63 #include <hpcmips/dev/plumohcireg.h>
64 
65 int plumohci_match(struct device *, struct cfdata *, void *);
66 void plumohci_attach(struct device *, struct device *, void *);
67 int plumohci_intr(void *);
68 
69 void __plumohci_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
70     bus_addr_t, bus_size_t, int);
71 int __plumohci_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
72     bus_size_t, bus_dma_segment_t *, int, int *, int);
73 void __plumohci_dmamem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
74 int __plumohci_dmamem_map(bus_dma_tag_t, bus_dma_segment_t *,
75     int, size_t, caddr_t *, int);
76 void __plumohci_dmamem_unmap(bus_dma_tag_t, caddr_t, size_t);
77 
78 struct bus_dma_tag_hpcmips plumohci_bus_dma_tag = {
79 	{
80 		NULL,
81 		{
82 			_hpcmips_bd_map_create,
83 			_hpcmips_bd_map_destroy,
84 			_hpcmips_bd_map_load,
85 			_hpcmips_bd_map_load_mbuf,
86 			_hpcmips_bd_map_load_uio,
87 			_hpcmips_bd_map_load_raw,
88 			_hpcmips_bd_map_unload,
89 			__plumohci_dmamap_sync,
90 			__plumohci_dmamem_alloc,
91 			__plumohci_dmamem_free,
92 			__plumohci_dmamem_map,
93 			__plumohci_dmamem_unmap,
94 			_hpcmips_bd_mem_mmap,
95 		},
96 	},
97 	NULL,
98 };
99 
100 struct plumohci_shm {
101 	bus_space_handle_t ps_bsh;
102 	paddr_t	ps_paddr;
103 	caddr_t	ps_caddr;
104 	size_t	ps_size;
105 	LIST_ENTRY(plumohci_shm) ps_link;
106 };
107 
108 struct plumohci_softc {
109 	struct ohci_softc sc;
110 	void *sc_ih;
111 	void *sc_wakeih;
112 
113 	LIST_HEAD(, plumohci_shm) sc_shm_head;
114 };
115 
116 struct cfattach plumohci_ca = {
117 	sizeof(struct plumohci_softc), plumohci_match, plumohci_attach,
118 };
119 
120 int
121 plumohci_match(struct device *parent, struct cfdata *match, void *aux)
122 {
123 	/* PLUM2 builtin OHCI module */
124 
125 	return (1);
126 }
127 
128 void
129 plumohci_attach(struct device *parent, struct device *self, void *aux)
130 {
131 	struct plumohci_softc *sc = (struct plumohci_softc *)self;
132 	struct plum_attach_args *pa = aux;
133 	usbd_status r;
134 
135 	sc->sc.iot = pa->pa_iot;
136 	sc->sc.sc_bus.dmatag = &plumohci_bus_dma_tag.bdt;
137 	plumohci_bus_dma_tag._dmamap_chipset_v = sc;
138 
139 	/* Map I/O space */
140 	if (bus_space_map(sc->sc.iot, PLUM_OHCI_REGBASE, OHCI_PAGE_SIZE,
141 	    0, &sc->sc.ioh)) {
142 		printf(": cannot map mem space\n");
143 		return;
144 	}
145 
146 	/* power up */
147 	/*
148 	 * in the case of PLUM2, UHOSTC uses the VRAM as the shared RAM
149 	 * so establish power/clock of Video contoroller
150 	 */
151 	plum_power_establish(pa->pa_pc, PLUM_PWR_EXTPW1);
152 	plum_power_establish(pa->pa_pc, PLUM_PWR_USB);
153 
154 	/* Disable interrupts, so we don't can any spurious ones. */
155 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
156 	    OHCI_ALL_INTRS);
157 
158 	/* master enable */
159 	sc->sc_ih = plum_intr_establish(pa->pa_pc, PLUM_INT_USB, IST_EDGE,
160 	    IPL_USB, ohci_intr, sc);
161 #if 0
162 	/*
163 	 *  enable the clock restart request interrupt
164 	 *  (for USBSUSPEND state)
165 	 */
166 	sc->sc_wakeih = plum_intr_establish(pa->pa_pc, PLUM_INT_USBWAKE,
167 	    IST_EDGE, IPL_USB,
168 	    plumohci_intr, sc);
169 #endif
170 	/*
171 	 * Shared memory list.
172 	 */
173 	LIST_INIT(&sc->sc_shm_head);
174 
175 	printf("\n");
176 
177 	r = ohci_init(&sc->sc);
178 
179 	if (r != USBD_NORMAL_COMPLETION) {
180 		printf(": init failed, error=%d\n", r);
181 
182 		plum_intr_disestablish(pa->pa_pc, sc->sc_ih);
183 		plum_intr_disestablish(pa->pa_pc, sc->sc_wakeih);
184 
185 		return;
186 	}
187 
188 	/* Attach usb device. */
189 	sc->sc.sc_child = config_found((void *) sc, &sc->sc.sc_bus,
190 	    usbctlprint);
191 }
192 
193 int
194 plumohci_intr(void *arg)
195 {
196 	printf("Plum2 OHCI: wakeup intr\n");
197 	return 0;
198 }
199 
200 /*
201  * Plum2 OHCI specific busdma routines.
202  *	Plum2 OHCI shared buffer can't allocate on memory
203  *	but V-RAM (busspace).
204  */
205 
206 void
207 __plumohci_dmamap_sync(bus_dma_tag_t tx, bus_dmamap_t map, bus_addr_t offset,
208     bus_size_t len, int ops)
209 {
210 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
211 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
212 
213 	/*
214 	 * Flush the write buffer allocated on the V-RAM.
215 	 * Accessing any host controller register flushs write buffer
216 	 */
217 	(void)bus_space_read_4(sc->sc.iot, sc->sc.ioh, OHCI_REVISION);
218 }
219 
220 int
221 __plumohci_dmamem_alloc(bus_dma_tag_t tx, bus_size_t size,
222     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
223     int nsegs, int *rsegs, int flags)
224 {
225 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
226 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
227 	struct plumohci_shm *ps;
228 	bus_space_handle_t bsh;
229 	paddr_t paddr;
230 	caddr_t caddr;
231 	int error;
232 
233 	size = round_page(size);
234 
235 	/*
236 	 * Allocate buffer from V-RAM area.
237 	 */
238 	error = bus_space_alloc(sc->sc.iot, PLUM_OHCI_SHMEMBASE,
239 	    PLUM_OHCI_SHMEMBASE + PLUM_OHCI_SHMEMSIZE - 1,
240 	    size, OHCI_PAGE_SIZE, OHCI_PAGE_SIZE, 0,
241 	    (bus_addr_t*)&caddr, &bsh);
242 	if (error)
243 		return (1);
244 
245 	pmap_extract(pmap_kernel(), (vaddr_t)caddr, &paddr);
246 
247 	ps = malloc(sizeof(struct plumohci_shm), M_DEVBUF, M_NOWAIT);
248 	if (ps == 0)
249 		return (1);
250 
251 	ps->ps_bsh = bsh;
252 	ps->ps_size = segs[0].ds_len = size;
253 	ps->ps_paddr = segs[0].ds_addr = paddr;
254 	ps->ps_caddr = caddr;
255 
256 	LIST_INSERT_HEAD(&sc->sc_shm_head, ps, ps_link);
257 
258 	*rsegs = 1;
259 
260 	return (0);
261 }
262 
263 void
264 __plumohci_dmamem_free(bus_dma_tag_t tx, bus_dma_segment_t *segs, int nsegs)
265 {
266 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
267 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
268 	struct plumohci_shm *ps;
269 
270 	for (ps = LIST_FIRST(&sc->sc_shm_head); ps;
271 	    ps = LIST_NEXT(ps, ps_link)) {
272 
273 		if (ps->ps_paddr == segs[0].ds_addr) {
274 			bus_space_free(sc->sc.iot, ps->ps_bsh, ps->ps_size);
275 			LIST_REMOVE(ps, ps_link);
276 			free(ps, M_DEVBUF);
277 
278 			return;
279 		}
280 	}
281 
282 	panic("__plumohci_dmamem_free: can't find corresponding handle.");
283 	/* NOTREACHED */
284 }
285 
286 int
287 __plumohci_dmamem_map(bus_dma_tag_t tx, bus_dma_segment_t *segs, int nsegs,
288     size_t size, caddr_t *kvap, int flags)
289 {
290 	struct bus_dma_tag_hpcmips *t = (struct bus_dma_tag_hpcmips *)tx;
291 	struct plumohci_softc *sc = t->_dmamap_chipset_v;
292 	struct plumohci_shm *ps;
293 
294 	for (ps = LIST_FIRST(&sc->sc_shm_head); ps;
295 	    ps = LIST_NEXT(ps, ps_link)) {
296 		if (ps->ps_paddr == segs[0].ds_addr) {
297 
298 			*kvap = ps->ps_caddr;
299 
300 			return (0);
301 		}
302 	}
303 
304 	return (1);
305 }
306 
307 void
308 __plumohci_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
309 {
310 	/* nothing to do */
311 }
312