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