OMAP3: PM: Prevented DVFS state switches when enabling off-mode
[linux-ginger.git] / drivers / char / rio / riointr.c
blob71f87600907ce04f1da12bb7d3279650a19c2857
1 /*
2 ** -----------------------------------------------------------------------------
3 **
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.
23 ** Module : riointr.c
24 ** SID : 1.2
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 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/errno.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
38 #include <asm/io.h>
39 #include <asm/system.h>
40 #include <asm/string.h>
41 #include <asm/uaccess.h>
43 #include <linux/termios.h>
44 #include <linux/serial.h>
46 #include <linux/generic_serial.h>
48 #include <linux/delay.h>
50 #include "linux_compat.h"
51 #include "rio_linux.h"
52 #include "pkt.h"
53 #include "daemon.h"
54 #include "rio.h"
55 #include "riospace.h"
56 #include "cmdpkt.h"
57 #include "map.h"
58 #include "rup.h"
59 #include "port.h"
60 #include "riodrvr.h"
61 #include "rioinfo.h"
62 #include "func.h"
63 #include "errors.h"
64 #include "pci.h"
66 #include "parmmap.h"
67 #include "unixrup.h"
68 #include "board.h"
69 #include "host.h"
70 #include "phb.h"
71 #include "link.h"
72 #include "cmdblk.h"
73 #include "route.h"
74 #include "cirrus.h"
75 #include "rioioctl.h"
78 static void RIOReceive(struct rio_info *, struct Port *);
81 static char *firstchars(char *p, int nch)
83 static char buf[2][128];
84 static int t = 0;
85 t = !t;
86 memcpy(buf[t], p, nch);
87 buf[t][nch] = 0;
88 return buf[t];
92 #define INCR( P, I ) ((P) = (((P)+(I)) & p->RIOBufferMask))
93 /* Enable and start the transmission of packets */
94 void RIOTxEnable(char *en)
96 struct Port *PortP;
97 struct rio_info *p;
98 struct tty_struct *tty;
99 int c;
100 struct PKT __iomem *PacketP;
101 unsigned long flags;
103 PortP = (struct Port *) en;
104 p = (struct rio_info *) PortP->p;
105 tty = PortP->gs.port.tty;
108 rio_dprintk(RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n", PortP->PortNum, PortP->gs.xmit_cnt);
110 if (!PortP->gs.xmit_cnt)
111 return;
114 /* This routine is an order of magnitude simpler than the specialix
115 version. One of the disadvantages is that this version will send
116 an incomplete packet (usually 64 bytes instead of 72) once for
117 every 4k worth of data. Let's just say that this won't influence
118 performance significantly..... */
120 rio_spin_lock_irqsave(&PortP->portSem, flags);
122 while (can_add_transmit(&PacketP, PortP)) {
123 c = PortP->gs.xmit_cnt;
124 if (c > PKT_MAX_DATA_LEN)
125 c = PKT_MAX_DATA_LEN;
127 /* Don't copy past the end of the source buffer */
128 if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail)
129 c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
132 int t;
133 t = (c > 10) ? 10 : c;
135 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));
137 /* If for one reason or another, we can't copy more data,
138 we're done! */
139 if (c == 0)
140 break;
142 rio_memcpy_toio(PortP->HostP->Caddr, PacketP->data, PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
143 /* udelay (1); */
145 writeb(c, &(PacketP->len));
146 if (!(PortP->State & RIO_DELETED)) {
147 add_transmit(PortP);
149 ** Count chars tx'd for port statistics reporting
151 if (PortP->statsGather)
152 PortP->txchars += c;
154 PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE - 1);
155 PortP->gs.xmit_cnt -= c;
158 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
160 if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2 * PKT_MAX_DATA_LEN))
161 tty_wakeup(PortP->gs.port.tty);
167 ** RIO Host Service routine. Does all the work traditionally associated with an
168 ** interrupt.
170 static int RupIntr;
171 static int RxIntr;
172 static int TxIntr;
174 void RIOServiceHost(struct rio_info *p, struct Host *HostP)
176 rio_spin_lock(&HostP->HostLock);
177 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
178 static int t = 0;
179 rio_spin_unlock(&HostP->HostLock);
180 if ((t++ % 200) == 0)
181 rio_dprintk(RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int) HostP->Flags);
182 return;
184 rio_spin_unlock(&HostP->HostLock);
186 if (readw(&HostP->ParmMapP->rup_intr)) {
187 writew(0, &HostP->ParmMapP->rup_intr);
188 p->RIORupCount++;
189 RupIntr++;
190 rio_dprintk(RIO_DEBUG_INTR, "rio: RUP interrupt on host %Zd\n", HostP - p->RIOHosts);
191 RIOPollHostCommands(p, HostP);
194 if (readw(&HostP->ParmMapP->rx_intr)) {
195 int port;
197 writew(0, &HostP->ParmMapP->rx_intr);
198 p->RIORxCount++;
199 RxIntr++;
201 rio_dprintk(RIO_DEBUG_INTR, "rio: RX interrupt on host %Zd\n", HostP - p->RIOHosts);
203 ** Loop through every port. If the port is mapped into
204 ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
205 ** worth checking. If the port isn't open, grab any packets
206 ** hanging on its receive queue and stuff them on the free
207 ** list; check for commands on the way.
209 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
210 struct Port *PortP = p->RIOPortp[port];
211 struct tty_struct *ttyP;
212 struct PKT __iomem *PacketP;
215 ** not mapped in - most of the RIOPortp[] information
216 ** has not been set up!
217 ** Optimise: ports come in bundles of eight.
219 if (!PortP->Mapped) {
220 port += 7;
221 continue; /* with the next port */
225 ** If the host board isn't THIS host board, check the next one.
226 ** optimise: ports come in bundles of eight.
228 if (PortP->HostP != HostP) {
229 port += 7;
230 continue;
234 ** Let us see - is the port open? If not, then don't service it.
236 if (!(PortP->PortState & PORT_ISOPEN)) {
237 continue;
241 ** find corresponding tty structure. The process of mapping
242 ** the ports puts these here.
244 ttyP = PortP->gs.port.tty;
247 ** Lock the port before we begin working on it.
249 rio_spin_lock(&PortP->portSem);
252 ** Process received data if there is any.
254 if (can_remove_receive(&PacketP, PortP))
255 RIOReceive(p, PortP);
258 ** If there is no data left to be read from the port, and
259 ** it's handshake bit is set, then we must clear the handshake,
260 ** so that that downstream RTA is re-enabled.
262 if (!can_remove_receive(&PacketP, PortP) && (readw(&PortP->PhbP->handshake) == PHB_HANDSHAKE_SET)) {
264 ** MAGIC! ( Basically, handshake the RX buffer, so that
265 ** the RTAs upstream can be re-enabled. )
267 rio_dprintk(RIO_DEBUG_INTR, "Set RX handshake bit\n");
268 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &PortP->PhbP->handshake);
270 rio_spin_unlock(&PortP->portSem);
274 if (readw(&HostP->ParmMapP->tx_intr)) {
275 int port;
277 writew(0, &HostP->ParmMapP->tx_intr);
279 p->RIOTxCount++;
280 TxIntr++;
281 rio_dprintk(RIO_DEBUG_INTR, "rio: TX interrupt on host %Zd\n", HostP - p->RIOHosts);
284 ** Loop through every port.
285 ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
286 ** associated ) then it is worth checking.
288 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
289 struct Port *PortP = p->RIOPortp[port];
290 struct tty_struct *ttyP;
291 struct PKT __iomem *PacketP;
294 ** not mapped in - most of the RIOPortp[] information
295 ** has not been set up!
297 if (!PortP->Mapped) {
298 port += 7;
299 continue; /* with the next port */
303 ** If the host board isn't running, then its data structures
304 ** are no use to us - continue quietly.
306 if (PortP->HostP != HostP) {
307 port += 7;
308 continue; /* with the next port */
312 ** Let us see - is the port open? If not, then don't service it.
314 if (!(PortP->PortState & PORT_ISOPEN)) {
315 continue;
318 rio_dprintk(RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
320 ** Lock the port before we begin working on it.
322 rio_spin_lock(&PortP->portSem);
325 ** If we can't add anything to the transmit queue, then
326 ** we need do none of this processing.
328 if (!can_add_transmit(&PacketP, PortP)) {
329 rio_dprintk(RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
330 rio_spin_unlock(&PortP->portSem);
331 continue;
335 ** find corresponding tty structure. The process of mapping
336 ** the ports puts these here.
338 ttyP = PortP->gs.port.tty;
339 /* If ttyP is NULL, the port is getting closed. Forget about it. */
340 if (!ttyP) {
341 rio_dprintk(RIO_DEBUG_INTR, "no tty, so skipping.\n");
342 rio_spin_unlock(&PortP->portSem);
343 continue;
346 ** If there is more room available we start up the transmit
347 ** data process again. This can be direct I/O, if the cookmode
348 ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
349 ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
350 ** characters via the line discipline. We must always call
351 ** the line discipline,
352 ** so that user input characters can be echoed correctly.
354 ** ++++ Update +++++
355 ** With the advent of double buffering, we now see if
356 ** TxBufferOut-In is non-zero. If so, then we copy a packet
357 ** to the output place, and set it going. If this empties
358 ** the buffer, then we must issue a wakeup( ) on OUT.
359 ** If it frees space in the buffer then we must issue
360 ** a wakeup( ) on IN.
362 ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
363 ** have to send a WFLUSH command down the PHB, to mark the
364 ** end point of a WFLUSH. We also need to clear out any
365 ** data from the double buffer! ( note that WflushFlag is a
366 ** *count* of the number of WFLUSH commands outstanding! )
368 ** ++++ And there's more!
369 ** If an RTA is powered off, then on again, and rebooted,
370 ** whilst it has ports open, then we need to re-open the ports.
371 ** ( reasonable enough ). We can't do this when we spot the
372 ** re-boot, in interrupt time, because the queue is probably
373 ** full. So, when we come in here, we need to test if any
374 ** ports are in this condition, and re-open the port before
375 ** we try to send any more data to it. Now, the re-booted
376 ** RTA will be discarding packets from the PHB until it
377 ** receives this open packet, but don't worry tooo much
378 ** about that. The one thing that is interesting is the
379 ** combination of this effect and the WFLUSH effect!
381 /* For now don't handle RTA reboots. -- REW.
382 Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
383 if (PortP->MagicFlags) {
384 if (PortP->MagicFlags & MAGIC_REBOOT) {
386 ** well, the RTA has been rebooted, and there is room
387 ** on its queue to add the open packet that is required.
389 ** The messy part of this line is trying to decide if
390 ** we need to call the Param function as a tty or as
391 ** a modem.
392 ** DONT USE CLOCAL AS A TEST FOR THIS!
394 ** If we can't param the port, then move on to the
395 ** next port.
397 PortP->InUse = NOT_INUSE;
399 rio_spin_unlock(&PortP->portSem);
400 if (RIOParam(PortP, RIOC_OPEN, ((PortP->Cor2Copy & (RIOC_COR2_RTSFLOW | RIOC_COR2_CTSFLOW)) == (RIOC_COR2_RTSFLOW | RIOC_COR2_CTSFLOW)) ? 1 : 0, DONT_SLEEP) == RIO_FAIL)
401 continue; /* with next port */
402 rio_spin_lock(&PortP->portSem);
403 PortP->MagicFlags &= ~MAGIC_REBOOT;
407 ** As mentioned above, this is a tacky hack to cope
408 ** with WFLUSH
410 if (PortP->WflushFlag) {
411 rio_dprintk(RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
413 if (PortP->InUse)
414 rio_dprintk(RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
417 while (PortP->WflushFlag && can_add_transmit(&PacketP, PortP) && (PortP->InUse == NOT_INUSE)) {
418 int p;
419 struct PktCmd __iomem *PktCmdP;
421 rio_dprintk(RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
423 ** make it look just like a WFLUSH command
425 PktCmdP = (struct PktCmd __iomem *) &PacketP->data[0];
427 writeb(RIOC_WFLUSH, &PktCmdP->Command);
429 p = PortP->HostPort % (u16) PORTS_PER_RTA;
432 ** If second block of ports for 16 port RTA, add 8
433 ** to index 8-15.
435 if (PortP->SecondBlock)
436 p += PORTS_PER_RTA;
438 writeb(p, &PktCmdP->PhbNum);
441 ** to make debuggery easier
443 writeb('W', &PacketP->data[2]);
444 writeb('F', &PacketP->data[3]);
445 writeb('L', &PacketP->data[4]);
446 writeb('U', &PacketP->data[5]);
447 writeb('S', &PacketP->data[6]);
448 writeb('H', &PacketP->data[7]);
449 writeb(' ', &PacketP->data[8]);
450 writeb('0' + PortP->WflushFlag, &PacketP->data[9]);
451 writeb(' ', &PacketP->data[10]);
452 writeb(' ', &PacketP->data[11]);
453 writeb('\0', &PacketP->data[12]);
456 ** its two bytes long!
458 writeb(PKT_CMD_BIT | 2, &PacketP->len);
461 ** queue it!
463 if (!(PortP->State & RIO_DELETED)) {
464 add_transmit(PortP);
466 ** Count chars tx'd for port statistics reporting
468 if (PortP->statsGather)
469 PortP->txchars += 2;
472 if (--(PortP->WflushFlag) == 0) {
473 PortP->MagicFlags &= ~MAGIC_FLUSH;
476 rio_dprintk(RIO_DEBUG_INTR, "Wflush count now stands at %d\n", PortP->WflushFlag);
478 if (PortP->MagicFlags & MORE_OUTPUT_EYGOR) {
479 if (PortP->MagicFlags & MAGIC_FLUSH) {
480 PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
481 } else {
482 if (!can_add_transmit(&PacketP, PortP)) {
483 rio_spin_unlock(&PortP->portSem);
484 continue;
486 rio_spin_unlock(&PortP->portSem);
487 RIOTxEnable((char *) PortP);
488 rio_spin_lock(&PortP->portSem);
489 PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
496 ** If we can't add anything to the transmit queue, then
497 ** we need do none of the remaining processing.
499 if (!can_add_transmit(&PacketP, PortP)) {
500 rio_spin_unlock(&PortP->portSem);
501 continue;
504 rio_spin_unlock(&PortP->portSem);
505 RIOTxEnable((char *) PortP);
511 ** Routine for handling received data for tty drivers
513 static void RIOReceive(struct rio_info *p, struct Port *PortP)
515 struct tty_struct *TtyP;
516 unsigned short transCount;
517 struct PKT __iomem *PacketP;
518 register unsigned int DataCnt;
519 unsigned char __iomem *ptr;
520 unsigned char *buf;
521 int copied = 0;
523 static int intCount, RxIntCnt;
526 ** The receive data process is to remove packets from the
527 ** PHB until there aren't any more or the current cblock
528 ** is full. When this occurs, there will be some left over
529 ** data in the packet, that we must do something with.
530 ** As we haven't unhooked the packet from the read list
531 ** yet, we can just leave the packet there, having first
532 ** made a note of how far we got. This means that we need
533 ** a pointer per port saying where we start taking the
534 ** data from - this will normally be zero, but when we
535 ** run out of space it will be set to the offset of the
536 ** next byte to copy from the packet data area. The packet
537 ** length field is decremented by the number of bytes that
538 ** we successfully removed from the packet. When this reaches
539 ** zero, we reset the offset pointer to be zero, and free
540 ** the packet from the front of the queue.
543 intCount++;
545 TtyP = PortP->gs.port.tty;
546 if (!TtyP) {
547 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
548 return;
551 if (PortP->State & RIO_THROTTLE_RX) {
552 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
553 return;
556 if (PortP->State & RIO_DELETED) {
557 while (can_remove_receive(&PacketP, PortP)) {
558 remove_receive(PortP);
559 put_free_end(PortP->HostP, PacketP);
561 } else {
563 ** loop, just so long as:
564 ** i ) there's some data ( i.e. can_remove_receive )
565 ** ii ) we haven't been blocked
566 ** iii ) there's somewhere to put the data
567 ** iv ) we haven't outstayed our welcome
569 transCount = 1;
570 while (can_remove_receive(&PacketP, PortP)
571 && transCount) {
572 RxIntCnt++;
575 ** check that it is not a command!
577 if (readb(&PacketP->len) & PKT_CMD_BIT) {
578 rio_dprintk(RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
579 /* rio_dprint(RIO_DEBUG_INTR, (" sysport = %d\n", p->RIOPortp->PortNum)); */
580 rio_dprintk(RIO_DEBUG_INTR, " dest_unit = %d\n", readb(&PacketP->dest_unit));
581 rio_dprintk(RIO_DEBUG_INTR, " dest_port = %d\n", readb(&PacketP->dest_port));
582 rio_dprintk(RIO_DEBUG_INTR, " src_unit = %d\n", readb(&PacketP->src_unit));
583 rio_dprintk(RIO_DEBUG_INTR, " src_port = %d\n", readb(&PacketP->src_port));
584 rio_dprintk(RIO_DEBUG_INTR, " len = %d\n", readb(&PacketP->len));
585 rio_dprintk(RIO_DEBUG_INTR, " control = %d\n", readb(&PacketP->control));
586 rio_dprintk(RIO_DEBUG_INTR, " csum = %d\n", readw(&PacketP->csum));
587 rio_dprintk(RIO_DEBUG_INTR, " data bytes: ");
588 for (DataCnt = 0; DataCnt < PKT_MAX_DATA_LEN; DataCnt++)
589 rio_dprintk(RIO_DEBUG_INTR, "%d\n", readb(&PacketP->data[DataCnt]));
590 remove_receive(PortP);
591 put_free_end(PortP->HostP, PacketP);
592 continue; /* with next packet */
596 ** How many characters can we move 'upstream' ?
598 ** Determine the minimum of the amount of data
599 ** available and the amount of space in which to
600 ** put it.
602 ** 1. Get the packet length by masking 'len'
603 ** for only the length bits.
604 ** 2. Available space is [buffer size] - [space used]
606 ** Transfer count is the minimum of packet length
607 ** and available space.
610 transCount = tty_buffer_request_room(TtyP, readb(&PacketP->len) & PKT_LEN_MASK);
611 rio_dprintk(RIO_DEBUG_REC, "port %d: Copy %d bytes\n", PortP->PortNum, transCount);
613 ** To use the following 'kkprintfs' for debugging - change the '#undef'
614 ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
615 ** driver).
617 ptr = (unsigned char __iomem *) PacketP->data + PortP->RxDataStart;
619 tty_prepare_flip_string(TtyP, &buf, transCount);
620 rio_memcpy_fromio(buf, ptr, transCount);
621 PortP->RxDataStart += transCount;
622 writeb(readb(&PacketP->len)-transCount, &PacketP->len);
623 copied += transCount;
627 if (readb(&PacketP->len) == 0) {
629 ** If we have emptied the packet, then we can
630 ** free it, and reset the start pointer for
631 ** the next packet.
633 remove_receive(PortP);
634 put_free_end(PortP->HostP, PacketP);
635 PortP->RxDataStart = 0;
639 if (copied) {
640 rio_dprintk(RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
641 tty_flip_buffer_push(TtyP);
644 return;