1/*
2 *  arch/m68k/bvme6000/config.c
3 *
4 *  Copyright (C) 1997 Richard Hirst [richard@sleepie.demon.co.uk]
5 *
6 * Based on:
7 *
8 *  linux/amiga/config.c
9 *
10 *  Copyright (C) 1993 Hamish Macdonald
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License.  See the file README.legal in the main directory of this archive
14 * for more details.
15 */
16
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/tty.h>
21#include <linux/console.h>
22#include <linux/linkage.h>
23#include <linux/init.h>
24#include <linux/major.h>
25#include <linux/genhd.h>
26#include <linux/rtc.h>
27#include <linux/interrupt.h>
28
29#include <asm/bootinfo.h>
30#include <asm/system.h>
31#include <asm/pgtable.h>
32#include <asm/setup.h>
33#include <asm/irq.h>
34#include <asm/traps.h>
35#include <asm/rtc.h>
36#include <asm/machdep.h>
37#include <asm/bvme6000hw.h>
38
39static void bvme6000_get_model(char *model);
40static int  bvme6000_get_hardware_list(char *buffer);
41extern void bvme6000_sched_init(irq_handler_t handler);
42extern unsigned long bvme6000_gettimeoffset (void);
43extern int bvme6000_hwclk (int, struct rtc_time *);
44extern int bvme6000_set_clock_mmss (unsigned long);
45extern void bvme6000_reset (void);
46extern void bvme6000_waitbut(void);
47void bvme6000_set_vectors (void);
48
49static unsigned char bcd2bin (unsigned char b);
50static unsigned char bin2bcd (unsigned char b);
51
52/* Save tick handler routine pointer, will point to do_timer() in
53 * kernel/sched.c, called via bvme6000_process_int() */
54
55static irq_handler_t tick_handler;
56
57
58int bvme6000_parse_bootinfo(const struct bi_record *bi)
59{
60	if (bi->tag == BI_VME_TYPE)
61		return 0;
62	else
63		return 1;
64}
65
66void bvme6000_reset(void)
67{
68	volatile PitRegsPtr pit = (PitRegsPtr)BVME_PIT_BASE;
69
70	printk ("\r\n\nCalled bvme6000_reset\r\n"
71			"\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r");
72	/* The string of returns is to delay the reset until the whole
73	 * message is output. */
74	/* Enable the watchdog, via PIT port C bit 4 */
75
76	pit->pcddr	|= 0x10;	/* WDOG enable */
77
78	while(1)
79		;
80}
81
82static void bvme6000_get_model(char *model)
83{
84    sprintf(model, "BVME%d000", m68k_cputype == CPU_68060 ? 6 : 4);
85}
86
87
88/* No hardware options on BVME6000? */
89
90static int bvme6000_get_hardware_list(char *buffer)
91{
92    *buffer = '\0';
93    return 0;
94}
95
96/*
97 * This function is called during kernel startup to initialize
98 * the bvme6000 IRQ handling routines.
99 */
100static void bvme6000_init_IRQ(void)
101{
102	m68k_setup_user_interrupt(VEC_USER, 192, NULL);
103}
104
105void __init config_bvme6000(void)
106{
107    volatile PitRegsPtr pit = (PitRegsPtr)BVME_PIT_BASE;
108
109    /* Board type is only set by newer versions of vmelilo/tftplilo */
110    if (!vme_brdtype) {
111	if (m68k_cputype == CPU_68060)
112	    vme_brdtype = VME_TYPE_BVME6000;
113	else
114	    vme_brdtype = VME_TYPE_BVME4000;
115    }
116
117    mach_max_dma_address = 0xffffffff;
118    mach_sched_init      = bvme6000_sched_init;
119    mach_init_IRQ        = bvme6000_init_IRQ;
120    mach_gettimeoffset   = bvme6000_gettimeoffset;
121    mach_hwclk           = bvme6000_hwclk;
122    mach_set_clock_mmss	 = bvme6000_set_clock_mmss;
123    mach_reset		 = bvme6000_reset;
124    mach_get_model       = bvme6000_get_model;
125    mach_get_hardware_list = bvme6000_get_hardware_list;
126
127    printk ("Board is %sconfigured as a System Controller\n",
128		*config_reg_ptr & BVME_CONFIG_SW1 ? "" : "not ");
129
130    /* Now do the PIT configuration */
131
132    pit->pgcr	= 0x00;	/* Unidirectional 8 bit, no handshake for now */
133    pit->psrr	= 0x18;	/* PIACK and PIRQ functions enabled */
134    pit->pacr	= 0x00;	/* Sub Mode 00, H2 i/p, no DMA */
135    pit->padr	= 0x00;	/* Just to be tidy! */
136    pit->paddr	= 0x00;	/* All inputs for now (safest) */
137    pit->pbcr	= 0x80;	/* Sub Mode 1x, H4 i/p, no DMA */
138    pit->pbdr	= 0xbc | (*config_reg_ptr & BVME_CONFIG_SW1 ? 0 : 0x40);
139			/* PRI, SYSCON?, Level3, SCC clks from xtal */
140    pit->pbddr	= 0xf3;	/* Mostly outputs */
141    pit->pcdr	= 0x01;	/* PA transceiver disabled */
142    pit->pcddr	= 0x03;	/* WDOG disable */
143
144    /* Disable snooping for Ethernet and VME accesses */
145
146    bvme_acr_addrctl = 0;
147}
148
149
150irqreturn_t bvme6000_abort_int (int irq, void *dev_id)
151{
152        unsigned long *new = (unsigned long *)vectors;
153        unsigned long *old = (unsigned long *)0xf8000000;
154
155        /* Wait for button release */
156        while (*(volatile unsigned char *)BVME_LOCAL_IRQ_STAT & BVME_ABORT_STATUS)
157                ;
158
159        *(new+4) = *(old+4);            /* Illegal instruction */
160        *(new+9) = *(old+9);            /* Trace */
161        *(new+47) = *(old+47);          /* Trap #15 */
162        *(new+0x1f) = *(old+0x1f);      /* ABORT switch */
163	return IRQ_HANDLED;
164}
165
166
167static irqreturn_t bvme6000_timer_int (int irq, void *dev_id)
168{
169    volatile RtcPtr_t rtc = (RtcPtr_t)BVME_RTC_BASE;
170    unsigned char msr = rtc->msr & 0xc0;
171
172    rtc->msr = msr | 0x20;		/* Ack the interrupt */
173
174    return tick_handler(irq, dev_id);
175}
176
177/*
178 * Set up the RTC timer 1 to mode 2, so T1 output toggles every 5ms
179 * (40000 x 125ns).  It will interrupt every 10ms, when T1 goes low.
180 * So, when reading the elapsed time, you should read timer1,
181 * subtract it from 39999, and then add 40000 if T1 is high.
182 * That gives you the number of 125ns ticks in to the 10ms period,
183 * so divide by 8 to get the microsecond result.
184 */
185
186void bvme6000_sched_init (irq_handler_t timer_routine)
187{
188    volatile RtcPtr_t rtc = (RtcPtr_t)BVME_RTC_BASE;
189    unsigned char msr = rtc->msr & 0xc0;
190
191    rtc->msr = 0;	/* Ensure timer registers accessible */
192
193    tick_handler = timer_routine;
194    if (request_irq(BVME_IRQ_RTC, bvme6000_timer_int, 0,
195				"timer", bvme6000_timer_int))
196	panic ("Couldn't register timer int");
197
198    rtc->t1cr_omr = 0x04;	/* Mode 2, ext clk */
199    rtc->t1msb = 39999 >> 8;
200    rtc->t1lsb = 39999 & 0xff;
201    rtc->irr_icr1 &= 0xef;	/* Route timer 1 to INTR pin */
202    rtc->msr = 0x40;		/* Access int.cntrl, etc */
203    rtc->pfr_icr0 = 0x80;	/* Just timer 1 ints enabled */
204    rtc->irr_icr1 = 0;
205    rtc->t1cr_omr = 0x0a;	/* INTR+T1 active lo, push-pull */
206    rtc->t0cr_rtmr &= 0xdf;	/* Stop timers in standby */
207    rtc->msr = 0;		/* Access timer 1 control */
208    rtc->t1cr_omr = 0x05;	/* Mode 2, ext clk, GO */
209
210    rtc->msr = msr;
211
212    if (request_irq(BVME_IRQ_ABORT, bvme6000_abort_int, 0,
213				"abort", bvme6000_abort_int))
214	panic ("Couldn't register abort int");
215}
216
217
218/* This is always executed with interrupts disabled.  */
219
220/*
221 * NOTE:  Don't accept any readings within 5us of rollover, as
222 * the T1INT bit may be a little slow getting set.  There is also
223 * a fault in the chip, meaning that reads may produce invalid
224 * results...
225 */
226
227unsigned long bvme6000_gettimeoffset (void)
228{
229    volatile RtcPtr_t rtc = (RtcPtr_t)BVME_RTC_BASE;
230    volatile PitRegsPtr pit = (PitRegsPtr)BVME_PIT_BASE;
231    unsigned char msr = rtc->msr & 0xc0;
232    unsigned char t1int, t1op;
233    unsigned long v = 800000, ov;
234
235    rtc->msr = 0;	/* Ensure timer registers accessible */
236
237    do {
238	ov = v;
239	t1int = rtc->msr & 0x20;
240	t1op  = pit->pcdr & 0x04;
241	rtc->t1cr_omr |= 0x40;		/* Latch timer1 */
242	v = rtc->t1msb << 8;		/* Read timer1 */
243	v |= rtc->t1lsb;		/* Read timer1 */
244    } while (t1int != (rtc->msr & 0x20) ||
245		t1op != (pit->pcdr & 0x04) ||
246			abs(ov-v) > 80 ||
247				v > 39960);
248
249    v = 39999 - v;
250    if (!t1op)				/* If in second half cycle.. */
251	v += 40000;
252    v /= 8;				/* Convert ticks to microseconds */
253    if (t1int)
254	v += 10000;			/* Int pending, + 10ms */
255    rtc->msr = msr;
256
257    return v;
258}
259
260static unsigned char bcd2bin (unsigned char b)
261{
262	return ((b>>4)*10 + (b&15));
263}
264
265static unsigned char bin2bcd (unsigned char b)
266{
267	return (((b/10)*16) + (b%10));
268}
269
270
271/*
272 * Looks like op is non-zero for setting the clock, and zero for
273 * reading the clock.
274 *
275 *  struct hwclk_time {
276 *         unsigned        sec;       0..59
277 *         unsigned        min;       0..59
278 *         unsigned        hour;      0..23
279 *         unsigned        day;       1..31
280 *         unsigned        mon;       0..11
281 *         unsigned        year;      00...
282 *         int             wday;      0..6, 0 is Sunday, -1 means unknown/don't set
283 * };
284 */
285
286int bvme6000_hwclk(int op, struct rtc_time *t)
287{
288	volatile RtcPtr_t rtc = (RtcPtr_t)BVME_RTC_BASE;
289	unsigned char msr = rtc->msr & 0xc0;
290
291	rtc->msr = 0x40;	/* Ensure clock and real-time-mode-register
292				 * are accessible */
293	if (op)
294	{	/* Write.... */
295		rtc->t0cr_rtmr = t->tm_year%4;
296		rtc->bcd_tenms = 0;
297		rtc->bcd_sec = bin2bcd(t->tm_sec);
298		rtc->bcd_min = bin2bcd(t->tm_min);
299		rtc->bcd_hr  = bin2bcd(t->tm_hour);
300		rtc->bcd_dom = bin2bcd(t->tm_mday);
301		rtc->bcd_mth = bin2bcd(t->tm_mon + 1);
302		rtc->bcd_year = bin2bcd(t->tm_year%100);
303		if (t->tm_wday >= 0)
304			rtc->bcd_dow = bin2bcd(t->tm_wday+1);
305		rtc->t0cr_rtmr = t->tm_year%4 | 0x08;
306	}
307	else
308	{	/* Read....  */
309		do {
310			t->tm_sec  = bcd2bin(rtc->bcd_sec);
311			t->tm_min  = bcd2bin(rtc->bcd_min);
312			t->tm_hour = bcd2bin(rtc->bcd_hr);
313			t->tm_mday = bcd2bin(rtc->bcd_dom);
314			t->tm_mon  = bcd2bin(rtc->bcd_mth)-1;
315			t->tm_year = bcd2bin(rtc->bcd_year);
316			if (t->tm_year < 70)
317				t->tm_year += 100;
318			t->tm_wday = bcd2bin(rtc->bcd_dow)-1;
319		} while (t->tm_sec != bcd2bin(rtc->bcd_sec));
320	}
321
322	rtc->msr = msr;
323
324	return 0;
325}
326
327/*
328 * Set the minutes and seconds from seconds value 'nowtime'.  Fail if
329 * clock is out by > 30 minutes.  Logic lifted from atari code.
330 * Algorithm is to wait for the 10ms register to change, and then to
331 * wait a short while, and then set it.
332 */
333
334int bvme6000_set_clock_mmss (unsigned long nowtime)
335{
336	int retval = 0;
337	short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
338	unsigned char rtc_minutes, rtc_tenms;
339	volatile RtcPtr_t rtc = (RtcPtr_t)BVME_RTC_BASE;
340	unsigned char msr = rtc->msr & 0xc0;
341	unsigned long flags;
342	volatile int i;
343
344	rtc->msr = 0;		/* Ensure clock accessible */
345	rtc_minutes = bcd2bin (rtc->bcd_min);
346
347	if ((rtc_minutes < real_minutes
348		? real_minutes - rtc_minutes
349			: rtc_minutes - real_minutes) < 30)
350	{
351		local_irq_save(flags);
352		rtc_tenms = rtc->bcd_tenms;
353		while (rtc_tenms == rtc->bcd_tenms)
354			;
355		for (i = 0; i < 1000; i++)
356			;
357		rtc->bcd_min = bin2bcd(real_minutes);
358		rtc->bcd_sec = bin2bcd(real_seconds);
359		local_irq_restore(flags);
360	}
361	else
362		retval = -1;
363
364	rtc->msr = msr;
365
366	return retval;
367}
368