4d68cc6b14ab8c9450aa228597c16e57ece2e03b
[openwrt/svn-archive/archive.git] / package / ifxmips_adsl / src / cmvwrite.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 */
16 //-----------------------------------------------------------------------
17 //Description:
18 // read the CMV register under Linux for Amazon
19 //-----------------------------------------------------------------------
20 //Author: Joe.Lin@infineon.com
21 //Created: 31-December-2004
22 //-----------------------------------------------------------------------
23 /* History
24 * Last changed on:
25 * 507051:linmars 2005/07/5 fix makeCMV problem
26 * Last changed by:
27 *
28 */
29
30 #define _IFXMIPS_ADSL_APP
31 //#define DEBUG
32 #define u32 unsigned int
33 #define u16 unsigned short
34 #define u8 unsigned char
35 #define IFXMIPS_MEI_DEV "/dev/ifxmips/mei"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <time.h>
43 #include <getopt.h>
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/mman.h>
49 #include <asm/ifxmips/ifxmips_mei_app_ioctl.h>
50 #include <asm/ifxmips/ifxmips_mei_app.h>
51 #include <asm/ifxmips/ifxmips_mei_ioctl.h>
52 #include <asm/ifxmips/ifxmips_mei.h>
53
54 #ifdef IFX_MULTILIB_UTIL
55 #define main cmvwrite_main
56 #define display_version cmvwrite_display_version
57 #endif
58
59 /*============================definitions======================*/
60 #define OPTN 5
61 #define CNFG 8
62 #define CNTL 1
63 #define STAT 2
64 #define RATE 6
65 #define PLAM 7
66 #define INFO 3
67 #define TEST 4
68
69 typedef unsigned short UINT16;
70 typedef unsigned long UINT32;
71
72 /*=============================================================*/
73
74
75 /*=============================global variables================*/
76 #ifdef IFX_MULTILIB_UTIL
77 extern int c;
78 extern int input_flag;
79 extern int digit_optind;
80 extern FILE* script_file;
81 extern void (*func)();
82 extern int fd;
83
84 UINT16 var16[8];
85 UINT32 var32[8];
86 UINT16 Message[16];
87 #else
88 int c=0;
89 int input_flag=0;
90 int digit_optind=0;
91 FILE* script_file;
92 void (*func)()=NULL;
93 int fd;
94
95 UINT16 var16[8];
96 UINT32 var32[8];
97 UINT16 Message[16];
98 #endif
99 /*=============================================================*/
100 /*165001:henryhsu 2005/9/7 Modify some error in cmvwrite utility*/
101 int ifx_makeCMV(unsigned char opcode, unsigned char group, unsigned short address, unsigned short index, int size, unsigned short * data, unsigned short *Message, int msg_len)
102 {
103 if (msg_len < 16*2)
104 return -1;
105 memset(Message, 0, 16*2);
106 Message[0]= (opcode<<4) + (size&0xf);
107 if(opcode == H2D_DEBUG_WRITE_DM)
108 Message[1]= (group&0x7f);
109 else
110 Message[1]= (((index==0)?0:1)<<7) + (group&0x7f);
111 Message[2]= address;
112 Message[3]= index;
113 if((opcode == H2D_CMV_WRITE)||(opcode == H2D_DEBUG_WRITE_DM))
114 memcpy(Message+4, data, size*2);
115
116 return 0;
117 }
118
119
120 void display_version()
121 {
122 printf("adsl cmv write version1.0\nby Joe Lin \nJoe.Lin@infineon.com\n");
123 return;
124 }
125
126 //165001:henryhsu 2005/9/7 No need to check this any more
127 //#ifndef IFX_MULTILIB_UTIL
128
129 //void cmvreader_help()
130 void cmvwrite_help()
131 {
132 printf("Usage:cmvwrite [options] [group name][address][index][data] ...\n");
133 printf("options:\n");
134 printf(" -h --help Display help information\n");
135 printf(" -v --version Display version information\n");
136 printf("group name: --group name of CMV to read\n");
137 printf(" OPTN -- CMV Group 5 \n");
138 printf(" CNFG -- CMV Group 8 \n");
139 printf(" CNTL -- CMV Group 1 \n");
140 printf(" STAT -- CMV Group 2 \n");
141 printf(" RATE -- CMV Group 6 \n");
142 printf(" PLAM -- CMV Group 7 \n");
143 printf(" INFO -- CMV Group 3 \n");
144 printf(" TEST -- CMV Group 4 \n");
145 printf("address --address value of CMV to write\n");
146 printf("index --index value of CMV to write\n");
147 printf("data --data to write in Hex.\n");
148
149 return;
150 }
151
152 int main (int argc, char** argv) {
153
154 char *endptr;
155 unsigned short value;
156 if (argc < 2)
157 {
158 cmvwrite_help();
159 return;
160 }
161
162 if (strstr(argv[1], "-h") != NULL){
163 cmvwrite_help();
164 return;
165 }
166
167 if (strstr(argv[1], "-v") != NULL){
168 display_version();
169 return;
170 }
171
172
173 fd=open(IFXMIPS_MEI_DEV, O_RDWR);
174 if(fd<0){
175 printf("\n\n autoboot open device fail\n");
176 return -1;
177 }
178
179
180
181 int group=0,address,index,size;
182 if((strcmp(argv[1],"optn")==0)||(strcmp(argv[1],"OPTN")==0))
183 group=OPTN;
184 else if((strcmp(argv[1],"cnfg")==0)||(strcmp(argv[1],"CNFG")==0))
185 group=CNFG;
186 else if((strcmp(argv[1],"cntl")==0)||(strcmp(argv[1],"CNTL")==0))
187 group=CNTL;
188 else if((strcmp(argv[1],"stat")==0)||(strcmp(argv[1],"STAT")==0))
189 group=STAT;
190 else if((strcmp(argv[1],"rate")==0)||(strcmp(argv[1],"RATE")==0))
191 group=RATE;
192 else if((strcmp(argv[1],"plam")==0)||(strcmp(argv[1],"PLAM")==0))
193 group=PLAM;
194 else if((strcmp(argv[1],"info")==0)||(strcmp(argv[1],"INFO")==0))
195 group=INFO;
196 else if((strcmp(argv[1],"test")==0)||(strcmp(argv[1],"TEST")==0))
197 group=TEST;
198 else
199 {
200 printf("wrong group type!\nplease slect group:OPTN CNFG CNTL STAT RATE PLAM INFO TEST \n");
201 close(fd);
202 exit(0);
203 }
204
205 address = strtoul(argv[2], &endptr, 10);
206 index = strtoul(argv[3], &endptr, 10);
207
208 value = strtoul(argv[4],NULL,0);
209 ifx_makeCMV(H2D_CMV_WRITE, group, address, index, 1, &value, Message, sizeof(Message));
210 if(ioctl(fd, IFXMIPS_MEI_CMV_WINHOST, &Message)<0){
211 printf("cr read %d %d %d fail",group,address,index);
212 close(fd);
213 exit(0);
214 }
215
216 printf ("write %s %d %d 0x%X\n",argv[1],address,index,value);
217
218 close(fd);
219 return 0;
220 }
221