1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include <common.h>
52#include <asm/arch/at91sam9260.h>
53#include <asm/arch/at91_common.h>
54#include <asm/arch/gpio.h>
55#include <asm/io.h>
56#include <i2c.h>
57
58#include "ethernut5_pwrman.h"
59
60
61static int pwrman_major;
62static int pwrman_minor;
63
64
65
66
67
68
69
70
71
72
73
74
75void ethernut5_power_init(void)
76{
77 pwrman_minor = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VERS);
78 pwrman_major = pwrman_minor >> 4;
79 pwrman_minor &= 15;
80
81#ifndef CONFIG_CMD_BSP
82
83 if (pwrman_major == 2) {
84
85 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, ~PWRMAN_ETHRST);
86 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
87 }
88#endif
89}
90
91
92
93
94
95
96
97void ethernut5_phy_reset(void)
98{
99
100 if (pwrman_major != 2)
101 return;
102
103
104
105
106
107 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHCLK);
108 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
109 udelay(100);
110
111
112
113
114
115
116
117
118 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
119 at91_set_pio_input(AT91_PIO_PORTA, 15, 1);
120 at91_set_pio_input(AT91_PIO_PORTA, 17, 1);
121 at91_set_pio_input(AT91_PIO_PORTA, 18, 0);
122
123
124 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHRST);
125 udelay(100);
126 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
127
128 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
129}
130
131
132
133
134void ethernut5_print_version(void)
135{
136 printf("%u.%u\n", pwrman_major, pwrman_minor);
137}
138
139
140
141
142
143#ifdef CONFIG_CMD_BSP
144
145
146char *pwrman_feat[8] = {
147 "board", "vbin", "vbout", "mmc",
148 "rs232", "ethclk", "ethrst", "wakeup"
149};
150
151
152
153
154static void print_flagged_features(u8 flags)
155{
156 int i;
157
158 for (i = 0; i < 8; i++) {
159 if (flags & (1 << i))
160 printf("%s ", pwrman_feat[i]);
161 }
162}
163
164
165
166
167
168
169
170static int feature_flags(char * const names[], int num, u8 *flags)
171{
172 int i, j;
173
174 *flags = 0;
175 for (i = 0; i < num; i++) {
176 for (j = 0; j < 8; j++) {
177 if (strcmp(pwrman_feat[j], names[i]) == 0) {
178 *flags |= 1 << j;
179 break;
180 }
181 }
182 if (j > 7)
183 break;
184 }
185 return i;
186}
187
188void ethernut5_print_power(void)
189{
190 u8 flags;
191 int i;
192
193 flags = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA);
194 for (i = 0; i < 2; i++) {
195 if (flags) {
196 print_flagged_features(flags);
197 printf("%s\n", i ? "off" : "on");
198 }
199 flags = ~flags;
200 }
201}
202
203void ethernut5_print_celsius(void)
204{
205 int val;
206
207
208 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_TEMP);
209 val *= 5000;
210 val += 128;
211 val /= 256;
212 val -= 450;
213
214 printf("%d\n", (val + 5) / 10);
215}
216
217void ethernut5_print_voltage(void)
218{
219 int val;
220
221
222 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VAUX);
223
224 val += 5;
225 val *= 180948;
226 val /= 100000;
227 val++;
228
229 printf("%d\n", (val + 5) / 10);
230}
231
232
233
234
235int do_pwrman(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
236{
237 u8 val;
238 int i;
239
240 if (argc == 1) {
241 ethernut5_print_power();
242 } else if (argc == 2 && strcmp(argv[1], "reset") == 0) {
243 at91_set_pio_output(AT91_PIO_PORTB, 8, 1);
244 udelay(100);
245 at91_set_pio_output(AT91_PIO_PORTB, 8, 0);
246 udelay(100000);
247 } else if (argc == 2 && strcmp(argv[1], "temp") == 0) {
248 ethernut5_print_celsius();
249 } else if (argc == 2 && strcmp(argv[1], "vaux") == 0) {
250 ethernut5_print_voltage();
251 } else if (argc == 2 && strcmp(argv[1], "version") == 0) {
252 ethernut5_print_version();
253 } else if (strcmp(argv[1], "led") == 0) {
254
255
256 if (argc == 2) {
257
258 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL);
259 printf("led %u %u\n", val >> 4, val & 15);
260 } else {
261
262 val = (u8) simple_strtoul(argv[2], NULL, 0);
263 val <<= 4;
264 if (argc > 3) {
265
266 val |= (u8) (simple_strtoul(argv[3], NULL, 0)
267 & 15);
268 }
269
270 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL, val);
271 }
272 } else {
273
274 argc--;
275 i = feature_flags(&argv[1], argc, &val);
276 if (argc == i) {
277
278 val &= i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_STA);
279 if (val) {
280 if (i > 1)
281 print_flagged_features(val);
282 printf("active\n");
283 } else {
284 printf("inactive\n");
285 }
286 } else {
287
288 if (i == 0) {
289
290 val = PWRMAN_DISPENSIBLE;
291 }
292 if (strcmp(argv[i + 1], "on") == 0) {
293
294 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA,
295 val);
296 } else if (strcmp(argv[i + 1], "off") == 0) {
297
298 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS,
299 val);
300 } else {
301 printf("Bad parameter %s\n", argv[i + 1]);
302 return 1;
303 }
304 }
305 }
306 return 0;
307}
308
309U_BOOT_CMD(
310 pwrman, CONFIG_SYS_MAXARGS, 1, do_pwrman,
311 "power management",
312 "- print settings\n"
313 "pwrman feature ...\n"
314 " - print status\n"
315 "pwrman [feature ...] on|off\n"
316 " - enable/disable specified or all dispensible features\n"
317 "pwrman led [on-time [off-time]]\n"
318 " - print or set led blink timer\n"
319 "pwrman temp\n"
320 " - print board temperature (Celsius)\n"
321 "pwrman vaux\n"
322 " - print auxiliary input voltage\n"
323 "pwrman reset\n"
324 " - reset power management controller\n"
325 "pwrman version\n"
326 " - print firmware version\n"
327 "\n"
328 " features, (*)=dispensible:\n"
329 " board - 1.8V and 3.3V supply\n"
330 " vbin - supply via USB device connector\n"
331 " vbout - USB host connector supply(*)\n"
332 " mmc - MMC slot supply(*)\n"
333 " rs232 - RS232 driver\n"
334 " ethclk - Ethernet PHY clock(*)\n"
335 " ethrst - Ethernet PHY reset\n"
336 " wakeup - RTC alarm"
337);
338#endif
339