icom.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,703 行 · 第 1/3 页
C
1,703 行
/* * icom.c * * Copyright (C) 2001 IBM Corporation. All rights reserved. * * Serial device driver. * * Based on code from serial.c * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#define SERIAL_DO_RESTART#include <linux/module.h>#include <linux/config.h>#include <linux/version.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/tty.h>#include <linux/termios.h>#include <linux/fs.h>#include <linux/tty_flip.h>#include <linux/serial.h>#include <linux/serial_reg.h>#include <linux/major.h>#include <linux/string.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/ioport.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/pci.h>#include <linux/vmalloc.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <linux/spinlock.h>#include <linux/kobject.h>#include <linux/firmware.h>#include <asm/system.h>#include <asm/segment.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/uaccess.h>#include <asm/bitops.h>#include "icom.h"/*#define ICOM_TRACE enable port trace capabilities */#define ICOM_DRIVER_NAME "icom"#define ICOM_VERSION_STR "1.3.1"#define NR_PORTS 128#define ICOM_PORT ((struct icom_port *)port)#define to_icom_adapter(d) container_of(d, struct icom_adapter, kobj)static const struct pci_device_id icom_pci_table[] = { { .vendor = PCI_VENDOR_ID_IBM, .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_1, .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, .driver_data = ADAPTER_V1, }, { .vendor = PCI_VENDOR_ID_IBM, .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2, .subvendor = PCI_VENDOR_ID_IBM, .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_TWO_PORTS_RVX, .driver_data = ADAPTER_V2, }, { .vendor = PCI_VENDOR_ID_IBM, .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2, .subvendor = PCI_VENDOR_ID_IBM, .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM, .driver_data = ADAPTER_V2, }, { .vendor = PCI_VENDOR_ID_IBM, .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2, .subvendor = PCI_VENDOR_ID_IBM, .subdevice = PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL, .driver_data = ADAPTER_V2, }, {}};struct lookup_proc_table start_proc[4] = { {0, ICOM_CONTROL_START_A}, {0, ICOM_CONTROL_START_B}, {0, ICOM_CONTROL_START_C}, {0, ICOM_CONTROL_START_D}};struct lookup_proc_table stop_proc[4] = { {0, ICOM_CONTROL_STOP_A}, {0, ICOM_CONTROL_STOP_B}, {0, ICOM_CONTROL_STOP_C}, {0, ICOM_CONTROL_STOP_D}};struct lookup_int_table int_mask_tbl[4] = { {0, ICOM_INT_MASK_PRC_A}, {0, ICOM_INT_MASK_PRC_B}, {0, ICOM_INT_MASK_PRC_C}, {0, ICOM_INT_MASK_PRC_D},};MODULE_DEVICE_TABLE(pci, icom_pci_table);static LIST_HEAD(icom_adapter_head);/* spinlock for adapter initialization and changing adapter operations */static spinlock_t icom_lock;#ifdef ICOM_TRACEstatic inline void trace(struct icom_port *, char *, unsigned long) {};#elsestatic inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};#endifstatic void msleep(unsigned long msecs){ set_current_state(TASK_UNINTERRUPTIBLE); schedule_timeout(MSECS_TO_JIFFIES(msecs));}static void free_port_memory(struct icom_port *icom_port){ struct pci_dev *dev = icom_port->adapter->pci_dev; trace(icom_port, "RET_PORT_MEM", 0); if (icom_port->recv_buf) { pci_free_consistent(dev, 4096, icom_port->recv_buf, icom_port->recv_buf_pci); icom_port->recv_buf = 0; } if (icom_port->xmit_buf) { pci_free_consistent(dev, 4096, icom_port->xmit_buf, icom_port->xmit_buf_pci); icom_port->xmit_buf = 0; } if (icom_port->statStg) { pci_free_consistent(dev, 4096, icom_port->statStg, icom_port->statStg_pci); icom_port->statStg = 0; } if (icom_port->xmitRestart) { pci_free_consistent(dev, 4096, icom_port->xmitRestart, icom_port->xmitRestart_pci); icom_port->xmitRestart = 0; }}static int __init get_port_memory(struct icom_port *icom_port){ int index; unsigned long stgAddr; unsigned long startStgAddr; unsigned long offset; struct pci_dev *dev = icom_port->adapter->pci_dev; icom_port->xmit_buf = pci_alloc_consistent(dev, 4096, &icom_port->xmit_buf_pci); if (!icom_port->xmit_buf) { dev_err(&dev->dev, "Can not allocate Transmit buffer\n"); return -ENOMEM; } trace(icom_port, "GET_PORT_MEM", (unsigned long) icom_port->xmit_buf); icom_port->recv_buf = pci_alloc_consistent(dev, 4096, &icom_port->recv_buf_pci); if (!icom_port->recv_buf) { dev_err(&dev->dev, "Can not allocate Receive buffer\n"); free_port_memory(icom_port); return -ENOMEM; } trace(icom_port, "GET_PORT_MEM", (unsigned long) icom_port->recv_buf); icom_port->statStg = pci_alloc_consistent(dev, 4096, &icom_port->statStg_pci); if (!icom_port->statStg) { dev_err(&dev->dev, "Can not allocate Status buffer\n"); free_port_memory(icom_port); return -ENOMEM; } trace(icom_port, "GET_PORT_MEM", (unsigned long) icom_port->statStg); icom_port->xmitRestart = pci_alloc_consistent(dev, 4096, &icom_port->xmitRestart_pci); if (!icom_port->xmitRestart) { dev_err(&dev->dev, "Can not allocate xmit Restart buffer\n"); free_port_memory(icom_port); return -ENOMEM; } memset(icom_port->statStg, 0, 4096); /* FODs: Frame Out Descriptor Queue, this is a FIFO queue that indicates that frames are to be transmitted */ stgAddr = (unsigned long) icom_port->statStg; for (index = 0; index < NUM_XBUFFS; index++) { trace(icom_port, "FOD_ADDR", stgAddr); stgAddr = stgAddr + sizeof(icom_port->statStg->xmit[0]); if (index < (NUM_XBUFFS - 1)) { memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area)); icom_port->statStg->xmit[index].leLengthASD = (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ); trace(icom_port, "FOD_ADDR", stgAddr); trace(icom_port, "FOD_XBUFF", (unsigned long) icom_port->xmit_buf); icom_port->statStg->xmit[index].leBuffer = cpu_to_le32(icom_port->xmit_buf_pci); } else if (index == (NUM_XBUFFS - 1)) { memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area)); icom_port->statStg->xmit[index].leLengthASD = (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ); trace(icom_port, "FOD_XBUFF", (unsigned long) icom_port->xmit_buf); icom_port->statStg->xmit[index].leBuffer = cpu_to_le32(icom_port->xmit_buf_pci); } else { memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area)); } } /* FIDs */ startStgAddr = stgAddr; /* fill in every entry, even if no buffer */ for (index = 0; index < NUM_RBUFFS; index++) { trace(icom_port, "FID_ADDR", stgAddr); stgAddr = stgAddr + sizeof(icom_port->statStg->rcv[0]); icom_port->statStg->rcv[index].leLength = 0; icom_port->statStg->rcv[index].WorkingLength = (unsigned short int) cpu_to_le16(RCV_BUFF_SZ); if (index < (NUM_RBUFFS - 1) ) { offset = stgAddr - (unsigned long) icom_port->statStg; icom_port->statStg->rcv[index].leNext = cpu_to_le32(icom_port-> statStg_pci + offset); trace(icom_port, "FID_RBUFF", (unsigned long) icom_port->recv_buf); icom_port->statStg->rcv[index].leBuffer = cpu_to_le32(icom_port->recv_buf_pci); } else if (index == (NUM_RBUFFS -1) ) { offset = startStgAddr - (unsigned long) icom_port->statStg; icom_port->statStg->rcv[index].leNext = cpu_to_le32(icom_port-> statStg_pci + offset); trace(icom_port, "FID_RBUFF", (unsigned long) icom_port->recv_buf + 2048); icom_port->statStg->rcv[index].leBuffer = cpu_to_le32(icom_port->recv_buf_pci + 2048); } else { icom_port->statStg->rcv[index].leNext = 0; icom_port->statStg->rcv[index].leBuffer = 0; } } return 0;}static void stop_processor(struct icom_port *icom_port){ unsigned long temp; unsigned long flags; int port; spin_lock_irqsave(&icom_lock, flags); port = icom_port->port; if (port == 0 || port == 1) stop_proc[port].global_control_reg = &icom_port->global_reg->control; else stop_proc[port].global_control_reg = &icom_port->global_reg->control_2; if (port < 4) { temp = readl(stop_proc[port].global_control_reg); temp = (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id; writel(temp, stop_proc[port].global_control_reg); /* write flush */ readl(stop_proc[port].global_control_reg); } else { dev_err(&icom_port->adapter->pci_dev->dev, "Invalid port assignment\n"); } spin_unlock_irqrestore(&icom_lock, flags);}static void start_processor(struct icom_port *icom_port){ unsigned long temp; unsigned long flags; int port; spin_lock_irqsave(&icom_lock, flags); port = icom_port->port; if (port == 0 || port == 1) start_proc[port].global_control_reg = &icom_port->global_reg->control; else start_proc[port].global_control_reg = &icom_port->global_reg->control_2; if (port < 4) { temp = readl(start_proc[port].global_control_reg); temp = (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id; writel(temp, start_proc[port].global_control_reg); /* write flush */ readl(start_proc[port].global_control_reg); } else { dev_err(&icom_port->adapter->pci_dev->dev, "Invalid port assignment\n"); } spin_unlock_irqrestore(&icom_lock, flags);}static void load_code(struct icom_port *icom_port){ const struct firmware *fw; char *iram_ptr; int index; int status = 0; char *dram_ptr = (char *) icom_port->dram; dma_addr_t temp_pci; unsigned char *new_page = NULL; unsigned char cable_id = NO_CABLE; struct pci_dev *dev = icom_port->adapter->pci_dev; /* Clear out any pending interrupts */ writew(0x3FFF, (void *) icom_port->int_reg); trace(icom_port, "CLEAR_INTERRUPTS", 0); /* Stop processor */ stop_processor(icom_port); /* Zero out DRAM */ memset_io(dram_ptr, 0, 512); /* Load Call Setup into Adapter */ if (request_firmware(&fw, "icom_call_setup.bin", &dev->dev) < 0) { dev_err(&dev->dev,"Unable to load icom_call_setup.bin firmware image\n"); status = -1; goto load_code_exit; } if (fw->size > ICOM_DCE_IRAM_OFFSET) { dev_err(&dev->dev, "Invalid firmware image for icom_call_setup.bin found.\n"); release_firmware(fw); status = -1; goto load_code_exit; } iram_ptr = (char *) icom_port->dram + ICOM_IRAM_OFFSET; for (index = 0; index < fw->size; index++) writeb(fw->data[index], &iram_ptr[index]); release_firmware(fw); /* Load Resident DCE portion of Adapter */ if (request_firmware(&fw, "icom_res_dce.bin", &dev->dev) < 0) { dev_err(&dev->dev,"Unable to load icom_res_dce.bin firmware image\n"); status = -1; goto load_code_exit; } if (fw->size > ICOM_IRAM_SIZE) { dev_err(&dev->dev, "Invalid firmware image for icom_res_dce.bin found.\n"); release_firmware(fw); status = -1; goto load_code_exit; } iram_ptr = (char *) icom_port->dram + ICOM_IRAM_OFFSET; for (index = ICOM_DCE_IRAM_OFFSET; index < fw->size; index++) writeb(fw->data[index], &iram_ptr[index]); release_firmware(fw); /* Set Hardware level */ if ((icom_port->adapter->version | ADAPTER_V2) == ADAPTER_V2) writeb(V2_HARDWARE, &(icom_port->dram->misc_flags)); /* Start the processor in Adapter */ start_processor(icom_port); writeb((HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL), &(icom_port->dram->HDLCConfigReg)); writeb(0x04, &(icom_port->dram->FlagFillIdleTimer)); /* 0.5 seconds */ writeb(0x00, &(icom_port->dram->CmdReg)); writeb(0x10, &(icom_port->dram->async_config3)); writeb((ICOM_ACFG_DRIVE1 | ICOM_ACFG_NO_PARITY | ICOM_ACFG_8BPC | ICOM_ACFG_1STOP_BIT), &(icom_port->dram->async_config2)); /*Set up data in icom DRAM to indicate where personality *code is located and its length. */ new_page = pci_alloc_consistent(dev, 4096, &temp_pci); if (!new_page) { dev_err(&dev->dev, "Can not allocate DMA buffer\n"); status = -1; goto load_code_exit; } if (request_firmware(&fw, "icom_asc.bin", &dev->dev) < 0) { dev_err(&dev->dev,"Unable to load icom_asc.bin firmware image\n"); status = -1; goto load_code_exit; } if (fw->size > ICOM_DCE_IRAM_OFFSET) { dev_err(&dev->dev, "Invalid firmware image for icom_asc.bin found.\n"); release_firmware(fw); status = -1; goto load_code_exit; } for (index = 0; index < fw->size; index++) new_page[index] = fw->data[index]; release_firmware(fw); writeb((char) ((fw->size + 16)/16), &icom_port->dram->mac_length); writel(temp_pci, &icom_port->dram->mac_load_addr); /*Setting the syncReg to 0x80 causes adapter to start downloading the personality code into adapter instruction RAM. Once code is loaded, it will begin executing and, based on information provided above, will start DMAing data from shared memory to adapter DRAM. */ /* the wait loop below verifies this write operation has been done and processed */ writeb(START_DOWNLOAD, &icom_port->dram->sync); /* Wait max 1 Sec for data download and processor to start */ for (index = 0; index < 10; index++) { msleep(100); if (readb(&icom_port->dram->misc_flags) & ICOM_HDW_ACTIVE) break; } if (index == 10) status = -1; /* * check Cable ID */ cable_id = readb(&icom_port->dram->cable_id); if (cable_id & ICOM_CABLE_ID_VALID) { /* Get cable ID into the lower 4 bits (standard form) */ cable_id = (cable_id & ICOM_CABLE_ID_MASK) >> 4; icom_port->cable_id = cable_id; } else { dev_err(&dev->dev,"Invalid or no cable attached\n"); icom_port->cable_id = NO_CABLE; } load_code_exit: if (status != 0) { /* Clear out any pending interrupts */ writew(0x3FFF, (void *) icom_port->int_reg); /* Turn off port */ writeb(ICOM_DISABLE, &(icom_port->dram->disable)); /* Stop processor */ stop_processor(icom_port); dev_err(&icom_port->adapter->pci_dev->dev,"Port not opertional\n"); } if (new_page != NULL) pci_free_consistent(dev, 4096, new_page, temp_pci);}static int startup(struct icom_port *icom_port){ unsigned long temp; unsigned char cable_id, raw_cable_id; unsigned long flags; int port; trace(icom_port, "STARTUP", 0); if (icom_port->dram == 0x00000000) { /* should NEVER be zero */ dev_err(&icom_port->adapter->pci_dev->dev, "Unusable Port, port configuration missing\n"); return -ENODEV; } /* * check Cable ID */ raw_cable_id = readb(&icom_port->dram->cable_id); trace(icom_port, "CABLE_ID", raw_cable_id); /* Get cable ID into the lower 4 bits (standard form) */ cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4; /* Check for valid Cable ID */ if (!(raw_cable_id & ICOM_CABLE_ID_VALID) || (cable_id != icom_port->cable_id)) { /* reload adapter code, pick up any potential changes in cable id */ load_code(icom_port); /* still no sign of cable, error out */ raw_cable_id = readb(&icom_port->dram->cable_id); cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4; if (!(raw_cable_id & ICOM_CABLE_ID_VALID) || (icom_port->cable_id == NO_CABLE)) return -EIO; } /* * Finally, clear and enable interrupts */ spin_lock_irqsave(&icom_lock, flags); port = icom_port->port; if (port == 0 || port == 1) int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask; else int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2; if (port == 0 || port == 2) writew(0x00FF,(void *) icom_port->int_reg); else writew(0x3F00,(void *) icom_port->int_reg); if (port < 4) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?