lpc177x_8x_emc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #ifndef _LPC177X_8X_EMC_H_
  2. #define _LPC177X_8X_EMC_H_
  3. /*
  4. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. *
  36. *
  37. * Parts taken from lpc177x_8x_emc.h 2011-06-02
  38. *
  39. * file lpc177x_8x_emc.h
  40. * brief Contains all macro definitions and function prototypes
  41. * support for EMC firmware library on LPC177x_8x
  42. * version 1.0
  43. * date 02. June. 2011
  44. * author NXP MCU SW Application Team
  45. *
  46. * Copyright(C) 2011, NXP Semiconductor
  47. * All rights reserved.
  48. *
  49. ***********************************************************************
  50. * Software that is described herein is for illustrative purposes only
  51. * which provides customers with programming information regarding the
  52. * products. This software is supplied "AS IS" without any warranties.
  53. * NXP Semiconductors assumes no responsibility or liability for the
  54. * use of the software, conveys no license or title under any patent,
  55. * copyright, or mask work right to the product. NXP Semiconductors
  56. * reserves the right to make changes in the software without
  57. * notification. NXP Semiconductors also make no representation or
  58. * warranty that such application will be suitable for the specified
  59. * use without further testing or modification.
  60. **********************************************************************/
  61. #include <inttypes.h>
  62. #include <dev/sdram.h>
  63. /*----------------------------------------------------------------------------*
  64. External memory controller settings and functions
  65. *----------------------------------------------------------------------------*/
  66. /*----------------------------------------------------------------------------*
  67. EMC Control Register (EMCControl)
  68. *----------------------------------------------------------------------------*/
  69. #define EMC_Control_MASK ((uint32_t) 0x07) /* Control register mask */
  70. #define EMC_Control_E ((uint32_t)(1<<0)) /* Control register EMC: Enable control. */
  71. #define EMC_Control_M ((uint32_t)(1<<1)) /* Control register EMC: Address mirror control. */
  72. #define EMC_Control_L ((uint32_t)(1<<2)) /* Control register EMC: Low-power mode control. */
  73. /*----------------------------------------------------------------------------*
  74. EMC Status Register (EMCStatus)
  75. *----------------------------------------------------------------------------*/
  76. #define EMC_Status_MASK ((uint32_t) 0x07) /* Status register mask */
  77. #define EMC_Status_B ((uint32_t)(1<<0)) /* Status register EMC: Busy. */
  78. #define EMC_Status_S ((uint32_t)(1<<1)) /* Status register EMC: Write buffer status. */
  79. #define EMC_Status_SA ((uint32_t)(1<<2)) /* Status register EMC: Self-refresh acknowledge.. */
  80. /*----------------------------------------------------------------------------*
  81. EMC Configuration register (EMCConfig)
  82. *----------------------------------------------------------------------------*/
  83. #define EMC_Config_Endian_Mode ((uint32_t)(1<<0)) /* EMC Configuration register : Enable control. */
  84. #define EMC_Config_CCLK ((uint32_t)(1<<8)) /* EMC Configuration register: CCLK. */
  85. #define EMC_Config_MASK ((uint32_t)(0x101)) /* EMC Configuration register mask */
  86. /*----------------------------------------------------------------------------*
  87. Dynamic Memory Control register (EMCDynamicControl)
  88. *----------------------------------------------------------------------------*/
  89. #define EMC_DynamicControl_CE ((uint32_t)(1<<0)) /* Dynamic Memory Control register EMC: Dynamic memory clock enable. */
  90. #define EMC_DynamicControl_CS ((uint32_t)(1<<1)) /* Dynamic Memory Control register EMC: Dynamic memory clock control */
  91. #define EMC_DynamicControl_SR ((uint32_t)(1<<2)) /* Dynamic Memory Control register EMC: Self-refresh request, EMCSREFREQ*/
  92. #define EMC_DynamicControl_MMC ((uint32_t)(1<<5)) /* Dynamic Memory Control register EMC: Memory clock control (MMC)*/
  93. #define EMC_DynamicControl_I(n) ((uint32_t)(n<<7)) /* Dynamic Memory Control register EMC: SDRAM initialization*/
  94. #define EMC_DynamicControl_DP ((uint32_t)(1<<13)) /* Dynamic Memory Control register EMC: Low-power SDRAM deep-sleep mode (DP)*/
  95. /*----------------------------------------------------------------------------*
  96. Dynamic Memory Refresh Timer register (EMCDynamicRefresh)
  97. *----------------------------------------------------------------------------*/
  98. #define EMC_DynamicRefresh_REFRESH(n) ((uint32_t ) (n & 0x3ff)) /* Dynamic Memory Refresh Timer register EMC: Refresh timer (REFRESH) */
  99. /*----------------------------------------------------------------------------*
  100. Dynamic Memory Read Configuration register (EMCDynamicReadConfig)
  101. *----------------------------------------------------------------------------*/
  102. #define EMC_DynamicReadConfig_RD(n) ((uint32_t )(n & 0x03)) /* EMCDynamicReadConfig register EMC:Read data strategy (RD) */
  103. /*----------------------------------------------------------------------------*
  104. Dynamic Memory Percentage Command Period register (EMCDynamictRP)
  105. *----------------------------------------------------------------------------*/
  106. #define EMC_DynamictRP_tRP(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictRP register EMC: Precharge command period (tRP). */
  107. /*----------------------------------------------------------------------------*
  108. Dynamic Memory Active to Precharge Command Period register (EMCDynamictRAS)
  109. *----------------------------------------------------------------------------*/
  110. #define EMC_DynamictRP_tRAS(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictRAS register EMC: Active to precharge command period (tRAS) */
  111. /*----------------------------------------------------------------------------*
  112. Dynamic Memory Last Data Out to Active Time register (EMCDynamictAPR)
  113. *----------------------------------------------------------------------------*/
  114. #define EMC_DynamictAPR_tAPR(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictAPR register EMC: Last-data-out to active command time (tAPR) */
  115. /*----------------------------------------------------------------------------*
  116. Dynamic Memory Data-in to Active Command Time register (EMCDynamictDAL)
  117. *----------------------------------------------------------------------------*/
  118. #define EMC_DynamictDAL_tDAL(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictDAL register EMC: Data-in to active command (tDAL)*/
  119. /*----------------------------------------------------------------------------*
  120. Dynamic Memory Write Recovery Time register (EMCDynamictWR)
  121. *----------------------------------------------------------------------------*/
  122. #define EMC_DynamictWR_tWR(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictWR register EMC: Write recovery time (tWR)*/
  123. /*----------------------------------------------------------------------------*
  124. Dynamic Memory Active to Active Command Period register (EMCDynamictRC)
  125. *----------------------------------------------------------------------------*/
  126. #define EMC_DynamictRC_tRC(n) ((uint32_t )(n & 0x1f)) /* EMCDynamictRC register EMC: Active to active command period (tRC)*/
  127. /*----------------------------------------------------------------------------*
  128. Dynamic Memory Auto-refresh Period register (EMCDynamictRFC)
  129. *----------------------------------------------------------------------------*/
  130. #define EMC_DynamictRFC_tRFC(n) ((uint32_t )(n & 0x1f)) /* EMCDynamictRFC register EMC: Auto-refresh period and auto-refresh to active command period (tRFC)*/
  131. /*----------------------------------------------------------------------------*
  132. Dynamic Memory Exit Self-refresh register (EMCDynamictXSR)
  133. *----------------------------------------------------------------------------*/
  134. #define EMC_DynamictXSR_tXSR(n) ((uint32_t )(n & 0x1f)) /* EMCDynamictXSR register EMC: Exit self-refresh to active command time (tXSR)*/
  135. /*----------------------------------------------------------------------------*
  136. Dynamic Memory Active Bank A to Active Bank B Time register (EMCDynamictRRD)
  137. *----------------------------------------------------------------------------*/
  138. #define EMC_DynamictRRD_tRRD(n) ((uint32_t )(n & 0x0f)) /* EMCDynamictRRD register EMC: Active bank A to active bank B latency (tRRD )*/
  139. /*----------------------------------------------------------------------------*
  140. Dynamic Memory Load Mode register to Active Command Time (EMCDynamictMRD)
  141. *----------------------------------------------------------------------------*/
  142. #define EMC_DynamictMRD_tMRD(n) ((uint32_t )(n & 0x1f)) /* EMCDynamictMRD register EMC: Load mode register to active command time (tMRD)*/
  143. /*----------------------------------------------------------------------------*
  144. Static Memory Extended Wait Register (EMCStaticExtendedWait)
  145. *----------------------------------------------------------------------------*/
  146. #define EMC_StaticExtendedWait_EXTENDEDWAIT(n) ((uint32_t )(n & 0x3ff)) /* StaticExtendedWait register EMC: External wait time out. */
  147. /*----------------------------------------------------------------------------*
  148. Dynamic Memory Configuration registers (EMCDynamicConfig0-3)
  149. *----------------------------------------------------------------------------*/
  150. #define EMC_DynamicConfig_MD(n) ((uint32_t )(n << 3)) /* DynamicConfig register EMC: Memory device (MD). */
  151. #define EMC_DynamicConfig_AM1(n) ((uint32_t )(n << 7)) /* DynamicConfig register EMC: Address mapping (AM) */
  152. #define EMC_DynamicConfig_AM2(n) ((uint32_t )(1 << 14)) /* DynamicConfig register EMC: Address mapping (AM) */
  153. #define EMC_DynamicConfig_B ((uint32_t )(1 << 19)) /* DynamicConfig register EMC: Buffer enable */
  154. #define EMC_DynamicConfig_P ((uint32_t )(1 << 20)) /* DynamicConfig register EMC: Write protect (P) */
  155. /*----------------------------------------------------------------------------*
  156. Dynamic Memory RAS & CAS Delay registers (EMCDynamicRASCAS0-3)
  157. *----------------------------------------------------------------------------*/
  158. #define EMC_DynamicConfig_RAS(n) ((uint32_t )(n & 0x03)) /* DynamicRASCAS register EMC: RAS latency (active to read/write delay) (RAS). */
  159. #define EMC_DynamicConfig_CAS(n) ((uint32_t )(n << 8)) /* DynamicRASCAS register EMC: CAS latency (CAS)*/
  160. /*----------------------------------------------------------------------------*
  161. Static Memory Configuration registers (EMCStaticConfig0-3)
  162. *----------------------------------------------------------------------------*/
  163. #define EMC_StaticConfig_MW(n) ((uint32_t )(n & 0x03)) /* StaticConfig register EMC: Memory width (MW). */
  164. #define EMC_StaticConfig_MW_8BITS (EMC_StaticConfig_MW(0)) /* StaticConfig register EMC: Memory width 8bit . */
  165. #define EMC_StaticConfig_MW_16BITS (EMC_StaticConfig_MW(1)) /* StaticConfig register EMC: Memory width 16bit . */
  166. #define EMC_StaticConfig_MW_32BITS (EMC_StaticConfig_MW(2)) /* StaticConfig register EMC: Memory width 32bit . */
  167. #define EMC_StaticConfig_PM ((uint32_t )(1 << 3)) /* StaticConfig register EMC: Page mode (PM) */
  168. #define EMC_StaticConfig_PC ((uint32_t )(1 << 6)) /* StaticConfig register EMC: Chip select polarity (PC) */
  169. #define EMC_StaticConfig_PB ((uint32_t )(1 << 7)) /* StaticConfig register EMC: Byte lane state (PB) */
  170. #define EMC_StaticConfig_EW ((uint32_t )(1 << 8)) /* StaticConfig register EMC: Extended wait (EW) */
  171. #define EMC_StaticConfig_B ((uint32_t )(1 << 19)) /* StaticConfig register EMC: Buffer enable (B) */
  172. #define EMC_StaticConfig_P ((uint32_t )(1 << 20)) /* StaticConfig register EMC: Write protect (P) */
  173. /*----------------------------------------------------------------------------*
  174. Static Memory Write Enable Delay registers (EMCStaticWaitWen0-3)
  175. *----------------------------------------------------------------------------*/
  176. #define EMC_StaticWaitWen_WAITWEN(n) ((uint32_t )(n & 0x0f)) /* StaticWaitWen register EMC: Wait write enable (WAITWEN). */
  177. /*----------------------------------------------------------------------------*
  178. Static Memory Output Enable Delay registers (EMCStaticWaitOen0-3)
  179. *----------------------------------------------------------------------------*/
  180. #define EMC_StaticWaitOen_WAITOEN(n) ((uint32_t )(n & 0x0f)) /* StaticWaitOen register EMC: Wait output enable (WAITOEN). */
  181. /*----------------------------------------------------------------------------*
  182. Static Memory Read Delay registers (EMCStaticWaitRd0-3)
  183. *----------------------------------------------------------------------------*/
  184. #define EMC_StaticWaitRd_WAITRD(n) ((uint32_t )(n & 0x1f)) /* StaticWaitRd register EMC: Non-page mode read wait
  185. states or asynchronous page mode read first access
  186. wait state (WAITRD) */
  187. /*----------------------------------------------------------------------------*
  188. Static Memory Page Mode Read Delay registers (EMCStaticwaitPage0-3)
  189. *----------------------------------------------------------------------------*/
  190. #define EMC_StaticwaitPage_WAITPAGE(n) ((uint32_t )(n & 0x1f)) /* StaticwaitPage register EMC: Asynchronous page mode
  191. read after the first read wait states (WAITPAGE). */
  192. /*----------------------------------------------------------------------------*
  193. Static Memory Write Delay registers (EMCStaticWaitwr0-3)
  194. *----------------------------------------------------------------------------*/
  195. #define EMC_StaticWaitwr_WAITWR(n) ((uint32_t )(n & 0x1f)) /* StaticWaitwr register EMC: Write wait states (WAITWR). */
  196. /*----------------------------------------------------------------------------*
  197. Static Memory Turn Round Delay registers (EMCStaticWaitTurn0-3)
  198. *----------------------------------------------------------------------------*/
  199. #define EMC_StaticWaitTurn_WAITTURN(n) ((uint32_t )(n & 0x0f)) /* StaticWaitTurn register EMC: Bus turnaround cycles (WAITTURN). */
  200. /*----------------------------------------------------------------------------*
  201. Delay Control register (EMCDLYCTL)
  202. *----------------------------------------------------------------------------*/
  203. #define EMC_DLYCTL_CMDDLY(n) ((uint32_t)(n&0x1F))
  204. #define EMC_DLYCTL_FBCLKDLY(n) ((uint32_t)((n&0x1F)<<8))
  205. #define EMC_DLYCTL_CLKOUT0DLY(n) ((uint32_t)((n&0x1F)<<16))
  206. #define EMC_DLYCTL_CLKOUT1DLY(n) ((uint32_t)((n&0x1F)<<24))
  207. /*----------------------------------------------------------------------------*
  208. EMC Calibration register (EMCCAL)
  209. *----------------------------------------------------------------------------*/
  210. #define EMC_CAL_CALVALUE(n) ((uint32_t)(n&0xFF))
  211. #define EMC_CAL_START ((uint32_t)(1<<14))
  212. #define EMC_CAL_DONE ((uint32_t)(1<<15))
  213. /*----------------------------------------------------------------------------*
  214. EMC endianess modes
  215. *----------------------------------------------------------------------------*/
  216. #define EMC_LITTLE_ENDIAN_MODE ((uint32_t)(0))
  217. #define EMC_BIG_ENDIAN_MODE ((uint32_t)(1))
  218. /*----------------------------------------------------------------------------*
  219. EMC dynamic memory registers enum
  220. *----------------------------------------------------------------------------*/
  221. typedef enum
  222. {
  223. EMC_DYN_MEM_REFRESH_TIMER,
  224. EMC_DYN_MEM_READ_CONFIG,
  225. EMC_DYN_MEM_TRP,
  226. EMC_DYN_MEM_TRAS,
  227. EMC_DYN_MEM_TSREX,
  228. EMC_DYN_MEM_TAPR,
  229. EMC_DYN_MEM_TDAL,
  230. EMC_DYN_MEM_TWR,
  231. EMC_DYN_MEM_TRC,
  232. EMC_DYN_MEM_TRFC,
  233. EMC_DYN_MEM_TXSR,
  234. EMC_DYN_MEM_TRRD,
  235. EMC_DYN_MEM_TMRD
  236. } EMC_DYN_MEM_PAR;
  237. /*----------------------------------------------------------------------------*
  238. EMC static memory registers enum
  239. *----------------------------------------------------------------------------*/
  240. typedef enum
  241. {
  242. EMC_STA_MEM_WAITWEN,
  243. EMC_STA_MEM_WAITOEN,
  244. EMC_STA_MEM_WAITRD,
  245. EMC_STA_MEM_WAITPAGE,
  246. EMC_STA_MEM_WAITWR,
  247. EMC_STA_MEM_WAITTURN,
  248. } EMC_STA_MEM_PAR;
  249. /*----------------------------------------------------------------------------*
  250. Public functions
  251. *----------------------------------------------------------------------------*/
  252. extern void Lpc177x_8x_EmcInit(void);
  253. extern void Lpc177x_8x_EmcSDRAMAdjustTiming(void);
  254. extern int Lpc177x_8x_EmcSDRAMCheck(SDRAM sdram, uint32_t offset);
  255. extern void Lpc177x_8x_EmcSDRAMInit(SDRAM sdram, uint32_t dynamic_config);
  256. extern void Lpc177x_8x_EmcConfigEndianMode(uint32_t endian_mode);
  257. extern void Lpc177x_8x_EmcDynCtrlClockEnable(uint32_t clock_enable);
  258. extern void Lpc177x_8x_EmcDynCtrlClockControl(uint32_t clock_control);
  259. extern void Lpc177x_8x_EmcDynCtrlSelfRefresh(uint32_t self_refresh_mode);
  260. extern void Lpc177x_8x_EmcDynCtrlMMC(uint32_t mmc_val);
  261. extern void Lpc177x_8x_EmcDynCtrlSDRAMCmd(uint32_t sdram_command);
  262. extern void Lpc177x_8x_EmcDynCtrlPowerDownMode(uint32_t power_command);
  263. extern void Lpc177x_8x_EmcSetDynMemoryParameter(EMC_DYN_MEM_PAR par, uint32_t val);
  264. extern void Lpc177x_8x_EmcStaticExtendedWait(uint32_t Extended_wait_time_out);
  265. extern void Lpc177x_8x_EmcDynMemConfigMD(uint32_t cs, uint32_t mem_dev);
  266. extern void Lpc177x_8x_EmcDynMemConfigAM(uint32_t cs, uint32_t addr_mapped);
  267. extern void Lpc177x_8x_EmcDynMemConfigB(uint32_t cs, uint32_t buff_control);
  268. extern void Lpc177x_8x_EmcDynMemConfigP(uint32_t cs, uint32_t permission);
  269. extern void Lpc177x_8x_EmcDynMemRAS(uint32_t cs, uint32_t ras_val);
  270. extern void Lpc177x_8x_EmcDynMemCAS(uint32_t cs, uint32_t cas_val);
  271. extern void Lpc177x_8x_EmcStaticMemConfigMW(uint32_t cs, uint32_t mem_width);
  272. extern void Lpc177x_8x_EmcStaticMemConfigPM(uint32_t cs, uint32_t page_mode);
  273. extern void Lpc177x_8x_EmcStaticMemConfigPC(uint32_t cs, uint32_t polarity);
  274. extern void Lpc177x_8x_EmcStaticMemConfigPB(uint32_t cs, uint32_t pb_val);
  275. extern void Lpc177x_8x_EmcStaticMemConfigEW(uint32_t cs, uint32_t ex_wait);
  276. extern void Lpc177x_8x_EmcStaticMemConfigB(uint32_t cs, uint32_t buf_val);
  277. extern void Lpc177x_8x_EmcStaticMemConfigP(uint32_t cs, uint32_t permission);
  278. extern void Lpc177x_8x_EmcSetStaticMemoryParameter(uint32_t cs, EMC_STA_MEM_PAR par, uint32_t val);
  279. #endif /* _LPC177X_8X_EMC_H_ */