vrtc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * 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/init.h>
  21. #include <linux/sfi.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/mrst.h>
  24. #include <asm/mrst-vrtc.h>
  25. #include <asm/time.h>
  26. #include <asm/fixmap.h>
  27. static unsigned char __iomem *vrtc_virt_base;
  28. unsigned char vrtc_cmos_read(unsigned char reg)
  29. {
  30. unsigned char retval;
  31. /* vRTC's registers range from 0x0 to 0xD */
  32. if (reg > 0xd || !vrtc_virt_base)
  33. return 0xff;
  34. lock_cmos_prefix(reg);
  35. retval = __raw_readb(vrtc_virt_base + (reg << 2));
  36. lock_cmos_suffix(reg);
  37. return retval;
  38. }
  39. EXPORT_SYMBOL_GPL(vrtc_cmos_read);
  40. void vrtc_cmos_write(unsigned char val, unsigned char reg)
  41. {
  42. if (reg > 0xd || !vrtc_virt_base)
  43. return;
  44. lock_cmos_prefix(reg);
  45. __raw_writeb(val, vrtc_virt_base + (reg << 2));
  46. lock_cmos_suffix(reg);
  47. }
  48. EXPORT_SYMBOL_GPL(vrtc_cmos_write);
  49. unsigned long vrtc_get_time(void)
  50. {
  51. u8 sec, min, hour, mday, mon;
  52. unsigned long flags;
  53. u32 year;
  54. spin_lock_irqsave(&rtc_lock, flags);
  55. while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
  56. cpu_relax();
  57. sec = vrtc_cmos_read(RTC_SECONDS);
  58. min = vrtc_cmos_read(RTC_MINUTES);
  59. hour = vrtc_cmos_read(RTC_HOURS);
  60. mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
  61. mon = vrtc_cmos_read(RTC_MONTH);
  62. year = vrtc_cmos_read(RTC_YEAR);
  63. spin_unlock_irqrestore(&rtc_lock, flags);
  64. /* vRTC YEAR reg contains the offset to 1960 */
  65. year += 1960;
  66. printk(KERN_INFO "vRTC: sec: %d min: %d hour: %d day: %d "
  67. "mon: %d year: %d\n", sec, min, hour, mday, mon, year);
  68. return mktime(year, mon, mday, hour, min, sec);
  69. }
  70. /* Only care about the minutes and seconds */
  71. int vrtc_set_mmss(unsigned long nowtime)
  72. {
  73. int real_sec, real_min;
  74. unsigned long flags;
  75. int vrtc_min;
  76. spin_lock_irqsave(&rtc_lock, flags);
  77. vrtc_min = vrtc_cmos_read(RTC_MINUTES);
  78. real_sec = nowtime % 60;
  79. real_min = nowtime / 60;
  80. if (((abs(real_min - vrtc_min) + 15)/30) & 1)
  81. real_min += 30;
  82. real_min %= 60;
  83. vrtc_cmos_write(real_sec, RTC_SECONDS);
  84. vrtc_cmos_write(real_min, RTC_MINUTES);
  85. spin_unlock_irqrestore(&rtc_lock, flags);
  86. return 0;
  87. }
  88. void __init mrst_rtc_init(void)
  89. {
  90. unsigned long vrtc_paddr;
  91. sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
  92. vrtc_paddr = sfi_mrtc_array[0].phys_addr;
  93. if (!sfi_mrtc_num || !vrtc_paddr)
  94. return;
  95. vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
  96. vrtc_paddr);
  97. x86_platform.get_wallclock = vrtc_get_time;
  98. x86_platform.set_wallclock = vrtc_set_mmss;
  99. }
  100. /*
  101. * The Moorestown platform has a memory mapped virtual RTC device that emulates
  102. * the programming interface of the RTC.
  103. */
  104. static struct resource vrtc_resources[] = {
  105. [0] = {
  106. .flags = IORESOURCE_MEM,
  107. },
  108. [1] = {
  109. .flags = IORESOURCE_IRQ,
  110. }
  111. };
  112. static struct platform_device vrtc_device = {
  113. .name = "rtc_mrst",
  114. .id = -1,
  115. .resource = vrtc_resources,
  116. .num_resources = ARRAY_SIZE(vrtc_resources),
  117. };
  118. /* Register the RTC device if appropriate */
  119. static int __init mrst_device_create(void)
  120. {
  121. /* No Moorestown, no device */
  122. if (!mrst_identify_cpu())
  123. return -ENODEV;
  124. /* No timer, no device */
  125. if (!sfi_mrtc_num)
  126. return -ENODEV;
  127. /* iomem resource */
  128. vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
  129. vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
  130. MRST_VRTC_MAP_SZ;
  131. /* irq resource */
  132. vrtc_resources[1].start = sfi_mrtc_array[0].irq;
  133. vrtc_resources[1].end = sfi_mrtc_array[0].irq;
  134. return platform_device_register(&vrtc_device);
  135. }
  136. module_init(mrst_device_create);