mcclock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* $OpenBSD: mcclock.c,v 1.3 2010/01/22 21:44:00 miod Exp $ */
  2. /* $NetBSD: mcclock.c,v 1.4 1996/10/13 02:59:41 christos Exp $ */
  3. /*
  4. * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
  5. * All rights reserved.
  6. *
  7. * Author: Chris G. Demetriou
  8. *
  9. * Permission to use, copy, modify and distribute this software and
  10. * its documentation is hereby granted, provided that both the copyright
  11. * notice and this permission notice appear in all copies of the
  12. * software, derivative works or modified versions, and any portions
  13. * thereof, and that both notices appear in supporting documentation.
  14. *
  15. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  16. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
  17. * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  18. *
  19. * Carnegie Mellon requests users of this software to return to
  20. *
  21. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  22. * School of Computer Science
  23. * Carnegie Mellon University
  24. * Pittsburgh PA 15213-3890
  25. *
  26. * any improvements or extensions that they make and grant Carnegie the
  27. * rights to redistribute these changes.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/kernel.h>
  31. #include <sys/systm.h>
  32. #include <sys/device.h>
  33. #include <mips64/dev/clockvar.h>
  34. #include <loongson/dev/mcclockvar.h>
  35. #include <dev/ic/mc146818reg.h>
  36. struct cfdriver mcclock_cd = {
  37. NULL, "mcclock", DV_DULL,
  38. };
  39. void mcclock_get(void *, time_t, struct tod_time *);
  40. void mcclock_set(void *, struct tod_time *);
  41. struct tod_desc mcclock_clockfns = {
  42. NULL, mcclock_get, mcclock_set,
  43. };
  44. #define mc146818_write(dev, reg, datum) \
  45. (*(dev)->sc_busfns->mc_bf_write)(dev, reg, datum)
  46. #define mc146818_read(dev, reg) \
  47. (*(dev)->sc_busfns->mc_bf_read)(dev, reg)
  48. void
  49. mcclock_attach(sc, busfns)
  50. struct mcclock_softc *sc;
  51. const struct mcclock_busfns *busfns;
  52. {
  53. printf(": mc146818 or compatible\n");
  54. sc->sc_busfns = busfns;
  55. /* Turn interrupts off, just in case. */
  56. mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
  57. mc146818_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_NONE);
  58. sys_tod.tod_cookie = sc;
  59. sys_tod.tod_get = mcclock_get;
  60. sys_tod.tod_set = mcclock_set;
  61. }
  62. /*
  63. * Get the time of day, based on the clock's value and/or the base value.
  64. */
  65. void
  66. mcclock_get(dev, base, ct)
  67. void *dev;
  68. time_t base;
  69. struct tod_time *ct;
  70. {
  71. struct mcclock_softc *sc = (struct mcclock_softc *)dev;
  72. mc_todregs regs;
  73. int s;
  74. s = splclock();
  75. MC146818_GETTOD(sc, &regs)
  76. splx(s);
  77. ct->sec = regs[MC_SEC];
  78. ct->min = regs[MC_MIN];
  79. ct->hour = regs[MC_HOUR];
  80. ct->dow = regs[MC_DOW];
  81. ct->day = regs[MC_DOM];
  82. ct->mon = regs[MC_MONTH];
  83. ct->year = regs[MC_YEAR] + 100;
  84. }
  85. /*
  86. * Reset the TODR based on the time value.
  87. */
  88. void
  89. mcclock_set(dev, ct)
  90. void *dev;
  91. struct tod_time *ct;
  92. {
  93. struct mcclock_softc *sc = (struct mcclock_softc *)dev;
  94. mc_todregs regs;
  95. int s;
  96. s = splclock();
  97. MC146818_GETTOD(sc, &regs);
  98. splx(s);
  99. regs[MC_SEC] = ct->sec;
  100. regs[MC_MIN] = ct->min;
  101. regs[MC_HOUR] = ct->hour;
  102. regs[MC_DOW] = ct->dow;
  103. regs[MC_DOM] = ct->day;
  104. regs[MC_MONTH] = ct->mon;
  105. regs[MC_YEAR] = ct->year - 100;
  106. s = splclock();
  107. MC146818_PUTTOD(sc, &regs);
  108. splx(s);
  109. }