1 |
/* |
2 |
* Copyright (C) 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_rs5c313.c,v 1.1 2007/04/13 16:07:26 debug Exp $ |
29 |
* |
30 |
* RICOH RS5C313 Real Time Clock |
31 |
* |
32 |
* The RS5C313 has 16 registers, see rs5c313reg.h for details. These registers |
33 |
* are addressed at byte offsets. |
34 |
* |
35 |
* Note: The only use for this device so far is in the Landisk, connected to |
36 |
* the SH4 SCI pins. In the Landisk machine, the RS5C313 is placed at |
37 |
* a fake (high) address in memory. |
38 |
*/ |
39 |
|
40 |
#include <stdio.h> |
41 |
#include <stdlib.h> |
42 |
#include <string.h> |
43 |
#include <time.h> |
44 |
|
45 |
#include "cpu.h" |
46 |
#include "device.h" |
47 |
#include "machine.h" |
48 |
#include "memory.h" |
49 |
#include "misc.h" |
50 |
|
51 |
#include "rs5c313reg.h" |
52 |
|
53 |
|
54 |
#define DEV_RS5C313_LENGTH 16 |
55 |
|
56 |
struct rs5c313_data { |
57 |
uint8_t reg[DEV_RS5C313_LENGTH]; |
58 |
}; |
59 |
|
60 |
|
61 |
/* |
62 |
* rs5c313_update_time(): |
63 |
* |
64 |
* Set the RS5C313 registers to correspond to the host's clock. |
65 |
*/ |
66 |
static void rs5c313_update_time(struct rs5c313_data *d) |
67 |
{ |
68 |
struct tm *tmp; |
69 |
time_t timet; |
70 |
|
71 |
timet = time(NULL); |
72 |
tmp = gmtime(&timet); |
73 |
|
74 |
d->reg[RS5C313_SEC1] = tmp->tm_sec % 10; |
75 |
d->reg[RS5C313_SEC10] = tmp->tm_sec / 10; |
76 |
d->reg[RS5C313_MIN1] = tmp->tm_min % 10; |
77 |
d->reg[RS5C313_MIN10] = tmp->tm_min / 10; |
78 |
d->reg[RS5C313_HOUR1] = tmp->tm_hour % 10; |
79 |
d->reg[RS5C313_HOUR10] = tmp->tm_hour / 10; |
80 |
|
81 |
/* WDAY. Zero-based. TODO: Is this correct? */ |
82 |
d->reg[RS5C313_WDAY] = tmp->tm_wday; |
83 |
|
84 |
d->reg[RS5C313_DAY1] = tmp->tm_mday % 10; |
85 |
d->reg[RS5C313_DAY10] = tmp->tm_mday / 10; |
86 |
d->reg[RS5C313_MON1] = (tmp->tm_mon + 1) % 10; |
87 |
d->reg[RS5C313_MON10] = (tmp->tm_mon + 1) / 10; |
88 |
d->reg[RS5C313_YEAR1] = tmp->tm_year % 10; |
89 |
d->reg[RS5C313_YEAR10] = (tmp->tm_year / 10) % 10; |
90 |
} |
91 |
|
92 |
|
93 |
DEVICE_ACCESS(rs5c313) |
94 |
{ |
95 |
struct rs5c313_data *d = (struct rs5c313_data *) extra; |
96 |
uint64_t idata = 0, odata = 0; |
97 |
|
98 |
rs5c313_update_time(d); |
99 |
|
100 |
/* Generic register read/write: */ |
101 |
if (writeflag == MEM_WRITE) { |
102 |
idata = memory_readmax64(cpu, data, len); |
103 |
d->reg[relative_addr] = idata & 0x0f; |
104 |
} else { |
105 |
odata = d->reg[relative_addr] & 0x0f; |
106 |
memory_writemax64(cpu, data, len, odata); |
107 |
} |
108 |
|
109 |
return 1; |
110 |
} |
111 |
|
112 |
|
113 |
DEVINIT(rs5c313) |
114 |
{ |
115 |
struct rs5c313_data *d = malloc(sizeof(struct rs5c313_data)); |
116 |
|
117 |
if (d == NULL) { |
118 |
fprintf(stderr, "out of memory\n"); |
119 |
exit(1); |
120 |
} |
121 |
memset(d, 0, sizeof(struct rs5c313_data)); |
122 |
|
123 |
/* Default values: */ |
124 |
d->reg[RS5C313_CTRL] = CTRL_24H; |
125 |
|
126 |
memory_device_register(devinit->machine->memory, devinit->name, |
127 |
devinit->addr, DEV_RS5C313_LENGTH, |
128 |
dev_rs5c313_access, (void *)d, DM_DEFAULT, NULL); |
129 |
|
130 |
return 1; |
131 |
} |
132 |
|