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 	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
137 	.start			= rt2x00mac_start,
138 	.stop			= rt2x00mac_stop,
139 	.add_interface		= rt2x00mac_add_interface,
140 	.remove_interface	= rt2x00mac_remove_interface,
141 	.config			= rt2x00mac_config,
142 	.configure_filter	= rt2x00mac_configure_filter,
143 	.set_key		= rt2x00mac_set_key,
144 	.sw_scan_start		= rt2x00mac_sw_scan_start,
145 	.sw_scan_complete	= rt2x00mac_sw_scan_complete,
146 	.get_stats		= rt2x00mac_get_stats,
147 	.get_key_seq		= rt2800_get_key_seq,
148 	.set_rts_threshold	= rt2800_set_rts_threshold,
149 	.sta_add		= rt2800_sta_add,
150 	.sta_remove		= rt2800_sta_remove,
151 	.bss_info_changed	= rt2x00mac_bss_info_changed,
152 	.conf_tx		= rt2800_conf_tx,
153 	.get_tsf		= rt2800_get_tsf,
154 	.rfkill_poll		= rt2x00mac_rfkill_poll,
155 	.ampdu_action		= rt2800_ampdu_action,
156 	.flush			= rt2x00mac_flush,
157 	.get_survey		= rt2800_get_survey,
158 	.get_ringparam		= rt2x00mac_get_ringparam,
159 	.tx_frames_pending	= rt2x00mac_tx_frames_pending,
160 	.reconfig_complete	= rt2x00mac_reconfig_complete,
161 };
162 
163 static const struct rt2800_ops rt2800soc_rt2800_ops = {
164 	.register_read		= rt2x00mmio_register_read,
165 	.register_read_lock	= rt2x00mmio_register_read, /* same for SoCs */
166 	.register_write		= rt2x00mmio_register_write,
167 	.register_write_lock	= rt2x00mmio_register_write, /* same for SoCs */
168 	.register_multiread	= rt2x00mmio_register_multiread,
169 	.register_multiwrite	= rt2x00mmio_register_multiwrite,
170 	.regbusy_read		= rt2x00mmio_regbusy_read,
171 	.read_eeprom		= rt2800soc_read_eeprom,
172 	.hwcrypt_disabled	= rt2800soc_hwcrypt_disabled,
173 	.drv_write_firmware	= rt2800soc_write_firmware,
174 	.drv_init_registers	= rt2800mmio_init_registers,
175 	.drv_get_txwi		= rt2800mmio_get_txwi,
176 	.drv_get_dma_done	= rt2800mmio_get_dma_done,
177 };
178 
179 static const struct rt2x00lib_ops rt2800soc_rt2x00_ops = {
180 	.irq_handler		= rt2800mmio_interrupt,
181 	.txstatus_tasklet	= rt2800mmio_txstatus_tasklet,
182 	.pretbtt_tasklet	= rt2800mmio_pretbtt_tasklet,
183 	.tbtt_tasklet		= rt2800mmio_tbtt_tasklet,
184 	.rxdone_tasklet		= rt2800mmio_rxdone_tasklet,
185 	.autowake_tasklet	= rt2800mmio_autowake_tasklet,
186 	.probe_hw		= rt2800mmio_probe_hw,
187 	.get_firmware_name	= rt2800soc_get_firmware_name,
188 	.check_firmware		= rt2800soc_check_firmware,
189 	.load_firmware		= rt2800soc_load_firmware,
190 	.initialize		= rt2x00mmio_initialize,
191 	.uninitialize		= rt2x00mmio_uninitialize,
192 	.get_entry_state	= rt2800mmio_get_entry_state,
193 	.clear_entry		= rt2800mmio_clear_entry,
194 	.set_device_state	= rt2800soc_set_device_state,
195 	.rfkill_poll		= rt2800_rfkill_poll,
196 	.link_stats		= rt2800_link_stats,
197 	.reset_tuner		= rt2800_reset_tuner,
198 	.link_tuner		= rt2800_link_tuner,
199 	.gain_calibration	= rt2800_gain_calibration,
200 	.vco_calibration	= rt2800_vco_calibration,
201 	.watchdog		= rt2800_watchdog,
202 	.start_queue		= rt2800mmio_start_queue,
203 	.kick_queue		= rt2800mmio_kick_queue,
204 	.stop_queue		= rt2800mmio_stop_queue,
205 	.flush_queue		= rt2800mmio_flush_queue,
206 	.write_tx_desc		= rt2800mmio_write_tx_desc,
207 	.write_tx_data		= rt2800_write_tx_data,
208 	.write_beacon		= rt2800_write_beacon,
209 	.clear_beacon		= rt2800_clear_beacon,
210 	.fill_rxdone		= rt2800mmio_fill_rxdone,
211 	.config_shared_key	= rt2800_config_shared_key,
212 	.config_pairwise_key	= rt2800_config_pairwise_key,
213 	.config_filter		= rt2800_config_filter,
214 	.config_intf		= rt2800_config_intf,
215 	.config_erp		= rt2800_config_erp,
216 	.config_ant		= rt2800_config_ant,
217 	.config			= rt2800_config,
218 	.pre_reset_hw		= rt2800_pre_reset_hw,
219 };
220 
221 static const struct rt2x00_ops rt2800soc_ops = {
222 	.name			= KBUILD_MODNAME,
223 	.drv_data_size		= sizeof(struct rt2800_drv_data),
224 	.max_ap_intf		= 8,
225 	.eeprom_size		= EEPROM_SIZE,
226 	.rf_size		= RF_SIZE,
227 	.tx_queues		= NUM_TX_QUEUES,
228 	.queue_init		= rt2800mmio_queue_init,
229 	.lib			= &rt2800soc_rt2x00_ops,
230 	.drv			= &rt2800soc_rt2800_ops,
231 	.hw			= &rt2800soc_mac80211_ops,
232 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
233 	.debugfs		= &rt2800_rt2x00debug,
234 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
235 };
236 
237 static int rt2800soc_probe(struct platform_device *pdev)
238 {
239 	return rt2x00soc_probe(pdev, &rt2800soc_ops);
240 }
241 
242 static struct platform_driver rt2800soc_driver = {
243 	.driver		= {
244 		.name		= "rt2800_wmac",
245 		.mod_name	= KBUILD_MODNAME,
246 	},
247 	.probe		= rt2800soc_probe,
248 	.remove		= rt2x00soc_remove,
249 	.suspend	= rt2x00soc_suspend,
250 	.resume		= rt2x00soc_resume,
251 };
252 
253 module_platform_driver(rt2800soc_driver);
254 
255 MODULE_AUTHOR(DRV_PROJECT);
256 MODULE_VERSION(DRV_VERSION);
257 MODULE_DESCRIPTION("Ralink WiSoC Wireless LAN driver.");
258 MODULE_LICENSE("GPL");
259