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 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 *
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
21 *
22 * Contact Information:
23 * wlanfae <wlanfae@realtek.com>
24******************************************************************************/
25#include "rtl_core.h"
26#include "rtl_eeprom.h"
27
28static void eprom_cs(struct net_device *dev, short bit)
29{
30	if (bit)
31		write_nic_byte(dev, EPROM_CMD,
32			       (1 << EPROM_CS_SHIFT) |
33			       read_nic_byte(dev, EPROM_CMD));
34	else
35		write_nic_byte(dev, EPROM_CMD, read_nic_byte(dev, EPROM_CMD)
36			       & ~(1<<EPROM_CS_SHIFT));
37
38	udelay(EPROM_DELAY);
39}
40
41
42static void eprom_ck_cycle(struct net_device *dev)
43{
44	write_nic_byte(dev, EPROM_CMD,
45		       (1<<EPROM_CK_SHIFT) | read_nic_byte(dev, EPROM_CMD));
46	udelay(EPROM_DELAY);
47	write_nic_byte(dev, EPROM_CMD,
48		       read_nic_byte(dev, EPROM_CMD) & ~(1<<EPROM_CK_SHIFT));
49	udelay(EPROM_DELAY);
50}
51
52
53static void eprom_w(struct net_device *dev, short bit)
54{
55	if (bit)
56		write_nic_byte(dev, EPROM_CMD, (1<<EPROM_W_SHIFT) |
57			       read_nic_byte(dev, EPROM_CMD));
58	else
59		write_nic_byte(dev, EPROM_CMD, read_nic_byte(dev, EPROM_CMD)
60			       & ~(1<<EPROM_W_SHIFT));
61
62	udelay(EPROM_DELAY);
63}
64
65
66static short eprom_r(struct net_device *dev)
67{
68	short bit;
69
70	bit = (read_nic_byte(dev, EPROM_CMD) & (1<<EPROM_R_SHIFT));
71	udelay(EPROM_DELAY);
72
73	if (bit)
74		return 1;
75	return 0;
76}
77
78static void eprom_send_bits_string(struct net_device *dev, short b[], int len)
79{
80	int i;
81
82	for (i = 0; i < len; i++) {
83		eprom_w(dev, b[i]);
84		eprom_ck_cycle(dev);
85	}
86}
87
88u32 eprom_read(struct net_device *dev, u32 addr)
89{
90	struct r8192_priv *priv = rtllib_priv(dev);
91	short read_cmd[] = {1, 1, 0};
92	short addr_str[8];
93	int i;
94	int addr_len;
95	u32 ret;
96
97	ret = 0;
98	write_nic_byte(dev, EPROM_CMD,
99		       (EPROM_CMD_PROGRAM << EPROM_CMD_OPERATING_MODE_SHIFT));
100	udelay(EPROM_DELAY);
101
102	if (priv->epromtype == EEPROM_93C56) {
103		addr_str[7] = addr & 1;
104		addr_str[6] = addr & (1<<1);
105		addr_str[5] = addr & (1<<2);
106		addr_str[4] = addr & (1<<3);
107		addr_str[3] = addr & (1<<4);
108		addr_str[2] = addr & (1<<5);
109		addr_str[1] = addr & (1<<6);
110		addr_str[0] = addr & (1<<7);
111		addr_len = 8;
112	} else {
113		addr_str[5] = addr & 1;
114		addr_str[4] = addr & (1<<1);
115		addr_str[3] = addr & (1<<2);
116		addr_str[2] = addr & (1<<3);
117		addr_str[1] = addr & (1<<4);
118		addr_str[0] = addr & (1<<5);
119		addr_len = 6;
120	}
121	eprom_cs(dev, 1);
122	eprom_ck_cycle(dev);
123	eprom_send_bits_string(dev, read_cmd, 3);
124	eprom_send_bits_string(dev, addr_str, addr_len);
125
126	eprom_w(dev, 0);
127
128	for (i = 0; i < 16; i++) {
129		eprom_ck_cycle(dev);
130		ret |= (eprom_r(dev)<<(15-i));
131	}
132
133	eprom_cs(dev, 0);
134	eprom_ck_cycle(dev);
135
136	write_nic_byte(dev, EPROM_CMD,
137		       (EPROM_CMD_NORMAL<<EPROM_CMD_OPERATING_MODE_SHIFT));
138	return ret;
139}
140