⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hc_sl811.c

📁 基于linux嵌入式操作系统,sl811芯片的usb驱动源代码以及例子和Makefile.
💻 C
📖 第 1 页 / 共 3 页
字号:
/*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------* * SL811HS USB HCD for Linux Version 0.1 (10/28/2001) *  * requires (includes) hc_simple.[hc] simple generic HCD frontend *   * COPYRIGHT(C) 2001 by CYPRESS SEMICONDUCTOR INC. * *-------------------------------------------------------------------------* * 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 * *-------------------------------------------------------------------------*/#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/sched.h>#include <linux/malloc.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/smp_lock.h>#include <linux/list.h>#include <linux/ioport.h>#include <asm/io.h>#include <asm/arch/irq.h>#include <linux/usb.h>#undef HC_URB_TIMEOUT#undef HC_SWITCH_INT#undef HC_ENABLE_ISOC#define SL811_DEBUG_ERR//#ifdef SL811_DEBUG_ERR//#define DBGERR(fmt, args...) printk(fmt,## args)//#else#define DBGERR(fmt, args...)//#endif//#ifdef SL811_DEBUG//#define DBG(fmt, args...) printk(fmt,## args)//#else#define DBG(fmt, args...)//#endif//#ifdef SL811_DEBUG_FUNC//#define DBGFUNC(fmt, args...) printk(fmt,## args)//#else#define DBGFUNC(fmt, args...)//#endif//#ifdef SL811_DEBUG_DATA//#define DBGDATAR(fmt, args...) printk(fmt,## args)//#define DBGDATAW(fmt, args...) printk(fmt,## args)//#else#define DBGDATAR(fmt, args...)#define DBGDATAW(fmt, args...)//#endif//#ifdef SL811_DEBUG_VERBOSE//#define DBGVERBOSE(fmt, args...) printk(fmt,## args)//#else#define DBGVERBOSE(fmt, args...)//#endif#define TRUE 1#define FALSE 0#define HC_SWITCH_INT#include "hc_sl811.h"#include "hc_simple.h"static int urb_debug = 0; #include "hc_simple.c"#include "hc_sl811_rh.c"#define MX1_PORTD_VA_BASE       0xf021c300#define _reg_PORTD_DDIR         (*((volatile U32 *)(MX1_PORTD_VA_BASE)))    #define _reg_PORTD_GIUS         (*((volatile U32 *)(MX1_PORTD_VA_BASE + 0x20))) #define _reg_PORTD_ICR1         (*((volatile U32 *)(MX1_PORTD_VA_BASE + 0x28))) #define _reg_PORTD_IMR          (*((volatile U32 *)(MX1_PORTD_VA_BASE + 0x30))) #define _reg_PORTD_ISR          (*((volatile U32 *)(MX1_PORTD_VA_BASE + 0x34))) #define _reg_PORTD_PUEN         (*((volatile U32 *)(MX1_PORTD_VA_BASE + 0x40))) #define _reg_INTENNUM     (*((volatile U32 *)(0xf0223008)))#define _reg_INTENABLEH      (*((volatile U32 *)(0xf0223010)))#define U32 unsigned long/* The base_addr, data_reg_addr, and irq number are board specific. * The current values are design to run on the Accelent SA1110 IDP * NOTE: values need to modify for different development boards  */static int base_addr = 0xf4000000;static int data_reg_addr = 0xf4000001;static int irq = 62;/* forware declaration */int SL11StartXaction(hci_t *hci, __u8 addr, __u8 epaddr, int pid, int len,                      int toggle, int slow, int urb_state);static int sofWaitCnt =0;MODULE_PARM(urb_debug,"i");MODULE_PARM_DESC(urb_debug,"debug urb messages, default is 0 (no)");MODULE_PARM(base_addr,"i");MODULE_PARM_DESC(base_addr,"sl811 base address 0xd3800000");MODULE_PARM(data_reg_addr,"i");MODULE_PARM_DESC(data_reg_addr,"sl811 data register address 0xd3810000");MODULE_PARM(irq,"i");MODULE_PARM_DESC(irq,"IRQ 34 (default)");static int hc_reset (hci_t * hci);/*******************************************************************/void  set_interrupt(void) {  _reg_PORTD_GIUS |= 0x00000040;  // select GPIO - 6th pin   _reg_PORTD_DDIR &= 0xFFFFFFbF;  // config as input     _reg_PORTD_IMR |= 0x00000040;   // enable interrupt   _reg_PORTD_ICR1 &= 0xFFFFeFFF;  // config as positive edge trigger    _reg_PORTD_ICR1 |= 0x2000;_reg_PORTD_PUEN |= 0xFFFFFFBF;  // pull up  enable interrupt   _reg_INTENNUM=62;  _reg_INTENABLEH |=1<<30;}   void    Clear_interrupt(void) {   _reg_PORTD_ISR = 0x00000040;         } /*************************************************************************** * Function Name : SL811Read * * Read a byte of data from the SL811H/SL11H * * Input:  hci = data structure for the host controller *         offset = address of SL811/SL11H register or memory * * Return: data  **************************************************************************/char SL811Read(hci_t *hci, char offset){     char data;   //  writeb(offset, base_addr);   //wmb();   // data = readb (data_reg_addr);   //rmb();  *((volatile unsigned char *)0xf4000000)=offset;  wmb();    data= *((volatile unsigned char *)0xf4000001);    rmb();   return (data);}/*************************************************************************** * Function Name : SL811Write * * Write a byte of data to the SL811H/SL11H * * Input:  hci = data structure for the host controller *         offset = address of SL811/SL11H register or memory *         data  = the data going to write to SL811H * * Return: none  **************************************************************************/void SL811Write(hci_t *hci, char offset, char data){   //  writeb(offset, base_addr);  // writeb(data, data_reg_addr);  // wmb();  *((volatile unsigned char *)0xf4000000)=offset;  *((volatile unsigned char *)0xf4000001)=data;  wmb();}/*************************************************************************** * Function Name : SL811BufRead * * Read consecutive bytes of data from the SL811H/SL11H buffer * * Input:  hci = data structure for the host controller *         offset = SL811/SL11H register offset *         buf = the buffer where the data will store *         size = number of bytes to read * * Return: none  **************************************************************************/void SL811BufRead(hci_t *hci, short offset, char *buf, short size){	   hcipriv_t *hp = &hci->hp;    if( size <= 0)        return;   //  writeb((char) offset, hp->hcport);   *((volatile unsigned char *)0xf4000000)=(char)offset;   wmb();   DBGDATAR ("SL811BufRead: offset = 0x%x, data = ", offset);   while (size--)   {     // *buf++ = (char)readb(hp->hcport2);     *buf++=*((volatile unsigned char *)0xf4000001);       DBGDATAR ("0x%x ", *(buf-1));       rmb();    }   DBGDATAR ("\n");}/*************************************************************************** * Function Name : SL811BufWrite * * Write consecutive bytes of data to the SL811H/SL11H buffer * * Input:  hci = data structure for the host controller *         offset = SL811/SL11H register offset *         buf = the data buffer  *         size = number of bytes to write * * Return: none  **************************************************************************/void SL811BufWrite(hci_t *hci, short offset, char *buf, short size){	   hcipriv_t *hp = &hci->hp;    if(size<=0)        return;   // writeb((char) offset, hp->hcport);   *((volatile unsigned char*)0xf4000000)=(char)offset;     wmb();   DBGDATAW ("SL811BufWrite: offset = 0x%x, data = ", offset);   while (size--)    {       DBGDATAW ("0x%x ", *buf);       // writeb(*buf, hp->hcport2);       *((volatile unsigned char *)0xf4000001)=*buf;       wmb();          buf++;   }   DBGDATAW ("\n");}/*************************************************************************** * Function Name : regTest * * This routine test the Read/Write functionality of SL811HS registers   * * 1) Store original register value into a buffer * 2) Write to registers with a RAMP pattern. (10, 11, 12, ..., 255) * 3) Read from register * 4) Compare the written value with the read value and make sure they are  *    equivalent * 5) Restore the original register value  * * Input:  hci = data structure for the host controller *    * * Return: TRUE = passed; FALSE = failed  **************************************************************************/int regTest(hci_t *hci){   int i, data, result = TRUE;   char buf[256];      DBGFUNC ("Enter regTest\n");   for (i=0x10; i < 256; i++)   {       /* save the original buffer */       buf[i] = (char) SL811Read(hci, i);        /* Write the new data to the buffer */       SL811Write(hci, i, i);   }   /* compare the written data */   for (i=0x10; i < 256; i++)   {       data = SL811Read(hci, i);       if (data != i)       {	   DBGERR ("Pattern test failed!! value = 0x%x, s/b 0x%x\n",                     data, i);           result = FALSE;	       }   }   /* restore the data */   for (i=0x10; i < 256; i++)   {       SL811Write(hci, i, buf[i]);   }      return (result);}/*************************************************************************** * Function Name : regShow * * Display all SL811HS register values * * Input:  hci = data structure for the host controller * * Return: none  **************************************************************************/void regShow(hci_t *hci){   int i;   for (i = 0; i<256; i++)   {       printk ("offset %d: 0x%x\n", i, SL811Read(hci, i));   }} /************************************************************************ * Function Name : USBReset *   * This function resets SL811HS controller and detects the speed of * the connecting device				   * * Input:  hci = data structure for the host controller *                 * Return: 0 = no device attached; 1 = USB device attached *                 ***********************************************************************/static int USBReset (hci_t *hci){    int status ;    hcipriv_t *hp = &hci->hp;     DBGFUNC ("enter USBReset\n");		    SL811Write(hci, SL11H_CTLREG2, 0xae);    // setup master and full speed     SL811Write(hci, SL11H_CTLREG1, 0x08 );	// reset USB    mdelay(20);                             // 20ms				    SL811Write(hci, SL11H_CTLREG1, 0);     // remove SE0	    for (status=0; status < 100; status++)        SL811Write (hci, SL11H_INTSTATREG, 0xff); // clear all interrupt bits    status = SL811Read (hci, SL11H_INTSTATREG);    if (status & 0x40)  // Check if device is removed    {        DBG ("USBReset: Device removed\n");	SL811Write(hci, SL11H_INTENBLREG, SL11H_INTMASK_XFERDONE                    | SL11H_INTMASK_SOFINTR|SL11H_INTMASK_INSRMV);         hp->RHportStatus->portStatus &= ~(PORT_CONNECT_STAT | PORT_ENABLE_STAT);                		return 0;    }	    SL811Write (hci, SL11H_BUFLNTHREG_B, 0 ) ;	//zero lenth    SL811Write (hci, SL11H_PIDEPREG_B, 0x50 ) ;	//send SOF to EP0	    SL811Write (hci, SL11H_DEVADDRREG_B, 0x01 ) ;	//address0    SL811Write (hci, SL11H_SOFLOWREG, 0xe0 ) ;    if (!(status & 0x80 ))    {            /* slow speed device connect directly to root-hub */                  DBG ("USBReset: low speed Device attached\n");	 SL811Write (hci, SL11H_CTLREG1, 0x8);         mdelay(20);	 SL811Write (hci, SL11H_SOFTMRREG, 0xee );  	 SL811Write (hci, SL11H_CTLREG1, 0x21 );                  /* start the SOF or EOP */	          SL811Write (hci, SL11H_HOSTCTLREG_B, 0x01 ) ;	         hp->RHportStatus->portStatus |= (PORT_CONNECT_STAT              | PORT_LOW_SPEED_DEV_ATTACH_STAT);                  /* clear all interrupt bits */                  for (status=0; status < 20; status++)             SL811Write (hci, SL11H_INTSTATREG, 0xff);      }    else    {	        /* full speed device connect directly to root hub */        DBG ("USBReset: full speed Device attached\n");	SL811Write (hci, SL11H_CTLREG1, 0x8);        mdelay(20);	SL811Write (hci, SL11H_SOFTMRREG, 0xae ) ;        SL811Write (hci, SL11H_CTLREG1, 0x01 ) ;         /* start the SOF or EOP */                SL811Write (hci, SL11H_HOSTCTLREG_B, 0x01 ) ;	        hp->RHportStatus->portStatus |= (PORT_CONNECT_STAT);        hp->RHportStatus->portStatus &= ~PORT_LOW_SPEED_DEV_ATTACH_STAT;                 /* clear all interrupt bits */        SL811Write (hci, SL11H_INTSTATREG, 0xff);      }        /* enable all interrupts */	    SL811Write(hci, SL11H_INTENBLREG, SL11H_INTMASK_XFERDONE               | SL11H_INTMASK_SOFINTR|SL11H_INTMASK_INSRMV);    return 1;}/*-------------------------------------------------------------------------*//* tl functions */static inline void hc_mark_last_trans (hci_t * hci) {	hcipriv_t * hp = &hci->hp; 	__u8 * ptd = hp->tl;	        dbg ("enter hc_mark_last_trans\n");        if (ptd == NULL)        {		printk ("hc_mark_last_trans: ptd = null\n"); 		return;        }	if (hp->xferPktLen > 0)		*(ptd + hp->tl_last) |= (1 << 3);}static inline void hc_flush_data_cache (hci_t * hci, void * data, int len) {}/************************************************************************ * Function Name : hc_add_trans *   * This function sets up the SL811HS register and transmit the USB packets. *  * 1) Determine if enough time within the current frame to send the packet * 2) Load the data into the SL811HS register * 3) Set the appropriate command to the register and trigger the transmit * * Input:  hci = data structure for the host controller *         len = data length *         data = transmitting data *         toggle = USB toggle bit, either 0 or 1 *         maxps = maximum packet size for this endpoint *         slow = speed of the device *         endpoint = endpoint number *         address = USB address of the device *         pid = packet ID *         format =  *         urb_state = the current stage of USB transaction *        * Return: 0 = no time left to schedule the transfer *         1 = success  *                 ***********************************************************************/static inline int hc_add_trans (hci_t * hci, int len, void * data,		int toggle, int maxps, int slow, int endpoint, int address, int pid, int format, int urb_state){    hcipriv_t * hp = &hci->hp;     __u16 speed;    int ii, jj, kk;	    //    DBGFUNC ("enter hc_addr_trans: len =0x%x, toggle:0x%x, endpoing:0x%x,"     //       " addr:0x%x, pid:0x%x,format:0x%x\n", len, toggle, endpoint, i    //       address, pid, format);	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -