params_setup.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <string.h>
  10. #include <lib/bl_aux_params/bl_aux_params.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <drivers/console.h>
  14. #include <drivers/gpio.h>
  15. #include <libfdt.h>
  16. #include <lib/coreboot.h>
  17. #include <lib/mmio.h>
  18. #include <plat/common/platform.h>
  19. #include <plat_params.h>
  20. #include <plat_private.h>
  21. static struct bl_aux_gpio_info rst_gpio = { .index = UINT_MAX } ;
  22. static struct bl_aux_gpio_info poweroff_gpio = { .index = UINT_MAX };
  23. static struct bl_aux_gpio_info suspend_gpio[10];
  24. uint32_t suspend_gpio_cnt;
  25. static struct bl_aux_rk_apio_info suspend_apio;
  26. #if COREBOOT
  27. static int dt_process_fdt(u_register_t param_from_bl2)
  28. {
  29. return -ENODEV;
  30. }
  31. #else
  32. static uint32_t rk_uart_base = PLAT_RK_UART_BASE;
  33. static uint32_t rk_uart_baudrate = PLAT_RK_UART_BAUDRATE;
  34. static uint32_t rk_uart_clock = PLAT_RK_UART_CLOCK;
  35. #define FDT_BUFFER_SIZE 0x20000
  36. static uint64_t fdt_buffer[FDT_BUFFER_SIZE / 8];
  37. void *plat_get_fdt(void)
  38. {
  39. return &fdt_buffer[0];
  40. }
  41. static void plat_rockchip_dt_process_fdt_uart(void *fdt)
  42. {
  43. const char *path_name = "/chosen";
  44. const char *prop_name = "stdout-path";
  45. int node_offset;
  46. int stdout_path_len;
  47. const char *stdout_path;
  48. const char *separator;
  49. const char *baud_start;
  50. char serial_char;
  51. int serial_no;
  52. uint32_t uart_base;
  53. uint32_t baud;
  54. node_offset = fdt_path_offset(fdt, path_name);
  55. if (node_offset < 0)
  56. return;
  57. stdout_path = fdt_getprop(fdt, node_offset, prop_name,
  58. &stdout_path_len);
  59. if (stdout_path == NULL)
  60. return;
  61. /*
  62. * We expect something like:
  63. * "serial0:baudrate"
  64. */
  65. if (strncmp("serial", stdout_path, 6) != 0)
  66. return;
  67. serial_char = stdout_path[6];
  68. serial_no = serial_char - '0';
  69. switch (serial_no) {
  70. case 0:
  71. uart_base = UART0_BASE;
  72. break;
  73. case 1:
  74. uart_base = UART1_BASE;
  75. break;
  76. case 2:
  77. uart_base = UART2_BASE;
  78. break;
  79. #ifdef UART3_BASE
  80. case 3:
  81. uart_base = UART3_BASE;
  82. break;
  83. #endif
  84. #ifdef UART4_BASE
  85. case 4:
  86. uart_base = UART4_BASE;
  87. break;
  88. #endif
  89. #ifdef UART5_BASE
  90. case 5:
  91. uart_base = UART5_BASE;
  92. break;
  93. #endif
  94. default:
  95. return;
  96. }
  97. rk_uart_base = uart_base;
  98. separator = strchr(stdout_path, ':');
  99. if (!separator)
  100. return;
  101. baud = 0;
  102. baud_start = separator + 1;
  103. while (*baud_start != '\0') {
  104. /*
  105. * uart binding is <baud>{<parity>{<bits>{...}}}
  106. * So the baudrate either is the whole string, or
  107. * we end in the parity characters.
  108. */
  109. if (*baud_start == 'n' || *baud_start == 'o' ||
  110. *baud_start == 'e')
  111. break;
  112. baud = baud * 10 + (*baud_start - '0');
  113. baud_start++;
  114. }
  115. rk_uart_baudrate = baud;
  116. }
  117. static int dt_process_fdt(u_register_t param_from_bl2)
  118. {
  119. void *fdt = plat_get_fdt();
  120. int ret;
  121. ret = fdt_open_into((void *)param_from_bl2, fdt, FDT_BUFFER_SIZE);
  122. if (ret < 0)
  123. return ret;
  124. plat_rockchip_dt_process_fdt_uart(fdt);
  125. return 0;
  126. }
  127. #endif
  128. uint32_t rockchip_get_uart_base(void)
  129. {
  130. #if COREBOOT
  131. return coreboot_serial.baseaddr;
  132. #else
  133. return rk_uart_base;
  134. #endif
  135. }
  136. uint32_t rockchip_get_uart_baudrate(void)
  137. {
  138. #if COREBOOT
  139. return coreboot_serial.baud;
  140. #else
  141. return rk_uart_baudrate;
  142. #endif
  143. }
  144. uint32_t rockchip_get_uart_clock(void)
  145. {
  146. #if COREBOOT
  147. return coreboot_serial.input_hertz;
  148. #else
  149. return rk_uart_clock;
  150. #endif
  151. }
  152. struct bl_aux_gpio_info *plat_get_rockchip_gpio_reset(void)
  153. {
  154. if (rst_gpio.index == UINT_MAX)
  155. return NULL;
  156. return &rst_gpio;
  157. }
  158. struct bl_aux_gpio_info *plat_get_rockchip_gpio_poweroff(void)
  159. {
  160. if (poweroff_gpio.index == UINT_MAX)
  161. return NULL;
  162. return &poweroff_gpio;
  163. }
  164. struct bl_aux_gpio_info *plat_get_rockchip_suspend_gpio(uint32_t *count)
  165. {
  166. *count = suspend_gpio_cnt;
  167. return &suspend_gpio[0];
  168. }
  169. struct bl_aux_rk_apio_info *plat_get_rockchip_suspend_apio(void)
  170. {
  171. return &suspend_apio;
  172. }
  173. static bool rk_aux_param_handler(struct bl_aux_param_header *param)
  174. {
  175. /* Store platform parameters for later processing if needed. */
  176. switch (param->type) {
  177. case BL_AUX_PARAM_RK_RESET_GPIO:
  178. rst_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
  179. return true;
  180. case BL_AUX_PARAM_RK_POWEROFF_GPIO:
  181. poweroff_gpio = ((struct bl_aux_param_gpio *)param)->gpio;
  182. return true;
  183. case BL_AUX_PARAM_RK_SUSPEND_GPIO:
  184. if (suspend_gpio_cnt >= ARRAY_SIZE(suspend_gpio)) {
  185. ERROR("Exceeded the supported suspend GPIO number.\n");
  186. return true;
  187. }
  188. suspend_gpio[suspend_gpio_cnt++] =
  189. ((struct bl_aux_param_gpio *)param)->gpio;
  190. return true;
  191. case BL_AUX_PARAM_RK_SUSPEND_APIO:
  192. suspend_apio = ((struct bl_aux_param_rk_apio *)param)->apio;
  193. return true;
  194. }
  195. return false;
  196. }
  197. void params_early_setup(u_register_t plat_param_from_bl2)
  198. {
  199. int ret;
  200. /*
  201. * Test if this is a FDT passed as a platform-specific parameter
  202. * block.
  203. */
  204. ret = dt_process_fdt(plat_param_from_bl2);
  205. if (!ret) {
  206. return;
  207. } else if (ret != -FDT_ERR_BADMAGIC) {
  208. /*
  209. * If we found an FDT but couldn't parse it (e.g. corrupt, not
  210. * enough space), return and don't attempt to parse the param
  211. * as something else, since we know that will also fail. All
  212. * we're doing is setting up UART, this doesn't need to be
  213. * fatal.
  214. */
  215. WARN("%s: found FDT but could not parse: error %d\n",
  216. __func__, ret);
  217. return;
  218. }
  219. bl_aux_params_parse(plat_param_from_bl2, rk_aux_param_handler);
  220. }