board.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright (c) 2022-2024 HPMicro
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. */
  6. #include "board.h"
  7. #include "hpm_uart_drv.h"
  8. #include "hpm_gptmr_drv.h"
  9. #include "hpm_i2c_drv.h"
  10. #include "hpm_gpio_drv.h"
  11. #include "hpm_femc_drv.h"
  12. #include "pinmux.h"
  13. #include "hpm_pmp_drv.h"
  14. #include "assert.h"
  15. #include "hpm_clock_drv.h"
  16. #include "hpm_sysctl_drv.h"
  17. #include "hpm_sdxc_drv.h"
  18. #include "hpm_pwm_drv.h"
  19. #include "hpm_trgm_drv.h"
  20. #include "hpm_pllctlv2_drv.h"
  21. #include "hpm_enet_drv.h"
  22. #include "hpm_pcfg_drv.h"
  23. #include "hpm_debug_console.h"
  24. #include <rtconfig.h>
  25. /**
  26. * @brief FLASH configuration option definitions:
  27. * option[0]:
  28. * [31:16] 0xfcf9 - FLASH configuration option tag
  29. * [15:4] 0 - Reserved
  30. * [3:0] option words (exclude option[0])
  31. * option[1]:
  32. * [31:28] Flash probe type
  33. * 0 - SFDP SDR / 1 - SFDP DDR
  34. * 2 - 1-4-4 Read (0xEB, 24-bit address) / 3 - 1-2-2 Read(0xBB, 24-bit address)
  35. * 4 - HyperFLASH 1.8V / 5 - HyperFLASH 3V
  36. * 6 - OctaBus DDR (SPI -> OPI DDR)
  37. * 8 - Xccela DDR (SPI -> OPI DDR)
  38. * 10 - EcoXiP DDR (SPI -> OPI DDR)
  39. * [27:24] Command Pads after Power-on Reset
  40. * 0 - SPI / 1 - DPI / 2 - QPI / 3 - OPI
  41. * [23:20] Command Pads after Configuring FLASH
  42. * 0 - SPI / 1 - DPI / 2 - QPI / 3 - OPI
  43. * [19:16] Quad Enable Sequence (for the device support SFDP 1.0 only)
  44. * 0 - Not needed
  45. * 1 - QE bit is at bit 6 in Status Register 1
  46. * 2 - QE bit is at bit1 in Status Register 2
  47. * 3 - QE bit is at bit7 in Status Register 2
  48. * 4 - QE bit is at bit1 in Status Register 2 and should be programmed by 0x31
  49. * [15:8] Dummy cycles
  50. * 0 - Auto-probed / detected / default value
  51. * Others - User specified value, for DDR read, the dummy cycles should be 2 * cycles on FLASH datasheet
  52. * [7:4] Misc.
  53. * 0 - Not used
  54. * 1 - SPI mode
  55. * 2 - Internal loopback
  56. * 3 - External DQS
  57. * [3:0] Frequency option
  58. * 1 - 30MHz / 2 - 50MHz / 3 - 66MHz / 4 - 80MHz / 5 - 100MHz / 6 - 120MHz / 7 - 133MHz / 8 - 166MHz
  59. *
  60. * option[2] (Effective only if the bit[3:0] in option[0] > 1)
  61. * [31:20] Reserved
  62. * [19:16] IO voltage
  63. * 0 - 3V / 1 - 1.8V
  64. * [15:12] Pin group
  65. * 0 - 1st group / 1 - 2nd group
  66. * [11:8] Connection selection
  67. * 0 - CA_CS0 / 1 - CB_CS0 / 2 - CA_CS0 + CB_CS0 (Two FLASH connected to CA and CB respectively)
  68. * [7:0] Drive Strength
  69. * 0 - Default value
  70. * option[3] (Effective only if the bit[3:0] in option[0] > 2, required only for the QSPI NOR FLASH that not supports
  71. * JESD216)
  72. * [31:16] reserved
  73. * [15:12] Sector Erase Command Option, not required here
  74. * [11:8] Sector Size Option, not required here
  75. * [7:0] Flash Size Option
  76. * 0 - 4MB / 1 - 8MB / 2 - 16MB
  77. */
  78. #if defined(FLASH_XIP) && FLASH_XIP
  79. __attribute__ ((section(".nor_cfg_option"), used)) const uint32_t option[4] = {0xfcf90001, 0x00000007, 0x0, 0x0};
  80. #endif
  81. #if defined(FLASH_UF2) && FLASH_UF2
  82. ATTR_PLACE_AT(".uf2_signature") __attribute__((used)) const uint32_t uf2_signature = BOARD_UF2_SIGNATURE;
  83. #endif
  84. void board_init_console(void)
  85. {
  86. #if !defined(CONFIG_NDEBUG_CONSOLE) || !CONFIG_NDEBUG_CONSOLE
  87. #if CONSOLE_TYPE_UART == BOARD_CONSOLE_TYPE
  88. console_config_t cfg;
  89. /* uart needs to configure pin function before enabling clock, otherwise the level change of
  90. uart rx pin when configuring pin function will cause a wrong data to be received.
  91. And a uart rx dma request will be generated by default uart fifo dma trigger level. */
  92. init_uart_pins((UART_Type *) BOARD_CONSOLE_UART_BASE);
  93. clock_add_to_group(BOARD_CONSOLE_UART_CLK_NAME, 0);
  94. cfg.type = BOARD_CONSOLE_TYPE;
  95. cfg.base = (uint32_t) BOARD_CONSOLE_UART_BASE;
  96. cfg.src_freq_in_hz = clock_get_frequency(BOARD_CONSOLE_UART_CLK_NAME);
  97. cfg.baudrate = BOARD_CONSOLE_UART_BAUDRATE;
  98. if (status_success != console_init(&cfg)) {
  99. /* failed to initialize debug console */
  100. while (1) {
  101. }
  102. }
  103. #else
  104. while (1) {
  105. }
  106. #endif
  107. #endif
  108. }
  109. void board_print_clock_freq(void)
  110. {
  111. printf("==============================\n");
  112. printf(" %s clock summary\n", BOARD_NAME);
  113. printf("==============================\n");
  114. printf("cpu0:\t\t %luHz\n", clock_get_frequency(clock_cpu0));
  115. printf("axi:\t\t %luHz\n", clock_get_frequency(clock_axi));
  116. printf("ahb:\t\t %luHz\n", clock_get_frequency(clock_ahb));
  117. printf("mchtmr0:\t %luHz\n", clock_get_frequency(clock_mchtmr0));
  118. printf("xpi0:\t\t %luHz\n", clock_get_frequency(clock_xpi0));
  119. printf("xpi1:\t\t %luHz\n", clock_get_frequency(clock_xpi1));
  120. printf("femc:\t\t %luHz\n", clock_get_frequency(clock_femc));
  121. printf("==============================\n");
  122. }
  123. void board_init_uart(UART_Type *ptr)
  124. {
  125. /* configure uart's pin before opening uart's clock */
  126. init_uart_pins(ptr);
  127. board_init_uart_clock(ptr);
  128. }
  129. void board_print_banner(void)
  130. {
  131. const uint8_t banner[] = {"\n\
  132. ----------------------------------------------------------------------\n\
  133. $$\\ $$\\ $$$$$$$\\ $$\\ $$\\ $$\\\n\
  134. $$ | $$ |$$ __$$\\ $$$\\ $$$ |\\__|\n\
  135. $$ | $$ |$$ | $$ |$$$$\\ $$$$ |$$\\ $$$$$$$\\ $$$$$$\\ $$$$$$\\\n\
  136. $$$$$$$$ |$$$$$$$ |$$\\$$\\$$ $$ |$$ |$$ _____|$$ __$$\\ $$ __$$\\\n\
  137. $$ __$$ |$$ ____/ $$ \\$$$ $$ |$$ |$$ / $$ | \\__|$$ / $$ |\n\
  138. $$ | $$ |$$ | $$ |\\$ /$$ |$$ |$$ | $$ | $$ | $$ |\n\
  139. $$ | $$ |$$ | $$ | \\_/ $$ |$$ |\\$$$$$$$\\ $$ | \\$$$$$$ |\n\
  140. \\__| \\__|\\__| \\__| \\__|\\__| \\_______|\\__| \\______/\n\
  141. ----------------------------------------------------------------------\n"};
  142. #ifdef SDK_VERSION_STRING
  143. printf("hpm_sdk: %s\n", SDK_VERSION_STRING);
  144. #endif
  145. printf("%s", banner);
  146. }
  147. void board_ungate_mchtmr_at_lp_mode(void)
  148. {
  149. /* Keep cpu clock on wfi, so that mchtmr irq can still work after wfi */
  150. sysctl_set_cpu_lp_mode(HPM_SYSCTL, BOARD_RUNNING_CORE, cpu_lp_mode_ungate_cpu_clock);
  151. }
  152. void board_init(void)
  153. {
  154. board_init_clock();
  155. board_init_console();
  156. board_init_pmp();
  157. #if BOARD_SHOW_CLOCK
  158. board_print_clock_freq();
  159. #endif
  160. #if BOARD_SHOW_BANNER
  161. board_print_banner();
  162. #endif
  163. }
  164. void board_init_sdram_pins(void)
  165. {
  166. init_femc_pins();
  167. }
  168. uint32_t board_init_femc_clock(void)
  169. {
  170. clock_add_to_group(clock_femc, 0);
  171. /* Configure the SDRAM to 166MHz */
  172. clock_set_source_divider(clock_femc, clk_src_pll0_clk1, 2U);
  173. return clock_get_frequency(clock_femc);
  174. }
  175. void board_delay_us(uint32_t us)
  176. {
  177. clock_cpu_delay_us(us);
  178. }
  179. void board_delay_ms(uint32_t ms)
  180. {
  181. clock_cpu_delay_ms(ms);
  182. }
  183. #if !defined(NO_BOARD_TIMER_SUPPORT) || !NO_BOARD_TIMER_SUPPORT
  184. static board_timer_cb timer_cb;
  185. SDK_DECLARE_EXT_ISR_M(BOARD_CALLBACK_TIMER_IRQ, board_timer_isr)
  186. void board_timer_isr(void)
  187. {
  188. if (gptmr_check_status(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_STAT_MASK(BOARD_CALLBACK_TIMER_CH))) {
  189. gptmr_clear_status(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_STAT_MASK(BOARD_CALLBACK_TIMER_CH));
  190. timer_cb();
  191. }
  192. }
  193. void board_timer_create(uint32_t ms, board_timer_cb cb)
  194. {
  195. uint32_t gptmr_freq;
  196. gptmr_channel_config_t config;
  197. timer_cb = cb;
  198. gptmr_channel_get_default_config(BOARD_CALLBACK_TIMER, &config);
  199. clock_add_to_group(BOARD_CALLBACK_TIMER_CLK_NAME, 0);
  200. gptmr_freq = clock_get_frequency(BOARD_CALLBACK_TIMER_CLK_NAME);
  201. config.reload = gptmr_freq / 1000 * ms;
  202. gptmr_channel_config(BOARD_CALLBACK_TIMER, BOARD_CALLBACK_TIMER_CH, &config, false);
  203. gptmr_enable_irq(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_IRQ_MASK(BOARD_CALLBACK_TIMER_CH));
  204. intc_m_enable_irq_with_priority(BOARD_CALLBACK_TIMER_IRQ, 1);
  205. gptmr_start_counter(BOARD_CALLBACK_TIMER, BOARD_CALLBACK_TIMER_CH);
  206. }
  207. #endif
  208. void board_i2c_bus_clear(I2C_Type *ptr)
  209. {
  210. init_i2c_pins_as_gpio(ptr);
  211. if (ptr == BOARD_APP_I2C_BASE) {
  212. gpio_set_pin_input(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SDA_GPIO_INDEX, BOARD_I2C_SDA_GPIO_PIN);
  213. gpio_set_pin_input(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SCL_GPIO_INDEX, BOARD_I2C_SCL_GPIO_PIN);
  214. if (!gpio_read_pin(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SCL_GPIO_INDEX, BOARD_I2C_SCL_GPIO_PIN)) {
  215. printf("CLK is low, please power cycle the board\n");
  216. while (1) {
  217. }
  218. }
  219. if (!gpio_read_pin(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SDA_GPIO_INDEX, BOARD_I2C_SDA_GPIO_PIN)) {
  220. printf("SDA is low, try to issue I2C bus clear\n");
  221. } else {
  222. printf("I2C bus is ready\n");
  223. return;
  224. }
  225. gpio_set_pin_output(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SCL_GPIO_INDEX, BOARD_I2C_SCL_GPIO_PIN);
  226. while (1) {
  227. for (uint32_t i = 0; i < 9; i++) {
  228. gpio_write_pin(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SCL_GPIO_INDEX, BOARD_I2C_SCL_GPIO_PIN, 1);
  229. board_delay_ms(10);
  230. gpio_write_pin(BOARD_I2C_GPIO_CTRL, BOARD_I2C_SCL_GPIO_INDEX, BOARD_I2C_SCL_GPIO_PIN, 0);
  231. board_delay_ms(10);
  232. }
  233. board_delay_ms(100);
  234. }
  235. printf("I2C bus is cleared\n");
  236. }
  237. }
  238. uint32_t board_init_i2c_clock(I2C_Type *ptr)
  239. {
  240. uint32_t freq = 0;
  241. if (ptr == HPM_I2C0) {
  242. clock_add_to_group(clock_i2c0, 0);
  243. freq = clock_get_frequency(clock_i2c0);
  244. } else if (ptr == HPM_I2C1) {
  245. clock_add_to_group(clock_i2c1, 0);
  246. freq = clock_get_frequency(clock_i2c1);
  247. } else if (ptr == HPM_I2C2) {
  248. clock_add_to_group(clock_i2c2, 0);
  249. freq = clock_get_frequency(clock_i2c2);
  250. } else if (ptr == HPM_I2C3) {
  251. clock_add_to_group(clock_i2c3, 0);
  252. freq = clock_get_frequency(clock_i2c3);
  253. } else {
  254. ;
  255. }
  256. return freq;
  257. }
  258. void board_init_i2c(I2C_Type *ptr)
  259. {
  260. i2c_config_t config;
  261. hpm_stat_t stat;
  262. uint32_t freq;
  263. freq = board_init_i2c_clock(ptr);
  264. board_i2c_bus_clear(ptr);
  265. init_i2c_pins(ptr);
  266. config.i2c_mode = i2c_mode_normal;
  267. config.is_10bit_addressing = false;
  268. stat = i2c_init_master(ptr, freq, &config);
  269. if (stat != status_success) {
  270. printf("failed to initialize i2c 0x%lx\n", (uint32_t) ptr);
  271. while (1) {
  272. }
  273. }
  274. }
  275. uint32_t board_init_spi_clock(SPI_Type *ptr)
  276. {
  277. if (ptr == HPM_SPI3) {
  278. clock_add_to_group(clock_spi3, 0);
  279. return clock_get_frequency(clock_spi3);
  280. }
  281. return 0;
  282. }
  283. void board_init_gpio_pins(void)
  284. {
  285. init_gpio_pins();
  286. }
  287. void board_init_spi_pins(SPI_Type *ptr)
  288. {
  289. init_spi_pins(ptr);
  290. }
  291. void board_init_spi_pins_with_gpio_as_cs(SPI_Type *ptr)
  292. {
  293. init_spi_pins_with_gpio_as_cs(ptr);
  294. gpio_set_pin_output_with_initial(BOARD_SPI_CS_GPIO_CTRL, GPIO_GET_PORT_INDEX(BOARD_SPI_CS_PIN),
  295. GPIO_GET_PIN_INDEX(BOARD_SPI_CS_PIN), !BOARD_SPI_CS_ACTIVE_LEVEL);
  296. }
  297. void board_write_spi_cs(uint32_t pin, uint8_t state)
  298. {
  299. gpio_write_pin(BOARD_SPI_CS_GPIO_CTRL, GPIO_GET_PORT_INDEX(pin), GPIO_GET_PIN_INDEX(pin), state);
  300. }
  301. uint8_t board_get_led_gpio_off_level(void)
  302. {
  303. return BOARD_LED_OFF_LEVEL;
  304. }
  305. void board_init_led_pins(void)
  306. {
  307. init_led_pins();
  308. gpio_set_pin_output_with_initial(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN, board_get_led_gpio_off_level());
  309. }
  310. void board_led_toggle(void)
  311. {
  312. gpio_toggle_pin(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN);
  313. }
  314. void board_led_write(uint8_t state)
  315. {
  316. gpio_write_pin(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN, state);
  317. }
  318. void board_init_usb(USB_Type *ptr)
  319. {
  320. if (ptr == HPM_USB0) {
  321. init_usb_pins(ptr);
  322. clock_add_to_group(clock_usb0, 0);
  323. }
  324. }
  325. void board_init_pmp(void)
  326. {
  327. extern uint32_t __noncacheable_start__[];
  328. extern uint32_t __noncacheable_end__[];
  329. uint32_t start_addr = (uint32_t) __noncacheable_start__;
  330. uint32_t end_addr = (uint32_t) __noncacheable_end__;
  331. uint32_t length = end_addr - start_addr;
  332. if (length == 0) {
  333. return;
  334. }
  335. /* Ensure the address and the length are power of 2 aligned */
  336. assert((length & (length - 1U)) == 0U);
  337. assert((start_addr & (length - 1U)) == 0U);
  338. pmp_entry_t pmp_entry[3] = {0};
  339. pmp_entry[0].pmp_addr = PMP_NAPOT_ADDR(0x0000000, 0x80000000);
  340. pmp_entry[0].pmp_cfg.val = PMP_CFG(READ_EN, WRITE_EN, EXECUTE_EN, ADDR_MATCH_NAPOT, REG_UNLOCK);
  341. pmp_entry[1].pmp_addr = PMP_NAPOT_ADDR(0x80000000, 0x80000000);
  342. pmp_entry[1].pmp_cfg.val = PMP_CFG(READ_EN, WRITE_EN, EXECUTE_EN, ADDR_MATCH_NAPOT, REG_UNLOCK);
  343. pmp_entry[2].pmp_addr = PMP_NAPOT_ADDR(start_addr, length);
  344. pmp_entry[2].pmp_cfg.val = PMP_CFG(READ_EN, WRITE_EN, EXECUTE_EN, ADDR_MATCH_NAPOT, REG_UNLOCK);
  345. pmp_entry[2].pma_addr = PMA_NAPOT_ADDR(start_addr, length);
  346. pmp_entry[2].pma_cfg.val = PMA_CFG(ADDR_MATCH_NAPOT, MEM_TYPE_MEM_NON_CACHE_BUF, AMO_EN);
  347. pmp_config(&pmp_entry[0], ARRAY_SIZE(pmp_entry));
  348. }
  349. void board_init_clock(void)
  350. {
  351. uint32_t cpu0_freq = clock_get_frequency(clock_cpu0);
  352. if (cpu0_freq == PLLCTL_SOC_PLL_REFCLK_FREQ) {
  353. /* Configure the External OSC ramp-up time: ~9ms */
  354. pllctlv2_xtal_set_rampup_time(HPM_PLLCTLV2, 32UL * 1000UL * 9U);
  355. /* Select clock setting preset1 */
  356. sysctl_clock_set_preset(HPM_SYSCTL, 2);
  357. }
  358. /* Add clocks to group 0 */
  359. clock_add_to_group(clock_cpu0, 0);
  360. clock_add_to_group(clock_mchtmr0, 0);
  361. clock_add_to_group(clock_ahbp, 0);
  362. clock_add_to_group(clock_axic, 0);
  363. clock_add_to_group(clock_axis, 0);
  364. clock_add_to_group(clock_xpi0, 0);
  365. clock_add_to_group(clock_xpi1, 0);
  366. clock_add_to_group(clock_xdma, 0);
  367. clock_add_to_group(clock_hdma, 0);
  368. clock_add_to_group(clock_ram0, 0);
  369. clock_add_to_group(clock_lmm0, 0);
  370. clock_add_to_group(clock_gpio, 0);
  371. clock_add_to_group(clock_mot0, 0);
  372. clock_add_to_group(clock_mot1, 0);
  373. clock_add_to_group(clock_synt, 0);
  374. clock_add_to_group(clock_ptpc, 0);
  375. /* Connect Group0 to CPU0 */
  376. clock_connect_group_to_cpu(0, 0);
  377. /* Bump up DCDC voltage to 1275mv */
  378. pcfg_dcdc_set_voltage(HPM_PCFG, 1275);
  379. /* Configure CPU to 648MHz, AXI/AHB to 162MHz */
  380. sysctl_config_cpu0_domain_clock(HPM_SYSCTL, clock_source_pll1_clk0, 1, 4, 4);
  381. /* Configure PLL1_CLK0 Post Divider to 1 */
  382. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll1, pllctlv2_clk0, pllctlv2_div_1p0); /* PLL1CLK0: 648MHz */
  383. /* Configure PLL1_CLK1 Post Divider to 2 */
  384. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll1, pllctlv2_clk1, pllctlv2_div_2p0); /* PLL1CLK1: 324MHz */
  385. /* Configure PLL1 clock frequency to 648MHz */
  386. pllctlv2_init_pll_with_freq(HPM_PLLCTLV2, pllctlv2_pll1, BOARD_CPU_FREQ);
  387. clock_update_core_clock();
  388. /* Configure mchtmr to 24MHz */
  389. clock_set_source_divider(clock_mchtmr0, clk_src_osc24m, 1);
  390. }
  391. uint32_t board_init_dao_clock(void)
  392. {
  393. return clock_get_frequency(clock_dao);
  394. }
  395. uint32_t board_init_pdm_clock(void)
  396. {
  397. return clock_get_frequency(clock_pdm);
  398. }
  399. hpm_stat_t board_set_audio_pll_clock(uint32_t freq)
  400. {
  401. return pllctlv2_init_pll_with_freq(HPM_PLLCTLV2, 2, freq); /* pll2clk */
  402. }
  403. uint32_t board_init_i2s_clock(I2S_Type *ptr)
  404. {
  405. (void) ptr;
  406. return 0;
  407. }
  408. void board_init_adc16_pins(void)
  409. {
  410. init_adc_pins();
  411. }
  412. uint32_t board_init_adc_clock(void *ptr, bool clk_src_bus)
  413. {
  414. uint32_t freq = 0;
  415. if (ptr == (void *)HPM_ADC0) {
  416. if (clk_src_bus) {
  417. /* Configure the ADC clock from AHB (@160MHz by default)*/
  418. clock_set_adc_source(clock_adc0, clk_adc_src_ahb0);
  419. } else {
  420. /* Configure the ADC clock from pll0_clk1 divided by 2 (@166MHz by default) */
  421. clock_set_adc_source(clock_adc0, clk_adc_src_ana0);
  422. clock_set_source_divider(clock_ana0, clk_src_pll0_clk1, 2U);
  423. }
  424. clock_add_to_group(clock_adc0, 0);
  425. freq = clock_get_frequency(clock_adc0);
  426. } else if (ptr == (void *)HPM_ADC1) {
  427. if (clk_src_bus) {
  428. /* Configure the ADC clock from AHB (@160MHz by default)*/
  429. clock_set_adc_source(clock_adc1, clk_adc_src_ahb0);
  430. } else {
  431. /* Configure the ADC clock from pll1_clk1 divided by 2 (@166MHz by default) */
  432. clock_set_adc_source(clock_adc1, clk_adc_src_ana1);
  433. clock_set_source_divider(clock_ana1, clk_src_pll0_clk1, 2U);
  434. }
  435. clock_add_to_group(clock_adc1, 0);
  436. freq = clock_get_frequency(clock_adc1);
  437. } else if (ptr == (void *)HPM_ADC2) {
  438. if (clk_src_bus) {
  439. /* Configure the ADC clock from AHB (@160MHz by default)*/
  440. clock_set_adc_source(clock_adc2, clk_adc_src_ahb0);
  441. } else {
  442. /* Configure the ADC clock from pll1_clk1 divided by 2 (@166MHz by default) */
  443. clock_set_adc_source(clock_adc2, clk_adc_src_ana2);
  444. clock_set_source_divider(clock_ana2, clk_src_pll0_clk1, 2U);
  445. }
  446. clock_add_to_group(clock_adc2, 0);
  447. freq = clock_get_frequency(clock_adc2);
  448. }
  449. return freq;
  450. }
  451. void board_init_acmp_pins(void)
  452. {
  453. init_acmp_pins();
  454. }
  455. void board_init_acmp_clock(ACMP_Type *ptr)
  456. {
  457. (void)ptr;
  458. clock_add_to_group(BOARD_ACMP_CLK, BOARD_RUNNING_CORE & 0x1);
  459. }
  460. uint32_t board_init_dac_clock(DAC_Type *ptr, bool clk_src_ahb)
  461. {
  462. uint32_t freq = 0;
  463. if (ptr == HPM_DAC) {
  464. if (clk_src_ahb == true) {
  465. /* Configure the DAC clock to 160MHz */
  466. clock_set_dac_source(clock_dac0, clk_dac_src_ahb0);
  467. } else {
  468. /* Configure the DAC clock to 166MHz */
  469. clock_set_dac_source(clock_dac0, clk_dac_src_ana3);
  470. clock_set_source_divider(clock_ana3, clk_src_pll0_clk1, 2);
  471. }
  472. clock_add_to_group(clock_dac0, 0);
  473. freq = clock_get_frequency(clock_dac0);
  474. }
  475. return freq;
  476. }
  477. void board_init_can(CAN_Type *ptr)
  478. {
  479. init_can_pins(ptr);
  480. }
  481. uint32_t board_init_can_clock(CAN_Type *ptr)
  482. {
  483. uint32_t freq = 0;
  484. if (ptr == HPM_CAN0) {
  485. /* Set the CAN0 peripheral clock to 80MHz */
  486. clock_set_source_divider(clock_can0, clk_src_pll0_clk0, 5);
  487. clock_add_to_group(clock_can0, 0);
  488. freq = clock_get_frequency(clock_can0);
  489. } else if (ptr == HPM_CAN1) {
  490. /* Set the CAN1 peripheral clock to 80MHz */
  491. clock_set_source_divider(clock_can1, clk_src_pll0_clk0, 5);
  492. clock_add_to_group(clock_can1, 0);
  493. freq = clock_get_frequency(clock_can1);
  494. } else {
  495. /* Invalid CAN instance */
  496. }
  497. return freq;
  498. }
  499. #ifdef INIT_EXT_RAM_FOR_DATA
  500. /*
  501. * this function will be called during startup to initialize external memory for data use
  502. */
  503. void _init_ext_ram(void)
  504. {
  505. uint32_t femc_clk_in_hz;
  506. femc_config_t config = {0};
  507. femc_sdram_config_t sdram_config = {0};
  508. board_init_sdram_pins();
  509. femc_clk_in_hz = board_init_femc_clock();
  510. femc_default_config(HPM_FEMC, &config);
  511. femc_init(HPM_FEMC, &config);
  512. femc_get_typical_sdram_config(HPM_FEMC, &sdram_config);
  513. sdram_config.bank_num = FEMC_SDRAM_BANK_NUM_4;
  514. sdram_config.prescaler = 0x3;
  515. sdram_config.burst_len_in_byte = 8;
  516. sdram_config.auto_refresh_count_in_one_burst = 1;
  517. sdram_config.col_addr_bits = BOARD_SDRAM_COLUMN_ADDR_BITS;
  518. sdram_config.cas_latency = FEMC_SDRAM_CAS_LATENCY_3;
  519. sdram_config.refresh_to_refresh_in_ns = 60; /* Trc */
  520. sdram_config.refresh_recover_in_ns = 60; /* Trc */
  521. sdram_config.act_to_precharge_in_ns = 42; /* Tras */
  522. sdram_config.act_to_rw_in_ns = 18; /* Trcd */
  523. sdram_config.precharge_to_act_in_ns = 18; /* Trp */
  524. sdram_config.act_to_act_in_ns = 12; /* Trrd */
  525. sdram_config.write_recover_in_ns = 12; /* Twr/Tdpl */
  526. sdram_config.self_refresh_recover_in_ns = 72; /* Txsr */
  527. sdram_config.cs = BOARD_SDRAM_CS;
  528. sdram_config.base_address = BOARD_SDRAM_ADDRESS;
  529. sdram_config.size_in_byte = BOARD_SDRAM_SIZE;
  530. sdram_config.port_size = BOARD_SDRAM_PORT_SIZE;
  531. sdram_config.refresh_count = BOARD_SDRAM_REFRESH_COUNT;
  532. sdram_config.refresh_in_ms = BOARD_SDRAM_REFRESH_IN_MS;
  533. sdram_config.delay_cell_disable = true;
  534. sdram_config.delay_cell_value = 0;
  535. femc_config_sdram(HPM_FEMC, femc_clk_in_hz, &sdram_config);
  536. }
  537. #endif
  538. uint32_t board_sd_configure_clock(SDXC_Type *ptr, uint32_t freq, bool need_inverse)
  539. {
  540. uint32_t actual_freq = 0;
  541. do {
  542. clock_name_t sdxc_clk = clock_sdxc0;
  543. clock_add_to_group(sdxc_clk, 0);
  544. sdxc_enable_inverse_clock(ptr, false);
  545. sdxc_enable_sd_clock(ptr, false);
  546. /* Configure the SDXC Frequency to 200MHz */
  547. clock_set_source_divider(sdxc_clk, clk_src_pll0_clk0, 2);
  548. sdxc_enable_freq_selection(ptr);
  549. hpm_stat_t status = clock_wait_source_stable(sdxc_clk);
  550. if (status != status_success) {
  551. break;
  552. }
  553. /* Configure the clock below 400KHz for the identification state */
  554. if (freq <= 400000UL) {
  555. sdxc_set_clock_divider(ptr, 600);
  556. }
  557. /* configure the clock to 24MHz for the SDR12/Default speed */
  558. else if (freq <= 26000000UL) {
  559. sdxc_set_clock_divider(ptr, 8);
  560. }
  561. /* Configure the clock to 50MHz for the SDR25/High speed/50MHz DDR/50MHz SDR */
  562. else if (freq <= 52000000UL) {
  563. sdxc_set_clock_divider(ptr, 4);
  564. }
  565. /* Configure the clock to 100MHz for the SDR50 */
  566. else if (freq <= 100000000UL) {
  567. sdxc_set_clock_divider(ptr, 2);
  568. }
  569. /* Configure the clock to 166MHz for SDR104/HS200/HS400 */
  570. else if (freq <= 208000000UL) {
  571. sdxc_set_clock_divider(ptr, 1);
  572. }
  573. /* For other unsupported clock ranges, configure the clock to 24MHz */
  574. else {
  575. sdxc_set_clock_divider(ptr, 8);
  576. }
  577. if (need_inverse) {
  578. sdxc_enable_inverse_clock(ptr, true);
  579. }
  580. sdxc_enable_sd_clock(ptr, true);
  581. actual_freq = clock_get_frequency(sdxc_clk) / sdxc_get_clock_divider(ptr);
  582. } while (false);
  583. return actual_freq;
  584. }
  585. void board_sd_switch_pins_to_1v8(SDXC_Type *ptr)
  586. {
  587. (void) ptr;
  588. /* This feature is not supported */
  589. }
  590. bool board_sd_detect_card(SDXC_Type *ptr)
  591. {
  592. return sdxc_is_card_inserted(ptr);
  593. }
  594. hpm_stat_t board_init_enet_ptp_clock(ENET_Type *ptr)
  595. {
  596. /* set clock source */
  597. if (ptr == HPM_ENET0) {
  598. clock_add_to_group(clock_ptp0, BOARD_RUNNING_CORE & 0x1);
  599. /* make sure pll0_clk0 output clock at 400MHz to get a clock at 100MHz for ent0 ptp clock */
  600. clock_set_source_divider(clock_ptp0, clk_src_pll0_clk0, 4); /* 100MHz */
  601. } else {
  602. return status_invalid_argument;
  603. }
  604. return status_success;
  605. }
  606. hpm_stat_t board_init_enet_rmii_reference_clock(ENET_Type *ptr, bool internal)
  607. {
  608. /* Configure Enet clock to output reference clock */
  609. if (ptr == HPM_ENET0) {
  610. clock_add_to_group(clock_eth0, BOARD_RUNNING_CORE & 0x1);
  611. if (internal) {
  612. /* set pll output frequency at 1GHz */
  613. if (pllctlv2_init_pll_with_freq(HPM_PLLCTLV2, pllctlv2_pll2, 1000000000UL) == status_success) {
  614. /* set pll2_clk1 output frequency at 250MHz from PLL2 divided by 4 (1 + 15 / 5) */
  615. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll2, pllctlv2_clk1, pllctlv2_div_4p0);
  616. /* set eth clock frequency at 50MHz for enet0 */
  617. clock_set_source_divider(clock_eth0, clk_src_pll2_clk1, 5);
  618. } else {
  619. return status_fail;
  620. }
  621. }
  622. } else {
  623. return status_invalid_argument;
  624. }
  625. enet_rmii_enable_clock(ptr, internal);
  626. return status_success;
  627. }
  628. hpm_stat_t board_init_enet_pins(ENET_Type *ptr)
  629. {
  630. init_enet_pins(ptr);
  631. return status_success;
  632. }
  633. hpm_stat_t board_reset_enet_phy(ENET_Type *ptr)
  634. {
  635. (void) ptr;
  636. return status_success;
  637. }
  638. void board_init_dac_pins(DAC_Type *ptr)
  639. {
  640. init_dac_pins(ptr);
  641. }
  642. uint32_t board_init_uart_clock(UART_Type *ptr)
  643. {
  644. uint32_t freq = 0U;
  645. if (ptr == HPM_UART0) {
  646. clock_add_to_group(clock_uart0, 0);
  647. freq = clock_get_frequency(clock_uart0);
  648. } else if (ptr == HPM_UART1) {
  649. clock_add_to_group(clock_uart1, 0);
  650. freq = clock_get_frequency(clock_uart1);
  651. } else if (ptr == HPM_UART2) {
  652. clock_add_to_group(clock_uart2, 0);
  653. freq = clock_get_frequency(clock_uart2);
  654. } else {
  655. /* Not supported */
  656. }
  657. return freq;
  658. }
  659. uint8_t board_get_enet_dma_pbl(ENET_Type *ptr)
  660. {
  661. (void) ptr;
  662. return enet_pbl_16;
  663. }
  664. hpm_stat_t board_enable_enet_irq(ENET_Type *ptr)
  665. {
  666. if (ptr == HPM_ENET0) {
  667. intc_m_enable_irq(IRQn_ENET0);
  668. } else {
  669. return status_invalid_argument;
  670. }
  671. return status_success;
  672. }
  673. hpm_stat_t board_disable_enet_irq(ENET_Type *ptr)
  674. {
  675. if (ptr == HPM_ENET0) {
  676. intc_m_disable_irq(IRQn_ENET0);
  677. } else {
  678. return status_invalid_argument;
  679. }
  680. return status_success;
  681. }
  682. void board_init_enet_pps_pins(ENET_Type *ptr)
  683. {
  684. (void) ptr;
  685. init_enet_pps_pins();
  686. }
  687. void board_init_enet_pps_capture_pins(ENET_Type *ptr)
  688. {
  689. (void) ptr;
  690. init_enet_pps_capture_pins();
  691. }
  692. void board_init_gptmr_channel_pin(GPTMR_Type *ptr, uint32_t channel, bool as_comp)
  693. {
  694. init_gptmr_channel_pin(ptr, channel, as_comp);
  695. }
  696. uint32_t board_init_gptmr_clock(GPTMR_Type *ptr)
  697. {
  698. uint32_t freq = 0U;
  699. if (ptr == HPM_GPTMR0) {
  700. clock_add_to_group(clock_gptmr0, BOARD_RUNNING_CORE & 0x1);
  701. freq = clock_get_frequency(clock_gptmr0);
  702. } else if (ptr == HPM_GPTMR1) {
  703. clock_add_to_group(clock_gptmr1, BOARD_RUNNING_CORE & 0x1);
  704. freq = clock_get_frequency(clock_gptmr1);
  705. } else if (ptr == HPM_GPTMR2) {
  706. clock_add_to_group(clock_gptmr2, BOARD_RUNNING_CORE & 0x1);
  707. freq = clock_get_frequency(clock_gptmr2);
  708. } else if (ptr == HPM_GPTMR3) {
  709. clock_add_to_group(clock_gptmr3, BOARD_RUNNING_CORE & 0x1);
  710. freq = clock_get_frequency(clock_gptmr3);
  711. } else if (ptr == HPM_PTMR) {
  712. clock_add_to_group(clock_ptmr, BOARD_RUNNING_CORE & 0x1);
  713. freq = clock_get_frequency(clock_ptmr);
  714. } else {
  715. /* Not supported */
  716. }
  717. return freq;
  718. }