ufs_aio_utils.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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) 2015. 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. #ifndef _UFS_AIO_UTILS_H
  32. #define _UFS_AIO_UTILS_H
  33. #include "ufs_aio_cfg.h"
  34. #include "ufs_aio_types.h"
  35. #include "ufs_aio.h"
  36. #include "ufs_aio_hcd.h"
  37. #if defined(MTK_UFS_DRV_LK)
  38. #include <debug.h>
  39. #include <string.h> //For memcpy, memset
  40. #include <mt_gpt.h>
  41. #endif
  42. #if defined(MTK_UFS_DRV_DA)
  43. #include <debug.h>
  44. #include <string.h> //For memcpy, memset
  45. #endif
  46. #if defined(MTK_UFS_DRV_PRELOADER)
  47. #include "pal_log.h" /* For print */
  48. #endif
  49. /* Byte order for UTF-16 strings */
  50. enum utf16_endian {
  51. UTF16_HOST_ENDIAN,
  52. UTF16_LITTLE_ENDIAN,
  53. UTF16_BIG_ENDIAN
  54. };
  55. extern struct ufs_ocs_error g_ufs_ocs_error_str[];
  56. void ufs_mtk_dump_asc_ascq(struct ufs_hba *hba, u8 asc, u8 ascq);
  57. /**
  58. * printing
  59. */
  60. #undef MSG
  61. #define MSG(evt, fmt, args...)
  62. #define MSG_FUNC(f)
  63. #undef BUG_ON
  64. #define BUG_ON(x) \
  65. do { \
  66. if (x) { \
  67. while(1); \
  68. } \
  69. }while(0)
  70. #undef WARN_ON
  71. #define WARN_ON(x) \
  72. do { \
  73. if (x) { \
  74. MSG(WRN, "[WARN] %s LINE:%d\n", #x, __LINE__); \
  75. } \
  76. }while(0)
  77. #if defined(MTK_UFS_DRV_DA)
  78. /*
  79. * In DA, LOGE and LOGI will always print logs.
  80. * Use LOGE here and ensure log control is by UFS driver.
  81. */
  82. #define printf LOGE
  83. #endif
  84. #if defined(MTK_UFS_DRV_LK)
  85. #undef printf
  86. #define printf(fmt, args...) dprintf(CRITICAL, fmt, ##args)
  87. #endif
  88. #if defined(MTK_UFS_DRV_PRELOADER)
  89. #undef printf
  90. #define printf pal_log_info
  91. #endif
  92. #ifndef printf
  93. #define printf(fmt, args...) do{} while(0)
  94. #endif
  95. /* Log level control, lower UFS_DBG_LVL means more log */
  96. #if (UFS_DBG_LVL <= UFS_DBG_LVL_FATAL)
  97. #define UFS_DBG_LOGF printf
  98. #else
  99. #define UFS_DBG_LOGF(...) do{} while(0)
  100. #endif
  101. #if (UFS_DBG_LVL <= UFS_DBG_LVL_CRITICAL)
  102. #define UFS_DBG_LOGE printf
  103. #else
  104. #define UFS_DBG_LOGE(...) do{} while(0)
  105. #endif
  106. #if (UFS_DBG_LVL <= UFS_DBG_LVL_INFO)
  107. #define UFS_DBG_LOGI printf
  108. #else
  109. #define UFS_DBG_LOGI(...) do{} while(0)
  110. #endif
  111. #if (UFS_DBG_LVL <= UFS_DBG_LVL_DEBUG)
  112. #define UFS_DBG_LOGD printf
  113. #else
  114. #define UFS_DBG_LOGD(...) do{} while(0)
  115. #endif
  116. #if (UFS_DBG_LVL <= UFS_DBG_LVL_VERBOSE)
  117. #define UFS_DBG_LOGV printf
  118. #else
  119. #define UFS_DBG_LOGV(...) do{} while(0)
  120. #endif
  121. #define UFS_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  122. /**
  123. * min, max tool
  124. */
  125. #ifndef min_t
  126. #define min_t(type, x, y) ({ \
  127. type __min1 = (x); \
  128. type __min2 = (y); \
  129. __min1 < __min2 ? __min1: __min2; })
  130. #endif
  131. /**
  132. * upper_32_bits - return bits 32-63 of a number
  133. * @n: the number we're accessing
  134. *
  135. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  136. * the "right shift count >= width of type" warning when that quantity is
  137. * 32-bits.
  138. */
  139. #ifndef upper_32_bits
  140. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  141. #endif
  142. /**
  143. * lower_32_bits - return bits 0-31 of a number
  144. * @n: the number we're accessing
  145. */
  146. #ifndef lower_32_bits
  147. #define lower_32_bits(n) ((u32)(n))
  148. #endif
  149. /**
  150. * for sleep and delay (mdelay and udelay)
  151. */
  152. #if defined(MTK_UFS_DRV_PRELOADER)
  153. #include "timer.h"
  154. #elif defined(MTK_UFS_DRV_LK)
  155. #include "mt_gpt.h"
  156. #elif defined(MTK_UFS_DRV_DA)
  157. #include <dev/gpt_timer/gpt_timer.h>
  158. #endif
  159. #ifndef msleep
  160. #define msleep mdelay
  161. #endif
  162. #ifndef usleep
  163. #define usleep udelay
  164. #endif
  165. #ifndef swab16
  166. #define swab16(x) \
  167. ((u16)( \
  168. (((u16)(x) & (u16)0x00ffU) << 8) | \
  169. (((u16)(x) & (u16)0xff00U) >> 8) ))
  170. #endif
  171. #ifndef swab32
  172. #define swab32(x) \
  173. ((u32)( \
  174. (((u32)(x) & (u32)0x000000ffUL) << 24) | \
  175. (((u32)(x) & (u32)0x0000ff00UL) << 8) | \
  176. (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \
  177. (((u32)(x) & (u32)0xff000000UL) >> 24) ))
  178. #endif
  179. #ifndef swab64
  180. #define swab64(x) \
  181. ((u64)( \
  182. (((u64)(x) & (u64)0x00000000000000ffULL) << 56) | \
  183. (((u64)(x) & (u64)0x000000000000ff00ULL) << 40) | \
  184. (((u64)(x) & (u64)0x0000000000ff0000ULL) << 24) | \
  185. (((u64)(x) & (u64)0x00000000ff000000ULL) << 8) | \
  186. (((u64)(x) & (u64)0x000000ff00000000ULL) >> 8) | \
  187. (((u64)(x) & (u64)0x0000ff0000000000ULL) >> 24) | \
  188. (((u64)(x) & (u64)0x00ff000000000000ULL) >> 40) | \
  189. (((u64)(x) & (u64)0xff00000000000000ULL) >> 56) ))
  190. #endif
  191. // must ensure we are using little endian CPU
  192. #ifndef cpu_to_be16
  193. #define cpu_to_be16(x) ({ u16 _x = x; swab16(_x); })
  194. #endif
  195. #ifndef cpu_to_be32
  196. #define cpu_to_be32(x) ({ u32 _x = x; swab32(_x); })
  197. #endif
  198. #ifndef cpu_to_be64
  199. #define cpu_to_be64(x) ({ u64 _x = x; swab64(_x); })
  200. #endif
  201. #ifndef be16_to_cpu
  202. #define be16_to_cpu(x) cpu_to_be16(x)
  203. #endif
  204. #ifndef be32_to_cpu
  205. #define be32_to_cpu(x) cpu_to_be32(x)
  206. #endif
  207. #ifndef be64_to_cpu
  208. #define be64_to_cpu(x) cpu_to_be64(x)
  209. #endif
  210. #ifndef cpu_to_le16
  211. #define cpu_to_le16(x) ((u16)(x))
  212. #endif
  213. #ifndef cpu_to_le32
  214. #define cpu_to_le32(x) ((u32)(x))
  215. #endif
  216. #ifndef le16_to_cpu
  217. #define le16_to_cpu(x) ((u16)(x))
  218. #endif
  219. #ifndef le32_to_cpu
  220. #define le32_to_cpu(x) ((u32)(x))
  221. #endif
  222. int utf16s_to_utf8s(const wchar_t *pwcs, int inlen, enum utf16_endian endian, u8 *s, int maxout);
  223. int ufs_util_sanitize_inquiry_string(unsigned char *s, int len);
  224. void itoa(char **buf, int i, int base);
  225. #if defined(MTK_UFS_DRV_DA)
  226. int strcmp(const char *cs, const char *ct);
  227. #endif
  228. #if defined(MTK_UFS_DRV_CTP)
  229. int strncmp(const char *s1, const char *s2, size_t n);
  230. #endif
  231. #endif /* _UFS_AIO_UTILS_H */