rtc-mrst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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/module.h>
  34. #include <linux/init.h>
  35. #include <linux/sfi.h>
  36. #include <asm-generic/rtc.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. /* Basic alarms only support hour, minute, and seconds fields.
  129. * Some also support day and month, for alarms up to a year in
  130. * the future.
  131. */
  132. t->time.tm_mday = -1;
  133. t->time.tm_mon = -1;
  134. t->time.tm_year = -1;
  135. /* vRTC only supports binary mode */
  136. spin_lock_irq(&rtc_lock);
  137. t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
  138. t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
  139. t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
  140. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  141. spin_unlock_irq(&rtc_lock);
  142. t->enabled = !!(rtc_control & RTC_AIE);
  143. t->pending = 0;
  144. return 0;
  145. }
  146. static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
  147. {
  148. unsigned char rtc_intr;
  149. /*
  150. * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  151. * allegedly some older rtcs need that to handle irqs properly
  152. */
  153. rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
  154. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  155. if (is_intr(rtc_intr))
  156. rtc_update_irq(mrst->rtc, 1, rtc_intr);
  157. }
  158. static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
  159. {
  160. unsigned char rtc_control;
  161. /*
  162. * Flush any pending IRQ status, notably for update irqs,
  163. * before we enable new IRQs
  164. */
  165. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  166. mrst_checkintr(mrst, rtc_control);
  167. rtc_control |= mask;
  168. vrtc_cmos_write(rtc_control, RTC_CONTROL);
  169. mrst_checkintr(mrst, rtc_control);
  170. }
  171. static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
  172. {
  173. unsigned char rtc_control;
  174. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  175. rtc_control &= ~mask;
  176. vrtc_cmos_write(rtc_control, RTC_CONTROL);
  177. mrst_checkintr(mrst, rtc_control);
  178. }
  179. static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  180. {
  181. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  182. unsigned char hrs, min, sec;
  183. int ret = 0;
  184. if (!mrst->irq)
  185. return -EIO;
  186. hrs = t->time.tm_hour;
  187. min = t->time.tm_min;
  188. sec = t->time.tm_sec;
  189. spin_lock_irq(&rtc_lock);
  190. /* Next rtc irq must not be from previous alarm setting */
  191. mrst_irq_disable(mrst, RTC_AIE);
  192. /* Update alarm */
  193. vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
  194. vrtc_cmos_write(min, RTC_MINUTES_ALARM);
  195. vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
  196. spin_unlock_irq(&rtc_lock);
  197. ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
  198. if (ret)
  199. return ret;
  200. spin_lock_irq(&rtc_lock);
  201. if (t->enabled)
  202. mrst_irq_enable(mrst, RTC_AIE);
  203. spin_unlock_irq(&rtc_lock);
  204. return 0;
  205. }
  206. /* Currently, the vRTC doesn't support UIE ON/OFF */
  207. static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  208. {
  209. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  210. unsigned long flags;
  211. spin_lock_irqsave(&rtc_lock, flags);
  212. if (enabled)
  213. mrst_irq_enable(mrst, RTC_AIE);
  214. else
  215. mrst_irq_disable(mrst, RTC_AIE);
  216. spin_unlock_irqrestore(&rtc_lock, flags);
  217. return 0;
  218. }
  219. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  220. static int mrst_procfs(struct device *dev, struct seq_file *seq)
  221. {
  222. unsigned char rtc_control, valid;
  223. spin_lock_irq(&rtc_lock);
  224. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  225. valid = vrtc_cmos_read(RTC_VALID);
  226. spin_unlock_irq(&rtc_lock);
  227. seq_printf(seq,
  228. "periodic_IRQ\t: %s\n"
  229. "alarm\t\t: %s\n"
  230. "BCD\t\t: no\n"
  231. "periodic_freq\t: daily (not adjustable)\n",
  232. (rtc_control & RTC_PIE) ? "on" : "off",
  233. (rtc_control & RTC_AIE) ? "on" : "off");
  234. return 0;
  235. }
  236. #else
  237. #define mrst_procfs NULL
  238. #endif
  239. static const struct rtc_class_ops mrst_rtc_ops = {
  240. .read_time = mrst_read_time,
  241. .set_time = mrst_set_time,
  242. .read_alarm = mrst_read_alarm,
  243. .set_alarm = mrst_set_alarm,
  244. .proc = mrst_procfs,
  245. .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
  246. };
  247. static struct mrst_rtc mrst_rtc;
  248. /*
  249. * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
  250. * Reg B, so no need for this driver to clear it
  251. */
  252. static irqreturn_t mrst_rtc_irq(int irq, void *p)
  253. {
  254. u8 irqstat;
  255. spin_lock(&rtc_lock);
  256. /* This read will clear all IRQ flags inside Reg C */
  257. irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
  258. spin_unlock(&rtc_lock);
  259. irqstat &= RTC_IRQMASK | RTC_IRQF;
  260. if (is_intr(irqstat)) {
  261. rtc_update_irq(p, 1, irqstat);
  262. return IRQ_HANDLED;
  263. }
  264. return IRQ_NONE;
  265. }
  266. static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
  267. int rtc_irq)
  268. {
  269. int retval = 0;
  270. unsigned char rtc_control;
  271. /* There can be only one ... */
  272. if (mrst_rtc.dev)
  273. return -EBUSY;
  274. if (!iomem)
  275. return -ENODEV;
  276. iomem = request_mem_region(iomem->start, resource_size(iomem),
  277. driver_name);
  278. if (!iomem) {
  279. dev_dbg(dev, "i/o mem already in use.\n");
  280. return -EBUSY;
  281. }
  282. mrst_rtc.irq = rtc_irq;
  283. mrst_rtc.iomem = iomem;
  284. mrst_rtc.dev = dev;
  285. dev_set_drvdata(dev, &mrst_rtc);
  286. mrst_rtc.rtc = rtc_device_register(driver_name, dev,
  287. &mrst_rtc_ops, THIS_MODULE);
  288. if (IS_ERR(mrst_rtc.rtc)) {
  289. retval = PTR_ERR(mrst_rtc.rtc);
  290. goto cleanup0;
  291. }
  292. rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
  293. spin_lock_irq(&rtc_lock);
  294. mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
  295. rtc_control = vrtc_cmos_read(RTC_CONTROL);
  296. spin_unlock_irq(&rtc_lock);
  297. if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
  298. dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
  299. if (rtc_irq) {
  300. retval = request_irq(rtc_irq, mrst_rtc_irq,
  301. 0, dev_name(&mrst_rtc.rtc->dev),
  302. mrst_rtc.rtc);
  303. if (retval < 0) {
  304. dev_dbg(dev, "IRQ %d is already in use, err %d\n",
  305. rtc_irq, retval);
  306. goto cleanup1;
  307. }
  308. }
  309. dev_dbg(dev, "initialised\n");
  310. return 0;
  311. cleanup1:
  312. rtc_device_unregister(mrst_rtc.rtc);
  313. cleanup0:
  314. mrst_rtc.dev = NULL;
  315. release_mem_region(iomem->start, resource_size(iomem));
  316. dev_err(dev, "rtc-mrst: unable to initialise\n");
  317. return retval;
  318. }
  319. static void rtc_mrst_do_shutdown(void)
  320. {
  321. spin_lock_irq(&rtc_lock);
  322. mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
  323. spin_unlock_irq(&rtc_lock);
  324. }
  325. static void rtc_mrst_do_remove(struct device *dev)
  326. {
  327. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  328. struct resource *iomem;
  329. rtc_mrst_do_shutdown();
  330. if (mrst->irq)
  331. free_irq(mrst->irq, mrst->rtc);
  332. rtc_device_unregister(mrst->rtc);
  333. mrst->rtc = NULL;
  334. iomem = mrst->iomem;
  335. release_mem_region(iomem->start, resource_size(iomem));
  336. mrst->iomem = NULL;
  337. mrst->dev = NULL;
  338. }
  339. #ifdef CONFIG_PM_SLEEP
  340. static int mrst_suspend(struct device *dev)
  341. {
  342. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  343. unsigned char tmp;
  344. /* Only the alarm might be a wakeup event source */
  345. spin_lock_irq(&rtc_lock);
  346. mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
  347. if (tmp & (RTC_PIE | RTC_AIE)) {
  348. unsigned char mask;
  349. if (device_may_wakeup(dev))
  350. mask = RTC_IRQMASK & ~RTC_AIE;
  351. else
  352. mask = RTC_IRQMASK;
  353. tmp &= ~mask;
  354. vrtc_cmos_write(tmp, RTC_CONTROL);
  355. mrst_checkintr(mrst, tmp);
  356. }
  357. spin_unlock_irq(&rtc_lock);
  358. if (tmp & RTC_AIE) {
  359. mrst->enabled_wake = 1;
  360. enable_irq_wake(mrst->irq);
  361. }
  362. dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
  363. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  364. tmp);
  365. return 0;
  366. }
  367. /*
  368. * We want RTC alarms to wake us from the deep power saving state
  369. */
  370. static inline int mrst_poweroff(struct device *dev)
  371. {
  372. return mrst_suspend(dev);
  373. }
  374. static int mrst_resume(struct device *dev)
  375. {
  376. struct mrst_rtc *mrst = dev_get_drvdata(dev);
  377. unsigned char tmp = mrst->suspend_ctrl;
  378. /* Re-enable any irqs previously active */
  379. if (tmp & RTC_IRQMASK) {
  380. unsigned char mask;
  381. if (mrst->enabled_wake) {
  382. disable_irq_wake(mrst->irq);
  383. mrst->enabled_wake = 0;
  384. }
  385. spin_lock_irq(&rtc_lock);
  386. do {
  387. vrtc_cmos_write(tmp, RTC_CONTROL);
  388. mask = vrtc_cmos_read(RTC_INTR_FLAGS);
  389. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  390. if (!is_intr(mask))
  391. break;
  392. rtc_update_irq(mrst->rtc, 1, mask);
  393. tmp &= ~RTC_AIE;
  394. } while (mask & RTC_AIE);
  395. spin_unlock_irq(&rtc_lock);
  396. }
  397. dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
  398. return 0;
  399. }
  400. static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
  401. #define MRST_PM_OPS (&mrst_pm_ops)
  402. #else
  403. #define MRST_PM_OPS NULL
  404. static inline int mrst_poweroff(struct device *dev)
  405. {
  406. return -ENOSYS;
  407. }
  408. #endif
  409. static int vrtc_mrst_platform_probe(struct platform_device *pdev)
  410. {
  411. return vrtc_mrst_do_probe(&pdev->dev,
  412. platform_get_resource(pdev, IORESOURCE_MEM, 0),
  413. platform_get_irq(pdev, 0));
  414. }
  415. static int vrtc_mrst_platform_remove(struct platform_device *pdev)
  416. {
  417. rtc_mrst_do_remove(&pdev->dev);
  418. return 0;
  419. }
  420. static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
  421. {
  422. if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
  423. return;
  424. rtc_mrst_do_shutdown();
  425. }
  426. MODULE_ALIAS("platform:vrtc_mrst");
  427. static struct platform_driver vrtc_mrst_platform_driver = {
  428. .probe = vrtc_mrst_platform_probe,
  429. .remove = vrtc_mrst_platform_remove,
  430. .shutdown = vrtc_mrst_platform_shutdown,
  431. .driver = {
  432. .name = driver_name,
  433. .pm = MRST_PM_OPS,
  434. }
  435. };
  436. module_platform_driver(vrtc_mrst_platform_driver);
  437. MODULE_AUTHOR("Jacob Pan; Feng Tang");
  438. MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
  439. MODULE_LICENSE("GPL");