rtc-stk17ta8.c 12 KB

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