arc_timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2016 Synopsys, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <timer.h>
  10. #include <asm/arcregs.h>
  11. #include <asm/io.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define NH_MODE (1 << 1)
  14. /*
  15. * ARC timer control registers are mapped to auxiliary address space.
  16. * There are special ARC asm command to access that addresses.
  17. * Therefore we use built-in functions to read from and write to timer
  18. * control register.
  19. */
  20. /* Driver private data. Contains timer id. Could be either 0 or 1. */
  21. struct arc_timer_priv {
  22. uint timer_id;
  23. };
  24. static int arc_timer_get_count(struct udevice *dev, u64 *count)
  25. {
  26. u32 val = 0;
  27. struct arc_timer_priv *priv = dev_get_priv(dev);
  28. switch (priv->timer_id) {
  29. case 0:
  30. val = read_aux_reg(ARC_AUX_TIMER0_CNT);
  31. break;
  32. case 1:
  33. val = read_aux_reg(ARC_AUX_TIMER1_CNT);
  34. break;
  35. }
  36. *count = timer_conv_64(val);
  37. return 0;
  38. }
  39. static int arc_timer_probe(struct udevice *dev)
  40. {
  41. int id;
  42. struct arc_timer_priv *priv = dev_get_priv(dev);
  43. /* Get registers offset and size */
  44. id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
  45. if (id < 0)
  46. return -EINVAL;
  47. if (id > 1)
  48. return -ENXIO;
  49. priv->timer_id = (uint)id;
  50. /*
  51. * In ARC core there're special registers (Auxiliary or AUX) in its
  52. * separate memory space that are used for accessing some hardware
  53. * features of the core. They are not mapped in normal memory space
  54. * and also always have the same location regardless core configuration.
  55. * Thus to simplify understanding of the programming model we chose to
  56. * access AUX regs of Timer0 and Timer1 separately instead of using
  57. * offsets from some base address.
  58. */
  59. switch (priv->timer_id) {
  60. case 0:
  61. /* Disable timer if CPU is halted */
  62. write_aux_reg(ARC_AUX_TIMER0_CTRL, NH_MODE);
  63. /* Set max value for counter/timer */
  64. write_aux_reg(ARC_AUX_TIMER0_LIMIT, 0xffffffff);
  65. /* Set initial count value and restart counter/timer */
  66. write_aux_reg(ARC_AUX_TIMER0_CNT, 0);
  67. break;
  68. case 1:
  69. /* Disable timer if CPU is halted */
  70. write_aux_reg(ARC_AUX_TIMER1_CTRL, NH_MODE);
  71. /* Set max value for counter/timer */
  72. write_aux_reg(ARC_AUX_TIMER1_LIMIT, 0xffffffff);
  73. /* Set initial count value and restart counter/timer */
  74. write_aux_reg(ARC_AUX_TIMER1_CNT, 0);
  75. break;
  76. }
  77. return 0;
  78. }
  79. static const struct timer_ops arc_timer_ops = {
  80. .get_count = arc_timer_get_count,
  81. };
  82. static const struct udevice_id arc_timer_ids[] = {
  83. { .compatible = "snps,arc-timer" },
  84. {}
  85. };
  86. U_BOOT_DRIVER(arc_timer) = {
  87. .name = "arc_timer",
  88. .id = UCLASS_TIMER,
  89. .of_match = arc_timer_ids,
  90. .probe = arc_timer_probe,
  91. .ops = &arc_timer_ops,
  92. .flags = DM_FLAG_PRE_RELOC,
  93. .priv_auto_alloc_size = sizeof(struct arc_timer_priv),
  94. };