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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/* $OpenBSD: tctrl.c,v 1.5 2003/05/16 18:40:32 miod Exp $ */
/* $NetBSD: tctrl.c,v 1.2 1999/08/11 00:46:06 matt Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matt Thomas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/device.h>
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <sparc/dev/ts102reg.h>
#include <sparc/dev/tctrlvar.h>
const char *tctrl_ext_statuses[16] = {
"main power available",
"internal battery attached",
"external battery attached",
"external VGA attached",
"external keyboard attached",
"external mouse attached",
"lid down",
"internal battery charging",
"external battery charging",
"internal battery discharging",
"external battery discharging",
};
struct tctrl_softc {
struct device sc_dev;
struct uctrl_regs *sc_regs;
struct intrhand sc_ih;
int sc_node;
unsigned int sc_junk;
unsigned int sc_ext_status;
unsigned int sc_pending;
#define TCTRL_SEND_BITPORT 0x0001
#define TCTRL_SEND_POWEROFF 0x0002
#define TCTRL_SEND_RD_EXT_STATUS 0x0004
#define TCTRL_SEND_RD_EVENT_STATUS 0x0008
#define TCTRL_SEND_BITPORT_NOP 0x0010
#define TCTRL_SEND_BRIGHTNESS 0x0020
#define TCTRL_SEND_BRIGHTNESS_NOP 0x0040
enum { TCTRL_IDLE, TCTRL_ARGS,
TCTRL_ACK, TCTRL_DATA } sc_state;
u_int8_t sc_cmdbuf[16];
u_int8_t sc_rspbuf[16];
u_int8_t sc_bitport;
u_int8_t sc_tft_on;
u_int8_t sc_brightness;
u_int8_t sc_op;
u_int8_t sc_cmdoff;
u_int8_t sc_cmdlen;
u_int8_t sc_rspoff;
u_int8_t sc_rsplen;
struct evcnt sc_intrcnt; /* interrupt counting */
};
int tctrl_match(struct device *, void *, void *);
void tctrl_attach(struct device *, struct device *, void *);
void tctrl_write_data(struct tctrl_softc *, u_int8_t);
u_int8_t tctrl_read_data(struct tctrl_softc *);
int tctrl_intr(void *);
void tctrl_setup_bitport(struct tctrl_softc *, int);
void tctrl_setup_brightness(struct tctrl_softc *, int, int);
void tctrl_process_response(struct tctrl_softc *);
struct cfattach tctrl_ca = {
sizeof(struct tctrl_softc), tctrl_match, tctrl_attach
};
struct cfdriver tctrl_cd = {
NULL, "tctrl", DV_DULL
};
int
tctrl_match(parent, vcf, aux)
struct device *parent;
void *vcf;
void *aux;
{
struct confargs *ca = aux;
struct romaux *ra = &ca->ca_ra;
/*
* Tadpole 3GX/3GS uses "uctrl" for the Tadpole Microcontroller
* (which is really part of the TS102 PCMCIA controller, but there
* exists a distinct OpenProm node for the microcontroller interface).
*/
if (strcmp("uctrl", ra->ra_name))
return (0);
return (1);
}
void
tctrl_attach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct confargs *ca = aux;
struct tctrl_softc *sc = (void *)self;
int pri;
unsigned int i, v;
/*
* We're living on a sbus slot that looks like an obio that
* looks like an sbus slot.
*/
if (ca->ca_ra.ra_nintr != 1) {
printf(": expected 1 interrupt, got %d\n",
ca->ca_ra.ra_nintr);
return;
}
pri = ca->ca_ra.ra_intr[0].int_pri;
if (ca->ca_ra.ra_nreg != 1) {
printf(": expected 1 register, got %d\n",
ca->ca_ra.ra_nreg);
return;
}
sc->sc_regs = mapiodev(&(ca->ca_ra.ra_reg[0]), 0,
ca->ca_ra.ra_reg[0].rr_len);
sc->sc_node = ca->ca_ra.ra_node;
printf(" pri %d\n", pri);
sc->sc_tft_on = 1;
/* clear any pending data.
*/
for (i = 0; i < 10000; i++) {
if ((TS102_UCTRL_STS_RXNE_STA & sc->sc_regs->stat) == 0)
break;
v = sc->sc_regs->data;
sc->sc_regs->stat = TS102_UCTRL_STS_RXNE_STA;
}
sc->sc_ih.ih_fun = tctrl_intr;
sc->sc_ih.ih_arg = sc;
intr_establish(pri, &sc->sc_ih, -1);
evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
/* See what the external status is
*/
sc->sc_pending |= TCTRL_SEND_RD_EXT_STATUS;
do {
tctrl_intr(sc);
} while (sc->sc_state != TCTRL_IDLE);
if (sc->sc_ext_status != 0) {
const char *sep;
printf("%s: ", sc->sc_dev.dv_xname);
v = sc->sc_ext_status;
for (i = 0, sep = ""; v != 0; i++, v >>= 1) {
if (v & 1) {
printf("%s%s", sep, tctrl_ext_statuses[i]);
sep = ", ";
}
}
printf("\n");
}
/*
* Get a few status values.
*/
sc->sc_pending |= TCTRL_SEND_BITPORT_NOP;
do {
tctrl_intr(sc);
} while (sc->sc_state != TCTRL_IDLE);
sc->sc_pending |= TCTRL_SEND_BRIGHTNESS_NOP;
do {
tctrl_intr(sc);
} while (sc->sc_state != TCTRL_IDLE);
sc->sc_regs->intr = TS102_UCTRL_INT_RXNE_REQ|TS102_UCTRL_INT_RXNE_MSK;
}
int
tctrl_intr(void *arg)
{
struct tctrl_softc *sc = arg;
unsigned int v, d;
int progress = 0;
again:
/* find out the cause(s) of the interrupt */
v = sc->sc_regs->stat;
/* clear the cause(s) of the interrupt */
sc->sc_regs->stat = v;
v &= ~(TS102_UCTRL_STS_RXO_STA|TS102_UCTRL_STS_TXE_STA);
if (sc->sc_cmdoff >= sc->sc_cmdlen) {
v &= ~TS102_UCTRL_STS_TXNF_STA;
}
if ((v == 0) && (sc->sc_pending == 0 || sc->sc_state != TCTRL_IDLE)) {
return (progress);
}
progress = 1;
if (v & TS102_UCTRL_STS_RXNE_STA) {
d = tctrl_read_data(sc);
switch (sc->sc_state) {
case TCTRL_IDLE:
if (d == 0xfa) {
sc->sc_pending |= TCTRL_SEND_RD_EVENT_STATUS;
} else {
printf("%s: (op=0x%02x): unexpected data (0x%02x)\n",
sc->sc_dev.dv_xname, sc->sc_op, d);
}
goto again;
case TCTRL_ACK:
if (d != 0xfe) {
printf("%s: (op=0x%02x): unexpected ack value (0x%02x)\n",
sc->sc_dev.dv_xname, sc->sc_op, d);
}
#if 0
printf(" ack=0x%02x", d);
#endif
sc->sc_rsplen--;
sc->sc_rspoff = 0;
sc->sc_state = sc->sc_rsplen ? TCTRL_DATA : TCTRL_IDLE;
#if 0
if (sc->sc_rsplen > 0) {
printf(" [data(%u)]", sc->sc_rsplen);
} else {
printf(" [idle]\n");
}
#endif
goto again;
case TCTRL_DATA:
sc->sc_rspbuf[sc->sc_rspoff++] = d;
#if 0
printf(" [%d]=0x%02x", sc->sc_rspoff-1, d);
#endif
if (sc->sc_rspoff == sc->sc_rsplen) {
#if 0
printf(" [idle]\n");
#endif
sc->sc_state = TCTRL_IDLE;
tctrl_process_response(sc);
}
goto again;
default:
printf("%s: (op=0x%02x): unexpected data (0x%02x) in state %d\n",
sc->sc_dev.dv_xname, sc->sc_op, d, sc->sc_state);
goto again;
}
}
if (sc->sc_state == TCTRL_IDLE) {
sc->sc_cmdoff = 0;
sc->sc_cmdlen = 0;
if (sc->sc_pending & TCTRL_SEND_POWEROFF) {
sc->sc_pending &= ~TCTRL_SEND_POWEROFF;
sc->sc_cmdbuf[0] = TS102_OP_ADMIN_POWER_OFF;
sc->sc_cmdlen = 1;
sc->sc_rsplen = 0;
} else if (sc->sc_pending & TCTRL_SEND_RD_EVENT_STATUS) {
sc->sc_pending &= ~TCTRL_SEND_RD_EVENT_STATUS;
sc->sc_cmdbuf[0] = TS102_OP_RD_EVENT_STATUS;
sc->sc_cmdlen = 1;
sc->sc_rsplen = 3;
} else if (sc->sc_pending & TCTRL_SEND_RD_EXT_STATUS) {
sc->sc_pending &= ~TCTRL_SEND_RD_EXT_STATUS;
sc->sc_cmdbuf[0] = TS102_OP_RD_EXT_STATUS;
sc->sc_cmdlen = 1;
sc->sc_rsplen = 3;
} else if (sc->sc_pending & TCTRL_SEND_BITPORT_NOP) {
sc->sc_pending &= ~TCTRL_SEND_BITPORT_NOP;
tctrl_setup_bitport(sc, 1);
} else if (sc->sc_pending & TCTRL_SEND_BITPORT) {
sc->sc_pending &= ~TCTRL_SEND_BITPORT;
tctrl_setup_bitport(sc, 0);
} else if (sc->sc_pending & TCTRL_SEND_BRIGHTNESS_NOP) {
sc->sc_pending &= ~TCTRL_SEND_BRIGHTNESS_NOP;
tctrl_setup_brightness(sc, 0xff, 0);
} else if (sc->sc_pending & TCTRL_SEND_BRIGHTNESS) {
sc->sc_pending &= ~TCTRL_SEND_BRIGHTNESS;
tctrl_setup_brightness(sc, 0, sc->sc_brightness);
}
if (sc->sc_cmdlen > 0) {
sc->sc_regs->intr =
sc->sc_regs->intr | TS102_UCTRL_INT_TXNF_MSK
|TS102_UCTRL_INT_TXNF_REQ;
v = sc->sc_regs->stat;
}
}
if ((sc->sc_cmdoff < sc->sc_cmdlen) && (v & TS102_UCTRL_STS_TXNF_STA)) {
tctrl_write_data(sc, sc->sc_cmdbuf[sc->sc_cmdoff++]);
#if 0
if (sc->sc_cmdoff == 1) {
printf("%s: op=0x%02x(l=%u)", sc->sc_dev.dv_xname,
sc->sc_cmdbuf[0], sc->sc_rsplen);
} else {
printf(" [%d]=0x%02x", sc->sc_cmdoff-1,
sc->sc_cmdbuf[sc->sc_cmdoff-1]);
}
#endif
if (sc->sc_cmdoff == sc->sc_cmdlen) {
sc->sc_state = sc->sc_rsplen ? TCTRL_ACK : TCTRL_IDLE;
#if 0
printf(" %s", sc->sc_rsplen ? "[ack]" : "[idle]\n");
#endif
if (sc->sc_cmdoff == 1) {
sc->sc_op = sc->sc_cmdbuf[0];
}
sc->sc_regs->intr =
sc->sc_regs->intr & (~TS102_UCTRL_INT_TXNF_MSK
|TS102_UCTRL_INT_TXNF_REQ);
} else if (sc->sc_state == TCTRL_IDLE) {
sc->sc_op = sc->sc_cmdbuf[0];
sc->sc_state = TCTRL_ARGS;
#if 0
printf(" [args]");
#endif
}
}
goto again;
}
void
tctrl_setup_bitport(struct tctrl_softc *sc, int nop)
{
if (nop) {
sc->sc_cmdbuf[0] = TS102_OP_CTL_BITPORT;
sc->sc_cmdbuf[1] = 0xff;
sc->sc_cmdbuf[2] = 0;
sc->sc_cmdlen = 3;
sc->sc_rsplen = 2;
} else {
if ((sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN)
|| (!sc->sc_tft_on)) {
sc->sc_cmdbuf[2] = TS102_BITPORT_TFTPWR;
} else {
sc->sc_cmdbuf[2] = 0;
}
sc->sc_cmdbuf[0] = TS102_OP_CTL_BITPORT;
sc->sc_cmdbuf[1] = ~TS102_BITPORT_TFTPWR;
sc->sc_cmdlen = 3;
sc->sc_rsplen = 2;
}
}
void
tctrl_setup_brightness(struct tctrl_softc *sc, int mask, int value)
{
sc->sc_cmdbuf[0] = TS102_OP_CTL_TFT_BRIGHTNESS;
sc->sc_cmdbuf[1] = mask;
sc->sc_cmdbuf[2] = value;
sc->sc_cmdlen = 3;
sc->sc_rsplen = 2;
}
void
tctrl_process_response(struct tctrl_softc *sc)
{
switch (sc->sc_op) {
case TS102_OP_RD_EXT_STATUS: {
sc->sc_ext_status = sc->sc_rspbuf[0] * 256 + sc->sc_rspbuf[1];
break;
}
case TS102_OP_RD_EVENT_STATUS: {
unsigned int v = sc->sc_rspbuf[0] * 256 + sc->sc_rspbuf[1];
if (v & TS102_EVENT_STATUS_SHUTDOWN_REQUEST) {
printf("%s: SHUTDOWN REQUEST!\n", sc->sc_dev.dv_xname);
}
if (v & TS102_EVENT_STATUS_VERY_LOW_POWER_WARNING) {
printf("%s: VERY LOW POWER WARNING!\n", sc->sc_dev.dv_xname);
}
if (v & TS102_EVENT_STATUS_LOW_POWER_WARNING) {
printf("%s: LOW POWER WARNING!\n", sc->sc_dev.dv_xname);
}
if (v & TS102_EVENT_STATUS_DC_STATUS_CHANGE) {
sc->sc_pending |= TCTRL_SEND_RD_EXT_STATUS;
printf("%s: main power %s\n", sc->sc_dev.dv_xname,
(sc->sc_ext_status & TS102_EXT_STATUS_MAIN_POWER_AVAILABLE) ? "removed" : "restored");
}
if (v & TS102_EVENT_STATUS_LID_STATUS_CHANGE) {
sc->sc_pending |= TCTRL_SEND_RD_EXT_STATUS;
sc->sc_pending |= TCTRL_SEND_BITPORT;
#if 0
printf("%s: lid %s\n", sc->sc_dev.dv_xname,
(sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) ? "opened" : "closed");
#endif
}
break;
}
case TS102_OP_CTL_BITPORT:
sc->sc_bitport = (sc->sc_rspbuf[0] & sc->sc_cmdbuf[1]) ^ sc->sc_cmdbuf[2];
break;
case TS102_OP_CTL_TFT_BRIGHTNESS:
sc->sc_brightness = sc->sc_rspbuf[0];
default:
break;
}
}
void
tadpole_powerdown(void)
{
struct tctrl_softc *sc;
int i, s;
if (tctrl_cd.cd_devs == NULL
|| tctrl_cd.cd_ndevs == 0
|| tctrl_cd.cd_devs[0] == NULL) {
return;
}
sc = (struct tctrl_softc *) tctrl_cd.cd_devs[0];
s = splhigh();
sc->sc_pending |= TCTRL_SEND_POWEROFF;
for (i = 0; i < 10000; i++) {
tctrl_intr(sc);
DELAY(1);
}
splx(s);
}
void
tadpole_set_brightness(int value)
{
struct tctrl_softc *sc;
int s;
if (tctrl_cd.cd_devs == NULL
|| tctrl_cd.cd_ndevs == 0
|| tctrl_cd.cd_devs[0] == NULL) {
return;
}
sc = (struct tctrl_softc *) tctrl_cd.cd_devs[0];
s = splhigh();
if (value != sc->sc_brightness) {
sc->sc_brightness = value;
sc->sc_pending |= TCTRL_SEND_BRIGHTNESS;
tctrl_intr(sc);
}
splx(s);
}
int
tadpole_get_brightness()
{
struct tctrl_softc *sc;
if (tctrl_cd.cd_devs == NULL
|| tctrl_cd.cd_ndevs == 0
|| tctrl_cd.cd_devs[0] == NULL) {
return 0;
}
sc = (struct tctrl_softc *) tctrl_cd.cd_devs[0];
return sc->sc_brightness;
}
void
tadpole_set_video(int enabled)
{
struct tctrl_softc *sc;
int s;
if (tctrl_cd.cd_devs == NULL
|| tctrl_cd.cd_ndevs == 0
|| tctrl_cd.cd_devs[0] == NULL) {
return;
}
sc = (struct tctrl_softc *) tctrl_cd.cd_devs[0];
s = splhigh();
if ((sc->sc_tft_on && !enabled) || (!sc->sc_tft_on && enabled)) {
sc->sc_tft_on = enabled;
if (sc->sc_ext_status & TS102_EXT_STATUS_LID_DOWN) {
splx(s);
return;
}
sc->sc_pending |= TCTRL_SEND_BITPORT;
tctrl_intr(sc);
}
splx(s);
}
int
tadpole_get_video()
{
struct tctrl_softc *sc;
if (tctrl_cd.cd_devs == NULL
|| tctrl_cd.cd_ndevs == 0
|| tctrl_cd.cd_devs[0] == NULL) {
return 0;
}
sc = (struct tctrl_softc *) tctrl_cd.cd_devs[0];
return sc->sc_tft_on;
}
void
tctrl_write_data(sc, v)
struct tctrl_softc *sc;
u_int8_t v;
{
unsigned int i;
for (i = 0; i < 100; i++) {
if (sc->sc_regs->stat & TS102_UCTRL_STS_TXNF_STA)
break;
}
sc->sc_regs->data = v;
}
u_int8_t
tctrl_read_data(sc)
struct tctrl_softc *sc;
{
unsigned int i, v;
for (i = 0; i < 100000; i++) {
if (sc->sc_regs->stat & TS102_UCTRL_STS_RXNE_STA)
break;
DELAY(1);
}
v = sc->sc_regs->data;
sc->sc_regs->stat = TS102_UCTRL_STS_RXNE_STA;
return v;
}
|