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