drv_timer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /*
  26. * Copyright (c) 2006-2025, RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #ifndef DRV_TIMER_H__
  31. #define DRV_TIMER_H__
  32. #include <stdint.h>
  33. #include <drivers/hwtimer.h>
  34. #include "sysctl_clk.h"
  35. #define MHz 1000000
  36. /* TIMER Control Register */
  37. #define TIMER_CR_ENABLE 0x00000001
  38. #define TIMER_CR_MODE_MASK 0x00000002
  39. #define TIMER_CR_FREE_MODE 0x00000000
  40. #define TIMER_CR_USER_MODE 0x00000002
  41. #define TIMER_CR_INTERRUPT_MASK 0x00000004
  42. #define TIMER_CR_PWM_ENABLE 0x00000008
  43. #define IRQN_TIMER_0_INTERRUPT 16+85
  44. #define IRQN_TIMER_1_INTERRUPT 16+86
  45. #define IRQN_TIMER_2_INTERRUPT 16+87
  46. #define IRQN_TIMER_3_INTERRUPT 16+88
  47. #define IRQN_TIMER_4_INTERRUPT 16+89
  48. #define IRQN_TIMER_5_INTERRUPT 16+90
  49. typedef struct _timer_regs_channel
  50. {
  51. /* TIMER_N Load Count Register (0x00+(N-1)*0x14) */
  52. volatile uint32_t load_count;
  53. /* TIMER_N Current Value Register (0x04+(N-1)*0x14) */
  54. volatile uint32_t current_value;
  55. /* TIMER_N Control Register (0x08+(N-1)*0x14) */
  56. volatile uint32_t control;
  57. /* TIMER_N Interrupt Clear Register (0x0c+(N-1)*0x14) */
  58. volatile uint32_t eoi;
  59. /* TIMER_N Interrupt Status Register (0x10+(N-1)*0x14) */
  60. volatile uint32_t intr_stat;
  61. } __attribute__((packed, aligned(4))) k230_timer_regs_channel_t;
  62. typedef struct _k230_timer_regs
  63. {
  64. /* TIMER_N Register (0x00-0x4c) */
  65. volatile k230_timer_regs_channel_t channel[5];
  66. /* reserverd (0x50-0x9c) */
  67. volatile uint32_t resv1[20];
  68. /* TIMER Interrupt Status Register (0xa0) */
  69. volatile uint32_t intr_stat;
  70. /* TIMER Interrupt Clear Register (0xa4) */
  71. volatile uint32_t eoi;
  72. /* TIMER Raw Interrupt Status Register (0xa8) */
  73. volatile uint32_t raw_intr_stat;
  74. /* TIMER Component Version Register (0xac) */
  75. volatile uint32_t comp_version;
  76. /* TIMER_N Load Count2 Register (0xb0-0xbc) */
  77. volatile uint32_t load_count2[4];
  78. } __attribute__((packed, aligned(4))) k230_timer_regs_t;
  79. struct k230_timer {
  80. struct rt_hwtimer_device device;
  81. const char *name;
  82. rt_ubase_t base;
  83. uint32_t id;
  84. sysctl_clk_node_e clk;
  85. sysctl_clk_node_e clk_src;
  86. int irq_num;
  87. };
  88. #endif