hal_eise.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * ===========================================================================================
  3. *
  4. * Filename: hal_eise.c
  5. *
  6. * Description: hal impl. of eise.
  7. *
  8. * Version: Melis3.0
  9. * Create: 2020-01-08 14:20:56
  10. * Revision: none
  11. * Compiler:
  12. *
  13. * Author: ganqiuye(ganqiuye@allwinnertech.com)
  14. * Organization: SWC-MPD
  15. * Last Modified: 2020-04-02 17:33:33
  16. *
  17. * ===========================================================================================
  18. */
  19. #include <interrupt.h>
  20. #include <hal_queue.h>
  21. #include <sunxi_hal_common.h>
  22. #include "sunxi_hal_eise.h"
  23. #include <init.h>
  24. #include <hal_clk.h>
  25. #include <interrupt.h>
  26. #include <log.h>
  27. #define eise_err(x, arg...) printf("[EISE_ERR] (%s, %d)"x"\n", __func__, __LINE__, ##arg)
  28. #define eise_warn(x, arg...) printf("[EISE_WARN] (%s, %d)"x"\n", __func__, __LINE__, ##arg)
  29. #define eise_print(x, arg...) printf("[EISE] (%s, %d)"x"\n", __func__, __LINE__, ##arg)
  30. #define eise_debug(x, arg...) printf("[EISE_DEBUG] (%s, %d)"x"\n", __func__, __LINE__, ##arg)
  31. typedef enum
  32. {
  33. EISE_WRITE_REGISTER = 0x100,
  34. EISE_READ_REGISTER,
  35. ENABLE_EISE,
  36. DISABLE_EISE,
  37. WAIT_EISE_FINISH,
  38. SET_EISE_FREQ
  39. } hal_eise_transfer_cmd_t;
  40. #define EISE_CLK_HIGH_WATER (700)
  41. #define EISE_CLK_LOW_WATER (300)
  42. #define EISE_DEFAULT_RATE (432000000)
  43. #define EISE_INTERRUPT_ID (58)
  44. #define EISE_BASE_ADDR (0x02300000)
  45. #define CCMU_BASE_ADDR (0x03001000)
  46. static hal_eise_t sHalEise;
  47. static irqreturn_t eise_interrupt(int irq, void* dev_id)
  48. {
  49. int irq_status;
  50. hal_eise_t* pHe = &sHalEise;
  51. hal_writel(0x00, pHe->eise_base_addr + EISE_INTERRUPT_EN);
  52. irq_status = hal_readl(pHe->eise_base_addr + EISE_INTERRUPT_STATUS);
  53. if (0x00 != (irq_status & 0x07))
  54. {
  55. hal_sem_post(pHe->hal_sem);
  56. }
  57. /*
  58. if (0x01 != (irq_status & 0x01))
  59. {
  60. pHe->err_cnt++;
  61. }
  62. pHe->interrupt_times++;
  63. */
  64. return IRQ_HANDLED;
  65. }
  66. static int32_t eise_hal_init(int32_t dev)
  67. {
  68. hal_eise_t* pHe = &sHalEise;
  69. pHe->irq_id = EISE_INTERRUPT_ID;
  70. pHe->eise_base_addr = EISE_BASE_ADDR;
  71. pHe->ccmu_base_addr = CCMU_BASE_ADDR;
  72. pHe->pclk = HAL_CLK_PLL_PERI0;
  73. pHe->mclk = HAL_CLK_PERIPH_EISE;
  74. hal_clk_set_parent(pHe->mclk, pHe->pclk);
  75. pHe->hal_sem = hal_sem_create(0);
  76. if (pHe->hal_sem == NULL)
  77. {
  78. eise_err("creating hal semaphore failed\n");
  79. return SUNXI_HAL_ERROR;
  80. }
  81. int ret = request_irq(pHe->irq_id, (irq_handler_t)eise_interrupt, 0, "sunxi_eise", NULL);
  82. if (ret < 0) {
  83. eise_err("Request EISE Irq error! return %d\n", ret);
  84. return SUNXI_HAL_ERROR;
  85. } else {
  86. //eise_print("Request EISE Irq success! irq_id = %d, return %d\n", pHe->irq_id, ret);
  87. }
  88. enable_irq(pHe->irq_id);
  89. return SUNXI_HAL_OK;
  90. }
  91. static int32_t eise_hal_uninit(int32_t dev)
  92. {
  93. hal_eise_t* pHe = &sHalEise;
  94. hal_clock_disable(pHe->mclk);
  95. free_irq(pHe->irq_id, NULL);
  96. eise_print("EISE device has been removed!\n");
  97. return 0;
  98. }
  99. static int32_t eise_hal_write(int32_t dev, const char *data, uint32_t num)
  100. {
  101. return 0;
  102. }
  103. static int32_t eise_hal_read(int32_t dev, int *data, uint32_t size)
  104. {
  105. return 0;
  106. }
  107. static int32_t eise_hal_ctl(int32_t dev, uint32_t cmd, void* arg)
  108. {
  109. hal_eise_t* pHe = &sHalEise;
  110. struct eise_register reg;
  111. long ret = 0;
  112. int hal_sem_ret;
  113. unsigned int rData = 0x5a5a5a5a;
  114. memcpy(&reg, arg, sizeof(struct eise_register));
  115. switch (cmd) {
  116. case EISE_WRITE_REGISTER:
  117. hal_writel(reg.value, pHe->eise_base_addr + reg.addr);
  118. return 0;
  119. case EISE_READ_REGISTER:
  120. rData = hal_readl(pHe->eise_base_addr + reg.addr);
  121. return rData;
  122. case ENABLE_EISE:
  123. if (hal_clock_enable(pHe->mclk))
  124. eise_err("[SYS] enable eise_moduleclk failed!\n");
  125. break;
  126. case DISABLE_EISE:
  127. hal_clock_disable(pHe->mclk);
  128. break;
  129. case WAIT_EISE_FINISH:
  130. hal_sem_ret = hal_sem_timedwait(pHe->hal_sem, reg.value);//1 tick = 10ms
  131. if (hal_sem_ret < 0)
  132. eise_print("[SYS] wait_event_timeout!\n");
  133. break;
  134. case SET_EISE_FREQ:
  135. {
  136. unsigned int arg_rate = reg.value;
  137. if (arg_rate >= EISE_CLK_LOW_WATER &&
  138. arg_rate <= EISE_CLK_HIGH_WATER) {
  139. if (!hal_clk_set_rate(pHe->mclk, arg_rate*1000000)) {
  140. //eise_err("set clock failed!\n");
  141. }
  142. else
  143. {
  144. //eise_print("set clock success!");
  145. }
  146. }
  147. //ret = hal_clk_get_rate(pHe->mclk);
  148. //eise_print("eise real_fre=%ld\n", ret);
  149. break;
  150. }
  151. default:
  152. {
  153. eise_warn("[SYS] do not supprot this ioctrl now!\n");
  154. break;
  155. }
  156. }
  157. return 0;
  158. }
  159. const sunxi_hal_driver_eise_t sunxi_hal_eise_driver =
  160. {
  161. .initialize = eise_hal_init,
  162. .uninitialize = eise_hal_uninit,
  163. .send = eise_hal_write,
  164. .receive = eise_hal_read,
  165. .control = eise_hal_ctl,
  166. };
  167. int eise_driver_init(void)
  168. {
  169. __log("eise hal driver init");
  170. return 0;
  171. }
  172. //late_initcall(eise_driver_init);