ar9003_mci.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/export.h>
  17. #include "hw.h"
  18. #include "hw-ops.h"
  19. #include "ar9003_phy.h"
  20. #include "ar9003_mci.h"
  21. static void ar9003_mci_reset_req_wakeup(struct ath_hw *ah)
  22. {
  23. REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
  24. AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 1);
  25. udelay(1);
  26. REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
  27. AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 0);
  28. }
  29. static int ar9003_mci_wait_for_interrupt(struct ath_hw *ah, u32 address,
  30. u32 bit_position, int time_out)
  31. {
  32. struct ath_common *common = ath9k_hw_common(ah);
  33. while (time_out) {
  34. if (!(REG_READ(ah, address) & bit_position)) {
  35. udelay(10);
  36. time_out -= 10;
  37. if (time_out < 0)
  38. break;
  39. else
  40. continue;
  41. }
  42. REG_WRITE(ah, address, bit_position);
  43. if (address != AR_MCI_INTERRUPT_RX_MSG_RAW)
  44. break;
  45. if (bit_position & AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE)
  46. ar9003_mci_reset_req_wakeup(ah);
  47. if (bit_position & (AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING |
  48. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING))
  49. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  50. AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
  51. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_RX_MSG);
  52. break;
  53. }
  54. if (time_out <= 0) {
  55. ath_dbg(common, MCI,
  56. "MCI Wait for Reg 0x%08x = 0x%08x timeout\n",
  57. address, bit_position);
  58. ath_dbg(common, MCI,
  59. "MCI INT_RAW = 0x%08x, RX_MSG_RAW = 0x%08x\n",
  60. REG_READ(ah, AR_MCI_INTERRUPT_RAW),
  61. REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
  62. time_out = 0;
  63. }
  64. return time_out;
  65. }
  66. static void ar9003_mci_remote_reset(struct ath_hw *ah, bool wait_done)
  67. {
  68. u32 payload[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00};
  69. ar9003_mci_send_message(ah, MCI_REMOTE_RESET, 0, payload, 16,
  70. wait_done, false);
  71. udelay(5);
  72. }
  73. static void ar9003_mci_send_lna_transfer(struct ath_hw *ah, bool wait_done)
  74. {
  75. u32 payload = 0x00000000;
  76. ar9003_mci_send_message(ah, MCI_LNA_TRANS, 0, &payload, 1,
  77. wait_done, false);
  78. }
  79. static void ar9003_mci_send_req_wake(struct ath_hw *ah, bool wait_done)
  80. {
  81. ar9003_mci_send_message(ah, MCI_REQ_WAKE, MCI_FLAG_DISABLE_TIMESTAMP,
  82. NULL, 0, wait_done, false);
  83. udelay(5);
  84. }
  85. static void ar9003_mci_send_sys_waking(struct ath_hw *ah, bool wait_done)
  86. {
  87. ar9003_mci_send_message(ah, MCI_SYS_WAKING, MCI_FLAG_DISABLE_TIMESTAMP,
  88. NULL, 0, wait_done, false);
  89. }
  90. static void ar9003_mci_send_lna_take(struct ath_hw *ah, bool wait_done)
  91. {
  92. u32 payload = 0x70000000;
  93. ar9003_mci_send_message(ah, MCI_LNA_TAKE, 0, &payload, 1,
  94. wait_done, false);
  95. }
  96. static void ar9003_mci_send_sys_sleeping(struct ath_hw *ah, bool wait_done)
  97. {
  98. ar9003_mci_send_message(ah, MCI_SYS_SLEEPING,
  99. MCI_FLAG_DISABLE_TIMESTAMP,
  100. NULL, 0, wait_done, false);
  101. }
  102. static void ar9003_mci_send_coex_version_query(struct ath_hw *ah,
  103. bool wait_done)
  104. {
  105. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  106. u32 payload[4] = {0, 0, 0, 0};
  107. if (mci->bt_version_known ||
  108. (mci->bt_state == MCI_BT_SLEEP))
  109. return;
  110. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  111. MCI_GPM_COEX_VERSION_QUERY);
  112. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  113. }
  114. static void ar9003_mci_send_coex_version_response(struct ath_hw *ah,
  115. bool wait_done)
  116. {
  117. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  118. u32 payload[4] = {0, 0, 0, 0};
  119. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  120. MCI_GPM_COEX_VERSION_RESPONSE);
  121. *(((u8 *)payload) + MCI_GPM_COEX_B_MAJOR_VERSION) =
  122. mci->wlan_ver_major;
  123. *(((u8 *)payload) + MCI_GPM_COEX_B_MINOR_VERSION) =
  124. mci->wlan_ver_minor;
  125. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  126. }
  127. static void ar9003_mci_send_coex_wlan_channels(struct ath_hw *ah,
  128. bool wait_done)
  129. {
  130. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  131. u32 *payload = &mci->wlan_channels[0];
  132. if (!mci->wlan_channels_update ||
  133. (mci->bt_state == MCI_BT_SLEEP))
  134. return;
  135. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  136. MCI_GPM_COEX_WLAN_CHANNELS);
  137. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  138. MCI_GPM_SET_TYPE_OPCODE(payload, 0xff, 0xff);
  139. }
  140. static void ar9003_mci_send_coex_bt_status_query(struct ath_hw *ah,
  141. bool wait_done, u8 query_type)
  142. {
  143. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  144. u32 payload[4] = {0, 0, 0, 0};
  145. bool query_btinfo;
  146. if (mci->bt_state == MCI_BT_SLEEP)
  147. return;
  148. query_btinfo = !!(query_type & (MCI_GPM_COEX_QUERY_BT_ALL_INFO |
  149. MCI_GPM_COEX_QUERY_BT_TOPOLOGY));
  150. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  151. MCI_GPM_COEX_STATUS_QUERY);
  152. *(((u8 *)payload) + MCI_GPM_COEX_B_BT_BITMAP) = query_type;
  153. /*
  154. * If bt_status_query message is not sent successfully,
  155. * then need_flush_btinfo should be set again.
  156. */
  157. if (!ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
  158. wait_done, true)) {
  159. if (query_btinfo)
  160. mci->need_flush_btinfo = true;
  161. }
  162. if (query_btinfo)
  163. mci->query_bt = false;
  164. }
  165. static void ar9003_mci_send_coex_halt_bt_gpm(struct ath_hw *ah, bool halt,
  166. bool wait_done)
  167. {
  168. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  169. u32 payload[4] = {0, 0, 0, 0};
  170. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  171. MCI_GPM_COEX_HALT_BT_GPM);
  172. if (halt) {
  173. mci->query_bt = true;
  174. /* Send next unhalt no matter halt sent or not */
  175. mci->unhalt_bt_gpm = true;
  176. mci->need_flush_btinfo = true;
  177. *(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
  178. MCI_GPM_COEX_BT_GPM_HALT;
  179. } else
  180. *(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
  181. MCI_GPM_COEX_BT_GPM_UNHALT;
  182. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  183. }
  184. static void ar9003_mci_prep_interface(struct ath_hw *ah)
  185. {
  186. struct ath_common *common = ath9k_hw_common(ah);
  187. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  188. u32 saved_mci_int_en;
  189. u32 mci_timeout = 150;
  190. mci->bt_state = MCI_BT_SLEEP;
  191. saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
  192. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  193. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  194. REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
  195. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  196. REG_READ(ah, AR_MCI_INTERRUPT_RAW));
  197. ar9003_mci_remote_reset(ah, true);
  198. ar9003_mci_send_req_wake(ah, true);
  199. if (!ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  200. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING, 500))
  201. goto clear_redunt;
  202. mci->bt_state = MCI_BT_AWAKE;
  203. /*
  204. * we don't need to send more remote_reset at this moment.
  205. * If BT receive first remote_reset, then BT HW will
  206. * be cleaned up and will be able to receive req_wake
  207. * and BT HW will respond sys_waking.
  208. * In this case, WLAN will receive BT's HW sys_waking.
  209. * Otherwise, if BT SW missed initial remote_reset,
  210. * that remote_reset will still clean up BT MCI RX,
  211. * and the req_wake will wake BT up,
  212. * and BT SW will respond this req_wake with a remote_reset and
  213. * sys_waking. In this case, WLAN will receive BT's SW
  214. * sys_waking. In either case, BT's RX is cleaned up. So we
  215. * don't need to reply BT's remote_reset now, if any.
  216. * Similarly, if in any case, WLAN can receive BT's sys_waking,
  217. * that means WLAN's RX is also fine.
  218. */
  219. ar9003_mci_send_sys_waking(ah, true);
  220. udelay(10);
  221. /*
  222. * Set BT priority interrupt value to be 0xff to
  223. * avoid having too many BT PRIORITY interrupts.
  224. */
  225. REG_WRITE(ah, AR_MCI_BT_PRI0, 0xFFFFFFFF);
  226. REG_WRITE(ah, AR_MCI_BT_PRI1, 0xFFFFFFFF);
  227. REG_WRITE(ah, AR_MCI_BT_PRI2, 0xFFFFFFFF);
  228. REG_WRITE(ah, AR_MCI_BT_PRI3, 0xFFFFFFFF);
  229. REG_WRITE(ah, AR_MCI_BT_PRI, 0X000000FF);
  230. /*
  231. * A contention reset will be received after send out
  232. * sys_waking. Also BT priority interrupt bits will be set.
  233. * Clear those bits before the next step.
  234. */
  235. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  236. AR_MCI_INTERRUPT_RX_MSG_CONT_RST);
  237. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_BT_PRI);
  238. if (mci->is_2g) {
  239. ar9003_mci_send_lna_transfer(ah, true);
  240. udelay(5);
  241. }
  242. if ((mci->is_2g && !mci->update_2g5g)) {
  243. if (ar9003_mci_wait_for_interrupt(ah,
  244. AR_MCI_INTERRUPT_RX_MSG_RAW,
  245. AR_MCI_INTERRUPT_RX_MSG_LNA_INFO,
  246. mci_timeout))
  247. ath_dbg(common, MCI,
  248. "MCI WLAN has control over the LNA & BT obeys it\n");
  249. else
  250. ath_dbg(common, MCI,
  251. "MCI BT didn't respond to LNA_TRANS\n");
  252. }
  253. clear_redunt:
  254. /* Clear the extra redundant SYS_WAKING from BT */
  255. if ((mci->bt_state == MCI_BT_AWAKE) &&
  256. (REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  257. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING)) &&
  258. (REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  259. AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) == 0)) {
  260. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  261. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING);
  262. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  263. AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
  264. }
  265. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
  266. }
  267. void ar9003_mci_set_full_sleep(struct ath_hw *ah)
  268. {
  269. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  270. if (ar9003_mci_state(ah, MCI_STATE_ENABLE) &&
  271. (mci->bt_state != MCI_BT_SLEEP) &&
  272. !mci->halted_bt_gpm) {
  273. ar9003_mci_send_coex_halt_bt_gpm(ah, true, true);
  274. }
  275. mci->ready = false;
  276. }
  277. static void ar9003_mci_disable_interrupt(struct ath_hw *ah)
  278. {
  279. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  280. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
  281. }
  282. static void ar9003_mci_enable_interrupt(struct ath_hw *ah)
  283. {
  284. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, AR_MCI_INTERRUPT_DEFAULT);
  285. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN,
  286. AR_MCI_INTERRUPT_RX_MSG_DEFAULT);
  287. }
  288. static bool ar9003_mci_check_int(struct ath_hw *ah, u32 ints)
  289. {
  290. u32 intr;
  291. intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
  292. return ((intr & ints) == ints);
  293. }
  294. void ar9003_mci_get_interrupt(struct ath_hw *ah, u32 *raw_intr,
  295. u32 *rx_msg_intr)
  296. {
  297. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  298. *raw_intr = mci->raw_intr;
  299. *rx_msg_intr = mci->rx_msg_intr;
  300. /* Clean int bits after the values are read. */
  301. mci->raw_intr = 0;
  302. mci->rx_msg_intr = 0;
  303. }
  304. EXPORT_SYMBOL(ar9003_mci_get_interrupt);
  305. void ar9003_mci_get_isr(struct ath_hw *ah, enum ath9k_int *masked)
  306. {
  307. struct ath_common *common = ath9k_hw_common(ah);
  308. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  309. u32 raw_intr, rx_msg_intr;
  310. rx_msg_intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
  311. raw_intr = REG_READ(ah, AR_MCI_INTERRUPT_RAW);
  312. if ((raw_intr == 0xdeadbeef) || (rx_msg_intr == 0xdeadbeef)) {
  313. ath_dbg(common, MCI,
  314. "MCI gets 0xdeadbeef during int processing\n");
  315. } else {
  316. mci->rx_msg_intr |= rx_msg_intr;
  317. mci->raw_intr |= raw_intr;
  318. *masked |= ATH9K_INT_MCI;
  319. if (rx_msg_intr & AR_MCI_INTERRUPT_RX_MSG_CONT_INFO)
  320. mci->cont_status = REG_READ(ah, AR_MCI_CONT_STATUS);
  321. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, rx_msg_intr);
  322. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, raw_intr);
  323. }
  324. }
  325. static void ar9003_mci_2g5g_changed(struct ath_hw *ah, bool is_2g)
  326. {
  327. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  328. if (!mci->update_2g5g &&
  329. (mci->is_2g != is_2g))
  330. mci->update_2g5g = true;
  331. mci->is_2g = is_2g;
  332. }
  333. static bool ar9003_mci_is_gpm_valid(struct ath_hw *ah, u32 msg_index)
  334. {
  335. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  336. u32 *payload;
  337. u32 recv_type, offset;
  338. if (msg_index == MCI_GPM_INVALID)
  339. return false;
  340. offset = msg_index << 4;
  341. payload = (u32 *)(mci->gpm_buf + offset);
  342. recv_type = MCI_GPM_TYPE(payload);
  343. if (recv_type == MCI_GPM_RSVD_PATTERN)
  344. return false;
  345. return true;
  346. }
  347. static void ar9003_mci_observation_set_up(struct ath_hw *ah)
  348. {
  349. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  350. if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MCI) {
  351. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_DATA);
  352. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_CLK);
  353. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
  354. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
  355. } else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_TXRX) {
  356. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_WL_IN_TX);
  357. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_WL_IN_RX);
  358. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
  359. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
  360. ath9k_hw_cfg_output(ah, 5, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  361. } else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_BT) {
  362. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
  363. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
  364. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
  365. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
  366. } else
  367. return;
  368. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
  369. REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL, AR_GLB_DS_JTAG_DISABLE, 1);
  370. REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL, AR_GLB_WLAN_UART_INTF_EN, 0);
  371. REG_SET_BIT(ah, AR_GLB_GPIO_CONTROL, ATH_MCI_CONFIG_MCI_OBS_GPIO);
  372. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_GPIO_OBS_SEL, 0);
  373. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_MAC_BB_OBS_SEL, 1);
  374. REG_WRITE(ah, AR_OBS, 0x4b);
  375. REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL1, 0x03);
  376. REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL2, 0x01);
  377. REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_LSB, 0x02);
  378. REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_MSB, 0x03);
  379. REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS,
  380. AR_PHY_TEST_CTL_DEBUGPORT_SEL, 0x07);
  381. }
  382. static bool ar9003_mci_send_coex_bt_flags(struct ath_hw *ah, bool wait_done,
  383. u8 opcode, u32 bt_flags)
  384. {
  385. u32 pld[4] = {0, 0, 0, 0};
  386. MCI_GPM_SET_TYPE_OPCODE(pld, MCI_GPM_COEX_AGENT,
  387. MCI_GPM_COEX_BT_UPDATE_FLAGS);
  388. *(((u8 *)pld) + MCI_GPM_COEX_B_BT_FLAGS_OP) = opcode;
  389. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 0) = bt_flags & 0xFF;
  390. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 1) = (bt_flags >> 8) & 0xFF;
  391. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 2) = (bt_flags >> 16) & 0xFF;
  392. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 3) = (bt_flags >> 24) & 0xFF;
  393. return ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16,
  394. wait_done, true);
  395. }
  396. static void ar9003_mci_sync_bt_state(struct ath_hw *ah)
  397. {
  398. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  399. u32 cur_bt_state;
  400. cur_bt_state = ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP);
  401. if (mci->bt_state != cur_bt_state)
  402. mci->bt_state = cur_bt_state;
  403. if (mci->bt_state != MCI_BT_SLEEP) {
  404. ar9003_mci_send_coex_version_query(ah, true);
  405. ar9003_mci_send_coex_wlan_channels(ah, true);
  406. if (mci->unhalt_bt_gpm == true)
  407. ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
  408. }
  409. }
  410. void ar9003_mci_check_bt(struct ath_hw *ah)
  411. {
  412. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  413. if (!mci_hw->ready)
  414. return;
  415. /*
  416. * check BT state again to make
  417. * sure it's not changed.
  418. */
  419. ar9003_mci_sync_bt_state(ah);
  420. ar9003_mci_2g5g_switch(ah, true);
  421. if ((mci_hw->bt_state == MCI_BT_AWAKE) &&
  422. (mci_hw->query_bt == true)) {
  423. mci_hw->need_flush_btinfo = true;
  424. }
  425. }
  426. static void ar9003_mci_process_gpm_extra(struct ath_hw *ah, u8 gpm_type,
  427. u8 gpm_opcode, u32 *p_gpm)
  428. {
  429. struct ath_common *common = ath9k_hw_common(ah);
  430. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  431. u8 *p_data = (u8 *) p_gpm;
  432. if (gpm_type != MCI_GPM_COEX_AGENT)
  433. return;
  434. switch (gpm_opcode) {
  435. case MCI_GPM_COEX_VERSION_QUERY:
  436. ath_dbg(common, MCI, "MCI Recv GPM COEX Version Query\n");
  437. ar9003_mci_send_coex_version_response(ah, true);
  438. break;
  439. case MCI_GPM_COEX_VERSION_RESPONSE:
  440. ath_dbg(common, MCI, "MCI Recv GPM COEX Version Response\n");
  441. mci->bt_ver_major =
  442. *(p_data + MCI_GPM_COEX_B_MAJOR_VERSION);
  443. mci->bt_ver_minor =
  444. *(p_data + MCI_GPM_COEX_B_MINOR_VERSION);
  445. mci->bt_version_known = true;
  446. ath_dbg(common, MCI, "MCI BT Coex version: %d.%d\n",
  447. mci->bt_ver_major, mci->bt_ver_minor);
  448. break;
  449. case MCI_GPM_COEX_STATUS_QUERY:
  450. ath_dbg(common, MCI,
  451. "MCI Recv GPM COEX Status Query = 0x%02X\n",
  452. *(p_data + MCI_GPM_COEX_B_WLAN_BITMAP));
  453. mci->wlan_channels_update = true;
  454. ar9003_mci_send_coex_wlan_channels(ah, true);
  455. break;
  456. case MCI_GPM_COEX_BT_PROFILE_INFO:
  457. mci->query_bt = true;
  458. ath_dbg(common, MCI, "MCI Recv GPM COEX BT_Profile_Info\n");
  459. break;
  460. case MCI_GPM_COEX_BT_STATUS_UPDATE:
  461. mci->query_bt = true;
  462. ath_dbg(common, MCI,
  463. "MCI Recv GPM COEX BT_Status_Update SEQ=%d (drop&query)\n",
  464. *(p_gpm + 3));
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. static u32 ar9003_mci_wait_for_gpm(struct ath_hw *ah, u8 gpm_type,
  471. u8 gpm_opcode, int time_out)
  472. {
  473. struct ath_common *common = ath9k_hw_common(ah);
  474. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  475. u32 *p_gpm = NULL, mismatch = 0, more_data;
  476. u32 offset;
  477. u8 recv_type = 0, recv_opcode = 0;
  478. bool b_is_bt_cal_done = (gpm_type == MCI_GPM_BT_CAL_DONE);
  479. more_data = time_out ? MCI_GPM_NOMORE : MCI_GPM_MORE;
  480. while (time_out > 0) {
  481. if (p_gpm) {
  482. MCI_GPM_RECYCLE(p_gpm);
  483. p_gpm = NULL;
  484. }
  485. if (more_data != MCI_GPM_MORE)
  486. time_out = ar9003_mci_wait_for_interrupt(ah,
  487. AR_MCI_INTERRUPT_RX_MSG_RAW,
  488. AR_MCI_INTERRUPT_RX_MSG_GPM,
  489. time_out);
  490. if (!time_out)
  491. break;
  492. offset = ar9003_mci_get_next_gpm_offset(ah, false, &more_data);
  493. if (offset == MCI_GPM_INVALID)
  494. continue;
  495. p_gpm = (u32 *) (mci->gpm_buf + offset);
  496. recv_type = MCI_GPM_TYPE(p_gpm);
  497. recv_opcode = MCI_GPM_OPCODE(p_gpm);
  498. if (MCI_GPM_IS_CAL_TYPE(recv_type)) {
  499. if (recv_type == gpm_type) {
  500. if ((gpm_type == MCI_GPM_BT_CAL_DONE) &&
  501. !b_is_bt_cal_done) {
  502. gpm_type = MCI_GPM_BT_CAL_GRANT;
  503. continue;
  504. }
  505. break;
  506. }
  507. } else if ((recv_type == gpm_type) &&
  508. (recv_opcode == gpm_opcode))
  509. break;
  510. /*
  511. * check if it's cal_grant
  512. *
  513. * When we're waiting for cal_grant in reset routine,
  514. * it's possible that BT sends out cal_request at the
  515. * same time. Since BT's calibration doesn't happen
  516. * that often, we'll let BT completes calibration then
  517. * we continue to wait for cal_grant from BT.
  518. * Orginal: Wait BT_CAL_GRANT.
  519. * New: Receive BT_CAL_REQ -> send WLAN_CAL_GRANT->wait
  520. * BT_CAL_DONE -> Wait BT_CAL_GRANT.
  521. */
  522. if ((gpm_type == MCI_GPM_BT_CAL_GRANT) &&
  523. (recv_type == MCI_GPM_BT_CAL_REQ)) {
  524. u32 payload[4] = {0, 0, 0, 0};
  525. gpm_type = MCI_GPM_BT_CAL_DONE;
  526. MCI_GPM_SET_CAL_TYPE(payload,
  527. MCI_GPM_WLAN_CAL_GRANT);
  528. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
  529. false, false);
  530. continue;
  531. } else {
  532. ath_dbg(common, MCI, "MCI GPM subtype not match 0x%x\n",
  533. *(p_gpm + 1));
  534. mismatch++;
  535. ar9003_mci_process_gpm_extra(ah, recv_type,
  536. recv_opcode, p_gpm);
  537. }
  538. }
  539. if (p_gpm) {
  540. MCI_GPM_RECYCLE(p_gpm);
  541. p_gpm = NULL;
  542. }
  543. if (time_out <= 0)
  544. time_out = 0;
  545. while (more_data == MCI_GPM_MORE) {
  546. offset = ar9003_mci_get_next_gpm_offset(ah, false, &more_data);
  547. if (offset == MCI_GPM_INVALID)
  548. break;
  549. p_gpm = (u32 *) (mci->gpm_buf + offset);
  550. recv_type = MCI_GPM_TYPE(p_gpm);
  551. recv_opcode = MCI_GPM_OPCODE(p_gpm);
  552. if (!MCI_GPM_IS_CAL_TYPE(recv_type))
  553. ar9003_mci_process_gpm_extra(ah, recv_type,
  554. recv_opcode, p_gpm);
  555. MCI_GPM_RECYCLE(p_gpm);
  556. }
  557. return time_out;
  558. }
  559. bool ar9003_mci_start_reset(struct ath_hw *ah, struct ath9k_channel *chan)
  560. {
  561. struct ath_common *common = ath9k_hw_common(ah);
  562. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  563. u32 payload[4] = {0, 0, 0, 0};
  564. ar9003_mci_2g5g_changed(ah, IS_CHAN_2GHZ(chan));
  565. if (mci_hw->bt_state != MCI_BT_CAL_START)
  566. return false;
  567. mci_hw->bt_state = MCI_BT_CAL;
  568. /*
  569. * MCI FIX: disable mci interrupt here. This is to avoid
  570. * SW_MSG_DONE or RX_MSG bits to trigger MCI_INT and
  571. * lead to mci_intr reentry.
  572. */
  573. ar9003_mci_disable_interrupt(ah);
  574. MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_GRANT);
  575. ar9003_mci_send_message(ah, MCI_GPM, 0, payload,
  576. 16, true, false);
  577. /* Wait BT calibration to be completed for 25ms */
  578. if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_DONE,
  579. 0, 25000))
  580. ath_dbg(common, MCI, "MCI BT_CAL_DONE received\n");
  581. else
  582. ath_dbg(common, MCI,
  583. "MCI BT_CAL_DONE not received\n");
  584. mci_hw->bt_state = MCI_BT_AWAKE;
  585. /* MCI FIX: enable mci interrupt here */
  586. ar9003_mci_enable_interrupt(ah);
  587. return true;
  588. }
  589. EXPORT_SYMBOL(ar9003_mci_start_reset);
  590. int ar9003_mci_end_reset(struct ath_hw *ah, struct ath9k_channel *chan,
  591. struct ath9k_hw_cal_data *caldata)
  592. {
  593. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  594. if (!mci_hw->ready)
  595. return 0;
  596. if (!IS_CHAN_2GHZ(chan) || (mci_hw->bt_state != MCI_BT_SLEEP))
  597. goto exit;
  598. if (!ar9003_mci_check_int(ah, AR_MCI_INTERRUPT_RX_MSG_REMOTE_RESET) &&
  599. !ar9003_mci_check_int(ah, AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE))
  600. goto exit;
  601. /*
  602. * BT is sleeping. Check if BT wakes up during
  603. * WLAN calibration. If BT wakes up during
  604. * WLAN calibration, need to go through all
  605. * message exchanges again and recal.
  606. */
  607. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  608. (AR_MCI_INTERRUPT_RX_MSG_REMOTE_RESET |
  609. AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE));
  610. ar9003_mci_remote_reset(ah, true);
  611. ar9003_mci_send_sys_waking(ah, true);
  612. udelay(1);
  613. if (IS_CHAN_2GHZ(chan))
  614. ar9003_mci_send_lna_transfer(ah, true);
  615. mci_hw->bt_state = MCI_BT_AWAKE;
  616. if (caldata) {
  617. caldata->done_txiqcal_once = false;
  618. caldata->done_txclcal_once = false;
  619. caldata->rtt_done = false;
  620. }
  621. if (!ath9k_hw_init_cal(ah, chan))
  622. return -EIO;
  623. exit:
  624. ar9003_mci_enable_interrupt(ah);
  625. return 0;
  626. }
  627. static void ar9003_mci_mute_bt(struct ath_hw *ah)
  628. {
  629. /* disable all MCI messages */
  630. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE, 0xffff0000);
  631. REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  632. /* wait pending HW messages to flush out */
  633. udelay(10);
  634. /*
  635. * Send LNA_TAKE and SYS_SLEEPING when
  636. * 1. reset not after resuming from full sleep
  637. * 2. before reset MCI RX, to quiet BT and avoid MCI RX misalignment
  638. */
  639. ar9003_mci_send_lna_take(ah, true);
  640. udelay(5);
  641. ar9003_mci_send_sys_sleeping(ah, true);
  642. }
  643. static void ar9003_mci_osla_setup(struct ath_hw *ah, bool enable)
  644. {
  645. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  646. u32 thresh;
  647. if (!enable) {
  648. REG_CLR_BIT(ah, AR_BTCOEX_CTRL,
  649. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  650. return;
  651. }
  652. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2, AR_MCI_SCHD_TABLE_2_HW_BASED, 1);
  653. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2,
  654. AR_MCI_SCHD_TABLE_2_MEM_BASED, 1);
  655. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_AGGR_THRESH)) {
  656. thresh = MS(mci->config, ATH_MCI_CONFIG_AGGR_THRESH);
  657. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  658. AR_BTCOEX_CTRL_AGGR_THRESH, thresh);
  659. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  660. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 1);
  661. } else
  662. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  663. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 0);
  664. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  665. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN, 1);
  666. }
  667. int ar9003_mci_reset(struct ath_hw *ah, bool en_int, bool is_2g,
  668. bool is_full_sleep)
  669. {
  670. struct ath_common *common = ath9k_hw_common(ah);
  671. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  672. u32 regval, i;
  673. ath_dbg(common, MCI, "MCI Reset (full_sleep = %d, is_2g = %d)\n",
  674. is_full_sleep, is_2g);
  675. if (!mci->gpm_addr && !mci->sched_addr) {
  676. ath_err(common, "MCI GPM and schedule buffers are not allocated\n");
  677. return -ENOMEM;
  678. }
  679. if (REG_READ(ah, AR_BTCOEX_CTRL) == 0xdeadbeef) {
  680. ath_err(common, "BTCOEX control register is dead\n");
  681. return -EINVAL;
  682. }
  683. /* Program MCI DMA related registers */
  684. REG_WRITE(ah, AR_MCI_GPM_0, mci->gpm_addr);
  685. REG_WRITE(ah, AR_MCI_GPM_1, mci->gpm_len);
  686. REG_WRITE(ah, AR_MCI_SCHD_TABLE_0, mci->sched_addr);
  687. /*
  688. * To avoid MCI state machine be affected by incoming remote MCI msgs,
  689. * MCI mode will be enabled later, right before reset the MCI TX and RX.
  690. */
  691. regval = SM(1, AR_BTCOEX_CTRL_AR9462_MODE) |
  692. SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
  693. SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
  694. SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
  695. SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
  696. SM(3, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
  697. SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
  698. SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
  699. SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  700. REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
  701. if (is_2g && !(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  702. ar9003_mci_osla_setup(ah, true);
  703. else
  704. ar9003_mci_osla_setup(ah, false);
  705. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  706. AR_BTCOEX_CTRL_SPDT_ENABLE);
  707. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL3,
  708. AR_BTCOEX_CTRL3_CONT_INFO_TIMEOUT, 20);
  709. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_RX_DEWEIGHT, 1);
  710. REG_RMW_FIELD(ah, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX, 0);
  711. /* concurrent tx priority */
  712. if (mci->config & ATH_MCI_CONFIG_CONCUR_TX) {
  713. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
  714. AR_BTCOEX_CTRL2_DESC_BASED_TXPWR_ENABLE, 0);
  715. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
  716. AR_BTCOEX_CTRL2_TXPWR_THRESH, 0x7f);
  717. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  718. AR_BTCOEX_CTRL_REDUCE_TXPWR, 0);
  719. for (i = 0; i < 8; i++)
  720. REG_WRITE(ah, AR_BTCOEX_MAX_TXPWR(i), 0x7f7f7f7f);
  721. }
  722. regval = MS(mci->config, ATH_MCI_CONFIG_CLK_DIV);
  723. REG_RMW_FIELD(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_CLK_DIV, regval);
  724. REG_SET_BIT(ah, AR_BTCOEX_CTRL, AR_BTCOEX_CTRL_MCI_MODE_EN);
  725. /* Resetting the Rx and Tx paths of MCI */
  726. regval = REG_READ(ah, AR_MCI_COMMAND2);
  727. regval |= SM(1, AR_MCI_COMMAND2_RESET_TX);
  728. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  729. udelay(1);
  730. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_TX);
  731. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  732. if (is_full_sleep) {
  733. ar9003_mci_mute_bt(ah);
  734. udelay(100);
  735. }
  736. /* Check pending GPM msg before MCI Reset Rx */
  737. ar9003_mci_check_gpm_offset(ah);
  738. regval |= SM(1, AR_MCI_COMMAND2_RESET_RX);
  739. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  740. udelay(1);
  741. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_RX);
  742. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  743. ar9003_mci_get_next_gpm_offset(ah, true, NULL);
  744. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE,
  745. (SM(0xe801, AR_MCI_MSG_ATTRIBUTES_TABLE_INVALID_HDR) |
  746. SM(0x0000, AR_MCI_MSG_ATTRIBUTES_TABLE_CHECKSUM)));
  747. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  748. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  749. ar9003_mci_observation_set_up(ah);
  750. mci->ready = true;
  751. ar9003_mci_prep_interface(ah);
  752. if (en_int)
  753. ar9003_mci_enable_interrupt(ah);
  754. return 0;
  755. }
  756. void ar9003_mci_stop_bt(struct ath_hw *ah, bool save_fullsleep)
  757. {
  758. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  759. ar9003_mci_disable_interrupt(ah);
  760. if (mci_hw->ready && !save_fullsleep) {
  761. ar9003_mci_mute_bt(ah);
  762. udelay(20);
  763. REG_WRITE(ah, AR_BTCOEX_CTRL, 0);
  764. }
  765. mci_hw->bt_state = MCI_BT_SLEEP;
  766. mci_hw->ready = false;
  767. }
  768. static void ar9003_mci_send_2g5g_status(struct ath_hw *ah, bool wait_done)
  769. {
  770. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  771. u32 new_flags, to_set, to_clear;
  772. if (!mci->update_2g5g || (mci->bt_state == MCI_BT_SLEEP))
  773. return;
  774. if (mci->is_2g) {
  775. new_flags = MCI_2G_FLAGS;
  776. to_clear = MCI_2G_FLAGS_CLEAR_MASK;
  777. to_set = MCI_2G_FLAGS_SET_MASK;
  778. } else {
  779. new_flags = MCI_5G_FLAGS;
  780. to_clear = MCI_5G_FLAGS_CLEAR_MASK;
  781. to_set = MCI_5G_FLAGS_SET_MASK;
  782. }
  783. if (to_clear)
  784. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  785. MCI_GPM_COEX_BT_FLAGS_CLEAR,
  786. to_clear);
  787. if (to_set)
  788. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  789. MCI_GPM_COEX_BT_FLAGS_SET,
  790. to_set);
  791. }
  792. static void ar9003_mci_queue_unsent_gpm(struct ath_hw *ah, u8 header,
  793. u32 *payload, bool queue)
  794. {
  795. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  796. u8 type, opcode;
  797. /* check if the message is to be queued */
  798. if (header != MCI_GPM)
  799. return;
  800. type = MCI_GPM_TYPE(payload);
  801. opcode = MCI_GPM_OPCODE(payload);
  802. if (type != MCI_GPM_COEX_AGENT)
  803. return;
  804. switch (opcode) {
  805. case MCI_GPM_COEX_BT_UPDATE_FLAGS:
  806. if (*(((u8 *)payload) + MCI_GPM_COEX_B_BT_FLAGS_OP) ==
  807. MCI_GPM_COEX_BT_FLAGS_READ)
  808. break;
  809. mci->update_2g5g = queue;
  810. break;
  811. case MCI_GPM_COEX_WLAN_CHANNELS:
  812. mci->wlan_channels_update = queue;
  813. break;
  814. case MCI_GPM_COEX_HALT_BT_GPM:
  815. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  816. MCI_GPM_COEX_BT_GPM_UNHALT) {
  817. mci->unhalt_bt_gpm = queue;
  818. if (!queue)
  819. mci->halted_bt_gpm = false;
  820. }
  821. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  822. MCI_GPM_COEX_BT_GPM_HALT) {
  823. mci->halted_bt_gpm = !queue;
  824. }
  825. break;
  826. default:
  827. break;
  828. }
  829. }
  830. void ar9003_mci_2g5g_switch(struct ath_hw *ah, bool force)
  831. {
  832. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  833. if (!mci->update_2g5g && !force)
  834. return;
  835. if (mci->is_2g) {
  836. ar9003_mci_send_2g5g_status(ah, true);
  837. ar9003_mci_send_lna_transfer(ah, true);
  838. udelay(5);
  839. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  840. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  841. REG_CLR_BIT(ah, AR_PHY_GLB_CONTROL,
  842. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  843. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  844. ar9003_mci_osla_setup(ah, true);
  845. REG_WRITE(ah, AR_SELFGEN_MASK, 0x02);
  846. } else {
  847. ar9003_mci_send_lna_take(ah, true);
  848. udelay(5);
  849. REG_SET_BIT(ah, AR_MCI_TX_CTRL,
  850. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  851. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  852. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  853. ar9003_mci_osla_setup(ah, false);
  854. ar9003_mci_send_2g5g_status(ah, true);
  855. }
  856. }
  857. bool ar9003_mci_send_message(struct ath_hw *ah, u8 header, u32 flag,
  858. u32 *payload, u8 len, bool wait_done,
  859. bool check_bt)
  860. {
  861. struct ath_common *common = ath9k_hw_common(ah);
  862. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  863. bool msg_sent = false;
  864. u32 regval;
  865. u32 saved_mci_int_en;
  866. int i;
  867. saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
  868. regval = REG_READ(ah, AR_BTCOEX_CTRL);
  869. if ((regval == 0xdeadbeef) || !(regval & AR_BTCOEX_CTRL_MCI_MODE_EN)) {
  870. ath_dbg(common, MCI,
  871. "MCI Not sending 0x%x. MCI is not enabled. full_sleep = %d\n",
  872. header, (ah->power_mode == ATH9K_PM_FULL_SLEEP) ? 1 : 0);
  873. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  874. return false;
  875. } else if (check_bt && (mci->bt_state == MCI_BT_SLEEP)) {
  876. ath_dbg(common, MCI,
  877. "MCI Don't send message 0x%x. BT is in sleep state\n",
  878. header);
  879. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  880. return false;
  881. }
  882. if (wait_done)
  883. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  884. /* Need to clear SW_MSG_DONE raw bit before wait */
  885. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  886. (AR_MCI_INTERRUPT_SW_MSG_DONE |
  887. AR_MCI_INTERRUPT_MSG_FAIL_MASK));
  888. if (payload) {
  889. for (i = 0; (i * 4) < len; i++)
  890. REG_WRITE(ah, (AR_MCI_TX_PAYLOAD0 + i * 4),
  891. *(payload + i));
  892. }
  893. REG_WRITE(ah, AR_MCI_COMMAND0,
  894. (SM((flag & MCI_FLAG_DISABLE_TIMESTAMP),
  895. AR_MCI_COMMAND0_DISABLE_TIMESTAMP) |
  896. SM(len, AR_MCI_COMMAND0_LEN) |
  897. SM(header, AR_MCI_COMMAND0_HEADER)));
  898. if (wait_done &&
  899. !(ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RAW,
  900. AR_MCI_INTERRUPT_SW_MSG_DONE, 500)))
  901. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  902. else {
  903. ar9003_mci_queue_unsent_gpm(ah, header, payload, false);
  904. msg_sent = true;
  905. }
  906. if (wait_done)
  907. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
  908. return msg_sent;
  909. }
  910. EXPORT_SYMBOL(ar9003_mci_send_message);
  911. void ar9003_mci_init_cal_req(struct ath_hw *ah, bool *is_reusable)
  912. {
  913. struct ath_common *common = ath9k_hw_common(ah);
  914. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  915. u32 pld[4] = {0, 0, 0, 0};
  916. if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
  917. (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
  918. return;
  919. MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_REQ);
  920. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_seq++;
  921. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  922. if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_GRANT, 0, 50000)) {
  923. ath_dbg(common, MCI, "MCI BT_CAL_GRANT received\n");
  924. } else {
  925. *is_reusable = false;
  926. ath_dbg(common, MCI, "MCI BT_CAL_GRANT not received\n");
  927. }
  928. }
  929. void ar9003_mci_init_cal_done(struct ath_hw *ah)
  930. {
  931. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  932. u32 pld[4] = {0, 0, 0, 0};
  933. if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
  934. (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
  935. return;
  936. MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_DONE);
  937. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_done++;
  938. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  939. }
  940. int ar9003_mci_setup(struct ath_hw *ah, u32 gpm_addr, void *gpm_buf,
  941. u16 len, u32 sched_addr)
  942. {
  943. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  944. mci->gpm_addr = gpm_addr;
  945. mci->gpm_buf = gpm_buf;
  946. mci->gpm_len = len;
  947. mci->sched_addr = sched_addr;
  948. return ar9003_mci_reset(ah, true, true, true);
  949. }
  950. EXPORT_SYMBOL(ar9003_mci_setup);
  951. void ar9003_mci_cleanup(struct ath_hw *ah)
  952. {
  953. /* Turn off MCI and Jupiter mode. */
  954. REG_WRITE(ah, AR_BTCOEX_CTRL, 0x00);
  955. ar9003_mci_disable_interrupt(ah);
  956. }
  957. EXPORT_SYMBOL(ar9003_mci_cleanup);
  958. u32 ar9003_mci_state(struct ath_hw *ah, u32 state_type)
  959. {
  960. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  961. u32 value = 0;
  962. u8 query_type;
  963. switch (state_type) {
  964. case MCI_STATE_ENABLE:
  965. if (mci->ready) {
  966. value = REG_READ(ah, AR_BTCOEX_CTRL);
  967. if ((value == 0xdeadbeef) || (value == 0xffffffff))
  968. value = 0;
  969. }
  970. value &= AR_BTCOEX_CTRL_MCI_MODE_EN;
  971. break;
  972. case MCI_STATE_LAST_SCHD_MSG_OFFSET:
  973. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  974. AR_MCI_RX_LAST_SCHD_MSG_INDEX);
  975. /* Make it in bytes */
  976. value <<= 4;
  977. break;
  978. case MCI_STATE_REMOTE_SLEEP:
  979. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  980. AR_MCI_RX_REMOTE_SLEEP) ?
  981. MCI_BT_SLEEP : MCI_BT_AWAKE;
  982. break;
  983. case MCI_STATE_SET_BT_AWAKE:
  984. mci->bt_state = MCI_BT_AWAKE;
  985. ar9003_mci_send_coex_version_query(ah, true);
  986. ar9003_mci_send_coex_wlan_channels(ah, true);
  987. if (mci->unhalt_bt_gpm)
  988. ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
  989. ar9003_mci_2g5g_switch(ah, false);
  990. break;
  991. case MCI_STATE_RESET_REQ_WAKE:
  992. ar9003_mci_reset_req_wakeup(ah);
  993. mci->update_2g5g = true;
  994. if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MASK) {
  995. /* Check if we still have control of the GPIOs */
  996. if ((REG_READ(ah, AR_GLB_GPIO_CONTROL) &
  997. ATH_MCI_CONFIG_MCI_OBS_GPIO) !=
  998. ATH_MCI_CONFIG_MCI_OBS_GPIO) {
  999. ar9003_mci_observation_set_up(ah);
  1000. }
  1001. }
  1002. break;
  1003. case MCI_STATE_SEND_WLAN_COEX_VERSION:
  1004. ar9003_mci_send_coex_version_response(ah, true);
  1005. break;
  1006. case MCI_STATE_SEND_VERSION_QUERY:
  1007. ar9003_mci_send_coex_version_query(ah, true);
  1008. break;
  1009. case MCI_STATE_SEND_STATUS_QUERY:
  1010. query_type = MCI_GPM_COEX_QUERY_BT_TOPOLOGY;
  1011. ar9003_mci_send_coex_bt_status_query(ah, true, query_type);
  1012. break;
  1013. case MCI_STATE_RECOVER_RX:
  1014. ar9003_mci_prep_interface(ah);
  1015. mci->query_bt = true;
  1016. mci->need_flush_btinfo = true;
  1017. ar9003_mci_send_coex_wlan_channels(ah, true);
  1018. ar9003_mci_2g5g_switch(ah, false);
  1019. break;
  1020. case MCI_STATE_NEED_FTP_STOMP:
  1021. value = !(mci->config & ATH_MCI_CONFIG_DISABLE_FTP_STOMP);
  1022. break;
  1023. case MCI_STATE_NEED_FLUSH_BT_INFO:
  1024. value = (!mci->unhalt_bt_gpm && mci->need_flush_btinfo) ? 1 : 0;
  1025. mci->need_flush_btinfo = false;
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. return value;
  1031. }
  1032. EXPORT_SYMBOL(ar9003_mci_state);
  1033. void ar9003_mci_bt_gain_ctrl(struct ath_hw *ah)
  1034. {
  1035. struct ath_common *common = ath9k_hw_common(ah);
  1036. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1037. ath_dbg(common, MCI, "Give LNA and SPDT control to BT\n");
  1038. ar9003_mci_send_lna_take(ah, true);
  1039. udelay(50);
  1040. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL, AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  1041. mci->is_2g = false;
  1042. mci->update_2g5g = true;
  1043. ar9003_mci_send_2g5g_status(ah, true);
  1044. /* Force another 2g5g update at next scanning */
  1045. mci->update_2g5g = true;
  1046. }
  1047. void ar9003_mci_set_power_awake(struct ath_hw *ah)
  1048. {
  1049. u32 btcoex_ctrl2, diag_sw;
  1050. int i;
  1051. u8 lna_ctrl, bt_sleep;
  1052. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1053. btcoex_ctrl2 = REG_READ(ah, AR_BTCOEX_CTRL2);
  1054. if (btcoex_ctrl2 != 0xdeadbeef)
  1055. break;
  1056. udelay(AH_TIME_QUANTUM);
  1057. }
  1058. REG_WRITE(ah, AR_BTCOEX_CTRL2, (btcoex_ctrl2 | BIT(23)));
  1059. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1060. diag_sw = REG_READ(ah, AR_DIAG_SW);
  1061. if (diag_sw != 0xdeadbeef)
  1062. break;
  1063. udelay(AH_TIME_QUANTUM);
  1064. }
  1065. REG_WRITE(ah, AR_DIAG_SW, (diag_sw | BIT(27) | BIT(19) | BIT(18)));
  1066. lna_ctrl = REG_READ(ah, AR_OBS_BUS_CTRL) & 0x3;
  1067. bt_sleep = MS(REG_READ(ah, AR_MCI_RX_STATUS), AR_MCI_RX_REMOTE_SLEEP);
  1068. REG_WRITE(ah, AR_BTCOEX_CTRL2, btcoex_ctrl2);
  1069. REG_WRITE(ah, AR_DIAG_SW, diag_sw);
  1070. if (bt_sleep && (lna_ctrl == 2)) {
  1071. REG_SET_BIT(ah, AR_BTCOEX_RC, 0x1);
  1072. REG_CLR_BIT(ah, AR_BTCOEX_RC, 0x1);
  1073. udelay(50);
  1074. }
  1075. }
  1076. void ar9003_mci_check_gpm_offset(struct ath_hw *ah)
  1077. {
  1078. struct ath_common *common = ath9k_hw_common(ah);
  1079. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1080. u32 offset;
  1081. /*
  1082. * This should only be called before "MAC Warm Reset" or "MCI Reset Rx".
  1083. */
  1084. offset = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1085. if (mci->gpm_idx == offset)
  1086. return;
  1087. ath_dbg(common, MCI, "GPM cached write pointer mismatch %d %d\n",
  1088. mci->gpm_idx, offset);
  1089. mci->query_bt = true;
  1090. mci->need_flush_btinfo = true;
  1091. mci->gpm_idx = 0;
  1092. }
  1093. u32 ar9003_mci_get_next_gpm_offset(struct ath_hw *ah, bool first, u32 *more)
  1094. {
  1095. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1096. u32 offset, more_gpm = 0, gpm_ptr;
  1097. if (first) {
  1098. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1099. if (gpm_ptr >= mci->gpm_len)
  1100. gpm_ptr = 0;
  1101. mci->gpm_idx = gpm_ptr;
  1102. return gpm_ptr;
  1103. }
  1104. /*
  1105. * This could be useful to avoid new GPM message interrupt which
  1106. * may lead to spurious interrupt after power sleep, or multiple
  1107. * entry of ath_mci_intr().
  1108. * Adding empty GPM check by returning HAL_MCI_GPM_INVALID can
  1109. * alleviate this effect, but clearing GPM RX interrupt bit is
  1110. * safe, because whether this is called from hw or driver code
  1111. * there must be an interrupt bit set/triggered initially
  1112. */
  1113. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  1114. AR_MCI_INTERRUPT_RX_MSG_GPM);
  1115. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1116. offset = gpm_ptr;
  1117. if (!offset)
  1118. offset = mci->gpm_len - 1;
  1119. else if (offset >= mci->gpm_len) {
  1120. if (offset != 0xFFFF)
  1121. offset = 0;
  1122. } else {
  1123. offset--;
  1124. }
  1125. if ((offset == 0xFFFF) || (gpm_ptr == mci->gpm_idx)) {
  1126. offset = MCI_GPM_INVALID;
  1127. more_gpm = MCI_GPM_NOMORE;
  1128. goto out;
  1129. }
  1130. for (;;) {
  1131. u32 temp_index;
  1132. /* skip reserved GPM if any */
  1133. if (offset != mci->gpm_idx)
  1134. more_gpm = MCI_GPM_MORE;
  1135. else
  1136. more_gpm = MCI_GPM_NOMORE;
  1137. temp_index = mci->gpm_idx;
  1138. if (temp_index >= mci->gpm_len)
  1139. temp_index = 0;
  1140. mci->gpm_idx++;
  1141. if (mci->gpm_idx >= mci->gpm_len)
  1142. mci->gpm_idx = 0;
  1143. if (ar9003_mci_is_gpm_valid(ah, temp_index)) {
  1144. offset = temp_index;
  1145. break;
  1146. }
  1147. if (more_gpm == MCI_GPM_NOMORE) {
  1148. offset = MCI_GPM_INVALID;
  1149. break;
  1150. }
  1151. }
  1152. if (offset != MCI_GPM_INVALID)
  1153. offset <<= 4;
  1154. out:
  1155. if (more)
  1156. *more = more_gpm;
  1157. return offset;
  1158. }
  1159. EXPORT_SYMBOL(ar9003_mci_get_next_gpm_offset);
  1160. void ar9003_mci_set_bt_version(struct ath_hw *ah, u8 major, u8 minor)
  1161. {
  1162. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1163. mci->bt_ver_major = major;
  1164. mci->bt_ver_minor = minor;
  1165. mci->bt_version_known = true;
  1166. ath_dbg(ath9k_hw_common(ah), MCI, "MCI BT version set: %d.%d\n",
  1167. mci->bt_ver_major, mci->bt_ver_minor);
  1168. }
  1169. EXPORT_SYMBOL(ar9003_mci_set_bt_version);
  1170. void ar9003_mci_send_wlan_channels(struct ath_hw *ah)
  1171. {
  1172. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1173. mci->wlan_channels_update = true;
  1174. ar9003_mci_send_coex_wlan_channels(ah, true);
  1175. }
  1176. EXPORT_SYMBOL(ar9003_mci_send_wlan_channels);
  1177. u16 ar9003_mci_get_max_txpower(struct ath_hw *ah, u8 ctlmode)
  1178. {
  1179. if (!ah->btcoex_hw.mci.concur_tx)
  1180. goto out;
  1181. if (ctlmode == CTL_2GHT20)
  1182. return ATH_BTCOEX_HT20_MAX_TXPOWER;
  1183. else if (ctlmode == CTL_2GHT40)
  1184. return ATH_BTCOEX_HT40_MAX_TXPOWER;
  1185. out:
  1186. return -1;
  1187. }