1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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
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**************************************/
49 * Interface to the Thunder specific devices
51 * <hr>$Revision: 70030 $<hr>
56 #include "cvmx-sysinfo.h"
57 #include "cvmx-thunder.h"
58 #include "cvmx-gpio.h"
59 #include "cvmx-twsi.h"
62 static const int BYPASS_STATUS = 1<<5; /* GPIO 5 */
63 static const int BYPASS_EN = 1<<6; /* GPIO 6 */
64 static const int WDT_BP_CLR = 1<<7; /* GPIO 7 */
66 static const int RTC_CTL_ADDR = 0x7;
67 static const int RTC_CTL_BIT_EOSC = 0x80;
68 static const int RTC_CTL_BIT_WACE = 0x40;
69 static const int RTC_CTL_BIT_WD_ALM = 0x20;
70 static const int RTC_CTL_BIT_WDSTR = 0x8;
71 static const int RTC_CTL_BIT_AIE = 0x1;
72 static const int RTC_WD_ALM_CNT_BYTE0_ADDR = 0x4;
74 #define CVMX_LAN_BYPASS_MSG(...) do {} while(0)
77 * Board-specifc RTC read
78 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
80 uint32_t cvmx_rtc_ds1374_read(void)
86 for(retry=0; retry<2; retry++)
88 time = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
89 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 8;
90 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 16;
91 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 24;
93 sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
94 if (sec == (time & 0xff))
95 break; /* Time did not roll-over, value is correct */
102 * Board-specific RTC write
103 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
105 int cvmx_rtc_ds1374_write(uint32_t time)
111 for(retry=0; retry<2; retry++)
113 rc = cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x0, time & 0xff);
114 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x1, (time >> 8) & 0xff);
115 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x2, (time >> 16) & 0xff);
116 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x3, (time >> 24) & 0xff);
117 sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
118 if (sec == (time & 0xff))
119 break; /* Time did not roll-over, value is correct */
122 return (rc ? -1 : 0);
125 static int cvmx_rtc_ds1374_alarm_config(int WD, int WDSTR, int AIE)
129 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
130 val = val & ~RTC_CTL_BIT_EOSC; /* Make sure that oscillator is running */
131 WD?(val = val | RTC_CTL_BIT_WD_ALM):(val = val & ~RTC_CTL_BIT_WD_ALM);
132 WDSTR?(val = val | RTC_CTL_BIT_WDSTR):(val = val & ~RTC_CTL_BIT_WDSTR);
133 AIE?(val = val | RTC_CTL_BIT_AIE):(val = val & ~RTC_CTL_BIT_AIE);
134 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val);
138 static int cvmx_rtc_ds1374_alarm_set(int alarm_on)
144 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
145 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val | RTC_CTL_BIT_WACE);
149 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
150 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val & ~RTC_CTL_BIT_WACE);
156 static int cvmx_rtc_ds1374_alarm_counter_set(uint32_t interval)
163 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, RTC_WD_ALM_CNT_BYTE0_ADDR+i, interval & 0xFF);
169 #if 0 /* XXX unused */
170 static uint32_t cvmx_rtc_ds1374_alarm_counter_get(void)
173 uint32_t interval = 0;
177 interval |= ( cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_WD_ALM_CNT_BYTE0_ADDR+i) & 0xff) << (i*8);
184 #ifdef CVMX_RTC_DEBUG
186 void cvmx_rtc_ds1374_dump_state(void)
190 cvmx_dprintf("RTC:\n");
191 cvmx_dprintf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0));
194 cvmx_dprintf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR));
199 #endif /* CVMX_RTC_DEBUG */
203 * LAN bypass functionality
206 /* Private initialization function */
207 static int cvmx_lan_bypass_init(void)
209 const int CLR_PULSE = 100; /* Longer than 100 ns (on CPUs up to 1 GHz) */
212 cvmx_gpio_clear(BYPASS_EN);
215 cvmx_rtc_ds1374_alarm_set(0);
217 //GPIO(7) Send a low pulse
218 cvmx_gpio_clear(WDT_BP_CLR);
219 cvmx_wait(CLR_PULSE);
220 cvmx_gpio_set(WDT_BP_CLR);
225 * Set LAN bypass mode.
227 * Supported modes are:
228 * - CVMX_LAN_BYPASS_OFF
229 * <br>LAN ports are connected ( port 0 <--> Octeon <--> port 1 )
231 * - CVMX_LAN_BYPASS_GPIO
232 * <br>LAN bypass is controlled by software using cvmx_lan_bypass_force() function.
233 * When transitioning to this mode, default is LAN bypass enabled
234 * ( port 0 <--> port 1, -- Octeon ).
236 * - CVMX_LAN_BYPASS_WATCHDOG
237 * <br>LAN bypass is inactive as long as a watchdog is kept alive.
238 * The default expiration time is 1 second and the function to
239 * call periodically to prevent watchdog expiration is
240 * cvmx_lan_bypass_keep_alive().
242 * @param mode LAN bypass mode
244 * @return Error code, or 0 in case of success
246 int cvmx_lan_bypass_mode_set(cvmx_lan_bypass_mode_t mode)
250 case CVMX_LAN_BYPASS_GPIO:
251 /* make lan bypass enable */
252 cvmx_lan_bypass_init();
253 cvmx_gpio_set(BYPASS_EN);
254 CVMX_LAN_BYPASS_MSG("Enable LAN bypass by GPIO. \n");
257 case CVMX_LAN_BYPASS_WATCHDOG:
258 /* make lan bypass enable */
259 cvmx_lan_bypass_init();
260 /* Set WDT parameters and turn it on */
261 cvmx_rtc_ds1374_alarm_counter_set(0x1000); /* 4096 ticks = 1 sec */
262 cvmx_rtc_ds1374_alarm_config(1,1,1);
263 cvmx_rtc_ds1374_alarm_set(1);
264 CVMX_LAN_BYPASS_MSG("Enable LAN bypass by WDT. \n");
267 case CVMX_LAN_BYPASS_OFF:
268 /* make lan bypass disable */
269 cvmx_lan_bypass_init();
270 CVMX_LAN_BYPASS_MSG("Disable LAN bypass. \n");
274 CVMX_LAN_BYPASS_MSG("%s: LAN bypass mode %d not supported\n", __FUNCTION__, mode);
281 * Refresh watchdog timer.
283 * Call periodically (less than 1 second) to prevent triggering LAN bypass.
284 * The alternative cvmx_lan_bypass_keep_alive_ms() is provided for cases
285 * where a variable interval is required.
287 void cvmx_lan_bypass_keep_alive(void)
289 cvmx_rtc_ds1374_alarm_counter_set(0x1000); /* 4096 ticks = 1 second */
293 * Refresh watchdog timer, setting a specific expiration interval.
295 * @param interval_ms Interval, in milliseconds, to next watchdog expiration.
297 void cvmx_lan_bypass_keep_alive_ms(uint32_t interval_ms)
299 cvmx_rtc_ds1374_alarm_counter_set((interval_ms * 0x1000) / 1000);
303 * Control LAN bypass via software.
305 * @param force_bypass Force LAN bypass to active (1) or inactive (0)
307 * @return Error code, or 0 in case of success
309 int cvmx_lan_bypass_force(int force_bypass)
314 cvmx_gpio_set(BYPASS_EN);
318 cvmx_lan_bypass_init();
324 * Return status of LAN bypass circuit.
326 * @return 1 if ports are in LAN bypass, or 0 if normally connected
328 int cvmx_lan_bypass_is_active(void)
330 return !!(cvmx_gpio_read() & BYPASS_STATUS);