1/*
2 *	OSS handling
3 *	Written by Joshua M. Thompson (funaho@jurai.org)
4 *
5 *
6 *	This chip is used in the IIfx in place of VIA #2. It acts like a fancy
7 *	VIA chip with prorammable interrupt levels.
8 *
9 * 990502 (jmt) - Major rewrite for new interrupt architecture as well as some
10 *		  recent insights into OSS operational details.
11 * 990610 (jmt) - Now taking fulll advantage of the OSS. Interrupts are mapped
12 *		  to mostly match the A/UX interrupt scheme supported on the
13 *		  VIA side. Also added support for enabling the ISM irq again
14 *		  since we now have a functional IOP manager.
15 */
16
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/delay.h>
21#include <linux/init.h>
22
23#include <asm/bootinfo.h>
24#include <asm/machw.h>
25#include <asm/macintosh.h>
26#include <asm/macints.h>
27#include <asm/mac_via.h>
28#include <asm/mac_oss.h>
29
30int oss_present;
31volatile struct mac_oss *oss;
32
33irqreturn_t oss_irq(int, void *);
34irqreturn_t oss_nubus_irq(int, void *);
35
36extern irqreturn_t via1_irq(int, void *);
37extern irqreturn_t mac_scc_dispatch(int, void *);
38
39/*
40 * Initialize the OSS
41 *
42 * The OSS "detection" code is actually in via_init() which is always called
43 * before us. Thus we can count on oss_present being valid on entry.
44 */
45
46void __init oss_init(void)
47{
48	int i;
49
50	if (!oss_present) return;
51
52	oss = (struct mac_oss *) OSS_BASE;
53
54	/* Disable all interrupts. Unlike a VIA it looks like we    */
55	/* do this by setting the source's interrupt level to zero. */
56
57	for (i = 0; i <= OSS_NUM_SOURCES; i++) {
58		oss->irq_level[i] = OSS_IRQLEV_DISABLED;
59	}
60	/* If we disable VIA1 here, we never really handle it... */
61	oss->irq_level[OSS_VIA1] = OSS_IRQLEV_VIA1;
62}
63
64/*
65 * Register the OSS and NuBus interrupt dispatchers.
66 */
67
68void __init oss_register_interrupts(void)
69{
70	request_irq(OSS_IRQLEV_SCSI, oss_irq, IRQ_FLG_LOCK,
71			"scsi", (void *) oss);
72	request_irq(OSS_IRQLEV_IOPSCC, mac_scc_dispatch, IRQ_FLG_LOCK,
73			"scc", mac_scc_dispatch);
74	request_irq(OSS_IRQLEV_NUBUS, oss_nubus_irq, IRQ_FLG_LOCK,
75			"nubus", (void *) oss);
76	request_irq(OSS_IRQLEV_SOUND, oss_irq, IRQ_FLG_LOCK,
77			"sound", (void *) oss);
78	request_irq(OSS_IRQLEV_VIA1, via1_irq, IRQ_FLG_LOCK,
79			"via1", (void *) via1);
80}
81
82/*
83 * Initialize OSS for Nubus access
84 */
85
86void __init oss_nubus_init(void)
87{
88}
89
90/*
91 * Handle miscellaneous OSS interrupts. Right now that's just sound
92 * and SCSI; everything else is routed to its own autovector IRQ.
93 */
94
95irqreturn_t oss_irq(int irq, void *dev_id)
96{
97	int events;
98
99	events = oss->irq_pending & (OSS_IP_SOUND|OSS_IP_SCSI);
100	if (!events)
101		return IRQ_NONE;
102
103#ifdef DEBUG_IRQS
104	if ((console_loglevel == 10) && !(events & OSS_IP_SCSI)) {
105		printk("oss_irq: irq %d events = 0x%04X\n", irq,
106			(int) oss->irq_pending);
107	}
108#endif
109
110	if (events & OSS_IP_SOUND) {
111		oss->irq_pending &= ~OSS_IP_SOUND;
112	} else if (events & OSS_IP_SCSI) {
113		oss->irq_pending &= ~OSS_IP_SCSI;
114		m68k_handle_int(IRQ_MAC_SCSI);
115	} else {
116	}
117	return IRQ_HANDLED;
118}
119
120/*
121 * Nubus IRQ handler, OSS style
122 *
123 * Unlike the VIA/RBV this is on its own autovector interrupt level.
124 */
125
126irqreturn_t oss_nubus_irq(int irq, void *dev_id)
127{
128	int events, irq_bit, i;
129
130	events = oss->irq_pending & OSS_IP_NUBUS;
131	if (!events)
132		return IRQ_NONE;
133
134#ifdef DEBUG_NUBUS_INT
135	if (console_loglevel > 7) {
136		printk("oss_nubus_irq: events = 0x%04X\n", events);
137	}
138#endif
139	/* There are only six slots on the OSS, not seven */
140
141	i = 6;
142	irq_bit = 0x40;
143	do {
144		--i;
145		irq_bit >>= 1;
146		if (events & irq_bit) {
147			oss->irq_pending &= ~irq_bit;
148			m68k_handle_int(NUBUS_SOURCE_BASE + i);
149		}
150	} while(events & (irq_bit - 1));
151	return IRQ_HANDLED;
152}
153
154/*
155 * Enable an OSS interrupt
156 *
157 * It looks messy but it's rather straightforward. The switch() statement
158 * just maps the machspec interrupt numbers to the right OSS interrupt
159 * source (if the OSS handles that interrupt) and then sets the interrupt
160 * level for that source to nonzero, thus enabling the interrupt.
161 */
162
163void oss_irq_enable(int irq) {
164#ifdef DEBUG_IRQUSE
165	printk("oss_irq_enable(%d)\n", irq);
166#endif
167	switch(irq) {
168		case IRQ_SCC:
169		case IRQ_SCCA:
170		case IRQ_SCCB:
171			oss->irq_level[OSS_IOPSCC] = OSS_IRQLEV_IOPSCC;
172			break;
173		case IRQ_MAC_ADB:
174			oss->irq_level[OSS_IOPISM] = OSS_IRQLEV_IOPISM;
175			break;
176		case IRQ_MAC_SCSI:
177			oss->irq_level[OSS_SCSI] = OSS_IRQLEV_SCSI;
178			break;
179		case IRQ_NUBUS_9:
180		case IRQ_NUBUS_A:
181		case IRQ_NUBUS_B:
182		case IRQ_NUBUS_C:
183		case IRQ_NUBUS_D:
184		case IRQ_NUBUS_E:
185			irq -= NUBUS_SOURCE_BASE;
186			oss->irq_level[irq] = OSS_IRQLEV_NUBUS;
187			break;
188#ifdef DEBUG_IRQUSE
189		default:
190			printk("%s unknown irq %d\n",__FUNCTION__, irq);
191			break;
192#endif
193	}
194}
195
196/*
197 * Disable an OSS interrupt
198 *
199 * Same as above except we set the source's interrupt level to zero,
200 * to disable the interrupt.
201 */
202
203void oss_irq_disable(int irq) {
204#ifdef DEBUG_IRQUSE
205	printk("oss_irq_disable(%d)\n", irq);
206#endif
207	switch(irq) {
208		case IRQ_SCC:
209		case IRQ_SCCA:
210		case IRQ_SCCB:
211			oss->irq_level[OSS_IOPSCC] = OSS_IRQLEV_DISABLED;
212			break;
213		case IRQ_MAC_ADB:
214			oss->irq_level[OSS_IOPISM] = OSS_IRQLEV_DISABLED;
215			break;
216		case IRQ_MAC_SCSI:
217			oss->irq_level[OSS_SCSI] = OSS_IRQLEV_DISABLED;
218			break;
219		case IRQ_NUBUS_9:
220		case IRQ_NUBUS_A:
221		case IRQ_NUBUS_B:
222		case IRQ_NUBUS_C:
223		case IRQ_NUBUS_D:
224		case IRQ_NUBUS_E:
225			irq -= NUBUS_SOURCE_BASE;
226			oss->irq_level[irq] = OSS_IRQLEV_DISABLED;
227			break;
228#ifdef DEBUG_IRQUSE
229		default:
230			printk("%s unknown irq %d\n", __FUNCTION__, irq);
231			break;
232#endif
233	}
234}
235
236/*
237 * Clear an OSS interrupt
238 *
239 * Not sure if this works or not but it's the only method I could
240 * think of based on the contents of the mac_oss structure.
241 */
242
243void oss_irq_clear(int irq) {
244	switch(irq) {
245		case IRQ_SCC:
246		case IRQ_SCCA:
247		case IRQ_SCCB:
248			oss->irq_pending &= ~OSS_IP_IOPSCC;
249			break;
250		case IRQ_MAC_ADB:
251			oss->irq_pending &= ~OSS_IP_IOPISM;
252			break;
253		case IRQ_MAC_SCSI:
254			oss->irq_pending &= ~OSS_IP_SCSI;
255			break;
256		case IRQ_NUBUS_9:
257		case IRQ_NUBUS_A:
258		case IRQ_NUBUS_B:
259		case IRQ_NUBUS_C:
260		case IRQ_NUBUS_D:
261		case IRQ_NUBUS_E:
262			irq -= NUBUS_SOURCE_BASE;
263			oss->irq_pending &= ~(1 << irq);
264			break;
265	}
266}
267
268/*
269 * Check to see if a specific OSS interrupt is pending
270 */
271
272int oss_irq_pending(int irq)
273{
274	switch(irq) {
275		case IRQ_SCC:
276		case IRQ_SCCA:
277		case IRQ_SCCB:
278			return oss->irq_pending & OSS_IP_IOPSCC;
279			break;
280		case IRQ_MAC_ADB:
281			return oss->irq_pending & OSS_IP_IOPISM;
282			break;
283		case IRQ_MAC_SCSI:
284			return oss->irq_pending & OSS_IP_SCSI;
285			break;
286		case IRQ_NUBUS_9:
287		case IRQ_NUBUS_A:
288		case IRQ_NUBUS_B:
289		case IRQ_NUBUS_C:
290		case IRQ_NUBUS_D:
291		case IRQ_NUBUS_E:
292			irq -= NUBUS_SOURCE_BASE;
293			return oss->irq_pending & (1 << irq);
294			break;
295	}
296	return 0;
297}
298