spi_mmc_npl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2005 by egnite Software GmbH
  3. * Copyright (C) 2010 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 Support.
  37. *
  38. * MMC support routines for the programmable logic provided
  39. * on the Ethernut 3 reference design.
  40. *
  41. * \verbatim
  42. *
  43. * $Id$
  44. *
  45. * \endverbatim
  46. */
  47. #include <cfg/clock.h>
  48. #include <cfg/mmci.h>
  49. #include <dev/npl.h>
  50. #include <dev/mmcard.h>
  51. #include <dev/spibus.h>
  52. #if !defined(NPL_MMC_CLOCK) || (NPL_MMC_CLOCK < 1000)
  53. #undef NPL_MMC_CLOCK
  54. #define NPL_MMC_CLOCK 12500000
  55. #endif
  56. #if 0
  57. /* Use for local debugging. */
  58. #define NUTDEBUG
  59. #include <stdio.h>
  60. #endif
  61. /*!
  62. * \addtogroup xgNplMemCardSupport
  63. */
  64. /*@{*/
  65. static int NplMmc0Reset(NUTDEVICE * dev);
  66. static void NplMmc0Led(int mode);
  67. static int NplMmc0Power(int mode);
  68. /*!
  69. * \brief NPL memory card support structure for MMC0.
  70. */
  71. static MEMCARDSUPP mcsSpiMmc0Npl = {
  72. 0, /*!< \brief Status change flag (mcs_cf). */
  73. 0, /*!< \brief Socket status (mcs_sf). */
  74. NplMmc0Reset, /*!< \brief Status reset (mcs_reset). */
  75. NplMmc0Led, /*!< \brief Set activity indicator (mcs_act). */
  76. NplMmc0Power, /*!< \brief Power control (mcs_power). */
  77. };
  78. /*!
  79. * \brief Card insertion interrupt routine for socket 0.
  80. *
  81. * \param arg Pointer to the memory card support structure.
  82. */
  83. static void NplMmc0Insertion(void *arg)
  84. {
  85. MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
  86. /* Set the change flag. */
  87. mcs->mcs_cf = 1;
  88. /* Set the card detect flag. */
  89. mcs->mcs_sf = NUTMC_SF_CD;
  90. /* Set the card write protect flag. */
  91. if (inw(NPL_SLR) & NPL_LANWAKEUP) {
  92. mcs->mcs_sf |= NUTMC_SF_WP;
  93. }
  94. /* Disable insert and enable remove interrupts. */
  95. NplIrqDisable(&sig_MMCD);
  96. NplIrqEnable(&sig_NMMCD);
  97. }
  98. /*!
  99. * \brief Card removal interrupt routine.
  100. *
  101. * \param arg Pointer to the memory card support structure.
  102. */
  103. static void NplMmcRemoval(void *arg)
  104. {
  105. MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
  106. /* Set the change flag. */
  107. mcs->mcs_cf = 1;
  108. /* Clear card detect and write protect flags. */
  109. mcs->mcs_sf = 0;
  110. /* Disable remove and enable insert interrupts. */
  111. NplIrqDisable(&sig_NMMCD);
  112. NplIrqEnable(&sig_MMCD);
  113. }
  114. /*!
  115. * \brief Card activity indicator control.
  116. *
  117. * \return 0 on success, -1 if no card is detected.
  118. */
  119. static void NplMmc0Led(int mode)
  120. {
  121. #ifdef NPL_MMC0_ULED
  122. if (mode == NUTMC_IND_OFF) {
  123. outb(NPL_XER, inb(NPL_XER) | NPL_USRLED);
  124. } else {
  125. outb(NPL_XER, inb(NPL_XER) & ~NPL_USRLED);
  126. }
  127. #endif
  128. }
  129. /*!
  130. * \brief Card 0 power control.
  131. *
  132. * Not implemented.
  133. *
  134. * \param mode If zero, the card will be put into power down mode.
  135. * Otherwise the card will be activated.
  136. *
  137. * \return Always -1.
  138. */
  139. static int NplMmc0Power(int mode)
  140. {
  141. return -1;
  142. }
  143. /*!
  144. * \brief Card socket 0 reset.
  145. *
  146. * \return Always 0.
  147. */
  148. static int NplMmc0Reset(NUTDEVICE * dev)
  149. {
  150. uint_fast8_t i;
  151. /* Deactivate negated chip select. */
  152. outb(NPL_XER, inb(NPL_XER) | NPL_MMCS);
  153. /* Perform 80 clock cycles. */
  154. for (i = 0; i < 10; i++) {
  155. outb(NPL_MMCDR, 0xFF);
  156. while ((inb(NPL_SLR) & NPL_MMCREADY) == 0);
  157. inb(NPL_MMCDR);
  158. }
  159. /* Switch LED off. */
  160. NplMmc0Led(NUTMC_IND_OFF);
  161. return 0;
  162. }
  163. /*!
  164. * \brief Initialize MMC driver for socket 0.
  165. *
  166. * This function is automatically executed during during device
  167. * registration via NutRegisterDevice().
  168. *
  169. * \param dev Identifies the device to initialize.
  170. */
  171. static int SpiMmc0NplInit(NUTDEVICE * dev)
  172. {
  173. int rc;
  174. NplMmc0Reset(dev);
  175. /* Register card detection interrupts. */
  176. rc = NplRegisterIrqHandler(&sig_MMCD, NplMmc0Insertion, dev->dev_dcb);
  177. if (rc == 0) {
  178. rc = NplRegisterIrqHandler(&sig_NMMCD, NplMmcRemoval, dev->dev_dcb);
  179. if (rc == 0) {
  180. NplIrqEnable(&sig_MMCD);
  181. /* Initialize the SPI interface. */
  182. rc = SpiMmcInit(dev);
  183. }
  184. }
  185. return rc;
  186. }
  187. /*!
  188. * \brief NPL SPI node implementation structure for MMC0.
  189. */
  190. NUTSPINODE nodeSpiMmc0Npl = {
  191. NULL, /*!< \brief Pointer to the bus controller driver, node_bus. */
  192. NULL, /*!< \brief Pointer to device driver specific settings, node_stat. */
  193. NPL_MMC_CLOCK, /*!< \brief Initial clock rate, node_rate. */
  194. SPI_MODE_0, /*!< \brief Initial mode, node_mode. */
  195. 8, /*!< \brief Initial data bits, node_bits. */
  196. 0, /*!< \brief Chip select, node_cs. */
  197. &mcsSpiMmc0Npl /*!< Driver control block used by the low level part, node_dcb. */
  198. };
  199. /*!
  200. * \brief Multimedia card device information structure.
  201. *
  202. * A pointer to this structure must be passed to NutRegisterDevice()
  203. * to bind this driver to the Nut/OS kernel. An application may then
  204. * call
  205. * /verbatim
  206. * _open("MMC0:", _O_RDWR | _O_BINARY);
  207. * /endverbatim
  208. * to mount the first active primary partition with any previously
  209. * registered file system driver (typically devPhat0).
  210. */
  211. NUTDEVICE devNplSpiMmc0 = {
  212. 0, /*!< Pointer to next device, dev_next. */
  213. {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
  214. , /*!< Unique device name, dev_name. */
  215. 0, /*!< Type of device, dev_type. Obsolete. */
  216. 0, /*!< Base address, dev_base. Unused. */
  217. 0, /*!< First interrupt number, dev_irq. Unused. */
  218. &nodeSpiMmc0Npl, /*!< Interface control block, dev_icb. */
  219. &mcsSpiMmc0Npl, /*!< Driver control block used by the low level part, dev_dcb. */
  220. SpiMmc0NplInit, /*!< Driver initialization routine, dev_init. */
  221. SpiMmcIOCtl, /*!< Driver specific control function, dev_ioctl. */
  222. SpiMmcBlockRead, /*!< Read data from a file, dev_read. */
  223. SpiMmcBlockWrite, /*!< Write data to a file, dev_write. */
  224. #ifdef __HARVARD_ARCH__
  225. SpiMmcBlockWrite_P, /*!< Write data from program space to a file, dev_write_P. */
  226. #endif
  227. SpiMmcMount, /*!< Mount a file system, dev_open. */
  228. SpiMmcUnmount, /*!< Unmount a file system, dev_close. */
  229. NULL, /*!< Return file size, dev_size. */
  230. NULL, /*!< Select function, optional, not yet implemented */
  231. };
  232. /*@}*/