Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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