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