1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * PTSWARM-specific commands File: ui_pt1120.c
6 * A temporary sandbox for misc test routines and commands.
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
10 * modification history
11 * --------------------
12 * 01a,27jun02,gtb unchanged from ui_ptswarm.c.
14 *********************************************************************
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"
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"
70 #include "cfe_ioctl.h"
72 #include "bsp_config.h"
75 #include "sb1250_regs.h"
76 #include "sb1250_smbus.h"
77 #include "sb1250_scd.h"
81 /* *********************************************************************
83 ********************************************************************* */
85 #define _MAX6654_ /* Support Maxim 6654 temperature chip w/parasitic mode */
87 /* *********************************************************************
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 *);
97 static int ui_cmd_flashop(ui_cmdline_t
*cmd
,int argc
,char *argv
[]);
99 #define XTOI(x) xtoq(x)
101 #define XTOI(x) xtoi(x)
103 #endif /* _FLASHPROG_ */
106 /* *********************************************************************
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
124 ********************************************************************* */
127 int ui_init_swarmcmds(void)
129 cmd_addcmd("show temp",
132 "Display CPU temperature",
134 "-continuous;Poll for temperature changes|"
135 "-stop;Stop polling for temperature changes");
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");
150 cmd_addcmd("flashop",
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"
166 cfe_bg_add(temp_timer_proc
,NULL
);
173 /* *********************************************************************
174 * temp_smbus_init(chan)
176 * Initialize the specified SMBus channel for the temp sensor
179 * chan - channel # (0 or 1)
183 ********************************************************************* */
185 static void temp_smbus_init(int chan
)
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.
206 * chan - channel (0 or 1)
210 ********************************************************************* */
211 static int temp_smbus_waitready(int chan
)
216 reg
= PHYS_TO_K1(A_SMB_REGISTER(chan
,R_SMB_STATUS
));
219 status
= SBREADCSR(reg
);
220 if (status
& M_SMB_BUSY
) continue;
224 if (status
& M_SMB_ERROR
) {
225 SBWRITECSR(reg
,(status
& M_SMB_ERROR
));
231 /* *********************************************************************
232 * temp_smbus_read(chan,slaveaddr,devaddr)
234 * Read a byte from the temperature sensor chip
237 * chan - SMBus channel
238 * slaveaddr - SMBus slave address
239 * devaddr - byte with in the sensor device to read
244 ********************************************************************* */
246 static int temp_smbus_read(int chan
,int slaveaddr
,int devaddr
)
252 * Make sure the bus is idle (probably should
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
);
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
);
284 /* *********************************************************************
285 * temp_smbus_write(chan,slaveaddr,devaddr,data)
287 * write a byte to the temperature sensor chip
290 * chan - SMBus channel
291 * slaveaddr - SMBus slave address
292 * devaddr - byte with in the sensor device to read
297 ********************************************************************* */
299 static int temp_smbus_write(int chan
,int slaveaddr
,int devaddr
,int data
)
305 * Make sure the bus is idle (probably should
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
);
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.
349 * noisy - display whether or not changed
353 ********************************************************************* */
355 static int temp_showtemp(int noisy
)
357 int local
,remote
,status
;
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");
369 if (noisy
|| (local
!= temp_prev_local
) || (remote
!= temp_prev_remote
)) {
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
;
391 /* *********************************************************************
392 * ui_cmd_reset(cmd,argc,argv)
397 * cmd - command structure
398 * argc,argv - parameters
401 * -1 if error occured. Does not return otherwise
402 ********************************************************************* */
404 static int ui_cmd_reset(ui_cmdline_t
*cmd
,int argc
,char *argv
[])
411 data
= SBREADCSR(A_SCD_SYSTEM_CFG
) & ~M_SYS_SB_SOFTRES
;
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
);
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 */
441 /* *********************************************************************
442 * ui_cmd_showtemp(cmd,argc,argv)
447 * cmd - command structure
448 * argc,argv - parameters
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
);
462 static int didinit
= 0;
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);
481 if (temp_showtemp(1) < 0) {
482 TIMER_CLEAR(temp_timer
);
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
);
496 /* *********************************************************************
499 * So we can be fancy and log temperature changes as they happen.
506 ********************************************************************* */
508 void temp_timer_proc(void *arg
)
510 if (!TIMER_RUNNING(temp_timer
)) return;
512 if (TIMER_EXPIRED(temp_timer
)) {
514 TIMER_SET(temp_timer
,2*CFE_HZ
);
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
529 * cmd - command table entry
530 * argc,argv - parameters
535 ********************************************************************* */
536 static int ui_cmd_flashop(ui_cmdline_t
*cmd
,int argc
,char *argv
[])
542 flash_info_t flashinfo
;
547 unsigned int startaddr
= 0;
548 unsigned int endaddr
= 0;
553 flashdev
= cmd_getarg(cmd
,0);
555 if (!flashdev
) flashdev
= "flash0";
558 * Make sure it's a flash device.
561 res
= cfe_getdevinfo(flashdev
);
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");
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
;
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
);
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);
616 return CFE_ERR_IOERR
;
619 all
= cmd_sw_isset(cmd
,"-all");
621 if (cmd_sw_value(cmd
,"-startaddr",&x
)) {
625 xprintf("Need option: -startaddr\n");
627 return CFE_ERR_INV_PARAM
;
630 if (cmd_sw_value(cmd
,"-endaddr",&x
)) {
634 xprintf("Need option: -endaddr\n");
636 return CFE_ERR_INV_PARAM
;
639 if (startaddr
> endaddr
) {
640 xprintf("Final offset (endaddr) must not be less than startaddr \n");
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
);
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
;
658 range
.range_base
= 0;
659 range
.range_length
= flashinfo
.flash_size
;
664 res
= cfe_ioctl(fd
, IOCTL_FLASH_ERASE_RANGE
,
665 (void *) &range
, NULL
, NULL
, NULL
);
670 res
= cfe_ioctl(fd
, IOCTL_FLASH_PROTECT_RANGE
,
671 (void *) &range
, NULL
, NULL
, NULL
);
676 res
= cfe_ioctl(fd
, IOCTL_FLASH_UNPROTECT_RANGE
,
677 (void *) &range
, NULL
, NULL
, NULL
);
681 printf("ioctl error\n");