30 #include <linux/export.h>
40 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
77 static void efuse_word_enable_data_read(
u8 word_en,
u8 *sourdata,
84 static u8 efuse_calculate_word_cnts(
u8 word_en);
92 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
SYS_FUNC_EN] + 1);
93 temp = bytetemp | 0x20;
94 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
SYS_FUNC_EN] + 1, temp);
96 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
SYS_ISO_CTRL] + 1);
97 temp = bytetemp & 0xFE;
100 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_TEST] + 3);
101 temp = bytetemp | 0x80;
102 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_TEST] + 3, temp);
104 rtl_write_byte(rtlpriv, 0x2F8, 0x3);
106 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL] + 3, 0x72);
117 const u32 efuse_len =
120 if (address < efuse_len) {
121 temp = address & 0xFF;
124 bytetemp = rtl_read_byte(rtlpriv,
126 temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
130 bytetemp = rtl_read_byte(rtlpriv,
132 temp = bytetemp & 0x7F;
136 bytetemp = rtl_read_byte(rtlpriv,
138 while (!(bytetemp & 0x80)) {
139 bytetemp = rtl_read_byte(rtlpriv,
148 data = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL]);
162 const u32 efuse_len =
168 if (address < efuse_len) {
169 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL], value);
171 temp = address & 0xFF;
174 bytetemp = rtl_read_byte(rtlpriv,
177 temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
178 rtl_write_byte(rtlpriv,
181 bytetemp = rtl_read_byte(rtlpriv,
183 temp = bytetemp | 0x80;
184 rtl_write_byte(rtlpriv,
187 bytetemp = rtl_read_byte(rtlpriv,
190 while (bytetemp & 0x80) {
191 bytetemp = rtl_read_byte(rtlpriv,
213 readbyte = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL] + 2);
215 ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
217 readbyte = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL] + 3);
222 value32 = rtl_read_dword(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL]);
223 while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
224 value32 = rtl_read_dword(rtlpriv,
230 value32 = rtl_read_dword(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL]);
232 *pbuf = (
u8) (value32 & 0xff);
245 const u16 efuse_max_section =
247 const u32 efuse_len =
250 u16 efuse_utilized = 0;
255 "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n",
256 _offset, _size_byte);
269 efuse_word[
i] =
kmalloc(efuse_max_section *
sizeof(
u16),
275 for (i = 0; i < efuse_max_section; i++)
277 efuse_word[j][i] = 0xFFFF;
280 if (*rtemp8 != 0xFF) {
283 "Addr=%d\n", efuse_addr);
287 while ((*rtemp8 != 0xFF) && (efuse_addr < efuse_len)) {
288 offset = ((*rtemp8 >> 4) & 0x0f);
290 if (offset < efuse_max_section) {
291 wren = (*rtemp8 & 0x0f);
293 "offset-%d Worden=%x\n", offset, wren);
296 if (!(wren & 0x01)) {
299 "Addr=%d\n", efuse_addr);
307 if (efuse_addr >= efuse_len)
312 "Addr=%d\n", efuse_addr);
318 (((
u16)*rtemp8 << 8) & 0xff00);
320 if (efuse_addr >= efuse_len)
329 "Addr=%d\n", efuse_addr);
331 if (*rtemp8 != 0xFF && (efuse_addr < efuse_len)) {
337 for (i = 0; i < efuse_max_section; i++) {
339 efuse_tbl[(i * 8) + (j * 2)] =
340 (efuse_word[
j][
i] & 0xff);
341 efuse_tbl[(i * 8) + ((j * 2) + 1)] =
342 ((efuse_word[j][i] >> 8) & 0xff);
346 for (i = 0; i < _size_byte; i++)
347 pbuf[i] = efuse_tbl[_offset + i];
350 efuse_usage = (
u8) ((efuse_utilized * 100) / efuse_len);
353 (
u8 *)&efuse_utilized);
358 kfree(efuse_word[i]);
367 u8 section_idx,
i, Base;
368 u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used;
369 bool wordchanged,
result =
true;
371 for (section_idx = 0; section_idx < 16; section_idx++) {
372 Base = section_idx * 8;
375 for (i = 0; i < 8; i = i + 2) {
390 totalbytes = hdr_num + words_need * 2;
393 if ((totalbytes + efuse_used) >=
398 "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
399 totalbytes, hdr_num, words_need, efuse_used);
408 efuse_shadow_read_1byte(hw, offset, (
u8 *) value);
410 efuse_shadow_read_2byte(hw, offset, (
u16 *) value);
412 efuse_shadow_read_4byte(hw, offset, value);
420 efuse_shadow_write_1byte(hw, offset, (
u8) value);
422 efuse_shadow_write_2byte(hw, offset, (
u16) value);
424 efuse_shadow_write_4byte(hw, offset, value);
445 "<---efuse out of capacity!!\n");
448 efuse_power_switch(hw,
true,
true);
450 for (offset = 0; offset < 16; offset++) {
455 for (i = 0; i < 8; i++) {
458 word_en &= ~(
BIT(i / 2));
466 word_en &= ~(
BIT(i / 2));
474 if (word_en != 0x0F) {
480 "U-efuse", tmpdata, 8);
482 if (!efuse_pg_packet_write(hw, (
u8) offset, word_en,
485 "PG section(%#x) fail!!\n", offset);
492 efuse_power_switch(hw,
true,
false);
523 u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
525 efuse_power_switch(hw,
true,
true);
527 efuse_pg_packet_write(hw, 1, 0xD, tmpdata);
529 efuse_power_switch(hw,
true,
false);
544 static void efuse_shadow_read_2byte(
struct ieee80211_hw *hw,
554 static void efuse_shadow_read_4byte(
struct ieee80211_hw *hw,
565 static void efuse_shadow_write_1byte(
struct ieee80211_hw *hw,
566 u16 offset,
u8 value)
573 static void efuse_shadow_write_2byte(
struct ieee80211_hw *hw,
583 static void efuse_shadow_write_4byte(
struct ieee80211_hw *hw,
589 (
u8) (value & 0x000000FF);
591 (
u8) ((value >> 8) & 0x0000FF);
593 (
u8) ((value >> 16) & 0x00FF);
595 (
u8) ((value >> 24) & 0xFF);
608 ((
u8) ((addr >> 8) & 0x03)) |
609 (rtl_read_byte(rtlpriv,
613 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL] + 3, 0x72);
615 while (!(0x80 & rtl_read_byte(rtlpriv,
622 *data = rtl_read_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL]);
639 rtl_write_byte(rtlpriv,
642 (rtl_read_byte(rtlpriv,
644 2) & 0xFC) | (
u8) ((addr >> 8) & 0x03));
646 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL], data);
647 rtl_write_byte(rtlpriv, rtlpriv->
cfg->maps[
EFUSE_CTRL] + 3, 0xF2);
649 while ((0x80 & rtl_read_byte(rtlpriv,
664 efuse_power_switch(hw,
false,
true);
666 efuse_power_switch(hw,
false,
false);
669 static void efuse_read_data_case1(
struct ieee80211_hw *hw,
u16 *efuse_addr,
670 u8 efuse_data,
u8 offset,
u8 *tmpdata,
673 bool dataempty =
true;
679 hoffset = (efuse_data >> 4) & 0x0F;
680 hworden = efuse_data & 0x0F;
681 word_cnts = efuse_calculate_word_cnts(hworden);
683 if (hoffset == offset) {
684 for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
685 if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
687 tmpdata[tmpidx] = efuse_data;
688 if (efuse_data != 0xff)
696 *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
701 *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
706 static int efuse_pg_packet_read(
struct ieee80211_hw *hw,
u8 offset,
u8 *data)
709 bool continual =
true;
710 u8 efuse_data, word_cnts = 0;
724 if (efuse_one_byte_read(hw, efuse_addr, &efuse_data)
725 && (efuse_data != 0xFF))
726 efuse_read_data_case1(hw, &efuse_addr,
733 efuse_word_enable_data_read(0, tmpdata, data);
734 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
740 if ((data[0] == 0xff) && (data[1] == 0xff) &&
741 (data[2] == 0xff) && (data[3] == 0xff) &&
742 (data[4] == 0xff) && (data[5] == 0xff) &&
743 (data[6] == 0xff) && (data[7] == 0xff))
750 static void efuse_write_data_case1(
struct ieee80211_hw *hw,
u16 *efuse_addr,
751 u8 efuse_data,
u8 offset,
int *continual,
753 int *repeat_times,
int *result,
u8 word_en)
757 bool dataempty =
true;
758 u8 originaldata[8 *
sizeof(
u8)];
760 u8 match_word_en, tmp_word_en;
762 u8 tmp_header = efuse_data;
765 tmp_pkt.
offset = (tmp_header >> 4) & 0x0F;
766 tmp_pkt.word_en = tmp_header & 0x0F;
767 tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
769 if (tmp_pkt.offset != target_pkt->
offset) {
770 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
773 for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
775 if (efuse_one_byte_read(hw, address,
776 &efuse_data) && (efuse_data != 0xFF))
781 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
784 match_word_en = 0x0F;
786 (tmp_pkt.word_en &
BIT(0))))
787 match_word_en &= (~
BIT(0));
790 (tmp_pkt.word_en &
BIT(1))))
791 match_word_en &= (~
BIT(1));
794 (tmp_pkt.word_en &
BIT(2))))
795 match_word_en &= (~
BIT(2));
798 (tmp_pkt.word_en &
BIT(3))))
799 match_word_en &= (~
BIT(3));
801 if ((match_word_en & 0x0F) != 0x0F) {
802 badworden = efuse_word_enable_data_write(
807 if (0x0F != (badworden & 0x0F)) {
809 u8 reorg_worden = badworden;
810 efuse_pg_packet_write(hw, reorg_offset,
817 (match_word_en &
BIT(0)))
818 tmp_word_en &= (~
BIT(0));
821 (match_word_en &
BIT(1)))
822 tmp_word_en &= (~
BIT(1));
825 (match_word_en &
BIT(2)))
826 tmp_word_en &= (~
BIT(2));
829 (match_word_en &
BIT(3)))
830 tmp_word_en &= (~
BIT(3));
832 if ((tmp_word_en & 0x0F) != 0x0F) {
833 *efuse_addr = efuse_get_current_size(hw);
835 target_pkt->
word_en = tmp_word_en;
846 *efuse_addr += (2 * tmp_word_cnts) + 1;
856 static void efuse_write_data_case2(
struct ieee80211_hw *hw,
u16 *efuse_addr,
857 int *continual,
u8 *write_state,
859 int *repeat_times,
int *result)
865 u8 originaldata[8 *
sizeof(
u8)];
869 pg_header = ((target_pkt.
offset << 4) & 0xf0) | target_pkt.
word_en;
870 efuse_one_byte_write(hw, *efuse_addr, pg_header);
871 efuse_one_byte_read(hw, *efuse_addr, &tmp_header);
873 if (tmp_header == pg_header) {
875 }
else if (tmp_header == 0xFF) {
883 tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
884 tmp_pkt.word_en = tmp_header & 0x0F;
886 tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
888 memset(originaldata, 0xff, 8 *
sizeof(
u8));
890 if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
891 badworden = efuse_word_enable_data_write(hw,
892 *efuse_addr + 1, tmp_pkt.word_en,
895 if (0x0F != (badworden & 0x0F)) {
896 u8 reorg_offset = tmp_pkt.offset;
897 u8 reorg_worden = badworden;
898 efuse_pg_packet_write(hw, reorg_offset,
901 *efuse_addr = efuse_get_current_size(hw);
903 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2)
907 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
918 "efuse PG_STATE_HEADER-2\n");
922 static int efuse_pg_packet_write(
struct ieee80211_hw *hw,
923 u8 offset,
u8 word_en,
u8 *data)
928 int continual =
true, result =
true;
931 u8 target_word_cnts = 0;
933 static int repeat_times;
935 if (efuse_get_current_size(hw) >=
938 "efuse_pg_packet_write error\n");
947 efuse_word_enable_data_read(word_en, data, target_pkt.
data);
948 target_word_cnts = efuse_calculate_word_cnts(target_pkt.
word_en);
952 while (continual && (efuse_addr <
955 if (write_state == PG_STATE_HEADER) {
958 "efuse PG_STATE_HEADER\n");
960 if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
961 (efuse_data != 0xFF))
962 efuse_write_data_case1(hw, &efuse_addr,
965 &write_state, &target_pkt,
966 &repeat_times, &result,
969 efuse_write_data_case2(hw, &efuse_addr,
978 "efuse PG_STATE_DATA\n");
980 efuse_word_enable_data_write(hw, efuse_addr + 1,
984 if ((badworden & 0x0F) == 0x0F) {
987 efuse_addr += (2 * target_word_cnts) + 1;
990 target_pkt.
word_en = badworden;
992 efuse_calculate_word_cnts(target_pkt.
1001 "efuse PG_STATE_HEADER-3\n");
1008 "efuse_addr(%#x) Out of size!!\n", efuse_addr);
1014 static void efuse_word_enable_data_read(
u8 word_en,
1015 u8 *sourdata,
u8 *targetdata)
1017 if (!(word_en &
BIT(0))) {
1018 targetdata[0] = sourdata[0];
1019 targetdata[1] = sourdata[1];
1022 if (!(word_en &
BIT(1))) {
1023 targetdata[2] = sourdata[2];
1024 targetdata[3] = sourdata[3];
1027 if (!(word_en &
BIT(2))) {
1028 targetdata[4] = sourdata[4];
1029 targetdata[5] = sourdata[5];
1032 if (!(word_en &
BIT(3))) {
1033 targetdata[6] = sourdata[6];
1034 targetdata[7] = sourdata[7];
1039 u16 efuse_addr,
u8 word_en,
u8 *data)
1044 u8 badworden = 0x0F;
1049 word_en, efuse_addr);
1051 if (!(word_en &
BIT(0))) {
1053 efuse_one_byte_write(hw, start_addr++, data[0]);
1054 efuse_one_byte_write(hw, start_addr++, data[1]);
1056 efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
1057 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
1058 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
1059 badworden &= (~
BIT(0));
1062 if (!(word_en &
BIT(1))) {
1064 efuse_one_byte_write(hw, start_addr++, data[2]);
1065 efuse_one_byte_write(hw, start_addr++, data[3]);
1067 efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
1068 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
1069 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
1070 badworden &= (~
BIT(1));
1073 if (!(word_en &
BIT(2))) {
1075 efuse_one_byte_write(hw, start_addr++, data[4]);
1076 efuse_one_byte_write(hw, start_addr++, data[5]);
1078 efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
1079 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
1080 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
1081 badworden &= (~
BIT(2));
1084 if (!(word_en &
BIT(3))) {
1086 efuse_one_byte_write(hw, start_addr++, data[6]);
1087 efuse_one_byte_write(hw, start_addr++, data[7]);
1089 efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
1090 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
1091 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
1092 badworden &= (~
BIT(3));
1105 if (pwrstate && (rtlhal->
hw_type !=
1107 tmpV16 = rtl_read_word(rtlpriv,
1111 rtl_write_word(rtlpriv,
1116 tmpV16 = rtl_read_word(rtlpriv,
1120 rtl_write_word(rtlpriv,
1124 tmpV16 = rtl_read_word(rtlpriv, rtlpriv->
cfg->maps[
SYS_CLK]);
1129 rtl_write_word(rtlpriv,
1136 tempval = rtl_read_byte(rtlpriv,
1145 rtl_write_byte(rtlpriv,
1157 tempval = rtl_read_byte(rtlpriv,
1160 rtl_write_byte(rtlpriv,
1176 int continual =
true;
1179 u8 efuse_data, word_cnts;
1181 while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data)
1183 if (efuse_data != 0xFF) {
1184 hworden = efuse_data & 0x0F;
1185 word_cnts = efuse_calculate_word_cnts(hworden);
1186 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
1195 static u8 efuse_calculate_word_cnts(
u8 word_en)
1198 if (!(word_en &
BIT(0)))
1200 if (!(word_en &
BIT(1)))
1202 if (!(word_en &
BIT(2)))
1204 if (!(word_en &
BIT(3)))