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