rtciobrg.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * RTC I/O Bridge interfaces for CSR SiRFprimaII/atlas7
  3. * ARM access the registers of SYSRTC, GPSRTC and PWRC through this module
  4. *
  5. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  6. *
  7. * Licensed under GPLv2 or later.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/io.h>
  12. #include <linux/regmap.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_platform.h>
  17. #define SIRFSOC_CPUIOBRG_CTRL 0x00
  18. #define SIRFSOC_CPUIOBRG_WRBE 0x04
  19. #define SIRFSOC_CPUIOBRG_ADDR 0x08
  20. #define SIRFSOC_CPUIOBRG_DATA 0x0c
  21. /*
  22. * suspend asm codes will access this address to make system deepsleep
  23. * after DRAM becomes self-refresh
  24. */
  25. void __iomem *sirfsoc_rtciobrg_base;
  26. static DEFINE_SPINLOCK(rtciobrg_lock);
  27. /*
  28. * symbols without lock are only used by suspend asm codes
  29. * and these symbols are not exported too
  30. */
  31. void sirfsoc_rtc_iobrg_wait_sync(void)
  32. {
  33. while (readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL))
  34. cpu_relax();
  35. }
  36. void sirfsoc_rtc_iobrg_besyncing(void)
  37. {
  38. unsigned long flags;
  39. spin_lock_irqsave(&rtciobrg_lock, flags);
  40. sirfsoc_rtc_iobrg_wait_sync();
  41. spin_unlock_irqrestore(&rtciobrg_lock, flags);
  42. }
  43. EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_besyncing);
  44. u32 __sirfsoc_rtc_iobrg_readl(u32 addr)
  45. {
  46. sirfsoc_rtc_iobrg_wait_sync();
  47. writel_relaxed(0x00, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
  48. writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
  49. writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
  50. sirfsoc_rtc_iobrg_wait_sync();
  51. return readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
  52. }
  53. u32 sirfsoc_rtc_iobrg_readl(u32 addr)
  54. {
  55. unsigned long flags, val;
  56. /* TODO: add hwspinlock to sync with M3 */
  57. spin_lock_irqsave(&rtciobrg_lock, flags);
  58. val = __sirfsoc_rtc_iobrg_readl(addr);
  59. spin_unlock_irqrestore(&rtciobrg_lock, flags);
  60. return val;
  61. }
  62. EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_readl);
  63. void sirfsoc_rtc_iobrg_pre_writel(u32 val, u32 addr)
  64. {
  65. sirfsoc_rtc_iobrg_wait_sync();
  66. writel_relaxed(0xf1, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
  67. writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
  68. writel_relaxed(val, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
  69. }
  70. void sirfsoc_rtc_iobrg_writel(u32 val, u32 addr)
  71. {
  72. unsigned long flags;
  73. /* TODO: add hwspinlock to sync with M3 */
  74. spin_lock_irqsave(&rtciobrg_lock, flags);
  75. sirfsoc_rtc_iobrg_pre_writel(val, addr);
  76. writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
  77. sirfsoc_rtc_iobrg_wait_sync();
  78. spin_unlock_irqrestore(&rtciobrg_lock, flags);
  79. }
  80. EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_writel);
  81. static int regmap_iobg_regwrite(void *context, unsigned int reg,
  82. unsigned int val)
  83. {
  84. sirfsoc_rtc_iobrg_writel(val, reg);
  85. return 0;
  86. }
  87. static int regmap_iobg_regread(void *context, unsigned int reg,
  88. unsigned int *val)
  89. {
  90. *val = (u32)sirfsoc_rtc_iobrg_readl(reg);
  91. return 0;
  92. }
  93. static struct regmap_bus regmap_iobg = {
  94. .reg_write = regmap_iobg_regwrite,
  95. .reg_read = regmap_iobg_regread,
  96. };
  97. /**
  98. * devm_regmap_init_iobg(): Initialise managed register map
  99. *
  100. * @iobg: Device that will be interacted with
  101. * @config: Configuration for register map
  102. *
  103. * The return value will be an ERR_PTR() on error or a valid pointer
  104. * to a struct regmap. The regmap will be automatically freed by the
  105. * device management code.
  106. */
  107. struct regmap *devm_regmap_init_iobg(struct device *dev,
  108. const struct regmap_config *config)
  109. {
  110. const struct regmap_bus *bus = &regmap_iobg;
  111. return devm_regmap_init(dev, bus, dev, config);
  112. }
  113. EXPORT_SYMBOL_GPL(devm_regmap_init_iobg);
  114. static const struct of_device_id rtciobrg_ids[] = {
  115. { .compatible = "sirf,prima2-rtciobg" },
  116. {}
  117. };
  118. static int sirfsoc_rtciobrg_probe(struct platform_device *op)
  119. {
  120. struct device_node *np = op->dev.of_node;
  121. sirfsoc_rtciobrg_base = of_iomap(np, 0);
  122. if (!sirfsoc_rtciobrg_base)
  123. panic("unable to map rtc iobrg registers\n");
  124. return 0;
  125. }
  126. static struct platform_driver sirfsoc_rtciobrg_driver = {
  127. .probe = sirfsoc_rtciobrg_probe,
  128. .driver = {
  129. .name = "sirfsoc-rtciobrg",
  130. .of_match_table = rtciobrg_ids,
  131. },
  132. };
  133. static int __init sirfsoc_rtciobrg_init(void)
  134. {
  135. return platform_driver_register(&sirfsoc_rtciobrg_driver);
  136. }
  137. postcore_initcall(sirfsoc_rtciobrg_init);
  138. MODULE_AUTHOR("Zhiwu Song <zhiwu.song@csr.com>");
  139. MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
  140. MODULE_DESCRIPTION("CSR SiRFprimaII rtc io bridge");
  141. MODULE_LICENSE("GPL v2");