btif_dma_plat.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. #include <linux/kernel.h>
  2. #ifdef DFT_TAG
  3. #undef DFT_TAG
  4. #endif
  5. #define DFT_TAG "MTK-BTIF-DMA"
  6. #include "btif_dma_priv.h"
  7. #define DMA_USER_ID "btif_driver"
  8. /************************************Global variable***********************************/
  9. static MTK_BTIF_DMA_VFIFO mtk_tx_dma_vfifo = {
  10. .vfifo = {
  11. .p_vir_addr = NULL,
  12. .phy_addr = 0,
  13. .vfifo_size = TX_DMA_VFF_SIZE,
  14. .thre = DMA_TX_THRE(TX_DMA_VFF_SIZE),
  15. },
  16. .wpt = 0,
  17. .last_wpt_wrap = 0,
  18. .rpt = 0,
  19. .last_rpt_wrap = 0,
  20. };
  21. static MTK_BTIF_IRQ_STR mtk_btif_tx_dma_irq = {
  22. .name = "mtk btif tx dma irq",
  23. .is_irq_sup = true,
  24. .reg_flag = false,
  25. #ifdef CONFIG_OF
  26. .irq_flags = IRQF_TRIGGER_NONE,
  27. #else
  28. .irq_id = MT_DMA_BTIF_TX_IRQ_ID,
  29. .sens_type = IRQ_SENS_LVL,
  30. .lvl_type = IRQ_LVL_LOW,
  31. #endif
  32. .p_irq_handler = NULL,
  33. };
  34. static MTK_BTIF_DMA_VFIFO mtk_rx_dma_vfifo = {
  35. .vfifo = {
  36. .p_vir_addr = NULL,
  37. .phy_addr = 0,
  38. .vfifo_size = RX_DMA_VFF_SIZE,
  39. .thre = DMA_RX_THRE(RX_DMA_VFF_SIZE),
  40. },
  41. .wpt = 0,
  42. .last_wpt_wrap = 0,
  43. .rpt = 0,
  44. .last_rpt_wrap = 0,
  45. };
  46. static MTK_BTIF_IRQ_STR mtk_btif_rx_dma_irq = {
  47. .name = "mtk btif rx dma irq",
  48. .is_irq_sup = true,
  49. .reg_flag = false,
  50. #ifdef CONFIG_OF
  51. .irq_flags = IRQF_TRIGGER_NONE,
  52. #else
  53. .irq_id = MT_DMA_BTIF_RX_IRQ_ID,
  54. .sens_type = IRQ_SENS_LVL,
  55. .lvl_type = IRQ_LVL_LOW,
  56. #endif
  57. .p_irq_handler = NULL,
  58. };
  59. static MTK_DMA_INFO_STR mtk_btif_tx_dma = {
  60. #ifndef CONFIG_OF
  61. .base = AP_DMA_BASE + BTIF_TX_DMA_OFFSET,
  62. #endif
  63. .dir = DMA_DIR_TX,
  64. .p_irq = &mtk_btif_tx_dma_irq,
  65. .p_vfifo = &(mtk_tx_dma_vfifo.vfifo),
  66. };
  67. static MTK_DMA_INFO_STR mtk_btif_rx_dma = {
  68. #ifndef CONFIG_OF
  69. .base = AP_DMA_BASE + BTIF_RX_DMA_OFFSET,
  70. #endif
  71. .dir = DMA_DIR_RX,
  72. .p_irq = &mtk_btif_rx_dma_irq,
  73. .p_vfifo = &(mtk_rx_dma_vfifo.vfifo),
  74. };
  75. static spinlock_t g_clk_cg_spinlock; /*dma clock's spinlock */
  76. /************************************Function declearation***********************************/
  77. static int _is_tx_dma_in_flush(P_MTK_DMA_INFO_STR p_dma_info);
  78. static int _tx_dma_flush(P_MTK_DMA_INFO_STR p_dma_info);
  79. static int btif_rx_dma_ctrl(P_MTK_DMA_INFO_STR p_dma_info,
  80. ENUM_DMA_CTRL ctrl_id);
  81. static int btif_tx_dma_ctrl(P_MTK_DMA_INFO_STR p_dma_info,
  82. ENUM_DMA_CTRL ctrl_id);
  83. static int btif_rx_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en);
  84. static int btif_tx_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en);
  85. static int hal_rx_dma_dump_reg(P_MTK_DMA_INFO_STR p_dma_info,
  86. ENUM_BTIF_REG_ID flag);
  87. static int hal_tx_dma_dump_reg(P_MTK_DMA_INFO_STR p_dma_info,
  88. ENUM_BTIF_REG_ID flag);
  89. static int is_tx_dma_irq_finish_done(P_MTK_DMA_INFO_STR p_dma_info);
  90. static int _btif_dma_dump_dbg_reg(void);
  91. /*****************************************************************************
  92. * FUNCTION
  93. * hal_tx_dma_ier_ctrl
  94. * DESCRIPTION
  95. * BTIF Tx DMA's interrupt enable/disable
  96. * PARAMETERS
  97. * p_dma_info [IN] pointer to BTIF dma channel's information
  98. * enable [IN] control if tx interrupt enabled or not
  99. * dma_dir [IN] DMA's direction
  100. * RETURNS
  101. * 0 means success, negative means fail
  102. *****************************************************************************/
  103. static int hal_btif_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en);
  104. /*****************************************************************************
  105. * FUNCTION
  106. * hal_dma_receive_data
  107. * DESCRIPTION
  108. * receive data from btif module in DMA polling mode
  109. * PARAMETERS
  110. * p_dma_info [IN] pointer to BTIF dma channel's information
  111. * p_buf [IN/OUT] pointer to rx data buffer
  112. * max_len [IN] max length of rx buffer
  113. * RETURNS
  114. * positive means data is available, 0 means no data available
  115. *****************************************************************************/
  116. #ifndef MTK_BTIF_MARK_UNUSED_API
  117. static int hal_dma_receive_data(P_MTK_DMA_INFO_STR p_dma_info,
  118. unsigned char *p_buf,
  119. const unsigned int max_len);
  120. /************************************Function***********************************/
  121. #endif
  122. #ifdef CONFIG_OF
  123. static void hal_dma_set_default_setting(ENUM_DMA_DIR dma_dir)
  124. {
  125. struct device_node *node = NULL;
  126. unsigned int irq_info[3] = {0, 0, 0};
  127. unsigned int phy_base;
  128. if (DMA_DIR_RX == dma_dir) {
  129. node = of_find_compatible_node(NULL, NULL, "mediatek,btif_rx");
  130. if (node) {
  131. mtk_btif_rx_dma.p_irq->irq_id = irq_of_parse_and_map(node, 0);
  132. /*fixme, be compitable arch 64bits*/
  133. mtk_btif_rx_dma.base = (unsigned long)of_iomap(node, 0);
  134. BTIF_INFO_FUNC("get rx_dma irq(%d),register base(0x%lx)\n",
  135. mtk_btif_rx_dma.p_irq->irq_id, mtk_btif_rx_dma.base);
  136. } else {
  137. BTIF_ERR_FUNC("get rx_dma device node fail\n");
  138. }
  139. /* get the interrupt line behaviour */
  140. if (of_property_read_u32_array(node, "interrupts",
  141. irq_info, ARRAY_SIZE(irq_info))) {
  142. BTIF_ERR_FUNC("get interrupt flag from DTS fail\n");
  143. } else {
  144. mtk_btif_rx_dma.p_irq->irq_flags = irq_info[2];
  145. BTIF_INFO_FUNC("get interrupt flag(0x%x)\n",
  146. mtk_btif_rx_dma.p_irq->irq_flags);
  147. }
  148. if (of_property_read_u32_index(node, "reg", 0, &phy_base)) {
  149. BTIF_ERR_FUNC("get register phy base from DTS fail,dma_dir(%d)\n",
  150. dma_dir);
  151. } else {
  152. BTIF_INFO_FUNC("get register phy base dma_dir(%d)(0x%x)\n",
  153. dma_dir, (unsigned int)phy_base);
  154. }
  155. } else if (DMA_DIR_TX == dma_dir) {
  156. node = of_find_compatible_node(NULL, NULL, "mediatek,btif_tx");
  157. if (node) {
  158. mtk_btif_tx_dma.p_irq->irq_id = irq_of_parse_and_map(node, 0);
  159. /*fixme, be compitable arch 64bits*/
  160. mtk_btif_tx_dma.base = (unsigned long)of_iomap(node, 0);
  161. BTIF_INFO_FUNC("get tx_dma irq(%d),register base(0x%lx)\n",
  162. mtk_btif_tx_dma.p_irq->irq_id, mtk_btif_tx_dma.base);
  163. } else {
  164. BTIF_ERR_FUNC("get tx_dma device node fail\n");
  165. }
  166. /* get the interrupt line behaviour */
  167. if (of_property_read_u32_array(node, "interrupts",
  168. irq_info, ARRAY_SIZE(irq_info))) {
  169. BTIF_ERR_FUNC("get interrupt flag from DTS fail\n");
  170. } else {
  171. mtk_btif_tx_dma.p_irq->irq_flags = irq_info[2];
  172. BTIF_INFO_FUNC("get interrupt flag(0x%x)\n",
  173. mtk_btif_tx_dma.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,dma_dir(%d)\n",
  177. dma_dir);
  178. } else {
  179. BTIF_INFO_FUNC("get register phy base dma_dir(%d)(0x%x)\n",
  180. dma_dir, (unsigned int)phy_base);
  181. }
  182. }
  183. }
  184. #endif
  185. /*****************************************************************************
  186. * FUNCTION
  187. * hal_tx_dma_info_get
  188. * DESCRIPTION
  189. * get btif tx dma channel's information
  190. * PARAMETERS
  191. * dma_dir [IN] DMA's direction
  192. * RETURNS
  193. * pointer to btif dma's information structure
  194. *****************************************************************************/
  195. P_MTK_DMA_INFO_STR hal_btif_dma_info_get(ENUM_DMA_DIR dma_dir)
  196. {
  197. P_MTK_DMA_INFO_STR p_dma_info = NULL;
  198. BTIF_TRC_FUNC();
  199. #ifdef CONFIG_OF
  200. hal_dma_set_default_setting(dma_dir);
  201. #endif
  202. if (DMA_DIR_RX == dma_dir)
  203. /*Rx DMA*/
  204. p_dma_info = &mtk_btif_rx_dma;
  205. else if (DMA_DIR_TX == dma_dir)
  206. /*Tx DMA*/
  207. p_dma_info = &mtk_btif_tx_dma;
  208. else
  209. /*print error log*/
  210. BTIF_ERR_FUNC("invalid DMA dir (%d)\n", dma_dir);
  211. spin_lock_init(&g_clk_cg_spinlock);
  212. BTIF_TRC_FUNC();
  213. return p_dma_info;
  214. }
  215. /*****************************************************************************
  216. * FUNCTION
  217. * hal_btif_clk_ctrl
  218. * DESCRIPTION
  219. * control clock output enable/disable of DMA module
  220. * PARAMETERS
  221. * p_dma_info [IN] pointer to BTIF dma channel's information
  222. * RETURNS
  223. * 0 means success, negative means fail
  224. *****************************************************************************/
  225. int hal_btif_dma_clk_ctrl(P_MTK_DMA_INFO_STR p_dma_info, ENUM_CLOCK_CTRL flag)
  226. {
  227. /*In MTK DMA BTIF channel, there's only one global CG on AP_DMA, no sub channel's CG bit*/
  228. /*according to Artis's comment, clock of DMA and BTIF is default off, so we assume it to be off by default*/
  229. int i_ret = 0;
  230. unsigned long irq_flag = 0;
  231. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  232. static atomic_t s_clk_ref = ATOMIC_INIT(0);
  233. #else
  234. static ENUM_CLOCK_CTRL status = CLK_OUT_DISABLE;
  235. #endif
  236. spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);
  237. #if MTK_BTIF_ENABLE_CLK_CTL
  238. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  239. if (CLK_OUT_ENABLE == flag) {
  240. if (1 == atomic_inc_return(&s_clk_ref)) {
  241. #if defined(CONFIG_MTK_CLKMGR)
  242. i_ret = enable_clock(MTK_BTIF_APDMA_CLK_CG, DMA_USER_ID);
  243. #else
  244. BTIF_DBG_FUNC("[CCF]enable clk_btif_apdma\n");
  245. i_ret = clk_enable(clk_btif_apdma);
  246. #endif /* defined(CONFIG_MTK_CLKMGR) */
  247. if (i_ret) {
  248. BTIF_WARN_FUNC
  249. ("enable_clock for MTK_BTIF_APDMA_CLK_CG failed, ret:%d",
  250. i_ret);
  251. }
  252. }
  253. } else if (CLK_OUT_DISABLE == flag) {
  254. if (0 == atomic_dec_return(&s_clk_ref)) {
  255. #if defined(CONFIG_MTK_CLKMGR)
  256. i_ret = disable_clock(MTK_BTIF_APDMA_CLK_CG, DMA_USER_ID);
  257. if (i_ret) {
  258. BTIF_WARN_FUNC
  259. ("disable_clock for MTK_BTIF_APDMA_CLK_CG failed, ret:%d",
  260. i_ret);
  261. }
  262. #else
  263. BTIF_DBG_FUNC("[CCF] clk_disable(clk_btif_apdma) calling\n");
  264. clk_disable(clk_btif_apdma);
  265. #endif /* defined(CONFIG_MTK_CLKMGR) */
  266. }
  267. } else {
  268. i_ret = ERR_INVALID_PAR;
  269. BTIF_ERR_FUNC("invalid clock ctrl flag (%d)\n", flag);
  270. }
  271. #else
  272. if (status == flag) {
  273. i_ret = 0;
  274. BTIF_DBG_FUNC("dma clock already %s\n",
  275. CLK_OUT_ENABLE ==
  276. status ? "enabled" : "disabled");
  277. } else {
  278. if (CLK_OUT_ENABLE == flag) {
  279. #if defined(CONFIG_MTK_CLKMGR)
  280. i_ret = enable_clock(MTK_BTIF_APDMA_CLK_CG, DMA_USER_ID);
  281. #else
  282. BTIF_DBG_FUNC("[CCF]enable clk_btif_apdma\n");
  283. i_ret = clk_enable(clk_btif_apdma);
  284. #endif /* defined(CONFIG_MTK_CLKMGR) */
  285. status = (0 == i_ret) ? flag : status;
  286. if (i_ret) {
  287. BTIF_WARN_FUNC
  288. ("enable_clock for MTK_BTIF_APDMA_CLK_CG failed, ret:%d",
  289. i_ret);
  290. }
  291. } else if (CLK_OUT_DISABLE == flag) {
  292. #if defined(CONFIG_MTK_CLKMGR)
  293. i_ret = disable_clock(MTK_BTIF_APDMA_CLK_CG, DMA_USER_ID);
  294. status = (0 == i_ret) ? flag : status;
  295. if (i_ret) {
  296. BTIF_WARN_FUNC
  297. ("disable_clock for MTK_BTIF_APDMA_CLK_CG failed, ret:%d",
  298. i_ret);
  299. }
  300. #else
  301. BTIF_DBG_FUNC("[CCF] clk_disable_unprepare(clk_btif_apdma) calling\n");
  302. clk_disable(clk_btif_apdma);
  303. #endif /* defined(CONFIG_MTK_CLKMGR) */
  304. } else {
  305. i_ret = ERR_INVALID_PAR;
  306. BTIF_ERR_FUNC("invalid clock ctrl flag (%d)\n", flag);
  307. }
  308. }
  309. #endif
  310. #else
  311. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  312. #else
  313. status = flag;
  314. #endif
  315. i_ret = 0;
  316. #endif
  317. spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);
  318. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  319. if (0 == i_ret) {
  320. BTIF_DBG_FUNC("dma clock %s\n",
  321. CLK_OUT_ENABLE == flag ? "enabled" : "disabled");
  322. } else {
  323. BTIF_ERR_FUNC("%s dma clock failed, ret(%d)\n",
  324. CLK_OUT_ENABLE == flag ? "enable" : "disable",
  325. i_ret);
  326. }
  327. #else
  328. if (0 == i_ret) {
  329. BTIF_DBG_FUNC("dma clock %s\n",
  330. CLK_OUT_ENABLE == flag ? "enabled" : "disabled");
  331. } else {
  332. BTIF_ERR_FUNC("%s dma clock failed, ret(%d)\n",
  333. CLK_OUT_ENABLE == flag ? "enable" : "disable",
  334. i_ret);
  335. }
  336. #endif
  337. #if defined(CONFIG_MTK_CLKMGR)
  338. BTIF_DBG_FUNC("DMA's clock is %s\n",
  339. (0 == clock_is_on(MTK_BTIF_APDMA_CLK_CG)) ? "off" : "on");
  340. #endif
  341. return i_ret;
  342. }
  343. int hal_btif_dma_hw_init(P_MTK_DMA_INFO_STR p_dma_info)
  344. {
  345. int i_ret = 0;
  346. unsigned int dat = 0;
  347. unsigned long base = p_dma_info->base;
  348. P_DMA_VFIFO p_vfifo = p_dma_info->p_vfifo;
  349. P_MTK_BTIF_DMA_VFIFO p_mtk_dma_vfifo = container_of(p_vfifo,
  350. MTK_BTIF_DMA_VFIFO,
  351. vfifo);
  352. if (DMA_DIR_RX == p_dma_info->dir) {
  353. /*Rx DMA*/
  354. /*do hardware reset*/
  355. /* BTIF_SET_BIT(RX_DMA_RST(base), DMA_HARD_RST);*/
  356. /* BTIF_CLR_BIT(RX_DMA_RST(base), DMA_HARD_RST);*/
  357. BTIF_SET_BIT(RX_DMA_RST(base), DMA_WARM_RST);
  358. do {
  359. dat = BTIF_READ32(RX_DMA_EN(base));
  360. } while (0x01 & dat);
  361. /*write vfifo base address to VFF_ADDR*/
  362. btif_reg_sync_writel(p_vfifo->phy_addr, RX_DMA_VFF_ADDR(base));
  363. /*write vfifo length to VFF_LEN*/
  364. btif_reg_sync_writel(p_vfifo->vfifo_size, RX_DMA_VFF_LEN(base));
  365. /*write wpt to VFF_WPT*/
  366. btif_reg_sync_writel(p_mtk_dma_vfifo->wpt,
  367. RX_DMA_VFF_WPT(base));
  368. btif_reg_sync_writel(p_mtk_dma_vfifo->rpt,
  369. RX_DMA_VFF_RPT(base));
  370. /*write vff_thre to VFF_THRESHOLD*/
  371. btif_reg_sync_writel(p_vfifo->thre, RX_DMA_VFF_THRE(base));
  372. /*clear Rx DMA's interrupt status*/
  373. BTIF_SET_BIT(RX_DMA_INT_FLAG(base),
  374. RX_DMA_INT_DONE | RX_DMA_INT_THRE);
  375. /*enable Rx IER by default*/
  376. btif_rx_dma_ier_ctrl(p_dma_info, true);
  377. } else {
  378. /*Tx DMA*/
  379. /*do hardware reset*/
  380. /* BTIF_SET_BIT(TX_DMA_RST(base), DMA_HARD_RST);*/
  381. /* BTIF_CLR_BIT(TX_DMA_RST(base), DMA_HARD_RST);*/
  382. BTIF_SET_BIT(TX_DMA_RST(base), DMA_WARM_RST);
  383. do {
  384. dat = BTIF_READ32(TX_DMA_EN(base));
  385. } while (0x01 & dat);
  386. /*write vfifo base address to VFF_ADDR*/
  387. btif_reg_sync_writel(p_vfifo->phy_addr, TX_DMA_VFF_ADDR(base));
  388. /*write vfifo length to VFF_LEN*/
  389. btif_reg_sync_writel(p_vfifo->vfifo_size, TX_DMA_VFF_LEN(base));
  390. /*write wpt to VFF_WPT*/
  391. btif_reg_sync_writel(p_mtk_dma_vfifo->wpt,
  392. TX_DMA_VFF_WPT(base));
  393. btif_reg_sync_writel(p_mtk_dma_vfifo->rpt,
  394. TX_DMA_VFF_RPT(base));
  395. /*write vff_thre to VFF_THRESHOLD*/
  396. btif_reg_sync_writel(p_vfifo->thre, TX_DMA_VFF_THRE(base));
  397. BTIF_CLR_BIT(TX_DMA_INT_FLAG(base), TX_DMA_INT_FLAG_MASK);
  398. hal_btif_dma_ier_ctrl(p_dma_info, false);
  399. }
  400. return i_ret;
  401. }
  402. /*****************************************************************************
  403. * FUNCTION
  404. * hal_tx_dma_ctrl
  405. * DESCRIPTION
  406. * enable/disable Tx DMA channel
  407. * PARAMETERS
  408. * p_dma_info [IN] pointer to BTIF dma channel's information
  409. * ctrl_id [IN] enable/disable ID
  410. * RETURNS
  411. * 0 means success; negative means fail
  412. *****************************************************************************/
  413. int hal_btif_dma_ctrl(P_MTK_DMA_INFO_STR p_dma_info, ENUM_DMA_CTRL ctrl_id)
  414. {
  415. unsigned int i_ret = -1;
  416. ENUM_DMA_DIR dir = p_dma_info->dir;
  417. if (DMA_DIR_RX == dir)
  418. i_ret = btif_rx_dma_ctrl(p_dma_info, ctrl_id);
  419. else if (DMA_DIR_TX == dir)
  420. i_ret = btif_tx_dma_ctrl(p_dma_info, ctrl_id);
  421. else {
  422. /*TODO: print error log*/
  423. BTIF_ERR_FUNC("invalid dma ctrl id (%d)\n", ctrl_id);
  424. i_ret = ERR_INVALID_PAR;
  425. }
  426. return i_ret;
  427. }
  428. int hal_btif_dma_rx_cb_reg(P_MTK_DMA_INFO_STR p_dma_info,
  429. dma_rx_buf_write rx_cb)
  430. {
  431. if (NULL != p_dma_info->rx_cb) {
  432. BTIF_DBG_FUNC
  433. ("rx_cb already registered, replace (0x%p) with (0x%p)\n",
  434. p_dma_info->rx_cb, rx_cb);
  435. }
  436. p_dma_info->rx_cb = rx_cb;
  437. return 0;
  438. }
  439. int btif_tx_dma_ctrl(P_MTK_DMA_INFO_STR p_dma_info, ENUM_DMA_CTRL ctrl_id)
  440. {
  441. unsigned int i_ret = -1;
  442. unsigned long base = p_dma_info->base;
  443. unsigned int dat;
  444. BTIF_TRC_FUNC();
  445. if (DMA_CTRL_DISABLE == ctrl_id) {
  446. /*if write 0 to EN bit, DMA will be stopped imediately*/
  447. /*if write 1 to STOP bit, DMA will be stopped after current transaction finished*/
  448. /*BTIF_CLR_BIT(TX_DMA_EN(base), DMA_EN_BIT);*/
  449. BTIF_SET_BIT(TX_DMA_STOP(base), DMA_STOP_BIT);
  450. do {
  451. dat = BTIF_READ32(TX_DMA_STOP(base));
  452. } while (0x1 & dat);
  453. BTIF_DBG_FUNC("BTIF Tx DMA disabled,EN(0x%x),STOP(0x%x)\n",
  454. BTIF_READ32(TX_DMA_EN(base)), BTIF_READ32(TX_DMA_STOP(base)));
  455. i_ret = 0;
  456. } else if (DMA_CTRL_ENABLE == ctrl_id) {
  457. BTIF_SET_BIT(TX_DMA_EN(base), DMA_EN_BIT);
  458. BTIF_DBG_FUNC("BTIF Tx DMA enabled\n");
  459. i_ret = 0;
  460. } else {
  461. /*TODO: print error log*/
  462. BTIF_ERR_FUNC("invalid DMA ctrl_id (%d)\n", ctrl_id);
  463. i_ret = ERR_INVALID_PAR;
  464. }
  465. BTIF_TRC_FUNC();
  466. return i_ret;
  467. }
  468. int btif_rx_dma_ctrl(P_MTK_DMA_INFO_STR p_dma_info, ENUM_DMA_CTRL ctrl_id)
  469. {
  470. unsigned int i_ret = -1;
  471. unsigned long base = p_dma_info->base;
  472. unsigned int dat;
  473. BTIF_TRC_FUNC();
  474. if (DMA_CTRL_DISABLE == ctrl_id) {
  475. /*if write 0 to EN bit, DMA will be stopped imediately*/
  476. /*if write 1 to STOP bit, DMA will be stopped after current transaction finished*/
  477. /*BTIF_CLR_BIT(RX_DMA_EN(base), DMA_EN_BIT);*/
  478. BTIF_SET_BIT(RX_DMA_STOP(base), DMA_STOP_BIT);
  479. do {
  480. dat = BTIF_READ32(RX_DMA_STOP(base));
  481. } while (0x1 & dat);
  482. BTIF_DBG_FUNC("BTIF Rx DMA disabled,EN(0x%x),STOP(0x%x)\n",
  483. BTIF_READ32(RX_DMA_EN(base)), BTIF_READ32(RX_DMA_STOP(base)));
  484. i_ret = 0;
  485. } else if (DMA_CTRL_ENABLE == ctrl_id) {
  486. BTIF_SET_BIT(RX_DMA_EN(base), DMA_EN_BIT);
  487. BTIF_DBG_FUNC("BTIF Rx DMA enabled\n");
  488. i_ret = 0;
  489. } else {
  490. /*TODO: print error log*/
  491. BTIF_ERR_FUNC("invalid DMA ctrl_id (%d)\n", ctrl_id);
  492. i_ret = ERR_INVALID_PAR;
  493. }
  494. BTIF_TRC_FUNC();
  495. return i_ret;
  496. }
  497. /*****************************************************************************
  498. * FUNCTION
  499. * hal_tx_vfifo_reset
  500. * DESCRIPTION
  501. * reset tx virtual fifo information, except memory information
  502. * PARAMETERS
  503. * p_dma_info [IN] pointer to BTIF dma channel's information
  504. * RETURNS
  505. * 0 means success, negative means fail
  506. *****************************************************************************/
  507. int hal_btif_vfifo_reset(P_MTK_DMA_INFO_STR p_dma_info)
  508. {
  509. unsigned int i_ret = -1;
  510. P_DMA_VFIFO p_vfifo = p_dma_info->p_vfifo;
  511. P_MTK_BTIF_DMA_VFIFO p_mtk_dma_vfifo = container_of(p_vfifo,
  512. MTK_BTIF_DMA_VFIFO,
  513. vfifo);
  514. BTIF_TRC_FUNC();
  515. p_mtk_dma_vfifo->rpt = 0;
  516. p_mtk_dma_vfifo->last_rpt_wrap = 0;
  517. p_mtk_dma_vfifo->wpt = 0;
  518. p_mtk_dma_vfifo->last_wpt_wrap = 0;
  519. BTIF_TRC_FUNC();
  520. return i_ret;
  521. }
  522. /*****************************************************************************
  523. * FUNCTION
  524. * hal_tx_dma_ier_ctrl
  525. * DESCRIPTION
  526. * BTIF Tx DMA's interrupt enable/disable
  527. * PARAMETERS
  528. * p_dma_info [IN] pointer to BTIF dma channel's information
  529. * enable [IN] control if tx interrupt enabled or not
  530. * RETURNS
  531. * 0 means success, negative means fail
  532. *****************************************************************************/
  533. int hal_btif_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en)
  534. {
  535. unsigned int i_ret = -1;
  536. ENUM_DMA_DIR dir = p_dma_info->dir;
  537. if (DMA_DIR_RX == dir) {
  538. i_ret = btif_rx_dma_ier_ctrl(p_dma_info, en);
  539. } else if (DMA_DIR_TX == dir) {
  540. i_ret = btif_tx_dma_ier_ctrl(p_dma_info, en);
  541. } else {
  542. /*TODO: print error log*/
  543. BTIF_ERR_FUNC("invalid DMA dma dir (%d)\n", dir);
  544. i_ret = ERR_INVALID_PAR;
  545. }
  546. return i_ret;
  547. }
  548. int btif_rx_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en)
  549. {
  550. unsigned int i_ret = -1;
  551. unsigned long base = p_dma_info->base;
  552. BTIF_TRC_FUNC();
  553. if (!en) {
  554. BTIF_CLR_BIT(RX_DMA_INT_EN(base),
  555. (RX_DMA_INT_THRE_EN | RX_DMA_INT_DONE_EN));
  556. } else {
  557. BTIF_SET_BIT(RX_DMA_INT_EN(base),
  558. (RX_DMA_INT_THRE_EN | RX_DMA_INT_DONE_EN));
  559. }
  560. i_ret = 0;
  561. BTIF_TRC_FUNC();
  562. return i_ret;
  563. }
  564. int btif_tx_dma_ier_ctrl(P_MTK_DMA_INFO_STR p_dma_info, bool en)
  565. {
  566. unsigned int i_ret = -1;
  567. unsigned long base = p_dma_info->base;
  568. BTIF_TRC_FUNC();
  569. if (!en)
  570. BTIF_CLR_BIT(TX_DMA_INT_EN(base), TX_DMA_INTEN_BIT);
  571. else
  572. BTIF_SET_BIT(TX_DMA_INT_EN(base), TX_DMA_INTEN_BIT);
  573. i_ret = 0;
  574. BTIF_TRC_FUNC();
  575. return i_ret;
  576. }
  577. static int is_tx_dma_irq_finish_done(P_MTK_DMA_INFO_STR p_dma_info)
  578. {
  579. int tx_irq_done = 0;
  580. #if MTK_BTIF_ENABLE_CLK_REF_COUNTER
  581. /*if we enable this clock reference couner, just return , because when enter IRQ handler, DMA's clock will be opened*/
  582. tx_irq_done = 1;
  583. #else
  584. unsigned long flag = 0;
  585. unsigned long base = p_dma_info->base;
  586. spin_lock_irqsave(&(g_clk_cg_spinlock), flag);
  587. tx_irq_done =
  588. (0 ==
  589. (BTIF_READ32(TX_DMA_INT_FLAG(base)) &
  590. TX_DMA_INT_FLAG_MASK)) ? 1 : 0;
  591. spin_unlock_irqrestore(&(g_clk_cg_spinlock), flag);
  592. #endif
  593. return tx_irq_done;
  594. }
  595. /*****************************************************************************
  596. * FUNCTION
  597. * hal_tx_dma_irq_handler
  598. * DESCRIPTION
  599. * lower level tx interrupt handler
  600. * PARAMETERS
  601. * p_dma_info [IN] pointer to BTIF dma channel's information
  602. * RETURNS
  603. * 0 means success, negative means fail
  604. *****************************************************************************/
  605. int hal_tx_dma_irq_handler(P_MTK_DMA_INFO_STR p_dma_info)
  606. {
  607. #define MAX_CONTINIOUS_TIMES 512
  608. unsigned int i_ret = -1;
  609. unsigned int valid_size = 0;
  610. unsigned int vff_len = 0;
  611. unsigned int left_len = 0;
  612. unsigned long base = p_dma_info->base;
  613. static int flush_irq_counter;
  614. static struct timeval start_timer;
  615. static struct timeval end_timer;
  616. unsigned long flag = 0;
  617. spin_lock_irqsave(&(g_clk_cg_spinlock), flag);
  618. #if defined(CONFIG_MTK_CLKMGR)
  619. if (0 == clock_is_on(MTK_BTIF_APDMA_CLK_CG)) {
  620. spin_unlock_irqrestore(&(g_clk_cg_spinlock), flag);
  621. BTIF_ERR_FUNC
  622. ("%s: clock is off before irq status clear done!!!\n",
  623. __FILE__);
  624. return i_ret;
  625. }
  626. #endif
  627. /*check if Tx VFF Left Size equal to VFIFO size or not*/
  628. vff_len = BTIF_READ32(TX_DMA_VFF_LEN(base));
  629. valid_size = BTIF_READ32(TX_DMA_VFF_VALID_SIZE(base));
  630. left_len = BTIF_READ32(TX_DMA_VFF_LEFT_SIZE(base));
  631. if (0 == flush_irq_counter)
  632. do_gettimeofday(&start_timer);
  633. if ((0 < valid_size) && (8 > valid_size)) {
  634. i_ret = _tx_dma_flush(p_dma_info);
  635. flush_irq_counter++;
  636. if (MAX_CONTINIOUS_TIMES <= flush_irq_counter) {
  637. do_gettimeofday(&end_timer);
  638. /*when btif tx fifo cannot accept any data and counts of bytes left in tx vfifo < 8 for a while
  639. we assume that btif cannot send data for a long time
  640. in order not to generate interrupt continiously, which may effect system's performance.
  641. we clear tx flag and disable btif tx interrupt
  642. */
  643. /*clear interrupt flag*/
  644. BTIF_CLR_BIT(TX_DMA_INT_FLAG(base),
  645. TX_DMA_INT_FLAG_MASK);
  646. /*vFIFO data has been read by DMA controller, just disable tx dma's irq*/
  647. i_ret = hal_btif_dma_ier_ctrl(p_dma_info, false);
  648. BTIF_ERR_FUNC
  649. ("**********************ERROR, ERROR, ERROR**************************\n");
  650. BTIF_ERR_FUNC
  651. ("BTIF Tx IRQ happened %d times (continiously), between %d.%d and %d.%d\n",
  652. MAX_CONTINIOUS_TIMES, start_timer.tv_sec,
  653. start_timer.tv_usec, end_timer.tv_usec,
  654. end_timer.tv_usec);
  655. }
  656. } else if (vff_len == left_len) {
  657. flush_irq_counter = 0;
  658. /*clear interrupt flag*/
  659. BTIF_CLR_BIT(TX_DMA_INT_FLAG(base), TX_DMA_INT_FLAG_MASK);
  660. /*vFIFO data has been read by DMA controller, just disable tx dma's irq*/
  661. i_ret = hal_btif_dma_ier_ctrl(p_dma_info, false);
  662. } else {
  663. #if 0
  664. BTIF_ERR_FUNC
  665. ("**********************WARNING**************************\n");
  666. BTIF_ERR_FUNC("invalid irq condition, dump register\n");
  667. hal_dma_dump_reg(p_dma_info, REG_TX_DMA_ALL);
  668. #endif
  669. BTIF_DBG_FUNC
  670. ("superious IRQ occurs, vff_len(%d), valid_size(%d), left_len(%d)\n",
  671. vff_len, valid_size, left_len);
  672. }
  673. spin_unlock_irqrestore(&(g_clk_cg_spinlock), flag);
  674. return i_ret;
  675. }
  676. /*****************************************************************************
  677. * FUNCTION
  678. * hal_dma_send_data
  679. * DESCRIPTION
  680. * send data through btif in DMA mode
  681. * PARAMETERS
  682. * p_dma_info [IN] pointer to BTIF dma channel's information
  683. * p_buf [IN] pointer to rx data buffer
  684. * max_len [IN] tx buffer length
  685. * RETURNS
  686. * 0 means success, negative means fail
  687. *****************************************************************************/
  688. int hal_dma_send_data(P_MTK_DMA_INFO_STR p_dma_info,
  689. const unsigned char *p_buf, const unsigned int buf_len)
  690. {
  691. unsigned int i_ret = -1;
  692. unsigned long base = p_dma_info->base;
  693. P_DMA_VFIFO p_vfifo = p_dma_info->p_vfifo;
  694. unsigned int len_to_send = buf_len;
  695. unsigned int ava_len = 0;
  696. unsigned int wpt = 0;
  697. unsigned int last_wpt_wrap = 0;
  698. unsigned int vff_size = 0;
  699. unsigned char *p_data = (unsigned char *)p_buf;
  700. P_MTK_BTIF_DMA_VFIFO p_mtk_vfifo = container_of(p_vfifo,
  701. MTK_BTIF_DMA_VFIFO,
  702. vfifo);
  703. BTIF_TRC_FUNC();
  704. if ((NULL == p_buf) || (0 == buf_len)) {
  705. i_ret = ERR_INVALID_PAR;
  706. BTIF_ERR_FUNC("invalid parameters, p_buf:0x%p, buf_len:%d\n",
  707. p_buf, buf_len);
  708. return i_ret;
  709. }
  710. /*check if tx dma in flush operation? if yes, should wait until DMA finish flush operation*/
  711. /*currently uplayer logic will make sure this pre-condition*/
  712. /*disable Tx IER, in case Tx irq happens, flush bit may be set in irq handler*/
  713. btif_tx_dma_ier_ctrl(p_dma_info, false);
  714. vff_size = p_mtk_vfifo->vfifo.vfifo_size;
  715. ava_len = BTIF_READ32(TX_DMA_VFF_LEFT_SIZE(base));
  716. wpt = BTIF_READ32(TX_DMA_VFF_WPT(base)) & DMA_WPT_MASK;
  717. last_wpt_wrap = BTIF_READ32(TX_DMA_VFF_WPT(base)) & DMA_WPT_WRAP;
  718. /*copy data to vFIFO, Note: ava_len should always large than buf_len,
  719. otherwise common logic layer will not call hal_dma_send_data*/
  720. if (buf_len > ava_len) {
  721. BTIF_ERR_FUNC
  722. ("length to send:(%d) < length available(%d), abnormal!!!---!!!\n",
  723. buf_len, ava_len);
  724. BUG_ON(buf_len > ava_len); /* this will cause kernel panic */
  725. }
  726. len_to_send = buf_len < ava_len ? buf_len : ava_len;
  727. if (len_to_send + wpt >= vff_size) {
  728. unsigned int tail_len = vff_size - wpt;
  729. memcpy((p_mtk_vfifo->vfifo.p_vir_addr + wpt), p_data, tail_len);
  730. p_data += tail_len;
  731. memcpy(p_mtk_vfifo->vfifo.p_vir_addr,
  732. p_data, len_to_send - tail_len);
  733. /*make sure all data write to memory area tx vfifo locates*/
  734. mb();
  735. /*calculate WPT*/
  736. wpt = wpt + len_to_send - vff_size;
  737. last_wpt_wrap ^= DMA_WPT_WRAP;
  738. } else {
  739. memcpy((p_mtk_vfifo->vfifo.p_vir_addr + wpt),
  740. p_data, len_to_send);
  741. /*make sure all data write to memory area tx vfifo locates*/
  742. mb();
  743. /*calculate WPT*/
  744. wpt += len_to_send;
  745. }
  746. p_mtk_vfifo->wpt = wpt;
  747. p_mtk_vfifo->last_wpt_wrap = last_wpt_wrap;
  748. /*make sure tx dma is allowed(tx flush bit is not set) to use before update WPT*/
  749. if (hal_dma_is_tx_allow(p_dma_info)) {
  750. /*make sure tx dma enabled*/
  751. hal_btif_dma_ctrl(p_dma_info, DMA_CTRL_ENABLE);
  752. /*update WTP to Tx DMA controller's control register*/
  753. btif_reg_sync_writel(wpt | last_wpt_wrap, TX_DMA_VFF_WPT(base));
  754. if ((8 > BTIF_READ32(TX_DMA_VFF_VALID_SIZE(base))) &&
  755. (0 < BTIF_READ32(TX_DMA_VFF_VALID_SIZE(base)))) {
  756. /*0 < valid size in Tx vFIFO < 8 && TX Flush is not in process<should always be done>?
  757. if yes, set flush bit to DMA*/
  758. _tx_dma_flush(p_dma_info);
  759. }
  760. i_ret = len_to_send;
  761. } else {
  762. /*TODO: print error log*/
  763. BTIF_ERR_FUNC("Tx DMA flush operation is in process, this case should never happen,",
  764. "please check if tx operation is allowed before call this API\n");
  765. /*if flush operation is in process , we will return 0*/
  766. i_ret = 0;
  767. }
  768. /*Enable Tx IER*/
  769. btif_tx_dma_ier_ctrl(p_dma_info, true);
  770. BTIF_TRC_FUNC();
  771. return i_ret;
  772. }
  773. /*****************************************************************************
  774. * FUNCTION
  775. * hal_dma_is_tx_complete
  776. * DESCRIPTION
  777. * get tx complete flag
  778. * PARAMETERS
  779. * p_dma_info [IN] pointer to BTIF dma channel's information
  780. * RETURNS
  781. * true means tx complete, false means tx in process
  782. *****************************************************************************/
  783. bool hal_dma_is_tx_complete(P_MTK_DMA_INFO_STR p_dma_info)
  784. {
  785. bool b_ret = -1;
  786. unsigned long base = p_dma_info->base;
  787. unsigned int valid_size = BTIF_READ32(TX_DMA_VFF_VALID_SIZE(base));
  788. unsigned int inter_size = BTIF_READ32(TX_DMA_INT_BUF_SIZE(base));
  789. unsigned int tx_done = is_tx_dma_irq_finish_done(p_dma_info);
  790. /*only when virtual FIFO valid size and Tx channel internal buffer size are both becomes to be 0,
  791. we can identify tx operation finished
  792. confirmed with DE.
  793. */
  794. if ((0 == valid_size) && (0 == inter_size) && (1 == tx_done)) {
  795. b_ret = true;
  796. BTIF_DBG_FUNC("DMA tx finished.\n");
  797. } else {
  798. BTIF_DBG_FUNC
  799. ("DMA tx is in process. vfifo valid size(%d), dma internal size (%d), tx_done(%d)\n",
  800. valid_size, inter_size, tx_done);
  801. b_ret = false;
  802. }
  803. return b_ret;
  804. }
  805. /*****************************************************************************
  806. * FUNCTION
  807. * hal_dma_get_ava_room
  808. * DESCRIPTION
  809. * get tx available room
  810. * PARAMETERS
  811. * p_dma_info [IN] pointer to BTIF dma channel's information
  812. * RETURNS
  813. * available room size
  814. *****************************************************************************/
  815. int hal_dma_get_ava_room(P_MTK_DMA_INFO_STR p_dma_info)
  816. {
  817. int i_ret = -1;
  818. unsigned long base = p_dma_info->base;
  819. /*read vFIFO's left size*/
  820. i_ret = BTIF_READ32(TX_DMA_VFF_LEFT_SIZE(base));
  821. BTIF_DBG_FUNC("DMA tx ava room (%d).\n", i_ret);
  822. if (0 == i_ret)
  823. BTIF_INFO_FUNC("DMA tx vfifo is full.\n");
  824. return i_ret;
  825. }
  826. /*****************************************************************************
  827. * FUNCTION
  828. * hal_dma_is_tx_allow
  829. * DESCRIPTION
  830. * is tx operation allowed by DMA
  831. * PARAMETERS
  832. * p_dma_info [IN] pointer to BTIF dma channel's information
  833. * RETURNS
  834. * true if tx operation is allowed; false if tx is not allowed
  835. *****************************************************************************/
  836. bool hal_dma_is_tx_allow(P_MTK_DMA_INFO_STR p_dma_info)
  837. {
  838. #define MIN_TX_MB ((26 * 1000000 / 13) / 1000000)
  839. #define AVE_TX_MB ((26 * 1000000 / 8) / 1000000)
  840. bool b_ret = false;
  841. unsigned int wait_us = 8 / MIN_TX_MB; /*only ava length */
  842. /*see if flush operation is in process*/
  843. b_ret = _is_tx_dma_in_flush(p_dma_info) ? false : true;
  844. if (!b_ret) {
  845. usleep_range(wait_us, 2 * wait_us);
  846. b_ret = _is_tx_dma_in_flush(p_dma_info) ? false : true;
  847. }
  848. if (!b_ret)
  849. BTIF_WARN_FUNC("btif tx dma is not allowed\n");
  850. /*after Tx flush operation finished, HW will set DMA_EN back to 0 and stop DMA*/
  851. return b_ret;
  852. }
  853. /*****************************************************************************
  854. * FUNCTION
  855. * hal_rx_dma_irq_handler
  856. * DESCRIPTION
  857. * lower level rx interrupt handler
  858. * PARAMETERS
  859. * p_dma_info [IN] pointer to BTIF dma channel's information
  860. * p_buf [IN/OUT] pointer to rx data buffer
  861. * max_len [IN] max length of rx buffer
  862. * RETURNS
  863. * 0 means success, negative means fail
  864. *****************************************************************************/
  865. int hal_rx_dma_irq_handler(P_MTK_DMA_INFO_STR p_dma_info,
  866. unsigned char *p_buf, const unsigned int max_len)
  867. {
  868. int i_ret = -1;
  869. unsigned int valid_len = 0;
  870. unsigned int wpt_wrap = 0;
  871. unsigned int rpt_wrap = 0;
  872. unsigned int wpt = 0;
  873. unsigned int rpt = 0;
  874. unsigned int tail_len = 0;
  875. unsigned int real_len = 0;
  876. unsigned long base = p_dma_info->base;
  877. P_DMA_VFIFO p_vfifo = p_dma_info->p_vfifo;
  878. dma_rx_buf_write rx_cb = p_dma_info->rx_cb;
  879. unsigned char *p_vff_buf = NULL;
  880. unsigned char *vff_base = p_vfifo->p_vir_addr;
  881. unsigned int vff_size = p_vfifo->vfifo_size;
  882. P_MTK_BTIF_DMA_VFIFO p_mtk_vfifo = container_of(p_vfifo,
  883. MTK_BTIF_DMA_VFIFO,
  884. vfifo);
  885. unsigned long flag = 0;
  886. spin_lock_irqsave(&(g_clk_cg_spinlock), flag);
  887. #if defined(CONFIG_MTK_CLKMGR)
  888. if (0 == clock_is_on(MTK_BTIF_APDMA_CLK_CG)) {
  889. spin_unlock_irqrestore(&(g_clk_cg_spinlock), flag);
  890. BTIF_ERR_FUNC("%s: clock is off before irq handle done!!!\n",
  891. __FILE__);
  892. return i_ret;
  893. }
  894. #endif
  895. /*disable DMA Rx IER*/
  896. hal_btif_dma_ier_ctrl(p_dma_info, false);
  897. /*clear Rx DMA's interrupt status*/
  898. BTIF_SET_BIT(RX_DMA_INT_FLAG(base), RX_DMA_INT_DONE | RX_DMA_INT_THRE);
  899. valid_len = BTIF_READ32(RX_DMA_VFF_VALID_SIZE(base));
  900. rpt = BTIF_READ32(RX_DMA_VFF_RPT(base));
  901. wpt = BTIF_READ32(RX_DMA_VFF_WPT(base));
  902. if ((0 == valid_len) && (rpt == wpt)) {
  903. BTIF_DBG_FUNC
  904. ("rx interrupt, no data available in Rx DMA, wpt(0x%08x), rpt(0x%08x)\n",
  905. rpt, wpt);
  906. }
  907. i_ret = 0;
  908. while ((0 < valid_len) || (rpt != wpt)) {
  909. rpt_wrap = rpt & DMA_RPT_WRAP;
  910. wpt_wrap = wpt & DMA_WPT_WRAP;
  911. rpt &= DMA_RPT_MASK;
  912. wpt &= DMA_WPT_MASK;
  913. /*calcaute length of available data in vFIFO*/
  914. if (wpt_wrap != p_mtk_vfifo->last_wpt_wrap)
  915. real_len = wpt + vff_size - rpt;
  916. else
  917. real_len = wpt - rpt;
  918. if (NULL != rx_cb) {
  919. tail_len = vff_size - rpt;
  920. p_vff_buf = vff_base + rpt;
  921. if (tail_len >= real_len) {
  922. (*rx_cb) (p_dma_info, p_vff_buf, real_len);
  923. } else {
  924. (*rx_cb) (p_dma_info, p_vff_buf, tail_len);
  925. p_vff_buf = vff_base;
  926. (*rx_cb) (p_dma_info, p_vff_buf, real_len -
  927. tail_len);
  928. }
  929. i_ret += real_len;
  930. } else
  931. BTIF_ERR_FUNC("no rx_cb found, please check your init process\n");
  932. mb();
  933. rpt += real_len;
  934. if (rpt >= vff_size) {
  935. /*read wrap bit should be revert*/
  936. rpt_wrap ^= DMA_RPT_WRAP;
  937. rpt %= vff_size;
  938. }
  939. rpt |= rpt_wrap;
  940. /*record wpt, last_wpt_wrap, rpt, last_rpt_wrap*/
  941. p_mtk_vfifo->wpt = wpt;
  942. p_mtk_vfifo->last_wpt_wrap = wpt_wrap;
  943. p_mtk_vfifo->rpt = rpt;
  944. p_mtk_vfifo->last_rpt_wrap = rpt_wrap;
  945. /*update rpt information to DMA controller*/
  946. btif_reg_sync_writel(rpt, RX_DMA_VFF_RPT(base));
  947. /*get vff valid size again and check if rx data is processed completely*/
  948. valid_len = BTIF_READ32(RX_DMA_VFF_VALID_SIZE(base));
  949. rpt = BTIF_READ32(RX_DMA_VFF_RPT(base));
  950. wpt = BTIF_READ32(RX_DMA_VFF_WPT(base));
  951. }
  952. /*enable DMA Rx IER*/
  953. hal_btif_dma_ier_ctrl(p_dma_info, true);
  954. spin_unlock_irqrestore(&(g_clk_cg_spinlock), flag);
  955. return i_ret;
  956. }
  957. static int hal_tx_dma_dump_reg(P_MTK_DMA_INFO_STR p_dma_info,
  958. ENUM_BTIF_REG_ID flag)
  959. {
  960. int i_ret = -1;
  961. unsigned long base = p_dma_info->base;
  962. unsigned int int_flag = 0;
  963. unsigned int enable = 0;
  964. unsigned int stop = 0;
  965. unsigned int flush = 0;
  966. unsigned int wpt = 0;
  967. unsigned int rpt = 0;
  968. unsigned int int_buf = 0;
  969. unsigned int valid_size = 0;
  970. /*unsigned long irq_flag = 0;*/
  971. #if defined(CONFIG_MTK_CLKMGR)
  972. /*spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);*/
  973. if (0 == clock_is_on(MTK_BTIF_APDMA_CLK_CG)) {
  974. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  975. BTIF_ERR_FUNC("%s: clock is off, this should never happen!!!\n",
  976. __FILE__);
  977. return i_ret;
  978. }
  979. #endif
  980. int_flag = BTIF_READ32(TX_DMA_INT_FLAG(base));
  981. enable = BTIF_READ32(TX_DMA_EN(base));
  982. stop = BTIF_READ32(TX_DMA_STOP(base));
  983. flush = BTIF_READ32(TX_DMA_FLUSH(base));
  984. wpt = BTIF_READ32(TX_DMA_VFF_WPT(base));
  985. rpt = BTIF_READ32(TX_DMA_VFF_RPT(base));
  986. int_buf = BTIF_READ32(TX_DMA_INT_BUF_SIZE(base));
  987. valid_size = BTIF_READ32(TX_DMA_VFF_VALID_SIZE(base));
  988. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  989. BTIF_INFO_FUNC("DMA's clock is on\n");
  990. BTIF_INFO_FUNC("Tx DMA's base address: 0x%lx\n", base);
  991. if (REG_TX_DMA_ALL == flag) {
  992. BTIF_INFO_FUNC("TX_EN(:0x%x\n", enable);
  993. BTIF_INFO_FUNC("INT_FLAG:0x%x\n", int_flag);
  994. BTIF_INFO_FUNC("TX_STOP:0x%x\n", stop);
  995. BTIF_INFO_FUNC("TX_FLUSH:0x%x\n", flush);
  996. BTIF_INFO_FUNC("TX_WPT:0x%x\n", wpt);
  997. BTIF_INFO_FUNC("TX_RPT:0x%x\n", rpt);
  998. BTIF_INFO_FUNC("INT_BUF_SIZE:0x%x\n", int_buf);
  999. BTIF_INFO_FUNC("VALID_SIZE:0x%x\n", valid_size);
  1000. BTIF_INFO_FUNC("INT_EN:0x%x\n",
  1001. BTIF_READ32(TX_DMA_INT_EN(base)));
  1002. BTIF_INFO_FUNC("TX_RST:0x%x\n", BTIF_READ32(TX_DMA_RST(base)));
  1003. BTIF_INFO_FUNC("VFF_ADDR:0x%x\n",
  1004. BTIF_READ32(TX_DMA_VFF_ADDR(base)));
  1005. BTIF_INFO_FUNC("VFF_LEN:0x%x\n",
  1006. BTIF_READ32(TX_DMA_VFF_LEN(base)));
  1007. BTIF_INFO_FUNC("TX_THRE:0x%x\n",
  1008. BTIF_READ32(TX_DMA_VFF_THRE(base)));
  1009. BTIF_INFO_FUNC("W_INT_BUF_SIZE:0x%x\n",
  1010. BTIF_READ32(TX_DMA_W_INT_BUF_SIZE(base)));
  1011. BTIF_INFO_FUNC("LEFT_SIZE:0x%x\n",
  1012. BTIF_READ32(TX_DMA_VFF_LEFT_SIZE(base)));
  1013. BTIF_INFO_FUNC("DBG_STATUS:0x%x\n",
  1014. BTIF_READ32(TX_DMA_DEBUG_STATUS(base)));
  1015. i_ret = 0;
  1016. } else {
  1017. BTIF_WARN_FUNC("unknown flag:%d\n", flag);
  1018. }
  1019. BTIF_INFO_FUNC("tx dma %s\n", (enable & DMA_EN_BIT) &&
  1020. (!(stop && DMA_STOP_BIT)) ? "enabled" : "stopped");
  1021. BTIF_INFO_FUNC("data in tx dma is %s sent by HW\n",
  1022. ((wpt == rpt) &&
  1023. (int_buf == 0)) ? "completely" : "not completely");
  1024. return i_ret;
  1025. }
  1026. static int hal_rx_dma_dump_reg(P_MTK_DMA_INFO_STR p_dma_info,
  1027. ENUM_BTIF_REG_ID flag)
  1028. {
  1029. int i_ret = -1;
  1030. unsigned long base = p_dma_info->base;
  1031. unsigned int int_flag = 0;
  1032. unsigned int enable = 0;
  1033. unsigned int stop = 0;
  1034. unsigned int flush = 0;
  1035. unsigned int wpt = 0;
  1036. unsigned int rpt = 0;
  1037. unsigned int int_buf = 0;
  1038. unsigned int valid_size = 0;
  1039. /*unsigned long irq_flag = 0;*/
  1040. #if defined(CONFIG_MTK_CLKMGR)
  1041. /*spin_lock_irqsave(&(g_clk_cg_spinlock), irq_flag);*/
  1042. if (0 == clock_is_on(MTK_BTIF_APDMA_CLK_CG)) {
  1043. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  1044. BTIF_ERR_FUNC("%s: clock is off, this should never happen!!!\n",
  1045. __FILE__);
  1046. return i_ret;
  1047. }
  1048. #endif
  1049. BTIF_INFO_FUNC("dump DMA status register\n");
  1050. _btif_dma_dump_dbg_reg();
  1051. int_flag = BTIF_READ32(RX_DMA_INT_FLAG(base));
  1052. enable = BTIF_READ32(RX_DMA_EN(base));
  1053. stop = BTIF_READ32(RX_DMA_STOP(base));
  1054. flush = BTIF_READ32(RX_DMA_FLUSH(base));
  1055. wpt = BTIF_READ32(RX_DMA_VFF_WPT(base));
  1056. rpt = BTIF_READ32(RX_DMA_VFF_RPT(base));
  1057. int_buf = BTIF_READ32(RX_DMA_INT_BUF_SIZE(base));
  1058. valid_size = BTIF_READ32(RX_DMA_VFF_VALID_SIZE(base));
  1059. /*spin_unlock_irqrestore(&(g_clk_cg_spinlock), irq_flag);*/
  1060. BTIF_INFO_FUNC("DMA's clock is on\n");
  1061. BTIF_INFO_FUNC("Rx DMA's base address: 0x%lx\n", base);
  1062. if (REG_RX_DMA_ALL == flag) {
  1063. BTIF_INFO_FUNC("RX_EN(:0x%x\n", enable);
  1064. BTIF_INFO_FUNC("RX_STOP:0x%x\n", stop);
  1065. BTIF_INFO_FUNC("RX_FLUSH:0x%x\n", flush);
  1066. BTIF_INFO_FUNC("INT_FLAG:0x%x\n", int_flag);
  1067. BTIF_INFO_FUNC("RX_WPT:0x%x\n", wpt);
  1068. BTIF_INFO_FUNC("RX_RPT:0x%x\n", rpt);
  1069. BTIF_INFO_FUNC("INT_BUF_SIZE:0x%x\n", int_buf);
  1070. BTIF_INFO_FUNC("VALID_SIZE:0x%x\n", valid_size);
  1071. BTIF_INFO_FUNC("INT_EN:0x%x\n",
  1072. BTIF_READ32(RX_DMA_INT_EN(base)));
  1073. BTIF_INFO_FUNC("RX_RST:0x%x\n", BTIF_READ32(RX_DMA_RST(base)));
  1074. BTIF_INFO_FUNC("VFF_ADDR:0x%x\n",
  1075. BTIF_READ32(RX_DMA_VFF_ADDR(base)));
  1076. BTIF_INFO_FUNC("VFF_LEN:0x%x\n",
  1077. BTIF_READ32(RX_DMA_VFF_LEN(base)));
  1078. BTIF_INFO_FUNC("RX_THRE:0x%x\n",
  1079. BTIF_READ32(RX_DMA_VFF_THRE(base)));
  1080. BTIF_INFO_FUNC("RX_FLOW_CTRL_THRE:0x%x\n",
  1081. BTIF_READ32(RX_DMA_FLOW_CTRL_THRE(base)));
  1082. BTIF_INFO_FUNC("LEFT_SIZE:0x%x\n",
  1083. BTIF_READ32(RX_DMA_VFF_LEFT_SIZE(base)));
  1084. BTIF_INFO_FUNC("DBG_STATUS:0x%x\n",
  1085. BTIF_READ32(RX_DMA_DEBUG_STATUS(base)));
  1086. i_ret = 0;
  1087. } else {
  1088. BTIF_WARN_FUNC("unknown flag:%d\n", flag);
  1089. }
  1090. BTIF_INFO_FUNC("rx dma %s\n", (enable & DMA_EN_BIT) &&
  1091. (!(stop && DMA_STOP_BIT)) ? "enabled" : "stopped");
  1092. BTIF_INFO_FUNC("data in rx dma is %s by driver\n",
  1093. ((wpt == rpt) &&
  1094. (int_buf == 0)) ? "received" : "not received");
  1095. return i_ret;
  1096. }
  1097. /*****************************************************************************
  1098. * FUNCTION
  1099. * hal_dma_dump_reg
  1100. * DESCRIPTION
  1101. * dump BTIF module's information when needed
  1102. * PARAMETERS
  1103. * p_dma_info [IN] pointer to BTIF dma channel's information
  1104. * flag [IN] register id flag
  1105. * RETURNS
  1106. * 0 means success, negative means fail
  1107. *****************************************************************************/
  1108. int hal_dma_dump_reg(P_MTK_DMA_INFO_STR p_dma_info, ENUM_BTIF_REG_ID flag)
  1109. {
  1110. unsigned int i_ret = -1;
  1111. if (DMA_DIR_TX == p_dma_info->dir)
  1112. i_ret = hal_tx_dma_dump_reg(p_dma_info, flag);
  1113. else if (DMA_DIR_RX == p_dma_info->dir)
  1114. i_ret = hal_rx_dma_dump_reg(p_dma_info, flag);
  1115. else
  1116. BTIF_WARN_FUNC("unknown dir:%d\n", p_dma_info->dir);
  1117. return i_ret;
  1118. }
  1119. static int _tx_dma_flush(P_MTK_DMA_INFO_STR p_dma_info)
  1120. {
  1121. unsigned int i_ret = -1;
  1122. unsigned long base = p_dma_info->base;
  1123. unsigned int stop = BTIF_READ32(TX_DMA_STOP(base));
  1124. /*in MTK DMA BTIF channel we cannot set STOP and FLUSH bit at the same time*/
  1125. if ((DMA_STOP_BIT && stop) != 0)
  1126. BTIF_ERR_FUNC("BTIF's DMA in stop state, omit flush operation\n");
  1127. else {
  1128. BTIF_DBG_FUNC("flush tx dma\n");
  1129. BTIF_SET_BIT(TX_DMA_FLUSH(base), DMA_FLUSH_BIT);
  1130. i_ret = 0;
  1131. }
  1132. return i_ret;
  1133. }
  1134. static int _is_tx_dma_in_flush(P_MTK_DMA_INFO_STR p_dma_info)
  1135. {
  1136. bool b_ret = true;
  1137. unsigned long base = p_dma_info->base;
  1138. /*see if flush operation is in process*/
  1139. b_ret =
  1140. ((DMA_FLUSH_BIT & BTIF_READ32(TX_DMA_FLUSH(base))) !=
  1141. 0) ? true : false;
  1142. return b_ret;
  1143. }
  1144. int hal_dma_pm_ops(P_MTK_DMA_INFO_STR p_dma_info, MTK_BTIF_PM_OPID opid)
  1145. {
  1146. int i_ret = -1;
  1147. BTIF_INFO_FUNC("op id: %d\n", opid);
  1148. switch (opid) {
  1149. case BTIF_PM_DPIDLE_EN:
  1150. i_ret = 0;
  1151. break;
  1152. case BTIF_PM_DPIDLE_DIS:
  1153. i_ret = 0;
  1154. break;
  1155. case BTIF_PM_SUSPEND:
  1156. i_ret = 0;
  1157. break;
  1158. case BTIF_PM_RESUME:
  1159. i_ret = 0;
  1160. break;
  1161. case BTIF_PM_RESTORE_NOIRQ:{
  1162. unsigned int flag = 0;
  1163. P_MTK_BTIF_IRQ_STR p_irq = p_dma_info->p_irq;
  1164. #ifdef CONFIG_OF
  1165. flag = p_irq->irq_flags;
  1166. #else
  1167. switch (p_irq->sens_type) {
  1168. case IRQ_SENS_EDGE:
  1169. if (IRQ_EDGE_FALL == p_irq->edge_type)
  1170. flag = IRQF_TRIGGER_FALLING;
  1171. else if (IRQ_EDGE_RAISE == p_irq->edge_type)
  1172. flag = IRQF_TRIGGER_RISING;
  1173. else if (IRQ_EDGE_BOTH == p_irq->edge_type)
  1174. flag = IRQF_TRIGGER_RISING |
  1175. IRQF_TRIGGER_FALLING;
  1176. else
  1177. flag = IRQF_TRIGGER_FALLING; /*make this as default type */
  1178. break;
  1179. case IRQ_SENS_LVL:
  1180. if (IRQ_LVL_LOW == p_irq->lvl_type)
  1181. flag = IRQF_TRIGGER_LOW;
  1182. else if (IRQ_LVL_HIGH == p_irq->lvl_type)
  1183. flag = IRQF_TRIGGER_HIGH;
  1184. else
  1185. flag = IRQF_TRIGGER_LOW; /*make this as default type */
  1186. break;
  1187. default:
  1188. flag = IRQF_TRIGGER_LOW; /*make this as default type */
  1189. break;
  1190. }
  1191. #endif
  1192. /* irq_set_irq_type(p_irq->irq_id, flag); */
  1193. i_ret = 0;
  1194. }
  1195. i_ret = 0;
  1196. break;
  1197. default:
  1198. i_ret = ERR_INVALID_PAR;
  1199. break;
  1200. }
  1201. return i_ret;
  1202. }
  1203. /*****************************************************************************
  1204. * FUNCTION
  1205. * hal_dma_receive_data
  1206. * DESCRIPTION
  1207. * receive data from btif module in DMA polling mode
  1208. * PARAMETERS
  1209. * p_dma_info [IN] pointer to BTIF dma channel's information
  1210. * p_buf [IN/OUT] pointer to rx data buffer
  1211. * max_len [IN] max length of rx buffer
  1212. * RETURNS
  1213. * positive means data is available, 0 means no data available
  1214. *****************************************************************************/
  1215. #ifndef MTK_BTIF_MARK_UNUSED_API
  1216. int hal_dma_receive_data(P_MTK_DMA_INFO_STR p_dma_info,
  1217. unsigned char *p_buf, const unsigned int max_len)
  1218. {
  1219. unsigned int i_ret = -1;
  1220. return i_ret;
  1221. }
  1222. #endif
  1223. int _btif_dma_dump_dbg_reg(void)
  1224. {
  1225. #if 0
  1226. static MTK_BTIF_DMA_REG_DMP_DBG g_dma_dbg_regs[] = {
  1227. {0x10201180, 0x0},
  1228. {0x10201184, 0x0},
  1229. {0x10201188, 0x0},
  1230. {0x1020118C, 0x0},
  1231. {0x10201190, 0x0},
  1232. {0x1000320C, 0x0},
  1233. {0x10003210, 0x0},
  1234. {0x10003214, 0x0},
  1235. };
  1236. int i = 0;
  1237. char *addr1 = NULL;
  1238. char *addr2 = NULL;
  1239. int array_num = sizeof(g_dma_dbg_regs) / sizeof(g_dma_dbg_regs[0]);
  1240. addr1 = ioremap(g_dma_dbg_regs[0].reg_addr, 0x20);
  1241. if (addr1) {
  1242. for (i = 0; i < 5; i++)
  1243. g_dma_dbg_regs[i].reg_val = *(volatile unsigned int*)(addr1 + i*4);
  1244. iounmap(addr1);
  1245. }
  1246. addr2 = ioremap(g_dma_dbg_regs[5].reg_addr, 0x10);
  1247. if (addr2) {
  1248. g_dma_dbg_regs[5].reg_val = *(volatile unsigned int*)(addr2);
  1249. g_dma_dbg_regs[6].reg_val = *(volatile unsigned int*)(addr2+4);
  1250. g_dma_dbg_regs[7].reg_val = *(volatile unsigned int*)(addr2+8);
  1251. iounmap(addr2);
  1252. }
  1253. for (i = 0; i < array_num; i++)
  1254. BTIF_INFO_FUNC("<reg, val>-<0x%lx, 0x%08x>\n", g_dma_dbg_regs[i].reg_addr, g_dma_dbg_regs[i].reg_val);
  1255. #endif
  1256. return 0;
  1257. }