1 // SPDX-License-Identifier: GPL-2.0+
4 * egnite GmbH <info@egnite.de>
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>
41 #include <linux/delay.h>
43 #include "ethernut5_pwrman.h"
45 /* PMC firmware version */
46 static int pwrman_major;
47 static int pwrman_minor;
50 * Enable Ethernut 5 power management.
52 * This function must be called during board initialization.
53 * While we are using u-boot's I2C subsystem, it may be required
54 * to enable the serial port before calling this function,
55 * in particular when debugging is enabled.
57 * If board specific commands are not available, we will activate
58 * all board components.
60 void ethernut5_power_init(void)
62 pwrman_minor = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VERS);
63 pwrman_major = pwrman_minor >> 4;
66 #ifndef CONFIG_CMD_BSP
67 /* Do not modify anything, if we do not have a known version. */
68 if (pwrman_major == 2) {
69 /* Without board specific commands we enable all features. */
70 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, ~PWRMAN_ETHRST);
71 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
79 * This function allows the re-configure the PHY after
80 * changing its strap pins.
82 void ethernut5_phy_reset(void)
84 /* Do not modify anything, if we do not have a known version. */
85 if (pwrman_major != 2)
89 * Make sure that the Ethernet clock is enabled and the PHY reset
90 * is disabled for at least 100 us.
92 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHCLK);
93 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
97 * LAN8710 strap pins are
100 * PA17 => PHY MODE2 => 111b all capable
101 * PA18 => PHY ADDR0 => 0b
103 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
104 at91_set_pio_input(AT91_PIO_PORTA, 15, 1);
105 at91_set_pio_input(AT91_PIO_PORTA, 17, 1);
106 at91_set_pio_input(AT91_PIO_PORTA, 18, 0);
108 /* Activate PHY reset for 100 us. */
109 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHRST);
111 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
113 at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
117 * Output the firmware version we got during initialization.
119 void ethernut5_print_version(void)
121 printf("%u.%u\n", pwrman_major, pwrman_minor);
125 * All code below this point is optional and implements
126 * the 'pwrman' command.
128 #ifdef CONFIG_CMD_BSP
130 /* Human readable names of PMC features */
131 char *pwrman_feat[8] = {
132 "board", "vbin", "vbout", "mmc",
133 "rs232", "ethclk", "ethrst", "wakeup"
137 * Print all feature names, that have its related flags enabled.
139 static void print_flagged_features(u8 flags)
143 for (i = 0; i < 8; i++) {
144 if (flags & (1 << i))
145 printf("%s ", pwrman_feat[i]);
150 * Return flags of a given list of feature names.
152 * The function stops at the first unknown list entry and
153 * returns the number of detected names as a function result.
155 static int feature_flags(char * const names[], int num, u8 *flags)
160 for (i = 0; i < num; i++) {
161 for (j = 0; j < 8; j++) {
162 if (strcmp(pwrman_feat[j], names[i]) == 0) {
173 void ethernut5_print_power(void)
178 flags = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA);
179 for (i = 0; i < 2; i++) {
181 print_flagged_features(flags);
182 printf("%s\n", i ? "off" : "on");
188 void ethernut5_print_celsius(void)
192 /* Read ADC value from LM50 and return Celsius degrees. */
193 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_TEMP);
194 val *= 5000; /* 100mV/degree with 5V reference */
195 val += 128; /* 8 bit resolution */
197 val -= 450; /* Celsius offset, still x10 */
198 /* Output full degrees. */
199 printf("%d\n", (val + 5) / 10);
202 void ethernut5_print_voltage(void)
206 /* Read ADC value from divider and return voltage. */
207 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VAUX);
208 /* Resistors are 100k and 12.1k */
213 /* Calculation was done in 0.1V units. */
214 printf("%d\n", (val + 5) / 10);
218 * Process the board specific 'pwrman' command.
220 int do_pwrman(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
226 ethernut5_print_power();
227 } else if (argc == 2 && strcmp(argv[1], "reset") == 0) {
228 at91_set_pio_output(AT91_PIO_PORTB, 8, 1);
230 at91_set_pio_output(AT91_PIO_PORTB, 8, 0);
232 } else if (argc == 2 && strcmp(argv[1], "temp") == 0) {
233 ethernut5_print_celsius();
234 } else if (argc == 2 && strcmp(argv[1], "vaux") == 0) {
235 ethernut5_print_voltage();
236 } else if (argc == 2 && strcmp(argv[1], "version") == 0) {
237 ethernut5_print_version();
238 } else if (strcmp(argv[1], "led") == 0) {
239 /* Control the green status LED. Blink frequency unit
240 ** is 0.1s, very roughly. */
242 /* No more arguments, output current settings. */
243 val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL);
244 printf("led %u %u\n", val >> 4, val & 15);
246 /* First argument specifies the on-time. */
247 val = (u8) simple_strtoul(argv[2], NULL, 0);
250 /* Second argument specifies the off-time. */
251 val |= (u8) (simple_strtoul(argv[3], NULL, 0)
254 /* Update the LED control register. */
255 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL, val);
258 /* We expect a list of features followed an optional status. */
260 i = feature_flags(&argv[1], argc, &val);
262 /* We got a list only, print status. */
263 val &= i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_STA);
266 print_flagged_features(val);
269 printf("inactive\n");
272 /* More arguments. */
274 /* No given feature, use despensibles. */
275 val = PWRMAN_DISPENSIBLE;
277 if (strcmp(argv[i + 1], "on") == 0) {
278 /* Enable features. */
279 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA,
281 } else if (strcmp(argv[i + 1], "off") == 0) {
282 /* Disable features. */
283 i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS,
286 printf("Bad parameter %s\n", argv[i + 1]);
295 pwrman, CONFIG_SYS_MAXARGS, 1, do_pwrman,
298 "pwrman feature ...\n"
300 "pwrman [feature ...] on|off\n"
301 " - enable/disable specified or all dispensible features\n"
302 "pwrman led [on-time [off-time]]\n"
303 " - print or set led blink timer\n"
305 " - print board temperature (Celsius)\n"
307 " - print auxiliary input voltage\n"
309 " - reset power management controller\n"
311 " - print firmware version\n"
313 " features, (*)=dispensible:\n"
314 " board - 1.8V and 3.3V supply\n"
315 " vbin - supply via USB device connector\n"
316 " vbout - USB host connector supply(*)\n"
317 " mmc - MMC slot supply(*)\n"
318 " rs232 - RS232 driver\n"
319 " ethclk - Ethernet PHY clock(*)\n"
320 " ethrst - Ethernet PHY reset\n"
321 " wakeup - RTC alarm"
323 #endif /* CONFIG_CMD_BSP */