blob: c1ccff4a8321088d5ced6e563b227f79547eefc1 [file] [log] [blame]
Larry Finger94a79942011-08-23 19:00:42 -05001/******************************************************************************
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 <andreamrl@tiscali.it>, 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
Larry Finger49aab5f2011-08-25 14:07:05 -050028static void eprom_cs(struct net_device *dev, short bit)
Larry Finger94a79942011-08-23 19:00:42 -050029{
30 if (bit)
31 write_nic_byte(dev, EPROM_CMD,
Larry Fingera44325f2011-08-25 11:48:23 -050032 (1 << EPROM_CS_SHIFT) |
Larry Finger94a79942011-08-23 19:00:42 -050033 read_nic_byte(dev, EPROM_CMD));
34 else
Larry Fingera44325f2011-08-25 11:48:23 -050035 write_nic_byte(dev, EPROM_CMD, read_nic_byte(dev, EPROM_CMD)
36 & ~(1<<EPROM_CS_SHIFT));
Larry Finger94a79942011-08-23 19:00:42 -050037
38 udelay(EPROM_DELAY);
39}
40
41
Larry Finger49aab5f2011-08-25 14:07:05 -050042static void eprom_ck_cycle(struct net_device *dev)
Larry Finger94a79942011-08-23 19:00:42 -050043{
44 write_nic_byte(dev, EPROM_CMD,
Larry Fingera44325f2011-08-25 11:48:23 -050045 (1<<EPROM_CK_SHIFT) | read_nic_byte(dev, EPROM_CMD));
Larry Finger94a79942011-08-23 19:00:42 -050046 udelay(EPROM_DELAY);
47 write_nic_byte(dev, EPROM_CMD,
Larry Fingera44325f2011-08-25 11:48:23 -050048 read_nic_byte(dev, EPROM_CMD) & ~(1<<EPROM_CK_SHIFT));
Larry Finger94a79942011-08-23 19:00:42 -050049 udelay(EPROM_DELAY);
50}
51
52
Larry Finger49aab5f2011-08-25 14:07:05 -050053static void eprom_w(struct net_device *dev, short bit)
Larry Finger94a79942011-08-23 19:00:42 -050054{
55 if (bit)
Larry Fingera44325f2011-08-25 11:48:23 -050056 write_nic_byte(dev, EPROM_CMD, (1<<EPROM_W_SHIFT) |
57 read_nic_byte(dev, EPROM_CMD));
Larry Finger94a79942011-08-23 19:00:42 -050058 else
Larry Fingera44325f2011-08-25 11:48:23 -050059 write_nic_byte(dev, EPROM_CMD, read_nic_byte(dev, EPROM_CMD)
60 & ~(1<<EPROM_W_SHIFT));
Larry Finger94a79942011-08-23 19:00:42 -050061
62 udelay(EPROM_DELAY);
63}
64
65
Larry Finger49aab5f2011-08-25 14:07:05 -050066static short eprom_r(struct net_device *dev)
Larry Finger94a79942011-08-23 19:00:42 -050067{
68 short bit;
69
Larry Fingera44325f2011-08-25 11:48:23 -050070 bit = (read_nic_byte(dev, EPROM_CMD) & (1<<EPROM_R_SHIFT));
Larry Finger94a79942011-08-23 19:00:42 -050071 udelay(EPROM_DELAY);
72
73 if (bit)
74 return 1;
75 return 0;
76}
77
Larry Finger49aab5f2011-08-25 14:07:05 -050078static void eprom_send_bits_string(struct net_device *dev, short b[], int len)
Larry Finger94a79942011-08-23 19:00:42 -050079{
80 int i;
81
Larry Fingera44325f2011-08-25 11:48:23 -050082 for (i = 0; i < len; i++) {
Larry Finger94a79942011-08-23 19:00:42 -050083 eprom_w(dev, b[i]);
84 eprom_ck_cycle(dev);
85 }
86}
87
Larry Finger94a79942011-08-23 19:00:42 -050088u32 eprom_read(struct net_device *dev, u32 addr)
89{
90 struct r8192_priv *priv = rtllib_priv(dev);
Larry Fingera44325f2011-08-25 11:48:23 -050091 short read_cmd[] = {1, 1, 0};
Larry Finger94a79942011-08-23 19:00:42 -050092 short addr_str[8];
93 int i;
94 int addr_len;
95 u32 ret;
96
Larry Fingera44325f2011-08-25 11:48:23 -050097 ret = 0;
Larry Finger94a79942011-08-23 19:00:42 -050098 write_nic_byte(dev, EPROM_CMD,
Larry Fingera44325f2011-08-25 11:48:23 -050099 (EPROM_CMD_PROGRAM << EPROM_CMD_OPERATING_MODE_SHIFT));
Larry Finger94a79942011-08-23 19:00:42 -0500100 udelay(EPROM_DELAY);
101
Larry Fingera44325f2011-08-25 11:48:23 -0500102 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;
Larry Finger94a79942011-08-23 19:00:42 -0500120 }
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
Larry Fingera44325f2011-08-25 11:48:23 -0500128 for (i = 0; i < 16; i++) {
Larry Finger94a79942011-08-23 19:00:42 -0500129 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}