rtc-tx4939.c 8.2 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 = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
  82. (buf[3] << 8) | buf[2];
  83. rtc_time_to_tm(sec, tm);
  84. return rtc_valid_tm(tm);
  85. }
  86. static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  87. {
  88. struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
  89. struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
  90. int i, ret;
  91. unsigned long sec;
  92. unsigned char buf[6];
  93. if (alrm->time.tm_sec < 0 ||
  94. alrm->time.tm_min < 0 ||
  95. alrm->time.tm_hour < 0 ||
  96. alrm->time.tm_mday < 0 ||
  97. alrm->time.tm_mon < 0 ||
  98. alrm->time.tm_year < 0)
  99. return -EINVAL;
  100. rtc_tm_to_time(&alrm->time, &sec);
  101. buf[0] = 0;
  102. buf[1] = 0;
  103. buf[2] = sec;
  104. buf[3] = sec >> 8;
  105. buf[4] = sec >> 16;
  106. buf[5] = sec >> 24;
  107. spin_lock_irq(&pdata->lock);
  108. __raw_writel(0, &rtcreg->adr);
  109. for (i = 0; i < 6; i++)
  110. __raw_writel(buf[i], &rtcreg->dat);
  111. ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
  112. (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
  113. spin_unlock_irq(&pdata->lock);
  114. return ret;
  115. }
  116. static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  117. {
  118. struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
  119. struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
  120. int i, ret;
  121. unsigned long sec;
  122. unsigned char buf[6];
  123. u32 ctl;
  124. spin_lock_irq(&pdata->lock);
  125. ret = tx4939_rtc_cmd(rtcreg,
  126. TX4939_RTCCTL_COMMAND_GETALARM |
  127. (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
  128. if (ret) {
  129. spin_unlock_irq(&pdata->lock);
  130. return ret;
  131. }
  132. __raw_writel(2, &rtcreg->adr);
  133. for (i = 2; i < 6; i++)
  134. buf[i] = __raw_readl(&rtcreg->dat);
  135. ctl = __raw_readl(&rtcreg->ctl);
  136. alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
  137. alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
  138. spin_unlock_irq(&pdata->lock);
  139. sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
  140. (buf[3] << 8) | buf[2];
  141. rtc_time_to_tm(sec, &alrm->time);
  142. return rtc_valid_tm(&alrm->time);
  143. }
  144. static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  145. {
  146. struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
  147. spin_lock_irq(&pdata->lock);
  148. tx4939_rtc_cmd(pdata->rtcreg,
  149. TX4939_RTCCTL_COMMAND_NOP |
  150. (enabled ? TX4939_RTCCTL_ALME : 0));
  151. spin_unlock_irq(&pdata->lock);
  152. return 0;
  153. }
  154. static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
  155. {
  156. struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
  157. struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
  158. unsigned long events = RTC_IRQF;
  159. spin_lock(&pdata->lock);
  160. if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
  161. events |= RTC_AF;
  162. tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
  163. }
  164. spin_unlock(&pdata->lock);
  165. rtc_update_irq(pdata->rtc, 1, events);
  166. return IRQ_HANDLED;
  167. }
  168. static const struct rtc_class_ops tx4939_rtc_ops = {
  169. .read_time = tx4939_rtc_read_time,
  170. .read_alarm = tx4939_rtc_read_alarm,
  171. .set_alarm = tx4939_rtc_set_alarm,
  172. .set_mmss = tx4939_rtc_set_mmss,
  173. .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
  174. };
  175. static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj,
  176. struct bin_attribute *bin_attr,
  177. char *buf, loff_t pos, size_t size)
  178. {
  179. struct device *dev = container_of(kobj, struct device, kobj);
  180. struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
  181. struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
  182. ssize_t count;
  183. spin_lock_irq(&pdata->lock);
  184. for (count = 0; count < size; count++) {
  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; count < size; count++) {
  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");