btif_plat.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. #ifdef DFT_TAG
  2. #undef DFT_TAG
  3. #endif
  4. #define DFT_TAG "MTK-BTIF"
  5. #define NEW_TX_HANDLING_SUPPORT 1
  6. #include "btif_pub.h"
  7. #include "btif_priv.h"
  8. #define BTIF_USER_ID "btif_driver"
  9. static spinlock_t g_clk_cg_spinlock; /*BTIF clock's spinlock */
  10. /*-----------------------------BTIF Module Clock and Power Control Defination------------------*/
  11. MTK_BTIF_IRQ_STR mtk_btif_irq = {
  12. .name = "mtk btif irq",
  13. .is_irq_sup = true,
  14. .reg_flag = false,
  15. #ifdef CONFIG_OF
  16. .irq_flags = IRQF_TRIGGER_NONE,
  17. #else
  18. .irq_id = MT_BTIF_IRQ_ID,
  19. .sens_type = IRQ_SENS_LVL,
  20. .lvl_type = IRQ_LVL_LOW,
  21. #endif
  22. .p_irq_handler = NULL,
  23. };
  24. /*will call clock manager's API export by WCP to control BTIF's clock,
  25. but we may need to access these registers in case of btif clock control logic is wrong in clock manager*/
  26. MTK_BTIF_INFO_STR mtk_btif = {
  27. #ifndef CONFIG_OF
  28. .base = MTK_BTIF_REG_BASE,
  29. #endif
  30. .p_irq = &mtk_btif_irq,
  31. .tx_fifo_size = BTIF_TX_FIFO_SIZE,
  32. .rx_fifo_size = BTIF_RX_FIFO_SIZE,
  33. .tx_tri_lvl = BTIF_TX_FIFO_THRE,
  34. .rx_tri_lvl = BTIF_RX_FIFO_THRE,
  35. .rx_data_len = 0,
  36. .p_tx_fifo = NULL,
  37. };
  38. #if !(NEW_TX_HANDLING_SUPPORT)
  39. static bool _btif_is_tx_allow(P_MTK_BTIF_INFO_STR p_btif);
  40. #endif
  41. static int btif_rx_irq_handler(P_MTK_BTIF_INFO_STR p_btif_info,
  42. unsigned char *p_buf,
  43. const unsigned int max_len);
  44. static int btif_tx_irq_handler(P_MTK_BTIF_INFO_STR p_btif);
  45. /*****************************************************************************
  46. * FUNCTION
  47. * hal_btif_rx_ier_ctrl
  48. * DESCRIPTION
  49. * BTIF Rx interrupt enable/disable
  50. * PARAMETERS
  51. * p_base [IN] BTIF module's base address
  52. * enable [IN] control if rx interrupt enabled or not
  53. * RETURNS
  54. * 0 means success, negative means fail
  55. *****************************************************************************/
  56. static int hal_btif_rx_ier_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en);
  57. /*****************************************************************************
  58. * FUNCTION
  59. * hal_btif_tx_ier_ctrl
  60. * DESCRIPTION
  61. * BTIF Tx interrupt enable/disable
  62. * PARAMETERS
  63. * p_base [IN] BTIF module's base address
  64. * enable [IN] control if tx interrupt enabled or not
  65. * RETURNS
  66. * 0 means success, negative means fail
  67. *****************************************************************************/
  68. static int hal_btif_tx_ier_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en);
  69. #ifndef MTK_BTIF_MARK_UNUSED_API
  70. static int btif_sleep_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en);
  71. /*****************************************************************************
  72. * FUNCTION
  73. * _btif_receive_data
  74. * DESCRIPTION
  75. * receive data from btif module in FIFO polling mode
  76. * PARAMETERS
  77. * p_base [IN] BTIF module's base address
  78. * p_buf [IN/OUT] pointer to rx data buffer
  79. * max_len [IN] max length of rx buffer
  80. * RETURNS
  81. * positive means data is available, 0 means no data available
  82. *****************************************************************************/
  83. static int _btif_receive_data(P_MTK_BTIF_INFO_STR p_btif,
  84. unsigned char *p_buf, const unsigned int max_len);
  85. static int btif_tx_thr_set(P_MTK_BTIF_INFO_STR p_btif, unsigned int thr_count);
  86. #endif
  87. static int btif_dump_array(char *string, char *p_buf, int len)
  88. {
  89. unsigned int idx = 0;
  90. pr_debug("========dump %s start <length:%d>========\n", string, len);
  91. for (idx = 0; idx < len; idx++, p_buf++) {
  92. pr_debug("%02x ", *p_buf);
  93. if (3 == (idx % 4))
  94. pr_debug("\n");
  95. }
  96. pr_debug("\n");
  97. pr_debug("========dump %s end========\n", string);
  98. return 0;
  99. }
  100. #if NEW_TX_HANDLING_SUPPORT
  101. static int _btif_tx_fifo_init(P_MTK_BTIF_INFO_STR p_btif_info)
  102. {
  103. int i_ret = -1;
  104. spin_lock_init(&(p_btif_info->tx_fifo_spinlock));
  105. if (NULL == p_btif_info->p_tx_fifo) {
  106. p_btif_info->p_tx_fifo = kzalloc(sizeof(struct kfifo),
  107. GFP_ATOMIC);
  108. if (NULL == p_btif_info->p_tx_fifo) {
  109. i_ret = -ENOMEM;
  110. BTIF_ERR_FUNC("kzalloc for p_btif->p_tx_fifo failed\n");
  111. goto ret;
  112. }
  113. i_ret = kfifo_alloc(p_btif_info->p_tx_fifo,
  114. BTIF_HAL_TX_FIFO_SIZE, GFP_ATOMIC);
  115. if (0 != i_ret) {
  116. BTIF_ERR_FUNC("kfifo_alloc failed, errno(%d)\n", i_ret);
  117. i_ret = -ENOMEM;
  118. goto ret;
  119. }
  120. i_ret = 0;
  121. } else {
  122. BTIF_WARN_FUNC
  123. ("p_btif_info->p_tx_fifo is already init p_btif_info->p_tx_fifo(0x%p)\n",
  124. p_btif_info->p_tx_fifo);
  125. i_ret = 0;
  126. }
  127. ret:
  128. return i_ret;
  129. }
  130. static int _get_btif_tx_fifo_room(P_MTK_BTIF_INFO_STR p_btif_info)
  131. {
  132. int i_ret = 0;
  133. unsigned long flag = 0;
  134. spin_lock_irqsave(&(p_btif_info->tx_fifo_spinlock), flag);
  135. if (NULL == p_btif_info->p_tx_fifo)
  136. i_ret = 0;
  137. else
  138. i_ret = kfifo_avail(p_btif_info->p_tx_fifo);
  139. spin_unlock_irqrestore(&(p_btif_info->tx_fifo_spinlock), flag);
  140. BTIF_DBG_FUNC("tx kfifo:0x%p, available room:%d\n", p_btif_info->p_tx_fifo, i_ret);
  141. return i_ret;
  142. }
  143. static int _btif_tx_fifo_reset(P_MTK_BTIF_INFO_STR p_btif_info)
  144. {
  145. int i_ret = 0;
  146. if (NULL != p_btif_info->p_tx_fifo)
  147. kfifo_reset(p_btif_info->p_tx_fifo);
  148. return i_ret;
  149. }
  150. #endif
  151. #ifdef CONFIG_OF
  152. static void _btif_set_default_setting(void)
  153. {
  154. struct device_node *node = NULL;
  155. unsigned int irq_info[3] = {0, 0, 0};
  156. unsigned int phy_base;
  157. node = of_find_compatible_node(NULL, NULL, "mediatek,btif");
  158. if (node) {
  159. mtk_btif.p_irq->irq_id = irq_of_parse_and_map(node, 0);
  160. /*fixme, be compitable arch 64bits*/
  161. mtk_btif.base = (unsigned long)of_iomap(node, 0);
  162. BTIF_INFO_FUNC("get btif irq(%d),register base(0x%lx)\n",
  163. mtk_btif.p_irq->irq_id, mtk_btif.base);
  164. } else {
  165. BTIF_ERR_FUNC("get btif device node fail\n");
  166. }
  167. /* get the interrupt line behaviour */
  168. if (of_property_read_u32_array(node, "interrupts",
  169. irq_info, ARRAY_SIZE(irq_info))) {
  170. BTIF_ERR_FUNC("get interrupt flag from DTS fail\n");
  171. } else {
  172. mtk_btif.p_irq->irq_flags = irq_info[2];
  173. BTIF_INFO_FUNC("get interrupt flag(0x%x)\n", mtk_btif.p_irq->irq_flags);
  174. }
  175. if (of_property_read_u32_index(node, "reg", 0, &phy_base))
  176. BTIF_ERR_FUNC("get register phy base from DTS fail\n");
  177. else
  178. BTIF_INFO_FUNC("get register phy base(0x%x)\n", (unsigned int)phy_base);
  179. }
  180. #endif
  181. /*****************************************************************************
  182. * FUNCTION
  183. * hal_btif_info_get
  184. * DESCRIPTION
  185. * get btif's information included base address , irq related information
  186. * PARAMETERS
  187. * RETURNS
  188. * BTIF's information
  189. *****************************************************************************/
  190. P_MTK_BTIF_INFO_STR hal_btif_info_get(void)
  191. {
  192. #if NEW_TX_HANDLING_SUPPORT
  193. int i_ret = 0;
  194. /*tx fifo and fifo lock init*/
  195. i_ret = _btif_tx_fifo_init(&mtk_btif);
  196. if (0 == i_ret)
  197. BTIF_INFO_FUNC("_btif_tx_fifo_init succeed\n");
  198. else
  199. BTIF_ERR_FUNC("_btif_tx_fifo_init failed, i_ret:%d\n", i_ret);
  200. #endif
  201. #ifdef CONFIG_OF
  202. _btif_set_default_setting();
  203. #endif
  204. spin_lock_init(&g_clk_cg_spinlock);
  205. return &mtk_btif;
  206. }
  207. /*****************************************************************************
  208. * FUNCTION
  209. * hal_btif_clk_get_and_prepare
  210. * DESCRIPTION
  211. * get clock from device tree and prepare for enable/disable control
  212. * PARAMETERS
  213. * pdev device pointer
  214. * RETURNS
  215. * 0 means success, negative means fail
  216. *****************************************************************************/
  217. #if !defined(CONFIG_MTK_CLKMGR)
  218. int hal_btif_clk_get_and_prepare(struct platform_device *pdev)
  219. {
  220. int i_ret = -1;
  221. clk_btif = devm_clk_get(&pdev->dev, "btifc");
  222. if (IS_ERR(clk_btif)) {
  223. BTIF_ERR_FUNC("[CCF]cannot get clk_btif clock.\n");
  224. return PTR_ERR(clk_btif);
  225. }
  226. BTIF_ERR_FUNC("[CCF]clk_btif=%p\n", clk_btif);
  227. clk_btif_apdma = devm_clk_get(&pdev->dev, "apdmac");
  228. if (IS_ERR(clk_btif_apdma)) {
  229. BTIF_ERR_FUNC("[CCF]cannot get clk_btif_apdma clock.\n");
  230. return PTR_ERR(clk_btif_apdma);
  231. }
  232. BTIF_ERR_FUNC("[CCF]clk_btif_apdma=%p\n", clk_btif_apdma);
  233. i_ret = clk_prepare(clk_btif);
  234. if (0 != i_ret) {
  235. BTIF_ERR_FUNC("clk_prepare clk_btif failed! ret:%d\n", i_ret);
  236. return i_ret;
  237. }
  238. i_ret = clk_prepare(clk_btif_apdma);
  239. if (0 != i_ret) {
  240. BTIF_ERR_FUNC("clk_prepare clk_btif_apdma failed! ret:%d\n", i_ret);
  241. return i_ret;
  242. }
  243. return i_ret;
  244. }
  245. /*****************************************************************************
  246. * FUNCTION
  247. * hal_btif_clk_unprepare
  248. * DESCRIPTION
  249. * unprepare btif clock and apdma clock
  250. * PARAMETERS
  251. * none
  252. * RETURNS
  253. * 0 means success, negative means fail
  254. *****************************************************************************/
  255. int hal_btif_clk_unprepare(void)
  256. {
  257. clk_unprepare(clk_btif);
  258. clk_unprepare(clk_btif_apdma);
  259. return 0;
  260. }
  261. #endif
  262. /*****************************************************************************
  263. * FUNCTION
  264. * hal_btif_clk_ctrl
  265. * DESCRIPTION
  266. * control clock output enable/disable of BTIF module
  267. * PARAMETERS
  268. * p_base [IN] BTIF module's base address
  269. * RETURNS
  270. * 0 means success, negative means fail
  271. *****************************************************************************/
  272. int hal_btif_clk_ctrl(P_MTK_BTIF_INFO_STR p_btif, ENUM_CLOCK_CTRL flag)
  273. {
  274. /*In MTK BTIF, there's only one global CG on AP_DMA, no sub channel's CG bit*/
  275. /*according to Artis's comment, clock of DMA and BTIF is default off, so we assume it to be off by default*/
  276. int i_ret = 0;
  277. unsigned long irq_flag = 0;
  278. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  279. static atomic_t s_clk_ref = ATOMIC_INIT(0);
  280. #else
  281. static ENUM_CLOCK_CTRL status = CLK_OUT_DISABLE;
  282. #endif
  283. spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);
  284. #if MTK_BTIF_ENABLE_CLK_CTL
  285. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  286. if (CLK_OUT_ENABLE == flag) {
  287. if (1 == atomic_inc_return(&s_clk_ref)) {
  288. #if defined(CONFIG_MTK_CLKMGR)
  289. i_ret = enable_clock(MTK_BTIF_CG_BIT, BTIF_USER_ID);
  290. #else
  291. BTIF_DBG_FUNC("[CCF]enable clk_btif\n");
  292. i_ret = clk_enable(clk_btif);
  293. #endif /* defined(CONFIG_MTK_CLKMGR) */
  294. if (i_ret) {
  295. BTIF_WARN_FUNC
  296. ("enable_clock for MTK_BTIF_CG_BIT failed, ret:%d",
  297. i_ret);
  298. }
  299. }
  300. } else if (CLK_OUT_DISABLE == flag) {
  301. if (0 == atomic_dec_return(&s_clk_ref)) {
  302. #if defined(CONFIG_MTK_CLKMGR)
  303. i_ret = disable_clock(MTK_BTIF_CG_BIT, BTIF_USER_ID);
  304. if (i_ret) {
  305. BTIF_WARN_FUNC
  306. ("disable_clock for MTK_BTIF_CG_BIT failed, ret:%d",
  307. i_ret);
  308. }
  309. #else
  310. BTIF_DBG_FUNC("[CCF] clk_disable(clk_btif) calling\n");
  311. clk_disable(clk_btif);
  312. #endif /* defined(CONFIG_MTK_CLKMGR) */
  313. }
  314. } else {
  315. i_ret = ERR_INVALID_PAR;
  316. BTIF_ERR_FUNC("invalid clock ctrl flag (%d)\n", flag);
  317. }
  318. #else
  319. if (status == flag) {
  320. i_ret = 0;
  321. BTIF_DBG_FUNC("btif clock already %s\n",
  322. CLK_OUT_ENABLE ==
  323. status ? "enabled" : "disabled");
  324. } else {
  325. if (CLK_OUT_ENABLE == flag) {
  326. #if defined(CONFIG_MTK_CLKMGR)
  327. i_ret = enable_clock(MTK_BTIF_CG_BIT, BTIF_USER_ID);
  328. #else
  329. BTIF_DBG_FUNC("[CCF]enable clk_btif\n");
  330. i_ret = clk_enable(clk_btif);
  331. #endif /* defined(CONFIG_MTK_CLKMGR) */
  332. status = (0 == i_ret) ? flag : status;
  333. if (i_ret) {
  334. BTIF_WARN_FUNC
  335. ("enable_clock for MTK_BTIF_CG_BIT failed, ret:%d",
  336. i_ret);
  337. }
  338. } else if (CLK_OUT_DISABLE == flag) {
  339. #if defined(CONFIG_MTK_CLKMGR)
  340. i_ret = disable_clock(MTK_BTIF_CG_BIT, BTIF_USER_ID);
  341. status = (0 == i_ret) ? flag : status;
  342. if (i_ret) {
  343. BTIF_WARN_FUNC
  344. ("disable_clock for MTK_BTIF_CG_BIT failed, ret:%d",
  345. i_ret);
  346. }
  347. #else
  348. BTIF_DBG_FUNC("[CCF] clk_disable(clk_btif) calling\n");
  349. clk_disable(clk_btif);
  350. #endif /* defined(CONFIG_MTK_CLKMGR) */
  351. } else {
  352. i_ret = ERR_INVALID_PAR;
  353. BTIF_ERR_FUNC("invalid clock ctrl flag (%d)\n", flag);
  354. }
  355. }
  356. #endif
  357. #else
  358. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  359. #else
  360. status = flag;
  361. #endif
  362. i_ret = 0;
  363. #endif
  364. spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);
  365. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  366. if (0 == i_ret) {
  367. BTIF_DBG_FUNC("btif clock %s\n",
  368. CLK_OUT_ENABLE == flag ? "enabled" : "disabled");
  369. } else {
  370. BTIF_ERR_FUNC("%s btif clock failed, ret(%d)\n",
  371. CLK_OUT_ENABLE == flag ? "enable" : "disable",
  372. i_ret);
  373. }
  374. #else
  375. if (0 == i_ret) {
  376. BTIF_DBG_FUNC("btif clock %s\n",
  377. CLK_OUT_ENABLE == flag ? "enabled" : "disabled");
  378. } else {
  379. BTIF_ERR_FUNC("%s btif clock failed, ret(%d)\n",
  380. CLK_OUT_ENABLE == flag ? "enable" : "disable",
  381. i_ret);
  382. }
  383. #endif
  384. #if defined(CONFIG_MTK_CLKMGR)
  385. BTIF_DBG_FUNC("BTIF's clock is %s\n",
  386. (0 == clock_is_on(MTK_BTIF_CG_BIT)) ? "off" : "on");
  387. #endif
  388. return i_ret;
  389. }
  390. static int btif_new_handshake_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool enable)
  391. {
  392. unsigned long base = p_btif->base;
  393. if (true == enable)
  394. BTIF_SET_BIT(BTIF_HANDSHAKE(base), BTIF_HANDSHAKE_EN_HANDSHAKE);
  395. else
  396. BTIF_CLR_BIT(BTIF_HANDSHAKE(base), BTIF_HANDSHAKE_EN_HANDSHAKE);
  397. return true;
  398. }
  399. /*****************************************************************************
  400. * FUNCTION
  401. * hal_btif_hw_init
  402. * DESCRIPTION
  403. * BTIF hardware init
  404. * PARAMETERS
  405. * p_base [IN] BTIF module's base address
  406. * RETURNS
  407. * 0 means success, negative means fail
  408. *****************************************************************************/
  409. int hal_btif_hw_init(P_MTK_BTIF_INFO_STR p_btif)
  410. {
  411. /*Chaozhong: To be implement*/
  412. int i_ret = -1;
  413. unsigned long base = p_btif->base;
  414. #if NEW_TX_HANDLING_SUPPORT
  415. _btif_tx_fifo_reset(p_btif);
  416. #endif
  417. /*set to normal mode*/
  418. btif_reg_sync_writel(BTIF_FAKELCR_NORMAL_MODE, BTIF_FAKELCR(base));
  419. /*set to newhandshake mode*/
  420. btif_new_handshake_ctrl(p_btif, true);
  421. /*No need to access: enable sleep mode*/
  422. /*No need to access: set Rx timeout count*/
  423. /*set Tx threshold*/
  424. /*set Rx threshold*/
  425. /*disable internal loopback test*/
  426. /*set Rx FIFO clear bit to 1*/
  427. BTIF_SET_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_RX);
  428. /*clear Rx FIFO clear bit to 0*/
  429. BTIF_CLR_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_RX);
  430. /*set Tx FIFO clear bit to 1*/
  431. BTIF_SET_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_TX);
  432. /*clear Tx FIFO clear bit to 0*/
  433. BTIF_CLR_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_TX);
  434. btif_reg_sync_writel(BTIF_TRI_LVL_TX(p_btif->tx_tri_lvl)
  435. | BTIF_TRI_LVL_RX(p_btif->rx_tri_lvl)
  436. | BTIF_TRI_LOOP_DIS, BTIF_TRI_LVL(base));
  437. hal_btif_loopback_ctrl(p_btif, false);
  438. /*disable BTIF Tx DMA mode*/
  439. hal_btif_tx_mode_ctrl(p_btif, false);
  440. /*disable BTIF Rx DMA mode*/
  441. hal_btif_rx_mode_ctrl(p_btif, false);
  442. /*auto reset*/
  443. BTIF_SET_BIT(BTIF_DMA_EN(base), BTIF_DMA_EN_AUTORST_EN);
  444. /*disable Tx IER*/
  445. hal_btif_tx_ier_ctrl(p_btif, false);
  446. /*enable Rx IER by default*/
  447. hal_btif_rx_ier_ctrl(p_btif, true);
  448. i_ret = 0;
  449. return i_ret;
  450. }
  451. /*****************************************************************************
  452. * FUNCTION
  453. * hal_btif_rx_ier_ctrl
  454. * DESCRIPTION
  455. * BTIF Rx interrupt enable/disable
  456. * PARAMETERS
  457. * p_base [IN] BTIF module's base address
  458. * enable [IN] control if rx interrupt enabled or not
  459. * RETURNS
  460. * 0 means success, negative means fail
  461. *****************************************************************************/
  462. int hal_btif_rx_ier_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en)
  463. {
  464. int i_ret = -1;
  465. unsigned long base = p_btif->base;
  466. if (false == en)
  467. BTIF_CLR_BIT(BTIF_IER(base), BTIF_IER_RXFEN);
  468. else
  469. BTIF_SET_BIT(BTIF_IER(base), BTIF_IER_RXFEN);
  470. /*TODO:do we need to read back ? Answer: no*/
  471. i_ret = 0;
  472. return i_ret;
  473. }
  474. /*****************************************************************************
  475. * FUNCTION
  476. * hal_btif_tx_ier_ctrl
  477. * DESCRIPTION
  478. * BTIF Tx interrupt enable/disable
  479. * PARAMETERS
  480. * p_base [IN] BTIF module's base address
  481. * enable [IN] control if tx interrupt enabled or not
  482. * RETURNS
  483. * 0 means success, negative means fail
  484. *****************************************************************************/
  485. int hal_btif_tx_ier_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en)
  486. {
  487. int i_ret = -1;
  488. unsigned long base = p_btif->base;
  489. if (false == en)
  490. BTIF_CLR_BIT(BTIF_IER(base), BTIF_IER_TXEEN);
  491. else
  492. BTIF_SET_BIT(BTIF_IER(base), BTIF_IER_TXEEN);
  493. /*TODO:do we need to read back ? Answer: no*/
  494. i_ret = 0;
  495. return i_ret;
  496. }
  497. #ifndef MTK_BTIF_MARK_UNUSED_API
  498. /*****************************************************************************
  499. * FUNCTION
  500. * _btif_receive_data
  501. * DESCRIPTION
  502. * receive data from btif module in FIFO polling mode
  503. * PARAMETERS
  504. * p_base [IN] BTIF module's base address
  505. * p_buf [IN/OUT] pointer to rx data buffer
  506. * max_len [IN] max length of rx buffer
  507. * RETURNS
  508. * positive means data is available, 0 means no data available
  509. *****************************************************************************/
  510. int _btif_receive_data(P_MTK_BTIF_INFO_STR p_btif,
  511. unsigned char *p_buf, const unsigned int max_len)
  512. {
  513. /*Chaozhong: To be implement*/
  514. int i_ret = -1;
  515. /*check parameter valid or not*/
  516. if ((NULL == p_buf) || (max_len == 0)) {
  517. i_ret = ERR_INVALID_PAR;
  518. return i_ret;
  519. }
  520. i_ret = btif_rx_irq_handler(p_btif, p_buf, max_len);
  521. return i_ret;
  522. }
  523. int btif_sleep_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en)
  524. {
  525. int i_ret = -1;
  526. unsigned long base = p_btif->base;
  527. if (false == en)
  528. BTIF_CLR_BIT(BTIF_SLEEP_EN(base), BTIF_SLEEP_EN_BIT);
  529. else
  530. BTIF_SET_BIT(BTIF_SLEEP_EN(base), BTIF_SLEEP_EN_BIT);
  531. /*TODO:do we need to read back ? Answer: no*/
  532. /*TODO:do we need to dsb?*/
  533. i_ret = 0;
  534. return i_ret;
  535. }
  536. static int btif_tx_thr_set(P_MTK_BTIF_INFO_STR p_btif, unsigned int thr_count)
  537. {
  538. int i_ret = -1;
  539. unsigned long base = p_btif->base;
  540. unsigned int value = 0;
  541. /*read BTIF_TRI_LVL*/
  542. value = BTIF_READ32(BTIF_TRI_LVL(base));
  543. /*clear Tx threshold bits*/
  544. value &= (~BTIF_TRI_LVL_TX_MASK);
  545. /*set tx threshold bits*/
  546. value |= BTIF_TRI_LVL_TX(BTIF_TX_FIFO_THRE);
  547. /*write back to BTIF_TRI_LVL*/
  548. btif_reg_sync_writel(value, BTIF_TRI_LVL(base));
  549. return i_ret;
  550. }
  551. /*****************************************************************************
  552. * FUNCTION
  553. * btif_rx_fifo_reset
  554. * DESCRIPTION
  555. * reset BTIF's rx fifo
  556. * PARAMETERS
  557. * p_base [IN] BTIF module's base address
  558. * ec [IN] control if loopback mode is enabled or not
  559. * RETURNS
  560. * 0 means success, negative means fail
  561. *****************************************************************************/
  562. static int btif_rx_fifo_reset(P_MTK_BTIF_INFO_STR p_btif)
  563. {
  564. /*Chaozhong: To be implement*/
  565. int i_ret = -1;
  566. unsigned long base = p_btif->base;
  567. /*set Rx FIFO clear bit to 1*/
  568. BTIF_SET_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_RX);
  569. /*clear Rx FIFO clear bit to 0*/
  570. BTIF_CLR_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_RX);
  571. /*TODO:do we need to read back ? Answer: no*/
  572. /*TODO:do we need to dsb?*/
  573. i_ret = 0;
  574. return i_ret;
  575. }
  576. /*****************************************************************************
  577. * FUNCTION
  578. * btif_tx_fifo_reset
  579. * DESCRIPTION
  580. * reset BTIF's tx fifo
  581. * PARAMETERS
  582. * p_base [IN] BTIF module's base address
  583. * RETURNS
  584. * 0 means success, negative means fail
  585. *****************************************************************************/
  586. static int btif_tx_fifo_reset(P_MTK_BTIF_INFO_STR p_btif)
  587. {
  588. int i_ret = -1;
  589. unsigned long base = p_btif->base;
  590. /*set Tx FIFO clear bit to 1*/
  591. BTIF_SET_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_TX);
  592. /*clear Tx FIFO clear bit to 0*/
  593. BTIF_CLR_BIT(BTIF_FIFOCTRL(base), BTIF_FIFOCTRL_CLR_TX);
  594. /*TODO:do we need to read back ? Answer: no*/
  595. /*TODO:do we need to dsb?*/
  596. i_ret = 0;
  597. return i_ret;
  598. }
  599. #endif
  600. /*****************************************************************************
  601. * FUNCTION
  602. * hal_btif_loopback_ctrl
  603. * DESCRIPTION
  604. * BTIF Tx/Rx loopback mode set, this operation can only be done after set BTIF to normal mode
  605. * PARAMETERS
  606. * RETURNS
  607. * 0 means success, negative means fail
  608. *****************************************************************************/
  609. int hal_btif_loopback_ctrl(P_MTK_BTIF_INFO_STR p_btif, bool en)
  610. {
  611. int i_ret = -1;
  612. unsigned long base = p_btif->base;
  613. if (false == en)
  614. BTIF_CLR_BIT(BTIF_TRI_LVL(base), BTIF_TRI_LOOP_EN);
  615. else
  616. BTIF_SET_BIT(BTIF_TRI_LVL(base), BTIF_TRI_LOOP_EN);
  617. /*TODO:do we need to read back ? Answer: no*/
  618. /*TODO:do we need to dsb?*/
  619. i_ret = 0;
  620. return i_ret;
  621. }
  622. /*****************************************************************************
  623. * FUNCTION
  624. * hal_btif_rx_handler
  625. * DESCRIPTION
  626. * lower level interrupt handler
  627. * PARAMETERS
  628. * p_base [IN] BTIF module's base address
  629. * p_buf [IN/OUT] pointer to rx data buffer
  630. * max_len [IN] max length of rx buffer
  631. * RETURNS
  632. * 0 means success; negative means fail; positive means rx data length
  633. *****************************************************************************/
  634. int hal_btif_irq_handler(P_MTK_BTIF_INFO_STR p_btif,
  635. unsigned char *p_buf, const unsigned int max_len)
  636. {
  637. /*Chaozhong: To be implement*/
  638. int i_ret = -1;
  639. unsigned int iir = 0;
  640. unsigned int rx_len = 0;
  641. unsigned long base = p_btif->base;
  642. #if 0
  643. /*check parameter valid or not*/
  644. if ((NULL == p_buf) || (max_len == 0)) {
  645. i_ret = ERR_INVALID_PAR;
  646. return i_ret;
  647. }
  648. #endif
  649. unsigned long irq_flag = 0;
  650. spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);
  651. #if defined(CONFIG_MTK_CLKMGR)
  652. if (0 == clock_is_on(MTK_BTIF_CG_BIT)) {
  653. spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);
  654. BTIF_ERR_FUNC("%s: clock is off before irq handle done!!!\n",
  655. __FILE__);
  656. return i_ret;
  657. }
  658. #endif
  659. /*read interrupt identifier register*/
  660. iir = BTIF_READ32(BTIF_IIR(base));
  661. /*is rx interrupt exist?*/
  662. #if 0
  663. while ((iir & BTIF_IIR_RX) && (rx_len < max_len)) {
  664. rx_len +=
  665. btif_rx_irq_handler(p_btif, (p_buf + rx_len),
  666. (max_len - rx_len));
  667. /*update IIR*/
  668. iir = BTIF_READ32(BTIF_IIR(base));
  669. }
  670. #endif
  671. while (iir & (BTIF_IIR_RX | BTIF_IIR_RX_TIMEOUT)) {
  672. rx_len += btif_rx_irq_handler(p_btif, p_buf, max_len);
  673. /*update IIR*/
  674. iir = BTIF_READ32(BTIF_IIR(base));
  675. }
  676. /*is tx interrupt exist?*/
  677. if (iir & BTIF_IIR_TX_EMPTY)
  678. i_ret = btif_tx_irq_handler(p_btif);
  679. spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);
  680. i_ret = rx_len != 0 ? rx_len : i_ret;
  681. return i_ret;
  682. }
  683. int hal_btif_rx_cb_reg(P_MTK_BTIF_INFO_STR p_btif_info, btif_rx_buf_write rx_cb)
  684. {
  685. if (NULL != p_btif_info->rx_cb)
  686. BTIF_DBG_FUNC("rx_cb already registered, replace (0x%p) with (0x%p)\n",
  687. p_btif_info->rx_cb, rx_cb);
  688. p_btif_info->rx_cb = rx_cb;
  689. return 0;
  690. }
  691. /*****************************************************************************
  692. * FUNCTION
  693. * btif_rx_irq_handler
  694. * DESCRIPTION
  695. * lower level rx interrupt handler
  696. * PARAMETERS
  697. * p_base [IN] BTIF module's base address
  698. * RETURNS
  699. * positive means length of rx data , negative means fail
  700. *****************************************************************************/
  701. static int btif_rx_irq_handler(P_MTK_BTIF_INFO_STR p_btif_info,
  702. unsigned char *p_buf, const unsigned int max_len)
  703. {
  704. /*Chaozhong: To be implement*/
  705. int i_ret = 0;
  706. unsigned int iir = 0;
  707. unsigned int rx_len = 0;
  708. unsigned long base = p_btif_info->base;
  709. unsigned char rx_buf[256];
  710. unsigned int local_buf_len = 256;
  711. btif_rx_buf_write rx_cb = p_btif_info->rx_cb;
  712. unsigned int total_len = 0;
  713. /*read interrupt identifier register*/
  714. iir = BTIF_READ32(BTIF_IIR(base));
  715. while ((iir & (BTIF_IIR_RX | BTIF_IIR_RX_TIMEOUT)) &&
  716. (rx_len < local_buf_len)) {
  717. rx_buf[rx_len] = BTIF_READ8(base);
  718. rx_len++;
  719. /*need to consult CC Hwang for advice */
  720. /*whether we need to do memory barrier here
  721. Ans: no
  722. */
  723. /*whether we need to d memory barrier when call BTIF_SET_BIT or BTIF_CLR_BIT
  724. Ans: no
  725. */
  726. if (rx_len == local_buf_len) {
  727. if (rx_cb)
  728. (*rx_cb) (p_btif_info, rx_buf, rx_len);
  729. rx_len = 0;
  730. total_len += rx_len;
  731. }
  732. iir = BTIF_READ32(BTIF_IIR(base));
  733. }
  734. total_len += rx_len;
  735. if (rx_len && rx_cb)
  736. (*rx_cb) (p_btif_info, rx_buf, rx_len);
  737. /*make sure all data write back to memory, mb or dsb?
  738. need to consult CC Hwang for advice
  739. Ans: no need here
  740. */
  741. i_ret = total_len;
  742. return i_ret;
  743. }
  744. /*****************************************************************************
  745. * FUNCTION
  746. * btif_tx_irq_handler
  747. * DESCRIPTION
  748. * lower level tx interrupt handler
  749. * PARAMETERS
  750. * p_base [IN] BTIF module's base address
  751. * p_buf [IN/OUT] pointer to rx data buffer
  752. * max_len [IN] max length of rx buffer
  753. * RETURNS
  754. * 0 means success, negative means fail
  755. *****************************************************************************/
  756. static int btif_tx_irq_handler(P_MTK_BTIF_INFO_STR p_btif)
  757. {
  758. int i_ret = -1;
  759. #if NEW_TX_HANDLING_SUPPORT
  760. int how_many = 0;
  761. unsigned int lsr;
  762. unsigned int ava_len = 0;
  763. unsigned long base = p_btif->base;
  764. char local_buf[BTIF_TX_FIFO_SIZE];
  765. char *p_data = local_buf;
  766. unsigned long flag = 0;
  767. struct kfifo *p_tx_fifo = p_btif->p_tx_fifo;
  768. /*read LSR and check THER or TEMT, either one is 1 means can accept tx data*/
  769. lsr = BTIF_READ32(BTIF_LSR(base));
  770. if (lsr & BTIF_LSR_TEMT_BIT)
  771. /*Tx Holding Register if empty, which means we can write tx FIFO count to BTIF*/
  772. ava_len = BTIF_TX_FIFO_SIZE;
  773. else if (lsr & BTIF_LSR_THRE_BIT)
  774. /*Tx Holding Register if empty, which means we can write (Tx FIFO count - Tx threshold)to BTIF*/
  775. ava_len = BTIF_TX_FIFO_SIZE - BTIF_TX_FIFO_THRE;
  776. else {
  777. /*this means data size in tx FIFO is more than Tx threshold, we will not write data to THR*/
  778. ava_len = 0;
  779. goto ret;
  780. }
  781. spin_lock_irqsave(&(p_btif->tx_fifo_spinlock), flag);
  782. how_many = kfifo_out(p_tx_fifo, local_buf, ava_len);
  783. spin_unlock_irqrestore(&(p_btif->tx_fifo_spinlock), flag);
  784. BTIF_DBG_FUNC("BTIF tx size %d done, left:%d\n", how_many,
  785. kfifo_avail(p_tx_fifo));
  786. while (how_many--)
  787. btif_reg_sync_writeb(*(p_data++), BTIF_THR(base));
  788. spin_lock_irqsave(&(p_btif->tx_fifo_spinlock), flag);
  789. /*clear Tx enable flag if necessary*/
  790. if (kfifo_is_empty(p_tx_fifo)) {
  791. hal_btif_tx_ier_ctrl(p_btif, false);
  792. BTIF_DBG_FUNC("BTIF tx FIFO is empty\n");
  793. }
  794. spin_unlock_irqrestore(&(p_btif->tx_fifo_spinlock), flag);
  795. ret:
  796. #else
  797. /*clear Tx enable flag*/
  798. hal_btif_tx_ier_ctrl(p_btif, false);
  799. #endif
  800. i_ret = 0;
  801. return i_ret;
  802. }
  803. /*****************************************************************************
  804. * FUNCTION
  805. * hal_btif_tx_mode_ctrl
  806. * DESCRIPTION
  807. * set BTIF tx to corresponding mode (PIO/DMA)
  808. * PARAMETERS
  809. * p_base [IN] BTIF module's base address
  810. * mode [IN] rx mode <PIO/DMA>
  811. * RETURNS
  812. * 0 means success, negative means fail
  813. *****************************************************************************/
  814. int hal_btif_tx_mode_ctrl(P_MTK_BTIF_INFO_STR p_btif, ENUM_BTIF_MODE mode)
  815. {
  816. int i_ret = -1;
  817. unsigned long base = p_btif->base;
  818. if (BTIF_MODE_DMA == mode)
  819. /*set to DMA mode*/
  820. BTIF_SET_BIT(BTIF_DMA_EN(base), BTIF_DMA_EN_TX);
  821. else
  822. /*set to PIO mode*/
  823. BTIF_CLR_BIT(BTIF_DMA_EN(base), BTIF_DMA_EN_TX);
  824. i_ret = 0;
  825. return i_ret;
  826. }
  827. /*****************************************************************************
  828. * FUNCTION
  829. * hal_btif_rx_mode_ctrl
  830. * DESCRIPTION
  831. * set BTIF rx to corresponding mode (PIO/DMA)
  832. * PARAMETERS
  833. * p_base [IN] BTIF module's base address
  834. * mode [IN] rx mode <PIO/DMA>
  835. * RETURNS
  836. * 0 means success, negative means fail
  837. *****************************************************************************/
  838. int hal_btif_rx_mode_ctrl(P_MTK_BTIF_INFO_STR p_btif, ENUM_BTIF_MODE mode)
  839. {
  840. int i_ret = -1;
  841. unsigned long base = p_btif->base;
  842. if (BTIF_MODE_DMA == mode)
  843. /*set to DMA mode*/
  844. BTIF_SET_BIT(BTIF_DMA_EN(base), BTIF_DMA_EN_RX);
  845. else
  846. /*set to PIO mode*/
  847. BTIF_CLR_BIT(BTIF_DMA_EN(base), BTIF_DMA_EN_RX);
  848. i_ret = 0;
  849. return i_ret;
  850. }
  851. /*****************************************************************************
  852. * FUNCTION
  853. * hal_btif_send_data
  854. * DESCRIPTION
  855. * send data through btif in FIFO mode
  856. * PARAMETERS
  857. * p_base [IN] BTIF module's base address
  858. * p_buf [IN] pointer to rx data buffer
  859. * max_len [IN] tx buffer length
  860. * RETURNS
  861. * positive means number of data sent; 0 means no data put to FIFO; negative means error happens
  862. *****************************************************************************/
  863. int hal_btif_send_data(P_MTK_BTIF_INFO_STR p_btif,
  864. const unsigned char *p_buf, const unsigned int buf_len)
  865. {
  866. /*Chaozhong: To be implement*/
  867. int i_ret = -1;
  868. unsigned int ava_len = 0;
  869. unsigned int sent_len = 0;
  870. #if !(NEW_TX_HANDLING_SUPPORT)
  871. unsigned long base = p_btif->base;
  872. unsigned int lsr = 0;
  873. unsigned int left_len = 0;
  874. unsigned char *p_data = (unsigned char *)p_buf;
  875. #endif
  876. /*check parameter valid or not*/
  877. if ((NULL == p_buf) || (buf_len == 0)) {
  878. i_ret = ERR_INVALID_PAR;
  879. return i_ret;
  880. }
  881. #if NEW_TX_HANDLING_SUPPORT
  882. ava_len = _get_btif_tx_fifo_room(p_btif);
  883. sent_len = buf_len <= ava_len ? buf_len : ava_len;
  884. if (0 < sent_len) {
  885. int enqueue_len = 0;
  886. unsigned long flag = 0;
  887. spin_lock_irqsave(&(p_btif->tx_fifo_spinlock), flag);
  888. enqueue_len = kfifo_in(p_btif->p_tx_fifo,
  889. (unsigned char *)p_buf, sent_len);
  890. if (sent_len != enqueue_len) {
  891. BTIF_ERR_FUNC("target tx len:%d, len sent:%d\n",
  892. sent_len, enqueue_len);
  893. }
  894. i_ret = enqueue_len;
  895. mb();
  896. /*enable BTIF Tx IRQ*/
  897. hal_btif_tx_ier_ctrl(p_btif, true);
  898. spin_unlock_irqrestore(&(p_btif->tx_fifo_spinlock), flag);
  899. BTIF_DBG_FUNC("enqueue len:%d\n", enqueue_len);
  900. } else {
  901. i_ret = 0;
  902. }
  903. #else
  904. while ((_btif_is_tx_allow(p_btif)) && (sent_len < buf_len)) {
  905. /*read LSR and check THER or TEMT, either one is 1 means can accept tx data*/
  906. lsr = BTIF_READ32(BTIF_LSR(base));
  907. if (lsr & BTIF_LSR_TEMT_BIT)
  908. /*Tx Holding Register if empty, which means we can write tx FIFO count to BTIF*/
  909. ava_len = BTIF_TX_FIFO_SIZE;
  910. else if (lsr & BTIF_LSR_THRE_BIT)
  911. /*Tx Holding Register if empty, which means we can write (Tx FIFO count - Tx threshold)to BTIF*/
  912. ava_len = BTIF_TX_FIFO_SIZE - BTIF_TX_FIFO_THRE;
  913. else {
  914. /*this means data size in tx FIFO is more than Tx threshold, we will not write data to THR*/
  915. ava_len = 0;
  916. break;
  917. }
  918. left_len = buf_len - sent_len;
  919. /*ava_len will be real length will write to BTIF THR*/
  920. ava_len = ava_len > left_len ? left_len : ava_len;
  921. /*update sent length valud after this operation*/
  922. sent_len += ava_len;
  923. /*whether we need memory barrier here?
  924. Ans: No, no memory ordering issue exist,
  925. CPU will make sure logically right
  926. */
  927. while (ava_len--)
  928. btif_reg_sync_writeb(*(p_data++), BTIF_THR(base));
  929. }
  930. /* while ((hal_btif_is_tx_allow()) && (sent_len < buf_len)); */
  931. i_ret = sent_len;
  932. /*enable BTIF Tx IRQ*/
  933. hal_btif_tx_ier_ctrl(p_btif, true);
  934. #endif
  935. return i_ret;
  936. }
  937. /*****************************************************************************
  938. * FUNCTION
  939. * hal_btif_raise_wak_sig
  940. * DESCRIPTION
  941. * raise wakeup signal to counterpart
  942. * PARAMETERS
  943. * p_base [IN] BTIF module's base address
  944. * RETURNS
  945. * 0 means success, negative means fail
  946. *****************************************************************************/
  947. int hal_btif_raise_wak_sig(P_MTK_BTIF_INFO_STR p_btif)
  948. {
  949. int i_ret = -1;
  950. unsigned long base = p_btif->base;
  951. #if defined(CONFIG_MTK_CLKMGR)
  952. if (0 == clock_is_on(MTK_BTIF_CG_BIT)) {
  953. BTIF_ERR_FUNC("%s: clock is off before send wakeup signal!!!\n",
  954. __FILE__);
  955. return i_ret;
  956. }
  957. #endif
  958. /*write 0 to BTIF_WAK to pull ap_wakeup_consyss low */
  959. BTIF_CLR_BIT(BTIF_WAK(base), BTIF_WAK_BIT);
  960. /*wait for a period for longer than 1/32k period, here we use 40us*/
  961. set_current_state(TASK_UNINTERRUPTIBLE);
  962. usleep_range(64, 96);
  963. /*according to linux/documentation/timers/timers-how-to, we choose usleep_range
  964. SLEEPING FOR ~USECS OR SMALL MSECS ( 10us - 20ms): * Use usleep_range
  965. */
  966. /*write 1 to pull ap_wakeup_consyss high*/
  967. BTIF_SET_BIT(BTIF_WAK(base), BTIF_WAK_BIT);
  968. i_ret = 0;
  969. return i_ret;
  970. }
  971. /*****************************************************************************
  972. * FUNCTION
  973. * hal_btif_dump_reg
  974. * DESCRIPTION
  975. * dump BTIF module's information when needed
  976. * PARAMETERS
  977. * p_base [IN] BTIF module's base address
  978. * flag [IN] register id flag
  979. * RETURNS
  980. * 0 means success, negative means fail
  981. *****************************************************************************/
  982. int hal_btif_dump_reg(P_MTK_BTIF_INFO_STR p_btif, ENUM_BTIF_REG_ID flag)
  983. {
  984. /*Chaozhong: To be implement*/
  985. int i_ret = -1;
  986. int idx = 0;
  987. /*unsigned long irq_flag = 0;*/
  988. unsigned long base = p_btif->base;
  989. unsigned char reg_map[0xE0 / 4] = { 0 };
  990. unsigned int lsr = 0x0;
  991. unsigned int dma_en = 0;
  992. /*spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);*/
  993. #if defined(CONFIG_MTK_CLKMGR)
  994. if (0 == clock_is_on(MTK_BTIF_CG_BIT)) {
  995. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  996. BTIF_ERR_FUNC("%s: clock is off, this should never happen!!!\n",
  997. __FILE__);
  998. return i_ret;
  999. }
  1000. #endif
  1001. lsr = BTIF_READ32(BTIF_LSR(base));
  1002. dma_en = BTIF_READ32(BTIF_DMA_EN(base));
  1003. /*here we omit 1st register which is THR/RBR register to avoid
  1004. Rx data read by this debug information accidently*/
  1005. for (idx = 1; idx < sizeof(reg_map); idx++)
  1006. reg_map[idx] = BTIF_READ8(p_btif->base + (4 * idx));
  1007. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  1008. BTIF_INFO_FUNC("BTIF's clock is on\n");
  1009. BTIF_INFO_FUNC("base address: 0x%lx\n", base);
  1010. switch (flag) {
  1011. case REG_BTIF_ALL:
  1012. #if 0
  1013. BTIF_INFO_FUNC("BTIF_IER:0x%x\n", BTIF_READ32(BTIF_IER(base)));
  1014. BTIF_INFO_FUNC("BTIF_IIR:0x%x\n", BTIF_READ32(BTIF_IIR(base)));
  1015. BTIF_INFO_FUNC("BTIF_FAKELCR:0x%x\n",
  1016. BTIF_READ32(BTIF_FAKELCR(base)));
  1017. BTIF_INFO_FUNC("BTIF_LSR:0x%x\n", BTIF_READ32(BTIF_LSR(base)));
  1018. BTIF_INFO_FUNC("BTIF_SLEEP_EN:0x%x\n",
  1019. BTIF_READ32(BTIF_SLEEP_EN(base)));
  1020. BTIF_INFO_FUNC("BTIF_DMA_EN:0x%x\n",
  1021. BTIF_READ32(BTIF_DMA_EN(base)));
  1022. BTIF_INFO_FUNC("BTIF_RTOCNT:0x%x\n",
  1023. BTIF_READ32(BTIF_RTOCNT(base)));
  1024. BTIF_INFO_FUNC("BTIF_TRI_LVL:0x%x\n",
  1025. BTIF_READ32(BTIF_TRI_LVL(base)));
  1026. BTIF_INFO_FUNC("BTIF_WAT_TIME:0x%x\n",
  1027. BTIF_READ32(BTIF_WAT_TIME(base)));
  1028. BTIF_INFO_FUNC("BTIF_HANDSHAKE:0x%x\n",
  1029. BTIF_READ32(BTIF_HANDSHAKE(base)));
  1030. #endif
  1031. btif_dump_array("BTIF register", reg_map, sizeof(reg_map));
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. BTIF_INFO_FUNC("Tx DMA %s\n",
  1037. (dma_en & BTIF_DMA_EN_TX) ? "enabled" : "disabled");
  1038. BTIF_INFO_FUNC("Rx DMA %s\n",
  1039. (dma_en & BTIF_DMA_EN_RX) ? "enabled" : "disabled");
  1040. BTIF_INFO_FUNC("Rx data is %s\n",
  1041. (lsr & BTIF_LSR_DR_BIT) ? "not empty" : "empty");
  1042. BTIF_INFO_FUNC("Tx data is %s\n",
  1043. (lsr & BTIF_LSR_TEMT_BIT) ? "empty" : "not empty");
  1044. return i_ret;
  1045. }
  1046. /*****************************************************************************
  1047. * FUNCTION
  1048. * hal_btif_is_tx_complete
  1049. * DESCRIPTION
  1050. * get tx complete flag
  1051. * PARAMETERS
  1052. * p_base [IN] BTIF module's base address
  1053. * RETURNS
  1054. * true means tx complete, false means tx in process
  1055. *****************************************************************************/
  1056. bool hal_btif_is_tx_complete(P_MTK_BTIF_INFO_STR p_btif)
  1057. {
  1058. /*Chaozhong: To be implement*/
  1059. bool b_ret = false;
  1060. unsigned int lsr = 0;
  1061. unsigned long flags = 0;
  1062. unsigned long base = p_btif->base;
  1063. unsigned int tx_empty = 0;
  1064. unsigned int rx_dr = 0;
  1065. unsigned int tx_irq_disable = 0;
  1066. /*3 conditions allow clock to be disable
  1067. 1. if TEMT is set or not
  1068. 2. if DR is set or not
  1069. 3. Tx IRQ is disabled or not*/
  1070. lsr = BTIF_READ32(BTIF_LSR(base));
  1071. tx_empty = lsr & BTIF_LSR_TEMT_BIT;
  1072. rx_dr = lsr & BTIF_LSR_DR_BIT;
  1073. tx_irq_disable = BTIF_READ32(BTIF_IER(base)) & BTIF_IER_TXEEN;
  1074. b_ret =
  1075. (tx_empty && (0 == tx_irq_disable) && (0 == rx_dr)) ? true : false;
  1076. if (!b_ret) {
  1077. BTIF_DBG_FUNC
  1078. ("BTIF flag, tx_empty:%d, rx_dr:%d, tx_irq_disable:%d\n",
  1079. tx_empty, rx_dr, tx_irq_disable);
  1080. }
  1081. #if NEW_TX_HANDLING_SUPPORT
  1082. spin_lock_irqsave(&(p_btif->tx_fifo_spinlock), flags);
  1083. /*clear Tx enable flag if necessary*/
  1084. if (!(kfifo_is_empty(p_btif->p_tx_fifo))) {
  1085. BTIF_DBG_FUNC("BTIF tx FIFO is not empty\n");
  1086. b_ret = false;
  1087. }
  1088. spin_unlock_irqrestore(&(p_btif->tx_fifo_spinlock), flags);
  1089. #endif
  1090. return b_ret;
  1091. }
  1092. /*****************************************************************************
  1093. * FUNCTION
  1094. * hal_btif_is_tx_allow
  1095. * DESCRIPTION
  1096. * whether tx is allowed
  1097. * PARAMETERS
  1098. * p_base [IN] BTIF module's base address
  1099. * RETURNS
  1100. * true if tx operation is allowed; false if tx is not allowed
  1101. *****************************************************************************/
  1102. bool hal_btif_is_tx_allow(P_MTK_BTIF_INFO_STR p_btif)
  1103. {
  1104. #define MIN_TX_MB ((26 * 1000000 / 13) / 1000000)
  1105. #define AVE_TX_MB ((26 * 1000000 / 8) / 1000000)
  1106. /*Chaozhong: To be implement*/
  1107. bool b_ret = false;
  1108. #if NEW_TX_HANDLING_SUPPORT
  1109. unsigned long flags = 0;
  1110. spin_lock_irqsave(&(p_btif->tx_fifo_spinlock), flags);
  1111. /*clear Tx enable flag if necessary*/
  1112. if (kfifo_is_full(p_btif->p_tx_fifo)) {
  1113. BTIF_WARN_FUNC("BTIF tx FIFO is full\n");
  1114. b_ret = false;
  1115. } else {
  1116. b_ret = true;
  1117. }
  1118. spin_unlock_irqrestore(&(p_btif->tx_fifo_spinlock), flags);
  1119. #else
  1120. unsigned int lsr = 0;
  1121. unsigned long base = p_btif->base;
  1122. unsigned int wait_us = (BTIF_TX_FIFO_SIZE - BTIF_TX_FIFO_THRE) / MIN_TX_MB; /*only ava length */
  1123. /*read LSR and check THER or TEMT, either one is 1 means can accept tx data*/
  1124. lsr = BTIF_READ32(BTIF_LSR(base));
  1125. if (!(lsr & (BTIF_LSR_TEMT_BIT | BTIF_LSR_THRE_BIT))) {
  1126. BTIF_DBG_FUNC("wait for %d ~ %d us\n", wait_us, 3 * wait_us);
  1127. /* usleep_range(wait_us, 3 * 10 * wait_us); */
  1128. usleep_range(wait_us, 3 * wait_us);
  1129. }
  1130. lsr = BTIF_READ32(BTIF_LSR(base));
  1131. b_ret = (lsr & (BTIF_LSR_TEMT_BIT | BTIF_LSR_THRE_BIT)) ? true : false;
  1132. if (!b_ret)
  1133. BTIF_DBG_FUNC(" tx is not allowed for the moment\n");
  1134. else
  1135. BTIF_DBG_FUNC(" tx is allowed\n");
  1136. #endif
  1137. return b_ret;
  1138. }
  1139. #if !(NEW_TX_HANDLING_SUPPORT)
  1140. static bool _btif_is_tx_allow(P_MTK_BTIF_INFO_STR p_btif)
  1141. {
  1142. /*Chaozhong: To be implement*/
  1143. bool b_ret = false;
  1144. unsigned long base = p_btif->base;
  1145. unsigned int lsr = 0;
  1146. /*read LSR and check THER or TEMT, either one is 1 means can accept tx data*/
  1147. lsr = BTIF_READ32(BTIF_LSR(base));
  1148. b_ret = (lsr & (BTIF_LSR_TEMT_BIT | BTIF_LSR_THRE_BIT)) ? true : false;
  1149. return b_ret;
  1150. }
  1151. #endif
  1152. int hal_btif_pm_ops(P_MTK_BTIF_INFO_STR p_btif_info, MTK_BTIF_PM_OPID opid)
  1153. {
  1154. int i_ret = -1;
  1155. BTIF_DBG_FUNC("op id: %d\n", opid);
  1156. switch (opid) {
  1157. case BTIF_PM_DPIDLE_EN:
  1158. i_ret = 0;
  1159. break;
  1160. case BTIF_PM_DPIDLE_DIS:
  1161. i_ret = 0;
  1162. break;
  1163. case BTIF_PM_SUSPEND:
  1164. i_ret = 0;
  1165. break;
  1166. case BTIF_PM_RESUME:
  1167. i_ret = 0;
  1168. break;
  1169. case BTIF_PM_RESTORE_NOIRQ:{
  1170. unsigned int flag = 0;
  1171. P_MTK_BTIF_IRQ_STR p_irq = p_btif_info->p_irq;
  1172. #ifdef CONFIG_OF
  1173. flag = p_irq->irq_flags;
  1174. #else
  1175. switch (p_irq->sens_type) {
  1176. case IRQ_SENS_EDGE:
  1177. if (IRQ_EDGE_FALL == p_irq->edge_type)
  1178. flag = IRQF_TRIGGER_FALLING;
  1179. else if (IRQ_EDGE_RAISE == p_irq->edge_type)
  1180. flag = IRQF_TRIGGER_RISING;
  1181. else if (IRQ_EDGE_BOTH == p_irq->edge_type)
  1182. flag = IRQF_TRIGGER_RISING |
  1183. IRQF_TRIGGER_FALLING;
  1184. else
  1185. flag = IRQF_TRIGGER_FALLING; /*make this as default type */
  1186. break;
  1187. case IRQ_SENS_LVL:
  1188. if (IRQ_LVL_LOW == p_irq->lvl_type)
  1189. flag = IRQF_TRIGGER_LOW;
  1190. else if (IRQ_LVL_HIGH == p_irq->lvl_type)
  1191. flag = IRQF_TRIGGER_HIGH;
  1192. else
  1193. flag = IRQF_TRIGGER_LOW; /*make this as default type */
  1194. break;
  1195. default:
  1196. flag = IRQF_TRIGGER_LOW; /*make this as default type */
  1197. break;
  1198. }
  1199. #endif
  1200. /* irq_set_irq_type(p_irq->irq_id, flag); */
  1201. i_ret = 0;
  1202. }
  1203. break;
  1204. default:
  1205. i_ret = ERR_INVALID_PAR;
  1206. break;
  1207. }
  1208. return i_ret;
  1209. }