tspld.c revision 1.23 1 /* $NetBSD: tspld.c,v 1.23 2012/11/12 18:00:40 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2004 Jesse Off
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: tspld.c,v 1.23 2012/11/12 18:00:40 skrll Exp $");
32
33 #include <sys/param.h>
34 #include <sys/callout.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/wdog.h>
40
41 #include <sys/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/autoconf.h>
44 #include "isa.h"
45 #if NISA > 0
46 #include <dev/isa/isavar.h>
47 #include <machine/isa_machdep.h>
48 #endif
49
50 #include <evbarm/tsarm/tsarmreg.h>
51 #include <evbarm/tsarm/tspldvar.h>
52 #include <arm/ep93xx/ep93xxvar.h>
53 #include <arm/ep93xx/ep93xxreg.h>
54 #include <arm/ep93xx/epgpioreg.h>
55 #include <arm/arm32/machdep.h>
56 #include <arm/cpufunc.h>
57 #include <dev/sysmon/sysmonvar.h>
58
59 int tspldmatch (device_t, cfdata_t, void *);
60 void tspldattach (device_t, device_t, void *);
61 static int tspld_wdog_setmode (struct sysmon_wdog *);
62 static int tspld_wdog_tickle (struct sysmon_wdog *);
63 int tspld_search (device_t, cfdata_t, const int *, void *);
64 int tspld_print (void *, const char *);
65 void boardtemp_poll (void *);
66
67 struct tspld_softc {
68 bus_space_tag_t sc_iot;
69 bus_space_handle_t sc_wdogfeed_ioh;
70 bus_space_handle_t sc_wdogctrl_ioh;
71 struct sysmon_wdog sc_wdog;
72 bus_space_handle_t sc_ssph;
73 bus_space_handle_t sc_gpioh;
74 unsigned const char * sc_com2mode;
75 unsigned const char * sc_model;
76 unsigned char sc_pldrev[4];
77 uint32_t sc_rs485;
78 uint32_t sc_adc;
79 uint32_t sc_jp[6];
80 uint32_t sc_blaster_present;
81 uint32_t sc_blaster_boot;
82 uint32_t boardtemp;
83 uint32_t boardtemp_5s;
84 uint32_t boardtemp_30s;
85 struct callout boardtemp_callout;
86 };
87
88 CFATTACH_DECL_NEW(tspld, sizeof(struct tspld_softc),
89 tspldmatch, tspldattach, NULL, NULL);
90
91 void tspld_callback(device_t);
92
93 #define GPIO_GET(x) bus_space_read_4(sc->sc_iot, sc->sc_gpioh, \
94 (EP93XX_GPIO_ ## x))
95
96 #define GPIO_SET(x, y) bus_space_write_4(sc->sc_iot, sc->sc_gpioh, \
97 (EP93XX_GPIO_ ## x), (y))
98
99 #define GPIO_SETBITS(x, y) bus_space_write_4(sc->sc_iot, sc->sc_gpioh, \
100 (EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
101
102 #define GPIO_CLEARBITS(x, y) bus_space_write_4(sc->sc_iot, sc->sc_gpioh, \
103 (EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
104
105 #define SSP_GET(x) bus_space_read_4(sc->sc_iot, sc->sc_ssph, \
106 (EP93XX_SSP_ ## x))
107
108 #define SSP_SET(x, y) bus_space_write_4(sc->sc_iot, sc->sc_ssph, \
109 (EP93XX_SSP_ ## x), (y))
110
111 #define SSP_SETBITS(x, y) bus_space_write_4(sc->sc_iot, sc->sc_ssph, \
112 (EP93XX_SSP_ ## x), SSP_GET(x) | (y))
113
114 #define SSP_CLEARBITS(x, y) bus_space_write_4(sc->sc_iot, sc->sc_ssph, \
115 (EP93XX_SSP_ ## x), SSP_GET(x) & (~(y)))
116
117 int
118 tspldmatch(device_t parent, cfdata_t match, void *aux)
119 {
120
121 return 1;
122 }
123
124 void
125 boardtemp_poll(void *arg)
126 {
127 struct tspld_softc *sc = arg;
128 uint16_t val;
129
130 /* Disable chip select */
131 GPIO_SET(PFDDR, 0x0);
132
133 val = SSP_GET(SSPDR) & 0xffff;
134 sc->boardtemp = ((int16_t)val >> 3) * 62500;
135 sc->boardtemp_5s = sc->boardtemp_5s / 20 * 19 + sc->boardtemp / 20;
136 sc->boardtemp_30s = sc->boardtemp_30s / 120 * 119 + sc->boardtemp / 120;
137
138 callout_schedule(&sc->boardtemp_callout, hz / 4);
139
140 /* Enable chip select */
141 GPIO_SET(PFDDR, 0x4);
142
143 /* Send read command */
144 SSP_SET(SSPDR, 0x8000);
145 }
146
147 void
148 tspldattach(device_t parent, device_t self, void *aux)
149 {
150 int i, rev, features, jp, model;
151 struct tspld_softc *sc = device_private(self);
152 bus_space_handle_t ioh;
153 const struct sysctlnode *node;
154
155 if (sysctl_createv(NULL, 0, NULL, NULL,
156 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
157 NULL, NULL, 0, NULL, 0,
158 CTL_HW, CTL_EOL) != 0) {
159 printf("%s: could not create sysctl\n",
160 device_xname(self));
161 return;
162 }
163 if (sysctl_createv(NULL, 0, NULL, &node,
164 0, CTLTYPE_NODE, device_xname(self),
165 NULL,
166 NULL, 0, NULL, 0,
167 CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
168 printf("%s: could not create sysctl\n",
169 device_xname(self));
170 return;
171 }
172
173 sc->sc_iot = &ep93xx_bs_tag;
174 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_MODEL, 2, 0,
175 &ioh);
176 model = bus_space_read_2(sc->sc_iot, ioh, 0) & 0x7;
177 sc->sc_model = (model ? "TS-7250" : "TS-7200");
178 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
179 0, CTLTYPE_STRING, "boardmodel",
180 SYSCTL_DESCR("Technologic Systems board model"),
181 NULL, 0, __UNCONST(sc->sc_model), 0,
182 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
183 != 0) {
184 printf("%s: could not create sysctl\n",
185 device_xname(self));
186 return;
187 }
188 bus_space_unmap(sc->sc_iot, ioh, 2);
189
190 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_PLDREV, 2, 0,
191 &ioh);
192 rev = bus_space_read_2(sc->sc_iot, ioh, 0) & 0x7;
193 rev = 'A' + rev - 1;
194 sc->sc_pldrev[0] = rev;
195 sc->sc_pldrev[1] = 0;
196 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
197 0, CTLTYPE_STRING, "pldrev",
198 SYSCTL_DESCR("CPLD revision"),
199 NULL, 0, sc->sc_pldrev, 0,
200 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
201 != 0) {
202 printf("%s: could not create sysctl\n",
203 device_xname(self));
204 return;
205 }
206 bus_space_unmap(sc->sc_iot, ioh, 2);
207
208 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_FEATURES, 2, 0,
209 &ioh);
210 features = bus_space_read_2(sc->sc_iot, ioh, 0) & 0x7;
211 bus_space_unmap(sc->sc_iot, ioh, 2);
212
213 bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_STATUS1, 1, 0,
214 &ioh);
215 i = bus_space_read_1(sc->sc_iot, ioh, 0) & 0x1f;
216 jp = (~((i & 0x18) >> 1) & 0xc) | (i & 0x3);
217 bus_space_unmap(sc->sc_iot, ioh, 1);
218
219 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
220 0, CTLTYPE_INT, "blaster_present",
221 SYSCTL_DESCR("Whether or not a TS-9420/TS-9202 blaster board is connected"),
222 NULL, 0, &sc->sc_blaster_present, 0,
223 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
224 != 0) {
225 printf("%s: could not create sysctl\n",
226 device_xname(self));
227 return;
228 }
229 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
230 0, CTLTYPE_INT, "blaster_boot",
231 SYSCTL_DESCR("Whether or not a blast board was used to boot"),
232 NULL, 0, &sc->sc_blaster_boot, 0,
233 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
234 != 0) {
235 printf("%s: could not create sysctl\n",
236 device_xname(self));
237 return;
238 }
239 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_STATUS2, 2, 0,
240 &ioh);
241 i = bus_space_read_2(sc->sc_iot, ioh, 0) & 0x6;
242 sc->sc_blaster_boot = sc->sc_blaster_present = 0;
243 if (i & 0x2)
244 sc->sc_blaster_boot = 1;
245 if (i & 0x4)
246 sc->sc_blaster_present = 1;
247 jp |= (i << 4);
248 bus_space_unmap(sc->sc_iot, ioh, 1);
249
250 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
251 0, CTLTYPE_INT, "rs485_avail",
252 SYSCTL_DESCR("RS485 level driver for COM2 available"),
253 NULL, 0, &sc->sc_rs485, 0,
254 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
255 != 0) {
256 printf("%s: could not create sysctl\n",
257 device_xname(self));
258 return;
259 }
260 sc->sc_com2mode = "rs232";
261 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
262 0, CTLTYPE_STRING, "com2_mode",
263 SYSCTL_DESCR("line driver type for COM2"),
264 NULL, 0, __UNCONST(sc->sc_com2mode), 0,
265 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
266 != 0) {
267 printf("%s: could not create sysctl\n",
268 device_xname(self));
269 return;
270 }
271 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
272 0, CTLTYPE_INT, "max197adc_avail",
273 SYSCTL_DESCR("Maxim 197 Analog to Digital Converter available"),
274 NULL, 0, &sc->sc_adc, 0,
275 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
276 != 0) {
277 printf("%s: could not create sysctl\n",
278 device_xname(self));
279 return;
280 }
281 printf(": Technologic Systems %s rev %c, features 0x%x",
282 sc->sc_model, rev, features);
283 sc->sc_adc = sc->sc_rs485 = 0;
284 if (features == 0x1) {
285 printf("<MAX197-ADC>");
286 sc->sc_adc = 1;
287 } else if (features == 0x2) {
288 printf("<RS485>");
289 sc->sc_rs485 = 1;
290 } else if (features == 0x3) {
291 printf("<MAX197-ADC,RS485>");
292 sc->sc_adc = sc->sc_rs485 = 1;
293 }
294 printf("\n");
295 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
296 0, CTLTYPE_INT, "jp1",
297 SYSCTL_DESCR("onboard jumper setting"),
298 NULL, 0, &sc->sc_jp[0], 0,
299 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
300 != 0) {
301 printf("%s: could not create sysctl\n",
302 device_xname(self));
303 return;
304 }
305 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
306 0, CTLTYPE_INT, "jp2",
307 SYSCTL_DESCR("onboard jumper setting"),
308 NULL, 0, &sc->sc_jp[1], 0,
309 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
310 != 0) {
311 printf("%s: could not create sysctl\n",
312 device_xname(self));
313 return;
314 }
315 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
316 0, CTLTYPE_INT, "jp3",
317 SYSCTL_DESCR("onboard jumper setting"),
318 NULL, 0, &sc->sc_jp[2], 0,
319 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
320 != 0) {
321 printf("%s: could not create sysctl\n",
322 device_xname(self));
323 return;
324 }
325 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
326 0, CTLTYPE_INT, "jp4",
327 SYSCTL_DESCR("onboard jumper setting"),
328 NULL, 0, &sc->sc_jp[3], 0,
329 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
330 != 0) {
331 printf("%s: could not create sysctl\n",
332 device_xname(self));
333 return;
334 }
335 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
336 0, CTLTYPE_INT, "jp5",
337 SYSCTL_DESCR("onboard jumper setting"),
338 NULL, 0, &sc->sc_jp[4], 0,
339 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
340 != 0) {
341 printf("%s: could not create sysctl\n",
342 device_xname(self));
343 return;
344 }
345 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
346 0, CTLTYPE_INT, "jp6",
347 SYSCTL_DESCR("onboard jumper setting"),
348 NULL, 0, &sc->sc_jp[5], 0,
349 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
350 != 0) {
351 printf("%s: could not create sysctl\n",
352 device_xname(self));
353 return;
354 }
355 printf("%s: jumpers 0x%x", device_xname(self), jp);
356 if (jp) {
357 printf("<");
358 for(i = 0; i < 5; i++) {
359 if (jp & (1 << i)) {
360 sc->sc_jp[i + 1] = 1;
361 printf("JP%d", i + 2);
362 jp &= ~(1 << i);
363 if (jp) printf(",");
364 } else {
365 sc->sc_jp[i + 2] = 0;
366 }
367 }
368 printf(">");
369 }
370 printf("\n");
371
372
373 bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_SSP,
374 EP93XX_APB_SSP_SIZE, 0, &sc->sc_ssph);
375 bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
376 EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh);
377 SSP_SETBITS(SSPCR1, 0x10);
378 SSP_SET(SSPCR0, 0xf);
379 SSP_SET(SSPCPSR, 0xfe);
380 SSP_CLEARBITS(SSPCR1, 0x10);
381 SSP_SETBITS(SSPCR1, 0x10);
382 GPIO_SET(PFDR, 0x0);
383 callout_init(&sc->boardtemp_callout, 0);
384 callout_setfunc(&sc->boardtemp_callout, boardtemp_poll, sc);
385 boardtemp_poll(sc);
386 delay(1000);
387 boardtemp_poll(sc);
388 sc->boardtemp_5s = sc->boardtemp_30s = sc->boardtemp;
389 #define DEGF(c) ((c) * 9 / 5 + 32000000)
390 printf("%s: board temperature %d.%02d degC (%d.%02d degF)\n",
391 device_xname(self),
392 sc->boardtemp / 1000000, sc->boardtemp / 10000 % 100,
393 DEGF(sc->boardtemp) / 1000000, DEGF(sc->boardtemp) / 10000 % 100);
394 #undef DEGF
395 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
396 0, CTLTYPE_INT, "board_temp",
397 SYSCTL_DESCR("board temperature in micro degrees Celsius"),
398 NULL, 0, &sc->boardtemp, 0,
399 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
400 != 0) {
401 printf("%s: could not create sysctl\n",
402 device_xname(self));
403 return;
404 }
405
406 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
407 0, CTLTYPE_INT, "board_temp_5s",
408 SYSCTL_DESCR("5 second average board temperature in micro degrees Celsius"),
409 NULL, 0, &sc->boardtemp_5s, 0,
410 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
411 != 0) {
412 printf("%s: could not create sysctl\n",
413 device_xname(self));
414 return;
415 }
416
417 if ((i = sysctl_createv(NULL, 0, NULL, NULL,
418 0, CTLTYPE_INT, "board_temp_30s",
419 SYSCTL_DESCR("30 second average board temperature in micro degrees Celsius"),
420 NULL, 0, &sc->boardtemp_30s, 0,
421 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
422 != 0) {
423 printf("%s: could not create sysctl\n",
424 device_xname(self));
425 return;
426 }
427
428 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_WDOGCTRL, 2, 0,
429 &sc->sc_wdogctrl_ioh);
430 bus_space_map(sc->sc_iot, TS7XXX_IO16_HWBASE + TS7XXX_WDOGFEED, 2, 0,
431 &sc->sc_wdogfeed_ioh);
432
433 sc->sc_wdog.smw_name = device_xname(self);
434 sc->sc_wdog.smw_cookie = sc;
435 sc->sc_wdog.smw_setmode = tspld_wdog_setmode;
436 sc->sc_wdog.smw_tickle = tspld_wdog_tickle;
437 sc->sc_wdog.smw_period = 8;
438 sysmon_wdog_register(&sc->sc_wdog);
439 tspld_wdog_setmode(&sc->sc_wdog);
440
441 /* Set the on board peripherals bus callback */
442 config_defer(self, tspld_callback);
443 }
444
445 int
446 tspld_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
447 {
448 struct tspld_softc *sc = device_private(parent);
449 struct tspld_attach_args sa;
450
451 sa.ta_iot = sc->sc_iot;
452
453 if (config_match(parent, cf, &sa) > 0)
454 config_attach(parent, cf, &sa, tspld_print);
455
456 return (0);
457 }
458
459 int
460 tspld_print(void *aux, const char *name)
461 {
462
463 return (UNCONF);
464 }
465
466 void
467 tspld_callback(device_t self)
468 {
469 #if NISA > 0
470 extern void isa_bs_mallocok(void);
471 struct isabus_attach_args iba;
472
473 /*
474 * Attach the ISA bus behind this bridge.
475 */
476 memset(&iba, 0, sizeof(iba));
477 iba.iba_iot = &isa_io_bs_tag;
478 iba.iba_memt = &isa_mem_bs_tag;
479 isa_bs_mallocok();
480 config_found_ia(self, "isabus", &iba, isabusprint);
481 #endif
482 /*
483 * Attach each devices
484 */
485 config_search_ia(tspld_search, self, "tspldbus", NULL);
486
487 }
488
489 static int
490 tspld_wdog_tickle(struct sysmon_wdog *smw)
491 {
492 struct tspld_softc *sc = (struct tspld_softc *)smw->smw_cookie;
493
494 bus_space_write_2(sc->sc_iot, sc->sc_wdogfeed_ioh, 0, 0x5);
495 return 0;
496 }
497
498 static int
499 tspld_wdog_setmode(struct sysmon_wdog *smw)
500 {
501 int i, ret = 0;
502 struct tspld_softc *sc = (struct tspld_softc *)smw->smw_cookie;
503
504 i = disable_interrupts(I32_bit|F32_bit);
505 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
506 bus_space_write_2(sc->sc_iot, sc->sc_wdogfeed_ioh, 0, 0x5);
507 bus_space_write_2(sc->sc_iot, sc->sc_wdogctrl_ioh, 0, 0);
508 } else {
509 if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
510 smw->smw_period = 8;
511 }
512
513 bus_space_write_2(sc->sc_iot, sc->sc_wdogfeed_ioh, 0, 0x5);
514 switch (smw->smw_period) {
515 case 1:
516 bus_space_write_2(sc->sc_iot, sc->sc_wdogctrl_ioh, 0,
517 0x3);
518 break;
519 case 2:
520 bus_space_write_2(sc->sc_iot, sc->sc_wdogctrl_ioh, 0,
521 0x5);
522 break;
523 case 4:
524 bus_space_write_2(sc->sc_iot, sc->sc_wdogctrl_ioh, 0,
525 0x6);
526 break;
527 case 8:
528 bus_space_write_2(sc->sc_iot, sc->sc_wdogctrl_ioh, 0,
529 0x7);
530 break;
531 default:
532 ret = EINVAL;
533 }
534 }
535 restore_interrupts(i);
536 return ret;
537 }
538