summaryrefslogtreecommitdiff
path: root/env4826.c
blob: ead49a966ac61f4f0228c5e8feed4674fa22dabc (plain)
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
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <machine/cpufunc.h>

#define BASE    0x200

static void
usage()
{
    fprintf(stderr, "usage: env4826 [-i seconds]\n");
    exit(2);
}

static void
pollb(void)
{
    int j, k, l;

    j = 0;
    do {
        if (++j > 1000)
            break;
        l = inb (BASE + 1);
    } while (!(l & 0x40));
}

static int
read_temp()
{
    u_int u;

    u = inl(0x6000 + 0x30);
    u &= ~(1 << 19);
    u |= 1 << 19;
    outl(0x6000 + 0x30, u);
    u = inl(0x6000 + 0x30);

    outb(0x15c, 0x07); outb(0x15d, 0x06);        /* ACB2 */
    outb(0x15c, 0x60); outb(0x15d, BASE >> 8);    /* base addr MSB */
    outb(0x15c, 0x61); outb(0x15d, BASE & 0xff);    /* base addr LSB */
    outb(0x15c, 0x30); outb(0x15d, 0x01);        /* enable */
    outb(0x15c, 0xf0); outb(0x15d, 0x02);        /* internal pull-up */

    outb(BASE + 5, 0);

    outb(BASE + 5, 0xfe | 0x01);

    usleep(100000);
    outb(BASE + 3, 0x01);
    pollb();
    outb(BASE + 0, 0x90);
    pollb();
    outb(BASE + 0, 0x00);
    pollb();
    outb(BASE + 3, 0x02);

    outb(BASE + 3, 0x01);

    pollb();

    outb(BASE + 0, 0x91);

    pollb();

    outb(BASE + 3, 0x10);

    u = inb(BASE + 0) << 8;

    pollb();

    u |= inb(BASE + 0);

    outb(BASE + 3, 0x02);

    printf("lm75: %.2fC\n", u / 256.0);

    outb(BASE + 5, 0);
    usleep(100000);

    return 0;
}

int
main(int argc, char **argv)
{
    int seconds = 0;
    char *t;
    FILE *f;
    int ch;

    while ((ch = getopt (argc, argv, "i:")) != -1) {
        switch(ch) {
        case 'i':
            seconds = strtol (optarg, &t, 10);
            if (*t || seconds < 0)
                usage ();
            break;
        default:
            usage ();
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if (argc != 0)
        usage ();

    f = fopen("/dev/io", "r");
    if (!f)
        err (1, "couldn't open /dev/io");

    for (;;) {
        read_temp ();
        fflush(stdout);
        if (!seconds)
            break;
        sleep (seconds);
    }

    return 0;
}