timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2016 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/machine/kernel.h>
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. #include <grub/time.h>
  23. #include <grub/fdtbus.h>
  24. #include <grub/misc.h>
  25. grub_uint64_t
  26. grub_armv7_get_timer_value(void);
  27. grub_uint32_t
  28. grub_armv7_get_timer_frequency(void);
  29. grub_uint32_t
  30. grub_arm_pfr1(void);
  31. static int have_timer = 0;
  32. static volatile grub_uint32_t *sp804_regs;
  33. static grub_uint64_t
  34. sp804_get_time_ms (void)
  35. {
  36. static grub_uint32_t high, last_low;
  37. grub_uint32_t low = ~sp804_regs[1];
  38. if (last_low > low)
  39. high++;
  40. last_low = low;
  41. return grub_divmod64 ((((grub_uint64_t) high) << 32) | low,
  42. 1000, 0);
  43. }
  44. static grub_err_t
  45. sp804_attach(const struct grub_fdtbus_dev *dev)
  46. {
  47. if (have_timer)
  48. return GRUB_ERR_NONE;
  49. sp804_regs = grub_fdtbus_map_reg (dev, 0, 0);
  50. if (!grub_fdtbus_is_mapping_valid (sp804_regs))
  51. return grub_error (GRUB_ERR_IO, "could not map sp804: %p", sp804_regs);
  52. grub_install_get_time_ms (sp804_get_time_ms);
  53. have_timer = 1;
  54. return GRUB_ERR_NONE;
  55. }
  56. struct grub_fdtbus_driver sp804 =
  57. {
  58. .compatible = "arm,sp804",
  59. .attach = sp804_attach
  60. };
  61. static grub_uint32_t timer_frequency_in_khz;
  62. static grub_uint64_t
  63. generic_get_time_ms (void)
  64. {
  65. return grub_divmod64 (grub_armv7_get_timer_value(), timer_frequency_in_khz, 0);
  66. }
  67. static int
  68. try_generic_timer (void)
  69. {
  70. if (((grub_arm_pfr1 () >> 16) & 0xf) != 1)
  71. return 0;
  72. grub_printf ("freq = %x\n", grub_armv7_get_timer_frequency());
  73. timer_frequency_in_khz = 0x016e3600 / 1000; //grub_armv7_get_timer_frequency() / 1000;
  74. if (timer_frequency_in_khz == 0)
  75. return 0;
  76. grub_install_get_time_ms (generic_get_time_ms);
  77. have_timer = 1;
  78. return 1;
  79. }
  80. void
  81. grub_machine_timer_init (void)
  82. {
  83. grub_fdtbus_register (&sp804);
  84. if (!have_timer)
  85. try_generic_timer ();
  86. if (!have_timer)
  87. grub_fatal ("No timer found");
  88. }