www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / broadcom / cfm / util / psi / board_api.c
1 /***************************************************************************
2  * Broadcom Corp. Confidential
3  * Copyright 2001 Broadcom Corp. All Rights Reserved.
4  *
5  * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED
6  * SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM.
7  * YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT
8  * SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9  *
10  ***************************************************************************
11  * File Name  : board_api.c
12  *
13  * Description: interface for board level calls: flash,
14  *              led, soft reset, free memory page, and memory dump.
15  *              Adapted form flash_api.c by Yen Tran.
16  *
17  * Created on : 02/20/2002  seanl
18  *
19  ***************************************************************************/
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <fcntl.h>      /* open */
25 #include <unistd.h>     /* exit */
26 #include <sys/ioctl.h>  /* ioctl */
27 #include <memory.h>
28
29 #include <bcmtypes.h>
30 #include <board_api.h>
31 int boardIoctl(int board_ioctl, BOARD_IOCTL_ACTION action, char *string, int strLen, int offset, char *buf)
32 {
33     BOARD_IOCTL_PARMS IoctlParms;
34     int boardFd = 0;
35
36     boardFd = open("/dev/brcmboard", O_RDWR);
37     if ( boardFd != -1 ) {
38         IoctlParms.string = string;
39         IoctlParms.strLen = strLen;
40         IoctlParms.offset = offset;
41         IoctlParms.action = action;
42         IoctlParms.buf    = buf;
43         ioctl(boardFd, board_ioctl, &IoctlParms);
44         close(boardFd);
45         boardFd = IoctlParms.result;
46     } else
47         printf("Unable to open device /dev/brcmboard.\n");
48
49     return boardFd;
50 }
51 #ifdef USE_ALL
52
53
54 /*****************************************************************************
55 * sysScratchPadGet - get the contents of persistent stratch pad flash memory
56 * INPUT:   tokenId -- token id, ASCIZ tokBuf (up to 15 char)
57 *          tokBuf  -- buf 
58 *          bufLen  -- buf len
59 * RETURNS: 0 - ok, -1 fail.
60 */
61 int sysScratchPadGet(char *tokenId, char *tokBuf, int bufLen)
62 {
63     return (boardIoctl(BOARD_IOCTL_FLASH_READ, SCRATCH_PAD, tokenId, bufLen, bufLen, tokBuf));
64 }
65
66 /*****************************************************************************
67 * sysScratchPadSet - write the contents of persistent scratch pad flash memory
68 * INPUT:   tokenId -- token id, ASCIZ tokBuf (up to 15 char)
69 *          tokBuf  -- buf 
70 *          bufLen  -- buf len
71 * RETURNS: 0 - ok, -1 fail.
72 */
73 int sysScratchPadSet(char *tokenId, char *tokBuf, int bufLen)
74 {
75     char *buf = malloc(bufLen);
76
77     //If the data associated with the tokenId already exists in the scratch pad,
78     //we don't want to set it again.
79         //Note that writing to the scratch pad is a non-preemptive time consuming
80         //operation that should be avoided.
81     if (buf != NULL &&
82         boardIoctl(BOARD_IOCTL_FLASH_READ, SCRATCH_PAD, tokenId, bufLen, bufLen, buf) >= 0) {
83         if (bcmp(buf, tokBuf, bufLen) == 0) {
84             free(buf);
85             return 0;
86         }
87     }
88     if (buf != NULL) free(buf);
89
90     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, SCRATCH_PAD, tokenId, bufLen, bufLen, tokBuf));
91 }
92
93 /*****************************************************************************
94 * sysScratchPadClearAll  - wipeout the scratch pad
95 * RETURNS: 0 - ok, -1 fail.
96 */
97 int sysScratchPadClearAll(void)
98 {
99     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, SCRATCH_PAD, "", -1, -1, ""));
100 }
101
102 /*****************************************************************************
103 * sysPersistentGet - get the contents of non-volatile RAM,
104 * RETURNS: OK, always.
105 */
106 int sysNvRamGet(char *string, int strLen, int offset)
107 {
108     return (boardIoctl(BOARD_IOCTL_FLASH_READ, NVRAM, string, strLen, offset, ""));
109 }
110
111
112
113 /*****************************************************************************
114 * sysPersistentSet - write the contents of non-volatile RAM
115 * RETURNS: OK, always.
116 */
117 int sysNvRamSet(char *string, int strLen, int offset)
118 {
119     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, NVRAM, string, strLen, offset, ""));
120 }
121
122 /*****************************************************************************
123 * sysNrPagesGet - returns number of free system pages.  Each page is 4K bytes.
124 * RETURNS: Number of 4K pages.
125 */
126 int sysNrPagesGet(void)
127 {
128     return (boardIoctl(BOARD_IOCTL_GET_NR_PAGES, 0, "", 0, 0, ""));
129 }
130
131 /*****************************************************************************
132 * sysDumpAddr - Dump kernel memory.
133 * RETURNS: OK, always.
134 */
135 int sysDumpAddr(char *addr, int len)
136 {
137     return (boardIoctl(BOARD_IOCTL_DUMP_ADDR, 0, addr, len, 0, ""));
138 }
139
140 /*****************************************************************************
141 * sysDumpAddr - Set kernel memory.
142 * RETURNS: OK, always.
143 */
144 int sysSetMemory(char *addr, int size, unsigned long value )
145 {
146     return (boardIoctl(BOARD_IOCTL_SET_MEMORY, 0, addr, size, (int) value, ""));
147 }
148
149 /*****************************************************************************
150  * image points to image to be programmed to flash; size is the size (in bytes)
151  * of the image.
152  * if error, return -1; otherwise return 0
153  */
154
155 #endif // USE_ALL
156
157 /*****************************************************************************
158 * sysPersistentGet - get the contents of persistent flash memory
159 * RETURNS: OK, always.
160 */
161 int sysPersistentGet(char *string, int strLen, int offset)
162 {
163     return (boardIoctl(BOARD_IOCTL_FLASH_READ, PERSISTENT, string, strLen, offset, ""));
164 }
165
166 /*****************************************************************************
167 * sysPersistenSet - write the contents of persistent Scrach Pad flash memory
168 * RETURNS: OK, always.
169 */
170 int sysPersistentSet(char *string, int strLen, int offset)
171 {
172     return (boardIoctl(BOARD_IOCTL_FLASH_WRITE, PERSISTENT, string, strLen, offset, ""));
173 }
174
175 //********************************************************************************
176 // Get PSI size
177 //********************************************************************************
178 int sysGetPsiSize( void )
179 {
180     return( boardIoctl(BOARD_IOCTL_GET_PSI_SIZE, 0, "", 0, 0, "") );
181 }
182
183 int sysFlashImageSet(void *image, int size, int addr,
184     BOARD_IOCTL_ACTION imageType)
185 {
186     int result;
187
188     result = boardIoctl(BOARD_IOCTL_FLASH_WRITE, imageType, image, size, addr, "");
189
190     return(result);
191 }
192
193 /*****************************************************************************
194  * Get flash size 
195  * return int flash size
196  */
197 int sysFlashSizeGet(void)
198 {
199     return (boardIoctl(BOARD_IOCTL_FLASH_READ, FLASH_SIZE, "", 0, 0, ""));
200 }
201
202 /*****************************************************************************
203 * kerSysMipsSoftReset - soft reset the mips. (reboot, go to 0xbfc00000)
204 * RETURNS: NEVER
205 */
206 void sysMipsSoftReset(void)
207 {  
208     boardIoctl(BOARD_IOCTL_MIPS_SOFT_RESET, 0, "", 0, 0, "");
209 }
210
211 //********************************************************************************
212 // Get Chip Id
213 //********************************************************************************
214 int sysGetChipId( void )
215 {
216     return( boardIoctl(BOARD_IOCTL_GET_CHIP_ID, 0, "", 0, 0, "") );
217 }
218
219 //********************************************************************************
220 // Wakeup monitor task
221 //********************************************************************************
222 void sysWakeupMonitorTask(void)
223 {
224     boardIoctl(BOARD_IOCTL_WAKEUP_MONITOR_TASK, 0, "", 0, 0, "");
225 }
226
227 #ifdef USE_ALL
228 //********************************************************************************
229 // LED status display:  ADSL link: DOWN/UP, PPP: DOWN/STARTING/UP
230 //********************************************************************************
231 void sysLedCtrl(BOARD_LED_NAME ledName, BOARD_LED_STATE ledState)
232 {
233     boardIoctl(BOARD_IOCTL_LED_CTRL, 0, "", (int)ledName, (int)ledState, "");
234 }
235
236
237 //********************************************************************************
238 // Get board id
239 //********************************************************************************
240 int sysGetBoardIdName(char *name, int length)
241 {
242     return( boardIoctl(BOARD_IOCTL_GET_ID, 0, name, length, 0, "") );
243 }
244
245
246 //********************************************************************************
247 // Get MAC Address
248 //********************************************************************************
249 int sysGetMacAddress( unsigned char *pucaAddr, unsigned long ulId )
250 {
251     return(boardIoctl(BOARD_IOCTL_GET_MAC_ADDRESS, 0, pucaAddr, 6, (int) ulId, ""));
252 }
253
254
255 //********************************************************************************
256 // Release MAC Address
257 //********************************************************************************
258 int sysReleaseMacAddress( unsigned char *pucaAddr )
259 {
260     return( boardIoctl(BOARD_IOCTL_RELEASE_MAC_ADDRESS, 0, pucaAddr, 6, 0, "") );
261 }
262
263
264 //********************************************************************************
265 // Get SDRAM size
266 //********************************************************************************
267 int sysGetSdramSize( void )
268 {
269     return( boardIoctl(BOARD_IOCTL_GET_SDRAM_SIZE, 0, "", 0, 0, "") );
270 }
271
272
273 /*****************************************************************************
274 * sysGetBooline - get bootline
275 * RETURNS: OK, always.
276 */
277 int sysGetBootline(char *string, int strLen)
278 {
279     return (boardIoctl(BOARD_IOCTL_GET_BOOTLINE, 0, string, strLen, 0, ""));
280 }
281
282 /*****************************************************************************
283 * sysSetBootline - write the bootline to nvram
284 * RETURNS: OK, always.
285 */
286 int sysSetBootline(char *string, int strLen)
287 {
288     return (boardIoctl(BOARD_IOCTL_SET_BOOTLINE, 0, string, strLen, 0, ""));
289 }
290
291 //********************************************************************************
292 // Get MAC Address
293 //********************************************************************************
294 int sysGetBaseMacAddress( unsigned char *pucaAddr )
295 {
296     return(boardIoctl(BOARD_IOCTL_GET_BASE_MAC_ADDRESS, 0, pucaAddr, 6, 0, ""));
297 }
298
299 //********************************************************************************
300 // Get number of Ethernet phys
301 //********************************************************************************
302 int sysGetNumEnet(void)
303 {
304     return(boardIoctl(BOARD_IOCTL_GET_NUM_ENET, 0, "", 0, 0, ""));
305 }
306
307 //********************************************************************************
308 // Get CFE vesion info
309 //********************************************************************************
310 int sysGetCFEVersion(char *string, int strLen)
311 {
312     return (boardIoctl(BOARD_IOCTL_GET_CFE_VER, 0, string, strLen, 0, ""));
313 }
314
315 //********************************************************************************
316 // Get board Ethernet configuration
317 //********************************************************************************
318 int sysGetEnetCfg(char *string, int strLen)
319 {
320     return(boardIoctl(BOARD_IOCTL_GET_ENET_CFG, 0, string, strLen, 0, ""));
321 }
322 //********************************************************************************
323 // Get Wi-Fi Country selection
324 //********************************************************************************
325 int sysGetCountry(char *string, int strLen)
326 {
327     return(boardIoctl(BOARD_IOCTL_GET_CNTRY_SEL, 0, string, strLen, 0, ""));
328 }
329
330 //********************************************************************************
331 // Set monitor loop file descriptor
332 //********************************************************************************
333 int sysSetMonitorFd(int fd)
334 {
335     return (boardIoctl(BOARD_IOCTL_SET_MONITOR_FD, 0, "", 0, fd, ""));
336 }
337
338 //******************************************************************************
339 // Get VCOPE board information: cs, gpio, board revision
340 //******************************************************************************
341 int sysGetVcopeInfo(int info_type)
342 {
343     return(boardIoctl(BOARD_IOCTL_GET_VCOPE_GPIO, 0, "", 0, info_type, ""));
344 }
345
346 //******************************************************************************
347 // Configure Chip Select, by setting given parameter to a passed value
348 //******************************************************************************
349 int sysConfigCs (int cs_number, void *cs_info)
350 {
351     return(boardIoctl(BOARD_IOCTL_SET_CS_PAR, 0, "", 0, cs_number, cs_info));
352 }
353
354 //******************************************************************************
355 // Set up PLL clock register according to the passed values
356 //******************************************************************************
357 int  sysSetPllClockRegister(mask, value)
358 {
359     return(boardIoctl(BOARD_IOCTL_SET_PLL, 0, "", mask, value, ""));
360 }
361
362 //******************************************************************************
363 // Configure GPIO bit according to the passed values
364 //******************************************************************************
365 int  sysSetGpioBit (int gpio_bit, GPIO_STATE_t flag)
366 {
367     return(boardIoctl(BOARD_IOCTL_SET_GPIO, 0, "", gpio_bit, flag, ""));
368 }
369
370 #endif // USE_ALL
371