ethernut5_pwrman.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * (C) Copyright 2011
  3. * egnite GmbH <info@egnite.de>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Ethernut 5 power management support
  9. *
  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.
  16. *
  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.
  21. *
  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.
  28. *
  29. * Note, that the AVR firmware of the PMC is released under BSDL.
  30. *
  31. * For additional information visit the project home page at
  32. * http://www.ethernut.de/
  33. */
  34. #include <common.h>
  35. #include <asm/arch/at91sam9260.h>
  36. #include <asm/arch/at91_common.h>
  37. #include <asm/arch/gpio.h>
  38. #include <asm/io.h>
  39. #include <i2c.h>
  40. #include "ethernut5_pwrman.h"
  41. /* PMC firmware version */
  42. static int pwrman_major;
  43. static int pwrman_minor;
  44. /*
  45. * Enable Ethernut 5 power management.
  46. *
  47. * This function must be called during board initialization.
  48. * While we are using u-boot's I2C subsystem, it may be required
  49. * to enable the serial port before calling this function,
  50. * in particular when debugging is enabled.
  51. *
  52. * If board specific commands are not available, we will activate
  53. * all board components.
  54. */
  55. void ethernut5_power_init(void)
  56. {
  57. pwrman_minor = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VERS);
  58. pwrman_major = pwrman_minor >> 4;
  59. pwrman_minor &= 15;
  60. #ifndef CONFIG_CMD_BSP
  61. /* Do not modify anything, if we do not have a known version. */
  62. if (pwrman_major == 2) {
  63. /* Without board specific commands we enable all features. */
  64. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, ~PWRMAN_ETHRST);
  65. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
  66. }
  67. #endif
  68. }
  69. /*
  70. * Reset Ethernet PHY.
  71. *
  72. * This function allows the re-configure the PHY after
  73. * changing its strap pins.
  74. */
  75. void ethernut5_phy_reset(void)
  76. {
  77. /* Do not modify anything, if we do not have a known version. */
  78. if (pwrman_major != 2)
  79. return;
  80. /*
  81. * Make sure that the Ethernet clock is enabled and the PHY reset
  82. * is disabled for at least 100 us.
  83. */
  84. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHCLK);
  85. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
  86. udelay(100);
  87. /*
  88. * LAN8710 strap pins are
  89. * PA14 => PHY MODE0
  90. * PA15 => PHY MODE1
  91. * PA17 => PHY MODE2 => 111b all capable
  92. * PA18 => PHY ADDR0 => 0b
  93. */
  94. at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
  95. at91_set_pio_input(AT91_PIO_PORTA, 15, 1);
  96. at91_set_pio_input(AT91_PIO_PORTA, 17, 1);
  97. at91_set_pio_input(AT91_PIO_PORTA, 18, 0);
  98. /* Activate PHY reset for 100 us. */
  99. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA, PWRMAN_ETHRST);
  100. udelay(100);
  101. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS, PWRMAN_ETHRST);
  102. at91_set_pio_input(AT91_PIO_PORTA, 14, 1);
  103. }
  104. /*
  105. * Output the firmware version we got during initialization.
  106. */
  107. void ethernut5_print_version(void)
  108. {
  109. printf("%u.%u\n", pwrman_major, pwrman_minor);
  110. }
  111. /*
  112. * All code below this point is optional and implements
  113. * the 'pwrman' command.
  114. */
  115. #ifdef CONFIG_CMD_BSP
  116. /* Human readable names of PMC features */
  117. char *pwrman_feat[8] = {
  118. "board", "vbin", "vbout", "mmc",
  119. "rs232", "ethclk", "ethrst", "wakeup"
  120. };
  121. /*
  122. * Print all feature names, that have its related flags enabled.
  123. */
  124. static void print_flagged_features(u8 flags)
  125. {
  126. int i;
  127. for (i = 0; i < 8; i++) {
  128. if (flags & (1 << i))
  129. printf("%s ", pwrman_feat[i]);
  130. }
  131. }
  132. /*
  133. * Return flags of a given list of feature names.
  134. *
  135. * The function stops at the first unknown list entry and
  136. * returns the number of detected names as a function result.
  137. */
  138. static int feature_flags(char * const names[], int num, u8 *flags)
  139. {
  140. int i, j;
  141. *flags = 0;
  142. for (i = 0; i < num; i++) {
  143. for (j = 0; j < 8; j++) {
  144. if (strcmp(pwrman_feat[j], names[i]) == 0) {
  145. *flags |= 1 << j;
  146. break;
  147. }
  148. }
  149. if (j > 7)
  150. break;
  151. }
  152. return i;
  153. }
  154. void ethernut5_print_power(void)
  155. {
  156. u8 flags;
  157. int i;
  158. flags = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA);
  159. for (i = 0; i < 2; i++) {
  160. if (flags) {
  161. print_flagged_features(flags);
  162. printf("%s\n", i ? "off" : "on");
  163. }
  164. flags = ~flags;
  165. }
  166. }
  167. void ethernut5_print_celsius(void)
  168. {
  169. int val;
  170. /* Read ADC value from LM50 and return Celsius degrees. */
  171. val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_TEMP);
  172. val *= 5000; /* 100mV/degree with 5V reference */
  173. val += 128; /* 8 bit resolution */
  174. val /= 256;
  175. val -= 450; /* Celsius offset, still x10 */
  176. /* Output full degrees. */
  177. printf("%d\n", (val + 5) / 10);
  178. }
  179. void ethernut5_print_voltage(void)
  180. {
  181. int val;
  182. /* Read ADC value from divider and return voltage. */
  183. val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_VAUX);
  184. /* Resistors are 100k and 12.1k */
  185. val += 5;
  186. val *= 180948;
  187. val /= 100000;
  188. val++;
  189. /* Calculation was done in 0.1V units. */
  190. printf("%d\n", (val + 5) / 10);
  191. }
  192. /*
  193. * Process the board specific 'pwrman' command.
  194. */
  195. int do_pwrman(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  196. {
  197. u8 val;
  198. int i;
  199. if (argc == 1) {
  200. ethernut5_print_power();
  201. } else if (argc == 2 && strcmp(argv[1], "reset") == 0) {
  202. at91_set_pio_output(AT91_PIO_PORTB, 8, 1);
  203. udelay(100);
  204. at91_set_pio_output(AT91_PIO_PORTB, 8, 0);
  205. udelay(100000);
  206. } else if (argc == 2 && strcmp(argv[1], "temp") == 0) {
  207. ethernut5_print_celsius();
  208. } else if (argc == 2 && strcmp(argv[1], "vaux") == 0) {
  209. ethernut5_print_voltage();
  210. } else if (argc == 2 && strcmp(argv[1], "version") == 0) {
  211. ethernut5_print_version();
  212. } else if (strcmp(argv[1], "led") == 0) {
  213. /* Control the green status LED. Blink frequency unit
  214. ** is 0.1s, very roughly. */
  215. if (argc == 2) {
  216. /* No more arguments, output current settings. */
  217. val = i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL);
  218. printf("led %u %u\n", val >> 4, val & 15);
  219. } else {
  220. /* First argument specifies the on-time. */
  221. val = (u8) simple_strtoul(argv[2], NULL, 0);
  222. val <<= 4;
  223. if (argc > 3) {
  224. /* Second argument specifies the off-time. */
  225. val |= (u8) (simple_strtoul(argv[3], NULL, 0)
  226. & 15);
  227. }
  228. /* Update the LED control register. */
  229. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_LEDCTL, val);
  230. }
  231. } else {
  232. /* We expect a list of features followed an optional status. */
  233. argc--;
  234. i = feature_flags(&argv[1], argc, &val);
  235. if (argc == i) {
  236. /* We got a list only, print status. */
  237. val &= i2c_reg_read(PWRMAN_I2C_ADDR, PWRMAN_REG_STA);
  238. if (val) {
  239. if (i > 1)
  240. print_flagged_features(val);
  241. printf("active\n");
  242. } else {
  243. printf("inactive\n");
  244. }
  245. } else {
  246. /* More arguments. */
  247. if (i == 0) {
  248. /* No given feature, use despensibles. */
  249. val = PWRMAN_DISPENSIBLE;
  250. }
  251. if (strcmp(argv[i + 1], "on") == 0) {
  252. /* Enable features. */
  253. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_ENA,
  254. val);
  255. } else if (strcmp(argv[i + 1], "off") == 0) {
  256. /* Disable features. */
  257. i2c_reg_write(PWRMAN_I2C_ADDR, PWRMAN_REG_DIS,
  258. val);
  259. } else {
  260. printf("Bad parameter %s\n", argv[i + 1]);
  261. return 1;
  262. }
  263. }
  264. }
  265. return 0;
  266. }
  267. U_BOOT_CMD(
  268. pwrman, CONFIG_SYS_MAXARGS, 1, do_pwrman,
  269. "power management",
  270. "- print settings\n"
  271. "pwrman feature ...\n"
  272. " - print status\n"
  273. "pwrman [feature ...] on|off\n"
  274. " - enable/disable specified or all dispensible features\n"
  275. "pwrman led [on-time [off-time]]\n"
  276. " - print or set led blink timer\n"
  277. "pwrman temp\n"
  278. " - print board temperature (Celsius)\n"
  279. "pwrman vaux\n"
  280. " - print auxiliary input voltage\n"
  281. "pwrman reset\n"
  282. " - reset power management controller\n"
  283. "pwrman version\n"
  284. " - print firmware version\n"
  285. "\n"
  286. " features, (*)=dispensible:\n"
  287. " board - 1.8V and 3.3V supply\n"
  288. " vbin - supply via USB device connector\n"
  289. " vbout - USB host connector supply(*)\n"
  290. " mmc - MMC slot supply(*)\n"
  291. " rs232 - RS232 driver\n"
  292. " ethclk - Ethernet PHY clock(*)\n"
  293. " ethrst - Ethernet PHY reset\n"
  294. " wakeup - RTC alarm"
  295. );
  296. #endif /* CONFIG_CMD_BSP */