summaryrefslogtreecommitdiffstats
path: root/sys/contrib/octeon-sdk/cvmx-thunder.c
blob: 31c7805df7f181643e014d936d0a078aeabd8cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***********************license start***************
 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
 * reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.

 *   * Neither the name of Cavium Inc. nor the names of
 *     its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written
 *     permission.

 * This Software, including technical data, may be subject to U.S. export  control
 * laws, including the U.S. Export Administration Act and its  associated
 * regulations, and may be subject to export or import  regulations in other
 * countries.

 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
 ***********************license end**************************************/







/**
 * @file
 *
 * Interface to the Thunder specific devices
 *
 * <hr>$Revision: 70030 $<hr>
 *
 */

#include "cvmx.h"
#include "cvmx-sysinfo.h"
#include "cvmx-thunder.h"
#include "cvmx-gpio.h"
#include "cvmx-twsi.h"


static const int BYPASS_STATUS = 1<<5; /* GPIO 5 */
static const int BYPASS_EN     = 1<<6; /* GPIO 6 */
static const int WDT_BP_CLR    = 1<<7; /* GPIO 7 */

static const int RTC_CTL_ADDR = 0x7;
static const int RTC_CTL_BIT_EOSC   = 0x80;
static const int RTC_CTL_BIT_WACE   = 0x40;
static const int RTC_CTL_BIT_WD_ALM = 0x20;
static const int RTC_CTL_BIT_WDSTR  = 0x8;
static const int RTC_CTL_BIT_AIE    = 0x1;
static const int RTC_WD_ALM_CNT_BYTE0_ADDR = 0x4;

#define CVMX_LAN_BYPASS_MSG(...)  do {} while(0)

/*
 * Board-specifc RTC read
 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
 */
uint32_t cvmx_rtc_ds1374_read(void)
{
    int      retry;
    uint8_t  sec;
    uint32_t time;

    for(retry=0; retry<2; retry++)
    {
        time = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
        time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 8;
        time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 16;
        time |= (cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR) & 0xff) << 24;

        sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
        if (sec == (time & 0xff))
            break; /* Time did not roll-over, value is correct */
    }

    return time;
}

/*
 * Board-specific RTC write
 * Time is expressed in seconds from epoch (Jan 1 1970 at 00:00:00 UTC)
 */
int cvmx_rtc_ds1374_write(uint32_t time)
{
    int      rc;
    int      retry;
    uint8_t  sec;

    for(retry=0; retry<2; retry++)
    {
        rc  = cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x0, time & 0xff);
        rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x1, (time >> 8) & 0xff);
        rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x2, (time >> 16) & 0xff);
        rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, 0x3, (time >> 24) & 0xff);
        sec = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0);
        if (sec == (time & 0xff))
            break; /* Time did not roll-over, value is correct */
    }

    return (rc ? -1 : 0);
}

static int cvmx_rtc_ds1374_alarm_config(int WD, int WDSTR, int AIE)
{
    int val;

    val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
    val = val & ~RTC_CTL_BIT_EOSC; /* Make sure that oscillator is running */
    WD?(val = val | RTC_CTL_BIT_WD_ALM):(val = val & ~RTC_CTL_BIT_WD_ALM);
    WDSTR?(val = val | RTC_CTL_BIT_WDSTR):(val = val & ~RTC_CTL_BIT_WDSTR);
    AIE?(val = val | RTC_CTL_BIT_AIE):(val = val & ~RTC_CTL_BIT_AIE);
    cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val);
    return 0;
}

static int cvmx_rtc_ds1374_alarm_set(int alarm_on)
{
    uint8_t val;

    if (alarm_on)
    {
        val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
        cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val | RTC_CTL_BIT_WACE);
    }
    else
    {
        val = cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR);
        cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR,RTC_CTL_ADDR, val & ~RTC_CTL_BIT_WACE);
    }
    return 0;
}


static int cvmx_rtc_ds1374_alarm_counter_set(uint32_t interval)
{
    int i;
    int rc = 0;

    for(i=0;i<3;i++)
    {
        rc |= cvmx_twsi_write8(CVMX_RTC_DS1374_ADDR, RTC_WD_ALM_CNT_BYTE0_ADDR+i, interval & 0xFF);
        interval >>= 8;
    }
    return rc;
}

#if 0 /* XXX unused */
static uint32_t cvmx_rtc_ds1374_alarm_counter_get(void)
{
    int i;
    uint32_t interval = 0;

    for(i=0;i<3;i++)
    {
        interval |= ( cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR,RTC_WD_ALM_CNT_BYTE0_ADDR+i) & 0xff) << (i*8);
    }
    return interval;
}
#endif


#ifdef CVMX_RTC_DEBUG

void cvmx_rtc_ds1374_dump_state(void)
{
    int i = 0;

    cvmx_dprintf("RTC:\n");
    cvmx_dprintf("%d : %02X ", i, cvmx_twsi_read8(CVMX_RTC_DS1374_ADDR, 0x0));
    for(i=1; i<10; i++)
    {
        cvmx_dprintf("%02X ", cvmx_twsi_read8_cur_addr(CVMX_RTC_DS1374_ADDR));
    }
    cvmx_dprintf("\n");
}

#endif /* CVMX_RTC_DEBUG */


/*
 *  LAN bypass functionality
 */

/* Private initialization function */
static int cvmx_lan_bypass_init(void)
{
    const int CLR_PULSE = 100;  /* Longer than 100 ns (on CPUs up to 1 GHz) */

    //Clear GPIO 6
    cvmx_gpio_clear(BYPASS_EN);

    //Disable WDT
    cvmx_rtc_ds1374_alarm_set(0);

    //GPIO(7) Send a low pulse
    cvmx_gpio_clear(WDT_BP_CLR);
    cvmx_wait(CLR_PULSE);
    cvmx_gpio_set(WDT_BP_CLR);
    return 0;
}

/**
 * Set LAN bypass mode.
 *
 * Supported modes are:
 * - CVMX_LAN_BYPASS_OFF
 *     <br>LAN ports are connected ( port 0 <--> Octeon <--> port 1 )
 *
 * - CVMX_LAN_BYPASS_GPIO
 *     <br>LAN bypass is controlled by software using cvmx_lan_bypass_force() function.
 *     When transitioning to this mode, default is LAN bypass enabled
 *     ( port 0 <--> port 1, -- Octeon ).
 *
 * - CVMX_LAN_BYPASS_WATCHDOG
 *     <br>LAN bypass is inactive as long as a watchdog is kept alive.
 *     The default expiration time is 1 second and the function to
 *     call periodically to prevent watchdog expiration is
 *     cvmx_lan_bypass_keep_alive().
 *
 * @param mode           LAN bypass mode
 *
 * @return Error code, or 0 in case of success
 */
int cvmx_lan_bypass_mode_set(cvmx_lan_bypass_mode_t mode)
{
    switch(mode)
    {
    case CVMX_LAN_BYPASS_GPIO:
        /* make lan bypass enable */
        cvmx_lan_bypass_init();
        cvmx_gpio_set(BYPASS_EN);
        CVMX_LAN_BYPASS_MSG("Enable LAN bypass by GPIO. \n");
        break;

    case CVMX_LAN_BYPASS_WATCHDOG:
        /* make lan bypass enable */
        cvmx_lan_bypass_init();
        /* Set WDT parameters and turn it on */
        cvmx_rtc_ds1374_alarm_counter_set(0x1000);    /* 4096 ticks = 1 sec */
        cvmx_rtc_ds1374_alarm_config(1,1,1);
        cvmx_rtc_ds1374_alarm_set(1);
        CVMX_LAN_BYPASS_MSG("Enable LAN bypass by WDT. \n");
        break;

    case CVMX_LAN_BYPASS_OFF:
        /* make lan bypass disable */
        cvmx_lan_bypass_init();
        CVMX_LAN_BYPASS_MSG("Disable LAN bypass. \n");
        break;

    default:
        CVMX_LAN_BYPASS_MSG("%s: LAN bypass mode %d not supported\n", __FUNCTION__, mode);
        break;
    }
    return 0;
}

/**
 * Refresh watchdog timer.
 *
 * Call periodically (less than 1 second) to prevent triggering LAN bypass.
 * The alternative cvmx_lan_bypass_keep_alive_ms() is provided for cases
 * where a variable interval is required.
 */
void cvmx_lan_bypass_keep_alive(void)
{
    cvmx_rtc_ds1374_alarm_counter_set(0x1000);    /* 4096 ticks = 1 second */
}

/**
 * Refresh watchdog timer, setting a specific expiration interval.
 *
 * @param interval_ms     Interval, in milliseconds, to next watchdog expiration.
 */
void cvmx_lan_bypass_keep_alive_ms(uint32_t interval_ms)
{
    cvmx_rtc_ds1374_alarm_counter_set((interval_ms * 0x1000) / 1000);
}

/**
 * Control LAN bypass via software.
 *
 * @param force_bypass   Force LAN bypass to active (1) or inactive (0)
 *
 * @return Error code, or 0 in case of success
 */
int cvmx_lan_bypass_force(int force_bypass)
{
    if (force_bypass)
    {
        //Set GPIO 6
        cvmx_gpio_set(BYPASS_EN);
    }
    else
    {
        cvmx_lan_bypass_init();
    }
    return 0;
}

/**
 * Return status of LAN bypass circuit.
 *
 * @return 1 if ports are in LAN bypass, or 0 if normally connected
 */
int cvmx_lan_bypass_is_active(void)
{
    return !!(cvmx_gpio_read() & BYPASS_STATUS);
}
OpenPOWER on IntegriCloud