/[gxemul]/upstream/0.4.6/src/devices/dev_rtc.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/0.4.6/src/devices/dev_rtc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (show annotations)
Mon Oct 8 16:22:43 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 4468 byte(s)
0.4.6
1 /*
2 * Copyright (C) 2006-2007 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 * $Id: dev_rtc.c,v 1.7 2007/06/15 19:57:34 debug Exp $
29 *
30 * COMMENT: A generic Real-Time Clock device, for the test machines
31 *
32 * It can be used to retrieve the current system time, and to cause periodic
33 * interrupts.
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/time.h>
40
41 #include "cpu.h"
42 #include "device.h"
43 #include "emul.h"
44 #include "machine.h"
45 #include "memory.h"
46 #include "misc.h"
47 #include "timer.h"
48
49 #include "testmachine/dev_rtc.h"
50
51
52 #define DEV_RTC_TICK_SHIFT 14
53
54 struct rtc_data {
55 struct interrupt irq;
56 int pending_interrupts;
57
58 int hz;
59 struct timer *timer;
60
61 struct timeval cur_time;
62 };
63
64
65 /*
66 * timer_tick():
67 *
68 * This function is called d->hz times per second.
69 */
70 static void timer_tick(struct timer *t, void *extra)
71 {
72 struct rtc_data *d = (struct rtc_data *) extra;
73 d->pending_interrupts ++;
74 }
75
76
77 DEVICE_TICK(rtc)
78 {
79 struct rtc_data *d = extra;
80
81 if (d->pending_interrupts > 0)
82 INTERRUPT_ASSERT(d->irq);
83 else
84 INTERRUPT_DEASSERT(d->irq);
85 }
86
87
88 DEVICE_ACCESS(rtc)
89 {
90 struct rtc_data *d = extra;
91 uint64_t idata = 0, odata = 0;
92
93 if (writeflag == MEM_WRITE)
94 idata = memory_readmax64(cpu, data, len);
95
96 switch (relative_addr) {
97
98 case DEV_RTC_TRIGGER_READ:
99 gettimeofday(&d->cur_time, NULL);
100 break;
101
102 case DEV_RTC_SEC:
103 odata = d->cur_time.tv_sec;
104 break;
105
106 case DEV_RTC_USEC:
107 odata = d->cur_time.tv_usec;
108 break;
109
110 case DEV_RTC_HZ:
111 if (writeflag == MEM_READ) {
112 odata = d->hz;
113 } else {
114 d->hz = idata;
115
116 if (d->hz == 0) {
117 /* Remove the timer, if any: */
118 if (d->timer != NULL)
119 timer_remove(d->timer);
120
121 d->timer = NULL;
122 d->pending_interrupts = 0;
123 } else {
124 /* Add a timer, or update the existing one: */
125 if (d->timer == NULL)
126 d->timer = timer_add(d->hz,
127 timer_tick, d);
128 else
129 timer_update_frequency(d->timer, d->hz);
130 }
131 }
132 break;
133
134 case DEV_RTC_INTERRUPT_ACK:
135 if (d->pending_interrupts > 0)
136 d->pending_interrupts --;
137
138 INTERRUPT_DEASSERT(d->irq);
139
140 /* TODO: Reassert the interrupt here, if
141 d->pending_interrupts is still above zero? */
142
143 break;
144
145 default:if (writeflag == MEM_WRITE) {
146 fatal("[ rtc: unimplemented write to "
147 "offset 0x%x: data=0x%x ]\n", (int)
148 relative_addr, (int)idata);
149 } else {
150 fatal("[ rtc: unimplemented read from "
151 "offset 0x%x ]\n", (int)relative_addr);
152 }
153 }
154
155 if (writeflag == MEM_READ)
156 memory_writemax64(cpu, data, len, odata);
157
158 return 1;
159 }
160
161
162 DEVINIT(rtc)
163 {
164 struct rtc_data *d;
165
166 CHECK_ALLOCATION(d = malloc(sizeof(struct rtc_data)));
167 memset(d, 0, sizeof(struct rtc_data));
168
169 INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
170
171 memory_device_register(devinit->machine->memory, devinit->name,
172 devinit->addr, DEV_RTC_LENGTH, dev_rtc_access, (void *)d,
173 DM_DEFAULT, NULL);
174
175 machine_add_tickfunction(devinit->machine,
176 dev_rtc_tick, d, DEV_RTC_TICK_SHIFT);
177
178 return 1;
179 }
180

  ViewVC Help
Powered by ViewVC 1.1.26