1
2/*
3 * Radio tuning for GCT GRF5101 on RTL8180
4 *
5 * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
6 *
7 * Code from the BSD driver and the rtl8181 project have been
8 * very useful to understand certain things
9 *
10 * I want to thanks the Authors of such projects and the Ndiswrapper
11 * project Authors.
12 *
13 * A special Big Thanks also is for all people who donated me cards,
14 * making possible the creation of the original rtl8180 driver
15 * from which this code is derived!
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <linux/pci.h>
23#include <linux/delay.h>
24#include <net/mac80211.h>
25
26#include "rtl8180.h"
27#include "grf5101.h"
28
29static const int grf5101_encode[] = {
30	0x0, 0x8, 0x4, 0xC,
31	0x2, 0xA, 0x6, 0xE,
32	0x1, 0x9, 0x5, 0xD,
33	0x3, 0xB, 0x7, 0xF
34};
35
36static void write_grf5101(struct ieee80211_hw *dev, u8 addr, u32 data)
37{
38	struct rtl8180_priv *priv = dev->priv;
39	u32 phy_config;
40
41	phy_config =  grf5101_encode[(data >> 8) & 0xF];
42	phy_config |= grf5101_encode[(data >> 4) & 0xF] << 4;
43	phy_config |= grf5101_encode[data & 0xF] << 8;
44	phy_config |= grf5101_encode[(addr >> 1) & 0xF] << 12;
45	phy_config |= (addr & 1) << 16;
46	phy_config |= grf5101_encode[(data & 0xf000) >> 12] << 24;
47
48	/* MAC will bang bits to the chip */
49	phy_config |= 0x90000000;
50
51	rtl818x_iowrite32(priv,
52		(__le32 __iomem *) &priv->map->RFPinsOutput, phy_config);
53
54	msleep(3);
55}
56
57static void grf5101_write_phy_antenna(struct ieee80211_hw *dev, short chan)
58{
59	struct rtl8180_priv *priv = dev->priv;
60	u8 ant = GRF5101_ANTENNA;
61
62	if (priv->rfparam & RF_PARAM_ANTBDEFAULT)
63		ant |= BB_ANTENNA_B;
64
65	if (chan == 14)
66		ant |= BB_ANTATTEN_CHAN14;
67
68	rtl8180_write_phy(dev, 0x10, ant);
69}
70
71static u8 grf5101_rf_calc_rssi(u8 agc, u8 sq)
72{
73	if (agc > 60)
74		return 65;
75
76	/* TODO(?): just return agc (or agc + 5) to avoid mult / div */
77	return 65 * agc / 60;
78}
79
80static void grf5101_rf_set_channel(struct ieee80211_hw *dev,
81				   struct ieee80211_conf *conf)
82{
83	struct rtl8180_priv *priv = dev->priv;
84	int channel =
85		ieee80211_frequency_to_channel(conf->chandef.chan->center_freq);
86	u32 txpw = priv->channels[channel - 1].hw_value & 0xFF;
87	u32 chan = channel - 1;
88
89	/* set TX power */
90	write_grf5101(dev, 0x15, 0x0);
91	write_grf5101(dev, 0x06, txpw);
92	write_grf5101(dev, 0x15, 0x10);
93	write_grf5101(dev, 0x15, 0x0);
94
95	/* set frequency */
96	write_grf5101(dev, 0x07, 0x0);
97	write_grf5101(dev, 0x0B, chan);
98	write_grf5101(dev, 0x07, 0x1000);
99
100	grf5101_write_phy_antenna(dev, channel);
101}
102
103static void grf5101_rf_stop(struct ieee80211_hw *dev)
104{
105	struct rtl8180_priv *priv = dev->priv;
106	u32 anaparam;
107
108	anaparam = priv->anaparam;
109	anaparam &= 0x000fffff;
110	anaparam |= 0x3f900000;
111	rtl8180_set_anaparam(priv, anaparam);
112
113	write_grf5101(dev, 0x07, 0x0);
114	write_grf5101(dev, 0x1f, 0x45);
115	write_grf5101(dev, 0x1f, 0x5);
116	write_grf5101(dev, 0x00, 0x8e4);
117}
118
119static void grf5101_rf_init(struct ieee80211_hw *dev)
120{
121	struct rtl8180_priv *priv = dev->priv;
122
123	rtl8180_set_anaparam(priv, priv->anaparam);
124
125	write_grf5101(dev, 0x1f, 0x0);
126	write_grf5101(dev, 0x1f, 0x0);
127	write_grf5101(dev, 0x1f, 0x40);
128	write_grf5101(dev, 0x1f, 0x60);
129	write_grf5101(dev, 0x1f, 0x61);
130	write_grf5101(dev, 0x1f, 0x61);
131	write_grf5101(dev, 0x00, 0xae4);
132	write_grf5101(dev, 0x1f, 0x1);
133	write_grf5101(dev, 0x1f, 0x41);
134	write_grf5101(dev, 0x1f, 0x61);
135
136	write_grf5101(dev, 0x01, 0x1a23);
137	write_grf5101(dev, 0x02, 0x4971);
138	write_grf5101(dev, 0x03, 0x41de);
139	write_grf5101(dev, 0x04, 0x2d80);
140	write_grf5101(dev, 0x05, 0x68ff);	/* 0x61ff original value */
141	write_grf5101(dev, 0x06, 0x0);
142	write_grf5101(dev, 0x07, 0x0);
143	write_grf5101(dev, 0x08, 0x7533);
144	write_grf5101(dev, 0x09, 0xc401);
145	write_grf5101(dev, 0x0a, 0x0);
146	write_grf5101(dev, 0x0c, 0x1c7);
147	write_grf5101(dev, 0x0d, 0x29d3);
148	write_grf5101(dev, 0x0e, 0x2e8);
149	write_grf5101(dev, 0x10, 0x192);
150	write_grf5101(dev, 0x11, 0x248);
151	write_grf5101(dev, 0x12, 0x0);
152	write_grf5101(dev, 0x13, 0x20c4);
153	write_grf5101(dev, 0x14, 0xf4fc);
154	write_grf5101(dev, 0x15, 0x0);
155	write_grf5101(dev, 0x16, 0x1500);
156
157	write_grf5101(dev, 0x07, 0x1000);
158
159	/* baseband configuration */
160	rtl8180_write_phy(dev, 0, 0xa8);
161	rtl8180_write_phy(dev, 3, 0x0);
162	rtl8180_write_phy(dev, 4, 0xc0);
163	rtl8180_write_phy(dev, 5, 0x90);
164	rtl8180_write_phy(dev, 6, 0x1e);
165	rtl8180_write_phy(dev, 7, 0x64);
166
167	grf5101_write_phy_antenna(dev, 1);
168
169	rtl8180_write_phy(dev, 0x11, 0x88);
170
171	if (rtl818x_ioread8(priv, &priv->map->CONFIG2) &
172	    RTL818X_CONFIG2_ANTENNA_DIV)
173		rtl8180_write_phy(dev, 0x12, 0xc0); /* enable ant diversity */
174	else
175		rtl8180_write_phy(dev, 0x12, 0x40); /* disable ant diversity */
176
177	rtl8180_write_phy(dev, 0x13, 0x90 | priv->csthreshold);
178
179	rtl8180_write_phy(dev, 0x19, 0x0);
180	rtl8180_write_phy(dev, 0x1a, 0xa0);
181	rtl8180_write_phy(dev, 0x1b, 0x44);
182}
183
184const struct rtl818x_rf_ops grf5101_rf_ops = {
185	.name		= "GCT",
186	.init		= grf5101_rf_init,
187	.stop		= grf5101_rf_stop,
188	.set_chan	= grf5101_rf_set_channel,
189	.calc_rssi	= grf5101_rf_calc_rssi,
190};
191