/*---------------------------------------------------------------------------
* Copyright (C) 2007, Morninghan Electronics, Inc. All Rights Reserved.
* THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
* AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
*--------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
//
// File: cspiutil.c
//
// This file contains the utility function for accessing perpherials via CSPI.
// The code is currently used for accessing LCD. We assume 9 bit access.
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <Devload.h>
#include <ceddk.h>
#include "bsp.h"
#include "cspiutil.h"
//-----------------------------------------------------------------------------
// External Functions
static BOOL CSPIGetSysIntr(int index, DWORD *cspiSysIntr);
static BOOL CSPIMapIoSpace(int index);
static void CSPIWriteTXFIFO(unsigned int data);
static UINT32 CSPIReadRXFIFO(void);
static void CSPIExchange(void);
static void CSPIWaitTransactionComplete(void);
static void CSPIWaitReadReady(void);
static void CSPIEnable(void);
static void CSPIDisable(void);
static void CSPIRXIRQEnable(void);
static void CSPIIRQDisable(void);
static UINT32 CSPIGetFreqIn(void);
static BOOL CSPISetClockGating(BOOL bEnable);
static BOOL CSPIInit(int index);
//-----------------------------------------------------------------------------
// Defines
#define CSPI_READ 0
#define CSPI_WRITE 1
#define CSPI_TIMEOUT 1000
//-----------------------------------------------------------------------------
// Types
#ifdef DEBUG
// Debug zone bit positions
#define ZONEID_ERROR 0
#define ZONEID_WARN 1
#define ZONEID_INIT 2
#define ZONEID_FUNC 3
#define ZONEID_INFO 4
// Debug zone masks
#define ZONEMASK_ERROR (1 << ZONEID_ERROR)
#define ZONEMASK_WARN (1 << ZONEID_WARN)
#define ZONEMASK_INIT (1 << ZONEID_INIT)
#define ZONEMASK_FUNC (1 << ZONEID_FUNC)
#define ZONEMASK_INFO (1 << ZONEID_INFO)
// Debug zone args to DEBUGMSG
#define ZONE_ERROR DEBUGZONE(ZONEID_ERROR)
#define ZONE_WARN DEBUGZONE(ZONEID_WARN)
#define ZONE_INIT DEBUGZONE(ZONEID_INIT)
#define ZONE_FUNC DEBUGZONE(ZONEID_FUNC)
#define ZONE_INFO DEBUGZONE(ZONEID_INFO)
#endif
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// Local Variables
static HANDLE g_hCspiIntrEvent;
static HANDLE g_hCspiWaitEvent;
static HANDLE g_hCspiIntrServThread;
static DWORD g_cspiSysIntr;
static BOOL g_bUsePolling = FALSE;
static volatile BOOL g_bSync;
static HANDLE g_hCspiMutex;
static PCSP_CSPI_REGS g_pCSPI;
static CRITICAL_SECTION cs;
//-----------------------------------------------------------------------------
// Local Functions
static DWORD WaitForInterruptEvent(HANDLE hIntrEvent, DWORD dwMilliseconds);
static DWORD WINAPI CspiIntrServThread (LPVOID lpParam);
//-----------------------------------------------------------------------------
//
// Function: cspiInitialize
//
// Initializes the CSPI interface and data structures.
//
// Parameters:
// index
// [in] CSPI instance (1 = CSPI1, 2 = CSPI2) to initialize.
//
// dwFrequency
// [in] Frequency requested.
//
// Returns:
// TRUE is CSPI is initialized successfully.
//
//-----------------------------------------------------------------------------
BOOL cspiInitialize(int index, UINT32 dwFrequency)
{
InitializeCriticalSection(&cs);
if (!CSPIGetSysIntr(index, &g_cspiSysIntr))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: Failed to obtain SYSINTR value.\r\n")));
goto Error;
}
if (!CSPIMapIoSpace(index))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: MmMapIoSpace failed for CSP_CSPI_REG.\r\n")));
goto Error;
}
if (!CSPIInit(index))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: CSPIInit failed for CSP_CSPI_REG.\r\n")));
goto Error;
}
// Create event for CSPI interrupt signaling
g_hCspiIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (g_hCspiIntrEvent == NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: CreateEvent failed for g_hCspiIntrEvent.\r\n")));
goto Error;
}
// Register CSPI interrupt
if (!InterruptInitialize(g_cspiSysIntr, g_hCspiIntrEvent, NULL, 0))
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: InterruptInitialize failed.\r\n")));
goto Error;
}
// Create event for CSPI thread wait signaling
g_hCspiWaitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (g_hCspiWaitEvent == NULL)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: CreateEvent failed for g_hCspiWaitEvent.\r\n")));
goto Error;
}
g_hCspiMutex = CreateMutex(NULL, FALSE, L"MUTEX_PMIC");
// Create IST for CSPI interrupts
g_hCspiIntrServThread = CreateThread(NULL, 0, CspiIntrServThread, NULL, 0, NULL);
if (!g_hCspiIntrServThread)
{
DEBUGMSG(ZONE_ERROR,
(TEXT("cspiInitialize: CreateThread failed for CSPI IST\r\n")));
goto Error;
}
// Initialize global for sync with threads blocked on CSPI transfer
g_bSync = FALSE;
// Configure for interrupt-driven communication
cspiConfigPolling(g_bUsePolling);
return TRUE;
Error:
return FALSE;
}
//-----------------------------------------------------------------------------
//
// Function: BSPPmicInitCspi
//
// Initializes the platform-specific configuration for the CSPI port.
//
// Parameters:
// index
// [in] CSPI instance (1 = CSPI1, 2 = CSPI2) to initialize
//
// Returns:
// TRUE if successful, FALSE otherwise.
//
//-----------------------------------------------------------------------------
static BOOL CSPIInit(int index)
{
UINT32 reg = 0;
// Turn on CSPI clocks
CSPISetClockGating(TRUE);
SETREG32(&g_pCSPI->RESET, 0x1);
// Configure the CSPI interface and enable it before writing to
// other CSPI registers
OUTREG32(&g_pCSPI->CONTROLREG,
CSP_BITFVAL(CSPI_CONTROLREG_SPIEN, CSPI_CONTROLREG_SPIEN_ENABLE) |
CSP_BITFVAL(CSPI_CONTROLREG_MODE, CSPI_CONTROLREG_MODE_MASTER) |
CSP_BITFVAL(CSPI_CONTROLREG_XCH, CSPI_CONTROLREG_XCH_IDLE) |
CSP_BITFVAL(CSPI_CONTROLREG_POL, CSPI_CONTROLREG_POL_ACTIVE_LOW) |
CSP_BITFVAL(CSPI_CONTROLREG_PHA, CSPI_CONTROLREG_PHA1) |
CSP_BITFVAL(CSPI_CONTROLREG_SSCTL, CSPI_CONTROLREG_SSCTL_PULSE) |
CSP_BITFVAL(CSPI_CONTROLREG_SSPOL, CSPI_CONTROLREG_SSPOL_ACTIVE_LOW) |
CSP_BITFVAL(CSPI_CONTROLREG_BITCOUNT, CSPI_CONTROLREG_BITCOUNT_24BIT) |
CSP_BITFVAL(CSPI_CONTROLREG_DATARATE, CSPI_CONTROLREG_DATARATE_DIV32) |
CSP_BITFVAL(CSPI_CONTROLREG_DRCTL, CSPI_CONTROLREG_DRCTL_DONTCARE) |
CSP_BITFVAL(CSPI_CONTROLREG_CS, CSPI_CONTROLREG_CS_SS0));
OUTREG32(&g_pCSPI->INT, 0);
OUTREG32(&g_pCSPI->DMA, 0);
OUTREG32(&g_pCSPI->TEST, 0);
CSPISetClockGating(FALSE);
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: cspiRelease
//
// Frees allocated memory, closes reference to handles, and resets the state
// of global member variables.
//
// Parameters:
// index
// [in]
评论0