rtc-mrst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * rtc-mrst.c: Driver for Moorestown virtual RTC
  3. *
  4. * (C) Copyright 2009 Intel Corporation
  5. * Author: Jacob Pan (jacob.jun.pan@intel.com)
  6. * Feng Tang (feng.tang@intel.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. *
  13. * Note:
  14. * VRTC is emulated by system controller firmware, the real HW
  15. * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
  16. * in a memory mapped IO space that is visible to the host IA
  17. * processor.
  18. *
  19. * This driver is based upon drivers/rtc/rtc-cmos.c
  20. */
  21. /*
  22. * Note:
  23. * * vRTC only supports binary mode and 24H mode
  24. * * vRTC only support PIE and AIE, no UIE, and its PIE only happens
  25. * at 23:59:59pm everyday, no support for adjustable frequency
  26. * * Alarm function is also limited to hr/min/sec.
  27. */
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mc146818rtc.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/sfi.h>
  37. #include <asm/intel_scu_ipc.h>
  38. #include <asm/intel-mid.h>
  39. #include <asm/intel_mid_vrtc.h>
  40. struct mrst_rtc {
  41. struct rtc_device *rtc;
  42. struct device *dev;
  43. int irq;
  44. u8 enabled_wake;
  45. u8 suspend_ctrl;
  46. };
  47. static const char driver_name[] = "rtc_mrst";
  48. #define RTC_IRQMASK (RTC_PF | RTC_AF)
  49. static inline int is_intr(u8 rtc_intr)
  50. {
  51. if (!(rtc_intr & RTC_IRQF))
  52. return 0;
  53. return rtc_intr & RTC_IRQMASK;
  54. }
  55. static inline unsigned char vrtc_is_updating(void)
  56. {
  57. unsigned char uip;
  58. unsigned long flags;
  59. spin_lock_irqsave(&rtc_lock, flags);
  60. uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
  61. spin_unlock_irqrestore(&rtc_lock, flags);
  62. return uip;
  63. }
  64. /*
  65. * rtc_time's year contains the increment over 1900, but vRTC's YEAR
  66. * register can't be programmed to value larger than 0x64, so vRTC
  67. * driver chose to use 1972 (1970 is UNIX time start point) as the base,
  68. * and does the translation at read/write time.
  69. *
  70. * Why not just use 1970 as the offset? it's because using 1972 will
  71. * make it consistent in leap year setting for both vrtc and low-level
  72. * physical rtc devices. Then why not use 1960 as the offset? If we use
  73. * 1960, for a device's first use, its YEAR register is 0 and the system
  74. * year will be parsed as 1960 which is not a valid UNIX time and will
  75. * cause many applications to fail mysteriously.
  76. */
  77. static int mrst_read_time(struct device *dev, struct rtc_time *time)
  78. {
  79. unsigned long flags;
  80. if (vrtc_is_updating())
  81. msleep(20);
  82. spin_lock_irqsave(&rtc_lock, flags);
  83. time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
  84. time->tm_min = vrtc_cmos_read(RTC_MINUTES);
  85. time->tm_hour = vrtc_cmos_read(RTC_HOURS);
  86. time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
  87. time->tm_mon = vrtc_cmos_read(RTC_MONTH);
  88. time->tm_year = vrtc_cmos_read(RTC_YEAR);
  89. spin_unlock_irqrestore(&rtc_lock, flags);
  90. /* Adjust for the 1972/1900 */
  91. time->tm_year += 72;
  92. time->tm_mon--;
  93. return 0;
  94. }
  95. static int mrst_set_time(struct device *dev, struct rtc_time *time)
  96. {
  97. int ret;
  98. unsigned long flags;
  99. unsigned char mon, day, hrs, min, sec;
  100. unsigned int yrs;
  101. yrs = time->tm_year;
  102. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  103. day = time->tm_mday;
  104. hrs = time->tm_hour;
  105. min = time->tm_min;
  106. sec = time->tm_sec;
  107. if (yrs < 72 || yrs > 172)
  108. return -EINVAL;
  109. yrs -= 72;
  110. spin_lock_irqsave(&rtc_lock, flags);
  111. vrtc_cmos_write(yrs, RTC_YEAR);
  112. vrtc_cmos_write(mon, RTC_MONTH);
  113. vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
  114. vrtc_cmos_write(hrs, RTC_HOURS);
  115. vrtc_cmos_write(min, RTC_MINUTES);
  116. vrtc_cmos_write(sec, RTC_SECONDS);
  117. spin_unlock_irqrestore(&rtc_lock, flags);
  118. ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
  119. return ret;
  120. }
  121. static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  122. {
  123. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  124. unsigned char rtc_control;
  125. if (mrst->irq <= 0)
  126. return -EIO;
  127. /* vRTC only supports binary mode */
  128. spin_lock_irq(&rtc_lock);
  129. t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
  130. t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
  131. t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
  132. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  133. spin_unlock_irq(&rtc_lock);
  134. t->enabled = !!(rtc_control & RTC_AIE);
  135. t->pending = 0;
  136. return 0;
  137. }
  138. static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
  139. {
  140. unsigned char rtc_intr;
  141. /*
  142. * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  143. * allegedly some older rtcs need that to handle irqs properly
  144. */
  145. rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
  146. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  147. if (is_intr(rtc_intr))
  148. rtc_update_irq(mrst->rtc, 1, rtc_intr);
  149. }
  150. static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
  151. {
  152. unsigned char rtc_control;
  153. /*
  154. * Flush any pending IRQ status, notably for update irqs,
  155. * before we enable new IRQs
  156. */
  157. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  158. mrst_checkintr(mrst, rtc_control);
  159. rtc_control |= mask;
  160. vrtc_cmos_write(rtc_control, RTC_CONTROL);
  161. mrst_checkintr(mrst, rtc_control);
  162. }
  163. static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
  164. {
  165. unsigned char rtc_control;
  166. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  167. rtc_control &= ~mask;
  168. vrtc_cmos_write(rtc_control, RTC_CONTROL);
  169. mrst_checkintr(mrst, rtc_control);
  170. }
  171. static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  172. {
  173. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  174. unsigned char hrs, min, sec;
  175. int ret = 0;
  176. if (!mrst->irq)
  177. return -EIO;
  178. hrs = t->time.tm_hour;
  179. min = t->time.tm_min;
  180. sec = t->time.tm_sec;
  181. spin_lock_irq(&rtc_lock);
  182. /* Next rtc irq must not be from previous alarm setting */
  183. mrst_irq_disable(mrst, RTC_AIE);
  184. /* Update alarm */
  185. vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
  186. vrtc_cmos_write(min, RTC_MINUTES_ALARM);
  187. vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
  188. spin_unlock_irq(&rtc_lock);
  189. ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
  190. if (ret)
  191. return ret;
  192. spin_lock_irq(&rtc_lock);
  193. if (t->enabled)
  194. mrst_irq_enable(mrst, RTC_AIE);
  195. spin_unlock_irq(&rtc_lock);
  196. return 0;
  197. }
  198. /* Currently, the vRTC doesn't support UIE ON/OFF */
  199. static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  200. {
  201. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  202. unsigned long flags;
  203. spin_lock_irqsave(&rtc_lock, flags);
  204. if (enabled)
  205. mrst_irq_enable(mrst, RTC_AIE);
  206. else
  207. mrst_irq_disable(mrst, RTC_AIE);
  208. spin_unlock_irqrestore(&rtc_lock, flags);
  209. return 0;
  210. }
  211. #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
  212. static int mrst_procfs(struct device *dev, struct seq_file *seq)
  213. {
  214. unsigned char rtc_control;
  215. spin_lock_irq(&rtc_lock);
  216. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  217. spin_unlock_irq(&rtc_lock);
  218. seq_printf(seq,
  219. "periodic_IRQ\t: %s\n"
  220. "alarm\t\t: %s\n"
  221. "BCD\t\t: no\n"
  222. "periodic_freq\t: daily (not adjustable)\n",
  223. (rtc_control & RTC_PIE) ? "on" : "off",
  224. (rtc_control & RTC_AIE) ? "on" : "off");
  225. return 0;
  226. }
  227. #else
  228. #define mrst_procfs NULL
  229. #endif
  230. static const struct rtc_class_ops mrst_rtc_ops = {
  231. .read_time = mrst_read_time,
  232. .set_time = mrst_set_time,
  233. .read_alarm = mrst_read_alarm,
  234. .set_alarm = mrst_set_alarm,
  235. .proc = mrst_procfs,
  236. .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
  237. };
  238. static struct mrst_rtc mrst_rtc;
  239. /*
  240. * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
  241. * Reg B, so no need for this driver to clear it
  242. */
  243. static irqreturn_t mrst_rtc_irq(int irq, void *p)
  244. {
  245. u8 irqstat;
  246. spin_lock(&rtc_lock);
  247. /* This read will clear all IRQ flags inside Reg C */
  248. irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
  249. spin_unlock(&rtc_lock);
  250. irqstat &= RTC_IRQMASK | RTC_IRQF;
  251. if (is_intr(irqstat)) {
  252. rtc_update_irq(p, 1, irqstat);
  253. return IRQ_HANDLED;
  254. }
  255. return IRQ_NONE;
  256. }
  257. static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
  258. int rtc_irq)
  259. {
  260. int retval = 0;
  261. unsigned char rtc_control;
  262. /* There can be only one ... */
  263. if (mrst_rtc.dev)
  264. return -EBUSY;
  265. if (!iomem)
  266. return -ENODEV;
  267. iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem),
  268. driver_name);
  269. if (!iomem) {
  270. dev_dbg(dev, "i/o mem already in use.\n");
  271. return -EBUSY;
  272. }
  273. mrst_rtc.irq = rtc_irq;
  274. mrst_rtc.dev = dev;
  275. dev_set_drvdata(dev, &mrst_rtc);
  276. mrst_rtc.rtc = devm_rtc_allocate_device(dev);
  277. if (IS_ERR(mrst_rtc.rtc))
  278. return PTR_ERR(mrst_rtc.rtc);
  279. mrst_rtc.rtc->ops = &mrst_rtc_ops;
  280. rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
  281. spin_lock_irq(&rtc_lock);
  282. mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
  283. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  284. spin_unlock_irq(&rtc_lock);
  285. if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
  286. dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
  287. if (rtc_irq) {
  288. retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq,
  289. 0, dev_name(&mrst_rtc.rtc->dev),
  290. mrst_rtc.rtc);
  291. if (retval < 0) {
  292. dev_dbg(dev, "IRQ %d is already in use, err %d\n",
  293. rtc_irq, retval);
  294. goto cleanup0;
  295. }
  296. }
  297. retval = rtc_register_device(mrst_rtc.rtc);
  298. if (retval)
  299. goto cleanup0;
  300. dev_dbg(dev, "initialised\n");
  301. return 0;
  302. cleanup0:
  303. mrst_rtc.dev = NULL;
  304. dev_err(dev, "rtc-mrst: unable to initialise\n");
  305. return retval;
  306. }
  307. static void rtc_mrst_do_shutdown(void)
  308. {
  309. spin_lock_irq(&rtc_lock);
  310. mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
  311. spin_unlock_irq(&rtc_lock);
  312. }
  313. static void rtc_mrst_do_remove(struct device *dev)
  314. {
  315. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  316. rtc_mrst_do_shutdown();
  317. mrst->rtc = NULL;
  318. mrst->dev = NULL;
  319. }
  320. #ifdef CONFIG_PM_SLEEP
  321. static int mrst_suspend(struct device *dev)
  322. {
  323. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  324. unsigned char tmp;
  325. /* Only the alarm might be a wakeup event source */
  326. spin_lock_irq(&rtc_lock);
  327. mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
  328. if (tmp & (RTC_PIE | RTC_AIE)) {
  329. unsigned char mask;
  330. if (device_may_wakeup(dev))
  331. mask = RTC_IRQMASK & ~RTC_AIE;
  332. else
  333. mask = RTC_IRQMASK;
  334. tmp &= ~mask;
  335. vrtc_cmos_write(tmp, RTC_CONTROL);
  336. mrst_checkintr(mrst, tmp);
  337. }
  338. spin_unlock_irq(&rtc_lock);
  339. if (tmp & RTC_AIE) {
  340. mrst->enabled_wake = 1;
  341. enable_irq_wake(mrst->irq);
  342. }
  343. dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
  344. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  345. tmp);
  346. return 0;
  347. }
  348. /*
  349. * We want RTC alarms to wake us from the deep power saving state
  350. */
  351. static inline int mrst_poweroff(struct device *dev)
  352. {
  353. return mrst_suspend(dev);
  354. }
  355. static int mrst_resume(struct device *dev)
  356. {
  357. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  358. unsigned char tmp = mrst->suspend_ctrl;
  359. /* Re-enable any irqs previously active */
  360. if (tmp & RTC_IRQMASK) {
  361. unsigned char mask;
  362. if (mrst->enabled_wake) {
  363. disable_irq_wake(mrst->irq);
  364. mrst->enabled_wake = 0;
  365. }
  366. spin_lock_irq(&rtc_lock);
  367. do {
  368. vrtc_cmos_write(tmp, RTC_CONTROL);
  369. mask = vrtc_cmos_read(RTC_INTR_FLAGS);
  370. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  371. if (!is_intr(mask))
  372. break;
  373. rtc_update_irq(mrst->rtc, 1, mask);
  374. tmp &= ~RTC_AIE;
  375. } while (mask & RTC_AIE);
  376. spin_unlock_irq(&rtc_lock);
  377. }
  378. dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
  379. return 0;
  380. }
  381. static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
  382. #define MRST_PM_OPS (&mrst_pm_ops)
  383. #else
  384. #define MRST_PM_OPS NULL
  385. static inline int mrst_poweroff(struct device *dev)
  386. {
  387. return -ENOSYS;
  388. }
  389. #endif
  390. static int vrtc_mrst_platform_probe(struct platform_device *pdev)
  391. {
  392. return vrtc_mrst_do_probe(&pdev->dev,
  393. platform_get_resource(pdev, IORESOURCE_MEM, 0),
  394. platform_get_irq(pdev, 0));
  395. }
  396. static int vrtc_mrst_platform_remove(struct platform_device *pdev)
  397. {
  398. rtc_mrst_do_remove(&pdev->dev);
  399. return 0;
  400. }
  401. static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
  402. {
  403. if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
  404. return;
  405. rtc_mrst_do_shutdown();
  406. }
  407. MODULE_ALIAS("platform:vrtc_mrst");
  408. static struct platform_driver vrtc_mrst_platform_driver = {
  409. .probe = vrtc_mrst_platform_probe,
  410. .remove = vrtc_mrst_platform_remove,
  411. .shutdown = vrtc_mrst_platform_shutdown,
  412. .driver = {
  413. .name = driver_name,
  414. .pm = MRST_PM_OPS,
  415. }
  416. };
  417. module_platform_driver(vrtc_mrst_platform_driver);
  418. MODULE_AUTHOR("Jacob Pan; Feng Tang");
  419. MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
  420. MODULE_LICENSE("GPL");