sbi_mmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH
  3. * Copyright (C) 2008 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \brief Low Level Multimedia Card Access.
  37. *
  38. * Low level MMC hardware routines for SPI emulation by software
  39. * (bit banging).
  40. *
  41. * These routines support SPI mode only and are required by the
  42. * basic MMC driver.
  43. *
  44. * \verbatim
  45. * $Id: sbi_mmc.c 5472 2013-12-06 00:16:28Z olereinhardt $
  46. * \endverbatim
  47. */
  48. /*!
  49. * \addtogroup xgSbiMmCard
  50. */
  51. /*@{*/
  52. #if defined (NUTDEBUG)
  53. #include <stdio.h>
  54. #endif
  55. #if defined(MMC_CLK_PIO_BIT) && defined(MMC_CLK_PIO_ID)
  56. #undef GPIO_ID
  57. #define GPIO_ID MMC_CLK_PIO_ID
  58. #include <cfg/arch/porttran.h>
  59. static INLINE void MMC_CLK_LO(void) { GPIO_SET_LO(MMC_CLK_PIO_BIT); }
  60. static INLINE void MMC_CLK_HI(void) { GPIO_SET_HI(MMC_CLK_PIO_BIT); }
  61. static INLINE void MMC_CLK_EN(void) { GPIO_ENABLE(MMC_CLK_PIO_BIT); }
  62. static INLINE void MMC_CLK_SO(void) { GPIO_OUTPUT(MMC_CLK_PIO_BIT); }
  63. #else
  64. #define MMC_CLK_LO()
  65. #define MMC_CLK_HI()
  66. #define MMC_CLK_EN()
  67. #define MMC_CLK_SO()
  68. #endif
  69. #if defined(MMC_MOSI_PIO_BIT) && defined(MMC_MOSI_PIO_ID)
  70. #undef GPIO_ID
  71. #define GPIO_ID MMC_MOSI_PIO_ID
  72. #include <cfg/arch/porttran.h>
  73. static INLINE void MMC_MOSI_LO(void) { GPIO_SET_LO(MMC_MOSI_PIO_BIT); }
  74. static INLINE void MMC_MOSI_HI(void) { GPIO_SET_HI(MMC_MOSI_PIO_BIT); }
  75. static INLINE void MMC_MOSI_EN(void) { GPIO_ENABLE(MMC_MOSI_PIO_BIT); }
  76. static INLINE void MMC_MOSI_SO(void) { GPIO_OUTPUT(MMC_MOSI_PIO_BIT); }
  77. #else
  78. #define MMC_MOSI_LO()
  79. #define MMC_MOSI_HI()
  80. #define MMC_MOSI_EN()
  81. #define MMC_MOSI_SO()
  82. #endif
  83. #if defined(MMC_MISO_PIO_BIT) && defined(MMC_MISO_PIO_ID)
  84. #undef GPIO_ID
  85. #define GPIO_ID MMC_MISO_PIO_ID
  86. #include <cfg/arch/porttran.h>
  87. static INLINE int MMC_MISO_TEST(void) { return GPIO_GET(MMC_MISO_PIO_BIT); }
  88. static INLINE void MMC_MISO_EN(void) { GPIO_ENABLE(MMC_MISO_PIO_BIT); }
  89. static INLINE void MMC_MISO_SI(void) { GPIO_INPUT(MMC_MISO_PIO_BIT); }
  90. #else
  91. #define MMC_MISO_TEST() (1)
  92. #define MMC_MISO_EN()
  93. #define MMC_MISO_SI()
  94. #endif
  95. #if defined(MMC_CS_PIO_BIT) && defined(MMC_CS_PIO_ID)
  96. #undef GPIO_ID
  97. #define GPIO_ID MMC_CS_PIO_ID
  98. #include <cfg/arch/porttran.h>
  99. static INLINE void MMC_CS_LO(void) { GPIO_SET_LO(MMC_CS_PIO_BIT); }
  100. static INLINE void MMC_CS_HI(void) { GPIO_SET_HI(MMC_CS_PIO_BIT); }
  101. static INLINE int MMC_CS_IS(void) { return GPIO_IS_HI(MMC_CS_PIO_BIT); }
  102. static INLINE void MMC_CS_EN(void) { GPIO_ENABLE(MMC_CS_PIO_BIT); }
  103. static INLINE void MMC_CS_SO(void) { GPIO_OUTPUT(MMC_CS_PIO_BIT); }
  104. #else
  105. #define MMC_CS_LO()
  106. #define MMC_CS_HI()
  107. #define MMC_CS_IS() (1)
  108. #define MMC_CS_EN()
  109. #define MMC_CS_SO()
  110. #endif
  111. /*!
  112. * \brief Private data of NPL card interface.
  113. */
  114. typedef struct _MMCDCB {
  115. int dcb_avail; /*!< Card is available. */
  116. int dcb_changed; /*!< Card has changed. */
  117. int dcb_addr_mode; /*!< Card addressing mode (byte/block. */
  118. } MMCDCB;
  119. static MMCDCB mmc_dcb;
  120. /*!
  121. * \brief Initialize the card in slot 0.
  122. *
  123. * Called by the MMC driver during card reset. The card change
  124. * detection flag will be cleared.
  125. *
  126. * \return 0 on success, -1 if no card is detected.
  127. */
  128. static int SbiMmCardInit(void)
  129. {
  130. mmc_dcb.dcb_changed = 0;
  131. if (mmc_dcb.dcb_avail) {
  132. return 0;
  133. }
  134. return -1;
  135. }
  136. /*!
  137. * \brief Activate or deactivate chip select on card slot 0.
  138. *
  139. * \param on The card will be selected if 1, deselected if 0.
  140. * Any other value can be used to query the status.
  141. *
  142. * \return Previous select status. 1 if selected, 0 otherwise.
  143. */
  144. static int SbiMmCardSelect(int on)
  145. {
  146. int rc = MMC_CS_IS();
  147. /* MMC select is low active. */
  148. if (on == 1) {
  149. MMC_CS_LO();
  150. } else if (on == 0) {
  151. MMC_CS_HI();
  152. }
  153. return rc;
  154. }
  155. /*!
  156. * \brief Read the previously received byte and transmit a new one.
  157. *
  158. * \param val Byte to transmit.
  159. *
  160. * \return Last byte received.
  161. */
  162. static uint8_t SbiMmCardIo(uint8_t val)
  163. {
  164. uint_fast8_t msk = 0x80;
  165. #if defined(NUTDEBUG)
  166. putchar('[');
  167. if (val != 0xFF) {
  168. printf("s%02X", val);
  169. }
  170. #endif
  171. /* SPI bit banging. */
  172. while (msk) {
  173. MMC_CLK_LO();
  174. if (val & msk) {
  175. MMC_MOSI_HI();
  176. } else {
  177. MMC_MOSI_LO();
  178. }
  179. _NOP(); _NOP(); _NOP(); _NOP();
  180. _NOP(); _NOP(); _NOP(); _NOP();
  181. _NOP(); _NOP(); _NOP(); _NOP();
  182. _NOP(); _NOP(); _NOP(); _NOP();
  183. MMC_CLK_HI();
  184. if (MMC_MISO_TEST()) {
  185. val |= msk;
  186. }
  187. else {
  188. val &= ~msk;
  189. }
  190. msk >>= 1;
  191. }
  192. #if defined(NUTDEBUG)
  193. if (val != 0xFF) {
  194. printf("r%02X", val);
  195. }
  196. putchar(']');
  197. #endif
  198. return val;
  199. }
  200. /*!
  201. * \brief Check if card is available in slot 0.
  202. *
  203. * \todo Card change should verify the card identifier. Right now
  204. * any detection of removing and re-inserting a card counts
  205. * as a card change.
  206. *
  207. * \return 0 if no card is detected, 1 if a card is available or 2 if
  208. * a card change had been detected after the last mount.
  209. */
  210. static int SbiMmCardAvail(void)
  211. {
  212. if (mmc_dcb.dcb_avail) {
  213. if (mmc_dcb.dcb_changed) {
  214. return 2;
  215. }
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. /*!
  221. * \brief Check if card in slot 0 is write protected.
  222. *
  223. * \todo Not implemented.
  224. *
  225. * \return Always 0.
  226. */
  227. static int SbiMmCardWrProt(void)
  228. {
  229. return 0;
  230. }
  231. /*!
  232. * \brief set addressing mode
  233. *
  234. */
  235. int SbiMmCardSetAdrMode(int mode)
  236. {
  237. mmc_dcb.dcb_addr_mode = mode;
  238. return(0);
  239. }
  240. /*!
  241. * \brief get addressing mode
  242. *
  243. */
  244. int SbiMmCardGetAdrMode(void)
  245. {
  246. return(mmc_dcb.dcb_addr_mode);
  247. }
  248. /*!
  249. * \brief Initialize MMC hardware interface.
  250. *
  251. * This function is automatically executed during during device
  252. * registration via NutRegisterDevice().
  253. *
  254. * \param dev Identifies the device to initialize.
  255. */
  256. static int SbiMmcIfcInit(NUTDEVICE * dev)
  257. {
  258. /* Enable all clocks. */
  259. #if defined(PMC_PCER)
  260. #if defined(MMC_CLK_PIO_ID)
  261. outr(PMC_PCER, _BV(MMC_CLK_PIO_ID));
  262. #endif
  263. #if defined(MMC_MOSI_PIO_ID)
  264. outr(PMC_PCER, _BV(MMC_MOSI_PIO_ID));
  265. #endif
  266. #if defined(MMC_MISO_PIO_ID)
  267. outr(PMC_PCER, _BV(MMC_MISO_PIO_ID));
  268. #endif
  269. #if defined(MMC_CS_PIO_ID)
  270. outr(PMC_PCER, _BV(MMC_CS_PIO_ID));
  271. #endif
  272. #if defined(MMC_CD_PIO_ID)
  273. outr(PMC_PCER, _BV(MMC_CD_PIO_ID));
  274. #endif
  275. #if defined(MMC_WP_PIO_ID)
  276. outr(PMC_PCER, _BV(MMC_WP_PIO_ID));
  277. #endif
  278. #endif /* PMC_PCER */
  279. /* Set all outputs high. */
  280. MMC_CLK_HI();
  281. MMC_MOSI_HI();
  282. MMC_CS_HI();
  283. /* Enable GPIO on all lines. */
  284. MMC_CLK_EN();
  285. MMC_MOSI_EN();
  286. MMC_MISO_EN();
  287. MMC_CS_EN();
  288. /* Set line directions. */
  289. MMC_CLK_SO();
  290. MMC_MOSI_SO();
  291. MMC_MISO_SI();
  292. MMC_CS_SO();
  293. mmc_dcb.dcb_avail = 1;
  294. mmc_dcb.dcb_changed = 0;
  295. return MmCardDevInit(dev);
  296. }
  297. static MMCIFC mmc_ifc = {
  298. SbiMmCardInit, /*!< mmcifc_in */
  299. SbiMmCardIo, /*!< mmcifc_io */
  300. SbiMmCardSelect, /*!< mmcifc_cs */
  301. SbiMmCardAvail, /*!< mmcifc_cd */
  302. SbiMmCardWrProt, /*!< mmcifc_wp */
  303. SbiMmCardSetAdrMode, /*!< mmcifc_sm */
  304. SbiMmCardGetAdrMode /*!< mmcifc_gm */
  305. };
  306. /*!
  307. * \brief Multimedia card device information structure.
  308. *
  309. * A pointer to this structure must be passed to NutRegisterDevice()
  310. * to bind this driver to the Nut/OS kernel. An application may then
  311. * call
  312. * /verbatim
  313. * _open("MMC0:", _O_RDWR | _O_BINARY);
  314. * /endverbatim
  315. * to mount the first active primary partition with any previously
  316. * registered file system driver (typically devPhat0).
  317. */
  318. NUTDEVICE devSbiMmCard = {
  319. 0, /*!< Pointer to next device, dev_next. */
  320. MMC_DEV_NAME, /*!< Unique device name, dev_name. */
  321. 0, /*!< Type of device, dev_type. Obsolete. */
  322. 0, /*!< Base address, dev_base. Unused. */
  323. 0, /*!< First interrupt number, dev_irq. Unused. */
  324. &mmc_ifc, /*!< Interface control block, dev_icb. */
  325. &mmc_dcb, /*!< Driver control block used by the low level part, dev_dcb. */
  326. SbiMmcIfcInit, /*!< Driver initialization routine, dev_init. */
  327. MmCardIOCtl, /*!< Driver specific control function, dev_ioctl. */
  328. MmCardBlockRead, /*!< Read data from a file, dev_read. */
  329. MmCardBlockWrite, /*!< Write data to a file, dev_write. */
  330. #ifdef __HARVARD_ARCH__
  331. MmCardBlockWrite_P, /*!< Write data from program space to a file, dev_write_P. */
  332. #endif
  333. MmCardMount, /*!< Mount a file system, dev_open. */
  334. MmCardUnmount, /*!< Unmount a file system, dev_close. */
  335. NULL, /*!< Return file size, dev_size. */
  336. NULL, /*!< Select function, optional, not yet implemented */
  337. };
  338. /*@}*/