/*1* I/O Processor (IOP) management2* Written and (C) 1999 by Joshua M. Thompson ([email protected])3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice and this list of conditions.9* 2. Redistributions in binary form must reproduce the above copyright10* notice and this list of conditions in the documentation and/or other11* materials provided with the distribution.12*/1314/*15* The IOP chips are used in the IIfx and some Quadras (900, 950) to manage16* serial and ADB. They are actually a 6502 processor and some glue logic.17*18* 990429 (jmt) - Initial implementation, just enough to knock the SCC IOP19* into compatible mode so nobody has to fiddle with the20* Serial Switch control panel anymore.21* 990603 (jmt) - Added code to grab the correct ISM IOP interrupt for OSS22* and non-OSS machines (at least I hope it's correct on a23* non-OSS machine -- someone with a Q900 or Q950 needs to24* check this.)25* 990605 (jmt) - Rearranged things a bit wrt IOP detection; iop_present is26* gone, IOP base addresses are now in an array and the27* globally-visible functions take an IOP number instead of an28* an actual base address.29* 990610 (jmt) - Finished the message passing framework and it seems to work.30* Sending _definitely_ works; my adb-bus.c mods can send31* messages and receive the MSG_COMPLETED status back from the32* IOP. The trick now is figuring out the message formats.33* 990611 (jmt) - More cleanups. Fixed problem where unclaimed messages on a34* receive channel were never properly acknowledged. Bracketed35* the remaining debug printk's with #ifdef's and disabled36* debugging. I can now type on the console.37* 990612 (jmt) - Copyright notice added. Reworked the way replies are handled.38* It turns out that replies are placed back in the send buffer39* for that channel; messages on the receive channels are always40* unsolicited messages from the IOP (and our replies to them41* should go back in the receive channel.) Also added tracking42* of device names to the listener functions ala the interrupt43* handlers.44* 990729 (jmt) - Added passing of pt_regs structure to IOP handlers. This is45* used by the new unified ADB driver.46*47* TODO:48*49* o Something should be periodically checking iop_alive() to make sure the50* IOP hasn't died.51* o Some of the IOP manager routines need better error checking and52* return codes. Nothing major, just prettying up.53*/5455/*56* -----------------------57* IOP Message Passing 10158* -----------------------59*60* The host talks to the IOPs using a rather simple message-passing scheme via61* a shared memory area in the IOP RAM. Each IOP has seven "channels"; each62* channel is conneced to a specific software driver on the IOP. For example63* on the SCC IOP there is one channel for each serial port. Each channel has64* an incoming and and outgoing message queue with a depth of one.65*66* A message is 32 bytes plus a state byte for the channel (MSG_IDLE, MSG_NEW,67* MSG_RCVD, MSG_COMPLETE). To send a message you copy the message into the68* buffer, set the state to MSG_NEW and signal the IOP by setting the IRQ flag69* in the IOP control to 1. The IOP will move the state to MSG_RCVD when it70* receives the message and then to MSG_COMPLETE when the message processing71* has completed. It is the host's responsibility at that point to read the72* reply back out of the send channel buffer and reset the channel state back73* to MSG_IDLE.74*75* To receive message from the IOP the same procedure is used except the roles76* are reversed. That is, the IOP puts message in the channel with a state of77* MSG_NEW, and the host receives the message and move its state to MSG_RCVD78* and then to MSG_COMPLETE when processing is completed and the reply (if any)79* has been placed back in the receive channel. The IOP will then reset the80* channel state to MSG_IDLE.81*82* Two sets of host interrupts are provided, INT0 and INT1. Both appear on one83* interrupt level; they are distinguished by a pair of bits in the IOP status84* register. The IOP will raise INT0 when one or more messages in the send85* channels have gone to the MSG_COMPLETE state and it will raise INT1 when one86* or more messages on the receive channels have gone to the MSG_NEW state.87*88* Since each channel handles only one message we have to implement a small89* interrupt-driven queue on our end. Messages to be sent are placed on the90* queue for sending and contain a pointer to an optional callback function.91* The handler for a message is called when the message state goes to92* MSG_COMPLETE.93*94* For receiving message we maintain a list of handler functions to call when95* a message is received on that IOP/channel combination. The handlers are96* called much like an interrupt handler and are passed a copy of the message97* from the IOP. The message state will be in MSG_RCVD while the handler runs;98* it is the handler's responsibility to call iop_complete_message() when99* finished; this function moves the message state to MSG_COMPLETE and signals100* the IOP. This two-step process is provided to allow the handler to defer101* message processing to a bottom-half handler if the processing will take102* a significant amount of time (handlers are called at interrupt time so they103* should execute quickly.)104*/105106#include <linux/types.h>107#include <linux/kernel.h>108#include <linux/mm.h>109#include <linux/delay.h>110#include <linux/init.h>111#include <linux/interrupt.h>112113#include <asm/bootinfo.h>114#include <asm/macintosh.h>115#include <asm/macints.h>116#include <asm/mac_iop.h>117#include <asm/mac_oss.h>118119/*#define DEBUG_IOP*/120121/* Set to non-zero if the IOPs are present. Set by iop_init() */122123int iop_scc_present,iop_ism_present;124125/* structure for tracking channel listeners */126127struct listener {128const char *devname;129void (*handler)(struct iop_msg *);130};131132/*133* IOP structures for the two IOPs134*135* The SCC IOP controls both serial ports (A and B) as its two functions.136* The ISM IOP controls the SWIM (floppy drive) and ADB.137*/138139static volatile struct mac_iop *iop_base[NUM_IOPS];140141/*142* IOP message queues143*/144145static struct iop_msg iop_msg_pool[NUM_IOP_MSGS];146static struct iop_msg *iop_send_queue[NUM_IOPS][NUM_IOP_CHAN];147static struct listener iop_listeners[NUM_IOPS][NUM_IOP_CHAN];148149irqreturn_t iop_ism_irq(int, void *);150151extern void oss_irq_enable(int);152153/*154* Private access functions155*/156157static __inline__ void iop_loadaddr(volatile struct mac_iop *iop, __u16 addr)158{159iop->ram_addr_lo = addr;160iop->ram_addr_hi = addr >> 8;161}162163static __inline__ __u8 iop_readb(volatile struct mac_iop *iop, __u16 addr)164{165iop->ram_addr_lo = addr;166iop->ram_addr_hi = addr >> 8;167return iop->ram_data;168}169170static __inline__ void iop_writeb(volatile struct mac_iop *iop, __u16 addr, __u8 data)171{172iop->ram_addr_lo = addr;173iop->ram_addr_hi = addr >> 8;174iop->ram_data = data;175}176177static __inline__ void iop_stop(volatile struct mac_iop *iop)178{179iop->status_ctrl &= ~IOP_RUN;180}181182static __inline__ void iop_start(volatile struct mac_iop *iop)183{184iop->status_ctrl = IOP_RUN | IOP_AUTOINC;185}186187static __inline__ void iop_bypass(volatile struct mac_iop *iop)188{189iop->status_ctrl |= IOP_BYPASS;190}191192static __inline__ void iop_interrupt(volatile struct mac_iop *iop)193{194iop->status_ctrl |= IOP_IRQ;195}196197static int iop_alive(volatile struct mac_iop *iop)198{199int retval;200201retval = (iop_readb(iop, IOP_ADDR_ALIVE) == 0xFF);202iop_writeb(iop, IOP_ADDR_ALIVE, 0);203return retval;204}205206static struct iop_msg *iop_alloc_msg(void)207{208int i;209unsigned long flags;210211local_irq_save(flags);212213for (i = 0 ; i < NUM_IOP_MSGS ; i++) {214if (iop_msg_pool[i].status == IOP_MSGSTATUS_UNUSED) {215iop_msg_pool[i].status = IOP_MSGSTATUS_WAITING;216local_irq_restore(flags);217return &iop_msg_pool[i];218}219}220221local_irq_restore(flags);222return NULL;223}224225static void iop_free_msg(struct iop_msg *msg)226{227msg->status = IOP_MSGSTATUS_UNUSED;228}229230/*231* This is called by the startup code before anything else. Its purpose232* is to find and initialize the IOPs early in the boot sequence, so that233* the serial IOP can be placed into bypass mode _before_ we try to234* initialize the serial console.235*/236237void __init iop_preinit(void)238{239if (macintosh_config->scc_type == MAC_SCC_IOP) {240if (macintosh_config->ident == MAC_MODEL_IIFX) {241iop_base[IOP_NUM_SCC] = (struct mac_iop *) SCC_IOP_BASE_IIFX;242} else {243iop_base[IOP_NUM_SCC] = (struct mac_iop *) SCC_IOP_BASE_QUADRA;244}245iop_base[IOP_NUM_SCC]->status_ctrl = 0x87;246iop_scc_present = 1;247} else {248iop_base[IOP_NUM_SCC] = NULL;249iop_scc_present = 0;250}251if (macintosh_config->adb_type == MAC_ADB_IOP) {252if (macintosh_config->ident == MAC_MODEL_IIFX) {253iop_base[IOP_NUM_ISM] = (struct mac_iop *) ISM_IOP_BASE_IIFX;254} else {255iop_base[IOP_NUM_ISM] = (struct mac_iop *) ISM_IOP_BASE_QUADRA;256}257iop_base[IOP_NUM_ISM]->status_ctrl = 0;258iop_ism_present = 1;259} else {260iop_base[IOP_NUM_ISM] = NULL;261iop_ism_present = 0;262}263}264265/*266* Initialize the IOPs, if present.267*/268269void __init iop_init(void)270{271int i;272273if (iop_scc_present) {274printk("IOP: detected SCC IOP at %p\n", iop_base[IOP_NUM_SCC]);275}276if (iop_ism_present) {277printk("IOP: detected ISM IOP at %p\n", iop_base[IOP_NUM_ISM]);278iop_start(iop_base[IOP_NUM_ISM]);279iop_alive(iop_base[IOP_NUM_ISM]); /* clears the alive flag */280}281282/* Make the whole pool available and empty the queues */283284for (i = 0 ; i < NUM_IOP_MSGS ; i++) {285iop_msg_pool[i].status = IOP_MSGSTATUS_UNUSED;286}287288for (i = 0 ; i < NUM_IOP_CHAN ; i++) {289iop_send_queue[IOP_NUM_SCC][i] = NULL;290iop_send_queue[IOP_NUM_ISM][i] = NULL;291iop_listeners[IOP_NUM_SCC][i].devname = NULL;292iop_listeners[IOP_NUM_SCC][i].handler = NULL;293iop_listeners[IOP_NUM_ISM][i].devname = NULL;294iop_listeners[IOP_NUM_ISM][i].handler = NULL;295}296}297298/*299* Register the interrupt handler for the IOPs.300* TODO: might be wrong for non-OSS machines. Anyone?301*/302303void __init iop_register_interrupts(void)304{305if (iop_ism_present) {306if (oss_present) {307if (request_irq(OSS_IRQLEV_IOPISM, iop_ism_irq,308IRQ_FLG_LOCK, "ISM IOP",309(void *) IOP_NUM_ISM))310pr_err("Couldn't register ISM IOP interrupt\n");311oss_irq_enable(IRQ_MAC_ADB);312} else {313if (request_irq(IRQ_VIA2_0, iop_ism_irq,314IRQ_FLG_LOCK|IRQ_FLG_FAST, "ISM IOP",315(void *) IOP_NUM_ISM))316pr_err("Couldn't register ISM IOP interrupt\n");317}318if (!iop_alive(iop_base[IOP_NUM_ISM])) {319printk("IOP: oh my god, they killed the ISM IOP!\n");320} else {321printk("IOP: the ISM IOP seems to be alive.\n");322}323}324}325326/*327* Register or unregister a listener for a specific IOP and channel328*329* If the handler pointer is NULL the current listener (if any) is330* unregistered. Otherwise the new listener is registered provided331* there is no existing listener registered.332*/333334int iop_listen(uint iop_num, uint chan,335void (*handler)(struct iop_msg *),336const char *devname)337{338if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;339if (chan >= NUM_IOP_CHAN) return -EINVAL;340if (iop_listeners[iop_num][chan].handler && handler) return -EINVAL;341iop_listeners[iop_num][chan].devname = devname;342iop_listeners[iop_num][chan].handler = handler;343return 0;344}345346/*347* Complete reception of a message, which just means copying the reply348* into the buffer, setting the channel state to MSG_COMPLETE and349* notifying the IOP.350*/351352void iop_complete_message(struct iop_msg *msg)353{354int iop_num = msg->iop_num;355int chan = msg->channel;356int i,offset;357358#ifdef DEBUG_IOP359printk("iop_complete(%p): iop %d chan %d\n", msg, msg->iop_num, msg->channel);360#endif361362offset = IOP_ADDR_RECV_MSG + (msg->channel * IOP_MSG_LEN);363364for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {365iop_writeb(iop_base[iop_num], offset, msg->reply[i]);366}367368iop_writeb(iop_base[iop_num],369IOP_ADDR_RECV_STATE + chan, IOP_MSG_COMPLETE);370iop_interrupt(iop_base[msg->iop_num]);371372iop_free_msg(msg);373}374375/*376* Actually put a message into a send channel buffer377*/378379static void iop_do_send(struct iop_msg *msg)380{381volatile struct mac_iop *iop = iop_base[msg->iop_num];382int i,offset;383384offset = IOP_ADDR_SEND_MSG + (msg->channel * IOP_MSG_LEN);385386for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {387iop_writeb(iop, offset, msg->message[i]);388}389390iop_writeb(iop, IOP_ADDR_SEND_STATE + msg->channel, IOP_MSG_NEW);391392iop_interrupt(iop);393}394395/*396* Handle sending a message on a channel that397* has gone into the IOP_MSG_COMPLETE state.398*/399400static void iop_handle_send(uint iop_num, uint chan)401{402volatile struct mac_iop *iop = iop_base[iop_num];403struct iop_msg *msg,*msg2;404int i,offset;405406#ifdef DEBUG_IOP407printk("iop_handle_send: iop %d channel %d\n", iop_num, chan);408#endif409410iop_writeb(iop, IOP_ADDR_SEND_STATE + chan, IOP_MSG_IDLE);411412if (!(msg = iop_send_queue[iop_num][chan])) return;413414msg->status = IOP_MSGSTATUS_COMPLETE;415offset = IOP_ADDR_SEND_MSG + (chan * IOP_MSG_LEN);416for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {417msg->reply[i] = iop_readb(iop, offset);418}419if (msg->handler) (*msg->handler)(msg);420msg2 = msg;421msg = msg->next;422iop_free_msg(msg2);423424iop_send_queue[iop_num][chan] = msg;425if (msg) iop_do_send(msg);426}427428/*429* Handle reception of a message on a channel that has430* gone into the IOP_MSG_NEW state.431*/432433static void iop_handle_recv(uint iop_num, uint chan)434{435volatile struct mac_iop *iop = iop_base[iop_num];436int i,offset;437struct iop_msg *msg;438439#ifdef DEBUG_IOP440printk("iop_handle_recv: iop %d channel %d\n", iop_num, chan);441#endif442443msg = iop_alloc_msg();444msg->iop_num = iop_num;445msg->channel = chan;446msg->status = IOP_MSGSTATUS_UNSOL;447msg->handler = iop_listeners[iop_num][chan].handler;448449offset = IOP_ADDR_RECV_MSG + (chan * IOP_MSG_LEN);450451for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {452msg->message[i] = iop_readb(iop, offset);453}454455iop_writeb(iop, IOP_ADDR_RECV_STATE + chan, IOP_MSG_RCVD);456457/* If there is a listener, call it now. Otherwise complete */458/* the message ourselves to avoid possible stalls. */459460if (msg->handler) {461(*msg->handler)(msg);462} else {463#ifdef DEBUG_IOP464printk("iop_handle_recv: unclaimed message on iop %d channel %d\n", iop_num, chan);465printk("iop_handle_recv:");466for (i = 0 ; i < IOP_MSG_LEN ; i++) {467printk(" %02X", (uint) msg->message[i]);468}469printk("\n");470#endif471iop_complete_message(msg);472}473}474475/*476* Send a message477*478* The message is placed at the end of the send queue. Afterwards if the479* channel is idle we force an immediate send of the next message in the480* queue.481*/482483int iop_send_message(uint iop_num, uint chan, void *privdata,484uint msg_len, __u8 *msg_data,485void (*handler)(struct iop_msg *))486{487struct iop_msg *msg, *q;488489if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;490if (chan >= NUM_IOP_CHAN) return -EINVAL;491if (msg_len > IOP_MSG_LEN) return -EINVAL;492493msg = iop_alloc_msg();494if (!msg) return -ENOMEM;495496msg->next = NULL;497msg->status = IOP_MSGSTATUS_WAITING;498msg->iop_num = iop_num;499msg->channel = chan;500msg->caller_priv = privdata;501memcpy(msg->message, msg_data, msg_len);502msg->handler = handler;503504if (!(q = iop_send_queue[iop_num][chan])) {505iop_send_queue[iop_num][chan] = msg;506} else {507while (q->next) q = q->next;508q->next = msg;509}510511if (iop_readb(iop_base[iop_num],512IOP_ADDR_SEND_STATE + chan) == IOP_MSG_IDLE) {513iop_do_send(msg);514}515516return 0;517}518519/*520* Upload code to the shared RAM of an IOP.521*/522523void iop_upload_code(uint iop_num, __u8 *code_start,524uint code_len, __u16 shared_ram_start)525{526if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;527528iop_loadaddr(iop_base[iop_num], shared_ram_start);529530while (code_len--) {531iop_base[iop_num]->ram_data = *code_start++;532}533}534535/*536* Download code from the shared RAM of an IOP.537*/538539void iop_download_code(uint iop_num, __u8 *code_start,540uint code_len, __u16 shared_ram_start)541{542if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;543544iop_loadaddr(iop_base[iop_num], shared_ram_start);545546while (code_len--) {547*code_start++ = iop_base[iop_num]->ram_data;548}549}550551/*552* Compare the code in the shared RAM of an IOP with a copy in system memory553* and return 0 on match or the first nonmatching system memory address on554* failure.555*/556557__u8 *iop_compare_code(uint iop_num, __u8 *code_start,558uint code_len, __u16 shared_ram_start)559{560if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return code_start;561562iop_loadaddr(iop_base[iop_num], shared_ram_start);563564while (code_len--) {565if (*code_start != iop_base[iop_num]->ram_data) {566return code_start;567}568code_start++;569}570return (__u8 *) 0;571}572573/*574* Handle an ISM IOP interrupt575*/576577irqreturn_t iop_ism_irq(int irq, void *dev_id)578{579uint iop_num = (uint) dev_id;580volatile struct mac_iop *iop = iop_base[iop_num];581int i,state;582583#ifdef DEBUG_IOP584printk("iop_ism_irq: status = %02X\n", (uint) iop->status_ctrl);585#endif586587/* INT0 indicates a state change on an outgoing message channel */588589if (iop->status_ctrl & IOP_INT0) {590iop->status_ctrl = IOP_INT0 | IOP_RUN | IOP_AUTOINC;591#ifdef DEBUG_IOP592printk("iop_ism_irq: new status = %02X, send states",593(uint) iop->status_ctrl);594#endif595for (i = 0 ; i < NUM_IOP_CHAN ; i++) {596state = iop_readb(iop, IOP_ADDR_SEND_STATE + i);597#ifdef DEBUG_IOP598printk(" %02X", state);599#endif600if (state == IOP_MSG_COMPLETE) {601iop_handle_send(iop_num, i);602}603}604#ifdef DEBUG_IOP605printk("\n");606#endif607}608609if (iop->status_ctrl & IOP_INT1) { /* INT1 for incoming msgs */610iop->status_ctrl = IOP_INT1 | IOP_RUN | IOP_AUTOINC;611#ifdef DEBUG_IOP612printk("iop_ism_irq: new status = %02X, recv states",613(uint) iop->status_ctrl);614#endif615for (i = 0 ; i < NUM_IOP_CHAN ; i++) {616state = iop_readb(iop, IOP_ADDR_RECV_STATE + i);617#ifdef DEBUG_IOP618printk(" %02X", state);619#endif620if (state == IOP_MSG_NEW) {621iop_handle_recv(iop_num, i);622}623}624#ifdef DEBUG_IOP625printk("\n");626#endif627}628return IRQ_HANDLED;629}630631632