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