bsp_ccu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /* This access spans pages.
  44. * Must map two pages to make it possible: */
  45. g_ccu_mapped_size *= 2;
  46. }
  47. g_ccu_fd = open("/dev/mem", O_RDWR | O_SYNC);
  48. if (g_ccu_fd < 0)
  49. {
  50. printf("open(/dev/mem) failed.\n");
  51. return -1;
  52. }
  53. fflush(stdout);
  54. 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));
  55. if (g_ccu_map_base == (void *)-1)
  56. {
  57. printf ("NULL pointer!\n");
  58. }
  59. else
  60. {
  61. printf ("CCU map Successfull!\n");
  62. }
  63. fflush(stdout);
  64. g_ccu_virt_addr = (char*)g_ccu_map_base + offset_in_page;
  65. printf("g_ccu_virt_addr: %p %p %x\n", g_ccu_virt_addr, g_ccu_map_base, offset_in_page);
  66. // read_result = *(volatile u_int32_t*)g_ccu_virt_addr;
  67. // printf("ccu_init res: 0x%08lx\n", read_result);
  68. return 0;
  69. }
  70. /******************************************************************************
  71. 函数名称: ccu_exit
  72. 函数版本: 01.01
  73. 创建作者: zhaoyang
  74. 创建日期: 2025-07-28
  75. 函数说明: CCU模块注销
  76. 参数说明: 无
  77. 返回值: 无
  78. 修改记录:
  79. */
  80. int ccu_exit(void)
  81. {
  82. if (g_ccu_fd >= 0)
  83. {
  84. close(g_ccu_fd);
  85. g_ccu_fd = -1;
  86. }
  87. if (munmap(g_ccu_map_base, g_ccu_mapped_size) == -1) {
  88. printf("munmap failed!");
  89. return -1;
  90. }
  91. return 0;
  92. }