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