[PATCH] m68k: introduce irq controller
[powerpc.git] / arch / m68k / q40 / config.c
1 /*
2  *  arch/m68k/q40/config.c
3  *
4  *  Copyright (C) 1999 Richard Zidlicky
5  *
6  * originally based on:
7  *
8  *  linux/bvme/config.c
9  *
10  * This file is subject to the terms and conditions of the GNU General Public
11  * License.  See the file README.legal in the main directory of this archive
12  * for more details.
13  */
14
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/tty.h>
20 #include <linux/console.h>
21 #include <linux/linkage.h>
22 #include <linux/init.h>
23 #include <linux/major.h>
24 #include <linux/serial_reg.h>
25 #include <linux/rtc.h>
26 #include <linux/vt_kern.h>
27
28 #include <asm/io.h>
29 #include <asm/rtc.h>
30 #include <asm/bootinfo.h>
31 #include <asm/system.h>
32 #include <asm/pgtable.h>
33 #include <asm/setup.h>
34 #include <asm/irq.h>
35 #include <asm/traps.h>
36 #include <asm/machdep.h>
37 #include <asm/q40_master.h>
38
39 extern irqreturn_t q40_process_int (int level, struct pt_regs *regs);
40 extern void q40_init_IRQ (void);
41 extern void q40_free_irq (unsigned int, void *);
42 extern int  show_q40_interrupts (struct seq_file *, void *);
43 extern void q40_enable_irq (unsigned int);
44 extern void q40_disable_irq (unsigned int);
45 static void q40_get_model(char *model);
46 static int  q40_get_hardware_list(char *buffer);
47 extern int  q40_request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *devname, void *dev_id);
48 extern void q40_sched_init(irqreturn_t (*handler)(int, void *, struct pt_regs *));
49
50 extern unsigned long q40_gettimeoffset (void);
51 extern int q40_hwclk (int, struct rtc_time *);
52 extern unsigned int q40_get_ss (void);
53 extern int q40_set_clock_mmss (unsigned long);
54 static int q40_get_rtc_pll(struct rtc_pll_info *pll);
55 static int q40_set_rtc_pll(struct rtc_pll_info *pll);
56 extern void q40_reset (void);
57 void q40_halt(void);
58 extern void q40_waitbut(void);
59 void q40_set_vectors (void);
60
61 extern void q40_mksound(unsigned int /*freq*/, unsigned int /*ticks*/ );
62
63 extern char m68k_debug_device[];
64 static void q40_mem_console_write(struct console *co, const char *b,
65                                     unsigned int count);
66
67 extern int ql_ticks;
68
69 static struct console q40_console_driver = {
70         .name =         "debug",
71         .flags =        CON_PRINTBUFFER,
72         .index =        -1,
73 };
74
75
76 /* early debugging function:*/
77 extern char *q40_mem_cptr; /*=(char *)0xff020000;*/
78 static int _cpleft;
79
80 static void q40_mem_console_write(struct console *co, const char *s,
81                                   unsigned int count)
82 {
83   char *p=(char *)s;
84
85   if (count<_cpleft)
86     while (count-- >0){
87       *q40_mem_cptr=*p++;
88       q40_mem_cptr+=4;
89       _cpleft--;
90     }
91 }
92 #if 0
93 void printq40(char *str)
94 {
95   int l=strlen(str);
96   char *p=q40_mem_cptr;
97
98   while (l-- >0 && _cpleft-- >0)
99     {
100       *p=*str++;
101       p+=4;
102     }
103   q40_mem_cptr=p;
104 }
105 #endif
106
107 static int halted=0;
108
109 #ifdef CONFIG_HEARTBEAT
110 static void q40_heartbeat(int on)
111 {
112   if (halted) return;
113
114   if (on)
115     Q40_LED_ON();
116   else
117     Q40_LED_OFF();
118 }
119 #endif
120
121 void q40_reset(void)
122 {
123         halted=1;
124         printk ("\n\n*******************************************\n"
125                 "Called q40_reset : press the RESET button!! \n"
126                 "*******************************************\n");
127         Q40_LED_ON();
128         while(1) ;
129 }
130 void q40_halt(void)
131 {
132         halted=1;
133         printk ("\n\n*******************\n"
134                     "  Called q40_halt\n"
135                     "*******************\n");
136         Q40_LED_ON();
137         while(1) ;
138 }
139
140 static void q40_get_model(char *model)
141 {
142     sprintf(model, "Q40");
143 }
144
145 /* No hardware options on Q40? */
146
147 static int q40_get_hardware_list(char *buffer)
148 {
149     *buffer = '\0';
150     return 0;
151 }
152
153 static unsigned int serports[]={0x3f8,0x2f8,0x3e8,0x2e8,0};
154 void q40_disable_irqs(void)
155 {
156   unsigned i,j;
157
158   j=0;
159   while((i=serports[j++])) outb(0,i+UART_IER);
160   master_outb(0,EXT_ENABLE_REG);
161   master_outb(0,KEY_IRQ_ENABLE_REG);
162 }
163
164 void __init config_q40(void)
165 {
166     mach_sched_init      = q40_sched_init;
167
168     mach_init_IRQ        = q40_init_IRQ;
169     mach_gettimeoffset   = q40_gettimeoffset;
170     mach_hwclk           = q40_hwclk;
171     mach_get_ss          = q40_get_ss;
172     mach_get_rtc_pll     = q40_get_rtc_pll;
173     mach_set_rtc_pll     = q40_set_rtc_pll;
174     mach_set_clock_mmss  = q40_set_clock_mmss;
175
176     mach_reset           = q40_reset;
177     mach_free_irq        = q40_free_irq;
178     mach_process_int     = q40_process_int;
179     mach_get_irq_list    = show_q40_interrupts;
180     mach_request_irq     = q40_request_irq;
181     enable_irq           = q40_enable_irq;
182     disable_irq          = q40_disable_irq;
183     mach_get_model       = q40_get_model;
184     mach_get_hardware_list = q40_get_hardware_list;
185
186 #if defined(CONFIG_INPUT_M68K_BEEP) || defined(CONFIG_INPUT_M68K_BEEP_MODULE)
187     mach_beep            = q40_mksound;
188 #endif
189 #ifdef CONFIG_HEARTBEAT
190     mach_heartbeat = q40_heartbeat;
191 #endif
192     mach_halt = q40_halt;
193
194     /* disable a few things that SMSQ might have left enabled */
195     q40_disable_irqs();
196
197     /* no DMA at all, but ide-scsi requires it.. make sure
198      * all physical RAM fits into the boundary - otherwise
199      * allocator may play costly and useless tricks */
200     mach_max_dma_address = 1024*1024*1024;
201
202     /* useful for early debugging stages - writes kernel messages into SRAM */
203     if (!strncmp( m68k_debug_device,"mem",3 ))
204       {
205         /*printk("using NVRAM debug, q40_mem_cptr=%p\n",q40_mem_cptr);*/
206         _cpleft=2000-((long)q40_mem_cptr-0xff020000)/4;
207         q40_console_driver.write = q40_mem_console_write;
208         register_console(&q40_console_driver);
209       }
210 }
211
212
213 int q40_parse_bootinfo(const struct bi_record *rec)
214 {
215   return 1;
216 }
217
218
219 static inline unsigned char bcd2bin (unsigned char b)
220 {
221         return ((b>>4)*10 + (b&15));
222 }
223
224 static inline unsigned char bin2bcd (unsigned char b)
225 {
226         return (((b/10)*16) + (b%10));
227 }
228
229
230 unsigned long q40_gettimeoffset (void)
231 {
232     return 5000*(ql_ticks!=0);
233 }
234
235
236 /*
237  * Looks like op is non-zero for setting the clock, and zero for
238  * reading the clock.
239  *
240  *  struct hwclk_time {
241  *         unsigned        sec;       0..59
242  *         unsigned        min;       0..59
243  *         unsigned        hour;      0..23
244  *         unsigned        day;       1..31
245  *         unsigned        mon;       0..11
246  *         unsigned        year;      00...
247  *         int             wday;      0..6, 0 is Sunday, -1 means unknown/don't set
248  * };
249  */
250
251 int q40_hwclk(int op, struct rtc_time *t)
252 {
253         if (op)
254         {       /* Write.... */
255                 Q40_RTC_CTRL |= Q40_RTC_WRITE;
256
257                 Q40_RTC_SECS = bin2bcd(t->tm_sec);
258                 Q40_RTC_MINS = bin2bcd(t->tm_min);
259                 Q40_RTC_HOUR = bin2bcd(t->tm_hour);
260                 Q40_RTC_DATE = bin2bcd(t->tm_mday);
261                 Q40_RTC_MNTH = bin2bcd(t->tm_mon + 1);
262                 Q40_RTC_YEAR = bin2bcd(t->tm_year%100);
263                 if (t->tm_wday >= 0)
264                         Q40_RTC_DOW = bin2bcd(t->tm_wday+1);
265
266                 Q40_RTC_CTRL &= ~(Q40_RTC_WRITE);
267         }
268         else
269         {       /* Read....  */
270           Q40_RTC_CTRL |= Q40_RTC_READ;
271
272           t->tm_year = bcd2bin (Q40_RTC_YEAR);
273           t->tm_mon  = bcd2bin (Q40_RTC_MNTH)-1;
274           t->tm_mday = bcd2bin (Q40_RTC_DATE);
275           t->tm_hour = bcd2bin (Q40_RTC_HOUR);
276           t->tm_min  = bcd2bin (Q40_RTC_MINS);
277           t->tm_sec  = bcd2bin (Q40_RTC_SECS);
278
279           Q40_RTC_CTRL &= ~(Q40_RTC_READ);
280
281           if (t->tm_year < 70)
282             t->tm_year += 100;
283           t->tm_wday = bcd2bin(Q40_RTC_DOW)-1;
284
285         }
286
287         return 0;
288 }
289
290 unsigned int q40_get_ss(void)
291 {
292         return bcd2bin(Q40_RTC_SECS);
293 }
294
295 /*
296  * Set the minutes and seconds from seconds value 'nowtime'.  Fail if
297  * clock is out by > 30 minutes.  Logic lifted from atari code.
298  */
299
300 int q40_set_clock_mmss (unsigned long nowtime)
301 {
302         int retval = 0;
303         short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
304
305         int rtc_minutes;
306
307
308         rtc_minutes = bcd2bin (Q40_RTC_MINS);
309
310         if ((rtc_minutes < real_minutes
311                 ? real_minutes - rtc_minutes
312                         : rtc_minutes - real_minutes) < 30)
313         {
314                 Q40_RTC_CTRL |= Q40_RTC_WRITE;
315                 Q40_RTC_MINS = bin2bcd(real_minutes);
316                 Q40_RTC_SECS = bin2bcd(real_seconds);
317                 Q40_RTC_CTRL &= ~(Q40_RTC_WRITE);
318         }
319         else
320                 retval = -1;
321
322
323         return retval;
324 }
325
326
327 /* get and set PLL calibration of RTC clock */
328 #define Q40_RTC_PLL_MASK ((1<<5)-1)
329 #define Q40_RTC_PLL_SIGN (1<<5)
330
331 static int q40_get_rtc_pll(struct rtc_pll_info *pll)
332 {
333         int tmp=Q40_RTC_CTRL;
334         pll->pll_value = tmp & Q40_RTC_PLL_MASK;
335         if (tmp & Q40_RTC_PLL_SIGN)
336                 pll->pll_value = -pll->pll_value;
337         pll->pll_max=31;
338         pll->pll_min=-31;
339         pll->pll_posmult=512;
340         pll->pll_negmult=256;
341         pll->pll_clock=125829120;
342         return 0;
343 }
344
345 static int q40_set_rtc_pll(struct rtc_pll_info *pll)
346 {
347         if (!pll->pll_ctrl){
348                 /* the docs are a bit unclear so I am doublesetting */
349                 /* RTC_WRITE here ... */
350                 int tmp = (pll->pll_value & 31) | (pll->pll_value<0 ? 32 : 0) |
351                           Q40_RTC_WRITE;
352                 Q40_RTC_CTRL |= Q40_RTC_WRITE;
353                 Q40_RTC_CTRL = tmp;
354                 Q40_RTC_CTRL &= ~(Q40_RTC_WRITE);
355                 return 0;
356         } else
357                 return -EINVAL;
358 }