efuse.c 36 KB

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