rtc-tx4939.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. 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. irq = platform_get_irq(pdev, 0);
  223. if (irq < 0)
  224. return -ENODEV;
  225. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  226. if (!pdata)
  227. return -ENOMEM;
  228. platform_set_drvdata(pdev, pdata);
  229. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  230. pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
  231. if (IS_ERR(pdata->rtcreg))
  232. return PTR_ERR(pdata->rtcreg);
  233. spin_lock_init(&pdata->lock);
  234. tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
  235. if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
  236. 0, pdev->name, &pdev->dev) < 0)
  237. return -EBUSY;
  238. rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  239. &tx4939_rtc_ops, THIS_MODULE);
  240. if (IS_ERR(rtc))
  241. return PTR_ERR(rtc);
  242. pdata->rtc = rtc;
  243. ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
  244. return ret;
  245. }
  246. static int __exit tx4939_rtc_remove(struct platform_device *pdev)
  247. {
  248. struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
  249. sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
  250. spin_lock_irq(&pdata->lock);
  251. tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
  252. spin_unlock_irq(&pdata->lock);
  253. return 0;
  254. }
  255. static struct platform_driver tx4939_rtc_driver = {
  256. .remove = __exit_p(tx4939_rtc_remove),
  257. .driver = {
  258. .name = "tx4939rtc",
  259. },
  260. };
  261. module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
  262. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  263. MODULE_DESCRIPTION("TX4939 internal RTC driver");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:tx4939rtc");