GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / arch / mips / board / pt1125 / src / ui_pt1125.c
blob88d0fc1728c7d2a5d77470e7958f4ff0ec7a4bd6
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * PTSWARM-specific commands File: ui_pt1125.c
5 *
6 * A temporary sandbox for misc test routines and commands.
7 *
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
10 * modification history
11 * --------------------
12 * 01a,27jun02,gtb unchanged from ui_ptswarm.c.
14 *********************************************************************
16 * Copyright 2000,2001
17 * Broadcom Corporation. All rights reserved.
19 * This software is furnished under license and may be used and
20 * copied only in accordance with the following terms and
21 * conditions. Subject to these conditions, you may download,
22 * copy, install, use, modify and distribute modified or unmodified
23 * copies of this software in source and/or binary form. No title
24 * or ownership is transferred hereby.
26 * 1) Any source code used, modified or distributed must reproduce
27 * and retain this copyright notice and list of conditions as
28 * they appear in the source file.
30 * 2) No right is granted to use any trade name, trademark, or
31 * logo of Broadcom Corporation. Neither the "Broadcom
32 * Corporation" name nor any trademark or logo of Broadcom
33 * Corporation may be used to endorse or promote products
34 * derived from this software without the prior written
35 * permission of Broadcom Corporation.
37 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
39 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
40 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
41 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
42 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
45 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
46 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
47 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
48 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
49 * THE POSSIBILITY OF SUCH DAMAGE.
50 ********************************************************************* */
53 #include "lib_types.h"
54 #include "lib_string.h"
55 #include "lib_queue.h"
56 #include "lib_malloc.h"
57 #include "lib_printf.h"
59 #include "cfe_iocb.h"
60 #include "cfe_device.h"
61 #include "cfe_console.h"
62 #include "cfe_devfuncs.h"
63 #include "cfe_timer.h"
65 #include "cfe_error.h"
67 #include "ui_command.h"
68 #include "cfe.h"
70 #include "cfe_ioctl.h"
72 #include "bsp_config.h"
74 #include "sbmips.h"
75 #include "sb1250_regs.h"
76 #include "sb1250_smbus.h"
77 #include "sb1250_scd.h"
79 #include "pt1125.h"
81 /* *********************************************************************
82 * Configuration
83 ********************************************************************* */
85 #define _MAX6654_ /* Support Maxim 6654 temperature chip w/parasitic mode */
87 /* *********************************************************************
88 * prototypes
89 ********************************************************************* */
91 int ui_init_swarmcmds(void);
92 static int ui_cmd_showtemp(ui_cmdline_t *cmd,int argc,char *argv[]);
93 static int ui_cmd_reset(ui_cmdline_t *cmd,int argc,char *argv[]);
94 static void temp_timer_proc(void *);
96 #ifdef _FLASHPROG_
97 static int ui_cmd_flashop(ui_cmdline_t *cmd,int argc,char *argv[]);
98 #ifdef __long64
99 #define XTOI(x) xtoq(x)
100 #else
101 #define XTOI(x) xtoi(x)
102 #endif
103 #endif /* _FLASHPROG_ */
106 /* *********************************************************************
107 * Data
108 ********************************************************************* */
110 static int64_t temp_timer = 0;
111 static int temp_prev_local = 0;
112 static int temp_prev_remote = 0;
114 /* *********************************************************************
115 * ui_init_swarmcmds()
117 * Add SWARM-specific commands to the command table
119 * Input parameters:
120 * nothing
122 * Return value:
124 ********************************************************************* */
127 int ui_init_swarmcmds(void)
129 cmd_addcmd("show temp",
130 ui_cmd_showtemp,
131 NULL,
132 "Display CPU temperature",
133 "show temp",
134 "-continuous;Poll for temperature changes|"
135 "-stop;Stop polling for temperature changes");
137 cmd_addcmd("reset",
138 ui_cmd_reset,
139 NULL,
140 "Reset the system.",
141 "reset [-yes] -softreset|-cpu|-unicpu1|-unicpu0|-sysreset",
142 "-yes;Don't ask for confirmation|"
143 "-softreset;Soft reset of the entire chip|"
144 "-cpu;Reset the CPUs|"
145 "-unicpu1;Reset to uniprocessor using CPU1|"
146 "-unicpu0;Reset to uniprocessor using CPU0|"
147 "-sysreset;Full system reset");
149 #ifdef _FLASHPROG_
150 cmd_addcmd("flashop",
151 ui_cmd_flashop,
152 NULL,
153 "Perform flash operations on a single flash device",
154 "flashop [options] [flashdevice]\n\n"
155 "Erases, protects, or unprotects sectors on a flash memory device.\n"
157 "-erase;Erase an address range of flash, no load|"
158 "-protect;Protect an address range of flash, no load|"
159 "-unprotect;Unprotect an address range of flash, no load|"
160 "-startaddr=*;Start address of erase or protect|"
161 "-endaddr=*;End address of erase or protect|"
162 "-all;Perform operation on entire flash"
164 #endif
166 cfe_bg_add(temp_timer_proc,NULL);
168 return 0;
173 /* *********************************************************************
174 * temp_smbus_init(chan)
176 * Initialize the specified SMBus channel for the temp sensor
178 * Input parameters:
179 * chan - channel # (0 or 1)
181 * Return value:
182 * nothing
183 ********************************************************************* */
185 static void temp_smbus_init(int chan)
187 uintptr_t reg;
189 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_FREQ));
191 SBWRITECSR(reg,K_SMB_FREQ_100KHZ); /* 400Khz clock */
193 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_CONTROL));
195 SBWRITECSR(reg,0); /* not in direct mode, no interrupts, will poll */
199 /* *********************************************************************
200 * temp_smbus_waitready(chan)
202 * Wait until the SMBus channel is ready. We simply poll
203 * the busy bit until it clears.
205 * Input parameters:
206 * chan - channel (0 or 1)
208 * Return value:
209 * nothing
210 ********************************************************************* */
211 static int temp_smbus_waitready(int chan)
213 uintptr_t reg;
214 uint64_t status;
216 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_STATUS));
218 for (;;) {
219 status = SBREADCSR(reg);
220 if (status & M_SMB_BUSY) continue;
221 break;
224 if (status & M_SMB_ERROR) {
225 SBWRITECSR(reg,(status & M_SMB_ERROR));
226 return -1;
228 return 0;
231 /* *********************************************************************
232 * temp_smbus_read(chan,slaveaddr,devaddr)
234 * Read a byte from the temperature sensor chip
236 * Input parameters:
237 * chan - SMBus channel
238 * slaveaddr - SMBus slave address
239 * devaddr - byte with in the sensor device to read
241 * Return value:
242 * 0 if ok
243 * else -1
244 ********************************************************************* */
246 static int temp_smbus_read(int chan,int slaveaddr,int devaddr)
248 uintptr_t reg;
249 int err;
252 * Make sure the bus is idle (probably should
253 * ignore error here)
256 if (temp_smbus_waitready(chan) < 0) return -1;
259 * Write the device address to the controller. There are two
260 * parts, the high part goes in the "CMD" field, and the
261 * low part is the data field.
264 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_CMD));
265 SBWRITECSR(reg,devaddr);
268 * Read the data byte
271 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_START));
272 SBWRITECSR(reg,V_SMB_TT(K_SMB_TT_CMD_RD1BYTE) | slaveaddr);
274 err = temp_smbus_waitready(chan);
275 if (err < 0) return err;
277 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_DATA));
278 err = SBREADCSR(reg);
280 return (err & 0xFF);
283 #ifdef _MAX6654_
284 /* *********************************************************************
285 * temp_smbus_write(chan,slaveaddr,devaddr,data)
287 * write a byte to the temperature sensor chip
289 * Input parameters:
290 * chan - SMBus channel
291 * slaveaddr - SMBus slave address
292 * devaddr - byte with in the sensor device to read
294 * Return value:
295 * 0 if ok
296 * else -1
297 ********************************************************************* */
299 static int temp_smbus_write(int chan,int slaveaddr,int devaddr,int data)
301 uintptr_t reg;
302 int err;
305 * Make sure the bus is idle (probably should
306 * ignore error here)
309 if (temp_smbus_waitready(chan) < 0) return -1;
312 * Write the device address to the controller. There are two
313 * parts, the high part goes in the "CMD" field, and the
314 * low part is the data field.
317 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_CMD));
318 SBWRITECSR(reg,devaddr);
321 * Write the data byte
324 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_DATA));
325 SBWRITECSR(reg,data);
328 * Do the write command.
331 reg = PHYS_TO_K1(A_SMB_REGISTER(chan,R_SMB_START));
332 SBWRITECSR(reg,V_SMB_TT(K_SMB_TT_WR2BYTE) | slaveaddr);
334 err = temp_smbus_waitready(chan);
336 return err;
338 #endif
341 /* *********************************************************************
342 * temp_showtemp(noisy)
344 * Display the temperature. If 'noisy' is true, display it
345 * regardless of whether it has changed, otherwise only display
346 * when it has changed.
348 * Input parameters:
349 * noisy - display whether or not changed
351 * Return value:
352 * nothing
353 ********************************************************************* */
355 static int temp_showtemp(int noisy)
357 int local,remote,status;
358 char statstr[50];
360 local = temp_smbus_read(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,0);
361 remote = temp_smbus_read(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,1);
362 status = temp_smbus_read(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,2);
364 if ((local < 0) || (remote < 0) || (status < 0)) {
365 if (noisy) printf("Temperature sensor device did not respond\n");
366 return -1;
369 if (noisy || (local != temp_prev_local) || (remote != temp_prev_remote)) {
370 statstr[0] = 0;
371 if (status & 0x80) strcat(statstr,"Busy ");
372 if (status & 0x40) strcat(statstr,"HiTempLcl ");
373 if (status & 0x20) strcat(statstr,"LoTempLcl ");
374 if (status & 0x10) strcat(statstr,"HiTempRem ");
375 if (status & 0x08) strcat(statstr,"LoTempRem ");
376 if (status & 0x04) strcat(statstr,"Fault ");
378 if (noisy || !(status & 0x80)) {
379 /* don't display if busy, always display if noisy */
380 console_log("Temperature: CPU: %dC Board: %dC Status:%02X [ %s]",
381 remote,local,status,statstr);
385 temp_prev_local = local;
386 temp_prev_remote = remote;
388 return 0;
391 /* *********************************************************************
392 * ui_cmd_reset(cmd,argc,argv)
394 * RESET command.
396 * Input parameters:
397 * cmd - command structure
398 * argc,argv - parameters
400 * Return value:
401 * -1 if error occured. Does not return otherwise
402 ********************************************************************* */
404 static int ui_cmd_reset(ui_cmdline_t *cmd,int argc,char *argv[])
406 uint64_t data;
407 uint64_t olddata;
408 int confirm = 1;
409 char str[50];
411 data = SBREADCSR(A_SCD_SYSTEM_CFG) & ~M_SYS_SB_SOFTRES;
412 olddata = data;
414 if (cmd_sw_isset(cmd,"-yes")) confirm = 0;
416 if (cmd_sw_isset(cmd,"-softreset")) data |= M_SYS_SB_SOFTRES;
418 if (cmd_sw_isset(cmd,"-unicpu0")) data |= M_SYS_UNICPU0;
419 else if (cmd_sw_isset(cmd,"-unicpu1")) data |= M_SYS_UNICPU1;
421 if (cmd_sw_isset(cmd,"-sysreset")) data |= M_SYS_SYSTEM_RESET;
423 if (cmd_sw_isset(cmd,"-cpu")) data |= (M_SYS_CPU_RESET_0 | M_SYS_CPU_RESET_1);
425 if (data == olddata) { /* no changes to reset pins were specified */
426 return ui_showusage(cmd);
429 if (confirm) {
430 console_readline("Are you sure you want to reset? ",str,sizeof(str));
431 if ((str[0] != 'Y') && (str[0] != 'y')) return -1;
434 SBWRITECSR(A_SCD_SYSTEM_CFG,data);
436 /* should not return */
438 return -1;
441 /* *********************************************************************
442 * ui_cmd_showtemp(cmd,argc,argv)
444 * Show temperature
446 * Input parameters:
447 * cmd - command structure
448 * argc,argv - parameters
450 * Return value:
451 * -1 if error occured. Does not return otherwise
452 ********************************************************************* */
454 static int ui_cmd_showtemp(ui_cmdline_t *cmd,int argc,char *argv[])
457 temp_smbus_init(TEMPSENSOR_SMBUS_CHAN);
459 #ifdef _MAX6654_
460 do {
461 int dev,rev;
462 static int didinit = 0;
464 if (!didinit) {
465 didinit = 1;
466 dev = temp_smbus_read(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,0xFE);
467 rev = temp_smbus_read(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,0xFF);
468 printf("Temperature Sensor Device ID %02X rev %02X\n",dev,rev);
470 if (dev == 0x4D) { /* MAX6654 */
471 printf("Switching MAX6654 to parasitic mode\n");
472 /* Switch to 1hz conversion rate (1 seconds per conversion) */
473 temp_smbus_write(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,0x0A,0x04);
474 /* Switch to parasitic mode */
475 temp_smbus_write(TEMPSENSOR_SMBUS_CHAN,TEMPSENSOR_SMBUS_DEV,9,0x10);
478 } while (0);
479 #endif
481 if (temp_showtemp(1) < 0) {
482 TIMER_CLEAR(temp_timer);
483 return -1;
486 if (cmd_sw_isset(cmd,"-continuous")) {
487 TIMER_SET(temp_timer,2*CFE_HZ);
489 if (cmd_sw_isset(cmd,"-stop")) {
490 TIMER_CLEAR(temp_timer);
493 return 0;
496 /* *********************************************************************
497 * temp_timer_proc()
499 * So we can be fancy and log temperature changes as they happen.
501 * Input parameters:
502 * nothing
504 * Return value:
505 * nothing
506 ********************************************************************* */
508 void temp_timer_proc(void *arg)
510 if (!TIMER_RUNNING(temp_timer)) return;
512 if (TIMER_EXPIRED(temp_timer)) {
513 temp_showtemp(0);
514 TIMER_SET(temp_timer,2*CFE_HZ);
519 #ifdef _FLASHPROG_
520 /* *********************************************************************
521 * ui_cmd_flashop(cmd,argc,argv)
523 * The 'flashop' command lives here. This command does a variety
524 * of flash operations over a range of bytes:
526 * erase, protect, unprotect
528 * Input parameters:
529 * cmd - command table entry
530 * argc,argv - parameters
532 * Return value:
533 * 0 if ok
534 * else error
535 ********************************************************************* */
536 static int ui_cmd_flashop(ui_cmdline_t *cmd,int argc,char *argv[])
538 int fd;
539 int res;
540 char *flashdev;
541 int devtype;
542 flash_info_t flashinfo;
543 int erase;
544 int protect;
545 int unprotect;
546 int retlen;
547 unsigned int startaddr = 0;
548 unsigned int endaddr = 0;
549 char *x;
550 int all;
551 flash_range_t range;
553 flashdev = cmd_getarg(cmd,0);
555 if (!flashdev) flashdev = "flash0";
558 * Make sure it's a flash device.
561 res = cfe_getdevinfo(flashdev);
562 if (res < 0) {
563 return ui_showerror(CFE_ERR_DEVNOTFOUND,flashdev);
566 devtype = res & CFE_DEV_MASK;
568 if (res != CFE_DEV_FLASH) {
569 xprintf("Device '%s' is not a flash device.\n",flashdev);
570 return CFE_ERR_INV_PARAM;
573 protect = cmd_sw_isset(cmd,"-protect");
574 unprotect = cmd_sw_isset(cmd,"-unprotect");
575 erase = cmd_sw_isset(cmd,"-erase");
577 if (protect == 1) {
578 if (erase || unprotect)
580 xprintf("Conflicting options\n");
581 return CFE_ERR_INV_PARAM;
585 if (unprotect == 1) {
586 if (erase || protect)
588 xprintf("Conflicting options\n");
589 return CFE_ERR_INV_PARAM;
593 if (erase == 1) {
594 if (unprotect || protect)
596 xprintf("Conflicting options\n");
597 return CFE_ERR_INV_PARAM;
601 if (erase == 0 && protect == 0 && unprotect == 0) {
602 xprintf("Need one of following options: -erase -protect -unprotect\n");
603 return CFE_ERR_INV_PARAM;
607 fd = cfe_open(flashdev);
608 if (fd < 0) {
609 xprintf("Could not open device '%s'\n",flashdev);
610 return CFE_ERR_DEVNOTFOUND;
613 res = cfe_ioctl(fd,IOCTL_FLASH_GETINFO,(uint8_t *) &flashinfo,sizeof(flash_info_t),&retlen,0);
614 if (res != 0) {
615 cfe_close (fd);
616 return CFE_ERR_IOERR;
619 all = cmd_sw_isset(cmd,"-all");
620 if (all == 0) {
621 if (cmd_sw_value(cmd,"-startaddr",&x)) {
622 startaddr = XTOI(x);
624 else {
625 xprintf("Need option: -startaddr\n");
626 cfe_close (fd);
627 return CFE_ERR_INV_PARAM;
630 if (cmd_sw_value(cmd,"-endaddr",&x)) {
631 endaddr = XTOI(x);
633 else {
634 xprintf("Need option: -endaddr\n");
635 cfe_close (fd);
636 return CFE_ERR_INV_PARAM;
639 if (startaddr > endaddr) {
640 xprintf("Final offset (endaddr) must not be less than startaddr \n");
641 cfe_close (fd);
642 return CFE_ERR_INV_PARAM;
645 /* Make sure endaddr is within flash */
647 if (endaddr >= flashinfo.flash_size) {
648 xprintf("Final offset (endaddr) must less than 0x%x\n", flashinfo.flash_size);
649 cfe_close (fd);
650 return CFE_ERR_INV_PARAM;
653 /* We got here, so params are OK */
654 range.range_base = startaddr;
655 range.range_length = endaddr-startaddr;
657 else {
658 range.range_base = 0;
659 range.range_length = flashinfo.flash_size;
662 if (erase)
664 res = cfe_ioctl(fd, IOCTL_FLASH_ERASE_RANGE,
665 (void *) &range, NULL, NULL, NULL);
668 else if (protect)
670 res = cfe_ioctl(fd, IOCTL_FLASH_PROTECT_RANGE,
671 (void *) &range, NULL, NULL, NULL);
674 else if (unprotect)
676 res = cfe_ioctl(fd, IOCTL_FLASH_UNPROTECT_RANGE,
677 (void *) &range, NULL, NULL, NULL);
680 if (res != 0) {
681 printf("ioctl error\n");
684 cfe_close (fd);
685 return 0;
687 #endif