efuse.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * Tmis program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * Tmis program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Tme full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. #include "wifi.h"
  26. #include "efuse.h"
  27. #include <linux/export.h>
  28. static const u8 MAX_PGPKT_SIZE = 9;
  29. static const u8 PGPKT_DATA_SIZE = 8;
  30. static const int EFUSE_MAX_SIZE = 512;
  31. static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
  32. {0, 0, 0, 2},
  33. {0, 1, 0, 2},
  34. {0, 2, 0, 2},
  35. {1, 0, 0, 1},
  36. {1, 0, 1, 1},
  37. {1, 1, 0, 1},
  38. {1, 1, 1, 3},
  39. {1, 3, 0, 17},
  40. {3, 3, 1, 48},
  41. {10, 0, 0, 6},
  42. {10, 3, 0, 1},
  43. {10, 3, 1, 1},
  44. {11, 0, 0, 28}
  45. };
  46. static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset,
  47. u8 *value);
  48. static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset,
  49. u16 *value);
  50. static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset,
  51. u32 *value);
  52. static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset,
  53. u8 value);
  54. static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset,
  55. u16 value);
  56. static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset,
  57. u32 value);
  58. static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr,
  59. u8 data);
  60. static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse);
  61. static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset,
  62. u8 *data);
  63. static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset,
  64. u8 word_en, u8 *data);
  65. static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
  66. u8 *targetdata);
  67. static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
  68. u16 efuse_addr, u8 word_en, u8 *data);
  69. static void efuse_power_switch(struct ieee80211_hw *hw, u8 write,
  70. u8 pwrstate);
  71. static u16 efuse_get_current_size(struct ieee80211_hw *hw);
  72. static u8 efuse_calculate_word_cnts(u8 word_en);
  73. void efuse_initialize(struct ieee80211_hw *hw)
  74. {
  75. struct rtl_priv *rtlpriv = rtl_priv(hw);
  76. u8 bytetemp;
  77. u8 temp;
  78. bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1);
  79. temp = bytetemp | 0x20;
  80. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1, temp);
  81. bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1);
  82. temp = bytetemp & 0xFE;
  83. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1, temp);
  84. bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3);
  85. temp = bytetemp | 0x80;
  86. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3, temp);
  87. rtl_write_byte(rtlpriv, 0x2F8, 0x3);
  88. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
  89. }
  90. u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address)
  91. {
  92. struct rtl_priv *rtlpriv = rtl_priv(hw);
  93. u8 data;
  94. u8 bytetemp;
  95. u8 temp;
  96. u32 k = 0;
  97. const u32 efuse_len =
  98. rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
  99. if (address < efuse_len) {
  100. temp = address & 0xFF;
  101. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
  102. temp);
  103. bytetemp = rtl_read_byte(rtlpriv,
  104. rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
  105. temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
  106. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
  107. temp);
  108. bytetemp = rtl_read_byte(rtlpriv,
  109. rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
  110. temp = bytetemp & 0x7F;
  111. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
  112. temp);
  113. bytetemp = rtl_read_byte(rtlpriv,
  114. rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
  115. while (!(bytetemp & 0x80)) {
  116. bytetemp = rtl_read_byte(rtlpriv,
  117. rtlpriv->cfg->
  118. maps[EFUSE_CTRL] + 3);
  119. k++;
  120. if (k == 1000) {
  121. k = 0;
  122. break;
  123. }
  124. }
  125. data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
  126. return data;
  127. } else
  128. return 0xFF;
  129. }
  130. EXPORT_SYMBOL(efuse_read_1byte);
  131. void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value)
  132. {
  133. struct rtl_priv *rtlpriv = rtl_priv(hw);
  134. u8 bytetemp;
  135. u8 temp;
  136. u32 k = 0;
  137. const u32 efuse_len =
  138. rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
  139. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "Addr=%x Data =%x\n",
  140. address, value);
  141. if (address < efuse_len) {
  142. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value);
  143. temp = address & 0xFF;
  144. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
  145. temp);
  146. bytetemp = rtl_read_byte(rtlpriv,
  147. rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
  148. temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
  149. rtl_write_byte(rtlpriv,
  150. rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp);
  151. bytetemp = rtl_read_byte(rtlpriv,
  152. rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
  153. temp = bytetemp | 0x80;
  154. rtl_write_byte(rtlpriv,
  155. rtlpriv->cfg->maps[EFUSE_CTRL] + 3, temp);
  156. bytetemp = rtl_read_byte(rtlpriv,
  157. rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
  158. while (bytetemp & 0x80) {
  159. bytetemp = rtl_read_byte(rtlpriv,
  160. rtlpriv->cfg->
  161. maps[EFUSE_CTRL] + 3);
  162. k++;
  163. if (k == 100) {
  164. k = 0;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf)
  171. {
  172. struct rtl_priv *rtlpriv = rtl_priv(hw);
  173. u32 value32;
  174. u8 readbyte;
  175. u16 retry;
  176. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
  177. (_offset & 0xff));
  178. readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
  179. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
  180. ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
  181. readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
  182. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
  183. (readbyte & 0x7f));
  184. retry = 0;
  185. value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
  186. while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
  187. value32 = rtl_read_dword(rtlpriv,
  188. rtlpriv->cfg->maps[EFUSE_CTRL]);
  189. retry++;
  190. }
  191. udelay(50);
  192. value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
  193. *pbuf = (u8) (value32 & 0xff);
  194. }
  195. EXPORT_SYMBOL_GPL(read_efuse_byte);
  196. void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
  197. {
  198. struct rtl_priv *rtlpriv = rtl_priv(hw);
  199. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  200. u8 *efuse_tbl;
  201. u8 rtemp8[1];
  202. u16 efuse_addr = 0;
  203. u8 offset, wren;
  204. u8 u1temp = 0;
  205. u16 i;
  206. u16 j;
  207. const u16 efuse_max_section =
  208. rtlpriv->cfg->maps[EFUSE_MAX_SECTION_MAP];
  209. const u32 efuse_len =
  210. rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
  211. u16 **efuse_word;
  212. u16 efuse_utilized = 0;
  213. u8 efuse_usage;
  214. if ((_offset + _size_byte) > rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]) {
  215. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  216. "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n",
  217. _offset, _size_byte);
  218. return;
  219. }
  220. /* allocate memory for efuse_tbl and efuse_word */
  221. efuse_tbl = kzalloc(rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE] *
  222. sizeof(u8), GFP_ATOMIC);
  223. if (!efuse_tbl)
  224. return;
  225. efuse_word = kzalloc(EFUSE_MAX_WORD_UNIT * sizeof(u16 *), GFP_ATOMIC);
  226. if (!efuse_word)
  227. goto out;
  228. for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
  229. efuse_word[i] = kzalloc(efuse_max_section * sizeof(u16),
  230. GFP_ATOMIC);
  231. if (!efuse_word[i])
  232. goto done;
  233. }
  234. for (i = 0; i < efuse_max_section; i++)
  235. for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
  236. efuse_word[j][i] = 0xFFFF;
  237. read_efuse_byte(hw, efuse_addr, rtemp8);
  238. if (*rtemp8 != 0xFF) {
  239. efuse_utilized++;
  240. RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
  241. "Addr=%d\n", efuse_addr);
  242. efuse_addr++;
  243. }
  244. while ((*rtemp8 != 0xFF) && (efuse_addr < efuse_len)) {
  245. /* Check PG header for section num. */
  246. if ((*rtemp8 & 0x1F) == 0x0F) {/* extended header */
  247. u1temp = ((*rtemp8 & 0xE0) >> 5);
  248. read_efuse_byte(hw, efuse_addr, rtemp8);
  249. if ((*rtemp8 & 0x0F) == 0x0F) {
  250. efuse_addr++;
  251. read_efuse_byte(hw, efuse_addr, rtemp8);
  252. if (*rtemp8 != 0xFF &&
  253. (efuse_addr < efuse_len)) {
  254. efuse_addr++;
  255. }
  256. continue;
  257. } else {
  258. offset = ((*rtemp8 & 0xF0) >> 1) | u1temp;
  259. wren = (*rtemp8 & 0x0F);
  260. efuse_addr++;
  261. }
  262. } else {
  263. offset = ((*rtemp8 >> 4) & 0x0f);
  264. wren = (*rtemp8 & 0x0f);
  265. }
  266. if (offset < efuse_max_section) {
  267. RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
  268. "offset-%d Worden=%x\n", offset, wren);
  269. for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
  270. if (!(wren & 0x01)) {
  271. RTPRINT(rtlpriv, FEEPROM,
  272. EFUSE_READ_ALL,
  273. "Addr=%d\n", efuse_addr);
  274. read_efuse_byte(hw, efuse_addr, rtemp8);
  275. efuse_addr++;
  276. efuse_utilized++;
  277. efuse_word[i][offset] =
  278. (*rtemp8 & 0xff);
  279. if (efuse_addr >= efuse_len)
  280. break;
  281. RTPRINT(rtlpriv, FEEPROM,
  282. EFUSE_READ_ALL,
  283. "Addr=%d\n", efuse_addr);
  284. read_efuse_byte(hw, efuse_addr, rtemp8);
  285. efuse_addr++;
  286. efuse_utilized++;
  287. efuse_word[i][offset] |=
  288. (((u16)*rtemp8 << 8) & 0xff00);
  289. if (efuse_addr >= efuse_len)
  290. break;
  291. }
  292. wren >>= 1;
  293. }
  294. }
  295. RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
  296. "Addr=%d\n", efuse_addr);
  297. read_efuse_byte(hw, efuse_addr, rtemp8);
  298. if (*rtemp8 != 0xFF && (efuse_addr < efuse_len)) {
  299. efuse_utilized++;
  300. efuse_addr++;
  301. }
  302. }
  303. for (i = 0; i < efuse_max_section; i++) {
  304. for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
  305. efuse_tbl[(i * 8) + (j * 2)] =
  306. (efuse_word[j][i] & 0xff);
  307. efuse_tbl[(i * 8) + ((j * 2) + 1)] =
  308. ((efuse_word[j][i] >> 8) & 0xff);
  309. }
  310. }
  311. for (i = 0; i < _size_byte; i++)
  312. pbuf[i] = efuse_tbl[_offset + i];
  313. rtlefuse->efuse_usedbytes = efuse_utilized;
  314. efuse_usage = (u8) ((efuse_utilized * 100) / efuse_len);
  315. rtlefuse->efuse_usedpercentage = efuse_usage;
  316. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_BYTES,
  317. (u8 *)&efuse_utilized);
  318. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_USAGE,
  319. &efuse_usage);
  320. done:
  321. for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++)
  322. kfree(efuse_word[i]);
  323. kfree(efuse_word);
  324. out:
  325. kfree(efuse_tbl);
  326. }
  327. bool efuse_shadow_update_chk(struct ieee80211_hw *hw)
  328. {
  329. struct rtl_priv *rtlpriv = rtl_priv(hw);
  330. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  331. u8 section_idx, i, Base;
  332. u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used;
  333. bool wordchanged, result = true;
  334. for (section_idx = 0; section_idx < 16; section_idx++) {
  335. Base = section_idx * 8;
  336. wordchanged = false;
  337. for (i = 0; i < 8; i = i + 2) {
  338. if ((rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i] !=
  339. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i]) ||
  340. (rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i + 1] !=
  341. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i +
  342. 1])) {
  343. words_need++;
  344. wordchanged = true;
  345. }
  346. }
  347. if (wordchanged)
  348. hdr_num++;
  349. }
  350. totalbytes = hdr_num + words_need * 2;
  351. efuse_used = rtlefuse->efuse_usedbytes;
  352. if ((totalbytes + efuse_used) >=
  353. (EFUSE_MAX_SIZE - rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))
  354. result = false;
  355. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  356. "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
  357. totalbytes, hdr_num, words_need, efuse_used);
  358. return result;
  359. }
  360. void efuse_shadow_read(struct ieee80211_hw *hw, u8 type,
  361. u16 offset, u32 *value)
  362. {
  363. if (type == 1)
  364. efuse_shadow_read_1byte(hw, offset, (u8 *)value);
  365. else if (type == 2)
  366. efuse_shadow_read_2byte(hw, offset, (u16 *)value);
  367. else if (type == 4)
  368. efuse_shadow_read_4byte(hw, offset, value);
  369. }
  370. EXPORT_SYMBOL(efuse_shadow_read);
  371. void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset,
  372. u32 value)
  373. {
  374. if (type == 1)
  375. efuse_shadow_write_1byte(hw, offset, (u8) value);
  376. else if (type == 2)
  377. efuse_shadow_write_2byte(hw, offset, (u16) value);
  378. else if (type == 4)
  379. efuse_shadow_write_4byte(hw, offset, value);
  380. }
  381. bool efuse_shadow_update(struct ieee80211_hw *hw)
  382. {
  383. struct rtl_priv *rtlpriv = rtl_priv(hw);
  384. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  385. u16 i, offset, base;
  386. u8 word_en = 0x0F;
  387. u8 first_pg = false;
  388. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
  389. if (!efuse_shadow_update_chk(hw)) {
  390. efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
  391. memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
  392. &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
  393. rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
  394. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  395. "efuse out of capacity!!\n");
  396. return false;
  397. }
  398. efuse_power_switch(hw, true, true);
  399. for (offset = 0; offset < 16; offset++) {
  400. word_en = 0x0F;
  401. base = offset * 8;
  402. for (i = 0; i < 8; i++) {
  403. if (first_pg) {
  404. word_en &= ~(BIT(i / 2));
  405. rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
  406. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
  407. } else {
  408. if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
  409. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) {
  410. word_en &= ~(BIT(i / 2));
  411. rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
  412. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
  413. }
  414. }
  415. }
  416. if (word_en != 0x0F) {
  417. u8 tmpdata[8];
  418. memcpy(tmpdata,
  419. &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base],
  420. 8);
  421. RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD,
  422. "U-efuse\n", tmpdata, 8);
  423. if (!efuse_pg_packet_write(hw, (u8) offset, word_en,
  424. tmpdata)) {
  425. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  426. "PG section(%#x) fail!!\n", offset);
  427. break;
  428. }
  429. }
  430. }
  431. efuse_power_switch(hw, true, false);
  432. efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
  433. memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
  434. &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
  435. rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
  436. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
  437. return true;
  438. }
  439. void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw)
  440. {
  441. struct rtl_priv *rtlpriv = rtl_priv(hw);
  442. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  443. if (rtlefuse->autoload_failflag)
  444. memset((&rtlefuse->efuse_map[EFUSE_INIT_MAP][0]),
  445. 0xFF, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
  446. else
  447. efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
  448. memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
  449. &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
  450. rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
  451. }
  452. EXPORT_SYMBOL(rtl_efuse_shadow_map_update);
  453. void efuse_force_write_vendor_Id(struct ieee80211_hw *hw)
  454. {
  455. u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
  456. efuse_power_switch(hw, true, true);
  457. efuse_pg_packet_write(hw, 1, 0xD, tmpdata);
  458. efuse_power_switch(hw, true, false);
  459. }
  460. void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx)
  461. {
  462. }
  463. static void efuse_shadow_read_1byte(struct ieee80211_hw *hw,
  464. u16 offset, u8 *value)
  465. {
  466. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  467. *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
  468. }
  469. static void efuse_shadow_read_2byte(struct ieee80211_hw *hw,
  470. u16 offset, u16 *value)
  471. {
  472. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  473. *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
  474. *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
  475. }
  476. static void efuse_shadow_read_4byte(struct ieee80211_hw *hw,
  477. u16 offset, u32 *value)
  478. {
  479. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  480. *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
  481. *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
  482. *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16;
  483. *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24;
  484. }
  485. static void efuse_shadow_write_1byte(struct ieee80211_hw *hw,
  486. u16 offset, u8 value)
  487. {
  488. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  489. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value;
  490. }
  491. static void efuse_shadow_write_2byte(struct ieee80211_hw *hw,
  492. u16 offset, u16 value)
  493. {
  494. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  495. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF;
  496. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8;
  497. }
  498. static void efuse_shadow_write_4byte(struct ieee80211_hw *hw,
  499. u16 offset, u32 value)
  500. {
  501. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  502. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] =
  503. (u8) (value & 0x000000FF);
  504. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] =
  505. (u8) ((value >> 8) & 0x0000FF);
  506. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] =
  507. (u8) ((value >> 16) & 0x00FF);
  508. rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] =
  509. (u8) ((value >> 24) & 0xFF);
  510. }
  511. int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data)
  512. {
  513. struct rtl_priv *rtlpriv = rtl_priv(hw);
  514. u8 tmpidx = 0;
  515. int result;
  516. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
  517. (u8) (addr & 0xff));
  518. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
  519. ((u8) ((addr >> 8) & 0x03)) |
  520. (rtl_read_byte(rtlpriv,
  521. rtlpriv->cfg->maps[EFUSE_CTRL] + 2) &
  522. 0xFC));
  523. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
  524. while (!(0x80 & rtl_read_byte(rtlpriv,
  525. rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
  526. && (tmpidx < 100)) {
  527. tmpidx++;
  528. }
  529. if (tmpidx < 100) {
  530. *data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
  531. result = true;
  532. } else {
  533. *data = 0xff;
  534. result = false;
  535. }
  536. return result;
  537. }
  538. EXPORT_SYMBOL(efuse_one_byte_read);
  539. static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data)
  540. {
  541. struct rtl_priv *rtlpriv = rtl_priv(hw);
  542. u8 tmpidx = 0;
  543. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  544. "Addr = %x Data=%x\n", addr, data);
  545. rtl_write_byte(rtlpriv,
  546. rtlpriv->cfg->maps[EFUSE_CTRL] + 1, (u8) (addr & 0xff));
  547. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
  548. (rtl_read_byte(rtlpriv,
  549. rtlpriv->cfg->maps[EFUSE_CTRL] +
  550. 2) & 0xFC) | (u8) ((addr >> 8) & 0x03));
  551. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data);
  552. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2);
  553. while ((0x80 & rtl_read_byte(rtlpriv,
  554. rtlpriv->cfg->maps[EFUSE_CTRL] + 3))
  555. && (tmpidx < 100)) {
  556. tmpidx++;
  557. }
  558. if (tmpidx < 100)
  559. return true;
  560. return false;
  561. }
  562. static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse)
  563. {
  564. struct rtl_priv *rtlpriv = rtl_priv(hw);
  565. efuse_power_switch(hw, false, true);
  566. read_efuse(hw, 0, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE], efuse);
  567. efuse_power_switch(hw, false, false);
  568. }
  569. static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
  570. u8 efuse_data, u8 offset, u8 *tmpdata,
  571. u8 *readstate)
  572. {
  573. bool dataempty = true;
  574. u8 hoffset;
  575. u8 tmpidx;
  576. u8 hworden;
  577. u8 word_cnts;
  578. hoffset = (efuse_data >> 4) & 0x0F;
  579. hworden = efuse_data & 0x0F;
  580. word_cnts = efuse_calculate_word_cnts(hworden);
  581. if (hoffset == offset) {
  582. for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
  583. if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
  584. &efuse_data)) {
  585. tmpdata[tmpidx] = efuse_data;
  586. if (efuse_data != 0xff)
  587. dataempty = false;
  588. }
  589. }
  590. if (!dataempty) {
  591. *readstate = PG_STATE_DATA;
  592. } else {
  593. *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
  594. *readstate = PG_STATE_HEADER;
  595. }
  596. } else {
  597. *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
  598. *readstate = PG_STATE_HEADER;
  599. }
  600. }
  601. static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data)
  602. {
  603. u8 readstate = PG_STATE_HEADER;
  604. bool continual = true;
  605. u8 efuse_data, word_cnts = 0;
  606. u16 efuse_addr = 0;
  607. u8 tmpdata[8];
  608. if (data == NULL)
  609. return false;
  610. if (offset > 15)
  611. return false;
  612. memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
  613. memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
  614. while (continual && (efuse_addr < EFUSE_MAX_SIZE)) {
  615. if (readstate & PG_STATE_HEADER) {
  616. if (efuse_one_byte_read(hw, efuse_addr, &efuse_data)
  617. && (efuse_data != 0xFF))
  618. efuse_read_data_case1(hw, &efuse_addr,
  619. efuse_data, offset,
  620. tmpdata, &readstate);
  621. else
  622. continual = false;
  623. } else if (readstate & PG_STATE_DATA) {
  624. efuse_word_enable_data_read(0, tmpdata, data);
  625. efuse_addr = efuse_addr + (word_cnts * 2) + 1;
  626. readstate = PG_STATE_HEADER;
  627. }
  628. }
  629. if ((data[0] == 0xff) && (data[1] == 0xff) &&
  630. (data[2] == 0xff) && (data[3] == 0xff) &&
  631. (data[4] == 0xff) && (data[5] == 0xff) &&
  632. (data[6] == 0xff) && (data[7] == 0xff))
  633. return false;
  634. else
  635. return true;
  636. }
  637. static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
  638. u8 efuse_data, u8 offset,
  639. int *continual, u8 *write_state,
  640. struct pgpkt_struct *target_pkt,
  641. int *repeat_times, int *result, u8 word_en)
  642. {
  643. struct rtl_priv *rtlpriv = rtl_priv(hw);
  644. struct pgpkt_struct tmp_pkt;
  645. int dataempty = true;
  646. u8 originaldata[8 * sizeof(u8)];
  647. u8 badworden = 0x0F;
  648. u8 match_word_en, tmp_word_en;
  649. u8 tmpindex;
  650. u8 tmp_header = efuse_data;
  651. u8 tmp_word_cnts;
  652. tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
  653. tmp_pkt.word_en = tmp_header & 0x0F;
  654. tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
  655. if (tmp_pkt.offset != target_pkt->offset) {
  656. *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
  657. *write_state = PG_STATE_HEADER;
  658. } else {
  659. for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
  660. if (efuse_one_byte_read(hw,
  661. (*efuse_addr + 1 + tmpindex),
  662. &efuse_data) &&
  663. (efuse_data != 0xFF))
  664. dataempty = false;
  665. }
  666. if (!dataempty) {
  667. *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
  668. *write_state = PG_STATE_HEADER;
  669. } else {
  670. match_word_en = 0x0F;
  671. if (!((target_pkt->word_en & BIT(0)) |
  672. (tmp_pkt.word_en & BIT(0))))
  673. match_word_en &= (~BIT(0));
  674. if (!((target_pkt->word_en & BIT(1)) |
  675. (tmp_pkt.word_en & BIT(1))))
  676. match_word_en &= (~BIT(1));
  677. if (!((target_pkt->word_en & BIT(2)) |
  678. (tmp_pkt.word_en & BIT(2))))
  679. match_word_en &= (~BIT(2));
  680. if (!((target_pkt->word_en & BIT(3)) |
  681. (tmp_pkt.word_en & BIT(3))))
  682. match_word_en &= (~BIT(3));
  683. if ((match_word_en & 0x0F) != 0x0F) {
  684. badworden =
  685. enable_efuse_data_write(hw,
  686. *efuse_addr + 1,
  687. tmp_pkt.word_en,
  688. target_pkt->data);
  689. if (0x0F != (badworden & 0x0F)) {
  690. u8 reorg_offset = offset;
  691. u8 reorg_worden = badworden;
  692. efuse_pg_packet_write(hw, reorg_offset,
  693. reorg_worden,
  694. originaldata);
  695. }
  696. tmp_word_en = 0x0F;
  697. if ((target_pkt->word_en & BIT(0)) ^
  698. (match_word_en & BIT(0)))
  699. tmp_word_en &= (~BIT(0));
  700. if ((target_pkt->word_en & BIT(1)) ^
  701. (match_word_en & BIT(1)))
  702. tmp_word_en &= (~BIT(1));
  703. if ((target_pkt->word_en & BIT(2)) ^
  704. (match_word_en & BIT(2)))
  705. tmp_word_en &= (~BIT(2));
  706. if ((target_pkt->word_en & BIT(3)) ^
  707. (match_word_en & BIT(3)))
  708. tmp_word_en &= (~BIT(3));
  709. if ((tmp_word_en & 0x0F) != 0x0F) {
  710. *efuse_addr = efuse_get_current_size(hw);
  711. target_pkt->offset = offset;
  712. target_pkt->word_en = tmp_word_en;
  713. } else {
  714. *continual = false;
  715. }
  716. *write_state = PG_STATE_HEADER;
  717. *repeat_times += 1;
  718. if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
  719. *continual = false;
  720. *result = false;
  721. }
  722. } else {
  723. *efuse_addr += (2 * tmp_word_cnts) + 1;
  724. target_pkt->offset = offset;
  725. target_pkt->word_en = word_en;
  726. *write_state = PG_STATE_HEADER;
  727. }
  728. }
  729. }
  730. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse PG_STATE_HEADER-1\n");
  731. }
  732. static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr,
  733. int *continual, u8 *write_state,
  734. struct pgpkt_struct target_pkt,
  735. int *repeat_times, int *result)
  736. {
  737. struct rtl_priv *rtlpriv = rtl_priv(hw);
  738. struct pgpkt_struct tmp_pkt;
  739. u8 pg_header;
  740. u8 tmp_header;
  741. u8 originaldata[8 * sizeof(u8)];
  742. u8 tmp_word_cnts;
  743. u8 badworden = 0x0F;
  744. pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en;
  745. efuse_one_byte_write(hw, *efuse_addr, pg_header);
  746. efuse_one_byte_read(hw, *efuse_addr, &tmp_header);
  747. if (tmp_header == pg_header) {
  748. *write_state = PG_STATE_DATA;
  749. } else if (tmp_header == 0xFF) {
  750. *write_state = PG_STATE_HEADER;
  751. *repeat_times += 1;
  752. if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
  753. *continual = false;
  754. *result = false;
  755. }
  756. } else {
  757. tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
  758. tmp_pkt.word_en = tmp_header & 0x0F;
  759. tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
  760. memset(originaldata, 0xff, 8 * sizeof(u8));
  761. if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
  762. badworden = enable_efuse_data_write(hw,
  763. *efuse_addr + 1,
  764. tmp_pkt.word_en,
  765. originaldata);
  766. if (0x0F != (badworden & 0x0F)) {
  767. u8 reorg_offset = tmp_pkt.offset;
  768. u8 reorg_worden = badworden;
  769. efuse_pg_packet_write(hw, reorg_offset,
  770. reorg_worden,
  771. originaldata);
  772. *efuse_addr = efuse_get_current_size(hw);
  773. } else {
  774. *efuse_addr = *efuse_addr +
  775. (tmp_word_cnts * 2) + 1;
  776. }
  777. } else {
  778. *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
  779. }
  780. *write_state = PG_STATE_HEADER;
  781. *repeat_times += 1;
  782. if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
  783. *continual = false;
  784. *result = false;
  785. }
  786. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
  787. "efuse PG_STATE_HEADER-2\n");
  788. }
  789. }
  790. static int efuse_pg_packet_write(struct ieee80211_hw *hw,
  791. u8 offset, u8 word_en, u8 *data)
  792. {
  793. struct rtl_priv *rtlpriv = rtl_priv(hw);
  794. struct pgpkt_struct target_pkt;
  795. u8 write_state = PG_STATE_HEADER;
  796. int continual = true, dataempty = true, result = true;
  797. u16 efuse_addr = 0;
  798. u8 efuse_data;
  799. u8 target_word_cnts = 0;
  800. u8 badworden = 0x0F;
  801. static int repeat_times;
  802. if (efuse_get_current_size(hw) >= (EFUSE_MAX_SIZE -
  803. rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
  804. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
  805. "efuse_pg_packet_write error\n");
  806. return false;
  807. }
  808. target_pkt.offset = offset;
  809. target_pkt.word_en = word_en;
  810. memset(target_pkt.data, 0xFF, 8 * sizeof(u8));
  811. efuse_word_enable_data_read(word_en, data, target_pkt.data);
  812. target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en);
  813. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse Power ON\n");
  814. while (continual && (efuse_addr < (EFUSE_MAX_SIZE -
  815. rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))) {
  816. if (write_state == PG_STATE_HEADER) {
  817. dataempty = true;
  818. badworden = 0x0F;
  819. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
  820. "efuse PG_STATE_HEADER\n");
  821. if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
  822. (efuse_data != 0xFF))
  823. efuse_write_data_case1(hw, &efuse_addr,
  824. efuse_data, offset,
  825. &continual,
  826. &write_state,
  827. &target_pkt,
  828. &repeat_times, &result,
  829. word_en);
  830. else
  831. efuse_write_data_case2(hw, &efuse_addr,
  832. &continual,
  833. &write_state,
  834. target_pkt,
  835. &repeat_times,
  836. &result);
  837. } else if (write_state == PG_STATE_DATA) {
  838. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
  839. "efuse PG_STATE_DATA\n");
  840. badworden = 0x0f;
  841. badworden =
  842. enable_efuse_data_write(hw, efuse_addr + 1,
  843. target_pkt.word_en,
  844. target_pkt.data);
  845. if ((badworden & 0x0F) == 0x0F) {
  846. continual = false;
  847. } else {
  848. efuse_addr =
  849. efuse_addr + (2 * target_word_cnts) + 1;
  850. target_pkt.offset = offset;
  851. target_pkt.word_en = badworden;
  852. target_word_cnts =
  853. efuse_calculate_word_cnts(target_pkt.
  854. word_en);
  855. write_state = PG_STATE_HEADER;
  856. repeat_times++;
  857. if (repeat_times > EFUSE_REPEAT_THRESHOLD_) {
  858. continual = false;
  859. result = false;
  860. }
  861. RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
  862. "efuse PG_STATE_HEADER-3\n");
  863. }
  864. }
  865. }
  866. if (efuse_addr >= (EFUSE_MAX_SIZE -
  867. rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
  868. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  869. "efuse_addr(%#x) Out of size!!\n", efuse_addr);
  870. }
  871. return true;
  872. }
  873. static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
  874. u8 *targetdata)
  875. {
  876. if (!(word_en & BIT(0))) {
  877. targetdata[0] = sourdata[0];
  878. targetdata[1] = sourdata[1];
  879. }
  880. if (!(word_en & BIT(1))) {
  881. targetdata[2] = sourdata[2];
  882. targetdata[3] = sourdata[3];
  883. }
  884. if (!(word_en & BIT(2))) {
  885. targetdata[4] = sourdata[4];
  886. targetdata[5] = sourdata[5];
  887. }
  888. if (!(word_en & BIT(3))) {
  889. targetdata[6] = sourdata[6];
  890. targetdata[7] = sourdata[7];
  891. }
  892. }
  893. static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
  894. u16 efuse_addr, u8 word_en, u8 *data)
  895. {
  896. struct rtl_priv *rtlpriv = rtl_priv(hw);
  897. u16 tmpaddr;
  898. u16 start_addr = efuse_addr;
  899. u8 badworden = 0x0F;
  900. u8 tmpdata[8];
  901. memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
  902. RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
  903. "word_en = %x efuse_addr=%x\n", word_en, efuse_addr);
  904. if (!(word_en & BIT(0))) {
  905. tmpaddr = start_addr;
  906. efuse_one_byte_write(hw, start_addr++, data[0]);
  907. efuse_one_byte_write(hw, start_addr++, data[1]);
  908. efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
  909. efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
  910. if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
  911. badworden &= (~BIT(0));
  912. }
  913. if (!(word_en & BIT(1))) {
  914. tmpaddr = start_addr;
  915. efuse_one_byte_write(hw, start_addr++, data[2]);
  916. efuse_one_byte_write(hw, start_addr++, data[3]);
  917. efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
  918. efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
  919. if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
  920. badworden &= (~BIT(1));
  921. }
  922. if (!(word_en & BIT(2))) {
  923. tmpaddr = start_addr;
  924. efuse_one_byte_write(hw, start_addr++, data[4]);
  925. efuse_one_byte_write(hw, start_addr++, data[5]);
  926. efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
  927. efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
  928. if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
  929. badworden &= (~BIT(2));
  930. }
  931. if (!(word_en & BIT(3))) {
  932. tmpaddr = start_addr;
  933. efuse_one_byte_write(hw, start_addr++, data[6]);
  934. efuse_one_byte_write(hw, start_addr++, data[7]);
  935. efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
  936. efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
  937. if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
  938. badworden &= (~BIT(3));
  939. }
  940. return badworden;
  941. }
  942. static void efuse_power_switch(struct ieee80211_hw *hw, u8 write, u8 pwrstate)
  943. {
  944. struct rtl_priv *rtlpriv = rtl_priv(hw);
  945. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  946. u8 tempval;
  947. u16 tmpV16;
  948. if (pwrstate && (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)) {
  949. if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
  950. rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) {
  951. rtl_write_byte(rtlpriv,
  952. rtlpriv->cfg->maps[EFUSE_ACCESS], 0x69);
  953. } else {
  954. tmpV16 =
  955. rtl_read_word(rtlpriv,
  956. rtlpriv->cfg->maps[SYS_ISO_CTRL]);
  957. if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) {
  958. tmpV16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V];
  959. rtl_write_word(rtlpriv,
  960. rtlpriv->cfg->maps[SYS_ISO_CTRL],
  961. tmpV16);
  962. }
  963. }
  964. tmpV16 = rtl_read_word(rtlpriv,
  965. rtlpriv->cfg->maps[SYS_FUNC_EN]);
  966. if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) {
  967. tmpV16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR];
  968. rtl_write_word(rtlpriv,
  969. rtlpriv->cfg->maps[SYS_FUNC_EN], tmpV16);
  970. }
  971. tmpV16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]);
  972. if ((!(tmpV16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) ||
  973. (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) {
  974. tmpV16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] |
  975. rtlpriv->cfg->maps[EFUSE_ANA8M]);
  976. rtl_write_word(rtlpriv,
  977. rtlpriv->cfg->maps[SYS_CLK], tmpV16);
  978. }
  979. }
  980. if (pwrstate) {
  981. if (write) {
  982. tempval = rtl_read_byte(rtlpriv,
  983. rtlpriv->cfg->maps[EFUSE_TEST] +
  984. 3);
  985. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
  986. tempval &= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6));
  987. tempval |= (VOLTAGE_V25 << 3);
  988. } else if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE) {
  989. tempval &= 0x0F;
  990. tempval |= (VOLTAGE_V25 << 4);
  991. }
  992. rtl_write_byte(rtlpriv,
  993. rtlpriv->cfg->maps[EFUSE_TEST] + 3,
  994. (tempval | 0x80));
  995. }
  996. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
  997. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
  998. 0x03);
  999. }
  1000. } else {
  1001. if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
  1002. rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE)
  1003. rtl_write_byte(rtlpriv,
  1004. rtlpriv->cfg->maps[EFUSE_ACCESS], 0);
  1005. if (write) {
  1006. tempval = rtl_read_byte(rtlpriv,
  1007. rtlpriv->cfg->maps[EFUSE_TEST] +
  1008. 3);
  1009. rtl_write_byte(rtlpriv,
  1010. rtlpriv->cfg->maps[EFUSE_TEST] + 3,
  1011. (tempval & 0x7F));
  1012. }
  1013. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
  1014. rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
  1015. 0x02);
  1016. }
  1017. }
  1018. }
  1019. static u16 efuse_get_current_size(struct ieee80211_hw *hw)
  1020. {
  1021. int continual = true;
  1022. u16 efuse_addr = 0;
  1023. u8 hoffset, hworden;
  1024. u8 efuse_data, word_cnts;
  1025. while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
  1026. (efuse_addr < EFUSE_MAX_SIZE)) {
  1027. if (efuse_data != 0xFF) {
  1028. hoffset = (efuse_data >> 4) & 0x0F;
  1029. hworden = efuse_data & 0x0F;
  1030. word_cnts = efuse_calculate_word_cnts(hworden);
  1031. efuse_addr = efuse_addr + (word_cnts * 2) + 1;
  1032. } else {
  1033. continual = false;
  1034. }
  1035. }
  1036. return efuse_addr;
  1037. }
  1038. static u8 efuse_calculate_word_cnts(u8 word_en)
  1039. {
  1040. u8 word_cnts = 0;
  1041. if (!(word_en & BIT(0)))
  1042. word_cnts++;
  1043. if (!(word_en & BIT(1)))
  1044. word_cnts++;
  1045. if (!(word_en & BIT(2)))
  1046. word_cnts++;
  1047. if (!(word_en & BIT(3)))
  1048. word_cnts++;
  1049. return word_cnts;
  1050. }