a2kbbc.c revision 1.22
1/*	$NetBSD: a2kbbc.c,v 1.22 2011/02/08 20:20:08 rmind Exp $ */
2
3/*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 *
38 *	@(#)clock.c	7.6 (Berkeley) 5/7/91
39 */
40
41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: a2kbbc.c,v 1.22 2011/02/08 20:20:08 rmind Exp $");
43
44#include <sys/param.h>
45#include <sys/kernel.h>
46#include <sys/device.h>
47#include <sys/systm.h>
48#include <machine/psl.h>
49#include <machine/cpu.h>
50#include <amiga/amiga/device.h>
51#include <amiga/amiga/custom.h>
52#include <amiga/amiga/cia.h>
53#include <amiga/dev/rtc.h>
54#include <amiga/dev/zbusvar.h>
55
56#include <dev/clock_subr.h>
57
58int a2kbbc_match(struct device *, struct cfdata *, void *);
59void a2kbbc_attach(struct device *, struct device *, void *);
60
61CFATTACH_DECL(a2kbbc, sizeof(struct device),
62    a2kbbc_match, a2kbbc_attach, NULL, NULL);
63
64void *a2kclockaddr;
65int a2kugettod(todr_chip_handle_t, struct clock_ymdhms *);
66int a2kusettod(todr_chip_handle_t, struct clock_ymdhms *);
67static struct todr_chip_handle a2ktodr;
68
69int
70a2kbbc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
71{
72	struct clock_ymdhms dt;
73	static int a2kbbc_matched = 0;
74
75	if (!matchname("a2kbbc", auxp))
76		return (0);
77
78	/* Allow only one instance. */
79	if (a2kbbc_matched)
80		return (0);
81
82	if (/* is_a1200() || */ is_a3000() || is_a4000()
83#ifdef DRACO
84	    || is_draco()
85#endif
86	    )
87		return (0);
88
89	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
90	if (a2kugettod(&a2ktodr, &dt) != 0)
91		return (0);
92
93	a2kbbc_matched = 1;
94	return (1);
95}
96
97/*
98 * Attach us to the rtc function pointers.
99 */
100void
101a2kbbc_attach(struct device *pdp, struct device *dp, void *auxp)
102{
103	printf("\n");
104	a2kclockaddr = (void *)__UNVOLATILE(ztwomap(0xdc0000));
105
106	a2ktodr.cookie = a2kclockaddr;
107	a2ktodr.todr_gettime_ymdhms = a2kugettod;
108	a2ktodr.todr_settime_ymdhms = a2kusettod;
109	todr_attach(&a2ktodr);
110}
111
112int
113a2kugettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
114{
115	struct rtclock2000 *rt;
116	int i;
117
118	rt = a2kclockaddr;
119
120	/*
121	 * hold clock
122	 */
123	rt->control1 |= A2CONTROL1_HOLD;
124	i = 0x1000;
125	while (rt->control1 & A2CONTROL1_BUSY && i--)
126		;
127	if (rt->control1 & A2CONTROL1_BUSY)
128		return (ENXIO);		/* Give up and say it's not there */
129
130	/* Copy the info.  Careful about the order! */
131	dt->dt_sec   = rt->second1 * 10 + rt->second2;
132	dt->dt_min   = rt->minute1 * 10 + rt->minute2;
133	dt->dt_hour  = (rt->hour1 & 3) * 10 + rt->hour2;
134	dt->dt_day   = rt->day1    * 10 + rt->day2;
135	dt->dt_mon   = rt->month1  * 10 + rt->month2;
136	dt->dt_year  = rt->year1   * 10 + rt->year2;
137	dt->dt_wday  = rt->weekday;
138
139	/*
140	 * The oki clock chip has a register to put the clock into
141	 * 12/24h mode.
142	 *
143	 *  clockmode   |    A2HOUR1_PM
144	 *  24h   12h   |  am = 0, pm = 1
145	 * ---------------------------------
146	 *   0    12    |       0
147	 *   1     1    |       0
148	 *  ..    ..    |       0
149	 *  11    11    |       0
150	 *  12    12    |       1
151	 *  13     1    |       1
152	 *  ..    ..    |       1
153	 *  23    11    |       1
154	 *
155	 */
156
157	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
158		if ((rt->hour1 & A2HOUR1_PM) == 0 && dt->dt_hour == 12)
159			dt->dt_hour = 0;
160		else if ((rt->hour1 & A2HOUR1_PM) && dt->dt_hour != 12)
161			dt->dt_hour += 12;
162	}
163
164	/*
165	 * release the clock
166	 */
167	rt->control1 &= ~A2CONTROL1_HOLD;
168
169	dt->dt_year += CLOCK_BASE_YEAR;
170	if (dt->dt_year < STARTOFTIME)
171		dt->dt_year += 100;
172
173	/*
174	 * Note that this check is redundant with one in kern_todr.c, but
175	 * attach relies on it being here.
176	 */
177	if ((dt->dt_hour > 23) ||
178	    (dt->dt_day  > 31) ||
179	    (dt->dt_mon  > 12) ||
180	    /* (dt->dt_year < STARTOFTIME) || */ (dt->dt_year > 2036))
181		return (EINVAL);
182
183	return (0);
184}
185
186int
187a2kusettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
188{
189	struct rtclock2000 *rt;
190	int ampm, i;
191
192	rt = a2kclockaddr;
193	/*
194	 * there seem to be problems with the bitfield addressing
195	 * currently used..
196	 */
197	if (! rt)
198		return (ENXIO);
199
200	/*
201	 * hold clock
202	 */
203	rt->control1 |= A2CONTROL1_HOLD;
204	i = 0x1000;
205	while (rt->control1 & A2CONTROL1_BUSY && i--)
206		;
207	if (rt->control1 & A2CONTROL1_BUSY)
208		return (ENXIO);		/* Give up and say it's not there */
209
210	ampm = 0;
211	if ((rt->control3 & A2CONTROL3_24HMODE) == 0) {
212		if (dt->dt_hour >= 12) {
213			ampm = A2HOUR1_PM;
214			if (dt->dt_hour != 12)
215				dt->dt_hour -= 12;
216		} else if (dt->dt_hour == 0) {
217			dt->dt_hour = 12;
218		}
219	}
220	rt->hour1   = (dt->dt_hour / 10) | ampm;
221	rt->hour2   = dt->dt_hour % 10;
222	rt->second1 = dt->dt_sec / 10;
223	rt->second2 = dt->dt_sec % 10;
224	rt->minute1 = dt->dt_min / 10;
225	rt->minute2 = dt->dt_min % 10;
226	rt->day1    = dt->dt_day / 10;
227	rt->day2    = dt->dt_day % 10;
228	rt->month1  = dt->dt_mon / 10;
229	rt->month2  = dt->dt_mon % 10;
230	rt->year1   = (dt->dt_year / 10) % 10;
231	rt->year2   = dt->dt_year % 10;
232	rt->weekday = dt->dt_wday;
233
234	/*
235	 * release the clock
236	 */
237	rt->control2 &= ~A2CONTROL1_HOLD;
238
239	return (0);
240}
241