2 ** -----------------------------------------------------------------------------
4 ** Perle Specialix driver for Linux
5 ** Ported from existing RIO Driver for SCO sources.
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ** Last Modified : 11/6/98 10:33:44
26 ** Retrieved : 11/6/98 10:33:49
28 ** ident @(#)riointr.c 1.2
30 ** -----------------------------------------------------------------------------
33 static char *_riointr_c_sccs_ = "@(#)riointr.c 1.2";
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
43 #include <asm/system.h>
44 #include <asm/string.h>
45 #include <asm/uaccess.h>
47 #include <linux/termios.h>
48 #include <linux/serial.h>
50 #include <linux/generic_serial.h>
52 #include <linux/delay.h>
54 #include "linux_compat.h"
55 #include "rio_linux.h"
82 static void RIOReceive(struct rio_info *, struct Port *);
85 static char *firstchars(char *p, int nch)
87 static char buf[2][128];
90 memcpy(buf[t], p, nch);
96 #define INCR( P, I ) ((P) = (((P)+(I)) & p->RIOBufferMask))
97 /* Enable and start the transmission of packets */
98 void RIOTxEnable(char *en)
102 struct tty_struct *tty;
104 struct PKT __iomem *PacketP;
107 PortP = (struct Port *) en;
108 p = (struct rio_info *) PortP->p;
112 rio_dprintk(RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n", PortP->PortNum, PortP->gs.xmit_cnt);
114 if (!PortP->gs.xmit_cnt)
118 /* This routine is an order of magnitude simpler than the specialix
119 version. One of the disadvantages is that this version will send
120 an incomplete packet (usually 64 bytes instead of 72) once for
121 every 4k worth of data. Let's just say that this won't influence
122 performance significantly..... */
124 rio_spin_lock_irqsave(&PortP->portSem, flags);
126 while (can_add_transmit(&PacketP, PortP)) {
127 c = PortP->gs.xmit_cnt;
128 if (c > PKT_MAX_DATA_LEN)
129 c = PKT_MAX_DATA_LEN;
131 /* Don't copy past the end of the source buffer */
132 if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail)
133 c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
137 t = (c > 10) ? 10 : c;
139 rio_dprintk(RIO_DEBUG_INTR, "rio: tx port %d: copying %d chars: %s - %s\n", PortP->PortNum, c, firstchars(PortP->gs.xmit_buf + PortP->gs.xmit_tail, t), firstchars(PortP->gs.xmit_buf + PortP->gs.xmit_tail + c - t, t));
141 /* If for one reason or another, we can't copy more data,
146 rio_memcpy_toio(PortP->HostP->Caddr, PacketP->data, PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
149 writeb(c, &(PacketP->len));
150 if (!(PortP->State & RIO_DELETED)) {
153 ** Count chars tx'd for port statistics reporting
155 if (PortP->statsGather)
158 PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE - 1);
159 PortP->gs.xmit_cnt -= c;
162 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
164 if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2 * PKT_MAX_DATA_LEN))
165 tty_wakeup(PortP->gs.tty);
171 ** RIO Host Service routine. Does all the work traditionally associated with an
178 void RIOServiceHost(struct rio_info *p, struct Host *HostP)
180 rio_spin_lock(&HostP->HostLock);
181 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
183 rio_spin_unlock(&HostP->HostLock);
184 if ((t++ % 200) == 0)
185 rio_dprintk(RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int) HostP->Flags);
188 rio_spin_unlock(&HostP->HostLock);
190 if (readw(&HostP->ParmMapP->rup_intr)) {
191 writew(0, &HostP->ParmMapP->rup_intr);
194 rio_dprintk(RIO_DEBUG_INTR, "rio: RUP interrupt on host %Zd\n", HostP - p->RIOHosts);
195 RIOPollHostCommands(p, HostP);
198 if (readw(&HostP->ParmMapP->rx_intr)) {
201 writew(0, &HostP->ParmMapP->rx_intr);
205 rio_dprintk(RIO_DEBUG_INTR, "rio: RX interrupt on host %Zd\n", HostP - p->RIOHosts);
207 ** Loop through every port. If the port is mapped into
208 ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
209 ** worth checking. If the port isn't open, grab any packets
210 ** hanging on its receive queue and stuff them on the free
211 ** list; check for commands on the way.
213 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
214 struct Port *PortP = p->RIOPortp[port];
215 struct tty_struct *ttyP;
216 struct PKT __iomem *PacketP;
219 ** not mapped in - most of the RIOPortp[] information
220 ** has not been set up!
221 ** Optimise: ports come in bundles of eight.
223 if (!PortP->Mapped) {
225 continue; /* with the next port */
229 ** If the host board isn't THIS host board, check the next one.
230 ** optimise: ports come in bundles of eight.
232 if (PortP->HostP != HostP) {
238 ** Let us see - is the port open? If not, then don't service it.
240 if (!(PortP->PortState & PORT_ISOPEN)) {
245 ** find corresponding tty structure. The process of mapping
246 ** the ports puts these here.
248 ttyP = PortP->gs.tty;
251 ** Lock the port before we begin working on it.
253 rio_spin_lock(&PortP->portSem);
256 ** Process received data if there is any.
258 if (can_remove_receive(&PacketP, PortP))
259 RIOReceive(p, PortP);
262 ** If there is no data left to be read from the port, and
263 ** it's handshake bit is set, then we must clear the handshake,
264 ** so that that downstream RTA is re-enabled.
266 if (!can_remove_receive(&PacketP, PortP) && (readw(&PortP->PhbP->handshake) == PHB_HANDSHAKE_SET)) {
268 ** MAGIC! ( Basically, handshake the RX buffer, so that
269 ** the RTAs upstream can be re-enabled. )
271 rio_dprintk(RIO_DEBUG_INTR, "Set RX handshake bit\n");
272 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &PortP->PhbP->handshake);
274 rio_spin_unlock(&PortP->portSem);
278 if (readw(&HostP->ParmMapP->tx_intr)) {
281 writew(0, &HostP->ParmMapP->tx_intr);
285 rio_dprintk(RIO_DEBUG_INTR, "rio: TX interrupt on host %Zd\n", HostP - p->RIOHosts);
288 ** Loop through every port.
289 ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
290 ** associated ) then it is worth checking.
292 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
293 struct Port *PortP = p->RIOPortp[port];
294 struct tty_struct *ttyP;
295 struct PKT __iomem *PacketP;
298 ** not mapped in - most of the RIOPortp[] information
299 ** has not been set up!
301 if (!PortP->Mapped) {
303 continue; /* with the next port */
307 ** If the host board isn't running, then its data structures
308 ** are no use to us - continue quietly.
310 if (PortP->HostP != HostP) {
312 continue; /* with the next port */
316 ** Let us see - is the port open? If not, then don't service it.
318 if (!(PortP->PortState & PORT_ISOPEN)) {
322 rio_dprintk(RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
324 ** Lock the port before we begin working on it.
326 rio_spin_lock(&PortP->portSem);
329 ** If we can't add anything to the transmit queue, then
330 ** we need do none of this processing.
332 if (!can_add_transmit(&PacketP, PortP)) {
333 rio_dprintk(RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
334 rio_spin_unlock(&PortP->portSem);
339 ** find corresponding tty structure. The process of mapping
340 ** the ports puts these here.
342 ttyP = PortP->gs.tty;
343 /* If ttyP is NULL, the port is getting closed. Forget about it. */
345 rio_dprintk(RIO_DEBUG_INTR, "no tty, so skipping.\n");
346 rio_spin_unlock(&PortP->portSem);
350 ** If there is more room available we start up the transmit
351 ** data process again. This can be direct I/O, if the cookmode
352 ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
353 ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
354 ** characters via the line discipline. We must always call
355 ** the line discipline,
356 ** so that user input characters can be echoed correctly.
359 ** With the advent of double buffering, we now see if
360 ** TxBufferOut-In is non-zero. If so, then we copy a packet
361 ** to the output place, and set it going. If this empties
362 ** the buffer, then we must issue a wakeup( ) on OUT.
363 ** If it frees space in the buffer then we must issue
364 ** a wakeup( ) on IN.
366 ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
367 ** have to send a WFLUSH command down the PHB, to mark the
368 ** end point of a WFLUSH. We also need to clear out any
369 ** data from the double buffer! ( note that WflushFlag is a
370 ** *count* of the number of WFLUSH commands outstanding! )
372 ** ++++ And there's more!
373 ** If an RTA is powered off, then on again, and rebooted,
374 ** whilst it has ports open, then we need to re-open the ports.
375 ** ( reasonable enough ). We can't do this when we spot the
376 ** re-boot, in interrupt time, because the queue is probably
377 ** full. So, when we come in here, we need to test if any
378 ** ports are in this condition, and re-open the port before
379 ** we try to send any more data to it. Now, the re-booted
380 ** RTA will be discarding packets from the PHB until it
381 ** receives this open packet, but don't worry tooo much
382 ** about that. The one thing that is interesting is the
383 ** combination of this effect and the WFLUSH effect!
385 /* For now don't handle RTA reboots. -- REW.
386 Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
387 if (PortP->MagicFlags) {
388 if (PortP->MagicFlags & MAGIC_REBOOT) {
390 ** well, the RTA has been rebooted, and there is room
391 ** on its queue to add the open packet that is required.
393 ** The messy part of this line is trying to decide if
394 ** we need to call the Param function as a tty or as
396 ** DONT USE CLOCAL AS A TEST FOR THIS!
398 ** If we can't param the port, then move on to the
401 PortP->InUse = NOT_INUSE;
403 rio_spin_unlock(&PortP->portSem);
404 if (RIOParam(PortP, OPEN, ((PortP->Cor2Copy & (COR2_RTSFLOW | COR2_CTSFLOW)) == (COR2_RTSFLOW | COR2_CTSFLOW)) ? 1 : 0, DONT_SLEEP) == RIO_FAIL) {
405 continue; /* with next port */
407 rio_spin_lock(&PortP->portSem);
408 PortP->MagicFlags &= ~MAGIC_REBOOT;
412 ** As mentioned above, this is a tacky hack to cope
415 if (PortP->WflushFlag) {
416 rio_dprintk(RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
419 rio_dprintk(RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
422 while (PortP->WflushFlag && can_add_transmit(&PacketP, PortP) && (PortP->InUse == NOT_INUSE)) {
424 struct PktCmd __iomem *PktCmdP;
426 rio_dprintk(RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
428 ** make it look just like a WFLUSH command
430 PktCmdP = (struct PktCmd __iomem *) &PacketP->data[0];
432 writeb(WFLUSH, &PktCmdP->Command);
434 p = PortP->HostPort % (u16) PORTS_PER_RTA;
437 ** If second block of ports for 16 port RTA, add 8
440 if (PortP->SecondBlock)
443 writeb(p, &PktCmdP->PhbNum);
446 ** to make debuggery easier
448 writeb('W', &PacketP->data[2]);
449 writeb('F', &PacketP->data[3]);
450 writeb('L', &PacketP->data[4]);
451 writeb('U', &PacketP->data[5]);
452 writeb('S', &PacketP->data[6]);
453 writeb('H', &PacketP->data[7]);
454 writeb(' ', &PacketP->data[8]);
455 writeb('0' + PortP->WflushFlag, &PacketP->data[9]);
456 writeb(' ', &PacketP->data[10]);
457 writeb(' ', &PacketP->data[11]);
458 writeb('\0', &PacketP->data[12]);
461 ** its two bytes long!
463 writeb(PKT_CMD_BIT | 2, &PacketP->len);
468 if (!(PortP->State & RIO_DELETED)) {
471 ** Count chars tx'd for port statistics reporting
473 if (PortP->statsGather)
477 if (--(PortP->WflushFlag) == 0) {
478 PortP->MagicFlags &= ~MAGIC_FLUSH;
481 rio_dprintk(RIO_DEBUG_INTR, "Wflush count now stands at %d\n", PortP->WflushFlag);
483 if (PortP->MagicFlags & MORE_OUTPUT_EYGOR) {
484 if (PortP->MagicFlags & MAGIC_FLUSH) {
485 PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
487 if (!can_add_transmit(&PacketP, PortP)) {
488 rio_spin_unlock(&PortP->portSem);
491 rio_spin_unlock(&PortP->portSem);
492 RIOTxEnable((char *) PortP);
493 rio_spin_lock(&PortP->portSem);
494 PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
501 ** If we can't add anything to the transmit queue, then
502 ** we need do none of the remaining processing.
504 if (!can_add_transmit(&PacketP, PortP)) {
505 rio_spin_unlock(&PortP->portSem);
509 rio_spin_unlock(&PortP->portSem);
510 RIOTxEnable((char *) PortP);
516 ** Routine for handling received data for tty drivers
518 static void RIOReceive(struct rio_info *p, struct Port *PortP)
520 struct tty_struct *TtyP;
521 unsigned short transCount;
522 struct PKT __iomem *PacketP;
523 register unsigned int DataCnt;
524 unsigned char __iomem *ptr;
528 static int intCount, RxIntCnt;
531 ** The receive data process is to remove packets from the
532 ** PHB until there aren't any more or the current cblock
533 ** is full. When this occurs, there will be some left over
534 ** data in the packet, that we must do something with.
535 ** As we haven't unhooked the packet from the read list
536 ** yet, we can just leave the packet there, having first
537 ** made a note of how far we got. This means that we need
538 ** a pointer per port saying where we start taking the
539 ** data from - this will normally be zero, but when we
540 ** run out of space it will be set to the offset of the
541 ** next byte to copy from the packet data area. The packet
542 ** length field is decremented by the number of bytes that
543 ** we successfully removed from the packet. When this reaches
544 ** zero, we reset the offset pointer to be zero, and free
545 ** the packet from the front of the queue.
550 TtyP = PortP->gs.tty;
552 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
556 if (PortP->State & RIO_THROTTLE_RX) {
557 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
561 if (PortP->State & RIO_DELETED) {
562 while (can_remove_receive(&PacketP, PortP)) {
563 remove_receive(PortP);
564 put_free_end(PortP->HostP, PacketP);
568 ** loop, just so long as:
569 ** i ) there's some data ( i.e. can_remove_receive )
570 ** ii ) we haven't been blocked
571 ** iii ) there's somewhere to put the data
572 ** iv ) we haven't outstayed our welcome
575 while (can_remove_receive(&PacketP, PortP)
580 ** check that it is not a command!
582 if (readb(&PacketP->len) & PKT_CMD_BIT) {
583 rio_dprintk(RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
584 /* rio_dprint(RIO_DEBUG_INTR, (" sysport = %d\n", p->RIOPortp->PortNum)); */
585 rio_dprintk(RIO_DEBUG_INTR, " dest_unit = %d\n", readb(&PacketP->dest_unit));
586 rio_dprintk(RIO_DEBUG_INTR, " dest_port = %d\n", readb(&PacketP->dest_port));
587 rio_dprintk(RIO_DEBUG_INTR, " src_unit = %d\n", readb(&PacketP->src_unit));
588 rio_dprintk(RIO_DEBUG_INTR, " src_port = %d\n", readb(&PacketP->src_port));
589 rio_dprintk(RIO_DEBUG_INTR, " len = %d\n", readb(&PacketP->len));
590 rio_dprintk(RIO_DEBUG_INTR, " control = %d\n", readb(&PacketP->control));
591 rio_dprintk(RIO_DEBUG_INTR, " csum = %d\n", readw(&PacketP->csum));
592 rio_dprintk(RIO_DEBUG_INTR, " data bytes: ");
593 for (DataCnt = 0; DataCnt < PKT_MAX_DATA_LEN; DataCnt++)
594 rio_dprintk(RIO_DEBUG_INTR, "%d\n", readb(&PacketP->data[DataCnt]));
595 remove_receive(PortP);
596 put_free_end(PortP->HostP, PacketP);
597 continue; /* with next packet */
601 ** How many characters can we move 'upstream' ?
603 ** Determine the minimum of the amount of data
604 ** available and the amount of space in which to
607 ** 1. Get the packet length by masking 'len'
608 ** for only the length bits.
609 ** 2. Available space is [buffer size] - [space used]
611 ** Transfer count is the minimum of packet length
612 ** and available space.
615 transCount = tty_buffer_request_room(TtyP, readb(&PacketP->len) & PKT_LEN_MASK);
616 rio_dprintk(RIO_DEBUG_REC, "port %d: Copy %d bytes\n", PortP->PortNum, transCount);
618 ** To use the following 'kkprintfs' for debugging - change the '#undef'
619 ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
622 ptr = (unsigned char __iomem *) PacketP->data + PortP->RxDataStart;
624 tty_prepare_flip_string(TtyP, &buf, transCount);
625 rio_memcpy_fromio(buf, ptr, transCount);
626 PortP->RxDataStart += transCount;
627 writeb(readb(&PacketP->len)-transCount, &PacketP->len);
628 copied += transCount;
632 if (readb(&PacketP->len) == 0) {
634 ** If we have emptied the packet, then we can
635 ** free it, and reset the start pointer for
638 remove_receive(PortP);
639 put_free_end(PortP->HostP, PacketP);
640 PortP->RxDataStart = 0;
645 rio_dprintk(RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
646 tty_flip_buffer_push(TtyP);