1 // SPDX-License-Identifier: GPL-2.0+
8 * Ethernut 5 power management support
10 * This board may be supplied via USB, IEEE 802.3af PoE or an
11 * auxiliary DC input. An on-board ATmega168 microcontroller,
12 * the so called power management controller or PMC, is used
13 * to select the supply source and to switch on and off certain
14 * energy consuming board components. This allows to reduce the
15 * total stand-by consumption to less than 70mW.
17 * The main CPU communicates with the PMC via I2C. When
18 * CONFIG_CMD_BSP is defined in the board configuration file,
19 * then the board specific command 'pwrman' becomes available,
20 * which allows to manually deal with the PMC.
22 * Two distinct registers are provided by the PMC for enabling
23 * and disabling specific features. This avoids the often seen
24 * read-modify-write cycle or shadow register requirement.
25 * Additional registers are available to query the board
26 * status and temperature, the auxiliary voltage and to control
27 * the green user LED that is integrated in the reset switch.
29 * Note, that the AVR firmware of the PMC is released under BSDL.
31 * For additional information visit the project home page at
32 * http://www.ethernut.de/
36 #include <asm/arch/at91sam9260.h>
37 #include <asm/arch/at91_common.h>
38 #include <asm/arch/gpio.h>
42 #include "ethernut5_pwrman.h"
44 /* PMC firmware version */
45 static int pwrman_major;
46 static int pwrman_minor;
49 * Enable Ethernut 5 power management.
51 * This function must be called during board initialization.
52 * While we are using u-boot's I2C subsystem, it may be required
53 * to enable the serial port before calling this function,
54 * in particular when debugging is enabled.
56 * If board specific commands are not available, we will activate
57 * all board components.
59 void ethernut5_power_init(void)
61 pwrman_minor = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VERS);
62 pwrman_major = pwrman_minor >> 4;
65 #ifndef CONFIG_CMD_BSP
66 /* Do not modify anything, if we do not have a known version. */
67 if (pwrman_major == 2) {
68 /* Without board specific commands we enable all features. */
69 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, ~PWRMAN_ETHRST);
70 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
78 * This function allows the re-configure the PHY after
79 * changing its strap pins.
81 void ethernut5_phy_reset(void)
83 /* Do not modify anything, if we do not have a known version. */
84 if (pwrman_major != 2)
88 * Make sure that the Ethernet clock is enabled and the PHY reset
89 * is disabled for at least 100 us.
91 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHCLK);
92 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
96 * LAN8710 strap pins are
99 * PA17 => PHY MODE2 => 111b all capable
100 * PA18 => PHY ADDR0 => 0b
102 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
103 at91_set_pio_input(AT91_PIO_PORTA, 15, 1);
104 at91_set_pio_input(AT91_PIO_PORTA, 17, 1);
105 at91_set_pio_input(AT91_PIO_PORTA, 18, 0);
107 /* Activate PHY reset for 100 us. */
108 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHRST);
110 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
112 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
116 * Output the firmware version we got during initialization.
118 void ethernut5_print_version(void)
120 printf("%u.%u\n", pwrman_major, pwrman_minor);
124 * All code below this point is optional and implements
125 * the 'pwrman' command.
127 #ifdef CONFIG_CMD_BSP
129 /* Human readable names of PMC features */
130 char *pwrman_feat[8] = {
131 "board", "vbin", "vbout", "mmc",
132 "rs232", "ethclk", "ethrst", "wakeup"
136 * Print all feature names, that have its related flags enabled.
138 static void print_flagged_features(u8 flags)
142 for (i = 0; i < 8; i++) {
143 if (flags & (1 << i))
144 printf("%s ", pwrman_feat[i]);
149 * Return flags of a given list of feature names.
151 * The function stops at the first unknown list entry and
152 * returns the number of detected names as a function result.
154 static int feature_flags(char * const names[], int num, u8 *flags)
159 for (i = 0; i < num; i++) {
160 for (j = 0; j < 8; j++) {
161 if (strcmp(pwrman_feat[j], names[i]) == 0) {
172 void ethernut5_print_power(void)
177 flags = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA);
178 for (i = 0; i < 2; i++) {
180 print_flagged_features(flags);
181 printf("%s\n", i ? "off" : "on");
187 void ethernut5_print_celsius(void)
191 /* Read ADC value from LM50 and return Celsius degrees. */
192 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_TEMP);
193 val *= 5000; /* 100mV/degree with 5V reference */
194 val += 128; /* 8 bit resolution */
196 val -= 450; /* Celsius offset, still x10 */
197 /* Output full degrees. */
198 printf("%d\n", (val + 5) / 10);
201 void ethernut5_print_voltage(void)
205 /* Read ADC value from divider and return voltage. */
206 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VAUX);
207 /* Resistors are 100k and 12.1k */
212 /* Calculation was done in 0.1V units. */
213 printf("%d\n", (val + 5) / 10);
217 * Process the board specific 'pwrman' command.
219 int do_pwrman(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
225 ethernut5_print_power();
226 } else if (argc == 2 && strcmp(argv[1], "reset") == 0) {
227 at91_set_pio_output(AT91_PIO_PORTB, 8, 1);
229 at91_set_pio_output(AT91_PIO_PORTB, 8, 0);
231 } else if (argc == 2 && strcmp(argv[1], "temp") == 0) {
232 ethernut5_print_celsius();
233 } else if (argc == 2 && strcmp(argv[1], "vaux") == 0) {
234 ethernut5_print_voltage();
235 } else if (argc == 2 && strcmp(argv[1], "version") == 0) {
236 ethernut5_print_version();
237 } else if (strcmp(argv[1], "led") == 0) {
238 /* Control the green status LED. Blink frequency unit
239 ** is 0.1s, very roughly. */
241 /* No more arguments, output current settings. */
242 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL);
243 printf("led %u %u\n", val >> 4, val & 15);
245 /* First argument specifies the on-time. */
246 val = (u8) simple_strtoul(argv[2], NULL, 0);
249 /* Second argument specifies the off-time. */
250 val |= (u8) (simple_strtoul(argv[3], NULL, 0)
253 /* Update the LED control register. */
254 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL, val);
257 /* We expect a list of features followed an optional status. */
259 i = feature_flags(&argv[1], argc, &val);
261 /* We got a list only, print status. */
262 val &= i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_STA);
265 print_flagged_features(val);
268 printf("inactive\n");
271 /* More arguments. */
273 /* No given feature, use despensibles. */
274 val = PWRMAN_DISPENSIBLE;
276 if (strcmp(argv[i + 1], "on") == 0) {
277 /* Enable features. */
278 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA,
280 } else if (strcmp(argv[i + 1], "off") == 0) {
281 /* Disable features. */
282 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS,
285 printf("Bad parameter %s\n", argv[i + 1]);
294 pwrman, CONFIG_SYS_MAXARGS, 1, do_pwrman,
297 "pwrman feature ...\n"
299 "pwrman [feature ...] on|off\n"
300 " - enable/disable specified or all dispensible features\n"
301 "pwrman led [on-time [off-time]]\n"
302 " - print or set led blink timer\n"
304 " - print board temperature (Celsius)\n"
306 " - print auxiliary input voltage\n"
308 " - reset power management controller\n"
310 " - print firmware version\n"
312 " features, (*)=dispensible:\n"
313 " board - 1.8V and 3.3V supply\n"
314 " vbin - supply via USB device connector\n"
315 " vbout - USB host connector supply(*)\n"
316 " mmc - MMC slot supply(*)\n"
317 " rs232 - RS232 driver\n"
318 " ethclk - Ethernet PHY clock(*)\n"
319 " ethrst - Ethernet PHY reset\n"
320 " wakeup - RTC alarm"
322 #endif /* CONFIG_CMD_BSP */