zssp.c revision 1.5 1 1.5 nonaka /* $NetBSD: zssp.c,v 1.5 2009/01/29 12:51:15 nonaka Exp $ */
2 1.1 ober /* $OpenBSD: zaurus_ssp.c,v 1.6 2005/04/08 21:58:49 uwe Exp $ */
3 1.1 ober
4 1.1 ober /*
5 1.1 ober * Copyright (c) 2005 Uwe Stuehler <uwe (at) bsdx.de>
6 1.1 ober *
7 1.1 ober * Permission to use, copy, modify, and distribute this software for any
8 1.1 ober * purpose with or without fee is hereby granted, provided that the above
9 1.1 ober * copyright notice and this permission notice appear in all copies.
10 1.1 ober *
11 1.1 ober * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 ober * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 ober * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 ober * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 ober * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 ober * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 ober * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 ober */
19 1.1 ober
20 1.1 ober #include <sys/cdefs.h>
21 1.5 nonaka __KERNEL_RCSID(0, "$NetBSD: zssp.c,v 1.5 2009/01/29 12:51:15 nonaka Exp $");
22 1.1 ober
23 1.1 ober #include <sys/param.h>
24 1.1 ober #include <sys/systm.h>
25 1.1 ober #include <sys/device.h>
26 1.1 ober
27 1.1 ober #include <machine/bus.h>
28 1.1 ober
29 1.1 ober #include <arm/xscale/pxa2x0reg.h>
30 1.1 ober #include <arm/xscale/pxa2x0var.h>
31 1.1 ober #include <arm/xscale/pxa2x0_gpio.h>
32 1.1 ober
33 1.1 ober #include <zaurus/dev/zsspvar.h>
34 1.1 ober #include <zaurus/zaurus/zaurus_var.h>
35 1.1 ober
36 1.1 ober #define GPIO_ADS7846_CS_C3000 14 /* SSP SFRM */
37 1.1 ober #define GPIO_MAX1111_CS_C3000 20
38 1.1 ober #define GPIO_TG_CS_C3000 53
39 1.1 ober
40 1.2 nonaka #define SSCR0_ADS7846_C3000 0x06ab /* 12bit/Microwire/div by 7 */
41 1.2 nonaka #define SSCR0_MAX1111 0x0387
42 1.1 ober #define SSCR0_LZ9JG18 0x01ab
43 1.1 ober
44 1.1 ober struct zssp_softc {
45 1.4 nonaka device_t sc_dev;
46 1.1 ober bus_space_tag_t sc_iot;
47 1.1 ober bus_space_handle_t sc_ioh;
48 1.1 ober };
49 1.1 ober
50 1.4 nonaka static int zssp_match(device_t, cfdata_t, void *);
51 1.4 nonaka static void zssp_attach(device_t, device_t, void *);
52 1.2 nonaka
53 1.4 nonaka CFATTACH_DECL_NEW(zssp, sizeof(struct zssp_softc),
54 1.2 nonaka zssp_match, zssp_attach, NULL, NULL);
55 1.2 nonaka
56 1.1 ober static void zssp_init(void);
57 1.1 ober static void zssp_powerhook(int, void *);
58 1.1 ober
59 1.2 nonaka static struct zssp_softc *zssp_sc;
60 1.1 ober
61 1.1 ober static int
62 1.4 nonaka zssp_match(device_t parent, cfdata_t cf, void *aux)
63 1.1 ober {
64 1.1 ober
65 1.2 nonaka if (zssp_sc != NULL)
66 1.2 nonaka return 0;
67 1.2 nonaka
68 1.1 ober return 1;
69 1.1 ober }
70 1.1 ober
71 1.1 ober static void
72 1.4 nonaka zssp_attach(device_t parent, device_t self, void *aux)
73 1.1 ober {
74 1.4 nonaka struct zssp_softc *sc = device_private(self);
75 1.4 nonaka
76 1.4 nonaka sc->sc_dev = self;
77 1.4 nonaka zssp_sc = sc;
78 1.4 nonaka
79 1.4 nonaka aprint_normal("\n");
80 1.4 nonaka aprint_naive("\n");
81 1.1 ober
82 1.1 ober sc->sc_iot = &pxa2x0_bs_tag;
83 1.1 ober if (bus_space_map(sc->sc_iot, PXA2X0_SSP1_BASE, PXA2X0_SSP_SIZE,
84 1.1 ober 0, &sc->sc_ioh)) {
85 1.4 nonaka aprint_error_dev(sc->sc_dev, "can't map bus space\n");
86 1.1 ober return;
87 1.1 ober }
88 1.1 ober
89 1.4 nonaka if (powerhook_establish(device_xname(sc->sc_dev), zssp_powerhook, sc)
90 1.1 ober == NULL) {
91 1.4 nonaka aprint_error_dev(sc->sc_dev, "can't establish power hook\n");
92 1.1 ober }
93 1.1 ober
94 1.1 ober zssp_init();
95 1.1 ober }
96 1.1 ober
97 1.1 ober /*
98 1.1 ober * Initialize the dedicated SSP unit and disable all chip selects.
99 1.1 ober * This function is called with interrupts disabled.
100 1.1 ober */
101 1.1 ober static void
102 1.1 ober zssp_init(void)
103 1.1 ober {
104 1.1 ober struct zssp_softc *sc;
105 1.1 ober
106 1.2 nonaka KASSERT(zssp_sc != NULL);
107 1.2 nonaka sc = zssp_sc;
108 1.1 ober
109 1.1 ober pxa2x0_clkman_config(CKEN_SSP, 1);
110 1.1 ober
111 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, SSCR0_LZ9JG18);
112 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR1, 0);
113 1.1 ober
114 1.1 ober pxa2x0_gpio_set_function(GPIO_ADS7846_CS_C3000, GPIO_OUT|GPIO_SET);
115 1.1 ober pxa2x0_gpio_set_function(GPIO_MAX1111_CS_C3000, GPIO_OUT|GPIO_SET);
116 1.1 ober pxa2x0_gpio_set_function(GPIO_TG_CS_C3000, GPIO_OUT|GPIO_SET);
117 1.1 ober }
118 1.1 ober
119 1.1 ober static void
120 1.1 ober zssp_powerhook(int why, void *arg)
121 1.1 ober {
122 1.1 ober int s;
123 1.1 ober
124 1.1 ober if (why == PWR_RESUME) {
125 1.1 ober s = splhigh();
126 1.1 ober zssp_init();
127 1.1 ober splx(s);
128 1.1 ober }
129 1.1 ober }
130 1.1 ober
131 1.1 ober /*
132 1.1 ober * Transmit a single data word to one of the ICs, keep the chip selected
133 1.1 ober * afterwards, and don't wait for data to be returned in SSDR. Interrupts
134 1.1 ober * must be held off until zssp_ic_stop() gets called.
135 1.1 ober */
136 1.1 ober void
137 1.1 ober zssp_ic_start(int ic, uint32_t data)
138 1.1 ober {
139 1.1 ober struct zssp_softc *sc;
140 1.1 ober
141 1.2 nonaka KASSERT(zssp_sc != NULL);
142 1.2 nonaka sc = zssp_sc;
143 1.1 ober
144 1.1 ober /* disable other ICs */
145 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, 0);
146 1.1 ober if (ic != ZSSP_IC_ADS7846)
147 1.1 ober pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
148 1.1 ober if (ic != ZSSP_IC_LZ9JG18)
149 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
150 1.1 ober if (ic != ZSSP_IC_MAX1111)
151 1.1 ober pxa2x0_gpio_set_bit(GPIO_MAX1111_CS_C3000);
152 1.1 ober
153 1.1 ober /* activate the chosen one */
154 1.1 ober switch (ic) {
155 1.1 ober case ZSSP_IC_ADS7846:
156 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0,
157 1.1 ober SSCR0_ADS7846_C3000);
158 1.1 ober pxa2x0_gpio_clear_bit(GPIO_ADS7846_CS_C3000);
159 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR, data);
160 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
161 1.1 ober & SSSR_TNF) != SSSR_TNF)
162 1.1 ober continue; /* poll */
163 1.1 ober break;
164 1.1 ober case ZSSP_IC_LZ9JG18:
165 1.1 ober pxa2x0_gpio_clear_bit(GPIO_TG_CS_C3000);
166 1.1 ober break;
167 1.1 ober case ZSSP_IC_MAX1111:
168 1.1 ober pxa2x0_gpio_clear_bit(GPIO_MAX1111_CS_C3000);
169 1.1 ober break;
170 1.1 ober }
171 1.1 ober }
172 1.1 ober
173 1.1 ober /*
174 1.1 ober * Read the last value from SSDR and deactivate all chip-selects.
175 1.1 ober */
176 1.1 ober uint32_t
177 1.1 ober zssp_ic_stop(int ic)
178 1.1 ober {
179 1.1 ober struct zssp_softc *sc;
180 1.1 ober uint32_t rv;
181 1.1 ober
182 1.2 nonaka KASSERT(zssp_sc != NULL);
183 1.2 nonaka sc = zssp_sc;
184 1.1 ober
185 1.1 ober switch (ic) {
186 1.1 ober case ZSSP_IC_ADS7846:
187 1.1 ober /* read result of last command */
188 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
189 1.1 ober & SSSR_RNE) != SSSR_RNE)
190 1.1 ober continue; /* poll */
191 1.1 ober rv = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR);
192 1.1 ober break;
193 1.1 ober case ZSSP_IC_LZ9JG18:
194 1.1 ober case ZSSP_IC_MAX1111:
195 1.1 ober /* last value received is irrelevant or undefined */
196 1.1 ober default:
197 1.1 ober rv = 0;
198 1.1 ober break;
199 1.1 ober }
200 1.1 ober
201 1.1 ober pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
202 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
203 1.1 ober pxa2x0_gpio_set_bit(GPIO_MAX1111_CS_C3000);
204 1.1 ober
205 1.1 ober return rv;
206 1.1 ober }
207 1.1 ober
208 1.1 ober /*
209 1.1 ober * Activate one of the chip-select lines, transmit one word value in
210 1.1 ober * each direction, and deactivate the chip-select again.
211 1.1 ober */
212 1.1 ober uint32_t
213 1.1 ober zssp_ic_send(int ic, uint32_t data)
214 1.1 ober {
215 1.1 ober
216 1.1 ober switch (ic) {
217 1.1 ober case ZSSP_IC_MAX1111:
218 1.1 ober return (zssp_read_max1111(data));
219 1.1 ober case ZSSP_IC_ADS7846:
220 1.1 ober return (zssp_read_ads7846(data));
221 1.1 ober case ZSSP_IC_LZ9JG18:
222 1.1 ober zssp_write_lz9jg18(data);
223 1.1 ober return 0;
224 1.1 ober default:
225 1.5 nonaka aprint_error("zssp: zssp_ic_send: invalid IC %d\n", ic);
226 1.1 ober return 0;
227 1.1 ober }
228 1.1 ober }
229 1.1 ober
230 1.1 ober int
231 1.1 ober zssp_read_max1111(uint32_t cmd)
232 1.1 ober {
233 1.1 ober struct zssp_softc *sc;
234 1.2 nonaka int voltage[2];
235 1.2 nonaka int i;
236 1.2 nonaka int s;
237 1.1 ober
238 1.2 nonaka KASSERT(zssp_sc != NULL);
239 1.2 nonaka sc = zssp_sc;
240 1.1 ober
241 1.1 ober s = splhigh();
242 1.1 ober
243 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, 0);
244 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, SSCR0_MAX1111);
245 1.1 ober
246 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
247 1.1 ober pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
248 1.1 ober pxa2x0_gpio_clear_bit(GPIO_MAX1111_CS_C3000);
249 1.1 ober
250 1.1 ober delay(1);
251 1.1 ober
252 1.1 ober /* Send the command word and read a dummy word back. */
253 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR, cmd);
254 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
255 1.1 ober & SSSR_TNF) != SSSR_TNF)
256 1.1 ober continue; /* poll */
257 1.1 ober /* XXX is this delay necessary? */
258 1.1 ober delay(1);
259 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
260 1.1 ober & SSSR_RNE) != SSSR_RNE)
261 1.1 ober continue; /* poll */
262 1.1 ober (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR);
263 1.1 ober
264 1.1 ober for (i = 0; i < 2; i++) {
265 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR, 0);
266 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
267 1.1 ober & SSSR_TNF) != SSSR_TNF)
268 1.1 ober continue; /* poll */
269 1.1 ober /* XXX again, is this delay necessary? */
270 1.1 ober delay(1);
271 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
272 1.1 ober & SSSR_RNE) != SSSR_RNE)
273 1.1 ober continue; /* poll */
274 1.1 ober voltage[i] = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
275 1.1 ober SSP_SSDR);
276 1.1 ober }
277 1.1 ober
278 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
279 1.1 ober pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
280 1.1 ober pxa2x0_gpio_set_bit(GPIO_MAX1111_CS_C3000);
281 1.1 ober
282 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, 0);
283 1.1 ober
284 1.1 ober splx(s);
285 1.1 ober
286 1.1 ober /* XXX no idea what this means, but it's what Linux would do. */
287 1.1 ober if ((voltage[0] & 0xc0) != 0 || (voltage[1] & 0x3f) != 0) {
288 1.1 ober voltage[0] = -1;
289 1.1 ober } else {
290 1.1 ober voltage[0] = ((voltage[0] << 2) & 0xfc) |
291 1.1 ober ((voltage[1] >> 6) & 0x03);
292 1.1 ober }
293 1.1 ober
294 1.1 ober return voltage[0];
295 1.1 ober }
296 1.1 ober
297 1.1 ober /* XXX - only does CS_ADS7846 */
298 1.1 ober uint32_t
299 1.1 ober zssp_read_ads7846(uint32_t cmd)
300 1.1 ober {
301 1.1 ober struct zssp_softc *sc;
302 1.1 ober unsigned int cr0;
303 1.2 nonaka uint32_t val;
304 1.1 ober int s;
305 1.1 ober
306 1.2 nonaka if (zssp_sc == NULL) {
307 1.1 ober printf("zssp_read_ads7846: not configured\n");
308 1.1 ober return 0;
309 1.1 ober }
310 1.2 nonaka sc = zssp_sc;
311 1.1 ober
312 1.2 nonaka s = splhigh();
313 1.1 ober
314 1.2 nonaka if (ZAURUS_ISC3000) {
315 1.2 nonaka cr0 = SSCR0_ADS7846_C3000;
316 1.1 ober } else {
317 1.2 nonaka cr0 = 0x00ab;
318 1.1 ober }
319 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, 0);
320 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSCR0, cr0);
321 1.1 ober
322 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
323 1.1 ober pxa2x0_gpio_set_bit(GPIO_MAX1111_CS_C3000);
324 1.1 ober pxa2x0_gpio_clear_bit(GPIO_ADS7846_CS_C3000);
325 1.1 ober
326 1.1 ober bus_space_write_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR, cmd);
327 1.1 ober
328 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
329 1.1 ober & SSSR_TNF) != SSSR_TNF)
330 1.1 ober continue; /* poll */
331 1.1 ober
332 1.1 ober delay(1);
333 1.1 ober
334 1.1 ober while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSSR)
335 1.1 ober & SSSR_RNE) != SSSR_RNE)
336 1.1 ober continue; /* poll */
337 1.1 ober
338 1.1 ober val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SSP_SSDR);
339 1.1 ober
340 1.2 nonaka pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
341 1.1 ober
342 1.1 ober splx(s);
343 1.1 ober
344 1.1 ober return val;
345 1.1 ober }
346 1.1 ober
347 1.1 ober void
348 1.1 ober zssp_write_lz9jg18(uint32_t data)
349 1.1 ober {
350 1.1 ober int sclk_pin, sclk_fn;
351 1.1 ober int sfrm_pin, sfrm_fn;
352 1.1 ober int txd_pin, txd_fn;
353 1.1 ober int rxd_pin, rxd_fn;
354 1.1 ober int i;
355 1.2 nonaka int s;
356 1.1 ober
357 1.1 ober /* XXX this creates a DAC command from a backlight duty value. */
358 1.1 ober data = 0x40 | (data & 0x1f);
359 1.1 ober
360 1.1 ober if (ZAURUS_ISC3000) {
361 1.1 ober sclk_pin = 19;
362 1.1 ober sfrm_pin = 14;
363 1.1 ober txd_pin = 87;
364 1.1 ober rxd_pin = 86;
365 1.1 ober } else {
366 1.1 ober sclk_pin = 23;
367 1.1 ober sfrm_pin = 24;
368 1.1 ober txd_pin = 25;
369 1.1 ober rxd_pin = 26;
370 1.1 ober }
371 1.1 ober
372 1.1 ober s = splhigh();
373 1.1 ober
374 1.1 ober sclk_fn = pxa2x0_gpio_get_function(sclk_pin);
375 1.1 ober sfrm_fn = pxa2x0_gpio_get_function(sfrm_pin);
376 1.1 ober txd_fn = pxa2x0_gpio_get_function(txd_pin);
377 1.1 ober rxd_fn = pxa2x0_gpio_get_function(rxd_pin);
378 1.1 ober
379 1.1 ober pxa2x0_gpio_set_function(sfrm_pin, GPIO_OUT | GPIO_SET);
380 1.1 ober pxa2x0_gpio_set_function(sclk_pin, GPIO_OUT | GPIO_CLR);
381 1.1 ober pxa2x0_gpio_set_function(txd_pin, GPIO_OUT | GPIO_CLR);
382 1.1 ober pxa2x0_gpio_set_function(rxd_pin, GPIO_IN);
383 1.1 ober
384 1.1 ober pxa2x0_gpio_set_bit(GPIO_MAX1111_CS_C3000);
385 1.1 ober pxa2x0_gpio_set_bit(GPIO_ADS7846_CS_C3000);
386 1.1 ober pxa2x0_gpio_clear_bit(GPIO_TG_CS_C3000);
387 1.1 ober
388 1.1 ober delay(10);
389 1.1 ober
390 1.1 ober for (i = 0; i < 8; i++) {
391 1.1 ober if (data & 0x80)
392 1.1 ober pxa2x0_gpio_set_bit(txd_pin);
393 1.1 ober else
394 1.1 ober pxa2x0_gpio_clear_bit(txd_pin);
395 1.1 ober delay(10);
396 1.1 ober pxa2x0_gpio_set_bit(sclk_pin);
397 1.1 ober delay(10);
398 1.1 ober pxa2x0_gpio_clear_bit(sclk_pin);
399 1.1 ober delay(10);
400 1.1 ober data <<= 1;
401 1.1 ober }
402 1.1 ober
403 1.1 ober pxa2x0_gpio_clear_bit(txd_pin);
404 1.1 ober pxa2x0_gpio_set_bit(GPIO_TG_CS_C3000);
405 1.1 ober
406 1.1 ober pxa2x0_gpio_set_function(sclk_pin, sclk_fn);
407 1.1 ober pxa2x0_gpio_set_function(sfrm_pin, sfrm_fn);
408 1.1 ober pxa2x0_gpio_set_function(txd_pin, txd_fn);
409 1.1 ober pxa2x0_gpio_set_function(rxd_pin, rxd_fn);
410 1.1 ober
411 1.1 ober splx(s);
412 1.1 ober }
413