]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ieee802154/at86rf230.c
ieee802154: rework cca setting
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ieee802154 / at86rf230.c
CommitLineData
7b8e19b6 1/*
2 * AT86RF230/RF231 driver
3 *
4 * Copyright (C) 2009-2012 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
7b8e19b6 15 * Written by:
16 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
01ebd60b 18 * Alexander Aring <aar@pengutronix.de>
7b8e19b6 19 */
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
4af619ae 23#include <linux/irq.h>
7b8e19b6 24#include <linux/gpio.h>
25#include <linux/delay.h>
7b8e19b6 26#include <linux/spinlock.h>
27#include <linux/spi/spi.h>
28#include <linux/spi/at86rf230.h>
f76014f7 29#include <linux/regmap.h>
7b8e19b6 30#include <linux/skbuff.h>
fa2d3e94 31#include <linux/of_gpio.h>
4ca24aca 32#include <linux/ieee802154.h>
7b8e19b6 33
34#include <net/mac802154.h>
5ad60d36 35#include <net/cfg802154.h>
7b8e19b6 36
a53d1f7c
AA
37struct at86rf230_local;
38/* at86rf2xx chip depend data.
39 * All timings are in us.
40 */
41struct at86rf2xx_chip_data {
7a4ef918 42 u16 t_sleep_cycle;
984e0c68 43 u16 t_channel_switch;
09e536cd 44 u16 t_reset_to_off;
2e0571c0
AA
45 u16 t_off_to_aack;
46 u16 t_off_to_tx_on;
1d15d6b5
AA
47 u16 t_frame;
48 u16 t_p_ack;
1d15d6b5
AA
49 /* completion timeout for tx in msecs */
50 u16 t_tx_timeout;
a53d1f7c
AA
51 int rssi_base_val;
52
e37d2ec8 53 int (*set_channel)(struct at86rf230_local *, u8, u8);
a7d7eda9 54 int (*get_desense_steps)(struct at86rf230_local *, s32);
a53d1f7c
AA
55};
56
1d15d6b5 57#define AT86RF2XX_MAX_BUF (127 + 3)
7b8e19b6 58
1d15d6b5
AA
59struct at86rf230_state_change {
60 struct at86rf230_local *lp;
7b8e19b6 61
1d15d6b5
AA
62 struct spi_message msg;
63 struct spi_transfer trx;
64 u8 buf[AT86RF2XX_MAX_BUF];
65
66 void (*complete)(void *context);
67 u8 from_state;
68 u8 to_state;
97fed795
AA
69
70 bool irq_enable;
1d15d6b5
AA
71};
72
73struct at86rf230_local {
74 struct spi_device *spi;
7b8e19b6 75
5a504397 76 struct ieee802154_hw *hw;
1d15d6b5 77 struct at86rf2xx_chip_data *data;
f76014f7 78 struct regmap *regmap;
7b8e19b6 79
2e0571c0
AA
80 struct completion state_complete;
81 struct at86rf230_state_change state;
82
1d15d6b5 83 struct at86rf230_state_change irq;
6ca00197 84
a53d1f7c 85 bool tx_aret;
850f43ac 86 s8 max_frame_retries;
1d15d6b5
AA
87 bool is_tx;
88 /* spinlock for is_tx protection */
89 spinlock_t lock;
1d15d6b5
AA
90 struct sk_buff *tx_skb;
91 struct at86rf230_state_change tx;
7b8e19b6 92};
93
94#define RG_TRX_STATUS (0x01)
95#define SR_TRX_STATUS 0x01, 0x1f, 0
96#define SR_RESERVED_01_3 0x01, 0x20, 5
97#define SR_CCA_STATUS 0x01, 0x40, 6
98#define SR_CCA_DONE 0x01, 0x80, 7
99#define RG_TRX_STATE (0x02)
100#define SR_TRX_CMD 0x02, 0x1f, 0
101#define SR_TRAC_STATUS 0x02, 0xe0, 5
102#define RG_TRX_CTRL_0 (0x03)
103#define SR_CLKM_CTRL 0x03, 0x07, 0
104#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
105#define SR_PAD_IO_CLKM 0x03, 0x30, 4
106#define SR_PAD_IO 0x03, 0xc0, 6
107#define RG_TRX_CTRL_1 (0x04)
108#define SR_IRQ_POLARITY 0x04, 0x01, 0
109#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
110#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
111#define SR_RX_BL_CTRL 0x04, 0x10, 4
112#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
113#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
114#define SR_PA_EXT_EN 0x04, 0x80, 7
115#define RG_PHY_TX_PWR (0x05)
116#define SR_TX_PWR 0x05, 0x0f, 0
117#define SR_PA_LT 0x05, 0x30, 4
118#define SR_PA_BUF_LT 0x05, 0xc0, 6
119#define RG_PHY_RSSI (0x06)
120#define SR_RSSI 0x06, 0x1f, 0
121#define SR_RND_VALUE 0x06, 0x60, 5
122#define SR_RX_CRC_VALID 0x06, 0x80, 7
123#define RG_PHY_ED_LEVEL (0x07)
124#define SR_ED_LEVEL 0x07, 0xff, 0
125#define RG_PHY_CC_CCA (0x08)
126#define SR_CHANNEL 0x08, 0x1f, 0
127#define SR_CCA_MODE 0x08, 0x60, 5
128#define SR_CCA_REQUEST 0x08, 0x80, 7
129#define RG_CCA_THRES (0x09)
130#define SR_CCA_ED_THRES 0x09, 0x0f, 0
131#define SR_RESERVED_09_1 0x09, 0xf0, 4
132#define RG_RX_CTRL (0x0a)
133#define SR_PDT_THRES 0x0a, 0x0f, 0
134#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
135#define RG_SFD_VALUE (0x0b)
136#define SR_SFD_VALUE 0x0b, 0xff, 0
137#define RG_TRX_CTRL_2 (0x0c)
138#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
8fad346f
PB
139#define SR_SUB_MODE 0x0c, 0x04, 2
140#define SR_BPSK_QPSK 0x0c, 0x08, 3
643e53c2
PB
141#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
142#define SR_RESERVED_0c_5 0x0c, 0x60, 5
7b8e19b6 143#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
144#define RG_ANT_DIV (0x0d)
145#define SR_ANT_CTRL 0x0d, 0x03, 0
146#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
147#define SR_ANT_DIV_EN 0x0d, 0x08, 3
148#define SR_RESERVED_0d_2 0x0d, 0x70, 4
149#define SR_ANT_SEL 0x0d, 0x80, 7
150#define RG_IRQ_MASK (0x0e)
151#define SR_IRQ_MASK 0x0e, 0xff, 0
152#define RG_IRQ_STATUS (0x0f)
153#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
154#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
155#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
156#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
157#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
158#define SR_IRQ_5_AMI 0x0f, 0x20, 5
159#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
160#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
161#define RG_VREG_CTRL (0x10)
162#define SR_RESERVED_10_6 0x10, 0x03, 0
163#define SR_DVDD_OK 0x10, 0x04, 2
164#define SR_DVREG_EXT 0x10, 0x08, 3
165#define SR_RESERVED_10_3 0x10, 0x30, 4
166#define SR_AVDD_OK 0x10, 0x40, 6
167#define SR_AVREG_EXT 0x10, 0x80, 7
168#define RG_BATMON (0x11)
169#define SR_BATMON_VTH 0x11, 0x0f, 0
170#define SR_BATMON_HR 0x11, 0x10, 4
171#define SR_BATMON_OK 0x11, 0x20, 5
172#define SR_RESERVED_11_1 0x11, 0xc0, 6
173#define RG_XOSC_CTRL (0x12)
174#define SR_XTAL_TRIM 0x12, 0x0f, 0
175#define SR_XTAL_MODE 0x12, 0xf0, 4
176#define RG_RX_SYN (0x15)
177#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
178#define SR_RESERVED_15_2 0x15, 0x70, 4
179#define SR_RX_PDT_DIS 0x15, 0x80, 7
180#define RG_XAH_CTRL_1 (0x17)
181#define SR_RESERVED_17_8 0x17, 0x01, 0
182#define SR_AACK_PROM_MODE 0x17, 0x02, 1
183#define SR_AACK_ACK_TIME 0x17, 0x04, 2
184#define SR_RESERVED_17_5 0x17, 0x08, 3
185#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
186#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
84dda3c6 187#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
7b8e19b6 188#define SR_RESERVED_17_1 0x17, 0x80, 7
189#define RG_FTN_CTRL (0x18)
190#define SR_RESERVED_18_2 0x18, 0x7f, 0
191#define SR_FTN_START 0x18, 0x80, 7
192#define RG_PLL_CF (0x1a)
193#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
194#define SR_PLL_CF_START 0x1a, 0x80, 7
195#define RG_PLL_DCU (0x1b)
196#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
197#define SR_RESERVED_1b_2 0x1b, 0x40, 6
198#define SR_PLL_DCU_START 0x1b, 0x80, 7
199#define RG_PART_NUM (0x1c)
200#define SR_PART_NUM 0x1c, 0xff, 0
201#define RG_VERSION_NUM (0x1d)
202#define SR_VERSION_NUM 0x1d, 0xff, 0
203#define RG_MAN_ID_0 (0x1e)
204#define SR_MAN_ID_0 0x1e, 0xff, 0
205#define RG_MAN_ID_1 (0x1f)
206#define SR_MAN_ID_1 0x1f, 0xff, 0
207#define RG_SHORT_ADDR_0 (0x20)
208#define SR_SHORT_ADDR_0 0x20, 0xff, 0
209#define RG_SHORT_ADDR_1 (0x21)
210#define SR_SHORT_ADDR_1 0x21, 0xff, 0
211#define RG_PAN_ID_0 (0x22)
212#define SR_PAN_ID_0 0x22, 0xff, 0
213#define RG_PAN_ID_1 (0x23)
214#define SR_PAN_ID_1 0x23, 0xff, 0
215#define RG_IEEE_ADDR_0 (0x24)
216#define SR_IEEE_ADDR_0 0x24, 0xff, 0
217#define RG_IEEE_ADDR_1 (0x25)
218#define SR_IEEE_ADDR_1 0x25, 0xff, 0
219#define RG_IEEE_ADDR_2 (0x26)
220#define SR_IEEE_ADDR_2 0x26, 0xff, 0
221#define RG_IEEE_ADDR_3 (0x27)
222#define SR_IEEE_ADDR_3 0x27, 0xff, 0
223#define RG_IEEE_ADDR_4 (0x28)
224#define SR_IEEE_ADDR_4 0x28, 0xff, 0
225#define RG_IEEE_ADDR_5 (0x29)
226#define SR_IEEE_ADDR_5 0x29, 0xff, 0
227#define RG_IEEE_ADDR_6 (0x2a)
228#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
229#define RG_IEEE_ADDR_7 (0x2b)
230#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
231#define RG_XAH_CTRL_0 (0x2c)
232#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
233#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
234#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
235#define RG_CSMA_SEED_0 (0x2d)
236#define SR_CSMA_SEED_0 0x2d, 0xff, 0
237#define RG_CSMA_SEED_1 (0x2e)
238#define SR_CSMA_SEED_1 0x2e, 0x07, 0
239#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
240#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
241#define SR_AACK_SET_PD 0x2e, 0x20, 5
242#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
243#define RG_CSMA_BE (0x2f)
244#define SR_MIN_BE 0x2f, 0x0f, 0
245#define SR_MAX_BE 0x2f, 0xf0, 4
246
247#define CMD_REG 0x80
248#define CMD_REG_MASK 0x3f
249#define CMD_WRITE 0x40
250#define CMD_FB 0x20
251
252#define IRQ_BAT_LOW (1 << 7)
253#define IRQ_TRX_UR (1 << 6)
254#define IRQ_AMI (1 << 5)
255#define IRQ_CCA_ED (1 << 4)
256#define IRQ_TRX_END (1 << 3)
257#define IRQ_RX_START (1 << 2)
258#define IRQ_PLL_UNL (1 << 1)
259#define IRQ_PLL_LOCK (1 << 0)
260
43b5abe0
SH
261#define IRQ_ACTIVE_HIGH 0
262#define IRQ_ACTIVE_LOW 1
263
7b8e19b6 264#define STATE_P_ON 0x00 /* BUSY */
265#define STATE_BUSY_RX 0x01
266#define STATE_BUSY_TX 0x02
267#define STATE_FORCE_TRX_OFF 0x03
268#define STATE_FORCE_TX_ON 0x04 /* IDLE */
269/* 0x05 */ /* INVALID_PARAMETER */
270#define STATE_RX_ON 0x06
271/* 0x07 */ /* SUCCESS */
272#define STATE_TRX_OFF 0x08
273#define STATE_TX_ON 0x09
274/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
275#define STATE_SLEEP 0x0F
48d5dbaf 276#define STATE_PREP_DEEP_SLEEP 0x10
7b8e19b6 277#define STATE_BUSY_RX_AACK 0x11
278#define STATE_BUSY_TX_ARET 0x12
028889b0 279#define STATE_RX_AACK_ON 0x16
280#define STATE_TX_ARET_ON 0x19
7b8e19b6 281#define STATE_RX_ON_NOCLK 0x1C
282#define STATE_RX_AACK_ON_NOCLK 0x1D
283#define STATE_BUSY_RX_AACK_NOCLK 0x1E
284#define STATE_TRANSITION_IN_PROGRESS 0x1F
285
f76014f7
AA
286#define AT86RF2XX_NUMREGS 0x3F
287
97fed795 288static void
1d15d6b5
AA
289at86rf230_async_state_change(struct at86rf230_local *lp,
290 struct at86rf230_state_change *ctx,
97fed795
AA
291 const u8 state, void (*complete)(void *context),
292 const bool irq_enable);
1d15d6b5 293
f76014f7
AA
294static inline int
295__at86rf230_write(struct at86rf230_local *lp,
296 unsigned int addr, unsigned int data)
297{
298 return regmap_write(lp->regmap, addr, data);
299}
300
301static inline int
302__at86rf230_read(struct at86rf230_local *lp,
303 unsigned int addr, unsigned int *data)
304{
305 return regmap_read(lp->regmap, addr, data);
306}
307
308static inline int
309at86rf230_read_subreg(struct at86rf230_local *lp,
310 unsigned int addr, unsigned int mask,
311 unsigned int shift, unsigned int *data)
312{
313 int rc;
314
315 rc = __at86rf230_read(lp, addr, data);
316 if (rc > 0)
317 *data = (*data & mask) >> shift;
318
319 return rc;
320}
321
322static inline int
323at86rf230_write_subreg(struct at86rf230_local *lp,
324 unsigned int addr, unsigned int mask,
325 unsigned int shift, unsigned int data)
326{
327 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
328}
329
330static bool
331at86rf230_reg_writeable(struct device *dev, unsigned int reg)
332{
333 switch (reg) {
334 case RG_TRX_STATE:
335 case RG_TRX_CTRL_0:
336 case RG_TRX_CTRL_1:
337 case RG_PHY_TX_PWR:
338 case RG_PHY_ED_LEVEL:
339 case RG_PHY_CC_CCA:
340 case RG_CCA_THRES:
341 case RG_RX_CTRL:
342 case RG_SFD_VALUE:
343 case RG_TRX_CTRL_2:
344 case RG_ANT_DIV:
345 case RG_IRQ_MASK:
346 case RG_VREG_CTRL:
347 case RG_BATMON:
348 case RG_XOSC_CTRL:
349 case RG_RX_SYN:
350 case RG_XAH_CTRL_1:
351 case RG_FTN_CTRL:
352 case RG_PLL_CF:
353 case RG_PLL_DCU:
354 case RG_SHORT_ADDR_0:
355 case RG_SHORT_ADDR_1:
356 case RG_PAN_ID_0:
357 case RG_PAN_ID_1:
358 case RG_IEEE_ADDR_0:
359 case RG_IEEE_ADDR_1:
360 case RG_IEEE_ADDR_2:
361 case RG_IEEE_ADDR_3:
362 case RG_IEEE_ADDR_4:
363 case RG_IEEE_ADDR_5:
364 case RG_IEEE_ADDR_6:
365 case RG_IEEE_ADDR_7:
366 case RG_XAH_CTRL_0:
367 case RG_CSMA_SEED_0:
368 case RG_CSMA_SEED_1:
369 case RG_CSMA_BE:
370 return true;
371 default:
372 return false;
373 }
374}
375
376static bool
377at86rf230_reg_readable(struct device *dev, unsigned int reg)
378{
379 bool rc;
380
381 /* all writeable are also readable */
382 rc = at86rf230_reg_writeable(dev, reg);
383 if (rc)
384 return rc;
385
386 /* readonly regs */
387 switch (reg) {
388 case RG_TRX_STATUS:
389 case RG_PHY_RSSI:
390 case RG_IRQ_STATUS:
391 case RG_PART_NUM:
392 case RG_VERSION_NUM:
393 case RG_MAN_ID_1:
394 case RG_MAN_ID_0:
395 return true;
396 default:
397 return false;
398 }
399}
400
401static bool
402at86rf230_reg_volatile(struct device *dev, unsigned int reg)
403{
404 /* can be changed during runtime */
405 switch (reg) {
406 case RG_TRX_STATUS:
407 case RG_TRX_STATE:
408 case RG_PHY_RSSI:
409 case RG_PHY_ED_LEVEL:
410 case RG_IRQ_STATUS:
411 case RG_VREG_CTRL:
412 return true;
413 default:
414 return false;
415 }
416}
417
418static bool
419at86rf230_reg_precious(struct device *dev, unsigned int reg)
420{
421 /* don't clear irq line on read */
422 switch (reg) {
423 case RG_IRQ_STATUS:
424 return true;
425 default:
426 return false;
427 }
428}
429
430static struct regmap_config at86rf230_regmap_spi_config = {
431 .reg_bits = 8,
432 .val_bits = 8,
433 .write_flag_mask = CMD_REG | CMD_WRITE,
434 .read_flag_mask = CMD_REG,
435 .cache_type = REGCACHE_RBTREE,
436 .max_register = AT86RF2XX_NUMREGS,
437 .writeable_reg = at86rf230_reg_writeable,
438 .readable_reg = at86rf230_reg_readable,
439 .volatile_reg = at86rf230_reg_volatile,
440 .precious_reg = at86rf230_reg_precious,
441};
442
1d15d6b5
AA
443static void
444at86rf230_async_error_recover(void *context)
445{
446 struct at86rf230_state_change *ctx = context;
447 struct at86rf230_local *lp = ctx->lp;
448
97fed795 449 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
955aee8b 450 ieee802154_wake_queue(lp->hw);
1d15d6b5
AA
451}
452
453static void
454at86rf230_async_error(struct at86rf230_local *lp,
455 struct at86rf230_state_change *ctx, int rc)
456{
457 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
458
459 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
97fed795 460 at86rf230_async_error_recover, false);
1d15d6b5
AA
461}
462
463/* Generic function to get some register value in async mode */
97fed795 464static void
1d15d6b5
AA
465at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
466 struct at86rf230_state_change *ctx,
97fed795
AA
467 void (*complete)(void *context),
468 const bool irq_enable)
7b8e19b6 469{
97fed795
AA
470 int rc;
471
1d15d6b5
AA
472 u8 *tx_buf = ctx->buf;
473
474 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
475 ctx->trx.len = 2;
476 ctx->msg.complete = complete;
97fed795
AA
477 ctx->irq_enable = irq_enable;
478 rc = spi_async(lp->spi, &ctx->msg);
479 if (rc) {
480 if (irq_enable)
481 enable_irq(lp->spi->irq);
482
483 at86rf230_async_error(lp, ctx, rc);
484 }
1d15d6b5
AA
485}
486
487static void
488at86rf230_async_state_assert(void *context)
489{
490 struct at86rf230_state_change *ctx = context;
491 struct at86rf230_local *lp = ctx->lp;
492 const u8 *buf = ctx->buf;
493 const u8 trx_state = buf[1] & 0x1f;
494
495 /* Assert state change */
496 if (trx_state != ctx->to_state) {
497 /* Special handling if transceiver state is in
498 * STATE_BUSY_RX_AACK and a SHR was detected.
499 */
500 if (trx_state == STATE_BUSY_RX_AACK) {
501 /* Undocumented race condition. If we send a state
502 * change to STATE_RX_AACK_ON the transceiver could
503 * change his state automatically to STATE_BUSY_RX_AACK
504 * if a SHR was detected. This is not an error, but we
505 * can't assert this.
506 */
507 if (ctx->to_state == STATE_RX_AACK_ON)
508 goto done;
509
510 /* If we change to STATE_TX_ON without forcing and
511 * transceiver state is STATE_BUSY_RX_AACK, we wait
512 * 'tFrame + tPAck' receiving time. In this time the
513 * PDU should be received. If the transceiver is still
514 * in STATE_BUSY_RX_AACK, we run a force state change
515 * to STATE_TX_ON. This is a timeout handling, if the
516 * transceiver stucks in STATE_BUSY_RX_AACK.
517 */
518 if (ctx->to_state == STATE_TX_ON) {
519 at86rf230_async_state_change(lp, ctx,
520 STATE_FORCE_TX_ON,
97fed795
AA
521 ctx->complete,
522 ctx->irq_enable);
1d15d6b5
AA
523 return;
524 }
525 }
526
527
528 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
529 ctx->from_state, ctx->to_state, trx_state);
530 }
531
532done:
533 if (ctx->complete)
534 ctx->complete(context);
535}
536
537/* Do state change timing delay. */
538static void
539at86rf230_async_state_delay(void *context)
540{
541 struct at86rf230_state_change *ctx = context;
542 struct at86rf230_local *lp = ctx->lp;
543 struct at86rf2xx_chip_data *c = lp->data;
544 bool force = false;
1d15d6b5
AA
545
546 /* The force state changes are will show as normal states in the
547 * state status subregister. We change the to_state to the
548 * corresponding one and remember if it was a force change, this
549 * differs if we do a state change from STATE_BUSY_RX_AACK.
550 */
551 switch (ctx->to_state) {
552 case STATE_FORCE_TX_ON:
553 ctx->to_state = STATE_TX_ON;
554 force = true;
555 break;
556 case STATE_FORCE_TRX_OFF:
557 ctx->to_state = STATE_TRX_OFF;
558 force = true;
559 break;
560 default:
561 break;
562 }
563
564 switch (ctx->from_state) {
2e0571c0
AA
565 case STATE_TRX_OFF:
566 switch (ctx->to_state) {
567 case STATE_RX_AACK_ON:
568 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
569 goto change;
570 case STATE_TX_ON:
571 usleep_range(c->t_off_to_tx_on,
572 c->t_off_to_tx_on + 10);
573 goto change;
574 default:
575 break;
576 }
577 break;
1d15d6b5
AA
578 case STATE_BUSY_RX_AACK:
579 switch (ctx->to_state) {
580 case STATE_TX_ON:
581 /* Wait for worst case receiving time if we
582 * didn't make a force change from BUSY_RX_AACK
583 * to TX_ON.
584 */
585 if (!force) {
586 usleep_range(c->t_frame + c->t_p_ack,
587 c->t_frame + c->t_p_ack + 1000);
588 goto change;
589 }
590 break;
591 default:
592 break;
593 }
594 break;
09e536cd
AA
595 /* Default value, means RESET state */
596 case STATE_P_ON:
597 switch (ctx->to_state) {
598 case STATE_TRX_OFF:
599 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
600 goto change;
601 default:
602 break;
603 }
604 break;
1d15d6b5
AA
605 default:
606 break;
607 }
608
609 /* Default delay is 1us in the most cases */
610 udelay(1);
611
612change:
97fed795
AA
613 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
614 at86rf230_async_state_assert,
615 ctx->irq_enable);
1d15d6b5
AA
616}
617
618static void
619at86rf230_async_state_change_start(void *context)
620{
621 struct at86rf230_state_change *ctx = context;
622 struct at86rf230_local *lp = ctx->lp;
623 u8 *buf = ctx->buf;
624 const u8 trx_state = buf[1] & 0x1f;
625 int rc;
626
627 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
628 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
629 udelay(1);
97fed795
AA
630 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
631 at86rf230_async_state_change_start,
632 ctx->irq_enable);
1d15d6b5
AA
633 return;
634 }
635
636 /* Check if we already are in the state which we change in */
637 if (trx_state == ctx->to_state) {
638 if (ctx->complete)
639 ctx->complete(context);
640 return;
641 }
642
643 /* Set current state to the context of state change */
644 ctx->from_state = trx_state;
645
646 /* Going into the next step for a state change which do a timing
647 * relevant delay.
648 */
649 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
650 buf[1] = ctx->to_state;
651 ctx->trx.len = 2;
652 ctx->msg.complete = at86rf230_async_state_delay;
653 rc = spi_async(lp->spi, &ctx->msg);
97fed795
AA
654 if (rc) {
655 if (ctx->irq_enable)
656 enable_irq(lp->spi->irq);
657
658 at86rf230_async_error(lp, &lp->state, rc);
97fed795 659 }
7b8e19b6 660}
661
97fed795 662static void
1d15d6b5
AA
663at86rf230_async_state_change(struct at86rf230_local *lp,
664 struct at86rf230_state_change *ctx,
97fed795
AA
665 const u8 state, void (*complete)(void *context),
666 const bool irq_enable)
7b8e19b6 667{
1d15d6b5
AA
668 /* Initialization for the state change context */
669 ctx->to_state = state;
670 ctx->complete = complete;
97fed795
AA
671 ctx->irq_enable = irq_enable;
672 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
673 at86rf230_async_state_change_start,
674 irq_enable);
1d15d6b5 675}
7b8e19b6 676
2e0571c0
AA
677static void
678at86rf230_sync_state_change_complete(void *context)
679{
680 struct at86rf230_state_change *ctx = context;
681 struct at86rf230_local *lp = ctx->lp;
682
683 complete(&lp->state_complete);
684}
685
686/* This function do a sync framework above the async state change.
687 * Some callbacks of the IEEE 802.15.4 driver interface need to be
688 * handled synchronously.
689 */
690static int
691at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
692{
693 int rc;
694
97fed795
AA
695 at86rf230_async_state_change(lp, &lp->state, state,
696 at86rf230_sync_state_change_complete,
697 false);
2e0571c0
AA
698
699 rc = wait_for_completion_timeout(&lp->state_complete,
700 msecs_to_jiffies(100));
d06c2199
AA
701 if (!rc) {
702 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
2e0571c0 703 return -ETIMEDOUT;
d06c2199 704 }
2e0571c0
AA
705
706 return 0;
707}
708
1d15d6b5
AA
709static void
710at86rf230_tx_complete(void *context)
711{
712 struct at86rf230_state_change *ctx = context;
713 struct at86rf230_local *lp = ctx->lp;
955aee8b 714 struct sk_buff *skb = lp->tx_skb;
1d15d6b5 715
35e92a8e 716 enable_irq(lp->spi->irq);
955aee8b 717
24ccb9f4
AA
718 if (lp->max_frame_retries <= 0)
719 ieee802154_xmit_complete(lp->hw, skb, true);
720 else
721 ieee802154_xmit_complete(lp->hw, skb, false);
1d15d6b5
AA
722}
723
724static void
725at86rf230_tx_on(void *context)
726{
727 struct at86rf230_state_change *ctx = context;
728 struct at86rf230_local *lp = ctx->lp;
1d15d6b5 729
97fed795
AA
730 at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
731 at86rf230_tx_complete, true);
1d15d6b5
AA
732}
733
734static void
735at86rf230_tx_trac_error(void *context)
736{
737 struct at86rf230_state_change *ctx = context;
738 struct at86rf230_local *lp = ctx->lp;
1d15d6b5 739
97fed795
AA
740 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
741 at86rf230_tx_on, true);
1d15d6b5
AA
742}
743
744static void
745at86rf230_tx_trac_check(void *context)
746{
747 struct at86rf230_state_change *ctx = context;
748 struct at86rf230_local *lp = ctx->lp;
749 const u8 *buf = ctx->buf;
750 const u8 trac = (buf[1] & 0xe0) >> 5;
1d15d6b5
AA
751
752 /* If trac status is different than zero we need to do a state change
753 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
754 * state to TX_ON.
755 */
756 if (trac) {
97fed795
AA
757 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
758 at86rf230_tx_trac_error, true);
1d15d6b5
AA
759 return;
760 }
761
762 at86rf230_tx_on(context);
763}
764
765
766static void
767at86rf230_tx_trac_status(void *context)
768{
769 struct at86rf230_state_change *ctx = context;
770 struct at86rf230_local *lp = ctx->lp;
1d15d6b5 771
97fed795
AA
772 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
773 at86rf230_tx_trac_check, true);
1d15d6b5
AA
774}
775
776static void
777at86rf230_rx(struct at86rf230_local *lp,
b89c3341 778 const u8 *data, const u8 len, const u8 lqi)
1d15d6b5 779{
1d15d6b5
AA
780 struct sk_buff *skb;
781 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
782
1d15d6b5
AA
783 memcpy(rx_local_buf, data, len);
784 enable_irq(lp->spi->irq);
785
61a22814 786 skb = dev_alloc_skb(IEEE802154_MTU);
1d15d6b5
AA
787 if (!skb) {
788 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
789 return;
790 }
791
792 memcpy(skb_put(skb, len), rx_local_buf, len);
b89c3341 793 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
1d15d6b5 794}
7b8e19b6 795
1d15d6b5
AA
796static void
797at86rf230_rx_read_frame_complete(void *context)
798{
799 struct at86rf230_state_change *ctx = context;
800 struct at86rf230_local *lp = ctx->lp;
801 const u8 *buf = lp->irq.buf;
d0e73c47
AA
802 u8 len = buf[1];
803
804 if (!ieee802154_is_valid_psdu_len(len)) {
805 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
806 len = IEEE802154_MTU;
807 }
7b8e19b6 808
2ac0f3a3 809 at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
1d15d6b5
AA
810}
811
97fed795 812static void
1d15d6b5
AA
813at86rf230_rx_read_frame(struct at86rf230_local *lp)
814{
97fed795
AA
815 int rc;
816
1d15d6b5 817 u8 *buf = lp->irq.buf;
7b8e19b6 818
819 buf[0] = CMD_FB;
1d15d6b5
AA
820 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
821 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
97fed795
AA
822 rc = spi_async(lp->spi, &lp->irq.msg);
823 if (rc) {
824 enable_irq(lp->spi->irq);
825 at86rf230_async_error(lp, &lp->irq, rc);
826 }
1d15d6b5
AA
827}
828
829static void
830at86rf230_rx_trac_check(void *context)
831{
832 struct at86rf230_state_change *ctx = context;
833 struct at86rf230_local *lp = ctx->lp;
1d15d6b5
AA
834
835 /* Possible check on trac status here. This could be useful to make
836 * some stats why receive is failed. Not used at the moment, but it's
837 * maybe timing relevant. Datasheet doesn't say anything about this.
838 * The programming guide say do it so.
839 */
840
97fed795 841 at86rf230_rx_read_frame(lp);
1d15d6b5
AA
842}
843
97fed795 844static void
1d15d6b5
AA
845at86rf230_irq_trx_end(struct at86rf230_local *lp)
846{
847 spin_lock(&lp->lock);
848 if (lp->is_tx) {
849 lp->is_tx = 0;
850 spin_unlock(&lp->lock);
1d15d6b5
AA
851
852 if (lp->tx_aret)
97fed795
AA
853 at86rf230_async_state_change(lp, &lp->irq,
854 STATE_FORCE_TX_ON,
855 at86rf230_tx_trac_status,
856 true);
1d15d6b5 857 else
97fed795
AA
858 at86rf230_async_state_change(lp, &lp->irq,
859 STATE_RX_AACK_ON,
860 at86rf230_tx_complete,
861 true);
1d15d6b5
AA
862 } else {
863 spin_unlock(&lp->lock);
97fed795
AA
864 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
865 at86rf230_rx_trac_check, true);
1d15d6b5
AA
866 }
867}
868
869static void
870at86rf230_irq_status(void *context)
871{
872 struct at86rf230_state_change *ctx = context;
873 struct at86rf230_local *lp = ctx->lp;
874 const u8 *buf = lp->irq.buf;
875 const u8 irq = buf[1];
1d15d6b5
AA
876
877 if (irq & IRQ_TRX_END) {
97fed795 878 at86rf230_irq_trx_end(lp);
1d15d6b5
AA
879 } else {
880 enable_irq(lp->spi->irq);
881 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
882 irq);
883 }
884}
885
886static irqreturn_t at86rf230_isr(int irq, void *data)
887{
888 struct at86rf230_local *lp = data;
889 struct at86rf230_state_change *ctx = &lp->irq;
890 u8 *buf = ctx->buf;
891 int rc;
892
90566363 893 disable_irq_nosync(irq);
1d15d6b5
AA
894
895 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
896 ctx->trx.len = 2;
897 ctx->msg.complete = at86rf230_irq_status;
898 rc = spi_async(lp->spi, &ctx->msg);
899 if (rc) {
e9310211 900 enable_irq(irq);
1d15d6b5
AA
901 at86rf230_async_error(lp, ctx, rc);
902 return IRQ_NONE;
903 }
904
905 return IRQ_HANDLED;
906}
907
908static void
909at86rf230_write_frame_complete(void *context)
910{
911 struct at86rf230_state_change *ctx = context;
912 struct at86rf230_local *lp = ctx->lp;
913 u8 *buf = ctx->buf;
914 int rc;
915
916 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
917 buf[1] = STATE_BUSY_TX;
918 ctx->trx.len = 2;
919 ctx->msg.complete = NULL;
920 rc = spi_async(lp->spi, &ctx->msg);
921 if (rc)
922 at86rf230_async_error(lp, ctx, rc);
923}
924
925static void
926at86rf230_write_frame(void *context)
927{
928 struct at86rf230_state_change *ctx = context;
929 struct at86rf230_local *lp = ctx->lp;
930 struct sk_buff *skb = lp->tx_skb;
931 u8 *buf = lp->tx.buf;
932 int rc;
933
934 spin_lock(&lp->lock);
935 lp->is_tx = 1;
936 spin_unlock(&lp->lock);
937
938 buf[0] = CMD_FB | CMD_WRITE;
939 buf[1] = skb->len + 2;
940 memcpy(buf + 2, skb->data, skb->len);
941 lp->tx.trx.len = skb->len + 2;
942 lp->tx.msg.complete = at86rf230_write_frame_complete;
943 rc = spi_async(lp->spi, &lp->tx.msg);
944 if (rc)
945 at86rf230_async_error(lp, ctx, rc);
946}
947
948static void
949at86rf230_xmit_tx_on(void *context)
950{
951 struct at86rf230_state_change *ctx = context;
952 struct at86rf230_local *lp = ctx->lp;
7b8e19b6 953
97fed795
AA
954 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
955 at86rf230_write_frame, false);
1d15d6b5
AA
956}
957
958static int
5a504397 959at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
1d15d6b5 960{
5a504397 961 struct at86rf230_local *lp = hw->priv;
1d15d6b5 962 struct at86rf230_state_change *ctx = &lp->tx;
7b8e19b6 963
1d15d6b5 964 void (*tx_complete)(void *context) = at86rf230_write_frame;
7b8e19b6 965
1d15d6b5 966 lp->tx_skb = skb;
7b8e19b6 967
1d15d6b5
AA
968 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
969 * are in STATE_TX_ON. The pfad differs here, so we change
970 * the complete handler.
971 */
972 if (lp->tx_aret)
973 tx_complete = at86rf230_xmit_tx_on;
7b8e19b6 974
97fed795
AA
975 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
976
1d15d6b5 977 return 0;
7b8e19b6 978}
979
980static int
5a504397 981at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
7b8e19b6 982{
7b8e19b6 983 BUG_ON(!level);
984 *level = 0xbe;
985 return 0;
986}
987
7b8e19b6 988static int
5a504397 989at86rf230_start(struct ieee802154_hw *hw)
7b8e19b6 990{
5a504397 991 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
7b8e19b6 992}
993
994static void
5a504397 995at86rf230_stop(struct ieee802154_hw *hw)
7b8e19b6 996{
5a504397 997 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
7b8e19b6 998}
999
8fad346f 1000static int
e37d2ec8 1001at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
8fad346f
PB
1002{
1003 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1004}
1005
1006static int
e37d2ec8 1007at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
8fad346f
PB
1008{
1009 int rc;
1010
1011 if (channel == 0)
1012 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1013 else
1014 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1015 if (rc < 0)
1016 return rc;
1017
6ca00197 1018 if (page == 0) {
643e53c2 1019 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
a53d1f7c 1020 lp->data->rssi_base_val = -100;
6ca00197 1021 } else {
643e53c2 1022 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
a53d1f7c 1023 lp->data->rssi_base_val = -98;
6ca00197 1024 }
643e53c2
PB
1025 if (rc < 0)
1026 return rc;
1027
24ccb9f4
AA
1028 /* This sets the symbol_duration according frequency on the 212.
1029 * TODO move this handling while set channel and page in cfg802154.
1030 * We can do that, this timings are according 802.15.4 standard.
1031 * If we do that in cfg802154, this is a more generic calculation.
1032 *
1033 * This should also protected from ifs_timer. Means cancel timer and
1034 * init with a new value. For now, this is okay.
1035 */
1036 if (channel == 0) {
1037 if (page == 0) {
1038 /* SUB:0 and BPSK:0 -> BPSK-20 */
1039 lp->hw->phy->symbol_duration = 50;
1040 } else {
1041 /* SUB:1 and BPSK:0 -> BPSK-40 */
1042 lp->hw->phy->symbol_duration = 25;
1043 }
1044 } else {
1045 if (page == 0)
2d6dde29 1046 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
24ccb9f4
AA
1047 lp->hw->phy->symbol_duration = 40;
1048 else
2d6dde29 1049 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
24ccb9f4
AA
1050 lp->hw->phy->symbol_duration = 16;
1051 }
1052
1053 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1054 lp->hw->phy->symbol_duration;
1055 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1056 lp->hw->phy->symbol_duration;
1057
8fad346f
PB
1058 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1059}
1060
7b8e19b6 1061static int
e37d2ec8 1062at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
7b8e19b6 1063{
5a504397 1064 struct at86rf230_local *lp = hw->priv;
7b8e19b6 1065 int rc;
1066
a53d1f7c 1067 rc = lp->data->set_channel(lp, page, channel);
984e0c68
AA
1068 /* Wait for PLL */
1069 usleep_range(lp->data->t_channel_switch,
1070 lp->data->t_channel_switch + 10);
820bd66f 1071 return rc;
7b8e19b6 1072}
1073
1486774d 1074static int
5a504397 1075at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1486774d 1076 struct ieee802154_hw_addr_filt *filt,
1077 unsigned long changed)
1078{
5a504397 1079 struct at86rf230_local *lp = hw->priv;
1486774d 1080
57205c14 1081 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
b70ab2e8
PB
1082 u16 addr = le16_to_cpu(filt->short_addr);
1083
1486774d 1084 dev_vdbg(&lp->spi->dev,
1085 "at86rf230_set_hw_addr_filt called for saddr\n");
b70ab2e8
PB
1086 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1087 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1486774d 1088 }
1089
57205c14 1090 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
b70ab2e8
PB
1091 u16 pan = le16_to_cpu(filt->pan_id);
1092
1486774d 1093 dev_vdbg(&lp->spi->dev,
1094 "at86rf230_set_hw_addr_filt called for pan id\n");
b70ab2e8
PB
1095 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1096 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1486774d 1097 }
1098
57205c14 1099 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
b70ab2e8
PB
1100 u8 i, addr[8];
1101
1102 memcpy(addr, &filt->ieee_addr, 8);
1486774d 1103 dev_vdbg(&lp->spi->dev,
1104 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
b70ab2e8
PB
1105 for (i = 0; i < 8; i++)
1106 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1486774d 1107 }
1108
57205c14 1109 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1486774d 1110 dev_vdbg(&lp->spi->dev,
1111 "at86rf230_set_hw_addr_filt called for panc change\n");
1112 if (filt->pan_coord)
1113 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1114 else
1115 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1116 }
1117
1118 return 0;
1119}
1120
9b2777d6 1121static int
5a504397 1122at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
9b2777d6 1123{
5a504397 1124 struct at86rf230_local *lp = hw->priv;
9b2777d6
PB
1125
1126 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1127 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1128 * 0dB.
1129 * thus, supported values for db range from -26 to 5, for 31dB of
1130 * reduction to 0dB of reduction.
1131 */
1132 if (db > 5 || db < -26)
1133 return -EINVAL;
1134
1135 db = -(db - 5);
1136
677676cd 1137 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
9b2777d6
PB
1138}
1139
84dda3c6 1140static int
5a504397 1141at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
84dda3c6 1142{
5a504397 1143 struct at86rf230_local *lp = hw->priv;
84dda3c6
PB
1144
1145 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1146}
1147
ba08fea5 1148static int
7fe9a388
AA
1149at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1150 const struct wpan_phy_cca *cca)
ba08fea5 1151{
5a504397 1152 struct at86rf230_local *lp = hw->priv;
7fe9a388 1153 u8 val;
ba08fea5 1154
7fe9a388
AA
1155 /* mapping 802.15.4 to driver spec */
1156 switch (cca->mode) {
1157 case NL802154_CCA_ENERGY:
1158 val = 1;
1159 break;
1160 case NL802154_CCA_CARRIER:
1161 val = 2;
1162 break;
1163 case NL802154_CCA_ENERGY_CARRIER:
1164 switch (cca->opt) {
1165 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1166 val = 3;
1167 break;
1168 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1169 val = 0;
1170 break;
1171 default:
1172 return -EINVAL;
1173 }
1174 break;
1175 default:
1176 return -EINVAL;
1177 }
1178
1179 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
ba08fea5
PB
1180}
1181
a7d7eda9
AA
1182static int
1183at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1184{
1185 return (level - lp->data->rssi_base_val) * 100 / 207;
1186}
1187
1188static int
1189at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1190{
1191 return (level - lp->data->rssi_base_val) / 2;
1192}
1193
6ca00197 1194static int
5a504397 1195at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
6ca00197 1196{
5a504397 1197 struct at86rf230_local *lp = hw->priv;
6ca00197 1198
a53d1f7c 1199 if (level < lp->data->rssi_base_val || level > 30)
6ca00197
PB
1200 return -EINVAL;
1201
a7d7eda9
AA
1202 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1203 lp->data->get_desense_steps(lp, level));
6ca00197
PB
1204}
1205
f2fdd67c 1206static int
5a504397 1207at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
f2fdd67c
PB
1208 u8 retries)
1209{
5a504397 1210 struct at86rf230_local *lp = hw->priv;
f2fdd67c
PB
1211 int rc;
1212
f2fdd67c
PB
1213 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1214 if (rc)
1215 return rc;
1216
1217 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1218 if (rc)
1219 return rc;
1220
39d7f320 1221 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
f2fdd67c
PB
1222}
1223
1224static int
5a504397 1225at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
f2fdd67c 1226{
5a504397 1227 struct at86rf230_local *lp = hw->priv;
f2fdd67c
PB
1228 int rc = 0;
1229
f2fdd67c 1230 lp->tx_aret = retries >= 0;
850f43ac 1231 lp->max_frame_retries = retries;
f2fdd67c
PB
1232
1233 if (retries >= 0)
1234 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1235
1236 return rc;
1237}
1238
92f45f54
AA
1239static int
1240at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1241{
1242 struct at86rf230_local *lp = hw->priv;
1243 int rc;
1244
1245 if (on) {
1246 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1247 if (rc < 0)
1248 return rc;
1249
1250 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1251 if (rc < 0)
1252 return rc;
1253 } else {
1254 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1255 if (rc < 0)
1256 return rc;
1257
1258 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1259 if (rc < 0)
1260 return rc;
1261 }
1262
1263 return 0;
1264}
1265
16301861 1266static const struct ieee802154_ops at86rf230_ops = {
7b8e19b6 1267 .owner = THIS_MODULE,
955aee8b 1268 .xmit_async = at86rf230_xmit,
7b8e19b6 1269 .ed = at86rf230_ed,
1270 .set_channel = at86rf230_channel,
1271 .start = at86rf230_start,
1272 .stop = at86rf230_stop,
1486774d 1273 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
640985ec
AA
1274 .set_txpower = at86rf230_set_txpower,
1275 .set_lbt = at86rf230_set_lbt,
1276 .set_cca_mode = at86rf230_set_cca_mode,
1277 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1278 .set_csma_params = at86rf230_set_csma_params,
1279 .set_frame_retries = at86rf230_set_frame_retries,
92f45f54 1280 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
8fad346f
PB
1281};
1282
a53d1f7c 1283static struct at86rf2xx_chip_data at86rf233_data = {
7a4ef918 1284 .t_sleep_cycle = 330,
984e0c68 1285 .t_channel_switch = 11,
09e536cd 1286 .t_reset_to_off = 26,
2e0571c0
AA
1287 .t_off_to_aack = 80,
1288 .t_off_to_tx_on = 80,
1d15d6b5
AA
1289 .t_frame = 4096,
1290 .t_p_ack = 545,
1d15d6b5 1291 .t_tx_timeout = 2000,
a53d1f7c
AA
1292 .rssi_base_val = -91,
1293 .set_channel = at86rf23x_set_channel,
a7d7eda9 1294 .get_desense_steps = at86rf23x_get_desens_steps
a53d1f7c
AA
1295};
1296
1297static struct at86rf2xx_chip_data at86rf231_data = {
7a4ef918 1298 .t_sleep_cycle = 330,
984e0c68 1299 .t_channel_switch = 24,
09e536cd 1300 .t_reset_to_off = 37,
2e0571c0
AA
1301 .t_off_to_aack = 110,
1302 .t_off_to_tx_on = 110,
1d15d6b5
AA
1303 .t_frame = 4096,
1304 .t_p_ack = 545,
1d15d6b5 1305 .t_tx_timeout = 2000,
a53d1f7c
AA
1306 .rssi_base_val = -91,
1307 .set_channel = at86rf23x_set_channel,
a7d7eda9 1308 .get_desense_steps = at86rf23x_get_desens_steps
a53d1f7c
AA
1309};
1310
1311static struct at86rf2xx_chip_data at86rf212_data = {
7a4ef918 1312 .t_sleep_cycle = 330,
984e0c68 1313 .t_channel_switch = 11,
09e536cd 1314 .t_reset_to_off = 26,
2e0571c0
AA
1315 .t_off_to_aack = 200,
1316 .t_off_to_tx_on = 200,
1d15d6b5
AA
1317 .t_frame = 4096,
1318 .t_p_ack = 545,
1d15d6b5 1319 .t_tx_timeout = 2000,
a53d1f7c
AA
1320 .rssi_base_val = -100,
1321 .set_channel = at86rf212_set_channel,
a7d7eda9 1322 .get_desense_steps = at86rf212_get_desens_steps
a53d1f7c
AA
1323};
1324
7b8e19b6 1325static int at86rf230_hw_init(struct at86rf230_local *lp)
1326{
1db0558e 1327 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
f76014f7 1328 unsigned int dvdd;
f2fdd67c 1329 u8 csma_seed[2];
7b8e19b6 1330
09e536cd 1331 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
7dcbd22a
PB
1332 if (rc)
1333 return rc;
7b8e19b6 1334
4af619ae 1335 irq_type = irq_get_trigger_type(lp->spi->irq);
1db0558e 1336 if (irq_type == IRQ_TYPE_EDGE_FALLING)
43b5abe0 1337 irq_pol = IRQ_ACTIVE_LOW;
43b5abe0 1338
18c65049 1339 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
43b5abe0
SH
1340 if (rc)
1341 return rc;
1342
6bd2b132
AA
1343 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1344 if (rc)
1345 return rc;
1346
057dad6f 1347 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
7b8e19b6 1348 if (rc)
1349 return rc;
1350
f2fdd67c
PB
1351 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1352 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1353 if (rc)
1354 return rc;
1355 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1356 if (rc)
1357 return rc;
1358
7b8e19b6 1359 /* CLKM changes are applied immediately */
1360 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1361 if (rc)
1362 return rc;
1363
1364 /* Turn CLKM Off */
1365 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1366 if (rc)
1367 return rc;
1368 /* Wait the next SLEEP cycle */
7a4ef918
AA
1369 usleep_range(lp->data->t_sleep_cycle,
1370 lp->data->t_sleep_cycle + 100);
7b8e19b6 1371
1cc9fc53 1372 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
7b8e19b6 1373 if (rc)
1374 return rc;
1cc9fc53 1375 if (!dvdd) {
7b8e19b6 1376 dev_err(&lp->spi->dev, "DVDD error\n");
1377 return -EINVAL;
1378 }
1379
05e3f2f3
AA
1380 /* Force setting slotted operation bit to 0. Sometimes the atben
1381 * sets this bit and I don't know why. We set this always force
1382 * to zero while probing.
1383 */
6cc6399c 1384 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
7b8e19b6 1385}
1386
fa2d3e94
AA
1387static struct at86rf230_platform_data *
1388at86rf230_get_pdata(struct spi_device *spi)
1389{
1390 struct at86rf230_platform_data *pdata;
fa2d3e94
AA
1391
1392 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1393 return spi->dev.platform_data;
1394
1395 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1396 if (!pdata)
1397 goto done;
1398
1399 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1400 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1401
fa2d3e94
AA
1402 spi->dev.platform_data = pdata;
1403done:
1404 return pdata;
1405}
1406
c8ee0f56
AA
1407static int
1408at86rf230_detect_device(struct at86rf230_local *lp)
1409{
1410 unsigned int part, version, val;
1411 u16 man_id = 0;
1412 const char *chip;
1413 int rc;
1414
1415 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1416 if (rc)
1417 return rc;
1418 man_id |= val;
1419
1420 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1421 if (rc)
1422 return rc;
1423 man_id |= (val << 8);
1424
1425 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1426 if (rc)
1427 return rc;
1428
1429 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1430 if (rc)
1431 return rc;
1432
1433 if (man_id != 0x001f) {
1434 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1435 man_id >> 8, man_id & 0xFF);
1436 return -EINVAL;
1437 }
1438
5a504397 1439 lp->hw->extra_tx_headroom = 0;
2ac0f3a3 1440 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
c8fc84ed 1441 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
92f45f54 1442 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
c8ee0f56
AA
1443
1444 switch (part) {
1445 case 2:
1446 chip = "at86rf230";
1447 rc = -ENOTSUPP;
1448 break;
1449 case 3:
1450 chip = "at86rf231";
a53d1f7c 1451 lp->data = &at86rf231_data;
5a504397 1452 lp->hw->phy->channels_supported[0] = 0x7FFF800;
fe58d016 1453 lp->hw->phy->current_channel = 11;
24ccb9f4 1454 lp->hw->phy->symbol_duration = 16;
c8ee0f56
AA
1455 break;
1456 case 7:
1457 chip = "at86rf212";
1458 if (version == 1) {
a53d1f7c 1459 lp->data = &at86rf212_data;
5a504397
AA
1460 lp->hw->flags |= IEEE802154_HW_LBT;
1461 lp->hw->phy->channels_supported[0] = 0x00007FF;
1462 lp->hw->phy->channels_supported[2] = 0x00007FF;
fe58d016 1463 lp->hw->phy->current_channel = 5;
24ccb9f4 1464 lp->hw->phy->symbol_duration = 25;
c8ee0f56
AA
1465 } else {
1466 rc = -ENOTSUPP;
1467 }
1468 break;
1469 case 11:
1470 chip = "at86rf233";
a53d1f7c 1471 lp->data = &at86rf233_data;
5a504397 1472 lp->hw->phy->channels_supported[0] = 0x7FFF800;
fe58d016 1473 lp->hw->phy->current_channel = 13;
24ccb9f4 1474 lp->hw->phy->symbol_duration = 16;
c8ee0f56
AA
1475 break;
1476 default:
1477 chip = "unkown";
1478 rc = -ENOTSUPP;
1479 break;
1480 }
1481
1482 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1483
1484 return rc;
1485}
1486
1d15d6b5
AA
1487static void
1488at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1489{
2e0571c0
AA
1490 lp->state.lp = lp;
1491 spi_message_init(&lp->state.msg);
1492 lp->state.msg.context = &lp->state;
1493 lp->state.trx.tx_buf = lp->state.buf;
1494 lp->state.trx.rx_buf = lp->state.buf;
1495 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1496
1d15d6b5
AA
1497 lp->irq.lp = lp;
1498 spi_message_init(&lp->irq.msg);
1499 lp->irq.msg.context = &lp->irq;
1500 lp->irq.trx.tx_buf = lp->irq.buf;
1501 lp->irq.trx.rx_buf = lp->irq.buf;
1502 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1503
1504 lp->tx.lp = lp;
1505 spi_message_init(&lp->tx.msg);
1506 lp->tx.msg.context = &lp->tx;
1507 lp->tx.trx.tx_buf = lp->tx.buf;
1508 lp->tx.trx.rx_buf = lp->tx.buf;
1509 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1510}
1511
bb1f4606 1512static int at86rf230_probe(struct spi_device *spi)
7b8e19b6 1513{
43b5abe0 1514 struct at86rf230_platform_data *pdata;
5a504397 1515 struct ieee802154_hw *hw;
7b8e19b6 1516 struct at86rf230_local *lp;
f76014f7 1517 unsigned int status;
4af619ae 1518 int rc, irq_type;
7b8e19b6 1519
1520 if (!spi->irq) {
1521 dev_err(&spi->dev, "no IRQ specified\n");
1522 return -EINVAL;
1523 }
1524
fa2d3e94 1525 pdata = at86rf230_get_pdata(spi);
43b5abe0
SH
1526 if (!pdata) {
1527 dev_err(&spi->dev, "no platform_data\n");
1528 return -EINVAL;
1529 }
1530
3fa27571 1531 if (gpio_is_valid(pdata->rstn)) {
0679e29b
AA
1532 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1533 GPIOF_OUT_INIT_HIGH, "rstn");
3fa27571
AA
1534 if (rc)
1535 return rc;
1536 }
7b8e19b6 1537
8fad346f 1538 if (gpio_is_valid(pdata->slp_tr)) {
0679e29b
AA
1539 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1540 GPIOF_OUT_INIT_LOW, "slp_tr");
7b8e19b6 1541 if (rc)
0679e29b 1542 return rc;
7b8e19b6 1543 }
1544
1545 /* Reset */
3fa27571
AA
1546 if (gpio_is_valid(pdata->rstn)) {
1547 udelay(1);
1548 gpio_set_value(pdata->rstn, 0);
1549 udelay(1);
1550 gpio_set_value(pdata->rstn, 1);
1551 usleep_range(120, 240);
1552 }
7b8e19b6 1553
5a504397
AA
1554 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1555 if (!hw)
640985ec
AA
1556 return -ENOMEM;
1557
5a504397
AA
1558 lp = hw->priv;
1559 lp->hw = hw;
640985ec 1560 lp->spi = spi;
5a504397 1561 hw->parent = &spi->dev;
7c118c1a 1562 hw->vif_data_size = sizeof(*lp);
f6f4e86a 1563 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
8fad346f 1564
f76014f7
AA
1565 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1566 if (IS_ERR(lp->regmap)) {
1567 rc = PTR_ERR(lp->regmap);
1568 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1569 rc);
1570 goto free_dev;
1571 }
1572
1d15d6b5
AA
1573 at86rf230_setup_spi_messages(lp);
1574
c8ee0f56
AA
1575 rc = at86rf230_detect_device(lp);
1576 if (rc < 0)
1577 goto free_dev;
1578
8fad346f 1579 spin_lock_init(&lp->lock);
2e0571c0 1580 init_completion(&lp->state_complete);
8fad346f
PB
1581
1582 spi_set_drvdata(spi, lp);
1583
7b8e19b6 1584 rc = at86rf230_hw_init(lp);
1585 if (rc)
1d15d6b5 1586 goto free_dev;
7b8e19b6 1587
19626946
AA
1588 /* Read irq status register to reset irq line */
1589 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
7b8e19b6 1590 if (rc)
1d15d6b5 1591 goto free_dev;
7b8e19b6 1592
1d15d6b5
AA
1593 irq_type = irq_get_trigger_type(spi->irq);
1594 if (!irq_type)
1595 irq_type = IRQF_TRIGGER_RISING;
1596
1597 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1598 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
057dad6f 1599 if (rc)
1d15d6b5 1600 goto free_dev;
057dad6f 1601
5a504397 1602 rc = ieee802154_register_hw(lp->hw);
7b8e19b6 1603 if (rc)
1d15d6b5 1604 goto free_dev;
7b8e19b6 1605
1606 return rc;
1607
640985ec 1608free_dev:
5a504397 1609 ieee802154_free_hw(lp->hw);
8fad346f 1610
7b8e19b6 1611 return rc;
1612}
1613
bb1f4606 1614static int at86rf230_remove(struct spi_device *spi)
7b8e19b6 1615{
1616 struct at86rf230_local *lp = spi_get_drvdata(spi);
1617
17e84a92
AA
1618 /* mask all at86rf230 irq's */
1619 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
5a504397
AA
1620 ieee802154_unregister_hw(lp->hw);
1621 ieee802154_free_hw(lp->hw);
7b8e19b6 1622 dev_dbg(&spi->dev, "unregistered at86rf230\n");
0679e29b 1623
7b8e19b6 1624 return 0;
1625}
1626
1086b4f6 1627static const struct of_device_id at86rf230_of_match[] = {
fa2d3e94
AA
1628 { .compatible = "atmel,at86rf230", },
1629 { .compatible = "atmel,at86rf231", },
1630 { .compatible = "atmel,at86rf233", },
1631 { .compatible = "atmel,at86rf212", },
1632 { },
1633};
835cb7d2 1634MODULE_DEVICE_TABLE(of, at86rf230_of_match);
fa2d3e94 1635
90b15520
AA
1636static const struct spi_device_id at86rf230_device_id[] = {
1637 { .name = "at86rf230", },
1638 { .name = "at86rf231", },
1639 { .name = "at86rf233", },
1640 { .name = "at86rf212", },
1641 { },
1642};
1643MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1644
7b8e19b6 1645static struct spi_driver at86rf230_driver = {
90b15520 1646 .id_table = at86rf230_device_id,
7b8e19b6 1647 .driver = {
fa2d3e94 1648 .of_match_table = of_match_ptr(at86rf230_of_match),
7b8e19b6 1649 .name = "at86rf230",
1650 .owner = THIS_MODULE,
1651 },
1652 .probe = at86rf230_probe,
bb1f4606 1653 .remove = at86rf230_remove,
7b8e19b6 1654};
1655
395a5738 1656module_spi_driver(at86rf230_driver);
7b8e19b6 1657
1658MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1659MODULE_LICENSE("GPL v2");