usb_glue_bk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sys_driver.h"
  7. #include "gpio_driver.h"
  8. #include <driver/gpio.h>
  9. #include <driver/gpio_types.h>
  10. #include <driver/int.h>
  11. #include "bk_misc.h"
  12. #include "usbd_core.h"
  13. #include "usbh_core.h"
  14. #include "usb_musb_reg.h"
  15. #define USB_BASE_ADDR SOC_USB_REG_BASE
  16. #if (CONFIG_SOC_BK7271)
  17. #define REG_AHB2_USB_OTG_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x80)))
  18. #define REG_AHB2_USB_DMA_ENDP (*((volatile unsigned char *)(USB_BASE_ADDR + 0x84)))
  19. #define REG_AHB2_USB_VTH (*((volatile unsigned char *)(USB_BASE_ADDR + 0x88)))
  20. #define REG_AHB2_USB_GEN (*((volatile unsigned char *)(USB_BASE_ADDR + 0x8C)))
  21. #define REG_AHB2_USB_STAT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x90)))
  22. #define REG_AHB2_USB_INT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x94)))
  23. #define REG_AHB2_USB_RESET (*((volatile unsigned char *)(USB_BASE_ADDR + 0x98)))
  24. #define REG_AHB2_USB_DEV_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x9C)))
  25. #elif (CONFIG_SOC_BK7256XX)
  26. #define REG_AHB2_USB_OTG_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x280)))
  27. #define REG_AHB2_USB_DMA_ENDP (*((volatile unsigned char *)(USB_BASE_ADDR + 0x284)))
  28. #define REG_AHB2_USB_VTH (*((volatile unsigned char *)(USB_BASE_ADDR + 0x288)))
  29. #define REG_AHB2_USB_GEN (*((volatile unsigned char *)(USB_BASE_ADDR + 0x28C)))
  30. #define REG_AHB2_USB_STAT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x290)))
  31. #define REG_AHB2_USB_INT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x294)))
  32. #define REG_AHB2_USB_RESET (*((volatile unsigned char *)(USB_BASE_ADDR + 0x298)))
  33. #define REG_AHB2_USB_DEV_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x29C)))
  34. #elif (CONFIG_SOC_BK7236XX) || (CONFIG_SOC_BK7239XX) || (CONFIG_SOC_BK7286XX)
  35. #define REG_AHB2_USB_DEVICE_ID (*((volatile unsigned char *)(USB_BASE_ADDR + 0x280)))
  36. #define REG_AHB2_USB_VERSION_ID (*((volatile unsigned char *)(USB_BASE_ADDR + 0x284)))
  37. #define REG_AHB2_USB_GLOBAL_CTRL (*((volatile unsigned char *)(USB_BASE_ADDR + 0x288)))
  38. #define REG_AHB2_USB_DEVICE_STATUS (*((volatile unsigned char *)(USB_BASE_ADDR + 0x28c)))
  39. #define REG_AHB2_USB_OTG_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x290)))
  40. #define REG_AHB2_USB_DMA_ENDP (*((volatile unsigned char *)(USB_BASE_ADDR + 0x294)))
  41. #define REG_AHB2_USB_VTH (*((volatile unsigned char *)(USB_BASE_ADDR + 0x298)))
  42. #define REG_AHB2_USB_GEN (*((volatile unsigned char *)(USB_BASE_ADDR + 0x29C)))
  43. #define REG_AHB2_USB_STAT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x2A0)))
  44. #define REG_AHB2_USB_INT (*((volatile unsigned char *)(USB_BASE_ADDR + 0x2A4)))
  45. #define REG_AHB2_USB_RESET (*((volatile unsigned char *)(USB_BASE_ADDR + 0x2A8)))
  46. #define REG_AHB2_USB_DEV_CFG (*((volatile unsigned char *)(USB_BASE_ADDR + 0x2AC)))
  47. #define REG_USB_USR_700 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x700)))
  48. #define REG_USB_USR_704 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x704)))
  49. #define REG_USB_USR_708 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x708)))
  50. #define REG_USB_USR_70C (*((volatile unsigned long *)(USB_BASE_ADDR + 0x70C)))
  51. #define REG_USB_USR_710 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x710)))
  52. #define REG_USB_USR_714 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x714)))
  53. #define REG_USB_PHY_00 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x400)))
  54. #define REG_USB_PHY_01 (*((volatile unsigned long *)(USB_BASE_ADDR + 0x404)))
  55. #endif
  56. #define USB_DP_CAPABILITY_VALUE (0xF)
  57. #define USB_DN_CAPABILITY_VALUE (0xF)
  58. #define NANENG_PHY_FC_REG01 (0x01 * 4)
  59. #define NANENG_PHY_FC_REG02 (0x02 * 4)
  60. #define NANENG_PHY_FC_REG03 (0x03 * 4)
  61. #define NANENG_PHY_FC_REG04 (0x04 * 4)
  62. #define NANENG_PHY_FC_REG05 (0x05 * 4)
  63. #define NANENG_PHY_FC_REG06 (0x06 * 4)
  64. #define NANENG_PHY_FC_REG07 (0x07 * 4)
  65. #define NANENG_PHY_FC_REG08 (0x08 * 4)
  66. #define NANENG_PHY_FC_REG09 (0x09 * 4)
  67. #define NANENG_PHY_FC_REG0A (0x0A * 4)
  68. #define NANENG_PHY_FC_REG0B (0x0B * 4)
  69. #define NANENG_PHY_FC_REG0C (0x0C * 4)
  70. #define NANENG_PHY_FC_REG0D (0x0D * 4)
  71. #define NANENG_PHY_FC_REG0E (0x0E * 4)
  72. #define NANENG_PHY_FC_REG0F (0x0F * 4)
  73. #define NANENG_PHY_FC_REG0F_BYTE 0x0F
  74. #define NANENG_PHY_FC_REG10 (0x10 * 4)
  75. #define NANENG_PHY_FC_REG11 (0x11 * 4)
  76. #define NANENG_PHY_FC_REG12 (0x12 * 4)
  77. #define NANENG_PHY_FC_REG13 (0x13 * 4)
  78. #define NANENG_PHY_FC_REG14 (0x14 * 4)
  79. #define NANENG_PHY_FC_REG15 (0x15 * 4)
  80. #define NANENG_PHY_FC_REG16 (0x16 * 4)
  81. #define NANENG_PHY_FC_REG17 (0x17 * 4)
  82. #define NANENG_PHY_FC_REG18 (0x18 * 4)
  83. #define NANENG_PHY_FC_REG19 (0x19 * 4)
  84. #define NANENG_PHY_FC_REG1A (0x1A * 4)
  85. #define NANENG_PHY_FC_REG1B (0x1B * 4)
  86. #define NANENG_PHY_FC_REG1C (0x1C * 4)
  87. #define NANENG_PHY_FC_REG1D (0x1D * 4)
  88. #define NANENG_PHY_FC_REG1E (0x1E * 4)
  89. #define NANENG_PHY_FC_REG1F (0x1F * 4)
  90. #if CONFIG_USBDEV_EP_NUM != 8
  91. #error beken chips only support 8 endpoints
  92. #endif
  93. #if CONFIG_USBHOST_PIPE_NUM != 8
  94. #error beken chips only support 8 pipes
  95. #endif
  96. // clang-format off
  97. static struct musb_fifo_cfg musb_device_table[] = {
  98. { .ep_num = 0, .style = FIFO_TXRX, .maxpacket = 64, },
  99. { .ep_num = 1, .style = FIFO_TX, .maxpacket = 1024, },
  100. { .ep_num = 1, .style = FIFO_RX, .maxpacket = 1024, },
  101. { .ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  102. { .ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  103. { .ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  104. { .ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  105. { .ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  106. { .ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  107. { .ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  108. { .ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  109. { .ep_num = 6, .style = FIFO_TXRX, .maxpacket = 512, },
  110. { .ep_num = 7, .style = FIFO_TXRX, .maxpacket = 512, }
  111. };
  112. static struct musb_fifo_cfg musb_host_table[] = {
  113. { .ep_num = 0, .style = FIFO_TXRX, .maxpacket = 64, },
  114. { .ep_num = 1, .style = FIFO_TX, .maxpacket = 1024, },
  115. { .ep_num = 1, .style = FIFO_RX, .maxpacket = 1024, },
  116. { .ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  117. { .ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  118. { .ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  119. { .ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  120. { .ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  121. { .ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  122. { .ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  123. { .ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  124. { .ep_num = 6, .style = FIFO_TXRX, .maxpacket = 512, },
  125. { .ep_num = 7, .style = FIFO_TXRX, .maxpacket = 512, }
  126. };
  127. // clang-format on
  128. uint8_t usbd_get_musb_fifo_cfg(struct musb_fifo_cfg **cfg)
  129. {
  130. *cfg = musb_device_table;
  131. return sizeof(musb_device_table) / sizeof(musb_device_table[0]);
  132. }
  133. uint8_t usbh_get_musb_fifo_cfg(struct musb_fifo_cfg **cfg)
  134. {
  135. *cfg = musb_host_table;
  136. return sizeof(musb_host_table) / sizeof(musb_host_table[0]);
  137. }
  138. uint32_t usb_get_musb_ram_size(void)
  139. {
  140. return 8192;
  141. }
  142. extern void USBD_IRQHandler(uint8_t busid);
  143. void USBD_IRQ(void)
  144. {
  145. USBD_IRQHandler(0);
  146. }
  147. static void bk_analog_layer_usb_sys_related_ops(uint32_t usb_mode, bool ops)
  148. {
  149. extern void delay(INT32 num);
  150. #if 0
  151. sys_drv_usb_analog_phy_en(ops, NULL);
  152. sys_drv_usb_analog_speed_en(ops, NULL);
  153. sys_drv_usb_analog_ckmcu_en(ops, NULL);
  154. #endif
  155. if (ops) {
  156. sys_drv_usb_clock_ctrl(true, NULL);
  157. delay(100);
  158. #if 0
  159. sys_drv_usb_analog_deepsleep_en(false);
  160. #endif
  161. sys_drv_usb_analog_dp_capability(USB_DP_CAPABILITY_VALUE);
  162. sys_drv_usb_analog_dn_capability(USB_DN_CAPABILITY_VALUE);
  163. if (!sys_hal_psram_ldo_status()) {
  164. sys_drv_psram_ldo_enable(1);
  165. }
  166. sys_drv_usb_analog_phy_en(1, NULL);
  167. if (usb_mode == 0) {
  168. REG_USB_USR_708 = 0x0;
  169. REG_USB_USR_710 &= ~(0x1 << 7);
  170. delay(100);
  171. REG_USB_USR_710 |= (0x1 << 15);
  172. //REG_USB_USR_710 |= (0x1<<14);
  173. REG_USB_USR_710 |= (0x1 << 16);
  174. REG_USB_USR_710 |= (0x1 << 17);
  175. REG_USB_USR_710 |= (0x1 << 18);
  176. REG_USB_USR_710 |= (0x1 << 19);
  177. REG_USB_USR_710 &= ~(0x1 << 20);
  178. REG_USB_USR_710 |= (0x1 << 21);
  179. REG_USB_USR_710 |= (0x0 << 0);
  180. REG_USB_USR_710 |= (0x1 << 5);
  181. REG_USB_USR_710 |= (0x1 << 6);
  182. REG_USB_USR_710 |= (0x1 << 9);
  183. REG_USB_USR_710 |= (0x1 << 10);
  184. REG_USB_USR_710 |= (0x1 << 1);
  185. REG_USB_USR_710 |= (0x1 << 7);
  186. REG_USB_USR_708 = 0x1;
  187. #if 0
  188. REG_USB_PHY_00 = 0x08;
  189. REG_USB_PHY_01 = 0x02;
  190. REG_USB_USR_710 |= (0x1<< 8);
  191. while(1){
  192. reg = REG_USB_USR_70C;
  193. if(reg & 0x100){
  194. USB_DRIVER_LOGI("SelfTest Fin!\r\n");
  195. USB_DRIVER_LOGI("test end!\r\n");
  196. break;
  197. } else {
  198. USB_DRIVER_LOGI("70c_reg:0x%x\r\n", reg);
  199. delay(10000);
  200. }
  201. }
  202. REG_USB_PHY_00 &= ~0x08;
  203. REG_USB_PHY_01 &= ~0x02;
  204. REG_USB_USR_710 &= ~(0x1<< 8);
  205. #endif
  206. } else {
  207. REG_USB_USR_710 |= (0x1 << 15);
  208. REG_USB_USR_710 |= (0x1 << 14);
  209. REG_USB_USR_710 |= (0x1 << 16);
  210. REG_USB_USR_710 |= (0x1 << 17);
  211. REG_USB_USR_710 |= (0x1 << 18);
  212. REG_USB_USR_710 |= (0x1 << 19);
  213. REG_USB_USR_710 &= ~(0x1 << 20);
  214. REG_USB_USR_710 |= (0x1 << 21);
  215. REG_USB_USR_710 |= (0x0 << 0);
  216. REG_USB_USR_710 |= (0x1 << 5);
  217. REG_USB_USR_710 |= (0x1 << 6);
  218. REG_USB_USR_710 |= (0x1 << 9);
  219. REG_USB_USR_710 |= (0x1 << 10);
  220. REG_USB_USR_710 |= (0x1 << 7);
  221. REG_USB_USR_708 = 0x1;
  222. }
  223. } else {
  224. sys_drv_usb_analog_phy_en(0, NULL);
  225. sys_drv_usb_clock_ctrl(false, NULL);
  226. }
  227. }
  228. void usb_dc_low_level_init(void)
  229. {
  230. #if CONFIG_SYS_CPU0
  231. bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_USB_1, 0x0, 0x0);
  232. #endif
  233. bk_analog_layer_usb_sys_related_ops(1, true);
  234. bk_gpio_set_output_high(CONFIG_USB_VBAT_CONTROL_GPIO_ID);
  235. bk_pm_module_vote_cpu_freq(PM_DEV_ID_USB_1, PM_CPU_FRQ_120M);
  236. sys_hal_usb_analog_phy_en(true);
  237. sys_drv_usb_clock_ctrl(true, NULL);
  238. sys_drv_int_enable(USB_INTERRUPT_CTRL_BIT);
  239. bk_int_isr_register(INT_SRC_USB, USBD_IRQ, NULL);
  240. bk_int_set_priority(INT_SRC_USB, 2);
  241. }
  242. void usb_dc_low_level_deinit(void)
  243. {
  244. bk_pm_module_vote_cpu_freq(PM_DEV_ID_USB_1, PM_CPU_FRQ_DEFAULT);
  245. sys_hal_usb_analog_phy_en(false);
  246. sys_drv_usb_clock_ctrl(false, NULL);
  247. sys_drv_int_disable(USB_INTERRUPT_CTRL_BIT);
  248. bk_int_isr_unregister(INT_SRC_USB);
  249. bk_analog_layer_usb_sys_related_ops(1, false);
  250. sys_drv_dev_clk_pwr_up(CLK_PWR_ID_USB_1, CLK_PWR_CTRL_PWR_DOWN);
  251. }
  252. void usbd_musb_delay_ms(uint8_t ms)
  253. {
  254. /* implement later */
  255. }
  256. extern void USBH_IRQHandler(uint8_t busid);
  257. void USBH_IRQ(void)
  258. {
  259. USBH_IRQHandler(0);
  260. }
  261. #define NANENG_PHY_CFG 1
  262. #define USB_PHY_BASE (SOC_USB_REG_BASE + 0x400)
  263. #define HWREG(x) \
  264. (*((volatile uint32_t *)(x)))
  265. #define HWREGH(x) \
  266. (*((volatile uint16_t *)(x)))
  267. #define HWREGB(x) \
  268. (*((volatile uint8_t *)(x)))
  269. void usb_hc_low_level_init(struct usbh_bus *bus)
  270. {
  271. #if CONFIG_SYS_CPU0
  272. bk_pm_module_vote_sleep_ctrl(PM_SLEEP_MODULE_NAME_USB_1, 0x0, 0x0);
  273. #endif
  274. bk_analog_layer_usb_sys_related_ops(0, true);
  275. bk_gpio_set_output_high(CONFIG_USB_VBAT_CONTROL_GPIO_ID);
  276. #if NANENG_PHY_CFG
  277. //NANENG_PHY_CFG_HSRX_TEST
  278. HWREGB(USB_PHY_BASE + NANENG_PHY_FC_REG0F) |= (0x1 << 4);
  279. //disconnect value 640mv
  280. HWREGB(USB_PHY_BASE + NANENG_PHY_FC_REG0B) = 0x7C;
  281. #endif
  282. bk_int_isr_register(INT_SRC_USB, USBH_IRQ, NULL);
  283. sys_drv_int_enable(USB_INTERRUPT_CTRL_BIT);
  284. }
  285. void usb_hc_low_level_deinit(struct usbh_bus *bus)
  286. {
  287. sys_drv_int_disable(USB_INTERRUPT_CTRL_BIT);
  288. bk_int_isr_unregister(INT_SRC_USB);
  289. sys_drv_dev_clk_pwr_up(CLK_PWR_ID_USB_1, CLK_PWR_CTRL_PWR_DOWN);
  290. bk_analog_layer_usb_sys_related_ops(0, false);
  291. }