ar9003_mci.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. REG_CLR_BIT(ah, AR_PHY_TIMING4,
  617. 1 << AR_PHY_TIMING_CONTROL4_DO_GAIN_DC_IQ_CAL_SHIFT);
  618. if (caldata) {
  619. caldata->done_txiqcal_once = false;
  620. caldata->done_txclcal_once = false;
  621. caldata->rtt_done = false;
  622. }
  623. if (!ath9k_hw_init_cal(ah, chan))
  624. return -EIO;
  625. REG_SET_BIT(ah, AR_PHY_TIMING4,
  626. 1 << AR_PHY_TIMING_CONTROL4_DO_GAIN_DC_IQ_CAL_SHIFT);
  627. exit:
  628. ar9003_mci_enable_interrupt(ah);
  629. return 0;
  630. }
  631. static void ar9003_mci_mute_bt(struct ath_hw *ah)
  632. {
  633. /* disable all MCI messages */
  634. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE, 0xffff0000);
  635. REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  636. /* wait pending HW messages to flush out */
  637. udelay(10);
  638. /*
  639. * Send LNA_TAKE and SYS_SLEEPING when
  640. * 1. reset not after resuming from full sleep
  641. * 2. before reset MCI RX, to quiet BT and avoid MCI RX misalignment
  642. */
  643. ar9003_mci_send_lna_take(ah, true);
  644. udelay(5);
  645. ar9003_mci_send_sys_sleeping(ah, true);
  646. }
  647. static void ar9003_mci_osla_setup(struct ath_hw *ah, bool enable)
  648. {
  649. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  650. u32 thresh;
  651. if (!enable) {
  652. REG_CLR_BIT(ah, AR_BTCOEX_CTRL,
  653. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  654. return;
  655. }
  656. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2, AR_MCI_SCHD_TABLE_2_HW_BASED, 1);
  657. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2,
  658. AR_MCI_SCHD_TABLE_2_MEM_BASED, 1);
  659. if (AR_SREV_9565(ah))
  660. REG_RMW_FIELD(ah, AR_MCI_MISC, AR_MCI_MISC_HW_FIX_EN, 1);
  661. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_AGGR_THRESH)) {
  662. thresh = MS(mci->config, ATH_MCI_CONFIG_AGGR_THRESH);
  663. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  664. AR_BTCOEX_CTRL_AGGR_THRESH, thresh);
  665. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  666. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 1);
  667. } else
  668. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  669. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 0);
  670. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  671. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN, 1);
  672. }
  673. int ar9003_mci_reset(struct ath_hw *ah, bool en_int, bool is_2g,
  674. bool is_full_sleep)
  675. {
  676. struct ath_common *common = ath9k_hw_common(ah);
  677. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  678. u32 regval, i;
  679. ath_dbg(common, MCI, "MCI Reset (full_sleep = %d, is_2g = %d)\n",
  680. is_full_sleep, is_2g);
  681. if (!mci->gpm_addr && !mci->sched_addr) {
  682. ath_err(common, "MCI GPM and schedule buffers are not allocated\n");
  683. return -ENOMEM;
  684. }
  685. if (REG_READ(ah, AR_BTCOEX_CTRL) == 0xdeadbeef) {
  686. ath_err(common, "BTCOEX control register is dead\n");
  687. return -EINVAL;
  688. }
  689. /* Program MCI DMA related registers */
  690. REG_WRITE(ah, AR_MCI_GPM_0, mci->gpm_addr);
  691. REG_WRITE(ah, AR_MCI_GPM_1, mci->gpm_len);
  692. REG_WRITE(ah, AR_MCI_SCHD_TABLE_0, mci->sched_addr);
  693. /*
  694. * To avoid MCI state machine be affected by incoming remote MCI msgs,
  695. * MCI mode will be enabled later, right before reset the MCI TX and RX.
  696. */
  697. regval = SM(1, AR_BTCOEX_CTRL_AR9462_MODE) |
  698. SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
  699. SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
  700. SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
  701. SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
  702. SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
  703. SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  704. if (AR_SREV_9565(ah)) {
  705. regval |= SM(1, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
  706. SM(1, AR_BTCOEX_CTRL_RX_CHAIN_MASK);
  707. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
  708. AR_BTCOEX_CTRL2_TX_CHAIN_MASK, 0x1);
  709. } else {
  710. regval |= SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
  711. SM(3, AR_BTCOEX_CTRL_RX_CHAIN_MASK);
  712. }
  713. REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
  714. if (is_2g && !(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  715. ar9003_mci_osla_setup(ah, true);
  716. else
  717. ar9003_mci_osla_setup(ah, false);
  718. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  719. AR_BTCOEX_CTRL_SPDT_ENABLE);
  720. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL3,
  721. AR_BTCOEX_CTRL3_CONT_INFO_TIMEOUT, 20);
  722. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_RX_DEWEIGHT, 1);
  723. REG_RMW_FIELD(ah, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX, 0);
  724. /* concurrent tx priority */
  725. if (mci->config & ATH_MCI_CONFIG_CONCUR_TX) {
  726. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
  727. AR_BTCOEX_CTRL2_DESC_BASED_TXPWR_ENABLE, 0);
  728. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2,
  729. AR_BTCOEX_CTRL2_TXPWR_THRESH, 0x7f);
  730. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  731. AR_BTCOEX_CTRL_REDUCE_TXPWR, 0);
  732. for (i = 0; i < 8; i++)
  733. REG_WRITE(ah, AR_BTCOEX_MAX_TXPWR(i), 0x7f7f7f7f);
  734. }
  735. regval = MS(mci->config, ATH_MCI_CONFIG_CLK_DIV);
  736. REG_RMW_FIELD(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_CLK_DIV, regval);
  737. REG_SET_BIT(ah, AR_BTCOEX_CTRL, AR_BTCOEX_CTRL_MCI_MODE_EN);
  738. /* Resetting the Rx and Tx paths of MCI */
  739. regval = REG_READ(ah, AR_MCI_COMMAND2);
  740. regval |= SM(1, AR_MCI_COMMAND2_RESET_TX);
  741. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  742. udelay(1);
  743. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_TX);
  744. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  745. if (is_full_sleep) {
  746. ar9003_mci_mute_bt(ah);
  747. udelay(100);
  748. }
  749. /* Check pending GPM msg before MCI Reset Rx */
  750. ar9003_mci_check_gpm_offset(ah);
  751. regval |= SM(1, AR_MCI_COMMAND2_RESET_RX);
  752. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  753. udelay(1);
  754. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_RX);
  755. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  756. ar9003_mci_get_next_gpm_offset(ah, true, NULL);
  757. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE,
  758. (SM(0xe801, AR_MCI_MSG_ATTRIBUTES_TABLE_INVALID_HDR) |
  759. SM(0x0000, AR_MCI_MSG_ATTRIBUTES_TABLE_CHECKSUM)));
  760. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  761. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  762. ar9003_mci_observation_set_up(ah);
  763. mci->ready = true;
  764. ar9003_mci_prep_interface(ah);
  765. if (AR_SREV_9565(ah))
  766. REG_RMW_FIELD(ah, AR_MCI_DBG_CNT_CTRL,
  767. AR_MCI_DBG_CNT_CTRL_ENABLE, 0);
  768. if (en_int)
  769. ar9003_mci_enable_interrupt(ah);
  770. return 0;
  771. }
  772. void ar9003_mci_stop_bt(struct ath_hw *ah, bool save_fullsleep)
  773. {
  774. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  775. ar9003_mci_disable_interrupt(ah);
  776. if (mci_hw->ready && !save_fullsleep) {
  777. ar9003_mci_mute_bt(ah);
  778. udelay(20);
  779. REG_WRITE(ah, AR_BTCOEX_CTRL, 0);
  780. }
  781. mci_hw->bt_state = MCI_BT_SLEEP;
  782. mci_hw->ready = false;
  783. }
  784. static void ar9003_mci_send_2g5g_status(struct ath_hw *ah, bool wait_done)
  785. {
  786. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  787. u32 new_flags, to_set, to_clear;
  788. if (!mci->update_2g5g || (mci->bt_state == MCI_BT_SLEEP))
  789. return;
  790. if (mci->is_2g) {
  791. new_flags = MCI_2G_FLAGS;
  792. to_clear = MCI_2G_FLAGS_CLEAR_MASK;
  793. to_set = MCI_2G_FLAGS_SET_MASK;
  794. } else {
  795. new_flags = MCI_5G_FLAGS;
  796. to_clear = MCI_5G_FLAGS_CLEAR_MASK;
  797. to_set = MCI_5G_FLAGS_SET_MASK;
  798. }
  799. if (to_clear)
  800. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  801. MCI_GPM_COEX_BT_FLAGS_CLEAR,
  802. to_clear);
  803. if (to_set)
  804. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  805. MCI_GPM_COEX_BT_FLAGS_SET,
  806. to_set);
  807. }
  808. static void ar9003_mci_queue_unsent_gpm(struct ath_hw *ah, u8 header,
  809. u32 *payload, bool queue)
  810. {
  811. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  812. u8 type, opcode;
  813. /* check if the message is to be queued */
  814. if (header != MCI_GPM)
  815. return;
  816. type = MCI_GPM_TYPE(payload);
  817. opcode = MCI_GPM_OPCODE(payload);
  818. if (type != MCI_GPM_COEX_AGENT)
  819. return;
  820. switch (opcode) {
  821. case MCI_GPM_COEX_BT_UPDATE_FLAGS:
  822. if (*(((u8 *)payload) + MCI_GPM_COEX_B_BT_FLAGS_OP) ==
  823. MCI_GPM_COEX_BT_FLAGS_READ)
  824. break;
  825. mci->update_2g5g = queue;
  826. break;
  827. case MCI_GPM_COEX_WLAN_CHANNELS:
  828. mci->wlan_channels_update = queue;
  829. break;
  830. case MCI_GPM_COEX_HALT_BT_GPM:
  831. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  832. MCI_GPM_COEX_BT_GPM_UNHALT) {
  833. mci->unhalt_bt_gpm = queue;
  834. if (!queue)
  835. mci->halted_bt_gpm = false;
  836. }
  837. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  838. MCI_GPM_COEX_BT_GPM_HALT) {
  839. mci->halted_bt_gpm = !queue;
  840. }
  841. break;
  842. default:
  843. break;
  844. }
  845. }
  846. void ar9003_mci_2g5g_switch(struct ath_hw *ah, bool force)
  847. {
  848. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  849. if (!mci->update_2g5g && !force)
  850. return;
  851. if (mci->is_2g) {
  852. ar9003_mci_send_2g5g_status(ah, true);
  853. ar9003_mci_send_lna_transfer(ah, true);
  854. udelay(5);
  855. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  856. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  857. REG_CLR_BIT(ah, AR_PHY_GLB_CONTROL,
  858. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  859. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  860. ar9003_mci_osla_setup(ah, true);
  861. if (AR_SREV_9462(ah))
  862. REG_WRITE(ah, AR_SELFGEN_MASK, 0x02);
  863. } else {
  864. ar9003_mci_send_lna_take(ah, true);
  865. udelay(5);
  866. REG_SET_BIT(ah, AR_MCI_TX_CTRL,
  867. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  868. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  869. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  870. ar9003_mci_osla_setup(ah, false);
  871. ar9003_mci_send_2g5g_status(ah, true);
  872. }
  873. }
  874. bool ar9003_mci_send_message(struct ath_hw *ah, u8 header, u32 flag,
  875. u32 *payload, u8 len, bool wait_done,
  876. bool check_bt)
  877. {
  878. struct ath_common *common = ath9k_hw_common(ah);
  879. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  880. bool msg_sent = false;
  881. u32 regval;
  882. u32 saved_mci_int_en;
  883. int i;
  884. saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
  885. regval = REG_READ(ah, AR_BTCOEX_CTRL);
  886. if ((regval == 0xdeadbeef) || !(regval & AR_BTCOEX_CTRL_MCI_MODE_EN)) {
  887. ath_dbg(common, MCI,
  888. "MCI Not sending 0x%x. MCI is not enabled. full_sleep = %d\n",
  889. header, (ah->power_mode == ATH9K_PM_FULL_SLEEP) ? 1 : 0);
  890. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  891. return false;
  892. } else if (check_bt && (mci->bt_state == MCI_BT_SLEEP)) {
  893. ath_dbg(common, MCI,
  894. "MCI Don't send message 0x%x. BT is in sleep state\n",
  895. header);
  896. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  897. return false;
  898. }
  899. if (wait_done)
  900. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  901. /* Need to clear SW_MSG_DONE raw bit before wait */
  902. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  903. (AR_MCI_INTERRUPT_SW_MSG_DONE |
  904. AR_MCI_INTERRUPT_MSG_FAIL_MASK));
  905. if (payload) {
  906. for (i = 0; (i * 4) < len; i++)
  907. REG_WRITE(ah, (AR_MCI_TX_PAYLOAD0 + i * 4),
  908. *(payload + i));
  909. }
  910. REG_WRITE(ah, AR_MCI_COMMAND0,
  911. (SM((flag & MCI_FLAG_DISABLE_TIMESTAMP),
  912. AR_MCI_COMMAND0_DISABLE_TIMESTAMP) |
  913. SM(len, AR_MCI_COMMAND0_LEN) |
  914. SM(header, AR_MCI_COMMAND0_HEADER)));
  915. if (wait_done &&
  916. !(ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RAW,
  917. AR_MCI_INTERRUPT_SW_MSG_DONE, 500)))
  918. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  919. else {
  920. ar9003_mci_queue_unsent_gpm(ah, header, payload, false);
  921. msg_sent = true;
  922. }
  923. if (wait_done)
  924. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
  925. return msg_sent;
  926. }
  927. EXPORT_SYMBOL(ar9003_mci_send_message);
  928. void ar9003_mci_init_cal_req(struct ath_hw *ah, bool *is_reusable)
  929. {
  930. struct ath_common *common = ath9k_hw_common(ah);
  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_REQ);
  937. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_seq++;
  938. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  939. if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_GRANT, 0, 50000)) {
  940. ath_dbg(common, MCI, "MCI BT_CAL_GRANT received\n");
  941. } else {
  942. *is_reusable = false;
  943. ath_dbg(common, MCI, "MCI BT_CAL_GRANT not received\n");
  944. }
  945. }
  946. void ar9003_mci_init_cal_done(struct ath_hw *ah)
  947. {
  948. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  949. u32 pld[4] = {0, 0, 0, 0};
  950. if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
  951. (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
  952. return;
  953. MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_DONE);
  954. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_done++;
  955. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  956. }
  957. int ar9003_mci_setup(struct ath_hw *ah, u32 gpm_addr, void *gpm_buf,
  958. u16 len, u32 sched_addr)
  959. {
  960. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  961. mci->gpm_addr = gpm_addr;
  962. mci->gpm_buf = gpm_buf;
  963. mci->gpm_len = len;
  964. mci->sched_addr = sched_addr;
  965. return ar9003_mci_reset(ah, true, true, true);
  966. }
  967. EXPORT_SYMBOL(ar9003_mci_setup);
  968. void ar9003_mci_cleanup(struct ath_hw *ah)
  969. {
  970. /* Turn off MCI and Jupiter mode. */
  971. REG_WRITE(ah, AR_BTCOEX_CTRL, 0x00);
  972. ar9003_mci_disable_interrupt(ah);
  973. }
  974. EXPORT_SYMBOL(ar9003_mci_cleanup);
  975. u32 ar9003_mci_state(struct ath_hw *ah, u32 state_type)
  976. {
  977. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  978. u32 value = 0;
  979. u8 query_type;
  980. switch (state_type) {
  981. case MCI_STATE_ENABLE:
  982. if (mci->ready) {
  983. value = REG_READ(ah, AR_BTCOEX_CTRL);
  984. if ((value == 0xdeadbeef) || (value == 0xffffffff))
  985. value = 0;
  986. }
  987. value &= AR_BTCOEX_CTRL_MCI_MODE_EN;
  988. break;
  989. case MCI_STATE_LAST_SCHD_MSG_OFFSET:
  990. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  991. AR_MCI_RX_LAST_SCHD_MSG_INDEX);
  992. /* Make it in bytes */
  993. value <<= 4;
  994. break;
  995. case MCI_STATE_REMOTE_SLEEP:
  996. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  997. AR_MCI_RX_REMOTE_SLEEP) ?
  998. MCI_BT_SLEEP : MCI_BT_AWAKE;
  999. break;
  1000. case MCI_STATE_SET_BT_AWAKE:
  1001. mci->bt_state = MCI_BT_AWAKE;
  1002. ar9003_mci_send_coex_version_query(ah, true);
  1003. ar9003_mci_send_coex_wlan_channels(ah, true);
  1004. if (mci->unhalt_bt_gpm)
  1005. ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
  1006. ar9003_mci_2g5g_switch(ah, false);
  1007. break;
  1008. case MCI_STATE_RESET_REQ_WAKE:
  1009. ar9003_mci_reset_req_wakeup(ah);
  1010. mci->update_2g5g = true;
  1011. if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MASK) {
  1012. /* Check if we still have control of the GPIOs */
  1013. if ((REG_READ(ah, AR_GLB_GPIO_CONTROL) &
  1014. ATH_MCI_CONFIG_MCI_OBS_GPIO) !=
  1015. ATH_MCI_CONFIG_MCI_OBS_GPIO) {
  1016. ar9003_mci_observation_set_up(ah);
  1017. }
  1018. }
  1019. break;
  1020. case MCI_STATE_SEND_WLAN_COEX_VERSION:
  1021. ar9003_mci_send_coex_version_response(ah, true);
  1022. break;
  1023. case MCI_STATE_SEND_VERSION_QUERY:
  1024. ar9003_mci_send_coex_version_query(ah, true);
  1025. break;
  1026. case MCI_STATE_SEND_STATUS_QUERY:
  1027. query_type = MCI_GPM_COEX_QUERY_BT_TOPOLOGY;
  1028. ar9003_mci_send_coex_bt_status_query(ah, true, query_type);
  1029. break;
  1030. case MCI_STATE_RECOVER_RX:
  1031. ar9003_mci_prep_interface(ah);
  1032. mci->query_bt = true;
  1033. mci->need_flush_btinfo = true;
  1034. ar9003_mci_send_coex_wlan_channels(ah, true);
  1035. ar9003_mci_2g5g_switch(ah, false);
  1036. break;
  1037. case MCI_STATE_NEED_FTP_STOMP:
  1038. value = !(mci->config & ATH_MCI_CONFIG_DISABLE_FTP_STOMP);
  1039. break;
  1040. case MCI_STATE_NEED_FLUSH_BT_INFO:
  1041. value = (!mci->unhalt_bt_gpm && mci->need_flush_btinfo) ? 1 : 0;
  1042. mci->need_flush_btinfo = false;
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. return value;
  1048. }
  1049. EXPORT_SYMBOL(ar9003_mci_state);
  1050. void ar9003_mci_bt_gain_ctrl(struct ath_hw *ah)
  1051. {
  1052. struct ath_common *common = ath9k_hw_common(ah);
  1053. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1054. ath_dbg(common, MCI, "Give LNA and SPDT control to BT\n");
  1055. ar9003_mci_send_lna_take(ah, true);
  1056. udelay(50);
  1057. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL, AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  1058. mci->is_2g = false;
  1059. mci->update_2g5g = true;
  1060. ar9003_mci_send_2g5g_status(ah, true);
  1061. /* Force another 2g5g update at next scanning */
  1062. mci->update_2g5g = true;
  1063. }
  1064. void ar9003_mci_set_power_awake(struct ath_hw *ah)
  1065. {
  1066. u32 btcoex_ctrl2, diag_sw;
  1067. int i;
  1068. u8 lna_ctrl, bt_sleep;
  1069. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1070. btcoex_ctrl2 = REG_READ(ah, AR_BTCOEX_CTRL2);
  1071. if (btcoex_ctrl2 != 0xdeadbeef)
  1072. break;
  1073. udelay(AH_TIME_QUANTUM);
  1074. }
  1075. REG_WRITE(ah, AR_BTCOEX_CTRL2, (btcoex_ctrl2 | BIT(23)));
  1076. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1077. diag_sw = REG_READ(ah, AR_DIAG_SW);
  1078. if (diag_sw != 0xdeadbeef)
  1079. break;
  1080. udelay(AH_TIME_QUANTUM);
  1081. }
  1082. REG_WRITE(ah, AR_DIAG_SW, (diag_sw | BIT(27) | BIT(19) | BIT(18)));
  1083. lna_ctrl = REG_READ(ah, AR_OBS_BUS_CTRL) & 0x3;
  1084. bt_sleep = MS(REG_READ(ah, AR_MCI_RX_STATUS), AR_MCI_RX_REMOTE_SLEEP);
  1085. REG_WRITE(ah, AR_BTCOEX_CTRL2, btcoex_ctrl2);
  1086. REG_WRITE(ah, AR_DIAG_SW, diag_sw);
  1087. if (bt_sleep && (lna_ctrl == 2)) {
  1088. REG_SET_BIT(ah, AR_BTCOEX_RC, 0x1);
  1089. REG_CLR_BIT(ah, AR_BTCOEX_RC, 0x1);
  1090. udelay(50);
  1091. }
  1092. }
  1093. void ar9003_mci_check_gpm_offset(struct ath_hw *ah)
  1094. {
  1095. struct ath_common *common = ath9k_hw_common(ah);
  1096. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1097. u32 offset;
  1098. /*
  1099. * This should only be called before "MAC Warm Reset" or "MCI Reset Rx".
  1100. */
  1101. offset = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1102. if (mci->gpm_idx == offset)
  1103. return;
  1104. ath_dbg(common, MCI, "GPM cached write pointer mismatch %d %d\n",
  1105. mci->gpm_idx, offset);
  1106. mci->query_bt = true;
  1107. mci->need_flush_btinfo = true;
  1108. mci->gpm_idx = 0;
  1109. }
  1110. u32 ar9003_mci_get_next_gpm_offset(struct ath_hw *ah, bool first, u32 *more)
  1111. {
  1112. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1113. u32 offset, more_gpm = 0, gpm_ptr;
  1114. if (first) {
  1115. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1116. if (gpm_ptr >= mci->gpm_len)
  1117. gpm_ptr = 0;
  1118. mci->gpm_idx = gpm_ptr;
  1119. return gpm_ptr;
  1120. }
  1121. /*
  1122. * This could be useful to avoid new GPM message interrupt which
  1123. * may lead to spurious interrupt after power sleep, or multiple
  1124. * entry of ath_mci_intr().
  1125. * Adding empty GPM check by returning HAL_MCI_GPM_INVALID can
  1126. * alleviate this effect, but clearing GPM RX interrupt bit is
  1127. * safe, because whether this is called from hw or driver code
  1128. * there must be an interrupt bit set/triggered initially
  1129. */
  1130. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  1131. AR_MCI_INTERRUPT_RX_MSG_GPM);
  1132. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1133. offset = gpm_ptr;
  1134. if (!offset)
  1135. offset = mci->gpm_len - 1;
  1136. else if (offset >= mci->gpm_len) {
  1137. if (offset != 0xFFFF)
  1138. offset = 0;
  1139. } else {
  1140. offset--;
  1141. }
  1142. if ((offset == 0xFFFF) || (gpm_ptr == mci->gpm_idx)) {
  1143. offset = MCI_GPM_INVALID;
  1144. more_gpm = MCI_GPM_NOMORE;
  1145. goto out;
  1146. }
  1147. for (;;) {
  1148. u32 temp_index;
  1149. /* skip reserved GPM if any */
  1150. if (offset != mci->gpm_idx)
  1151. more_gpm = MCI_GPM_MORE;
  1152. else
  1153. more_gpm = MCI_GPM_NOMORE;
  1154. temp_index = mci->gpm_idx;
  1155. if (temp_index >= mci->gpm_len)
  1156. temp_index = 0;
  1157. mci->gpm_idx++;
  1158. if (mci->gpm_idx >= mci->gpm_len)
  1159. mci->gpm_idx = 0;
  1160. if (ar9003_mci_is_gpm_valid(ah, temp_index)) {
  1161. offset = temp_index;
  1162. break;
  1163. }
  1164. if (more_gpm == MCI_GPM_NOMORE) {
  1165. offset = MCI_GPM_INVALID;
  1166. break;
  1167. }
  1168. }
  1169. if (offset != MCI_GPM_INVALID)
  1170. offset <<= 4;
  1171. out:
  1172. if (more)
  1173. *more = more_gpm;
  1174. return offset;
  1175. }
  1176. EXPORT_SYMBOL(ar9003_mci_get_next_gpm_offset);
  1177. void ar9003_mci_set_bt_version(struct ath_hw *ah, u8 major, u8 minor)
  1178. {
  1179. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1180. mci->bt_ver_major = major;
  1181. mci->bt_ver_minor = minor;
  1182. mci->bt_version_known = true;
  1183. ath_dbg(ath9k_hw_common(ah), MCI, "MCI BT version set: %d.%d\n",
  1184. mci->bt_ver_major, mci->bt_ver_minor);
  1185. }
  1186. EXPORT_SYMBOL(ar9003_mci_set_bt_version);
  1187. void ar9003_mci_send_wlan_channels(struct ath_hw *ah)
  1188. {
  1189. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1190. mci->wlan_channels_update = true;
  1191. ar9003_mci_send_coex_wlan_channels(ah, true);
  1192. }
  1193. EXPORT_SYMBOL(ar9003_mci_send_wlan_channels);
  1194. u16 ar9003_mci_get_max_txpower(struct ath_hw *ah, u8 ctlmode)
  1195. {
  1196. if (!ah->btcoex_hw.mci.concur_tx)
  1197. goto out;
  1198. if (ctlmode == CTL_2GHT20)
  1199. return ATH_BTCOEX_HT20_MAX_TXPOWER;
  1200. else if (ctlmode == CTL_2GHT40)
  1201. return ATH_BTCOEX_HT40_MAX_TXPOWER;
  1202. out:
  1203. return -1;
  1204. }