board.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Common board functions for siemens AM335X based boards
  4. * (C) Copyright 2013 Siemens Schweiz AG
  5. * (C) Heiko Schocher, DENX Software Engineering, hs@denx.de.
  6. *
  7. * Based on:
  8. * U-Boot file:/board/ti/am335x/board.c
  9. * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
  10. */
  11. #include <common.h>
  12. #include <env.h>
  13. #include <errno.h>
  14. #include <malloc.h>
  15. #include <serial.h>
  16. #include <spl.h>
  17. #include <asm/arch/cpu.h>
  18. #include <asm/arch/hardware.h>
  19. #include <asm/arch/omap.h>
  20. #include <asm/arch/ddr_defs.h>
  21. #include <asm/arch/clock.h>
  22. #include <asm/arch/gpio.h>
  23. #include <asm/arch/mmc_host_def.h>
  24. #include <asm/arch/sys_proto.h>
  25. #include <asm/io.h>
  26. #include <asm/emif.h>
  27. #include <asm/gpio.h>
  28. #include <i2c.h>
  29. #include <miiphy.h>
  30. #include <cpsw.h>
  31. #include <watchdog.h>
  32. #include <asm/mach-types.h>
  33. #include "../common/factoryset.h"
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #ifdef CONFIG_SPL_BUILD
  36. void set_uart_mux_conf(void)
  37. {
  38. enable_uart0_pin_mux();
  39. }
  40. void set_mux_conf_regs(void)
  41. {
  42. /* Initalize the board header */
  43. enable_i2c0_pin_mux();
  44. i2c_set_bus_num(0);
  45. /* enable early the console */
  46. gd->baudrate = CONFIG_BAUDRATE;
  47. serial_init();
  48. gd->have_console = 1;
  49. if (read_eeprom() < 0)
  50. puts("Could not get board ID.\n");
  51. enable_board_pin_mux();
  52. }
  53. void sdram_init(void)
  54. {
  55. spl_siemens_board_init();
  56. board_init_ddr();
  57. return;
  58. }
  59. #endif /* #ifdef CONFIG_SPL_BUILD */
  60. #ifndef CONFIG_SPL_BUILD
  61. /*
  62. * Basic board specific setup. Pinmux has been handled already.
  63. */
  64. int board_init(void)
  65. {
  66. #if defined(CONFIG_HW_WATCHDOG)
  67. hw_watchdog_init();
  68. #endif /* defined(CONFIG_HW_WATCHDOG) */
  69. i2c_set_bus_num(0);
  70. if (read_eeprom() < 0)
  71. puts("Could not get board ID.\n");
  72. #ifdef CONFIG_MACH_TYPE
  73. gd->bd->bi_arch_number = CONFIG_MACH_TYPE;
  74. #endif
  75. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  76. #ifdef CONFIG_FACTORYSET
  77. factoryset_read_eeprom(CONFIG_SYS_I2C_EEPROM_ADDR);
  78. #endif
  79. gpmc_init();
  80. #ifdef CONFIG_NAND_CS_INIT
  81. board_nand_cs_init();
  82. #endif
  83. #ifdef CONFIG_VIDEO
  84. board_video_init();
  85. #endif
  86. return 0;
  87. }
  88. #endif /* #ifndef CONFIG_SPL_BUILD */
  89. #define OSC (V_OSCK/1000000)
  90. const struct dpll_params dpll_ddr = {
  91. DDR_PLL_FREQ, OSC-1, 1, -1, -1, -1, -1};
  92. const struct dpll_params *get_dpll_ddr_params(void)
  93. {
  94. return &dpll_ddr;
  95. }
  96. #ifndef CONFIG_SPL_BUILD
  97. #define MAX_NR_LEDS 10
  98. #define MAX_PIN_NUMBER 128
  99. #define STARTUP 0
  100. #if defined(BOARD_DFU_BUTTON_GPIO)
  101. unsigned char get_button_state(char * const envname, unsigned char def)
  102. {
  103. int button = 0;
  104. int gpio;
  105. char *ptr_env;
  106. /* If button is not found we take default */
  107. ptr_env = env_get(envname);
  108. if (NULL == ptr_env) {
  109. gpio = def;
  110. } else {
  111. gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
  112. if (gpio > MAX_PIN_NUMBER)
  113. gpio = def;
  114. }
  115. gpio_request(gpio, "");
  116. gpio_direction_input(gpio);
  117. if (gpio_get_value(gpio))
  118. button = 1;
  119. else
  120. button = 0;
  121. gpio_free(gpio);
  122. return button;
  123. }
  124. /**
  125. * This command returns the status of the user button on
  126. * Input - none
  127. * Returns - 1 if button is held down
  128. * 0 if button is not held down
  129. */
  130. static int
  131. do_userbutton(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  132. {
  133. int button = 0;
  134. button = get_button_state("button_dfu0", BOARD_DFU_BUTTON_GPIO);
  135. button |= get_button_state("button_dfu1", BOARD_DFU_BUTTON_GPIO);
  136. return button;
  137. }
  138. U_BOOT_CMD(
  139. dfubutton, CONFIG_SYS_MAXARGS, 1, do_userbutton,
  140. "Return the status of the DFU button",
  141. ""
  142. );
  143. #endif
  144. static int
  145. do_usertestwdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  146. {
  147. printf("\n\n\n Go into infinite loop\n\n\n");
  148. while (1)
  149. ;
  150. return 0;
  151. };
  152. U_BOOT_CMD(
  153. testwdt, CONFIG_SYS_MAXARGS, 1, do_usertestwdt,
  154. "Sends U-Boot into infinite loop",
  155. ""
  156. );
  157. /**
  158. * Get led gpios from env and set them.
  159. * The led define in environment need to need to be of the form ledN=NN,S0,S1
  160. * where N is an unsigned integer from 0 to 9 and S0 and S1 is 0 or 1. S0
  161. * defines the startup state of the led, S1 the special state of the led when
  162. * it enters e.g. dfu mode.
  163. */
  164. void set_env_gpios(unsigned char state)
  165. {
  166. char *ptr_env;
  167. char str_tmp[5]; /* must contain "ledX"*/
  168. unsigned char i, idx, pos1, pos2, ccount;
  169. unsigned char gpio_n, gpio_s0, gpio_s1;
  170. for (i = 0; i < MAX_NR_LEDS; i++) {
  171. sprintf(str_tmp, "led%d", i);
  172. /* If env var is not found we stop */
  173. ptr_env = env_get(str_tmp);
  174. if (NULL == ptr_env)
  175. break;
  176. /* Find sperators position */
  177. pos1 = 0;
  178. pos2 = 0;
  179. ccount = 0;
  180. for (idx = 0; ptr_env[idx] != '\0'; idx++) {
  181. if (ptr_env[idx] == ',') {
  182. if (ccount++ < 1)
  183. pos1 = idx;
  184. else
  185. pos2 = idx;
  186. }
  187. }
  188. /* Bad led description skip this definition */
  189. if (pos2 <= pos1 || ccount > 2)
  190. continue;
  191. /* Get pin number and request gpio */
  192. memset(str_tmp, 0, sizeof(str_tmp));
  193. strncpy(str_tmp, ptr_env, pos1*sizeof(char));
  194. gpio_n = (unsigned char)simple_strtoul(str_tmp, NULL, 0);
  195. /* Invalid gpio number skip definition */
  196. if (gpio_n > MAX_PIN_NUMBER)
  197. continue;
  198. gpio_request(gpio_n, "");
  199. if (state == STARTUP) {
  200. /* get pin state 0 and set */
  201. memset(str_tmp, 0, sizeof(str_tmp));
  202. strncpy(str_tmp, ptr_env+pos1+1,
  203. (pos2-pos1-1)*sizeof(char));
  204. gpio_s0 = (unsigned char)simple_strtoul(str_tmp, NULL,
  205. 0);
  206. gpio_direction_output(gpio_n, gpio_s0);
  207. } else {
  208. /* get pin state 1 and set */
  209. memset(str_tmp, 0, sizeof(str_tmp));
  210. strcpy(str_tmp, ptr_env+pos2+1);
  211. gpio_s1 = (unsigned char)simple_strtoul(str_tmp, NULL,
  212. 0);
  213. gpio_direction_output(gpio_n, gpio_s1);
  214. }
  215. } /* loop through defined led in environment */
  216. }
  217. static int do_board_led(cmd_tbl_t *cmdtp, int flag, int argc,
  218. char *const argv[])
  219. {
  220. if (argc != 2)
  221. return CMD_RET_USAGE;
  222. if ((unsigned char)simple_strtoul(argv[1], NULL, 0) == STARTUP)
  223. set_env_gpios(0);
  224. else
  225. set_env_gpios(1);
  226. return 0;
  227. };
  228. U_BOOT_CMD(
  229. draco_led, CONFIG_SYS_MAXARGS, 2, do_board_led,
  230. "Set LEDs defined in environment",
  231. "<0|1>"
  232. );
  233. #endif /* !CONFIG_SPL_BUILD */