1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (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 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
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.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
47 * Interface to the Thunder specific devices
49 * <hr>$Revision: 41586 $<hr>
54 #include "cvmx-sysinfo.h"
55 #include "cvmx-thunder.h"
56 #include "cvmx-gpio.h"
57 #include "cvmx-twsi.h"
60 static const int BYPASS_STATUS = 1<<5; /* GPIO 5 */
61 static const int BYPASS_EN = 1<<6; /* GPIO 6 */
62 static const int WDT_BP_CLR = 1<<7; /* GPIO 7 */
64 static const int RTC_CTL_ADDR = 0x7;
65 static const int RTC_CTL_BIT_EOSC = 0x80;
66 static const int RTC_CTL_BIT_WACE = 0x40;
67 static const int RTC_CTL_BIT_WD_ALM = 0x20;
68 static const int RTC_CTL_BIT_WDSTR = 0x8;
69 static const int RTC_CTL_BIT_AIE = 0x1;
70 static const int RTC_WD_ALM_CNT_BYTE0_ADDR = 0x4;
72 #define CVMX_LAN_BYPASS_MSG(...) do {} while(0)
75 * Board-specifc RTC read
76 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
78 uint32_t cvmx_rtc_ds1374_read(void)
84 for(retry=0; retry<2; retry++)
86 time = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
87 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 8;
88 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 16;
89 time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 24;
91 sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
92 if (sec == (time & 0xff))
93 break; /* Time did not roll-over, value is correct */
100 * Board-specific RTC write
101 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
103 int cvmx_rtc_ds1374_write(uint32_t time)
109 for(retry=0; retry<2; retry++)
111 rc = cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x0, time & 0xff);
112 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x1, (time >> 8) & 0xff);
113 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x2, (time >> 16) & 0xff);
114 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x3, (time >> 24) & 0xff);
115 sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
116 if (sec == (time & 0xff))
117 break; /* Time did not roll-over, value is correct */
120 return (rc ? -1 : 0);
123 static int cvmx_rtc_ds1374_alarm_config(int WD, int WDSTR, int AIE)
127 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
128 val = val & ~RTC_CTL_BIT_EOSC; /* Make sure that oscillator is running */
129 WD?(val = val | RTC_CTL_BIT_WD_ALM):(val = val & ~RTC_CTL_BIT_WD_ALM);
130 WDSTR?(val = val | RTC_CTL_BIT_WDSTR):(val = val & ~RTC_CTL_BIT_WDSTR);
131 AIE?(val = val | RTC_CTL_BIT_AIE):(val = val & ~RTC_CTL_BIT_AIE);
132 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val);
136 static int cvmx_rtc_ds1374_alarm_set(int alarm_on)
142 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
143 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val | RTC_CTL_BIT_WACE);
147 val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
148 cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val & ~RTC_CTL_BIT_WACE);
154 static int cvmx_rtc_ds1374_alarm_counter_set(uint32_t interval)
161 rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, RTC_WD_ALM_CNT_BYTE0_ADDR+i, interval & 0xFF);
167 #if 0 /* XXX unused */
168 static uint32_t cvmx_rtc_ds1374_alarm_counter_get(void)
171 uint32_t interval = 0;
175 interval |= ( cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_WD_ALM_CNT_BYTE0_ADDR+i) & 0xff) << (i*8);
182 #ifdef CVMX_RTC_DEBUG
184 void cvmx_rtc_ds1374_dump_state(void)
188 cvmx_dprintf("RTC:\n");
189 cvmx_dprintf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0));
192 cvmx_dprintf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR));
197 #endif /* CVMX_RTC_DEBUG */
201 * LAN bypass functionality
204 /* Private initialization function */
205 static int cvmx_lan_bypass_init(void)
207 const int CLR_PULSE = 100; /* Longer than 100 ns (on CPUs up to 1 GHz) */
210 cvmx_gpio_clear(BYPASS_EN);
213 cvmx_rtc_ds1374_alarm_set(0);
215 //GPIO(7) Send a low pulse
216 cvmx_gpio_clear(WDT_BP_CLR);
217 cvmx_wait(CLR_PULSE);
218 cvmx_gpio_set(WDT_BP_CLR);
223 * Set LAN bypass mode.
225 * Supported modes are:
226 * - CVMX_LAN_BYPASS_OFF
227 * <br>LAN ports are connected ( port 0 <--> Octeon <--> port 1 )
229 * - CVMX_LAN_BYPASS_GPIO
230 * <br>LAN bypass is controlled by software using cvmx_lan_bypass_force() function.
231 * When transitioning to this mode, default is LAN bypass enabled
232 * ( port 0 <--> port 1, -- Octeon ).
234 * - CVMX_LAN_BYPASS_WATCHDOG
235 * <br>LAN bypass is inactive as long as a watchdog is kept alive.
236 * The default expiration time is 1 second and the function to
237 * call periodically to prevent watchdog expiration is
238 * cvmx_lan_bypass_keep_alive().
240 * @param mode LAN bypass mode
242 * @return Error code, or 0 in case of success
244 int cvmx_lan_bypass_mode_set(cvmx_lan_bypass_mode_t mode)
248 case CVMX_LAN_BYPASS_GPIO:
249 /* make lan bypass enable */
250 cvmx_lan_bypass_init();
251 cvmx_gpio_set(BYPASS_EN);
252 CVMX_LAN_BYPASS_MSG("Enable LAN bypass by GPIO. \n");
255 case CVMX_LAN_BYPASS_WATCHDOG:
256 /* make lan bypass enable */
257 cvmx_lan_bypass_init();
258 /* Set WDT parameters and turn it on */
259 cvmx_rtc_ds1374_alarm_counter_set(0x1000); /* 4096 ticks = 1 sec */
260 cvmx_rtc_ds1374_alarm_config(1,1,1);
261 cvmx_rtc_ds1374_alarm_set(1);
262 CVMX_LAN_BYPASS_MSG("Enable LAN bypass by WDT. \n");
265 case CVMX_LAN_BYPASS_OFF:
266 /* make lan bypass disable */
267 cvmx_lan_bypass_init();
268 CVMX_LAN_BYPASS_MSG("Disable LAN bypass. \n");
272 CVMX_LAN_BYPASS_MSG("%s: LAN bypass mode %d not supported\n", __FUNCTION__, mode);
279 * Refresh watchdog timer.
281 * Call periodically (less than 1 second) to prevent triggering LAN bypass.
282 * The alternative cvmx_lan_bypass_keep_alive_ms() is provided for cases
283 * where a variable interval is required.
285 void cvmx_lan_bypass_keep_alive(void)
287 cvmx_rtc_ds1374_alarm_counter_set(0x1000); /* 4096 ticks = 1 second */
291 * Refresh watchdog timer, setting a specific expiration interval.
293 * @param interval_ms Interval, in milliseconds, to next watchdog expiration.
295 void cvmx_lan_bypass_keep_alive_ms(uint32_t interval_ms)
297 cvmx_rtc_ds1374_alarm_counter_set((interval_ms * 0x1000) / 1000);
301 * Control LAN bypass via software.
303 * @param force_bypass Force LAN bypass to active (1) or inactive (0)
305 * @return Error code, or 0 in case of success
307 int cvmx_lan_bypass_force(int force_bypass)
312 cvmx_gpio_set(BYPASS_EN);
316 cvmx_lan_bypass_init();
322 * Return status of LAN bypass circuit.
324 * @return 1 if ports are in LAN bypass, or 0 if normally connected
326 int cvmx_lan_bypass_is_active(void)
328 return !!(cvmx_gpio_read() & BYPASS_STATUS);