GNU Linux-libre 4.9.317-gnu1
[releases.git] / arch / m68k / mac / iop.c
1 /*
2  * I/O Processor (IOP) management
3  * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org)
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 and this list of conditions.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice and this list of conditions in the documentation and/or other
12  *    materials provided with the distribution.
13  */
14
15 /*
16  * The IOP chips are used in the IIfx and some Quadras (900, 950) to manage
17  * serial and ADB. They are actually a 6502 processor and some glue logic.
18  *
19  * 990429 (jmt) - Initial implementation, just enough to knock the SCC IOP
20  *                into compatible mode so nobody has to fiddle with the
21  *                Serial Switch control panel anymore.
22  * 990603 (jmt) - Added code to grab the correct ISM IOP interrupt for OSS
23  *                and non-OSS machines (at least I hope it's correct on a
24  *                non-OSS machine -- someone with a Q900 or Q950 needs to
25  *                check this.)
26  * 990605 (jmt) - Rearranged things a bit wrt IOP detection; iop_present is
27  *                gone, IOP base addresses are now in an array and the
28  *                globally-visible functions take an IOP number instead of an
29  *                an actual base address.
30  * 990610 (jmt) - Finished the message passing framework and it seems to work.
31  *                Sending _definitely_ works; my adb-bus.c mods can send
32  *                messages and receive the MSG_COMPLETED status back from the
33  *                IOP. The trick now is figuring out the message formats.
34  * 990611 (jmt) - More cleanups. Fixed problem where unclaimed messages on a
35  *                receive channel were never properly acknowledged. Bracketed
36  *                the remaining debug printk's with #ifdef's and disabled
37  *                debugging. I can now type on the console.
38  * 990612 (jmt) - Copyright notice added. Reworked the way replies are handled.
39  *                It turns out that replies are placed back in the send buffer
40  *                for that channel; messages on the receive channels are always
41  *                unsolicited messages from the IOP (and our replies to them
42  *                should go back in the receive channel.) Also added tracking
43  *                of device names to the listener functions ala the interrupt
44  *                handlers.
45  * 990729 (jmt) - Added passing of pt_regs structure to IOP handlers. This is
46  *                used by the new unified ADB driver.
47  *
48  * TODO:
49  *
50  * o Something should be periodically checking iop_alive() to make sure the
51  *   IOP hasn't died.
52  * o Some of the IOP manager routines need better error checking and
53  *   return codes. Nothing major, just prettying up.
54  */
55
56 /*
57  * -----------------------
58  * IOP Message Passing 101
59  * -----------------------
60  *
61  * The host talks to the IOPs using a rather simple message-passing scheme via
62  * a shared memory area in the IOP RAM. Each IOP has seven "channels"; each
63  * channel is connected to a specific software driver on the IOP. For example
64  * on the SCC IOP there is one channel for each serial port. Each channel has
65  * an incoming and and outgoing message queue with a depth of one.
66  *
67  * A message is 32 bytes plus a state byte for the channel (MSG_IDLE, MSG_NEW,
68  * MSG_RCVD, MSG_COMPLETE). To send a message you copy the message into the
69  * buffer, set the state to MSG_NEW and signal the IOP by setting the IRQ flag
70  * in the IOP control to 1. The IOP will move the state to MSG_RCVD when it
71  * receives the message and then to MSG_COMPLETE when the message processing
72  * has completed. It is the host's responsibility at that point to read the
73  * reply back out of the send channel buffer and reset the channel state back
74  * to MSG_IDLE.
75  *
76  * To receive message from the IOP the same procedure is used except the roles
77  * are reversed. That is, the IOP puts message in the channel with a state of
78  * MSG_NEW, and the host receives the message and move its state to MSG_RCVD
79  * and then to MSG_COMPLETE when processing is completed and the reply (if any)
80  * has been placed back in the receive channel. The IOP will then reset the
81  * channel state to MSG_IDLE.
82  *
83  * Two sets of host interrupts are provided, INT0 and INT1. Both appear on one
84  * interrupt level; they are distinguished by a pair of bits in the IOP status
85  * register. The IOP will raise INT0 when one or more messages in the send
86  * channels have gone to the MSG_COMPLETE state and it will raise INT1 when one
87  * or more messages on the receive channels have gone to the MSG_NEW state.
88  *
89  * Since each channel handles only one message we have to implement a small
90  * interrupt-driven queue on our end. Messages to be sent are placed on the
91  * queue for sending and contain a pointer to an optional callback function.
92  * The handler for a message is called when the message state goes to
93  * MSG_COMPLETE.
94  *
95  * For receiving message we maintain a list of handler functions to call when
96  * a message is received on that IOP/channel combination. The handlers are
97  * called much like an interrupt handler and are passed a copy of the message
98  * from the IOP. The message state will be in MSG_RCVD while the handler runs;
99  * it is the handler's responsibility to call iop_complete_message() when
100  * finished; this function moves the message state to MSG_COMPLETE and signals
101  * the IOP. This two-step process is provided to allow the handler to defer
102  * message processing to a bottom-half handler if the processing will take
103  * a significant amount of time (handlers are called at interrupt time so they
104  * should execute quickly.)
105  */
106
107 #include <linux/types.h>
108 #include <linux/kernel.h>
109 #include <linux/mm.h>
110 #include <linux/delay.h>
111 #include <linux/init.h>
112 #include <linux/interrupt.h>
113
114 #include <asm/macintosh.h>
115 #include <asm/macints.h>
116 #include <asm/mac_iop.h>
117
118 /*#define DEBUG_IOP*/
119
120 /* Non-zero if the IOPs are present */
121
122 int iop_scc_present, iop_ism_present;
123
124 /* structure for tracking channel listeners */
125
126 struct listener {
127         const char *devname;
128         void (*handler)(struct iop_msg *);
129 };
130
131 /*
132  * IOP structures for the two IOPs
133  *
134  * The SCC IOP controls both serial ports (A and B) as its two functions.
135  * The ISM IOP controls the SWIM (floppy drive) and ADB.
136  */
137
138 static volatile struct mac_iop *iop_base[NUM_IOPS];
139
140 /*
141  * IOP message queues
142  */
143
144 static struct iop_msg iop_msg_pool[NUM_IOP_MSGS];
145 static struct iop_msg *iop_send_queue[NUM_IOPS][NUM_IOP_CHAN];
146 static struct listener iop_listeners[NUM_IOPS][NUM_IOP_CHAN];
147
148 irqreturn_t iop_ism_irq(int, void *);
149
150 /*
151  * Private access functions
152  */
153
154 static __inline__ void iop_loadaddr(volatile struct mac_iop *iop, __u16 addr)
155 {
156         iop->ram_addr_lo = addr;
157         iop->ram_addr_hi = addr >> 8;
158 }
159
160 static __inline__ __u8 iop_readb(volatile struct mac_iop *iop, __u16 addr)
161 {
162         iop->ram_addr_lo = addr;
163         iop->ram_addr_hi = addr >> 8;
164         return iop->ram_data;
165 }
166
167 static __inline__ void iop_writeb(volatile struct mac_iop *iop, __u16 addr, __u8 data)
168 {
169         iop->ram_addr_lo = addr;
170         iop->ram_addr_hi = addr >> 8;
171         iop->ram_data = data;
172 }
173
174 static __inline__ void iop_stop(volatile struct mac_iop *iop)
175 {
176         iop->status_ctrl = IOP_AUTOINC;
177 }
178
179 static __inline__ void iop_start(volatile struct mac_iop *iop)
180 {
181         iop->status_ctrl = IOP_RUN | IOP_AUTOINC;
182 }
183
184 static __inline__ void iop_interrupt(volatile struct mac_iop *iop)
185 {
186         iop->status_ctrl = IOP_IRQ | IOP_RUN | IOP_AUTOINC;
187 }
188
189 static int iop_alive(volatile struct mac_iop *iop)
190 {
191         int retval;
192
193         retval = (iop_readb(iop, IOP_ADDR_ALIVE) == 0xFF);
194         iop_writeb(iop, IOP_ADDR_ALIVE, 0);
195         return retval;
196 }
197
198 static struct iop_msg *iop_alloc_msg(void)
199 {
200         int i;
201         unsigned long flags;
202
203         local_irq_save(flags);
204
205         for (i = 0 ; i < NUM_IOP_MSGS ; i++) {
206                 if (iop_msg_pool[i].status == IOP_MSGSTATUS_UNUSED) {
207                         iop_msg_pool[i].status = IOP_MSGSTATUS_WAITING;
208                         local_irq_restore(flags);
209                         return &iop_msg_pool[i];
210                 }
211         }
212
213         local_irq_restore(flags);
214         return NULL;
215 }
216
217 static void iop_free_msg(struct iop_msg *msg)
218 {
219         msg->status = IOP_MSGSTATUS_UNUSED;
220 }
221
222 /*
223  * This is called by the startup code before anything else. Its purpose
224  * is to find and initialize the IOPs early in the boot sequence, so that
225  * the serial IOP can be placed into bypass mode _before_ we try to
226  * initialize the serial console.
227  */
228
229 void __init iop_preinit(void)
230 {
231         if (macintosh_config->scc_type == MAC_SCC_IOP) {
232                 if (macintosh_config->ident == MAC_MODEL_IIFX) {
233                         iop_base[IOP_NUM_SCC] = (struct mac_iop *) SCC_IOP_BASE_IIFX;
234                 } else {
235                         iop_base[IOP_NUM_SCC] = (struct mac_iop *) SCC_IOP_BASE_QUADRA;
236                 }
237                 iop_scc_present = 1;
238         } else {
239                 iop_base[IOP_NUM_SCC] = NULL;
240                 iop_scc_present = 0;
241         }
242         if (macintosh_config->adb_type == MAC_ADB_IOP) {
243                 if (macintosh_config->ident == MAC_MODEL_IIFX) {
244                         iop_base[IOP_NUM_ISM] = (struct mac_iop *) ISM_IOP_BASE_IIFX;
245                 } else {
246                         iop_base[IOP_NUM_ISM] = (struct mac_iop *) ISM_IOP_BASE_QUADRA;
247                 }
248                 iop_stop(iop_base[IOP_NUM_ISM]);
249                 iop_ism_present = 1;
250         } else {
251                 iop_base[IOP_NUM_ISM] = NULL;
252                 iop_ism_present = 0;
253         }
254 }
255
256 /*
257  * Initialize the IOPs, if present.
258  */
259
260 void __init iop_init(void)
261 {
262         int i;
263
264         if (iop_scc_present) {
265                 printk("IOP: detected SCC IOP at %p\n", iop_base[IOP_NUM_SCC]);
266         }
267         if (iop_ism_present) {
268                 printk("IOP: detected ISM IOP at %p\n", iop_base[IOP_NUM_ISM]);
269                 iop_start(iop_base[IOP_NUM_ISM]);
270                 iop_alive(iop_base[IOP_NUM_ISM]); /* clears the alive flag */
271         }
272
273         /* Make the whole pool available and empty the queues */
274
275         for (i = 0 ; i < NUM_IOP_MSGS ; i++) {
276                 iop_msg_pool[i].status = IOP_MSGSTATUS_UNUSED;
277         }
278
279         for (i = 0 ; i < NUM_IOP_CHAN ; i++) {
280                 iop_send_queue[IOP_NUM_SCC][i] = NULL;
281                 iop_send_queue[IOP_NUM_ISM][i] = NULL;
282                 iop_listeners[IOP_NUM_SCC][i].devname = NULL;
283                 iop_listeners[IOP_NUM_SCC][i].handler = NULL;
284                 iop_listeners[IOP_NUM_ISM][i].devname = NULL;
285                 iop_listeners[IOP_NUM_ISM][i].handler = NULL;
286         }
287 }
288
289 /*
290  * Register the interrupt handler for the IOPs.
291  * TODO: might be wrong for non-OSS machines. Anyone?
292  */
293
294 void __init iop_register_interrupts(void)
295 {
296         if (iop_ism_present) {
297                 if (macintosh_config->ident == MAC_MODEL_IIFX) {
298                         if (request_irq(IRQ_MAC_ADB, iop_ism_irq, 0,
299                                         "ISM IOP", (void *)IOP_NUM_ISM))
300                                 pr_err("Couldn't register ISM IOP interrupt\n");
301                 } else {
302                         if (request_irq(IRQ_VIA2_0, iop_ism_irq, 0, "ISM IOP",
303                                         (void *)IOP_NUM_ISM))
304                                 pr_err("Couldn't register ISM IOP interrupt\n");
305                 }
306                 if (!iop_alive(iop_base[IOP_NUM_ISM])) {
307                         printk("IOP: oh my god, they killed the ISM IOP!\n");
308                 } else {
309                         printk("IOP: the ISM IOP seems to be alive.\n");
310                 }
311         }
312 }
313
314 /*
315  * Register or unregister a listener for a specific IOP and channel
316  *
317  * If the handler pointer is NULL the current listener (if any) is
318  * unregistered. Otherwise the new listener is registered provided
319  * there is no existing listener registered.
320  */
321
322 int iop_listen(uint iop_num, uint chan,
323                 void (*handler)(struct iop_msg *),
324                 const char *devname)
325 {
326         if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;
327         if (chan >= NUM_IOP_CHAN) return -EINVAL;
328         if (iop_listeners[iop_num][chan].handler && handler) return -EINVAL;
329         iop_listeners[iop_num][chan].devname = devname;
330         iop_listeners[iop_num][chan].handler = handler;
331         return 0;
332 }
333
334 /*
335  * Complete reception of a message, which just means copying the reply
336  * into the buffer, setting the channel state to MSG_COMPLETE and
337  * notifying the IOP.
338  */
339
340 void iop_complete_message(struct iop_msg *msg)
341 {
342         int iop_num = msg->iop_num;
343         int chan = msg->channel;
344         int i,offset;
345
346 #ifdef DEBUG_IOP
347         printk("iop_complete(%p): iop %d chan %d\n", msg, msg->iop_num, msg->channel);
348 #endif
349
350         offset = IOP_ADDR_RECV_MSG + (msg->channel * IOP_MSG_LEN);
351
352         for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {
353                 iop_writeb(iop_base[iop_num], offset, msg->reply[i]);
354         }
355
356         iop_writeb(iop_base[iop_num],
357                    IOP_ADDR_RECV_STATE + chan, IOP_MSG_COMPLETE);
358         iop_interrupt(iop_base[msg->iop_num]);
359
360         iop_free_msg(msg);
361 }
362
363 /*
364  * Actually put a message into a send channel buffer
365  */
366
367 static void iop_do_send(struct iop_msg *msg)
368 {
369         volatile struct mac_iop *iop = iop_base[msg->iop_num];
370         int i,offset;
371
372         offset = IOP_ADDR_SEND_MSG + (msg->channel * IOP_MSG_LEN);
373
374         for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {
375                 iop_writeb(iop, offset, msg->message[i]);
376         }
377
378         iop_writeb(iop, IOP_ADDR_SEND_STATE + msg->channel, IOP_MSG_NEW);
379
380         iop_interrupt(iop);
381 }
382
383 /*
384  * Handle sending a message on a channel that
385  * has gone into the IOP_MSG_COMPLETE state.
386  */
387
388 static void iop_handle_send(uint iop_num, uint chan)
389 {
390         volatile struct mac_iop *iop = iop_base[iop_num];
391         struct iop_msg *msg,*msg2;
392         int i,offset;
393
394 #ifdef DEBUG_IOP
395         printk("iop_handle_send: iop %d channel %d\n", iop_num, chan);
396 #endif
397
398         iop_writeb(iop, IOP_ADDR_SEND_STATE + chan, IOP_MSG_IDLE);
399
400         if (!(msg = iop_send_queue[iop_num][chan])) return;
401
402         msg->status = IOP_MSGSTATUS_COMPLETE;
403         offset = IOP_ADDR_SEND_MSG + (chan * IOP_MSG_LEN);
404         for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {
405                 msg->reply[i] = iop_readb(iop, offset);
406         }
407         if (msg->handler) (*msg->handler)(msg);
408         msg2 = msg;
409         msg = msg->next;
410         iop_free_msg(msg2);
411
412         iop_send_queue[iop_num][chan] = msg;
413         if (msg && iop_readb(iop, IOP_ADDR_SEND_STATE + chan) == IOP_MSG_IDLE)
414                 iop_do_send(msg);
415 }
416
417 /*
418  * Handle reception of a message on a channel that has
419  * gone into the IOP_MSG_NEW state.
420  */
421
422 static void iop_handle_recv(uint iop_num, uint chan)
423 {
424         volatile struct mac_iop *iop = iop_base[iop_num];
425         int i,offset;
426         struct iop_msg *msg;
427
428 #ifdef DEBUG_IOP
429         printk("iop_handle_recv: iop %d channel %d\n", iop_num, chan);
430 #endif
431
432         msg = iop_alloc_msg();
433         msg->iop_num = iop_num;
434         msg->channel = chan;
435         msg->status = IOP_MSGSTATUS_UNSOL;
436         msg->handler = iop_listeners[iop_num][chan].handler;
437
438         offset = IOP_ADDR_RECV_MSG + (chan * IOP_MSG_LEN);
439
440         for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {
441                 msg->message[i] = iop_readb(iop, offset);
442         }
443
444         iop_writeb(iop, IOP_ADDR_RECV_STATE + chan, IOP_MSG_RCVD);
445
446         /* If there is a listener, call it now. Otherwise complete */
447         /* the message ourselves to avoid possible stalls.         */
448
449         if (msg->handler) {
450                 (*msg->handler)(msg);
451         } else {
452 #ifdef DEBUG_IOP
453                 printk("iop_handle_recv: unclaimed message on iop %d channel %d\n", iop_num, chan);
454                 printk("iop_handle_recv:");
455                 for (i = 0 ; i < IOP_MSG_LEN ; i++) {
456                         printk(" %02X", (uint) msg->message[i]);
457                 }
458                 printk("\n");
459 #endif
460                 iop_complete_message(msg);
461         }
462 }
463
464 /*
465  * Send a message
466  *
467  * The message is placed at the end of the send queue. Afterwards if the
468  * channel is idle we force an immediate send of the next message in the
469  * queue.
470  */
471
472 int iop_send_message(uint iop_num, uint chan, void *privdata,
473                       uint msg_len, __u8 *msg_data,
474                       void (*handler)(struct iop_msg *))
475 {
476         struct iop_msg *msg, *q;
477
478         if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;
479         if (chan >= NUM_IOP_CHAN) return -EINVAL;
480         if (msg_len > IOP_MSG_LEN) return -EINVAL;
481
482         msg = iop_alloc_msg();
483         if (!msg) return -ENOMEM;
484
485         msg->next = NULL;
486         msg->status = IOP_MSGSTATUS_WAITING;
487         msg->iop_num = iop_num;
488         msg->channel = chan;
489         msg->caller_priv = privdata;
490         memcpy(msg->message, msg_data, msg_len);
491         msg->handler = handler;
492
493         if (!(q = iop_send_queue[iop_num][chan])) {
494                 iop_send_queue[iop_num][chan] = msg;
495                 iop_do_send(msg);
496         } else {
497                 while (q->next) q = q->next;
498                 q->next = msg;
499         }
500
501         return 0;
502 }
503
504 /*
505  * Upload code to the shared RAM of an IOP.
506  */
507
508 void iop_upload_code(uint iop_num, __u8 *code_start,
509                      uint code_len, __u16 shared_ram_start)
510 {
511         if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;
512
513         iop_loadaddr(iop_base[iop_num], shared_ram_start);
514
515         while (code_len--) {
516                 iop_base[iop_num]->ram_data = *code_start++;
517         }
518 }
519
520 /*
521  * Download code from the shared RAM of an IOP.
522  */
523
524 void iop_download_code(uint iop_num, __u8 *code_start,
525                        uint code_len, __u16 shared_ram_start)
526 {
527         if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;
528
529         iop_loadaddr(iop_base[iop_num], shared_ram_start);
530
531         while (code_len--) {
532                 *code_start++ = iop_base[iop_num]->ram_data;
533         }
534 }
535
536 /*
537  * Compare the code in the shared RAM of an IOP with a copy in system memory
538  * and return 0 on match or the first nonmatching system memory address on
539  * failure.
540  */
541
542 __u8 *iop_compare_code(uint iop_num, __u8 *code_start,
543                        uint code_len, __u16 shared_ram_start)
544 {
545         if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return code_start;
546
547         iop_loadaddr(iop_base[iop_num], shared_ram_start);
548
549         while (code_len--) {
550                 if (*code_start != iop_base[iop_num]->ram_data) {
551                         return code_start;
552                 }
553                 code_start++;
554         }
555         return (__u8 *) 0;
556 }
557
558 /*
559  * Handle an ISM IOP interrupt
560  */
561
562 irqreturn_t iop_ism_irq(int irq, void *dev_id)
563 {
564         uint iop_num = (uint) dev_id;
565         volatile struct mac_iop *iop = iop_base[iop_num];
566         int i,state;
567
568 #ifdef DEBUG_IOP
569         printk("iop_ism_irq: status = %02X\n", (uint) iop->status_ctrl);
570 #endif
571
572         /* INT0 indicates a state change on an outgoing message channel */
573
574         if (iop->status_ctrl & IOP_INT0) {
575                 iop->status_ctrl = IOP_INT0 | IOP_RUN | IOP_AUTOINC;
576 #ifdef DEBUG_IOP
577                 printk("iop_ism_irq: new status = %02X, send states",
578                         (uint) iop->status_ctrl);
579 #endif
580                 for (i = 0 ; i < NUM_IOP_CHAN  ; i++) {
581                         state = iop_readb(iop, IOP_ADDR_SEND_STATE + i);
582 #ifdef DEBUG_IOP
583                         printk(" %02X", state);
584 #endif
585                         if (state == IOP_MSG_COMPLETE) {
586                                 iop_handle_send(iop_num, i);
587                         }
588                 }
589 #ifdef DEBUG_IOP
590                 printk("\n");
591 #endif
592         }
593
594         if (iop->status_ctrl & IOP_INT1) {      /* INT1 for incoming msgs */
595                 iop->status_ctrl = IOP_INT1 | IOP_RUN | IOP_AUTOINC;
596 #ifdef DEBUG_IOP
597                 printk("iop_ism_irq: new status = %02X, recv states",
598                         (uint) iop->status_ctrl);
599 #endif
600                 for (i = 0 ; i < NUM_IOP_CHAN ; i++) {
601                         state = iop_readb(iop, IOP_ADDR_RECV_STATE + i);
602 #ifdef DEBUG_IOP
603                         printk(" %02X", state);
604 #endif
605                         if (state == IOP_MSG_NEW) {
606                                 iop_handle_recv(iop_num, i);
607                         }
608                 }
609 #ifdef DEBUG_IOP
610                 printk("\n");
611 #endif
612         }
613         return IRQ_HANDLED;
614 }