Path: blob/master/drivers/net/wireless/mediatek/mt76/mt7921/init.c
216185 views
// SPDX-License-Identifier: BSD-3-Clause-Clear1/* Copyright (C) 2020 MediaTek Inc. */23#include <linux/etherdevice.h>4#include <linux/hwmon.h>5#include <linux/hwmon-sysfs.h>6#include <linux/thermal.h>7#include <linux/firmware.h>8#include "mt7921.h"9#include "../mt76_connac2_mac.h"10#include "mcu.h"1112static ssize_t mt7921_thermal_temp_show(struct device *dev,13struct device_attribute *attr,14char *buf)15{16switch (to_sensor_dev_attr(attr)->index) {17case 0: {18struct mt792x_phy *phy = dev_get_drvdata(dev);19struct mt792x_dev *mdev = phy->dev;20int temperature;2122mt792x_mutex_acquire(mdev);23temperature = mt7921_mcu_get_temperature(phy);24mt792x_mutex_release(mdev);2526if (temperature < 0)27return temperature;28/* display in millidegree Celsius */29return sprintf(buf, "%u\n", temperature * 1000);30}31default:32return -EINVAL;33}34}35static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);3637static struct attribute *mt7921_hwmon_attrs[] = {38&sensor_dev_attr_temp1_input.dev_attr.attr,39NULL,40};41ATTRIBUTE_GROUPS(mt7921_hwmon);4243static int mt7921_thermal_init(struct mt792x_phy *phy)44{45struct wiphy *wiphy = phy->mt76->hw->wiphy;46struct device *hwmon;47const char *name;4849if (!IS_REACHABLE(CONFIG_HWMON))50return 0;5152name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",53wiphy_name(wiphy));54if (!name)55return -ENOMEM;5657hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,58mt7921_hwmon_groups);59return PTR_ERR_OR_ZERO(hwmon);60}6162static void63mt7921_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)64{65#define IS_UNII_INVALID(idx, sfreq, efreq) \66(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))67struct ieee80211_supported_band *sband;68struct mt76_dev *mdev = &dev->mt76;69struct device_node *np, *band_np;70struct ieee80211_channel *ch;71int i, cfreq;7273np = mt76_find_power_limits_node(mdev);7475sband = wiphy->bands[NL80211_BAND_5GHZ];76band_np = np ? of_get_child_by_name(np, "txpower-5g") : NULL;77for (i = 0; i < sband->n_channels; i++) {78ch = &sband->channels[i];79cfreq = ch->center_freq;8081if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {82ch->flags |= IEEE80211_CHAN_DISABLED;83continue;84}8586/* UNII-4 */87if (IS_UNII_INVALID(0, 5845, 5925))88ch->flags |= IEEE80211_CHAN_DISABLED;89}9091sband = wiphy->bands[NL80211_BAND_6GHZ];92if (!sband)93return;9495band_np = np ? of_get_child_by_name(np, "txpower-6g") : NULL;96for (i = 0; i < sband->n_channels; i++) {97ch = &sband->channels[i];98cfreq = ch->center_freq;99100if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {101ch->flags |= IEEE80211_CHAN_DISABLED;102continue;103}104105/* UNII-5/6/7/8 */106if (IS_UNII_INVALID(1, 5925, 6425) ||107IS_UNII_INVALID(2, 6425, 6525) ||108IS_UNII_INVALID(3, 6525, 6875) ||109IS_UNII_INVALID(4, 6875, 7125))110ch->flags |= IEEE80211_CHAN_DISABLED;111}112}113114void mt7921_regd_update(struct mt792x_dev *dev)115{116struct mt76_dev *mdev = &dev->mt76;117struct ieee80211_hw *hw = mdev->hw;118struct wiphy *wiphy = hw->wiphy;119120mt7921_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env);121mt7921_regd_channel_update(wiphy, dev);122mt76_connac_mcu_set_channel_domain(hw->priv);123mt7921_set_tx_sar_pwr(hw, NULL);124}125EXPORT_SYMBOL_GPL(mt7921_regd_update);126127static void128mt7921_regd_notifier(struct wiphy *wiphy,129struct regulatory_request *request)130{131struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);132struct mt792x_dev *dev = mt792x_hw_dev(hw);133struct mt76_connac_pm *pm = &dev->pm;134135memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));136dev->mt76.region = request->dfs_region;137dev->country_ie_env = request->country_ie_env;138139if (request->initiator == NL80211_REGDOM_SET_BY_USER) {140if (dev->mt76.alpha2[0] == '0' && dev->mt76.alpha2[1] == '0')141wiphy->regulatory_flags &= ~REGULATORY_COUNTRY_IE_IGNORE;142else143wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE;144}145146if (pm->suspended)147return;148149dev->regd_in_progress = true;150151mt792x_mutex_acquire(dev);152mt7921_regd_update(dev);153mt792x_mutex_release(dev);154155dev->regd_in_progress = false;156wake_up(&dev->wait);157}158159int mt7921_mac_init(struct mt792x_dev *dev)160{161int i;162163mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);164/* enable hardware de-agg */165mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);166/* enable hardware rx header translation */167mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);168169for (i = 0; i < MT792x_WTBL_SIZE; i++)170mt7921_mac_wtbl_update(dev, i,171MT_WTBL_UPDATE_ADM_COUNT_CLEAR);172for (i = 0; i < 2; i++)173mt792x_mac_init_band(dev, i);174175return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);176}177EXPORT_SYMBOL_GPL(mt7921_mac_init);178179static int __mt7921_init_hardware(struct mt792x_dev *dev)180{181int ret;182183/* force firmware operation mode into normal state,184* which should be set before firmware download stage.185*/186mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);187ret = mt792x_mcu_init(dev);188if (ret)189goto out;190191ret = mt76_eeprom_override(&dev->mphy);192if (ret)193goto out;194195ret = mt7921_mcu_set_eeprom(dev);196if (ret)197goto out;198199ret = mt7921_mac_init(dev);200out:201return ret;202}203204static int mt7921_init_hardware(struct mt792x_dev *dev)205{206int ret, i;207208set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);209210for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {211ret = __mt7921_init_hardware(dev);212if (!ret)213break;214215mt792x_init_reset(dev);216}217218if (i == MT792x_MCU_INIT_RETRY_COUNT) {219dev_err(dev->mt76.dev, "hardware init failed\n");220return ret;221}222223return 0;224}225226static void mt7921_init_work(struct work_struct *work)227{228struct mt792x_dev *dev = container_of(work, struct mt792x_dev,229init_work);230int ret;231232ret = mt7921_init_hardware(dev);233if (ret)234return;235236mt76_set_stream_caps(&dev->mphy, true);237mt7921_set_stream_he_caps(&dev->phy);238mt792x_config_mac_addr_list(dev);239240ret = mt76_register_device(&dev->mt76, true, mt76_rates,241ARRAY_SIZE(mt76_rates));242if (ret) {243dev_err(dev->mt76.dev, "register device failed\n");244return;245}246247ret = mt7921_init_debugfs(dev);248if (ret) {249dev_err(dev->mt76.dev, "register debugfs failed\n");250return;251}252253ret = mt7921_thermal_init(&dev->phy);254if (ret) {255dev_err(dev->mt76.dev, "thermal init failed\n");256return;257}258259/* we support chip reset now */260dev->hw_init_done = true;261262mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);263}264265int mt7921_register_device(struct mt792x_dev *dev)266{267struct ieee80211_hw *hw = mt76_hw(dev);268int ret;269270dev->phy.dev = dev;271dev->phy.mt76 = &dev->mt76.phy;272dev->mt76.phy.priv = &dev->phy;273dev->mt76.tx_worker.fn = mt792x_tx_worker;274275INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);276INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);277spin_lock_init(&dev->pm.wake.lock);278mutex_init(&dev->pm.mutex);279init_waitqueue_head(&dev->pm.wait);280init_waitqueue_head(&dev->wait);281if (mt76_is_sdio(&dev->mt76))282init_waitqueue_head(&dev->mt76.sdio.wait);283spin_lock_init(&dev->pm.txq_lock);284INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);285INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);286INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);287#if IS_ENABLED(CONFIG_IPV6)288INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);289skb_queue_head_init(&dev->ipv6_ns_list);290#endif291skb_queue_head_init(&dev->phy.scan_event_list);292skb_queue_head_init(&dev->coredump.msg_list);293294INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);295INIT_WORK(&dev->init_work, mt7921_init_work);296297INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);298timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);299init_waitqueue_head(&dev->phy.roc_wait);300301dev->pm.idle_timeout = MT792x_PM_TIMEOUT;302dev->pm.stats.last_wake_event = jiffies;303dev->pm.stats.last_doze_event = jiffies;304305if (!mt76_is_usb(&dev->mt76) &&306!is_mt7902(&dev->mt76)) {307dev->pm.enable_user = true;308dev->pm.enable = true;309dev->pm.ds_enable_user = true;310dev->pm.ds_enable = true;311}312313if (!mt76_is_mmio(&dev->mt76))314hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;315316mt792x_init_acpi_sar(dev);317318ret = mt792x_init_wcid(dev);319if (ret)320return ret;321322ret = mt792x_init_wiphy(hw);323if (ret)324return ret;325326hw->wiphy->reg_notifier = mt7921_regd_notifier;327dev->mphy.sband_2g.sband.ht_cap.cap |=328IEEE80211_HT_CAP_LDPC_CODING |329IEEE80211_HT_CAP_MAX_AMSDU;330dev->mphy.sband_5g.sband.ht_cap.cap |=331IEEE80211_HT_CAP_LDPC_CODING |332IEEE80211_HT_CAP_MAX_AMSDU;333dev->mphy.sband_5g.sband.vht_cap.cap |=334IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |335IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |336IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |337IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |338(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);339if (is_mt7922(&dev->mt76))340dev->mphy.sband_5g.sband.vht_cap.cap |=341IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |342IEEE80211_VHT_CAP_SHORT_GI_160;343344dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;345dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;346347queue_work(system_percpu_wq, &dev->init_work);348349return 0;350}351EXPORT_SYMBOL_GPL(mt7921_register_device);352353354