The Pedigree Project  0.1
Rtc.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_MACHINE_X86_COMMON_RTC_H
21 #define KERNEL_MACHINE_X86_COMMON_RTC_H
22 
23 #include "pedigree/kernel/Spinlock.h"
24 #include "pedigree/kernel/compiler.h"
25 #include "pedigree/kernel/machine/IrqHandler.h"
26 #include "pedigree/kernel/machine/Timer.h"
27 #include "pedigree/kernel/machine/types.h"
28 #include "pedigree/kernel/processor/IoPort.h"
29 #include "pedigree/kernel/processor/state_forward.h"
30 #include "pedigree/kernel/processor/types.h"
31 #include "pedigree/kernel/utilities/List.h"
32 #include "pedigree/kernel/utilities/new"
33 
34 class TimerHandler;
35 
36 #define MAX_TIMER_HANDLERS 32
37 
42 class Rtc : public Timer, private IrqHandler
43 {
44  public:
45  inline static Rtc &instance()
46  {
47  return m_Instance;
48  }
49 
50  //
51  // Timer interface
52  //
53  virtual bool registerHandler(TimerHandler *handler);
54  virtual bool unregisterHandler(TimerHandler *handler);
55  virtual void
56  addAlarm(class Event *pEvent, size_t alarmSecs, size_t alarmUsecs = 0);
57  virtual void removeAlarm(class Event *pEvent);
58  virtual size_t removeAlarm(class Event *pEvent, bool bRetZero);
59  virtual size_t getYear();
60  virtual uint8_t getMonth();
61  virtual uint8_t getDayOfMonth();
62  virtual uint8_t getDayOfWeek();
63  virtual uint8_t getHour();
64  virtual uint8_t getMinute();
65  virtual uint8_t getSecond();
66  virtual uint64_t getNanosecond();
67  virtual uint64_t getTickCount();
68  virtual uint64_t getTickCountNano();
69 
76  virtual void synchronise(bool tohw = false);
78  void uninitialise();
79 
80  protected:
84  inline virtual ~Rtc()
85  {
86  }
87 
88  private:
91  Rtc(const Rtc &);
94  Rtc &operator=(const Rtc &);
95 
96  //
97  // IrqHandler interface
98  //
99  virtual bool irq(irq_id_t number, InterruptState &state);
100 
103  void setIndex(uint8_t index);
107  void waitForUpdateCompletion(uint8_t index);
111  void enableRtcUpdates(bool enable);
115  uint8_t read(uint8_t index);
119  void write(uint8_t index, uint8_t value);
120 
123 
125  irq_id_t m_IrqId;
126 
129 
131  bool m_bBCD;
132 
134  size_t m_Year;
136  uint8_t m_Month;
138  uint8_t m_DayOfMonth;
140  uint8_t m_Hour;
142  uint8_t m_Minute;
144  uint8_t m_Second;
146  uint64_t m_Nanosecond;
147 
149  uint64_t m_TickCount;
150 
153  {
155  size_t Hz;
157  uint8_t rateBits;
159  uint64_t ns[2];
160  };
161 
164 
166  static Rtc m_Instance;
167 
169  TimerHandler *m_Handlers[MAX_TIMER_HANDLERS];
170 
172  class Alarm
173  {
174  public:
175  Alarm(class Event *pEvent, size_t time, class Thread *pThread)
176  : m_pEvent(pEvent), m_Time(time), m_pThread(pThread)
177  {
178  }
179  class Event *m_pEvent;
180  size_t m_Time;
181  class Thread *m_pThread;
182 
183  private:
184  Alarm(const Alarm &);
185  Alarm &operator=(const Alarm &);
186  };
187 
192 
195 
197  uint64_t m_Tsc0;
198 };
199 
202 #endif
uint8_t m_DayOfMonth
Definition: Rtc.h:138
virtual void addAlarm(class Event *pEvent, size_t alarmSecs, size_t alarmUsecs=0)
Definition: Rtc.cc:72
IoPort m_IoPort
Definition: Rtc.h:122
uint64_t m_Nanosecond
Definition: Rtc.h:146
void write(uint8_t index, uint8_t value)
Definition: Rtc.cc:595
Rtc & operator=(const Rtc &)
uint64_t m_TscTicksPerNanosecond
Definition: Rtc.h:194
void waitForUpdateCompletion(uint8_t index)
Definition: Rtc.cc:566
size_t m_Year
Definition: Rtc.h:134
virtual uint8_t getHour()
Definition: Rtc.cc:201
virtual uint8_t getDayOfWeek()
Definition: Rtc.cc:186
uint8_t rateBits
Definition: Rtc.h:157
virtual uint64_t getTickCount()
Definition: Rtc.cc:217
virtual void synchronise(bool tohw=false)
Definition: Rtc.cc:336
uint8_t m_Second
Definition: Rtc.h:144
void uninitialise()
Definition: Rtc.cc:389
void enableRtcUpdates(bool enable)
Definition: Rtc.cc:575
virtual size_t getYear()
Definition: Rtc.cc:174
bool initialise1() INITIALISATION_ONLY
Definition: Rtc.cc:234
void setIndex(uint8_t index)
Definition: Rtc.cc:561
Definition: List.h:64
I/O port range.
Definition: IoPort.h:34
static periodicIrqInfo_t periodicIrqInfo[12]
Definition: Rtc.h:163
virtual uint64_t getNanosecond()
Definition: Rtc.cc:213
virtual uint8_t getMinute()
Definition: Rtc.cc:205
bool m_bBCD
Definition: Rtc.h:131
virtual uint8_t getDayOfMonth()
Definition: Rtc.cc:182
uint64_t ns[2]
Definition: Rtc.h:159
irq_id_t m_IrqId
Definition: Rtc.h:125
size_t m_PeriodicIrqInfoIndex
Definition: Rtc.h:128
virtual bool irq(irq_id_t number, InterruptState &state)
Definition: Rtc.cc:415
virtual uint8_t getSecond()
Definition: Rtc.cc:209
virtual uint8_t getMonth()
Definition: Rtc.cc:178
virtual ~Rtc()
Definition: Rtc.h:84
uint64_t m_TickCount
Definition: Rtc.h:149
TimerHandler * m_Handlers[MAX_TIMER_HANDLERS]
Definition: Rtc.h:169
Definition: Thread.h:54
Definition: Event.h:48
Spinlock m_Lock
Definition: Rtc.h:191
List< Alarm * > m_Alarms
Definition: Rtc.h:189
Rtc() INITIALISATION_ONLY
Definition: Rtc.cc:405
bool initialise2() INITIALISATION_ONLY
Definition: Rtc.cc:288
static Rtc m_Instance
Definition: Rtc.h:166
virtual void removeAlarm(class Event *pEvent)
Definition: Rtc.cc:86
uint8_t m_Hour
Definition: Rtc.h:140
uint64_t m_Tsc0
Definition: Rtc.h:197
Definition: Rtc.h:42
uint8_t read(uint8_t index)
Definition: Rtc.cc:584
uint8_t m_Month
Definition: Rtc.h:136
uint8_t m_Minute
Definition: Rtc.h:142
virtual uint64_t getTickCountNano()
Definition: Rtc.cc:221