rtc-tx4939.c 8.6 KB

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