board.c 5.8 KB

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