1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6 * Linux device driver for RTL8192SU
8 * Modifications for inclusion into the Linux staging tree are
9 * Copyright(c) 2010 Larry Finger. All rights reserved.
11 * Contact information:
15 ******************************************************************************/
17 #define _RTL8712_EFUSE_C_
19 #include "osdep_service.h"
20 #include "drv_types.h"
21 #include "rtl8712_efuse.h"
23 /* reserve 3 bytes for HW stop read */
24 static int efuse_available_max_size = EFUSE_MAX_SIZE - 3 /*0x1FD*/;
26 static void efuse_reg_ctrl(struct _adapter *adapter, u8 bPowerOn)
31 /* -----------------e-fuse pwr & clk reg ctrl ---------------
32 * Enable LDOE25 Macro Block
34 tmpu8 = r8712_read8(adapter, EFUSE_TEST + 3);
36 r8712_write8(adapter, EFUSE_TEST + 3, tmpu8);
37 msleep(20); /* for some platform , need some delay time */
38 /* Change Efuse Clock for write action to 40MHZ */
39 r8712_write8(adapter, EFUSE_CLK_CTRL, 0x03);
40 msleep(20); /* for some platform , need some delay time */
42 /* -----------------e-fuse pwr & clk reg ctrl -----------------
43 * Disable LDOE25 Macro Block
45 tmpu8 = r8712_read8(adapter, EFUSE_TEST + 3);
47 r8712_write8(adapter, EFUSE_TEST + 3, tmpu8);
48 /* Change Efuse Clock for write action to 500K */
49 r8712_write8(adapter, EFUSE_CLK_CTRL, 0x02);
54 * Before write E-Fuse, this function must be called.
56 u8 r8712_efuse_reg_init(struct _adapter *adapter)
61 void r8712_efuse_reg_uninit(struct _adapter *adapter)
63 efuse_reg_ctrl(adapter, false);
66 static u8 efuse_one_byte_read(struct _adapter *adapter, u16 addr, u8 *data)
68 u8 tmpidx = 0, bResult;
70 /* -----------------e-fuse reg ctrl --------------------------------- */
71 r8712_write8(adapter, EFUSE_CTRL + 1, (u8)(addr & 0xFF)); /* address */
72 r8712_write8(adapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
73 (r8712_read8(adapter, EFUSE_CTRL + 2) & 0xFC));
74 r8712_write8(adapter, EFUSE_CTRL + 3, 0x72); /* read cmd */
75 /* wait for complete */
76 while (!(0x80 & r8712_read8(adapter, EFUSE_CTRL + 3)) &&
80 *data = r8712_read8(adapter, EFUSE_CTRL);
89 static u8 efuse_one_byte_write(struct _adapter *adapter, u16 addr, u8 data)
91 u8 tmpidx = 0, bResult;
93 /* -----------------e-fuse reg ctrl -------------------------------- */
94 r8712_write8(adapter, EFUSE_CTRL + 1, (u8)(addr & 0xFF)); /* address */
95 r8712_write8(adapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
96 (r8712_read8(adapter, EFUSE_CTRL + 2) & 0xFC));
97 r8712_write8(adapter, EFUSE_CTRL, data); /* data */
98 r8712_write8(adapter, EFUSE_CTRL + 3, 0xF2); /* write cmd */
99 /* wait for complete */
100 while ((0x80 & r8712_read8(adapter, EFUSE_CTRL + 3)) &&
110 static u8 efuse_one_byte_rw(struct _adapter *adapter, u8 bRead, u16 addr,
113 u8 tmpidx = 0, tmpv8 = 0, bResult;
115 /* -----------------e-fuse reg ctrl --------------------------------- */
116 r8712_write8(adapter, EFUSE_CTRL + 1, (u8)(addr & 0xFF)); /* address */
117 tmpv8 = ((u8)((addr >> 8) & 0x03)) |
118 (r8712_read8(adapter, EFUSE_CTRL + 2) & 0xFC);
119 r8712_write8(adapter, EFUSE_CTRL + 2, tmpv8);
121 r8712_write8(adapter, EFUSE_CTRL + 3, 0x72); /* read cmd */
122 while (!(0x80 & r8712_read8(adapter, EFUSE_CTRL + 3)) &&
126 *data = r8712_read8(adapter, EFUSE_CTRL);
133 r8712_write8(adapter, EFUSE_CTRL, *data); /* data */
134 r8712_write8(adapter, EFUSE_CTRL + 3, 0xF2); /* write cmd */
135 while ((0x80 & r8712_read8(adapter, EFUSE_CTRL + 3)) &&
146 static u8 efuse_is_empty(struct _adapter *adapter, u8 *empty)
148 u8 value, ret = true;
150 /* read one byte to check if E-Fuse is empty */
151 if (efuse_one_byte_rw(adapter, true, 0, &value)) {
162 void r8712_efuse_change_max_size(struct _adapter *adapter)
164 u16 pre_pg_data_saddr = 0x1FB;
166 u16 pre_pg_data_size = 5;
169 for (i = 0; i < pre_pg_data_size; i++)
170 efuse_one_byte_read(adapter, pre_pg_data_saddr + i,
172 if ((pre_pg_data[0] == 0x03) && (pre_pg_data[1] == 0x00) &&
173 (pre_pg_data[2] == 0x00) && (pre_pg_data[3] == 0x00) &&
174 (pre_pg_data[4] == 0x0C))
175 efuse_available_max_size -= pre_pg_data_size;
178 int r8712_efuse_get_max_size(struct _adapter *adapter)
180 return efuse_available_max_size;
183 static u8 calculate_word_cnts(const u8 word_en)
188 for (word_idx = 0; word_idx < PGPKG_MAX_WORDS; word_idx++)
189 if (!(word_en & BIT(word_idx)))
190 word_cnts++; /* 0 : write enable */
194 static void pgpacket_copy_data(const u8 word_en, const u8 *sourdata,
198 u8 word_idx, byte_idx;
200 for (word_idx = 0; word_idx < PGPKG_MAX_WORDS; word_idx++) {
201 if (!(word_en & BIT(word_idx))) {
202 byte_idx = word_idx * 2;
203 targetdata[byte_idx] = sourdata[tmpindex++];
204 targetdata[byte_idx + 1] = sourdata[tmpindex++];
209 u16 r8712_efuse_get_current_size(struct _adapter *adapter)
211 int bContinual = true;
214 u8 efuse_data, word_cnts = 0;
216 while (bContinual && efuse_one_byte_read(adapter, efuse_addr,
217 &efuse_data) && (efuse_addr < efuse_available_max_size)) {
218 if (efuse_data != 0xFF) {
219 hworden = efuse_data & 0x0F;
220 word_cnts = calculate_word_cnts(hworden);
221 /* read next header */
222 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
230 u8 r8712_efuse_pg_packet_read(struct _adapter *adapter, u8 offset, u8 *data)
232 u8 hoffset = 0, hworden = 0, word_cnts = 0;
236 u8 tmpdata[PGPKT_DATA_SIZE];
243 memset(data, 0xFF, sizeof(u8) * PGPKT_DATA_SIZE);
244 while (efuse_addr < efuse_available_max_size) {
245 if (efuse_one_byte_read(adapter, efuse_addr, &efuse_data)) {
246 if (efuse_data == 0xFF)
248 hoffset = (efuse_data >> 4) & 0x0F;
249 hworden = efuse_data & 0x0F;
250 word_cnts = calculate_word_cnts(hworden);
251 if (hoffset == offset) {
252 memset(tmpdata, 0xFF, PGPKT_DATA_SIZE);
253 for (tmpidx = 0; tmpidx < word_cnts * 2;
255 if (efuse_one_byte_read(adapter,
256 efuse_addr + 1 + tmpidx,
258 tmpdata[tmpidx] = efuse_data;
263 pgpacket_copy_data(hworden, tmpdata, data);
265 efuse_addr += 1 + (word_cnts * 2);
274 static u8 fix_header(struct _adapter *adapter, u8 header, u16 header_addr)
276 struct PGPKT_STRUCT pkt;
277 u8 offset, word_en, value;
282 pkt.offset = GET_EFUSE_OFFSET(header);
283 pkt.word_en = GET_EFUSE_WORD_EN(header);
284 addr = header_addr + 1 + calculate_word_cnts(pkt.word_en) * 2;
285 if (addr > efuse_available_max_size)
287 /* retrieve original data */
289 while (addr < header_addr) {
290 if (!efuse_one_byte_read(adapter, addr++, &value)) {
294 offset = GET_EFUSE_OFFSET(value);
295 word_en = GET_EFUSE_WORD_EN(value);
296 if (pkt.offset != offset) {
297 addr += calculate_word_cnts(word_en) * 2;
300 for (i = 0; i < PGPKG_MAX_WORDS; i++) {
301 if (BIT(i) & word_en) {
302 if (BIT(i) & pkt.word_en) {
303 if (efuse_one_byte_read(
306 pkt.data[i * 2] = value;
309 if (efuse_one_byte_read(
313 pkt.data[i * 2 + 1] =
322 if (addr != header_addr)
325 /* fill original data */
326 for (i = 0; i < PGPKG_MAX_WORDS; i++) {
327 if (BIT(i) & pkt.word_en) {
328 efuse_one_byte_write(adapter, addr, pkt.data[i * 2]);
329 efuse_one_byte_write(adapter, addr + 1,
330 pkt.data[i * 2 + 1]);
331 /* additional check */
332 if (!efuse_one_byte_read(adapter, addr, &value)) {
334 } else if (pkt.data[i * 2] != value) {
336 if (value == 0xFF) /* write again */
337 efuse_one_byte_write(adapter, addr,
340 if (!efuse_one_byte_read(adapter, addr + 1, &value)) {
342 } else if (pkt.data[i * 2 + 1] != value) {
344 if (value == 0xFF) /* write again */
345 efuse_one_byte_write(adapter, addr + 1,
355 u8 r8712_efuse_pg_packet_write(struct _adapter *adapter, const u8 offset,
356 const u8 word_en, const u8 *data)
359 u16 efuse_addr = 0, curr_size = 0;
360 u8 efuse_data, target_word_cnts = 0;
365 /* check if E-Fuse Clock Enable and E-Fuse Clock is 40M */
366 efuse_data = r8712_read8(adapter, EFUSE_CLK_CTRL);
367 if (efuse_data != 0x03)
369 pg_header = MAKE_EFUSE_HEADER(offset, word_en);
370 target_word_cnts = calculate_word_cnts(word_en);
373 while (efuse_addr < efuse_available_max_size) {
374 curr_size = r8712_efuse_get_current_size(adapter);
375 if ((curr_size + 1 + target_word_cnts * 2) >
376 efuse_available_max_size)
377 return false; /*target_word_cnts + pg header(1 byte)*/
378 efuse_addr = curr_size; /* current size is also the last addr*/
379 efuse_one_byte_write(adapter, efuse_addr, pg_header); /*hdr*/
381 /* check if what we read is what we write */
382 while (!efuse_one_byte_read(adapter, efuse_addr,
384 if (++sub_repeat > _REPEAT_THRESHOLD_) {
385 bResult = false; /* continue to blind write */
386 break; /* continue to blind write */
389 if ((sub_repeat > _REPEAT_THRESHOLD_) ||
390 (pg_header == efuse_data)) {
391 /* write header ok OR can't check header(creep) */
394 /* go to next address */
396 for (i = 0; i < target_word_cnts * 2; i++) {
397 efuse_one_byte_write(adapter,
400 if (!efuse_one_byte_read(adapter,
404 else if (*(data + i) != efuse_data) /* fail */
409 /* write header fail */
411 if (efuse_data == 0xFF)
412 return bResult; /* nothing damaged. */
413 /* call rescue procedure */
414 if (!fix_header(adapter, efuse_data, efuse_addr))
415 return false; /* rescue fail */
417 if (++repeat_times > _REPEAT_THRESHOLD_) /* fail */
419 /* otherwise, take another risk... */
424 u8 r8712_efuse_access(struct _adapter *adapter, u8 bRead, u16 start_addr,
430 if (start_addr > EFUSE_MAX_SIZE)
432 if (!bRead && ((start_addr + cnts) >
433 efuse_available_max_size))
435 if (!bRead && !r8712_efuse_reg_init(adapter))
437 /* -----------------e-fuse one byte read / write ---------------------*/
438 for (i = 0; i < cnts; i++) {
439 if ((start_addr + i) > EFUSE_MAX_SIZE) {
443 res = efuse_one_byte_rw(adapter, bRead, start_addr + i,
449 r8712_efuse_reg_uninit(adapter);
453 u8 r8712_efuse_map_read(struct _adapter *adapter, u16 addr, u16 cnts, u8 *data)
455 u8 offset, ret = true;
456 u8 pktdata[PGPKT_DATA_SIZE];
459 if ((addr + cnts) > EFUSE_MAP_MAX_SIZE)
461 if (efuse_is_empty(adapter, &offset) && offset) {
462 for (i = 0; i < cnts; i++)
466 offset = (addr >> 3) & 0xF;
467 ret = r8712_efuse_pg_packet_read(adapter, offset, pktdata);
468 i = addr & 0x7; /* pktdata index */
469 idx = 0; /* data index */
472 for (; i < PGPKT_DATA_SIZE; i++) {
473 data[idx++] = pktdata[i];
478 if (!r8712_efuse_pg_packet_read(adapter, offset, pktdata))
485 u8 r8712_efuse_map_write(struct _adapter *adapter, u16 addr, u16 cnts,
488 u8 offset, word_en, empty;
489 u8 pktdata[PGPKT_DATA_SIZE], newdata[PGPKT_DATA_SIZE];
492 if ((addr + cnts) > EFUSE_MAP_MAX_SIZE)
494 /* check if E-Fuse Clock Enable and E-Fuse Clock is 40M */
495 empty = r8712_read8(adapter, EFUSE_CLK_CTRL);
498 if (efuse_is_empty(adapter, &empty)) {
500 memset(pktdata, 0xFF, PGPKT_DATA_SIZE);
504 offset = (addr >> 3) & 0xF;
506 if (!r8712_efuse_pg_packet_read(adapter, offset, pktdata))
509 memset(newdata, 0xFF, PGPKT_DATA_SIZE);
510 i = addr & 0x7; /* pktdata index */
511 j = 0; /* newdata index */
512 idx = 0; /* data index */
516 if (data[idx] != pktdata[i]) {
517 word_en &= ~BIT(i >> 1);
518 newdata[j++] = pktdata[i - 1];
519 newdata[j++] = data[idx];
525 for (; i < PGPKT_DATA_SIZE; i += 2) {
526 if ((cnts - idx) == 1) {
527 if (data[idx] != pktdata[i]) {
528 word_en &= ~BIT(i >> 1);
529 newdata[j++] = data[idx];
530 newdata[j++] = pktdata[1 + 1];
536 if ((data[idx] != pktdata[i]) || (data[idx + 1] !=
538 word_en &= ~BIT(i >> 1);
539 newdata[j++] = data[idx];
540 newdata[j++] = data[idx + 1];
549 if (!r8712_efuse_pg_packet_write(adapter, offset,
556 if (!r8712_efuse_pg_packet_read(adapter, offset,
562 memset(newdata, 0xFF, PGPKT_DATA_SIZE);