1 /* ieee802154_rf2xx.c - ATMEL RF2XX IEEE 802.15.4 Driver */
2 
3 #define DT_DRV_COMPAT atmel_rf2xx
4 
5 /*
6  * Copyright (c) 2019-2020 Gerson Fernando Budke
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #define LOG_MODULE_NAME ieee802154_rf2xx
12 #define LOG_LEVEL CONFIG_IEEE802154_DRIVER_LOG_LEVEL
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
16 
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <zephyr/kernel.h>
22 #include <zephyr/arch/cpu.h>
23 #include <zephyr/debug/stack.h>
24 
25 #include <zephyr/device.h>
26 #include <zephyr/init.h>
27 #include <zephyr/net/net_if.h>
28 #include <zephyr/net/net_pkt.h>
29 
30 #include <zephyr/sys/byteorder.h>
31 #include <string.h>
32 #include <zephyr/random/random.h>
33 #include <zephyr/linker/sections.h>
34 #include <zephyr/sys/atomic.h>
35 
36 #include <zephyr/drivers/spi.h>
37 #include <zephyr/drivers/gpio.h>
38 
39 #include <zephyr/net/ieee802154_radio.h>
40 
41 #include "ieee802154_rf2xx.h"
42 #include "ieee802154_rf2xx_regs.h"
43 #include "ieee802154_rf2xx_iface.h"
44 
45 #if defined(CONFIG_NET_L2_OPENTHREAD)
46 #include <zephyr/net/openthread.h>
47 
48 #define RF2XX_OT_PSDU_LENGTH              1280
49 
50 #define RF2XX_ACK_FRAME_LEN               3
51 #define RF2XX_ACK_FRAME_TYPE              (2 << 0)
52 #define RF2XX_ACK_FRAME_PENDING_BIT       (1 << 4)
53 #define RF2XX_FRAME_CTRL_ACK_REQUEST_BIT  (1 << 5)
54 
55 static uint8_t rf2xx_ack_psdu[RF2XX_ACK_FRAME_LEN] = { 0 };
56 static struct net_buf rf2xx_ack_frame = {
57 	.data  = rf2xx_ack_psdu,
58 	.size  = RF2XX_ACK_FRAME_LEN,
59 	.len   = RF2XX_ACK_FRAME_LEN,
60 	.__buf = rf2xx_ack_psdu,
61 	.frags = NULL,
62 };
63 static struct net_pkt rf2xx_ack_pkt = {
64 	.buffer = &rf2xx_ack_frame,
65 	.cb = {
66 		.lqi = 80,
67 		.rssi = -40,
68 	}
69 };
70 #endif /* CONFIG_NET_L2_OPENTHREAD */
71 
72 /* Radio Transceiver ISR */
trx_isr_handler(const struct device * port,struct gpio_callback * cb,uint32_t pins)73 static inline void trx_isr_handler(const struct device *port,
74 				   struct gpio_callback *cb,
75 				   uint32_t pins)
76 {
77 	struct rf2xx_context *ctx = CONTAINER_OF(cb,
78 						 struct rf2xx_context,
79 						 irq_cb);
80 
81 	ARG_UNUSED(port);
82 	ARG_UNUSED(pins);
83 
84 	k_sem_give(&ctx->trx_isr_lock);
85 }
86 
rf2xx_trx_set_state(const struct device * dev,enum rf2xx_trx_state_cmd_t state)87 static void rf2xx_trx_set_state(const struct device *dev,
88 				enum rf2xx_trx_state_cmd_t state)
89 {
90 	do {
91 		rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG,
92 				      RF2XX_TRX_PHY_STATE_CMD_FORCE_TRX_OFF);
93 	} while (RF2XX_TRX_PHY_STATUS_TRX_OFF !=
94 		 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
95 		  RF2XX_TRX_PHY_STATUS_MASK));
96 
97 	do {
98 		rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG, state);
99 	} while (state !=
100 		 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
101 		  RF2XX_TRX_PHY_STATUS_MASK));
102 }
103 
rf2xx_trx_set_tx_state(const struct device * dev)104 static void rf2xx_trx_set_tx_state(const struct device *dev)
105 {
106 	uint8_t status;
107 
108 	/**
109 	 * Ensures that RX automatically ACK will be sent when requested.
110 	 * Datasheet: Chapter 7.2.3 RX_AACK_ON – Receive with Automatic ACK
111 	 * Datasheet: Figure 7-13. Timing Example of an RX_AACK Transaction
112 	 * for Slotted Operation.
113 	 *
114 	 * This will create a spin lock that wait transceiver be free from
115 	 * current receive frame process
116 	 */
117 	do {
118 		status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
119 			  RF2XX_TRX_PHY_STATUS_MASK);
120 	} while (status == RF2XX_TRX_PHY_STATUS_BUSY_RX_AACK ||
121 		 status == RF2XX_TRX_PHY_STATUS_STATE_TRANSITION);
122 
123 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
124 	rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
125 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TX_ARET_ON);
126 }
127 
rf2xx_trx_set_rx_state(const struct device * dev)128 static void rf2xx_trx_set_rx_state(const struct device *dev)
129 {
130 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
131 	rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
132 	/**
133 	 * Set extended RX mode
134 	 * Datasheet: chapter 7.2 Extended Operating Mode
135 	 */
136 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_RX_AACK_ON);
137 }
138 
rf2xx_set_rssi_base(const struct device * dev,uint16_t channel)139 static void rf2xx_set_rssi_base(const struct device *dev, uint16_t channel)
140 {
141 	struct rf2xx_context *ctx = dev->data;
142 	int8_t base;
143 
144 	if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
145 		base = channel == 0
146 				? RF2XX_RSSI_BPSK_20
147 				: RF2XX_RSSI_BPSK_40;
148 	} else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
149 		base = channel == 0
150 				? RF2XX_RSSI_OQPSK_SIN_RC_100
151 				: RF2XX_RSSI_OQPSK_SIN_250;
152 	} else {
153 		base = RF2XX_RSSI_OQPSK_RC_250;
154 	}
155 
156 	ctx->trx_rssi_base = base;
157 }
158 
rf2xx_trx_rx(const struct device * dev)159 static void rf2xx_trx_rx(const struct device *dev)
160 {
161 	struct rf2xx_context *ctx = dev->data;
162 	struct net_pkt *pkt = NULL;
163 	uint8_t rx_buf[RX2XX_MAX_FRAME_SIZE];
164 	uint8_t pkt_len;
165 	uint8_t frame_len;
166 	uint8_t trac;
167 
168 	/*
169 	 * The rf2xx frame buffer can have length > 128 bytes. The
170 	 * net_pkt_rx_alloc_with_buffer allocates max value of 128 bytes.
171 	 *
172 	 * This obligate the driver to have rx_buf statically allocated with
173 	 * RX2XX_MAX_FRAME_SIZE.
174 	 */
175 	if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
176 		pkt_len = ctx->rx_phr;
177 	} else {
178 		rf2xx_iface_frame_read(dev, rx_buf, RX2XX_FRAME_HEADER_SIZE);
179 		pkt_len = rx_buf[RX2XX_FRAME_PHR_INDEX];
180 	}
181 
182 	if (!ctx->promiscuous && pkt_len < RX2XX_FRAME_MIN_PHR_SIZE) {
183 		LOG_ERR("Invalid RX frame length");
184 		return;
185 	}
186 
187 	frame_len = RX2XX_FRAME_HEADER_SIZE + pkt_len +
188 		    RX2XX_FRAME_FOOTER_SIZE;
189 
190 	rf2xx_iface_frame_read(dev, rx_buf, frame_len);
191 
192 	if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
193 		trac = rx_buf[pkt_len + RX2XX_FRAME_TRAC_INDEX];
194 		trac = (trac >> RF2XX_RX_TRAC_STATUS) & RF2XX_RX_TRAC_BIT_MASK;
195 
196 		ctx->pkt_ed = rx_buf[pkt_len + RX2XX_FRAME_ED_INDEX];
197 	} else {
198 		trac = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATE_REG)
199 			>> RF2XX_TRAC_STATUS) & RF2XX_TRAC_BIT_MASK;
200 
201 		ctx->pkt_ed = (rf2xx_iface_reg_read(dev, RF2XX_PHY_RSSI_REG)
202 			       >> RF2XX_RSSI) & RF2XX_RSSI_MASK;
203 	}
204 	ctx->pkt_lqi = rx_buf[pkt_len + RX2XX_FRAME_LQI_INDEX];
205 
206 	if (!ctx->promiscuous && trac == RF2XX_TRX_PHY_STATE_TRAC_INVALID) {
207 		LOG_ERR("Invalid RX frame");
208 		return;
209 	}
210 
211 	if (!IS_ENABLED(CONFIG_IEEE802154_L2_PKT_INCL_FCS) && pkt_len >= RX2XX_FRAME_FCS_LENGTH) {
212 		pkt_len -= RX2XX_FRAME_FCS_LENGTH;
213 	}
214 
215 	pkt = net_pkt_rx_alloc_with_buffer(ctx->iface, pkt_len,
216 					   AF_UNSPEC, 0, K_NO_WAIT);
217 	if (!pkt) {
218 		LOG_ERR("No RX buffer available");
219 		return;
220 	}
221 
222 	memcpy(pkt->buffer->data, rx_buf + RX2XX_FRAME_HEADER_SIZE, pkt_len);
223 	net_buf_add(pkt->buffer, pkt_len);
224 	net_pkt_set_ieee802154_lqi(pkt, ctx->pkt_lqi);
225 	net_pkt_set_ieee802154_rssi_dbm(pkt, ctx->pkt_ed + ctx->trx_rssi_base);
226 
227 	LOG_DBG("Caught a packet (%02X) (LQI: %02X, RSSI: %d, ED: %02X)",
228 		pkt_len, ctx->pkt_lqi, ctx->trx_rssi_base + ctx->pkt_ed,
229 		ctx->pkt_ed);
230 
231 	if (net_recv_data(ctx->iface, pkt) < 0) {
232 		LOG_DBG("RX Packet dropped by NET stack");
233 		net_pkt_unref(pkt);
234 		return;
235 	}
236 
237 	if (LOG_LEVEL >= LOG_LEVEL_DBG) {
238 		log_stack_usage(&ctx->trx_thread);
239 	}
240 }
241 
rf2xx_process_rx_frame(const struct device * dev)242 static void rf2xx_process_rx_frame(const struct device *dev)
243 {
244 	struct rf2xx_context *ctx = dev->data;
245 
246 	/*
247 	 * NOTE: In promiscuous mode invalid frames will be processed.
248 	 */
249 
250 	if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
251 		rf2xx_trx_rx(dev);
252 	} else {
253 		/* Ensures that automatically ACK will be sent
254 		 * when requested
255 		 */
256 		while (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) ==
257 		       RF2XX_TRX_PHY_STATUS_BUSY_RX_AACK) {
258 			;
259 		}
260 
261 		/* Set PLL_ON to avoid transceiver receive
262 		 * new data until finish reading process
263 		 */
264 		rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_PLL_ON);
265 		rf2xx_trx_rx(dev);
266 		rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_RX_AACK_ON);
267 	}
268 }
269 
rf2xx_process_tx_frame(const struct device * dev)270 static void rf2xx_process_tx_frame(const struct device *dev)
271 {
272 	struct rf2xx_context *ctx = dev->data;
273 
274 	ctx->trx_trac = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATE_REG) >>
275 			 RF2XX_TRAC_STATUS) & RF2XX_TRAC_BIT_MASK;
276 	k_sem_give(&ctx->trx_tx_sync);
277 	rf2xx_trx_set_rx_state(dev);
278 }
279 
rf2xx_process_trx_end(const struct device * dev)280 static void rf2xx_process_trx_end(const struct device *dev)
281 {
282 	uint8_t trx_status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
283 			   RF2XX_TRX_PHY_STATUS_MASK);
284 
285 	if (trx_status == RF2XX_TRX_PHY_STATUS_TX_ARET_ON) {
286 		rf2xx_process_tx_frame(dev);
287 	} else {
288 		rf2xx_process_rx_frame(dev);
289 	}
290 }
291 
rf2xx_thread_main(void * p1,void * p2,void * p3)292 static void rf2xx_thread_main(void *p1, void *p2, void *p3)
293 {
294 	ARG_UNUSED(p2);
295 	ARG_UNUSED(p3);
296 
297 	struct rf2xx_context *ctx = p1;
298 	uint8_t isr_status;
299 
300 	while (true) {
301 		k_sem_take(&ctx->trx_isr_lock, K_FOREVER);
302 
303 		isr_status = rf2xx_iface_reg_read(ctx->dev,
304 						  RF2XX_IRQ_STATUS_REG);
305 
306 		/*
307 		 *  IRQ_7 (BAT_LOW) Indicates a supply voltage below the
308 		 *    programmed threshold. 9.5.4
309 		 *  IRQ_6 (TRX_UR) Indicates a Frame Buffer access
310 		 *    violation. 9.3.3
311 		 *  IRQ_5 (AMI) Indicates address matching. 8.2
312 		 *  IRQ_4 (CCA_ED_DONE) Multi-functional interrupt:
313 		 *   1. AWAKE_END: 7.1.2.5
314 		 *      • Indicates finished transition to TRX_OFF state
315 		 *        from P_ON, SLEEP, DEEP_SLEEP, or RESET state.
316 		 *   2. CCA_ED_DONE: 8.5.4
317 		 *      • Indicates the end of a CCA or ED
318 		 *        measurement. 8.6.4
319 		 *  IRQ_3 (TRX_END)
320 		 *    RX: Indicates the completion of a frame
321 		 *      reception. 7.1.3
322 		 *    TX: Indicates the completion of a frame
323 		 *      transmission. 7.1.3
324 		 *  IRQ_2 (RX_START) Indicates the start of a PSDU
325 		 *    reception; the AT86RF233 state changed to BUSY_RX;
326 		 *    the PHR can be read from Frame Buffer. 7.1.3
327 		 *  IRQ_1 (PLL_UNLOCK) Indicates PLL unlock. If the radio
328 		 *    transceiver is in BUSY_TX / BUSY_TX_ARET state, the
329 		 *    PA is turned off immediately. 9.7.5
330 		 *  IRQ_0 (PLL_LOCK) Indicates PLL lock.
331 		 */
332 		if (isr_status & (1 << RF2XX_RX_START)) {
333 			if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
334 				rf2xx_iface_sram_read(ctx->dev, 0,
335 						      &ctx->rx_phr, 1);
336 			}
337 		}
338 		if (isr_status & (1 << RF2XX_TRX_END)) {
339 			rf2xx_process_trx_end(ctx->dev);
340 		}
341 	}
342 }
343 
get_mac(const struct device * dev)344 static inline uint8_t *get_mac(const struct device *dev)
345 {
346 	const struct rf2xx_config *conf = dev->config;
347 	struct rf2xx_context *ctx = dev->data;
348 
349 	if (!conf->has_mac) {
350 		sys_rand_get(ctx->mac_addr, sizeof(ctx->mac_addr));
351 	}
352 
353 	/*
354 	 * Clear bit 0 to ensure it isn't a multicast address and set
355 	 * bit 1 to indicate address is locally administered and may
356 	 * not be globally unique.
357 	 */
358 	ctx->mac_addr[0] = (ctx->mac_addr[0] & ~0x01) | 0x02;
359 
360 	return ctx->mac_addr;
361 }
362 
rf2xx_get_capabilities(const struct device * dev)363 static enum ieee802154_hw_caps rf2xx_get_capabilities(const struct device *dev)
364 {
365 	LOG_DBG("HW Caps");
366 
367 	return IEEE802154_HW_FCS |
368 	       IEEE802154_HW_PROMISC |
369 	       IEEE802154_HW_FILTER |
370 	       IEEE802154_HW_CSMA |
371 	       IEEE802154_HW_RETRANSMISSION |
372 	       IEEE802154_HW_TX_RX_ACK |
373 	       IEEE802154_HW_RX_TX_ACK;
374 }
375 
rf2xx_configure_sub_channel(const struct device * dev,uint16_t channel)376 static int rf2xx_configure_sub_channel(const struct device *dev, uint16_t channel)
377 {
378 	struct rf2xx_context *ctx = dev->data;
379 	uint8_t reg;
380 	uint8_t cc_mask;
381 
382 	if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
383 		cc_mask = channel == 0
384 				   ? RF2XX_CC_BPSK_20
385 				   : RF2XX_CC_BPSK_40;
386 	} else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
387 		cc_mask = channel == 0
388 				   ? RF2XX_CC_OQPSK_SIN_RC_100
389 				   : RF2XX_CC_OQPSK_SIN_250;
390 	} else {
391 		cc_mask = RF2XX_CC_OQPSK_RC_250;
392 	}
393 
394 	reg = rf2xx_iface_reg_read(dev, RF2XX_TRX_CTRL_2_REG)
395 	    & ~RF2XX_SUB_CHANNEL_MASK;
396 	rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, reg | cc_mask);
397 
398 	return 0;
399 }
400 
rf2xx_configure_trx_path(const struct device * dev)401 static int rf2xx_configure_trx_path(const struct device *dev)
402 {
403 	struct rf2xx_context *ctx = dev->data;
404 	uint8_t reg;
405 	uint8_t gc_tx_offset;
406 
407 	if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
408 		gc_tx_offset = 0x03;
409 	} else {
410 		gc_tx_offset = 0x02;
411 	}
412 
413 	reg = rf2xx_iface_reg_read(dev, RF2XX_RF_CTRL_0_REG)
414 	    & ~RF2XX_GC_TX_OFFS_MASK;
415 	rf2xx_iface_reg_write(dev, RF2XX_RF_CTRL_0_REG, reg | gc_tx_offset);
416 
417 	return 0;
418 }
419 
rf2xx_cca(const struct device * dev)420 static int rf2xx_cca(const struct device *dev)
421 {
422 	ARG_UNUSED(dev);
423 
424 	LOG_DBG("CCA");
425 
426 	return 0;
427 }
428 
rf2xx_set_channel(const struct device * dev,uint16_t channel)429 static int rf2xx_set_channel(const struct device *dev, uint16_t channel)
430 {
431 	struct rf2xx_context *ctx = dev->data;
432 	uint8_t reg;
433 
434 	LOG_DBG("Set Channel %d", channel);
435 
436 	if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
437 		if ((ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915
438 		     || ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915)
439 		    && channel > 10) {
440 			LOG_ERR("Unsupported channel %u", channel);
441 			return channel > 26 ? -EINVAL : -ENOTSUP;
442 		}
443 		if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780 &&
444 		    channel > 3) {
445 			LOG_ERR("Unsupported channel %u", channel);
446 			return channel > 7 ? -EINVAL : -ENOTSUP;
447 		}
448 
449 		rf2xx_configure_sub_channel(dev, channel);
450 		rf2xx_configure_trx_path(dev);
451 		rf2xx_set_rssi_base(dev, channel);
452 	} else {
453 		/* 2.4G O-QPSK, channel page zero */
454 		if (channel < 11 || channel > 26) {
455 			LOG_ERR("Unsupported channel %u", channel);
456 			return channel < 11 ? -ENOTSUP : -EINVAL;
457 		}
458 	}
459 
460 	reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_CC_CCA_REG) & ~0x1f;
461 	rf2xx_iface_reg_write(dev, RF2XX_PHY_CC_CCA_REG, reg | channel);
462 
463 	return 0;
464 }
465 
rf2xx_set_txpower(const struct device * dev,int16_t dbm)466 static int rf2xx_set_txpower(const struct device *dev, int16_t dbm)
467 {
468 	const struct rf2xx_config *conf = dev->config;
469 	struct rf2xx_context *ctx = dev->data;
470 	float min, max, step;
471 	uint8_t reg;
472 	uint8_t idx;
473 	uint8_t val;
474 
475 	LOG_DBG("Try set Power to %d", dbm);
476 
477 	/**
478 	 * if table size is equal 1 the code assumes a table was not defined. In
479 	 * this case the transceiver PHY_TX_PWR register will be set with value
480 	 * zero. This is a safe value for all variants and represents an output
481 	 * power above 0 dBm.
482 	 *
483 	 * Note: This is a special case too which avoid division by zero when
484 	 * computing the step variable.
485 	 */
486 	if (conf->tx_pwr_table_size == 1) {
487 		rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, 0);
488 
489 		return 0;
490 	}
491 
492 	min = conf->tx_pwr_min[1];
493 	if (conf->tx_pwr_min[0] == 0x01) {
494 		min *= -1.0f;
495 	}
496 
497 	max = conf->tx_pwr_max[1];
498 	if (conf->tx_pwr_max[0] == 0x01) {
499 		min *= -1.0f;
500 	}
501 
502 	step = (max - min) / ((float)conf->tx_pwr_table_size - 1.0f);
503 
504 	if (step == 0.0f) {
505 		step = 1.0f;
506 	}
507 
508 	LOG_DBG("Tx-power values: min %f, max %f, step %f, entries %d",
509 		(double)min, (double)max, (double)step, conf->tx_pwr_table_size);
510 
511 	if (dbm < min) {
512 		LOG_INF("TX-power %d dBm below min of %f dBm, using %f dBm",
513 			dbm, (double)min, (double)max);
514 		dbm = min;
515 	} else if (dbm > max) {
516 		LOG_INF("TX-power %d dBm above max of %f dBm, using %f dBm",
517 			dbm, (double)min, (double)max);
518 		dbm = max;
519 	}
520 
521 	idx = abs((int) (((float)(dbm - max) / step)));
522 	LOG_DBG("Tx-power idx: %d", idx);
523 
524 	if (idx >= conf->tx_pwr_table_size) {
525 		idx = conf->tx_pwr_table_size - 1;
526 	}
527 
528 	val = conf->tx_pwr_table[idx];
529 
530 	if (ctx->trx_model != RF2XX_TRX_MODEL_212) {
531 		reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_TX_PWR_REG) & 0xf0;
532 		val = reg + (val & 0x0f);
533 	}
534 
535 	LOG_DBG("Tx-power normalized: %d dBm, PHY_TX_PWR 0x%02x, idx %d",
536 		dbm, val, idx);
537 
538 	rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, val);
539 
540 	return 0;
541 }
542 
rf2xx_set_ieee_addr(const struct device * dev,bool set,const uint8_t * ieee_addr)543 static int rf2xx_set_ieee_addr(const struct device *dev, bool set,
544 			       const uint8_t *ieee_addr)
545 {
546 	const uint8_t *ptr_to_reg = ieee_addr;
547 
548 	LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
549 		ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
550 		ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
551 
552 	if (set) {
553 		for (uint8_t i = 0; i < 8; i++, ptr_to_reg++) {
554 			rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
555 					      *ptr_to_reg);
556 		}
557 	} else {
558 		for (uint8_t i = 0; i < 8; i++) {
559 			rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
560 					      0);
561 		}
562 	}
563 
564 	return 0;
565 }
566 
rf2xx_set_short_addr(const struct device * dev,bool set,uint16_t short_addr)567 static int rf2xx_set_short_addr(const struct device *dev, bool set,
568 				uint16_t short_addr)
569 {
570 	uint8_t short_addr_le[2] = { 0xFF, 0xFF };
571 
572 	if (set) {
573 		sys_put_le16(short_addr, short_addr_le);
574 	}
575 
576 	rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_0_REG, short_addr_le[0]);
577 	rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_1_REG, short_addr_le[1]);
578 	rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_0_REG,
579 			      short_addr_le[0] + short_addr_le[1]);
580 
581 	LOG_DBG("Short Address: 0x%02X%02X", short_addr_le[1],
582 		short_addr_le[0]);
583 
584 	return 0;
585 }
586 
rf2xx_set_pan_id(const struct device * dev,bool set,uint16_t pan_id)587 static int rf2xx_set_pan_id(const struct device *dev, bool set,
588 			    uint16_t pan_id)
589 {
590 	uint8_t pan_id_le[2] = { 0xFF, 0xFF };
591 
592 	if (set) {
593 		sys_put_le16(pan_id, pan_id_le);
594 	}
595 
596 	rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_0_REG, pan_id_le[0]);
597 	rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_1_REG, pan_id_le[1]);
598 
599 	LOG_DBG("Pan Id: 0x%02X%02X", pan_id_le[1], pan_id_le[0]);
600 
601 	return 0;
602 }
603 
rf2xx_filter(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)604 static int rf2xx_filter(const struct device *dev,
605 			bool set, enum ieee802154_filter_type type,
606 			const struct ieee802154_filter *filter)
607 {
608 	LOG_DBG("Applying filter %u", type);
609 
610 	if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
611 		return rf2xx_set_ieee_addr(dev, set, filter->ieee_addr);
612 	} else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
613 		return rf2xx_set_short_addr(dev, set, filter->short_addr);
614 	} else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
615 		return rf2xx_set_pan_id(dev, set, filter->pan_id);
616 	}
617 
618 	return -ENOTSUP;
619 }
620 
621 #if defined(CONFIG_NET_L2_OPENTHREAD)
rf2xx_handle_ack(struct rf2xx_context * ctx,struct net_buf * frag)622 static void rf2xx_handle_ack(struct rf2xx_context *ctx, struct net_buf *frag)
623 {
624 	if ((frag->data[0] & RF2XX_FRAME_CTRL_ACK_REQUEST_BIT) == 0) {
625 		return;
626 	}
627 
628 	rf2xx_ack_psdu[0] = RF2XX_ACK_FRAME_TYPE;
629 	rf2xx_ack_psdu[2] = frag->data[2];
630 
631 	if (ctx->trx_trac == RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING) {
632 		rf2xx_ack_psdu[0] |= RF2XX_ACK_FRAME_PENDING_BIT;
633 	}
634 
635 	net_pkt_cursor_init(&rf2xx_ack_pkt);
636 
637 	if (ieee802154_handle_ack(ctx->iface, &rf2xx_ack_pkt) != NET_OK) {
638 		LOG_INF("ACK packet not handled.");
639 	}
640 }
641 #else
642 	#define rf2xx_handle_ack(...)
643 #endif
644 
rf2xx_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)645 static int rf2xx_tx(const struct device *dev,
646 		    enum ieee802154_tx_mode mode,
647 		    struct net_pkt *pkt,
648 		    struct net_buf *frag)
649 {
650 	ARG_UNUSED(pkt);
651 
652 	struct rf2xx_context *ctx = dev->data;
653 	int response = 0;
654 
655 	LOG_DBG("TX");
656 
657 	if (ctx->tx_mode != mode) {
658 		switch (mode) {
659 		case IEEE802154_TX_MODE_DIRECT:
660 			/* skip retries & csma/ca algorithm */
661 			rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x0E);
662 			break;
663 		case IEEE802154_TX_MODE_CSMA_CA:
664 			/* backoff maxBE = 5, minBE = 3 */
665 			rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x53);
666 			/* max frame retries = 3, csma/ca retries = 4 */
667 			rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x38);
668 			break;
669 		case IEEE802154_TX_MODE_CCA:
670 			/* backoff period = 0 */
671 			rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x00);
672 			/* no frame retries & no csma/ca retries */
673 			rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x00);
674 			break;
675 		case IEEE802154_TX_MODE_TXTIME:
676 		case IEEE802154_TX_MODE_TXTIME_CCA:
677 		default:
678 			NET_ERR("TX mode %d not supported", mode);
679 			return -ENOTSUP;
680 		}
681 
682 		ctx->tx_mode = mode;
683 	}
684 
685 	rf2xx_trx_set_tx_state(dev);
686 	rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
687 
688 	k_sem_reset(&ctx->trx_tx_sync);
689 	rf2xx_iface_frame_write(dev, frag->data, frag->len);
690 	rf2xx_iface_phy_tx_start(dev);
691 	k_sem_take(&ctx->trx_tx_sync, K_FOREVER);
692 
693 	switch (ctx->trx_trac) {
694 	/* Channel is still busy after attempting MAX_CSMA_RETRIES of
695 	 * CSMA-CA
696 	 */
697 	case RF2XX_TRX_PHY_STATE_TRAC_CHANNEL_ACCESS_FAILED:
698 		response = -EBUSY;
699 		break;
700 	/* No acknowledgment frames were received during all retry
701 	 * attempts
702 	 */
703 	case RF2XX_TRX_PHY_STATE_TRAC_NO_ACK:
704 		response = -EAGAIN;
705 		break;
706 	/* Transaction not yet finished */
707 	case RF2XX_TRX_PHY_STATE_TRAC_INVALID:
708 		response = -EINTR;
709 		break;
710 	/* RF2XX_TRX_PHY_STATE_TRAC_SUCCESS:
711 	 *  The transaction was responded to by a valid ACK, or, if no
712 	 *  ACK is requested, after a successful frame transmission.
713 	 *
714 	 * RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING:
715 	 * Equivalent to SUCCESS and indicating that the “Frame
716 	 * Pending” bit (see Section 8.1.2.2) of the received
717 	 * acknowledgment frame was set.
718 	 */
719 	default:
720 		rf2xx_handle_ack(ctx, frag);
721 		break;
722 	}
723 
724 	return response;
725 }
726 
rf2xx_start(const struct device * dev)727 static int rf2xx_start(const struct device *dev)
728 {
729 	const struct rf2xx_config *conf = dev->config;
730 
731 	LOG_DBG("Start");
732 
733 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
734 	rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
735 	gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
736 					GPIO_INT_EDGE_TO_ACTIVE);
737 	rf2xx_trx_set_rx_state(dev);
738 
739 	return 0;
740 }
741 
rf2xx_stop(const struct device * dev)742 static int rf2xx_stop(const struct device *dev)
743 {
744 	const struct rf2xx_config *conf = dev->config;
745 
746 	LOG_DBG("Stop");
747 
748 	gpio_pin_interrupt_configure_dt(&conf->irq_gpio, GPIO_INT_DISABLE);
749 	rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
750 	rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
751 
752 	return 0;
753 }
754 
rf2xx_pan_coord_set(const struct device * dev,bool pan_coordinator)755 static int rf2xx_pan_coord_set(const struct device *dev, bool pan_coordinator)
756 {
757 	uint8_t reg;
758 
759 	if (pan_coordinator) {
760 		reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
761 		reg |= (1 << RF2XX_AACK_I_AM_COORD);
762 		rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
763 	} else {
764 		reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
765 		reg &= ~(1 << RF2XX_AACK_I_AM_COORD);
766 		rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
767 	}
768 
769 	return 0;
770 }
771 
rf2xx_promiscuous_set(const struct device * dev,bool promiscuous)772 static int rf2xx_promiscuous_set(const struct device *dev, bool promiscuous)
773 {
774 	struct rf2xx_context *ctx = dev->data;
775 	uint8_t reg;
776 
777 	ctx->promiscuous = promiscuous;
778 
779 	if (promiscuous) {
780 		reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
781 		reg |= (1 << RF2XX_AACK_PROM_MODE);
782 		rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
783 
784 		reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
785 		reg |= (1 << RF2XX_AACK_DIS_ACK);
786 		rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
787 	} else {
788 		reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
789 		reg &= ~(1 << RF2XX_AACK_PROM_MODE);
790 		rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
791 
792 		reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
793 		reg &= ~(1 << RF2XX_AACK_DIS_ACK);
794 		rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
795 	}
796 
797 	return 0;
798 }
799 
rf2xx_configure(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)800 int rf2xx_configure(const struct device *dev,
801 		    enum ieee802154_config_type type,
802 		    const struct ieee802154_config *config)
803 {
804 	int ret = -EINVAL;
805 
806 	LOG_DBG("Configure %d", type);
807 
808 	switch (type) {
809 	case IEEE802154_CONFIG_AUTO_ACK_FPB:
810 	case IEEE802154_CONFIG_ACK_FPB:
811 		break;
812 
813 	case IEEE802154_CONFIG_PAN_COORDINATOR:
814 		ret = rf2xx_pan_coord_set(dev, config->pan_coordinator);
815 		break;
816 
817 	case IEEE802154_CONFIG_PROMISCUOUS:
818 		ret = rf2xx_promiscuous_set(dev, config->promiscuous);
819 		break;
820 
821 	case IEEE802154_CONFIG_EVENT_HANDLER:
822 	default:
823 		break;
824 	}
825 
826 	return ret;
827 }
828 
rf2xx_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)829 static int rf2xx_attr_get(const struct device *dev, enum ieee802154_attr attr,
830 			   struct ieee802154_attr_value *value)
831 {
832 	struct rf2xx_context *ctx = dev->data;
833 
834 	switch (attr) {
835 	case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES:
836 		value->phy_supported_channel_pages = ctx->cc_page;
837 		return 0;
838 
839 	case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES:
840 		value->phy_supported_channels = &ctx->cc_channels;
841 		return 0;
842 
843 	default:
844 		return -ENOENT;
845 	}
846 }
847 
power_on_and_setup(const struct device * dev)848 static int power_on_and_setup(const struct device *dev)
849 {
850 	const struct rf2xx_config *conf = dev->config;
851 	struct rf2xx_context *ctx = dev->data;
852 	uint8_t config;
853 
854 	rf2xx_iface_phy_rst(dev);
855 
856 	/* Sync transceiver state */
857 	do {
858 		rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG,
859 				      RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
860 	} while (RF2XX_TRX_PHY_STATUS_TRX_OFF !=
861 		 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
862 		  RF2XX_TRX_PHY_STATUS_MASK));
863 
864 	/* get device identification */
865 	ctx->trx_model = rf2xx_iface_reg_read(dev, RF2XX_PART_NUM_REG);
866 	ctx->trx_version = rf2xx_iface_reg_read(dev, RF2XX_VERSION_NUM_REG);
867 
868 	/**
869 	 * Valid transceiver are:
870 	 *  231-Rev-A (Version 0x02)
871 	 *  232-Rev-A (Version 0x02)
872 	 *  233-Rev-A (Version 0x01) (Warning)
873 	 *  233-Rev-B (Version 0x02)
874 	 */
875 	if (ctx->trx_model <= RF2XX_TRX_MODEL_230) {
876 		LOG_DBG("Invalid or not supported transceiver");
877 		return -ENODEV;
878 	}
879 
880 	if (ctx->trx_model == RF2XX_TRX_MODEL_233 && ctx->trx_version == 0x01) {
881 		LOG_DBG("Transceiver is old and unstable release");
882 	}
883 
884 	/* Set RSSI base */
885 	if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
886 		ctx->trx_rssi_base = -100;
887 	} else if (ctx->trx_model == RF2XX_TRX_MODEL_233) {
888 		ctx->trx_rssi_base = -94;
889 	} else if (ctx->trx_model == RF2XX_TRX_MODEL_231) {
890 		ctx->trx_rssi_base = -91;
891 	} else {
892 		ctx->trx_rssi_base = -90;
893 	}
894 
895 	/* Disable All Features of TRX_CTRL_0 */
896 	config = 0;
897 	rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_0_REG, config);
898 
899 	/* Configure PHY behaviour */
900 	config = (1 << RF2XX_TX_AUTO_CRC_ON) |
901 		 (3 << RF2XX_SPI_CMD_MODE) |
902 		 (1 << RF2XX_IRQ_MASK_MODE);
903 	rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_1_REG, config);
904 
905 	config = (1 << RF2XX_RX_SAFE_MODE);
906 	if (ctx->trx_model != RF2XX_TRX_MODEL_232) {
907 		config |= (1 << RF2XX_OQPSK_SCRAM_EN);
908 	}
909 	rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, config);
910 
911 	if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
912 		rf2xx_configure_trx_path(dev);
913 		rf2xx_iface_reg_write(dev, RF2XX_CC_CTRL_1_REG, 0);
914 	}
915 
916 	ctx->tx_mode = IEEE802154_TX_MODE_CSMA_CA;
917 	ctx->promiscuous = false;
918 
919 	/* Configure INT behaviour */
920 	config = (1 << RF2XX_RX_START) |
921 		 (1 << RF2XX_TRX_END);
922 	rf2xx_iface_reg_write(dev, RF2XX_IRQ_MASK_REG, config);
923 
924 	gpio_init_callback(&ctx->irq_cb, trx_isr_handler,
925 			   BIT(conf->irq_gpio.pin));
926 
927 	if (gpio_add_callback(conf->irq_gpio.port, &ctx->irq_cb) < 0) {
928 		LOG_ERR("Could not set IRQ callback.");
929 		return -ENXIO;
930 	}
931 
932 	return 0;
933 }
934 
configure_gpios(const struct device * dev)935 static inline int configure_gpios(const struct device *dev)
936 {
937 	const struct rf2xx_config *conf = dev->config;
938 
939 	/* Chip IRQ line */
940 	if (!gpio_is_ready_dt(&conf->irq_gpio)) {
941 		LOG_ERR("IRQ GPIO device not ready");
942 		return -ENODEV;
943 	}
944 	gpio_pin_configure_dt(&conf->irq_gpio, GPIO_INPUT);
945 	gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
946 					GPIO_INT_EDGE_TO_ACTIVE);
947 
948 	/* Chip RESET line */
949 	if (!gpio_is_ready_dt(&conf->reset_gpio)) {
950 		LOG_ERR("RESET GPIO device not ready");
951 		return -ENODEV;
952 	}
953 	gpio_pin_configure_dt(&conf->reset_gpio, GPIO_OUTPUT_INACTIVE);
954 
955 	/* Chip SLPTR line */
956 	if (!gpio_is_ready_dt(&conf->slptr_gpio)) {
957 		LOG_ERR("SLPTR GPIO device not ready");
958 		return -ENODEV;
959 	}
960 	gpio_pin_configure_dt(&conf->slptr_gpio, GPIO_OUTPUT_INACTIVE);
961 
962 	/* Chip DIG2 line (Optional feature) */
963 	if (conf->dig2_gpio.port != NULL) {
964 		if (!gpio_is_ready_dt(&conf->dig2_gpio)) {
965 			LOG_ERR("DIG2 GPIO device not ready");
966 			return -ENODEV;
967 		}
968 		LOG_INF("Optional instance of %s device activated",
969 			conf->dig2_gpio.port->name);
970 		gpio_pin_configure_dt(&conf->dig2_gpio, GPIO_INPUT);
971 		gpio_pin_interrupt_configure_dt(&conf->dig2_gpio,
972 						GPIO_INT_EDGE_TO_ACTIVE);
973 	}
974 
975 	/* Chip CLKM line (Optional feature) */
976 	if (conf->clkm_gpio.port != NULL) {
977 		if (!gpio_is_ready_dt(&conf->clkm_gpio)) {
978 			LOG_ERR("CLKM GPIO device not ready");
979 			return -ENODEV;
980 		}
981 		LOG_INF("Optional instance of %s device activated",
982 			conf->clkm_gpio.port->name);
983 		gpio_pin_configure_dt(&conf->clkm_gpio, GPIO_INPUT);
984 	}
985 
986 	return 0;
987 }
988 
configure_spi(const struct device * dev)989 static inline int configure_spi(const struct device *dev)
990 {
991 	const struct rf2xx_config *conf = dev->config;
992 
993 	if (!spi_is_ready_dt(&conf->spi)) {
994 		LOG_ERR("SPI bus %s is not ready",
995 			conf->spi.bus->name);
996 		return -ENODEV;
997 	}
998 
999 	return 0;
1000 }
1001 
rf2xx_init(const struct device * dev)1002 static int rf2xx_init(const struct device *dev)
1003 {
1004 	struct rf2xx_context *ctx = dev->data;
1005 	const struct rf2xx_config *conf = dev->config;
1006 	char thread_name[20];
1007 
1008 	LOG_DBG("\nInitialize RF2XX Transceiver\n");
1009 
1010 	ctx->dev = dev;
1011 
1012 	k_sem_init(&ctx->trx_tx_sync, 0, 1);
1013 	k_sem_init(&ctx->trx_isr_lock, 0, 1);
1014 
1015 	if (configure_gpios(dev) != 0) {
1016 		LOG_ERR("Configuring GPIOS failed");
1017 		return -EIO;
1018 	}
1019 
1020 	if (configure_spi(dev) != 0) {
1021 		LOG_ERR("Configuring SPI failed");
1022 		return -EIO;
1023 	}
1024 
1025 	LOG_DBG("GPIO and SPI configured");
1026 
1027 	if (power_on_and_setup(dev) != 0) {
1028 		LOG_ERR("Configuring RF2XX failed");
1029 		return -EIO;
1030 	}
1031 
1032 	LOG_DBG("RADIO configured");
1033 
1034 	k_thread_create(&ctx->trx_thread,
1035 			ctx->trx_stack,
1036 			CONFIG_IEEE802154_RF2XX_RX_STACK_SIZE,
1037 			rf2xx_thread_main,
1038 			ctx, NULL, NULL,
1039 			K_PRIO_COOP(2), 0, K_NO_WAIT);
1040 
1041 	snprintk(thread_name, sizeof(thread_name),
1042 		 "rf2xx_trx [%d]", conf->inst);
1043 	k_thread_name_set(&ctx->trx_thread, thread_name);
1044 
1045 	LOG_DBG("Thread OK");
1046 
1047 	return 0;
1048 }
1049 
rf2xx_iface_init(struct net_if * iface)1050 static void rf2xx_iface_init(struct net_if *iface)
1051 {
1052 	const struct device *dev = net_if_get_device(iface);
1053 	struct rf2xx_context *ctx = dev->data;
1054 	uint8_t *mac = get_mac(dev);
1055 
1056 	net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
1057 
1058 	ctx->iface = iface;
1059 
1060 	if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
1061 		if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915 ||
1062 		    ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
1063 			ctx->cc_range.from_channel = 0U;
1064 			ctx->cc_range.to_channel = 10U;
1065 		} else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780) {
1066 			ctx->cc_range.from_channel = 0U;
1067 			ctx->cc_range.to_channel = 3U;
1068 		} else {
1069 			__ASSERT(false, "Unsupported channel page %u.", ctx->cc_page);
1070 		}
1071 	} else {
1072 		__ASSERT(ctx->cc_page ==
1073 				 IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915,
1074 			 "Unsupported channel page %u.", ctx->cc_page);
1075 		ctx->cc_range.from_channel = 11U;
1076 		ctx->cc_range.to_channel = 26U;
1077 	}
1078 
1079 	ieee802154_init(iface);
1080 }
1081 
1082 static const struct ieee802154_radio_api rf2xx_radio_api = {
1083 	.iface_api.init		= rf2xx_iface_init,
1084 
1085 	.get_capabilities	= rf2xx_get_capabilities,
1086 	.cca			= rf2xx_cca,
1087 	.set_channel		= rf2xx_set_channel,
1088 	.filter			= rf2xx_filter,
1089 	.set_txpower		= rf2xx_set_txpower,
1090 	.tx			= rf2xx_tx,
1091 	.start			= rf2xx_start,
1092 	.stop			= rf2xx_stop,
1093 	.configure		= rf2xx_configure,
1094 	.attr_get		= rf2xx_attr_get,
1095 };
1096 
1097 #if !defined(CONFIG_IEEE802154_RAW_MODE)
1098     #if defined(CONFIG_NET_L2_IEEE802154)
1099 	#define L2 IEEE802154_L2
1100 	#define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
1101 	#define MTU RF2XX_MAX_PSDU_LENGTH
1102     #elif defined(CONFIG_NET_L2_OPENTHREAD)
1103 	#define L2 OPENTHREAD_L2
1104 	#define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
1105 	#define MTU RF2XX_OT_PSDU_LENGTH
1106     #endif
1107 #endif /* CONFIG_IEEE802154_RAW_MODE */
1108 
1109 #define DRV_INST_LOCAL_MAC_ADDRESS(n)					\
1110 	UTIL_AND(DT_INST_NODE_HAS_PROP(n, local_mac_address),		\
1111 		 UTIL_AND(DT_INST_PROP_LEN(n, local_mac_address) == 8,	\
1112 			  DT_INST_PROP(n, local_mac_address)))
1113 
1114 #define IEEE802154_RF2XX_DEVICE_CONFIG(n)				  \
1115 	BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_min) == 2,		  \
1116 	"rf2xx: Error TX-PWR-MIN len is different of two");		  \
1117 	BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_max) == 2,		  \
1118 	"rf2xx: Error TX-PWR-MAX len is different of two");		  \
1119 	BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_table) != 0,		  \
1120 	"rf2xx: Error TX-PWR-TABLE len must be greater than zero");	  \
1121 	static const uint8_t rf2xx_pwr_table_##n[] =			  \
1122 		DT_INST_PROP_OR(n, tx_pwr_table, 0);			  \
1123 	static const struct rf2xx_config rf2xx_ctx_config_##n = {	  \
1124 		.inst = n,						  \
1125 		.has_mac = DT_INST_NODE_HAS_PROP(n, local_mac_address),   \
1126 		.irq_gpio = GPIO_DT_SPEC_INST_GET(n, irq_gpios),	  \
1127 		.reset_gpio = GPIO_DT_SPEC_INST_GET(n, reset_gpios),	  \
1128 		.slptr_gpio = GPIO_DT_SPEC_INST_GET(n, slptr_gpios),	  \
1129 		.dig2_gpio = GPIO_DT_SPEC_INST_GET_OR(n, dig2_gpios, {}), \
1130 		.clkm_gpio = GPIO_DT_SPEC_INST_GET_OR(n, clkm_gpios, {}), \
1131 		.spi = SPI_DT_SPEC_INST_GET(n, SPI_WORD_SET(8) |	  \
1132 				 SPI_TRANSFER_MSB, 0),			  \
1133 									  \
1134 		.tx_pwr_min = DT_INST_PROP_OR(n, tx_pwr_min, 0),	  \
1135 		.tx_pwr_max = DT_INST_PROP_OR(n, tx_pwr_max, 0),	  \
1136 		.tx_pwr_table = rf2xx_pwr_table_##n,			  \
1137 		.tx_pwr_table_size = DT_INST_PROP_LEN(n, tx_pwr_table),	  \
1138 	}
1139 
1140 #define IEEE802154_RF2XX_DEVICE_DATA(n)                                 \
1141 	static struct rf2xx_context rf2xx_ctx_data_##n = {              \
1142 		.mac_addr = { DRV_INST_LOCAL_MAC_ADDRESS(n) },          \
1143 		.cc_page = BIT(DT_INST_ENUM_IDX_OR(n, channel_page, 0)),\
1144 		.cc_channels = {                                        \
1145 			.ranges = &rf2xx_ctx_data_##n.cc_range,         \
1146 			.num_ranges = 1U,                               \
1147 		}                                                       \
1148 	}
1149 
1150 #define IEEE802154_RF2XX_RAW_DEVICE_INIT(n)	   \
1151 	DEVICE_DT_INST_DEFINE(			   \
1152 		n,				   \
1153 		&rf2xx_init,			   \
1154 		NULL,				   \
1155 		&rf2xx_ctx_data_##n,		   \
1156 		&rf2xx_ctx_config_##n,		   \
1157 		POST_KERNEL,			   \
1158 		CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1159 		&rf2xx_radio_api)
1160 
1161 #define IEEE802154_RF2XX_NET_DEVICE_INIT(n)	   \
1162 	NET_DEVICE_DT_INST_DEFINE(		   \
1163 		n,				   \
1164 		&rf2xx_init,			   \
1165 		NULL,				   \
1166 		&rf2xx_ctx_data_##n,		   \
1167 		&rf2xx_ctx_config_##n,		   \
1168 		CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1169 		&rf2xx_radio_api,		   \
1170 		L2,				   \
1171 		L2_CTX_TYPE,			   \
1172 		MTU)
1173 
1174 #define IEEE802154_RF2XX_INIT(inst)				\
1175 	IEEE802154_RF2XX_DEVICE_CONFIG(inst);			\
1176 	IEEE802154_RF2XX_DEVICE_DATA(inst);			\
1177 								\
1178 	COND_CODE_1(CONFIG_IEEE802154_RAW_MODE,			\
1179 		    (IEEE802154_RF2XX_RAW_DEVICE_INIT(inst);),	\
1180 		    (IEEE802154_RF2XX_NET_DEVICE_INIT(inst);))
1181 
1182 DT_INST_FOREACH_STATUS_OKAY(IEEE802154_RF2XX_INIT)
1183