rtc-tx4939.c 8.7 KB

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