blob: d4852d24dc7d44010c400ba9425847f96c81eee8 [file] [log] [blame]
Zhiwu Song684f7412011-08-30 19:20:34 -07001/*
Guo Zengb1999472015-04-14 11:55:55 +00002 * RTC I/O Bridge interfaces for CSR SiRFprimaII/atlas7
Zhiwu Song684f7412011-08-30 19:20:34 -07003 * 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
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/io.h>
Guo Zengb1999472015-04-14 11:55:55 +000013#include <linux/regmap.h>
Zhiwu Song684f7412011-08-30 19:20:34 -070014#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/of_device.h>
17#include <linux/of_platform.h>
18
19#define SIRFSOC_CPUIOBRG_CTRL 0x00
20#define SIRFSOC_CPUIOBRG_WRBE 0x04
21#define SIRFSOC_CPUIOBRG_ADDR 0x08
22#define SIRFSOC_CPUIOBRG_DATA 0x0c
23
24/*
25 * suspend asm codes will access this address to make system deepsleep
26 * after DRAM becomes self-refresh
27 */
28void __iomem *sirfsoc_rtciobrg_base;
29static DEFINE_SPINLOCK(rtciobrg_lock);
30
31/*
32 * symbols without lock are only used by suspend asm codes
33 * and these symbols are not exported too
34 */
35void sirfsoc_rtc_iobrg_wait_sync(void)
36{
37 while (readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL))
38 cpu_relax();
39}
40
41void sirfsoc_rtc_iobrg_besyncing(void)
42{
43 unsigned long flags;
44
45 spin_lock_irqsave(&rtciobrg_lock, flags);
46
47 sirfsoc_rtc_iobrg_wait_sync();
48
49 spin_unlock_irqrestore(&rtciobrg_lock, flags);
50}
51EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_besyncing);
52
53u32 __sirfsoc_rtc_iobrg_readl(u32 addr)
54{
55 sirfsoc_rtc_iobrg_wait_sync();
56
57 writel_relaxed(0x00, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
58 writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
59 writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
60
61 sirfsoc_rtc_iobrg_wait_sync();
62
63 return readl_relaxed(sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
64}
65
66u32 sirfsoc_rtc_iobrg_readl(u32 addr)
67{
68 unsigned long flags, val;
69
Guo Zengb1999472015-04-14 11:55:55 +000070 /* TODO: add hwspinlock to sync with M3 */
Zhiwu Song684f7412011-08-30 19:20:34 -070071 spin_lock_irqsave(&rtciobrg_lock, flags);
72
73 val = __sirfsoc_rtc_iobrg_readl(addr);
74
75 spin_unlock_irqrestore(&rtciobrg_lock, flags);
76
77 return val;
78}
79EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_readl);
80
81void sirfsoc_rtc_iobrg_pre_writel(u32 val, u32 addr)
82{
83 sirfsoc_rtc_iobrg_wait_sync();
84
85 writel_relaxed(0xf1, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_WRBE);
86 writel_relaxed(addr, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_ADDR);
87
88 writel_relaxed(val, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_DATA);
89}
90
91void sirfsoc_rtc_iobrg_writel(u32 val, u32 addr)
92{
93 unsigned long flags;
94
Guo Zengb1999472015-04-14 11:55:55 +000095 /* TODO: add hwspinlock to sync with M3 */
Zhiwu Song684f7412011-08-30 19:20:34 -070096 spin_lock_irqsave(&rtciobrg_lock, flags);
97
98 sirfsoc_rtc_iobrg_pre_writel(val, addr);
99
100 writel_relaxed(0x01, sirfsoc_rtciobrg_base + SIRFSOC_CPUIOBRG_CTRL);
101
102 sirfsoc_rtc_iobrg_wait_sync();
103
104 spin_unlock_irqrestore(&rtciobrg_lock, flags);
105}
106EXPORT_SYMBOL_GPL(sirfsoc_rtc_iobrg_writel);
107
Guo Zengb1999472015-04-14 11:55:55 +0000108
109static int regmap_iobg_regwrite(void *context, unsigned int reg,
110 unsigned int val)
111{
112 sirfsoc_rtc_iobrg_writel(val, reg);
113 return 0;
114}
115
116static int regmap_iobg_regread(void *context, unsigned int reg,
117 unsigned int *val)
118{
119 *val = (u32)sirfsoc_rtc_iobrg_readl(reg);
120 return 0;
121}
122
123static struct regmap_bus regmap_iobg = {
124 .reg_write = regmap_iobg_regwrite,
125 .reg_read = regmap_iobg_regread,
126};
127
128/**
129 * devm_regmap_init_iobg(): Initialise managed register map
130 *
131 * @iobg: Device that will be interacted with
132 * @config: Configuration for register map
133 *
134 * The return value will be an ERR_PTR() on error or a valid pointer
135 * to a struct regmap. The regmap will be automatically freed by the
136 * device management code.
137 */
138struct regmap *devm_regmap_init_iobg(struct device *dev,
139 const struct regmap_config *config)
140{
141 const struct regmap_bus *bus = &regmap_iobg;
142
143 return devm_regmap_init(dev, bus, dev, config);
144}
145EXPORT_SYMBOL_GPL(devm_regmap_init_iobg);
146
Zhiwu Song684f7412011-08-30 19:20:34 -0700147static const struct of_device_id rtciobrg_ids[] = {
148 { .compatible = "sirf,prima2-rtciobg" },
149 {}
150};
151
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800152static int sirfsoc_rtciobrg_probe(struct platform_device *op)
Zhiwu Song684f7412011-08-30 19:20:34 -0700153{
154 struct device_node *np = op->dev.of_node;
155
156 sirfsoc_rtciobrg_base = of_iomap(np, 0);
157 if (!sirfsoc_rtciobrg_base)
158 panic("unable to map rtc iobrg registers\n");
159
160 return 0;
161}
162
163static struct platform_driver sirfsoc_rtciobrg_driver = {
164 .probe = sirfsoc_rtciobrg_probe,
165 .driver = {
166 .name = "sirfsoc-rtciobrg",
Zhiwu Song684f7412011-08-30 19:20:34 -0700167 .of_match_table = rtciobrg_ids,
168 },
169};
170
171static int __init sirfsoc_rtciobrg_init(void)
172{
173 return platform_driver_register(&sirfsoc_rtciobrg_driver);
174}
175postcore_initcall(sirfsoc_rtciobrg_init);
176
Guo Zengb1999472015-04-14 11:55:55 +0000177MODULE_AUTHOR("Zhiwu Song <zhiwu.song@csr.com>");
178MODULE_AUTHOR("Barry Song <baohua.song@csr.com>");
Zhiwu Song684f7412011-08-30 19:20:34 -0700179MODULE_DESCRIPTION("CSR SiRFprimaII rtc io bridge");
Barry Song5b7356b2014-02-18 14:46:30 +0800180MODULE_LICENSE("GPL v2");