[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/isa/atrtc.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (c) 2008 Poul-Henning Kamp
  3  * All rights reserved.
  4  *
  5  * Redistribution and use in source and binary forms, with or without
  6  * modification, are permitted provided that the following conditions
  7  * are met:
  8  * 1. Redistributions of source code must retain the above copyright
  9  *    notice, this list of conditions and the following disclaimer.
 10  * 2. Redistributions in binary form must reproduce the above copyright
 11  *    notice, this list of conditions and the following disclaimer in the
 12  *    documentation and/or other materials provided with the distribution.
 13  *
 14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  * SUCH DAMAGE.
 25  *
 26  * $FreeBSD: src/sys/isa/atrtc.c,v 1.247 2008/04/14 07:57:15 phk Exp $
 27  */
 28 
 29 #include <sys/cdefs.h>
 30 __FBSDID("$FreeBSD: src/sys/isa/atrtc.c,v 1.247 2008/04/14 07:57:15 phk Exp $");
 31 
 32 #include "opt_isa.h"
 33 
 34 #include <sys/param.h>
 35 #include <sys/systm.h>
 36 #include <sys/bus.h>
 37 #include <sys/clock.h>
 38 #include <sys/lock.h>
 39 #include <sys/mutex.h>
 40 #include <sys/kernel.h>
 41 #include <sys/module.h>
 42 
 43 #include <isa/rtc.h>
 44 #ifdef DEV_ISA
 45 #include <isa/isareg.h>
 46 #include <isa/isavar.h>
 47 #endif
 48 
 49 #define RTC_LOCK        mtx_lock_spin(&clock_lock)
 50 #define RTC_UNLOCK      mtx_unlock_spin(&clock_lock)
 51 
 52 static  int     rtc_reg = -1;
 53 static  u_char  rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF;
 54 static  u_char  rtc_statusb = RTCSB_24HR;
 55 
 56 /*
 57  * RTC support routines
 58  */
 59 
 60 int
 61 rtcin(int reg)
 62 {
 63         u_char val;
 64 
 65         RTC_LOCK;
 66         if (rtc_reg != reg) {
 67                 inb(0x84);
 68                 outb(IO_RTC, reg);
 69                 rtc_reg = reg;
 70                 inb(0x84);
 71         }
 72         val = inb(IO_RTC + 1);
 73         RTC_UNLOCK;
 74         return (val);
 75 }
 76 
 77 void
 78 writertc(int reg, u_char val)
 79 {
 80 
 81         RTC_LOCK;
 82         if (rtc_reg != reg) {
 83                 inb(0x84);
 84                 outb(IO_RTC, reg);
 85                 rtc_reg = reg;
 86                 inb(0x84);
 87         }
 88         outb(IO_RTC + 1, val);
 89         inb(0x84);
 90         RTC_UNLOCK;
 91 }
 92 
 93 static __inline int
 94 readrtc(int port)
 95 {
 96         return(bcd2bin(rtcin(port)));
 97 }
 98 
 99 void
100 atrtc_start(void)
101 {
102 
103         writertc(RTC_STATUSA, rtc_statusa);
104         writertc(RTC_STATUSB, RTCSB_24HR);
105 }
106 
107 void
108 atrtc_rate(unsigned rate)
109 {
110 
111         rtc_statusa = RTCSA_DIVIDER | rate;
112         writertc(RTC_STATUSA, rtc_statusa);
113 }
114 
115 void
116 atrtc_enable_intr(void)
117 {
118 
119         rtc_statusb |= RTCSB_PINTR;
120         writertc(RTC_STATUSB, rtc_statusb);
121         rtcin(RTC_INTR);
122 }
123 
124 void
125 atrtc_restore(void)
126 {
127 
128         /* Restore all of the RTC's "status" (actually, control) registers. */
129         rtcin(RTC_STATUSA);     /* dummy to get rtc_reg set */
130         writertc(RTC_STATUSB, RTCSB_24HR);
131         writertc(RTC_STATUSA, rtc_statusa);
132         writertc(RTC_STATUSB, rtc_statusb);
133         rtcin(RTC_INTR);
134 }
135 
136 /**********************************************************************
137  * RTC driver for subr_rtc
138  */
139 
140 #include "clock_if.h"
141 
142 #include <sys/rman.h>
143 
144 struct atrtc_softc {
145         int port_rid, intr_rid;
146         struct resource *port_res;
147         struct resource *intr_res;
148 };
149 
150 /*
151  * Attach to the ISA PnP descriptors for the timer and realtime clock.
152  */
153 static struct isa_pnp_id atrtc_ids[] = {
154         { 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
155         { 0 }
156 };
157 
158 static int
159 atrtc_probe(device_t dev)
160 {
161         int result;
162         
163         device_set_desc(dev, "AT Real Time Clock");
164         result = ISA_PNP_PROBE(device_get_parent(dev), dev, atrtc_ids);
165         /* ENXIO if wrong PnP-ID, ENOENT ifno PnP-ID, zero if good PnP-iD */
166         if (result != ENOENT)
167                 return(result);
168         /* All PC's have an RTC, and we're hosed without it, so... */
169         return (BUS_PROBE_LOW_PRIORITY);
170 }
171 
172 static int
173 atrtc_attach(device_t dev)
174 {
175         struct atrtc_softc *sc;
176 
177         /*
178          * Not that we need them or anything, but grab our resources
179          * so they show up, correctly attributed, in the big picture.
180          */
181         
182         sc = device_get_softc(dev);
183         if (!(sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
184             &sc->port_rid, IO_RTC, IO_RTC + 1, 2, RF_ACTIVE)))
185                 device_printf(dev,"Warning: Couldn't map I/O.\n");
186         if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
187             &sc->intr_rid, 8, 8, 1, RF_ACTIVE)))
188                 device_printf(dev,"Warning: Couldn't map Interrupt.\n");
189         clock_register(dev, 1000000);
190         return(0);
191 }
192 
193 
194 static int
195 atrtc_settime(device_t dev __unused, struct timespec *ts)
196 {
197         struct clocktime ct;
198 
199         clock_ts_to_ct(ts, &ct);
200 
201         /* Disable RTC updates and interrupts. */
202         writertc(RTC_STATUSB, RTCSB_HALT | RTCSB_24HR);
203 
204         writertc(RTC_SEC, bin2bcd(ct.sec));             /* Write back Seconds */
205         writertc(RTC_MIN, bin2bcd(ct.min));             /* Write back Minutes */
206         writertc(RTC_HRS, bin2bcd(ct.hour));            /* Write back Hours   */
207 
208         writertc(RTC_WDAY, ct.dow + 1);                 /* Write back Weekday */
209         writertc(RTC_DAY, bin2bcd(ct.day));             /* Write back Day */
210         writertc(RTC_MONTH, bin2bcd(ct.mon));           /* Write back Month   */
211         writertc(RTC_YEAR, bin2bcd(ct.year % 100));     /* Write back Year    */
212 #ifdef USE_RTC_CENTURY
213         writertc(RTC_CENTURY, bin2bcd(ct.year / 100));  /* ... and Century    */
214 #endif
215 
216         /* Reenable RTC updates and interrupts. */
217         writertc(RTC_STATUSB, rtc_statusb);
218         rtcin(RTC_INTR);
219         return (0);
220 }
221 
222 static int
223 atrtc_gettime(device_t dev, struct timespec *ts)
224 {
225         struct clocktime ct;
226         int s;
227 
228         /* Look if we have a RTC present and the time is valid */
229         if (!(rtcin(RTC_STATUSD) & RTCSD_PWR)) {
230                 device_printf(dev, "WARNING: Battery failure indication\n");
231                 return (EINVAL);
232         }
233 
234         /* wait for time update to complete */
235         /* If RTCSA_TUP is zero, we have at least 244us before next update */
236         s = splhigh();
237         while (rtcin(RTC_STATUSA) & RTCSA_TUP) {
238                 splx(s);
239                 s = splhigh();
240         }
241         ct.nsec = 0;
242         ct.sec = readrtc(RTC_SEC);
243         ct.min = readrtc(RTC_MIN);
244         ct.hour = readrtc(RTC_HRS);
245         ct.day = readrtc(RTC_DAY);
246         ct.dow = readrtc(RTC_WDAY) - 1;
247         ct.mon = readrtc(RTC_MONTH);
248         ct.year = readrtc(RTC_YEAR);
249 #ifdef USE_RTC_CENTURY
250         ct.year += readrtc(RTC_CENTURY) * 100;
251 #else
252         ct.year += 2000;
253 #endif
254         /* Set dow = -1 because some clocks don't set it correctly. */
255         ct.dow = -1;
256         return (clock_ct_to_ts(&ct, ts));
257 }
258 
259 static device_method_t atrtc_methods[] = {
260         /* Device interface */
261         DEVMETHOD(device_probe,         atrtc_probe),
262         DEVMETHOD(device_attach,        atrtc_attach),
263         DEVMETHOD(device_detach,        bus_generic_detach),
264         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
265         DEVMETHOD(device_suspend,       bus_generic_suspend),
266                 /* XXX stop statclock? */
267         DEVMETHOD(device_resume,        bus_generic_resume),
268                 /* XXX restart statclock? */
269 
270         /* clock interface */
271         DEVMETHOD(clock_gettime,        atrtc_gettime),
272         DEVMETHOD(clock_settime,        atrtc_settime),
273 
274         { 0, 0 }
275 };
276 
277 static driver_t atrtc_driver = {
278         "atrtc",
279         atrtc_methods,
280         sizeof(struct atrtc_softc),
281 };
282 
283 static devclass_t atrtc_devclass;
284 
285 DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0);
286 DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0);
287 
288 #include "opt_ddb.h"
289 #ifdef DDB
290 #include <ddb/ddb.h>
291 
292 DB_SHOW_COMMAND(rtc, rtc)
293 {
294         printf("%02x/%02x/%02x %02x:%02x:%02x, A = %02x, B = %02x, C = %02x\n",
295                 rtcin(RTC_YEAR), rtcin(RTC_MONTH), rtcin(RTC_DAY),
296                 rtcin(RTC_HRS), rtcin(RTC_MIN), rtcin(RTC_SEC),
297                 rtcin(RTC_STATUSA), rtcin(RTC_STATUSB), rtcin(RTC_INTR));
298 }
299 #endif /* DDB */
300 

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.