Subversion Repositories shark

Rev

Rev 684 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
684 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
23
 CVS :        $Id: rtc.c,v 1.1 2004-05-26 10:12:41 giacomo Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2004-05-26 10:12:41 $
28
 ------------
29
 
30
 Author:      Massimiliano Giorgi
31
 
32
 A source from Linux 2.2.9 modified to work with S.Ha.R.K.
33
 
34
**/
35
 
36
/*
37
 * Copyright (C) 2000 Paolo Gai
38
 *
39
 * This program is free software; you can redistribute it and/or modify
40
 * it under the terms of the GNU General Public License as published by
41
 * the Free Software Foundation; either version 2 of the License, or
42
 * (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
52
 *
53
 */
54
 
55
/*
56
 *      Real Time Clock interface for Linux    
57
 *
58
 *      Copyright (C) 1996 Paul Gortmaker
59
 *
60
 *      This driver allows use of the real time clock (built into
61
 *      nearly all computers) from user space. It exports the /dev/rtc
62
 *      interface supporting various ioctl() and also the /proc/rtc
63
 *      pseudo-file for status information.
64
 *
65
 *      The ioctls can be used to set the interrupt behaviour and
66
 *      generation rate from the RTC via IRQ 8. Then the /dev/rtc
67
 *      interface can be used to make use of these timer interrupts,
68
 *      be they interval or alarm based.
69
 *
70
 *      The /dev/rtc interface will block on reads until an interrupt
71
 *      has been received. If a RTC interrupt has already happened,
72
 *      it will output an unsigned long and then block. The output value
73
 *      contains the interrupt status in the low byte and the number of
74
 *      interrupts since the last read in the remaining high bytes. The
75
 *      /dev/rtc interface can also be used with the select(2) call.
76
 *
77
 *      This program is free software; you can redistribute it and/or
78
 *      modify it under the terms of the GNU General Public License
79
 *      as published by the Free Software Foundation; either version
80
 *      2 of the License, or (at your option) any later version.
81
 *
82
 *      Based on other minimal char device drivers, like Alan's
83
 *      watchdog, Ted's random, etc. etc.
84
 *
85
 *      1.07    Paul Gortmaker.
86
 *      1.08    Miquel van Smoorenburg: disallow certain things on the
87
 *              DEC Alpha as the CMOS clock is also used for other things.
88
 *      1.09    Nikita Schmidt: epoch support and some Alpha cleanup.
89
 *
90
 */
91
 
92
#define RTC_VERSION             "1.09"
93
 
94
#define RTC_IRQ         8       /* Can't see this changing soon.        */
95
#define RTC_IO_EXTENT   0x10    /* Only really two ports, but...        */
96
 
97
/*
98
 *      Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
99
 *      interrupts disabled. Due to the index-port/data-port (0x70/0x71)
100
 *      design of the RTC, we don't want two different things trying to
101
 *      get to it at once. (e.g. the periodic 11 min sync from time.c vs.
102
 *      this driver.)
103
 */
104
 
105
#include <kernel/kern.h>
106
#include <rtcfunc.h>
107
#include "rtc.h"
108
/*
109
#define CMOS_READ(addr) ({ \
110
outb_p((addr),RTC_PORT(0)); \
111
inb_p(RTC_PORT(1)); \
112
})
113
#define CMOS_WRITE(val, addr) ({ \
114
outb_p((addr),RTC_PORT(0)); \
115
outb_p((val),RTC_PORT(1)); \
116
})
117
*/
118
 
119
#define CMOS_READ(addr) (     \
120
  ll_out(RTC_PORT(0),addr),   \
121
  ll_in(RTC_PORT(1))          \
122
)
123
 
124
#define CMOS_WRITE(val, addr) (\
125
  ll_out(RTC_PORT(0),addr),    \
126
  ll_out(RTC_PORT(1),val)      \
127
)
128
 
129
/*
130
 *      We sponge a minor off of the misc major. No need slurping
131
 *      up another valuable major dev number for this. If you add
132
 *      an ioctl, make sure you don't conflict with SPARC's RTC
133
 *      ioctls.
134
 */
135
 
136
 
137
int get_rtc_time (struct rtc_time *rtc_tm);
138
int set_rtc_time (struct rtc_time *rtc_tm);
139
int get_rtc_alm_time (struct rtc_time *alm_tm);
140
 
141
void set_rtc_irq_bit(unsigned char bit);
142
void mask_rtc_irq_bit(unsigned char bit);
143
 
144
static inline unsigned char rtc_is_updating(void);
145
 
146
/*
147
 *      Bits in rtc_status. (6 bits of room for future expansion)
148
 */
149
 
150
#define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
151
#define RTC_TIMER_ON            0x02    /* missed irq timer active      */
152
 
153
unsigned char rtc_status = 0;           /* bitmapped status byte.       */
154
unsigned long rtc_freq = 0;             /* Current periodic IRQ rate    */
155
unsigned long rtc_irq_data = 0;         /* our output to the world      */
156
 
157
/*
158
 *      If this driver ever becomes modularised, it will be really nice
159
 *      to make the epoch retain its value across module reload...
160
 */
161
 
162
static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
163
 
164
unsigned char days_in_mo[] =
165
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
166
 
167
/*
168
 * Returns true if a clock update is in progress
169
 */
170
static inline unsigned char rtc_is_updating(void)
171
{
172
  SYS_FLAGS flags;
173
  unsigned char uip;
174
 
175
  flags=kern_fsave();
176
  uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
177
  kern_frestore(flags);
178
  return uip;
179
}
180
 
181
int get_rtc_time(struct rtc_time *rtc_tm)
182
{
183
  SYS_FLAGS flags;
184
  unsigned char ctrl;
185
  unsigned retries=0;
186
  struct timespec delay;
187
 
188
  /*
189
   * read RTC once any update in progress is done. The update
190
   * can take just over 2ms. We wait 10 to 20ms. There is no need to
191
   * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
192
   * If you need to know *exactly* when a second has started, enable
193
   * periodic update complete interrupts, (via ioctl) and then
194
   * immediately read /dev/rtc which will block until you get the IRQ.
195
   * Once the read clears, read the RTC time (again via ioctl). Easy.
196
   */
197
 
198
  /*
199
  if (rtc_is_updating() != 0)
200
    while (jiffies - uip_watchdog < 2*HZ/100)
201
      barrier();
202
  */
203
 
204
  delay.tv_nsec = 1000000;
205
  delay.tv_sec = 0;
206
  while (rtc_is_updating()&&++retries<=5) nanosleep(&delay, NULL);
207
  if (retries>5) return -1;
208
 
209
  /*
210
   * Only the values that we read from the RTC are set. We leave
211
   * tm_wday, tm_yday and tm_isdst untouched. Even though the
212
   * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
213
   * by the RTC when initially set to a non-zero value.
214
   */
215
  flags=kern_fsave();
216
  rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
217
  rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
218
  rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
219
  rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
220
  rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
221
  rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
222
  ctrl = CMOS_READ(RTC_CONTROL);
223
  kern_frestore(flags);
224
 
225
  if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
226
    {
227
      BCD_TO_BIN(rtc_tm->tm_sec);
228
      BCD_TO_BIN(rtc_tm->tm_min);
229
      BCD_TO_BIN(rtc_tm->tm_hour);
230
      BCD_TO_BIN(rtc_tm->tm_mday);
231
      BCD_TO_BIN(rtc_tm->tm_mon);
232
      BCD_TO_BIN(rtc_tm->tm_year);
233
    }
234
 
235
  /*
236
   * Account for differences between how the RTC uses the values
237
   * and how they are defined in a struct rtc_time;
238
   */
239
  if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
240
    rtc_tm->tm_year += 100;
241
 
242
  rtc_tm->tm_mon--;
243
 
244
  return 0;
245
}
246
 
247
int set_rtc_time(struct rtc_time *rtc_tm)
248
{
249
  unsigned char mon, day, hrs, min, sec, leap_yr;
250
  unsigned char save_control, save_freq_select;
251
  unsigned int yrs;
252
  SYS_FLAGS flags;
253
 
254
  yrs = rtc_tm->tm_year + 1900;
255
  mon = rtc_tm->tm_mon + 1;   /* tm_mon starts at zero */
256
  day = rtc_tm->tm_mday;
257
  hrs = rtc_tm->tm_hour;
258
  min = rtc_tm->tm_min;
259
  sec = rtc_tm->tm_sec;
260
 
261
  if (yrs < 1970)
262
    return -EINVAL;
263
 
264
  leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
265
 
266
  if ((mon > 12) || (day == 0))
267
    return -EINVAL;
268
 
269
  if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
270
    return -EINVAL;
271
 
272
  if ((hrs >= 24) || (min >= 60) || (sec >= 60))
273
    return -EINVAL;
274
 
275
  if ((yrs -= epoch) > 255)    /* They are unsigned */
276
    return -EINVAL;
277
 
278
  flags=kern_fsave();
279
  if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
280
      || RTC_ALWAYS_BCD) {
281
    if (yrs > 169) {
282
      kern_frestore(flags);
283
      return -EINVAL;
284
    }
285
    if (yrs >= 100)
286
      yrs -= 100;
287
 
288
    BIN_TO_BCD(sec);
289
    BIN_TO_BCD(min);
290
    BIN_TO_BCD(hrs);
291
    BIN_TO_BCD(day);
292
    BIN_TO_BCD(mon);
293
    BIN_TO_BCD(yrs);
294
  }
295
 
296
  save_control = CMOS_READ(RTC_CONTROL);
297
  CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
298
  save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
299
  CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
300
 
301
  CMOS_WRITE(yrs, RTC_YEAR);
302
  CMOS_WRITE(mon, RTC_MONTH);
303
  CMOS_WRITE(day, RTC_DAY_OF_MONTH);
304
  CMOS_WRITE(hrs, RTC_HOURS);
305
  CMOS_WRITE(min, RTC_MINUTES);
306
  CMOS_WRITE(sec, RTC_SECONDS);
307
 
308
  CMOS_WRITE(save_control, RTC_CONTROL);
309
  CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
310
 
311
  kern_frestore(flags);
312
 
313
  return 0;
314
}
315
 
316
int get_rtc_alm_time(struct rtc_time *alm_tm)
317
{
318
  SYS_FLAGS flags;
319
  unsigned char ctrl;
320
 
321
  /*
322
   * Only the values that we read from the RTC are set. That
323
   * means only tm_hour, tm_min, and tm_sec.
324
   */
325
  flags=kern_fsave();
326
  alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
327
  alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
328
  alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
329
  ctrl = CMOS_READ(RTC_CONTROL);
330
  kern_frestore(flags);
331
 
332
  if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
333
    {
334
      BCD_TO_BIN(alm_tm->tm_sec);
335
      BCD_TO_BIN(alm_tm->tm_min);
336
      BCD_TO_BIN(alm_tm->tm_hour);
337
    }
338
 
339
  return 0;
340
}
341
 
342
/*
343
 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
344
 * Rumour has it that if you frob the interrupt enable/disable
345
 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
346
 * ensure you actually start getting interrupts. Probably for
347
 * compatibility with older/broken chipset RTC implementations.
348
 * We also clear out any old irq data after an ioctl() that
349
 * meddles with the interrupt enable/disable bits.
350
 */
351
 
352
void mask_rtc_irq_bit(unsigned char bit)
353
{
354
  unsigned char val;
355
  SYS_FLAGS flags;
356
 
357
  flags=kern_fsave();
358
  //cli();
359
  val = CMOS_READ(RTC_CONTROL);
360
  val &=  ~bit;
361
  CMOS_WRITE(val, RTC_CONTROL);
362
  CMOS_READ(RTC_INTR_FLAGS);
363
  kern_frestore(flags);
364
  //rtc_irq_data = 0;
365
}
366
 
367
void set_rtc_irq_bit(unsigned char bit)
368
{
369
  unsigned char val;
370
  SYS_FLAGS flags;
371
 
372
  flags=kern_fsave();
373
  //cli();
374
  val = CMOS_READ(RTC_CONTROL);
375
  val |= bit;
376
  CMOS_WRITE(val, RTC_CONTROL);
377
  CMOS_READ(RTC_INTR_FLAGS);
378
  //rtc_irq_data = 0;
379
  kern_frestore(flags);
380
}
381
 
382
/* added by Massy */
383
/* to find the date in seconds from the Epoch (1 Gen 1970 00:00 GMT) */
384
/* (modifing a source from Linux) */
385
 
386
static int day_n[]={
387
  0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0
388
};
389
 
390
time_t sys_getdate(void)
391
{
392
  struct rtc_time rtc;
393
  time_t secs;
394
 
395
  get_rtc_time (&rtc);
396
 
397
  secs = rtc.tm_sec+60l*rtc.tm_min+rtc.tm_hour*3600l+86400l*
398
    (rtc.tm_mday-1+day_n[rtc.tm_mon]+(rtc.tm_year/4l)
399
    +rtc.tm_year*365l-
400
    ((rtc.tm_year & 3) == 0 && rtc.tm_mon < 2 ? 1 : 0)+3653l);
401
 
402
  /* days since 1.1.70 plus 80's leap day */
403
  /*secs += sys_tz.tz_minuteswest*60;*/
404
  /*if (sys_tz.tz_dsttime) secs -= 3600;*/
405
  return secs;
406
}