drv_hardlock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include <rtdevice.h>
  28. #include <riscv_io.h>
  29. #include <string.h>
  30. #include "ioremap.h"
  31. #include "board.h"
  32. #include "drv_hardlock.h"
  33. #include <rtdbg.h>
  34. #define DBG_TAG "HARDLOCK"
  35. #ifdef RT_DEBUG
  36. #define DBG_LVL DBG_LOG
  37. #else
  38. #define DBG_LVL DBG_WARNING
  39. #endif
  40. #define DBG_COLOR
  41. struct device_hardlock
  42. {
  43. volatile void *hw_base;
  44. char used[HARDLOCK_MAX];
  45. };
  46. static struct device_hardlock hardlock;
  47. int kd_hardlock_lock(hardlock_type num)
  48. {
  49. if(num < 0 || num >= HARDLOCK_MAX)
  50. return -1;
  51. if(!readl(hardlock.hw_base + num * 0x4))
  52. {
  53. LOG_D("hardlock-%d locked\n", num);
  54. return 0;
  55. }
  56. LOG_D("hardlock-%d is busy\n", num);
  57. return -1;
  58. }
  59. RTM_EXPORT(kd_hardlock_lock);
  60. void kd_hardlock_unlock(hardlock_type num)
  61. {
  62. if(num < 0 || num >= HARDLOCK_MAX)
  63. return;
  64. if(readl(hardlock.hw_base + num * 0x4))
  65. {
  66. writel(0x0, hardlock.hw_base + num * 0x4);
  67. }
  68. LOG_D("hardlock-%d unlock\n", num);
  69. }
  70. RTM_EXPORT(kd_hardlock_unlock);
  71. int kd_request_lock(hardlock_type num)
  72. {
  73. if(num < 0 || num >= HARDLOCK_MAX)
  74. return -1;
  75. if(!hardlock.used[num])
  76. {
  77. hardlock.used[num] = 1;
  78. return 0;
  79. }
  80. LOG_E("request hardlock failed, hardlock-%d is used\n", num);
  81. return -1;
  82. }
  83. RTM_EXPORT(kd_request_lock);
  84. int rt_hw_hardlock_init(void)
  85. {
  86. struct device_hardlock *hard = &hardlock;
  87. hard->hw_base = 0xA0 + rt_ioremap((void *)MAILBOX_BASE_ADDR, MAILBOX_IO_SIZE);
  88. if(hard->hw_base == RT_NULL)
  89. {
  90. rt_kprintf("hardlock ioremap error\n");
  91. return -1;
  92. }
  93. memset(hard->used, 0, sizeof(hard->used));
  94. #if 0
  95. rt_kprintf("canaan hardlock init OK\n");
  96. #endif
  97. return 0;
  98. }
  99. INIT_BOARD_EXPORT(rt_hw_hardlock_init);