http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / broadcom / cfm / util / psi / board_api.c
1 /***************************************************************************
2 # <:copyright-gpl
3 # Copyright 2004 Broadcom Corp. All Rights Reserved.
4 # This program is free software; you can distribute it and/or modify it
5 # under the terms of the GNU General Public License (Version 2) as
6 # published by the Free Software Foundation.
7 # This program is distributed in the hope it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 # for more details.
11 # You should have received a copy of the GNU General Public License along
12 # with this program; if not, write to the Free Software Foundation, Inc.,
13 # 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
14 # :>
15  ***************************************************************************
16  * File Name  : board_api.c
17  *
18  * Description: interface for board level calls: flash,
19  *              led, soft reset, free memory page, and memory dump.
20  *              Adapted form flash_api.c by Yen Tran.
21  *
22  * Created on : 02/20/2002  seanl
23  *
24  ***************************************************************************/
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <fcntl.h>      /* open */
30 #include <unistd.h>     /* exit */
31 #include <sys/ioctl.h>  /* ioctl */
32 #include <memory.h>
33
34 #include <bcmtypes.h>
35 #include <board_api.h>
36
37 int boardIoctl(int board_ioctl, BOARD_IOCTL_ACTION action, char *string, int strLen, int offset, char *buf)
38 {
39     BOARD_IOCTL_PARMS IoctlParms;
40     int boardFd = 0;
41
42     boardFd = open("/dev/brcmboard", O_RDWR);
43     if ( boardFd != -1 ) {
44         IoctlParms.string = string;
45         IoctlParms.strLen = strLen;
46         IoctlParms.offset = offset;
47         IoctlParms.action = action;
48         IoctlParms.buf    = buf;
49         ioctl(boardFd, board_ioctl, &IoctlParms);
50         close(boardFd);
51         boardFd = IoctlParms.result;
52     } else
53         printf("Unable to open device /dev/brcmboard.\n");
54
55     return boardFd;
56 }
57 #ifdef USE_ALL
58
59
60 /*****************************************************************************
61 * sysScratchPadGet - get the contents of persistent stratch pad flash memory
62 * INPUT:   tokenId -- token id, ASCIZ tokBuf (up to 15 char)
63 *          tokBuf  -- buf 
64 *          bufLen  -- buf len
65 * RETURNS: 0 - ok, -1 fail.
66 */
67 int sysScratchPadGet(char *tokenId, char *tokBuf, int bufLen)
68 {
69     return (boardIoctl(BOARD_IOCTL_FLASH_READ, SCRATCH_PAD, tokenId, bufLen, bufLen, tokBuf));
70 }
71
72 /*****************************************************************************
73 * sysScratchPadSet - write the contents of persistent scratch pad flash memory
74 * INPUT:   tokenId -- token id, ASCIZ tokBuf (up to 15 char)
75 *          tokBuf  -- buf 
76 *          bufLen  -- buf len
77 * RETURNS: 0 - ok, -1 fail.
78 */
79 int sysScratchPadSet(char *tokenId, char *tokBuf, int bufLen)
80 {
81     char *buf = malloc(bufLen);
82
83     //If the data associated with the tokenId already exists in the scratch pad,
84     //we don't want to set it again.
85         //Note that writing to the scratch pad is a non-preemptive time consuming
86         //operation that should be avoided.
87     if (buf != NULL &&
88         boardIoctl(BOARD_IOCTL_FLASH_READ, SCRATCH_PAD, tokenId, bufLen, bufLen, buf) >= 0) {
89         if (bcmp(buf, tokBuf, bufLen) == 0) {
90             free(buf);
91             return 0;
92         }
93     }
94     if (buf != NULL) free(buf);
95
96     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, SCRATCH_PAD, tokenId, bufLen, bufLen, tokBuf));
97 }
98
99 /*****************************************************************************
100 * sysScratchPadClearAll  - wipeout the scratch pad
101 * RETURNS: 0 - ok, -1 fail.
102 */
103 int sysScratchPadClearAll(void)
104 {
105     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, SCRATCH_PAD, "", -1, -1, ""));
106 }
107
108 /*****************************************************************************
109 * sysPersistentGet - get the contents of non-volatile RAM,
110 * RETURNS: OK, always.
111 */
112 int sysNvRamGet(char *string, int strLen, int offset)
113 {
114     return (boardIoctl(BOARD_IOCTL_FLASH_READ, NVRAM, string, strLen, offset, ""));
115 }
116
117
118
119 /*****************************************************************************
120 * sysPersistentSet - write the contents of non-volatile RAM
121 * RETURNS: OK, always.
122 */
123 int sysNvRamSet(char *string, int strLen, int offset)
124 {
125     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, NVRAM, string, strLen, offset, ""));
126 }
127
128 /*****************************************************************************
129 * sysNrPagesGet - returns number of free system pages.  Each page is 4K bytes.
130 * RETURNS: Number of 4K pages.
131 */
132 int sysNrPagesGet(void)
133 {
134     return (boardIoctl(BOARD_IOCTL_GET_NR_PAGES, 0, "", 0, 0, ""));
135 }
136
137 /*****************************************************************************
138 * sysDumpAddr - Dump kernel memory.
139 * RETURNS: OK, always.
140 */
141 int sysDumpAddr(char *addr, int len)
142 {
143     return (boardIoctl(BOARD_IOCTL_DUMP_ADDR, 0, addr, len, 0, ""));
144 }
145
146 /*****************************************************************************
147 * sysDumpAddr - Set kernel memory.
148 * RETURNS: OK, always.
149 */
150 int sysSetMemory(char *addr, int size, unsigned long value )
151 {
152     return (boardIoctl(BOARD_IOCTL_SET_MEMORY, 0, addr, size, (int) value, ""));
153 }
154
155 /*****************************************************************************
156  * image points to image to be programmed to flash; size is the size (in bytes)
157  * of the image.
158  * if error, return -1; otherwise return 0
159  */
160
161 #endif // USE_ALL
162
163 /*****************************************************************************
164 * sysPersistentGet - get the contents of persistent flash memory
165 * RETURNS: OK, always.
166 */
167 int sysPersistentGet(char *string, int strLen, int offset)
168 {
169     return (boardIoctl(BOARD_IOCTL_FLASH_READ, PERSISTENT, string, strLen, offset, ""));
170 }
171
172 /*****************************************************************************
173 * sysPersistenSet - write the contents of persistent Scrach Pad flash memory
174 * RETURNS: OK, always.
175 */
176 int sysPersistentSet(char *string, int strLen, int offset)
177 {
178     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, PERSISTENT, string, strLen, offset, ""));
179 }
180
181 //********************************************************************************
182 // Get PSI size
183 //********************************************************************************
184 int sysGetPsiSize( void )
185 {
186     return( boardIoctl(BOARD_IOCTL_GET_PSI_SIZE, 0, "", 0, 0, "") );
187 }
188
189 int sysFlashImageSet(void *image, int size, int addr,
190     BOARD_IOCTL_ACTION imageType)
191 {
192     int result;
193
194     result = boardIoctl(BOARD_IOCTL_FLASH_WRITE, imageType, image, size, addr, "");
195
196     return(result);
197 }
198
199 /*****************************************************************************
200  * Get flash size 
201  * return int flash size
202  */
203 int sysFlashSizeGet(void)
204 {
205     return (boardIoctl(BOARD_IOCTL_FLASH_READ, FLASH_SIZE, "", 0, 0, ""));
206 }
207
208 /*****************************************************************************
209 * kerSysMipsSoftReset - soft reset the mips. (reboot, go to 0xbfc00000)
210 * RETURNS: NEVER
211 */
212 void sysMipsSoftReset(void)
213 {  
214     boardIoctl(BOARD_IOCTL_MIPS_SOFT_RESET, 0, "", 0, 0, "");
215 }
216
217 //********************************************************************************
218 // Get Chip Id
219 //********************************************************************************
220 int sysGetChipId( void )
221 {
222 #if BCM6348A
223 return (0x6348A);
224 #elif BCM6348B
225 return (0x6348B);
226 #elif BCM6338A
227 return (0x6338A);
228 #elif BCM6338B
229 return (0x6338B);
230 #elif BCM6345A
231 return (0x6345A);
232 #elif BCM6345A2
233 return (0x6345A2);
234 #elif BCM6345B
235 return (0x6345B);
236 #else
237 return (0x6348);
238 #endif
239 //ODM Michael    return( boardIoctl(BOARD_IOCTL_GET_CHIP_ID, 0, "", 0, 0, "") );
240 }
241
242 //********************************************************************************
243 // Wakeup monitor task
244 //********************************************************************************
245 void sysWakeupMonitorTask(void)
246 {
247     boardIoctl(BOARD_IOCTL_WAKEUP_MONITOR_TASK, 0, "", 0, 0, "");
248 }
249
250 #ifdef USE_ALL
251 //********************************************************************************
252 // LED status display:  ADSL link: DOWN/UP, PPP: DOWN/STARTING/UP
253 //********************************************************************************
254 void sysLedCtrl(BOARD_LED_NAME ledName, BOARD_LED_STATE ledState)
255 {
256     boardIoctl(BOARD_IOCTL_LED_CTRL, 0, "", (int)ledName, (int)ledState, "");
257 }
258
259
260 //********************************************************************************
261 // Get board id
262 //********************************************************************************
263 int sysGetBoardIdName(char *name, int length)
264 {
265     return( boardIoctl(BOARD_IOCTL_GET_ID, 0, name, length, 0, "") );
266 }
267
268
269 //********************************************************************************
270 // Get MAC Address
271 //********************************************************************************
272 int sysGetMacAddress( unsigned char *pucaAddr, unsigned long ulId )
273 {
274     return(boardIoctl(BOARD_IOCTL_GET_MAC_ADDRESS, 0, pucaAddr, 6, (int) ulId, ""));
275 }
276
277
278 //********************************************************************************
279 // Release MAC Address
280 //********************************************************************************
281 int sysReleaseMacAddress( unsigned char *pucaAddr )
282 {
283     return( boardIoctl(BOARD_IOCTL_RELEASE_MAC_ADDRESS, 0, pucaAddr, 6, 0, "") );
284 }
285
286
287 //********************************************************************************
288 // Get SDRAM size
289 //********************************************************************************
290 int sysGetSdramSize( void )
291 {
292     return( boardIoctl(BOARD_IOCTL_GET_SDRAM_SIZE, 0, "", 0, 0, "") );
293 }
294
295
296 /*****************************************************************************
297 * sysGetBooline - get bootline
298 * RETURNS: OK, always.
299 */
300 int sysGetBootline(char *string, int strLen)
301 {
302     return (boardIoctl(BOARD_IOCTL_GET_BOOTLINE, 0, string, strLen, 0, ""));
303 }
304
305 /*****************************************************************************
306 * sysSetBootline - write the bootline to nvram
307 * RETURNS: OK, always.
308 */
309 int sysSetBootline(char *string, int strLen)
310 {
311     return (boardIoctl(BOARD_IOCTL_SET_BOOTLINE, 0, string, strLen, 0, ""));
312 }
313
314 //********************************************************************************
315 // Get MAC Address
316 //********************************************************************************
317 int sysGetBaseMacAddress( unsigned char *pucaAddr )
318 {
319     return(boardIoctl(BOARD_IOCTL_GET_BASE_MAC_ADDRESS, 0, pucaAddr, 6, 0, ""));
320 }
321
322 //********************************************************************************
323 // Get number of Ethernet phys
324 //********************************************************************************
325 int sysGetNumEnet(void)
326 {
327     return(boardIoctl(BOARD_IOCTL_GET_NUM_ENET, 0, "", 0, 0, ""));
328 }
329
330 //********************************************************************************
331 // Get CFE vesion info
332 //********************************************************************************
333 int sysGetCFEVersion(char *string, int strLen)
334 {
335     return (boardIoctl(BOARD_IOCTL_GET_CFE_VER, 0, string, strLen, 0, ""));
336 }
337
338 //********************************************************************************
339 // Get board Ethernet configuration
340 //********************************************************************************
341 int sysGetEnetCfg(char *string, int strLen)
342 {
343     return(boardIoctl(BOARD_IOCTL_GET_ENET_CFG, 0, string, strLen, 0, ""));
344 }
345
346 //********************************************************************************
347 // Set monitor loop file descriptor
348 //********************************************************************************
349 int sysSetMonitorFd(int fd)
350 {
351     return (boardIoctl(BOARD_IOCTL_SET_MONITOR_FD, 0, "", 0, fd, ""));
352 }
353
354 //******************************************************************************
355 // Get VCOPE board information: cs, gpio, board revision
356 //******************************************************************************
357 int sysGetVcopeInfo(int info_type)
358 {
359     return(boardIoctl(BOARD_IOCTL_GET_VCOPE_GPIO, 0, "", 0, info_type, ""));
360 }
361
362 //******************************************************************************
363 // Configure Chip Select, by setting given parameter to a passed value
364 //******************************************************************************
365 int sysConfigCs (int cs_number, void *cs_info)
366 {
367     return(boardIoctl(BOARD_IOCTL_SET_CS_PAR, 0, "", 0, cs_number, cs_info));
368 }
369
370 //******************************************************************************
371 // Set up PLL clock register according to the passed values
372 //******************************************************************************
373 int  sysSetPllClockRegister(mask, value)
374 {
375     return(boardIoctl(BOARD_IOCTL_SET_PLL, 0, "", mask, value, ""));
376 }
377
378 //******************************************************************************
379 // Configure GPIO bit according to the passed values
380 //******************************************************************************
381 int  sysSetGpioBit (int gpio_bit, GPIO_STATE_t flag)
382 {
383     return(boardIoctl(BOARD_IOCTL_SET_GPIO, 0, "", gpio_bit, flag, ""));
384 }
385
386 //swda add,02/04/2005
387 //get serial number of board
388 int swdasysGetSerialNo(char *serial, int length)
389 {
390     return( boardIoctl(BOARD_IOCTL_GET_SERIAL_NUMBER, 0, serial, length, 0, "") );
391 }
392
393 #if     DEFAULT_WLAN_WEP128
394 int swdasysGetWEP128Key(char *WEP128key, int length)
395 {
396     return( boardIoctl(BOARD_IOCTL_GET_WEP128_KEY, 0, WEP128key, length, 0, "") );
397 }
398 #endif
399
400 int swdasysInfoBootComplete(void)
401 {
402         return( boardIoctl(BOARD_IOCTL_INFO_BOOT_COMPLETE, 0, "", 0, 0, "") );
403 }
404
405 #if ODM_AUTO_PROVISION_LAN
406 int swdasysGetAutoProvisionGPIOstatus(void)
407 {
408         return( boardIoctl(BOARD_IOCTL_AUTO_PROVISION_LAN, 0, "", 0, 0, "") );
409 }
410 #endif
411
412 /***************************************************************
413 Function: swdaSetAolTestLedState()
414 Description: Set LLL Test led state
415 Arguments: state - 0:off  
416                    1:on for 30 seconds 
417                    2:blinking for 30 seconds        
418 Returns: 1 - TRUE  else - FALSE
419 Author: Swda, 11/02/2005
420 ***************************************************************/
421 #if LLL_TEST_LED
422 int swdaSetAolTestLedState(int state)
423 {
424         return( boardIoctl(BOARD_IOCTL_SET_LLL_TEST_LED, 0, "", 0, state, "") );
425 }
426 #endif
427
428 #if     WLAN_ENABLE_CTRL_BUTTON
429 #define GET_WLAN_ENABLE_GPIO_STATUS     0       //the same as in board.c
430
431 int swdaGetWlanEnableGPIOstatus(void)
432 {
433         return(boardIoctl(BOARD_IOCTL_WLAN_ENABLE_CTRL_BUTTON, PERSISTENT, "", GET_WLAN_ENABLE_GPIO_STATUS, 0, ""));
434 }
435 #endif
436
437 int swdasysSetVar(char *name, int value)
438 {
439     return( boardIoctl(BOARD_IOCTL_SET_VAR, 0, name, value, 0, "") );
440 }
441 //swda add end
442
443 #endif // USE_ALL
444
445 /*==== add by Andrew (2004/09/14) ====*/
446 #if defined(CFG_XFER_TO_FACDEFLT)
447 int sysFactDefltGet(char *string, int strLen, int offset)
448 {
449    return (boardIoctl(BOARD_IOCTL_FLASH_READ, FACTDEFLT, string, strLen, offset, ""));
450 }
451
452 int sysFactDefltSet(char *string, int strLen, int offset)
453 {
454     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, FACTDEFLT, string, strLen, offset, ""));
455 }
456 #endif
457 /*==== end ====*/
458