realtek-rtl88xxau/core/rtw_eeprom.c

370 lines
8.1 KiB
C
Raw Permalink Normal View History

2017-04-05 03:06:43 -07:00
/******************************************************************************
*
2018-08-21 05:51:37 -07:00
* Copyright(c) 2007 - 2017 Realtek Corporation.
2017-08-28 07:11:31 -07:00
*
2017-04-05 03:06:43 -07:00
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
2018-08-21 05:51:37 -07:00
*****************************************************************************/
2017-04-05 03:06:43 -07:00
#define _RTW_EEPROM_C_
#include <drv_conf.h>
#include <osdep_service.h>
#include <drv_types.h>
2017-08-28 07:11:31 -07:00
void up_clk(_adapter *padapter, u16 *x)
2017-04-05 03:06:43 -07:00
{
*x = *x | _EESK;
rtw_write8(padapter, EE_9346CR, (u8)*x);
rtw_udelay_os(CLOCK_RATE);
2017-08-28 07:11:31 -07:00
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
void down_clk(_adapter *padapter, u16 *x)
2017-04-05 03:06:43 -07:00
{
*x = *x & ~_EESK;
rtw_write8(padapter, EE_9346CR, (u8)*x);
rtw_udelay_os(CLOCK_RATE);
}
2017-08-28 07:11:31 -07:00
void shift_out_bits(_adapter *padapter, u16 data, u16 count)
2017-04-05 03:06:43 -07:00
{
2017-08-28 07:11:31 -07:00
u16 x, mask;
2017-04-05 03:06:43 -07:00
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
mask = 0x01 << (count - 1);
x = rtw_read8(padapter, EE_9346CR);
x &= ~(_EEDO | _EEDI);
2017-08-28 07:11:31 -07:00
do {
2017-04-05 03:06:43 -07:00
x &= ~_EEDI;
2017-08-28 07:11:31 -07:00
if (data & mask)
2017-04-05 03:06:43 -07:00
x |= _EEDI;
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
rtw_write8(padapter, EE_9346CR, (u8)x);
rtw_udelay_os(CLOCK_RATE);
up_clk(padapter, &x);
down_clk(padapter, &x);
mask = mask >> 1;
2017-08-28 07:11:31 -07:00
} while (mask);
2017-04-05 03:06:43 -07:00
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x &= ~_EEDI;
rtw_write8(padapter, EE_9346CR, (u8)x);
2017-08-28 07:11:31 -07:00
out:
return;
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
u16 shift_in_bits(_adapter *padapter)
2017-04-05 03:06:43 -07:00
{
2017-08-28 07:11:31 -07:00
u16 x, d = 0, i;
2017-04-05 03:06:43 -07:00
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x = rtw_read8(padapter, EE_9346CR);
2017-08-28 07:11:31 -07:00
x &= ~(_EEDO | _EEDI);
2017-04-05 03:06:43 -07:00
d = 0;
2017-08-28 07:11:31 -07:00
for (i = 0; i < 16; i++) {
2017-04-05 03:06:43 -07:00
d = d << 1;
up_clk(padapter, &x);
2017-08-28 07:11:31 -07:00
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
2017-04-05 03:06:43 -07:00
x = rtw_read8(padapter, EE_9346CR);
x &= ~(_EEDI);
2017-08-28 07:11:31 -07:00
if (x & _EEDO)
d |= 1;
2017-04-05 03:06:43 -07:00
down_clk(padapter, &x);
}
2017-08-28 07:11:31 -07:00
out:
2017-04-05 03:06:43 -07:00
return d;
}
2017-08-28 07:11:31 -07:00
void standby(_adapter *padapter)
2017-04-05 03:06:43 -07:00
{
u8 x;
x = rtw_read8(padapter, EE_9346CR);
x &= ~(_EECS | _EESK);
2017-08-28 07:11:31 -07:00
rtw_write8(padapter, EE_9346CR, x);
2017-04-05 03:06:43 -07:00
rtw_udelay_os(CLOCK_RATE);
x |= _EECS;
rtw_write8(padapter, EE_9346CR, x);
rtw_udelay_os(CLOCK_RATE);
}
2017-08-28 07:11:31 -07:00
u16 wait_eeprom_cmd_done(_adapter *padapter)
2017-04-05 03:06:43 -07:00
{
2017-08-28 07:11:31 -07:00
u8 x;
u16 i, res = _FALSE;
standby(padapter);
for (i = 0; i < 200; i++) {
2017-04-05 03:06:43 -07:00
x = rtw_read8(padapter, EE_9346CR);
2017-08-28 07:11:31 -07:00
if (x & _EEDO) {
res = _TRUE;
2017-04-05 03:06:43 -07:00
goto exit;
2017-08-28 07:11:31 -07:00
}
2017-04-05 03:06:43 -07:00
rtw_udelay_os(CLOCK_RATE);
}
2017-08-28 07:11:31 -07:00
exit:
2017-04-05 03:06:43 -07:00
return res;
}
2017-08-28 07:11:31 -07:00
void eeprom_clean(_adapter *padapter)
2017-04-05 03:06:43 -07:00
{
u16 x;
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x = rtw_read8(padapter, EE_9346CR);
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x &= ~(_EECS | _EEDI);
rtw_write8(padapter, EE_9346CR, (u8)x);
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
up_clk(padapter, &x);
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
down_clk(padapter, &x);
2017-08-28 07:11:31 -07:00
out:
return;
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
void eeprom_write16(_adapter *padapter, u16 reg, u16 data)
2017-04-05 03:06:43 -07:00
{
u8 x;
#ifdef CONFIG_RTL8712
2017-08-28 07:11:31 -07:00
u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
tmp8_ori = rtw_read8(padapter, 0x102502f1);
tmp8_new = tmp8_ori & 0xf7;
if (tmp8_ori != tmp8_new) {
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x102502f1, tmp8_new);
}
2017-08-28 07:11:31 -07:00
tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
tmp8_clk_new = tmp8_clk_ori | 0x20;
if (tmp8_clk_new != tmp8_clk_ori) {
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x10250003, tmp8_clk_new);
2017-08-28 07:11:31 -07:00
}
2017-04-05 03:06:43 -07:00
#endif
2017-08-28 07:11:31 -07:00
2017-04-05 03:06:43 -07:00
x = rtw_read8(padapter, EE_9346CR);
x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
x |= _EEM1 | _EECS;
rtw_write8(padapter, EE_9346CR, x);
shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
2017-08-28 07:11:31 -07:00
if (padapter->EepromAddressSize == 8) /* CF+ and SDIO */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, 0, 6);
2017-08-28 07:11:31 -07:00
else /* USB */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, 0, 4);
2017-08-28 07:11:31 -07:00
standby(padapter);
/* Commented out by rcnjko, 2004.0
* Erase this particular word. Write the erase opcode and register
* number in that order. The opcode is 3bits in length; reg is 6 bits long. */
/* shift_out_bits(Adapter, EEPROM_ERASE_OPCODE, 3);
* shift_out_bits(Adapter, reg, Adapter->EepromAddressSize);
*
* if (wait_eeprom_cmd_done(Adapter ) == FALSE)
* {
* return;
* } */
2017-04-05 03:06:43 -07:00
2017-08-28 07:11:31 -07:00
standby(padapter);
2017-04-05 03:06:43 -07:00
2017-08-28 07:11:31 -07:00
/* write the new word to the EEPROM */
2017-04-05 03:06:43 -07:00
2017-08-28 07:11:31 -07:00
/* send the write opcode the EEPORM */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
2017-08-28 07:11:31 -07:00
/* select which word in the EEPROM that we are writing to. */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, reg, padapter->EepromAddressSize);
2017-08-28 07:11:31 -07:00
/* write the data to the selected EEPROM word. */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, data, 16);
2017-08-28 07:11:31 -07:00
if (wait_eeprom_cmd_done(padapter) == _FALSE)
2017-04-05 03:06:43 -07:00
goto exit;
2017-08-28 07:11:31 -07:00
standby(padapter);
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
shift_out_bits(padapter, reg, 4);
2017-08-28 07:11:31 -07:00
eeprom_clean(padapter);
exit:
2017-04-05 03:06:43 -07:00
#ifdef CONFIG_RTL8712
2017-08-28 07:11:31 -07:00
if (tmp8_clk_new != tmp8_clk_ori)
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
2017-08-28 07:11:31 -07:00
if (tmp8_new != tmp8_ori)
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x102502f1, tmp8_ori);
#endif
return;
}
2017-08-28 07:11:31 -07:00
u16 eeprom_read16(_adapter *padapter, u16 reg) /* ReadEEprom */
2017-04-05 03:06:43 -07:00
{
u16 x;
2017-08-28 07:11:31 -07:00
u16 data = 0;
2017-04-05 03:06:43 -07:00
#ifdef CONFIG_RTL8712
2017-08-28 07:11:31 -07:00
u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
tmp8_ori = rtw_read8(padapter, 0x102502f1);
2017-04-05 03:06:43 -07:00
tmp8_new = tmp8_ori & 0xf7;
2017-08-28 07:11:31 -07:00
if (tmp8_ori != tmp8_new) {
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x102502f1, tmp8_new);
}
2017-08-28 07:11:31 -07:00
tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
tmp8_clk_new = tmp8_clk_ori | 0x20;
if (tmp8_clk_new != tmp8_clk_ori) {
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x10250003, tmp8_clk_new);
2017-08-28 07:11:31 -07:00
}
2017-04-05 03:06:43 -07:00
#endif
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
2017-08-28 07:11:31 -07:00
/* select EEPROM, reset bits, set _EECS */
2017-04-05 03:06:43 -07:00
x = rtw_read8(padapter, EE_9346CR);
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
x |= _EEM1 | _EECS;
rtw_write8(padapter, EE_9346CR, (unsigned char)x);
2017-08-28 07:11:31 -07:00
/* write the read opcode and register number in that order */
/* The opcode is 3bits in length, reg is 6 bits long */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
shift_out_bits(padapter, reg, padapter->EepromAddressSize);
2017-08-28 07:11:31 -07:00
/* Now read the data (16 bits) in from the selected EEPROM word */
2017-04-05 03:06:43 -07:00
data = shift_in_bits(padapter);
eeprom_clean(padapter);
2017-08-28 07:11:31 -07:00
out:
2017-04-05 03:06:43 -07:00
#ifdef CONFIG_RTL8712
2017-08-28 07:11:31 -07:00
if (tmp8_clk_new != tmp8_clk_ori)
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
2017-08-28 07:11:31 -07:00
if (tmp8_new != tmp8_ori)
2017-04-05 03:06:43 -07:00
rtw_write8(padapter, 0x102502f1, tmp8_ori);
#endif
return data;
}
2017-08-28 07:11:31 -07:00
/* From even offset */
void eeprom_read_sz(_adapter *padapter, u16 reg, u8 *data, u32 sz)
2017-04-05 03:06:43 -07:00
{
u16 x, data16;
u32 i;
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
2017-08-28 07:11:31 -07:00
/* select EEPROM, reset bits, set _EECS */
2017-04-05 03:06:43 -07:00
x = rtw_read8(padapter, EE_9346CR);
if (rtw_is_surprise_removed(padapter)) {
goto out;
}
x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
x |= _EEM1 | _EECS;
rtw_write8(padapter, EE_9346CR, (unsigned char)x);
2017-08-28 07:11:31 -07:00
/* write the read opcode and register number in that order */
/* The opcode is 3bits in length, reg is 6 bits long */
2017-04-05 03:06:43 -07:00
shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
shift_out_bits(padapter, reg, padapter->EepromAddressSize);
2017-08-28 07:11:31 -07:00
for (i = 0; i < sz; i += 2) {
2017-04-05 03:06:43 -07:00
data16 = shift_in_bits(padapter);
data[i] = data16 & 0xff;
2017-08-28 07:11:31 -07:00
data[i + 1] = data16 >> 8;
2017-04-05 03:06:43 -07:00
}
eeprom_clean(padapter);
2017-08-28 07:11:31 -07:00
out:
return;
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
/* addr_off : address offset of the entry in eeprom (not the tuple number of eeprom (reg); that is addr_off !=reg) */
u8 eeprom_read(_adapter *padapter, u32 addr_off, u8 sz, u8 *rbuf)
2017-04-05 03:06:43 -07:00
{
u8 quotient, remainder, addr_2align_odd;
2017-08-28 07:11:31 -07:00
u16 reg, stmp , i = 0, idx = 0;
2017-04-05 03:06:43 -07:00
reg = (u16)(addr_off >> 1);
addr_2align_odd = (u8)(addr_off & 0x1);
2017-08-28 07:11:31 -07:00
if (addr_2align_odd) { /* read that start at high part: e.g 1,3,5,7,9,... */
2017-04-05 03:06:43 -07:00
stmp = eeprom_read16(padapter, reg);
2017-08-28 07:11:31 -07:00
rbuf[idx++] = (u8)((stmp >> 8) & 0xff); /* return hogh-part of the short */
reg++;
sz--;
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
2017-04-05 03:06:43 -07:00
quotient = sz >> 1;
remainder = sz & 0x1;
2017-08-28 07:11:31 -07:00
for (i = 0 ; i < quotient; i++) {
stmp = eeprom_read16(padapter, reg + i);
rbuf[idx++] = (u8)(stmp & 0xff);
rbuf[idx++] = (u8)((stmp >> 8) & 0xff);
2017-04-05 03:06:43 -07:00
}
2017-08-28 07:11:31 -07:00
reg = reg + i;
if (remainder) { /* end of read at lower part of short : 0,2,4,6,... */
2017-04-05 03:06:43 -07:00
stmp = eeprom_read16(padapter, reg);
2017-08-28 07:11:31 -07:00
rbuf[idx] = (u8)(stmp & 0xff);
2017-04-05 03:06:43 -07:00
}
return _TRUE;
}
2017-08-28 07:11:31 -07:00
VOID read_eeprom_content(_adapter *padapter)
2017-04-05 03:06:43 -07:00
{
}