rtc-stk17ta8.c 11 KB

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