1e90354e0SLorenzo Bianconi // SPDX-License-Identifier: ISC
2e90354e0SLorenzo Bianconi /* Copyright (C) 2019 MediaTek Inc.
3e90354e0SLorenzo Bianconi *
4e90354e0SLorenzo Bianconi * Author: Roy Luo <royluo@google.com>
5e90354e0SLorenzo Bianconi * Ryder Lee <ryder.lee@mediatek.com>
6e90354e0SLorenzo Bianconi * Felix Fietkau <nbd@nbd.name>
7e90354e0SLorenzo Bianconi * Lorenzo Bianconi <lorenzo@kernel.org>
8e90354e0SLorenzo Bianconi */
9e90354e0SLorenzo Bianconi
10e90354e0SLorenzo Bianconi #include <linux/etherdevice.h>
11e90354e0SLorenzo Bianconi #include "mt7615.h"
12e90354e0SLorenzo Bianconi #include "mac.h"
13e90354e0SLorenzo Bianconi #include "eeprom.h"
14e90354e0SLorenzo Bianconi
mt7615_pci_init_work(struct work_struct * work)1595f381c5SFelix Fietkau static void mt7615_pci_init_work(struct work_struct *work)
16e90354e0SLorenzo Bianconi {
17e90354e0SLorenzo Bianconi struct mt7615_dev *dev = container_of(work, struct mt7615_dev,
18e90354e0SLorenzo Bianconi mcu_work);
19ced050aeSChuanhong Guo int i, ret;
20e90354e0SLorenzo Bianconi
21ced050aeSChuanhong Guo ret = mt7615_mcu_init(dev);
22ced050aeSChuanhong Guo for (i = 0; (ret == -EAGAIN) && (i < 10); i++) {
23ced050aeSChuanhong Guo msleep(200);
24ced050aeSChuanhong Guo ret = mt7615_mcu_init(dev);
25ced050aeSChuanhong Guo }
26ced050aeSChuanhong Guo
27ced050aeSChuanhong Guo if (ret)
28e90354e0SLorenzo Bianconi return;
29e90354e0SLorenzo Bianconi
3095f381c5SFelix Fietkau mt7615_init_work(dev);
31e90354e0SLorenzo Bianconi }
32e90354e0SLorenzo Bianconi
mt7615_init_hardware(struct mt7615_dev * dev)33e90354e0SLorenzo Bianconi static int mt7615_init_hardware(struct mt7615_dev *dev)
34e90354e0SLorenzo Bianconi {
35e90354e0SLorenzo Bianconi u32 addr = mt7615_reg_map(dev, MT_EFUSE_BASE);
36e90354e0SLorenzo Bianconi int ret, idx;
37e90354e0SLorenzo Bianconi
38e90354e0SLorenzo Bianconi mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
39e90354e0SLorenzo Bianconi
4095f381c5SFelix Fietkau INIT_WORK(&dev->mcu_work, mt7615_pci_init_work);
41e90354e0SLorenzo Bianconi ret = mt7615_eeprom_init(dev, addr);
42e90354e0SLorenzo Bianconi if (ret < 0)
43e90354e0SLorenzo Bianconi return ret;
44e90354e0SLorenzo Bianconi
4545387363SFelix Fietkau if (is_mt7663(&dev->mt76)) {
4645387363SFelix Fietkau /* Reset RGU */
4745387363SFelix Fietkau mt76_clear(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1));
4845387363SFelix Fietkau mt76_set(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1));
4945387363SFelix Fietkau }
5045387363SFelix Fietkau
51e90354e0SLorenzo Bianconi ret = mt7615_dma_init(dev);
52e90354e0SLorenzo Bianconi if (ret)
53e90354e0SLorenzo Bianconi return ret;
54e90354e0SLorenzo Bianconi
55e90354e0SLorenzo Bianconi set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
56e90354e0SLorenzo Bianconi
57e90354e0SLorenzo Bianconi /* Beacon and mgmt frames should occupy wcid 0 */
58e90354e0SLorenzo Bianconi idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
59e90354e0SLorenzo Bianconi if (idx)
60e90354e0SLorenzo Bianconi return -ENOSPC;
61e90354e0SLorenzo Bianconi
62e90354e0SLorenzo Bianconi dev->mt76.global_wcid.idx = idx;
63e90354e0SLorenzo Bianconi dev->mt76.global_wcid.hw_key_idx = -1;
64e90354e0SLorenzo Bianconi rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
65e90354e0SLorenzo Bianconi
66e90354e0SLorenzo Bianconi return 0;
67e90354e0SLorenzo Bianconi }
68e90354e0SLorenzo Bianconi
mt7615_register_device(struct mt7615_dev * dev)69e90354e0SLorenzo Bianconi int mt7615_register_device(struct mt7615_dev *dev)
70e90354e0SLorenzo Bianconi {
71e90354e0SLorenzo Bianconi int ret;
72e90354e0SLorenzo Bianconi
73e90354e0SLorenzo Bianconi mt7615_init_device(dev);
741cb7ea2aSFelix Fietkau INIT_WORK(&dev->reset_work, mt7615_mac_reset_work);
75e90354e0SLorenzo Bianconi
76e90354e0SLorenzo Bianconi /* init led callbacks */
77e90354e0SLorenzo Bianconi if (IS_ENABLED(CONFIG_MT76_LEDS)) {
783abd46ddSLorenzo Bianconi dev->mphy.leds.cdev.brightness_set = mt7615_led_set_brightness;
793abd46ddSLorenzo Bianconi dev->mphy.leds.cdev.blink_set = mt7615_led_set_blink;
80e90354e0SLorenzo Bianconi }
81e90354e0SLorenzo Bianconi
82e90354e0SLorenzo Bianconi ret = mt7622_wmac_init(dev);
83e90354e0SLorenzo Bianconi if (ret)
84e90354e0SLorenzo Bianconi return ret;
85e90354e0SLorenzo Bianconi
86e90354e0SLorenzo Bianconi ret = mt7615_init_hardware(dev);
87e90354e0SLorenzo Bianconi if (ret)
88e90354e0SLorenzo Bianconi return ret;
89e90354e0SLorenzo Bianconi
9054b8fdebSLorenzo Bianconi ret = mt76_register_device(&dev->mt76, true, mt76_rates,
9154b8fdebSLorenzo Bianconi ARRAY_SIZE(mt76_rates));
92e90354e0SLorenzo Bianconi if (ret)
93e90354e0SLorenzo Bianconi return ret;
94e90354e0SLorenzo Bianconi
95109e505aSRyder Lee ret = mt7615_thermal_init(dev);
96109e505aSRyder Lee if (ret)
97109e505aSRyder Lee return ret;
98109e505aSRyder Lee
99e90354e0SLorenzo Bianconi ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work);
100e90354e0SLorenzo Bianconi mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband);
101e90354e0SLorenzo Bianconi mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband);
102e90354e0SLorenzo Bianconi
1038c55516dSPeter Chiu if (dev->dbdc_support) {
1048c55516dSPeter Chiu ret = mt7615_register_ext_phy(dev);
1058c55516dSPeter Chiu if (ret)
1068c55516dSPeter Chiu return ret;
1078c55516dSPeter Chiu }
1088c55516dSPeter Chiu
109e90354e0SLorenzo Bianconi return mt7615_init_debugfs(dev);
110e90354e0SLorenzo Bianconi }
111e90354e0SLorenzo Bianconi
mt7615_unregister_device(struct mt7615_dev * dev)112e90354e0SLorenzo Bianconi void mt7615_unregister_device(struct mt7615_dev *dev)
113e90354e0SLorenzo Bianconi {
114e90354e0SLorenzo Bianconi bool mcu_running;
115e90354e0SLorenzo Bianconi
116e90354e0SLorenzo Bianconi mcu_running = mt7615_wait_for_mcu_init(dev);
117e90354e0SLorenzo Bianconi
118e90354e0SLorenzo Bianconi mt7615_unregister_ext_phy(dev);
119e90354e0SLorenzo Bianconi mt76_unregister_device(&dev->mt76);
120e90354e0SLorenzo Bianconi if (mcu_running)
121e90354e0SLorenzo Bianconi mt7615_mcu_exit(dev);
122e90354e0SLorenzo Bianconi
123a6275e93SRyder Lee mt7615_tx_token_put(dev);
1248ab31da7SRyder Lee mt7615_dma_cleanup(dev);
125*ec193b41SLorenzo Bianconi tasklet_disable(&dev->mt76.irq_tasklet);
126e90354e0SLorenzo Bianconi
127e90354e0SLorenzo Bianconi mt76_free_device(&dev->mt76);
128e90354e0SLorenzo Bianconi }
129