]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/wallclock/dallas/ds12887/v2_0/src/ds12887.cxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / wallclock / dallas / ds12887 / v2_0 / src / ds12887.cxx
1 //==========================================================================
2 //
3 //      devs/wallclock/ds12887.inl
4 //
5 //      Wallclock implementation for Dallas 12887
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     jskov
44 // Contributors:  jskov
45 // Date:          2001-07-06
46 // Purpose:       Wallclock driver for Dallas 12887
47 //
48 //####DESCRIPTIONEND####
49 //
50 //==========================================================================
51
52 #include <pkgconf/wallclock.h>          // Wallclock device config
53
54 #include <cyg/hal/hal_io.h>             // IO macros
55 #include <cyg/hal/hal_intr.h>           // interrupt enable/disable
56 #include <cyg/infra/cyg_type.h>         // Common type definitions and support
57
58 #include <cyg/io/wallclock.hxx>         // The WallClock API
59 #include <cyg/io/wallclock/wallclock.inl> // Helpers
60
61 #include <cyg/infra/diag.h>
62
63 #define nDEBUG
64
65 // Platform details
66 #include CYGDAT_DEVS_WALLCLOCK_DALLAS_12887_INL
67
68 #ifndef DS_READ_UINT8
69 # define DS_READ_UINT8(x,y) HAL_READ_UINT8(x,y)
70 # define DS_WRITE_UINT8(x,y) HAL_WRITE_UINT8(x,y)
71 #endif
72
73 #if !defined(DS_READ) && !defined(DS_WRITE) //  Allow for INL to define this
74 #ifdef DS_LINEAR
75 # ifndef DS_STEP
76 #  define DS_STEP 0
77 # endif
78 # ifndef DS_BASE
79 #  error "Need to know base of DS12887 part"
80 # endif
81 #define DS_READ( offset, data)  \
82     DS_READ_UINT8( DS_BASE + ((offset) << DS_STEP), (data))
83 #define DS_WRITE(offset, data)  \
84     DS_WRITE_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
85 #else   //  !DS_LINEAR
86 # if !defined(DS_ADDR) || !defined(DS_DATA)
87 #  error "Need to know addr/data locations of DS12887 part"
88 # endif
89 # define DS_READ(offset, data)                 \
90   CYG_MACRO_START                              \
91   DS_WRITE_UINT8(DS_ADDR, (offset));           \
92   DS_READ_UINT8(DS_DATA, (data));              \
93   CYG_MACRO_END
94 # define DS_WRITE(offset, data)                \
95   CYG_MACRO_START                              \
96   DS_WRITE_UINT8(DS_ADDR, (offset));           \
97   DS_WRITE_UINT8(DS_DATA, (data));             \
98   CYG_MACRO_END
99 #endif
100 #endif  //  ! DS_READ && ! DS_WRITE
101
102 // Registers
103 #define DS_SECONDS         0x00
104 #define DS_SECONDS_ALARM   0x01
105 #define DS_MINUTES         0x02
106 #define DS_MINUTES_ALARM   0x03
107 #define DS_HOURS           0x04
108 #define DS_HOURS_ALARM     0x05
109 #define DS_DOW             0x06
110 #define DS_DOM             0x07
111 #define DS_MONTH           0x08
112 #define DS_YEAR            0x09
113 #define DS_CENTURY         0x32
114
115 #define DS_REG_A           0x0a
116 #define DS_REG_B           0x0b
117 #define DS_REG_C           0x0c
118 #define DS_REG_D           0x0d
119
120 // Control bits
121 #define DS_REG_A_UIP       0x80
122 #define DS_REG_A_ENABLE    0x20
123
124 #define DS_REG_B_SET       0x80
125 #define DS_REG_B_DM        0x04
126 #define DS_REG_B_24H       0x02
127
128
129 //----------------------------------------------------------------------------
130 // Accessor functions
131 static inline void
132 init_ds_hwclock(void)
133 {
134     cyg_uint8 _regb, _tmp;
135
136     // Set 24H mode
137     DS_WRITE(DS_REG_B, DS_REG_B_24H);
138     // Enable clock
139     DS_WRITE(DS_REG_A, DS_REG_A_ENABLE);
140     
141     // Verify that there are reasonable default settings - otherwise
142     // set them.
143
144     // Stop counting
145     DS_READ(DS_REG_B, _regb);
146     _regb |= DS_REG_B_SET;
147     DS_WRITE(DS_REG_B, _regb);
148
149     DS_READ(DS_CENTURY, _tmp);
150     if (0xff == _tmp)
151         DS_WRITE(DS_CENTURY, TO_BCD(20));
152
153     DS_READ(DS_MONTH, _tmp);
154     if (0x00 == _tmp)
155         DS_WRITE(DS_MONTH, TO_BCD(1));
156
157     DS_READ(DS_DOM, _tmp);
158     if (0x00 == _tmp)
159         DS_WRITE(DS_DOM, TO_BCD(1));
160
161     DS_READ(DS_DOM, _tmp);
162     if (0x00 == _tmp)
163         DS_WRITE(DS_DOM, TO_BCD(1));
164
165     // Restart counting
166     _regb &= ~DS_REG_B_SET;
167     DS_WRITE(DS_REG_B, _regb);
168 }
169
170
171 static inline void
172 set_ds_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday,
173                cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second)
174 {
175     cyg_uint8 _regb;
176     // Stop counting
177     DS_READ(DS_REG_B, _regb);
178     _regb |= DS_REG_B_SET;
179     DS_WRITE(DS_REG_B, _regb);
180
181     DS_WRITE(DS_CENTURY, TO_BCD((cyg_uint8)(year / 100)));
182     DS_WRITE(DS_YEAR, TO_BCD((cyg_uint8)(year % 100)));
183     DS_WRITE(DS_MONTH, TO_BCD((cyg_uint8)month));
184     DS_WRITE(DS_DOM, TO_BCD((cyg_uint8)mday));
185     DS_WRITE(DS_HOURS, TO_BCD((cyg_uint8)hour));
186     DS_WRITE(DS_MINUTES, TO_BCD((cyg_uint8)minute));
187     DS_WRITE(DS_SECONDS, TO_BCD((cyg_uint8)second));
188
189     // Restart counting
190     _regb &= ~DS_REG_B_SET;
191     DS_WRITE(DS_REG_B, _regb);
192
193 #ifdef DEBUG
194     // This will cause the test to eventually fail due to these printouts
195     // causing timer interrupts to be lost...
196     diag_printf("Set -------------\n");
197     diag_printf("year %02d\n", year);
198     diag_printf("month %02d\n", month);
199     diag_printf("mday %02d\n", mday);
200     diag_printf("hour %02d\n", hour);
201     diag_printf("minute %02d\n", minute);
202     diag_printf("second %02d\n", second);
203 #endif
204 }
205
206 static inline void
207 get_ds_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday,
208                cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second)
209 {
210     cyg_uint8 _reg, _t1, _t2;
211     cyg_uint32 _old;
212
213     // Wait for update flag clears
214     do {
215         DS_READ(DS_REG_A, _reg);
216     } while (_reg & DS_REG_A_UIP);
217
218     // Disable interrupts while reading to ensure it doesn't take more
219     // than 244us.
220     HAL_DISABLE_INTERRUPTS(_old);
221
222     DS_READ(DS_CENTURY, _t1);
223     DS_READ(DS_YEAR, _t2);
224     *year = (cyg_uint32)TO_DEC(_t1)*100 + (cyg_uint32)TO_DEC(_t2);
225
226     DS_READ(DS_MONTH, _t1);
227     *month = (cyg_uint32)TO_DEC(_t1);
228
229     DS_READ(DS_DOM, _t1);
230     *mday = (cyg_uint32)TO_DEC(_t1);
231
232     DS_READ(DS_HOURS, _t1);
233     *hour = (cyg_uint32)TO_DEC(_t1);
234
235     DS_READ(DS_MINUTES, _t1);
236     *minute = (cyg_uint32)TO_DEC(_t1);
237
238     DS_READ(DS_SECONDS, _t1);
239     *second = (cyg_uint32)TO_DEC(_t1);
240
241     // Reenable interrupts
242     HAL_RESTORE_INTERRUPTS(_old);
243
244 #ifdef DEBUG
245     // This will cause the test to eventually fail due to these printouts
246     // causing timer interrupts to be lost...
247     diag_printf("year %02d\n", *year);
248     diag_printf("month %02d\n", *month);
249     diag_printf("mday %02d\n", *mday);
250     diag_printf("hour %02d\n", *hour);
251     diag_printf("minute %02d\n", *minute);
252     diag_printf("second %02d\n", *second);
253 #endif
254 }
255
256 //-----------------------------------------------------------------------------
257 // Functions required for the hardware-driver API.
258
259 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
260 cyg_uint32 
261 Cyg_WallClock::get_hw_seconds(void)
262 {
263     cyg_uint32 year, month, mday, hour, minute, second;
264
265     get_ds_hwclock(&year, &month, &mday, &hour, &minute, &second);
266
267     cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second);
268     return now;
269 }
270
271 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
272
273 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
274 void
275 Cyg_WallClock::set_hw_seconds( cyg_uint32 secs )
276 {
277     cyg_uint32 year, month, mday, hour, minute, second;
278
279     _simple_mkdate(secs, &year, &month, &mday, &hour, &minute, &second);
280
281     set_ds_hwclock(year, month, mday, hour, minute, second);
282 }
283
284 #endif
285
286 void
287 Cyg_WallClock::init_hw_seconds(void)
288 {
289 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
290     init_ds_hwclock();
291 #else
292     // This is our base: 1970-01-01 00:00:00
293     // Set the HW clock - if for nothing else, just to be sure it's in a
294     // legal range. Any arbitrary base could be used.
295     // After this the hardware clock is only read.
296     set_ds_hwclock(1970,1,1,0,0,0);
297 #endif
298 }
299
300 //-----------------------------------------------------------------------------
301 // End of devs/wallclock/ds12887.inl