xref: /netbsd-src/sys/arch/arm/xscale/pxa2x0_udc.c (revision 2980e352a13e8f0b545a366830c411e7a542ada8)
1 /*	$NetBSD: pxa2x0_udc.c,v 1.1 2006/12/17 16:03:33 peter 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 
56 	if (CPU_IS_PXA270)
57 		return 1;
58 	return 0;
59 }
60 
61 static void
62 pxaudc_attach(struct device *parent, struct device *self, void *aux)
63 {
64 	struct pxaudc_softc *sc = (struct pxaudc_softc *)self;
65 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
66 
67 	sc->sc_iot = pxa->pxa_iot;
68 	sc->sc_size = 0;
69 	sc->sc_powerhook = NULL;
70 
71 	if (bus_space_map(sc->sc_iot, PXA2X0_USBDC_BASE, PXA2X0_USBDC_SIZE, 0,
72 	    &sc->sc_ioh)) {
73 		aprint_error(": couldn't map memory space\n");
74 		return;
75 	}
76 	sc->sc_size = PXA2X0_USBDC_SIZE;
77 
78 	printf(": PXA2x0 USB Device Controller\n");
79 
80 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_size,
81 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
82 
83 	pxa2x0_gpio_set_function(35, GPIO_ALT_FN_2_IN); /* USB_P2_1 */
84 	pxa2x0_gpio_set_function(37, GPIO_ALT_FN_1_OUT); /* USB_P2_8 */
85 	pxa2x0_gpio_set_function(41, GPIO_ALT_FN_2_IN); /* USB_P2_7 */
86 	pxa2x0_gpio_set_function(89, GPIO_ALT_FN_2_OUT); /* USBHPEN<1> */
87 	pxa2x0_gpio_set_function(120, GPIO_ALT_FN_2_OUT); /* USBHPEN<2> */
88 
89 	pxa2x0_clkman_config(CKEN_USBDC, 1);
90 
91 	pxaudc_enable(sc);
92 
93 	pxa2x0_gpio_set_bit(37); /* USB_P2_8 */
94 
95 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
96 	    pxaudc_power, sc);
97 	if (sc->sc_powerhook == NULL) {
98 		aprint_error("%s: unable to establish powerhook.\n",
99 		    sc->sc_dev.dv_xname);
100 	}
101 }
102 
103 static int
104 pxaudc_detach(struct device *self, int flags)
105 {
106 	struct pxaudc_softc *sc = (struct pxaudc_softc *)self;
107 
108 	if (sc->sc_powerhook)
109 		powerhook_disestablish(sc->sc_powerhook);
110 
111 	if (sc->sc_size) {
112 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
113 		sc->sc_size = 0;
114 	}
115 
116 	return 0;
117 }
118 
119 static void
120 pxaudc_power(int why, void *arg)
121 {
122 	struct pxaudc_softc *sc = (struct pxaudc_softc *)arg;
123 
124 	switch (why) {
125 	case PWR_RESUME:
126 		pxaudc_enable(sc);
127 		break;
128 	}
129 }
130 
131 static void
132 pxaudc_enable(struct pxaudc_softc *sc)
133 {
134 	uint32_t hr;
135 
136 	/* disable the controller */
137 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR);
138 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCCR,
139 	    hr & ~USBDC_UDCCR_UDE);
140 
141 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1);
142 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UDCICR1,
143 	    hr | USBDC_UDCICR1_IERS);
144 
145 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR, 0);
146 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
147 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
148 	    hr | USBDC_UP2OCR_HXS);
149 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
150 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
151 	    hr | USBDC_UP2OCR_HXOE);
152 	hr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR);
153 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
154 	    hr | USBDC_UP2OCR_DPPDE|USBDC_UP2OCR_DMPDE);
155 }
156