1/***********************license start*************** 2 * Copyright (c) 2003-2010 Cavium Inc. (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 Inc. 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 * This Software, including technical data, may be subject to U.S. export control 24 * laws, including the U.S. Export Administration Act and its associated 25 * regulations, and may be subject to export or import regulations in other 26 * countries. 27 28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38 ***********************license end**************************************/ 39 40 41 42 43 44 45 46/** 47 * @file 48 * 49 * Interface to the EBH-30xx specific devices 50 * 51 * <hr>$Revision: 70030 $<hr> 52 * 53 */ 54 55#include <time.h> 56#include "cvmx-config.h" 57#include "cvmx.h" 58#include "cvmx-sysinfo.h" 59#include "cvmx-cn3010-evb-hs5.h" 60#include "cvmx-twsi.h" 61 62 63static inline uint8_t bin2bcd(uint8_t bin) 64{ 65 return (bin / 10) << 4 | (bin % 10); 66} 67 68static inline uint8_t bcd2bin(uint8_t bcd) 69{ 70 return (bcd >> 4) * 10 + (bcd & 0xf); 71} 72 73#define TM_CHECK(_expr, _msg) \ 74 do { \ 75 if (_expr) { \ 76 cvmx_dprintf("Warning: RTC has invalid %s field\n", (_msg)); \ 77 rc = -1; \ 78 } \ 79 } while(0); 80 81static int validate_tm_struct(struct tm * tms) 82{ 83 int rc = 0; 84 85 if (!tms) 86 return -1; 87 88 TM_CHECK(tms->tm_sec < 0 || tms->tm_sec > 60, "second"); /* + Leap sec */ 89 TM_CHECK(tms->tm_min < 0 || tms->tm_min > 59, "minute"); 90 TM_CHECK(tms->tm_hour < 0 || tms->tm_hour > 23, "hour"); 91 TM_CHECK(tms->tm_mday < 1 || tms->tm_mday > 31, "day"); 92 TM_CHECK(tms->tm_wday < 0 || tms->tm_wday > 6, "day of week"); 93 TM_CHECK(tms->tm_mon < 0 || tms->tm_mon > 11, "month"); 94 TM_CHECK(tms->tm_year < 0 || tms->tm_year > 200,"year"); 95 96 return rc; 97} 98 99/* 100 * Board-specifc RTC read 101 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC) 102 * and converted internally to calendar format. 103 */ 104uint32_t cvmx_rtc_ds1337_read(void) 105{ 106 int i, retry; 107 uint32_t time; 108 uint8_t reg[8]; 109 uint8_t sec; 110 struct tm tms; 111 112 113 memset(®, 0, sizeof(reg)); 114 memset(&tms, 0, sizeof(struct tm)); 115 116 for(retry=0; retry<2; retry++) 117 { 118 /* Lockless read: detects the infrequent roll-over and retries */ 119 reg[0] = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0); 120 for(i=1; i<7; i++) 121 reg[i] = cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1337_ADDR); 122 123 sec = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0); 124 if ((sec & 0xf) == (reg[0] & 0xf)) 125 break; /* Time did not roll-over, value is correct */ 126 } 127 128 tms.tm_sec = bcd2bin(reg[0] & 0x7f); 129 tms.tm_min = bcd2bin(reg[1] & 0x7f); 130 tms.tm_hour = bcd2bin(reg[2] & 0x3f); 131 if ((reg[2] & 0x40) && (reg[2] & 0x20)) /* AM/PM format and is PM time */ 132 { 133 tms.tm_hour = (tms.tm_hour + 12) % 24; 134 } 135 tms.tm_wday = (reg[3] & 0x7) - 1; /* Day of week field is 0..6 */ 136 tms.tm_mday = bcd2bin(reg[4] & 0x3f); 137 tms.tm_mon = bcd2bin(reg[5] & 0x1f) - 1; /* Month field is 0..11 */ 138 tms.tm_year = ((reg[5] & 0x80) ? 100 : 0) + bcd2bin(reg[6]); 139 140 141 if (validate_tm_struct(&tms)) 142 cvmx_dprintf("Warning: RTC calendar is not configured properly\n"); 143 144 time = mktime(&tms); 145 146 return time; 147} 148 149/* 150 * Board-specific RTC write 151 * Time returned is in seconds from epoch (Jan 1 1970 at 00:00:00 UTC) 152 */ 153int cvmx_rtc_ds1337_write(uint32_t time) 154{ 155 int i, rc, retry; 156 struct tm tms; 157 uint8_t reg[8]; 158 uint8_t sec; 159 time_t time_from_epoch = time; 160 161 162 localtime_r(&time_from_epoch, &tms); 163 164 if (validate_tm_struct(&tms)) 165 { 166 cvmx_dprintf("Error: RTC was passed wrong calendar values, write failed\n"); 167 goto tm_invalid; 168 } 169 170 reg[0] = bin2bcd(tms.tm_sec); 171 reg[1] = bin2bcd(tms.tm_min); 172 reg[2] = bin2bcd(tms.tm_hour); /* Force 0..23 format even if using AM/PM */ 173 reg[3] = bin2bcd(tms.tm_wday + 1); 174 reg[4] = bin2bcd(tms.tm_mday); 175 reg[5] = bin2bcd(tms.tm_mon + 1); 176 if (tms.tm_year >= 100) /* Set century bit*/ 177 { 178 reg[5] |= 0x80; 179 } 180 reg[6] = bin2bcd(tms.tm_year % 100); 181 182 /* Lockless write: detects the infrequent roll-over and retries */ 183 for(retry=0; retry<2; retry++) 184 { 185 rc = 0; 186 for(i=0; i<7; i++) 187 { 188 rc |= cvmx_twsi_write8(CVMX_RTC_DS1337_ADDR, i, reg[i]); 189 } 190 191 sec = cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0); 192 if ((sec & 0xf) == (reg[0] & 0xf)) 193 break; /* Time did not roll-over, value is correct */ 194 } 195 196 return (rc ? -1 : 0); 197 198 tm_invalid: 199 return -1; 200} 201 202#ifdef CVMX_RTC_DEBUG 203 204void cvmx_rtc_ds1337_dump_state(void) 205{ 206 int i = 0; 207 208 printf("RTC:\n"); 209 printf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1337_ADDR, 0x0)); 210 for(i=1; i<16; i++) { 211 printf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1337_ADDR)); 212 } 213 printf("\n"); 214} 215 216#endif /* CVMX_RTC_DEBUG */ 217