]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/contrib/octeon-sdk/cvmx-thunder.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / contrib / octeon-sdk / cvmx-thunder.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 Thunder specific devices
48  *
49  * <hr>$Revision: 41586 $<hr>
50  *
51  */
52
53 #include "cvmx.h"
54 #include "cvmx-sysinfo.h"
55 #include "cvmx-thunder.h"
56 #include "cvmx-gpio.h"
57 #include "cvmx-twsi.h"
58
59
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 */
63
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;
71
72 #define CVMX_LAN_BYPASS_MSG(...)  do {} while(0)
73
74 /*
75  * Board-specifc RTC read
76  * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
77  */
78 uint32_t cvmx_rtc_ds1374_read(void)
79 {
80     int      retry;
81     uint8_t  sec;
82     uint32_t time;
83
84     for(retry=0; retry<2; retry++)
85     {
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;
90
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 */
94     }
95
96     return time;
97 }
98
99 /*
100  * Board-specific RTC write
101  * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
102  */
103 int cvmx_rtc_ds1374_write(uint32_t time)
104 {
105     int      rc;
106     int      retry;
107     uint8_t  sec;
108
109     for(retry=0; retry<2; retry++)
110     {
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 */
118     }
119
120     return (rc ? -1 : 0);
121 }
122
123 static int cvmx_rtc_ds1374_alarm_config(int WD, int WDSTR, int AIE)
124 {
125     int val;
126
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);
133     return 0;
134 }
135
136 static int cvmx_rtc_ds1374_alarm_set(int alarm_on)
137 {
138     uint8_t val;
139
140     if (alarm_on)
141     {
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);
144     }
145     else
146     {
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);
149     }
150     return 0;
151 }
152
153
154 static int cvmx_rtc_ds1374_alarm_counter_set(uint32_t interval)
155 {
156     int i;
157     int rc = 0;
158
159     for(i=0;i<3;i++)
160     {
161         rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, RTC_WD_ALM_CNT_BYTE0_ADDR+i, interval & 0xFF);
162         interval >>= 8;
163     }
164     return rc;
165 }
166
167 #if 0 /* XXX unused */
168 static uint32_t cvmx_rtc_ds1374_alarm_counter_get(void)
169 {
170     int i;
171     uint32_t interval = 0;
172
173     for(i=0;i<3;i++)
174     {
175         interval |= ( cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_WD_ALM_CNT_BYTE0_ADDR+i) & 0xff) << (i*8);
176     }
177     return interval;
178 }
179 #endif
180
181
182 #ifdef CVMX_RTC_DEBUG
183
184 void cvmx_rtc_ds1374_dump_state(void)
185 {
186     int i = 0;
187
188     cvmx_dprintf("RTC:\n");
189     cvmx_dprintf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0));
190     for(i=1; i<10; i++)
191     {
192         cvmx_dprintf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR));
193     }
194     cvmx_dprintf("\n");
195 }
196
197 #endif /* CVMX_RTC_DEBUG */
198
199
200 /*
201  *  LAN bypass functionality
202  */
203
204 /* Private initialization function */
205 static int cvmx_lan_bypass_init(void)
206 {
207     const int CLR_PULSE = 100;  /* Longer than 100 ns (on CPUs up to 1 GHz) */
208
209     //Clear GPIO 6
210     cvmx_gpio_clear(BYPASS_EN);
211
212     //Disable WDT
213     cvmx_rtc_ds1374_alarm_set(0);
214
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);
219     return 0;
220 }
221
222 /**
223  * Set LAN bypass mode.
224  *
225  * Supported modes are:
226  * - CVMX_LAN_BYPASS_OFF
227  *     <br>LAN ports are connected ( port 0 <--> Octeon <--> port 1 )
228  *
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 ).
233  *
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().
239  *
240  * @param mode           LAN bypass mode
241  *
242  * @return Error code, or 0 in case of success
243  */
244 int cvmx_lan_bypass_mode_set(cvmx_lan_bypass_mode_t mode)
245 {
246     switch(mode)
247     {
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");
253         break;
254
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");
263         break;
264
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");
269         break;
270
271     default:
272         CVMX_LAN_BYPASS_MSG("%s: LAN bypass mode %d not supported\n", __FUNCTION__, mode);
273         break;
274     }
275     return 0;
276 }
277
278 /**
279  * Refresh watchdog timer.
280  *
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.
284  */
285 void cvmx_lan_bypass_keep_alive(void)
286 {
287     cvmx_rtc_ds1374_alarm_counter_set(0x1000);    /* 4096 ticks = 1 second */
288 }
289
290 /**
291  * Refresh watchdog timer, setting a specific expiration interval.
292  *
293  * @param interval_ms     Interval, in milliseconds, to next watchdog expiration.
294  */
295 void cvmx_lan_bypass_keep_alive_ms(uint32_t interval_ms)
296 {
297     cvmx_rtc_ds1374_alarm_counter_set((interval_ms * 0x1000) / 1000);
298 }
299
300 /**
301  * Control LAN bypass via software.
302  *
303  * @param force_bypass   Force LAN bypass to active (1) or inactive (0)
304  *
305  * @return Error code, or 0 in case of success
306  */
307 int cvmx_lan_bypass_force(int force_bypass)
308 {
309     if (force_bypass)
310     {
311         //Set GPIO 6
312         cvmx_gpio_set(BYPASS_EN);
313     }
314     else
315     {
316         cvmx_lan_bypass_init();
317     }
318     return 0;
319 }
320
321 /**
322  * Return status of LAN bypass circuit.
323  *
324  * @return 1 if ports are in LAN bypass, or 0 if normally connected
325  */
326 int cvmx_lan_bypass_is_active(void)
327 {
328     return !!(cvmx_gpio_read() & BYPASS_STATUS);
329 }