Nut/OS  4.10.3
API Reference
spimmc_at91.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00069 #include <cfg/arch.h>
00070 #include <cfg/arch/gpio.h>
00071 
00072 #include <dev/mmcard.h>
00073 #include <dev/spimmc_at91.h>
00074 
00075 #if 0
00076 /* Use for local debugging. */
00077 #define NUTDEBUG
00078 #include <stdio.h>
00079 #endif
00080 
00085 
00086 #if defined(MCU_AT91SAM9G45)
00087 
00088 #ifndef MMC_CS_BIT
00089 #define MMC_CS_BIT      PB3_SPI0_NPCS0_A
00090 #endif
00091 #define MMC_DATAOUT_BIT PB0_SPI0_MISO_A
00092 #define MMC_DATAIN_BIT  PB1_SPI0_MOSI_A
00093 #define MMC_CLK_BIT     PB2_SPI0_SPCK_A
00094 
00095 #define MMC_PIO_PDR     PIOB_PDR
00096 
00097 #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
00098 
00099 #ifndef MMC_CS_BIT
00100 #define MMC_CS_BIT      PA3_SPI0_NPCS0_A
00101 #endif
00102 #define MMC_DATAOUT_BIT PA0_SPI0_MISO_A
00103 #define MMC_DATAIN_BIT  PA1_SPI0_MOSI_A
00104 #define MMC_CLK_BIT     PA2_SPI0_SPCK_A
00105 
00106 #elif defined(MCU_AT91SAM7X)
00107 
00108 #ifndef MMC_CS_BIT
00109 #define MMC_CS_BIT      SPI0_NPCS1_PA13A
00110 #endif
00111 #define MMC_DATAOUT_BIT SPI0_MISO_PA16A
00112 #define MMC_DATAIN_BIT  SPI0_MOSI_PA17A
00113 #define MMC_CLK_BIT     SPI0_SPCK_PA18A
00114 
00115 //#elif defined(MCU_AT91SAM7S256) || defined(MCU_AT91SAM7SE512)
00116 #elif defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
00117 
00118 #ifndef MMC_CS_BIT
00119 #define MMC_CS_BIT      SPI0_NPCS0_PA11A
00120 #endif
00121 #define MMC_DATAOUT_BIT SPI0_MISO_PA12A
00122 #define MMC_DATAIN_BIT  SPI0_MOSI_PA13A
00123 #define MMC_CLK_BIT     SPI0_SPCK_PA14A
00124 
00125 #else   /* MCU_AT91SAM7X */
00126 #warning "MMC SPI mode not supported on this MCU"
00127 #endif
00128 
00129 #ifndef MMC_PIO_ASR
00130 #define MMC_PIO_ASR     PIOA_ASR
00131 #endif
00132 
00133 #ifndef MMC_PIO_BSR
00134 #define MMC_PIO_BSR     PIOA_BSR
00135 #endif
00136 
00137 #ifndef MMC_PIO_PDR
00138 #define MMC_PIO_PDR     PIOA_PDR
00139 #endif
00140 
00141 #ifndef MMC_PINS_A
00142 #define MMC_PINS_A      (_BV(MMC_DATAOUT_BIT) | _BV(MMC_DATAIN_BIT) | _BV(MMC_CLK_BIT))
00143 #endif
00144 
00145 #ifndef MMC_PINS_B
00146 #define MMC_PINS_B      0
00147 #endif
00148 
00149 #ifndef MMC_CS_PER
00150 #define MMC_CS_PER      PIOA_PER
00151 #endif
00152 
00153 #ifndef MMC_CS_OER
00154 #define MMC_CS_OER      PIOA_OER
00155 #endif
00156 
00157 #ifndef MMC_CS_SODR
00158 #define MMC_CS_SODR     PIOA_SODR
00159 #endif
00160 
00161 #ifndef MMC_CS_CODR
00162 #define MMC_CS_CODR     PIOA_CODR
00163 #endif
00164 
00165 #ifndef MMC_SPI_CR
00166 #define MMC_SPI_CR      SPI0_CR
00167 #endif
00168 
00169 #ifndef MMC_SPI_MR
00170 #define MMC_SPI_MR      SPI0_MR
00171 #endif
00172 
00173 #ifndef MMC_SPI_RDR
00174 #define MMC_SPI_RDR     SPI0_RDR
00175 #endif
00176 
00177 #ifndef MMC_SPI_TDR
00178 #define MMC_SPI_TDR     SPI0_TDR
00179 #endif
00180 
00181 #ifndef MMC_SPI_SR
00182 #define MMC_SPI_SR      SPI0_SR
00183 #endif
00184 
00185 #ifndef MMC_SPI_CSR1
00186 #define MMC_SPI_CSR1    SPI0_CSR1
00187 #endif
00188 
00189 #ifndef MMC_SPI_ID
00190 #define MMC_SPI_ID      SPI0_ID
00191 #endif
00192 
00193 
00202 static int At91SpiMmCard0Init(void)
00203 {
00204     return 0;
00205 }
00206 
00215 static int At91SpiMmCard0Select(int on)
00216 {
00217     int rc = (inr(PIOA_ODSR) & _BV(MMC_CS_BIT)) == 0;
00218 
00219     /* MMC select is low active. */
00220     if (on == 1) {
00221         outr(MMC_CS_CODR, _BV(MMC_CS_BIT));
00222     } else if (on == 0) {
00223         outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
00224     }
00225     return rc;
00226 }
00227 
00235 static uint8_t At91SpiMmCard0Io(uint8_t val)
00236 {
00237 #ifdef NUTDEBUG
00238     putchar('[');
00239     if (val != 0xFF) {
00240         printf("s%02X", val);
00241     }
00242 #endif
00243 
00244     /* Transmission is started by writing the transmit data. */
00245     outr(MMC_SPI_TDR, val);
00246     /* Wait for receiver data register full. */
00247     while((inr(MMC_SPI_SR) & SPI_RDRF) == 0);
00248     /* Read data. */
00249     val = (uint8_t)inr(MMC_SPI_RDR);
00250 
00251 #ifdef NUTDEBUG
00252     if (val != 0xFF) {
00253         printf("r%02X", val);
00254     }
00255     putchar(']');
00256 #endif
00257     return val;
00258 }
00259 
00270 int At91SpiMmCard0Avail(void)
00271 {
00272     return 1;
00273 }
00274 
00282 int At91SpiMmCard0WrProt(void)
00283 {
00284     return 0;
00285 }
00286 
00295 static int At91SpiMmcIfcInit(NUTDEVICE * dev)
00296 {
00297     /* Disable PIO lines used for SPI. */
00298     outr(MMC_PIO_PDR, MMC_PINS_A | MMC_PINS_B);
00299     /* Enable peripherals. */
00300     outr(MMC_PIO_ASR, MMC_PINS_A);
00301     outr(MMC_PIO_BSR, MMC_PINS_B);
00302 
00303     /* MMC chip select is manually controlled. */
00304     outr(MMC_CS_PER, _BV(MMC_CS_BIT));
00305         outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
00306         outr(MMC_CS_OER, _BV(MMC_CS_BIT));
00307 
00308     /* Enable SPI clock. */
00309     outr(PMC_PCER, _BV(MMC_SPI_ID));
00310 
00311     /* SPI enable and reset. */
00312     outr(MMC_SPI_CR, SPI_SPIEN | SPI_SWRST);
00313     outr(MMC_SPI_CR, SPI_SPIEN);
00314 
00315     /* Set SPI to master mode, fixed peripheral at CS1, fault detection disabled. */
00316     outr(MMC_SPI_MR, (1 << SPI_PCS_LSB) | SPI_MODFDIS | SPI_MSTR);
00317 
00318     /* Data changes during clock low and will be sampled on rising edges. */
00319     outr(MMC_SPI_CSR1, (3 << SPI_SCBR_LSB) | SPI_CPOL);
00320 
00321     return MmCardDevInit(dev);
00322 }
00323 
00324 static MMCIFC mmc0_ifc = {
00325     At91SpiMmCard0Init,             
00326     At91SpiMmCard0Io,               
00327     At91SpiMmCard0Select,           
00328     At91SpiMmCard0Avail,            
00329     At91SpiMmCard0WrProt            
00330 };
00331 
00344 NUTDEVICE devAt91SpiMmc0 = {
00345     0,                          
00346     {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
00347     ,                           
00348     0,                          
00349     0,                          
00350     0,                          
00351     &mmc0_ifc,                  
00352     0,                          
00353     At91SpiMmcIfcInit,          
00354     MmCardIOCtl,                
00355     MmCardBlockRead,            
00356     MmCardBlockWrite,           
00357 #ifdef __HARVARD_ARCH__
00358     MmCardBlockWrite_P,         
00359 #endif
00360     MmCardMount,                
00361     MmCardUnmount,              
00362     0                           
00363 };
00364