Linux 5.6.13
[linux/fpc-iii.git] / arch / arm / mach-prima2 / rtciobrg.c
blob97c0e333e3b9f264dba30c128d1e21f86594cd98
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * RTC I/O Bridge interfaces for CSR SiRFprimaII/atlas7
4 * ARM access the registers of SYSRTC, GPSRTC and PWRC through this module
6 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
7 */
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>
18 #define SIRFSOC_CPUIOBRG_CTRL 0x00
19 #define SIRFSOC_CPUIOBRG_WRBE 0x04
20 #define SIRFSOC_CPUIOBRG_ADDR 0x08
21 #define SIRFSOC_CPUIOBRG_DATA 0x0c
24 * suspend asm codes will access this address to make system deepsleep
25 * after DRAM becomes self-refresh
27 void __iomem *sirfsoc_rtciobrg_base;
28 static DEFINE_SPINLOCK(rtciobrg_lock);
31 * symbols without lock are only used by suspend asm codes
32 * and these symbols are not exported too
34 void sirfsoc_rtc_iobrg_wait_sync(void)
36 while (readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL))
37 cpu_relax();
40 void sirfsoc_rtc_iobrg_besyncing(void)
42 unsigned long flags;
44 spin_lock_irqsave(&rtciobrg_lock, flags);
46 sirfsoc_rtc_iobrg_wait_sync();
48 spin_unlock_irqrestore(&rtciobrg_lock, flags);
50 EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_besyncing);
52 u32 __sirfsoc_rtc_iobrg_readl(u32 addr)
54 sirfsoc_rtc_iobrg_wait_sync();
56 writel_relaxed(0x00, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
57 writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
58 writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
60 sirfsoc_rtc_iobrg_wait_sync();
62 return readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
65 u32 sirfsoc_rtc_iobrg_readl(u32 addr)
67 unsigned long flags, val;
69 /* TODO: add hwspinlock to sync with M3 */
70 spin_lock_irqsave(&rtciobrg_lock, flags);
72 val = __sirfsoc_rtc_iobrg_readl(addr);
74 spin_unlock_irqrestore(&rtciobrg_lock, flags);
76 return val;
78 EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_readl);
80 void sirfsoc_rtc_iobrg_pre_writel(u32 val, u32 addr)
82 sirfsoc_rtc_iobrg_wait_sync();
84 writel_relaxed(0xf1, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
85 writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
87 writel_relaxed(val, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
90 void sirfsoc_rtc_iobrg_writel(u32 val, u32 addr)
92 unsigned long flags;
94 /* TODO: add hwspinlock to sync with M3 */
95 spin_lock_irqsave(&rtciobrg_lock, flags);
97 sirfsoc_rtc_iobrg_pre_writel(val, addr);
99 writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
101 sirfsoc_rtc_iobrg_wait_sync();
103 spin_unlock_irqrestore(&rtciobrg_lock, flags);
105 EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_writel);
108 static int regmap_iobg_regwrite(void *context, unsigned int reg,
109 unsigned int val)
111 sirfsoc_rtc_iobrg_writel(val, reg);
112 return 0;
115 static int regmap_iobg_regread(void *context, unsigned int reg,
116 unsigned int *val)
118 *val = (u32)sirfsoc_rtc_iobrg_readl(reg);
119 return 0;
122 static struct regmap_bus regmap_iobg = {
123 .reg_write = regmap_iobg_regwrite,
124 .reg_read = regmap_iobg_regread,
128 * devm_regmap_init_iobg(): Initialise managed register map
130 * @iobg: Device that will be interacted with
131 * @config: Configuration for register map
133 * The return value will be an ERR_PTR() on error or a valid pointer
134 * to a struct regmap. The regmap will be automatically freed by the
135 * device management code.
137 struct regmap *devm_regmap_init_iobg(struct device *dev,
138 const struct regmap_config *config)
140 const struct regmap_bus *bus = &regmap_iobg;
142 return devm_regmap_init(dev, bus, dev, config);
144 EXPORT_SYMBOL_GPL(devm_regmap_init_iobg);
146 static const struct of_device_id rtciobrg_ids[] = {
147 { .compatible = "sirf,prima2-rtciobg" },
151 static int sirfsoc_rtciobrg_probe(struct platform_device *op)
153 struct device_node *np = op->dev.of_node;
155 sirfsoc_rtciobrg_base = of_iomap(np, 0);
156 if (!sirfsoc_rtciobrg_base)
157 panic("unable to map rtc iobrg registers\n");
159 return 0;
162 static struct platform_driver sirfsoc_rtciobrg_driver = {
163 .probe = sirfsoc_rtciobrg_probe,
164 .driver = {
165 .name = "sirfsoc-rtciobrg",
166 .of_match_table = rtciobrg_ids,
170 static int __init sirfsoc_rtciobrg_init(void)
172 return platform_driver_register(&sirfsoc_rtciobrg_driver);
174 postcore_initcall(sirfsoc_rtciobrg_init);
176 MODULE_AUTHOR("Zhiwu Song <zhiwu.song@csr.com>");
177 MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
178 MODULE_DESCRIPTION("CSR SiRFprimaII rtc io bridge");
179 MODULE_LICENSE("GPL v2");