1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * The full GNU General Public License is included in this distribution in the
16  * file called LICENSE.
17  *
18  * Contact Information:
19  * wlanfae <wlanfae@realtek.com>
20 ******************************************************************************/
21 #include "rtl_core.h"
22 #include "rtl_eeprom.h"
23 
_rtl92e_gpio_write_bit(struct net_device * dev,int no,bool val)24 static void _rtl92e_gpio_write_bit(struct net_device *dev, int no, bool val)
25 {
26 	u8 reg = rtl92e_readb(dev, EPROM_CMD);
27 
28 	if (val)
29 		reg |= 1 << no;
30 	else
31 		reg &= ~(1 << no);
32 
33 	rtl92e_writeb(dev, EPROM_CMD, reg);
34 	udelay(EPROM_DELAY);
35 }
36 
_rtl92e_gpio_get_bit(struct net_device * dev,int no)37 static bool _rtl92e_gpio_get_bit(struct net_device *dev, int no)
38 {
39 	u8 reg = rtl92e_readb(dev, EPROM_CMD);
40 
41 	return (reg >> no) & 0x1;
42 }
43 
_rtl92e_eeprom_ck_cycle(struct net_device * dev)44 static void _rtl92e_eeprom_ck_cycle(struct net_device *dev)
45 {
46 	_rtl92e_gpio_write_bit(dev, EPROM_CK_BIT, 1);
47 	_rtl92e_gpio_write_bit(dev, EPROM_CK_BIT, 0);
48 }
49 
_rtl92e_eeprom_xfer(struct net_device * dev,u16 data,int tx_len)50 static u16 _rtl92e_eeprom_xfer(struct net_device *dev, u16 data, int tx_len)
51 {
52 	u16 ret = 0;
53 	int rx_len = 16;
54 
55 	_rtl92e_gpio_write_bit(dev, EPROM_CS_BIT, 1);
56 	_rtl92e_eeprom_ck_cycle(dev);
57 
58 	while (tx_len--) {
59 		_rtl92e_gpio_write_bit(dev, EPROM_W_BIT,
60 				       (data >> tx_len) & 0x1);
61 		_rtl92e_eeprom_ck_cycle(dev);
62 	}
63 
64 	_rtl92e_gpio_write_bit(dev, EPROM_W_BIT, 0);
65 
66 	while (rx_len--) {
67 		_rtl92e_eeprom_ck_cycle(dev);
68 		ret |= _rtl92e_gpio_get_bit(dev, EPROM_R_BIT) << rx_len;
69 	}
70 
71 	_rtl92e_gpio_write_bit(dev, EPROM_CS_BIT, 0);
72 	_rtl92e_eeprom_ck_cycle(dev);
73 
74 	return ret;
75 }
76 
rtl92e_eeprom_read(struct net_device * dev,u32 addr)77 u32 rtl92e_eeprom_read(struct net_device *dev, u32 addr)
78 {
79 	struct r8192_priv *priv = rtllib_priv(dev);
80 	u32 ret = 0;
81 
82 	rtl92e_writeb(dev, EPROM_CMD,
83 		      (EPROM_CMD_PROGRAM << EPROM_CMD_OPERATING_MODE_SHIFT));
84 	udelay(EPROM_DELAY);
85 
86 	/* EEPROM is configured as x16 */
87 	if (priv->epromtype == EEPROM_93C56)
88 		ret = _rtl92e_eeprom_xfer(dev, (addr & 0xFF) | (0x6 << 8), 11);
89 	else
90 		ret = _rtl92e_eeprom_xfer(dev, (addr & 0x3F) | (0x6 << 6), 9);
91 
92 	rtl92e_writeb(dev, EPROM_CMD,
93 		      (EPROM_CMD_NORMAL<<EPROM_CMD_OPERATING_MODE_SHIFT));
94 	return ret;
95 }
96