rtc-stk17ta8.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 baseaddr;
  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. };
  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->rtc->irq_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->rtc->irq_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 = RTC_IRQF;
  180. /* read and clear interrupt */
  181. if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF))
  182. return IRQ_NONE;
  183. if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
  184. events |= RTC_UF;
  185. else
  186. events |= RTC_AF;
  187. rtc_update_irq(pdata->rtc, 1, events);
  188. return IRQ_HANDLED;
  189. }
  190. static int stk17ta8_rtc_ioctl(struct device *dev, unsigned int cmd,
  191. unsigned long arg)
  192. {
  193. struct platform_device *pdev = to_platform_device(dev);
  194. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  195. if (pdata->irq <= 0)
  196. return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
  197. switch (cmd) {
  198. case RTC_AIE_OFF:
  199. pdata->irqen &= ~RTC_AF;
  200. stk17ta8_rtc_update_alarm(pdata);
  201. break;
  202. case RTC_AIE_ON:
  203. pdata->irqen |= RTC_AF;
  204. stk17ta8_rtc_update_alarm(pdata);
  205. break;
  206. default:
  207. return -ENOIOCTLCMD;
  208. }
  209. return 0;
  210. }
  211. static const struct rtc_class_ops stk17ta8_rtc_ops = {
  212. .read_time = stk17ta8_rtc_read_time,
  213. .set_time = stk17ta8_rtc_set_time,
  214. .read_alarm = stk17ta8_rtc_read_alarm,
  215. .set_alarm = stk17ta8_rtc_set_alarm,
  216. .ioctl = stk17ta8_rtc_ioctl,
  217. };
  218. static ssize_t stk17ta8_nvram_read(struct kobject *kobj,
  219. struct bin_attribute *attr, char *buf,
  220. loff_t pos, size_t size)
  221. {
  222. struct platform_device *pdev =
  223. to_platform_device(container_of(kobj, struct device, kobj));
  224. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  225. void __iomem *ioaddr = pdata->ioaddr;
  226. ssize_t count;
  227. for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
  228. *buf++ = readb(ioaddr + pos++);
  229. return count;
  230. }
  231. static ssize_t stk17ta8_nvram_write(struct kobject *kobj,
  232. struct bin_attribute *attr, char *buf,
  233. loff_t pos, size_t size)
  234. {
  235. struct platform_device *pdev =
  236. to_platform_device(container_of(kobj, struct device, kobj));
  237. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  238. void __iomem *ioaddr = pdata->ioaddr;
  239. ssize_t count;
  240. for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
  241. writeb(*buf++, ioaddr + pos++);
  242. return count;
  243. }
  244. static struct bin_attribute stk17ta8_nvram_attr = {
  245. .attr = {
  246. .name = "nvram",
  247. .mode = S_IRUGO | S_IWUSR,
  248. },
  249. .size = RTC_OFFSET,
  250. .read = stk17ta8_nvram_read,
  251. .write = stk17ta8_nvram_write,
  252. };
  253. static int __init stk17ta8_rtc_probe(struct platform_device *pdev)
  254. {
  255. struct rtc_device *rtc;
  256. struct resource *res;
  257. unsigned int cal;
  258. unsigned int flags;
  259. struct rtc_plat_data *pdata;
  260. void __iomem *ioaddr = NULL;
  261. int ret = 0;
  262. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. if (!res)
  264. return -ENODEV;
  265. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  266. if (!pdata)
  267. return -ENOMEM;
  268. if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
  269. ret = -EBUSY;
  270. goto out;
  271. }
  272. pdata->baseaddr = res->start;
  273. ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
  274. if (!ioaddr) {
  275. ret = -ENOMEM;
  276. goto out;
  277. }
  278. pdata->ioaddr = ioaddr;
  279. pdata->irq = platform_get_irq(pdev, 0);
  280. /* turn RTC on if it was not on */
  281. cal = readb(ioaddr + RTC_CALIBRATION);
  282. if (cal & RTC_STOP) {
  283. cal &= RTC_CAL_MASK;
  284. flags = readb(ioaddr + RTC_FLAGS);
  285. writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
  286. writeb(cal, ioaddr + RTC_CALIBRATION);
  287. writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
  288. }
  289. if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
  290. dev_warn(&pdev->dev, "voltage-low detected.\n");
  291. if (pdata->irq > 0) {
  292. writeb(0, ioaddr + RTC_INTERRUPTS);
  293. if (request_irq(pdata->irq, stk17ta8_rtc_interrupt,
  294. IRQF_DISABLED | IRQF_SHARED,
  295. pdev->name, pdev) < 0) {
  296. dev_warn(&pdev->dev, "interrupt not available.\n");
  297. pdata->irq = 0;
  298. }
  299. }
  300. rtc = rtc_device_register(pdev->name, &pdev->dev,
  301. &stk17ta8_rtc_ops, THIS_MODULE);
  302. if (IS_ERR(rtc)) {
  303. ret = PTR_ERR(rtc);
  304. goto out;
  305. }
  306. pdata->rtc = rtc;
  307. pdata->last_jiffies = jiffies;
  308. platform_set_drvdata(pdev, pdata);
  309. ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  310. if (ret)
  311. goto out;
  312. return 0;
  313. out:
  314. if (pdata->rtc)
  315. rtc_device_unregister(pdata->rtc);
  316. if (pdata->irq > 0)
  317. free_irq(pdata->irq, pdev);
  318. if (ioaddr)
  319. iounmap(ioaddr);
  320. if (pdata->baseaddr)
  321. release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
  322. kfree(pdata);
  323. return ret;
  324. }
  325. static int __devexit stk17ta8_rtc_remove(struct platform_device *pdev)
  326. {
  327. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  328. sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
  329. rtc_device_unregister(pdata->rtc);
  330. if (pdata->irq > 0) {
  331. writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
  332. free_irq(pdata->irq, pdev);
  333. }
  334. iounmap(pdata->ioaddr);
  335. release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
  336. kfree(pdata);
  337. return 0;
  338. }
  339. /* work with hotplug and coldplug */
  340. MODULE_ALIAS("platform:stk17ta8");
  341. static struct platform_driver stk17ta8_rtc_driver = {
  342. .probe = stk17ta8_rtc_probe,
  343. .remove = __devexit_p(stk17ta8_rtc_remove),
  344. .driver = {
  345. .name = "stk17ta8",
  346. .owner = THIS_MODULE,
  347. },
  348. };
  349. static __init int stk17ta8_init(void)
  350. {
  351. return platform_driver_register(&stk17ta8_rtc_driver);
  352. }
  353. static __exit void stk17ta8_exit(void)
  354. {
  355. platform_driver_unregister(&stk17ta8_rtc_driver);
  356. }
  357. module_init(stk17ta8_init);
  358. module_exit(stk17ta8_exit);
  359. MODULE_AUTHOR("Thomas Hommel <thomas.hommel@gefanuc.com>");
  360. MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
  361. MODULE_LICENSE("GPL");
  362. MODULE_VERSION(DRV_VERSION);