[4c6651]: drivers / rtc / rtc-gcn.c Maximize Restore History

Download this file

rtc-gcn.c    340 lines (269 with data), 6.6 kB

  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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* drivers/rtc/rtc-gcn.c
*
* Nintendo GameCube/Wii RTC/SRAM driver
* Copyright (C) 2004-2009 The GameCube Linux Team
* Copyright (C) 2005,2008,2009 Albert Herranz
*
* Based on gamecube_time.c from Torben Nielsen.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
*/
#include <linux/init.h>
#include <linux/time.h>
#include <linux/rtc.h>
#include <linux/exi.h>
#include <asm/machdep.h>
#define DRV_MODULE_NAME "rtc-gcn"
#define DRV_DESCRIPTION "Nintendo GameCube/Wii RTC/SRAM driver"
#define DRV_AUTHOR "Torben Nielsen, " \
"Albert Herranz"
static char gcnrtc_driver_version[] = "1.0i";
#define drv_printk(level, format, arg...) \
printk(level DRV_MODULE_NAME ": " format , ## arg)
#define RTC_EXI_GCN_ID 0xffff1698
#define RTC_EXI_RVL_ID 0xfffff308
#define RTC_EXI_CHANNEL 0
#define RTC_EXI_DEVICE 1
#define RTC_EXI_FREQ 3 /* 8MHz */
#define RTC_OFFSET 946684800L
struct gcn_sram {
u16 csum1;
u16 csum2;
u32 ead0;
u32 ead1;
int bias;
s8 horz_display_offset;
u8 ntd;
u8 language;
u8 flags;
u8 reserved[44];
};
struct gcnrtc_drvdata {
spinlock_t lock;
struct exi_device *dev;
struct rtc_device *rtc_dev;
struct gcn_sram sram;
};
static struct gcnrtc_drvdata gcnrtc_drvdata;
/*
* Hardware interfaces.
*
*/
/*
* Loads the SRAM contents.
* Context: user.
*/
static void sram_load(struct exi_device *dev)
{
struct gcnrtc_drvdata *drvdata = exi_get_drvdata(dev);
struct gcn_sram *sram = &drvdata->sram;
u32 req;
exi_dev_take(dev);
/* select the SRAM device */
exi_dev_select(dev);
/* send the appropriate command */
req = 0x20000100;
exi_dev_write(dev, &req, sizeof(req));
/* read the SRAM data */
exi_dev_read(dev, sram, sizeof(*sram));
/* deselect the SRAM device */
exi_dev_deselect(dev);
exi_dev_give(dev);
return;
}
/*
* Gets the hardware clock date and time.
* Context: user.
*/
static unsigned long gcnrtc_read_time(struct exi_device *dev)
{
unsigned long a = 0;
exi_dev_take(dev);
/* select the SRAM device */
exi_dev_select(dev);
/* send the appropriate command */
a = 0x20000000;
exi_dev_write(dev, &a, sizeof(a));
/* read the time and date value */
exi_dev_read(dev, &a, sizeof(a));
/* deselect the RTC device */
exi_dev_deselect(dev);
exi_dev_give(dev);
return a;
}
/*
* Sets the hardware clock date and time to @aval.
* Context: user, interrupt (adjtimex).
*/
static int gcnrtc_write_time(struct exi_device *dev, unsigned long aval)
{
u32 req;
int retval;
/*
* We may get called from the timer interrupt. In that case,
* we could fail if the exi channel used to access the RTC
* is busy. If this happens, we just return an error. The timer
* interrupt code is prepared to deal with such case.
*/
retval = exi_dev_try_take(dev);
if (!retval) {
/* select the RTC device */
exi_dev_select(dev);
/* send the appropriate command */
req = 0xa0000000;
exi_dev_write(dev, &req, sizeof(req));
/* set the new time and date value */
exi_dev_write(dev, &aval, sizeof(aval));
/* deselect the RTC device */
exi_dev_deselect(dev);
exi_dev_give(dev);
}
return retval;
}
/*
* Platform time functions.
*
*/
/*
* Platform specific function to return the current date and time.
*/
static void gcnrtc_plat_rtc_get_time(struct rtc_time *t)
{
struct gcnrtc_drvdata *drvdata = &gcnrtc_drvdata;
unsigned long nowtime;
if (!drvdata->dev)
return;
nowtime = gcnrtc_read_time(drvdata->dev) +
drvdata->sram.bias + RTC_OFFSET;
rtc_time_to_tm(nowtime, t);
}
/*
* Platform specific function to set the current date and time.
*
*/
static int gcnrtc_plat_rtc_set_time(struct rtc_time *t)
{
struct gcnrtc_drvdata *drvdata = &gcnrtc_drvdata;
unsigned long nowtime;
if (!drvdata->dev)
return -ENODEV;
rtc_tm_to_time(t, &nowtime);
return gcnrtc_write_time(drvdata->dev,
nowtime - RTC_OFFSET - drvdata->sram.bias);
}
/*
* RTC class driver.
*
*/
/*
*
*/
static int gcnrtc_rtc_read_time(struct device *dev, struct rtc_time *t)
{
gcnrtc_plat_rtc_get_time(t);
return 0;
}
/*
*
*/
static int gcnrtc_rtc_set_time(struct device *dev, struct rtc_time *t)
{
return gcnrtc_plat_rtc_set_time(t);
}
static const struct rtc_class_ops gcnrtc_ops = {
.read_time = gcnrtc_rtc_read_time,
.set_time = gcnrtc_rtc_set_time,
};
/*
* EXI driver.
*
*/
/*
*
*/
static int gcnrtc_probe(struct exi_device *dev)
{
struct gcnrtc_drvdata *drvdata = &gcnrtc_drvdata;
unsigned long flags;
int retval = -ENODEV;
if (exi_device_get(dev)) {
spin_lock_init(&drvdata->lock);
exi_set_drvdata(dev, drvdata);
drvdata->dev = dev;
memset(&drvdata->sram, 0, sizeof(struct gcn_sram));
sram_load(dev);
spin_lock_irqsave(&drvdata->lock, flags);
ppc_md.set_rtc_time = gcnrtc_plat_rtc_set_time;
ppc_md.get_rtc_time = gcnrtc_plat_rtc_get_time;
spin_unlock_irqrestore(&drvdata->lock, flags);
drvdata->rtc_dev = rtc_device_register(DRV_MODULE_NAME,
&dev->dev,
&gcnrtc_ops,
THIS_MODULE);
retval = 0;
}
return retval;
}
/*
*
*/
static void gcnrtc_remove(struct exi_device *dev)
{
struct gcnrtc_drvdata *drvdata = exi_get_drvdata(dev);
unsigned long flags;
if (drvdata) {
spin_lock_irqsave(&drvdata->lock, flags);
ppc_md.set_rtc_time = NULL;
ppc_md.get_rtc_time = NULL;
spin_unlock_irqrestore(&drvdata->lock, flags);
if (!IS_ERR(drvdata->rtc_dev))
rtc_device_unregister(drvdata->rtc_dev);
}
exi_device_put(dev);
}
static struct exi_device_id gcnrtc_eid_table[] = {
{
.channel = RTC_EXI_CHANNEL,
.device = RTC_EXI_DEVICE,
.id = RTC_EXI_GCN_ID
},
{
.channel = RTC_EXI_CHANNEL,
.device = RTC_EXI_DEVICE,
.id = RTC_EXI_RVL_ID
},
{ },
};
static struct exi_driver gcnrtc_driver = {
.name = DRV_MODULE_NAME,
.eid_table = gcnrtc_eid_table,
.frequency = RTC_EXI_FREQ,
.probe = gcnrtc_probe,
.remove = gcnrtc_remove,
};
/*
*
*/
static int __init gcnrtc_init_module(void)
{
drv_printk(KERN_INFO, "%s - version %s\n",
DRV_DESCRIPTION, gcnrtc_driver_version);
return exi_driver_register(&gcnrtc_driver);
}
/*
*
*/
static void __exit gcnrtc_exit_module(void)
{
exi_driver_unregister(&gcnrtc_driver);
}
module_init(gcnrtc_init_module);
module_exit(gcnrtc_exit_module);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_LICENSE("GPL");