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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
|
/* $OpenBSD: lom.c,v 1.25 2014/12/10 12:27:57 mikeb Exp $ */
/*
* Copyright (c) 2009 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/sensors.h>
#include <sys/systm.h>
#include <sys/timeout.h>
#include <machine/autoconf.h>
#include <machine/openfirm.h>
#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>
/*
* LOMlite is a so far unidentified microcontroller.
*/
#define LOM1_STATUS 0x00 /* R */
#define LOM1_STATUS_BUSY 0x80
#define LOM1_CMD 0x00 /* W */
#define LOM1_DATA 0x01 /* R/W */
/*
* LOMlite2 is implemented as a H8/3437 microcontroller which has its
* on-chip host interface hooked up to EBus.
*/
#define LOM2_DATA 0x00 /* R/W */
#define LOM2_CMD 0x01 /* W */
#define LOM2_STATUS 0x01 /* R */
#define LOM2_STATUS_OBF 0x01 /* Output Buffer Full */
#define LOM2_STATUS_IBF 0x02 /* Input Buffer Full */
#define LOM_IDX_CMD 0x00
#define LOM_IDX_CMD_GENERIC 0x00
#define LOM_IDX_CMD_TEMP 0x04
#define LOM_IDX_CMD_FAN 0x05
#define LOM_IDX_FW_REV 0x01 /* Firmware revision */
#define LOM_IDX_FAN1 0x04 /* Fan speed */
#define LOM_IDX_FAN2 0x05
#define LOM_IDX_FAN3 0x06
#define LOM_IDX_FAN4 0x07
#define LOM_IDX_PSU1 0x08 /* PSU status */
#define LOM_IDX_PSU2 0x09
#define LOM_IDX_PSU3 0x0a
#define LOM_PSU_INPUTA 0x01
#define LOM_PSU_INPUTB 0x02
#define LOM_PSU_OUTPUT 0x04
#define LOM_PSU_PRESENT 0x08
#define LOM_PSU_STANDBY 0x10
#define LOM_IDX_TEMP1 0x18 /* Temperature */
#define LOM_IDX_TEMP2 0x19
#define LOM_IDX_TEMP3 0x1a
#define LOM_IDX_TEMP4 0x1b
#define LOM_IDX_TEMP5 0x1c
#define LOM_IDX_TEMP6 0x1d
#define LOM_IDX_TEMP7 0x1e
#define LOM_IDX_TEMP8 0x1f
#define LOM_IDX_LED1 0x25
#define LOM_IDX_ALARM 0x30
#define LOM_IDX_WDOG_CTL 0x31
#define LOM_WDOG_ENABLE 0x01
#define LOM_WDOG_RESET 0x02
#define LOM_WDOG_AL3_WDOG 0x04
#define LOM_WDOG_AL3_FANPSU 0x08
#define LOM_IDX_WDOG_TIME 0x32
#define LOM_WDOG_TIME_MAX 126
#define LOM1_IDX_HOSTNAME1 0x33
#define LOM1_IDX_HOSTNAME2 0x34
#define LOM1_IDX_HOSTNAME3 0x35
#define LOM1_IDX_HOSTNAME4 0x36
#define LOM1_IDX_HOSTNAME5 0x37
#define LOM1_IDX_HOSTNAME6 0x38
#define LOM1_IDX_HOSTNAME7 0x39
#define LOM1_IDX_HOSTNAME8 0x3a
#define LOM1_IDX_HOSTNAME9 0x3b
#define LOM1_IDX_HOSTNAME10 0x3c
#define LOM1_IDX_HOSTNAME11 0x3d
#define LOM1_IDX_HOSTNAME12 0x3e
#define LOM2_IDX_HOSTNAMELEN 0x38
#define LOM2_IDX_HOSTNAME 0x39
#define LOM_IDX_CONFIG 0x5d
#define LOM_IDX_FAN1_CAL 0x5e
#define LOM_IDX_FAN2_CAL 0x5f
#define LOM_IDX_FAN3_CAL 0x60
#define LOM_IDX_FAN4_CAL 0x61
#define LOM_IDX_FAN1_LOW 0x62
#define LOM_IDX_FAN2_LOW 0x63
#define LOM_IDX_FAN3_LOW 0x64
#define LOM_IDX_FAN4_LOW 0x65
#define LOM_IDX_CONFIG2 0x66
#define LOM_IDX_CONFIG3 0x67
#define LOM_IDX_PROBE55 0x7e /* Always returns 0x55 */
#define LOM_IDX_PROBEAA 0x7f /* Always returns 0xaa */
#define LOM_IDX_WRITE 0x80
#define LOM_IDX4_TEMP_NAME_START 0x40
#define LOM_IDX4_TEMP_NAME_END 0xff
#define LOM_IDX5_FAN_NAME_START 0x40
#define LOM_IDX5_FAN_NAME_END 0xff
#define LOM_MAX_FAN 4
#define LOM_MAX_PSU 3
#define LOM_MAX_TEMP 8
struct lom_cmd {
uint8_t lc_cmd;
uint8_t lc_data;
TAILQ_ENTRY(lom_cmd) lc_next;
};
struct lom_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
int sc_type;
#define LOM_LOMLITE 0
#define LOM_LOMLITE2 2
int sc_space;
struct ksensor sc_fan[LOM_MAX_FAN];
struct ksensor sc_psu[LOM_MAX_PSU];
struct ksensor sc_temp[LOM_MAX_TEMP];
struct ksensordev sc_sensordev;
int sc_num_fan;
int sc_num_psu;
int sc_num_temp;
uint8_t sc_fan_cal[LOM_MAX_FAN];
uint8_t sc_fan_low[LOM_MAX_FAN];
char sc_hostname[MAXHOSTNAMELEN];
struct timeout sc_wdog_to;
int sc_wdog_period;
uint8_t sc_wdog_ctl;
struct lom_cmd sc_wdog_pat;
TAILQ_HEAD(, lom_cmd) sc_queue;
struct mutex sc_queue_mtx;
struct timeout sc_state_to;
int sc_state;
#define LOM_STATE_IDLE 0
#define LOM_STATE_CMD 1
#define LOM_STATE_DATA 2
int sc_retry;
};
int lom_match(struct device *, void *, void *);
void lom_attach(struct device *, struct device *, void *);
int lom_activate(struct device *, int);
struct cfattach lom_ca = {
sizeof(struct lom_softc), lom_match, lom_attach,
NULL, lom_activate
};
struct cfdriver lom_cd = {
NULL, "lom", DV_DULL
};
int lom_read(struct lom_softc *, uint8_t, uint8_t *);
int lom_write(struct lom_softc *, uint8_t, uint8_t);
void lom_queue_cmd(struct lom_softc *, struct lom_cmd *);
void lom_dequeue_cmd(struct lom_softc *, struct lom_cmd *);
int lom1_read(struct lom_softc *, uint8_t, uint8_t *);
int lom1_write(struct lom_softc *, uint8_t, uint8_t);
int lom1_read_polled(struct lom_softc *, uint8_t, uint8_t *);
int lom1_write_polled(struct lom_softc *, uint8_t, uint8_t);
void lom1_queue_cmd(struct lom_softc *, struct lom_cmd *);
void lom1_process_queue(void *);
void lom1_process_queue_locked(struct lom_softc *);
int lom2_read(struct lom_softc *, uint8_t, uint8_t *);
int lom2_write(struct lom_softc *, uint8_t, uint8_t);
int lom2_read_polled(struct lom_softc *, uint8_t, uint8_t *);
int lom2_write_polled(struct lom_softc *, uint8_t, uint8_t);
void lom2_queue_cmd(struct lom_softc *, struct lom_cmd *);
int lom2_intr(void *);
int lom_init_desc(struct lom_softc *sc);
void lom_refresh(void *);
void lom1_write_hostname(struct lom_softc *);
void lom2_write_hostname(struct lom_softc *);
void lom_wdog_pat(void *);
int lom_wdog_cb(void *, int);
void lom_shutdown(void *);
int
lom_match(struct device *parent, void *match, void *aux)
{
struct ebus_attach_args *ea = aux;
if (strcmp(ea->ea_name, "SUNW,lom") == 0 ||
strcmp(ea->ea_name, "SUNW,lomh") == 0)
return (1);
return (0);
}
void
lom_attach(struct device *parent, struct device *self, void *aux)
{
struct lom_softc *sc = (void *)self;
struct ebus_attach_args *ea = aux;
uint8_t reg, fw_rev, config, config2, config3;
uint8_t cal, low;
int i;
if (strcmp(ea->ea_name, "SUNW,lomh") == 0) {
if (ea->ea_nintrs < 1) {
printf(": no interrupt\n");
return;
}
sc->sc_type = LOM_LOMLITE2;
}
if (ebus_bus_map(ea->ea_iotag, 0,
EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
sc->sc_iot = ea->ea_iotag;
} else if (ebus_bus_map(ea->ea_memtag, 0,
EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
sc->sc_iot = ea->ea_memtag;
} else {
printf(": can't map register space\n");
return;
}
if (sc->sc_type < LOM_LOMLITE2) {
/* XXX Magic */
bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
bus_space_write_1(sc->sc_iot, sc->sc_ioh, 3, 0xca);
}
if (lom_read(sc, LOM_IDX_PROBE55, ®) || reg != 0x55 ||
lom_read(sc, LOM_IDX_PROBEAA, ®) || reg != 0xaa ||
lom_read(sc, LOM_IDX_FW_REV, &fw_rev) ||
lom_read(sc, LOM_IDX_CONFIG, &config))
{
printf(": not responding\n");
return;
}
TAILQ_INIT(&sc->sc_queue);
mtx_init(&sc->sc_queue_mtx, IPL_BIO);
config2 = config3 = 0;
if (sc->sc_type < LOM_LOMLITE2) {
/*
* LOMlite doesn't do interrupts so we limp along on
* timeouts.
*/
timeout_set(&sc->sc_state_to, lom1_process_queue, sc);
} else {
lom_read(sc, LOM_IDX_CONFIG2, &config2);
lom_read(sc, LOM_IDX_CONFIG3, &config3);
bus_intr_establish(sc->sc_iot, ea->ea_intrs[0],
IPL_BIO, 0, lom2_intr, sc, self->dv_xname);
}
sc->sc_num_fan = min((config >> 5) & 0x7, LOM_MAX_FAN);
sc->sc_num_psu = min((config >> 3) & 0x3, LOM_MAX_PSU);
sc->sc_num_temp = min((config2 >> 4) & 0xf, LOM_MAX_TEMP);
for (i = 0; i < sc->sc_num_fan; i++) {
if (lom_read(sc, LOM_IDX_FAN1_CAL + i, &cal) ||
lom_read(sc, LOM_IDX_FAN1_LOW + i, &low)) {
printf(": can't read fan information\n");
return;
}
sc->sc_fan_cal[i] = cal;
sc->sc_fan_low[i] = low;
}
/* Initialize sensor data. */
strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
sizeof(sc->sc_sensordev.xname));
for (i = 0; i < sc->sc_num_fan; i++) {
sc->sc_fan[i].type = SENSOR_FANRPM;
sensor_attach(&sc->sc_sensordev, &sc->sc_fan[i]);
snprintf(sc->sc_fan[i].desc, sizeof(sc->sc_fan[i].desc),
"fan%d", i + 1);
}
for (i = 0; i < sc->sc_num_psu; i++) {
sc->sc_psu[i].type = SENSOR_INDICATOR;
sensor_attach(&sc->sc_sensordev, &sc->sc_psu[i]);
snprintf(sc->sc_psu[i].desc, sizeof(sc->sc_psu[i].desc),
"PSU%d", i + 1);
}
for (i = 0; i < sc->sc_num_temp; i++) {
sc->sc_temp[i].type = SENSOR_TEMP;
sensor_attach(&sc->sc_sensordev, &sc->sc_temp[i]);
}
if (lom_init_desc(sc)) {
printf(": can't read sensor names\n");
return;
}
if (sensor_task_register(sc, lom_refresh, 5) == NULL) {
printf(": unable to register update task\n");
return;
}
sensordev_install(&sc->sc_sensordev);
/*
* We configure the watchdog to turn on the fault LED when the
* watchdog timer expires. We run our own timeout to pat it
* such that this won't happen unless the kernel hangs. When
* the watchdog is explicitly configured using sysctl(8), we
* reconfigure it to reset the machine and let the standard
* watchdog(4) machinery take over.
*/
lom_write(sc, LOM_IDX_WDOG_TIME, LOM_WDOG_TIME_MAX);
lom_read(sc, LOM_IDX_WDOG_CTL, &sc->sc_wdog_ctl);
sc->sc_wdog_ctl &= ~LOM_WDOG_RESET;
sc->sc_wdog_ctl |= LOM_WDOG_ENABLE;
lom_write(sc, LOM_IDX_WDOG_CTL, sc->sc_wdog_ctl);
timeout_set(&sc->sc_wdog_to, lom_wdog_pat, sc);
timeout_add_sec(&sc->sc_wdog_to, LOM_WDOG_TIME_MAX / 2);
wdog_register(lom_wdog_cb, sc);
printf(": %s rev %d.%d\n",
sc->sc_type < LOM_LOMLITE2 ? "LOMlite" : "LOMlite2",
fw_rev >> 4, fw_rev & 0x0f);
}
int
lom_activate(struct device *self, int act)
{
int ret = 0;
switch (act) {
case DVACT_POWERDOWN:
wdog_shutdown(self);
lom_shutdown(self);
break;
}
return (ret);
}
int
lom_read(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
if (sc->sc_type < LOM_LOMLITE2)
return lom1_read(sc, reg, val);
else
return lom2_read(sc, reg, val);
}
int
lom_write(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
if (sc->sc_type < LOM_LOMLITE2)
return lom1_write(sc, reg, val);
else
return lom2_write(sc, reg, val);
}
void
lom_queue_cmd(struct lom_softc *sc, struct lom_cmd *lc)
{
if (sc->sc_type < LOM_LOMLITE2)
return lom1_queue_cmd(sc, lc);
else
return lom2_queue_cmd(sc, lc);
}
void
lom_dequeue_cmd(struct lom_softc *sc, struct lom_cmd *lc)
{
struct lom_cmd *lcp;
mtx_enter(&sc->sc_queue_mtx);
TAILQ_FOREACH(lcp, &sc->sc_queue, lc_next) {
if (lcp == lc) {
TAILQ_REMOVE(&sc->sc_queue, lc, lc_next);
break;
}
}
mtx_leave(&sc->sc_queue_mtx);
}
int
lom1_read(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
struct lom_cmd lc;
int error;
if (cold)
return lom1_read_polled(sc, reg, val);
lc.lc_cmd = reg;
lc.lc_data = 0xff;
lom1_queue_cmd(sc, &lc);
error = tsleep(&lc, PZERO, "lomrd", hz);
if (error)
lom_dequeue_cmd(sc, &lc);
*val = lc.lc_data;
return (error);
}
int
lom1_write(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
struct lom_cmd lc;
int error;
if (cold)
return lom1_write_polled(sc, reg, val);
lc.lc_cmd = reg | LOM_IDX_WRITE;
lc.lc_data = val;
lom1_queue_cmd(sc, &lc);
error = tsleep(&lc, PZERO, "lomwr", 2 * hz);
if (error)
lom_dequeue_cmd(sc, &lc);
return (error);
}
int
lom1_read_polled(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_CMD, reg);
/* Wait until the microcontroller fills output buffer. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
*val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA);
return (0);
}
int
lom1_write_polled(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
reg |= LOM_IDX_WRITE;
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_CMD, reg);
/* Wait until the microcontroller fills output buffer. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA, val);
return (0);
}
void
lom1_queue_cmd(struct lom_softc *sc, struct lom_cmd *lc)
{
mtx_enter(&sc->sc_queue_mtx);
TAILQ_INSERT_TAIL(&sc->sc_queue, lc, lc_next);
if (sc->sc_state == LOM_STATE_IDLE) {
sc->sc_state = LOM_STATE_CMD;
lom1_process_queue_locked(sc);
}
mtx_leave(&sc->sc_queue_mtx);
}
void
lom1_process_queue(void *arg)
{
struct lom_softc *sc = arg;
mtx_enter(&sc->sc_queue_mtx);
lom1_process_queue_locked(sc);
mtx_leave(&sc->sc_queue_mtx);
}
void
lom1_process_queue_locked(struct lom_softc *sc)
{
struct lom_cmd *lc;
uint8_t str;
lc = TAILQ_FIRST(&sc->sc_queue);
if (lc == NULL) {
sc->sc_state = LOM_STATE_IDLE;
return;
}
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
if (str & LOM1_STATUS_BUSY) {
if (sc->sc_retry++ < 30) {
timeout_add_msec(&sc->sc_state_to, 1);
return;
}
/*
* Looks like the microcontroller got wedged. Unwedge
* it by writing this magic value. Give it some time
* to recover.
*/
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA, 0xac);
timeout_add_msec(&sc->sc_state_to, 1000);
sc->sc_state = LOM_STATE_CMD;
return;
}
sc->sc_retry = 0;
if (sc->sc_state == LOM_STATE_CMD) {
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_CMD, lc->lc_cmd);
sc->sc_state = LOM_STATE_DATA;
timeout_add_msec(&sc->sc_state_to, 250);
return;
}
KASSERT(sc->sc_state == LOM_STATE_DATA);
if ((lc->lc_cmd & LOM_IDX_WRITE) == 0)
lc->lc_data = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA);
else
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA, lc->lc_data);
TAILQ_REMOVE(&sc->sc_queue, lc, lc_next);
wakeup(lc);
if (!TAILQ_EMPTY(&sc->sc_queue)) {
sc->sc_state = LOM_STATE_CMD;
timeout_add_msec(&sc->sc_state_to, 1);
return;
}
sc->sc_state = LOM_STATE_IDLE;
}
int
lom2_read(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
struct lom_cmd lc;
int error;
if (cold)
return lom2_read_polled(sc, reg, val);
lc.lc_cmd = reg;
lc.lc_data = 0xff;
lom2_queue_cmd(sc, &lc);
error = tsleep(&lc, PZERO, "lom2rd", hz);
if (error)
lom_dequeue_cmd(sc, &lc);
*val = lc.lc_data;
return (error);
}
int
lom2_read_polled(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM2_CMD, reg);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
*val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_DATA);
return (0);
}
int
lom2_write(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
struct lom_cmd lc;
int error;
if (cold)
return lom2_write_polled(sc, reg, val);
lc.lc_cmd = reg | LOM_IDX_WRITE;
lc.lc_data = val;
lom2_queue_cmd(sc, &lc);
error = tsleep(&lc, PZERO, "lom2wr", hz);
if (error)
lom_dequeue_cmd(sc, &lc);
return (error);
}
int
lom2_write_polled(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
if (sc->sc_space == LOM_IDX_CMD_GENERIC && reg != LOM_IDX_CMD)
reg |= LOM_IDX_WRITE;
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM2_CMD, reg);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_DATA);
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM2_DATA, val);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_DATA);
/* If we switched spaces, remember the one we're in now. */
if (reg == LOM_IDX_CMD)
sc->sc_space = val;
return (0);
}
void
lom2_queue_cmd(struct lom_softc *sc, struct lom_cmd *lc)
{
uint8_t str;
mtx_enter(&sc->sc_queue_mtx);
TAILQ_INSERT_TAIL(&sc->sc_queue, lc, lc_next);
if (sc->sc_state == LOM_STATE_IDLE) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
if ((str & LOM2_STATUS_IBF) == 0) {
bus_space_write_1(sc->sc_iot, sc->sc_ioh,
LOM2_CMD, lc->lc_cmd);
sc->sc_state = LOM_STATE_DATA;
}
}
mtx_leave(&sc->sc_queue_mtx);
}
int
lom2_intr(void *arg)
{
struct lom_softc *sc = arg;
struct lom_cmd *lc;
uint8_t str, obr;
mtx_enter(&sc->sc_queue_mtx);
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
obr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_DATA);
lc = TAILQ_FIRST(&sc->sc_queue);
if (lc == NULL) {
mtx_leave(&sc->sc_queue_mtx);
return (0);
}
if (lc->lc_cmd & LOM_IDX_WRITE) {
bus_space_write_1(sc->sc_iot, sc->sc_ioh,
LOM2_DATA, lc->lc_data);
lc->lc_cmd &= ~LOM_IDX_WRITE;
mtx_leave(&sc->sc_queue_mtx);
return (1);
}
KASSERT(sc->sc_state = LOM_STATE_DATA);
lc->lc_data = obr;
TAILQ_REMOVE(&sc->sc_queue, lc, lc_next);
wakeup(lc);
sc->sc_state = LOM_STATE_IDLE;
if (!TAILQ_EMPTY(&sc->sc_queue)) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
if ((str & LOM2_STATUS_IBF) == 0) {
bus_space_write_1(sc->sc_iot, sc->sc_ioh,
LOM2_CMD, lc->lc_cmd);
sc->sc_state = LOM_STATE_DATA;
}
}
mtx_leave(&sc->sc_queue_mtx);
return (1);
}
int
lom_init_desc(struct lom_softc *sc)
{
uint8_t val;
int i, j, k;
int error;
/* LOMlite doesn't provide sensor descriptions. */
if (sc->sc_type < LOM_LOMLITE2)
return (0);
/*
* Read temperature sensor names.
*/
error = lom_write(sc, LOM_IDX_CMD, LOM_IDX_CMD_TEMP);
if (error)
return (error);
i = 0;
j = 0;
k = LOM_IDX4_TEMP_NAME_START;
while (k <= LOM_IDX4_TEMP_NAME_END) {
error = lom_read(sc, k++, &val);
if (error)
goto fail;
if (val == 0xff)
break;
if (j < sizeof (sc->sc_temp[i].desc) - 1)
sc->sc_temp[i].desc[j++] = val;
if (val == '\0') {
i++;
j = 0;
if (i < sc->sc_num_temp)
continue;
break;
}
}
/*
* Read fan names.
*/
error = lom_write(sc, LOM_IDX_CMD, LOM_IDX_CMD_FAN);
if (error)
return (error);
i = 0;
j = 0;
k = LOM_IDX5_FAN_NAME_START;
while (k <= LOM_IDX5_FAN_NAME_END) {
error = lom_read(sc, k++, &val);
if (error)
goto fail;
if (val == 0xff)
break;
if (j < sizeof (sc->sc_fan[i].desc) - 1)
sc->sc_fan[i].desc[j++] = val;
if (val == '\0') {
i++;
j = 0;
if (i < sc->sc_num_fan)
continue;
break;
}
}
fail:
lom_write(sc, LOM_IDX_CMD, LOM_IDX_CMD_GENERIC);
return (error);
}
void
lom_refresh(void *arg)
{
struct lom_softc *sc = arg;
uint8_t val;
int i;
for (i = 0; i < sc->sc_num_fan; i++) {
if (lom_read(sc, LOM_IDX_FAN1 + i, &val)) {
sc->sc_fan[i].flags |= SENSOR_FINVALID;
continue;
}
sc->sc_fan[i].value = (60 * sc->sc_fan_cal[i] * val) / 100;
if (val < sc->sc_fan_low[i])
sc->sc_fan[i].status = SENSOR_S_CRIT;
else
sc->sc_fan[i].status = SENSOR_S_OK;
sc->sc_fan[i].flags &= ~SENSOR_FINVALID;
}
for (i = 0; i < sc->sc_num_psu; i++) {
if (lom_read(sc, LOM_IDX_PSU1 + i, &val) ||
!ISSET(val, LOM_PSU_PRESENT)) {
sc->sc_psu[i].flags |= SENSOR_FINVALID;
continue;
}
if (val & LOM_PSU_STANDBY) {
sc->sc_psu[i].value = 0;
sc->sc_psu[i].status = SENSOR_S_UNSPEC;
} else {
sc->sc_psu[i].value = 1;
if (ISSET(val, LOM_PSU_INPUTA) &&
ISSET(val, LOM_PSU_INPUTB) &&
ISSET(val, LOM_PSU_OUTPUT))
sc->sc_psu[i].status = SENSOR_S_OK;
else
sc->sc_psu[i].status = SENSOR_S_CRIT;
}
sc->sc_psu[i].flags &= ~SENSOR_FINVALID;
}
for (i = 0; i < sc->sc_num_temp; i++) {
if (lom_read(sc, LOM_IDX_TEMP1 + i, &val)) {
sc->sc_temp[i].flags |= SENSOR_FINVALID;
continue;
}
sc->sc_temp[i].value = val * 1000000 + 273150000;
sc->sc_temp[i].flags &= ~SENSOR_FINVALID;
}
/*
* If our hostname is set and differs from what's stored in
* the LOM, write the new hostname back to the LOM. Note that
* we include the terminating NUL when writing the hostname
* back to the LOM, otherwise the LOM will print any trailing
* garbage.
*/
if (hostnamelen > 0 &&
strncmp(sc->sc_hostname, hostname, sizeof(hostname)) != 0) {
if (sc->sc_type < LOM_LOMLITE2)
lom1_write_hostname(sc);
else
lom2_write_hostname(sc);
strlcpy(sc->sc_hostname, hostname, sizeof(hostname));
}
}
void
lom1_write_hostname(struct lom_softc *sc)
{
char name[(LOM1_IDX_HOSTNAME12 - LOM1_IDX_HOSTNAME1 + 1) + 1];
char *p;
int i;
/*
* LOMlite generally doesn't have enough space to store the
* fully qualified hostname. If the hostname is too long,
* strip off the domain name.
*/
strlcpy(name, hostname, sizeof(name));
if (hostnamelen >= sizeof(name)) {
p = strchr(name, '.');
if (p)
*p = '\0';
}
for (i = 0; i < strlen(name) + 1; i++)
if (lom_write(sc, LOM1_IDX_HOSTNAME1 + i, name[i]))
break;
}
void
lom2_write_hostname(struct lom_softc *sc)
{
int i;
lom_write(sc, LOM2_IDX_HOSTNAMELEN, hostnamelen + 1);
for (i = 0; i < hostnamelen + 1; i++)
lom_write(sc, LOM2_IDX_HOSTNAME, hostname[i]);
}
void
lom_wdog_pat(void *arg)
{
struct lom_softc *sc = arg;
/* Pat the dog. */
sc->sc_wdog_pat.lc_cmd = LOM_IDX_WDOG_CTL | LOM_IDX_WRITE;
sc->sc_wdog_pat.lc_data = sc->sc_wdog_ctl;
lom_queue_cmd(sc, &sc->sc_wdog_pat);
timeout_add_sec(&sc->sc_wdog_to, LOM_WDOG_TIME_MAX / 2);
}
int
lom_wdog_cb(void *arg, int period)
{
struct lom_softc *sc = arg;
if (period > LOM_WDOG_TIME_MAX)
period = LOM_WDOG_TIME_MAX;
else if (period < 0)
period = 0;
if (period == 0) {
if (sc->sc_wdog_period != 0) {
/* Stop watchdog from resetting the machine. */
sc->sc_wdog_ctl &= ~LOM_WDOG_RESET;
lom_write(sc, LOM_IDX_WDOG_CTL, sc->sc_wdog_ctl);
lom_write(sc, LOM_IDX_WDOG_TIME, LOM_WDOG_TIME_MAX);
timeout_add_sec(&sc->sc_wdog_to, LOM_WDOG_TIME_MAX / 2);
}
} else {
if (sc->sc_wdog_period != period) {
/* Set new timeout. */
lom_write(sc, LOM_IDX_WDOG_TIME, period);
}
if (sc->sc_wdog_period == 0) {
/* Make watchdog reset the machine. */
sc->sc_wdog_ctl |= LOM_WDOG_RESET;
lom_write(sc, LOM_IDX_WDOG_CTL, sc->sc_wdog_ctl);
timeout_del(&sc->sc_wdog_to);
} else {
/* Pat the dog. */
lom_dequeue_cmd(sc, &sc->sc_wdog_pat);
sc->sc_wdog_pat.lc_cmd = LOM_IDX_WDOG_CTL | LOM_IDX_WRITE;
sc->sc_wdog_pat.lc_data = sc->sc_wdog_ctl;
lom_queue_cmd(sc, &sc->sc_wdog_pat);
}
}
sc->sc_wdog_period = period;
return (period);
}
void
lom_shutdown(void *arg)
{
struct lom_softc *sc = arg;
sc->sc_wdog_ctl &= ~LOM_WDOG_ENABLE;
lom_write(sc, LOM_IDX_WDOG_CTL, sc->sc_wdog_ctl);
}
|