msdc_utils.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2018. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #ifndef _MSDC_UTILS_H_
  36. #define _MSDC_UTILS_H_
  37. #include "msdc_cfg.h"
  38. #include "mmc_types.h"
  39. #if defined(MMC_MSDC_DRV_CTP)
  40. #include <common.h>
  41. #endif
  42. #if defined(MMC_MSDC_DRV_LK)
  43. #include <debug.h>
  44. #include <string.h> //For memcpy, memset
  45. #include <mt_gpt.h>
  46. #include <malloc.h>
  47. #endif
  48. /* Print function */
  49. #ifndef printf
  50. #define printf(fmt, args...) do{}while(0)
  51. #endif
  52. #if defined(MMC_MSDC_DRV_PRELOADER)
  53. #include "pal_log.h"
  54. #ifdef pal_log_err
  55. #define msdc_pr_err(fmt, args...) pal_log_err(fmt, ##args)
  56. #else
  57. #define msdc_pr_err(fmt, args...) print(fmt, ##args)
  58. #endif
  59. #ifdef pal_log_info
  60. #define msdc_pr_info(fmt, args...) pal_log_info(fmt, ##args)
  61. #else
  62. #define msdc_pr_info(fmt, args...) print(fmt, ##args)
  63. #endif
  64. #endif
  65. #if defined(MMC_MSDC_DRV_LK)
  66. #define msdc_pr_err(fmt, args...) dprintf(CRITICAL, fmt, ##args)
  67. #define msdc_pr_info(fmt, args...) dprintf(INFO, fmt, ##args)
  68. #endif
  69. #if defined(MMC_MSDC_DRV_CTP)
  70. #define msdc_pr_err(fmt, args...) printf(fmt, ##args)
  71. #define msdc_pr_info(fmt, args...) MSG(INF, fmt, ##args)
  72. #endif
  73. /* Debug message event */
  74. #define MSG_EVT_NONE 0x00000000 /* No event */
  75. #define MSG_EVT_DMA 0x00000001 /* DMA related event */
  76. #define MSG_EVT_CMD 0x00000002 /* MSDC CMD related event */
  77. #define MSG_EVT_RSP 0x00000004 /* MSDC CMD RSP related event */
  78. #define MSG_EVT_INT 0x00000008 /* MSDC INT event */
  79. #define MSG_EVT_CFG 0x00000010 /* MSDC CFG event */
  80. #define MSG_EVT_FUC 0x00000020 /* Function event */
  81. #define MSG_EVT_OPS 0x00000040 /* Read/Write operation event */
  82. #define MSG_EVT_FIO 0x00000080 /* FIFO operation event */
  83. #define MSG_EVT_PWR 0x00000100 /* MSDC power related event */
  84. #define MSG_EVT_INF 0x01000000 /* information event */
  85. #define MSG_EVT_WRN 0x02000000 /* Warning event */
  86. #define MSG_EVT_ERR 0x04000000 /* Error event */
  87. #define MSG_EVT_ALL 0xffffffff
  88. //#define MSG_EVT_MASK (MSG_EVT_ALL & ~MSG_EVT_DMA & ~MSG_EVT_WRN & ~MSG_EVT_RSP & ~MSG_EVT_INT & ~MSG_EVT_CMD & ~MSG_EVT_OPS)
  89. //#define MSG_EVT_MASK (MSG_EVT_ALL & ~MSG_EVT_FIO)
  90. //#define MSG_EVT_MASK (MSG_EVT_FIO)
  91. //#define MSG_EVT_MASK (MSG_EVT_ALL)
  92. //#define MSG_EVT_MASK (MSG_EVT_ALL & ~MSG_EVT_DMA & ~MSG_EVT_OPS & ~MSG_EVT_INT & MSG_EVT_WRN)
  93. #define MSG_EVT_MASK (MSG_EVT_ALL & ~MSG_EVT_DMA & ~MSG_EVT_OPS & ~MSG_EVT_FIO)
  94. //#define MSG_EVT_MASK (MSG_EVT_ERR)
  95. #if (MSDC_SLT==1) && (MSDC_SLT_MASK_LOG == 1)
  96. #undef MSG_EVT_MASK
  97. #define MSG_EVT_MASK (MSG_EVT_ALL & ~MSG_EVT_DMA & ~MSG_EVT_OPS & ~MSG_EVT_INT & ~MSG_EVT_WRN & ~MSG_EVT_INF & ~MSG_EVT_CMD & ~MSG_EVT_RSP)
  98. #endif
  99. #undef MSG
  100. #ifdef MSG_DEBUG
  101. #define MSG(evt, fmt, args...) \
  102. do { \
  103. if ((MSG_EVT_##evt) & MSG_EVT_MASK) { \
  104. msdc_pr_err(fmt, ##args); \
  105. } \
  106. } while(0)
  107. #define MSG_FUNC(f) MSG(FUC, "<FUNC>: %s\n", __FUNCTION__)
  108. #else
  109. #define MSG(evt, fmt, args...)
  110. #define MSG_FUNC(f)
  111. #endif
  112. #undef BUG_ON
  113. #define BUG_ON(x) \
  114. do { \
  115. if (x) { \
  116. while(1); \
  117. } \
  118. }while(0)
  119. #undef WARN_ON
  120. #define WARN_ON(x) \
  121. do { \
  122. if (x) { \
  123. MSG(WRN, "[WARN] %s LINE:%d\n", #x, __LINE__); \
  124. } \
  125. }while(0)
  126. #define ERR_EXIT(expr, ret, expected_ret) \
  127. do { \
  128. (ret) = (expr);\
  129. if ((ret) != (expected_ret)) { \
  130. goto exit; \
  131. } \
  132. } while(0)
  133. #undef ARRAY_SIZE
  134. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  135. extern unsigned int msdc_uffs(unsigned int x);
  136. extern unsigned int msdc_ntohl(unsigned int n);
  137. extern void msdc_get_field(volatile u32 *reg, u32 field, u32 *val);
  138. #define uffs(x) msdc_uffs(x)
  139. #define ntohl(n) msdc_ntohl(n)
  140. #define get_field(r,f,v) msdc_get_field((volatile u32*)r,f,&v)
  141. #if defined(MMC_MSDC_DRV_CTP)
  142. #include <kal_mem.h>
  143. #define memcpy(dst,src,sz) KAL_memcpy(dst, src, sz)
  144. #define memset(p,v,s) KAL_memset(p, v, s)
  145. #define free(p) KAL_free(p)
  146. #define malloc(sz) KAL_malloc(sz, 4, KAL_USER_MSDC)
  147. #endif
  148. #ifndef min
  149. #define min(x, y) (x < y ? x : y)
  150. #endif
  151. #ifndef max
  152. #define max(x, y) (x > y ? x : y)
  153. #endif
  154. #if defined(MMC_MSDC_DRV_CTP)
  155. #if !defined(FPGA_PLATFORM)
  156. #define udelay(us) do{GPT_Delay_us(us);}while(0)
  157. #define mdelay(ms) do{GPT_Delay_ms(ms);}while(0)
  158. #else
  159. #define udelay(us) \
  160. do { \
  161. volatile int count = us * 10; \
  162. while (count--); \
  163. }while(0)
  164. #define udelay1(us) \
  165. do{ \
  166. MSDC_WRITE32(0x10008040,0x31);\
  167. MSDC_WRITE32(0x10008044,0x0);\
  168. u32 test_t1 = MSDC_READ32(0x10008048); \
  169. u32 test_t2; \
  170. do{ \
  171. test_t2= MSDC_READ32(0x10008048); \
  172. } \
  173. while((test_t2-test_t1) < us * 6);\
  174. }while(0)
  175. #define mdelay(ms) \
  176. do { \
  177. unsigned long i; \
  178. for (i = 0; i < ms; i++) \
  179. udelay(1000); \
  180. }while(0)
  181. #define mdelay1(ms) \
  182. do { \
  183. unsigned long i; \
  184. for (i = 0; i < ms; i++) \
  185. udelay1(1000); \
  186. }while(0)
  187. #endif
  188. #endif
  189. #define WAIT_COND(cond,tmo,left) \
  190. do { \
  191. u32 t = tmo; \
  192. while (1) { \
  193. if ((cond) || (t == 0)) break; \
  194. if (t > 0) {udelay(1); t--;} \
  195. } \
  196. left = t; \
  197. }while(0)
  198. #endif /* _MSDC_UTILS_H_ */