00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00057 #include <cfg/arch.h>
00058 #include <cfg/arch/gpio.h>
00059
00060 #include <dev/mmcard.h>
00061 #include <dev/spimmc_at91.h>
00062
00063 #if 0
00064
00065 #define NUTDEBUG
00066 #include <stdio.h>
00067 #endif
00068
00073
00074 #if defined(MCU_AT91SAM9260)
00075
00076 #ifndef MMC_CS_BIT
00077 #define MMC_CS_BIT PA3_SPI0_NPCS0_A
00078 #endif
00079 #define MMC_DATAOUT_BIT PA0_SPI0_MISO_A
00080 #define MMC_DATAIN_BIT PA1_SPI0_MOSI_A
00081 #define MMC_CLK_BIT PA2_SPI0_SPCK_A
00082
00083 #elif defined(MCU_AT91SAM7X256)
00084
00085 #ifndef MMC_CS_BIT
00086 #define MMC_CS_BIT SPI0_NPCS1_PA13A
00087 #endif
00088 #define MMC_DATAOUT_BIT SPI0_MISO_PA16A
00089 #define MMC_DATAIN_BIT SPI0_MOSI_PA17A
00090 #define MMC_CLK_BIT SPI0_SPCK_PA18A
00091
00092 #elif defined(MCU_AT91SAM7S256)
00093
00094 #ifndef MMC_CS_BIT
00095 #define MMC_CS_BIT SPI0_NPCS0_PA11A
00096 #endif
00097 #define MMC_DATAOUT_BIT SPI0_MISO_PA12A
00098 #define MMC_DATAIN_BIT SPI0_MOSI_PA13A
00099 #define MMC_CLK_BIT SPI0_SPCK_PA14A
00100
00101 #else
00102 #warning "MMC SPI mode not supported on this MCU"
00103 #endif
00104
00105 #ifndef MMC_PIO_ASR
00106 #define MMC_PIO_ASR PIOA_ASR
00107 #endif
00108
00109 #ifndef MMC_PIO_BSR
00110 #define MMC_PIO_BSR PIOA_BSR
00111 #endif
00112
00113 #ifndef MMC_PIO_PDR
00114 #define MMC_PIO_PDR PIOA_PDR
00115 #endif
00116
00117 #ifndef MMC_PINS_A
00118 #define MMC_PINS_A (_BV(MMC_DATAOUT_BIT) | _BV(MMC_DATAIN_BIT) | _BV(MMC_CLK_BIT))
00119 #endif
00120
00121 #ifndef MMC_PINS_B
00122 #define MMC_PINS_B 0
00123 #endif
00124
00125 #ifndef MMC_CS_PER
00126 #define MMC_CS_PER PIOA_PER
00127 #endif
00128
00129 #ifndef MMC_CS_OER
00130 #define MMC_CS_OER PIOA_OER
00131 #endif
00132
00133 #ifndef MMC_CS_SODR
00134 #define MMC_CS_SODR PIOA_SODR
00135 #endif
00136
00137 #ifndef MMC_CS_CODR
00138 #define MMC_CS_CODR PIOA_CODR
00139 #endif
00140
00141 #ifndef MMC_SPI_CR
00142 #define MMC_SPI_CR SPI0_CR
00143 #endif
00144
00145 #ifndef MMC_SPI_MR
00146 #define MMC_SPI_MR SPI0_MR
00147 #endif
00148
00149 #ifndef MMC_SPI_RDR
00150 #define MMC_SPI_RDR SPI0_RDR
00151 #endif
00152
00153 #ifndef MMC_SPI_TDR
00154 #define MMC_SPI_TDR SPI0_TDR
00155 #endif
00156
00157 #ifndef MMC_SPI_SR
00158 #define MMC_SPI_SR SPI0_SR
00159 #endif
00160
00161 #ifndef MMC_SPI_CSR1
00162 #define MMC_SPI_CSR1 SPI0_CSR1
00163 #endif
00164
00165 #ifndef MMC_SPI_ID
00166 #define MMC_SPI_ID SPI0_ID
00167 #endif
00168
00169
00178 static int At91SpiMmCard0Init(void)
00179 {
00180 return 0;
00181 }
00182
00191 static int At91SpiMmCard0Select(int on)
00192 {
00193 int rc = (inr(PIOA_ODSR) & _BV(MMC_CS_BIT)) == 0;
00194
00195
00196 if (on == 1) {
00197 outr(MMC_CS_CODR, _BV(MMC_CS_BIT));
00198 } else if (on == 0) {
00199 outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
00200 }
00201 return rc;
00202 }
00203
00211 static u_char At91SpiMmCard0Io(u_char val)
00212 {
00213 #ifdef NUTDEBUG
00214 putchar('[');
00215 if (val != 0xFF) {
00216 printf("s%02X", val);
00217 }
00218 #endif
00219
00220
00221 outr(MMC_SPI_TDR, val);
00222
00223 while((inr(MMC_SPI_SR) & SPI_RDRF) == 0);
00224
00225 val = (u_char)inr(MMC_SPI_RDR);
00226
00227 #ifdef NUTDEBUG
00228 if (val != 0xFF) {
00229 printf("r%02X", val);
00230 }
00231 putchar(']');
00232 #endif
00233 return val;
00234 }
00235
00246 int At91SpiMmCard0Avail(void)
00247 {
00248 return 1;
00249 }
00250
00258 int At91SpiMmCard0WrProt(void)
00259 {
00260 return 0;
00261 }
00262
00271 static int At91SpiMmcIfcInit(NUTDEVICE * dev)
00272 {
00273
00274 outr(MMC_PIO_PDR, MMC_PINS_A | MMC_PINS_B);
00275
00276 outr(MMC_PIO_ASR, MMC_PINS_A);
00277 outr(MMC_PIO_BSR, MMC_PINS_B);
00278
00279
00280 outr(MMC_CS_PER, _BV(MMC_CS_BIT));
00281 outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
00282 outr(MMC_CS_OER, _BV(MMC_CS_BIT));
00283
00284
00285 outr(PMC_PCER, _BV(MMC_SPI_ID));
00286
00287
00288 outr(MMC_SPI_CR, SPI_SPIEN | SPI_SWRST);
00289 outr(MMC_SPI_CR, SPI_SPIEN);
00290
00291
00292 outr(MMC_SPI_MR, (1 << SPI_PCS_LSB) | SPI_MODFDIS | SPI_MSTR);
00293
00294
00295 outr(MMC_SPI_CSR1, (3 << SPI_SCBR_LSB) | SPI_CPOL);
00296
00297 return MmCardDevInit(dev);
00298 }
00299
00300 static MMCIFC mmc0_ifc = {
00301 At91SpiMmCard0Init,
00302 At91SpiMmCard0Io,
00303 At91SpiMmCard0Select,
00304 At91SpiMmCard0Avail,
00305 At91SpiMmCard0WrProt
00306 };
00307
00320 NUTDEVICE devAt91SpiMmc0 = {
00321 0,
00322 {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
00323 ,
00324 0,
00325 0,
00326 0,
00327 &mmc0_ifc,
00328 0,
00329 At91SpiMmcIfcInit,
00330 MmCardIOCtl,
00331 MmCardBlockRead,
00332 MmCardBlockWrite,
00333 #ifdef __HARVARD_ARCH__
00334 MmCardBlockWrite_P,
00335 #endif
00336 MmCardMount,
00337 MmCardUnmount,
00338 0
00339 };
00340