gemini_lpc.c revision 1.3.4.2 1 1.3.4.2 mjf /* $NetBSD: gemini_lpc.c,v 1.3.4.2 2009/01/17 13:27:52 mjf Exp $ */
2 1.3.4.2 mjf
3 1.3.4.2 mjf #include "opt_gemini.h"
4 1.3.4.2 mjf #include "locators.h"
5 1.3.4.2 mjf
6 1.3.4.2 mjf #include <sys/cdefs.h>
7 1.3.4.2 mjf __KERNEL_RCSID(0, "$NetBSD: gemini_lpc.c,v 1.3.4.2 2009/01/17 13:27:52 mjf Exp $");
8 1.3.4.2 mjf
9 1.3.4.2 mjf #include <sys/param.h>
10 1.3.4.2 mjf #include <sys/systm.h>
11 1.3.4.2 mjf #include <sys/device.h>
12 1.3.4.2 mjf #include <arch/arm/gemini/gemini_lpcvar.h>
13 1.3.4.2 mjf #include <arch/arm/gemini/gemini_lpchcvar.h>
14 1.3.4.2 mjf #include <arch/arm/gemini/gemini_reg.h>
15 1.3.4.2 mjf
16 1.3.4.2 mjf
17 1.3.4.2 mjf /* XXX these should be in lpcreg.h or it8721reg.h */
18 1.3.4.2 mjf #define IT8712_CFGCTL 0x02
19 1.3.4.2 mjf # define CFGCTL_WAITKEY __BIT(1)
20 1.3.4.2 mjf #define IT8712_LDN 0x07
21 1.3.4.2 mjf #define IT8712_CHIPID1 0x20
22 1.3.4.2 mjf #define IT8712_CHIPID2 0x21
23 1.3.4.2 mjf #define IT8712_CSCV 0x22
24 1.3.4.2 mjf # define CSCV_VERSION __BITS(3,0);
25 1.3.4.2 mjf #define IT8712_CLKSEL 0x23
26 1.3.4.2 mjf #define IT8712_SWSUSPEND 0x24
27 1.3.4.2 mjf #define IT8712_ADDR 0x2e
28 1.3.4.2 mjf #define IT8712_DATA 0x2f
29 1.3.4.2 mjf
30 1.3.4.2 mjf static int gemini_lpc_match(struct device *, struct cfdata *, void *);
31 1.3.4.2 mjf static void gemini_lpc_attach(struct device *, struct device *, void *);
32 1.3.4.2 mjf static int gemini_lpc_search(device_t, cfdata_t, const int *, void *);
33 1.3.4.2 mjf static int gemini_lpc_busprint(void *, const char *);
34 1.3.4.2 mjf
35 1.3.4.2 mjf static uint8_t it8712_pnp_read(lpctag_t, int, uint);
36 1.3.4.2 mjf static void it8712_pnp_write(lpctag_t, int, uint, uint8_t);
37 1.3.4.2 mjf static void it8712_pnp_enter(lpctag_t);
38 1.3.4.2 mjf static void it8712_pnp_exit(lpctag_t);
39 1.3.4.2 mjf static void *it8712_intr_establish(lpctag_t, uint, int, int, int (*)(void *), void *);
40 1.3.4.2 mjf static void it8712_intr_disestablish(lpctag_t, void *);
41 1.3.4.2 mjf
42 1.3.4.2 mjf CFATTACH_DECL_NEW(lpc, sizeof(struct gemini_lpc_softc),
43 1.3.4.2 mjf gemini_lpc_match, gemini_lpc_attach, NULL, NULL);
44 1.3.4.2 mjf
45 1.3.4.2 mjf gemini_lpc_bus_ops_t gemini_lpc_bus_ops = {
46 1.3.4.2 mjf it8712_pnp_read,
47 1.3.4.2 mjf it8712_pnp_write,
48 1.3.4.2 mjf it8712_pnp_enter,
49 1.3.4.2 mjf it8712_pnp_exit,
50 1.3.4.2 mjf it8712_intr_establish,
51 1.3.4.2 mjf it8712_intr_disestablish,
52 1.3.4.2 mjf };
53 1.3.4.2 mjf
54 1.3.4.2 mjf
55 1.3.4.2 mjf static int
56 1.3.4.2 mjf gemini_lpc_match(struct device *parent, struct cfdata *cf, void *aux)
57 1.3.4.2 mjf {
58 1.3.4.2 mjf struct gemini_lpc_attach_args *lpc = aux;
59 1.3.4.2 mjf
60 1.3.4.2 mjf if (lpc->lpc_addr == LPCCF_ADDR_DEFAULT)
61 1.3.4.2 mjf panic("lpc must have addr specified in config.");
62 1.3.4.2 mjf
63 1.3.4.2 mjf return 1;
64 1.3.4.2 mjf }
65 1.3.4.2 mjf
66 1.3.4.2 mjf static void
67 1.3.4.2 mjf gemini_lpc_attach(struct device *parent, struct device *self, void *aux)
68 1.3.4.2 mjf {
69 1.3.4.2 mjf gemini_lpc_softc_t *sc = device_private(self);
70 1.3.4.2 mjf struct gemini_lpchc_attach_args *lpchc = aux;
71 1.3.4.2 mjf bus_space_tag_t iot;
72 1.3.4.2 mjf bus_space_handle_t ioh;
73 1.3.4.2 mjf uint8_t id1, id2, vers, clk, susp;
74 1.3.4.2 mjf
75 1.3.4.2 mjf sc->sc_addr = lpchc->lpchc_addr;
76 1.3.4.2 mjf #if 0
77 1.3.4.2 mjf sc->sc_size = lpchc->lpchc_size;
78 1.3.4.2 mjf #else
79 1.3.4.2 mjf /*
80 1.3.4.2 mjf * we just map the configuration registers
81 1.3.4.2 mjf * child devices can map their own I/O
82 1.3.4.2 mjf */
83 1.3.4.2 mjf sc->sc_size = 4096;
84 1.3.4.2 mjf #endif
85 1.3.4.2 mjf
86 1.3.4.2 mjf iot = lpchc->lpchc_iot;
87 1.3.4.2 mjf if (bus_space_map(iot, sc->sc_addr, sc->sc_size, 0, &ioh))
88 1.3.4.2 mjf panic("%s: Cannot map registers", device_xname(self));
89 1.3.4.2 mjf sc->sc_iot = iot;
90 1.3.4.2 mjf sc->sc_ioh = ioh;
91 1.3.4.2 mjf
92 1.3.4.2 mjf it8712_pnp_enter(sc);
93 1.3.4.2 mjf id1 = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID1);
94 1.3.4.2 mjf id2 = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID2);
95 1.3.4.2 mjf vers = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CSCV);
96 1.3.4.2 mjf vers &= CSCV_VERSION;
97 1.3.4.2 mjf clk = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CLKSEL);
98 1.3.4.2 mjf susp = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_SWSUSPEND);
99 1.3.4.2 mjf it8712_pnp_exit(sc);
100 1.3.4.2 mjf
101 1.3.4.2 mjf aprint_normal("\n%s: chip ID %x%x, version %d",
102 1.3.4.2 mjf device_xname(self), id1, id2, vers);
103 1.3.4.2 mjf aprint_normal("\n%s: clksel %#x, susp %#x ",
104 1.3.4.2 mjf device_xname(self), clk, susp);
105 1.3.4.2 mjf
106 1.3.4.2 mjf sc->sc_lpchctag = lpchc->lpchc_tag;
107 1.3.4.2 mjf sc->sc_bus_ops = &gemini_lpc_bus_ops;
108 1.3.4.2 mjf
109 1.3.4.2 mjf aprint_normal("\n");
110 1.3.4.2 mjf aprint_naive("\n");
111 1.3.4.2 mjf
112 1.3.4.2 mjf /*
113 1.3.4.2 mjf * attach the rest of our devices
114 1.3.4.2 mjf */
115 1.3.4.2 mjf config_search_ia(gemini_lpc_search, self, "lpc", NULL);
116 1.3.4.2 mjf }
117 1.3.4.2 mjf
118 1.3.4.2 mjf static int
119 1.3.4.2 mjf gemini_lpc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
120 1.3.4.2 mjf {
121 1.3.4.2 mjf gemini_lpc_softc_t *sc = device_private(parent);
122 1.3.4.2 mjf gemini_lpc_attach_args_t lpc;
123 1.3.4.2 mjf
124 1.3.4.2 mjf lpc.lpc_ldn = cf->cf_loc[LPCCF_LDN];
125 1.3.4.2 mjf lpc.lpc_addr = cf->cf_loc[LPCCF_ADDR];
126 1.3.4.2 mjf lpc.lpc_size = cf->cf_loc[LPCCF_SIZE];
127 1.3.4.2 mjf lpc.lpc_intr = cf->cf_loc[LPCCF_INTR];
128 1.3.4.2 mjf lpc.lpc_iot = sc->sc_iot;
129 1.3.4.2 mjf lpc.lpc_tag = sc;
130 1.3.4.2 mjf lpc.lpc_base = sc->sc_addr;
131 1.3.4.2 mjf
132 1.3.4.2 mjf if (config_match(parent, cf, &lpc)) {
133 1.3.4.2 mjf config_attach(parent, cf, &lpc, gemini_lpc_busprint);
134 1.3.4.2 mjf return 0; /* love it */
135 1.3.4.2 mjf }
136 1.3.4.2 mjf
137 1.3.4.2 mjf return UNCONF; /* hate it */
138 1.3.4.2 mjf }
139 1.3.4.2 mjf
140 1.3.4.2 mjf static int
141 1.3.4.2 mjf gemini_lpc_busprint(void *aux, const char *name)
142 1.3.4.2 mjf {
143 1.3.4.2 mjf struct gemini_lpc_attach_args *lpc = aux;
144 1.3.4.2 mjf
145 1.3.4.2 mjf if (lpc->lpc_ldn != LPCCF_LDN_DEFAULT)
146 1.3.4.2 mjf aprint_normal(" ldn %d", lpc->lpc_ldn);
147 1.3.4.2 mjf if (lpc->lpc_addr != LPCCF_ADDR_DEFAULT)
148 1.3.4.2 mjf aprint_normal(" addr %#lx", lpc->lpc_addr);
149 1.3.4.2 mjf if (lpc->lpc_size != LPCCF_SIZE_DEFAULT)
150 1.3.4.2 mjf aprint_normal(" size %#lx", lpc->lpc_size);
151 1.3.4.2 mjf if (lpc->lpc_intr != LPCCF_INTR_DEFAULT)
152 1.3.4.2 mjf aprint_normal(" intr %d", lpc->lpc_intr);
153 1.3.4.2 mjf
154 1.3.4.2 mjf return UNCONF;
155 1.3.4.2 mjf }
156 1.3.4.2 mjf
157 1.3.4.2 mjf /*
158 1.3.4.2 mjf * LPC bus ops
159 1.3.4.2 mjf */
160 1.3.4.2 mjf
161 1.3.4.2 mjf static uint8_t
162 1.3.4.2 mjf it8712_pnp_read(lpctag_t tag, int ldn, uint index)
163 1.3.4.2 mjf {
164 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
165 1.3.4.2 mjf bus_space_tag_t iot = sc->sc_iot;
166 1.3.4.2 mjf bus_space_handle_t ioh = sc->sc_ioh;
167 1.3.4.2 mjf
168 1.3.4.2 mjf if (ldn != GEMINI_LPC_LDN_ALL) {
169 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
170 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
171 1.3.4.2 mjf }
172 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, index);
173 1.3.4.2 mjf return bus_space_read_1(iot, ioh, IT8712_DATA);
174 1.3.4.2 mjf }
175 1.3.4.2 mjf
176 1.3.4.2 mjf static void
177 1.3.4.2 mjf it8712_pnp_write(lpctag_t tag, int ldn, uint index, uint8_t val)
178 1.3.4.2 mjf {
179 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
180 1.3.4.2 mjf bus_space_tag_t iot = sc->sc_iot;
181 1.3.4.2 mjf bus_space_handle_t ioh = sc->sc_ioh;
182 1.3.4.2 mjf
183 1.3.4.2 mjf if (ldn != GEMINI_LPC_LDN_ALL) {
184 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
185 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
186 1.3.4.2 mjf }
187 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, index);
188 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_DATA, val);
189 1.3.4.2 mjf }
190 1.3.4.2 mjf
191 1.3.4.2 mjf static void
192 1.3.4.2 mjf it8712_pnp_enter(lpctag_t tag)
193 1.3.4.2 mjf {
194 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
195 1.3.4.2 mjf bus_space_tag_t iot = sc->sc_iot;
196 1.3.4.2 mjf bus_space_handle_t ioh = sc->sc_ioh;
197 1.3.4.2 mjf
198 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, 0x87);
199 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, 0x01);
200 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
201 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
202 1.3.4.2 mjf }
203 1.3.4.2 mjf
204 1.3.4.2 mjf static void
205 1.3.4.2 mjf it8712_pnp_exit(lpctag_t tag)
206 1.3.4.2 mjf {
207 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
208 1.3.4.2 mjf bus_space_tag_t iot = sc->sc_iot;
209 1.3.4.2 mjf bus_space_handle_t ioh = sc->sc_ioh;
210 1.3.4.2 mjf
211 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_CFGCTL);
212 1.3.4.2 mjf bus_space_write_1(iot, ioh, IT8712_DATA, CFGCTL_WAITKEY);
213 1.3.4.2 mjf }
214 1.3.4.2 mjf
215 1.3.4.2 mjf static void *
216 1.3.4.2 mjf it8712_intr_establish(lpctag_t tag, uint intr, int ipl, int ist,
217 1.3.4.2 mjf int (*func)(void *), void *arg)
218 1.3.4.2 mjf {
219 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
220 1.3.4.2 mjf void *ih;
221 1.3.4.2 mjf
222 1.3.4.2 mjf ih = gemini_lpchc_intr_establish(sc->sc_lpchctag, intr, ipl, ist, func, arg);
223 1.3.4.2 mjf
224 1.3.4.2 mjf return ih;
225 1.3.4.2 mjf }
226 1.3.4.2 mjf
227 1.3.4.2 mjf static void
228 1.3.4.2 mjf it8712_intr_disestablish(lpctag_t tag, void *ih)
229 1.3.4.2 mjf {
230 1.3.4.2 mjf gemini_lpc_softc_t *sc = tag;
231 1.3.4.2 mjf
232 1.3.4.2 mjf gemini_lpchc_intr_disestablish(sc->sc_lpchctag, ih);
233 1.3.4.2 mjf }
234