1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*	Copyright (C) 2009 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
3  *	Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
4  *	Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5  *	Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
6  *	Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
7  *	Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
8  *	Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
9  *	Copyright (C) 2009 Bart Zolnierkiewicz <bzolnier@gmail.com>
10  *	<http://rt2x00.serialmonkey.com>
11  */
12 
13 /*	Module: rt2800soc
14  *	Abstract: rt2800 WiSoC specific routines.
15  */
16 
17 #include <linux/etherdevice.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 
23 #include "rt2x00.h"
24 #include "rt2x00mmio.h"
25 #include "rt2x00soc.h"
26 #include "rt2800.h"
27 #include "rt2800lib.h"
28 #include "rt2800mmio.h"
29 
30 /* Allow hardware encryption to be disabled. */
31 static bool modparam_nohwcrypt;
32 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, 0444);
33 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
34 
35 static bool rt2800soc_hwcrypt_disabled(struct rt2x00_dev *rt2x00dev)
36 {
37 	return modparam_nohwcrypt;
38 }
39 
40 static void rt2800soc_disable_radio(struct rt2x00_dev *rt2x00dev)
41 {
42 	u32 reg;
43 
44 	rt2800_disable_radio(rt2x00dev);
45 	rt2x00mmio_register_write(rt2x00dev, PWR_PIN_CFG, 0);
46 
47 	reg = 0;
48 	if (rt2x00_rt(rt2x00dev, RT3883))
49 		rt2x00_set_field32(&reg, TX_PIN_CFG_RFTR_EN, 1);
50 
51 	rt2x00mmio_register_write(rt2x00dev, TX_PIN_CFG, reg);
52 }
53 
54 static int rt2800soc_set_device_state(struct rt2x00_dev *rt2x00dev,
55 				      enum dev_state state)
56 {
57 	int retval = 0;
58 
59 	switch (state) {
60 	case STATE_RADIO_ON:
61 		retval = rt2800mmio_enable_radio(rt2x00dev);
62 		break;
63 
64 	case STATE_RADIO_OFF:
65 		rt2800soc_disable_radio(rt2x00dev);
66 		break;
67 
68 	case STATE_RADIO_IRQ_ON:
69 	case STATE_RADIO_IRQ_OFF:
70 		rt2800mmio_toggle_irq(rt2x00dev, state);
71 		break;
72 
73 	case STATE_DEEP_SLEEP:
74 	case STATE_SLEEP:
75 	case STATE_STANDBY:
76 	case STATE_AWAKE:
77 		/* These states are not supported, but don't report an error */
78 		retval = 0;
79 		break;
80 
81 	default:
82 		retval = -ENOTSUPP;
83 		break;
84 	}
85 
86 	if (unlikely(retval))
87 		rt2x00_err(rt2x00dev, "Device failed to enter state %d (%d)\n",
88 			   state, retval);
89 
90 	return retval;
91 }
92 
93 static int rt2800soc_read_eeprom(struct rt2x00_dev *rt2x00dev)
94 {
95 	void __iomem *base_addr = ioremap(0x1F040000, EEPROM_SIZE);
96 
97 	if (!base_addr)
98 		return -ENOMEM;
99 
100 	memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
101 
102 	iounmap(base_addr);
103 	return 0;
104 }
105 
106 /* Firmware functions */
107 static char *rt2800soc_get_firmware_name(struct rt2x00_dev *rt2x00dev)
108 {
109 	WARN_ON_ONCE(1);
110 	return NULL;
111 }
112 
113 static int rt2800soc_load_firmware(struct rt2x00_dev *rt2x00dev,
114 				   const u8 *data, const size_t len)
115 {
116 	WARN_ON_ONCE(1);
117 	return 0;
118 }
119 
120 static int rt2800soc_check_firmware(struct rt2x00_dev *rt2x00dev,
121 				    const u8 *data, const size_t len)
122 {
123 	WARN_ON_ONCE(1);
124 	return 0;
125 }
126 
127 static int rt2800soc_write_firmware(struct rt2x00_dev *rt2x00dev,
128 				    const u8 *data, const size_t len)
129 {
130 	WARN_ON_ONCE(1);
131 	return 0;
132 }
133 
134 static const struct ieee80211_ops rt2800soc_mac80211_ops = {
135 	.tx			= rt2x00mac_tx,
136 	.start			= rt2x00mac_start,
137 	.stop			= rt2x00mac_stop,
138 	.add_interface		= rt2x00mac_add_interface,
139 	.remove_interface	= rt2x00mac_remove_interface,
140 	.config			= rt2x00mac_config,
141 	.configure_filter	= rt2x00mac_configure_filter,
142 	.set_key		= rt2x00mac_set_key,
143 	.sw_scan_start		= rt2x00mac_sw_scan_start,
144 	.sw_scan_complete	= rt2x00mac_sw_scan_complete,
145 	.get_stats		= rt2x00mac_get_stats,
146 	.get_key_seq		= rt2800_get_key_seq,
147 	.set_rts_threshold	= rt2800_set_rts_threshold,
148 	.sta_add		= rt2800_sta_add,
149 	.sta_remove		= rt2800_sta_remove,
150 	.bss_info_changed	= rt2x00mac_bss_info_changed,
151 	.conf_tx		= rt2800_conf_tx,
152 	.get_tsf		= rt2800_get_tsf,
153 	.rfkill_poll		= rt2x00mac_rfkill_poll,
154 	.ampdu_action		= rt2800_ampdu_action,
155 	.flush			= rt2x00mac_flush,
156 	.get_survey		= rt2800_get_survey,
157 	.get_ringparam		= rt2x00mac_get_ringparam,
158 	.tx_frames_pending	= rt2x00mac_tx_frames_pending,
159 	.reconfig_complete	= rt2x00mac_reconfig_complete,
160 };
161 
162 static const struct rt2800_ops rt2800soc_rt2800_ops = {
163 	.register_read		= rt2x00mmio_register_read,
164 	.register_read_lock	= rt2x00mmio_register_read, /* same for SoCs */
165 	.register_write		= rt2x00mmio_register_write,
166 	.register_write_lock	= rt2x00mmio_register_write, /* same for SoCs */
167 	.register_multiread	= rt2x00mmio_register_multiread,
168 	.register_multiwrite	= rt2x00mmio_register_multiwrite,
169 	.regbusy_read		= rt2x00mmio_regbusy_read,
170 	.read_eeprom		= rt2800soc_read_eeprom,
171 	.hwcrypt_disabled	= rt2800soc_hwcrypt_disabled,
172 	.drv_write_firmware	= rt2800soc_write_firmware,
173 	.drv_init_registers	= rt2800mmio_init_registers,
174 	.drv_get_txwi		= rt2800mmio_get_txwi,
175 	.drv_get_dma_done	= rt2800mmio_get_dma_done,
176 };
177 
178 static const struct rt2x00lib_ops rt2800soc_rt2x00_ops = {
179 	.irq_handler		= rt2800mmio_interrupt,
180 	.txstatus_tasklet	= rt2800mmio_txstatus_tasklet,
181 	.pretbtt_tasklet	= rt2800mmio_pretbtt_tasklet,
182 	.tbtt_tasklet		= rt2800mmio_tbtt_tasklet,
183 	.rxdone_tasklet		= rt2800mmio_rxdone_tasklet,
184 	.autowake_tasklet	= rt2800mmio_autowake_tasklet,
185 	.probe_hw		= rt2800mmio_probe_hw,
186 	.get_firmware_name	= rt2800soc_get_firmware_name,
187 	.check_firmware		= rt2800soc_check_firmware,
188 	.load_firmware		= rt2800soc_load_firmware,
189 	.initialize		= rt2x00mmio_initialize,
190 	.uninitialize		= rt2x00mmio_uninitialize,
191 	.get_entry_state	= rt2800mmio_get_entry_state,
192 	.clear_entry		= rt2800mmio_clear_entry,
193 	.set_device_state	= rt2800soc_set_device_state,
194 	.rfkill_poll		= rt2800_rfkill_poll,
195 	.link_stats		= rt2800_link_stats,
196 	.reset_tuner		= rt2800_reset_tuner,
197 	.link_tuner		= rt2800_link_tuner,
198 	.gain_calibration	= rt2800_gain_calibration,
199 	.vco_calibration	= rt2800_vco_calibration,
200 	.watchdog		= rt2800_watchdog,
201 	.start_queue		= rt2800mmio_start_queue,
202 	.kick_queue		= rt2800mmio_kick_queue,
203 	.stop_queue		= rt2800mmio_stop_queue,
204 	.flush_queue		= rt2800mmio_flush_queue,
205 	.write_tx_desc		= rt2800mmio_write_tx_desc,
206 	.write_tx_data		= rt2800_write_tx_data,
207 	.write_beacon		= rt2800_write_beacon,
208 	.clear_beacon		= rt2800_clear_beacon,
209 	.fill_rxdone		= rt2800mmio_fill_rxdone,
210 	.config_shared_key	= rt2800_config_shared_key,
211 	.config_pairwise_key	= rt2800_config_pairwise_key,
212 	.config_filter		= rt2800_config_filter,
213 	.config_intf		= rt2800_config_intf,
214 	.config_erp		= rt2800_config_erp,
215 	.config_ant		= rt2800_config_ant,
216 	.config			= rt2800_config,
217 	.pre_reset_hw		= rt2800_pre_reset_hw,
218 };
219 
220 static const struct rt2x00_ops rt2800soc_ops = {
221 	.name			= KBUILD_MODNAME,
222 	.drv_data_size		= sizeof(struct rt2800_drv_data),
223 	.max_ap_intf		= 8,
224 	.eeprom_size		= EEPROM_SIZE,
225 	.rf_size		= RF_SIZE,
226 	.tx_queues		= NUM_TX_QUEUES,
227 	.queue_init		= rt2800mmio_queue_init,
228 	.lib			= &rt2800soc_rt2x00_ops,
229 	.drv			= &rt2800soc_rt2800_ops,
230 	.hw			= &rt2800soc_mac80211_ops,
231 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
232 	.debugfs		= &rt2800_rt2x00debug,
233 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
234 };
235 
236 static int rt2800soc_probe(struct platform_device *pdev)
237 {
238 	return rt2x00soc_probe(pdev, &rt2800soc_ops);
239 }
240 
241 static struct platform_driver rt2800soc_driver = {
242 	.driver		= {
243 		.name		= "rt2800_wmac",
244 		.mod_name	= KBUILD_MODNAME,
245 	},
246 	.probe		= rt2800soc_probe,
247 	.remove		= rt2x00soc_remove,
248 	.suspend	= rt2x00soc_suspend,
249 	.resume		= rt2x00soc_resume,
250 };
251 
252 module_platform_driver(rt2800soc_driver);
253 
254 MODULE_AUTHOR(DRV_PROJECT);
255 MODULE_VERSION(DRV_VERSION);
256 MODULE_DESCRIPTION("Ralink WiSoC Wireless LAN driver.");
257 MODULE_LICENSE("GPL");
258