efuse.c 37 KB

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