📄 ssp.c
字号:
/* * FILE: ssp.c * * DESCRIPTION: SSP Interface Driver Module implementation * * Copyright Cirrus Logic Corporation, 2001-2003. All rights reserved * * 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 *//* * This driver provides a way to read and write devices on the SSP * interface. * * For Tx devices, EGPIO7 is used as an address pin: * I2S Codec CS4228 = EGPIO7 == 1 * Serial Flash AT25F1024 = EGPIO7 == 0 */#include <linux/delay.h>#include <asm/irq.h>#include <asm/semaphore.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/arch/hardware.h>#include <asm/arch/ssp.h>#undef DEBUG// #define DEBUG 1#ifdef DEBUG#define DPRINTK( x... ) printk( ##x )#else#define DPRINTK( x... )#endif#define EP93XX_KEY_TIMER_PERIOD_MSEC 20static int SSP_Open(SSPDeviceType Device, SSPDataCallback Callback);static int SSP_Close(int Handle);static int SSP_Read(int Handle, unsigned int Addr, unsigned int *pValue);static int SSP_Write(int Handle, unsigned int Addr, unsigned int Value);static int CheckHandle(int Handle);static void SetSSPtoPS2(void);static void SetSSPtoI2S(void);static void SetSSPtoFLASH(void);static int ReadIntoBuffer(void);static int SSP_Write_I2SCodec(int Handle, unsigned int RegAddr,unsigned int RegValue);/* * Key buffer... */#define KEYBUF_SIZE 256static unsigned int uiKeyBuffer[KEYBUF_SIZE];static spinlock_t ssp_spinlock = SPIN_LOCK_UNLOCKED;typedef enum{ SSP_MODE_UNKNOWN = 0, SSP_MODE_PS2, SSP_MODE_I2S, SSP_MODE_FLASH,} SSPmodes_t;static struct timer_list g_KbdTimer;static SSPmodes_t gSSPmode = SSP_MODE_UNKNOWN;static SSPDataCallback gKeyCallback = 0;static int gHookedInterrupt = 0;/* * Keep the last valid handle for SSP for kbd, i2s, and flash */static int iLastValidHandle = -1;static int KeyboardHandle = 0;static int I2SHandle = 0;static int FlashHandle = 0;#define SSP_DEVICE_MASK 0xf0000000#define SSP_DEVICE_SHIFT 28SSP_DRIVER_API SSPinstance ={ SSP_Open, SSP_Read, SSP_Write, SSP_Close,};/* * The only instance of this driver. */SSP_DRIVER_API *SSPDriver = &SSPinstance;//=============================================================================// SSPIrqHandler//=============================================================================// This routine will get all of the keys out of the SPI FIFO.//=============================================================================void SSPIrqHandler( int irq, void *dev_id, struct pt_regs *regs){ // // Get key codes from SSP and send them to the keyboard callback. // ReadIntoBuffer(); // // Clear the interrupt. // outl( 0, SSPIIR );}//=============================================================================// TimerRoutine//=============================================================================// This function is called periodically to make sure that no keys are stuck in// the SPI FIFO. This is necessary because the SPI only interrupts on half// full FIFO which can leave up to one keyboard event in the FIFO until another// key is pressed.//=============================================================================static void TimerRoutine(unsigned long Data){ int keycount; // // Get key codes from SSP and send them to the keyboard callback. // keycount = ReadIntoBuffer(); // // If no keys were received, call the Data callback anyway so it can // check for stuck keys. // if( (keycount==0) && gKeyCallback ) { gKeyCallback(-1); } // // Reschedule our timer in another 20 mSec. // g_KbdTimer.expires = jiffies + MSECS_TO_JIFFIES( EP93XX_KEY_TIMER_PERIOD_MSEC ); add_timer(&g_KbdTimer);}/* * HookInterrupt * * Requests SSP interrupt, sets up interrupt handler, sets up keyboard polling * timer. */static int HookInterrupt(void){ if (gHookedInterrupt) { printk( KERN_ERR "SSP driver interrupt already hooked\n"); return(-1); } if (request_irq(IRQ_SSPRX, SSPIrqHandler, SA_INTERRUPT, "ep93xxsspd", 0)) { printk( KERN_ERR "SSP driver failed to get IRQ handler\n"); return(-1); } gHookedInterrupt = 1; // // Initialize the timer that we will use to poll the SPI. // init_timer(&g_KbdTimer); g_KbdTimer.function = TimerRoutine; g_KbdTimer.data = 1; g_KbdTimer.expires = jiffies + MSECS_TO_JIFFIES( EP93XX_KEY_TIMER_PERIOD_MSEC ); add_timer(&g_KbdTimer); return(0);}static int SSP_Open(SSPDeviceType Device, SSPDataCallback Callback){ int Handle; /* * Generate a handle and pass it back. * * Increment the last valid handle. * Check for wraparound (unlikely, but we like to be complete). */ iLastValidHandle++; if((iLastValidHandle & ~SSP_DEVICE_MASK) == 0) { /* * If we wrapped around start over. Unlikely. */ iLastValidHandle = 1; } Handle = iLastValidHandle | (Device << SSP_DEVICE_SHIFT); switch (Device) { case PS2_KEYBOARD: { DPRINTK("SSP_Open - PS2_KEYBOARD\n"); if (KeyboardHandle) { return(-1); } else { DPRINTK("Handle:%08x Callback:%08x -- Success\n", Handle, (unsigned int)Callback); KeyboardHandle = Handle; // // Hook the interrupt if we have not yet. // HookInterrupt(); SetSSPtoPS2(); gKeyCallback = Callback; } break; } case I2S_CODEC: { DPRINTK("SSP_Open - I2S_CODEC\n"); if (I2SHandle) { return(-1); } else { DPRINTK("Handle:%08x Callback:%08x -- Success\n", Handle, (unsigned int)Callback); I2SHandle = Handle; } break; } case SERIAL_FLASH: { DPRINTK("SSP_Open - SERIAL_FLASH\n"); if (FlashHandle) { return(-1); } else { DPRINTK("Handle:%08x Callback:%08x -- Success\n", Handle, (unsigned int)Callback); FlashHandle = Handle; } break; } default: { return(-1); } } /* * Return the handle. */ return(Handle );}/* * Release that Handle! */static int SSP_Close(int Handle){ // // Find out which device this API was called for. // switch( CheckHandle(Handle) ) { case PS2_KEYBOARD: { DPRINTK("SSP_Open - PS2_KEYBOARD\n"); del_timer(&g_KbdTimer); free_irq(IRQ_SSPRX, 0); gKeyCallback = 0; KeyboardHandle = 0; gHookedInterrupt = 0; break; } case I2S_CODEC: { DPRINTK("SSP_Open - I2S_CODEC\n"); I2SHandle = 0; break; } case SERIAL_FLASH: { DPRINTK("SSP_Open - SERIAL_FLASH\n"); FlashHandle = 0; break; } default: { return(-1); } } return 0;}static int SSP_Read_FLASH( int Handle, unsigned int RegAddr, unsigned int *pValue){ SSPmodes_t saved_mode; DPRINTK("SSP_Read_FLASH\n"); spin_lock(&ssp_spinlock); /* * Save the SSP mode. Switch to FLASH mode if we're not * already in FLASH mode. */ saved_mode = gSSPmode; SetSSPtoFLASH(); /* * Let TX fifo clear out. Poll the Transmit Fifo Empty bit. */ while( !( inl(SSPSR) & SSPSR_TFE ) ) barrier(); /* * Write the SPI read command. */ outl( 0x03, SSPDR ); outl( (RegAddr >> 16) & 255, SSPDR ); outl( (RegAddr >> 8) & 255, SSPDR ); outl( RegAddr & 255, SSPDR ); /* * Delay long enough for one byte to be transmitted. It takes 7.6uS to * write a single byte. */ udelay(10); /* * Read a byte to make sure the FIFO doesn't overrun. */ while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); inl( SSPDR ); /* * Write four more bytes so that we can read four bytes. */ outl( 0, SSPDR ); outl( 0, SSPDR ); outl( 0, SSPDR ); outl( 0, SSPDR ); /* * Delay long enough for three bytes to be transmitted. It takes 7.6uS * to write a single byte. */ udelay(25); /* * Read three and throw away the next tree bytes. */ while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); inl( SSPDR ); while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); inl( SSPDR ); while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); inl( SSPDR ); /* * Delay long enough for four bytes to be transmitted. It takes 7.6uS * to write a single byte. */ udelay(30); /* * Read the data word. */ while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); *pValue = inl( SSPDR ); while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); *pValue |= inl( SSPDR ) << 8; while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); *pValue |= inl( SSPDR ) << 16; while( !( inl(SSPSR) & SSPSR_RNE ) ) barrier(); *pValue |= inl( SSPDR ) << 24; /* * Wait until the transmit buffer is empty (it should be...). */ while( !( inl(SSPSR) & SSPSR_TFE ) ) barrier(); /* * Read any residual bytes in the receive buffer. */ while( inl(SSPSR) & SSPSR_RNE ) inl( SSPDR ); /* * If we were in PS2 mode, switch back to PS2 mode. * If we weren't in PS2 mode, that means we didn't compile in * the PS2 keyboard support, so no need to switch to PS2 mode. */ if( saved_mode == SSP_MODE_PS2 ) SetSSPtoPS2(); spin_unlock(&ssp_spinlock); /* * Return success. */ return 0;}static int SSP_Read(int Handle, unsigned int Addr, unsigned int *pValue){ DPRINTK("SSP_Read\n"); /* * Find out which device this API was called for. */ switch( CheckHandle(Handle) ) { case SERIAL_FLASH: { return SSP_Read_FLASH(Handle, Addr, pValue); } default: { return -1; } }}static int SSP_Write(int Handle, unsigned int Addr, unsigned int Value){ int iRet = 0; // DPRINTK("SSP_Write - Handle:0x%08x Addr:0x%08x Value:0x%08x\n", // Handle, Addr, Value ); // // Find out which device this API was called for. // switch( CheckHandle(Handle) ) { case PS2_KEYBOARD: { break; } case I2S_CODEC: {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -