]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_mii.c
* Some code cleanup
[karo-tx-uboot.git] / common / cmd_mii.c
1 /*
2  * (C) Copyright 2001
3  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * MII Utilities
26  */
27
28 #include <common.h>
29 #include <command.h>
30 #include <miiphy.h>
31
32 #if (CONFIG_COMMANDS & CFG_CMD_MII)
33
34 /*
35  * Display values from last command.
36  */
37 uint last_op;
38 uint last_addr;
39 uint last_data;
40 uint last_reg;
41
42 /*
43  * MII read/write
44  *
45  * Syntax:
46  *  mii read {addr} {reg}
47  *  mii write {addr} {reg} {data}
48  */
49
50 int do_mii (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
51 {
52         char            op;
53         unsigned char   addr, reg;
54         unsigned short  data;
55         int             rcode = 0;
56
57 #if defined(CONFIG_8xx) || defined(CONFIG_MCF52x2)
58         mii_init ();
59 #endif
60
61         /*
62          * We use the last specified parameters, unless new ones are
63          * entered.
64          */
65         op   = last_op;
66         addr = last_addr;
67         data = last_data;
68         reg  = last_reg;
69
70         if ((flag & CMD_FLAG_REPEAT) == 0) {
71                 op = argv[1][0];
72                 if (argc >= 3)
73                         addr = simple_strtoul (argv[2], NULL, 16);
74                 if (argc >= 4)
75                         reg  = simple_strtoul (argv[3], NULL, 16);
76                 if (argc >= 5)
77                         data = simple_strtoul (argv[4], NULL, 16);
78         }
79
80         /*
81          * check info/read/write.
82          */
83         if (op == 'i') {
84                 unsigned char j, start, end;
85                 unsigned int oui;
86                 unsigned char model;
87                 unsigned char rev;
88
89                 /*
90                  * Look for any and all PHYs.  Valid addresses are 0..31.
91                  */
92                 if (argc >= 3) {
93                         start = addr; end = addr + 1;
94                 } else {
95                         start = 0; end = 32;
96                 }
97
98                 for (j = start; j < end; j++) {
99                         if (miiphy_info (j, &oui, &model, &rev) == 0) {
100                                 printf ("PHY 0x%02X: "
101                                         "OUI = 0x%04X, "
102                                         "Model = 0x%02X, "
103                                         "Rev = 0x%02X, "
104                                         "%3dbaseT, %s\n",
105                                         j, oui, model, rev,
106                                         miiphy_speed (j) == _100BASET ? 100 : 10,
107                                         miiphy_duplex (j) == FULL ? "FDX" : "HDX");
108                         }
109                 }
110         } else if (op == 'r') {
111                 if (miiphy_read (addr, reg, &data) != 0) {
112                         printf ("Error reading from the PHY\n");
113                         rcode = 1;
114                 }
115                 printf ("%04X\n", data & 0x0000FFFF);
116         } else if (op == 'w') {
117                 if (miiphy_write (addr, reg, data) != 0) {
118                         printf ("Error writing to the PHY\n");
119                         rcode = 1;
120                 }
121         } else {
122                 printf ("Usage:\n%s\n", cmdtp->usage);
123                 return 1;
124         }
125
126         /*
127          * Save the parameters for repeats.
128          */
129         last_op = op;
130         last_addr = addr;
131         last_data = data;
132
133         return rcode;
134 }
135
136 /***************************************************/
137
138 U_BOOT_CMD(
139         mii,    5,      1,      do_mii,
140         "mii     - MII utility commands\n",
141         "info  <addr>              - display MII PHY info\n"
142         "mii read  <addr> <reg>        - read  MII PHY <addr> register <reg>\n"
143         "mii write <addr> <reg> <data> - write MII PHY <addr> register <reg>\n"
144 );
145
146
147 #endif /* CFG_CMD_MII */