rtc-stk17ta8.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * A RTC driver for the Simtek STK17TA8
  3. *
  4. * By Thomas Hommel <thomas.hommel@ge.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/gfp.h>
  17. #include <linux/delay.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/rtc.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #define RTC_REG_SIZE 0x20000
  25. #define RTC_OFFSET 0x1fff0
  26. #define RTC_FLAGS (RTC_OFFSET + 0)
  27. #define RTC_CENTURY (RTC_OFFSET + 1)
  28. #define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
  29. #define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
  30. #define RTC_HOURS_ALARM (RTC_OFFSET + 4)
  31. #define RTC_DATE_ALARM (RTC_OFFSET + 5)
  32. #define RTC_INTERRUPTS (RTC_OFFSET + 6)
  33. #define RTC_WATCHDOG (RTC_OFFSET + 7)
  34. #define RTC_CALIBRATION (RTC_OFFSET + 8)
  35. #define RTC_SECONDS (RTC_OFFSET + 9)
  36. #define RTC_MINUTES (RTC_OFFSET + 10)
  37. #define RTC_HOURS (RTC_OFFSET + 11)
  38. #define RTC_DAY (RTC_OFFSET + 12)
  39. #define RTC_DATE (RTC_OFFSET + 13)
  40. #define RTC_MONTH (RTC_OFFSET + 14)
  41. #define RTC_YEAR (RTC_OFFSET + 15)
  42. #define RTC_SECONDS_MASK 0x7f
  43. #define RTC_DAY_MASK 0x07
  44. #define RTC_CAL_MASK 0x3f
  45. /* Bits in the Calibration register */
  46. #define RTC_STOP 0x80
  47. /* Bits in the Flags register */
  48. #define RTC_FLAGS_AF 0x40
  49. #define RTC_FLAGS_PF 0x20
  50. #define RTC_WRITE 0x02
  51. #define RTC_READ 0x01
  52. /* Bits in the Interrupts register */
  53. #define RTC_INTS_AIE 0x40
  54. struct rtc_plat_data {
  55. struct rtc_device *rtc;
  56. void __iomem *ioaddr;
  57. unsigned long last_jiffies;
  58. int irq;
  59. unsigned int irqen;
  60. int alrm_sec;
  61. int alrm_min;
  62. int alrm_hour;
  63. int alrm_mday;
  64. spinlock_t lock;
  65. };
  66. static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
  67. {
  68. struct platform_device *pdev = to_platform_device(dev);
  69. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  70. void __iomem *ioaddr = pdata->ioaddr;
  71. u8 flags;
  72. flags = readb(pdata->ioaddr + RTC_FLAGS);
  73. writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
  74. writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
  75. writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
  76. writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
  77. writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
  78. writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
  79. writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
  80. writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
  81. writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
  82. writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
  83. return 0;
  84. }
  85. static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
  86. {
  87. struct platform_device *pdev = to_platform_device(dev);
  88. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  89. void __iomem *ioaddr = pdata->ioaddr;
  90. unsigned int year, month, day, hour, minute, second, week;
  91. unsigned int century;
  92. u8 flags;
  93. /* give enough time to update RTC in case of continuous read */
  94. if (pdata->last_jiffies == jiffies)
  95. msleep(1);
  96. pdata->last_jiffies = jiffies;
  97. flags = readb(pdata->ioaddr + RTC_FLAGS);
  98. writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
  99. second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
  100. minute = readb(ioaddr + RTC_MINUTES);
  101. hour = readb(ioaddr + RTC_HOURS);
  102. day = readb(ioaddr + RTC_DATE);
  103. week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
  104. month = readb(ioaddr + RTC_MONTH);
  105. year = readb(ioaddr + RTC_YEAR);
  106. century = readb(ioaddr + RTC_CENTURY);
  107. writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
  108. tm->tm_sec = bcd2bin(second);
  109. tm->tm_min = bcd2bin(minute);
  110. tm->tm_hour = bcd2bin(hour);
  111. tm->tm_mday = bcd2bin(day);
  112. tm->tm_wday = bcd2bin(week);
  113. tm->tm_mon = bcd2bin(month) - 1;
  114. /* year is 1900 + tm->tm_year */
  115. tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
  116. if (rtc_valid_tm(tm) < 0) {
  117. dev_err(dev, "retrieved date/time is not valid.\n");
  118. rtc_time_to_tm(0, tm);
  119. }
  120. return 0;
  121. }
  122. static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
  123. {
  124. void __iomem *ioaddr = pdata->ioaddr;
  125. unsigned long irqflags;
  126. u8 flags;
  127. spin_lock_irqsave(&pdata->lock, irqflags);
  128. flags = readb(ioaddr + RTC_FLAGS);
  129. writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
  130. writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  131. 0x80 : bin2bcd(pdata->alrm_mday),
  132. ioaddr + RTC_DATE_ALARM);
  133. writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  134. 0x80 : bin2bcd(pdata->alrm_hour),
  135. ioaddr + RTC_HOURS_ALARM);
  136. writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  137. 0x80 : bin2bcd(pdata->alrm_min),
  138. ioaddr + RTC_MINUTES_ALARM);
  139. writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  140. 0x80 : bin2bcd(pdata->alrm_sec),
  141. ioaddr + RTC_SECONDS_ALARM);
  142. writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
  143. readb(ioaddr + RTC_FLAGS); /* clear interrupts */
  144. writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
  145. spin_unlock_irqrestore(&pdata->lock, irqflags);
  146. }
  147. static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  148. {
  149. struct platform_device *pdev = to_platform_device(dev);
  150. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  151. if (pdata->irq <= 0)
  152. return -EINVAL;
  153. pdata->alrm_mday = alrm->time.tm_mday;
  154. pdata->alrm_hour = alrm->time.tm_hour;
  155. pdata->alrm_min = alrm->time.tm_min;
  156. pdata->alrm_sec = alrm->time.tm_sec;
  157. if (alrm->enabled)
  158. pdata->irqen |= RTC_AF;
  159. stk17ta8_rtc_update_alarm(pdata);
  160. return 0;
  161. }
  162. static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  163. {
  164. struct platform_device *pdev = to_platform_device(dev);
  165. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  166. if (pdata->irq <= 0)
  167. return -EINVAL;
  168. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  169. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  170. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  171. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  172. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  173. return 0;
  174. }
  175. static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
  176. {
  177. struct platform_device *pdev = dev_id;
  178. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  179. void __iomem *ioaddr = pdata->ioaddr;
  180. unsigned long events = 0;
  181. spin_lock(&pdata->lock);
  182. /* read and clear interrupt */
  183. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
  184. events = RTC_IRQF;
  185. if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
  186. events |= RTC_UF;
  187. else
  188. events |= RTC_AF;
  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 file *filp, 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; count < size; count++)
  225. *buf++ = readb(ioaddr + pos++);
  226. return count;
  227. }
  228. static ssize_t stk17ta8_nvram_write(struct file *filp, 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; count < size; count++)
  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 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. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  259. if (!pdata)
  260. return -ENOMEM;
  261. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  262. ioaddr = devm_ioremap_resource(&pdev->dev, res);
  263. if (IS_ERR(ioaddr))
  264. return PTR_ERR(ioaddr);
  265. pdata->ioaddr = ioaddr;
  266. pdata->irq = platform_get_irq(pdev, 0);
  267. /* turn RTC on if it was not on */
  268. cal = readb(ioaddr + RTC_CALIBRATION);
  269. if (cal & RTC_STOP) {
  270. cal &= RTC_CAL_MASK;
  271. flags = readb(ioaddr + RTC_FLAGS);
  272. writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
  273. writeb(cal, ioaddr + RTC_CALIBRATION);
  274. writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
  275. }
  276. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
  277. dev_warn(&pdev->dev, "voltage-low detected.\n");
  278. spin_lock_init(&pdata->lock);
  279. pdata->last_jiffies = jiffies;
  280. platform_set_drvdata(pdev, pdata);
  281. if (pdata->irq > 0) {
  282. writeb(0, ioaddr + RTC_INTERRUPTS);
  283. if (devm_request_irq(&pdev->dev, pdata->irq,
  284. stk17ta8_rtc_interrupt,
  285. IRQF_SHARED,
  286. pdev->name, pdev) < 0) {
  287. dev_warn(&pdev->dev, "interrupt not available.\n");
  288. pdata->irq = 0;
  289. }
  290. }
  291. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  292. &stk17ta8_rtc_ops, THIS_MODULE);
  293. if (IS_ERR(pdata->rtc))
  294. return PTR_ERR(pdata->rtc);
  295. ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  296. return ret;
  297. }
  298. static int stk17ta8_rtc_remove(struct platform_device *pdev)
  299. {
  300. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  301. sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  302. if (pdata->irq > 0)
  303. writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
  304. return 0;
  305. }
  306. /* work with hotplug and coldplug */
  307. MODULE_ALIAS("platform:stk17ta8");
  308. static struct platform_driver stk17ta8_rtc_driver = {
  309. .probe = stk17ta8_rtc_probe,
  310. .remove = stk17ta8_rtc_remove,
  311. .driver = {
  312. .name = "stk17ta8",
  313. },
  314. };
  315. module_platform_driver(stk17ta8_rtc_driver);
  316. MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>");
  317. MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
  318. MODULE_LICENSE("GPL");