intel_mid_vrtc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * intel_mid_vrtc.c: Driver for virtual RTC device on Intel MID platform
  3. *
  4. * (C) Copyright 2009 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; version 2
  9. * of the License.
  10. *
  11. * Note:
  12. * VRTC is emulated by system controller firmware, the real HW
  13. * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
  14. * in a memory mapped IO space that is visible to the host IA
  15. * processor.
  16. *
  17. * This driver is based on RTC CMOS driver.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/export.h>
  21. #include <linux/init.h>
  22. #include <linux/sfi.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mc146818rtc.h>
  25. #include <asm/intel-mid.h>
  26. #include <asm/intel_mid_vrtc.h>
  27. #include <asm/time.h>
  28. #include <asm/fixmap.h>
  29. static unsigned char __iomem *vrtc_virt_base;
  30. unsigned char vrtc_cmos_read(unsigned char reg)
  31. {
  32. unsigned char retval;
  33. /* vRTC's registers range from 0x0 to 0xD */
  34. if (reg > 0xd || !vrtc_virt_base)
  35. return 0xff;
  36. lock_cmos_prefix(reg);
  37. retval = __raw_readb(vrtc_virt_base + (reg << 2));
  38. lock_cmos_suffix(reg);
  39. return retval;
  40. }
  41. EXPORT_SYMBOL_GPL(vrtc_cmos_read);
  42. void vrtc_cmos_write(unsigned char val, unsigned char reg)
  43. {
  44. if (reg > 0xd || !vrtc_virt_base)
  45. return;
  46. lock_cmos_prefix(reg);
  47. __raw_writeb(val, vrtc_virt_base + (reg << 2));
  48. lock_cmos_suffix(reg);
  49. }
  50. EXPORT_SYMBOL_GPL(vrtc_cmos_write);
  51. void vrtc_get_time(struct timespec *now)
  52. {
  53. u8 sec, min, hour, mday, mon;
  54. unsigned long flags;
  55. u32 year;
  56. spin_lock_irqsave(&rtc_lock, flags);
  57. while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
  58. cpu_relax();
  59. sec = vrtc_cmos_read(RTC_SECONDS);
  60. min = vrtc_cmos_read(RTC_MINUTES);
  61. hour = vrtc_cmos_read(RTC_HOURS);
  62. mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
  63. mon = vrtc_cmos_read(RTC_MONTH);
  64. year = vrtc_cmos_read(RTC_YEAR);
  65. spin_unlock_irqrestore(&rtc_lock, flags);
  66. /* vRTC YEAR reg contains the offset to 1972 */
  67. year += 1972;
  68. pr_info("vRTC: sec: %d min: %d hour: %d day: %d "
  69. "mon: %d year: %d\n", sec, min, hour, mday, mon, year);
  70. now->tv_sec = mktime(year, mon, mday, hour, min, sec);
  71. now->tv_nsec = 0;
  72. }
  73. int vrtc_set_mmss(const struct timespec *now)
  74. {
  75. unsigned long flags;
  76. struct rtc_time tm;
  77. int year;
  78. int retval = 0;
  79. rtc_time_to_tm(now->tv_sec, &tm);
  80. if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) {
  81. /*
  82. * tm.year is the number of years since 1900, and the
  83. * vrtc need the years since 1972.
  84. */
  85. year = tm.tm_year - 72;
  86. spin_lock_irqsave(&rtc_lock, flags);
  87. vrtc_cmos_write(year, RTC_YEAR);
  88. vrtc_cmos_write(tm.tm_mon, RTC_MONTH);
  89. vrtc_cmos_write(tm.tm_mday, RTC_DAY_OF_MONTH);
  90. vrtc_cmos_write(tm.tm_hour, RTC_HOURS);
  91. vrtc_cmos_write(tm.tm_min, RTC_MINUTES);
  92. vrtc_cmos_write(tm.tm_sec, RTC_SECONDS);
  93. spin_unlock_irqrestore(&rtc_lock, flags);
  94. } else {
  95. pr_err("%s: Invalid vRTC value: write of %lx to vRTC failed\n",
  96. __func__, now->tv_sec);
  97. retval = -EINVAL;
  98. }
  99. return retval;
  100. }
  101. void __init intel_mid_rtc_init(void)
  102. {
  103. unsigned long vrtc_paddr;
  104. sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
  105. vrtc_paddr = sfi_mrtc_array[0].phys_addr;
  106. if (!sfi_mrtc_num || !vrtc_paddr)
  107. return;
  108. vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
  109. vrtc_paddr);
  110. x86_platform.get_wallclock = vrtc_get_time;
  111. x86_platform.set_wallclock = vrtc_set_mmss;
  112. }
  113. /*
  114. * The Moorestown platform has a memory mapped virtual RTC device that emulates
  115. * the programming interface of the RTC.
  116. */
  117. static struct resource vrtc_resources[] = {
  118. [0] = {
  119. .flags = IORESOURCE_MEM,
  120. },
  121. [1] = {
  122. .flags = IORESOURCE_IRQ,
  123. }
  124. };
  125. static struct platform_device vrtc_device = {
  126. .name = "rtc_mrst",
  127. .id = -1,
  128. .resource = vrtc_resources,
  129. .num_resources = ARRAY_SIZE(vrtc_resources),
  130. };
  131. /* Register the RTC device if appropriate */
  132. static int __init intel_mid_device_create(void)
  133. {
  134. /* No Moorestown, no device */
  135. if (!intel_mid_identify_cpu())
  136. return -ENODEV;
  137. /* No timer, no device */
  138. if (!sfi_mrtc_num)
  139. return -ENODEV;
  140. /* iomem resource */
  141. vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
  142. vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
  143. MRST_VRTC_MAP_SZ;
  144. /* irq resource */
  145. vrtc_resources[1].start = sfi_mrtc_array[0].irq;
  146. vrtc_resources[1].end = sfi_mrtc_array[0].irq;
  147. return platform_device_register(&vrtc_device);
  148. }
  149. device_initcall(intel_mid_device_create);