]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/devs/wallclock/dallas/ds12887/v2_0/src/ds12887.cxx
c51f25caf55b20735ac5ea932a55512bff4b8730
[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 #ifdef DS_LINEAR
74 # ifndef DS_STEP
75 #  define DS_STEP 0
76 # endif
77 # ifndef DS_BASE
78 #  error "Need to know base of DS12887 part"
79 # endif
80 # define DS_READ(offset, data) DS_READ_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
81 # define DS_WRITE(offset, data) DS_WRITE_UINT8(DS_BASE + ((offset) << DS_STEP), (data))
82 #else
83 # if !defined(DS_ADDR) || !defined(DS_DATA)
84 #  error "Need to know addr/data locations of DS12887 part"
85 # endif
86 # define DS_READ(offset, data)                 \
87   CYG_MACRO_START                              \
88   DS_WRITE_UINT8(DS_ADDR, (offset));           \
89   DS_READ_UINT8(DS_DATA, (data));              \
90   CYG_MACRO_END
91 # define DS_WRITE(offset, data)                \
92   CYG_MACRO_START                              \
93   DS_WRITE_UINT8(DS_ADDR, (offset));           \
94   DS_WRITE_UINT8(DS_DATA, (data));             \
95   CYG_MACRO_END
96 #endif
97
98 // Registers
99 #define DS_SECONDS         0x00
100 #define DS_SECONDS_ALARM   0x01
101 #define DS_MINUTES         0x02
102 #define DS_MINUTES_ALARM   0x03
103 #define DS_HOURS           0x04
104 #define DS_HOURS_ALARM     0x05
105 #define DS_DOW             0x06
106 #define DS_DOM             0x07
107 #define DS_MONTH           0x08
108 #define DS_YEAR            0x09
109 #define DS_CENTURY         0x32
110
111 #define DS_REG_A           0x0a
112 #define DS_REG_B           0x0b
113 #define DS_REG_C           0x0c
114 #define DS_REG_D           0x0d
115
116 // Control bits
117 #define DS_REG_A_UIP       0x80
118 #define DS_REG_A_ENABLE    0x20
119
120 #define DS_REG_B_SET       0x80
121 #define DS_REG_B_DM        0x04
122 #define DS_REG_B_24H       0x02
123
124
125 //----------------------------------------------------------------------------
126 // Accessor functions
127 static inline void
128 init_ds_hwclock(void)
129 {
130     cyg_uint8 _regb, _tmp;
131
132     // Set 24H mode
133     DS_WRITE(DS_REG_B, DS_REG_B_24H);
134     // Enable clock
135     DS_WRITE(DS_REG_A, DS_REG_A_ENABLE);
136     
137     // Verify that there are reasonable default settings - otherwise
138     // set them.
139
140     // Stop counting
141     DS_READ(DS_REG_B, _regb);
142     _regb |= DS_REG_B_SET;
143     DS_WRITE(DS_REG_B, _regb);
144
145     DS_READ(DS_CENTURY, _tmp);
146     if (0xff == _tmp)
147         DS_WRITE(DS_CENTURY, TO_BCD(20));
148
149     DS_READ(DS_MONTH, _tmp);
150     if (0x00 == _tmp)
151         DS_WRITE(DS_MONTH, TO_BCD(1));
152
153     DS_READ(DS_DOM, _tmp);
154     if (0x00 == _tmp)
155         DS_WRITE(DS_DOM, TO_BCD(1));
156
157     DS_READ(DS_DOM, _tmp);
158     if (0x00 == _tmp)
159         DS_WRITE(DS_DOM, TO_BCD(1));
160
161     // Restart counting
162     _regb &= ~DS_REG_B_SET;
163     DS_WRITE(DS_REG_B, _regb);
164 }
165
166
167 static inline void
168 set_ds_hwclock(cyg_uint32 year, cyg_uint32 month, cyg_uint32 mday,
169                cyg_uint32 hour, cyg_uint32 minute, cyg_uint32 second)
170 {
171     cyg_uint8 _regb;
172     // Stop counting
173     DS_READ(DS_REG_B, _regb);
174     _regb |= DS_REG_B_SET;
175     DS_WRITE(DS_REG_B, _regb);
176
177     DS_WRITE(DS_CENTURY, TO_BCD((cyg_uint8)(year / 100)));
178     DS_WRITE(DS_YEAR, TO_BCD((cyg_uint8)(year % 100)));
179     DS_WRITE(DS_MONTH, TO_BCD((cyg_uint8)month));
180     DS_WRITE(DS_DOM, TO_BCD((cyg_uint8)mday));
181     DS_WRITE(DS_HOURS, TO_BCD((cyg_uint8)hour));
182     DS_WRITE(DS_MINUTES, TO_BCD((cyg_uint8)minute));
183     DS_WRITE(DS_SECONDS, TO_BCD((cyg_uint8)second));
184
185     // Restart counting
186     _regb &= ~DS_REG_B_SET;
187     DS_WRITE(DS_REG_B, _regb);
188
189 #ifdef DEBUG
190     // This will cause the test to eventually fail due to these printouts
191     // causing timer interrupts to be lost...
192     diag_printf("Set -------------\n");
193     diag_printf("year %02d\n", year);
194     diag_printf("month %02d\n", month);
195     diag_printf("mday %02d\n", mday);
196     diag_printf("hour %02d\n", hour);
197     diag_printf("minute %02d\n", minute);
198     diag_printf("second %02d\n", second);
199 #endif
200 }
201
202 static inline void
203 get_ds_hwclock(cyg_uint32* year, cyg_uint32* month, cyg_uint32* mday,
204                cyg_uint32* hour, cyg_uint32* minute, cyg_uint32* second)
205 {
206     cyg_uint8 _reg, _t1, _t2;
207     cyg_uint32 _old;
208
209     // Wait for update flag clears
210     do {
211         DS_READ(DS_REG_A, _reg);
212     } while (_reg & DS_REG_A_UIP);
213
214     // Disable interrupts while reading to ensure it doesn't take more
215     // than 244us.
216     HAL_DISABLE_INTERRUPTS(_old);
217
218     DS_READ(DS_CENTURY, _t1);
219     DS_READ(DS_YEAR, _t2);
220     *year = (cyg_uint32)TO_DEC(_t1)*100 + (cyg_uint32)TO_DEC(_t2);
221
222     DS_READ(DS_MONTH, _t1);
223     *month = (cyg_uint32)TO_DEC(_t1);
224
225     DS_READ(DS_DOM, _t1);
226     *mday = (cyg_uint32)TO_DEC(_t1);
227
228     DS_READ(DS_HOURS, _t1);
229     *hour = (cyg_uint32)TO_DEC(_t1);
230
231     DS_READ(DS_MINUTES, _t1);
232     *minute = (cyg_uint32)TO_DEC(_t1);
233
234     DS_READ(DS_SECONDS, _t1);
235     *second = (cyg_uint32)TO_DEC(_t1);
236
237     // Reenable interrupts
238     HAL_RESTORE_INTERRUPTS(_old);
239
240 #ifdef DEBUG
241     // This will cause the test to eventually fail due to these printouts
242     // causing timer interrupts to be lost...
243     diag_printf("year %02d\n", *year);
244     diag_printf("month %02d\n", *month);
245     diag_printf("mday %02d\n", *mday);
246     diag_printf("hour %02d\n", *hour);
247     diag_printf("minute %02d\n", *minute);
248     diag_printf("second %02d\n", *second);
249 #endif
250 }
251
252 //-----------------------------------------------------------------------------
253 // Functions required for the hardware-driver API.
254
255 // Returns the number of seconds elapsed since 1970-01-01 00:00:00.
256 cyg_uint32 
257 Cyg_WallClock::get_hw_seconds(void)
258 {
259     cyg_uint32 year, month, mday, hour, minute, second;
260
261     get_ds_hwclock(&year, &month, &mday, &hour, &minute, &second);
262
263     cyg_uint32 now = _simple_mktime(year, month, mday, hour, minute, second);
264     return now;
265 }
266
267 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
268
269 // Sets the clock. Argument is seconds elapsed since 1970-01-01 00:00:00.
270 void
271 Cyg_WallClock::set_hw_seconds( cyg_uint32 secs )
272 {
273     cyg_uint32 year, month, mday, hour, minute, second;
274
275     _simple_mkdate(secs, &year, &month, &mday, &hour, &minute, &second);
276
277     set_ds_hwclock(year, month, mday, hour, minute, second);
278 }
279
280 #endif
281
282 void
283 Cyg_WallClock::init_hw_seconds(void)
284 {
285 #ifdef CYGSEM_WALLCLOCK_SET_GET_MODE
286     init_ds_hwclock();
287 #else
288     // This is our base: 1970-01-01 00:00:00
289     // Set the HW clock - if for nothing else, just to be sure it's in a
290     // legal range. Any arbitrary base could be used.
291     // After this the hardware clock is only read.
292     set_ds_hwclock(1970,1,1,0,0,0);
293 #endif
294 }
295
296 //-----------------------------------------------------------------------------
297 // End of devs/wallclock/ds12887.inl