root / hw / ds1338.c @ cf4dc461
History | View | Annotate | Download (3.5 kB)
1 | 1dfe3943 | Paul Brook | /*
|
---|---|---|---|
2 | 1dfe3943 | Paul Brook | * MAXIM DS1338 I2C RTC+NVRAM
|
3 | 1dfe3943 | Paul Brook | *
|
4 | 1dfe3943 | Paul Brook | * Copyright (c) 2009 CodeSourcery.
|
5 | 1dfe3943 | Paul Brook | * Written by Paul Brook
|
6 | 1dfe3943 | Paul Brook | *
|
7 | 8e31bf38 | Matthew Fernandez | * This code is licensed under the GNU GPL v2.
|
8 | 6b620ca3 | Paolo Bonzini | *
|
9 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
10 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
11 | 1dfe3943 | Paul Brook | */
|
12 | 1dfe3943 | Paul Brook | |
13 | 1dfe3943 | Paul Brook | #include "i2c.h" |
14 | 1dfe3943 | Paul Brook | |
15 | 1dfe3943 | Paul Brook | typedef struct { |
16 | 9e07bdf8 | Anthony Liguori | I2CSlave i2c; |
17 | 1dfe3943 | Paul Brook | time_t offset; |
18 | 1dfe3943 | Paul Brook | struct tm now;
|
19 | 1dfe3943 | Paul Brook | uint8_t nvram[56];
|
20 | 1dfe3943 | Paul Brook | int ptr;
|
21 | 1dfe3943 | Paul Brook | int addr_byte;
|
22 | 1dfe3943 | Paul Brook | } DS1338State; |
23 | 1dfe3943 | Paul Brook | |
24 | 9e07bdf8 | Anthony Liguori | static void ds1338_event(I2CSlave *i2c, enum i2c_event event) |
25 | 1dfe3943 | Paul Brook | { |
26 | 1dfe3943 | Paul Brook | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); |
27 | 1dfe3943 | Paul Brook | |
28 | 1dfe3943 | Paul Brook | switch (event) {
|
29 | 1dfe3943 | Paul Brook | case I2C_START_RECV:
|
30 | 1dfe3943 | Paul Brook | qemu_get_timedate(&s->now, s->offset); |
31 | 1dfe3943 | Paul Brook | s->nvram[0] = to_bcd(s->now.tm_sec);
|
32 | 1dfe3943 | Paul Brook | s->nvram[1] = to_bcd(s->now.tm_min);
|
33 | 1dfe3943 | Paul Brook | if (s->nvram[2] & 0x40) { |
34 | 1dfe3943 | Paul Brook | s->nvram[2] = (to_bcd((s->now.tm_hour % 12)) + 1) | 0x40; |
35 | 1dfe3943 | Paul Brook | if (s->now.tm_hour >= 12) { |
36 | 1dfe3943 | Paul Brook | s->nvram[2] |= 0x20; |
37 | 1dfe3943 | Paul Brook | } |
38 | 1dfe3943 | Paul Brook | } else {
|
39 | 1dfe3943 | Paul Brook | s->nvram[2] = to_bcd(s->now.tm_hour);
|
40 | 1dfe3943 | Paul Brook | } |
41 | 1dfe3943 | Paul Brook | s->nvram[3] = to_bcd(s->now.tm_wday) + 1; |
42 | 1dfe3943 | Paul Brook | s->nvram[4] = to_bcd(s->now.tm_mday);
|
43 | 1dfe3943 | Paul Brook | s->nvram[5] = to_bcd(s->now.tm_mon) + 1; |
44 | 1dfe3943 | Paul Brook | s->nvram[6] = to_bcd(s->now.tm_year - 100); |
45 | 1dfe3943 | Paul Brook | break;
|
46 | 1dfe3943 | Paul Brook | case I2C_START_SEND:
|
47 | 1dfe3943 | Paul Brook | s->addr_byte = 1;
|
48 | 1dfe3943 | Paul Brook | break;
|
49 | 1dfe3943 | Paul Brook | default:
|
50 | 1dfe3943 | Paul Brook | break;
|
51 | 1dfe3943 | Paul Brook | } |
52 | 1dfe3943 | Paul Brook | } |
53 | 1dfe3943 | Paul Brook | |
54 | 9e07bdf8 | Anthony Liguori | static int ds1338_recv(I2CSlave *i2c) |
55 | 1dfe3943 | Paul Brook | { |
56 | 1dfe3943 | Paul Brook | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); |
57 | 1dfe3943 | Paul Brook | uint8_t res; |
58 | 1dfe3943 | Paul Brook | |
59 | 1dfe3943 | Paul Brook | res = s->nvram[s->ptr]; |
60 | 1dfe3943 | Paul Brook | s->ptr = (s->ptr + 1) & 0xff; |
61 | 1dfe3943 | Paul Brook | return res;
|
62 | 1dfe3943 | Paul Brook | } |
63 | 1dfe3943 | Paul Brook | |
64 | 9e07bdf8 | Anthony Liguori | static int ds1338_send(I2CSlave *i2c, uint8_t data) |
65 | 1dfe3943 | Paul Brook | { |
66 | 1dfe3943 | Paul Brook | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); |
67 | 1dfe3943 | Paul Brook | if (s->addr_byte) {
|
68 | 1dfe3943 | Paul Brook | s->ptr = data; |
69 | 1dfe3943 | Paul Brook | s->addr_byte = 0;
|
70 | 1dfe3943 | Paul Brook | return 0; |
71 | 1dfe3943 | Paul Brook | } |
72 | 1dfe3943 | Paul Brook | s->nvram[s->ptr - 8] = data;
|
73 | 1dfe3943 | Paul Brook | if (data < 8) { |
74 | 1dfe3943 | Paul Brook | qemu_get_timedate(&s->now, s->offset); |
75 | 1dfe3943 | Paul Brook | switch(data) {
|
76 | 1dfe3943 | Paul Brook | case 0: |
77 | 1dfe3943 | Paul Brook | /* TODO: Implement CH (stop) bit. */
|
78 | 1dfe3943 | Paul Brook | s->now.tm_sec = from_bcd(data & 0x7f);
|
79 | 1dfe3943 | Paul Brook | break;
|
80 | 1dfe3943 | Paul Brook | case 1: |
81 | 1dfe3943 | Paul Brook | s->now.tm_min = from_bcd(data & 0x7f);
|
82 | 1dfe3943 | Paul Brook | break;
|
83 | 1dfe3943 | Paul Brook | case 2: |
84 | 1dfe3943 | Paul Brook | if (data & 0x40) { |
85 | 1dfe3943 | Paul Brook | if (data & 0x20) { |
86 | 1dfe3943 | Paul Brook | data = from_bcd(data & 0x4f) + 11; |
87 | 1dfe3943 | Paul Brook | } else {
|
88 | 1dfe3943 | Paul Brook | data = from_bcd(data & 0x1f) - 1; |
89 | 1dfe3943 | Paul Brook | } |
90 | 1dfe3943 | Paul Brook | } else {
|
91 | 1dfe3943 | Paul Brook | data = from_bcd(data); |
92 | 1dfe3943 | Paul Brook | } |
93 | 1dfe3943 | Paul Brook | s->now.tm_hour = data; |
94 | 1dfe3943 | Paul Brook | break;
|
95 | 1dfe3943 | Paul Brook | case 3: |
96 | 1dfe3943 | Paul Brook | s->now.tm_wday = from_bcd(data & 7) - 1; |
97 | 1dfe3943 | Paul Brook | break;
|
98 | 1dfe3943 | Paul Brook | case 4: |
99 | 1dfe3943 | Paul Brook | s->now.tm_mday = from_bcd(data & 0x3f);
|
100 | 1dfe3943 | Paul Brook | break;
|
101 | 1dfe3943 | Paul Brook | case 5: |
102 | 1dfe3943 | Paul Brook | s->now.tm_mon = from_bcd(data & 0x1f) - 1; |
103 | 1dfe3943 | Paul Brook | case 6: |
104 | 1dfe3943 | Paul Brook | s->now.tm_year = from_bcd(data) + 100;
|
105 | 1dfe3943 | Paul Brook | break;
|
106 | 1dfe3943 | Paul Brook | case 7: |
107 | 1dfe3943 | Paul Brook | /* Control register. Currently ignored. */
|
108 | 1dfe3943 | Paul Brook | break;
|
109 | 1dfe3943 | Paul Brook | } |
110 | 1dfe3943 | Paul Brook | s->offset = qemu_timedate_diff(&s->now); |
111 | 1dfe3943 | Paul Brook | } |
112 | 1dfe3943 | Paul Brook | s->ptr = (s->ptr + 1) & 0xff; |
113 | 1dfe3943 | Paul Brook | return 0; |
114 | 1dfe3943 | Paul Brook | } |
115 | 1dfe3943 | Paul Brook | |
116 | 9e07bdf8 | Anthony Liguori | static int ds1338_init(I2CSlave *i2c) |
117 | 1dfe3943 | Paul Brook | { |
118 | 1dfe3943 | Paul Brook | return 0; |
119 | 1dfe3943 | Paul Brook | } |
120 | 1dfe3943 | Paul Brook | |
121 | b5ea9327 | Anthony Liguori | static void ds1338_class_init(ObjectClass *klass, void *data) |
122 | b5ea9327 | Anthony Liguori | { |
123 | b5ea9327 | Anthony Liguori | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
124 | b5ea9327 | Anthony Liguori | |
125 | b5ea9327 | Anthony Liguori | k->init = ds1338_init; |
126 | b5ea9327 | Anthony Liguori | k->event = ds1338_event; |
127 | b5ea9327 | Anthony Liguori | k->recv = ds1338_recv; |
128 | b5ea9327 | Anthony Liguori | k->send = ds1338_send; |
129 | b5ea9327 | Anthony Liguori | } |
130 | b5ea9327 | Anthony Liguori | |
131 | 39bffca2 | Anthony Liguori | static TypeInfo ds1338_info = {
|
132 | 39bffca2 | Anthony Liguori | .name = "ds1338",
|
133 | 39bffca2 | Anthony Liguori | .parent = TYPE_I2C_SLAVE, |
134 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(DS1338State),
|
135 | 39bffca2 | Anthony Liguori | .class_init = ds1338_class_init, |
136 | 1dfe3943 | Paul Brook | }; |
137 | 1dfe3943 | Paul Brook | |
138 | 1dfe3943 | Paul Brook | static void ds1338_register_devices(void) |
139 | 1dfe3943 | Paul Brook | { |
140 | 39bffca2 | Anthony Liguori | type_register_static(&ds1338_info); |
141 | 1dfe3943 | Paul Brook | } |
142 | 1dfe3943 | Paul Brook | |
143 | 1dfe3943 | Paul Brook | device_init(ds1338_register_devices) |