rtc-tx4939.c 8.7 KB

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