1
2/* rio_linux.c -- Linux driver for the Specialix RIO series cards.
3 *
4 *
5 *   (C) 1999 R.E.Wolff@BitWizard.nl
6 *
7 * Specialix pays for the development and support of this driver.
8 * Please DO contact support@specialix.co.uk if you require
9 * support. But please read the documentation (rio.txt) first.
10 *
11 *
12 *
13 *      This program is free software; you can redistribute it and/or
14 *      modify it under the terms of the GNU General Public License as
15 *      published by the Free Software Foundation; either version 2 of
16 *      the License, or (at your option) any later version.
17 *
18 *      This program is distributed in the hope that it will be
19 *      useful, but WITHOUT ANY WARRANTY; without even the implied
20 *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 *      PURPOSE.  See the GNU General Public License for more details.
22 *
23 *      You should have received a copy of the GNU General Public
24 *      License along with this program; if not, write to the Free
25 *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
26 *      USA.
27 *
28 * Revision history:
29 * $Log: rio_linux.c,v $
30 * Revision 1.1.1.1  2007/08/03 18:52:29  rnuti
31 * Importing Linux MIPS Kernel 2.6.22
32 *
33 * Revision 1.1  1999/07/11 10:13:54  wolff
34 * Initial revision
35 *
36 * */
37
38#include <linux/module.h>
39#include <linux/kdev_t.h>
40#include <asm/io.h>
41#include <linux/kernel.h>
42#include <linux/sched.h>
43#include <linux/ioport.h>
44#include <linux/interrupt.h>
45#include <linux/errno.h>
46#include <linux/tty.h>
47#include <linux/tty_flip.h>
48#include <linux/mm.h>
49#include <linux/serial.h>
50#include <linux/fcntl.h>
51#include <linux/major.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/slab.h>
55#include <linux/miscdevice.h>
56#include <linux/init.h>
57
58#include <linux/generic_serial.h>
59#include <asm/uaccess.h>
60
61#include "linux_compat.h"
62#include "pkt.h"
63#include "daemon.h"
64#include "rio.h"
65#include "riospace.h"
66#include "cmdpkt.h"
67#include "map.h"
68#include "rup.h"
69#include "port.h"
70#include "riodrvr.h"
71#include "rioinfo.h"
72#include "func.h"
73#include "errors.h"
74#include "pci.h"
75
76#include "parmmap.h"
77#include "unixrup.h"
78#include "board.h"
79#include "host.h"
80#include "phb.h"
81#include "link.h"
82#include "cmdblk.h"
83#include "route.h"
84#include "cirrus.h"
85#include "rioioctl.h"
86#include "param.h"
87#include "protsts.h"
88#include "rioboard.h"
89
90
91#include "rio_linux.h"
92
93/* I don't think that this driver can handle more than 512 ports on
94one machine.  Specialix specifies max 4 boards in one machine. I don't
95know why. If you want to try anyway you'll have to increase the number
96of boards in rio.h.  You'll have to allocate more majors if you need
97more than 512 ports.... */
98
99#ifndef RIO_NORMAL_MAJOR0
100/* This allows overriding on the compiler commandline, or in a "major.h"
101   include or something like that */
102#define RIO_NORMAL_MAJOR0  154
103#define RIO_NORMAL_MAJOR1  156
104#endif
105
106#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
107#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
108#endif
109
110#ifndef RIO_WINDOW_LEN
111#define RIO_WINDOW_LEN 0x10000
112#endif
113
114
115/* Configurable options:
116   (Don't be too sure that it'll work if you toggle them) */
117
118/* Am I paranoid or not ? ;-) */
119#undef RIO_PARANOIA_CHECK
120
121
122/* 20 -> 2000 per second. The card should rate-limit interrupts at 1000
123   Hz, but it is user configurable. I don't recommend going above 1000
124   Hz. The interrupt ratelimit might trigger if the interrupt is
125   shared with a very active other device.
126   undef this if you want to disable the check....
127*/
128#define IRQ_RATE_LIMIT 200
129
130
131/* These constants are derived from SCO Source */
132static struct Conf
133 RIOConf = {
134	/* locator */ "RIO Config here",
135					/* startuptime */ HZ * 2,
136					/* how long to wait for card to run */
137				/* slowcook */ 0,
138				/* TRUE -> always use line disc. */
139				/* intrpolltime */ 1,
140				/* The frequency of OUR polls */
141				/* breakinterval */ 25,
142				/* timer */ 10,
143				/* mS */
144	/* RtaLoadBase */ 0x7000,
145	/* HostLoadBase */ 0x7C00,
146				/* XpHz */ 5,
147				/* number of Xprint hits per second */
148				/* XpCps */ 120,
149				/* Xprint characters per second */
150				/* XpOn */ "\033d#",
151				/* start Xprint for a wyse 60 */
152				/* XpOff */ "\024",
153				/* end Xprint for a wyse 60 */
154				/* MaxXpCps */ 2000,
155				/* highest Xprint speed */
156				/* MinXpCps */ 10,
157				/* slowest Xprint speed */
158				/* SpinCmds */ 1,
159				/* non-zero for mega fast boots */
160					/* First Addr */ 0x0A0000,
161					/* First address to look at */
162					/* Last Addr */ 0xFF0000,
163					/* Last address looked at */
164				/* BufferSize */ 1024,
165				/* Bytes per port of buffering */
166				/* LowWater */ 256,
167				/* how much data left before wakeup */
168				/* LineLength */ 80,
169				/* how wide is the console? */
170				/* CmdTimeout */ HZ,
171				/* how long a close command may take */
172};
173
174
175
176
177/* Function prototypes */
178
179static void rio_disable_tx_interrupts(void *ptr);
180static void rio_enable_tx_interrupts(void *ptr);
181static void rio_disable_rx_interrupts(void *ptr);
182static void rio_enable_rx_interrupts(void *ptr);
183static int rio_get_CD(void *ptr);
184static void rio_shutdown_port(void *ptr);
185static int rio_set_real_termios(void *ptr);
186static void rio_hungup(void *ptr);
187static void rio_close(void *ptr);
188static int rio_chars_in_buffer(void *ptr);
189static int rio_fw_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
190static int rio_init_drivers(void);
191
192static void my_hd(void *addr, int len);
193
194static struct tty_driver *rio_driver, *rio_driver2;
195
196/* The name "p" is a bit non-descript. But that's what the rio-lynxos
197sources use all over the place. */
198struct rio_info *p;
199
200int rio_debug;
201
202
203/* You can have the driver poll your card.
204    - Set rio_poll to 1 to poll every timer tick (10ms on Intel).
205      This is used when the card cannot use an interrupt for some reason.
206*/
207static int rio_poll = 1;
208
209
210/* These are the only open spaces in my computer. Yours may have more
211   or less.... */
212static int rio_probe_addrs[] = { 0xc0000, 0xd0000, 0xe0000 };
213
214#define NR_RIO_ADDRS ARRAY_SIZE(rio_probe_addrs)
215
216
217/* Set the mask to all-ones. This alas, only supports 32 interrupts.
218   Some architectures may need more. -- Changed to LONG to
219   support up to 64 bits on 64bit architectures. -- REW 20/06/99 */
220static long rio_irqmask = -1;
221
222MODULE_AUTHOR("Rogier Wolff <R.E.Wolff@bitwizard.nl>, Patrick van de Lageweg <patrick@bitwizard.nl>");
223MODULE_DESCRIPTION("RIO driver");
224MODULE_LICENSE("GPL");
225module_param(rio_poll, int, 0);
226module_param(rio_debug, int, 0644);
227module_param(rio_irqmask, long, 0);
228
229static struct real_driver rio_real_driver = {
230	rio_disable_tx_interrupts,
231	rio_enable_tx_interrupts,
232	rio_disable_rx_interrupts,
233	rio_enable_rx_interrupts,
234	rio_get_CD,
235	rio_shutdown_port,
236	rio_set_real_termios,
237	rio_chars_in_buffer,
238	rio_close,
239	rio_hungup,
240	NULL
241};
242
243/*
244 *  Firmware loader driver specific routines
245 *
246 */
247
248static const struct file_operations rio_fw_fops = {
249	.owner = THIS_MODULE,
250	.ioctl = rio_fw_ioctl,
251};
252
253static struct miscdevice rio_fw_device = {
254	RIOCTL_MISC_MINOR, "rioctl", &rio_fw_fops
255};
256
257
258
259
260
261#ifdef RIO_PARANOIA_CHECK
262
263/* This doesn't work. Who's paranoid around here? Not me! */
264
265static inline int rio_paranoia_check(struct rio_port const *port, char *name, const char *routine)
266{
267
268	static const char *badmagic = KERN_ERR "rio: Warning: bad rio port magic number for device %s in %s\n";
269	static const char *badinfo = KERN_ERR "rio: Warning: null rio port for device %s in %s\n";
270
271	if (!port) {
272		printk(badinfo, name, routine);
273		return 1;
274	}
275	if (port->magic != RIO_MAGIC) {
276		printk(badmagic, name, routine);
277		return 1;
278	}
279
280	return 0;
281}
282#else
283#define rio_paranoia_check(a,b,c) 0
284#endif
285
286
287#ifdef DEBUG
288static void my_hd(void *ad, int len)
289{
290	int i, j, ch;
291	unsigned char *addr = ad;
292
293	for (i = 0; i < len; i += 16) {
294		rio_dprintk(RIO_DEBUG_PARAM, "%08lx ", (unsigned long) addr + i);
295		for (j = 0; j < 16; j++) {
296			rio_dprintk(RIO_DEBUG_PARAM, "%02x %s", addr[j + i], (j == 7) ? " " : "");
297		}
298		for (j = 0; j < 16; j++) {
299			ch = addr[j + i];
300			rio_dprintk(RIO_DEBUG_PARAM, "%c", (ch < 0x20) ? '.' : ((ch > 0x7f) ? '.' : ch));
301		}
302		rio_dprintk(RIO_DEBUG_PARAM, "\n");
303	}
304}
305#else
306#define my_hd(ad,len) do{/* nothing*/ } while (0)
307#endif
308
309
310/* Delay a number of jiffies, allowing a signal to interrupt */
311int RIODelay(struct Port *PortP, int njiffies)
312{
313	func_enter();
314
315	rio_dprintk(RIO_DEBUG_DELAY, "delaying %d jiffies\n", njiffies);
316	msleep_interruptible(jiffies_to_msecs(njiffies));
317	func_exit();
318
319	if (signal_pending(current))
320		return RIO_FAIL;
321	else
322		return !RIO_FAIL;
323}
324
325
326/* Delay a number of jiffies, disallowing a signal to interrupt */
327int RIODelay_ni(struct Port *PortP, int njiffies)
328{
329	func_enter();
330
331	rio_dprintk(RIO_DEBUG_DELAY, "delaying %d jiffies (ni)\n", njiffies);
332	msleep(jiffies_to_msecs(njiffies));
333	func_exit();
334	return !RIO_FAIL;
335}
336
337void rio_copy_to_card(void *from, void __iomem *to, int len)
338{
339	rio_copy_toio(to, from, len);
340}
341
342int rio_minor(struct tty_struct *tty)
343{
344	return tty->index + (tty->driver == rio_driver) ? 0 : 256;
345}
346
347static int rio_set_real_termios(void *ptr)
348{
349	return RIOParam((struct Port *) ptr, CONFIG, 1, 1);
350}
351
352
353static void rio_reset_interrupt(struct Host *HostP)
354{
355	func_enter();
356
357	switch (HostP->Type) {
358	case RIO_AT:
359	case RIO_MCA:
360	case RIO_PCI:
361		writeb(0xFF, &HostP->ResetInt);
362	}
363
364	func_exit();
365}
366
367
368static irqreturn_t rio_interrupt(int irq, void *ptr)
369{
370	struct Host *HostP;
371	func_enter();
372
373	HostP = ptr;			/* &p->RIOHosts[(long)ptr]; */
374	rio_dprintk(RIO_DEBUG_IFLOW, "rio: enter rio_interrupt (%d/%d)\n", irq, HostP->Ivec);
375
376	/* AAargh! The order in which to do these things is essential and
377	   not trivial.
378
379	   - hardware twiddling goes before "recursive". Otherwise when we
380	   poll the card, and a recursive interrupt happens, we won't
381	   ack the card, so it might keep on interrupting us. (especially
382	   level sensitive interrupt systems like PCI).
383
384	   - Rate limit goes before hardware twiddling. Otherwise we won't
385	   catch a card that has gone bonkers.
386
387	   - The "initialized" test goes after the hardware twiddling. Otherwise
388	   the card will stick us in the interrupt routine again.
389
390	   - The initialized test goes before recursive.
391	 */
392
393	rio_dprintk(RIO_DEBUG_IFLOW, "rio: We've have noticed the interrupt\n");
394	if (HostP->Ivec == irq) {
395		/* Tell the card we've noticed the interrupt. */
396		rio_reset_interrupt(HostP);
397	}
398
399	if ((HostP->Flags & RUN_STATE) != RC_RUNNING)
400		return IRQ_HANDLED;
401
402	if (test_and_set_bit(RIO_BOARD_INTR_LOCK, &HostP->locks)) {
403		printk(KERN_ERR "Recursive interrupt! (host %p/irq%d)\n", ptr, HostP->Ivec);
404		return IRQ_HANDLED;
405	}
406
407	RIOServiceHost(p, HostP);
408
409	rio_dprintk(RIO_DEBUG_IFLOW, "riointr() doing host %p type %d\n", ptr, HostP->Type);
410
411	clear_bit(RIO_BOARD_INTR_LOCK, &HostP->locks);
412	rio_dprintk(RIO_DEBUG_IFLOW, "rio: exit rio_interrupt (%d/%d)\n", irq, HostP->Ivec);
413	func_exit();
414	return IRQ_HANDLED;
415}
416
417
418static void rio_pollfunc(unsigned long data)
419{
420	func_enter();
421
422	rio_interrupt(0, &p->RIOHosts[data]);
423	mod_timer(&p->RIOHosts[data].timer, jiffies + rio_poll);
424
425	func_exit();
426}
427
428
429/* ********************************************************************** *
430 *                Here are the routines that actually                     *
431 *              interface with the generic_serial driver                  *
432 * ********************************************************************** */
433
434/* Ehhm. I don't know how to fiddle with interrupts on the Specialix
435   cards. ....   Hmm. Ok I figured it out. You don't.  -- REW */
436
437static void rio_disable_tx_interrupts(void *ptr)
438{
439	func_enter();
440
441	/*  port->gs.flags &= ~GS_TX_INTEN; */
442
443	func_exit();
444}
445
446
447static void rio_enable_tx_interrupts(void *ptr)
448{
449	struct Port *PortP = ptr;
450	/* int hn; */
451
452	func_enter();
453
454	/* hn = PortP->HostP - p->RIOHosts;
455
456	   rio_dprintk (RIO_DEBUG_TTY, "Pushing host %d\n", hn);
457	   rio_interrupt (-1,(void *) hn, NULL); */
458
459	RIOTxEnable((char *) PortP);
460
461	/*
462	 * In general we cannot count on "tx empty" interrupts, although
463	 * the interrupt routine seems to be able to tell the difference.
464	 */
465	PortP->gs.flags &= ~GS_TX_INTEN;
466
467	func_exit();
468}
469
470
471static void rio_disable_rx_interrupts(void *ptr)
472{
473	func_enter();
474	func_exit();
475}
476
477static void rio_enable_rx_interrupts(void *ptr)
478{
479	/*  struct rio_port *port = ptr; */
480	func_enter();
481	func_exit();
482}
483
484
485/* Jeez. Isn't this simple?  */
486static int rio_get_CD(void *ptr)
487{
488	struct Port *PortP = ptr;
489	int rv;
490
491	func_enter();
492	rv = (PortP->ModemState & MSVR1_CD) != 0;
493
494	rio_dprintk(RIO_DEBUG_INIT, "Getting CD status: %d\n", rv);
495
496	func_exit();
497	return rv;
498}
499
500
501/* Jeez. Isn't this simple? Actually, we can sync with the actual port
502   by just pushing stuff into the queue going to the port... */
503static int rio_chars_in_buffer(void *ptr)
504{
505	func_enter();
506
507	func_exit();
508	return 0;
509}
510
511
512/* Nothing special here... */
513static void rio_shutdown_port(void *ptr)
514{
515	struct Port *PortP;
516
517	func_enter();
518
519	PortP = (struct Port *) ptr;
520	PortP->gs.tty = NULL;
521	func_exit();
522}
523
524
525/* I haven't the foggiest why the decrement use count has to happen
526   here. The whole linux serial drivers stuff needs to be redesigned.
527   My guess is that this is a hack to minimize the impact of a bug
528   elsewhere. Thinking about it some more. (try it sometime) Try
529   running minicom on a serial port that is driven by a modularized
530   driver. Have the modem hangup. Then remove the driver module. Then
531   exit minicom.  I expect an "oops".  -- REW */
532static void rio_hungup(void *ptr)
533{
534	struct Port *PortP;
535
536	func_enter();
537
538	PortP = (struct Port *) ptr;
539	PortP->gs.tty = NULL;
540
541	func_exit();
542}
543
544
545/* The standard serial_close would become shorter if you'd wrap it like
546   this.
547   rs_close (...){save_flags;cli;real_close();dec_use_count;restore_flags;}
548 */
549static void rio_close(void *ptr)
550{
551	struct Port *PortP;
552
553	func_enter();
554
555	PortP = (struct Port *) ptr;
556
557	riotclose(ptr);
558
559	if (PortP->gs.count) {
560		printk(KERN_ERR "WARNING port count:%d\n", PortP->gs.count);
561		PortP->gs.count = 0;
562	}
563
564	PortP->gs.tty = NULL;
565	func_exit();
566}
567
568
569
570static int rio_fw_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
571{
572	int rc = 0;
573	func_enter();
574
575	/* The "dev" argument isn't used. */
576	rc = riocontrol(p, 0, cmd, arg, capable(CAP_SYS_ADMIN));
577
578	func_exit();
579	return rc;
580}
581
582extern int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
583
584static int rio_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
585{
586	void __user *argp = (void __user *)arg;
587	int rc;
588	struct Port *PortP;
589	int ival;
590
591	func_enter();
592
593	PortP = (struct Port *) tty->driver_data;
594
595	rc = 0;
596	switch (cmd) {
597	case TIOCSSOFTCAR:
598		if ((rc = get_user(ival, (unsigned __user *) argp)) == 0) {
599			tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) | (ival ? CLOCAL : 0);
600		}
601		break;
602	case TIOCGSERIAL:
603		rc = -EFAULT;
604		if (access_ok(VERIFY_WRITE, argp, sizeof(struct serial_struct)))
605			rc = gs_getserial(&PortP->gs, argp);
606		break;
607	case TCSBRK:
608		if (PortP->State & RIO_DELETED) {
609			rio_dprintk(RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
610			rc = -EIO;
611		} else {
612			if (RIOShortCommand(p, PortP, SBREAK, 2, 250) == RIO_FAIL) {
613				rio_dprintk(RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
614				rc = -EIO;
615			}
616		}
617		break;
618	case TCSBRKP:
619		if (PortP->State & RIO_DELETED) {
620			rio_dprintk(RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
621			rc = -EIO;
622		} else {
623			int l;
624			l = arg ? arg * 100 : 250;
625			if (l > 255)
626				l = 255;
627			if (RIOShortCommand(p, PortP, SBREAK, 2, arg ? arg * 100 : 250) == RIO_FAIL) {
628				rio_dprintk(RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
629				rc = -EIO;
630			}
631		}
632		break;
633	case TIOCSSERIAL:
634		rc = -EFAULT;
635		if (access_ok(VERIFY_READ, argp, sizeof(struct serial_struct)))
636			rc = gs_setserial(&PortP->gs, argp);
637		break;
638	default:
639		rc = -ENOIOCTLCMD;
640		break;
641	}
642	func_exit();
643	return rc;
644}
645
646
647/* The throttle/unthrottle scheme for the Specialix card is different
648 * from other drivers and deserves some explanation.
649 * The Specialix hardware takes care of XON/XOFF
650 * and CTS/RTS flow control itself.  This means that all we have to
651 * do when signalled by the upper tty layer to throttle/unthrottle is
652 * to make a note of it here.  When we come to read characters from the
653 * rx buffers on the card (rio_receive_chars()) we look to see if the
654 * upper layer can accept more (as noted here in rio_rx_throt[]).
655 * If it can't we simply don't remove chars from the cards buffer.
656 * When the tty layer can accept chars, we again note that here and when
657 * rio_receive_chars() is called it will remove them from the cards buffer.
658 * The card will notice that a ports buffer has drained below some low
659 * water mark and will unflow control the line itself, using whatever
660 * flow control scheme is in use for that port. -- Simon Allen
661 */
662
663static void rio_throttle(struct tty_struct *tty)
664{
665	struct Port *port = (struct Port *) tty->driver_data;
666
667	func_enter();
668	/* If the port is using any type of input flow
669	 * control then throttle the port.
670	 */
671
672	if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
673		port->State |= RIO_THROTTLE_RX;
674	}
675
676	func_exit();
677}
678
679
680static void rio_unthrottle(struct tty_struct *tty)
681{
682	struct Port *port = (struct Port *) tty->driver_data;
683
684	func_enter();
685	/* Always unthrottle even if flow control is not enabled on
686	 * this port in case we disabled flow control while the port
687	 * was throttled
688	 */
689
690	port->State &= ~RIO_THROTTLE_RX;
691
692	func_exit();
693	return;
694}
695
696
697
698
699
700/* ********************************************************************** *
701 *                    Here are the initialization routines.               *
702 * ********************************************************************** */
703
704
705static struct vpd_prom *get_VPD_PROM(struct Host *hp)
706{
707	static struct vpd_prom vpdp;
708	char *p;
709	int i;
710
711	func_enter();
712	rio_dprintk(RIO_DEBUG_PROBE, "Going to verify vpd prom at %p.\n", hp->Caddr + RIO_VPD_ROM);
713
714	p = (char *) &vpdp;
715	for (i = 0; i < sizeof(struct vpd_prom); i++)
716		*p++ = readb(hp->Caddr + RIO_VPD_ROM + i * 2);
717	/* read_rio_byte (hp, RIO_VPD_ROM + i*2); */
718
719	/* Terminate the identifier string.
720	 *** requires one extra byte in struct vpd_prom *** */
721	*p++ = 0;
722
723	if (rio_debug & RIO_DEBUG_PROBE)
724		my_hd((char *) &vpdp, 0x20);
725
726	func_exit();
727
728	return &vpdp;
729}
730
731static const struct tty_operations rio_ops = {
732	.open = riotopen,
733	.close = gs_close,
734	.write = gs_write,
735	.put_char = gs_put_char,
736	.flush_chars = gs_flush_chars,
737	.write_room = gs_write_room,
738	.chars_in_buffer = gs_chars_in_buffer,
739	.flush_buffer = gs_flush_buffer,
740	.ioctl = rio_ioctl,
741	.throttle = rio_throttle,
742	.unthrottle = rio_unthrottle,
743	.set_termios = gs_set_termios,
744	.stop = gs_stop,
745	.start = gs_start,
746	.hangup = gs_hangup,
747};
748
749static int rio_init_drivers(void)
750{
751	int error = -ENOMEM;
752
753	rio_driver = alloc_tty_driver(256);
754	if (!rio_driver)
755		goto out;
756	rio_driver2 = alloc_tty_driver(256);
757	if (!rio_driver2)
758		goto out1;
759
760	func_enter();
761
762	rio_driver->owner = THIS_MODULE;
763	rio_driver->driver_name = "specialix_rio";
764	rio_driver->name = "ttySR";
765	rio_driver->major = RIO_NORMAL_MAJOR0;
766	rio_driver->type = TTY_DRIVER_TYPE_SERIAL;
767	rio_driver->subtype = SERIAL_TYPE_NORMAL;
768	rio_driver->init_termios = tty_std_termios;
769	rio_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
770	rio_driver->flags = TTY_DRIVER_REAL_RAW;
771	tty_set_operations(rio_driver, &rio_ops);
772
773	rio_driver2->owner = THIS_MODULE;
774	rio_driver2->driver_name = "specialix_rio";
775	rio_driver2->name = "ttySR";
776	rio_driver2->major = RIO_NORMAL_MAJOR1;
777	rio_driver2->type = TTY_DRIVER_TYPE_SERIAL;
778	rio_driver2->subtype = SERIAL_TYPE_NORMAL;
779	rio_driver2->init_termios = tty_std_termios;
780	rio_driver2->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
781	rio_driver2->flags = TTY_DRIVER_REAL_RAW;
782	tty_set_operations(rio_driver2, &rio_ops);
783
784	rio_dprintk(RIO_DEBUG_INIT, "set_termios = %p\n", gs_set_termios);
785
786	if ((error = tty_register_driver(rio_driver)))
787		goto out2;
788	if ((error = tty_register_driver(rio_driver2)))
789		goto out3;
790	func_exit();
791	return 0;
792      out3:
793	tty_unregister_driver(rio_driver);
794      out2:
795	put_tty_driver(rio_driver2);
796      out1:
797	put_tty_driver(rio_driver);
798      out:
799	printk(KERN_ERR "rio: Couldn't register a rio driver, error = %d\n", error);
800	return 1;
801}
802
803
804static void *ckmalloc(int size)
805{
806	void *p;
807
808	p = kmalloc(size, GFP_KERNEL);
809	if (p)
810		memset(p, 0, size);
811	return p;
812}
813
814
815
816static int rio_init_datastructures(void)
817{
818	int i;
819	struct Port *port;
820	func_enter();
821
822	/* Many drivers statically allocate the maximum number of ports
823	   There is no reason not to allocate them dynamically. Is there? -- REW */
824	/* However, the RIO driver allows users to configure their first
825	   RTA as the ports numbered 504-511. We therefore need to allocate
826	   the whole range. :-(   -- REW */
827
828#define RI_SZ   sizeof(struct rio_info)
829#define HOST_SZ sizeof(struct Host)
830#define PORT_SZ sizeof(struct Port *)
831#define TMIO_SZ sizeof(struct termios *)
832	rio_dprintk(RIO_DEBUG_INIT, "getting : %Zd %Zd %Zd %Zd %Zd bytes\n", RI_SZ, RIO_HOSTS * HOST_SZ, RIO_PORTS * PORT_SZ, RIO_PORTS * TMIO_SZ, RIO_PORTS * TMIO_SZ);
833
834	if (!(p = ckmalloc(RI_SZ)))
835		goto free0;
836	if (!(p->RIOHosts = ckmalloc(RIO_HOSTS * HOST_SZ)))
837		goto free1;
838	if (!(p->RIOPortp = ckmalloc(RIO_PORTS * PORT_SZ)))
839		goto free2;
840	p->RIOConf = RIOConf;
841	rio_dprintk(RIO_DEBUG_INIT, "Got : %p %p %p\n", p, p->RIOHosts, p->RIOPortp);
842
843	for (i = 0; i < RIO_PORTS; i++) {
844		port = p->RIOPortp[i] = ckmalloc(sizeof(struct Port));
845		if (!port) {
846			goto free6;
847		}
848		rio_dprintk(RIO_DEBUG_INIT, "initing port %d (%d)\n", i, port->Mapped);
849		port->PortNum = i;
850		port->gs.magic = RIO_MAGIC;
851		port->gs.close_delay = HZ / 2;
852		port->gs.closing_wait = 30 * HZ;
853		port->gs.rd = &rio_real_driver;
854		spin_lock_init(&port->portSem);
855		/*
856		 * Initializing wait queue
857		 */
858		init_waitqueue_head(&port->gs.open_wait);
859		init_waitqueue_head(&port->gs.close_wait);
860	}
861
862
863
864	if (rio_debug & RIO_DEBUG_INIT) {
865		my_hd(&rio_real_driver, sizeof(rio_real_driver));
866	}
867
868
869	func_exit();
870	return 0;
871
872      free6:for (i--; i >= 0; i--)
873		kfree(p->RIOPortp[i]);
874/*free5:
875 free4:
876 free3:*/ kfree(p->RIOPortp);
877      free2:kfree(p->RIOHosts);
878      free1:
879	rio_dprintk(RIO_DEBUG_INIT, "Not enough memory! %p %p %p\n", p, p->RIOHosts, p->RIOPortp);
880	kfree(p);
881      free0:
882	return -ENOMEM;
883}
884
885static void __exit rio_release_drivers(void)
886{
887	func_enter();
888	tty_unregister_driver(rio_driver2);
889	tty_unregister_driver(rio_driver);
890	put_tty_driver(rio_driver2);
891	put_tty_driver(rio_driver);
892	func_exit();
893}
894
895
896#ifdef CONFIG_PCI
897 /* This was written for SX, but applies to RIO too...
898    (including bugs....)
899
900    There is another bit besides Bit 17. Turning that bit off
901    (on boards shipped with the fix in the eeprom) results in a
902    hang on the next access to the card.
903  */
904
905 /********************************************************
906 * Setting bit 17 in the CNTRL register of the PLX 9050  *
907 * chip forces a retry on writes while a read is pending.*
908 * This is to prevent the card locking up on Intel Xeon  *
909 * multiprocessor systems with the NX chipset.    -- NV  *
910 ********************************************************/
911
912/* Newer cards are produced with this bit set from the configuration
913   EEprom.  As the bit is read/write for the CPU, we can fix it here,
914   if we detect that it isn't set correctly. -- REW */
915
916static void fix_rio_pci(struct pci_dev *pdev)
917{
918	unsigned long hwbase;
919	unsigned char __iomem *rebase;
920	unsigned int t;
921
922#define CNTRL_REG_OFFSET        0x50
923#define CNTRL_REG_GOODVALUE     0x18260000
924
925	hwbase = pci_resource_start(pdev, 0);
926	rebase = ioremap(hwbase, 0x80);
927	t = readl(rebase + CNTRL_REG_OFFSET);
928	if (t != CNTRL_REG_GOODVALUE) {
929		printk(KERN_DEBUG "rio: performing cntrl reg fix: %08x -> %08x\n", t, CNTRL_REG_GOODVALUE);
930		writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
931	}
932	iounmap(rebase);
933}
934#endif
935
936
937static int __init rio_init(void)
938{
939	int found = 0;
940	int i;
941	struct Host *hp;
942	int retval;
943	struct vpd_prom *vpdp;
944	int okboard;
945
946#ifdef CONFIG_PCI
947	struct pci_dev *pdev = NULL;
948	unsigned short tshort;
949#endif
950
951	func_enter();
952	rio_dprintk(RIO_DEBUG_INIT, "Initing rio module... (rio_debug=%d)\n", rio_debug);
953
954	if (abs((long) (&rio_debug) - rio_debug) < 0x10000) {
955		printk(KERN_WARNING "rio: rio_debug is an address, instead of a value. " "Assuming -1. Was %x/%p.\n", rio_debug, &rio_debug);
956		rio_debug = -1;
957	}
958
959	if (misc_register(&rio_fw_device) < 0) {
960		printk(KERN_ERR "RIO: Unable to register firmware loader driver.\n");
961		return -EIO;
962	}
963
964	retval = rio_init_datastructures();
965	if (retval < 0) {
966		misc_deregister(&rio_fw_device);
967		return retval;
968	}
969#ifdef CONFIG_PCI
970	/* First look for the JET devices: */
971	while ((pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, pdev))) {
972		u32 tint;
973
974		if (pci_enable_device(pdev))
975			continue;
976
977		/* Specialix has a whole bunch of cards with
978		   0x2000 as the device ID. They say its because
979		   the standard requires it. Stupid standard. */
980		/* It seems that reading a word doesn't work reliably on 2.0.
981		   Also, reading a non-aligned dword doesn't work. So we read the
982		   whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
983		   ourselves */
984		pci_read_config_dword(pdev, 0x2c, &tint);
985		tshort = (tint >> 16) & 0xffff;
986		rio_dprintk(RIO_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
987		if (tshort != 0x0100) {
988			rio_dprintk(RIO_DEBUG_PROBE, "But it's not a RIO card (%d)...\n", tshort);
989			continue;
990		}
991		rio_dprintk(RIO_DEBUG_PROBE, "cp1\n");
992
993		hp = &p->RIOHosts[p->RIONumHosts];
994		hp->PaddrP = pci_resource_start(pdev, 2);
995		hp->Ivec = pdev->irq;
996		if (((1 << hp->Ivec) & rio_irqmask) == 0)
997			hp->Ivec = 0;
998		hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
999		hp->CardP = (struct DpRam __iomem *) hp->Caddr;
1000		hp->Type = RIO_PCI;
1001		hp->Copy = rio_copy_to_card;
1002		hp->Mode = RIO_PCI_BOOT_FROM_RAM;
1003		spin_lock_init(&hp->HostLock);
1004		rio_reset_interrupt(hp);
1005		rio_start_card_running(hp);
1006
1007		rio_dprintk(RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n", (void *) p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr);
1008		if (RIOBoardTest(p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr, RIO_PCI, 0) == 0) {
1009			rio_dprintk(RIO_DEBUG_INIT, "Done RIOBoardTest\n");
1010			writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
1011			p->RIOHosts[p->RIONumHosts].UniqueNum =
1012			    ((readb(&p->RIOHosts[p->RIONumHosts].Unique[0]) & 0xFF) << 0) |
1013			    ((readb(&p->RIOHosts[p->RIONumHosts].Unique[1]) & 0xFF) << 8) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[2]) & 0xFF) << 16) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[3]) & 0xFF) << 24);
1014			rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1015
1016			fix_rio_pci(pdev);
1017
1018			p->RIOHosts[p->RIONumHosts].pdev = pdev;
1019			pci_dev_get(pdev);
1020
1021			p->RIOLastPCISearch = 0;
1022			p->RIONumHosts++;
1023			found++;
1024		} else {
1025			iounmap(p->RIOHosts[p->RIONumHosts].Caddr);
1026			p->RIOHosts[p->RIONumHosts].Caddr = NULL;
1027		}
1028	}
1029
1030	/* Then look for the older PCI card.... : */
1031
1032	/* These older PCI cards have problems (only byte-mode access is
1033	   supported), which makes them a bit awkward to support.
1034	   They also have problems sharing interrupts. Be careful.
1035	   (The driver now refuses to share interrupts for these
1036	   cards. This should be sufficient).
1037	 */
1038
1039	/* Then look for the older RIO/PCI devices: */
1040	while ((pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_RIO, pdev))) {
1041		if (pci_enable_device(pdev))
1042			continue;
1043
1044#ifdef CONFIG_RIO_OLDPCI
1045		hp = &p->RIOHosts[p->RIONumHosts];
1046		hp->PaddrP = pci_resource_start(pdev, 0);
1047		hp->Ivec = pdev->irq;
1048		if (((1 << hp->Ivec) & rio_irqmask) == 0)
1049			hp->Ivec = 0;
1050		hp->Ivec |= 0x8000;	/* Mark as non-sharable */
1051		hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1052		hp->CardP = (struct DpRam __iomem *) hp->Caddr;
1053		hp->Type = RIO_PCI;
1054		hp->Copy = rio_copy_to_card;
1055		hp->Mode = RIO_PCI_BOOT_FROM_RAM;
1056		spin_lock_init(&hp->HostLock);
1057
1058		rio_dprintk(RIO_DEBUG_PROBE, "Ivec: %x\n", hp->Ivec);
1059		rio_dprintk(RIO_DEBUG_PROBE, "Mode: %x\n", hp->Mode);
1060
1061		rio_reset_interrupt(hp);
1062		rio_start_card_running(hp);
1063		rio_dprintk(RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n", (void *) p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr);
1064		if (RIOBoardTest(p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr, RIO_PCI, 0) == 0) {
1065			writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
1066			p->RIOHosts[p->RIONumHosts].UniqueNum =
1067			    ((readb(&p->RIOHosts[p->RIONumHosts].Unique[0]) & 0xFF) << 0) |
1068			    ((readb(&p->RIOHosts[p->RIONumHosts].Unique[1]) & 0xFF) << 8) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[2]) & 0xFF) << 16) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[3]) & 0xFF) << 24);
1069			rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1070
1071			p->RIOHosts[p->RIONumHosts].pdev = pdev;
1072			pci_dev_get(pdev);
1073
1074			p->RIOLastPCISearch = 0;
1075			p->RIONumHosts++;
1076			found++;
1077		} else {
1078			iounmap(p->RIOHosts[p->RIONumHosts].Caddr);
1079			p->RIOHosts[p->RIONumHosts].Caddr = NULL;
1080		}
1081#else
1082		printk(KERN_ERR "Found an older RIO PCI card, but the driver is not " "compiled to support it.\n");
1083#endif
1084	}
1085#endif				/* PCI */
1086
1087	/* Now probe for ISA cards... */
1088	for (i = 0; i < NR_RIO_ADDRS; i++) {
1089		hp = &p->RIOHosts[p->RIONumHosts];
1090		hp->PaddrP = rio_probe_addrs[i];
1091		/* There was something about the IRQs of these cards. 'Forget what.--REW */
1092		hp->Ivec = 0;
1093		hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1094		hp->CardP = (struct DpRam __iomem *) hp->Caddr;
1095		hp->Type = RIO_AT;
1096		hp->Copy = rio_copy_to_card;	/* AT card PCI???? - PVDL
1097                                         * -- YES! this is now a normal copy. Only the
1098					 * old PCI card uses the special PCI copy.
1099					 * Moreover, the ISA card will work with the
1100					 * special PCI copy anyway. -- REW */
1101		hp->Mode = 0;
1102		spin_lock_init(&hp->HostLock);
1103
1104		vpdp = get_VPD_PROM(hp);
1105		rio_dprintk(RIO_DEBUG_PROBE, "Got VPD ROM\n");
1106		okboard = 0;
1107		if ((strncmp(vpdp->identifier, RIO_ISA_IDENT, 16) == 0) || (strncmp(vpdp->identifier, RIO_ISA2_IDENT, 16) == 0) || (strncmp(vpdp->identifier, RIO_ISA3_IDENT, 16) == 0)) {
1108			/* Board is present... */
1109			if (RIOBoardTest(hp->PaddrP, hp->Caddr, RIO_AT, 0) == 0) {
1110				/* ... and feeling fine!!!! */
1111				rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1112				if (RIOAssignAT(p, hp->PaddrP, hp->Caddr, 0)) {
1113					rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, host%d uniqid = %x.\n", p->RIONumHosts, p->RIOHosts[p->RIONumHosts - 1].UniqueNum);
1114					okboard++;
1115					found++;
1116				}
1117			}
1118
1119			if (!okboard) {
1120				iounmap(hp->Caddr);
1121				hp->Caddr = NULL;
1122			}
1123		}
1124	}
1125
1126
1127	for (i = 0; i < p->RIONumHosts; i++) {
1128		hp = &p->RIOHosts[i];
1129		if (hp->Ivec) {
1130			int mode = IRQF_SHARED;
1131			if (hp->Ivec & 0x8000) {
1132				mode = 0;
1133				hp->Ivec &= 0x7fff;
1134			}
1135			rio_dprintk(RIO_DEBUG_INIT, "Requesting interrupt hp: %p rio_interrupt: %d Mode: %x\n", hp, hp->Ivec, hp->Mode);
1136			retval = request_irq(hp->Ivec, rio_interrupt, mode, "rio", hp);
1137			rio_dprintk(RIO_DEBUG_INIT, "Return value from request_irq: %d\n", retval);
1138			if (retval) {
1139				printk(KERN_ERR "rio: Cannot allocate irq %d.\n", hp->Ivec);
1140				hp->Ivec = 0;
1141			}
1142			rio_dprintk(RIO_DEBUG_INIT, "Got irq %d.\n", hp->Ivec);
1143			if (hp->Ivec != 0) {
1144				rio_dprintk(RIO_DEBUG_INIT, "Enabling interrupts on rio card.\n");
1145				hp->Mode |= RIO_PCI_INT_ENABLE;
1146			} else
1147				hp->Mode &= ~RIO_PCI_INT_ENABLE;
1148			rio_dprintk(RIO_DEBUG_INIT, "New Mode: %x\n", hp->Mode);
1149			rio_start_card_running(hp);
1150		}
1151		/* Init the timer "always" to make sure that it can safely be
1152		   deleted when we unload... */
1153
1154		setup_timer(&hp->timer, rio_pollfunc, i);
1155		if (!hp->Ivec) {
1156			rio_dprintk(RIO_DEBUG_INIT, "Starting polling at %dj intervals.\n", rio_poll);
1157			mod_timer(&hp->timer, jiffies + rio_poll);
1158		}
1159	}
1160
1161	if (found) {
1162		rio_dprintk(RIO_DEBUG_INIT, "rio: total of %d boards detected.\n", found);
1163		rio_init_drivers();
1164	} else {
1165		/* deregister the misc device we created earlier */
1166		misc_deregister(&rio_fw_device);
1167	}
1168
1169	func_exit();
1170	return found ? 0 : -EIO;
1171}
1172
1173
1174static void __exit rio_exit(void)
1175{
1176	int i;
1177	struct Host *hp;
1178
1179	func_enter();
1180
1181	for (i = 0, hp = p->RIOHosts; i < p->RIONumHosts; i++, hp++) {
1182		RIOHostReset(hp->Type, hp->CardP, hp->Slot);
1183		if (hp->Ivec) {
1184			free_irq(hp->Ivec, hp);
1185			rio_dprintk(RIO_DEBUG_INIT, "freed irq %d.\n", hp->Ivec);
1186		}
1187		/* It is safe/allowed to del_timer a non-active timer */
1188		del_timer_sync(&hp->timer);
1189		if (hp->Caddr)
1190			iounmap(hp->Caddr);
1191		if (hp->Type == RIO_PCI)
1192			pci_dev_put(hp->pdev);
1193	}
1194
1195	if (misc_deregister(&rio_fw_device) < 0) {
1196		printk(KERN_INFO "rio: couldn't deregister control-device\n");
1197	}
1198
1199
1200	rio_dprintk(RIO_DEBUG_CLEANUP, "Cleaning up drivers\n");
1201
1202	rio_release_drivers();
1203
1204	/* Release dynamically allocated memory */
1205	kfree(p->RIOPortp);
1206	kfree(p->RIOHosts);
1207	kfree(p);
1208
1209	func_exit();
1210}
1211
1212module_init(rio_init);
1213module_exit(rio_exit);
1214