mmio.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Generic MMIO clocksource support
  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. #include <linux/clocksource.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. struct clocksource_mmio {
  13. void __iomem *reg;
  14. struct clocksource clksrc;
  15. };
  16. static inline struct clocksource_mmio *to_mmio_clksrc(struct clocksource *c)
  17. {
  18. return container_of(c, struct clocksource_mmio, clksrc);
  19. }
  20. cycle_t clocksource_mmio_readl_up(struct clocksource *c)
  21. {
  22. return readl_relaxed(to_mmio_clksrc(c)->reg);
  23. }
  24. cycle_t clocksource_mmio_readl_down(struct clocksource *c)
  25. {
  26. return ~readl_relaxed(to_mmio_clksrc(c)->reg);
  27. }
  28. cycle_t clocksource_mmio_readw_up(struct clocksource *c)
  29. {
  30. return readw_relaxed(to_mmio_clksrc(c)->reg);
  31. }
  32. cycle_t clocksource_mmio_readw_down(struct clocksource *c)
  33. {
  34. return ~(unsigned)readw_relaxed(to_mmio_clksrc(c)->reg);
  35. }
  36. /**
  37. * clocksource_mmio_init - Initialize a simple mmio based clocksource
  38. * @base: Virtual address of the clock readout register
  39. * @name: Name of the clocksource
  40. * @hz: Frequency of the clocksource in Hz
  41. * @rating: Rating of the clocksource
  42. * @bits: Number of valid bits
  43. * @read: One of clocksource_mmio_read*() above
  44. */
  45. int __init clocksource_mmio_init(void __iomem *base, const char *name,
  46. unsigned long hz, int rating, unsigned bits,
  47. cycle_t (*read)(struct clocksource *))
  48. {
  49. struct clocksource_mmio *cs;
  50. if (bits > 32 || bits < 16)
  51. return -EINVAL;
  52. cs = kzalloc(sizeof(struct clocksource_mmio), GFP_KERNEL);
  53. if (!cs)
  54. return -ENOMEM;
  55. cs->reg = base;
  56. cs->clksrc.name = name;
  57. cs->clksrc.rating = rating;
  58. cs->clksrc.read = read;
  59. cs->clksrc.mask = CLOCKSOURCE_MASK(bits);
  60. cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  61. return clocksource_register_hz(&cs->clksrc, hz);
  62. }