Path: blob/main/sys/contrib/dev/mediatek/mt76/mt7921/init.c
48525 views
// SPDX-License-Identifier: ISC1/* 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"1112#if defined(__linux__)13static ssize_t mt7921_thermal_temp_show(struct device *dev,14struct device_attribute *attr,15char *buf)16{17switch (to_sensor_dev_attr(attr)->index) {18case 0: {19struct mt792x_phy *phy = dev_get_drvdata(dev);20struct mt792x_dev *mdev = phy->dev;21int temperature;2223mt792x_mutex_acquire(mdev);24temperature = mt7921_mcu_get_temperature(phy);25mt792x_mutex_release(mdev);2627if (temperature < 0)28return temperature;29/* display in millidegree Celsius */30return sprintf(buf, "%u\n", temperature * 1000);31}32default:33return -EINVAL;34}35}36static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);3738static struct attribute *mt7921_hwmon_attrs[] = {39&sensor_dev_attr_temp1_input.dev_attr.attr,40NULL,41};42ATTRIBUTE_GROUPS(mt7921_hwmon);4344static int mt7921_thermal_init(struct mt792x_phy *phy)45{46struct wiphy *wiphy = phy->mt76->hw->wiphy;47struct device *hwmon;48const char *name;4950if (!IS_REACHABLE(CONFIG_HWMON))51return 0;5253name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",54wiphy_name(wiphy));55if (!name)56return -ENOMEM;5758hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,59mt7921_hwmon_groups);60return PTR_ERR_OR_ZERO(hwmon);61}62#endif6364static void65mt7921_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)66{67#define IS_UNII_INVALID(idx, sfreq, efreq) \68(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))69struct ieee80211_supported_band *sband;70struct mt76_dev *mdev = &dev->mt76;71struct device_node *np, *band_np;72struct ieee80211_channel *ch;73int i, cfreq;7475np = mt76_find_power_limits_node(mdev);7677sband = wiphy->bands[NL80211_BAND_5GHZ];78#if defined(CONFIG_OF)79band_np = np ? of_get_child_by_name(np, "txpower-5g") : NULL;80#else81band_np = NULL;82#endif83for (i = 0; i < sband->n_channels; i++) {84ch = &sband->channels[i];85cfreq = ch->center_freq;8687if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {88ch->flags |= IEEE80211_CHAN_DISABLED;89continue;90}9192/* UNII-4 */93if (IS_UNII_INVALID(0, 5845, 5925))94ch->flags |= IEEE80211_CHAN_DISABLED;95}9697sband = wiphy->bands[NL80211_BAND_6GHZ];98if (!sband)99return;100101#if defined(CONFIG_OF)102band_np = np ? of_get_child_by_name(np, "txpower-6g") : NULL;103#else104band_np = NULL;105#endif106for (i = 0; i < sband->n_channels; i++) {107ch = &sband->channels[i];108cfreq = ch->center_freq;109110if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {111ch->flags |= IEEE80211_CHAN_DISABLED;112continue;113}114115/* UNII-5/6/7/8 */116if (IS_UNII_INVALID(1, 5925, 6425) ||117IS_UNII_INVALID(2, 6425, 6525) ||118IS_UNII_INVALID(3, 6525, 6875) ||119IS_UNII_INVALID(4, 6875, 7125))120ch->flags |= IEEE80211_CHAN_DISABLED;121}122}123124void mt7921_regd_update(struct mt792x_dev *dev)125{126struct mt76_dev *mdev = &dev->mt76;127struct ieee80211_hw *hw = mdev->hw;128struct wiphy *wiphy = hw->wiphy;129130mt7921_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env);131mt7921_regd_channel_update(wiphy, dev);132mt76_connac_mcu_set_channel_domain(hw->priv);133mt7921_set_tx_sar_pwr(hw, NULL);134}135EXPORT_SYMBOL_GPL(mt7921_regd_update);136137static void138mt7921_regd_notifier(struct wiphy *wiphy,139struct regulatory_request *request)140{141struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);142struct mt792x_dev *dev = mt792x_hw_dev(hw);143struct mt76_connac_pm *pm = &dev->pm;144145memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));146dev->mt76.region = request->dfs_region;147dev->country_ie_env = request->country_ie_env;148149if (request->initiator == NL80211_REGDOM_SET_BY_USER) {150if (dev->mt76.alpha2[0] == '0' && dev->mt76.alpha2[1] == '0')151wiphy->regulatory_flags &= ~REGULATORY_COUNTRY_IE_IGNORE;152else153wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE;154}155156if (pm->suspended)157return;158159dev->regd_in_progress = true;160161mt792x_mutex_acquire(dev);162mt7921_regd_update(dev);163mt792x_mutex_release(dev);164165dev->regd_in_progress = false;166wake_up(&dev->wait);167}168169int mt7921_mac_init(struct mt792x_dev *dev)170{171int i;172173mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);174/* enable hardware de-agg */175mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);176/* enable hardware rx header translation */177mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);178179for (i = 0; i < MT792x_WTBL_SIZE; i++)180mt7921_mac_wtbl_update(dev, i,181MT_WTBL_UPDATE_ADM_COUNT_CLEAR);182for (i = 0; i < 2; i++)183mt792x_mac_init_band(dev, i);184185return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);186}187EXPORT_SYMBOL_GPL(mt7921_mac_init);188189static int __mt7921_init_hardware(struct mt792x_dev *dev)190{191int ret;192193/* force firmware operation mode into normal state,194* which should be set before firmware download stage.195*/196mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);197ret = mt792x_mcu_init(dev);198if (ret)199goto out;200201mt76_eeprom_override(&dev->mphy);202203ret = mt7921_mcu_set_eeprom(dev);204if (ret)205goto out;206207ret = mt7921_mac_init(dev);208out:209return ret;210}211212static int mt7921_init_hardware(struct mt792x_dev *dev)213{214int ret, i;215216set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);217218for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {219ret = __mt7921_init_hardware(dev);220if (!ret)221break;222223mt792x_init_reset(dev);224}225226if (i == MT792x_MCU_INIT_RETRY_COUNT) {227dev_err(dev->mt76.dev, "hardware init failed\n");228return ret;229}230231return 0;232}233234static void mt7921_init_work(struct work_struct *work)235{236struct mt792x_dev *dev = container_of(work, struct mt792x_dev,237init_work);238int ret;239240ret = mt7921_init_hardware(dev);241if (ret)242return;243244mt76_set_stream_caps(&dev->mphy, true);245mt7921_set_stream_he_caps(&dev->phy);246mt792x_config_mac_addr_list(dev);247248ret = mt76_register_device(&dev->mt76, true, mt76_rates,249ARRAY_SIZE(mt76_rates));250if (ret) {251dev_err(dev->mt76.dev, "register device failed\n");252return;253}254255#if !defined(__FreeBSD__) || defined(CONFIG_MT7921_DEBUGFS)256ret = mt7921_init_debugfs(dev);257if (ret) {258dev_err(dev->mt76.dev, "register debugfs failed\n");259return;260}261#endif262263#if defined(__linux__)264ret = mt7921_thermal_init(&dev->phy);265if (ret) {266dev_err(dev->mt76.dev, "thermal init failed\n");267return;268}269#endif270271/* we support chip reset now */272dev->hw_init_done = true;273274mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);275}276277int mt7921_register_device(struct mt792x_dev *dev)278{279struct ieee80211_hw *hw = mt76_hw(dev);280int ret;281282dev->phy.dev = dev;283dev->phy.mt76 = &dev->mt76.phy;284dev->mt76.phy.priv = &dev->phy;285dev->mt76.tx_worker.fn = mt792x_tx_worker;286287INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);288INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);289spin_lock_init(&dev->pm.wake.lock);290mutex_init(&dev->pm.mutex);291init_waitqueue_head(&dev->pm.wait);292init_waitqueue_head(&dev->wait);293if (mt76_is_sdio(&dev->mt76))294init_waitqueue_head(&dev->mt76.sdio.wait);295spin_lock_init(&dev->pm.txq_lock);296INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);297INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);298INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);299#if IS_ENABLED(CONFIG_IPV6)300INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);301skb_queue_head_init(&dev->ipv6_ns_list);302#endif303skb_queue_head_init(&dev->phy.scan_event_list);304skb_queue_head_init(&dev->coredump.msg_list);305306INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);307INIT_WORK(&dev->init_work, mt7921_init_work);308309INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);310timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);311init_waitqueue_head(&dev->phy.roc_wait);312313dev->pm.idle_timeout = MT792x_PM_TIMEOUT;314dev->pm.stats.last_wake_event = jiffies;315dev->pm.stats.last_doze_event = jiffies;316if (!mt76_is_usb(&dev->mt76)) {317dev->pm.enable_user = true;318dev->pm.enable = true;319dev->pm.ds_enable_user = true;320dev->pm.ds_enable = true;321}322323if (!mt76_is_mmio(&dev->mt76))324hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;325326mt792x_init_acpi_sar(dev);327328ret = mt792x_init_wcid(dev);329if (ret)330return ret;331332ret = mt792x_init_wiphy(hw);333if (ret)334return ret;335336hw->wiphy->reg_notifier = mt7921_regd_notifier;337dev->mphy.sband_2g.sband.ht_cap.cap |=338IEEE80211_HT_CAP_LDPC_CODING |339IEEE80211_HT_CAP_MAX_AMSDU;340dev->mphy.sband_5g.sband.ht_cap.cap |=341IEEE80211_HT_CAP_LDPC_CODING |342IEEE80211_HT_CAP_MAX_AMSDU;343dev->mphy.sband_5g.sband.vht_cap.cap |=344IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |345IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |346IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |347IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |348(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);349if (is_mt7922(&dev->mt76))350dev->mphy.sband_5g.sband.vht_cap.cap |=351IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |352IEEE80211_VHT_CAP_SHORT_GI_160;353354dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;355dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;356357queue_work(system_wq, &dev->init_work);358359return 0;360}361EXPORT_SYMBOL_GPL(mt7921_register_device);362363364