xref: /netbsd/sys/arch/arm/xscale/pxa2x0_udc.c (revision 6550d01e)
1 /*	$NetBSD: pxa2x0_udc.c,v 1.3 2009/08/09 06:24:03 kiyohara Exp $	*/
2 /*	$OpenBSD: pxa27x_udc.c,v 1.5 2005/03/30 14:24:39 dlg Exp $ */
3 
4 /*
5  * Copyright (c) 2005 David Gwynne <dlg@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/device.h>
23 #include <sys/kernel.h>
24 
25 #include <machine/intr.h>
26 #include <machine/bus.h>
27 
28 #include <arm/xscale/pxa2x0cpu.h>
29 #include <arm/xscale/pxa2x0reg.h>
30 #include <arm/xscale/pxa2x0var.h>
31 #include <arm/xscale/pxa2x0_gpio.h>
32 
33 struct pxaudc_softc {
34 	struct device		sc_dev;
35 	bus_space_tag_t		sc_iot;
36 	bus_space_handle_t	sc_ioh;
37 	bus_size_t		sc_size;
38 
39 	void 			*sc_powerhook;
40 };
41 
42 static int	pxaudc_match(struct device *, struct cfdata *, void *);
43 static void	pxaudc_attach(struct device *, struct device *, void *);
44 static int	pxaudc_detach(struct device *, int);
45 
46 CFATTACH_DECL(pxaudc, sizeof(struct pxaudc_softc),
47     pxaudc_match, pxaudc_attach, pxaudc_detach, NULL);
48 
49 static void	pxaudc_power(int, void *);
50 static void	pxaudc_enable(struct pxaudc_softc *);
51 
52 static int
53 pxaudc_match(struct device *parent, struct cfdata *cf, void *aux)
54 {
55 	struct pxaip_attach_args *pxa = aux;
56 
57 	if (CPU_IS_PXA270 && strcmp(pxa->pxa_name, cf->cf_name) == 0) {
58 		pxa->pxa_size = PXA270_USBDC_SIZE;
59 		return 1;
60 	}
61 	return 0;
62 }
63 
64 static void
65 pxaudc_attach(struct device *parent, struct device *self, void *aux)
66 {
67 	struct pxaudc_softc *sc = (struct pxaudc_softc *)self;
68 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
69 
70 	sc->sc_iot = pxa->pxa_iot;
71 	sc->sc_size = 0;
72 	sc->sc_powerhook = NULL;
73 
74 	if (bus_space_map(sc->sc_iot, pxa->pxa_addr, pxa->pxa_size, 0,
75 	    &sc->sc_ioh)) {
76 		aprint_error(": couldn't map memory space\n");
77 		return;
78 	}
79 	sc->sc_size = pxa->pxa_size;
80 
81 	printf(": PXA2x0 USB Device Controller\n");
82 
83 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_size,
84 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
85 
86 	pxa2x0_clkman_config(CKEN_USBDC, 1);
87 
88 	pxaudc_enable(sc);
89 
90 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
91 	    pxaudc_power, sc);
92 	if (sc->sc_powerhook == NULL) {
93 		aprint_error("%s: unable to establish powerhook.\n",
94 		    sc->sc_dev.dv_xname);
95 	}
96 }
97 
98 static int
99 pxaudc_detach(struct device *self, int flags)
100 {
101 	struct pxaudc_softc *sc = (struct pxaudc_softc *)self;
102 
103 	if (sc->sc_powerhook)
104 		powerhook_disestablish(sc->sc_powerhook);
105 
106 	if (sc->sc_size) {
107 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
108 		sc->sc_size = 0;
109 	}
110 
111 	return 0;
112 }
113 
114 static void
115 pxaudc_power(int why, void *arg)
116 {
117 	struct pxaudc_softc *sc = (struct pxaudc_softc *)arg;
118 
119 	switch (why) {
120 	case PWR_RESUME:
121 		pxaudc_enable(sc);
122 		break;
123 	}
124 }
125 
126 static void
127 pxaudc_enable(struct pxaudc_softc *sc)
128 {
129 	uint32_t hr;
130 
131 	/* disable the controller */
132 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR);
133 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR,
134 	    hr & ~USBDC_UDCCR_UDE);
135 
136 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1);
137 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1,
138 	    hr | USBDC_UDCICR1_IERS);
139 
140 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR, 0);
141 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
142 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
143 	    hr | USBDC_UP2OCR_HXS);
144 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
145 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
146 	    hr | USBDC_UP2OCR_HXOE);
147 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
148 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
149 	    hr | USBDC_UP2OCR_DPPDE|USBDC_UP2OCR_DMPDE);
150 }
151