1 /*
2 * Copyright (C) 2006-2009 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * COMMENT: A generic Real-Time Clock device, for the test machines
29 *
30 * It can be used to retrieve the current system time, and to cause periodic
31 * interrupts.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38
39 #include "cpu.h"
40 #include "device.h"
41 #include "emul.h"
42 #include "machine.h"
43 #include "memory.h"
44 #include "misc.h"
45 #include "timer.h"
46
47 #include "testmachine/dev_rtc.h"
48
49
50 #define DEV_RTC_TICK_SHIFT 14
51
52 struct rtc_data {
53 struct interrupt irq;
54 int pending_interrupts;
55
56 int hz;
57 struct timer *timer;
58
59 struct timeval cur_time;
60 };
61
62
63 /*
64 * timer_tick():
65 *
66 * This function is called d->hz times per second.
67 */
timer_tick(struct timer * t,void * extra)68 static void timer_tick(struct timer *t, void *extra)
69 {
70 struct rtc_data *d = (struct rtc_data *) extra;
71 d->pending_interrupts ++;
72 }
73
74
DEVICE_TICK(rtc)75 DEVICE_TICK(rtc)
76 {
77 struct rtc_data *d = (struct rtc_data *) extra;
78
79 if (d->pending_interrupts > 0)
80 INTERRUPT_ASSERT(d->irq);
81 else
82 INTERRUPT_DEASSERT(d->irq);
83 }
84
85
DEVICE_ACCESS(rtc)86 DEVICE_ACCESS(rtc)
87 {
88 struct rtc_data *d = (struct rtc_data *) extra;
89 uint64_t idata = 0, odata = 0;
90
91 if (writeflag == MEM_WRITE)
92 idata = memory_readmax64(cpu, data, len);
93
94 switch (relative_addr) {
95
96 case DEV_RTC_TRIGGER_READ:
97 gettimeofday(&d->cur_time, NULL);
98 break;
99
100 case DEV_RTC_SEC:
101 odata = d->cur_time.tv_sec;
102 break;
103
104 case DEV_RTC_USEC:
105 odata = d->cur_time.tv_usec;
106 break;
107
108 case DEV_RTC_HZ:
109 if (writeflag == MEM_READ) {
110 odata = d->hz;
111 } else {
112 d->hz = idata;
113
114 if (d->hz == 0) {
115 /* Remove the timer, if any: */
116 if (d->timer != NULL)
117 timer_remove(d->timer);
118
119 d->timer = NULL;
120 d->pending_interrupts = 0;
121 } else {
122 /* Add a timer, or update the existing one: */
123 if (d->timer == NULL)
124 d->timer = timer_add(d->hz,
125 timer_tick, d);
126 else
127 timer_update_frequency(d->timer, d->hz);
128 }
129 }
130 break;
131
132 case DEV_RTC_INTERRUPT_ACK:
133 if (d->pending_interrupts > 0)
134 d->pending_interrupts --;
135
136 INTERRUPT_DEASSERT(d->irq);
137
138 /* TODO: Reassert the interrupt here, if
139 d->pending_interrupts is still above zero? */
140
141 break;
142
143 default:if (writeflag == MEM_WRITE) {
144 fatal("[ rtc: unimplemented write to "
145 "offset 0x%x: data=0x%x ]\n", (int)
146 relative_addr, (int)idata);
147 } else {
148 fatal("[ rtc: unimplemented read from "
149 "offset 0x%x ]\n", (int)relative_addr);
150 }
151 }
152
153 if (writeflag == MEM_READ)
154 memory_writemax64(cpu, data, len, odata);
155
156 return 1;
157 }
158
159
DEVINIT(rtc)160 DEVINIT(rtc)
161 {
162 struct rtc_data *d;
163
164 CHECK_ALLOCATION(d = (struct rtc_data *) malloc(sizeof(struct rtc_data)));
165 memset(d, 0, sizeof(struct rtc_data));
166
167 INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
168
169 memory_device_register(devinit->machine->memory, devinit->name,
170 devinit->addr, DEV_RTC_LENGTH, dev_rtc_access, (void *)d,
171 DM_DEFAULT, NULL);
172
173 machine_add_tickfunction(devinit->machine,
174 dev_rtc_tick, d, DEV_RTC_TICK_SHIFT);
175
176 return 1;
177 }
178
179