rtc-stk17ta8.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * A RTC driver for the Simtek STK17TA8
  3. *
  4. * By Thomas Hommel <thomas.hommel@gefanuc.com>
  5. *
  6. * Based on the DS1553 driver from
  7. * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/bcd.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/rtc.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #define DRV_VERSION "0.1"
  23. #define RTC_REG_SIZE 0x20000
  24. #define RTC_OFFSET 0x1fff0
  25. #define RTC_FLAGS (RTC_OFFSET + 0)
  26. #define RTC_CENTURY (RTC_OFFSET + 1)
  27. #define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
  28. #define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
  29. #define RTC_HOURS_ALARM (RTC_OFFSET + 4)
  30. #define RTC_DATE_ALARM (RTC_OFFSET + 5)
  31. #define RTC_INTERRUPTS (RTC_OFFSET + 6)
  32. #define RTC_WATCHDOG (RTC_OFFSET + 7)
  33. #define RTC_CALIBRATION (RTC_OFFSET + 8)
  34. #define RTC_SECONDS (RTC_OFFSET + 9)
  35. #define RTC_MINUTES (RTC_OFFSET + 10)
  36. #define RTC_HOURS (RTC_OFFSET + 11)
  37. #define RTC_DAY (RTC_OFFSET + 12)
  38. #define RTC_DATE (RTC_OFFSET + 13)
  39. #define RTC_MONTH (RTC_OFFSET + 14)
  40. #define RTC_YEAR (RTC_OFFSET + 15)
  41. #define RTC_SECONDS_MASK 0x7f
  42. #define RTC_DAY_MASK 0x07
  43. #define RTC_CAL_MASK 0x3f
  44. /* Bits in the Calibration register */
  45. #define RTC_STOP 0x80
  46. /* Bits in the Flags register */
  47. #define RTC_FLAGS_AF 0x40
  48. #define RTC_FLAGS_PF 0x20
  49. #define RTC_WRITE 0x02
  50. #define RTC_READ 0x01
  51. /* Bits in the Interrupts register */
  52. #define RTC_INTS_AIE 0x40
  53. struct rtc_plat_data {
  54. struct rtc_device *rtc;
  55. void __iomem *ioaddr;
  56. unsigned long last_jiffies;
  57. int irq;
  58. unsigned int irqen;
  59. int alrm_sec;
  60. int alrm_min;
  61. int alrm_hour;
  62. int alrm_mday;
  63. spinlock_t lock;
  64. };
  65. static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct platform_device *pdev = to_platform_device(dev);
  68. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  69. void __iomem *ioaddr = pdata->ioaddr;
  70. u8 flags;
  71. flags = readb(pdata->ioaddr + RTC_FLAGS);
  72. writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
  73. writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
  74. writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
  75. writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
  76. writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
  77. writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
  78. writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
  79. writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
  80. writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
  81. writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
  82. return 0;
  83. }
  84. static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
  85. {
  86. struct platform_device *pdev = to_platform_device(dev);
  87. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  88. void __iomem *ioaddr = pdata->ioaddr;
  89. unsigned int year, month, day, hour, minute, second, week;
  90. unsigned int century;
  91. u8 flags;
  92. /* give enough time to update RTC in case of continuous read */
  93. if (pdata->last_jiffies == jiffies)
  94. msleep(1);
  95. pdata->last_jiffies = jiffies;
  96. flags = readb(pdata->ioaddr + RTC_FLAGS);
  97. writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
  98. second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
  99. minute = readb(ioaddr + RTC_MINUTES);
  100. hour = readb(ioaddr + RTC_HOURS);
  101. day = readb(ioaddr + RTC_DATE);
  102. week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
  103. month = readb(ioaddr + RTC_MONTH);
  104. year = readb(ioaddr + RTC_YEAR);
  105. century = readb(ioaddr + RTC_CENTURY);
  106. writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
  107. tm->tm_sec = bcd2bin(second);
  108. tm->tm_min = bcd2bin(minute);
  109. tm->tm_hour = bcd2bin(hour);
  110. tm->tm_mday = bcd2bin(day);
  111. tm->tm_wday = bcd2bin(week);
  112. tm->tm_mon = bcd2bin(month) - 1;
  113. /* year is 1900 + tm->tm_year */
  114. tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
  115. if (rtc_valid_tm(tm) < 0) {
  116. dev_err(dev, "retrieved date/time is not valid.\n");
  117. rtc_time_to_tm(0, tm);
  118. }
  119. return 0;
  120. }
  121. static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
  122. {
  123. void __iomem *ioaddr = pdata->ioaddr;
  124. unsigned long irqflags;
  125. u8 flags;
  126. spin_lock_irqsave(&pdata->lock, irqflags);
  127. flags = readb(ioaddr + RTC_FLAGS);
  128. writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
  129. writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  130. 0x80 : bin2bcd(pdata->alrm_mday),
  131. ioaddr + RTC_DATE_ALARM);
  132. writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  133. 0x80 : bin2bcd(pdata->alrm_hour),
  134. ioaddr + RTC_HOURS_ALARM);
  135. writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  136. 0x80 : bin2bcd(pdata->alrm_min),
  137. ioaddr + RTC_MINUTES_ALARM);
  138. writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  139. 0x80 : bin2bcd(pdata->alrm_sec),
  140. ioaddr + RTC_SECONDS_ALARM);
  141. writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
  142. readb(ioaddr + RTC_FLAGS); /* clear interrupts */
  143. writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
  144. spin_unlock_irqrestore(&pdata->lock, irqflags);
  145. }
  146. static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  147. {
  148. struct platform_device *pdev = to_platform_device(dev);
  149. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  150. if (pdata->irq <= 0)
  151. return -EINVAL;
  152. pdata->alrm_mday = alrm->time.tm_mday;
  153. pdata->alrm_hour = alrm->time.tm_hour;
  154. pdata->alrm_min = alrm->time.tm_min;
  155. pdata->alrm_sec = alrm->time.tm_sec;
  156. if (alrm->enabled)
  157. pdata->irqen |= RTC_AF;
  158. stk17ta8_rtc_update_alarm(pdata);
  159. return 0;
  160. }
  161. static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  162. {
  163. struct platform_device *pdev = to_platform_device(dev);
  164. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  165. if (pdata->irq <= 0)
  166. return -EINVAL;
  167. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  168. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  169. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  170. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  171. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  172. return 0;
  173. }
  174. static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
  175. {
  176. struct platform_device *pdev = dev_id;
  177. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  178. void __iomem *ioaddr = pdata->ioaddr;
  179. unsigned long events = 0;
  180. spin_lock(&pdata->lock);
  181. /* read and clear interrupt */
  182. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
  183. events = RTC_IRQF;
  184. if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
  185. events |= RTC_UF;
  186. else
  187. events |= RTC_AF;
  188. if (likely(pdata->rtc))
  189. rtc_update_irq(pdata->rtc, 1, events);
  190. }
  191. spin_unlock(&pdata->lock);
  192. return events ? IRQ_HANDLED : IRQ_NONE;
  193. }
  194. static int stk17ta8_rtc_alarm_irq_enable(struct device *dev,
  195. unsigned int enabled)
  196. {
  197. struct platform_device *pdev = to_platform_device(dev);
  198. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  199. if (pdata->irq <= 0)
  200. return -EINVAL;
  201. if (enabled)
  202. pdata->irqen |= RTC_AF;
  203. else
  204. pdata->irqen &= ~RTC_AF;
  205. stk17ta8_rtc_update_alarm(pdata);
  206. return 0;
  207. }
  208. static const struct rtc_class_ops stk17ta8_rtc_ops = {
  209. .read_time = stk17ta8_rtc_read_time,
  210. .set_time = stk17ta8_rtc_set_time,
  211. .read_alarm = stk17ta8_rtc_read_alarm,
  212. .set_alarm = stk17ta8_rtc_set_alarm,
  213. .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable,
  214. };
  215. static ssize_t stk17ta8_nvram_read(struct kobject *kobj,
  216. struct bin_attribute *attr, char *buf,
  217. loff_t pos, size_t size)
  218. {
  219. struct device *dev = container_of(kobj, struct device, kobj);
  220. struct platform_device *pdev = to_platform_device(dev);
  221. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  222. void __iomem *ioaddr = pdata->ioaddr;
  223. ssize_t count;
  224. for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
  225. *buf++ = readb(ioaddr + pos++);
  226. return count;
  227. }
  228. static ssize_t stk17ta8_nvram_write(struct kobject *kobj,
  229. struct bin_attribute *attr, char *buf,
  230. loff_t pos, size_t size)
  231. {
  232. struct device *dev = container_of(kobj, struct device, kobj);
  233. struct platform_device *pdev = to_platform_device(dev);
  234. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  235. void __iomem *ioaddr = pdata->ioaddr;
  236. ssize_t count;
  237. for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
  238. writeb(*buf++, ioaddr + pos++);
  239. return count;
  240. }
  241. static struct bin_attribute stk17ta8_nvram_attr = {
  242. .attr = {
  243. .name = "nvram",
  244. .mode = S_IRUGO | S_IWUSR,
  245. },
  246. .size = RTC_OFFSET,
  247. .read = stk17ta8_nvram_read,
  248. .write = stk17ta8_nvram_write,
  249. };
  250. static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev)
  251. {
  252. struct resource *res;
  253. unsigned int cal;
  254. unsigned int flags;
  255. struct rtc_plat_data *pdata;
  256. void __iomem *ioaddr;
  257. int ret = 0;
  258. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  259. if (!res)
  260. return -ENODEV;
  261. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  262. if (!pdata)
  263. return -ENOMEM;
  264. if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE,
  265. pdev->name))
  266. return -EBUSY;
  267. ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE);
  268. if (!ioaddr)
  269. return -ENOMEM;
  270. pdata->ioaddr = ioaddr;
  271. pdata->irq = platform_get_irq(pdev, 0);
  272. /* turn RTC on if it was not on */
  273. cal = readb(ioaddr + RTC_CALIBRATION);
  274. if (cal & RTC_STOP) {
  275. cal &= RTC_CAL_MASK;
  276. flags = readb(ioaddr + RTC_FLAGS);
  277. writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
  278. writeb(cal, ioaddr + RTC_CALIBRATION);
  279. writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
  280. }
  281. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
  282. dev_warn(&pdev->dev, "voltage-low detected.\n");
  283. spin_lock_init(&pdata->lock);
  284. pdata->last_jiffies = jiffies;
  285. platform_set_drvdata(pdev, pdata);
  286. if (pdata->irq > 0) {
  287. writeb(0, ioaddr + RTC_INTERRUPTS);
  288. if (devm_request_irq(&pdev->dev, pdata->irq,
  289. stk17ta8_rtc_interrupt,
  290. IRQF_DISABLED | IRQF_SHARED,
  291. pdev->name, pdev) < 0) {
  292. dev_warn(&pdev->dev, "interrupt not available.\n");
  293. pdata->irq = 0;
  294. }
  295. }
  296. pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
  297. &stk17ta8_rtc_ops, THIS_MODULE);
  298. if (IS_ERR(pdata->rtc))
  299. return PTR_ERR(pdata->rtc);
  300. ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  301. if (ret)
  302. rtc_device_unregister(pdata->rtc);
  303. return ret;
  304. }
  305. static int __devexit stk17ta8_rtc_remove(struct platform_device *pdev)
  306. {
  307. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  308. sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  309. rtc_device_unregister(pdata->rtc);
  310. if (pdata->irq > 0)
  311. writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
  312. return 0;
  313. }
  314. /* work with hotplug and coldplug */
  315. MODULE_ALIAS("platform:stk17ta8");
  316. static struct platform_driver stk17ta8_rtc_driver = {
  317. .probe = stk17ta8_rtc_probe,
  318. .remove = __devexit_p(stk17ta8_rtc_remove),
  319. .driver = {
  320. .name = "stk17ta8",
  321. .owner = THIS_MODULE,
  322. },
  323. };
  324. static __init int stk17ta8_init(void)
  325. {
  326. return platform_driver_register(&stk17ta8_rtc_driver);
  327. }
  328. static __exit void stk17ta8_exit(void)
  329. {
  330. platform_driver_unregister(&stk17ta8_rtc_driver);
  331. }
  332. module_init(stk17ta8_init);
  333. module_exit(stk17ta8_exit);
  334. MODULE_AUTHOR("Thomas Hommel <thomas.hommel@gefanuc.com>");
  335. MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
  336. MODULE_LICENSE("GPL");
  337. MODULE_VERSION(DRV_VERSION);