xref: /openbmc/linux/drivers/tty/serial/icom.c (revision 2c334f12)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3   * icom.c
4   *
5   * Copyright (C) 2001 IBM Corporation. All rights reserved.
6   *
7   * Serial device driver.
8   *
9   * Based on code from serial.c
10   */
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/signal.h>
15 #include <linux/timer.h>
16 #include <linux/interrupt.h>
17 #include <linux/tty.h>
18 #include <linux/termios.h>
19 #include <linux/fs.h>
20 #include <linux/tty_flip.h>
21 #include <linux/serial.h>
22 #include <linux/serial_reg.h>
23 #include <linux/major.h>
24 #include <linux/string.h>
25 #include <linux/fcntl.h>
26 #include <linux/ptrace.h>
27 #include <linux/ioport.h>
28 #include <linux/mm.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/vmalloc.h>
34 #include <linux/smp.h>
35 #include <linux/spinlock.h>
36 #include <linux/kref.h>
37 #include <linux/firmware.h>
38 #include <linux/bitops.h>
39 
40 #include <linux/io.h>
41 #include <asm/irq.h>
42 #include <linux/uaccess.h>
43 
44 #include "icom.h"
45 
46 /*#define ICOM_TRACE		 enable port trace capabilities */
47 
48 #define ICOM_DRIVER_NAME "icom"
49 #define NR_PORTS	       128
50 
51 static inline struct icom_port *to_icom_port(struct uart_port *port)
52 {
53 	return container_of(port, struct icom_port, uart_port);
54 }
55 
56 static const struct pci_device_id icom_pci_table[] = {
57 	{
58 		.vendor = PCI_VENDOR_ID_IBM,
59 		.device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_1,
60 		.subvendor = PCI_ANY_ID,
61 		.subdevice = PCI_ANY_ID,
62 		.driver_data = ADAPTER_V1,
63 	},
64 	{
65 		.vendor = PCI_VENDOR_ID_IBM,
66 		.device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
67 		.subvendor = PCI_VENDOR_ID_IBM,
68 		.subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_TWO_PORTS_RVX,
69 		.driver_data = ADAPTER_V2,
70 	},
71 	{
72 		.vendor = PCI_VENDOR_ID_IBM,
73 		.device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
74 		.subvendor = PCI_VENDOR_ID_IBM,
75 		.subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM,
76 		.driver_data = ADAPTER_V2,
77 	},
78 	{
79 		.vendor = PCI_VENDOR_ID_IBM,
80 		.device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
81 		.subvendor = PCI_VENDOR_ID_IBM,
82 		.subdevice = PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL,
83 		.driver_data = ADAPTER_V2,
84 	},
85 	{
86 		.vendor = PCI_VENDOR_ID_IBM,
87 		.device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
88 		.subvendor = PCI_VENDOR_ID_IBM,
89 		.subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM_PCIE,
90 		.driver_data = ADAPTER_V2,
91 	},
92 	{}
93 };
94 
95 static struct lookup_proc_table start_proc[4] = {
96 	{NULL, ICOM_CONTROL_START_A},
97 	{NULL, ICOM_CONTROL_START_B},
98 	{NULL, ICOM_CONTROL_START_C},
99 	{NULL, ICOM_CONTROL_START_D}
100 };
101 
102 
103 static struct lookup_proc_table stop_proc[4] = {
104 	{NULL, ICOM_CONTROL_STOP_A},
105 	{NULL, ICOM_CONTROL_STOP_B},
106 	{NULL, ICOM_CONTROL_STOP_C},
107 	{NULL, ICOM_CONTROL_STOP_D}
108 };
109 
110 static struct lookup_int_table int_mask_tbl[4] = {
111 	{NULL, ICOM_INT_MASK_PRC_A},
112 	{NULL, ICOM_INT_MASK_PRC_B},
113 	{NULL, ICOM_INT_MASK_PRC_C},
114 	{NULL, ICOM_INT_MASK_PRC_D},
115 };
116 
117 
118 MODULE_DEVICE_TABLE(pci, icom_pci_table);
119 
120 static LIST_HEAD(icom_adapter_head);
121 
122 /* spinlock for adapter initialization and changing adapter operations */
123 static DEFINE_SPINLOCK(icom_lock);
124 
125 #ifdef ICOM_TRACE
126 static inline void trace(struct icom_port *icom_port, char *trace_pt,
127 			unsigned long trace_data)
128 {
129 	dev_info(&icom_port->adapter->pci_dev->dev, ":%d:%s - %lx\n",
130 	icom_port->port, trace_pt, trace_data);
131 }
132 #else
133 static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};
134 #endif
135 static void icom_kref_release(struct kref *kref);
136 
137 static void free_port_memory(struct icom_port *icom_port)
138 {
139 	struct pci_dev *dev = icom_port->adapter->pci_dev;
140 
141 	trace(icom_port, "RET_PORT_MEM", 0);
142 	if (icom_port->recv_buf) {
143 		dma_free_coherent(&dev->dev, 4096, icom_port->recv_buf,
144 				  icom_port->recv_buf_pci);
145 		icom_port->recv_buf = NULL;
146 	}
147 	if (icom_port->xmit_buf) {
148 		dma_free_coherent(&dev->dev, 4096, icom_port->xmit_buf,
149 				  icom_port->xmit_buf_pci);
150 		icom_port->xmit_buf = NULL;
151 	}
152 	if (icom_port->statStg) {
153 		dma_free_coherent(&dev->dev, 4096, icom_port->statStg,
154 				  icom_port->statStg_pci);
155 		icom_port->statStg = NULL;
156 	}
157 
158 	if (icom_port->xmitRestart) {
159 		dma_free_coherent(&dev->dev, 4096, icom_port->xmitRestart,
160 				  icom_port->xmitRestart_pci);
161 		icom_port->xmitRestart = NULL;
162 	}
163 }
164 
165 static int get_port_memory(struct icom_port *icom_port)
166 {
167 	int index;
168 	unsigned long stgAddr;
169 	unsigned long startStgAddr;
170 	unsigned long offset;
171 	struct pci_dev *dev = icom_port->adapter->pci_dev;
172 
173 	icom_port->xmit_buf =
174 	    dma_alloc_coherent(&dev->dev, 4096, &icom_port->xmit_buf_pci,
175 			       GFP_KERNEL);
176 	if (!icom_port->xmit_buf) {
177 		dev_err(&dev->dev, "Can not allocate Transmit buffer\n");
178 		return -ENOMEM;
179 	}
180 
181 	trace(icom_port, "GET_PORT_MEM",
182 	      (unsigned long) icom_port->xmit_buf);
183 
184 	icom_port->recv_buf =
185 	    dma_alloc_coherent(&dev->dev, 4096, &icom_port->recv_buf_pci,
186 			       GFP_KERNEL);
187 	if (!icom_port->recv_buf) {
188 		dev_err(&dev->dev, "Can not allocate Receive buffer\n");
189 		free_port_memory(icom_port);
190 		return -ENOMEM;
191 	}
192 	trace(icom_port, "GET_PORT_MEM",
193 	      (unsigned long) icom_port->recv_buf);
194 
195 	icom_port->statStg =
196 	    dma_alloc_coherent(&dev->dev, 4096, &icom_port->statStg_pci,
197 			       GFP_KERNEL);
198 	if (!icom_port->statStg) {
199 		dev_err(&dev->dev, "Can not allocate Status buffer\n");
200 		free_port_memory(icom_port);
201 		return -ENOMEM;
202 	}
203 	trace(icom_port, "GET_PORT_MEM",
204 	      (unsigned long) icom_port->statStg);
205 
206 	icom_port->xmitRestart =
207 	    dma_alloc_coherent(&dev->dev, 4096, &icom_port->xmitRestart_pci,
208 			       GFP_KERNEL);
209 	if (!icom_port->xmitRestart) {
210 		dev_err(&dev->dev,
211 			"Can not allocate xmit Restart buffer\n");
212 		free_port_memory(icom_port);
213 		return -ENOMEM;
214 	}
215 
216 	/* FODs: Frame Out Descriptor Queue, this is a FIFO queue that
217            indicates that frames are to be transmitted
218 	*/
219 
220 	stgAddr = (unsigned long) icom_port->statStg;
221 	for (index = 0; index < NUM_XBUFFS; index++) {
222 		trace(icom_port, "FOD_ADDR", stgAddr);
223 		stgAddr = stgAddr + sizeof(icom_port->statStg->xmit[0]);
224 		if (index < (NUM_XBUFFS - 1)) {
225 			memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
226 			icom_port->statStg->xmit[index].leLengthASD =
227 			    (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
228 			trace(icom_port, "FOD_ADDR", stgAddr);
229 			trace(icom_port, "FOD_XBUFF",
230 			      (unsigned long) icom_port->xmit_buf);
231 			icom_port->statStg->xmit[index].leBuffer =
232 			    cpu_to_le32(icom_port->xmit_buf_pci);
233 		} else if (index == (NUM_XBUFFS - 1)) {
234 			memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
235 			icom_port->statStg->xmit[index].leLengthASD =
236 			    (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
237 			trace(icom_port, "FOD_XBUFF",
238 			      (unsigned long) icom_port->xmit_buf);
239 			icom_port->statStg->xmit[index].leBuffer =
240 			    cpu_to_le32(icom_port->xmit_buf_pci);
241 		} else {
242 			memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
243 		}
244 	}
245 	/* FIDs */
246 	startStgAddr = stgAddr;
247 
248 	/* fill in every entry, even if no buffer */
249 	for (index = 0; index <  NUM_RBUFFS; index++) {
250 		trace(icom_port, "FID_ADDR", stgAddr);
251 		stgAddr = stgAddr + sizeof(icom_port->statStg->rcv[0]);
252 		icom_port->statStg->rcv[index].leLength = 0;
253 		icom_port->statStg->rcv[index].WorkingLength =
254 		    (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
255 		if (index < (NUM_RBUFFS - 1) ) {
256 			offset = stgAddr - (unsigned long) icom_port->statStg;
257 			icom_port->statStg->rcv[index].leNext =
258 			      cpu_to_le32(icom_port-> statStg_pci + offset);
259 			trace(icom_port, "FID_RBUFF",
260 			      (unsigned long) icom_port->recv_buf);
261 			icom_port->statStg->rcv[index].leBuffer =
262 			    cpu_to_le32(icom_port->recv_buf_pci);
263 		} else if (index == (NUM_RBUFFS -1) ) {
264 			offset = startStgAddr - (unsigned long) icom_port->statStg;
265 			icom_port->statStg->rcv[index].leNext =
266 			    cpu_to_le32(icom_port-> statStg_pci + offset);
267 			trace(icom_port, "FID_RBUFF",
268 			      (unsigned long) icom_port->recv_buf + 2048);
269 			icom_port->statStg->rcv[index].leBuffer =
270 			    cpu_to_le32(icom_port->recv_buf_pci + 2048);
271 		} else {
272 			icom_port->statStg->rcv[index].leNext = 0;
273 			icom_port->statStg->rcv[index].leBuffer = 0;
274 		}
275 	}
276 
277 	return 0;
278 }
279 
280 static void stop_processor(struct icom_port *icom_port)
281 {
282 	unsigned long temp;
283 	unsigned long flags;
284 	int port;
285 
286 	spin_lock_irqsave(&icom_lock, flags);
287 
288 	port = icom_port->port;
289 	if (port >= ARRAY_SIZE(stop_proc)) {
290 		dev_err(&icom_port->adapter->pci_dev->dev,
291 			"Invalid port assignment\n");
292 		goto unlock;
293 	}
294 
295 	if (port == 0 || port == 1)
296 		stop_proc[port].global_control_reg = &icom_port->global_reg->control;
297 	else
298 		stop_proc[port].global_control_reg = &icom_port->global_reg->control_2;
299 
300 	temp = readl(stop_proc[port].global_control_reg);
301 	temp = (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
302 	writel(temp, stop_proc[port].global_control_reg);
303 
304 	/* write flush */
305 	readl(stop_proc[port].global_control_reg);
306 
307 unlock:
308 	spin_unlock_irqrestore(&icom_lock, flags);
309 }
310 
311 static void start_processor(struct icom_port *icom_port)
312 {
313 	unsigned long temp;
314 	unsigned long flags;
315 	int port;
316 
317 	spin_lock_irqsave(&icom_lock, flags);
318 
319 	port = icom_port->port;
320 	if (port >= ARRAY_SIZE(start_proc)) {
321 		dev_err(&icom_port->adapter->pci_dev->dev,
322 			"Invalid port assignment\n");
323 		goto unlock;
324 	}
325 
326 	if (port == 0 || port == 1)
327 		start_proc[port].global_control_reg = &icom_port->global_reg->control;
328 	else
329 		start_proc[port].global_control_reg = &icom_port->global_reg->control_2;
330 
331 	temp = readl(start_proc[port].global_control_reg);
332 	temp = (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
333 	writel(temp, start_proc[port].global_control_reg);
334 
335 	/* write flush */
336 	readl(start_proc[port].global_control_reg);
337 
338 unlock:
339 	spin_unlock_irqrestore(&icom_lock, flags);
340 }
341 
342 static void load_code(struct icom_port *icom_port)
343 {
344 	const struct firmware *fw;
345 	char __iomem *iram_ptr;
346 	int index;
347 	int status = 0;
348 	void __iomem *dram_ptr = icom_port->dram;
349 	dma_addr_t temp_pci;
350 	unsigned char *new_page = NULL;
351 	unsigned char cable_id = NO_CABLE;
352 	struct pci_dev *dev = icom_port->adapter->pci_dev;
353 
354 	/* Clear out any pending interrupts */
355 	writew(0x3FFF, icom_port->int_reg);
356 
357 	trace(icom_port, "CLEAR_INTERRUPTS", 0);
358 
359 	/* Stop processor */
360 	stop_processor(icom_port);
361 
362 	/* Zero out DRAM */
363 	memset_io(dram_ptr, 0, 512);
364 
365 	/* Load Call Setup into Adapter */
366 	if (request_firmware(&fw, "icom_call_setup.bin", &dev->dev) < 0) {
367 		dev_err(&dev->dev,"Unable to load icom_call_setup.bin firmware image\n");
368 		status = -1;
369 		goto load_code_exit;
370 	}
371 
372 	if (fw->size > ICOM_DCE_IRAM_OFFSET) {
373 		dev_err(&dev->dev, "Invalid firmware image for icom_call_setup.bin found.\n");
374 		release_firmware(fw);
375 		status = -1;
376 		goto load_code_exit;
377 	}
378 
379 	iram_ptr = (char __iomem *)icom_port->dram + ICOM_IRAM_OFFSET;
380 	for (index = 0; index < fw->size; index++)
381 		writeb(fw->data[index], &iram_ptr[index]);
382 
383 	release_firmware(fw);
384 
385 	/* Load Resident DCE portion of Adapter */
386 	if (request_firmware(&fw, "icom_res_dce.bin", &dev->dev) < 0) {
387 		dev_err(&dev->dev,"Unable to load icom_res_dce.bin firmware image\n");
388 		status = -1;
389 		goto load_code_exit;
390 	}
391 
392 	if (fw->size > ICOM_IRAM_SIZE) {
393 		dev_err(&dev->dev, "Invalid firmware image for icom_res_dce.bin found.\n");
394 		release_firmware(fw);
395 		status = -1;
396 		goto load_code_exit;
397 	}
398 
399 	iram_ptr = (char __iomem *) icom_port->dram + ICOM_IRAM_OFFSET;
400 	for (index = ICOM_DCE_IRAM_OFFSET; index < fw->size; index++)
401 		writeb(fw->data[index], &iram_ptr[index]);
402 
403 	release_firmware(fw);
404 
405 	/* Set Hardware level */
406 	if (icom_port->adapter->version == ADAPTER_V2)
407 		writeb(V2_HARDWARE, &(icom_port->dram->misc_flags));
408 
409 	/* Start the processor in Adapter */
410 	start_processor(icom_port);
411 
412 	writeb((HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL),
413 	       &(icom_port->dram->HDLCConfigReg));
414 	writeb(0x04, &(icom_port->dram->FlagFillIdleTimer));	/* 0.5 seconds */
415 	writeb(0x00, &(icom_port->dram->CmdReg));
416 	writeb(0x10, &(icom_port->dram->async_config3));
417 	writeb((ICOM_ACFG_DRIVE1 | ICOM_ACFG_NO_PARITY | ICOM_ACFG_8BPC |
418 		ICOM_ACFG_1STOP_BIT), &(icom_port->dram->async_config2));
419 
420 	/*Set up data in icom DRAM to indicate where personality
421 	 *code is located and its length.
422 	 */
423 	new_page = dma_alloc_coherent(&dev->dev, 4096, &temp_pci, GFP_KERNEL);
424 
425 	if (!new_page) {
426 		dev_err(&dev->dev, "Can not allocate DMA buffer\n");
427 		status = -1;
428 		goto load_code_exit;
429 	}
430 
431 	if (request_firmware(&fw, "icom_asc.bin", &dev->dev) < 0) {
432 		dev_err(&dev->dev,"Unable to load icom_asc.bin firmware image\n");
433 		status = -1;
434 		goto load_code_exit;
435 	}
436 
437 	if (fw->size > ICOM_DCE_IRAM_OFFSET) {
438 		dev_err(&dev->dev, "Invalid firmware image for icom_asc.bin found.\n");
439 		release_firmware(fw);
440 		status = -1;
441 		goto load_code_exit;
442 	}
443 
444 	for (index = 0; index < fw->size; index++)
445 		new_page[index] = fw->data[index];
446 
447 	writeb((char) ((fw->size + 16)/16), &icom_port->dram->mac_length);
448 	writel(temp_pci, &icom_port->dram->mac_load_addr);
449 
450 	release_firmware(fw);
451 
452 	/*Setting the syncReg to 0x80 causes adapter to start downloading
453 	   the personality code into adapter instruction RAM.
454 	   Once code is loaded, it will begin executing and, based on
455 	   information provided above, will start DMAing data from
456 	   shared memory to adapter DRAM.
457 	 */
458 	/* the wait loop below verifies this write operation has been done
459 	   and processed
460 	*/
461 	writeb(START_DOWNLOAD, &icom_port->dram->sync);
462 
463 	/* Wait max 1 Sec for data download and processor to start */
464 	for (index = 0; index < 10; index++) {
465 		msleep(100);
466 		if (readb(&icom_port->dram->misc_flags) & ICOM_HDW_ACTIVE)
467 			break;
468 	}
469 
470 	if (index == 10)
471 		status = -1;
472 
473 	/*
474 	 * check Cable ID
475 	 */
476 	cable_id = readb(&icom_port->dram->cable_id);
477 
478 	if (cable_id & ICOM_CABLE_ID_VALID) {
479 		/* Get cable ID into the lower 4 bits (standard form) */
480 		cable_id = (cable_id & ICOM_CABLE_ID_MASK) >> 4;
481 		icom_port->cable_id = cable_id;
482 	} else {
483 		dev_err(&dev->dev,"Invalid or no cable attached\n");
484 		icom_port->cable_id = NO_CABLE;
485 	}
486 
487       load_code_exit:
488 
489 	if (status != 0) {
490 		/* Clear out any pending interrupts */
491 		writew(0x3FFF, icom_port->int_reg);
492 
493 		/* Turn off port */
494 		writeb(ICOM_DISABLE, &(icom_port->dram->disable));
495 
496 		/* Stop processor */
497 		stop_processor(icom_port);
498 
499 		dev_err(&icom_port->adapter->pci_dev->dev,"Port not operational\n");
500 	}
501 
502 	if (new_page != NULL)
503 		dma_free_coherent(&dev->dev, 4096, new_page, temp_pci);
504 }
505 
506 static int startup(struct icom_port *icom_port)
507 {
508 	unsigned long temp;
509 	unsigned char cable_id, raw_cable_id;
510 	unsigned long flags;
511 	int port;
512 
513 	trace(icom_port, "STARTUP", 0);
514 
515 	if (!icom_port->dram) {
516 		/* should NEVER be NULL */
517 		dev_err(&icom_port->adapter->pci_dev->dev,
518 			"Unusable Port, port configuration missing\n");
519 		return -ENODEV;
520 	}
521 
522 	/*
523 	 * check Cable ID
524 	 */
525 	raw_cable_id = readb(&icom_port->dram->cable_id);
526 	trace(icom_port, "CABLE_ID", raw_cable_id);
527 
528 	/* Get cable ID into the lower 4 bits (standard form) */
529 	cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
530 
531 	/* Check for valid Cable ID */
532 	if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
533 	    (cable_id != icom_port->cable_id)) {
534 
535 		/* reload adapter code, pick up any potential changes in cable id */
536 		load_code(icom_port);
537 
538 		/* still no sign of cable, error out */
539 		raw_cable_id = readb(&icom_port->dram->cable_id);
540 		cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
541 		if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
542 		    (icom_port->cable_id == NO_CABLE))
543 			return -EIO;
544 	}
545 
546 	/*
547 	 * Finally, clear and  enable interrupts
548 	 */
549 	spin_lock_irqsave(&icom_lock, flags);
550 	port = icom_port->port;
551 	if (port >= ARRAY_SIZE(int_mask_tbl)) {
552 		dev_err(&icom_port->adapter->pci_dev->dev,
553 			"Invalid port assignment\n");
554 		goto unlock;
555 	}
556 
557 	if (port == 0 || port == 1)
558 		int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
559 	else
560 		int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
561 
562 	if (port == 0 || port == 2)
563 		writew(0x00FF, icom_port->int_reg);
564 	else
565 		writew(0x3F00, icom_port->int_reg);
566 
567 	temp = readl(int_mask_tbl[port].global_int_mask);
568 	writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
569 
570 	/* write flush */
571 	readl(int_mask_tbl[port].global_int_mask);
572 
573 unlock:
574 	spin_unlock_irqrestore(&icom_lock, flags);
575 	return 0;
576 }
577 
578 static void shutdown(struct icom_port *icom_port)
579 {
580 	unsigned long temp;
581 	unsigned char cmdReg;
582 	unsigned long flags;
583 	int port;
584 
585 	spin_lock_irqsave(&icom_lock, flags);
586 	trace(icom_port, "SHUTDOWN", 0);
587 
588 	/*
589 	 * disable all interrupts
590 	 */
591 	port = icom_port->port;
592 	if (port >= ARRAY_SIZE(int_mask_tbl)) {
593 		dev_err(&icom_port->adapter->pci_dev->dev,
594 			"Invalid port assignment\n");
595 		goto unlock;
596 	}
597 	if (port == 0 || port == 1)
598 		int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
599 	else
600 		int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
601 
602 	temp = readl(int_mask_tbl[port].global_int_mask);
603 	writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
604 
605 	/* write flush */
606 	readl(int_mask_tbl[port].global_int_mask);
607 
608 unlock:
609 	spin_unlock_irqrestore(&icom_lock, flags);
610 
611 	/*
612 	 * disable break condition
613 	 */
614 	cmdReg = readb(&icom_port->dram->CmdReg);
615 	if (cmdReg & CMD_SND_BREAK) {
616 		writeb(cmdReg & ~CMD_SND_BREAK, &icom_port->dram->CmdReg);
617 	}
618 }
619 
620 static int icom_write(struct uart_port *port)
621 {
622 	struct icom_port *icom_port = to_icom_port(port);
623 	unsigned long data_count;
624 	unsigned char cmdReg;
625 	unsigned long offset;
626 	int temp_tail = port->state->xmit.tail;
627 
628 	trace(icom_port, "WRITE", 0);
629 
630 	if (cpu_to_le16(icom_port->statStg->xmit[0].flags) &
631 	    SA_FLAGS_READY_TO_XMIT) {
632 		trace(icom_port, "WRITE_FULL", 0);
633 		return 0;
634 	}
635 
636 	data_count = 0;
637 	while ((port->state->xmit.head != temp_tail) &&
638 	       (data_count <= XMIT_BUFF_SZ)) {
639 
640 		icom_port->xmit_buf[data_count++] =
641 		    port->state->xmit.buf[temp_tail];
642 
643 		temp_tail++;
644 		temp_tail &= (UART_XMIT_SIZE - 1);
645 	}
646 
647 	if (data_count) {
648 		icom_port->statStg->xmit[0].flags =
649 		    cpu_to_le16(SA_FLAGS_READY_TO_XMIT);
650 		icom_port->statStg->xmit[0].leLength =
651 		    cpu_to_le16(data_count);
652 		offset =
653 		    (unsigned long) &icom_port->statStg->xmit[0] -
654 		    (unsigned long) icom_port->statStg;
655 		*icom_port->xmitRestart =
656 		    cpu_to_le32(icom_port->statStg_pci + offset);
657 		cmdReg = readb(&icom_port->dram->CmdReg);
658 		writeb(cmdReg | CMD_XMIT_RCV_ENABLE,
659 		       &icom_port->dram->CmdReg);
660 		writeb(START_XMIT, &icom_port->dram->StartXmitCmd);
661 		trace(icom_port, "WRITE_START", data_count);
662 		/* write flush */
663 		readb(&icom_port->dram->StartXmitCmd);
664 	}
665 
666 	return data_count;
667 }
668 
669 static inline void check_modem_status(struct icom_port *icom_port)
670 {
671 	static char old_status = 0;
672 	char delta_status;
673 	unsigned char status;
674 
675 	spin_lock(&icom_port->uart_port.lock);
676 
677 	/*modem input register */
678 	status = readb(&icom_port->dram->isr);
679 	trace(icom_port, "CHECK_MODEM", status);
680 	delta_status = status ^ old_status;
681 	if (delta_status) {
682 		if (delta_status & ICOM_RI)
683 			icom_port->uart_port.icount.rng++;
684 		if (delta_status & ICOM_DSR)
685 			icom_port->uart_port.icount.dsr++;
686 		if (delta_status & ICOM_DCD)
687 			uart_handle_dcd_change(&icom_port->uart_port,
688 					       delta_status & ICOM_DCD);
689 		if (delta_status & ICOM_CTS)
690 			uart_handle_cts_change(&icom_port->uart_port,
691 					       delta_status & ICOM_CTS);
692 
693 		wake_up_interruptible(&icom_port->uart_port.state->
694 				      port.delta_msr_wait);
695 		old_status = status;
696 	}
697 	spin_unlock(&icom_port->uart_port.lock);
698 }
699 
700 static void xmit_interrupt(u16 port_int_reg, struct icom_port *icom_port)
701 {
702 	unsigned short int count;
703 	int i;
704 
705 	if (port_int_reg & (INT_XMIT_COMPLETED)) {
706 		trace(icom_port, "XMIT_COMPLETE", 0);
707 
708 		/* clear buffer in use bit */
709 		icom_port->statStg->xmit[0].flags &=
710 			cpu_to_le16(~SA_FLAGS_READY_TO_XMIT);
711 
712 		count = (unsigned short int)
713 			cpu_to_le16(icom_port->statStg->xmit[0].leLength);
714 		icom_port->uart_port.icount.tx += count;
715 
716 		for (i=0; i<count &&
717 			!uart_circ_empty(&icom_port->uart_port.state->xmit); i++) {
718 
719 			icom_port->uart_port.state->xmit.tail++;
720 			icom_port->uart_port.state->xmit.tail &=
721 				(UART_XMIT_SIZE - 1);
722 		}
723 
724 		if (!icom_write(&icom_port->uart_port))
725 			/* activate write queue */
726 			uart_write_wakeup(&icom_port->uart_port);
727 	} else
728 		trace(icom_port, "XMIT_DISABLED", 0);
729 }
730 
731 static void recv_interrupt(u16 port_int_reg, struct icom_port *icom_port)
732 {
733 	short int count, rcv_buff;
734 	struct tty_port *port = &icom_port->uart_port.state->port;
735 	unsigned short int status;
736 	struct uart_icount *icount;
737 	unsigned long offset;
738 	unsigned char flag;
739 
740 	trace(icom_port, "RCV_COMPLETE", 0);
741 	rcv_buff = icom_port->next_rcv;
742 
743 	status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
744 	while (status & SA_FL_RCV_DONE) {
745 		int first = -1;
746 
747 		trace(icom_port, "FID_STATUS", status);
748 		count = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].leLength);
749 
750 		trace(icom_port, "RCV_COUNT", count);
751 
752 		trace(icom_port, "REAL_COUNT", count);
753 
754 		offset =
755 			cpu_to_le32(icom_port->statStg->rcv[rcv_buff].leBuffer) -
756 			icom_port->recv_buf_pci;
757 
758 		/* Block copy all but the last byte as this may have status */
759 		if (count > 0) {
760 			first = icom_port->recv_buf[offset];
761 			tty_insert_flip_string(port, icom_port->recv_buf + offset, count - 1);
762 		}
763 
764 		icount = &icom_port->uart_port.icount;
765 		icount->rx += count;
766 
767 		/* Break detect logic */
768 		if ((status & SA_FLAGS_FRAME_ERROR)
769 		    && first == 0) {
770 			status &= ~SA_FLAGS_FRAME_ERROR;
771 			status |= SA_FLAGS_BREAK_DET;
772 			trace(icom_port, "BREAK_DET", 0);
773 		}
774 
775 		flag = TTY_NORMAL;
776 
777 		if (status &
778 		    (SA_FLAGS_BREAK_DET | SA_FLAGS_PARITY_ERROR |
779 		     SA_FLAGS_FRAME_ERROR | SA_FLAGS_OVERRUN)) {
780 
781 			if (status & SA_FLAGS_BREAK_DET)
782 				icount->brk++;
783 			if (status & SA_FLAGS_PARITY_ERROR)
784 				icount->parity++;
785 			if (status & SA_FLAGS_FRAME_ERROR)
786 				icount->frame++;
787 			if (status & SA_FLAGS_OVERRUN)
788 				icount->overrun++;
789 
790 			/*
791 			 * Now check to see if character should be
792 			 * ignored, and mask off conditions which
793 			 * should be ignored.
794 			 */
795 			if (status & icom_port->ignore_status_mask) {
796 				trace(icom_port, "IGNORE_CHAR", 0);
797 				goto ignore_char;
798 			}
799 
800 			status &= icom_port->read_status_mask;
801 
802 			if (status & SA_FLAGS_BREAK_DET) {
803 				flag = TTY_BREAK;
804 			} else if (status & SA_FLAGS_PARITY_ERROR) {
805 				trace(icom_port, "PARITY_ERROR", 0);
806 				flag = TTY_PARITY;
807 			} else if (status & SA_FLAGS_FRAME_ERROR)
808 				flag = TTY_FRAME;
809 
810 		}
811 
812 		tty_insert_flip_char(port, *(icom_port->recv_buf + offset + count - 1), flag);
813 
814 		if (status & SA_FLAGS_OVERRUN)
815 			/*
816 			 * Overrun is special, since it's
817 			 * reported immediately, and doesn't
818 			 * affect the current character
819 			 */
820 			tty_insert_flip_char(port, 0, TTY_OVERRUN);
821 ignore_char:
822 		icom_port->statStg->rcv[rcv_buff].flags = 0;
823 		icom_port->statStg->rcv[rcv_buff].leLength = 0;
824 		icom_port->statStg->rcv[rcv_buff].WorkingLength =
825 			(unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
826 
827 		rcv_buff++;
828 		if (rcv_buff == NUM_RBUFFS)
829 			rcv_buff = 0;
830 
831 		status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
832 	}
833 	icom_port->next_rcv = rcv_buff;
834 
835 	tty_flip_buffer_push(port);
836 }
837 
838 static void process_interrupt(u16 port_int_reg,
839 			      struct icom_port *icom_port)
840 {
841 
842 	spin_lock(&icom_port->uart_port.lock);
843 	trace(icom_port, "INTERRUPT", port_int_reg);
844 
845 	if (port_int_reg & (INT_XMIT_COMPLETED | INT_XMIT_DISABLED))
846 		xmit_interrupt(port_int_reg, icom_port);
847 
848 	if (port_int_reg & INT_RCV_COMPLETED)
849 		recv_interrupt(port_int_reg, icom_port);
850 
851 	spin_unlock(&icom_port->uart_port.lock);
852 }
853 
854 static irqreturn_t icom_interrupt(int irq, void *dev_id)
855 {
856 	void __iomem * int_reg;
857 	u32 adapter_interrupts;
858 	u16 port_int_reg;
859 	struct icom_adapter *icom_adapter;
860 	struct icom_port *icom_port;
861 
862 	/* find icom_port for this interrupt */
863 	icom_adapter = (struct icom_adapter *) dev_id;
864 
865 	if (icom_adapter->version == ADAPTER_V2) {
866 		int_reg = icom_adapter->base_addr + 0x8024;
867 
868 		adapter_interrupts = readl(int_reg);
869 
870 		if (adapter_interrupts & 0x00003FFF) {
871 			/* port 2 interrupt,  NOTE:  for all ADAPTER_V2, port 2 will be active */
872 			icom_port = &icom_adapter->port_info[2];
873 			port_int_reg = (u16) adapter_interrupts;
874 			process_interrupt(port_int_reg, icom_port);
875 			check_modem_status(icom_port);
876 		}
877 		if (adapter_interrupts & 0x3FFF0000) {
878 			/* port 3 interrupt */
879 			icom_port = &icom_adapter->port_info[3];
880 			if (icom_port->status == ICOM_PORT_ACTIVE) {
881 				port_int_reg =
882 				    (u16) (adapter_interrupts >> 16);
883 				process_interrupt(port_int_reg, icom_port);
884 				check_modem_status(icom_port);
885 			}
886 		}
887 
888 		/* Clear out any pending interrupts */
889 		writel(adapter_interrupts, int_reg);
890 
891 		int_reg = icom_adapter->base_addr + 0x8004;
892 	} else {
893 		int_reg = icom_adapter->base_addr + 0x4004;
894 	}
895 
896 	adapter_interrupts = readl(int_reg);
897 
898 	if (adapter_interrupts & 0x00003FFF) {
899 		/* port 0 interrupt, NOTE:  for all adapters, port 0 will be active */
900 		icom_port = &icom_adapter->port_info[0];
901 		port_int_reg = (u16) adapter_interrupts;
902 		process_interrupt(port_int_reg, icom_port);
903 		check_modem_status(icom_port);
904 	}
905 	if (adapter_interrupts & 0x3FFF0000) {
906 		/* port 1 interrupt */
907 		icom_port = &icom_adapter->port_info[1];
908 		if (icom_port->status == ICOM_PORT_ACTIVE) {
909 			port_int_reg = (u16) (adapter_interrupts >> 16);
910 			process_interrupt(port_int_reg, icom_port);
911 			check_modem_status(icom_port);
912 		}
913 	}
914 
915 	/* Clear out any pending interrupts */
916 	writel(adapter_interrupts, int_reg);
917 
918 	/* flush the write */
919 	adapter_interrupts = readl(int_reg);
920 
921 	return IRQ_HANDLED;
922 }
923 
924 /*
925  * ------------------------------------------------------------------
926  * Begin serial-core API
927  * ------------------------------------------------------------------
928  */
929 static unsigned int icom_tx_empty(struct uart_port *port)
930 {
931 	struct icom_port *icom_port = to_icom_port(port);
932 	int ret;
933 	unsigned long flags;
934 
935 	spin_lock_irqsave(&port->lock, flags);
936 	if (cpu_to_le16(icom_port->statStg->xmit[0].flags) &
937 	    SA_FLAGS_READY_TO_XMIT)
938 		ret = TIOCSER_TEMT;
939 	else
940 		ret = 0;
941 
942 	spin_unlock_irqrestore(&port->lock, flags);
943 	return ret;
944 }
945 
946 static void icom_set_mctrl(struct uart_port *port, unsigned int mctrl)
947 {
948 	struct icom_port *icom_port = to_icom_port(port);
949 	unsigned char local_osr;
950 
951 	trace(icom_port, "SET_MODEM", 0);
952 	local_osr = readb(&icom_port->dram->osr);
953 
954 	if (mctrl & TIOCM_RTS) {
955 		trace(icom_port, "RAISE_RTS", 0);
956 		local_osr |= ICOM_RTS;
957 	} else {
958 		trace(icom_port, "LOWER_RTS", 0);
959 		local_osr &= ~ICOM_RTS;
960 	}
961 
962 	if (mctrl & TIOCM_DTR) {
963 		trace(icom_port, "RAISE_DTR", 0);
964 		local_osr |= ICOM_DTR;
965 	} else {
966 		trace(icom_port, "LOWER_DTR", 0);
967 		local_osr &= ~ICOM_DTR;
968 	}
969 
970 	writeb(local_osr, &icom_port->dram->osr);
971 }
972 
973 static unsigned int icom_get_mctrl(struct uart_port *port)
974 {
975 	struct icom_port *icom_port = to_icom_port(port);
976 	unsigned char status;
977 	unsigned int result;
978 
979 	trace(icom_port, "GET_MODEM", 0);
980 
981 	status = readb(&icom_port->dram->isr);
982 
983 	result = ((status & ICOM_DCD) ? TIOCM_CAR : 0)
984 	    | ((status & ICOM_RI) ? TIOCM_RNG : 0)
985 	    | ((status & ICOM_DSR) ? TIOCM_DSR : 0)
986 	    | ((status & ICOM_CTS) ? TIOCM_CTS : 0);
987 	return result;
988 }
989 
990 static void icom_stop_tx(struct uart_port *port)
991 {
992 	struct icom_port *icom_port = to_icom_port(port);
993 	unsigned char cmdReg;
994 
995 	trace(icom_port, "STOP", 0);
996 	cmdReg = readb(&icom_port->dram->CmdReg);
997 	writeb(cmdReg | CMD_HOLD_XMIT, &icom_port->dram->CmdReg);
998 }
999 
1000 static void icom_start_tx(struct uart_port *port)
1001 {
1002 	struct icom_port *icom_port = to_icom_port(port);
1003 	unsigned char cmdReg;
1004 
1005 	trace(icom_port, "START", 0);
1006 	cmdReg = readb(&icom_port->dram->CmdReg);
1007 	if ((cmdReg & CMD_HOLD_XMIT) == CMD_HOLD_XMIT)
1008 		writeb(cmdReg & ~CMD_HOLD_XMIT,
1009 		       &icom_port->dram->CmdReg);
1010 
1011 	icom_write(port);
1012 }
1013 
1014 static void icom_send_xchar(struct uart_port *port, char ch)
1015 {
1016 	struct icom_port *icom_port = to_icom_port(port);
1017 	unsigned char xdata;
1018 	int index;
1019 	unsigned long flags;
1020 
1021 	trace(icom_port, "SEND_XCHAR", ch);
1022 
1023 	/* wait .1 sec to send char */
1024 	for (index = 0; index < 10; index++) {
1025 		spin_lock_irqsave(&port->lock, flags);
1026 		xdata = readb(&icom_port->dram->xchar);
1027 		if (xdata == 0x00) {
1028 			trace(icom_port, "QUICK_WRITE", 0);
1029 			writeb(ch, &icom_port->dram->xchar);
1030 
1031 			/* flush write operation */
1032 			xdata = readb(&icom_port->dram->xchar);
1033 			spin_unlock_irqrestore(&port->lock, flags);
1034 			break;
1035 		}
1036 		spin_unlock_irqrestore(&port->lock, flags);
1037 		msleep(10);
1038 	}
1039 }
1040 
1041 static void icom_stop_rx(struct uart_port *port)
1042 {
1043 	struct icom_port *icom_port = to_icom_port(port);
1044 	unsigned char cmdReg;
1045 
1046 	cmdReg = readb(&icom_port->dram->CmdReg);
1047 	writeb(cmdReg & ~CMD_RCV_ENABLE, &icom_port->dram->CmdReg);
1048 }
1049 
1050 static void icom_break(struct uart_port *port, int break_state)
1051 {
1052 	struct icom_port *icom_port = to_icom_port(port);
1053 	unsigned char cmdReg;
1054 	unsigned long flags;
1055 
1056 	spin_lock_irqsave(&port->lock, flags);
1057 	trace(icom_port, "BREAK", 0);
1058 	cmdReg = readb(&icom_port->dram->CmdReg);
1059 	if (break_state == -1) {
1060 		writeb(cmdReg | CMD_SND_BREAK, &icom_port->dram->CmdReg);
1061 	} else {
1062 		writeb(cmdReg & ~CMD_SND_BREAK, &icom_port->dram->CmdReg);
1063 	}
1064 	spin_unlock_irqrestore(&port->lock, flags);
1065 }
1066 
1067 static int icom_open(struct uart_port *port)
1068 {
1069 	struct icom_port *icom_port = to_icom_port(port);
1070 	int retval;
1071 
1072 	kref_get(&icom_port->adapter->kref);
1073 	retval = startup(icom_port);
1074 
1075 	if (retval) {
1076 		kref_put(&icom_port->adapter->kref, icom_kref_release);
1077 		trace(icom_port, "STARTUP_ERROR", 0);
1078 		return retval;
1079 	}
1080 
1081 	return 0;
1082 }
1083 
1084 static void icom_close(struct uart_port *port)
1085 {
1086 	struct icom_port *icom_port = to_icom_port(port);
1087 	unsigned char cmdReg;
1088 
1089 	trace(icom_port, "CLOSE", 0);
1090 
1091 	/* stop receiver */
1092 	cmdReg = readb(&icom_port->dram->CmdReg);
1093 	writeb(cmdReg & ~CMD_RCV_ENABLE, &icom_port->dram->CmdReg);
1094 
1095 	shutdown(icom_port);
1096 
1097 	kref_put(&icom_port->adapter->kref, icom_kref_release);
1098 }
1099 
1100 static void icom_set_termios(struct uart_port *port,
1101 			     struct ktermios *termios,
1102 			     struct ktermios *old_termios)
1103 {
1104 	struct icom_port *icom_port = to_icom_port(port);
1105 	int baud;
1106 	unsigned cflag, iflag;
1107 	char new_config2;
1108 	char new_config3 = 0;
1109 	char tmp_byte;
1110 	int index;
1111 	int rcv_buff, xmit_buff;
1112 	unsigned long offset;
1113 	unsigned long flags;
1114 
1115 	spin_lock_irqsave(&port->lock, flags);
1116 	trace(icom_port, "CHANGE_SPEED", 0);
1117 
1118 	cflag = termios->c_cflag;
1119 	iflag = termios->c_iflag;
1120 
1121 	new_config2 = ICOM_ACFG_DRIVE1;
1122 
1123 	/* byte size and parity */
1124 	switch (cflag & CSIZE) {
1125 	case CS5:		/* 5 bits/char */
1126 		new_config2 |= ICOM_ACFG_5BPC;
1127 		break;
1128 	case CS6:		/* 6 bits/char */
1129 		new_config2 |= ICOM_ACFG_6BPC;
1130 		break;
1131 	case CS7:		/* 7 bits/char */
1132 		new_config2 |= ICOM_ACFG_7BPC;
1133 		break;
1134 	case CS8:		/* 8 bits/char */
1135 		new_config2 |= ICOM_ACFG_8BPC;
1136 		break;
1137 	default:
1138 		break;
1139 	}
1140 	if (cflag & CSTOPB) {
1141 		/* 2 stop bits */
1142 		new_config2 |= ICOM_ACFG_2STOP_BIT;
1143 	}
1144 	if (cflag & PARENB) {
1145 		/* parity bit enabled */
1146 		new_config2 |= ICOM_ACFG_PARITY_ENAB;
1147 		trace(icom_port, "PARENB", 0);
1148 	}
1149 	if (cflag & PARODD) {
1150 		/* odd parity */
1151 		new_config2 |= ICOM_ACFG_PARITY_ODD;
1152 		trace(icom_port, "PARODD", 0);
1153 	}
1154 
1155 	/* Determine divisor based on baud rate */
1156 	baud = uart_get_baud_rate(port, termios, old_termios,
1157 				  icom_acfg_baud[0],
1158 				  icom_acfg_baud[BAUD_TABLE_LIMIT]);
1159 	if (!baud)
1160 		baud = 9600;	/* B0 transition handled in rs_set_termios */
1161 
1162 	for (index = 0; index < BAUD_TABLE_LIMIT; index++) {
1163 		if (icom_acfg_baud[index] == baud) {
1164 			new_config3 = index;
1165 			break;
1166 		}
1167 	}
1168 
1169 	uart_update_timeout(port, cflag, baud);
1170 
1171 	/* CTS flow control flag and modem status interrupts */
1172 	tmp_byte = readb(&(icom_port->dram->HDLCConfigReg));
1173 	if (cflag & CRTSCTS)
1174 		tmp_byte |= HDLC_HDW_FLOW;
1175 	else
1176 		tmp_byte &= ~HDLC_HDW_FLOW;
1177 	writeb(tmp_byte, &(icom_port->dram->HDLCConfigReg));
1178 
1179 	/*
1180 	 * Set up parity check flag
1181 	 */
1182 	icom_port->read_status_mask = SA_FLAGS_OVERRUN | SA_FL_RCV_DONE;
1183 	if (iflag & INPCK)
1184 		icom_port->read_status_mask |=
1185 		    SA_FLAGS_FRAME_ERROR | SA_FLAGS_PARITY_ERROR;
1186 
1187 	if ((iflag & BRKINT) || (iflag & PARMRK))
1188 		icom_port->read_status_mask |= SA_FLAGS_BREAK_DET;
1189 
1190 	/*
1191 	 * Characters to ignore
1192 	 */
1193 	icom_port->ignore_status_mask = 0;
1194 	if (iflag & IGNPAR)
1195 		icom_port->ignore_status_mask |=
1196 		    SA_FLAGS_PARITY_ERROR | SA_FLAGS_FRAME_ERROR;
1197 	if (iflag & IGNBRK) {
1198 		icom_port->ignore_status_mask |= SA_FLAGS_BREAK_DET;
1199 		/*
1200 		 * If we're ignore parity and break indicators, ignore
1201 		 * overruns too.  (For real raw support).
1202 		 */
1203 		if (iflag & IGNPAR)
1204 			icom_port->ignore_status_mask |= SA_FLAGS_OVERRUN;
1205 	}
1206 
1207 	/*
1208 	 * !!! ignore all characters if CREAD is not set
1209 	 */
1210 	if ((cflag & CREAD) == 0)
1211 		icom_port->ignore_status_mask |= SA_FL_RCV_DONE;
1212 
1213 	/* Turn off Receiver to prepare for reset */
1214 	writeb(CMD_RCV_DISABLE, &icom_port->dram->CmdReg);
1215 
1216 	for (index = 0; index < 10; index++) {
1217 		if (readb(&icom_port->dram->PrevCmdReg) == 0x00) {
1218 			break;
1219 		}
1220 	}
1221 
1222 	/* clear all current buffers of data */
1223 	for (rcv_buff = 0; rcv_buff < NUM_RBUFFS; rcv_buff++) {
1224 		icom_port->statStg->rcv[rcv_buff].flags = 0;
1225 		icom_port->statStg->rcv[rcv_buff].leLength = 0;
1226 		icom_port->statStg->rcv[rcv_buff].WorkingLength =
1227 		    (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
1228 	}
1229 
1230 	for (xmit_buff = 0; xmit_buff < NUM_XBUFFS; xmit_buff++) {
1231 		icom_port->statStg->xmit[xmit_buff].flags = 0;
1232 	}
1233 
1234 	/* activate changes and start xmit and receiver here */
1235 	/* Enable the receiver */
1236 	writeb(new_config3, &(icom_port->dram->async_config3));
1237 	writeb(new_config2, &(icom_port->dram->async_config2));
1238 	tmp_byte = readb(&(icom_port->dram->HDLCConfigReg));
1239 	tmp_byte |= HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL;
1240 	writeb(tmp_byte, &(icom_port->dram->HDLCConfigReg));
1241 	writeb(0x04, &(icom_port->dram->FlagFillIdleTimer));	/* 0.5 seconds */
1242 	writeb(0xFF, &(icom_port->dram->ier));	/* enable modem signal interrupts */
1243 
1244 	/* reset processor */
1245 	writeb(CMD_RESTART, &icom_port->dram->CmdReg);
1246 
1247 	for (index = 0; index < 10; index++) {
1248 		if (readb(&icom_port->dram->CmdReg) == 0x00) {
1249 			break;
1250 		}
1251 	}
1252 
1253 	/* Enable Transmitter and Receiver */
1254 	offset =
1255 	    (unsigned long) &icom_port->statStg->rcv[0] -
1256 	    (unsigned long) icom_port->statStg;
1257 	writel(icom_port->statStg_pci + offset,
1258 	       &icom_port->dram->RcvStatusAddr);
1259 	icom_port->next_rcv = 0;
1260 	icom_port->put_length = 0;
1261 	*icom_port->xmitRestart = 0;
1262 	writel(icom_port->xmitRestart_pci,
1263 	       &icom_port->dram->XmitStatusAddr);
1264 	trace(icom_port, "XR_ENAB", 0);
1265 	writeb(CMD_XMIT_RCV_ENABLE, &icom_port->dram->CmdReg);
1266 
1267 	spin_unlock_irqrestore(&port->lock, flags);
1268 }
1269 
1270 static const char *icom_type(struct uart_port *port)
1271 {
1272 	return "icom";
1273 }
1274 
1275 static void icom_release_port(struct uart_port *port)
1276 {
1277 }
1278 
1279 static int icom_request_port(struct uart_port *port)
1280 {
1281 	return 0;
1282 }
1283 
1284 static void icom_config_port(struct uart_port *port, int flags)
1285 {
1286 	port->type = PORT_ICOM;
1287 }
1288 
1289 static const struct uart_ops icom_ops = {
1290 	.tx_empty = icom_tx_empty,
1291 	.set_mctrl = icom_set_mctrl,
1292 	.get_mctrl = icom_get_mctrl,
1293 	.stop_tx = icom_stop_tx,
1294 	.start_tx = icom_start_tx,
1295 	.send_xchar = icom_send_xchar,
1296 	.stop_rx = icom_stop_rx,
1297 	.break_ctl = icom_break,
1298 	.startup = icom_open,
1299 	.shutdown = icom_close,
1300 	.set_termios = icom_set_termios,
1301 	.type = icom_type,
1302 	.release_port = icom_release_port,
1303 	.request_port = icom_request_port,
1304 	.config_port = icom_config_port,
1305 };
1306 
1307 #define ICOM_CONSOLE NULL
1308 
1309 static struct uart_driver icom_uart_driver = {
1310 	.owner = THIS_MODULE,
1311 	.driver_name = ICOM_DRIVER_NAME,
1312 	.dev_name = "ttyA",
1313 	.major = ICOM_MAJOR,
1314 	.minor = ICOM_MINOR_START,
1315 	.nr = NR_PORTS,
1316 	.cons = ICOM_CONSOLE,
1317 };
1318 
1319 static int icom_init_ports(struct icom_adapter *icom_adapter)
1320 {
1321 	u32 subsystem_id = icom_adapter->subsystem_id;
1322 	int i;
1323 	struct icom_port *icom_port;
1324 
1325 	if (icom_adapter->version == ADAPTER_V1) {
1326 		icom_adapter->numb_ports = 2;
1327 
1328 		for (i = 0; i < 2; i++) {
1329 			icom_port = &icom_adapter->port_info[i];
1330 			icom_port->port = i;
1331 			icom_port->status = ICOM_PORT_ACTIVE;
1332 			icom_port->imbed_modem = ICOM_UNKNOWN;
1333 		}
1334 	} else {
1335 		if (subsystem_id == PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL) {
1336 			icom_adapter->numb_ports = 4;
1337 
1338 			for (i = 0; i < 4; i++) {
1339 				icom_port = &icom_adapter->port_info[i];
1340 
1341 				icom_port->port = i;
1342 				icom_port->status = ICOM_PORT_ACTIVE;
1343 				icom_port->imbed_modem = ICOM_IMBED_MODEM;
1344 			}
1345 		} else {
1346 			icom_adapter->numb_ports = 4;
1347 
1348 			icom_adapter->port_info[0].port = 0;
1349 			icom_adapter->port_info[0].status = ICOM_PORT_ACTIVE;
1350 
1351 			if (subsystem_id ==
1352 			    PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM) {
1353 				icom_adapter->port_info[0].imbed_modem = ICOM_IMBED_MODEM;
1354 			} else {
1355 				icom_adapter->port_info[0].imbed_modem = ICOM_RVX;
1356 			}
1357 
1358 			icom_adapter->port_info[1].status = ICOM_PORT_OFF;
1359 
1360 			icom_adapter->port_info[2].port = 2;
1361 			icom_adapter->port_info[2].status = ICOM_PORT_ACTIVE;
1362 			icom_adapter->port_info[2].imbed_modem = ICOM_RVX;
1363 			icom_adapter->port_info[3].status = ICOM_PORT_OFF;
1364 		}
1365 	}
1366 
1367 	return 0;
1368 }
1369 
1370 static void icom_port_active(struct icom_port *icom_port, struct icom_adapter *icom_adapter, int port_num)
1371 {
1372 	if (icom_adapter->version == ADAPTER_V1) {
1373 		icom_port->global_reg = icom_adapter->base_addr + 0x4000;
1374 		icom_port->int_reg = icom_adapter->base_addr +
1375 		    0x4004 + 2 - 2 * port_num;
1376 	} else {
1377 		icom_port->global_reg = icom_adapter->base_addr + 0x8000;
1378 		if (icom_port->port < 2)
1379 			icom_port->int_reg = icom_adapter->base_addr +
1380 			    0x8004 + 2 - 2 * icom_port->port;
1381 		else
1382 			icom_port->int_reg = icom_adapter->base_addr +
1383 			    0x8024 + 2 - 2 * (icom_port->port - 2);
1384 	}
1385 }
1386 static int icom_load_ports(struct icom_adapter *icom_adapter)
1387 {
1388 	struct icom_port *icom_port;
1389 	int port_num;
1390 
1391 	for (port_num = 0; port_num < icom_adapter->numb_ports; port_num++) {
1392 
1393 		icom_port = &icom_adapter->port_info[port_num];
1394 
1395 		if (icom_port->status == ICOM_PORT_ACTIVE) {
1396 			icom_port_active(icom_port, icom_adapter, port_num);
1397 			icom_port->dram = icom_adapter->base_addr +
1398 					0x2000 * icom_port->port;
1399 
1400 			icom_port->adapter = icom_adapter;
1401 
1402 			/* get port memory */
1403 			if (get_port_memory(icom_port) != 0) {
1404 				dev_err(&icom_port->adapter->pci_dev->dev,
1405 					"Memory allocation for port FAILED\n");
1406 			}
1407 		}
1408 	}
1409 	return 0;
1410 }
1411 
1412 static int icom_alloc_adapter(struct icom_adapter
1413 					**icom_adapter_ref)
1414 {
1415 	int adapter_count = 0;
1416 	struct icom_adapter *icom_adapter;
1417 	struct icom_adapter *cur_adapter_entry;
1418 	struct list_head *tmp;
1419 
1420 	icom_adapter = kzalloc(sizeof(struct icom_adapter), GFP_KERNEL);
1421 
1422 	if (!icom_adapter) {
1423 		return -ENOMEM;
1424 	}
1425 
1426 	list_for_each(tmp, &icom_adapter_head) {
1427 		cur_adapter_entry =
1428 		    list_entry(tmp, struct icom_adapter,
1429 			       icom_adapter_entry);
1430 		if (cur_adapter_entry->index != adapter_count) {
1431 			break;
1432 		}
1433 		adapter_count++;
1434 	}
1435 
1436 	icom_adapter->index = adapter_count;
1437 	list_add_tail(&icom_adapter->icom_adapter_entry, tmp);
1438 
1439 	*icom_adapter_ref = icom_adapter;
1440 	return 0;
1441 }
1442 
1443 static void icom_free_adapter(struct icom_adapter *icom_adapter)
1444 {
1445 	list_del(&icom_adapter->icom_adapter_entry);
1446 	kfree(icom_adapter);
1447 }
1448 
1449 static void icom_kref_release(struct kref *kref)
1450 {
1451 	struct icom_adapter *icom_adapter = container_of(kref,
1452 			struct icom_adapter, kref);
1453 	struct icom_port *icom_port;
1454 	int index;
1455 
1456 	for (index = 0; index < icom_adapter->numb_ports; index++) {
1457 		icom_port = &icom_adapter->port_info[index];
1458 
1459 		if (icom_port->status == ICOM_PORT_ACTIVE) {
1460 			dev_info(&icom_adapter->pci_dev->dev,
1461 				 "Device removed\n");
1462 
1463 			uart_remove_one_port(&icom_uart_driver,
1464 					     &icom_port->uart_port);
1465 
1466 			/* be sure that DTR and RTS are dropped */
1467 			writeb(0x00, &icom_port->dram->osr);
1468 
1469 			/* Wait 0.1 Sec for simple Init to complete */
1470 			msleep(100);
1471 
1472 			/* Stop proccessor */
1473 			stop_processor(icom_port);
1474 
1475 			free_port_memory(icom_port);
1476 		}
1477 	}
1478 
1479 	free_irq(icom_adapter->pci_dev->irq, (void *) icom_adapter);
1480 	iounmap(icom_adapter->base_addr);
1481 	pci_release_regions(icom_adapter->pci_dev);
1482 	icom_free_adapter(icom_adapter);
1483 }
1484 
1485 static int icom_probe(struct pci_dev *dev,
1486 				const struct pci_device_id *ent)
1487 {
1488 	int index;
1489 	unsigned int command_reg;
1490 	int retval;
1491 	struct icom_adapter *icom_adapter;
1492 	struct icom_port *icom_port;
1493 
1494 	retval = pci_enable_device(dev);
1495 	if (retval) {
1496 		dev_err(&dev->dev, "Device enable FAILED\n");
1497 		return retval;
1498 	}
1499 
1500 	retval = pci_request_regions(dev, "icom");
1501 	if (retval) {
1502 		 dev_err(&dev->dev, "pci_request_regions FAILED\n");
1503 		 pci_disable_device(dev);
1504 		 return retval;
1505 	 }
1506 
1507 	pci_set_master(dev);
1508 
1509 	retval = pci_read_config_dword(dev, PCI_COMMAND, &command_reg);
1510 	if (retval) {
1511 		dev_err(&dev->dev, "PCI Config read FAILED\n");
1512 		goto probe_exit0;
1513 	}
1514 
1515 	pci_write_config_dword(dev, PCI_COMMAND,
1516 		command_reg | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1517  		| PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1518 
1519 	if (ent->driver_data == ADAPTER_V1) {
1520 		pci_write_config_dword(dev, 0x44, 0x8300830A);
1521 	} else {
1522 		pci_write_config_dword(dev, 0x44, 0x42004200);
1523 		pci_write_config_dword(dev, 0x48, 0x42004200);
1524 	}
1525 
1526 
1527 	retval = icom_alloc_adapter(&icom_adapter);
1528 	if (retval) {
1529 		 dev_err(&dev->dev, "icom_alloc_adapter FAILED\n");
1530 		 retval = -EIO;
1531 		 goto probe_exit0;
1532 	}
1533 
1534 	icom_adapter->base_addr_pci = pci_resource_start(dev, 0);
1535 	icom_adapter->pci_dev = dev;
1536 	icom_adapter->version = ent->driver_data;
1537 	icom_adapter->subsystem_id = ent->subdevice;
1538 
1539 
1540 	retval = icom_init_ports(icom_adapter);
1541 	if (retval) {
1542 		dev_err(&dev->dev, "Port configuration failed\n");
1543 		goto probe_exit1;
1544 	}
1545 
1546 	icom_adapter->base_addr = pci_ioremap_bar(dev, 0);
1547 
1548 	if (!icom_adapter->base_addr) {
1549 		retval = -ENOMEM;
1550 		goto probe_exit1;
1551 	}
1552 
1553 	 /* save off irq and request irq line */
1554 	 retval = request_irq(dev->irq, icom_interrupt, IRQF_SHARED, ICOM_DRIVER_NAME, (void *)icom_adapter);
1555 	 if (retval) {
1556 		  goto probe_exit2;
1557 	 }
1558 
1559 	retval = icom_load_ports(icom_adapter);
1560 
1561 	for (index = 0; index < icom_adapter->numb_ports; index++) {
1562 		icom_port = &icom_adapter->port_info[index];
1563 
1564 		if (icom_port->status == ICOM_PORT_ACTIVE) {
1565 			icom_port->uart_port.irq = icom_port->adapter->pci_dev->irq;
1566 			icom_port->uart_port.type = PORT_ICOM;
1567 			icom_port->uart_port.iotype = UPIO_MEM;
1568 			icom_port->uart_port.membase =
1569 				(unsigned char __iomem *)icom_adapter->base_addr_pci;
1570 			icom_port->uart_port.fifosize = 16;
1571 			icom_port->uart_port.ops = &icom_ops;
1572 			icom_port->uart_port.line =
1573 		        icom_port->port + icom_adapter->index * 4;
1574 			if (uart_add_one_port (&icom_uart_driver, &icom_port->uart_port)) {
1575 				icom_port->status = ICOM_PORT_OFF;
1576 				dev_err(&dev->dev, "Device add failed\n");
1577 			 } else
1578 				dev_info(&dev->dev, "Device added\n");
1579 		}
1580 	}
1581 
1582 	kref_init(&icom_adapter->kref);
1583 	return 0;
1584 
1585 probe_exit2:
1586 	iounmap(icom_adapter->base_addr);
1587 probe_exit1:
1588 	icom_free_adapter(icom_adapter);
1589 
1590 probe_exit0:
1591 	pci_release_regions(dev);
1592 	pci_disable_device(dev);
1593 
1594 	return retval;
1595 }
1596 
1597 static void icom_remove(struct pci_dev *dev)
1598 {
1599 	struct icom_adapter *icom_adapter;
1600 	struct list_head *tmp;
1601 
1602 	list_for_each(tmp, &icom_adapter_head) {
1603 		icom_adapter = list_entry(tmp, struct icom_adapter,
1604 					  icom_adapter_entry);
1605 		if (icom_adapter->pci_dev == dev) {
1606 			kref_put(&icom_adapter->kref, icom_kref_release);
1607 			return;
1608 		}
1609 	}
1610 
1611 	dev_err(&dev->dev, "Unable to find device to remove\n");
1612 }
1613 
1614 static struct pci_driver icom_pci_driver = {
1615 	.name = ICOM_DRIVER_NAME,
1616 	.id_table = icom_pci_table,
1617 	.probe = icom_probe,
1618 	.remove = icom_remove,
1619 };
1620 
1621 static int __init icom_init(void)
1622 {
1623 	int ret;
1624 
1625 	ret = uart_register_driver(&icom_uart_driver);
1626 	if (ret)
1627 		return ret;
1628 
1629 	ret = pci_register_driver(&icom_pci_driver);
1630 
1631 	if (ret < 0)
1632 		uart_unregister_driver(&icom_uart_driver);
1633 
1634 	return ret;
1635 }
1636 
1637 static void __exit icom_exit(void)
1638 {
1639 	pci_unregister_driver(&icom_pci_driver);
1640 	uart_unregister_driver(&icom_uart_driver);
1641 }
1642 
1643 module_init(icom_init);
1644 module_exit(icom_exit);
1645 
1646 MODULE_AUTHOR("Michael Anderson <mjanders@us.ibm.com>");
1647 MODULE_DESCRIPTION("IBM iSeries Serial IOA driver");
1648 MODULE_LICENSE("GPL");
1649 MODULE_FIRMWARE("icom_call_setup.bin");
1650 MODULE_FIRMWARE("icom_res_dce.bin");
1651 MODULE_FIRMWARE("icom_asc.bin");
1652