Nut/OS  4.10.3
API Reference
spi_mmc_npl.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 by egnite Software GmbH
00003  * Copyright (C) 2010 by egnite GmbH
00004  *
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  */
00035 
00049 #include <cfg/clock.h>
00050 #include <cfg/mmci.h>
00051 
00052 #include <dev/npl.h>
00053 #include <dev/mmcard.h>
00054 #include <dev/spibus.h>
00055 
00056 #if !defined(NPL_MMC_CLOCK) || (NPL_MMC_CLOCK < 1000)
00057 #undef NPL_MMC_CLOCK
00058 #define NPL_MMC_CLOCK   12500000
00059 #endif
00060 
00061 #if 0
00062 /* Use for local debugging. */
00063 #define NUTDEBUG
00064 #include <stdio.h>
00065 #endif
00066 
00071 
00072 static int NplMmc0Reset(NUTDEVICE * dev);
00073 static void NplMmc0Led(int mode);
00074 static int NplMmc0Power(int mode);
00075 
00079 static MEMCARDSUPP mcsSpiMmc0Npl = {
00080     0,              
00081     0,              
00082     NplMmc0Reset,   
00083     NplMmc0Led,     
00084     NplMmc0Power,   
00085 };
00086 
00092 static void NplMmc0Insertion(void *arg)
00093 {
00094     MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
00095 
00096     /* Set the change flag. */
00097     mcs->mcs_cf = 1;
00098     /* Set the card detect flag. */
00099     mcs->mcs_sf = NUTMC_SF_CD;
00100     /* Set the card write protect flag. */
00101     if (inw(NPL_SLR) & NPL_LANWAKEUP) {
00102         mcs->mcs_sf |= NUTMC_SF_WP;
00103     }
00104 
00105     /* Disable insert and enable remove interrupts. */
00106     NplIrqDisable(&sig_MMCD);
00107     NplIrqEnable(&sig_NMMCD);
00108 }
00109 
00115 static void NplMmcRemoval(void *arg)
00116 {
00117     MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
00118 
00119     /* Set the change flag. */
00120     mcs->mcs_cf = 1;
00121     /* Clear card detect and write protect flags. */
00122     mcs->mcs_sf = 0;
00123 
00124     /* Disable remove and enable insert interrupts. */
00125     NplIrqDisable(&sig_NMMCD);
00126     NplIrqEnable(&sig_MMCD);
00127 }
00128 
00134 static void NplMmc0Led(int mode)
00135 {
00136 #ifdef NPL_MMC0_ULED
00137     if (mode == NUTMC_IND_OFF) {
00138         outb(NPL_XER, inb(NPL_XER) | NPL_USRLED);
00139     } else {
00140         outb(NPL_XER, inb(NPL_XER) & ~NPL_USRLED);
00141     }
00142 #endif
00143 }
00144 
00155 static int NplMmc0Power(int mode)
00156 {
00157     return -1;
00158 }
00159 
00165 static int NplMmc0Reset(NUTDEVICE * dev)
00166 {
00167     uint_fast8_t i;
00168 
00169     /* Deactivate negated chip select. */
00170     outb(NPL_XER, inb(NPL_XER) | NPL_MMCS);
00171     /* Perform 80 clock cycles. */
00172     for (i = 0; i < 10; i++) {
00173         outb(NPL_MMCDR, 0xFF);
00174         while ((inb(NPL_SLR) & NPL_MMCREADY) == 0);
00175         inb(NPL_MMCDR);
00176     }
00177     /* Switch LED off. */
00178     NplMmc0Led(NUTMC_IND_OFF);
00179 
00180     return 0;
00181 }
00182 
00191 static int SpiMmc0NplInit(NUTDEVICE * dev)
00192 {
00193     int rc;
00194 
00195     NplMmc0Reset(dev);
00196 
00197     /* Register card detection interrupts. */
00198     rc = NplRegisterIrqHandler(&sig_MMCD, NplMmc0Insertion, dev->dev_dcb);
00199     if (rc == 0) {
00200         rc = NplRegisterIrqHandler(&sig_NMMCD, NplMmcRemoval, dev->dev_dcb);
00201         if (rc == 0) {
00202             NplIrqEnable(&sig_MMCD);
00203             /* Initialize the SPI interface. */
00204             rc = SpiMmcInit(dev);
00205         }
00206     }
00207     return rc;
00208 }
00209 
00213 NUTSPINODE nodeSpiMmc0Npl = {
00214     NULL,           
00215     NULL,           
00216     NPL_MMC_CLOCK,  
00217     SPI_MODE_0,     
00218     8,              
00219     0               
00220 };
00221 
00234 NUTDEVICE devNplSpiMmc0 = {
00235     0,                  
00236     {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
00237     ,                   
00238     0,                  
00239     0,                  
00240     0,                  
00241     &nodeSpiMmc0Npl,    
00242     &mcsSpiMmc0Npl,     
00243     SpiMmc0NplInit,     
00244     SpiMmcIOCtl,        
00245     SpiMmcBlockRead,    
00246     SpiMmcBlockWrite,   
00247 #ifdef __HARVARD_ARCH__
00248     SpiMmcBlockWrite_P, 
00249 #endif
00250     SpiMmcMount,        
00251     SpiMmcUnmount,      
00252     0                   
00253 };
00254