soc_dsp.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 1997-2015, Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef SOC_DSP_H_
  7. #define SOC_DSP_H_
  8. #include <os_common_api.h>
  9. #include <stdint.h>
  10. #include <soc_regs.h>
  11. #include <soc_irq.h>
  12. #include <rbuf/rbuf_mem.h>
  13. /* if set, the user can externally shut down the DSP root clock (gated by CMU) */
  14. #define PSU_DSP_IDLE BIT(25)
  15. /* if set, only the DSP Internal core clock is gated */
  16. #define PSU_DSP_CORE_IDLE BIT(24)
  17. #define DSP_STATUS_EXT_CTL_DSP_WAIT_EN (1 << 0)
  18. #define DSP_STATUS_EXT_CTL_DSP_EXTERNAL_WAIT (1 << 1)
  19. #define DSP_MAILBOX_REG_BASE 0x106A000
  20. #define DSP_M2D_MAILBOX_REGISTER_BASE (DSP_MAILBOX_REG_BASE + 0x00)
  21. #define DSP_D2M_MAILBOX_REGISTER_BASE (DSP_MAILBOX_REG_BASE + 0x10)
  22. #define DSP_USER_REGION_REGISTER_BASE (DSP_MAILBOX_REG_BASE + 0x20)
  23. #define DSP_DEBUG_REGION_REGISTER_BASE (DSP_MAILBOX_REG_BASE + 0x40)
  24. #define DTCM_AND_PTCM 0
  25. #define ALL_EXT_MEM 1
  26. #define CODE_ADDR 1
  27. #define DATA_ADDR 0
  28. #define DTCM_SIZE 0xBFFF
  29. #define CPU_DTCM_BASE 0x1100000
  30. #define DSP_DTCM_BASE 0
  31. #define PTCM_SIZE 0x1FFF
  32. #define CPU_PTCM_BASE 0x1140000
  33. #define DSP_PTCM_BASE 0
  34. #define DSP_SHARE_MEM_SIZE 0x7FFF
  35. #define CPU_SHARE_BASE 0x1068000
  36. #define DSP_SHARE_BASE 0x40044000
  37. #define EXT_MEM_SIZE 0x17FFF
  38. #define CPU_EXT_BASE 0x1040000
  39. #define DSP_EXT_BASE 0x40000000
  40. #define IMG_BANK_INNER_ADDR(addr) (addr & 0x4003ffff)
  41. static inline unsigned int mcu_to_dsp_address(unsigned int mcu_addr, uint8_t addr_type)
  42. {
  43. int dsp_addr = 0;
  44. /** ptcm */
  45. if (mcu_addr >= CPU_PTCM_BASE && mcu_addr <= CPU_PTCM_BASE + PTCM_SIZE && addr_type == CODE_ADDR) {
  46. dsp_addr = DSP_PTCM_BASE + (mcu_addr - CPU_PTCM_BASE) / 2;
  47. } else
  48. /** dtcm */
  49. if(mcu_addr >= CPU_DTCM_BASE && mcu_addr <= CPU_DTCM_BASE + DTCM_SIZE && addr_type == DATA_ADDR) {
  50. dsp_addr = DSP_DTCM_BASE + (mcu_addr - CPU_DTCM_BASE) / 2;
  51. } else
  52. /** ext mem */
  53. if(mcu_addr >= CPU_EXT_BASE && mcu_addr <= CPU_EXT_BASE + EXT_MEM_SIZE) {
  54. dsp_addr = DSP_EXT_BASE + (mcu_addr - CPU_EXT_BASE) / 2;
  55. } else
  56. /** share ram */
  57. if (mcu_addr >= CPU_SHARE_BASE && mcu_addr <= CPU_SHARE_BASE + DSP_SHARE_MEM_SIZE) {
  58. dsp_addr = DSP_SHARE_BASE + (mcu_addr - CPU_SHARE_BASE) / 2;
  59. }
  60. //printk("mcu_addr %x dsp_addr %x addr_type %d \n",mcu_addr,dsp_addr,addr_type);
  61. return dsp_addr;
  62. }
  63. static inline unsigned int dsp_to_mcu_address(unsigned int dsp_addr, uint8_t addr_type)
  64. {
  65. int mcu_addr = -1;
  66. /** ptcm */
  67. if (dsp_addr >= DSP_PTCM_BASE && dsp_addr <= DSP_PTCM_BASE + PTCM_SIZE && addr_type == CODE_ADDR) {
  68. mcu_addr = CPU_PTCM_BASE + (dsp_addr - DSP_PTCM_BASE) * 2;
  69. } else
  70. /** dtcm */
  71. if(dsp_addr >= DSP_DTCM_BASE && dsp_addr <= DSP_DTCM_BASE + DTCM_SIZE && addr_type == DATA_ADDR) {
  72. mcu_addr = CPU_DTCM_BASE + (dsp_addr - DSP_DTCM_BASE) * 2;
  73. } else
  74. /** ext mem */
  75. if(dsp_addr >= DSP_EXT_BASE && dsp_addr <= DSP_EXT_BASE + EXT_MEM_SIZE) {
  76. mcu_addr = CPU_EXT_BASE + (dsp_addr - DSP_EXT_BASE) * 2;
  77. } else
  78. /** share ram */
  79. if (dsp_addr >= DSP_SHARE_BASE && dsp_addr <= DSP_SHARE_BASE + DSP_SHARE_MEM_SIZE / 2) {
  80. mcu_addr = CPU_SHARE_BASE + (dsp_addr - DSP_SHARE_BASE) * 2;
  81. } else {
  82. dsp_addr = IMG_BANK_INNER_ADDR(dsp_addr);
  83. /** bank ram */
  84. if (dsp_addr >= 0x4000 && dsp_addr <= 0x4000 + EXT_MEM_SIZE / 2) {
  85. mcu_addr = CPU_EXT_BASE + dsp_addr * 2;
  86. }
  87. }
  88. //printk("dsp_addr %x mcu_addr %x addr_type %d \n",dsp_addr,mcu_addr,addr_type);
  89. return mcu_addr;
  90. }
  91. static inline unsigned int mcu_to_dsp_data_address(unsigned int mcu_addr)
  92. {
  93. return mcu_to_dsp_address(mcu_addr, DATA_ADDR);
  94. }
  95. static inline int get_hw_idle(void)
  96. {
  97. return sys_read32(DSP_STATUS_EXT_CTL) & PSU_DSP_IDLE;
  98. }
  99. static inline void dsp_do_wait(void)
  100. {
  101. sys_write32(sys_read32(DSP_STATUS_EXT_CTL)
  102. | BIT(DSP_STATUS_EXT_CTL_DSP_WAIT_EN)
  103. | BIT(DSP_STATUS_EXT_CTL_DSP_EXTERNAL_WAIT),
  104. DSP_STATUS_EXT_CTL);
  105. }
  106. static inline void dsp_undo_wait(void)
  107. {
  108. sys_write32(sys_read32(DSP_STATUS_EXT_CTL)
  109. & ~(BIT(DSP_STATUS_EXT_CTL_DSP_WAIT_EN)
  110. | BIT(DSP_STATUS_EXT_CTL_DSP_EXTERNAL_WAIT)),
  111. DSP_STATUS_EXT_CTL);
  112. }
  113. static inline void clear_dsp_pageaddr(void)
  114. {
  115. sys_write32(0, DSP_PAGE_ADDR0);
  116. sys_write32(0, DSP_PAGE_ADDR0 + 4);
  117. sys_write32(0, DSP_PAGE_ADDR0 + 8);
  118. sys_write32(0, DSP_PAGE_ADDR0 + 12);
  119. }
  120. static inline void dsp_init_clk(void)
  121. {
  122. sys_write32(sys_read32(CMU_DEVCLKEN1) | 0x01, CMU_DEVCLKEN1);
  123. #ifndef CONFIG_SOC_EP_MODE
  124. sys_write32(0x12, CMU_DSPCLK);
  125. #else
  126. sys_write32(0xe3, CMU_DSPCLK);
  127. #endif
  128. }
  129. static inline int dsp_check_hw_idle(void)
  130. {
  131. return sys_read32(DSP_STATUS_EXT_CTL) & PSU_DSP_IDLE;
  132. }
  133. static inline int dsp_soc_request_addr(int cpu_addr)
  134. {
  135. if (cpu_addr >= CPU_EXT_BASE && cpu_addr < CPU_EXT_BASE + EXT_MEM_SIZE) {
  136. sys_write32(sys_read32(CMU_MEMCLKSRC0) | (1 << 24) | (1 << 22) | (1 << 20), CMU_MEMCLKSRC0);
  137. }
  138. return 0;
  139. }
  140. static inline int dsp_soc_release_addr(int cpu_addr)
  141. {
  142. if (cpu_addr >= CPU_EXT_BASE && cpu_addr < CPU_EXT_BASE + EXT_MEM_SIZE) {
  143. sys_write32(sys_read32(CMU_MEMCLKSRC0) & (~((1 << 24) | (1 << 22) | (1 << 20))), CMU_MEMCLKSRC0);
  144. }
  145. return 0;
  146. }
  147. static inline int dsp_soc_request_mem(int type)
  148. {
  149. sys_write32((sys_read32(CMU_MEMCLKSRC0) | (1 << 24) | (1 << 22) | (1 << 20) | (1 << 12)),CMU_MEMCLKSRC0);
  150. return 0;
  151. }
  152. static inline int dsp_soc_release_mem(int type)
  153. {
  154. sys_write32((sys_read32(CMU_MEMCLKSRC0) & (~((1 << 24) | (1 << 22) | (1 << 20) | (1 << 12)))), CMU_MEMCLKSRC0);
  155. return 0;
  156. }
  157. static inline void mem_controller_dsp_pageaddr_set(uint32_t index, uint32_t value)
  158. {
  159. sys_write32(value, DSP_PAGE_ADDR0 + index * 4);
  160. }
  161. static void inline set_dsp_vector_addr(unsigned int dsp_addr)
  162. {
  163. sys_write32(dsp_addr, DSP_VCT_ADDR);
  164. }
  165. static inline void clear_dsp_irq_pending(unsigned int irq)
  166. {
  167. sys_write32(0x1, PENDING_FROM_DSP);
  168. while(sys_read32(PENDING_FROM_DSP));
  169. }
  170. static inline void clear_dsp_all_irq_pending(void)
  171. {
  172. sys_write32(0x1, PENDING_FROM_DSP);
  173. }
  174. static inline int do_request_dsp(int in_user)
  175. {
  176. //clear info
  177. sys_write32(in_user, INFO_TO_DSP);
  178. // info
  179. sys_write32(0, INT_TO_DSP);
  180. for (int i = 0; i < 10; i ++) {
  181. ;
  182. }
  183. sys_write32(1, INT_TO_DSP);
  184. return 0;
  185. }
  186. static inline void mcu_trigger_irq_to_dsp(void)
  187. {
  188. // info
  189. sys_write32(0, INT_TO_DSP);
  190. for (int i = 0; i < 10; i ++) {
  191. ;
  192. }
  193. sys_write32(1, INT_TO_DSP);
  194. }
  195. static inline void mcu_untrigger_irq_to_dsp(void)
  196. {
  197. // info
  198. //sys_write32(0, INT_TO_DSP);
  199. }
  200. static inline int dsp_dump_info(void)
  201. {
  202. printk("DSP_PAGE_ADDR0 0x%x \n",sys_read32(DSP_PAGE_ADDR0));
  203. printk("DSP_PAGE_ADDR1 0x%x \n",sys_read32(DSP_PAGE_ADDR0 + 4));
  204. printk("DSP_PAGE_ADDR2 0x%x \n",sys_read32(DSP_PAGE_ADDR0 + 8));
  205. printk("DSP_PAGE_ADDR3 0x%x \n",sys_read32(DSP_PAGE_ADDR0 + 12));
  206. printk("DSP_VCT_ADDR 0x%x \n",sys_read32(DSP_VCT_ADDR));
  207. printk("PENDING_FROM_DSP 0x%x \n",sys_read32(PENDING_FROM_DSP));
  208. printk("INFO_TO_DSP 0x%x \n",sys_read32(INFO_TO_DSP));
  209. printk("DSP_VCT_ADDR 0x%x \n",sys_read32(DSP_VCT_ADDR));
  210. printk("MRCR1 0x%x \n",sys_read32(0x40000004));
  211. printk("CMU_DEVCLKEN1 0x%x \n",sys_read32(CMU_DEVCLKEN1));
  212. printk("CMU_MEMCLKSRC0 0x%x \n",sys_read32(CMU_MEMCLKSRC0));
  213. printk("CMU_MEMCLKSRC1 0x%x \n",sys_read32(CMU_MEMCLKSRC1));
  214. printk("CMU_MEMCLKSRC2 0x%x \n",sys_read32(CMU_MEMCLKSRC2));
  215. printk("MEMORYCTL 0x%x \n",sys_read32(MEMORY_CTL));
  216. printk("PWRGATE_DIG 0x%x \n",sys_read32(0x40004000 + 0x30));
  217. printk("CMU_DSPCLK 0x%x \n",sys_read32(0x40001000 +0x90));
  218. printk("COREPLL_CTL 0x%x \n",sys_read32(COREPLL_CTL));
  219. printk("CMU_SYSCLK 0x%x \n",sys_read32(CMU_SYSCLK));
  220. return 0;
  221. }
  222. #endif /* SOC_DSP_H_ */