time.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2011 Picochip Ltd., Jamie Iles
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * All enquiries to support@picochip.com
  9. */
  10. #include <linux/dw_apb_timer.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <asm/mach/time.h>
  15. #include <asm/sched_clock.h>
  16. #include "common.h"
  17. static void timer_get_base_and_rate(struct device_node *np,
  18. void __iomem **base, u32 *rate)
  19. {
  20. *base = of_iomap(np, 0);
  21. if (!*base)
  22. panic("Unable to map regs for %s", np->name);
  23. if (of_property_read_u32(np, "clock-freq", rate))
  24. panic("No clock-freq property for %s", np->name);
  25. }
  26. static void picoxcell_add_clockevent(struct device_node *event_timer)
  27. {
  28. void __iomem *iobase;
  29. struct dw_apb_clock_event_device *ced;
  30. u32 irq, rate;
  31. irq = irq_of_parse_and_map(event_timer, 0);
  32. if (irq == NO_IRQ)
  33. panic("No IRQ for clock event timer");
  34. timer_get_base_and_rate(event_timer, &iobase, &rate);
  35. ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
  36. rate);
  37. if (!ced)
  38. panic("Unable to initialise clockevent device");
  39. dw_apb_clockevent_register(ced);
  40. }
  41. static void picoxcell_add_clocksource(struct device_node *source_timer)
  42. {
  43. void __iomem *iobase;
  44. struct dw_apb_clocksource *cs;
  45. u32 rate;
  46. timer_get_base_and_rate(source_timer, &iobase, &rate);
  47. cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
  48. if (!cs)
  49. panic("Unable to initialise clocksource device");
  50. dw_apb_clocksource_start(cs);
  51. dw_apb_clocksource_register(cs);
  52. }
  53. static void __iomem *sched_io_base;
  54. static u32 picoxcell_read_sched_clock(void)
  55. {
  56. return __raw_readl(sched_io_base);
  57. }
  58. static const struct of_device_id picoxcell_rtc_ids[] __initconst = {
  59. { .compatible = "picochip,pc3x2-rtc" },
  60. { /* Sentinel */ },
  61. };
  62. static void picoxcell_init_sched_clock(void)
  63. {
  64. struct device_node *sched_timer;
  65. u32 rate;
  66. sched_timer = of_find_matching_node(NULL, picoxcell_rtc_ids);
  67. if (!sched_timer)
  68. panic("No RTC for sched clock to use");
  69. timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
  70. of_node_put(sched_timer);
  71. setup_sched_clock(picoxcell_read_sched_clock, 32, rate);
  72. }
  73. static const struct of_device_id picoxcell_timer_ids[] __initconst = {
  74. { .compatible = "picochip,pc3x2-timer" },
  75. {},
  76. };
  77. static void __init picoxcell_timer_init(void)
  78. {
  79. struct device_node *event_timer, *source_timer;
  80. event_timer = of_find_matching_node(NULL, picoxcell_timer_ids);
  81. if (!event_timer)
  82. panic("No timer for clockevent");
  83. picoxcell_add_clockevent(event_timer);
  84. source_timer = of_find_matching_node(event_timer, picoxcell_timer_ids);
  85. if (!source_timer)
  86. panic("No timer for clocksource");
  87. picoxcell_add_clocksource(source_timer);
  88. of_node_put(source_timer);
  89. picoxcell_init_sched_clock();
  90. }
  91. struct sys_timer picoxcell_timer = {
  92. .init = picoxcell_timer_init,
  93. };