162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * I/O Processor (IOP) management 362306a36Sopenharmony_ci * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org) 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 662306a36Sopenharmony_ci * modification, are permitted provided that the following conditions 762306a36Sopenharmony_ci * are met: 862306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 962306a36Sopenharmony_ci * notice and this list of conditions. 1062306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 1162306a36Sopenharmony_ci * notice and this list of conditions in the documentation and/or other 1262306a36Sopenharmony_ci * materials provided with the distribution. 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci/* 1662306a36Sopenharmony_ci * The IOP chips are used in the IIfx and some Quadras (900, 950) to manage 1762306a36Sopenharmony_ci * serial and ADB. They are actually a 6502 processor and some glue logic. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * 990429 (jmt) - Initial implementation, just enough to knock the SCC IOP 2062306a36Sopenharmony_ci * into compatible mode so nobody has to fiddle with the 2162306a36Sopenharmony_ci * Serial Switch control panel anymore. 2262306a36Sopenharmony_ci * 990603 (jmt) - Added code to grab the correct ISM IOP interrupt for OSS 2362306a36Sopenharmony_ci * and non-OSS machines (at least I hope it's correct on a 2462306a36Sopenharmony_ci * non-OSS machine -- someone with a Q900 or Q950 needs to 2562306a36Sopenharmony_ci * check this.) 2662306a36Sopenharmony_ci * 990605 (jmt) - Rearranged things a bit wrt IOP detection; iop_present is 2762306a36Sopenharmony_ci * gone, IOP base addresses are now in an array and the 2862306a36Sopenharmony_ci * globally-visible functions take an IOP number instead of 2962306a36Sopenharmony_ci * an actual base address. 3062306a36Sopenharmony_ci * 990610 (jmt) - Finished the message passing framework and it seems to work. 3162306a36Sopenharmony_ci * Sending _definitely_ works; my adb-bus.c mods can send 3262306a36Sopenharmony_ci * messages and receive the MSG_COMPLETED status back from the 3362306a36Sopenharmony_ci * IOP. The trick now is figuring out the message formats. 3462306a36Sopenharmony_ci * 990611 (jmt) - More cleanups. Fixed problem where unclaimed messages on a 3562306a36Sopenharmony_ci * receive channel were never properly acknowledged. Bracketed 3662306a36Sopenharmony_ci * the remaining debug printk's with #ifdef's and disabled 3762306a36Sopenharmony_ci * debugging. I can now type on the console. 3862306a36Sopenharmony_ci * 990612 (jmt) - Copyright notice added. Reworked the way replies are handled. 3962306a36Sopenharmony_ci * It turns out that replies are placed back in the send buffer 4062306a36Sopenharmony_ci * for that channel; messages on the receive channels are always 4162306a36Sopenharmony_ci * unsolicited messages from the IOP (and our replies to them 4262306a36Sopenharmony_ci * should go back in the receive channel.) Also added tracking 4362306a36Sopenharmony_ci * of device names to the listener functions ala the interrupt 4462306a36Sopenharmony_ci * handlers. 4562306a36Sopenharmony_ci * 990729 (jmt) - Added passing of pt_regs structure to IOP handlers. This is 4662306a36Sopenharmony_ci * used by the new unified ADB driver. 4762306a36Sopenharmony_ci * 4862306a36Sopenharmony_ci * TODO: 4962306a36Sopenharmony_ci * 5062306a36Sopenharmony_ci * o The SCC IOP has to be placed in bypass mode before the serial console 5162306a36Sopenharmony_ci * gets initialized. iop_init() would be one place to do that. Or the 5262306a36Sopenharmony_ci * bootloader could do that. For now, the Serial Switch control panel 5362306a36Sopenharmony_ci * is needed for that -- contrary to the changelog above. 5462306a36Sopenharmony_ci * o Something should be periodically checking iop_alive() to make sure the 5562306a36Sopenharmony_ci * IOP hasn't died. 5662306a36Sopenharmony_ci * o Some of the IOP manager routines need better error checking and 5762306a36Sopenharmony_ci * return codes. Nothing major, just prettying up. 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci/* 6162306a36Sopenharmony_ci * ----------------------- 6262306a36Sopenharmony_ci * IOP Message Passing 101 6362306a36Sopenharmony_ci * ----------------------- 6462306a36Sopenharmony_ci * 6562306a36Sopenharmony_ci * The host talks to the IOPs using a rather simple message-passing scheme via 6662306a36Sopenharmony_ci * a shared memory area in the IOP RAM. Each IOP has seven "channels"; each 6762306a36Sopenharmony_ci * channel is connected to a specific software driver on the IOP. For example 6862306a36Sopenharmony_ci * on the SCC IOP there is one channel for each serial port. Each channel has 6962306a36Sopenharmony_ci * an incoming and an outgoing message queue with a depth of one. 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * A message is 32 bytes plus a state byte for the channel (MSG_IDLE, MSG_NEW, 7262306a36Sopenharmony_ci * MSG_RCVD, MSG_COMPLETE). To send a message you copy the message into the 7362306a36Sopenharmony_ci * buffer, set the state to MSG_NEW and signal the IOP by setting the IRQ flag 7462306a36Sopenharmony_ci * in the IOP control to 1. The IOP will move the state to MSG_RCVD when it 7562306a36Sopenharmony_ci * receives the message and then to MSG_COMPLETE when the message processing 7662306a36Sopenharmony_ci * has completed. It is the host's responsibility at that point to read the 7762306a36Sopenharmony_ci * reply back out of the send channel buffer and reset the channel state back 7862306a36Sopenharmony_ci * to MSG_IDLE. 7962306a36Sopenharmony_ci * 8062306a36Sopenharmony_ci * To receive message from the IOP the same procedure is used except the roles 8162306a36Sopenharmony_ci * are reversed. That is, the IOP puts message in the channel with a state of 8262306a36Sopenharmony_ci * MSG_NEW, and the host receives the message and move its state to MSG_RCVD 8362306a36Sopenharmony_ci * and then to MSG_COMPLETE when processing is completed and the reply (if any) 8462306a36Sopenharmony_ci * has been placed back in the receive channel. The IOP will then reset the 8562306a36Sopenharmony_ci * channel state to MSG_IDLE. 8662306a36Sopenharmony_ci * 8762306a36Sopenharmony_ci * Two sets of host interrupts are provided, INT0 and INT1. Both appear on one 8862306a36Sopenharmony_ci * interrupt level; they are distinguished by a pair of bits in the IOP status 8962306a36Sopenharmony_ci * register. The IOP will raise INT0 when one or more messages in the send 9062306a36Sopenharmony_ci * channels have gone to the MSG_COMPLETE state and it will raise INT1 when one 9162306a36Sopenharmony_ci * or more messages on the receive channels have gone to the MSG_NEW state. 9262306a36Sopenharmony_ci * 9362306a36Sopenharmony_ci * Since each channel handles only one message we have to implement a small 9462306a36Sopenharmony_ci * interrupt-driven queue on our end. Messages to be sent are placed on the 9562306a36Sopenharmony_ci * queue for sending and contain a pointer to an optional callback function. 9662306a36Sopenharmony_ci * The handler for a message is called when the message state goes to 9762306a36Sopenharmony_ci * MSG_COMPLETE. 9862306a36Sopenharmony_ci * 9962306a36Sopenharmony_ci * For receiving message we maintain a list of handler functions to call when 10062306a36Sopenharmony_ci * a message is received on that IOP/channel combination. The handlers are 10162306a36Sopenharmony_ci * called much like an interrupt handler and are passed a copy of the message 10262306a36Sopenharmony_ci * from the IOP. The message state will be in MSG_RCVD while the handler runs; 10362306a36Sopenharmony_ci * it is the handler's responsibility to call iop_complete_message() when 10462306a36Sopenharmony_ci * finished; this function moves the message state to MSG_COMPLETE and signals 10562306a36Sopenharmony_ci * the IOP. This two-step process is provided to allow the handler to defer 10662306a36Sopenharmony_ci * message processing to a bottom-half handler if the processing will take 10762306a36Sopenharmony_ci * a significant amount of time (handlers are called at interrupt time so they 10862306a36Sopenharmony_ci * should execute quickly.) 10962306a36Sopenharmony_ci */ 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci#include <linux/types.h> 11262306a36Sopenharmony_ci#include <linux/kernel.h> 11362306a36Sopenharmony_ci#include <linux/mm.h> 11462306a36Sopenharmony_ci#include <linux/delay.h> 11562306a36Sopenharmony_ci#include <linux/init.h> 11662306a36Sopenharmony_ci#include <linux/interrupt.h> 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci#include <asm/macintosh.h> 11962306a36Sopenharmony_ci#include <asm/macints.h> 12062306a36Sopenharmony_ci#include <asm/mac_iop.h> 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci#ifdef DEBUG 12362306a36Sopenharmony_ci#define iop_pr_debug(fmt, ...) \ 12462306a36Sopenharmony_ci printk(KERN_DEBUG "%s: " fmt, __func__, ##__VA_ARGS__) 12562306a36Sopenharmony_ci#define iop_pr_cont(fmt, ...) \ 12662306a36Sopenharmony_ci printk(KERN_CONT fmt, ##__VA_ARGS__) 12762306a36Sopenharmony_ci#else 12862306a36Sopenharmony_ci#define iop_pr_debug(fmt, ...) \ 12962306a36Sopenharmony_ci no_printk(KERN_DEBUG "%s: " fmt, __func__, ##__VA_ARGS__) 13062306a36Sopenharmony_ci#define iop_pr_cont(fmt, ...) \ 13162306a36Sopenharmony_ci no_printk(KERN_CONT fmt, ##__VA_ARGS__) 13262306a36Sopenharmony_ci#endif 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci/* Non-zero if the IOPs are present */ 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciint iop_scc_present, iop_ism_present; 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci/* structure for tracking channel listeners */ 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_cistruct listener { 14162306a36Sopenharmony_ci const char *devname; 14262306a36Sopenharmony_ci void (*handler)(struct iop_msg *); 14362306a36Sopenharmony_ci}; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci/* 14662306a36Sopenharmony_ci * IOP structures for the two IOPs 14762306a36Sopenharmony_ci * 14862306a36Sopenharmony_ci * The SCC IOP controls both serial ports (A and B) as its two functions. 14962306a36Sopenharmony_ci * The ISM IOP controls the SWIM (floppy drive) and ADB. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_cistatic volatile struct mac_iop *iop_base[NUM_IOPS]; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci/* 15562306a36Sopenharmony_ci * IOP message queues 15662306a36Sopenharmony_ci */ 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_cistatic struct iop_msg iop_msg_pool[NUM_IOP_MSGS]; 15962306a36Sopenharmony_cistatic struct iop_msg *iop_send_queue[NUM_IOPS][NUM_IOP_CHAN]; 16062306a36Sopenharmony_cistatic struct listener iop_listeners[NUM_IOPS][NUM_IOP_CHAN]; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ciirqreturn_t iop_ism_irq(int, void *); 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci/* 16562306a36Sopenharmony_ci * Private access functions 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_cistatic __inline__ void iop_loadaddr(volatile struct mac_iop *iop, __u16 addr) 16962306a36Sopenharmony_ci{ 17062306a36Sopenharmony_ci iop->ram_addr_lo = addr; 17162306a36Sopenharmony_ci iop->ram_addr_hi = addr >> 8; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_cistatic __inline__ __u8 iop_readb(volatile struct mac_iop *iop, __u16 addr) 17562306a36Sopenharmony_ci{ 17662306a36Sopenharmony_ci iop->ram_addr_lo = addr; 17762306a36Sopenharmony_ci iop->ram_addr_hi = addr >> 8; 17862306a36Sopenharmony_ci return iop->ram_data; 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_cistatic __inline__ void iop_writeb(volatile struct mac_iop *iop, __u16 addr, __u8 data) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci iop->ram_addr_lo = addr; 18462306a36Sopenharmony_ci iop->ram_addr_hi = addr >> 8; 18562306a36Sopenharmony_ci iop->ram_data = data; 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_cistatic __inline__ void iop_stop(volatile struct mac_iop *iop) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci iop->status_ctrl = IOP_AUTOINC; 19162306a36Sopenharmony_ci} 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_cistatic __inline__ void iop_start(volatile struct mac_iop *iop) 19462306a36Sopenharmony_ci{ 19562306a36Sopenharmony_ci iop->status_ctrl = IOP_RUN | IOP_AUTOINC; 19662306a36Sopenharmony_ci} 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_cistatic __inline__ void iop_interrupt(volatile struct mac_iop *iop) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci iop->status_ctrl = IOP_IRQ | IOP_RUN | IOP_AUTOINC; 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_cistatic int iop_alive(volatile struct mac_iop *iop) 20462306a36Sopenharmony_ci{ 20562306a36Sopenharmony_ci int retval; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci retval = (iop_readb(iop, IOP_ADDR_ALIVE) == 0xFF); 20862306a36Sopenharmony_ci iop_writeb(iop, IOP_ADDR_ALIVE, 0); 20962306a36Sopenharmony_ci return retval; 21062306a36Sopenharmony_ci} 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_cistatic struct iop_msg *iop_get_unused_msg(void) 21362306a36Sopenharmony_ci{ 21462306a36Sopenharmony_ci int i; 21562306a36Sopenharmony_ci unsigned long flags; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci local_irq_save(flags); 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci for (i = 0 ; i < NUM_IOP_MSGS ; i++) { 22062306a36Sopenharmony_ci if (iop_msg_pool[i].status == IOP_MSGSTATUS_UNUSED) { 22162306a36Sopenharmony_ci iop_msg_pool[i].status = IOP_MSGSTATUS_WAITING; 22262306a36Sopenharmony_ci local_irq_restore(flags); 22362306a36Sopenharmony_ci return &iop_msg_pool[i]; 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci } 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci local_irq_restore(flags); 22862306a36Sopenharmony_ci return NULL; 22962306a36Sopenharmony_ci} 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci/* 23262306a36Sopenharmony_ci * Initialize the IOPs, if present. 23362306a36Sopenharmony_ci */ 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_civoid __init iop_init(void) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci int i; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci if (macintosh_config->scc_type == MAC_SCC_IOP) { 24062306a36Sopenharmony_ci if (macintosh_config->ident == MAC_MODEL_IIFX) 24162306a36Sopenharmony_ci iop_base[IOP_NUM_SCC] = (struct mac_iop *)SCC_IOP_BASE_IIFX; 24262306a36Sopenharmony_ci else 24362306a36Sopenharmony_ci iop_base[IOP_NUM_SCC] = (struct mac_iop *)SCC_IOP_BASE_QUADRA; 24462306a36Sopenharmony_ci iop_scc_present = 1; 24562306a36Sopenharmony_ci pr_debug("SCC IOP detected at %p\n", iop_base[IOP_NUM_SCC]); 24662306a36Sopenharmony_ci } 24762306a36Sopenharmony_ci if (macintosh_config->adb_type == MAC_ADB_IOP) { 24862306a36Sopenharmony_ci if (macintosh_config->ident == MAC_MODEL_IIFX) 24962306a36Sopenharmony_ci iop_base[IOP_NUM_ISM] = (struct mac_iop *)ISM_IOP_BASE_IIFX; 25062306a36Sopenharmony_ci else 25162306a36Sopenharmony_ci iop_base[IOP_NUM_ISM] = (struct mac_iop *)ISM_IOP_BASE_QUADRA; 25262306a36Sopenharmony_ci iop_ism_present = 1; 25362306a36Sopenharmony_ci pr_debug("ISM IOP detected at %p\n", iop_base[IOP_NUM_ISM]); 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci iop_stop(iop_base[IOP_NUM_ISM]); 25662306a36Sopenharmony_ci iop_start(iop_base[IOP_NUM_ISM]); 25762306a36Sopenharmony_ci iop_alive(iop_base[IOP_NUM_ISM]); /* clears the alive flag */ 25862306a36Sopenharmony_ci } 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci /* Make the whole pool available and empty the queues */ 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci for (i = 0 ; i < NUM_IOP_MSGS ; i++) { 26362306a36Sopenharmony_ci iop_msg_pool[i].status = IOP_MSGSTATUS_UNUSED; 26462306a36Sopenharmony_ci } 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci for (i = 0 ; i < NUM_IOP_CHAN ; i++) { 26762306a36Sopenharmony_ci iop_send_queue[IOP_NUM_SCC][i] = NULL; 26862306a36Sopenharmony_ci iop_send_queue[IOP_NUM_ISM][i] = NULL; 26962306a36Sopenharmony_ci iop_listeners[IOP_NUM_SCC][i].devname = NULL; 27062306a36Sopenharmony_ci iop_listeners[IOP_NUM_SCC][i].handler = NULL; 27162306a36Sopenharmony_ci iop_listeners[IOP_NUM_ISM][i].devname = NULL; 27262306a36Sopenharmony_ci iop_listeners[IOP_NUM_ISM][i].handler = NULL; 27362306a36Sopenharmony_ci } 27462306a36Sopenharmony_ci} 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci/* 27762306a36Sopenharmony_ci * Register the interrupt handler for the IOPs. 27862306a36Sopenharmony_ci */ 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_civoid __init iop_register_interrupts(void) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci if (iop_ism_present) { 28362306a36Sopenharmony_ci if (macintosh_config->ident == MAC_MODEL_IIFX) { 28462306a36Sopenharmony_ci if (request_irq(IRQ_MAC_ADB, iop_ism_irq, 0, 28562306a36Sopenharmony_ci "ISM IOP", (void *)IOP_NUM_ISM)) 28662306a36Sopenharmony_ci pr_err("Couldn't register ISM IOP interrupt\n"); 28762306a36Sopenharmony_ci } else { 28862306a36Sopenharmony_ci if (request_irq(IRQ_VIA2_0, iop_ism_irq, 0, "ISM IOP", 28962306a36Sopenharmony_ci (void *)IOP_NUM_ISM)) 29062306a36Sopenharmony_ci pr_err("Couldn't register ISM IOP interrupt\n"); 29162306a36Sopenharmony_ci } 29262306a36Sopenharmony_ci if (!iop_alive(iop_base[IOP_NUM_ISM])) { 29362306a36Sopenharmony_ci pr_warn("IOP: oh my god, they killed the ISM IOP!\n"); 29462306a36Sopenharmony_ci } else { 29562306a36Sopenharmony_ci pr_warn("IOP: the ISM IOP seems to be alive.\n"); 29662306a36Sopenharmony_ci } 29762306a36Sopenharmony_ci } 29862306a36Sopenharmony_ci} 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci/* 30162306a36Sopenharmony_ci * Register or unregister a listener for a specific IOP and channel 30262306a36Sopenharmony_ci * 30362306a36Sopenharmony_ci * If the handler pointer is NULL the current listener (if any) is 30462306a36Sopenharmony_ci * unregistered. Otherwise the new listener is registered provided 30562306a36Sopenharmony_ci * there is no existing listener registered. 30662306a36Sopenharmony_ci */ 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ciint iop_listen(uint iop_num, uint chan, 30962306a36Sopenharmony_ci void (*handler)(struct iop_msg *), 31062306a36Sopenharmony_ci const char *devname) 31162306a36Sopenharmony_ci{ 31262306a36Sopenharmony_ci if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL; 31362306a36Sopenharmony_ci if (chan >= NUM_IOP_CHAN) return -EINVAL; 31462306a36Sopenharmony_ci if (iop_listeners[iop_num][chan].handler && handler) return -EINVAL; 31562306a36Sopenharmony_ci iop_listeners[iop_num][chan].devname = devname; 31662306a36Sopenharmony_ci iop_listeners[iop_num][chan].handler = handler; 31762306a36Sopenharmony_ci return 0; 31862306a36Sopenharmony_ci} 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci/* 32162306a36Sopenharmony_ci * Complete reception of a message, which just means copying the reply 32262306a36Sopenharmony_ci * into the buffer, setting the channel state to MSG_COMPLETE and 32362306a36Sopenharmony_ci * notifying the IOP. 32462306a36Sopenharmony_ci */ 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_civoid iop_complete_message(struct iop_msg *msg) 32762306a36Sopenharmony_ci{ 32862306a36Sopenharmony_ci int iop_num = msg->iop_num; 32962306a36Sopenharmony_ci int chan = msg->channel; 33062306a36Sopenharmony_ci int i,offset; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci iop_pr_debug("iop_num %d chan %d reply %*ph\n", 33362306a36Sopenharmony_ci msg->iop_num, msg->channel, IOP_MSG_LEN, msg->reply); 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci offset = IOP_ADDR_RECV_MSG + (msg->channel * IOP_MSG_LEN); 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) { 33862306a36Sopenharmony_ci iop_writeb(iop_base[iop_num], offset, msg->reply[i]); 33962306a36Sopenharmony_ci } 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci iop_writeb(iop_base[iop_num], 34262306a36Sopenharmony_ci IOP_ADDR_RECV_STATE + chan, IOP_MSG_COMPLETE); 34362306a36Sopenharmony_ci iop_interrupt(iop_base[msg->iop_num]); 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci msg->status = IOP_MSGSTATUS_UNUSED; 34662306a36Sopenharmony_ci} 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci/* 34962306a36Sopenharmony_ci * Actually put a message into a send channel buffer 35062306a36Sopenharmony_ci */ 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_cistatic void iop_do_send(struct iop_msg *msg) 35362306a36Sopenharmony_ci{ 35462306a36Sopenharmony_ci volatile struct mac_iop *iop = iop_base[msg->iop_num]; 35562306a36Sopenharmony_ci int i,offset; 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci iop_pr_debug("iop_num %d chan %d message %*ph\n", 35862306a36Sopenharmony_ci msg->iop_num, msg->channel, IOP_MSG_LEN, msg->message); 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci offset = IOP_ADDR_SEND_MSG + (msg->channel * IOP_MSG_LEN); 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) { 36362306a36Sopenharmony_ci iop_writeb(iop, offset, msg->message[i]); 36462306a36Sopenharmony_ci } 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci iop_writeb(iop, IOP_ADDR_SEND_STATE + msg->channel, IOP_MSG_NEW); 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci iop_interrupt(iop); 36962306a36Sopenharmony_ci} 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci/* 37262306a36Sopenharmony_ci * Handle sending a message on a channel that 37362306a36Sopenharmony_ci * has gone into the IOP_MSG_COMPLETE state. 37462306a36Sopenharmony_ci */ 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_cistatic void iop_handle_send(uint iop_num, uint chan) 37762306a36Sopenharmony_ci{ 37862306a36Sopenharmony_ci volatile struct mac_iop *iop = iop_base[iop_num]; 37962306a36Sopenharmony_ci struct iop_msg *msg; 38062306a36Sopenharmony_ci int i,offset; 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci iop_writeb(iop, IOP_ADDR_SEND_STATE + chan, IOP_MSG_IDLE); 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci if (!(msg = iop_send_queue[iop_num][chan])) return; 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci msg->status = IOP_MSGSTATUS_COMPLETE; 38762306a36Sopenharmony_ci offset = IOP_ADDR_SEND_MSG + (chan * IOP_MSG_LEN); 38862306a36Sopenharmony_ci for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) { 38962306a36Sopenharmony_ci msg->reply[i] = iop_readb(iop, offset); 39062306a36Sopenharmony_ci } 39162306a36Sopenharmony_ci iop_pr_debug("iop_num %d chan %d reply %*ph\n", 39262306a36Sopenharmony_ci iop_num, chan, IOP_MSG_LEN, msg->reply); 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci if (msg->handler) (*msg->handler)(msg); 39562306a36Sopenharmony_ci msg->status = IOP_MSGSTATUS_UNUSED; 39662306a36Sopenharmony_ci msg = msg->next; 39762306a36Sopenharmony_ci iop_send_queue[iop_num][chan] = msg; 39862306a36Sopenharmony_ci if (msg && iop_readb(iop, IOP_ADDR_SEND_STATE + chan) == IOP_MSG_IDLE) 39962306a36Sopenharmony_ci iop_do_send(msg); 40062306a36Sopenharmony_ci} 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci/* 40362306a36Sopenharmony_ci * Handle reception of a message on a channel that has 40462306a36Sopenharmony_ci * gone into the IOP_MSG_NEW state. 40562306a36Sopenharmony_ci */ 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_cistatic void iop_handle_recv(uint iop_num, uint chan) 40862306a36Sopenharmony_ci{ 40962306a36Sopenharmony_ci volatile struct mac_iop *iop = iop_base[iop_num]; 41062306a36Sopenharmony_ci int i,offset; 41162306a36Sopenharmony_ci struct iop_msg *msg; 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_ci msg = iop_get_unused_msg(); 41462306a36Sopenharmony_ci msg->iop_num = iop_num; 41562306a36Sopenharmony_ci msg->channel = chan; 41662306a36Sopenharmony_ci msg->status = IOP_MSGSTATUS_UNSOL; 41762306a36Sopenharmony_ci msg->handler = iop_listeners[iop_num][chan].handler; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci offset = IOP_ADDR_RECV_MSG + (chan * IOP_MSG_LEN); 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci for (i = 0 ; i < IOP_MSG_LEN ; i++, offset++) { 42262306a36Sopenharmony_ci msg->message[i] = iop_readb(iop, offset); 42362306a36Sopenharmony_ci } 42462306a36Sopenharmony_ci iop_pr_debug("iop_num %d chan %d message %*ph\n", 42562306a36Sopenharmony_ci iop_num, chan, IOP_MSG_LEN, msg->message); 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci iop_writeb(iop, IOP_ADDR_RECV_STATE + chan, IOP_MSG_RCVD); 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci /* If there is a listener, call it now. Otherwise complete */ 43062306a36Sopenharmony_ci /* the message ourselves to avoid possible stalls. */ 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci if (msg->handler) { 43362306a36Sopenharmony_ci (*msg->handler)(msg); 43462306a36Sopenharmony_ci } else { 43562306a36Sopenharmony_ci memset(msg->reply, 0, IOP_MSG_LEN); 43662306a36Sopenharmony_ci iop_complete_message(msg); 43762306a36Sopenharmony_ci } 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci/* 44162306a36Sopenharmony_ci * Send a message 44262306a36Sopenharmony_ci * 44362306a36Sopenharmony_ci * The message is placed at the end of the send queue. Afterwards if the 44462306a36Sopenharmony_ci * channel is idle we force an immediate send of the next message in the 44562306a36Sopenharmony_ci * queue. 44662306a36Sopenharmony_ci */ 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ciint iop_send_message(uint iop_num, uint chan, void *privdata, 44962306a36Sopenharmony_ci uint msg_len, __u8 *msg_data, 45062306a36Sopenharmony_ci void (*handler)(struct iop_msg *)) 45162306a36Sopenharmony_ci{ 45262306a36Sopenharmony_ci struct iop_msg *msg, *q; 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return -EINVAL; 45562306a36Sopenharmony_ci if (chan >= NUM_IOP_CHAN) return -EINVAL; 45662306a36Sopenharmony_ci if (msg_len > IOP_MSG_LEN) return -EINVAL; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci msg = iop_get_unused_msg(); 45962306a36Sopenharmony_ci if (!msg) return -ENOMEM; 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci msg->next = NULL; 46262306a36Sopenharmony_ci msg->status = IOP_MSGSTATUS_WAITING; 46362306a36Sopenharmony_ci msg->iop_num = iop_num; 46462306a36Sopenharmony_ci msg->channel = chan; 46562306a36Sopenharmony_ci msg->caller_priv = privdata; 46662306a36Sopenharmony_ci memcpy(msg->message, msg_data, msg_len); 46762306a36Sopenharmony_ci msg->handler = handler; 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci if (!(q = iop_send_queue[iop_num][chan])) { 47062306a36Sopenharmony_ci iop_send_queue[iop_num][chan] = msg; 47162306a36Sopenharmony_ci iop_do_send(msg); 47262306a36Sopenharmony_ci } else { 47362306a36Sopenharmony_ci while (q->next) q = q->next; 47462306a36Sopenharmony_ci q->next = msg; 47562306a36Sopenharmony_ci } 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci return 0; 47862306a36Sopenharmony_ci} 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ci/* 48162306a36Sopenharmony_ci * Upload code to the shared RAM of an IOP. 48262306a36Sopenharmony_ci */ 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_civoid iop_upload_code(uint iop_num, __u8 *code_start, 48562306a36Sopenharmony_ci uint code_len, __u16 shared_ram_start) 48662306a36Sopenharmony_ci{ 48762306a36Sopenharmony_ci if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return; 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci iop_loadaddr(iop_base[iop_num], shared_ram_start); 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci while (code_len--) { 49262306a36Sopenharmony_ci iop_base[iop_num]->ram_data = *code_start++; 49362306a36Sopenharmony_ci } 49462306a36Sopenharmony_ci} 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci/* 49762306a36Sopenharmony_ci * Download code from the shared RAM of an IOP. 49862306a36Sopenharmony_ci */ 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_civoid iop_download_code(uint iop_num, __u8 *code_start, 50162306a36Sopenharmony_ci uint code_len, __u16 shared_ram_start) 50262306a36Sopenharmony_ci{ 50362306a36Sopenharmony_ci if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return; 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci iop_loadaddr(iop_base[iop_num], shared_ram_start); 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci while (code_len--) { 50862306a36Sopenharmony_ci *code_start++ = iop_base[iop_num]->ram_data; 50962306a36Sopenharmony_ci } 51062306a36Sopenharmony_ci} 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci/* 51362306a36Sopenharmony_ci * Compare the code in the shared RAM of an IOP with a copy in system memory 51462306a36Sopenharmony_ci * and return 0 on match or the first nonmatching system memory address on 51562306a36Sopenharmony_ci * failure. 51662306a36Sopenharmony_ci */ 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci__u8 *iop_compare_code(uint iop_num, __u8 *code_start, 51962306a36Sopenharmony_ci uint code_len, __u16 shared_ram_start) 52062306a36Sopenharmony_ci{ 52162306a36Sopenharmony_ci if ((iop_num >= NUM_IOPS) || !iop_base[iop_num]) return code_start; 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci iop_loadaddr(iop_base[iop_num], shared_ram_start); 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci while (code_len--) { 52662306a36Sopenharmony_ci if (*code_start != iop_base[iop_num]->ram_data) { 52762306a36Sopenharmony_ci return code_start; 52862306a36Sopenharmony_ci } 52962306a36Sopenharmony_ci code_start++; 53062306a36Sopenharmony_ci } 53162306a36Sopenharmony_ci return (__u8 *) 0; 53262306a36Sopenharmony_ci} 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci/* 53562306a36Sopenharmony_ci * Handle an ISM IOP interrupt 53662306a36Sopenharmony_ci */ 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ciirqreturn_t iop_ism_irq(int irq, void *dev_id) 53962306a36Sopenharmony_ci{ 54062306a36Sopenharmony_ci uint iop_num = (uint) dev_id; 54162306a36Sopenharmony_ci volatile struct mac_iop *iop = iop_base[iop_num]; 54262306a36Sopenharmony_ci int i,state; 54362306a36Sopenharmony_ci u8 events = iop->status_ctrl & (IOP_INT0 | IOP_INT1); 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci do { 54662306a36Sopenharmony_ci iop_pr_debug("iop_num %d status %02X\n", iop_num, 54762306a36Sopenharmony_ci iop->status_ctrl); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci /* INT0 indicates state change on an outgoing message channel */ 55062306a36Sopenharmony_ci if (events & IOP_INT0) { 55162306a36Sopenharmony_ci iop->status_ctrl = IOP_INT0 | IOP_RUN | IOP_AUTOINC; 55262306a36Sopenharmony_ci for (i = 0; i < NUM_IOP_CHAN; i++) { 55362306a36Sopenharmony_ci state = iop_readb(iop, IOP_ADDR_SEND_STATE + i); 55462306a36Sopenharmony_ci if (state == IOP_MSG_COMPLETE) 55562306a36Sopenharmony_ci iop_handle_send(iop_num, i); 55662306a36Sopenharmony_ci else if (state != IOP_MSG_IDLE) 55762306a36Sopenharmony_ci iop_pr_debug("chan %d send state %02X\n", 55862306a36Sopenharmony_ci i, state); 55962306a36Sopenharmony_ci } 56062306a36Sopenharmony_ci } 56162306a36Sopenharmony_ci 56262306a36Sopenharmony_ci /* INT1 for incoming messages */ 56362306a36Sopenharmony_ci if (events & IOP_INT1) { 56462306a36Sopenharmony_ci iop->status_ctrl = IOP_INT1 | IOP_RUN | IOP_AUTOINC; 56562306a36Sopenharmony_ci for (i = 0; i < NUM_IOP_CHAN; i++) { 56662306a36Sopenharmony_ci state = iop_readb(iop, IOP_ADDR_RECV_STATE + i); 56762306a36Sopenharmony_ci if (state == IOP_MSG_NEW) 56862306a36Sopenharmony_ci iop_handle_recv(iop_num, i); 56962306a36Sopenharmony_ci else if (state != IOP_MSG_IDLE) 57062306a36Sopenharmony_ci iop_pr_debug("chan %d recv state %02X\n", 57162306a36Sopenharmony_ci i, state); 57262306a36Sopenharmony_ci } 57362306a36Sopenharmony_ci } 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci events = iop->status_ctrl & (IOP_INT0 | IOP_INT1); 57662306a36Sopenharmony_ci } while (events); 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci return IRQ_HANDLED; 57962306a36Sopenharmony_ci} 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_civoid iop_ism_irq_poll(uint iop_num) 58262306a36Sopenharmony_ci{ 58362306a36Sopenharmony_ci unsigned long flags; 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci local_irq_save(flags); 58662306a36Sopenharmony_ci iop_ism_irq(0, (void *)iop_num); 58762306a36Sopenharmony_ci local_irq_restore(flags); 58862306a36Sopenharmony_ci} 589