The Pedigree Project  0.1
modules/drivers/x86/ib700_wdt/main.cc
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 #include "modules/Module.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/machine/Device.h"
23 #include "pedigree/kernel/machine/Machine.h"
24 #include "pedigree/kernel/machine/Timer.h"
25 #include "pedigree/kernel/machine/TimerHandler.h"
26 #include "pedigree/kernel/processor/IoBase.h"
27 #include "pedigree/kernel/processor/state_forward.h"
28 #include "pedigree/kernel/processor/types.h"
29 #include "pedigree/kernel/utilities/String.h"
30 #include "pedigree/kernel/utilities/Vector.h"
31 #include "pedigree/kernel/utilities/utility.h"
32 
33 enum Ib700TimeEntries
34 {
35  Seconds30 = 0,
36  Seconds28 = 1,
37  Seconds26 = 2,
38  Seconds24 = 3,
39  Seconds22 = 4,
40  Seconds20 = 5,
41  Seconds18 = 6,
42  Seconds16 = 7,
43  Seconds14 = 8,
44  Seconds12 = 9,
45  Seconds10 = 10,
46  Seconds8 = 11,
47  Seconds6 = 12,
48  Seconds4 = 13,
49  Seconds2 = 14,
50  Seconds0 = 15,
51 };
52 
53 class Ib700Watchdog : public Device, public TimerHandler
54 {
55  public:
56  Ib700Watchdog(Device *pDev);
57 
58  virtual ~Ib700Watchdog();
59 
60  virtual bool initialise();
61 
62  virtual void getName(String &str);
63 
64  virtual void timer(uint64_t delta, InterruptState &state);
65 
66  private:
67  IoBase *m_pBase;
68 };
69 
70 static bool entry()
71 {
72  auto f = [](Device *p) {
73  if (p->addresses().count() > 0)
74  {
75  if (p->addresses()[0]->m_Name == "ib700-base")
76  {
77  Ib700Watchdog *pNewChild = new Ib700Watchdog(p);
78  if (pNewChild->initialise())
79  {
80  return static_cast<Device *>(pNewChild);
81  }
82  else
83  {
84  ERROR("IB700 initialisation failed!");
85  delete pNewChild;
86  }
87  }
88  }
89 
90  return p;
91  };
92 
93  auto c = pedigree_std::make_callable(f);
94  Device::foreach (c, 0);
95 
96  return true;
97 }
98 
99 static void exit()
100 {
101 }
102 
103 MODULE_INFO("ib700_wdt", &entry, &exit);
104 
105 Ib700Watchdog::Ib700Watchdog(Device *pDev) : Device(pDev)
106 {
107  setSpecificType(String("watchdog-timer"));
108 }
109 
110 Ib700Watchdog::~Ib700Watchdog()
111 {
112  if (m_pBase)
113  {
114  // Disable any existing timer.
115  m_pBase->write16(0, 2);
116  }
117 }
118 
119 bool Ib700Watchdog::initialise()
120 {
121  m_pBase = addresses()[0]->m_Io;
122  if (!m_pBase)
123  return false;
124 
125  // Disable any existing timer.
126  m_pBase->write16(0, 0);
127 
128  // Register ourselves with the core timer so we can continually
129  // reset the watchdog timer as needed.
130  Timer *t = Machine::instance().getTimer();
131  if (t)
132  {
133  t->registerHandler(this);
134 
135  // Enable our timer with a 10 second timeout.
136  m_pBase->write16(Seconds10, 2);
137 
138  return true;
139  }
140 
141  return false;
142 }
143 
145 {
146  str = "ib700_wdt";
147 }
148 
149 void Ib700Watchdog::timer(uint64_t delta, InterruptState &state)
150 {
151  // Timer fired, push the watchdog back now (watchdog expects to be
152  // polled by the system regularly).
153  m_pBase->write16(Seconds10, 2);
154 }
virtual void timer(uint64_t delta, InterruptState &state)
virtual Timer * getTimer()=0
virtual Vector< Address * > & addresses()
Definition: Device.h:256
Definition: String.h:49
Abstrace base class for hardware I/O capabilities.
Definition: IoBase.h:31
Definition: Device.h:43
virtual void write16(uint16_t value, size_t offset=0)=0
virtual void setSpecificType(String str)
Definition: Device.h:174
virtual void getName(String &str)
static void foreach(Callback callback, Device *root=0)
Definition: Device.cc:94
#define ERROR(text)
Definition: Log.h:82