rtc-mrst.c 12 KB

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