1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  RoboSwitch SPI hacking commands		File: ui_robospi.c
5    *
6    *  These commands let you read and write RoboSwitch registers.
7    *
8    *********************************************************************
9    *
10    *  Copyright 2004
11    *  Broadcom Corporation. All rights reserved.
12    *
13    *  This software is furnished under license and may be used and
14    *  copied only in accordance with the following terms and
15    *  conditions.  Subject to these conditions, you may download,
16    *  copy, install, use, modify and distribute modified or unmodified
17    *  copies of this software in source and/or binary form.  No title
18    *  or ownership is transferred hereby.
19    *
20    *  1) Any source code used, modified or distributed must reproduce
21    *     and retain this copyright notice and list of conditions
22    *     as they appear in the source file.
23    *
24    *  2) No right is granted to use any trade name, trademark, or
25    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
26    *     name may not be used to endorse or promote products derived
27    *     from this software without the prior written permission of
28    *     Broadcom Corporation.
29    *
30    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
31    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
32    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
33    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
34    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
35    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
36    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
40    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
41    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
42    *     THE POSSIBILITY OF SUCH DAMAGE.
43    ********************************************************************* */
44
45
46#include "cfe.h"
47
48#include "ui_command.h"
49
50
51int ui_init_robocmds(void);
52
53static int ui_cmd_roboget(ui_cmdline_t *cmd,int argc,char *argv[]);
54static int ui_cmd_roboset(ui_cmdline_t *cmd,int argc,char *argv[]);
55
56int ui_init_robocmds(void)
57{
58    cmd_addcmd("robo get",
59	       ui_cmd_roboget,
60	       NULL,
61	       "Get register value on Robo switch",
62	       "robo get page reg [regsize]\n\n"
63	       "This command displays the contents of the register on a Robo switch.\n"
64	       "By default regsize is 1 (8-bit register)\n",
65	       "-dev=*;Specify Robo device number (default=0)");
66
67    cmd_addcmd("robo set",
68	       ui_cmd_roboset,
69	       NULL,
70	       "Set the value of a Robo switch register",
71	       "robo set page reg byte0 [byte1 byte2 ...]\n\n"
72	       "Sets the value of a register on a Robo switch.  All bytes must be\n"
73	       "specified to ensure correct behavior, e.g. for a 16-bit register\n"
74	       "byte0 and byte1 must be specified\n",
75	       "-dev=*;Specify Robo device number (default=0)");
76
77    return 0;
78}
79
80static int ui_cmd_roboget(ui_cmdline_t *cmd,int argc,char *argv[])
81{
82    char *x;
83    char fname[8];
84    int page, reg, len, i, offset, fh, bytes, devno;
85    unsigned char buf[8];
86
87    if (cmd_sw_value(cmd,"-dev",&x)) {
88	devno = atoi(x);
89	}
90    else devno = 0;
91    xsprintf(fname,"robo%d",devno);
92
93    x = cmd_getarg(cmd,0);
94    if (!x) return ui_showusage(cmd);
95    page = atoi(x);
96
97    x = cmd_getarg(cmd,1);
98    if (!x) return ui_showusage(cmd);
99    reg = atoi(x);
100
101    x = cmd_getarg(cmd,2);
102    len = x ? atoi(x) : 1;
103
104    fh = cfe_open(fname);
105    if (fh < 0) {
106        return ui_showerror(fh,"Could not open RoboSwitch device");
107        }
108
109    offset = ((page & 0xFF) << 8) | (reg & 0xFF);
110    bytes = cfe_readblk(fh,offset,PTR2HSADDR(buf),len);
111
112    cfe_close(fh);
113
114    if (bytes == 0) return -2;
115
116    printf("RoboSwitch register %02X on page %02X =",reg,page);
117    for (i = 0; i < len; i++) {
118        printf(" %02X", buf[i]);
119        }
120    printf("\n");
121
122    return 0;
123}
124
125static int ui_cmd_roboset(ui_cmdline_t *cmd,int argc,char *argv[])
126{
127    char *x;
128    char fname[8];
129    int page, reg, len, fh, offset, bytes, devno;
130    unsigned char buf[8];
131
132    if (cmd_sw_value(cmd,"-dev",&x)) {
133	devno = atoi(x);
134	}
135    else devno = 0;
136    xsprintf(fname,"robo%d",devno);
137
138    x = cmd_getarg(cmd,0);
139    if (!x) return ui_showusage(cmd);
140    page = atoi(x);
141
142    x = cmd_getarg(cmd,1);
143    if (!x) return ui_showusage(cmd);
144    reg = atoi(x);
145
146    len = 0;
147    do {
148        x = cmd_getarg(cmd,2+len);
149        if (x) {
150            buf[len++] = atoi(x);
151        }
152    } while (x && len < sizeof(buf));
153
154    if (len == 0) {
155        return ui_showusage(cmd);
156        }
157
158    fh = cfe_open(fname);
159    if (fh < 0) {
160        return ui_showerror(fh,"Could not open RoboSwitch device");
161        }
162
163    offset = ((page & 0xFF) << 8) | (reg & 0xFF);
164    bytes = cfe_writeblk(fh,offset,PTR2HSADDR(buf),len);
165
166    cfe_close(fh);
167
168    if (bytes == 0) return -2;
169
170    printf("Wrote %d byte(s) to RoboSwitch page 0x%02X register 0x%02X\n",
171           len,page,reg);
172
173    return 0;
174}
175