]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/mips/cavium/octeon_ds1337.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / mips / cavium / octeon_ds1337.c
1 /***********************license start***************
2  *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3  *  reserved.
4  *
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are
8  *  met:
9  *
10  *      * Redistributions of source code must retain the above copyright
11  *        notice, this list of conditions and the following disclaimer.
12  *
13  *      * Redistributions in binary form must reproduce the above
14  *        copyright notice, this list of conditions and the following
15  *        disclaimer in the documentation and/or other materials provided
16  *        with the distribution.
17  *
18  *      * Neither the name of Cavium Networks nor the names of
19  *        its contributors may be used to endorse or promote products
20  *        derived from this software without specific prior written
21  *        permission.
22  *
23  *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24  *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25  *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26  *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27  *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28  *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29  *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30  *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31  *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32  *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33  *
34  *
35  *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36  *
37  ***********************license end**************************************/
38
39
40
41
42
43
44 /**
45  * @file
46  *
47  * Interface to the EBH-30xx specific devices
48  *
49  * <hr>$Revision: 41586 $<hr>
50  *
51  */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include <sys/param.h>
57 #include <sys/timespec.h>
58 #include <sys/clock.h>
59 #include <sys/libkern.h>
60
61 #include <contrib/octeon-sdk/cvmx.h>
62 #include <contrib/octeon-sdk/cvmx-cn3010-evb-hs5.h>
63 #include <contrib/octeon-sdk/cvmx-twsi.h>
64
65 #define CT_CHECK(_expr, _msg) \
66         do { \
67             if (_expr) { \
68                 cvmx_dprintf("Warning: RTC has invalid %s field\n", (_msg)); \
69                 rc = -1; \
70             } \
71         } while(0);
72
73 static int validate_ct_struct(struct clocktime *ct)
74 {
75     int rc = 0;
76
77     if (!ct)
78         return -1;
79
80     CT_CHECK(ct->sec < 0  || ct->sec > 60,  "second"); /* + Leap sec */
81     CT_CHECK(ct->min < 0  || ct->min > 59,  "minute");
82     CT_CHECK(ct->hour < 0 || ct->hour > 23, "hour");
83     CT_CHECK(ct->day < 1 || ct->day > 31, "day");
84     CT_CHECK(ct->dow < 1 || ct->dow > 7,  "day of week");
85     CT_CHECK(ct->mon < 1  || ct->mon > 12,  "month");
86     CT_CHECK(ct->year > 2037,"year");
87
88     return rc;
89 }
90
91 /*
92  * Board-specifc RTC read
93  * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
94  * and converted internally to calendar format.
95  */
96 uint32_t cvmx_rtc_ds1337_read(void)
97 {
98     int       i, retry;
99     uint8_t   reg[8];
100     uint8_t   sec;
101     struct clocktime ct;
102     struct timespec ts;
103
104
105     memset(&reg, 0, sizeof(reg));
106     memset(&ct, 0, sizeof(ct));
107
108     for(retry=0; retry<2; retry++)
109     {
110         /* Lockless read: detects the infrequent roll-over and retries */
111         reg[0] = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0);
112         for(i=1; i<7; i++)
113             reg[i] = cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1337_ADDR);
114
115         sec = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0);
116         if ((sec & 0xf) == (reg[0] & 0xf))
117             break; /* Time did not roll-over, value is correct */
118     }
119
120     ct.sec  = bcd2bin(reg[0] & 0x7f);
121     ct.min  = bcd2bin(reg[1] & 0x7f);
122     ct.hour = bcd2bin(reg[2] & 0x3f);
123     if ((reg[2] & 0x40) && (reg[2] & 0x20))   /* AM/PM format and is PM time */
124     {
125         ct.hour = (ct.hour + 12) % 24;
126     }
127     ct.dow = (reg[3] & 0x7);         /* Day of week field is 1..7 */
128     ct.day = bcd2bin(reg[4] & 0x3f);
129     ct.mon  = bcd2bin(reg[5] & 0x1f); /* Month field is 1..12 */
130 #if defined(OCTEON_BOARD_CAPK_0100ND)
131     /*
132      * CAPK-0100ND uses DS1307 that does not have century bit
133      */
134     ct.year = 2000 + bcd2bin(reg[6]);
135 #else
136     ct.year = ((reg[5] & 0x80) ? 2000 : 1900) + bcd2bin(reg[6]);
137 #endif
138
139
140     if (validate_ct_struct(&ct))
141         cvmx_dprintf("Warning: RTC calendar is not configured properly\n");
142
143     if (clock_ct_to_ts(&ct, &ts) != 0) {
144         cvmx_dprintf("Warning: RTC calendar is not configured properly\n");
145         return 0;
146     }
147
148     return ts.tv_sec;
149 }
150
151 /*
152  * Board-specific RTC write
153  * Time returned is in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
154  */
155 int cvmx_rtc_ds1337_write(uint32_t time)
156 {
157     struct clocktime ct;
158     struct timespec ts;
159     int       i, rc, retry;
160     uint8_t   reg[8];
161     uint8_t   sec;
162
163     ts.tv_sec = time;
164     ts.tv_nsec = 0;
165
166     clock_ts_to_ct(&ts, &ct);
167
168     if (validate_ct_struct(&ct))
169     {
170         cvmx_dprintf("Error: RTC was passed wrong calendar values, write failed\n");
171         goto ct_invalid;
172     }
173
174     reg[0] = bin2bcd(ct.sec);
175     reg[1] = bin2bcd(ct.min);
176     reg[2] = bin2bcd(ct.hour);       /* Force 0..23 format even if using AM/PM */
177     reg[3] = bin2bcd(ct.dow);
178     reg[4] = bin2bcd(ct.day);
179     reg[5] = bin2bcd(ct.mon);
180     if (ct.year >= 2000)             /* Set century bit*/
181     {
182         reg[5] |= 0x80;
183     }
184     reg[6] = bin2bcd(ct.year % 100);
185
186     /* Lockless write: detects the infrequent roll-over and retries */
187     for(retry=0; retry<2; retry++)
188     {
189         rc = 0;
190         for(i=0; i<7; i++)
191         {
192             rc |= cvmx_twsi_write8(CVMX_RTC_DS1337_ADDR, i, reg[i]);
193         }
194
195         sec = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0);
196         if ((sec & 0xf) == (reg[0] & 0xf))
197             break; /* Time did not roll-over, value is correct */
198     }
199
200     return (rc ? -1 : 0);
201
202  ct_invalid:
203     return -1;
204 }
205
206 #ifdef CVMX_RTC_DEBUG
207
208 void cvmx_rtc_ds1337_dump_state(void)
209 {
210     int i = 0;
211
212     printf("RTC:\n");
213     printf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0));
214     for(i=1; i<16; i++) {
215         printf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1337_ADDR));
216     }
217     printf("\n");
218 }
219
220 #endif /* CVMX_RTC_DEBUG */