gemini_lpchc.c revision 1.1.6.2 1 1.1.6.2 skrll /* $NetBSD: gemini_lpchc.c,v 1.1.6.2 2009/01/19 13:15:58 skrll Exp $ */
2 1.1.6.2 skrll
3 1.1.6.2 skrll /*
4 1.1.6.2 skrll * GEMINI LPC Host Controller
5 1.1.6.2 skrll */
6 1.1.6.2 skrll #include "opt_gemini.h"
7 1.1.6.2 skrll #include "locators.h"
8 1.1.6.2 skrll
9 1.1.6.2 skrll #include <sys/cdefs.h>
10 1.1.6.2 skrll __KERNEL_RCSID(0, "$NetBSD: gemini_lpchc.c,v 1.1.6.2 2009/01/19 13:15:58 skrll Exp $");
11 1.1.6.2 skrll
12 1.1.6.2 skrll #include <sys/param.h>
13 1.1.6.2 skrll #include <sys/callout.h>
14 1.1.6.2 skrll #include <sys/cdefs.h>
15 1.1.6.2 skrll #include <sys/device.h>
16 1.1.6.2 skrll #include <sys/kernel.h>
17 1.1.6.2 skrll #include <sys/systm.h>
18 1.1.6.2 skrll #include <sys/malloc.h>
19 1.1.6.2 skrll
20 1.1.6.2 skrll #include <machine/param.h>
21 1.1.6.2 skrll #include <machine/bus.h>
22 1.1.6.2 skrll
23 1.1.6.2 skrll #include <arm/gemini/gemini_lpchcvar.h>
24 1.1.6.2 skrll #include <arm/gemini/gemini_lpcvar.h>
25 1.1.6.2 skrll #include <arm/gemini/gemini_reg.h>
26 1.1.6.2 skrll
27 1.1.6.2 skrll static inline void
28 1.1.6.2 skrll gemini_lpchc_sirq_cfg(bus_space_tag_t iot, bus_space_handle_t ioh,
29 1.1.6.2 skrll bus_size_t offset, uint32_t bit, boolean_t doset)
30 1.1.6.2 skrll {
31 1.1.6.2 skrll uint32_t r;
32 1.1.6.2 skrll
33 1.1.6.2 skrll r = bus_space_read_4(iot, ioh, offset);
34 1.1.6.2 skrll if (doset)
35 1.1.6.2 skrll r |= bit;
36 1.1.6.2 skrll else
37 1.1.6.2 skrll r &= ~bit;
38 1.1.6.2 skrll bus_space_write_4(iot, ioh, offset, r);
39 1.1.6.2 skrll }
40 1.1.6.2 skrll
41 1.1.6.2 skrll static inline void
42 1.1.6.2 skrll gemini_lpchc_sirq_ack(bus_space_tag_t iot, bus_space_handle_t ioh,
43 1.1.6.2 skrll uint32_t bit)
44 1.1.6.2 skrll {
45 1.1.6.2 skrll uint32_t r;
46 1.1.6.2 skrll
47 1.1.6.2 skrll r = bus_space_read_4(iot, ioh, GEMINI_LPCHC_SERIRQSTS);
48 1.1.6.2 skrll r &= bit;
49 1.1.6.2 skrll if (r != 0)
50 1.1.6.2 skrll bus_space_write_4(iot, ioh, GEMINI_LPCHC_SERIRQSTS, r);
51 1.1.6.2 skrll }
52 1.1.6.2 skrll
53 1.1.6.2 skrll static inline void
54 1.1.6.2 skrll gemini_lpchc_sirq_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
55 1.1.6.2 skrll {
56 1.1.6.2 skrll uint32_t r;
57 1.1.6.2 skrll
58 1.1.6.2 skrll r = bus_space_read_4(iot, ioh, GEMINI_LPCHC_IRQCTL);
59 1.1.6.2 skrll r &= ~LPCHC_IRQCTL_SIRQEN;
60 1.1.6.2 skrll bus_space_write_4(iot, ioh, GEMINI_LPCHC_IRQCTL, r);
61 1.1.6.2 skrll }
62 1.1.6.2 skrll
63 1.1.6.2 skrll static inline void
64 1.1.6.2 skrll gemini_lpchc_sirq_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
65 1.1.6.2 skrll {
66 1.1.6.2 skrll uint32_t r;
67 1.1.6.2 skrll
68 1.1.6.2 skrll r = bus_space_read_4(iot, ioh, GEMINI_LPCHC_IRQCTL);
69 1.1.6.2 skrll r |= LPCHC_IRQCTL_SIRQEN;
70 1.1.6.2 skrll r |= LPCHC_IRQCTL_SIRQMS; /* XXX "continuous mode" */
71 1.1.6.2 skrll r |= IRQCTL_SIRQFW_8; /* XXX SIRW Frame Width 8 clocks */
72 1.1.6.2 skrll bus_space_write_4(iot, ioh, GEMINI_LPCHC_IRQCTL, r);
73 1.1.6.2 skrll #if 0
74 1.1.6.2 skrll delay(10); /* wait 1 serial IRQ cycle */
75 1.1.6.2 skrll r &= ~LPCHC_IRQCTL_SIRQMS; /* XXX "quiet mode" */
76 1.1.6.2 skrll bus_space_write_4(iot, ioh, GEMINI_LPCHC_IRQCTL, r);
77 1.1.6.2 skrll #endif
78 1.1.6.2 skrll }
79 1.1.6.2 skrll
80 1.1.6.2 skrll static inline void
81 1.1.6.2 skrll gemini_lpchc_intrq_init(gemini_lpchc_softc_t *sc)
82 1.1.6.2 skrll {
83 1.1.6.2 skrll SIMPLEQ_INIT(&sc->sc_intrq);
84 1.1.6.2 skrll }
85 1.1.6.2 skrll
86 1.1.6.2 skrll static inline int
87 1.1.6.2 skrll gemini_lpchc_intrq_empty(gemini_lpchc_softc_t *sc)
88 1.1.6.2 skrll {
89 1.1.6.2 skrll return SIMPLEQ_EMPTY(&sc->sc_intrq);
90 1.1.6.2 skrll }
91 1.1.6.2 skrll
92 1.1.6.2 skrll static inline void *
93 1.1.6.2 skrll gemini_lpchc_intrq_insert(gemini_lpchc_softc_t *sc, int (*func)(void *),
94 1.1.6.2 skrll void *arg, uint32_t bit, boolean_t isedge)
95 1.1.6.2 skrll {
96 1.1.6.2 skrll gemini_lpchc_intrq_t *iqp;
97 1.1.6.2 skrll
98 1.1.6.2 skrll iqp = malloc(sizeof(*iqp), M_DEVBUF, M_NOWAIT|M_ZERO);
99 1.1.6.2 skrll if (iqp == NULL) {
100 1.1.6.2 skrll printf("gemini_lpchc_intrq_insert: malloc failed\n");
101 1.1.6.2 skrll return NULL;
102 1.1.6.2 skrll }
103 1.1.6.2 skrll
104 1.1.6.2 skrll iqp->iq_func = func;
105 1.1.6.2 skrll iqp->iq_arg = arg;
106 1.1.6.2 skrll iqp->iq_bit = bit;
107 1.1.6.2 skrll iqp->iq_isedge = isedge;
108 1.1.6.2 skrll SIMPLEQ_INSERT_TAIL(&sc->sc_intrq, iqp, iq_q);
109 1.1.6.2 skrll
110 1.1.6.2 skrll return (void *)iqp;
111 1.1.6.2 skrll }
112 1.1.6.2 skrll
113 1.1.6.2 skrll static inline void
114 1.1.6.2 skrll gemini_lpchc_intrq_remove(gemini_lpchc_softc_t *sc, void *cookie)
115 1.1.6.2 skrll {
116 1.1.6.2 skrll gemini_lpchc_intrq_t *iqp;
117 1.1.6.2 skrll
118 1.1.6.2 skrll SIMPLEQ_FOREACH(iqp, &sc->sc_intrq, iq_q) {
119 1.1.6.2 skrll if ((void *)iqp == cookie) {
120 1.1.6.2 skrll SIMPLEQ_REMOVE(&sc->sc_intrq,
121 1.1.6.2 skrll iqp, gemini_lpchc_intrq, iq_q);
122 1.1.6.2 skrll free(iqp, M_DEVBUF);
123 1.1.6.2 skrll return;
124 1.1.6.2 skrll }
125 1.1.6.2 skrll }
126 1.1.6.2 skrll }
127 1.1.6.2 skrll
128 1.1.6.2 skrll static inline int
129 1.1.6.2 skrll gemini_lpchc_intrq_dispatch(gemini_lpchc_softc_t *sc)
130 1.1.6.2 skrll {
131 1.1.6.2 skrll gemini_lpchc_intrq_t *iqp;
132 1.1.6.2 skrll uint32_t r;
133 1.1.6.2 skrll int rv = 0;
134 1.1.6.2 skrll
135 1.1.6.2 skrll r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_LPCHC_SERIRQSTS);
136 1.1.6.2 skrll r &= ~LPCHC_SERIRQSTS_RESV;
137 1.1.6.2 skrll SIMPLEQ_FOREACH(iqp, &sc->sc_intrq, iq_q) {
138 1.1.6.2 skrll if ((r & iqp->iq_bit) != 0) {
139 1.1.6.2 skrll if (iqp->iq_isedge) {
140 1.1.6.2 skrll gemini_lpchc_sirq_ack(sc->sc_iot, sc->sc_ioh,
141 1.1.6.2 skrll iqp->iq_bit);
142 1.1.6.2 skrll }
143 1.1.6.2 skrll rv |= (*iqp->iq_func)(iqp->iq_arg);
144 1.1.6.2 skrll }
145 1.1.6.2 skrll }
146 1.1.6.2 skrll return (rv != 0);
147 1.1.6.2 skrll }
148 1.1.6.2 skrll
149 1.1.6.2 skrll void
150 1.1.6.2 skrll gemini_lpchc_init(lpcintrtag_t tag)
151 1.1.6.2 skrll {
152 1.1.6.2 skrll gemini_lpchc_softc_t *sc = tag;
153 1.1.6.2 skrll uint32_t r;
154 1.1.6.2 skrll
155 1.1.6.2 skrll gemini_lpchc_intrq_init(sc);
156 1.1.6.2 skrll
157 1.1.6.2 skrll r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_LPCHC_CSR);
158 1.1.6.2 skrll r |= LPCHC_CSR_BEN;
159 1.1.6.2 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_LPCHC_CSR, r);
160 1.1.6.2 skrll }
161 1.1.6.2 skrll
162 1.1.6.2 skrll void *
163 1.1.6.2 skrll gemini_lpchc_intr_establish(lpcintrtag_t tag, uint irq,
164 1.1.6.2 skrll int ipl, int type, int (*func)(void *), void *arg)
165 1.1.6.2 skrll {
166 1.1.6.2 skrll gemini_lpchc_softc_t *sc = tag;
167 1.1.6.2 skrll bus_space_tag_t iot = sc->sc_iot;
168 1.1.6.2 skrll bus_space_handle_t ioh = sc->sc_ioh;
169 1.1.6.2 skrll uint32_t bit;
170 1.1.6.2 skrll boolean_t isedge;
171 1.1.6.2 skrll boolean_t ishigh;
172 1.1.6.2 skrll void *ih;
173 1.1.6.2 skrll
174 1.1.6.2 skrll isedge = ((type == IST_EDGE_RISING) || (type == IST_EDGE_FALLING));
175 1.1.6.2 skrll ishigh = ((type == IST_EDGE_RISING) || (type == IST_LEVEL_HIGH));
176 1.1.6.2 skrll
177 1.1.6.2 skrll if (irq >= GEMINI_LPCHC_NSERIRQ) {
178 1.1.6.2 skrll printf("%s: bad irq %d\n", __FUNCTION__, irq);
179 1.1.6.2 skrll return NULL;
180 1.1.6.2 skrll }
181 1.1.6.2 skrll #if 0
182 1.1.6.2 skrll bit = 1 << irq;
183 1.1.6.2 skrll #else
184 1.1.6.2 skrll bit = (1 << GEMINI_LPCHC_NSERIRQ) -1; /* XXX */
185 1.1.6.2 skrll #endif
186 1.1.6.2 skrll
187 1.1.6.2 skrll /* set IRQ type */
188 1.1.6.2 skrll gemini_lpchc_sirq_cfg(iot, ioh, GEMINI_LPCHC_SERIRQTYP,
189 1.1.6.2 skrll bit, isedge);
190 1.1.6.2 skrll
191 1.1.6.2 skrll /* set IRQ polarity */
192 1.1.6.2 skrll gemini_lpchc_sirq_cfg(iot, ioh, GEMINI_LPCHC_SERIRQPOLARITY,
193 1.1.6.2 skrll bit, ishigh);
194 1.1.6.2 skrll
195 1.1.6.2 skrll /* ack a-priori edge status */
196 1.1.6.2 skrll if (isedge)
197 1.1.6.2 skrll gemini_lpchc_sirq_ack(iot, ioh, bit);
198 1.1.6.2 skrll
199 1.1.6.2 skrll if (gemini_lpchc_intrq_empty(sc))
200 1.1.6.2 skrll gemini_lpchc_sirq_enable(iot, ioh);
201 1.1.6.2 skrll
202 1.1.6.2 skrll ih = gemini_lpchc_intrq_insert(sc, func, arg, bit, isedge);
203 1.1.6.2 skrll if (ih == NULL)
204 1.1.6.2 skrll if (gemini_lpchc_intrq_empty(sc))
205 1.1.6.2 skrll gemini_lpchc_sirq_disable(iot, ioh);
206 1.1.6.2 skrll
207 1.1.6.2 skrll return ih;
208 1.1.6.2 skrll }
209 1.1.6.2 skrll
210 1.1.6.2 skrll void
211 1.1.6.2 skrll gemini_lpchc_intr_disestablish(lpcintrtag_t tag, void *ih)
212 1.1.6.2 skrll {
213 1.1.6.2 skrll gemini_lpchc_softc_t *sc = tag;
214 1.1.6.2 skrll
215 1.1.6.2 skrll gemini_lpchc_intrq_remove(sc, ih);
216 1.1.6.2 skrll if (gemini_lpchc_intrq_empty(sc))
217 1.1.6.2 skrll gemini_lpchc_sirq_disable(sc->sc_iot, sc->sc_ioh);
218 1.1.6.2 skrll }
219 1.1.6.2 skrll
220 1.1.6.2 skrll int
221 1.1.6.2 skrll gemini_lpchc_intr(void *arg)
222 1.1.6.2 skrll {
223 1.1.6.2 skrll gemini_lpchc_softc_t *sc = arg;
224 1.1.6.2 skrll int rv;
225 1.1.6.2 skrll
226 1.1.6.2 skrll printf("%s: enter\n", __FUNCTION__);
227 1.1.6.2 skrll rv = gemini_lpchc_intrq_dispatch(sc);
228 1.1.6.2 skrll printf("%s: exit\n", __FUNCTION__);
229 1.1.6.2 skrll
230 1.1.6.2 skrll return rv;
231 1.1.6.2 skrll }
232 1.1.6.2 skrll
233