Path: blob/main/sys/contrib/dev/mediatek/mt76/mt76x2/pci_init.c
48525 views
// SPDX-License-Identifier: ISC1/*2* Copyright (C) 2016 Felix Fietkau <[email protected]>3*/45#include <linux/delay.h>6#include "mt76x2.h"7#include "eeprom.h"8#include "mcu.h"9#include "../mt76x02_mac.h"1011static void12mt76x2_mac_pbf_init(struct mt76x02_dev *dev)13{14u32 val;1516val = MT_PBF_SYS_CTRL_MCU_RESET |17MT_PBF_SYS_CTRL_DMA_RESET |18MT_PBF_SYS_CTRL_MAC_RESET |19MT_PBF_SYS_CTRL_PBF_RESET |20MT_PBF_SYS_CTRL_ASY_RESET;2122mt76_set(dev, MT_PBF_SYS_CTRL, val);23mt76_clear(dev, MT_PBF_SYS_CTRL, val);2425mt76_wr(dev, MT_PBF_TX_MAX_PCNT, 0xefef3f1f);26mt76_wr(dev, MT_PBF_RX_MAX_PCNT, 0xfebf);27}2829static void30mt76x2_fixup_xtal(struct mt76x02_dev *dev)31{32u16 eep_val;33s8 offset = 0;3435eep_val = mt76x02_eeprom_get(dev, MT_EE_XTAL_TRIM_2);3637offset = eep_val & 0x7f;38if ((eep_val & 0xff) == 0xff)39offset = 0;40else if (eep_val & 0x80)41offset = 0 - offset;4243eep_val >>= 8;44if (eep_val == 0x00 || eep_val == 0xff) {45eep_val = mt76x02_eeprom_get(dev, MT_EE_XTAL_TRIM_1);46eep_val &= 0xff;4748if (eep_val == 0x00 || eep_val == 0xff)49eep_val = 0x14;50}5152eep_val &= 0x7f;53mt76_rmw_field(dev, MT_XO_CTRL5, MT_XO_CTRL5_C2_VAL, eep_val + offset);54mt76_set(dev, MT_XO_CTRL6, MT_XO_CTRL6_C2_CTRL);5556eep_val = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_2);57switch (FIELD_GET(MT_EE_NIC_CONF_2_XTAL_OPTION, eep_val)) {58case 0:59mt76_wr(dev, MT_XO_CTRL7, 0x5c1fee80);60break;61case 1:62mt76_wr(dev, MT_XO_CTRL7, 0x5c1feed0);63break;64default:65break;66}67}6869int mt76x2_mac_reset(struct mt76x02_dev *dev, bool hard)70{71const u8 *macaddr = dev->mphy.macaddr;72u32 val;73int i, k;7475if (!mt76x02_wait_for_mac(&dev->mt76))76return -ETIMEDOUT;7778val = mt76_rr(dev, MT_WPDMA_GLO_CFG);7980val &= ~(MT_WPDMA_GLO_CFG_TX_DMA_EN |81MT_WPDMA_GLO_CFG_TX_DMA_BUSY |82MT_WPDMA_GLO_CFG_RX_DMA_EN |83MT_WPDMA_GLO_CFG_RX_DMA_BUSY |84MT_WPDMA_GLO_CFG_DMA_BURST_SIZE);85val |= FIELD_PREP(MT_WPDMA_GLO_CFG_DMA_BURST_SIZE, 3);8687mt76_wr(dev, MT_WPDMA_GLO_CFG, val);8889mt76x2_mac_pbf_init(dev);90mt76_write_mac_initvals(dev);91mt76x2_fixup_xtal(dev);9293mt76_clear(dev, MT_MAC_SYS_CTRL,94MT_MAC_SYS_CTRL_RESET_CSR |95MT_MAC_SYS_CTRL_RESET_BBP);9697if (is_mt7612(dev))98mt76_clear(dev, MT_COEXCFG0, MT_COEXCFG0_COEX_EN);99100mt76_set(dev, MT_EXT_CCA_CFG, 0x0000f000);101mt76_clear(dev, MT_TX_ALC_CFG_4, BIT(31));102103mt76_wr(dev, MT_RF_BYPASS_0, 0x06000000);104mt76_wr(dev, MT_RF_SETTING_0, 0x08800000);105usleep_range(5000, 10000);106mt76_wr(dev, MT_RF_BYPASS_0, 0x00000000);107108mt76_wr(dev, MT_MCU_CLOCK_CTL, 0x1401);109mt76_clear(dev, MT_FCE_L2_STUFF, MT_FCE_L2_STUFF_WR_MPDU_LEN_EN);110111mt76x02_mac_setaddr(dev, macaddr);112mt76x02e_init_beacon_config(dev);113if (!hard)114return 0;115116for (i = 0; i < 256 / 32; i++)117mt76_wr(dev, MT_WCID_DROP_BASE + i * 4, 0);118119for (i = 0; i < 256; i++) {120mt76x02_mac_wcid_setup(dev, i, 0, NULL);121mt76_wr(dev, MT_WCID_TX_RATE(i), 0);122mt76_wr(dev, MT_WCID_TX_RATE(i) + 4, 0);123}124125for (i = 0; i < MT_MAX_VIFS; i++)126mt76x02_mac_wcid_setup(dev, MT_VIF_WCID(i), i, NULL);127128for (i = 0; i < 16; i++)129for (k = 0; k < 4; k++)130mt76x02_mac_shared_key_setup(dev, i, k, NULL);131132for (i = 0; i < 16; i++)133mt76_rr(dev, MT_TX_STAT_FIFO);134135mt76x02_set_tx_ackto(dev);136137return 0;138}139140static void141mt76x2_power_on_rf_patch(struct mt76x02_dev *dev)142{143mt76_set(dev, 0x10130, BIT(0) | BIT(16));144udelay(1);145146mt76_clear(dev, 0x1001c, 0xff);147mt76_set(dev, 0x1001c, 0x30);148149mt76_wr(dev, 0x10014, 0x484f);150udelay(1);151152mt76_set(dev, 0x10130, BIT(17));153udelay(125);154155mt76_clear(dev, 0x10130, BIT(16));156udelay(50);157158mt76_set(dev, 0x1014c, BIT(19) | BIT(20));159}160161static void162mt76x2_power_on_rf(struct mt76x02_dev *dev, int unit)163{164int shift = unit ? 8 : 0;165166/* Enable RF BG */167mt76_set(dev, 0x10130, BIT(0) << shift);168udelay(10);169170/* Enable RFDIG LDO/AFE/ABB/ADDA */171mt76_set(dev, 0x10130, (BIT(1) | BIT(3) | BIT(4) | BIT(5)) << shift);172udelay(10);173174/* Switch RFDIG power to internal LDO */175mt76_clear(dev, 0x10130, BIT(2) << shift);176udelay(10);177178mt76x2_power_on_rf_patch(dev);179180mt76_set(dev, 0x530, 0xf);181}182183static void184mt76x2_power_on(struct mt76x02_dev *dev)185{186u32 val;187188/* Turn on WL MTCMOS */189mt76_set(dev, MT_WLAN_MTC_CTRL, MT_WLAN_MTC_CTRL_MTCMOS_PWR_UP);190191val = MT_WLAN_MTC_CTRL_STATE_UP |192MT_WLAN_MTC_CTRL_PWR_ACK |193MT_WLAN_MTC_CTRL_PWR_ACK_S;194195mt76_poll(dev, MT_WLAN_MTC_CTRL, val, val, 1000);196197mt76_clear(dev, MT_WLAN_MTC_CTRL, 0x7f << 16);198udelay(10);199200mt76_clear(dev, MT_WLAN_MTC_CTRL, 0xf << 24);201udelay(10);202203mt76_set(dev, MT_WLAN_MTC_CTRL, 0xf << 24);204mt76_clear(dev, MT_WLAN_MTC_CTRL, 0xfff);205206/* Turn on AD/DA power down */207mt76_clear(dev, 0x11204, BIT(3));208209/* WLAN function enable */210mt76_set(dev, 0x10080, BIT(0));211212/* Release BBP software reset */213mt76_clear(dev, 0x10064, BIT(18));214215mt76x2_power_on_rf(dev, 0);216mt76x2_power_on_rf(dev, 1);217}218219int mt76x2_resume_device(struct mt76x02_dev *dev)220{221int err;222223mt76x02_dma_disable(dev);224mt76x2_reset_wlan(dev, true);225mt76x2_power_on(dev);226227err = mt76x2_mac_reset(dev, true);228if (err)229return err;230231mt76x02_mac_start(dev);232233return mt76x2_mcu_init(dev);234}235236static int mt76x2_init_hardware(struct mt76x02_dev *dev)237{238int ret;239240mt76x02_dma_disable(dev);241mt76x2_reset_wlan(dev, true);242mt76x2_power_on(dev);243244ret = mt76x2_eeprom_init(dev);245if (ret)246return ret;247248ret = mt76x2_mac_reset(dev, true);249if (ret)250return ret;251252dev->mt76.rxfilter = mt76_rr(dev, MT_RX_FILTR_CFG);253254ret = mt76x02_dma_init(dev);255if (ret)256return ret;257258set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);259mt76x02_mac_start(dev);260261ret = mt76x2_mcu_init(dev);262if (ret)263return ret;264265mt76x2_mac_stop(dev, false);266267return 0;268}269270void mt76x2_stop_hardware(struct mt76x02_dev *dev)271{272cancel_delayed_work_sync(&dev->cal_work);273cancel_delayed_work_sync(&dev->mphy.mac_work);274cancel_delayed_work_sync(&dev->wdt_work);275clear_bit(MT76_RESTART, &dev->mphy.state);276mt76x02_mcu_set_radio_state(dev, false);277mt76x2_mac_stop(dev, false);278}279280void mt76x2_cleanup(struct mt76x02_dev *dev)281{282tasklet_disable(&dev->dfs_pd.dfs_tasklet);283tasklet_disable(&dev->mt76.pre_tbtt_tasklet);284mt76x2_stop_hardware(dev);285mt76_dma_cleanup(&dev->mt76);286mt76x02_mcu_cleanup(dev);287}288289int mt76x2_register_device(struct mt76x02_dev *dev)290{291int ret;292293INIT_DELAYED_WORK(&dev->cal_work, mt76x2_phy_calibrate);294ret = mt76x02_init_device(dev);295if (ret)296return ret;297298ret = mt76x2_init_hardware(dev);299if (ret)300return ret;301302mt76x02_config_mac_addr_list(dev);303304ret = mt76_register_device(&dev->mt76, true, mt76x02_rates,305ARRAY_SIZE(mt76x02_rates));306if (ret)307goto fail;308309mt76x02_init_debugfs(dev);310mt76x2_init_txpower(dev, &dev->mphy.sband_2g.sband);311mt76x2_init_txpower(dev, &dev->mphy.sband_5g.sband);312313return 0;314315fail:316mt76x2_stop_hardware(dev);317return ret;318}319320321322