nplmmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (C) 2005 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 for the programmable logic provided
  36. * on the Ethernut 3 reference design.
  37. *
  38. * These routines support SPI mode only and are required by the basic MMC
  39. * driver.
  40. *
  41. * \verbatim
  42. *
  43. * $Log$
  44. * Revision 1.6 2009/01/17 11:26:46 haraldkipp
  45. * Getting rid of two remaining BSD types in favor of stdint.
  46. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  47. *
  48. * Revision 1.5 2008/08/11 06:59:42 haraldkipp
  49. * BSD types replaced by stdint types (feature request #1282721).
  50. *
  51. * Revision 1.4 2008/07/14 13:13:45 haraldkipp
  52. * Added delays to make slow cards working.
  53. *
  54. * Revision 1.3 2006/08/05 12:01:05 haraldkipp
  55. * Hard coded PLL selections replaced by configurable definitions.
  56. *
  57. * Revision 1.2 2006/01/19 18:40:47 haraldkipp
  58. * MMC clock rate now uses the CY2239x driver routines to calculate a
  59. * configurable value. Additional NOPs had been added to the SPI I/O,
  60. * which seems to make the driver more stable.
  61. *
  62. * Revision 1.1 2006/01/05 16:31:02 haraldkipp
  63. * First check-in.
  64. *
  65. *
  66. * \endverbatim
  67. */
  68. #include <cfg/clock.h>
  69. #include <sys/event.h>
  70. #include <dev/twif.h>
  71. #include <dev/npl.h>
  72. #include <dev/mmcard.h>
  73. #include <dev/cy2239x.h>
  74. #include <dev/nplmmc.h>
  75. #if !defined(NPL_MMC_CLOCK) || (NPL_MMC_CLOCK < 1000)
  76. #undef NPL_MMC_CLOCK
  77. #define NPL_MMC_CLOCK 12500000
  78. #endif
  79. #if 0
  80. /* Use for local debugging. */
  81. #define NUTDEBUG
  82. #include <stdio.h>
  83. #endif
  84. /*!
  85. * \addtogroup xgNplMmc
  86. */
  87. /*@{*/
  88. #ifndef I2C_SLA_PLL
  89. #define I2C_SLA_PLL 0x69
  90. #endif
  91. /*!
  92. * \brief Private data of NPL card interface.
  93. */
  94. typedef struct _MMCDCB {
  95. int dcb_avail; /*!< Card is available. */
  96. int dcb_changed; /*!< Card has changed. */
  97. int dcb_addr_mode; /*!< Card addressing mode (byte / block) */
  98. } MMCDCB;
  99. static MMCDCB mmc0_dcb;
  100. /*!
  101. * \brief Initialize the card.
  102. *
  103. * \return 0 on success, -1 if no card is detected.
  104. */
  105. static int NplMmCard0Init(void)
  106. {
  107. mmc0_dcb.dcb_changed = 0;
  108. if (mmc0_dcb.dcb_avail) {
  109. return 0;
  110. }
  111. return -1;
  112. }
  113. /*!
  114. * \brief Activate or deactivate chip select on card slot 0.
  115. *
  116. * \param on The card will be selected if 1, deselected if 0.
  117. * Any other value can be used to query the status.
  118. *
  119. * \return Previous select status. 1 if selected, 0 otherwise.
  120. */
  121. static int NplMmCard0Select(int on)
  122. {
  123. int rc = (inb(NPL_XER) & NPL_MMCS) == NPL_MMCS;
  124. if (on == 1) {
  125. /*! \brief Activate negated chip select. */
  126. outb(NPL_XER, inb(NPL_XER) & ~NPL_MMCS);
  127. } else if (on == 0) {
  128. /*! \brief Deactivate negated chip select. */
  129. outb(NPL_XER, inb(NPL_XER) | NPL_MMCS);
  130. }
  131. return rc;
  132. }
  133. /*!
  134. * \brief Read the previously received byte and transmit a new one.
  135. *
  136. * \param val Byte to transmit.
  137. *
  138. * \return Last byte received.
  139. */
  140. static uint8_t NplMmCard0Io(uint8_t val)
  141. {
  142. uint8_t rc;
  143. unsigned int tmo = 255;
  144. while ((inb(NPL_SLR) & NPL_MMCREADY) == 0) {
  145. if (--tmo == 0) {
  146. break;
  147. }
  148. }
  149. _NOP(); _NOP(); _NOP(); _NOP();
  150. rc = inb(NPL_MMCDR);
  151. _NOP(); _NOP(); _NOP(); _NOP();
  152. outb(NPL_MMCDR, val);
  153. _NOP(); _NOP(); _NOP(); _NOP();
  154. #ifdef NUTDEBUG
  155. putchar('[');
  156. if (rc != 0xFF) {
  157. printf("r%02X", rc);
  158. } else if (val != 0xFF) {
  159. printf("s%02X", val);
  160. }
  161. putchar(']');
  162. #endif
  163. return rc;
  164. }
  165. /*!
  166. * \brief Check if card is available.
  167. *
  168. * \todo Card change should verify the card identifier. Right now
  169. * any detection of removing and re-inserting a card counts
  170. * as a card change.
  171. *
  172. * \return 0 if no card is detected, 1 if a card is available or 2 if
  173. * a card change had been detected after the last mount.
  174. */
  175. int NplMmCard0Avail(void)
  176. {
  177. if (mmc0_dcb.dcb_avail) {
  178. if (mmc0_dcb.dcb_changed) {
  179. return 2;
  180. }
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. /*!
  186. * \brief Check if card is write protected.
  187. *
  188. * \todo Not implemented.
  189. *
  190. * \return Always 0.
  191. */
  192. int NplMmCard0WrProt(void)
  193. {
  194. return 0;
  195. }
  196. /*!
  197. * \brief set addressing mode
  198. *
  199. */
  200. int NplMmCard0SetAdrMode(int mode)
  201. {
  202. mmc0_dcb.dcb_addr_mode = mode;
  203. return(0);
  204. }
  205. /*!
  206. * \brief get addressing mode
  207. *
  208. */
  209. int NplMmCard0GetAdrMode(void)
  210. {
  211. return (mmc0_dcb.dcb_addr_mode);
  212. }
  213. /*!
  214. * \brief Card insertion interrupt routine.
  215. *
  216. * \todo A different routine is required to support multiple cards.
  217. *
  218. * \param arg Pointer to the device information structure.
  219. */
  220. static void NplMmCard0InsIrq(void *arg)
  221. {
  222. NplIrqDisable(&sig_MMCD);
  223. mmc0_dcb.dcb_avail = 1;
  224. mmc0_dcb.dcb_changed = 1;
  225. NplIrqEnable(&sig_NMMCD);
  226. }
  227. /*!
  228. * \brief Card removal interrupt routine.
  229. *
  230. * \todo A different routine is required to support multiple cards.
  231. *
  232. * \param arg Pointer to the device information structure.
  233. */
  234. static void NplMmCard0RemIrq(void *arg)
  235. {
  236. NplIrqDisable(&sig_NMMCD);
  237. mmc0_dcb.dcb_avail = 0;
  238. NplIrqEnable(&sig_MMCD);
  239. }
  240. /*!
  241. * \brief Initialize MMC hardware interface.
  242. *
  243. * This function is automatically executed during during device
  244. * registration via NutRegisterDevice().
  245. *
  246. * \todo This routine needs some update to support multiple cards.
  247. *
  248. * \todo PLL clock changes should be based on NPL version.
  249. *
  250. * \param dev Identifies the device to initialize.
  251. */
  252. static int NplMmcIfcInit(NUTDEVICE * dev)
  253. {
  254. int rc;
  255. /* Disable card select. */
  256. NplMmCard0Select(0);
  257. #if defined(NUT_PLL_NPLCLK1)
  258. {
  259. uint32_t val;
  260. /* Query the PLL number routed to Clock B. */
  261. val = Cy2239xGetPll(NUT_PLL_NPLCLK1);
  262. /* Get the frequency of this PLL. */
  263. val = Cy2239xPllGetFreq((int)val, 7);
  264. /* Calculate the required divider value. */
  265. val = (val + NPL_MMC_CLOCK - 10) / NPL_MMC_CLOCK;
  266. /*
  267. * Not sure about the Cy-routines. The DIVSEL bit specifies which
  268. * divider is used, which is indirectly connected to S2, which is
  269. * high by default. For now set both dividers.
  270. */
  271. if (Cy2239xSetDivider(NUT_PLL_NPLCLK1, 1, (int)val)) {
  272. return -1;
  273. }
  274. if (Cy2239xSetDivider(NUT_PLL_NPLCLK1, 0, (int)val)) {
  275. return -1;
  276. }
  277. }
  278. #endif
  279. /* Initialize the CPLD SPI register. */
  280. inb(NPL_MMCDR);
  281. outb(NPL_MMCDR, 0xFF);
  282. /* Register card detection interrupts. */
  283. if ((rc = NplRegisterIrqHandler(&sig_MMCD, NplMmCard0InsIrq, 0)) == 0) {
  284. rc = NplRegisterIrqHandler(&sig_NMMCD, NplMmCard0RemIrq, 0);
  285. }
  286. NplIrqEnable(&sig_MMCD);
  287. return MmCardDevInit(dev);
  288. }
  289. static MMCIFC mmc0_ifc = {
  290. NplMmCard0Init, /*!< mmcifc_in */
  291. NplMmCard0Io, /*!< mmcifc_io */
  292. NplMmCard0Select, /*!< mmcifc_cs */
  293. NplMmCard0Avail, /*!< mmcifc_cd */
  294. NplMmCard0WrProt, /*!< mmcifc_wp */
  295. NplMmCard0SetAdrMode, /*!< mmcifc_sm */
  296. NplMmCard0GetAdrMode /*!< mmcifc_gm */
  297. };
  298. /*!
  299. * \brief Multimedia card device information structure.
  300. *
  301. * A pointer to this structure must be passed to NutRegisterDevice()
  302. * to bind this driver to the Nut/OS kernel. An application may then
  303. * call
  304. * /verbatim
  305. * _open("MMC0:", _O_RDWR | _O_BINARY);
  306. * /endverbatim
  307. * to mount the first active primary partition with any previously
  308. * registered file system driver (typically devPhat0).
  309. */
  310. NUTDEVICE devNplMmc0 = {
  311. 0, /*!< Pointer to next device, dev_next. */
  312. {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
  313. , /*!< Unique device name, dev_name. */
  314. 0, /*!< Type of device, dev_type. Obsolete. */
  315. 0, /*!< Base address, dev_base. Unused. */
  316. 0, /*!< First interrupt number, dev_irq. Unused. */
  317. &mmc0_ifc, /*!< Interface control block, dev_icb. */
  318. &mmc0_dcb, /*!< Driver control block used by the low level part, dev_dcb. */
  319. NplMmcIfcInit, /*!< Driver initialization routine, dev_init. */
  320. MmCardIOCtl, /*!< Driver specific control function, dev_ioctl. */
  321. MmCardBlockRead, /*!< Read data from a file, dev_read. */
  322. MmCardBlockWrite, /*!< Write data to a file, dev_write. */
  323. #ifdef __HARVARD_ARCH__
  324. MmCardBlockWrite_P, /*!< Write data from program space to a file, dev_write_P. */
  325. #endif
  326. MmCardMount, /*!< Mount a file system, dev_open. */
  327. MmCardUnmount, /*!< Unmount a file system, dev_close. */
  328. 0, /*!< Return file size, dev_size. */
  329. 0, /*!< Select function, optional, not yet implemented */
  330. };
  331. /*@}*/