/*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 of28* 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 The SCC IOP has to be placed in bypass mode before the serial console50* gets initialized. iop_init() would be one place to do that. Or the51* bootloader could do that. For now, the Serial Switch control panel52* is needed for that -- contrary to the changelog above.53* o Something should be periodically checking iop_alive() to make sure the54* IOP hasn't died.55* o Some of the IOP manager routines need better error checking and56* return codes. Nothing major, just prettying up.57*/5859/*60* -----------------------61* IOP Message Passing 10162* -----------------------63*64* The host talks to the IOPs using a rather simple message-passing scheme via65* a shared memory area in the IOP RAM. Each IOP has seven "channels"; each66* channel is connected to a specific software driver on the IOP. For example67* on the SCC IOP there is one channel for each serial port. Each channel has68* an incoming and an outgoing message queue with a depth of one.69*70* A message is 32 bytes plus a state byte for the channel (MSG_IDLE, MSG_NEW,71* MSG_RCVD, MSG_COMPLETE). To send a message you copy the message into the72* buffer, set the state to MSG_NEW and signal the IOP by setting the IRQ flag73* in the IOP control to 1. The IOP will move the state to MSG_RCVD when it74* receives the message and then to MSG_COMPLETE when the message processing75* has completed. It is the host's responsibility at that point to read the76* reply back out of the send channel buffer and reset the channel state back77* to MSG_IDLE.78*79* To receive message from the IOP the same procedure is used except the roles80* are reversed. That is, the IOP puts message in the channel with a state of81* MSG_NEW, and the host receives the message and move its state to MSG_RCVD82* and then to MSG_COMPLETE when processing is completed and the reply (if any)83* has been placed back in the receive channel. The IOP will then reset the84* channel state to MSG_IDLE.85*86* Two sets of host interrupts are provided, INT0 and INT1. Both appear on one87* interrupt level; they are distinguished by a pair of bits in the IOP status88* register. The IOP will raise INT0 when one or more messages in the send89* channels have gone to the MSG_COMPLETE state and it will raise INT1 when one90* or more messages on the receive channels have gone to the MSG_NEW state.91*92* Since each channel handles only one message we have to implement a small93* interrupt-driven queue on our end. Messages to be sent are placed on the94* queue for sending and contain a pointer to an optional callback function.95* The handler for a message is called when the message state goes to96* MSG_COMPLETE.97*98* For receiving message we maintain a list of handler functions to call when99* a message is received on that IOP/channel combination. The handlers are100* called much like an interrupt handler and are passed a copy of the message101* from the IOP. The message state will be in MSG_RCVD while the handler runs;102* it is the handler's responsibility to call iop_complete_message() when103* finished; this function moves the message state to MSG_COMPLETE and signals104* the IOP. This two-step process is provided to allow the handler to defer105* message processing to a bottom-half handler if the processing will take106* a significant amount of time (handlers are called at interrupt time so they107* should execute quickly.)108*/109110#include <linux/types.h>111#include <linux/kernel.h>112#include <linux/mm.h>113#include <linux/delay.h>114#include <linux/init.h>115#include <linux/interrupt.h>116117#include <asm/macintosh.h>118#include <asm/macints.h>119#include <asm/mac_iop.h>120121#include "mac.h"122123#ifdef DEBUG124#define iop_pr_debug(fmt, ...) \125printk(KERN_DEBUG "%s: " fmt, __func__, ##__VA_ARGS__)126#define iop_pr_cont(fmt, ...) \127printk(KERN_CONT fmt, ##__VA_ARGS__)128#else129#define iop_pr_debug(fmt, ...) \130no_printk(KERN_DEBUG "%s: " fmt, __func__, ##__VA_ARGS__)131#define iop_pr_cont(fmt, ...) \132no_printk(KERN_CONT fmt, ##__VA_ARGS__)133#endif134135/* Non-zero if the IOPs are present */136137int iop_scc_present, iop_ism_present;138139/* structure for tracking channel listeners */140141struct listener {142const char *devname;143void (*handler)(struct iop_msg *);144};145146/*147* IOP structures for the two IOPs148*149* The SCC IOP controls both serial ports (A and B) as its two functions.150* The ISM IOP controls the SWIM (floppy drive) and ADB.151*/152153static volatile struct mac_iop *iop_base[NUM_IOPS];154155/*156* IOP message queues157*/158159static struct iop_msg iop_msg_pool[NUM_IOP_MSGS];160static struct iop_msg *iop_send_queue[NUM_IOPS][NUM_IOP_CHAN];161static struct listener iop_listeners[NUM_IOPS][NUM_IOP_CHAN];162163irqreturn_t iop_ism_irq(int, void *);164165/*166* Private access functions167*/168169static __inline__ void iop_loadaddr(volatile struct mac_iop *iop, __u16 addr)170{171iop->ram_addr_lo = addr;172iop->ram_addr_hi = addr >> 8;173}174175static __inline__ __u8 iop_readb(volatile struct mac_iop *iop, __u16 addr)176{177iop->ram_addr_lo = addr;178iop->ram_addr_hi = addr >> 8;179return iop->ram_data;180}181182static __inline__ void iop_writeb(volatile struct mac_iop *iop, __u16 addr, __u8 data)183{184iop->ram_addr_lo = addr;185iop->ram_addr_hi = addr >> 8;186iop->ram_data = data;187}188189static __inline__ void iop_stop(volatile struct mac_iop *iop)190{191iop->status_ctrl = IOP_AUTOINC;192}193194static __inline__ void iop_start(volatile struct mac_iop *iop)195{196iop->status_ctrl = IOP_RUN | IOP_AUTOINC;197}198199static __inline__ void iop_interrupt(volatile struct mac_iop *iop)200{201iop->status_ctrl = IOP_IRQ | IOP_RUN | IOP_AUTOINC;202}203204static int iop_alive(volatile struct mac_iop *iop)205{206int retval;207208retval = (iop_readb(iop, IOP_ADDR_ALIVE) == 0xFF);209iop_writeb(iop, IOP_ADDR_ALIVE, 0);210return retval;211}212213static struct iop_msg *iop_get_unused_msg(void)214{215int i;216unsigned long flags;217218local_irq_save(flags);219220for (i = 0 ; i < NUM_IOP_MSGS ; i++) {221if (iop_msg_pool[i].status == IOP_MSGSTATUS_UNUSED) {222iop_msg_pool[i].status = IOP_MSGSTATUS_WAITING;223local_irq_restore(flags);224return &iop_msg_pool[i];225}226}227228local_irq_restore(flags);229return NULL;230}231232/*233* Initialize the IOPs, if present.234*/235236void __init iop_init(void)237{238int i;239240if (macintosh_config->scc_type == MAC_SCC_IOP) {241if (macintosh_config->ident == MAC_MODEL_IIFX)242iop_base[IOP_NUM_SCC] = (struct mac_iop *)SCC_IOP_BASE_IIFX;243else244iop_base[IOP_NUM_SCC] = (struct mac_iop *)SCC_IOP_BASE_QUADRA;245iop_scc_present = 1;246pr_debug("SCC IOP detected at %p\n", iop_base[IOP_NUM_SCC]);247}248if (macintosh_config->adb_type == MAC_ADB_IOP) {249if (macintosh_config->ident == MAC_MODEL_IIFX)250iop_base[IOP_NUM_ISM] = (struct mac_iop *)ISM_IOP_BASE_IIFX;251else252iop_base[IOP_NUM_ISM] = (struct mac_iop *)ISM_IOP_BASE_QUADRA;253iop_ism_present = 1;254pr_debug("ISM IOP detected at %p\n", iop_base[IOP_NUM_ISM]);255256iop_stop(iop_base[IOP_NUM_ISM]);257iop_start(iop_base[IOP_NUM_ISM]);258iop_alive(iop_base[IOP_NUM_ISM]); /* clears the alive flag */259}260261/* Make the whole pool available and empty the queues */262263for (i = 0 ; i < NUM_IOP_MSGS ; i++) {264iop_msg_pool[i].status = IOP_MSGSTATUS_UNUSED;265}266267for (i = 0 ; i < NUM_IOP_CHAN ; i++) {268iop_send_queue[IOP_NUM_SCC][i] = NULL;269iop_send_queue[IOP_NUM_ISM][i] = NULL;270iop_listeners[IOP_NUM_SCC][i].devname = NULL;271iop_listeners[IOP_NUM_SCC][i].handler = NULL;272iop_listeners[IOP_NUM_ISM][i].devname = NULL;273iop_listeners[IOP_NUM_ISM][i].handler = NULL;274}275}276277/*278* Register the interrupt handler for the IOPs.279*/280281void __init iop_register_interrupts(void)282{283if (iop_ism_present) {284if (macintosh_config->ident == MAC_MODEL_IIFX) {285if (request_irq(IRQ_MAC_ADB, iop_ism_irq, 0,286"ISM IOP", (void *)IOP_NUM_ISM))287pr_err("Couldn't register ISM IOP interrupt\n");288} else {289if (request_irq(IRQ_VIA2_0, iop_ism_irq, 0, "ISM IOP",290(void *)IOP_NUM_ISM))291pr_err("Couldn't register ISM IOP interrupt\n");292}293if (!iop_alive(iop_base[IOP_NUM_ISM])) {294pr_warn("IOP: oh my god, they killed the ISM IOP!\n");295} else {296pr_warn("IOP: the ISM IOP seems to be alive.\n");297}298}299}300301/*302* Register or unregister a listener for a specific IOP and channel303*304* If the handler pointer is NULL the current listener (if any) is305* unregistered. Otherwise the new listener is registered provided306* there is no existing listener registered.307*/308309int iop_listen(uint iop_num, uint chan,310void (*handler)(struct iop_msg *),311const char *devname)312{313if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;314if (chan >= NUM_IOP_CHAN) return -EINVAL;315if (iop_listeners[iop_num][chan].handler && handler) return -EINVAL;316iop_listeners[iop_num][chan].devname = devname;317iop_listeners[iop_num][chan].handler = handler;318return 0;319}320321/*322* Complete reception of a message, which just means copying the reply323* into the buffer, setting the channel state to MSG_COMPLETE and324* notifying the IOP.325*/326327void iop_complete_message(struct iop_msg *msg)328{329int iop_num = msg->iop_num;330int chan = msg->channel;331int i,offset;332333iop_pr_debug("iop_num %d chan %d reply %*ph\n",334msg->iop_num, msg->channel, IOP_MSG_LEN, msg->reply);335336offset = IOP_ADDR_RECV_MSG + (msg->channel * IOP_MSG_LEN);337338for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {339iop_writeb(iop_base[iop_num], offset, msg->reply[i]);340}341342iop_writeb(iop_base[iop_num],343IOP_ADDR_RECV_STATE + chan, IOP_MSG_COMPLETE);344iop_interrupt(iop_base[msg->iop_num]);345346msg->status = IOP_MSGSTATUS_UNUSED;347}348349/*350* Actually put a message into a send channel buffer351*/352353static void iop_do_send(struct iop_msg *msg)354{355volatile struct mac_iop *iop = iop_base[msg->iop_num];356int i,offset;357358iop_pr_debug("iop_num %d chan %d message %*ph\n",359msg->iop_num, msg->channel, IOP_MSG_LEN, msg->message);360361offset = IOP_ADDR_SEND_MSG + (msg->channel * IOP_MSG_LEN);362363for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {364iop_writeb(iop, offset, msg->message[i]);365}366367iop_writeb(iop, IOP_ADDR_SEND_STATE + msg->channel, IOP_MSG_NEW);368369iop_interrupt(iop);370}371372/*373* Handle sending a message on a channel that374* has gone into the IOP_MSG_COMPLETE state.375*/376377static void iop_handle_send(uint iop_num, uint chan)378{379volatile struct mac_iop *iop = iop_base[iop_num];380struct iop_msg *msg;381int i,offset;382383iop_writeb(iop, IOP_ADDR_SEND_STATE + chan, IOP_MSG_IDLE);384385if (!(msg = iop_send_queue[iop_num][chan])) return;386387msg->status = IOP_MSGSTATUS_COMPLETE;388offset = IOP_ADDR_SEND_MSG + (chan * IOP_MSG_LEN);389for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {390msg->reply[i] = iop_readb(iop, offset);391}392iop_pr_debug("iop_num %d chan %d reply %*ph\n",393iop_num, chan, IOP_MSG_LEN, msg->reply);394395if (msg->handler) (*msg->handler)(msg);396msg->status = IOP_MSGSTATUS_UNUSED;397msg = msg->next;398iop_send_queue[iop_num][chan] = msg;399if (msg && iop_readb(iop, IOP_ADDR_SEND_STATE + chan) == IOP_MSG_IDLE)400iop_do_send(msg);401}402403/*404* Handle reception of a message on a channel that has405* gone into the IOP_MSG_NEW state.406*/407408static void iop_handle_recv(uint iop_num, uint chan)409{410volatile struct mac_iop *iop = iop_base[iop_num];411int i,offset;412struct iop_msg *msg;413414msg = iop_get_unused_msg();415msg->iop_num = iop_num;416msg->channel = chan;417msg->status = IOP_MSGSTATUS_UNSOL;418msg->handler = iop_listeners[iop_num][chan].handler;419420offset = IOP_ADDR_RECV_MSG + (chan * IOP_MSG_LEN);421422for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) {423msg->message[i] = iop_readb(iop, offset);424}425iop_pr_debug("iop_num %d chan %d message %*ph\n",426iop_num, chan, IOP_MSG_LEN, msg->message);427428iop_writeb(iop, IOP_ADDR_RECV_STATE + chan, IOP_MSG_RCVD);429430/* If there is a listener, call it now. Otherwise complete */431/* the message ourselves to avoid possible stalls. */432433if (msg->handler) {434(*msg->handler)(msg);435} else {436memset(msg->reply, 0, IOP_MSG_LEN);437iop_complete_message(msg);438}439}440441/*442* Send a message443*444* The message is placed at the end of the send queue. Afterwards if the445* channel is idle we force an immediate send of the next message in the446* queue.447*/448449int iop_send_message(uint iop_num, uint chan, void *privdata,450uint msg_len, __u8 *msg_data,451void (*handler)(struct iop_msg *))452{453struct iop_msg *msg, *q;454455if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL;456if (chan >= NUM_IOP_CHAN) return -EINVAL;457if (msg_len > IOP_MSG_LEN) return -EINVAL;458459msg = iop_get_unused_msg();460if (!msg) return -ENOMEM;461462msg->next = NULL;463msg->status = IOP_MSGSTATUS_WAITING;464msg->iop_num = iop_num;465msg->channel = chan;466msg->caller_priv = privdata;467memcpy(msg->message, msg_data, msg_len);468msg->handler = handler;469470if (!(q = iop_send_queue[iop_num][chan])) {471iop_send_queue[iop_num][chan] = msg;472iop_do_send(msg);473} else {474while (q->next) q = q->next;475q->next = msg;476}477478return 0;479}480481/*482* Upload code to the shared RAM of an IOP.483*/484485void iop_upload_code(uint iop_num, __u8 *code_start,486uint code_len, __u16 shared_ram_start)487{488if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;489490iop_loadaddr(iop_base[iop_num], shared_ram_start);491492while (code_len--) {493iop_base[iop_num]->ram_data = *code_start++;494}495}496497/*498* Download code from the shared RAM of an IOP.499*/500501void iop_download_code(uint iop_num, __u8 *code_start,502uint code_len, __u16 shared_ram_start)503{504if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return;505506iop_loadaddr(iop_base[iop_num], shared_ram_start);507508while (code_len--) {509*code_start++ = iop_base[iop_num]->ram_data;510}511}512513/*514* Compare the code in the shared RAM of an IOP with a copy in system memory515* and return 0 on match or the first nonmatching system memory address on516* failure.517*/518519__u8 *iop_compare_code(uint iop_num, __u8 *code_start,520uint code_len, __u16 shared_ram_start)521{522if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return code_start;523524iop_loadaddr(iop_base[iop_num], shared_ram_start);525526while (code_len--) {527if (*code_start != iop_base[iop_num]->ram_data) {528return code_start;529}530code_start++;531}532return (__u8 *) 0;533}534535/*536* Handle an ISM IOP interrupt537*/538539irqreturn_t iop_ism_irq(int irq, void *dev_id)540{541uint iop_num = (uint) dev_id;542volatile struct mac_iop *iop = iop_base[iop_num];543int i,state;544u8 events = iop->status_ctrl & (IOP_INT0 | IOP_INT1);545546do {547iop_pr_debug("iop_num %d status %02X\n", iop_num,548iop->status_ctrl);549550/* INT0 indicates state change on an outgoing message channel */551if (events & IOP_INT0) {552iop->status_ctrl = IOP_INT0 | IOP_RUN | IOP_AUTOINC;553for (i = 0; i < NUM_IOP_CHAN; i++) {554state = iop_readb(iop, IOP_ADDR_SEND_STATE + i);555if (state == IOP_MSG_COMPLETE)556iop_handle_send(iop_num, i);557else if (state != IOP_MSG_IDLE)558iop_pr_debug("chan %d send state %02X\n",559i, state);560}561}562563/* INT1 for incoming messages */564if (events & IOP_INT1) {565iop->status_ctrl = IOP_INT1 | IOP_RUN | IOP_AUTOINC;566for (i = 0; i < NUM_IOP_CHAN; i++) {567state = iop_readb(iop, IOP_ADDR_RECV_STATE + i);568if (state == IOP_MSG_NEW)569iop_handle_recv(iop_num, i);570else if (state != IOP_MSG_IDLE)571iop_pr_debug("chan %d recv state %02X\n",572i, state);573}574}575576events = iop->status_ctrl & (IOP_INT0 | IOP_INT1);577} while (events);578579return IRQ_HANDLED;580}581582void iop_ism_irq_poll(uint iop_num)583{584unsigned long flags;585586local_irq_save(flags);587iop_ism_irq(0, (void *)iop_num);588local_irq_restore(flags);589}590591592