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