18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * Radio tuning for GCT GRF5101 on RTL8180
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Code from the BSD driver and the rtl8181 project have been
98c2ecf20Sopenharmony_ci * very useful to understand certain things
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * I want to thanks the Authors of such projects and the Ndiswrapper
128c2ecf20Sopenharmony_ci * project Authors.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * A special Big Thanks also is for all people who donated me cards,
158c2ecf20Sopenharmony_ci * making possible the creation of the original rtl8180 driver
168c2ecf20Sopenharmony_ci * from which this code is derived!
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/pci.h>
208c2ecf20Sopenharmony_ci#include <linux/delay.h>
218c2ecf20Sopenharmony_ci#include <net/mac80211.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "rtl8180.h"
248c2ecf20Sopenharmony_ci#include "grf5101.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic const int grf5101_encode[] = {
278c2ecf20Sopenharmony_ci	0x0, 0x8, 0x4, 0xC,
288c2ecf20Sopenharmony_ci	0x2, 0xA, 0x6, 0xE,
298c2ecf20Sopenharmony_ci	0x1, 0x9, 0x5, 0xD,
308c2ecf20Sopenharmony_ci	0x3, 0xB, 0x7, 0xF
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void write_grf5101(struct ieee80211_hw *dev, u8 addr, u32 data)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct rtl8180_priv *priv = dev->priv;
368c2ecf20Sopenharmony_ci	u32 phy_config;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	phy_config =  grf5101_encode[(data >> 8) & 0xF];
398c2ecf20Sopenharmony_ci	phy_config |= grf5101_encode[(data >> 4) & 0xF] << 4;
408c2ecf20Sopenharmony_ci	phy_config |= grf5101_encode[data & 0xF] << 8;
418c2ecf20Sopenharmony_ci	phy_config |= grf5101_encode[(addr >> 1) & 0xF] << 12;
428c2ecf20Sopenharmony_ci	phy_config |= (addr & 1) << 16;
438c2ecf20Sopenharmony_ci	phy_config |= grf5101_encode[(data & 0xf000) >> 12] << 24;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* MAC will bang bits to the chip */
468c2ecf20Sopenharmony_ci	phy_config |= 0x90000000;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	rtl818x_iowrite32(priv,
498c2ecf20Sopenharmony_ci		(__le32 __iomem *) &priv->map->RFPinsOutput, phy_config);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	msleep(3);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void grf5101_write_phy_antenna(struct ieee80211_hw *dev, short chan)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct rtl8180_priv *priv = dev->priv;
578c2ecf20Sopenharmony_ci	u8 ant = GRF5101_ANTENNA;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (priv->rfparam & RF_PARAM_ANTBDEFAULT)
608c2ecf20Sopenharmony_ci		ant |= BB_ANTENNA_B;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (chan == 14)
638c2ecf20Sopenharmony_ci		ant |= BB_ANTATTEN_CHAN14;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x10, ant);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic u8 grf5101_rf_calc_rssi(u8 agc, u8 sq)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	if (agc > 60)
718c2ecf20Sopenharmony_ci		return 65;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* TODO(?): just return agc (or agc + 5) to avoid mult / div */
748c2ecf20Sopenharmony_ci	return 65 * agc / 60;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic void grf5101_rf_set_channel(struct ieee80211_hw *dev,
788c2ecf20Sopenharmony_ci				   struct ieee80211_conf *conf)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct rtl8180_priv *priv = dev->priv;
818c2ecf20Sopenharmony_ci	int channel =
828c2ecf20Sopenharmony_ci		ieee80211_frequency_to_channel(conf->chandef.chan->center_freq);
838c2ecf20Sopenharmony_ci	u32 txpw = priv->channels[channel - 1].hw_value & 0xFF;
848c2ecf20Sopenharmony_ci	u32 chan = channel - 1;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* set TX power */
878c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x15, 0x0);
888c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x06, txpw);
898c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x15, 0x10);
908c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x15, 0x0);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* set frequency */
938c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x07, 0x0);
948c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x0B, chan);
958c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x07, 0x1000);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	grf5101_write_phy_antenna(dev, channel);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void grf5101_rf_stop(struct ieee80211_hw *dev)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct rtl8180_priv *priv = dev->priv;
1038c2ecf20Sopenharmony_ci	u32 anaparam;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	anaparam = priv->anaparam;
1068c2ecf20Sopenharmony_ci	anaparam &= 0x000fffff;
1078c2ecf20Sopenharmony_ci	anaparam |= 0x3f900000;
1088c2ecf20Sopenharmony_ci	rtl8180_set_anaparam(priv, anaparam);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x07, 0x0);
1118c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x45);
1128c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x5);
1138c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x00, 0x8e4);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void grf5101_rf_init(struct ieee80211_hw *dev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct rtl8180_priv *priv = dev->priv;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	rtl8180_set_anaparam(priv, priv->anaparam);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x0);
1238c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x0);
1248c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x40);
1258c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x60);
1268c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x61);
1278c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x61);
1288c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x00, 0xae4);
1298c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x1);
1308c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x41);
1318c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x1f, 0x61);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x01, 0x1a23);
1348c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x02, 0x4971);
1358c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x03, 0x41de);
1368c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x04, 0x2d80);
1378c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x05, 0x68ff);	/* 0x61ff original value */
1388c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x06, 0x0);
1398c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x07, 0x0);
1408c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x08, 0x7533);
1418c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x09, 0xc401);
1428c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x0a, 0x0);
1438c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x0c, 0x1c7);
1448c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x0d, 0x29d3);
1458c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x0e, 0x2e8);
1468c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x10, 0x192);
1478c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x11, 0x248);
1488c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x12, 0x0);
1498c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x13, 0x20c4);
1508c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x14, 0xf4fc);
1518c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x15, 0x0);
1528c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x16, 0x1500);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	write_grf5101(dev, 0x07, 0x1000);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* baseband configuration */
1578c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0, 0xa8);
1588c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 3, 0x0);
1598c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 4, 0xc0);
1608c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 5, 0x90);
1618c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 6, 0x1e);
1628c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 7, 0x64);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	grf5101_write_phy_antenna(dev, 1);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x11, 0x88);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	if (rtl818x_ioread8(priv, &priv->map->CONFIG2) &
1698c2ecf20Sopenharmony_ci	    RTL818X_CONFIG2_ANTENNA_DIV)
1708c2ecf20Sopenharmony_ci		rtl8180_write_phy(dev, 0x12, 0xc0); /* enable ant diversity */
1718c2ecf20Sopenharmony_ci	else
1728c2ecf20Sopenharmony_ci		rtl8180_write_phy(dev, 0x12, 0x40); /* disable ant diversity */
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x13, 0x90 | priv->csthreshold);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x19, 0x0);
1778c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x1a, 0xa0);
1788c2ecf20Sopenharmony_ci	rtl8180_write_phy(dev, 0x1b, 0x44);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciconst struct rtl818x_rf_ops grf5101_rf_ops = {
1828c2ecf20Sopenharmony_ci	.name		= "GCT",
1838c2ecf20Sopenharmony_ci	.init		= grf5101_rf_init,
1848c2ecf20Sopenharmony_ci	.stop		= grf5101_rf_stop,
1858c2ecf20Sopenharmony_ci	.set_chan	= grf5101_rf_set_channel,
1868c2ecf20Sopenharmony_ci	.calc_rssi	= grf5101_rf_calc_rssi,
1878c2ecf20Sopenharmony_ci};
188