rtc-tx4939.c 8.4 KB

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