pc300_drv.c 109.9 KB
Newer Older
L
Linus Torvalds 已提交
1
#define	USE_PCI_CLOCK
2
static const char rcsid[] =
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
"Revision: 3.4.5 Date: 2002/03/07 ";

/*
 * pc300.c	Cyclades-PC300(tm) Driver.
 *
 * Author:	Ivan Passos <ivan@cyclades.com>
 * Maintainer:	PC300 Maintainer <pc300@cyclades.com>
 *
 * Copyright:	(c) 1999-2003 Cyclades Corp.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *	
 *	Using tabstop = 4.
 * 
 * $Log: pc300_drv.c,v $
 * Revision 3.23  2002/03/20 13:58:40  henrique
 * Fixed ortographic mistakes
 *
 * Revision 3.22  2002/03/13 16:56:56  henrique
 * Take out the debug messages
 *
 * Revision 3.21  2002/03/07 14:17:09  henrique
 * License data fixed
 *
 * Revision 3.20  2002/01/17 17:58:52  ivan
 * Support for PC300-TE/M (PMC).
 *
 * Revision 3.19  2002/01/03 17:08:47  daniela
 * Enables DMA reception when the SCA-II disables it improperly.
 *
 * Revision 3.18  2001/12/03 18:47:50  daniela
 * Esthetic changes.
 *
 * Revision 3.17  2001/10/19 16:50:13  henrique
 * Patch to kernel 2.4.12 and new generic hdlc.
 *
 * Revision 3.16  2001/10/16 15:12:31  regina
 * clear statistics
 *
 * Revision 3.11 to 3.15  2001/10/11 20:26:04  daniela
 * More DMA fixes for noisy lines.
 * Return the size of bad frames in dma_get_rx_frame_size, so that the Rx buffer
 * descriptors can be cleaned by dma_buf_read (called in cpc_net_rx).
 * Renamed dma_start routine to rx_dma_start. Improved Rx statistics.
 * Fixed BOF interrupt treatment. Created dma_start routine.
 * Changed min and max to cpc_min and cpc_max.
 *
 * Revision 3.10  2001/08/06 12:01:51  regina
 * Fixed problem in DSR_DE bit.
 *
 * Revision 3.9  2001/07/18 19:27:26  daniela
 * Added some history comments.
 *
 * Revision 3.8  2001/07/12 13:11:19  regina
 * bug fix - DCD-OFF in pc300 tty driver
 *
 * Revision 3.3 to 3.7  2001/07/06 15:00:20  daniela
 * Removing kernel 2.4.3 and previous support.
 * DMA transmission bug fix.
 * MTU check in cpc_net_rx fixed.
 * Boot messages reviewed.
 * New configuration parameters (line code, CRC calculation and clock).
 *
 * Revision 3.2 2001/06/22 13:13:02  regina
 * MLPPP implementation. Changed the header of message trace to include
 * the device name. New format : "hdlcX[R/T]: ".
 * Default configuration changed.
 *
 * Revision 3.1 2001/06/15 regina
 * in cpc_queue_xmit, netif_stop_queue is called if don't have free descriptor
 * upping major version number
 *
 * Revision 1.1.1.1  2001/06/13 20:25:04  daniela
 * PC300 initial CVS version (3.4.0-pre1)
 *
 * Revision 3.0.1.2 2001/06/08 daniela
 * Did some changes in the DMA programming implementation to avoid the 
 * occurrence of a SCA-II bug when CDA is accessed during a DMA transfer.
 *
 * Revision 3.0.1.1 2001/05/02 daniela
 * Added kernel 2.4.3 support.
 * 
 * Revision 3.0.1.0 2001/03/13 daniela, henrique
 * Added Frame Relay Support.
 * Driver now uses HDLC generic driver to provide protocol support.
 * 
 * Revision 3.0.0.8 2001/03/02 daniela
 * Fixed ram size detection. 
 * Changed SIOCGPC300CONF ioctl, to give hw information to pc300util.
 * 
 * Revision 3.0.0.7 2001/02/23 daniela
 * netif_stop_queue called before the SCA-II transmition commands in 
 * cpc_queue_xmit, and with interrupts disabled to avoid race conditions with 
 * transmition interrupts.
 * Fixed falc_check_status for Unframed E1.
 * 
 * Revision 3.0.0.6 2000/12/13 daniela
 * Implemented pc300util support: trace, statistics, status and loopback
 * tests for the PC300 TE boards.
 * 
 * Revision 3.0.0.5 2000/12/12 ivan
 * Added support for Unframed E1.
 * Implemented monitor mode.
 * Fixed DCD sensitivity on the second channel.
 * Driver now complies with new PCI kernel architecture.
 *
 * Revision 3.0.0.4 2000/09/28 ivan
 * Implemented DCD sensitivity.
 * Moved hardware-specific open to the end of cpc_open, to avoid race
 * conditions with early reception interrupts.
 * Included code for [request|release]_mem_region().
 * Changed location of pc300.h .
 * Minor code revision (contrib. of Jeff Garzik).
 *
 * Revision 3.0.0.3 2000/07/03 ivan
 * Previous bugfix for the framing errors with external clock made X21
 * boards stop working. This version fixes it.
 *
 * Revision 3.0.0.2 2000/06/23 ivan
 * Revisited cpc_queue_xmit to prevent race conditions on Tx DMA buffer
 * handling when Tx timeouts occur.
 * Revisited Rx statistics.
 * Fixed a bug in the SCA-II programming that would cause framing errors
 * when external clock was configured.
 *
 * Revision 3.0.0.1 2000/05/26 ivan
 * Added logic in the SCA interrupt handler so that no board can monopolize
 * the driver.
 * Request PLX I/O region, although driver doesn't use it, to avoid
 * problems with other drivers accessing it.
 *
 * Revision 3.0.0.0 2000/05/15 ivan
 * Did some changes in the DMA programming implementation to avoid the
 * occurrence of a SCA-II bug in the second channel.
 * Implemented workaround for PLX9050 bug that would cause a system lockup
 * in certain systems, depending on the MMIO addresses allocated to the
 * board.
 * Fixed the FALC chip programming to avoid synchronization problems in the
 * second channel (TE only).
 * Implemented a cleaner and faster Tx DMA descriptor cleanup procedure in
 * cpc_queue_xmit().
 * Changed the built-in driver implementation so that the driver can use the
 * general 'hdlcN' naming convention instead of proprietary device names.
 * Driver load messages are now device-centric, instead of board-centric.
 * Dynamic allocation of net_device structures.
 * Code is now compliant with the new module interface (module_[init|exit]).
 * Make use of the PCI helper functions to access PCI resources.
 *
 * Revision 2.0.0.0 2000/04/15 ivan
 * Added support for the PC300/TE boards (T1/FT1/E1/FE1).
 *
 * Revision 1.1.0.0 2000/02/28 ivan
 * Major changes in the driver architecture.
 * Softnet compliancy implemented.
 * Driver now reports physical instead of virtual memory addresses.
 * Added cpc_change_mtu function.
 *
 * Revision 1.0.0.0 1999/12/16 ivan
 * First official release.
 * Support for 1- and 2-channel boards (which use distinct PCI Device ID's).
 * Support for monolythic installation (i.e., drv built into the kernel).
 * X.25 additional checking when lapb_[dis]connect_request returns an error.
 * SCA programming now covers X.21 as well.
 *
 * Revision 0.3.1.0 1999/11/18 ivan
 * Made X.25 support configuration-dependent (as it depends on external 
 * modules to work).
 * Changed X.25-specific function names to comply with adopted convention.
 * Fixed typos in X.25 functions that would cause compile errors (Daniela).
 * Fixed bug in ch_config that would disable interrupts on a previously 
 * enabled channel if the other channel on the same board was enabled later.
 *
 * Revision 0.3.0.0 1999/11/16 daniela
 * X.25 support.
 *
 * Revision 0.2.3.0 1999/11/15 ivan
 * Function cpc_ch_status now provides more detailed information.
 * Added support for X.21 clock configuration.
 * Changed TNR1 setting in order to prevent Tx FIFO overaccesses by the SCA.
 * Now using PCI clock instead of internal oscillator clock for the SCA.
 *
 * Revision 0.2.2.0 1999/11/10 ivan
 * Changed the *_dma_buf_check functions so that they would print only 
 * the useful info instead of the whole buffer descriptor bank.
 * Fixed bug in cpc_queue_xmit that would eventually crash the system 
 * in case of a packet drop.
 * Implemented TX underrun handling.
 * Improved SCA fine tuning to boost up its performance.
 *
 * Revision 0.2.1.0 1999/11/03 ivan
 * Added functions *dma_buf_pt_init to allow independent initialization 
 * of the next-descr. and DMA buffer pointers on the DMA descriptors.
 * Kernel buffer release and tbusy clearing is now done in the interrupt 
 * handler.
 * Fixed bug in cpc_open that would cause an interface reopen to fail.
 * Added a protocol-specific code section in cpc_net_rx.
 * Removed printk level defs (they might be added back after the beta phase).
 *
 * Revision 0.2.0.0 1999/10/28 ivan
 * Revisited the code so that new protocols can be easily added / supported. 
 *
 * Revision 0.1.0.1 1999/10/20 ivan
 * Mostly "esthetic" changes.
 *
 * Revision 0.1.0.0 1999/10/11 ivan
 * Initial version.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>
228
#include <linux/etherdevice.h>
L
Linus Torvalds 已提交
229 230
#include <linux/spinlock.h>
#include <linux/if.h>
231
#include <linux/slab.h>
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#include <net/arp.h>

#include <asm/io.h>
#include <asm/uaccess.h>

#include "pc300.h"

#define	CPC_LOCK(card,flags)		\
		do {						\
		spin_lock_irqsave(&card->card_lock, flags);	\
		} while (0)

#define CPC_UNLOCK(card,flags)			\
		do {							\
		spin_unlock_irqrestore(&card->card_lock, flags);	\
		} while (0)

#undef	PC300_DEBUG_PCI
#undef	PC300_DEBUG_INTR
#undef	PC300_DEBUG_TX
#undef	PC300_DEBUG_RX
#undef	PC300_DEBUG_OTHER

255
static DEFINE_PCI_DEVICE_TABLE(cpc_pci_dev_id) = {
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	/* PC300/RSV or PC300/X21, 2 chan */
	{0x120e, 0x300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x300},
	/* PC300/RSV or PC300/X21, 1 chan */
	{0x120e, 0x301, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x301},
	/* PC300/TE, 2 chan */
	{0x120e, 0x310, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x310},
	/* PC300/TE, 1 chan */
	{0x120e, 0x311, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x311},
	/* PC300/TE-M, 2 chan */
	{0x120e, 0x320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x320},
	/* PC300/TE-M, 1 chan */
	{0x120e, 0x321, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0x321},
	/* End of table */
	{0,},
};
MODULE_DEVICE_TABLE(pci, cpc_pci_dev_id);

#ifndef cpc_min
#define	cpc_min(a,b)	(((a)<(b))?(a):(b))
#endif
#ifndef cpc_max
#define	cpc_max(a,b)	(((a)>(b))?(a):(b))
#endif

/* prototypes */
static void tx_dma_buf_pt_init(pc300_t *, int);
static void tx_dma_buf_init(pc300_t *, int);
static void rx_dma_buf_pt_init(pc300_t *, int);
static void rx_dma_buf_init(pc300_t *, int);
static void tx_dma_buf_check(pc300_t *, int);
static void rx_dma_buf_check(pc300_t *, int);
287
static irqreturn_t cpc_intr(int, void *);
288 289
static int clock_rate_calc(u32, u32, int *);
static u32 detect_ram(pc300_t *);
L
Linus Torvalds 已提交
290 291 292
static void plx_init(pc300_t *);
static void cpc_trace(struct net_device *, struct sk_buff *, char);
static int cpc_attach(struct net_device *, unsigned short, unsigned short);
293
static int cpc_close(struct net_device *dev);
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

#ifdef CONFIG_PC300_MLPPP
void cpc_tty_init(pc300dev_t * dev);
void cpc_tty_unregister_service(pc300dev_t * pc300dev);
void cpc_tty_receive(pc300dev_t * pc300dev);
void cpc_tty_trigger_poll(pc300dev_t * pc300dev);
void cpc_tty_reset_var(void);
#endif

/************************/
/***   DMA Routines   ***/
/************************/
static void tx_dma_buf_pt_init(pc300_t * card, int ch)
{
	int i;
	int ch_factor = ch * N_DMA_TX_BUF;
	volatile pcsca_bd_t __iomem *ptdescr = (card->hw.rambase
			               + DMA_TX_BD_BASE + ch_factor * sizeof(pcsca_bd_t));

	for (i = 0; i < N_DMA_TX_BUF; i++, ptdescr++) {
314
		cpc_writel(&ptdescr->next, (u32)(DMA_TX_BD_BASE +
L
Linus Torvalds 已提交
315
			(ch_factor + ((i + 1) & (N_DMA_TX_BUF - 1))) * sizeof(pcsca_bd_t)));
316 317
		cpc_writel(&ptdescr->ptbuf,
			   (u32)(DMA_TX_BASE + (ch_factor + i) * BD_DEF_LEN));
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	}
}

static void tx_dma_buf_init(pc300_t * card, int ch)
{
	int i;
	int ch_factor = ch * N_DMA_TX_BUF;
	volatile pcsca_bd_t __iomem *ptdescr = (card->hw.rambase
			       + DMA_TX_BD_BASE + ch_factor * sizeof(pcsca_bd_t));

	for (i = 0; i < N_DMA_TX_BUF; i++, ptdescr++) {
		memset_io(ptdescr, 0, sizeof(pcsca_bd_t));
		cpc_writew(&ptdescr->len, 0);
		cpc_writeb(&ptdescr->status, DST_OSB);
	}
	tx_dma_buf_pt_init(card, ch);
}

static void rx_dma_buf_pt_init(pc300_t * card, int ch)
{
	int i;
	int ch_factor = ch * N_DMA_RX_BUF;
	volatile pcsca_bd_t __iomem *ptdescr = (card->hw.rambase
				       + DMA_RX_BD_BASE + ch_factor * sizeof(pcsca_bd_t));

	for (i = 0; i < N_DMA_RX_BUF; i++, ptdescr++) {
344 345
		cpc_writel(&ptdescr->next, (u32)(DMA_RX_BD_BASE +
			(ch_factor + ((i + 1) & (N_DMA_RX_BUF - 1))) * sizeof(pcsca_bd_t)));
L
Linus Torvalds 已提交
346
		cpc_writel(&ptdescr->ptbuf,
347
			   (u32)(DMA_RX_BASE + (ch_factor + i) * BD_DEF_LEN));
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	}
}

static void rx_dma_buf_init(pc300_t * card, int ch)
{
	int i;
	int ch_factor = ch * N_DMA_RX_BUF;
	volatile pcsca_bd_t __iomem *ptdescr = (card->hw.rambase
				       + DMA_RX_BD_BASE + ch_factor * sizeof(pcsca_bd_t));

	for (i = 0; i < N_DMA_RX_BUF; i++, ptdescr++) {
		memset_io(ptdescr, 0, sizeof(pcsca_bd_t));
		cpc_writew(&ptdescr->len, 0);
		cpc_writeb(&ptdescr->status, 0);
	}
	rx_dma_buf_pt_init(card, ch);
}

static void tx_dma_buf_check(pc300_t * card, int ch)
{
	volatile pcsca_bd_t __iomem *ptdescr;
	int i;
370 371
	u16 first_bd = card->chan[ch].tx_first_bd;
	u16 next_bd = card->chan[ch].tx_next_bd;
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	printk("#CH%d: f_bd = %d(0x%08zx), n_bd = %d(0x%08zx)\n", ch,
	       first_bd, TX_BD_ADDR(ch, first_bd),
	       next_bd, TX_BD_ADDR(ch, next_bd));
	for (i = first_bd,
	     ptdescr = (card->hw.rambase + TX_BD_ADDR(ch, first_bd));
	     i != ((next_bd + 1) & (N_DMA_TX_BUF - 1));
	     i = (i + 1) & (N_DMA_TX_BUF - 1), 
		 ptdescr = (card->hw.rambase + TX_BD_ADDR(ch, i))) {
		printk("\n CH%d TX%d: next=0x%x, ptbuf=0x%x, ST=0x%x, len=%d",
		       ch, i, cpc_readl(&ptdescr->next),
		       cpc_readl(&ptdescr->ptbuf),
		       cpc_readb(&ptdescr->status), cpc_readw(&ptdescr->len));
	}
	printk("\n");
}

#ifdef	PC300_DEBUG_OTHER
/* Show all TX buffer descriptors */
static void tx1_dma_buf_check(pc300_t * card, int ch)
{
	volatile pcsca_bd_t __iomem *ptdescr;
	int i;
395 396 397
	u16 first_bd = card->chan[ch].tx_first_bd;
	u16 next_bd = card->chan[ch].tx_next_bd;
	u32 scabase = card->hw.scabase;
L
Linus Torvalds 已提交
398

399
	printk ("\nnfree_tx_bd = %d\n", card->chan[ch].nfree_tx_bd);
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	printk("#CH%d: f_bd = %d(0x%08x), n_bd = %d(0x%08x)\n", ch,
	       first_bd, TX_BD_ADDR(ch, first_bd),
	       next_bd, TX_BD_ADDR(ch, next_bd));
	printk("TX_CDA=0x%08x, TX_EDA=0x%08x\n",
	       cpc_readl(scabase + DTX_REG(CDAL, ch)),
	       cpc_readl(scabase + DTX_REG(EDAL, ch)));
	for (i = 0; i < N_DMA_TX_BUF; i++) {
		ptdescr = (card->hw.rambase + TX_BD_ADDR(ch, i));
		printk("\n CH%d TX%d: next=0x%x, ptbuf=0x%x, ST=0x%x, len=%d",
		       ch, i, cpc_readl(&ptdescr->next),
		       cpc_readl(&ptdescr->ptbuf),
		       cpc_readb(&ptdescr->status), cpc_readw(&ptdescr->len));
	}
	printk("\n");
}
#endif
416

L
Linus Torvalds 已提交
417 418 419 420
static void rx_dma_buf_check(pc300_t * card, int ch)
{
	volatile pcsca_bd_t __iomem *ptdescr;
	int i;
421 422
	u16 first_bd = card->chan[ch].rx_first_bd;
	u16 last_bd = card->chan[ch].rx_last_bd;
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	int ch_factor;

	ch_factor = ch * N_DMA_RX_BUF;
	printk("#CH%d: f_bd = %d, l_bd = %d\n", ch, first_bd, last_bd);
	for (i = 0, ptdescr = (card->hw.rambase +
					      DMA_RX_BD_BASE + ch_factor * sizeof(pcsca_bd_t));
	     i < N_DMA_RX_BUF; i++, ptdescr++) {
		if (cpc_readb(&ptdescr->status) & DST_OSB)
			printk ("\n CH%d RX%d: next=0x%x, ptbuf=0x%x, ST=0x%x, len=%d",
				 ch, i, cpc_readl(&ptdescr->next),
				 cpc_readl(&ptdescr->ptbuf),
				 cpc_readb(&ptdescr->status),
				 cpc_readw(&ptdescr->len));
	}
	printk("\n");
}

440
static int dma_get_rx_frame_size(pc300_t * card, int ch)
L
Linus Torvalds 已提交
441 442
{
	volatile pcsca_bd_t __iomem *ptdescr;
443
	u16 first_bd = card->chan[ch].rx_first_bd;
L
Linus Torvalds 已提交
444
	int rcvd = 0;
445
	volatile u8 status;
L
Linus Torvalds 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	ptdescr = (card->hw.rambase + RX_BD_ADDR(ch, first_bd));
	while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
		rcvd += cpc_readw(&ptdescr->len);
		first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
		if ((status & DST_EOM) || (first_bd == card->chan[ch].rx_last_bd)) {
			/* Return the size of a good frame or incomplete bad frame 
			* (dma_buf_read will clean the buffer descriptors in this case). */
			return (rcvd);
		}
		ptdescr = (card->hw.rambase + cpc_readl(&ptdescr->next));
	}
	return (-1);
}

/*
 * dma_buf_write: writes a frame to the Tx DMA buffers
 * NOTE: this function writes one frame at a time.
 */
465
static int dma_buf_write(pc300_t *card, int ch, u8 *ptdata, int len)
L
Linus Torvalds 已提交
466 467 468 469
{
	int i, nchar;
	volatile pcsca_bd_t __iomem *ptdescr;
	int tosend = len;
470
	u8 nbuf = ((len - 1) / BD_DEF_LEN) + 1;
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

	if (nbuf >= card->chan[ch].nfree_tx_bd) {
		return -ENOMEM;
	}

	for (i = 0; i < nbuf; i++) {
		ptdescr = (card->hw.rambase +
					  TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
		nchar = cpc_min(BD_DEF_LEN, tosend);
		if (cpc_readb(&ptdescr->status) & DST_OSB) {
			memcpy_toio((card->hw.rambase + cpc_readl(&ptdescr->ptbuf)),
				    &ptdata[len - tosend], nchar);
			cpc_writew(&ptdescr->len, nchar);
			card->chan[ch].nfree_tx_bd--;
			if ((i + 1) == nbuf) {
				/* This must be the last BD to be used */
				cpc_writeb(&ptdescr->status, DST_EOM);
			} else {
				cpc_writeb(&ptdescr->status, 0);
			}
		} else {
			return -ENOMEM;
		}
		tosend -= nchar;
		card->chan[ch].tx_next_bd =
			(card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
	}
	/* If it gets to here, it means we have sent the whole frame */
	return 0;
}

/*
 * dma_buf_read: reads a frame from the Rx DMA buffers
 * NOTE: this function reads one frame at a time.
 */
506
static int dma_buf_read(pc300_t * card, int ch, struct sk_buff *skb)
L
Linus Torvalds 已提交
507 508 509 510 511
{
	int nchar;
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	volatile pcsca_bd_t __iomem *ptdescr;
	int rcvd = 0;
512
	volatile u8 status;
L
Linus Torvalds 已提交
513 514 515 516 517

	ptdescr = (card->hw.rambase +
				  RX_BD_ADDR(ch, chan->rx_first_bd));
	while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
		nchar = cpc_readw(&ptdescr->len);
518 519
		if ((status & (DST_OVR | DST_CRC | DST_RBIT | DST_SHRT | DST_ABT)) ||
		    (nchar > BD_DEF_LEN)) {
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

			if (nchar > BD_DEF_LEN)
				status |= DST_RBIT;
			rcvd = -status;
			/* Discard remaining descriptors used by the bad frame */
			while (chan->rx_first_bd != chan->rx_last_bd) {
				cpc_writeb(&ptdescr->status, 0);
				chan->rx_first_bd = (chan->rx_first_bd+1) & (N_DMA_RX_BUF-1);
				if (status & DST_EOM)
					break;
				ptdescr = (card->hw.rambase +
							  cpc_readl(&ptdescr->next));
				status = cpc_readb(&ptdescr->status);
			}
			break;
		}
		if (nchar != 0) {
			if (skb) {
				memcpy_fromio(skb_put(skb, nchar),
				 (card->hw.rambase+cpc_readl(&ptdescr->ptbuf)),nchar);
			}
			rcvd += nchar;
		}
		cpc_writeb(&ptdescr->status, 0);
		cpc_writeb(&ptdescr->len, 0);
		chan->rx_first_bd = (chan->rx_first_bd + 1) & (N_DMA_RX_BUF - 1);

		if (status & DST_EOM)
			break;

		ptdescr = (card->hw.rambase + cpc_readl(&ptdescr->next));
	}

	if (rcvd != 0) {
		/* Update pointer */
		chan->rx_last_bd = (chan->rx_first_bd - 1) & (N_DMA_RX_BUF - 1);
		/* Update EDA */
		cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
			   RX_BD_ADDR(ch, chan->rx_last_bd));
	}
	return (rcvd);
}

563
static void tx_dma_stop(pc300_t * card, int ch)
L
Linus Torvalds 已提交
564 565
{
	void __iomem *scabase = card->hw.scabase;
566 567
	u8 drr_ena_bit = 1 << (5 + 2 * ch);
	u8 drr_rst_bit = 1 << (1 + 2 * ch);
L
Linus Torvalds 已提交
568 569 570 571 572 573

	/* Disable DMA */
	cpc_writeb(scabase + DRR, drr_ena_bit);
	cpc_writeb(scabase + DRR, drr_rst_bit & ~drr_ena_bit);
}

574
static void rx_dma_stop(pc300_t * card, int ch)
L
Linus Torvalds 已提交
575 576
{
	void __iomem *scabase = card->hw.scabase;
577 578
	u8 drr_ena_bit = 1 << (4 + 2 * ch);
	u8 drr_rst_bit = 1 << (2 * ch);
L
Linus Torvalds 已提交
579 580 581 582 583 584

	/* Disable DMA */
	cpc_writeb(scabase + DRR, drr_ena_bit);
	cpc_writeb(scabase + DRR, drr_rst_bit & ~drr_ena_bit);
}

585
static void rx_dma_start(pc300_t * card, int ch)
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
{
	void __iomem *scabase = card->hw.scabase;
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	
	/* Start DMA */
	cpc_writel(scabase + DRX_REG(CDAL, ch),
		   RX_BD_ADDR(ch, chan->rx_first_bd));
	if (cpc_readl(scabase + DRX_REG(CDAL,ch)) !=
				  RX_BD_ADDR(ch, chan->rx_first_bd)) {
		cpc_writel(scabase + DRX_REG(CDAL, ch),
				   RX_BD_ADDR(ch, chan->rx_first_bd));
	}
	cpc_writel(scabase + DRX_REG(EDAL, ch),
		   RX_BD_ADDR(ch, chan->rx_last_bd));
	cpc_writew(scabase + DRX_REG(BFLL, ch), BD_DEF_LEN);
	cpc_writeb(scabase + DSR_RX(ch), DSR_DE);
	if (!(cpc_readb(scabase + DSR_RX(ch)) & DSR_DE)) {
	cpc_writeb(scabase + DSR_RX(ch), DSR_DE);
	}
}

/*************************/
/***   FALC Routines   ***/
/*************************/
610
static void falc_issue_cmd(pc300_t *card, int ch, u8 cmd)
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624
{
	void __iomem *falcbase = card->hw.falcbase;
	unsigned long i = 0;

	while (cpc_readb(falcbase + F_REG(SIS, ch)) & SIS_CEC) {
		if (i++ >= PC300_FALC_MAXLOOP) {
			printk("%s: FALC command locked(cmd=0x%x).\n",
			       card->chan[ch].d.name, cmd);
			break;
		}
	}
	cpc_writeb(falcbase + F_REG(CMDR, ch), cmd);
}

625
static void falc_intr_enable(pc300_t * card, int ch)
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	/* Interrupt pins are open-drain */
	cpc_writeb(falcbase + F_REG(IPC, ch),
		   cpc_readb(falcbase + F_REG(IPC, ch)) & ~IPC_IC0);
	/* Conters updated each second */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_ECM);
	/* Enable SEC and ES interrupts  */
	cpc_writeb(falcbase + F_REG(IMR3, ch),
		   cpc_readb(falcbase + F_REG(IMR3, ch)) & ~(IMR3_SEC | IMR3_ES));
	if (conf->fr_mode == PC300_FR_UNFRAMED) {
		cpc_writeb(falcbase + F_REG(IMR4, ch),
			   cpc_readb(falcbase + F_REG(IMR4, ch)) & ~(IMR4_LOS));
	} else {
		cpc_writeb(falcbase + F_REG(IMR4, ch),
			   cpc_readb(falcbase + F_REG(IMR4, ch)) &
			   ~(IMR4_LFA | IMR4_AIS | IMR4_LOS | IMR4_SLIP));
	}
	if (conf->media == IF_IFACE_T1) {
		cpc_writeb(falcbase + F_REG(IMR3, ch),
			   cpc_readb(falcbase + F_REG(IMR3, ch)) & ~IMR3_LLBSC);
	} else {
		cpc_writeb(falcbase + F_REG(IPC, ch),
			   cpc_readb(falcbase + F_REG(IPC, ch)) | IPC_SCI);
		if (conf->fr_mode == PC300_FR_UNFRAMED) {
			cpc_writeb(falcbase + F_REG(IMR2, ch),
				   cpc_readb(falcbase + F_REG(IMR2, ch)) & ~(IMR2_LOS));
		} else {
			cpc_writeb(falcbase + F_REG(IMR2, ch),
				   cpc_readb(falcbase + F_REG(IMR2, ch)) &
				   ~(IMR2_FAR | IMR2_LFA | IMR2_AIS | IMR2_LOS));
			if (pfalc->multiframe_mode) {
				cpc_writeb(falcbase + F_REG(IMR2, ch),
					   cpc_readb(falcbase + F_REG(IMR2, ch)) & 
					   ~(IMR2_T400MS | IMR2_MFAR));
			} else {
				cpc_writeb(falcbase + F_REG(IMR2, ch),
					   cpc_readb(falcbase + F_REG(IMR2, ch)) | 
					   IMR2_T400MS | IMR2_MFAR);
			}
		}
	}
}

675
static void falc_open_timeslot(pc300_t * card, int ch, int timeslot)
L
Linus Torvalds 已提交
676 677
{
	void __iomem *falcbase = card->hw.falcbase;
678
	u8 tshf = card->chan[ch].falc.offset;
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687 688 689 690

	cpc_writeb(falcbase + F_REG((ICB1 + (timeslot - tshf) / 8), ch),
		   cpc_readb(falcbase + F_REG((ICB1 + (timeslot - tshf) / 8), ch)) & 
		   	~(0x80 >> ((timeslot - tshf) & 0x07)));
	cpc_writeb(falcbase + F_REG((TTR1 + timeslot / 8), ch),
		   cpc_readb(falcbase + F_REG((TTR1 + timeslot / 8), ch)) | 
   			(0x80 >> (timeslot & 0x07)));
	cpc_writeb(falcbase + F_REG((RTR1 + timeslot / 8), ch),
		   cpc_readb(falcbase + F_REG((RTR1 + timeslot / 8), ch)) | 
			(0x80 >> (timeslot & 0x07)));
}

691
static void falc_close_timeslot(pc300_t * card, int ch, int timeslot)
L
Linus Torvalds 已提交
692 693
{
	void __iomem *falcbase = card->hw.falcbase;
694
	u8 tshf = card->chan[ch].falc.offset;
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702 703 704 705 706

	cpc_writeb(falcbase + F_REG((ICB1 + (timeslot - tshf) / 8), ch),
		   cpc_readb(falcbase + F_REG((ICB1 + (timeslot - tshf) / 8), ch)) | 
		   (0x80 >> ((timeslot - tshf) & 0x07)));
	cpc_writeb(falcbase + F_REG((TTR1 + timeslot / 8), ch),
		   cpc_readb(falcbase + F_REG((TTR1 + timeslot / 8), ch)) & 
		   ~(0x80 >> (timeslot & 0x07)));
	cpc_writeb(falcbase + F_REG((RTR1 + timeslot / 8), ch),
		   cpc_readb(falcbase + F_REG((RTR1 + timeslot / 8), ch)) & 
		   ~(0x80 >> (timeslot & 0x07)));
}

707
static void falc_close_all_timeslots(pc300_t * card, int ch)
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	void __iomem *falcbase = card->hw.falcbase;

	cpc_writeb(falcbase + F_REG(ICB1, ch), 0xff);
	cpc_writeb(falcbase + F_REG(TTR1, ch), 0);
	cpc_writeb(falcbase + F_REG(RTR1, ch), 0);
	cpc_writeb(falcbase + F_REG(ICB2, ch), 0xff);
	cpc_writeb(falcbase + F_REG(TTR2, ch), 0);
	cpc_writeb(falcbase + F_REG(RTR2, ch), 0);
	cpc_writeb(falcbase + F_REG(ICB3, ch), 0xff);
	cpc_writeb(falcbase + F_REG(TTR3, ch), 0);
	cpc_writeb(falcbase + F_REG(RTR3, ch), 0);
	if (conf->media == IF_IFACE_E1) {
		cpc_writeb(falcbase + F_REG(ICB4, ch), 0xff);
		cpc_writeb(falcbase + F_REG(TTR4, ch), 0);
		cpc_writeb(falcbase + F_REG(RTR4, ch), 0);
	}
}

729
static void falc_open_all_timeslots(pc300_t * card, int ch)
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	void __iomem *falcbase = card->hw.falcbase;

	cpc_writeb(falcbase + F_REG(ICB1, ch), 0);
	if (conf->fr_mode == PC300_FR_UNFRAMED) {
		cpc_writeb(falcbase + F_REG(TTR1, ch), 0xff);
		cpc_writeb(falcbase + F_REG(RTR1, ch), 0xff);
	} else {
		/* Timeslot 0 is never enabled */
		cpc_writeb(falcbase + F_REG(TTR1, ch), 0x7f);
		cpc_writeb(falcbase + F_REG(RTR1, ch), 0x7f);
	}
	cpc_writeb(falcbase + F_REG(ICB2, ch), 0);
	cpc_writeb(falcbase + F_REG(TTR2, ch), 0xff);
	cpc_writeb(falcbase + F_REG(RTR2, ch), 0xff);
	cpc_writeb(falcbase + F_REG(ICB3, ch), 0);
	cpc_writeb(falcbase + F_REG(TTR3, ch), 0xff);
	cpc_writeb(falcbase + F_REG(RTR3, ch), 0xff);
	if (conf->media == IF_IFACE_E1) {
		cpc_writeb(falcbase + F_REG(ICB4, ch), 0);
		cpc_writeb(falcbase + F_REG(TTR4, ch), 0xff);
		cpc_writeb(falcbase + F_REG(RTR4, ch), 0xff);
	} else {
		cpc_writeb(falcbase + F_REG(ICB4, ch), 0xff);
		cpc_writeb(falcbase + F_REG(TTR4, ch), 0x80);
		cpc_writeb(falcbase + F_REG(RTR4, ch), 0x80);
	}
}

761
static void falc_init_timeslot(pc300_t * card, int ch)
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	int tslot;

	for (tslot = 0; tslot < pfalc->num_channels; tslot++) {
		if (conf->tslot_bitmap & (1 << tslot)) {
			// Channel enabled
			falc_open_timeslot(card, ch, tslot + 1);
		} else {
			// Channel disabled
			falc_close_timeslot(card, ch, tslot + 1);
		}
	}
}

779
static void falc_enable_comm(pc300_t * card, int ch)
L
Linus Torvalds 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;

	if (pfalc->full_bandwidth) {
		falc_open_all_timeslots(card, ch);
	} else {
		falc_init_timeslot(card, ch);
	}
	// CTS/DCD ON
	cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
		   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) &
		   ~((CPLD_REG1_FALC_DCD | CPLD_REG1_FALC_CTS) << (2 * ch)));
}

795
static void falc_disable_comm(pc300_t * card, int ch)
L
Linus Torvalds 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;

	if (pfalc->loop_active != 2) {
		falc_close_all_timeslots(card, ch);
	}
	// CTS/DCD OFF
	cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
		   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) |
		   ((CPLD_REG1_FALC_DCD | CPLD_REG1_FALC_CTS) << (2 * ch)));
}

809
static void falc_init_t1(pc300_t * card, int ch)
L
Linus Torvalds 已提交
810 811 812 813 814
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
815
	u8 dja = (ch ? (LIM2_DJA2 | LIM2_DJA1) : 0);
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

	/* Switch to T1 mode (PCM 24) */
	cpc_writeb(falcbase + F_REG(FMR1, ch), FMR1_PMOD);

	/* Wait 20 us for setup */
	udelay(20);

	/* Transmit Buffer Size (1 frame) */
	cpc_writeb(falcbase + F_REG(SIC1, ch), SIC1_XBS0);

	/* Clock mode */
	if (conf->phys_settings.clock_type == CLOCK_INT) { /* Master mode */
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) | LIM0_MAS);
	} else { /* Slave mode */
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~LIM0_MAS);
		cpc_writeb(falcbase + F_REG(LOOP, ch),
			   cpc_readb(falcbase + F_REG(LOOP, ch)) & ~LOOP_RTM);
	}

	cpc_writeb(falcbase + F_REG(IPC, ch), IPC_SCI);
	cpc_writeb(falcbase + F_REG(FMR0, ch),
		   cpc_readb(falcbase + F_REG(FMR0, ch)) &
		   ~(FMR0_XC0 | FMR0_XC1 | FMR0_RC0 | FMR0_RC1));

	switch (conf->lcode) {
		case PC300_LC_AMI:
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) |
				   FMR0_XC1 | FMR0_RC1);
			/* Clear Channel register to ON for all channels */
			cpc_writeb(falcbase + F_REG(CCB1, ch), 0xff);
			cpc_writeb(falcbase + F_REG(CCB2, ch), 0xff);
			cpc_writeb(falcbase + F_REG(CCB3, ch), 0xff);
			break;

		case PC300_LC_B8ZS:
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) |
				   FMR0_XC0 | FMR0_XC1 | FMR0_RC0 | FMR0_RC1);
			break;

		case PC300_LC_NRZ:
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) | 0x00);
			break;
	}

	cpc_writeb(falcbase + F_REG(LIM0, ch),
		   cpc_readb(falcbase + F_REG(LIM0, ch)) | LIM0_ELOS);
	cpc_writeb(falcbase + F_REG(LIM0, ch),
		   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~(LIM0_SCL1 | LIM0_SCL0));
	/* Set interface mode to 2 MBPS */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_IMOD);

	switch (conf->fr_mode) {
		case PC300_FR_ESF:
			pfalc->multiframe_mode = 0;
			cpc_writeb(falcbase + F_REG(FMR4, ch),
				   cpc_readb(falcbase + F_REG(FMR4, ch)) | FMR4_FM1);
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) | 
				   FMR1_CRC | FMR1_EDL);
			cpc_writeb(falcbase + F_REG(XDL1, ch), 0);
			cpc_writeb(falcbase + F_REG(XDL2, ch), 0);
			cpc_writeb(falcbase + F_REG(XDL3, ch), 0);
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) & ~FMR0_SRAF);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2,ch)) | FMR2_MCSP | FMR2_SSP);
			break;

		case PC300_FR_D4:
			pfalc->multiframe_mode = 1;
			cpc_writeb(falcbase + F_REG(FMR4, ch),
				   cpc_readb(falcbase + F_REG(FMR4, ch)) &
				   ~(FMR4_FM1 | FMR4_FM0));
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) | FMR0_SRAF);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) & ~FMR2_SSP);
			break;
	}

	/* Enable Automatic Resynchronization */
	cpc_writeb(falcbase + F_REG(FMR4, ch),
		   cpc_readb(falcbase + F_REG(FMR4, ch)) | FMR4_AUTO);

	/* Transmit Automatic Remote Alarm */
	cpc_writeb(falcbase + F_REG(FMR2, ch),
		   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_AXRA);

	/* Channel translation mode 1 : one to one */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_CTM);

	/* No signaling */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) & ~FMR1_SIGM);
	cpc_writeb(falcbase + F_REG(FMR5, ch),
		   cpc_readb(falcbase + F_REG(FMR5, ch)) &
		   ~(FMR5_EIBR | FMR5_SRS));
	cpc_writeb(falcbase + F_REG(CCR1, ch), 0);

	cpc_writeb(falcbase + F_REG(LIM1, ch),
		   cpc_readb(falcbase + F_REG(LIM1, ch)) | LIM1_RIL0 | LIM1_RIL1);

	switch (conf->lbo) {
			/* Provides proper Line Build Out */
		case PC300_LBO_0_DB:
			cpc_writeb(falcbase + F_REG(LIM2, ch), (LIM2_LOS1 | dja));
			cpc_writeb(falcbase + F_REG(XPM0, ch), 0x5a);
			cpc_writeb(falcbase + F_REG(XPM1, ch), 0x8f);
			cpc_writeb(falcbase + F_REG(XPM2, ch), 0x20);
			break;
		case PC300_LBO_7_5_DB:
			cpc_writeb(falcbase + F_REG(LIM2, ch), (0x40 | LIM2_LOS1 | dja));
			cpc_writeb(falcbase + F_REG(XPM0, ch), 0x11);
			cpc_writeb(falcbase + F_REG(XPM1, ch), 0x02);
			cpc_writeb(falcbase + F_REG(XPM2, ch), 0x20);
			break;
		case PC300_LBO_15_DB:
			cpc_writeb(falcbase + F_REG(LIM2, ch), (0x80 | LIM2_LOS1 | dja));
			cpc_writeb(falcbase + F_REG(XPM0, ch), 0x8e);
			cpc_writeb(falcbase + F_REG(XPM1, ch), 0x01);
			cpc_writeb(falcbase + F_REG(XPM2, ch), 0x20);
			break;
		case PC300_LBO_22_5_DB:
			cpc_writeb(falcbase + F_REG(LIM2, ch), (0xc0 | LIM2_LOS1 | dja));
			cpc_writeb(falcbase + F_REG(XPM0, ch), 0x09);
			cpc_writeb(falcbase + F_REG(XPM1, ch), 0x01);
			cpc_writeb(falcbase + F_REG(XPM2, ch), 0x20);
			break;
	}

	/* Transmit Clock-Slot Offset */
	cpc_writeb(falcbase + F_REG(XC0, ch),
		   cpc_readb(falcbase + F_REG(XC0, ch)) | 0x01);
	/* Transmit Time-slot Offset */
	cpc_writeb(falcbase + F_REG(XC1, ch), 0x3e);
	/* Receive  Clock-Slot offset */
	cpc_writeb(falcbase + F_REG(RC0, ch), 0x05);
	/* Receive  Time-slot offset */
	cpc_writeb(falcbase + F_REG(RC1, ch), 0x00);

	/* LOS Detection after 176 consecutive 0s */
	cpc_writeb(falcbase + F_REG(PCDR, ch), 0x0a);
	/* LOS Recovery after 22 ones in the time window of PCD */
	cpc_writeb(falcbase + F_REG(PCRR, ch), 0x15);

	cpc_writeb(falcbase + F_REG(IDLE, ch), 0x7f);

	if (conf->fr_mode == PC300_FR_ESF_JAPAN) {
		cpc_writeb(falcbase + F_REG(RC1, ch),
			   cpc_readb(falcbase + F_REG(RC1, ch)) | 0x80);
	}

	falc_close_all_timeslots(card, ch);
}

978
static void falc_init_e1(pc300_t * card, int ch)
L
Linus Torvalds 已提交
979 980 981 982 983
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
984
	u8 dja = (ch ? (LIM2_DJA2 | LIM2_DJA1) : 0);
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

	/* Switch to E1 mode (PCM 30) */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) & ~FMR1_PMOD);

	/* Clock mode */
	if (conf->phys_settings.clock_type == CLOCK_INT) { /* Master mode */
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) | LIM0_MAS);
	} else { /* Slave mode */
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~LIM0_MAS);
	}
	cpc_writeb(falcbase + F_REG(LOOP, ch),
		   cpc_readb(falcbase + F_REG(LOOP, ch)) & ~LOOP_SFM);

	cpc_writeb(falcbase + F_REG(IPC, ch), IPC_SCI);
	cpc_writeb(falcbase + F_REG(FMR0, ch),
		   cpc_readb(falcbase + F_REG(FMR0, ch)) &
		   ~(FMR0_XC0 | FMR0_XC1 | FMR0_RC0 | FMR0_RC1));

	switch (conf->lcode) {
		case PC300_LC_AMI:
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) |
				   FMR0_XC1 | FMR0_RC1);
			break;

		case PC300_LC_HDB3:
			cpc_writeb(falcbase + F_REG(FMR0, ch),
				   cpc_readb(falcbase + F_REG(FMR0, ch)) |
				   FMR0_XC0 | FMR0_XC1 | FMR0_RC0 | FMR0_RC1);
			break;

		case PC300_LC_NRZ:
			break;
	}

	cpc_writeb(falcbase + F_REG(LIM0, ch),
		   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~(LIM0_SCL1 | LIM0_SCL0));
	/* Set interface mode to 2 MBPS */
	cpc_writeb(falcbase + F_REG(FMR1, ch),
		   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_IMOD);

	cpc_writeb(falcbase + F_REG(XPM0, ch), 0x18);
	cpc_writeb(falcbase + F_REG(XPM1, ch), 0x03);
	cpc_writeb(falcbase + F_REG(XPM2, ch), 0x00);

	switch (conf->fr_mode) {
		case PC300_FR_MF_CRC4:
			pfalc->multiframe_mode = 1;
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_XFS);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_RFS1);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) & ~FMR2_RFS0);
			cpc_writeb(falcbase + F_REG(FMR3, ch),
				   cpc_readb(falcbase + F_REG(FMR3, ch)) & ~FMR3_EXTIW);

			/* MultiFrame Resynchronization */
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_MFCS);

			/* Automatic Loss of Multiframe > 914 CRC errors */
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_ALMF);

			/* S1 and SI1/SI2 spare Bits set to 1 */
			cpc_writeb(falcbase + F_REG(XSP, ch),
				   cpc_readb(falcbase + F_REG(XSP, ch)) & ~XSP_AXS);
			cpc_writeb(falcbase + F_REG(XSP, ch),
				   cpc_readb(falcbase + F_REG(XSP, ch)) | XSP_EBP);
			cpc_writeb(falcbase + F_REG(XSP, ch),
				   cpc_readb(falcbase + F_REG(XSP, ch)) | XSP_XS13 | XSP_XS15);

			/* Automatic Force Resynchronization */
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_AFR);

			/* Transmit Automatic Remote Alarm */
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_AXRA);

			/* Transmit Spare Bits for National Use (Y, Sn, Sa) */
			cpc_writeb(falcbase + F_REG(XSW, ch),
				   cpc_readb(falcbase + F_REG(XSW, ch)) |
				   XSW_XY0 | XSW_XY1 | XSW_XY2 | XSW_XY3 | XSW_XY4);
			break;

		case PC300_FR_MF_NON_CRC4:
		case PC300_FR_D4:
			pfalc->multiframe_mode = 0;
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) & ~FMR1_XFS);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) & 
				   ~(FMR2_RFS1 | FMR2_RFS0));
			cpc_writeb(falcbase + F_REG(XSW, ch),
				   cpc_readb(falcbase + F_REG(XSW, ch)) | XSW_XSIS);
			cpc_writeb(falcbase + F_REG(XSP, ch),
				   cpc_readb(falcbase + F_REG(XSP, ch)) | XSP_XSIF);

			/* Automatic Force Resynchronization */
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) | FMR1_AFR);

			/* Transmit Automatic Remote Alarm */
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_AXRA);

			/* Transmit Spare Bits for National Use (Y, Sn, Sa) */
			cpc_writeb(falcbase + F_REG(XSW, ch),
				   cpc_readb(falcbase + F_REG(XSW, ch)) |
				   XSW_XY0 | XSW_XY1 | XSW_XY2 | XSW_XY3 | XSW_XY4);
			break;

		case PC300_FR_UNFRAMED:
			pfalc->multiframe_mode = 0;
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) & ~FMR1_XFS);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) & 
				   ~(FMR2_RFS1 | FMR2_RFS0));
			cpc_writeb(falcbase + F_REG(XSP, ch),
				   cpc_readb(falcbase + F_REG(XSP, ch)) | XSP_TT0);
			cpc_writeb(falcbase + F_REG(XSW, ch),
				   cpc_readb(falcbase + F_REG(XSW, ch)) & 
				   ~(XSW_XTM|XSW_XY0|XSW_XY1|XSW_XY2|XSW_XY3|XSW_XY4));
			cpc_writeb(falcbase + F_REG(TSWM, ch), 0xff);
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) |
				   (FMR2_RTM | FMR2_DAIS));
			cpc_writeb(falcbase + F_REG(FMR2, ch),
				   cpc_readb(falcbase + F_REG(FMR2, ch)) & ~FMR2_AXRA);
			cpc_writeb(falcbase + F_REG(FMR1, ch),
				   cpc_readb(falcbase + F_REG(FMR1, ch)) & ~FMR1_AFR);
			pfalc->sync = 1;
			cpc_writeb(falcbase + card->hw.cpld_reg2,
				   cpc_readb(falcbase + card->hw.cpld_reg2) |
				   (CPLD_REG2_FALC_LED2 << (2 * ch)));
			break;
	}

	/* No signaling */
	cpc_writeb(falcbase + F_REG(XSP, ch),
		   cpc_readb(falcbase + F_REG(XSP, ch)) & ~XSP_CASEN);
	cpc_writeb(falcbase + F_REG(CCR1, ch), 0);

	cpc_writeb(falcbase + F_REG(LIM1, ch),
		   cpc_readb(falcbase + F_REG(LIM1, ch)) | LIM1_RIL0 | LIM1_RIL1);
	cpc_writeb(falcbase + F_REG(LIM2, ch), (LIM2_LOS1 | dja));

	/* Transmit Clock-Slot Offset */
	cpc_writeb(falcbase + F_REG(XC0, ch),
		   cpc_readb(falcbase + F_REG(XC0, ch)) | 0x01);
	/* Transmit Time-slot Offset */
	cpc_writeb(falcbase + F_REG(XC1, ch), 0x3e);
	/* Receive  Clock-Slot offset */
	cpc_writeb(falcbase + F_REG(RC0, ch), 0x05);
	/* Receive  Time-slot offset */
	cpc_writeb(falcbase + F_REG(RC1, ch), 0x00);

	/* LOS Detection after 176 consecutive 0s */
	cpc_writeb(falcbase + F_REG(PCDR, ch), 0x0a);
	/* LOS Recovery after 22 ones in the time window of PCD */
	cpc_writeb(falcbase + F_REG(PCRR, ch), 0x15);

	cpc_writeb(falcbase + F_REG(IDLE, ch), 0x7f);

	falc_close_all_timeslots(card, ch);
}

1158
static void falc_init_hdlc(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
{
	void __iomem *falcbase = card->hw.falcbase;
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;

	/* Enable transparent data transfer */
	if (conf->fr_mode == PC300_FR_UNFRAMED) {
		cpc_writeb(falcbase + F_REG(MODE, ch), 0);
	} else {
		cpc_writeb(falcbase + F_REG(MODE, ch),
			   cpc_readb(falcbase + F_REG(MODE, ch)) |
			   (MODE_HRAC | MODE_MDS2));
		cpc_writeb(falcbase + F_REG(RAH2, ch), 0xff);
		cpc_writeb(falcbase + F_REG(RAH1, ch), 0xff);
		cpc_writeb(falcbase + F_REG(RAL2, ch), 0xff);
		cpc_writeb(falcbase + F_REG(RAL1, ch), 0xff);
	}

	/* Tx/Rx reset  */
	falc_issue_cmd(card, ch, CMDR_RRES | CMDR_XRES | CMDR_SRES);

	/* Enable interrupt sources */
	falc_intr_enable(card, ch);
}

1184
static void te_config(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1185 1186 1187 1188 1189
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
1190
	u8 dummy;
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	unsigned long flags;

	memset(pfalc, 0, sizeof(falc_t));
	switch (conf->media) {
		case IF_IFACE_T1:
			pfalc->num_channels = NUM_OF_T1_CHANNELS;
			pfalc->offset = 1;
			break;
		case IF_IFACE_E1:
			pfalc->num_channels = NUM_OF_E1_CHANNELS;
			pfalc->offset = 0;
			break;
	}
	if (conf->tslot_bitmap == 0xffffffffUL)
		pfalc->full_bandwidth = 1;
	else
		pfalc->full_bandwidth = 0;

	CPC_LOCK(card, flags);
	/* Reset the FALC chip */
	cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
		   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) |
		   (CPLD_REG1_FALC_RESET << (2 * ch)));
	udelay(10000);
	cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
		   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) &
		   ~(CPLD_REG1_FALC_RESET << (2 * ch)));

	if (conf->media == IF_IFACE_T1) {
		falc_init_t1(card, ch);
	} else {
		falc_init_e1(card, ch);
	}
	falc_init_hdlc(card, ch);
	if (conf->rx_sens == PC300_RX_SENS_SH) {
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~LIM0_EQON);
	} else {
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) | LIM0_EQON);
	}
	cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
		   cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
		   ((CPLD_REG2_FALC_TX_CLK | CPLD_REG2_FALC_RX_CLK) << (2 * ch)));

	/* Clear all interrupt registers */
	dummy = cpc_readb(falcbase + F_REG(FISR0, ch)) +
		cpc_readb(falcbase + F_REG(FISR1, ch)) +
		cpc_readb(falcbase + F_REG(FISR2, ch)) +
		cpc_readb(falcbase + F_REG(FISR3, ch));
	CPC_UNLOCK(card, flags);
}

1244
static void falc_check_status(pc300_t * card, int ch, unsigned char frs0)
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	/* Verify LOS */
	if (frs0 & FRS0_LOS) {
		if (!pfalc->red_alarm) {
			pfalc->red_alarm = 1;
			pfalc->los++;
			if (!pfalc->blue_alarm) {
				// EVENT_FALC_ABNORMAL
				if (conf->media == IF_IFACE_T1) {
					/* Disable this interrupt as it may otherwise interfere 
					 * with other working boards. */
					cpc_writeb(falcbase + F_REG(IMR0, ch), 
						   cpc_readb(falcbase + F_REG(IMR0, ch))
						   | IMR0_PDEN);
				}
				falc_disable_comm(card, ch);
				// EVENT_FALC_ABNORMAL
			}
		}
	} else {
		if (pfalc->red_alarm) {
			pfalc->red_alarm = 0;
			pfalc->losr++;
		}
	}

	if (conf->fr_mode != PC300_FR_UNFRAMED) {
		/* Verify AIS alarm */
		if (frs0 & FRS0_AIS) {
			if (!pfalc->blue_alarm) {
				pfalc->blue_alarm = 1;
				pfalc->ais++;
				// EVENT_AIS
				if (conf->media == IF_IFACE_T1) {
					/* Disable this interrupt as it may otherwise interfere with                       other working boards. */
					cpc_writeb(falcbase + F_REG(IMR0, ch),
						   cpc_readb(falcbase + F_REG(IMR0, ch)) | IMR0_PDEN);
				}
				falc_disable_comm(card, ch);
				// EVENT_AIS
			}
		} else {
			pfalc->blue_alarm = 0;
		}

		/* Verify LFA */
		if (frs0 & FRS0_LFA) {
			if (!pfalc->loss_fa) {
				pfalc->loss_fa = 1;
				pfalc->lfa++;
				if (!pfalc->blue_alarm && !pfalc->red_alarm) {
					// EVENT_FALC_ABNORMAL
					if (conf->media == IF_IFACE_T1) {
						/* Disable this interrupt as it may otherwise 
						 * interfere with other working boards. */
						cpc_writeb(falcbase + F_REG(IMR0, ch),
							   cpc_readb(falcbase + F_REG(IMR0, ch))
							   | IMR0_PDEN);
					}
					falc_disable_comm(card, ch);
					// EVENT_FALC_ABNORMAL
				}
			}
		} else {
			if (pfalc->loss_fa) {
				pfalc->loss_fa = 0;
				pfalc->farec++;
			}
		}

		/* Verify LMFA */
		if (pfalc->multiframe_mode && (frs0 & FRS0_LMFA)) {
			/* D4 or CRC4 frame mode */
			if (!pfalc->loss_mfa) {
				pfalc->loss_mfa = 1;
				pfalc->lmfa++;
				if (!pfalc->blue_alarm && !pfalc->red_alarm &&
				    !pfalc->loss_fa) {
					// EVENT_FALC_ABNORMAL
					if (conf->media == IF_IFACE_T1) {
						/* Disable this interrupt as it may otherwise 
						 * interfere with other working boards. */
						cpc_writeb(falcbase + F_REG(IMR0, ch),
							   cpc_readb(falcbase + F_REG(IMR0, ch))
							   | IMR0_PDEN);
					}
					falc_disable_comm(card, ch);
					// EVENT_FALC_ABNORMAL
				}
			}
		} else {
			pfalc->loss_mfa = 0;
		}

		/* Verify Remote Alarm */
		if (frs0 & FRS0_RRA) {
			if (!pfalc->yellow_alarm) {
				pfalc->yellow_alarm = 1;
				pfalc->rai++;
				if (pfalc->sync) {
					// EVENT_RAI
					falc_disable_comm(card, ch);
					// EVENT_RAI
				}
			}
		} else {
			pfalc->yellow_alarm = 0;
		}
	} /* if !PC300_UNFRAMED */

	if (pfalc->red_alarm || pfalc->loss_fa ||
	    pfalc->loss_mfa || pfalc->blue_alarm) {
		if (pfalc->sync) {
			pfalc->sync = 0;
			chan->d.line_off++;
			cpc_writeb(falcbase + card->hw.cpld_reg2,
				   cpc_readb(falcbase + card->hw.cpld_reg2) &
				   ~(CPLD_REG2_FALC_LED2 << (2 * ch)));
		}
	} else {
		if (!pfalc->sync) {
			pfalc->sync = 1;
			chan->d.line_on++;
			cpc_writeb(falcbase + card->hw.cpld_reg2,
				   cpc_readb(falcbase + card->hw.cpld_reg2) |
				   (CPLD_REG2_FALC_LED2 << (2 * ch)));
		}
	}

	if (pfalc->sync && !pfalc->yellow_alarm) {
		if (!pfalc->active) {
			// EVENT_FALC_NORMAL
			if (pfalc->loop_active) {
				return;
			}
			if (conf->media == IF_IFACE_T1) {
				cpc_writeb(falcbase + F_REG(IMR0, ch),
					   cpc_readb(falcbase + F_REG(IMR0, ch)) & ~IMR0_PDEN);
			}
			falc_enable_comm(card, ch);
			// EVENT_FALC_NORMAL
			pfalc->active = 1;
		}
	} else {
		if (pfalc->active) {
			pfalc->active = 0;
		}
	}
}

1400
static void falc_update_stats(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
1406
	u16 counter;
L
Linus Torvalds 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431

	counter = cpc_readb(falcbase + F_REG(FECL, ch));
	counter |= cpc_readb(falcbase + F_REG(FECH, ch)) << 8;
	pfalc->fec += counter;

	counter = cpc_readb(falcbase + F_REG(CVCL, ch));
	counter |= cpc_readb(falcbase + F_REG(CVCH, ch)) << 8;
	pfalc->cvc += counter;

	counter = cpc_readb(falcbase + F_REG(CECL, ch));
	counter |= cpc_readb(falcbase + F_REG(CECH, ch)) << 8;
	pfalc->cec += counter;

	counter = cpc_readb(falcbase + F_REG(EBCL, ch));
	counter |= cpc_readb(falcbase + F_REG(EBCH, ch)) << 8;
	pfalc->ebc += counter;

	if (cpc_readb(falcbase + F_REG(LCR1, ch)) & LCR1_EPRM) {
		mdelay(10);
		counter = cpc_readb(falcbase + F_REG(BECL, ch));
		counter |= cpc_readb(falcbase + F_REG(BECH, ch)) << 8;
		pfalc->bec += counter;

		if (((conf->media == IF_IFACE_T1) &&
		     (cpc_readb(falcbase + F_REG(FRS1, ch)) & FRS1_LLBAD) &&
1432
		     (!(cpc_readb(falcbase + F_REG(FRS1, ch)) & FRS1_PDEN))) ||
L
Linus Torvalds 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
		    ((conf->media == IF_IFACE_E1) &&
		     (cpc_readb(falcbase + F_REG(RSP, ch)) & RSP_LLBAD))) {
			pfalc->prbs = 2;
		} else {
			pfalc->prbs = 1;
		}
	}
}

/*----------------------------------------------------------------------------
 * falc_remote_loop
 *----------------------------------------------------------------------------
 * Description:	In the remote loopback mode the clock and data recovered
 *		from the line inputs RL1/2 or RDIP/RDIN are routed back
 *		to the line outputs XL1/2 or XDOP/XDON via the analog
 *		transmitter. As in normal mode they are processsed by
 *		the synchronizer and then sent to the system interface.
 *----------------------------------------------------------------------------
 */
1452
static void falc_remote_loop(pc300_t * card, int ch, int loop_on)
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (loop_on) {
		// EVENT_FALC_ABNORMAL
		if (conf->media == IF_IFACE_T1) {
			/* Disable this interrupt as it may otherwise interfere with 
			 * other working boards. */
			cpc_writeb(falcbase + F_REG(IMR0, ch),
				   cpc_readb(falcbase + F_REG(IMR0, ch)) | IMR0_PDEN);
		}
		falc_disable_comm(card, ch);
		// EVENT_FALC_ABNORMAL
		cpc_writeb(falcbase + F_REG(LIM1, ch),
			   cpc_readb(falcbase + F_REG(LIM1, ch)) | LIM1_RL);
		pfalc->loop_active = 1;
	} else {
		cpc_writeb(falcbase + F_REG(LIM1, ch),
			   cpc_readb(falcbase + F_REG(LIM1, ch)) & ~LIM1_RL);
		pfalc->sync = 0;
		cpc_writeb(falcbase + card->hw.cpld_reg2,
			   cpc_readb(falcbase + card->hw.cpld_reg2) &
			   ~(CPLD_REG2_FALC_LED2 << (2 * ch)));
		pfalc->active = 0;
		falc_issue_cmd(card, ch, CMDR_XRES);
		pfalc->loop_active = 0;
	}
}

/*----------------------------------------------------------------------------
 * falc_local_loop
 *----------------------------------------------------------------------------
 * Description: The local loopback mode disconnects the receive lines 
 *		RL1/RL2 resp. RDIP/RDIN from the receiver. Instead of the
 *		signals coming from the line the data provided by system
 *		interface are routed through the analog receiver back to
 *		the system interface. The unipolar bit stream will be
 *		undisturbed transmitted on the line. Receiver and transmitter
 *		coding must be identical.
 *----------------------------------------------------------------------------
 */
1497
static void falc_local_loop(pc300_t * card, int ch, int loop_on)
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (loop_on) {
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) | LIM0_LL);
		pfalc->loop_active = 1;
	} else {
		cpc_writeb(falcbase + F_REG(LIM0, ch),
			   cpc_readb(falcbase + F_REG(LIM0, ch)) & ~LIM0_LL);
		pfalc->loop_active = 0;
	}
}

/*----------------------------------------------------------------------------
 * falc_payload_loop
 *----------------------------------------------------------------------------
 * Description: This routine allows to enable/disable payload loopback.
 *		When the payload loop is activated, the received 192 bits
 *		of payload data will be looped back to the transmit
 *		direction. The framing bits, CRC6 and DL bits are not 
 *		looped. They are originated by the FALC-LH transmitter.
 *----------------------------------------------------------------------------
 */
1524
static void falc_payload_loop(pc300_t * card, int ch, int loop_on)
L
Linus Torvalds 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (loop_on) {
		// EVENT_FALC_ABNORMAL
		if (conf->media == IF_IFACE_T1) {
			/* Disable this interrupt as it may otherwise interfere with 
			 * other working boards. */
			cpc_writeb(falcbase + F_REG(IMR0, ch),
				   cpc_readb(falcbase + F_REG(IMR0, ch)) | IMR0_PDEN);
		}
		falc_disable_comm(card, ch);
		// EVENT_FALC_ABNORMAL
		cpc_writeb(falcbase + F_REG(FMR2, ch),
			   cpc_readb(falcbase + F_REG(FMR2, ch)) | FMR2_PLB);
		if (conf->media == IF_IFACE_T1) {
			cpc_writeb(falcbase + F_REG(FMR4, ch),
				   cpc_readb(falcbase + F_REG(FMR4, ch)) | FMR4_TM);
		} else {
			cpc_writeb(falcbase + F_REG(FMR5, ch),
				   cpc_readb(falcbase + F_REG(FMR5, ch)) | XSP_TT0);
		}
		falc_open_all_timeslots(card, ch);
		pfalc->loop_active = 2;
	} else {
		cpc_writeb(falcbase + F_REG(FMR2, ch),
			   cpc_readb(falcbase + F_REG(FMR2, ch)) & ~FMR2_PLB);
		if (conf->media == IF_IFACE_T1) {
			cpc_writeb(falcbase + F_REG(FMR4, ch),
				   cpc_readb(falcbase + F_REG(FMR4, ch)) & ~FMR4_TM);
		} else {
			cpc_writeb(falcbase + F_REG(FMR5, ch),
				   cpc_readb(falcbase + F_REG(FMR5, ch)) & ~XSP_TT0);
		}
		pfalc->sync = 0;
		cpc_writeb(falcbase + card->hw.cpld_reg2,
			   cpc_readb(falcbase + card->hw.cpld_reg2) &
			   ~(CPLD_REG2_FALC_LED2 << (2 * ch)));
		pfalc->active = 0;
		falc_issue_cmd(card, ch, CMDR_XRES);
		pfalc->loop_active = 0;
	}
}

/*----------------------------------------------------------------------------
 * turn_off_xlu
 *----------------------------------------------------------------------------
 * Description:	Turns XLU bit off in the proper register
 *----------------------------------------------------------------------------
 */
1578
static void turn_off_xlu(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	void __iomem *falcbase = card->hw.falcbase;

	if (conf->media == IF_IFACE_T1) {
		cpc_writeb(falcbase + F_REG(FMR5, ch),
			   cpc_readb(falcbase + F_REG(FMR5, ch)) & ~FMR5_XLU);
	} else {
		cpc_writeb(falcbase + F_REG(FMR3, ch),
			   cpc_readb(falcbase + F_REG(FMR3, ch)) & ~FMR3_XLU);
	}
}

/*----------------------------------------------------------------------------
 * turn_off_xld
 *----------------------------------------------------------------------------
 * Description: Turns XLD bit off in the proper register
 *----------------------------------------------------------------------------
 */
1599
static void turn_off_xld(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	void __iomem *falcbase = card->hw.falcbase;

	if (conf->media == IF_IFACE_T1) {
		cpc_writeb(falcbase + F_REG(FMR5, ch),
			   cpc_readb(falcbase + F_REG(FMR5, ch)) & ~FMR5_XLD);
	} else {
		cpc_writeb(falcbase + F_REG(FMR3, ch),
			   cpc_readb(falcbase + F_REG(FMR3, ch)) & ~FMR3_XLD);
	}
}

/*----------------------------------------------------------------------------
 * falc_generate_loop_up_code
 *----------------------------------------------------------------------------
 * Description:	This routine writes the proper FALC chip register in order
 *		to generate a LOOP activation code over a T1/E1 line.
 *----------------------------------------------------------------------------
 */
1621
static void falc_generate_loop_up_code(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (conf->media == IF_IFACE_T1) {
		cpc_writeb(falcbase + F_REG(FMR5, ch),
			   cpc_readb(falcbase + F_REG(FMR5, ch)) | FMR5_XLU);
	} else {
		cpc_writeb(falcbase + F_REG(FMR3, ch),
			   cpc_readb(falcbase + F_REG(FMR3, ch)) | FMR3_XLU);
	}
	// EVENT_FALC_ABNORMAL
	if (conf->media == IF_IFACE_T1) {
		/* Disable this interrupt as it may otherwise interfere with 
		 * other working boards. */
		cpc_writeb(falcbase + F_REG(IMR0, ch),
			   cpc_readb(falcbase + F_REG(IMR0, ch)) | IMR0_PDEN);
	}
	falc_disable_comm(card, ch);
	// EVENT_FALC_ABNORMAL
	pfalc->loop_gen = 1;
}

/*----------------------------------------------------------------------------
 * falc_generate_loop_down_code
 *----------------------------------------------------------------------------
 * Description:	This routine writes the proper FALC chip register in order
 *		to generate a LOOP deactivation code over a T1/E1 line.
 *----------------------------------------------------------------------------
 */
1654
static void falc_generate_loop_down_code(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (conf->media == IF_IFACE_T1) {
		cpc_writeb(falcbase + F_REG(FMR5, ch),
			   cpc_readb(falcbase + F_REG(FMR5, ch)) | FMR5_XLD);
	} else {
		cpc_writeb(falcbase + F_REG(FMR3, ch),
			   cpc_readb(falcbase + F_REG(FMR3, ch)) | FMR3_XLD);
	}
	pfalc->sync = 0;
	cpc_writeb(falcbase + card->hw.cpld_reg2,
		   cpc_readb(falcbase + card->hw.cpld_reg2) &
		   ~(CPLD_REG2_FALC_LED2 << (2 * ch)));
	pfalc->active = 0;
//?    falc_issue_cmd(card, ch, CMDR_XRES);
	pfalc->loop_gen = 0;
}

/*----------------------------------------------------------------------------
 * falc_pattern_test
 *----------------------------------------------------------------------------
 * Description:	This routine generates a pattern code and checks
 *		it on the reception side.
 *----------------------------------------------------------------------------
 */
1684
static void falc_pattern_test(pc300_t * card, int ch, unsigned int activate)
L
Linus Torvalds 已提交
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (activate) {
		pfalc->prbs = 1;
		pfalc->bec = 0;
		if (conf->media == IF_IFACE_T1) {
			/* Disable local loop activation/deactivation detect */
			cpc_writeb(falcbase + F_REG(IMR3, ch),
				   cpc_readb(falcbase + F_REG(IMR3, ch)) | IMR3_LLBSC);
		} else {
			/* Disable local loop activation/deactivation detect */
			cpc_writeb(falcbase + F_REG(IMR1, ch),
				   cpc_readb(falcbase + F_REG(IMR1, ch)) | IMR1_LLBSC);
		}
		/* Activates generation and monitoring of PRBS 
		 * (Pseudo Random Bit Sequence) */
		cpc_writeb(falcbase + F_REG(LCR1, ch),
			   cpc_readb(falcbase + F_REG(LCR1, ch)) | LCR1_EPRM | LCR1_XPRBS);
	} else {
		pfalc->prbs = 0;
		/* Deactivates generation and monitoring of PRBS 
		 * (Pseudo Random Bit Sequence) */
		cpc_writeb(falcbase + F_REG(LCR1, ch),
			   cpc_readb(falcbase+F_REG(LCR1,ch)) & ~(LCR1_EPRM | LCR1_XPRBS));
		if (conf->media == IF_IFACE_T1) {
			/* Enable local loop activation/deactivation detect */
			cpc_writeb(falcbase + F_REG(IMR3, ch),
				   cpc_readb(falcbase + F_REG(IMR3, ch)) & ~IMR3_LLBSC);
		} else {
			/* Enable local loop activation/deactivation detect */
			cpc_writeb(falcbase + F_REG(IMR1, ch),
				   cpc_readb(falcbase + F_REG(IMR1, ch)) & ~IMR1_LLBSC);
		}
	}
}

/*----------------------------------------------------------------------------
 * falc_pattern_test_error
 *----------------------------------------------------------------------------
 * Description:	This routine returns the bit error counter value
 *----------------------------------------------------------------------------
 */
1731
static u16 falc_pattern_test_error(pc300_t * card, int ch)
L
Linus Torvalds 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;

	return (pfalc->bec);
}

/**********************************/
/***   Net Interface Routines   ***/
/**********************************/

static void
cpc_trace(struct net_device *dev, struct sk_buff *skb_main, char rx_tx)
{
	struct sk_buff *skb;

	if ((skb = dev_alloc_skb(10 + skb_main->len)) == NULL) {
		printk("%s: out of memory\n", dev->name);
		return;
	}
	skb_put(skb, 10 + skb_main->len);

	skb->dev = dev;
	skb->protocol = htons(ETH_P_CUST);
1756
	skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
1757 1758 1759
	skb->pkt_type = PACKET_HOST;
	skb->len = 10 + skb_main->len;

1760
	skb_copy_to_linear_data(skb, dev->name, 5);
L
Linus Torvalds 已提交
1761 1762 1763 1764 1765
	skb->data[5] = '[';
	skb->data[6] = rx_tx;
	skb->data[7] = ']';
	skb->data[8] = ':';
	skb->data[9] = ' ';
1766
	skb_copy_from_linear_data(skb_main, &skb->data[10], skb_main->len);
L
Linus Torvalds 已提交
1767 1768 1769 1770

	netif_rx(skb);
}

1771
static void cpc_tx_timeout(struct net_device *dev)
L
Linus Torvalds 已提交
1772
{
1773
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
1774 1775 1776 1777
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	int ch = chan->channel;
	unsigned long flags;
1778
	u8 ilar;
L
Linus Torvalds 已提交
1779

1780 1781
	dev->stats.tx_errors++;
	dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
	CPC_LOCK(card, flags);
	if ((ilar = cpc_readb(card->hw.scabase + ILAR)) != 0) {
		printk("%s: ILAR=0x%x\n", dev->name, ilar);
		cpc_writeb(card->hw.scabase + ILAR, ilar);
		cpc_writeb(card->hw.scabase + DMER, 0x80);
	}
	if (card->hw.type == PC300_TE) {
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
			   ~(CPLD_REG2_FALC_LED1 << (2 * ch)));
	}
E
Eric Dumazet 已提交
1793
	dev->trans_start = jiffies; /* prevent tx timeout */
L
Linus Torvalds 已提交
1794 1795 1796 1797
	CPC_UNLOCK(card, flags);
	netif_wake_queue(dev);
}

1798
static int cpc_queue_xmit(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
1799
{
1800
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
1801 1802 1803 1804 1805 1806 1807 1808
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	int ch = chan->channel;
	unsigned long flags;
#ifdef PC300_DEBUG_TX
	int i;
#endif

1809
	if (!netif_carrier_ok(dev)) {
L
Linus Torvalds 已提交
1810 1811
		/* DCD must be OFF: drop packet */
		dev_kfree_skb(skb);
1812 1813
		dev->stats.tx_errors++;
		dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
1814 1815 1816
		return 0;
	} else if (cpc_readb(card->hw.scabase + M_REG(ST3, ch)) & ST3_DCD) {
		printk("%s: DCD is OFF. Going administrative down.\n", dev->name);
1817 1818
		dev->stats.tx_errors++;
		dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
		dev_kfree_skb(skb);
		netif_carrier_off(dev);
		CPC_LOCK(card, flags);
		cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR);
		if (card->hw.type == PC300_TE) {
			cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
				   cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) & 
				   			~(CPLD_REG2_FALC_LED1 << (2 * ch)));
		}
		CPC_UNLOCK(card, flags);
		netif_wake_queue(dev);
		return 0;
	}

	/* Write buffer to DMA buffers */
1834
	if (dma_buf_write(card, ch, (u8 *)skb->data, skb->len) != 0) {
L
Linus Torvalds 已提交
1835 1836 1837
//		printk("%s: write error. Dropping TX packet.\n", dev->name);
		netif_stop_queue(dev);
		dev_kfree_skb(skb);
1838 1839
		dev->stats.tx_errors++;
		dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
		return 0;
	}
#ifdef PC300_DEBUG_TX
	printk("%s T:", dev->name);
	for (i = 0; i < skb->len; i++)
		printk(" %02x", *(skb->data + i));
	printk("\n");
#endif

	if (d->trace_on) {
		cpc_trace(dev, skb, 'T');
	}

	/* Start transmission */
	CPC_LOCK(card, flags);
	/* verify if it has more than one free descriptor */
	if (card->chan[ch].nfree_tx_bd <= 1) {
		/* don't have so stop the queue */
		netif_stop_queue(dev);
	}
	cpc_writel(card->hw.scabase + DTX_REG(EDAL, ch),
		   TX_BD_ADDR(ch, chan->tx_next_bd));
	cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
	cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
	if (card->hw.type == PC300_TE) {
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
			   (CPLD_REG2_FALC_LED1 << (2 * ch)));
	}
	CPC_UNLOCK(card, flags);
	dev_kfree_skb(skb);

	return 0;
}

1875
static void cpc_net_rx(struct net_device *dev)
L
Linus Torvalds 已提交
1876
{
1877
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	int ch = chan->channel;
#ifdef PC300_DEBUG_RX
	int i;
#endif
	int rxb;
	struct sk_buff *skb;

	while (1) {
		if ((rxb = dma_get_rx_frame_size(card, ch)) == -1)
			return;

		if (!netif_carrier_ok(dev)) {
			/* DCD must be OFF: drop packet */
		    printk("%s : DCD is OFF - drop %d rx bytes\n", dev->name, rxb); 
			skb = NULL;
		} else {
			if (rxb > (dev->mtu + 40)) { /* add headers */
				printk("%s : MTU exceeded %d\n", dev->name, rxb); 
				skb = NULL;
			} else {
				skb = dev_alloc_skb(rxb);
				if (skb == NULL) {
					printk("%s: Memory squeeze!!\n", dev->name);
					return;
				}
				skb->dev = dev;
			}
		}

		if (((rxb = dma_buf_read(card, ch, skb)) <= 0) || (skb == NULL)) {
#ifdef PC300_DEBUG_RX
			printk("%s: rxb = %x\n", dev->name, rxb);
#endif
			if ((skb == NULL) && (rxb > 0)) {
				/* rxb > dev->mtu */
1915 1916
				dev->stats.rx_errors++;
				dev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
1917 1918 1919 1920 1921 1922
				continue;
			}

			if (rxb < 0) {	/* Invalid frame */
				rxb = -rxb;
				if (rxb & DST_OVR) {
1923 1924
					dev->stats.rx_errors++;
					dev->stats.rx_fifo_errors++;
L
Linus Torvalds 已提交
1925 1926
				}
				if (rxb & DST_CRC) {
1927 1928
					dev->stats.rx_errors++;
					dev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
1929 1930
				}
				if (rxb & (DST_RBIT | DST_SHRT | DST_ABT)) {
1931 1932
					dev->stats.rx_errors++;
					dev->stats.rx_frame_errors++;
L
Linus Torvalds 已提交
1933 1934 1935 1936 1937 1938 1939 1940
				}
			}
			if (skb) {
				dev_kfree_skb_irq(skb);
			}
			continue;
		}

1941
		dev->stats.rx_bytes += rxb;
L
Linus Torvalds 已提交
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951

#ifdef PC300_DEBUG_RX
		printk("%s R:", dev->name);
		for (i = 0; i < skb->len; i++)
			printk(" %02x", *(skb->data + i));
		printk("\n");
#endif
		if (d->trace_on) {
			cpc_trace(dev, skb, 'R');
		}
1952
		dev->stats.rx_packets++;
L
Linus Torvalds 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
		skb->protocol = hdlc_type_trans(skb, dev);
		netif_rx(skb);
	}
}

/************************************/
/***   PC300 Interrupt Routines   ***/
/************************************/
static void sca_tx_intr(pc300dev_t *dev)
{
	pc300ch_t *chan = (pc300ch_t *)dev->chan; 
	pc300_t *card = (pc300_t *)chan->card; 
	int ch = chan->channel; 
	volatile pcsca_bd_t __iomem * ptdescr; 

    /* Clean up descriptors from previous transmission */
	ptdescr = (card->hw.rambase +
						TX_BD_ADDR(ch,chan->tx_first_bd));
1971 1972 1973 1974 1975
	while ((cpc_readl(card->hw.scabase + DTX_REG(CDAL,ch)) !=
		TX_BD_ADDR(ch,chan->tx_first_bd)) &&
	       (cpc_readb(&ptdescr->status) & DST_OSB)) {
		dev->dev->stats.tx_packets++;
		dev->dev->stats.tx_bytes += cpc_readw(&ptdescr->len);
L
Linus Torvalds 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
		cpc_writeb(&ptdescr->status, DST_OSB);
		cpc_writew(&ptdescr->len, 0);
		chan->nfree_tx_bd++;
		chan->tx_first_bd = (chan->tx_first_bd + 1) & (N_DMA_TX_BUF - 1);
		ptdescr = (card->hw.rambase + TX_BD_ADDR(ch,chan->tx_first_bd));
    }

#ifdef CONFIG_PC300_MLPPP
	if (chan->conf.proto == PC300_PROTO_MLPPP) {
			cpc_tty_trigger_poll(dev);
	} else {
#endif
	/* Tell the upper layer we are ready to transmit more packets */
		netif_wake_queue(dev->dev);
#ifdef CONFIG_PC300_MLPPP
	}
#endif
}

static void sca_intr(pc300_t * card)
{
	void __iomem *scabase = card->hw.scabase;
1998
	volatile u32 status;
L
Linus Torvalds 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
	int ch;
	int intr_count = 0;
	unsigned char dsr_rx;

	while ((status = cpc_readl(scabase + ISR0)) != 0) {
		for (ch = 0; ch < card->hw.nchan; ch++) {
			pc300ch_t *chan = &card->chan[ch];
			pc300dev_t *d = &chan->d;
			struct net_device *dev = d->dev;

			spin_lock(&card->card_lock);

	    /**** Reception ****/
			if (status & IR0_DRX((IR0_DMIA | IR0_DMIB), ch)) {
2013
				u8 drx_stat = cpc_readb(scabase + DSR_RX(ch));
L
Linus Torvalds 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039

				/* Clear RX interrupts */
				cpc_writeb(scabase + DSR_RX(ch), drx_stat | DSR_DWE);

#ifdef PC300_DEBUG_INTR
				printk ("sca_intr: RX intr chan[%d] (st=0x%08lx, dsr=0x%02x)\n",
					 ch, status, drx_stat);
#endif
				if (status & IR0_DRX(IR0_DMIA, ch)) {
					if (drx_stat & DSR_BOF) {
#ifdef CONFIG_PC300_MLPPP
						if (chan->conf.proto == PC300_PROTO_MLPPP) {
							/* verify if driver is TTY */
							if ((cpc_readb(scabase + DSR_RX(ch)) & DSR_DE)) {
								rx_dma_stop(card, ch);
							}
							cpc_tty_receive(d);
							rx_dma_start(card, ch);
						} else 
#endif
						{
							if ((cpc_readb(scabase + DSR_RX(ch)) & DSR_DE)) {
								rx_dma_stop(card, ch);
							}
							cpc_net_rx(dev);
							/* Discard invalid frames */
2040 2041
							dev->stats.rx_errors++;
							dev->stats.rx_over_errors++;
L
Linus Torvalds 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
							chan->rx_first_bd = 0;
							chan->rx_last_bd = N_DMA_RX_BUF - 1;
							rx_dma_start(card, ch);
						}
					}
				}
				if (status & IR0_DRX(IR0_DMIB, ch)) {
					if (drx_stat & DSR_EOM) {
						if (card->hw.type == PC300_TE) {
							cpc_writeb(card->hw.falcbase +
								   card->hw.cpld_reg2,
								   cpc_readb (card->hw.falcbase +
								    	card->hw.cpld_reg2) |
								   (CPLD_REG2_FALC_LED1 << (2 * ch)));
						}
#ifdef CONFIG_PC300_MLPPP
						if (chan->conf.proto == PC300_PROTO_MLPPP) {
							/* verify if driver is TTY */
							cpc_tty_receive(d);
						} else {
							cpc_net_rx(dev);
						}
#else
						cpc_net_rx(dev);
#endif
						if (card->hw.type == PC300_TE) {
							cpc_writeb(card->hw.falcbase +
								   card->hw.cpld_reg2,
								   cpc_readb (card->hw.falcbase +
								    		card->hw.cpld_reg2) &
								   ~ (CPLD_REG2_FALC_LED1 << (2 * ch)));
						}
					}
				}
				if (!(dsr_rx = cpc_readb(scabase + DSR_RX(ch)) & DSR_DE)) {
#ifdef PC300_DEBUG_INTR
		printk("%s: RX intr chan[%d] (st=0x%08lx, dsr=0x%02x, dsr2=0x%02x)\n",
			dev->name, ch, status, drx_stat, dsr_rx);
#endif
					cpc_writeb(scabase + DSR_RX(ch), (dsr_rx | DSR_DE) & 0xfe);
				}
			}

	    /**** Transmission ****/
			if (status & IR0_DTX((IR0_EFT | IR0_DMIA | IR0_DMIB), ch)) {
2087
				u8 dtx_stat = cpc_readb(scabase + DSR_TX(ch));
L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

				/* Clear TX interrupts */
				cpc_writeb(scabase + DSR_TX(ch), dtx_stat | DSR_DWE);

#ifdef PC300_DEBUG_INTR
				printk ("sca_intr: TX intr chan[%d] (st=0x%08lx, dsr=0x%02x)\n",
					 ch, status, dtx_stat);
#endif
				if (status & IR0_DTX(IR0_EFT, ch)) {
					if (dtx_stat & DSR_UDRF) {
						if (cpc_readb (scabase + M_REG(TBN, ch)) != 0) {
							cpc_writeb(scabase + M_REG(CMD,ch), CMD_TX_BUF_CLR);
						}
						if (card->hw.type == PC300_TE) {
							cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
								   cpc_readb (card->hw.falcbase + 
										   card->hw.cpld_reg2) &
								   ~ (CPLD_REG2_FALC_LED1 << (2 * ch)));
						}
2107 2108
						dev->stats.tx_errors++;
						dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
						sca_tx_intr(d);
					}
				}
				if (status & IR0_DTX(IR0_DMIA, ch)) {
					if (dtx_stat & DSR_BOF) {
					}
				}
				if (status & IR0_DTX(IR0_DMIB, ch)) {
					if (dtx_stat & DSR_EOM) {
						if (card->hw.type == PC300_TE) {
							cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
								   cpc_readb (card->hw.falcbase +
								    			card->hw.cpld_reg2) &
								   ~ (CPLD_REG2_FALC_LED1 << (2 * ch)));
						}
						sca_tx_intr(d);
					}
				}
			}

	    /**** MSCI ****/
			if (status & IR0_M(IR0_RXINTA, ch)) {
2131
				u8 st1 = cpc_readb(scabase + M_REG(ST1, ch));
L
Linus Torvalds 已提交
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172

				/* Clear MSCI interrupts */
				cpc_writeb(scabase + M_REG(ST1, ch), st1);

#ifdef PC300_DEBUG_INTR
				printk("sca_intr: MSCI intr chan[%d] (st=0x%08lx, st1=0x%02x)\n",
					 ch, status, st1);
#endif
				if (st1 & ST1_CDCD) {	/* DCD changed */
					if (cpc_readb(scabase + M_REG(ST3, ch)) & ST3_DCD) {
						printk ("%s: DCD is OFF. Going administrative down.\n",
							 dev->name);
#ifdef CONFIG_PC300_MLPPP
						if (chan->conf.proto != PC300_PROTO_MLPPP) {
							netif_carrier_off(dev);
						}
#else
						netif_carrier_off(dev);

#endif
						card->chan[ch].d.line_off++;
					} else {	/* DCD = 1 */
						printk ("%s: DCD is ON. Going administrative up.\n",
							 dev->name);
#ifdef CONFIG_PC300_MLPPP
						if (chan->conf.proto != PC300_PROTO_MLPPP)
							/* verify if driver is not TTY */
#endif
							netif_carrier_on(dev);
						card->chan[ch].d.line_on++;
					}
				}
			}
			spin_unlock(&card->card_lock);
		}
		if (++intr_count == 10)
			/* Too much work at this board. Force exit */
			break;
	}
}

2173
static void falc_t1_loop_detection(pc300_t *card, int ch, u8 frs1)
L
Linus Torvalds 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (((cpc_readb(falcbase + F_REG(LCR1, ch)) & LCR1_XPRBS) == 0) &&
	    !pfalc->loop_gen) {
		if (frs1 & FRS1_LLBDD) {
			// A Line Loop Back Deactivation signal detected
			if (pfalc->loop_active) {
				falc_remote_loop(card, ch, 0);
			}
		} else {
			if ((frs1 & FRS1_LLBAD) &&
			    ((cpc_readb(falcbase + F_REG(LCR1, ch)) & LCR1_EPRM) == 0)) {
				// A Line Loop Back Activation signal detected  
				if (!pfalc->loop_active) {
					falc_remote_loop(card, ch, 1);
				}
			}
		}
	}
}

2198
static void falc_e1_loop_detection(pc300_t *card, int ch, u8 rsp)
L
Linus Torvalds 已提交
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;

	if (((cpc_readb(falcbase + F_REG(LCR1, ch)) & LCR1_XPRBS) == 0) &&
	    !pfalc->loop_gen) {
		if (rsp & RSP_LLBDD) {
			// A Line Loop Back Deactivation signal detected
			if (pfalc->loop_active) {
				falc_remote_loop(card, ch, 0);
			}
		} else {
			if ((rsp & RSP_LLBAD) &&
			    ((cpc_readb(falcbase + F_REG(LCR1, ch)) & LCR1_EPRM) == 0)) {
				// A Line Loop Back Activation signal detected  
				if (!pfalc->loop_active) {
					falc_remote_loop(card, ch, 1);
				}
			}
		}
	}
}

static void falc_t1_intr(pc300_t * card, int ch)
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
2228 2229
	u8 isr0, isr3, gis;
	u8 dummy;
L
Linus Torvalds 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274

	while ((gis = cpc_readb(falcbase + F_REG(GIS, ch))) != 0) {
		if (gis & GIS_ISR0) {
			isr0 = cpc_readb(falcbase + F_REG(FISR0, ch));
			if (isr0 & FISR0_PDEN) {
				/* Read the bit to clear the situation */
				if (cpc_readb(falcbase + F_REG(FRS1, ch)) &
				    FRS1_PDEN) {
					pfalc->pden++;
				}
			}
		}

		if (gis & GIS_ISR1) {
			dummy = cpc_readb(falcbase + F_REG(FISR1, ch));
		}

		if (gis & GIS_ISR2) {
			dummy = cpc_readb(falcbase + F_REG(FISR2, ch));
		}

		if (gis & GIS_ISR3) {
			isr3 = cpc_readb(falcbase + F_REG(FISR3, ch));
			if (isr3 & FISR3_SEC) {
				pfalc->sec++;
				falc_update_stats(card, ch);
				falc_check_status(card, ch,
						  cpc_readb(falcbase + F_REG(FRS0, ch)));
			}
			if (isr3 & FISR3_ES) {
				pfalc->es++;
			}
			if (isr3 & FISR3_LLBSC) {
				falc_t1_loop_detection(card, ch,
						       cpc_readb(falcbase + F_REG(FRS1, ch)));
			}
		}
	}
}

static void falc_e1_intr(pc300_t * card, int ch)
{
	pc300ch_t *chan = (pc300ch_t *) & card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	void __iomem *falcbase = card->hw.falcbase;
2275 2276
	u8 isr1, isr2, isr3, gis, rsp;
	u8 dummy;
L
Linus Torvalds 已提交
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286

	while ((gis = cpc_readb(falcbase + F_REG(GIS, ch))) != 0) {
		rsp = cpc_readb(falcbase + F_REG(RSP, ch));

		if (gis & GIS_ISR0) {
			dummy = cpc_readb(falcbase + F_REG(FISR0, ch));
		}
		if (gis & GIS_ISR1) {
			isr1 = cpc_readb(falcbase + F_REG(FISR1, ch));
			if (isr1 & FISR1_XMB) {
2287 2288
				if ((pfalc->xmb_cause & 2) &&
				    pfalc->multiframe_mode) {
L
Linus Torvalds 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
					if (cpc_readb (falcbase + F_REG(FRS0, ch)) & 
									(FRS0_LOS | FRS0_AIS | FRS0_LFA)) {
						cpc_writeb(falcbase + F_REG(XSP, ch),
							   cpc_readb(falcbase + F_REG(XSP, ch))
							   & ~XSP_AXS);
					} else {
						cpc_writeb(falcbase + F_REG(XSP, ch),
							   cpc_readb(falcbase + F_REG(XSP, ch))
							   | XSP_AXS);
					}
				}
				pfalc->xmb_cause = 0;
				cpc_writeb(falcbase + F_REG(IMR1, ch),
					   cpc_readb(falcbase + F_REG(IMR1, ch)) | IMR1_XMB);
			}
			if (isr1 & FISR1_LLBSC) {
				falc_e1_loop_detection(card, ch, rsp);
			}
		}
		if (gis & GIS_ISR2) {
			isr2 = cpc_readb(falcbase + F_REG(FISR2, ch));
			if (isr2 & FISR2_T400MS) {
				cpc_writeb(falcbase + F_REG(XSW, ch),
					   cpc_readb(falcbase + F_REG(XSW, ch)) | XSW_XRA);
			}
			if (isr2 & FISR2_MFAR) {
				cpc_writeb(falcbase + F_REG(XSW, ch),
					   cpc_readb(falcbase + F_REG(XSW, ch)) & ~XSW_XRA);
			}
			if (isr2 & (FISR2_FAR | FISR2_LFA | FISR2_AIS | FISR2_LOS)) {
				pfalc->xmb_cause |= 2;
				cpc_writeb(falcbase + F_REG(IMR1, ch),
					   cpc_readb(falcbase + F_REG(IMR1, ch)) & ~IMR1_XMB);
			}
		}
		if (gis & GIS_ISR3) {
			isr3 = cpc_readb(falcbase + F_REG(FISR3, ch));
			if (isr3 & FISR3_SEC) {
				pfalc->sec++;
				falc_update_stats(card, ch);
				falc_check_status(card, ch,
						  cpc_readb(falcbase + F_REG(FRS0, ch)));
			}
			if (isr3 & FISR3_ES) {
				pfalc->es++;
			}
		}
	}
}

static void falc_intr(pc300_t * card)
{
	int ch;

	for (ch = 0; ch < card->hw.nchan; ch++) {
		pc300ch_t *chan = &card->chan[ch];
		pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;

		if (conf->media == IF_IFACE_T1) {
			falc_t1_intr(card, ch);
		} else {
			falc_e1_intr(card, ch);
		}
	}
}

2355
static irqreturn_t cpc_intr(int irq, void *dev_id)
L
Linus Torvalds 已提交
2356
{
A
Al Viro 已提交
2357
	pc300_t *card = dev_id;
2358
	volatile u8 plx_status;
L
Linus Torvalds 已提交
2359

A
Al Viro 已提交
2360
	if (!card) {
L
Linus Torvalds 已提交
2361 2362 2363 2364 2365 2366
#ifdef PC300_DEBUG_INTR
		printk("cpc_intr: spurious intr %d\n", irq);
#endif
		return IRQ_NONE;		/* spurious intr */
	}

A
Al Viro 已提交
2367
	if (!card->hw.rambase) {
L
Linus Torvalds 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
#ifdef PC300_DEBUG_INTR
		printk("cpc_intr: spurious intr2 %d\n", irq);
#endif
		return IRQ_NONE;		/* spurious intr */
	}

	switch (card->hw.type) {
		case PC300_RSV:
		case PC300_X21:
			sca_intr(card);
			break;

		case PC300_TE:
			while ( (plx_status = (cpc_readb(card->hw.plxbase + card->hw.intctl_reg) &
				 (PLX_9050_LINT1_STATUS | PLX_9050_LINT2_STATUS))) != 0) {
				if (plx_status & PLX_9050_LINT1_STATUS) {	/* SCA Interrupt */
					sca_intr(card);
				}
				if (plx_status & PLX_9050_LINT2_STATUS) {	/* FALC Interrupt */
					falc_intr(card);
				}
			}
			break;
	}
	return IRQ_HANDLED;
}

2395
static void cpc_sca_status(pc300_t * card, int ch)
L
Linus Torvalds 已提交
2396
{
2397
	u8 ilar;
L
Linus Torvalds 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
	void __iomem *scabase = card->hw.scabase;
	unsigned long flags;

	tx_dma_buf_check(card, ch);
	rx_dma_buf_check(card, ch);
	ilar = cpc_readb(scabase + ILAR);
	printk ("ILAR=0x%02x, WCRL=0x%02x, PCR=0x%02x, BTCR=0x%02x, BOLR=0x%02x\n",
		 ilar, cpc_readb(scabase + WCRL), cpc_readb(scabase + PCR),
		 cpc_readb(scabase + BTCR), cpc_readb(scabase + BOLR));
	printk("TX_CDA=0x%08x, TX_EDA=0x%08x\n",
	       cpc_readl(scabase + DTX_REG(CDAL, ch)),
	       cpc_readl(scabase + DTX_REG(EDAL, ch)));
	printk("RX_CDA=0x%08x, RX_EDA=0x%08x, BFL=0x%04x\n",
	       cpc_readl(scabase + DRX_REG(CDAL, ch)),
	       cpc_readl(scabase + DRX_REG(EDAL, ch)),
	       cpc_readw(scabase + DRX_REG(BFLL, ch)));
	printk("DMER=0x%02x, DSR_TX=0x%02x, DSR_RX=0x%02x\n",
	       cpc_readb(scabase + DMER), cpc_readb(scabase + DSR_TX(ch)),
	       cpc_readb(scabase + DSR_RX(ch)));
	printk("DMR_TX=0x%02x, DMR_RX=0x%02x, DIR_TX=0x%02x, DIR_RX=0x%02x\n",
	       cpc_readb(scabase + DMR_TX(ch)), cpc_readb(scabase + DMR_RX(ch)),
	       cpc_readb(scabase + DIR_TX(ch)),
	       cpc_readb(scabase + DIR_RX(ch)));
	printk("DCR_TX=0x%02x, DCR_RX=0x%02x, FCT_TX=0x%02x, FCT_RX=0x%02x\n",
	       cpc_readb(scabase + DCR_TX(ch)), cpc_readb(scabase + DCR_RX(ch)),
	       cpc_readb(scabase + FCT_TX(ch)),
	       cpc_readb(scabase + FCT_RX(ch)));
	printk("MD0=0x%02x, MD1=0x%02x, MD2=0x%02x, MD3=0x%02x, IDL=0x%02x\n",
	       cpc_readb(scabase + M_REG(MD0, ch)),
	       cpc_readb(scabase + M_REG(MD1, ch)),
	       cpc_readb(scabase + M_REG(MD2, ch)),
	       cpc_readb(scabase + M_REG(MD3, ch)),
	       cpc_readb(scabase + M_REG(IDL, ch)));
	printk("CMD=0x%02x, SA0=0x%02x, SA1=0x%02x, TFN=0x%02x, CTL=0x%02x\n",
	       cpc_readb(scabase + M_REG(CMD, ch)),
	       cpc_readb(scabase + M_REG(SA0, ch)),
	       cpc_readb(scabase + M_REG(SA1, ch)),
	       cpc_readb(scabase + M_REG(TFN, ch)),
	       cpc_readb(scabase + M_REG(CTL, ch)));
	printk("ST0=0x%02x, ST1=0x%02x, ST2=0x%02x, ST3=0x%02x, ST4=0x%02x\n",
	       cpc_readb(scabase + M_REG(ST0, ch)),
	       cpc_readb(scabase + M_REG(ST1, ch)),
	       cpc_readb(scabase + M_REG(ST2, ch)),
	       cpc_readb(scabase + M_REG(ST3, ch)),
	       cpc_readb(scabase + M_REG(ST4, ch)));
	printk ("CST0=0x%02x, CST1=0x%02x, CST2=0x%02x, CST3=0x%02x, FST=0x%02x\n",
		 cpc_readb(scabase + M_REG(CST0, ch)),
		 cpc_readb(scabase + M_REG(CST1, ch)),
		 cpc_readb(scabase + M_REG(CST2, ch)),
		 cpc_readb(scabase + M_REG(CST3, ch)),
		 cpc_readb(scabase + M_REG(FST, ch)));
	printk("TRC0=0x%02x, TRC1=0x%02x, RRC=0x%02x, TBN=0x%02x, RBN=0x%02x\n",
	       cpc_readb(scabase + M_REG(TRC0, ch)),
	       cpc_readb(scabase + M_REG(TRC1, ch)),
	       cpc_readb(scabase + M_REG(RRC, ch)),
	       cpc_readb(scabase + M_REG(TBN, ch)),
	       cpc_readb(scabase + M_REG(RBN, ch)));
	printk("TFS=0x%02x, TNR0=0x%02x, TNR1=0x%02x, RNR=0x%02x\n",
	       cpc_readb(scabase + M_REG(TFS, ch)),
	       cpc_readb(scabase + M_REG(TNR0, ch)),
	       cpc_readb(scabase + M_REG(TNR1, ch)),
	       cpc_readb(scabase + M_REG(RNR, ch)));
	printk("TCR=0x%02x, RCR=0x%02x, TNR1=0x%02x, RNR=0x%02x\n",
	       cpc_readb(scabase + M_REG(TCR, ch)),
	       cpc_readb(scabase + M_REG(RCR, ch)),
	       cpc_readb(scabase + M_REG(TNR1, ch)),
	       cpc_readb(scabase + M_REG(RNR, ch)));
	printk("TXS=0x%02x, RXS=0x%02x, EXS=0x%02x, TMCT=0x%02x, TMCR=0x%02x\n",
	       cpc_readb(scabase + M_REG(TXS, ch)),
	       cpc_readb(scabase + M_REG(RXS, ch)),
	       cpc_readb(scabase + M_REG(EXS, ch)),
	       cpc_readb(scabase + M_REG(TMCT, ch)),
	       cpc_readb(scabase + M_REG(TMCR, ch)));
	printk("IE0=0x%02x, IE1=0x%02x, IE2=0x%02x, IE4=0x%02x, FIE=0x%02x\n",
	       cpc_readb(scabase + M_REG(IE0, ch)),
	       cpc_readb(scabase + M_REG(IE1, ch)),
	       cpc_readb(scabase + M_REG(IE2, ch)),
	       cpc_readb(scabase + M_REG(IE4, ch)),
	       cpc_readb(scabase + M_REG(FIE, ch)));
	printk("IER0=0x%08x\n", cpc_readl(scabase + IER0));

	if (ilar != 0) {
		CPC_LOCK(card, flags);
		cpc_writeb(scabase + ILAR, ilar);
		cpc_writeb(scabase + DMER, 0x80);
		CPC_UNLOCK(card, flags);
	}
}

2487
static void cpc_falc_status(pc300_t * card, int ch)
L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
{
	pc300ch_t *chan = &card->chan[ch];
	falc_t *pfalc = (falc_t *) & chan->falc;
	unsigned long flags;

	CPC_LOCK(card, flags);
	printk("CH%d:   %s %s  %d channels\n",
	       ch, (pfalc->sync ? "SYNC" : ""), (pfalc->active ? "ACTIVE" : ""),
	       pfalc->num_channels);

	printk("        pden=%d,  los=%d,  losr=%d,  lfa=%d,  farec=%d\n",
	       pfalc->pden, pfalc->los, pfalc->losr, pfalc->lfa, pfalc->farec);
	printk("        lmfa=%d,  ais=%d,  sec=%d,  es=%d,  rai=%d\n",
	       pfalc->lmfa, pfalc->ais, pfalc->sec, pfalc->es, pfalc->rai);
	printk("        bec=%d,  fec=%d,  cvc=%d,  cec=%d,  ebc=%d\n",
	       pfalc->bec, pfalc->fec, pfalc->cvc, pfalc->cec, pfalc->ebc);

	printk("\n");
	printk("        STATUS: %s  %s  %s  %s  %s  %s\n",
	       (pfalc->red_alarm ? "RED" : ""),
	       (pfalc->blue_alarm ? "BLU" : ""),
	       (pfalc->yellow_alarm ? "YEL" : ""),
	       (pfalc->loss_fa ? "LFA" : ""),
	       (pfalc->loss_mfa ? "LMF" : ""), (pfalc->prbs ? "PRB" : ""));
	CPC_UNLOCK(card, flags);
}

2515
static int cpc_change_mtu(struct net_device *dev, int new_mtu)
L
Linus Torvalds 已提交
2516 2517 2518 2519 2520 2521 2522
{
	if ((new_mtu < 128) || (new_mtu > PC300_DEF_MTU))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}

2523
static int cpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
L
Linus Torvalds 已提交
2524
{
2525
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	pc300conf_t conf_aux;
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	int ch = chan->channel;
	void __user *arg = ifr->ifr_data;
	struct if_settings *settings = &ifr->ifr_settings;
	void __iomem *scabase = card->hw.scabase;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	switch (cmd) {
		case SIOCGPC300CONF:
#ifdef CONFIG_PC300_MLPPP
			if (conf->proto != PC300_PROTO_MLPPP) {
2542
				conf->proto = /* FIXME hdlc->proto.id */ 0;
L
Linus Torvalds 已提交
2543 2544
			}
#else
2545
			conf->proto = /* FIXME hdlc->proto.id */ 0;
L
Linus Torvalds 已提交
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
#endif
			memcpy(&conf_aux.conf, conf, sizeof(pc300chconf_t));
			memcpy(&conf_aux.hw, &card->hw, sizeof(pc300hw_t));
			if (!arg || 
				copy_to_user(arg, &conf_aux, sizeof(pc300conf_t))) 
				return -EINVAL;
			return 0;
		case SIOCSPC300CONF:
			if (!capable(CAP_NET_ADMIN))
				return -EPERM;
			if (!arg || 
				copy_from_user(&conf_aux.conf, arg, sizeof(pc300chconf_t)))
				return -EINVAL;
			if (card->hw.cpld_id < 0x02 &&
			    conf_aux.conf.fr_mode == PC300_FR_UNFRAMED) {
				/* CPLD_ID < 0x02 doesn't support Unframed E1 */
				return -EINVAL;
			}
#ifdef CONFIG_PC300_MLPPP
			if (conf_aux.conf.proto == PC300_PROTO_MLPPP) {
				if (conf->proto != PC300_PROTO_MLPPP) {
					memcpy(conf, &conf_aux.conf, sizeof(pc300chconf_t));
					cpc_tty_init(d);	/* init TTY driver */
				}
			} else {
				if (conf_aux.conf.proto == 0xffff) {
					if (conf->proto == PC300_PROTO_MLPPP){ 
						/* ifdown interface */
						cpc_close(dev);
					}
				} else {
					memcpy(conf, &conf_aux.conf, sizeof(pc300chconf_t));
2578
					/* FIXME hdlc->proto.id = conf->proto; */
L
Linus Torvalds 已提交
2579 2580 2581 2582
				}
			}
#else
			memcpy(conf, &conf_aux.conf, sizeof(pc300chconf_t));
2583
			/* FIXME hdlc->proto.id = conf->proto; */
L
Linus Torvalds 已提交
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
#endif
			return 0;
		case SIOCGPC300STATUS:
			cpc_sca_status(card, ch);
			return 0;
		case SIOCGPC300FALCSTATUS:
			cpc_falc_status(card, ch);
			return 0;

		case SIOCGPC300UTILSTATS:
			{
				if (!arg) {	/* clear statistics */
2596
					memset(&dev->stats, 0, sizeof(dev->stats));
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
					if (card->hw.type == PC300_TE) {
						memset(&chan->falc, 0, sizeof(falc_t));
					}
				} else {
					pc300stats_t pc300stats;

					memset(&pc300stats, 0, sizeof(pc300stats_t));
					pc300stats.hw_type = card->hw.type;
					pc300stats.line_on = card->chan[ch].d.line_on;
					pc300stats.line_off = card->chan[ch].d.line_off;
2607 2608
					memcpy(&pc300stats.gen_stats, &dev->stats,
					       sizeof(dev->stats));
L
Linus Torvalds 已提交
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
					if (card->hw.type == PC300_TE)
						memcpy(&pc300stats.te_stats,&chan->falc,sizeof(falc_t));
				    	if (copy_to_user(arg, &pc300stats, sizeof(pc300stats_t)))
						return -EFAULT;
				}
				return 0;
			}

		case SIOCGPC300UTILSTATUS:
			{
				struct pc300status pc300status;

				pc300status.hw_type = card->hw.type;
				if (card->hw.type == PC300_TE) {
					pc300status.te_status.sync = chan->falc.sync;
					pc300status.te_status.red_alarm = chan->falc.red_alarm;
					pc300status.te_status.blue_alarm = chan->falc.blue_alarm;
					pc300status.te_status.loss_fa = chan->falc.loss_fa;
					pc300status.te_status.yellow_alarm =chan->falc.yellow_alarm;
					pc300status.te_status.loss_mfa = chan->falc.loss_mfa;
					pc300status.te_status.prbs = chan->falc.prbs;
				} else {
					pc300status.gen_status.dcd =
						!(cpc_readb (scabase + M_REG(ST3, ch)) & ST3_DCD);
					pc300status.gen_status.cts =
						!(cpc_readb (scabase + M_REG(ST3, ch)) & ST3_CTS);
					pc300status.gen_status.rts =
						!(cpc_readb (scabase + M_REG(CTL, ch)) & CTL_RTS);
					pc300status.gen_status.dtr =
						!(cpc_readb (scabase + M_REG(CTL, ch)) & CTL_DTR);
					/* There is no DSR in HD64572 */
				}
2641 2642 2643
				if (!arg ||
				    copy_to_user(arg, &pc300status, sizeof(pc300status_t)))
					return -EINVAL;
L
Linus Torvalds 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
				return 0;
			}

		case SIOCSPC300TRACE:
			/* Sets/resets a trace_flag for the respective device */
			if (!arg || copy_from_user(&d->trace_on, arg,sizeof(unsigned char)))
					return -EINVAL;
			return 0;

		case SIOCSPC300LOOPBACK:
			{
				struct pc300loopback pc300loop;

				/* TE boards only */
				if (card->hw.type != PC300_TE)
					return -EINVAL;

				if (!arg || 
					copy_from_user(&pc300loop, arg, sizeof(pc300loopback_t)))
						return -EINVAL;
				switch (pc300loop.loop_type) {
					case PC300LOCLOOP:	/* Turn the local loop on/off */
						falc_local_loop(card, ch, pc300loop.loop_on);
						return 0;

					case PC300REMLOOP:	/* Turn the remote loop on/off */
						falc_remote_loop(card, ch, pc300loop.loop_on);
						return 0;

					case PC300PAYLOADLOOP:	/* Turn the payload loop on/off */
						falc_payload_loop(card, ch, pc300loop.loop_on);
						return 0;

					case PC300GENLOOPUP:	/* Generate loop UP */
						if (pc300loop.loop_on) {
							falc_generate_loop_up_code (card, ch);
						} else {
							turn_off_xlu(card, ch);
						}
						return 0;

					case PC300GENLOOPDOWN:	/* Generate loop DOWN */
						if (pc300loop.loop_on) {
							falc_generate_loop_down_code (card, ch);
						} else {
							turn_off_xld(card, ch);
						}
						return 0;

					default:
						return -EINVAL;
				}
			}

		case SIOCSPC300PATTERNTEST:
			/* Turn the pattern test on/off and show the errors counter */
			{
				struct pc300patterntst pc300patrntst;

				/* TE boards only */
				if (card->hw.type != PC300_TE)
					return -EINVAL;

				if (card->hw.cpld_id < 0x02) {
					/* CPLD_ID < 0x02 doesn't support pattern test */
					return -EINVAL;
				}

				if (!arg || 
					copy_from_user(&pc300patrntst,arg,sizeof(pc300patterntst_t)))
						return -EINVAL;
				if (pc300patrntst.patrntst_on == 2) {
					if (chan->falc.prbs == 0) {
						falc_pattern_test(card, ch, 1);
					}
					pc300patrntst.num_errors =
						falc_pattern_test_error(card, ch);
2721 2722
					if (copy_to_user(arg, &pc300patrntst,
							 sizeof(pc300patterntst_t)))
L
Linus Torvalds 已提交
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
							return -EINVAL;
				} else {
					falc_pattern_test(card, ch, pc300patrntst.patrntst_on);
				}
				return 0;
			}

		case SIOCWANDEV:
			switch (ifr->ifr_settings.type) {
				case IF_GET_IFACE:
				{
					const size_t size = sizeof(sync_serial_settings);
					ifr->ifr_settings.type = conf->media;
					if (ifr->ifr_settings.size < size) {
						/* data size wanted */
						ifr->ifr_settings.size = size;
						return -ENOBUFS;
					}
	
					if (copy_to_user(settings->ifs_ifsu.sync,
							 &conf->phys_settings, size)) {
						return -EFAULT;
					}
					return 0;
				}

				case IF_IFACE_V35:
				case IF_IFACE_V24:
				case IF_IFACE_X21:
				{
					const size_t size = sizeof(sync_serial_settings);

					if (!capable(CAP_NET_ADMIN)) {
						return -EPERM;
					}
					/* incorrect data len? */
					if (ifr->ifr_settings.size != size) {
						return -ENOBUFS;
					}

					if (copy_from_user(&conf->phys_settings, 
							   settings->ifs_ifsu.sync, size)) {
						return -EFAULT;
					}

					if (conf->phys_settings.loopback) {
						cpc_writeb(card->hw.scabase + M_REG(MD2, ch),
							cpc_readb(card->hw.scabase + M_REG(MD2, ch)) | 
							MD2_LOOP_MIR);
					}
					conf->media = ifr->ifr_settings.type;
					return 0;
				}

				case IF_IFACE_T1:
				case IF_IFACE_E1:
				{
					const size_t te_size = sizeof(te1_settings);
					const size_t size = sizeof(sync_serial_settings);

					if (!capable(CAP_NET_ADMIN)) {
						return -EPERM;
					}

					/* incorrect data len? */
					if (ifr->ifr_settings.size != te_size) {
						return -ENOBUFS;
					}

					if (copy_from_user(&conf->phys_settings, 
							   settings->ifs_ifsu.te1, size)) {
						return -EFAULT;
					}/* Ignoring HDLC slot_map for a while */
					
					if (conf->phys_settings.loopback) {
						cpc_writeb(card->hw.scabase + M_REG(MD2, ch),
							cpc_readb(card->hw.scabase + M_REG(MD2, ch)) | 
							MD2_LOOP_MIR);
					}
					conf->media = ifr->ifr_settings.type;
					return 0;
				}
				default:
					return hdlc_ioctl(dev, ifr, cmd);
			}

		default:
			return hdlc_ioctl(dev, ifr, cmd);
	}
}

2814
static int clock_rate_calc(u32 rate, u32 clock, int *br_io)
L
Linus Torvalds 已提交
2815 2816 2817 2818
{
	int br, tc;
	int br_pwr, error;

2819 2820
	*br_io = 0;

L
Linus Torvalds 已提交
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
	if (rate == 0)
		return (0);

	for (br = 0, br_pwr = 1; br <= 9; br++, br_pwr <<= 1) {
		if ((tc = clock / br_pwr / rate) <= 0xff) {
			*br_io = br;
			break;
		}
	}

	if (tc <= 0xff) {
		error = ((rate - (clock / br_pwr / rate)) / rate) * 1000;
		/* Errors bigger than +/- 1% won't be tolerated */
		if (error < -10 || error > 10)
			return (-1);
		else
			return (tc);
	} else {
		return (-1);
	}
}

2843
static int ch_config(pc300dev_t * d)
L
Linus Torvalds 已提交
2844 2845 2846 2847 2848 2849 2850
{
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300chconf_t *conf = (pc300chconf_t *) & chan->conf;
	pc300_t *card = (pc300_t *) chan->card;
	void __iomem *scabase = card->hw.scabase;
	void __iomem *plxbase = card->hw.plxbase;
	int ch = chan->channel;
2851 2852 2853 2854 2855 2856
	u32 clkrate = chan->conf.phys_settings.clock_rate;
	u32 clktype = chan->conf.phys_settings.clock_type;
	u16 encoding = chan->conf.proto_settings.encoding;
	u16 parity = chan->conf.proto_settings.parity;
	u8 md0, md2;

L
Linus Torvalds 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
	/* Reset the channel */
	cpc_writeb(scabase + M_REG(CMD, ch), CMD_CH_RST);

	/* Configure the SCA registers */
	switch (parity) {
		case PARITY_NONE:
			md0 = MD0_BIT_SYNC;
			break;
		case PARITY_CRC16_PR0:
			md0 = MD0_CRC16_0|MD0_CRCC0|MD0_BIT_SYNC;
			break;
		case PARITY_CRC16_PR1:
			md0 = MD0_CRC16_1|MD0_CRCC0|MD0_BIT_SYNC;
			break;
		case PARITY_CRC32_PR1_CCITT:
			md0 = MD0_CRC32|MD0_CRCC0|MD0_BIT_SYNC;
			break;
		case PARITY_CRC16_PR1_CCITT:
		default:
			md0 = MD0_CRC_CCITT|MD0_CRCC0|MD0_BIT_SYNC;
			break;
	}
	switch (encoding) {
		case ENCODING_NRZI:
			md2 = MD2_F_DUPLEX|MD2_ADPLL_X8|MD2_NRZI;
			break;
		case ENCODING_FM_MARK:	/* FM1 */
			md2 = MD2_F_DUPLEX|MD2_ADPLL_X8|MD2_FM|MD2_FM1;
			break;
		case ENCODING_FM_SPACE:	/* FM0 */
			md2 = MD2_F_DUPLEX|MD2_ADPLL_X8|MD2_FM|MD2_FM0;
			break;
		case ENCODING_MANCHESTER: /* It's not working... */
			md2 = MD2_F_DUPLEX|MD2_ADPLL_X8|MD2_FM|MD2_MANCH;
			break;
		case ENCODING_NRZ:
		default:
			md2 = MD2_F_DUPLEX|MD2_ADPLL_X8|MD2_NRZ;
			break;
	}
	cpc_writeb(scabase + M_REG(MD0, ch), md0);
	cpc_writeb(scabase + M_REG(MD1, ch), 0);
	cpc_writeb(scabase + M_REG(MD2, ch), md2);
 	cpc_writeb(scabase + M_REG(IDL, ch), 0x7e);
	cpc_writeb(scabase + M_REG(CTL, ch), CTL_URSKP | CTL_IDLC);

	/* Configure HW media */
	switch (card->hw.type) {
		case PC300_RSV:
			if (conf->media == IF_IFACE_V35) {
				cpc_writel((plxbase + card->hw.gpioc_reg),
					   cpc_readl(plxbase + card->hw.gpioc_reg) | PC300_CHMEDIA_MASK(ch));
			} else {
				cpc_writel((plxbase + card->hw.gpioc_reg),
					   cpc_readl(plxbase + card->hw.gpioc_reg) & ~PC300_CHMEDIA_MASK(ch));
			}
			break;

		case PC300_X21:
			break;

		case PC300_TE:
			te_config(card, ch);
			break;
	}

	switch (card->hw.type) {
		case PC300_RSV:
		case PC300_X21:
			if (clktype == CLOCK_INT || clktype == CLOCK_TXINT) {
2927 2928
				int tmc, br;

L
Linus Torvalds 已提交
2929 2930
				/* Calculate the clkrate parameters */
				tmc = clock_rate_calc(clkrate, card->hw.clock, &br);
2931 2932
				if (tmc < 0)
					return -EIO;
L
Linus Torvalds 已提交
2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
				cpc_writeb(scabase + M_REG(TMCT, ch), tmc);
				cpc_writeb(scabase + M_REG(TXS, ch),
					   (TXS_DTRXC | TXS_IBRG | br));
				if (clktype == CLOCK_INT) {
					cpc_writeb(scabase + M_REG(TMCR, ch), tmc);
					cpc_writeb(scabase + M_REG(RXS, ch), 
						   (RXS_IBRG | br));
				} else {
					cpc_writeb(scabase + M_REG(TMCR, ch), 1);
					cpc_writeb(scabase + M_REG(RXS, ch), 0);
				}
	    			if (card->hw.type == PC300_X21) {
					cpc_writeb(scabase + M_REG(GPO, ch), 1);
					cpc_writeb(scabase + M_REG(EXS, ch), EXS_TES1 | EXS_RES1);
				} else {
					cpc_writeb(scabase + M_REG(EXS, ch), EXS_TES1);
				}
			} else {
				cpc_writeb(scabase + M_REG(TMCT, ch), 1);
				if (clktype == CLOCK_EXT) {
					cpc_writeb(scabase + M_REG(TXS, ch), 
						   TXS_DTRXC);
				} else {
					cpc_writeb(scabase + M_REG(TXS, ch), 
						   TXS_DTRXC|TXS_RCLK);
				}
	    			cpc_writeb(scabase + M_REG(TMCR, ch), 1);
				cpc_writeb(scabase + M_REG(RXS, ch), 0);
				if (card->hw.type == PC300_X21) {
					cpc_writeb(scabase + M_REG(GPO, ch), 0);
					cpc_writeb(scabase + M_REG(EXS, ch), EXS_TES1 | EXS_RES1);
				} else {
					cpc_writeb(scabase + M_REG(EXS, ch), EXS_TES1);
				}
			}
			break;

		case PC300_TE:
			/* SCA always receives clock from the FALC chip */
			cpc_writeb(scabase + M_REG(TMCT, ch), 1);
			cpc_writeb(scabase + M_REG(TXS, ch), 0);
			cpc_writeb(scabase + M_REG(TMCR, ch), 1);
			cpc_writeb(scabase + M_REG(RXS, ch), 0);
			cpc_writeb(scabase + M_REG(EXS, ch), 0);
			break;
	}

	/* Enable Interrupts */
	cpc_writel(scabase + IER0,
		   cpc_readl(scabase + IER0) |
		   IR0_M(IR0_RXINTA, ch) |
		   IR0_DRX(IR0_EFT | IR0_DMIA | IR0_DMIB, ch) |
		   IR0_DTX(IR0_EFT | IR0_DMIA | IR0_DMIB, ch));
	cpc_writeb(scabase + M_REG(IE0, ch),
		   cpc_readl(scabase + M_REG(IE0, ch)) | IE0_RXINTA);
	cpc_writeb(scabase + M_REG(IE1, ch),
		   cpc_readl(scabase + M_REG(IE1, ch)) | IE1_CDCD);

	return 0;
}

2994
static int rx_config(pc300dev_t * d)
L
Linus Torvalds 已提交
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
{
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	void __iomem *scabase = card->hw.scabase;
	int ch = chan->channel;

	cpc_writeb(scabase + DSR_RX(ch), 0);

	/* General RX settings */
	cpc_writeb(scabase + M_REG(RRC, ch), 0);
	cpc_writeb(scabase + M_REG(RNR, ch), 16);

	/* Enable reception */
	cpc_writeb(scabase + M_REG(CMD, ch), CMD_RX_CRC_INIT);
	cpc_writeb(scabase + M_REG(CMD, ch), CMD_RX_ENA);

	/* Initialize DMA stuff */
	chan->rx_first_bd = 0;
	chan->rx_last_bd = N_DMA_RX_BUF - 1;
	rx_dma_buf_init(card, ch);
	cpc_writeb(scabase + DCR_RX(ch), DCR_FCT_CLR);
	cpc_writeb(scabase + DMR_RX(ch), (DMR_TMOD | DMR_NF));
	cpc_writeb(scabase + DIR_RX(ch), (DIR_EOM | DIR_BOF));

	/* Start DMA */
	rx_dma_start(card, ch);

	return 0;
}

3025
static int tx_config(pc300dev_t * d)
L
Linus Torvalds 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
{
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	void __iomem *scabase = card->hw.scabase;
	int ch = chan->channel;

	cpc_writeb(scabase + DSR_TX(ch), 0);

	/* General TX settings */
	cpc_writeb(scabase + M_REG(TRC0, ch), 0);
	cpc_writeb(scabase + M_REG(TFS, ch), 32);
	cpc_writeb(scabase + M_REG(TNR0, ch), 20);
	cpc_writeb(scabase + M_REG(TNR1, ch), 48);
	cpc_writeb(scabase + M_REG(TCR, ch), 8);

	/* Enable transmission */
	cpc_writeb(scabase + M_REG(CMD, ch), CMD_TX_CRC_INIT);

	/* Initialize DMA stuff */
	chan->tx_first_bd = 0;
	chan->tx_next_bd = 0;
	tx_dma_buf_init(card, ch);
	cpc_writeb(scabase + DCR_TX(ch), DCR_FCT_CLR);
	cpc_writeb(scabase + DMR_TX(ch), (DMR_TMOD | DMR_NF));
	cpc_writeb(scabase + DIR_TX(ch), (DIR_EOM | DIR_BOF | DIR_UDRF));
	cpc_writel(scabase + DTX_REG(CDAL, ch), TX_BD_ADDR(ch, chan->tx_first_bd));
	cpc_writel(scabase + DTX_REG(EDAL, ch), TX_BD_ADDR(ch, chan->tx_next_bd));

	return 0;
}

static int cpc_attach(struct net_device *dev, unsigned short encoding,
		      unsigned short parity)
{
3060
	pc300dev_t *d = (pc300dev_t *)dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
	pc300ch_t *chan = (pc300ch_t *)d->chan;
	pc300_t *card = (pc300_t *)chan->card;
	pc300chconf_t *conf = (pc300chconf_t *)&chan->conf;

	if (card->hw.type == PC300_TE) {
		if (encoding != ENCODING_NRZ && encoding != ENCODING_NRZI) {
			return -EINVAL;
		}
	} else {
		if (encoding != ENCODING_NRZ && encoding != ENCODING_NRZI &&
		    encoding != ENCODING_FM_MARK && encoding != ENCODING_FM_SPACE) {
			/* Driver doesn't support ENCODING_MANCHESTER yet */
			return -EINVAL;
		}
	}

	if (parity != PARITY_NONE && parity != PARITY_CRC16_PR0 &&
	    parity != PARITY_CRC16_PR1 && parity != PARITY_CRC32_PR1_CCITT &&
	    parity != PARITY_CRC16_PR1_CCITT) {
		return -EINVAL;
	}

	conf->proto_settings.encoding = encoding;
	conf->proto_settings.parity = parity;
	return 0;
}

3088
static int cpc_opench(pc300dev_t * d)
L
Linus Torvalds 已提交
3089 3090 3091
{
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
3092
	int ch = chan->channel, rc;
L
Linus Torvalds 已提交
3093 3094
	void __iomem *scabase = card->hw.scabase;

3095 3096 3097
	rc = ch_config(d);
	if (rc)
		return rc;
L
Linus Torvalds 已提交
3098 3099 3100 3101 3102 3103 3104 3105

	rx_config(d);

	tx_config(d);

	/* Assert RTS and DTR */
	cpc_writeb(scabase + M_REG(CTL, ch),
		   cpc_readb(scabase + M_REG(CTL, ch)) & ~(CTL_RTS | CTL_DTR));
3106 3107

	return 0;
L
Linus Torvalds 已提交
3108 3109
}

3110
static void cpc_closech(pc300dev_t * d)
L
Linus Torvalds 已提交
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139
{
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	falc_t *pfalc = (falc_t *) & chan->falc;
	int ch = chan->channel;

	cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_CH_RST);
	rx_dma_stop(card, ch);
	tx_dma_stop(card, ch);

	if (card->hw.type == PC300_TE) {
		memset(pfalc, 0, sizeof(falc_t));
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
			   ~((CPLD_REG2_FALC_TX_CLK | CPLD_REG2_FALC_RX_CLK |
			      CPLD_REG2_FALC_LED2) << (2 * ch)));
		/* Reset the FALC chip */
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) |
			   (CPLD_REG1_FALC_RESET << (2 * ch)));
		udelay(10000);
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) &
			   ~(CPLD_REG1_FALC_RESET << (2 * ch)));
	}
}

int cpc_open(struct net_device *dev)
{
3140
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
3141 3142 3143 3144 3145 3146 3147 3148
	struct ifreq ifr;
	int result;

#ifdef	PC300_DEBUG_OTHER
	printk("pc300: cpc_open");
#endif

	result = hdlc_open(dev);
3149 3150

	if (result)
L
Linus Torvalds 已提交
3151 3152 3153
		return result;

	sprintf(ifr.ifr_name, "%s", dev->name);
3154 3155 3156 3157
	result = cpc_opench(d);
	if (result)
		goto err_out;

L
Linus Torvalds 已提交
3158 3159
	netif_start_queue(dev);
	return 0;
3160 3161 3162 3163

err_out:
	hdlc_close(dev);
	return result;
L
Linus Torvalds 已提交
3164 3165
}

3166
static int cpc_close(struct net_device *dev)
L
Linus Torvalds 已提交
3167
{
3168
	pc300dev_t *d = (pc300dev_t *) dev_to_hdlc(dev)->priv;
L
Linus Torvalds 已提交
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
	pc300ch_t *chan = (pc300ch_t *) d->chan;
	pc300_t *card = (pc300_t *) chan->card;
	unsigned long flags;

#ifdef	PC300_DEBUG_OTHER
	printk("pc300: cpc_close");
#endif

	netif_stop_queue(dev);

	CPC_LOCK(card, flags);
	cpc_closech(d);
	CPC_UNLOCK(card, flags);

	hdlc_close(dev);
3184

L
Linus Torvalds 已提交
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
#ifdef CONFIG_PC300_MLPPP
	if (chan->conf.proto == PC300_PROTO_MLPPP) {
		cpc_tty_unregister_service(d);
		chan->conf.proto = 0xffff;
	}
#endif

	return 0;
}

3195
static u32 detect_ram(pc300_t * card)
L
Linus Torvalds 已提交
3196
{
3197 3198
	u32 i;
	u8 data;
L
Linus Torvalds 已提交
3199 3200 3201 3202 3203
	void __iomem *rambase = card->hw.rambase;

	card->hw.ramsize = PC300_RAMSIZE;
	/* Let's find out how much RAM is present on this board */
	for (i = 0; i < card->hw.ramsize; i++) {
3204
		data = (u8)(i & 0xff);
L
Linus Torvalds 已提交
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
		cpc_writeb(rambase + i, data);
		if (cpc_readb(rambase + i) != data) {
			break;
		}
	}
	return (i);
}

static void plx_init(pc300_t * card)
{
	struct RUNTIME_9050 __iomem *plx_ctl = card->hw.plxbase;

	/* Reset PLX */
	cpc_writel(&plx_ctl->init_ctrl,
		   cpc_readl(&plx_ctl->init_ctrl) | 0x40000000);
	udelay(10000L);
	cpc_writel(&plx_ctl->init_ctrl,
		   cpc_readl(&plx_ctl->init_ctrl) & ~0x40000000);

	/* Reload Config. Registers from EEPROM */
	cpc_writel(&plx_ctl->init_ctrl,
		   cpc_readl(&plx_ctl->init_ctrl) | 0x20000000);
	udelay(10000L);
	cpc_writel(&plx_ctl->init_ctrl,
		   cpc_readl(&plx_ctl->init_ctrl) & ~0x20000000);

}

static inline void show_version(void)
{
	char *rcsvers, *rcsdate, *tmp;

	rcsvers = strchr(rcsid, ' ');
	rcsvers++;
	tmp = strchr(rcsvers, ' ');
	*tmp++ = '\0';
	rcsdate = strchr(tmp, ' ');
	rcsdate++;
	tmp = strrchr(rcsdate, ' ');
	*tmp = '\0';
	printk(KERN_INFO "Cyclades-PC300 driver %s %s (built %s %s)\n", 
		rcsvers, rcsdate, __DATE__, __TIME__);
}				/* show_version */

3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
static const struct net_device_ops cpc_netdev_ops = {
	.ndo_open		= cpc_open,
	.ndo_stop		= cpc_close,
	.ndo_tx_timeout		= cpc_tx_timeout,
	.ndo_set_mac_address	= NULL,
	.ndo_change_mtu		= cpc_change_mtu,
	.ndo_do_ioctl		= cpc_ioctl,
	.ndo_validate_addr	= eth_validate_addr,
};

L
Linus Torvalds 已提交
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
static void cpc_init_card(pc300_t * card)
{
	int i, devcount = 0;
	static int board_nbr = 1;

	/* Enable interrupts on the PCI bridge */
	plx_init(card);
	cpc_writew(card->hw.plxbase + card->hw.intctl_reg,
		   cpc_readw(card->hw.plxbase + card->hw.intctl_reg) | 0x0040);

#ifdef USE_PCI_CLOCK
	/* Set board clock to PCI clock */
	cpc_writel(card->hw.plxbase + card->hw.gpioc_reg,
		   cpc_readl(card->hw.plxbase + card->hw.gpioc_reg) | 0x00000004UL);
	card->hw.clock = PC300_PCI_CLOCK;
#else
	/* Set board clock to internal oscillator clock */
	cpc_writel(card->hw.plxbase + card->hw.gpioc_reg,
		   cpc_readl(card->hw.plxbase + card->hw.gpioc_reg) & ~0x00000004UL);
	card->hw.clock = PC300_OSC_CLOCK;
#endif

	/* Detect actual on-board RAM size */
	card->hw.ramsize = detect_ram(card);

	/* Set Global SCA-II registers */
	cpc_writeb(card->hw.scabase + PCR, PCR_PR2);
	cpc_writeb(card->hw.scabase + BTCR, 0x10);
	cpc_writeb(card->hw.scabase + WCRL, 0);
	cpc_writeb(card->hw.scabase + DMER, 0x80);

	if (card->hw.type == PC300_TE) {
3291
		u8 reg1;
L
Linus Torvalds 已提交
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358

		/* Check CPLD version */
		reg1 = cpc_readb(card->hw.falcbase + CPLD_REG1);
		cpc_writeb(card->hw.falcbase + CPLD_REG1, (reg1 + 0x5a));
		if (cpc_readb(card->hw.falcbase + CPLD_REG1) == reg1) {
			/* New CPLD */
			card->hw.cpld_id = cpc_readb(card->hw.falcbase + CPLD_ID_REG);
			card->hw.cpld_reg1 = CPLD_V2_REG1;
			card->hw.cpld_reg2 = CPLD_V2_REG2;
		} else {
			/* old CPLD */
			card->hw.cpld_id = 0;
			card->hw.cpld_reg1 = CPLD_REG1;
			card->hw.cpld_reg2 = CPLD_REG2;
			cpc_writeb(card->hw.falcbase + CPLD_REG1, reg1);
		}

		/* Enable the board's global clock */
		cpc_writeb(card->hw.falcbase + card->hw.cpld_reg1,
			   cpc_readb(card->hw.falcbase + card->hw.cpld_reg1) |
			   CPLD_REG1_GLOBAL_CLK);

	}

	for (i = 0; i < card->hw.nchan; i++) {
		pc300ch_t *chan = &card->chan[i];
		pc300dev_t *d = &chan->d;
		hdlc_device *hdlc;
		struct net_device *dev;

		chan->card = card;
		chan->channel = i;
		chan->conf.phys_settings.clock_rate = 0;
		chan->conf.phys_settings.clock_type = CLOCK_EXT;
		chan->conf.proto_settings.encoding = ENCODING_NRZ;
		chan->conf.proto_settings.parity = PARITY_CRC16_PR1_CCITT;
		switch (card->hw.type) {
			case PC300_TE:
				chan->conf.media = IF_IFACE_T1;
				chan->conf.lcode = PC300_LC_B8ZS;
				chan->conf.fr_mode = PC300_FR_ESF;
				chan->conf.lbo = PC300_LBO_0_DB;
				chan->conf.rx_sens = PC300_RX_SENS_SH;
				chan->conf.tslot_bitmap = 0xffffffffUL;
				break;

			case PC300_X21:
				chan->conf.media = IF_IFACE_X21;
				break;

			case PC300_RSV:
			default:
				chan->conf.media = IF_IFACE_V35;
				break;
		}
		chan->conf.proto = IF_PROTO_PPP;
		chan->tx_first_bd = 0;
		chan->tx_next_bd = 0;
		chan->rx_first_bd = 0;
		chan->rx_last_bd = N_DMA_RX_BUF - 1;
		chan->nfree_tx_bd = N_DMA_TX_BUF;

		d->chan = chan;
		d->trace_on = 0;
		d->line_on = 0;
		d->line_off = 0;

3359
		dev = alloc_hdlcdev(d);
L
Linus Torvalds 已提交
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
		if (dev == NULL)
			continue;

		hdlc = dev_to_hdlc(dev);
		hdlc->xmit = cpc_queue_xmit;
		hdlc->attach = cpc_attach;
		d->dev = dev;
		dev->mem_start = card->hw.ramphys;
		dev->mem_end = card->hw.ramphys + card->hw.ramsize - 1;
		dev->irq = card->hw.irq;
		dev->tx_queue_len = PC300_TX_QUEUE_LEN;
		dev->mtu = PC300_DEF_MTU;

3373
		dev->netdev_ops = &cpc_netdev_ops;
L
Linus Torvalds 已提交
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
		dev->watchdog_timeo = PC300_TX_TIMEOUT;

		if (register_hdlc_device(dev) == 0) {
			printk("%s: Cyclades-PC300/", dev->name);
			switch (card->hw.type) {
				case PC300_TE:
					if (card->hw.bus == PC300_PMC) {
						printk("TE-M");
					} else {
						printk("TE  ");
					}
					break;

				case PC300_X21:
					printk("X21 ");
					break;

				case PC300_RSV:
				default:
					printk("RSV ");
					break;
			}
			printk (" #%d, %dKB of RAM at 0x%08x, IRQ%d, channel %d.\n",
				 board_nbr, card->hw.ramsize / 1024,
				 card->hw.ramphys, card->hw.irq, i + 1);
			devcount++;
		} else {
			printk ("Dev%d on card(0x%08x): unable to allocate i/f name.\n",
				 i + 1, card->hw.ramphys);
			free_netdev(dev);
			continue;
		}
	}
	spin_lock_init(&card->card_lock);

	board_nbr++;
}

static int __devinit
cpc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	static int first_time = 1;
3416
	int err, eeprom_outdated = 0;
3417
	u16 device_id;
L
Linus Torvalds 已提交
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
	pc300_t *card;

	if (first_time) {
		first_time = 0;
		show_version();
#ifdef CONFIG_PC300_MLPPP
		cpc_tty_reset_var();
#endif
	}

3428 3429 3430
	if ((err = pci_enable_device(pdev)) < 0)
		return err;

3431
	card = kzalloc(sizeof(pc300_t), GFP_KERNEL);
L
Linus Torvalds 已提交
3432
	if (card == NULL) {
3433
		printk("PC300 found at RAM 0x%016llx, "
L
Linus Torvalds 已提交
3434
		       "but could not allocate card structure.\n",
3435
		       (unsigned long long)pci_resource_start(pdev, 3));
3436 3437
		err = -ENOMEM;
		goto err_disable_dev;
L
Linus Torvalds 已提交
3438 3439
	}

3440 3441
	err = -ENODEV;

L
Linus Torvalds 已提交
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
	/* read PCI configuration area */
	device_id = ent->device;
	card->hw.irq = pdev->irq;
	card->hw.iophys = pci_resource_start(pdev, 1);
	card->hw.iosize = pci_resource_len(pdev, 1);
	card->hw.scaphys = pci_resource_start(pdev, 2);
	card->hw.scasize = pci_resource_len(pdev, 2);
	card->hw.ramphys = pci_resource_start(pdev, 3);
	card->hw.alloc_ramsize = pci_resource_len(pdev, 3);
	card->hw.falcphys = pci_resource_start(pdev, 4);
	card->hw.falcsize = pci_resource_len(pdev, 4);
	card->hw.plxphys = pci_resource_start(pdev, 5);
	card->hw.plxsize = pci_resource_len(pdev, 5);

	switch (device_id) {
		case PCI_DEVICE_ID_PC300_RX_1:
		case PCI_DEVICE_ID_PC300_TE_1:
		case PCI_DEVICE_ID_PC300_TE_M_1:
			card->hw.nchan = 1;
			break;

		case PCI_DEVICE_ID_PC300_RX_2:
		case PCI_DEVICE_ID_PC300_TE_2:
		case PCI_DEVICE_ID_PC300_TE_M_2:
		default:
			card->hw.nchan = PC300_MAXCHAN;
			break;
	}
#ifdef PC300_DEBUG_PCI
	printk("cpc (bus=0x0%x,pci_id=0x%x,", pdev->bus->number, pdev->devfn);
3472
	printk("rev_id=%d) IRQ%d\n", pdev->revision, card->hw.irq);
L
Linus Torvalds 已提交
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
	printk("cpc:found  ramaddr=0x%08lx plxaddr=0x%08lx "
	       "ctladdr=0x%08lx falcaddr=0x%08lx\n",
	       card->hw.ramphys, card->hw.plxphys, card->hw.scaphys,
	       card->hw.falcphys);
#endif
	/* Although we don't use this I/O region, we should
	 * request it from the kernel anyway, to avoid problems
	 * with other drivers accessing it. */
	if (!request_region(card->hw.iophys, card->hw.iosize, "PLX Registers")) {
		/* In case we can't allocate it, warn user */
		printk("WARNING: couldn't allocate I/O region for PC300 board "
		       "at 0x%08x!\n", card->hw.ramphys);
	}

	if (card->hw.plxphys) {
		pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, card->hw.plxphys);
	} else {
		eeprom_outdated = 1;
		card->hw.plxphys = pci_resource_start(pdev, 0);
		card->hw.plxsize = pci_resource_len(pdev, 0);
	}

	if (!request_mem_region(card->hw.plxphys, card->hw.plxsize,
				"PLX Registers")) {
		printk("PC300 found at RAM 0x%08x, "
		       "but could not allocate PLX mem region.\n",
		       card->hw.ramphys);
		goto err_release_io;
	}
	if (!request_mem_region(card->hw.ramphys, card->hw.alloc_ramsize,
				"On-board RAM")) {
		printk("PC300 found at RAM 0x%08x, "
		       "but could not allocate RAM mem region.\n",
		       card->hw.ramphys);
		goto err_release_plx;
	}
	if (!request_mem_region(card->hw.scaphys, card->hw.scasize,
				"SCA-II Registers")) {
		printk("PC300 found at RAM 0x%08x, "
		       "but could not allocate SCA mem region.\n",
		       card->hw.ramphys);
		goto err_release_ram;
	}

	card->hw.plxbase = ioremap(card->hw.plxphys, card->hw.plxsize);
	card->hw.rambase = ioremap(card->hw.ramphys, card->hw.alloc_ramsize);
	card->hw.scabase = ioremap(card->hw.scaphys, card->hw.scasize);
	switch (device_id) {
		case PCI_DEVICE_ID_PC300_TE_1:
		case PCI_DEVICE_ID_PC300_TE_2:
		case PCI_DEVICE_ID_PC300_TE_M_1:
		case PCI_DEVICE_ID_PC300_TE_M_2:
			request_mem_region(card->hw.falcphys, card->hw.falcsize,
					   "FALC Registers");
			card->hw.falcbase = ioremap(card->hw.falcphys, card->hw.falcsize);
			break;

		case PCI_DEVICE_ID_PC300_RX_1:
		case PCI_DEVICE_ID_PC300_RX_2:
		default:
			card->hw.falcbase = NULL;
			break;
	}

#ifdef PC300_DEBUG_PCI
	printk("cpc: relocate ramaddr=0x%08lx plxaddr=0x%08lx "
	       "ctladdr=0x%08lx falcaddr=0x%08lx\n",
	       card->hw.rambase, card->hw.plxbase, card->hw.scabase,
	       card->hw.falcbase);
#endif

	/* Set PCI drv pointer to the card structure */
	pci_set_drvdata(pdev, card);

	/* Set board type */
	switch (device_id) {
		case PCI_DEVICE_ID_PC300_TE_1:
		case PCI_DEVICE_ID_PC300_TE_2:
		case PCI_DEVICE_ID_PC300_TE_M_1:
		case PCI_DEVICE_ID_PC300_TE_M_2:
			card->hw.type = PC300_TE;

			if ((device_id == PCI_DEVICE_ID_PC300_TE_M_1) ||
			    (device_id == PCI_DEVICE_ID_PC300_TE_M_2)) {
				card->hw.bus = PC300_PMC;
				/* Set PLX register offsets */
				card->hw.gpioc_reg = 0x54;
				card->hw.intctl_reg = 0x4c;
			} else {
				card->hw.bus = PC300_PCI;
				/* Set PLX register offsets */
				card->hw.gpioc_reg = 0x50;
				card->hw.intctl_reg = 0x4c;
			}
			break;

		case PCI_DEVICE_ID_PC300_RX_1:
		case PCI_DEVICE_ID_PC300_RX_2:
		default:
			card->hw.bus = PC300_PCI;
			/* Set PLX register offsets */
			card->hw.gpioc_reg = 0x50;
			card->hw.intctl_reg = 0x4c;

			if ((cpc_readl(card->hw.plxbase + card->hw.gpioc_reg) & PC300_CTYPE_MASK)) {
				card->hw.type = PC300_X21;
			} else {
				card->hw.type = PC300_RSV;
			}
			break;
	}

	/* Allocate IRQ */
3586
	if (request_irq(card->hw.irq, cpc_intr, IRQF_SHARED, "Cyclades-PC300", card)) {
L
Linus Torvalds 已提交
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
		printk ("PC300 found at RAM 0x%08x, but could not allocate IRQ%d.\n",
			 card->hw.ramphys, card->hw.irq);
		goto err_io_unmap;
	}

	cpc_init_card(card);

	if (eeprom_outdated)
		printk("WARNING: PC300 with outdated EEPROM.\n");
	return 0;

err_io_unmap:
	iounmap(card->hw.plxbase);
	iounmap(card->hw.scabase);
	iounmap(card->hw.rambase);
	if (card->hw.type == PC300_TE) {
		iounmap(card->hw.falcbase);
		release_mem_region(card->hw.falcphys, card->hw.falcsize);
	}
	release_mem_region(card->hw.scaphys, card->hw.scasize);
err_release_ram:
	release_mem_region(card->hw.ramphys, card->hw.alloc_ramsize);
err_release_plx:
	release_mem_region(card->hw.plxphys, card->hw.plxsize);
err_release_io:
	release_region(card->hw.iophys, card->hw.iosize);
	kfree(card);
3614 3615 3616
err_disable_dev:
	pci_disable_device(pdev);
	return err;
L
Linus Torvalds 已提交
3617 3618 3619 3620 3621 3622
}

static void __devexit cpc_remove_one(struct pci_dev *pdev)
{
	pc300_t *card = pci_get_drvdata(pdev);

A
Al Viro 已提交
3623
	if (card->hw.rambase) {
L
Linus Torvalds 已提交
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
		int i;

		/* Disable interrupts on the PCI bridge */
		cpc_writew(card->hw.plxbase + card->hw.intctl_reg,
			   cpc_readw(card->hw.plxbase + card->hw.intctl_reg) & ~(0x0040));

		for (i = 0; i < card->hw.nchan; i++) {
			unregister_hdlc_device(card->chan[i].d.dev);
		}
		iounmap(card->hw.plxbase);
		iounmap(card->hw.scabase);
		iounmap(card->hw.rambase);
		release_mem_region(card->hw.plxphys, card->hw.plxsize);
		release_mem_region(card->hw.ramphys, card->hw.alloc_ramsize);
		release_mem_region(card->hw.scaphys, card->hw.scasize);
		release_region(card->hw.iophys, card->hw.iosize);
		if (card->hw.type == PC300_TE) {
			iounmap(card->hw.falcbase);
			release_mem_region(card->hw.falcphys, card->hw.falcsize);
		}
		for (i = 0; i < card->hw.nchan; i++)
			if (card->chan[i].d.dev)
				free_netdev(card->chan[i].d.dev);
		if (card->hw.irq)
			free_irq(card->hw.irq, card);
		kfree(card);
3650
		pci_disable_device(pdev);
L
Linus Torvalds 已提交
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662
	}
}

static struct pci_driver cpc_driver = {
	.name           = "pc300",
	.id_table       = cpc_pci_dev_id,
	.probe          = cpc_init_one,
	.remove         = __devexit_p(cpc_remove_one),
};

static int __init cpc_init(void)
{
3663
	return pci_register_driver(&cpc_driver);
L
Linus Torvalds 已提交
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
}

static void __exit cpc_cleanup_module(void)
{
	pci_unregister_driver(&cpc_driver);
}

module_init(cpc_init);
module_exit(cpc_cleanup_module);

MODULE_DESCRIPTION("Cyclades-PC300 cards driver");
MODULE_AUTHOR(  "Author: Ivan Passos <ivan@cyclades.com>\r\n"
                "Maintainer: PC300 Maintainer <pc300@cyclades.com");
MODULE_LICENSE("GPL");