rtc-mrst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. mdelay(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, valid;
  215. spin_lock_irq(&rtc_lock);
  216. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  217. valid = vrtc_cmos_read(RTC_VALID);
  218. spin_unlock_irq(&rtc_lock);
  219. seq_printf(seq,
  220. "periodic_IRQ\t: %s\n"
  221. "alarm\t\t: %s\n"
  222. "BCD\t\t: no\n"
  223. "periodic_freq\t: daily (not adjustable)\n",
  224. (rtc_control & RTC_PIE) ? "on" : "off",
  225. (rtc_control & RTC_AIE) ? "on" : "off");
  226. return 0;
  227. }
  228. #else
  229. #define mrst_procfs NULL
  230. #endif
  231. static const struct rtc_class_ops mrst_rtc_ops = {
  232. .read_time = mrst_read_time,
  233. .set_time = mrst_set_time,
  234. .read_alarm = mrst_read_alarm,
  235. .set_alarm = mrst_set_alarm,
  236. .proc = mrst_procfs,
  237. .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
  238. };
  239. static struct mrst_rtc mrst_rtc;
  240. /*
  241. * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
  242. * Reg B, so no need for this driver to clear it
  243. */
  244. static irqreturn_t mrst_rtc_irq(int irq, void *p)
  245. {
  246. u8 irqstat;
  247. spin_lock(&rtc_lock);
  248. /* This read will clear all IRQ flags inside Reg C */
  249. irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
  250. spin_unlock(&rtc_lock);
  251. irqstat &= RTC_IRQMASK | RTC_IRQF;
  252. if (is_intr(irqstat)) {
  253. rtc_update_irq(p, 1, irqstat);
  254. return IRQ_HANDLED;
  255. }
  256. return IRQ_NONE;
  257. }
  258. static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
  259. int rtc_irq)
  260. {
  261. int retval = 0;
  262. unsigned char rtc_control;
  263. /* There can be only one ... */
  264. if (mrst_rtc.dev)
  265. return -EBUSY;
  266. if (!iomem)
  267. return -ENODEV;
  268. iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem),
  269. driver_name);
  270. if (!iomem) {
  271. dev_dbg(dev, "i/o mem already in use.\n");
  272. return -EBUSY;
  273. }
  274. mrst_rtc.irq = rtc_irq;
  275. mrst_rtc.dev = dev;
  276. dev_set_drvdata(dev, &mrst_rtc);
  277. mrst_rtc.rtc = devm_rtc_allocate_device(dev);
  278. if (IS_ERR(mrst_rtc.rtc))
  279. return PTR_ERR(mrst_rtc.rtc);
  280. mrst_rtc.rtc->ops = &mrst_rtc_ops;
  281. rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
  282. spin_lock_irq(&rtc_lock);
  283. mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
  284. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  285. spin_unlock_irq(&rtc_lock);
  286. if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
  287. dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
  288. if (rtc_irq) {
  289. retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq,
  290. 0, dev_name(&mrst_rtc.rtc->dev),
  291. mrst_rtc.rtc);
  292. if (retval < 0) {
  293. dev_dbg(dev, "IRQ %d is already in use, err %d\n",
  294. rtc_irq, retval);
  295. goto cleanup0;
  296. }
  297. }
  298. retval = rtc_register_device(mrst_rtc.rtc);
  299. if (retval)
  300. goto cleanup0;
  301. dev_dbg(dev, "initialised\n");
  302. return 0;
  303. cleanup0:
  304. mrst_rtc.dev = NULL;
  305. dev_err(dev, "rtc-mrst: unable to initialise\n");
  306. return retval;
  307. }
  308. static void rtc_mrst_do_shutdown(void)
  309. {
  310. spin_lock_irq(&rtc_lock);
  311. mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
  312. spin_unlock_irq(&rtc_lock);
  313. }
  314. static void rtc_mrst_do_remove(struct device *dev)
  315. {
  316. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  317. rtc_mrst_do_shutdown();
  318. mrst->rtc = NULL;
  319. mrst->dev = NULL;
  320. }
  321. #ifdef CONFIG_PM_SLEEP
  322. static int mrst_suspend(struct device *dev)
  323. {
  324. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  325. unsigned char tmp;
  326. /* Only the alarm might be a wakeup event source */
  327. spin_lock_irq(&rtc_lock);
  328. mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
  329. if (tmp & (RTC_PIE | RTC_AIE)) {
  330. unsigned char mask;
  331. if (device_may_wakeup(dev))
  332. mask = RTC_IRQMASK & ~RTC_AIE;
  333. else
  334. mask = RTC_IRQMASK;
  335. tmp &= ~mask;
  336. vrtc_cmos_write(tmp, RTC_CONTROL);
  337. mrst_checkintr(mrst, tmp);
  338. }
  339. spin_unlock_irq(&rtc_lock);
  340. if (tmp & RTC_AIE) {
  341. mrst->enabled_wake = 1;
  342. enable_irq_wake(mrst->irq);
  343. }
  344. dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
  345. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  346. tmp);
  347. return 0;
  348. }
  349. /*
  350. * We want RTC alarms to wake us from the deep power saving state
  351. */
  352. static inline int mrst_poweroff(struct device *dev)
  353. {
  354. return mrst_suspend(dev);
  355. }
  356. static int mrst_resume(struct device *dev)
  357. {
  358. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  359. unsigned char tmp = mrst->suspend_ctrl;
  360. /* Re-enable any irqs previously active */
  361. if (tmp & RTC_IRQMASK) {
  362. unsigned char mask;
  363. if (mrst->enabled_wake) {
  364. disable_irq_wake(mrst->irq);
  365. mrst->enabled_wake = 0;
  366. }
  367. spin_lock_irq(&rtc_lock);
  368. do {
  369. vrtc_cmos_write(tmp, RTC_CONTROL);
  370. mask = vrtc_cmos_read(RTC_INTR_FLAGS);
  371. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  372. if (!is_intr(mask))
  373. break;
  374. rtc_update_irq(mrst->rtc, 1, mask);
  375. tmp &= ~RTC_AIE;
  376. } while (mask & RTC_AIE);
  377. spin_unlock_irq(&rtc_lock);
  378. }
  379. dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
  380. return 0;
  381. }
  382. static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
  383. #define MRST_PM_OPS (&mrst_pm_ops)
  384. #else
  385. #define MRST_PM_OPS NULL
  386. static inline int mrst_poweroff(struct device *dev)
  387. {
  388. return -ENOSYS;
  389. }
  390. #endif
  391. static int vrtc_mrst_platform_probe(struct platform_device *pdev)
  392. {
  393. return vrtc_mrst_do_probe(&pdev->dev,
  394. platform_get_resource(pdev, IORESOURCE_MEM, 0),
  395. platform_get_irq(pdev, 0));
  396. }
  397. static int vrtc_mrst_platform_remove(struct platform_device *pdev)
  398. {
  399. rtc_mrst_do_remove(&pdev->dev);
  400. return 0;
  401. }
  402. static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
  403. {
  404. if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
  405. return;
  406. rtc_mrst_do_shutdown();
  407. }
  408. MODULE_ALIAS("platform:vrtc_mrst");
  409. static struct platform_driver vrtc_mrst_platform_driver = {
  410. .probe = vrtc_mrst_platform_probe,
  411. .remove = vrtc_mrst_platform_remove,
  412. .shutdown = vrtc_mrst_platform_shutdown,
  413. .driver = {
  414. .name = driver_name,
  415. .pm = MRST_PM_OPS,
  416. }
  417. };
  418. module_platform_driver(vrtc_mrst_platform_driver);
  419. MODULE_AUTHOR("Jacob Pan; Feng Tang");
  420. MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
  421. MODULE_LICENSE("GPL");