]>
Commit | Line | Data |
---|---|---|
0e149233 AN |
1 | /* |
2 | * TX4939 internal RTC driver | |
3 | * Based on RBTX49xx patch from CELF patch archive. | |
4 | * | |
5 | * This file is subject to the terms and conditions of the GNU General Public | |
6 | * License. See the file "COPYING" in the main directory of this archive | |
7 | * for more details. | |
8 | * | |
9 | * (C) Copyright TOSHIBA CORPORATION 2005-2007 | |
10 | */ | |
11 | #include <linux/rtc.h> | |
12 | #include <linux/platform_device.h> | |
13 | #include <linux/interrupt.h> | |
14 | #include <linux/io.h> | |
5a0e3ad6 | 15 | #include <linux/gfp.h> |
0e149233 AN |
16 | #include <asm/txx9/tx4939.h> |
17 | ||
18 | struct tx4939rtc_plat_data { | |
19 | struct rtc_device *rtc; | |
20 | struct tx4939_rtc_reg __iomem *rtcreg; | |
af69a180 | 21 | spinlock_t lock; |
0e149233 AN |
22 | }; |
23 | ||
24 | static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev) | |
25 | { | |
26 | return platform_get_drvdata(to_platform_device(dev)); | |
27 | } | |
28 | ||
29 | static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd) | |
30 | { | |
31 | int i = 0; | |
32 | ||
33 | __raw_writel(cmd, &rtcreg->ctl); | |
34 | /* This might take 30us (next 32.768KHz clock) */ | |
35 | while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) { | |
36 | /* timeout on approx. 100us (@ GBUS200MHz) */ | |
37 | if (i++ > 200 * 100) | |
38 | return -EBUSY; | |
39 | cpu_relax(); | |
40 | } | |
41 | return 0; | |
42 | } | |
43 | ||
44 | static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs) | |
45 | { | |
46 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
47 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
48 | int i, ret; | |
49 | unsigned char buf[6]; | |
50 | ||
51 | buf[0] = 0; | |
52 | buf[1] = 0; | |
53 | buf[2] = secs; | |
54 | buf[3] = secs >> 8; | |
55 | buf[4] = secs >> 16; | |
56 | buf[5] = secs >> 24; | |
af69a180 | 57 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
58 | __raw_writel(0, &rtcreg->adr); |
59 | for (i = 0; i < 6; i++) | |
60 | __raw_writel(buf[i], &rtcreg->dat); | |
61 | ret = tx4939_rtc_cmd(rtcreg, | |
62 | TX4939_RTCCTL_COMMAND_SETTIME | | |
63 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); | |
af69a180 | 64 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
65 | return ret; |
66 | } | |
67 | ||
68 | static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
69 | { | |
70 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
71 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
72 | int i, ret; | |
73 | unsigned long sec; | |
74 | unsigned char buf[6]; | |
75 | ||
af69a180 | 76 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
77 | ret = tx4939_rtc_cmd(rtcreg, |
78 | TX4939_RTCCTL_COMMAND_GETTIME | | |
79 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); | |
80 | if (ret) { | |
af69a180 | 81 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
82 | return ret; |
83 | } | |
84 | __raw_writel(2, &rtcreg->adr); | |
85 | for (i = 2; i < 6; i++) | |
86 | buf[i] = __raw_readl(&rtcreg->dat); | |
af69a180 | 87 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
88 | sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; |
89 | rtc_time_to_tm(sec, tm); | |
90 | return rtc_valid_tm(tm); | |
91 | } | |
92 | ||
93 | static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | |
94 | { | |
95 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
96 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
97 | int i, ret; | |
98 | unsigned long sec; | |
99 | unsigned char buf[6]; | |
100 | ||
101 | if (alrm->time.tm_sec < 0 || | |
102 | alrm->time.tm_min < 0 || | |
103 | alrm->time.tm_hour < 0 || | |
104 | alrm->time.tm_mday < 0 || | |
105 | alrm->time.tm_mon < 0 || | |
106 | alrm->time.tm_year < 0) | |
107 | return -EINVAL; | |
108 | rtc_tm_to_time(&alrm->time, &sec); | |
109 | buf[0] = 0; | |
110 | buf[1] = 0; | |
111 | buf[2] = sec; | |
112 | buf[3] = sec >> 8; | |
113 | buf[4] = sec >> 16; | |
114 | buf[5] = sec >> 24; | |
af69a180 | 115 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
116 | __raw_writel(0, &rtcreg->adr); |
117 | for (i = 0; i < 6; i++) | |
118 | __raw_writel(buf[i], &rtcreg->dat); | |
119 | ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM | | |
120 | (alrm->enabled ? TX4939_RTCCTL_ALME : 0)); | |
af69a180 | 121 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
122 | return ret; |
123 | } | |
124 | ||
125 | static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | |
126 | { | |
127 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
128 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
129 | int i, ret; | |
130 | unsigned long sec; | |
131 | unsigned char buf[6]; | |
132 | u32 ctl; | |
133 | ||
af69a180 | 134 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
135 | ret = tx4939_rtc_cmd(rtcreg, |
136 | TX4939_RTCCTL_COMMAND_GETALARM | | |
137 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); | |
138 | if (ret) { | |
af69a180 | 139 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
140 | return ret; |
141 | } | |
142 | __raw_writel(2, &rtcreg->adr); | |
143 | for (i = 2; i < 6; i++) | |
144 | buf[i] = __raw_readl(&rtcreg->dat); | |
145 | ctl = __raw_readl(&rtcreg->ctl); | |
146 | alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0; | |
147 | alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0; | |
af69a180 | 148 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
149 | sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; |
150 | rtc_time_to_tm(sec, &alrm->time); | |
151 | return rtc_valid_tm(&alrm->time); | |
152 | } | |
153 | ||
154 | static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) | |
155 | { | |
156 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
157 | ||
af69a180 | 158 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
159 | tx4939_rtc_cmd(pdata->rtcreg, |
160 | TX4939_RTCCTL_COMMAND_NOP | | |
161 | (enabled ? TX4939_RTCCTL_ALME : 0)); | |
af69a180 | 162 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
163 | return 0; |
164 | } | |
165 | ||
166 | static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id) | |
167 | { | |
168 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id); | |
169 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
170 | unsigned long events = RTC_IRQF; | |
171 | ||
af69a180 | 172 | spin_lock(&pdata->lock); |
0e149233 AN |
173 | if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) { |
174 | events |= RTC_AF; | |
175 | tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP); | |
176 | } | |
af69a180 AN |
177 | spin_unlock(&pdata->lock); |
178 | if (likely(pdata->rtc)) | |
179 | rtc_update_irq(pdata->rtc, 1, events); | |
0e149233 AN |
180 | return IRQ_HANDLED; |
181 | } | |
182 | ||
183 | static const struct rtc_class_ops tx4939_rtc_ops = { | |
184 | .read_time = tx4939_rtc_read_time, | |
185 | .read_alarm = tx4939_rtc_read_alarm, | |
186 | .set_alarm = tx4939_rtc_set_alarm, | |
187 | .set_mmss = tx4939_rtc_set_mmss, | |
188 | .alarm_irq_enable = tx4939_rtc_alarm_irq_enable, | |
189 | }; | |
190 | ||
2c3c8bea | 191 | static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj, |
0e149233 AN |
192 | struct bin_attribute *bin_attr, |
193 | char *buf, loff_t pos, size_t size) | |
194 | { | |
195 | struct device *dev = container_of(kobj, struct device, kobj); | |
196 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
197 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
198 | ssize_t count; | |
199 | ||
af69a180 | 200 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
201 | for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; |
202 | count++, size--) { | |
203 | __raw_writel(pos++, &rtcreg->adr); | |
204 | *buf++ = __raw_readl(&rtcreg->dat); | |
205 | } | |
af69a180 | 206 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
207 | return count; |
208 | } | |
209 | ||
2c3c8bea | 210 | static ssize_t tx4939_rtc_nvram_write(struct file *filp, struct kobject *kobj, |
0e149233 AN |
211 | struct bin_attribute *bin_attr, |
212 | char *buf, loff_t pos, size_t size) | |
213 | { | |
214 | struct device *dev = container_of(kobj, struct device, kobj); | |
215 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); | |
216 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; | |
217 | ssize_t count; | |
218 | ||
af69a180 | 219 | spin_lock_irq(&pdata->lock); |
0e149233 AN |
220 | for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; |
221 | count++, size--) { | |
222 | __raw_writel(pos++, &rtcreg->adr); | |
223 | __raw_writel(*buf++, &rtcreg->dat); | |
224 | } | |
af69a180 | 225 | spin_unlock_irq(&pdata->lock); |
0e149233 AN |
226 | return count; |
227 | } | |
228 | ||
229 | static struct bin_attribute tx4939_rtc_nvram_attr = { | |
230 | .attr = { | |
231 | .name = "nvram", | |
232 | .mode = S_IRUGO | S_IWUSR, | |
233 | }, | |
234 | .size = TX4939_RTC_REG_RAMSIZE, | |
235 | .read = tx4939_rtc_nvram_read, | |
236 | .write = tx4939_rtc_nvram_write, | |
237 | }; | |
238 | ||
239 | static int __init tx4939_rtc_probe(struct platform_device *pdev) | |
240 | { | |
241 | struct rtc_device *rtc; | |
242 | struct tx4939rtc_plat_data *pdata; | |
243 | struct resource *res; | |
244 | int irq, ret; | |
245 | ||
246 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
247 | if (!res) | |
248 | return -ENODEV; | |
249 | irq = platform_get_irq(pdev, 0); | |
250 | if (irq < 0) | |
251 | return -ENODEV; | |
252 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | |
253 | if (!pdata) | |
254 | return -ENOMEM; | |
255 | platform_set_drvdata(pdev, pdata); | |
256 | ||
257 | if (!devm_request_mem_region(&pdev->dev, res->start, | |
258 | resource_size(res), pdev->name)) | |
259 | return -EBUSY; | |
260 | pdata->rtcreg = devm_ioremap(&pdev->dev, res->start, | |
261 | resource_size(res)); | |
262 | if (!pdata->rtcreg) | |
263 | return -EBUSY; | |
264 | ||
af69a180 | 265 | spin_lock_init(&pdata->lock); |
0e149233 AN |
266 | tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); |
267 | if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt, | |
0a817f7f | 268 | IRQF_DISABLED, pdev->name, &pdev->dev) < 0) |
0e149233 | 269 | return -EBUSY; |
0e149233 AN |
270 | rtc = rtc_device_register(pdev->name, &pdev->dev, |
271 | &tx4939_rtc_ops, THIS_MODULE); | |
272 | if (IS_ERR(rtc)) | |
273 | return PTR_ERR(rtc); | |
274 | pdata->rtc = rtc; | |
275 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); | |
276 | if (ret) | |
277 | rtc_device_unregister(rtc); | |
278 | return ret; | |
279 | } | |
280 | ||
281 | static int __exit tx4939_rtc_remove(struct platform_device *pdev) | |
282 | { | |
283 | struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev); | |
0e149233 | 284 | |
0e149233 | 285 | sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); |
af69a180 AN |
286 | rtc_device_unregister(pdata->rtc); |
287 | spin_lock_irq(&pdata->lock); | |
288 | tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); | |
289 | spin_unlock_irq(&pdata->lock); | |
0e149233 AN |
290 | return 0; |
291 | } | |
292 | ||
293 | static struct platform_driver tx4939_rtc_driver = { | |
294 | .remove = __exit_p(tx4939_rtc_remove), | |
295 | .driver = { | |
296 | .name = "tx4939rtc", | |
297 | .owner = THIS_MODULE, | |
298 | }, | |
299 | }; | |
300 | ||
301 | static int __init tx4939rtc_init(void) | |
302 | { | |
303 | return platform_driver_probe(&tx4939_rtc_driver, tx4939_rtc_probe); | |
304 | } | |
305 | ||
306 | static void __exit tx4939rtc_exit(void) | |
307 | { | |
308 | platform_driver_unregister(&tx4939_rtc_driver); | |
309 | } | |
310 | ||
311 | module_init(tx4939rtc_init); | |
312 | module_exit(tx4939rtc_exit); | |
313 | ||
314 | MODULE_AUTHOR("Atsushi Nemoto <[email protected]>"); | |
315 | MODULE_DESCRIPTION("TX4939 internal RTC driver"); | |
316 | MODULE_LICENSE("GPL"); | |
317 | MODULE_ALIAS("platform:tx4939rtc"); |