bsp_ccu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /******************************************************************************
  2. 版权所有: @copyright (C) 2024-2034 HaiYang Technology Corp. All rights reserved.
  3. 文件名称: bsp_ccu.c
  4. 文件版本: 01.01
  5. 创建作者: zhaoyang
  6. 创建日期: 2025-07-28
  7. 功能说明: CCU模块驱动程序。
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "bsp_ccu.h"
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #include <fcntl.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <sys/mman.h>
  19. int g_ccu_fd = -1;
  20. unsigned g_ccu_mapped_size;
  21. void *g_ccu_map_base, *g_ccu_virt_addr = NULL;
  22. /******************************************************************************
  23. 函数名称: ccu_init
  24. 函数版本: 01.01
  25. 创建作者: zhaoyang
  26. 创建日期: 2025-07-28
  27. 函数说明: 映射CCU寄存器地址空间。
  28. 参数说明: 无
  29. 返回值: 无
  30. 修改记录:
  31. */
  32. int ccu_init(void)
  33. {
  34. // unsigned long read_result;
  35. off_t target;
  36. unsigned page_size, offset_in_page;
  37. unsigned width = 8 * sizeof(int);
  38. target = CCU_BASE;
  39. // 获取页面大小
  40. g_ccu_mapped_size = page_size = sysconf(_SC_PAGESIZE);
  41. offset_in_page = (unsigned)target & (page_size - 1);
  42. if (offset_in_page + width > page_size)
  43. {
  44. /* This access spans pages.
  45. * Must map two pages to make it possible: */
  46. g_ccu_mapped_size *= 2;
  47. }
  48. g_ccu_fd = open("/dev/mem", O_RDWR | O_SYNC);
  49. if (g_ccu_fd < 0)
  50. {
  51. printf("open(/dev/mem) failed.\n");
  52. return -1;
  53. }
  54. fflush(stdout);
  55. g_ccu_map_base = mmap(NULL, g_ccu_mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, g_ccu_fd, target & ~(off_t)(page_size - 1));
  56. if (g_ccu_map_base == (void *)-1)
  57. {
  58. printf("NULL pointer!\n");
  59. }
  60. else
  61. {
  62. printf("CCU map Successfull!\n");
  63. }
  64. fflush(stdout);
  65. g_ccu_virt_addr = (char *)g_ccu_map_base + offset_in_page;
  66. // printf("g_ccu_virt_addr: %p %p %x\n", g_ccu_virt_addr, g_ccu_map_base, offset_in_page);
  67. // read_result = *(volatile u_int32_t*)g_ccu_virt_addr;
  68. // printf("ccu_init res: 0x%08lx\n", read_result);
  69. return 0;
  70. }
  71. /******************************************************************************
  72. 函数名称: ccu_exit
  73. 函数版本: 01.01
  74. 创建作者: zhaoyang
  75. 创建日期: 2025-07-28
  76. 函数说明: CCU模块注销
  77. 参数说明: 无
  78. 返回值: 无
  79. 修改记录:
  80. */
  81. int ccu_exit(void)
  82. {
  83. if (g_ccu_fd >= 0)
  84. {
  85. close(g_ccu_fd);
  86. g_ccu_fd = -1;
  87. }
  88. if (munmap(g_ccu_map_base, g_ccu_mapped_size) == -1)
  89. {
  90. printf("munmap failed!");
  91. return -1;
  92. }
  93. return 0;
  94. }