drv_timer.h 3.7 KB

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