OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [char/] [ds1286.c] - Blame information for rev 62

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 62 marcus.erl
/*
2
 * DS1286 Real Time Clock interface for Linux
3
 *
4
 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
5
 *
6
 * Based on code written by Paul Gortmaker.
7
 *
8
 * This driver allows use of the real time clock (built into nearly all
9
 * computers) from user space. It exports the /dev/rtc interface supporting
10
 * various ioctl() and also the /proc/rtc pseudo-file for status
11
 * information.
12
 *
13
 * The ioctls can be used to set the interrupt behaviour and generation rate
14
 * from the RTC via IRQ 8. Then the /dev/rtc interface can be used to make
15
 * use of these timer interrupts, be they interval or alarm based.
16
 *
17
 * The /dev/rtc interface will block on reads until an interrupt has been
18
 * received. If a RTC interrupt has already happened, it will output an
19
 * unsigned long and then block. The output value contains the interrupt
20
 * status in the low byte and the number of interrupts since the last read
21
 * in the remaining high bytes. The /dev/rtc interface can also be used with
22
 * the select(2) call.
23
 *
24
 * This program is free software; you can redistribute it and/or modify it
25
 * under the terms of the GNU General Public License as published by the
26
 * Free Software Foundation; either version 2 of the License, or (at your
27
 * option) any later version.
28
 */
29
#include <linux/ds1286.h>
30
#include <linux/types.h>
31
#include <linux/errno.h>
32
#include <linux/miscdevice.h>
33
#include <linux/slab.h>
34
#include <linux/ioport.h>
35
#include <linux/fcntl.h>
36
#include <linux/init.h>
37
#include <linux/poll.h>
38
#include <linux/rtc.h>
39
#include <linux/spinlock.h>
40
#include <linux/bcd.h>
41
#include <linux/proc_fs.h>
42
 
43
#include <asm/uaccess.h>
44
#include <asm/system.h>
45
 
46
#define DS1286_VERSION          "1.0"
47
 
48
/*
49
 *      We sponge a minor off of the misc major. No need slurping
50
 *      up another valuable major dev number for this. If you add
51
 *      an ioctl, make sure you don't conflict with SPARC's RTC
52
 *      ioctls.
53
 */
54
 
55
static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait);
56
 
57
static ssize_t ds1286_read(struct file *file, char *buf,
58
                        size_t count, loff_t *ppos);
59
 
60
static int ds1286_ioctl(struct inode *inode, struct file *file,
61
                        unsigned int cmd, unsigned long arg);
62
 
63
static unsigned int ds1286_poll(struct file *file, poll_table *wait);
64
 
65
static void ds1286_get_alm_time (struct rtc_time *alm_tm);
66
static void ds1286_get_time(struct rtc_time *rtc_tm);
67
static int ds1286_set_time(struct rtc_time *rtc_tm);
68
 
69
static inline unsigned char ds1286_is_updating(void);
70
 
71
static DEFINE_SPINLOCK(ds1286_lock);
72
 
73
static int ds1286_read_proc(char *page, char **start, off_t off,
74
                            int count, int *eof, void *data);
75
 
76
/*
77
 *      Bits in rtc_status. (7 bits of room for future expansion)
78
 */
79
 
80
#define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
81
#define RTC_TIMER_ON            0x02    /* missed irq timer active      */
82
 
83
static unsigned char ds1286_status;     /* bitmapped status byte.       */
84
 
85
static unsigned char days_in_mo[] = {
86
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
87
};
88
 
89
/*
90
 *      Now all the various file operations that we export.
91
 */
92
 
93
static ssize_t ds1286_read(struct file *file, char *buf,
94
                           size_t count, loff_t *ppos)
95
{
96
        return -EIO;
97
}
98
 
99
static int ds1286_ioctl(struct inode *inode, struct file *file,
100
                        unsigned int cmd, unsigned long arg)
101
{
102
        struct rtc_time wtime;
103
 
104
        switch (cmd) {
105
        case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
106
        {
107
                unsigned long flags;
108
                unsigned char val;
109
 
110
                if (!capable(CAP_SYS_TIME))
111
                        return -EACCES;
112
 
113
                spin_lock_irqsave(&ds1286_lock, flags);
114
                val = rtc_read(RTC_CMD);
115
                val |=  RTC_TDM;
116
                rtc_write(val, RTC_CMD);
117
                spin_unlock_irqrestore(&ds1286_lock, flags);
118
 
119
                return 0;
120
        }
121
        case RTC_AIE_ON:        /* Allow alarm interrupts.      */
122
        {
123
                unsigned long flags;
124
                unsigned char val;
125
 
126
                if (!capable(CAP_SYS_TIME))
127
                        return -EACCES;
128
 
129
                spin_lock_irqsave(&ds1286_lock, flags);
130
                val = rtc_read(RTC_CMD);
131
                val &=  ~RTC_TDM;
132
                rtc_write(val, RTC_CMD);
133
                spin_unlock_irqrestore(&ds1286_lock, flags);
134
 
135
                return 0;
136
        }
137
        case RTC_WIE_OFF:       /* Mask watchdog int. enab. bit */
138
        {
139
                unsigned long flags;
140
                unsigned char val;
141
 
142
                if (!capable(CAP_SYS_TIME))
143
                        return -EACCES;
144
 
145
                spin_lock_irqsave(&ds1286_lock, flags);
146
                val = rtc_read(RTC_CMD);
147
                val |= RTC_WAM;
148
                rtc_write(val, RTC_CMD);
149
                spin_unlock_irqrestore(&ds1286_lock, flags);
150
 
151
                return 0;
152
        }
153
        case RTC_WIE_ON:        /* Allow watchdog interrupts.   */
154
        {
155
                unsigned long flags;
156
                unsigned char val;
157
 
158
                if (!capable(CAP_SYS_TIME))
159
                        return -EACCES;
160
 
161
                spin_lock_irqsave(&ds1286_lock, flags);
162
                val = rtc_read(RTC_CMD);
163
                val &= ~RTC_WAM;
164
                rtc_write(val, RTC_CMD);
165
                spin_unlock_irqrestore(&ds1286_lock, flags);
166
 
167
                return 0;
168
        }
169
        case RTC_ALM_READ:      /* Read the present alarm time */
170
        {
171
                /*
172
                 * This returns a struct rtc_time. Reading >= 0xc0
173
                 * means "don't care" or "match all". Only the tm_hour,
174
                 * tm_min, and tm_sec values are filled in.
175
                 */
176
 
177
                memset(&wtime, 0, sizeof(wtime));
178
                ds1286_get_alm_time(&wtime);
179
                break;
180
        }
181
        case RTC_ALM_SET:       /* Store a time into the alarm */
182
        {
183
                /*
184
                 * This expects a struct rtc_time. Writing 0xff means
185
                 * "don't care" or "match all". Only the tm_hour,
186
                 * tm_min and tm_sec are used.
187
                 */
188
                unsigned char hrs, min, sec;
189
                struct rtc_time alm_tm;
190
 
191
                if (!capable(CAP_SYS_TIME))
192
                        return -EACCES;
193
 
194
                if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
195
                                   sizeof(struct rtc_time)))
196
                        return -EFAULT;
197
 
198
                hrs = alm_tm.tm_hour;
199
                min = alm_tm.tm_min;
200
                sec = alm_tm.tm_sec;
201
 
202
                if (hrs >= 24)
203
                        hrs = 0xff;
204
 
205
                if (min >= 60)
206
                        min = 0xff;
207
 
208
                if (sec != 0)
209
                        return -EINVAL;
210
 
211
                min = BIN2BCD(min);
212
                min = BIN2BCD(hrs);
213
 
214
                spin_lock(&ds1286_lock);
215
                rtc_write(hrs, RTC_HOURS_ALARM);
216
                rtc_write(min, RTC_MINUTES_ALARM);
217
                spin_unlock(&ds1286_lock);
218
 
219
                return 0;
220
        }
221
        case RTC_RD_TIME:       /* Read the time/date from RTC  */
222
        {
223
                memset(&wtime, 0, sizeof(wtime));
224
                ds1286_get_time(&wtime);
225
                break;
226
        }
227
        case RTC_SET_TIME:      /* Set the RTC */
228
        {
229
                struct rtc_time rtc_tm;
230
 
231
                if (!capable(CAP_SYS_TIME))
232
                        return -EACCES;
233
 
234
                if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
235
                                   sizeof(struct rtc_time)))
236
                        return -EFAULT;
237
 
238
                return ds1286_set_time(&rtc_tm);
239
        }
240
        default:
241
                return -EINVAL;
242
        }
243
        return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
244
}
245
 
246
/*
247
 *      We enforce only one user at a time here with the open/close.
248
 *      Also clear the previous interrupt data on an open, and clean
249
 *      up things on a close.
250
 */
251
 
252
static int ds1286_open(struct inode *inode, struct file *file)
253
{
254
        spin_lock_irq(&ds1286_lock);
255
 
256
        if (ds1286_status & RTC_IS_OPEN)
257
                goto out_busy;
258
 
259
        ds1286_status |= RTC_IS_OPEN;
260
 
261
        spin_unlock_irq(&ds1286_lock);
262
        return 0;
263
 
264
out_busy:
265
        spin_lock_irq(&ds1286_lock);
266
        return -EBUSY;
267
}
268
 
269
static int ds1286_release(struct inode *inode, struct file *file)
270
{
271
        ds1286_status &= ~RTC_IS_OPEN;
272
 
273
        return 0;
274
}
275
 
276
static unsigned int ds1286_poll(struct file *file, poll_table *wait)
277
{
278
        poll_wait(file, &ds1286_wait, wait);
279
 
280
        return 0;
281
}
282
 
283
/*
284
 *      The various file operations we support.
285
 */
286
 
287
static const struct file_operations ds1286_fops = {
288
        .llseek         = no_llseek,
289
        .read           = ds1286_read,
290
        .poll           = ds1286_poll,
291
        .ioctl          = ds1286_ioctl,
292
        .open           = ds1286_open,
293
        .release        = ds1286_release,
294
};
295
 
296
static struct miscdevice ds1286_dev=
297
{
298
        .minor  = RTC_MINOR,
299
        .name   = "rtc",
300
        .fops   = &ds1286_fops,
301
};
302
 
303
static int __init ds1286_init(void)
304
{
305
        int err;
306
 
307
        printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION);
308
 
309
        err = misc_register(&ds1286_dev);
310
        if (err)
311
                goto out;
312
 
313
        if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc, NULL)) {
314
                err = -ENOMEM;
315
 
316
                goto out_deregister;
317
        }
318
 
319
        return 0;
320
 
321
out_deregister:
322
        misc_deregister(&ds1286_dev);
323
 
324
out:
325
        return err;
326
}
327
 
328
static void __exit ds1286_exit(void)
329
{
330
        remove_proc_entry("driver/rtc", NULL);
331
        misc_deregister(&ds1286_dev);
332
}
333
 
334
static char *days[] = {
335
        "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
336
};
337
 
338
/*
339
 *      Info exported via "/proc/rtc".
340
 */
341
static int ds1286_proc_output(char *buf)
342
{
343
        char *p, *s;
344
        struct rtc_time tm;
345
        unsigned char hundredth, month, cmd, amode;
346
 
347
        p = buf;
348
 
349
        ds1286_get_time(&tm);
350
        hundredth = rtc_read(RTC_HUNDREDTH_SECOND);
351
        BCD_TO_BIN(hundredth);
352
 
353
        p += sprintf(p,
354
                     "rtc_time\t: %02d:%02d:%02d.%02d\n"
355
                     "rtc_date\t: %04d-%02d-%02d\n",
356
                     tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth,
357
                     tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
358
 
359
        /*
360
         * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
361
         * match any value for that particular field. Values that are
362
         * greater than a valid time, but less than 0xc0 shouldn't appear.
363
         */
364
        ds1286_get_alm_time(&tm);
365
        p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]);
366
        if (tm.tm_hour <= 24)
367
                p += sprintf(p, "%02d:", tm.tm_hour);
368
        else
369
                p += sprintf(p, "**:");
370
 
371
        if (tm.tm_min <= 59)
372
                p += sprintf(p, "%02d\n", tm.tm_min);
373
        else
374
                p += sprintf(p, "**\n");
375
 
376
        month = rtc_read(RTC_MONTH);
377
        p += sprintf(p,
378
                     "oscillator\t: %s\n"
379
                     "square_wave\t: %s\n",
380
                     (month & RTC_EOSC) ? "disabled" : "enabled",
381
                     (month & RTC_ESQW) ? "disabled" : "enabled");
382
 
383
        amode = ((rtc_read(RTC_MINUTES_ALARM) & 0x80) >> 5) |
384
                ((rtc_read(RTC_HOURS_ALARM) & 0x80) >> 6) |
385
                ((rtc_read(RTC_DAY_ALARM) & 0x80) >> 7);
386
        if (amode == 7)      s = "each minute";
387
        else if (amode == 3) s = "minutes match";
388
        else if (amode == 1) s = "hours and minutes match";
389
        else if (amode == 0) s = "days, hours and minutes match";
390
        else                 s = "invalid";
391
        p += sprintf(p, "alarm_mode\t: %s\n", s);
392
 
393
        cmd = rtc_read(RTC_CMD);
394
        p += sprintf(p,
395
                     "alarm_enable\t: %s\n"
396
                     "wdog_alarm\t: %s\n"
397
                     "alarm_mask\t: %s\n"
398
                     "wdog_alarm_mask\t: %s\n"
399
                     "interrupt_mode\t: %s\n"
400
                     "INTB_mode\t: %s_active\n"
401
                     "interrupt_pins\t: %s\n",
402
                     (cmd & RTC_TDF) ? "yes" : "no",
403
                     (cmd & RTC_WAF) ? "yes" : "no",
404
                     (cmd & RTC_TDM) ? "disabled" : "enabled",
405
                     (cmd & RTC_WAM) ? "disabled" : "enabled",
406
                     (cmd & RTC_PU_LVL) ? "pulse" : "level",
407
                     (cmd & RTC_IBH_LO) ? "low" : "high",
408
                     (cmd & RTC_IPSW) ? "unswapped" : "swapped");
409
 
410
        return  p - buf;
411
}
412
 
413
static int ds1286_read_proc(char *page, char **start, off_t off,
414
                         int count, int *eof, void *data)
415
{
416
        int len = ds1286_proc_output (page);
417
        if (len <= off+count) *eof = 1;
418
        *start = page + off;
419
        len -= off;
420
        if (len>count)
421
                len = count;
422
        if (len<0)
423
                len = 0;
424
 
425
        return len;
426
}
427
 
428
/*
429
 * Returns true if a clock update is in progress
430
 */
431
static inline unsigned char ds1286_is_updating(void)
432
{
433
        return rtc_read(RTC_CMD) & RTC_TE;
434
}
435
 
436
 
437
static void ds1286_get_time(struct rtc_time *rtc_tm)
438
{
439
        unsigned char save_control;
440
        unsigned long flags;
441
        unsigned long uip_watchdog = jiffies;
442
 
443
        /*
444
         * read RTC once any update in progress is done. The update
445
         * can take just over 2ms. We wait 10 to 20ms. There is no need to
446
         * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
447
         * If you need to know *exactly* when a second has started, enable
448
         * periodic update complete interrupts, (via ioctl) and then
449
         * immediately read /dev/rtc which will block until you get the IRQ.
450
         * Once the read clears, read the RTC time (again via ioctl). Easy.
451
         */
452
 
453
        if (ds1286_is_updating() != 0)
454
                while (jiffies - uip_watchdog < 2*HZ/100)
455
                        barrier();
456
 
457
        /*
458
         * Only the values that we read from the RTC are set. We leave
459
         * tm_wday, tm_yday and tm_isdst untouched. Even though the
460
         * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
461
         * by the RTC when initially set to a non-zero value.
462
         */
463
        spin_lock_irqsave(&ds1286_lock, flags);
464
        save_control = rtc_read(RTC_CMD);
465
        rtc_write((save_control|RTC_TE), RTC_CMD);
466
 
467
        rtc_tm->tm_sec = rtc_read(RTC_SECONDS);
468
        rtc_tm->tm_min = rtc_read(RTC_MINUTES);
469
        rtc_tm->tm_hour = rtc_read(RTC_HOURS) & 0x3f;
470
        rtc_tm->tm_mday = rtc_read(RTC_DATE);
471
        rtc_tm->tm_mon = rtc_read(RTC_MONTH) & 0x1f;
472
        rtc_tm->tm_year = rtc_read(RTC_YEAR);
473
 
474
        rtc_write(save_control, RTC_CMD);
475
        spin_unlock_irqrestore(&ds1286_lock, flags);
476
 
477
        BCD_TO_BIN(rtc_tm->tm_sec);
478
        BCD_TO_BIN(rtc_tm->tm_min);
479
        BCD_TO_BIN(rtc_tm->tm_hour);
480
        BCD_TO_BIN(rtc_tm->tm_mday);
481
        BCD_TO_BIN(rtc_tm->tm_mon);
482
        BCD_TO_BIN(rtc_tm->tm_year);
483
 
484
        /*
485
         * Account for differences between how the RTC uses the values
486
         * and how they are defined in a struct rtc_time;
487
         */
488
        if (rtc_tm->tm_year < 45)
489
                rtc_tm->tm_year += 30;
490
        if ((rtc_tm->tm_year += 40) < 70)
491
                rtc_tm->tm_year += 100;
492
 
493
        rtc_tm->tm_mon--;
494
}
495
 
496
static int ds1286_set_time(struct rtc_time *rtc_tm)
497
{
498
        unsigned char mon, day, hrs, min, sec, leap_yr;
499
        unsigned char save_control;
500
        unsigned int yrs;
501
        unsigned long flags;
502
 
503
 
504
        yrs = rtc_tm->tm_year + 1900;
505
        mon = rtc_tm->tm_mon + 1;   /* tm_mon starts at zero */
506
        day = rtc_tm->tm_mday;
507
        hrs = rtc_tm->tm_hour;
508
        min = rtc_tm->tm_min;
509
        sec = rtc_tm->tm_sec;
510
 
511
        if (yrs < 1970)
512
                return -EINVAL;
513
 
514
        leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
515
 
516
        if ((mon > 12) || (day == 0))
517
                return -EINVAL;
518
 
519
        if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
520
                return -EINVAL;
521
 
522
        if ((hrs >= 24) || (min >= 60) || (sec >= 60))
523
                return -EINVAL;
524
 
525
        if ((yrs -= 1940) > 255)    /* They are unsigned */
526
                return -EINVAL;
527
 
528
        if (yrs >= 100)
529
                yrs -= 100;
530
 
531
        BIN_TO_BCD(sec);
532
        BIN_TO_BCD(min);
533
        BIN_TO_BCD(hrs);
534
        BIN_TO_BCD(day);
535
        BIN_TO_BCD(mon);
536
        BIN_TO_BCD(yrs);
537
 
538
        spin_lock_irqsave(&ds1286_lock, flags);
539
        save_control = rtc_read(RTC_CMD);
540
        rtc_write((save_control|RTC_TE), RTC_CMD);
541
 
542
        rtc_write(yrs, RTC_YEAR);
543
        rtc_write(mon, RTC_MONTH);
544
        rtc_write(day, RTC_DATE);
545
        rtc_write(hrs, RTC_HOURS);
546
        rtc_write(min, RTC_MINUTES);
547
        rtc_write(sec, RTC_SECONDS);
548
        rtc_write(0, RTC_HUNDREDTH_SECOND);
549
 
550
        rtc_write(save_control, RTC_CMD);
551
        spin_unlock_irqrestore(&ds1286_lock, flags);
552
 
553
        return 0;
554
}
555
 
556
static void ds1286_get_alm_time(struct rtc_time *alm_tm)
557
{
558
        unsigned char cmd;
559
        unsigned long flags;
560
 
561
        /*
562
         * Only the values that we read from the RTC are set. That
563
         * means only tm_wday, tm_hour, tm_min.
564
         */
565
        spin_lock_irqsave(&ds1286_lock, flags);
566
        alm_tm->tm_min = rtc_read(RTC_MINUTES_ALARM) & 0x7f;
567
        alm_tm->tm_hour = rtc_read(RTC_HOURS_ALARM)  & 0x1f;
568
        alm_tm->tm_wday = rtc_read(RTC_DAY_ALARM)    & 0x07;
569
        cmd = rtc_read(RTC_CMD);
570
        spin_unlock_irqrestore(&ds1286_lock, flags);
571
 
572
        BCD_TO_BIN(alm_tm->tm_min);
573
        BCD_TO_BIN(alm_tm->tm_hour);
574
        alm_tm->tm_sec = 0;
575
}
576
 
577
module_init(ds1286_init);
578
module_exit(ds1286_exit);
579
 
580
MODULE_AUTHOR("Ralf Baechle");
581
MODULE_LICENSE("GPL");
582
MODULE_ALIAS_MISCDEV(RTC_MINOR);

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.