spimmc_at91.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \brief Low Level Multimedia Card Access.
  34. *
  35. * Low level MMC hardware routines, tested on the AT91SAM7X.
  36. *
  37. * These routines support SPI mode only and are required by the
  38. * basic MMC driver.
  39. *
  40. * \verbatim
  41. *
  42. * $Log$
  43. * Revision 1.7 2010/12/15 15:14:13 ve2yag
  44. * Add support for all SAM7S and 7SE family.
  45. *
  46. * Revision 1.6 2008/08/11 06:59:13 haraldkipp
  47. * BSD types replaced by stdint types (feature request #1282721).
  48. *
  49. * Revision 1.5 2008/08/06 12:51:01 haraldkipp
  50. * Added support for Ethernut 5 (AT91SAM9XE reference design).
  51. *
  52. * Revision 1.4 2008/02/15 16:59:02 haraldkipp
  53. * Spport for AT91SAM7SE512 added.
  54. *
  55. * Revision 1.3 2008/01/31 09:16:03 haraldkipp
  56. * Failed to compile for SAM7S. Added some default settings.
  57. *
  58. * Revision 1.2 2006/09/05 12:27:55 haraldkipp
  59. * Added support for the AT91SAM9260.
  60. *
  61. * Revision 1.1 2006/07/26 11:20:08 haraldkipp
  62. * Added MMC/SD-Card support for AT91SAM7X Evaluation Kit.
  63. *
  64. *
  65. * \endverbatim
  66. */
  67. #include <cfg/arch.h>
  68. #include <cfg/arch/gpio.h>
  69. #include <dev/mmcard.h>
  70. #include <dev/spimmc_at91.h>
  71. #if 0
  72. /* Use for local debugging. */
  73. #define NUTDEBUG
  74. #include <stdio.h>
  75. #endif
  76. /*!
  77. * \addtogroup xgAt91SpiMmc
  78. */
  79. /*@{*/
  80. #if defined(MCU_AT91SAM9G45)
  81. #ifndef MMC_CS_BIT
  82. #define MMC_CS_BIT PB3_SPI0_NPCS0_A
  83. #endif
  84. #define MMC_DATAOUT_BIT PB0_SPI0_MISO_A
  85. #define MMC_DATAIN_BIT PB1_SPI0_MOSI_A
  86. #define MMC_CLK_BIT PB2_SPI0_SPCK_A
  87. #define MMC_PIO_PDR PIOB_PDR
  88. #elif defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE512)
  89. #ifndef MMC_CS_BIT
  90. #define MMC_CS_BIT PA3_SPI0_NPCS0_A
  91. #endif
  92. #define MMC_DATAOUT_BIT PA0_SPI0_MISO_A
  93. #define MMC_DATAIN_BIT PA1_SPI0_MOSI_A
  94. #define MMC_CLK_BIT PA2_SPI0_SPCK_A
  95. #elif defined(MCU_AT91SAM7X)
  96. #ifndef MMC_CS_BIT
  97. #define MMC_CS_BIT SPI0_NPCS1_PA13A
  98. #endif
  99. #define MMC_DATAOUT_BIT SPI0_MISO_PA16A
  100. #define MMC_DATAIN_BIT SPI0_MOSI_PA17A
  101. #define MMC_CLK_BIT SPI0_SPCK_PA18A
  102. //#elif defined(MCU_AT91SAM7S256) || defined(MCU_AT91SAM7SE512)
  103. #elif defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
  104. #ifndef MMC_CS_BIT
  105. #define MMC_CS_BIT SPI0_NPCS0_PA11A
  106. #endif
  107. #define MMC_DATAOUT_BIT SPI0_MISO_PA12A
  108. #define MMC_DATAIN_BIT SPI0_MOSI_PA13A
  109. #define MMC_CLK_BIT SPI0_SPCK_PA14A
  110. #else /* MCU_AT91SAM7X */
  111. #warning "MMC SPI mode not supported on this MCU"
  112. #endif
  113. #ifndef MMC_PIO_ASR
  114. #define MMC_PIO_ASR PIOA_ASR
  115. #endif
  116. #ifndef MMC_PIO_BSR
  117. #define MMC_PIO_BSR PIOA_BSR
  118. #endif
  119. #ifndef MMC_PIO_PDR
  120. #define MMC_PIO_PDR PIOA_PDR
  121. #endif
  122. #ifndef MMC_PINS_A
  123. #define MMC_PINS_A (_BV(MMC_DATAOUT_BIT) | _BV(MMC_DATAIN_BIT) | _BV(MMC_CLK_BIT))
  124. #endif
  125. #ifndef MMC_PINS_B
  126. #define MMC_PINS_B 0
  127. #endif
  128. #ifndef MMC_CS_PER
  129. #define MMC_CS_PER PIOA_PER
  130. #endif
  131. #ifndef MMC_CS_OER
  132. #define MMC_CS_OER PIOA_OER
  133. #endif
  134. #ifndef MMC_CS_SODR
  135. #define MMC_CS_SODR PIOA_SODR
  136. #endif
  137. #ifndef MMC_CS_CODR
  138. #define MMC_CS_CODR PIOA_CODR
  139. #endif
  140. #ifndef MMC_SPI_CR
  141. #define MMC_SPI_CR SPI0_CR
  142. #endif
  143. #ifndef MMC_SPI_MR
  144. #define MMC_SPI_MR SPI0_MR
  145. #endif
  146. #ifndef MMC_SPI_RDR
  147. #define MMC_SPI_RDR SPI0_RDR
  148. #endif
  149. #ifndef MMC_SPI_TDR
  150. #define MMC_SPI_TDR SPI0_TDR
  151. #endif
  152. #ifndef MMC_SPI_SR
  153. #define MMC_SPI_SR SPI0_SR
  154. #endif
  155. #ifndef MMC_SPI_CSR1
  156. #define MMC_SPI_CSR1 SPI0_CSR1
  157. #endif
  158. #ifndef MMC_SPI_ID
  159. #define MMC_SPI_ID SPI0_ID
  160. #endif
  161. /*!
  162. * \brief Private data of AT91 SPI card interface.
  163. * \note that the addressing mode indicates SD-HC or SD-normal cards
  164. * SD-HC uses byte addressing, SD-normal uses block addressing
  165. */
  166. typedef struct _MMCDCB
  167. {
  168. int dcb_avail; /*!< Card is available. */
  169. int dcb_changed; /*!< Card has changed. */
  170. int dcb_addr_mode; /*!< Card addressing mode (byte/block. */
  171. } MMCDCB;
  172. static MMCDCB mmc0_dcb;
  173. /*!
  174. * \brief Initialize the card in slot 0.
  175. *
  176. * Called by the MMC driver during card reset. The card change
  177. * detection flag will be cleared.
  178. *
  179. * \return 0 on success, -1 if no card is detected.
  180. */
  181. static int At91SpiMmCard0Init(void)
  182. {
  183. mmc0_dcb.dcb_changed = 0;
  184. if (mmc0_dcb.dcb_avail)
  185. {
  186. return (0);
  187. }
  188. return -1;
  189. }
  190. /*!
  191. * \brief Activate or deactivate chip select on card slot 0.
  192. *
  193. * \param on The card will be selected if 1, deselected if 0.
  194. * Any other value can be used to query the status.
  195. *
  196. * \return Previous select status. 1 if selected, 0 otherwise.
  197. */
  198. static int At91SpiMmCard0Select(int on)
  199. {
  200. int rc = (inr(PIOA_ODSR) & _BV(MMC_CS_BIT)) == 0;
  201. /* MMC select is low active. */
  202. if (on == 1) {
  203. outr(MMC_CS_CODR, _BV(MMC_CS_BIT));
  204. } else if (on == 0) {
  205. outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
  206. }
  207. return rc;
  208. }
  209. /*!
  210. * \brief Read the previously received byte and transmit a new one.
  211. *
  212. * \param val Byte to transmit.
  213. *
  214. * \return Last byte received.
  215. */
  216. static uint8_t At91SpiMmCard0Io(uint8_t val)
  217. {
  218. #ifdef NUTDEBUG
  219. putchar('[');
  220. if (val != 0xFF) {
  221. printf("s%02X", val);
  222. }
  223. #endif
  224. /* Transmission is started by writing the transmit data. */
  225. outr(MMC_SPI_TDR, val);
  226. /* Wait for receiver data register full. */
  227. while((inr(MMC_SPI_SR) & SPI_RDRF) == 0);
  228. /* Read data. */
  229. val = (uint8_t)inr(MMC_SPI_RDR);
  230. #ifdef NUTDEBUG
  231. if (val != 0xFF) {
  232. printf("r%02X", val);
  233. }
  234. putchar(']');
  235. #endif
  236. return val;
  237. }
  238. /*!
  239. * \brief Check if card is available in slot 0.
  240. *
  241. * \todo Card change should verify the card identifier. Right now
  242. * any detection of removing and re-inserting a card counts
  243. * as a card change.
  244. *
  245. * \return 0 if no card is detected, 1 if a card is available or 2 if
  246. * a card change had been detected after the last mount.
  247. */
  248. int At91SpiMmCard0Avail(void)
  249. {
  250. /*
  251. int bPresent;
  252. // Mmmm, low-level block device is calling the application????
  253. bPresent=GpioPinGet(MMC_CD_PORT, MMC_CD_PIN);
  254. if (mmc0_dcb.dcb_avail != bPresent)
  255. {
  256. mmc0_dcb.dcb_avail= bPresent;
  257. mmc0_dcb.dcb_changed=1; // not used for anything, just in this context....
  258. return (2);
  259. }
  260. return bPresent;
  261. */
  262. mmc0_dcb.dcb_avail = 1;
  263. mmc0_dcb.dcb_changed = 0;
  264. return 1;
  265. }
  266. /*!
  267. * \brief Check if card in slot 0 is write protected.
  268. *
  269. * \todo Not implemented.
  270. *
  271. * \return Always 0.
  272. */
  273. int At91SpiMmCard0WrProt(void)
  274. {
  275. return 0;
  276. }
  277. /*!
  278. * \brief set addressing mode
  279. *
  280. */
  281. int At91SpiMmCard0SetAdrMode(int mode)
  282. {
  283. mmc0_dcb.dcb_addr_mode = mode;
  284. return(0);
  285. }
  286. /*!
  287. * \brief get addressing mode
  288. *
  289. */
  290. int At91SpiMmCard0GetAdrMode(void)
  291. {
  292. return(mmc0_dcb.dcb_addr_mode);
  293. }
  294. /*!
  295. * \brief Initialize MMC hardware interface.
  296. *
  297. * This function is automatically executed during during device
  298. * registration via NutRegisterDevice().
  299. *
  300. * \param dev Identifies the device to initialize.
  301. */
  302. static int At91SpiMmcIfcInit(NUTDEVICE * dev)
  303. {
  304. /* Disable PIO lines used for SPI. */
  305. outr(MMC_PIO_PDR, MMC_PINS_A | MMC_PINS_B);
  306. /* Enable peripherals. */
  307. outr(MMC_PIO_ASR, MMC_PINS_A);
  308. outr(MMC_PIO_BSR, MMC_PINS_B);
  309. /* MMC chip select is manually controlled. */
  310. outr(MMC_CS_PER, _BV(MMC_CS_BIT));
  311. outr(MMC_CS_SODR, _BV(MMC_CS_BIT));
  312. outr(MMC_CS_OER, _BV(MMC_CS_BIT));
  313. /* Enable SPI clock. */
  314. outr(PMC_PCER, _BV(MMC_SPI_ID));
  315. /* SPI enable and reset. */
  316. outr(MMC_SPI_CR, SPI_SPIEN | SPI_SWRST);
  317. /* Apply a reset twice! See errata at page 652 in the datasheet:
  318. 41.4.8.7
  319. SPI: Software Reset must be Written Twice
  320. If a software reset (SWRST in the SPI Control Register) is performed,
  321. the SPI may not work properly (the clock is enabled before the chip select.)
  322. Problem Fix/Workaround
  323. The SPI Control Register field, SWRST (Software Reset) needs to be
  324. written twice to be correctly set.
  325. */
  326. outr(MMC_SPI_CR, SPI_SPIEN | SPI_SWRST);
  327. outr(MMC_SPI_CR, SPI_SPIEN);
  328. /* Set SPI to master mode, fixed peripheral at CS1, fault detection disabled. */
  329. outr(MMC_SPI_MR, (1 << SPI_PCS_LSB) | SPI_MODFDIS | SPI_MSTR);
  330. /* Data changes during clock low and will be sampled on rising edges. */
  331. outr(MMC_SPI_CSR1, (3 << SPI_SCBR_LSB) | SPI_CPOL);
  332. return MmCardDevInit(dev);
  333. }
  334. static MMCIFC mmc0_ifc = {
  335. At91SpiMmCard0Init, /*!< mmcifc_in */
  336. At91SpiMmCard0Io, /*!< mmcifc_io */
  337. At91SpiMmCard0Select, /*!< mmcifc_cs */
  338. At91SpiMmCard0Avail, /*!< mmcifc_cd */
  339. At91SpiMmCard0WrProt, /*!< mmcifc_wp */
  340. At91SpiMmCard0SetAdrMode, /*!< mmcifc_sm */
  341. At91SpiMmCard0GetAdrMode /*!< mmcifc_gm */
  342. };
  343. /*!
  344. * \brief Multimedia card device information structure.
  345. *
  346. * A pointer to this structure must be passed to NutRegisterDevice()
  347. * to bind this driver to the Nut/OS kernel. An application may then
  348. * call
  349. * /verbatim
  350. * _open("MMC0:", _O_RDWR | _O_BINARY);
  351. * /endverbatim
  352. * to mount the first active primary partition with any previously
  353. * registered file system driver (typically devPhat0).
  354. */
  355. NUTDEVICE devAt91SpiMmc0 = {
  356. 0, /*!< Pointer to next device, dev_next. */
  357. {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
  358. , /*!< Unique device name, dev_name. */
  359. 0, /*!< Type of device, dev_type. Obsolete. */
  360. 0, /*!< Base address, dev_base. Unused. */
  361. 0, /*!< First interrupt number, dev_irq. Unused. */
  362. &mmc0_ifc, /*!< Interface control block, dev_icb. */
  363. &mmc0_dcb, /*!< Driver control block used by the low level part, dev_dcb. */
  364. At91SpiMmcIfcInit, /*!< Driver initialization routine, dev_init. */
  365. MmCardIOCtl, /*!< Driver specific control function, dev_ioctl. */
  366. MmCardBlockRead, /*!< Read data from a file, dev_read. */
  367. MmCardBlockWrite, /*!< Write data to a file, dev_write. */
  368. #ifdef __HARVARD_ARCH__
  369. MmCardBlockWrite_P, /*!< Write data from program space to a file, dev_write_P. */
  370. #endif
  371. MmCardMount, /*!< Mount a file system, dev_open. */
  372. MmCardUnmount, /*!< Unmount a file system, dev_close. */
  373. 0, /*!< Return file size, dev_size. */
  374. 0, /*!< Select function, optional, not yet implemented */
  375. };
  376. /*@}*/