eeprom.c 7.8 KB
Newer Older
S
Stanislaw Gruszka 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/of.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/etherdevice.h>
#include <asm/unaligned.h>
#include "mt76x0.h"
#include "eeprom.h"
23
#include "../mt76x02_phy.h"
S
Stanislaw Gruszka 已提交
24

K
Kees Cook 已提交
25
#define MT_MAP_READS	DIV_ROUND_UP(MT_EFUSE_USAGE_MAP_SIZE, 16)
S
Stanislaw Gruszka 已提交
26 27 28
static int
mt76x0_efuse_physical_size_check(struct mt76x0_dev *dev)
{
K
Kees Cook 已提交
29
	u8 data[MT_MAP_READS * 16];
S
Stanislaw Gruszka 已提交
30 31 32
	int ret, i;
	u32 start = 0, end = 0, cnt_free;

33 34 35 36
	ret = mt76x02_get_efuse_data(&dev->mt76, MT_EE_USAGE_MAP_START,
				     data, sizeof(data), MT_EE_PHYSICAL_READ);
	if (ret)
		return ret;
S
Stanislaw Gruszka 已提交
37 38 39 40 41 42 43 44 45 46

	for (i = 0; i < MT_EFUSE_USAGE_MAP_SIZE; i++)
		if (!data[i]) {
			if (!start)
				start = MT_EE_USAGE_MAP_START + i;
			end = MT_EE_USAGE_MAP_START + i;
		}
	cnt_free = end - start + 1;

	if (MT_EFUSE_USAGE_MAP_SIZE - cnt_free < 5) {
47 48
		dev_err(dev->mt76.dev,
			"driver does not support default EEPROM\n");
S
Stanislaw Gruszka 已提交
49 50 51 52 53 54
		return -EINVAL;
	}

	return 0;
}

55
static void mt76x0_set_chip_cap(struct mt76x0_dev *dev)
S
Stanislaw Gruszka 已提交
56
{
57 58
	u16 nic_conf0 = mt76x02_eeprom_get(&dev->mt76, MT_EE_NIC_CONF_0);
	u16 nic_conf1 = mt76x02_eeprom_get(&dev->mt76, MT_EE_NIC_CONF_1);
S
Stanislaw Gruszka 已提交
59

60 61
	mt76x02_eeprom_parse_hw_cap(&dev->mt76);
	dev_dbg(dev->mt76.dev, "2GHz %d 5GHz %d\n",
62
		dev->mt76.cap.has_2ghz, dev->mt76.cap.has_5ghz);
S
Stanislaw Gruszka 已提交
63

64
	if (!mt76x02_field_valid(nic_conf1 & 0xff))
S
Stanislaw Gruszka 已提交
65 66 67 68
		nic_conf1 &= 0xff00;

	if (nic_conf1 & MT_EE_NIC_CONF_1_HW_RF_CTRL)
		dev_err(dev->mt76.dev,
69
			"driver does not support HW RF ctrl\n");
S
Stanislaw Gruszka 已提交
70

71
	if (!mt76x02_field_valid(nic_conf0 >> 8))
S
Stanislaw Gruszka 已提交
72 73 74 75
		return;

	if (FIELD_GET(MT_EE_NIC_CONF_0_RX_PATH, nic_conf0) > 1 ||
	    FIELD_GET(MT_EE_NIC_CONF_0_TX_PATH, nic_conf0) > 1)
76
		dev_err(dev->mt76.dev, "invalid tx-rx stream\n");
S
Stanislaw Gruszka 已提交
77 78
}

79
static void mt76x0_set_temp_offset(struct mt76x0_dev *dev)
S
Stanislaw Gruszka 已提交
80
{
81
	u8 val;
S
Stanislaw Gruszka 已提交
82

83 84 85
	val = mt76x02_eeprom_get(&dev->mt76, MT_EE_2G_TARGET_POWER) >> 8;
	if (mt76x02_field_valid(val))
		dev->caldata.temp_offset = mt76x02_sign_extend(val, 8);
S
Stanislaw Gruszka 已提交
86
	else
87
		dev->caldata.temp_offset = -10;
S
Stanislaw Gruszka 已提交
88 89
}

90
static void mt76x0_set_freq_offset(struct mt76x0_dev *dev)
S
Stanislaw Gruszka 已提交
91
{
92 93
	struct mt76x0_caldata *caldata = &dev->caldata;
	u8 val;
S
Stanislaw Gruszka 已提交
94

95 96 97 98
	val = mt76x02_eeprom_get(&dev->mt76, MT_EE_FREQ_OFFSET);
	if (!mt76x02_field_valid(val))
		val = 0;
	caldata->freq_offset = val;
99

100 101 102
	val = mt76x02_eeprom_get(&dev->mt76, MT_EE_TSSI_BOUND4) >> 8;
	if (!mt76x02_field_valid(val))
		val = 0;
S
Stanislaw Gruszka 已提交
103

104
	caldata->freq_offset -= mt76x02_sign_extend(val, 8);
S
Stanislaw Gruszka 已提交
105 106
}

L
Lorenzo Bianconi 已提交
107
void mt76x0_read_rx_gain(struct mt76x0_dev *dev)
S
Stanislaw Gruszka 已提交
108
{
L
Lorenzo Bianconi 已提交
109 110
	struct ieee80211_channel *chan = dev->mt76.chandef.chan;
	struct mt76x0_caldata *caldata = &dev->caldata;
111
	s8 val, lna_5g[3], lna_2g;
L
Lorenzo Bianconi 已提交
112
	u16 rssi_offset;
113
	int i;
S
Stanislaw Gruszka 已提交
114

L
Lorenzo Bianconi 已提交
115 116 117 118
	mt76x02_get_rx_gain(&dev->mt76, chan->band, &rssi_offset,
			    &lna_2g, lna_5g);
	caldata->lna_gain = mt76x02_get_lna_gain(&dev->mt76, &lna_2g,
						 lna_5g, chan);
S
Stanislaw Gruszka 已提交
119

120 121 122 123
	for (i = 0; i < ARRAY_SIZE(caldata->rssi_offset); i++) {
		val = rssi_offset >> (8 * i);
		if (val < -10 || val > 10)
			val = 0;
S
Stanislaw Gruszka 已提交
124

125
		caldata->rssi_offset[i] = val;
S
Stanislaw Gruszka 已提交
126 127 128
	}
}

129
static s8 mt76x0_get_delta(struct mt76_dev *dev)
S
Stanislaw Gruszka 已提交
130
{
131 132
	struct cfg80211_chan_def *chandef = &dev->chandef;
	u8 val;
S
Stanislaw Gruszka 已提交
133

134 135
	if (mt76x02_tssi_enabled(dev))
		return 0;
S
Stanislaw Gruszka 已提交
136

137 138 139 140
	if (chandef->width == NL80211_CHAN_WIDTH_80) {
		val = mt76x02_eeprom_get(dev, MT_EE_5G_TARGET_POWER) >> 8;
	} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
		u16 data;
S
Stanislaw Gruszka 已提交
141

142 143 144 145 146 147
		data = mt76x02_eeprom_get(dev, MT_EE_TX_POWER_DELTA_BW40);
		if (chandef->chan->band == NL80211_BAND_5GHZ)
			val = data >> 8;
		else
			val = data;
	} else {
S
Stanislaw Gruszka 已提交
148
		return 0;
149
	}
S
Stanislaw Gruszka 已提交
150

151
	return mt76x02_rate_power_val(val);
S
Stanislaw Gruszka 已提交
152 153
}

154
void mt76x0_get_tx_power_per_rate(struct mt76x0_dev *dev)
S
Stanislaw Gruszka 已提交
155
{
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	struct ieee80211_channel *chan = dev->mt76.chandef.chan;
	bool is_2ghz = chan->band == NL80211_BAND_2GHZ;
	struct mt76_rate_power *t = &dev->mt76.rate_power;
	s8 delta = mt76x0_get_delta(&dev->mt76);
	u16 val, addr;

	memset(t, 0, sizeof(*t));

	/* cck 1M, 2M, 5.5M, 11M */
	val = mt76x02_eeprom_get(&dev->mt76, MT_EE_TX_POWER_BYRATE_BASE);
	t->cck[0] = t->cck[1] = s6_to_s8(val);
	t->cck[2] = t->cck[3] = s6_to_s8(val >> 8);

	/* ofdm 6M, 9M, 12M, 18M */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 2 : 0x120;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->ofdm[0] = t->ofdm[1] = s6_to_s8(val);
	t->ofdm[2] = t->ofdm[3] = s6_to_s8(val >> 8);

	/* ofdm 24M, 36M, 48M, 54M */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 4 : 0x122;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->ofdm[4] = t->ofdm[5] = s6_to_s8(val);
	t->ofdm[6] = t->ofdm[7] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 0, 1, 2, 3 */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 6 : 0x124;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->ht[0] = t->ht[1] = t->vht[0] = t->vht[1] = s6_to_s8(val);
	t->ht[2] = t->ht[3] = t->vht[2] = t->vht[3] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 4, 5, 6 */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 8 : 0x126;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->ht[4] = t->ht[5] = t->vht[4] = t->vht[5] = s6_to_s8(val);
	t->ht[6] = t->vht[6] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 0, 1, 2, 3 stbc */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 14 : 0xec;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->stbc[0] = t->stbc[1] = s6_to_s8(val);
	t->stbc[2] = t->stbc[3] = s6_to_s8(val >> 8);

	/* ht-vht mcs 1ss 4, 5, 6 stbc */
	addr = is_2ghz ? MT_EE_TX_POWER_BYRATE_BASE + 16 : 0xee;
	val = mt76x02_eeprom_get(&dev->mt76, addr);
	t->stbc[4] = t->stbc[5] = s6_to_s8(val);
	t->stbc[6] = t->stbc[7] = s6_to_s8(val >> 8);

	/* vht mcs 8, 9 5GHz */
	val = mt76x02_eeprom_get(&dev->mt76, 0x132);
	t->vht[7] = s6_to_s8(val);
	t->vht[8] = s6_to_s8(val >> 8);

	mt76x02_add_rate_power_offset(t, delta);
S
Stanislaw Gruszka 已提交
211 212 213 214 215 216 217 218 219
}

static void
mt76x0_set_tx_power_per_chan(struct mt76x0_dev *dev, u8 *eeprom)
{
	int i;
	u8 tx_pwr;

	for (i = 0; i < 14; i++) {
220
		tx_pwr = eeprom[MT_EE_TX_POWER_DELTA_BW80 + i];
S
Stanislaw Gruszka 已提交
221 222 223 224 225 226 227
		if (tx_pwr <= 0x3f && tx_pwr > 0)
			dev->ee->tx_pwr_per_chan[i] = tx_pwr;
		else
			dev->ee->tx_pwr_per_chan[i] = 5;
	}

	for (i = 0; i < 40; i++) {
228
		tx_pwr = eeprom[MT_EE_TX_POWER_0_GRP4_TSSI_SLOPE + 2 + i];
S
Stanislaw Gruszka 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		if (tx_pwr <= 0x3f && tx_pwr > 0)
			dev->ee->tx_pwr_per_chan[14 + i] = tx_pwr;
		else
			dev->ee->tx_pwr_per_chan[14 + i] = 5;
	}

	dev->ee->tx_pwr_per_chan[54] = dev->ee->tx_pwr_per_chan[22];
	dev->ee->tx_pwr_per_chan[55] = dev->ee->tx_pwr_per_chan[28];
	dev->ee->tx_pwr_per_chan[56] = dev->ee->tx_pwr_per_chan[34];
	dev->ee->tx_pwr_per_chan[57] = dev->ee->tx_pwr_per_chan[44];
}

int
mt76x0_eeprom_init(struct mt76x0_dev *dev)
{
	u8 *eeprom;
245
	int ret;
S
Stanislaw Gruszka 已提交
246 247 248 249 250

	ret = mt76x0_efuse_physical_size_check(dev);
	if (ret)
		return ret;

251 252 253 254
	ret = mt76_eeprom_init(&dev->mt76, MT76X0_EEPROM_SIZE);
	if (ret < 0)
		return ret;

S
Stanislaw Gruszka 已提交
255 256 257 258 259 260 261 262
	dev->ee = devm_kzalloc(dev->mt76.dev, sizeof(*dev->ee), GFP_KERNEL);
	if (!dev->ee)
		return -ENOMEM;

	eeprom = kmalloc(MT76X0_EEPROM_SIZE, GFP_KERNEL);
	if (!eeprom)
		return -ENOMEM;

263 264 265 266
	ret = mt76x02_get_efuse_data(&dev->mt76, 0, eeprom,
				     MT76X0_EEPROM_SIZE, MT_EE_READ);
	if (ret)
		goto out;
S
Stanislaw Gruszka 已提交
267

268
	if (eeprom[MT_EE_VERSION + 1] > MT76X0U_EE_MAX_VER)
S
Stanislaw Gruszka 已提交
269 270
		dev_warn(dev->mt76.dev,
			 "Warning: unsupported EEPROM version %02hhx\n",
271
			 eeprom[MT_EE_VERSION + 1]);
S
Stanislaw Gruszka 已提交
272
	dev_info(dev->mt76.dev, "EEPROM ver:%02hhx fae:%02hhx\n",
273
		 eeprom[MT_EE_VERSION + 1], eeprom[MT_EE_VERSION]);
S
Stanislaw Gruszka 已提交
274

275
	mt76x02_mac_setaddr(&dev->mt76, eeprom + MT_EE_MAC_ADDR);
276
	mt76x0_set_chip_cap(dev);
277
	mt76x0_set_freq_offset(dev);
278
	mt76x0_set_temp_offset(dev);
S
Stanislaw Gruszka 已提交
279 280 281 282 283 284 285 286
	dev->chainmask = 0x0101;

	mt76x0_set_tx_power_per_chan(dev, eeprom);

out:
	kfree(eeprom);
	return ret;
}
287 288

MODULE_LICENSE("Dual BSD/GPL");