GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / ieee802154 / at86rf230.c
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  *
15  * Written by:
16  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
18  * Alexander Aring <aar@pengutronix.de>
19  */
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/hrtimer.h>
23 #include <linux/jiffies.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/at86rf230.h>
30 #include <linux/regmap.h>
31 #include <linux/skbuff.h>
32 #include <linux/of_gpio.h>
33 #include <linux/ieee802154.h>
34 #include <linux/debugfs.h>
35
36 #include <net/mac802154.h>
37 #include <net/cfg802154.h>
38
39 #include "at86rf230.h"
40
41 struct at86rf230_local;
42 /* at86rf2xx chip depend data.
43  * All timings are in us.
44  */
45 struct at86rf2xx_chip_data {
46         u16 t_sleep_cycle;
47         u16 t_channel_switch;
48         u16 t_reset_to_off;
49         u16 t_off_to_aack;
50         u16 t_off_to_tx_on;
51         u16 t_off_to_sleep;
52         u16 t_sleep_to_off;
53         u16 t_frame;
54         u16 t_p_ack;
55         int rssi_base_val;
56
57         int (*set_channel)(struct at86rf230_local *, u8, u8);
58         int (*set_txpower)(struct at86rf230_local *, s32);
59 };
60
61 #define AT86RF2XX_MAX_BUF               (127 + 3)
62 /* tx retries to access the TX_ON state
63  * if it's above then force change will be started.
64  *
65  * We assume the max_frame_retries (7) value of 802.15.4 here.
66  */
67 #define AT86RF2XX_MAX_TX_RETRIES        7
68 /* We use the recommended 5 minutes timeout to recalibrate */
69 #define AT86RF2XX_CAL_LOOP_TIMEOUT      (5 * 60 * HZ)
70
71 struct at86rf230_state_change {
72         struct at86rf230_local *lp;
73         int irq;
74
75         struct hrtimer timer;
76         struct spi_message msg;
77         struct spi_transfer trx;
78         u8 buf[AT86RF2XX_MAX_BUF];
79
80         void (*complete)(void *context);
81         u8 from_state;
82         u8 to_state;
83
84         bool free;
85 };
86
87 struct at86rf230_trac {
88         u64 success;
89         u64 success_data_pending;
90         u64 success_wait_for_ack;
91         u64 channel_access_failure;
92         u64 no_ack;
93         u64 invalid;
94 };
95
96 struct at86rf230_local {
97         struct spi_device *spi;
98
99         struct ieee802154_hw *hw;
100         struct at86rf2xx_chip_data *data;
101         struct regmap *regmap;
102         int slp_tr;
103         bool sleep;
104
105         struct completion state_complete;
106         struct at86rf230_state_change state;
107
108         unsigned long cal_timeout;
109         bool is_tx;
110         bool is_tx_from_off;
111         bool was_tx;
112         u8 tx_retry;
113         struct sk_buff *tx_skb;
114         struct at86rf230_state_change tx;
115
116         struct at86rf230_trac trac;
117 };
118
119 #define AT86RF2XX_NUMREGS 0x3F
120
121 static void
122 at86rf230_async_state_change(struct at86rf230_local *lp,
123                              struct at86rf230_state_change *ctx,
124                              const u8 state, void (*complete)(void *context));
125
126 static inline void
127 at86rf230_sleep(struct at86rf230_local *lp)
128 {
129         if (gpio_is_valid(lp->slp_tr)) {
130                 gpio_set_value(lp->slp_tr, 1);
131                 usleep_range(lp->data->t_off_to_sleep,
132                              lp->data->t_off_to_sleep + 10);
133                 lp->sleep = true;
134         }
135 }
136
137 static inline void
138 at86rf230_awake(struct at86rf230_local *lp)
139 {
140         if (gpio_is_valid(lp->slp_tr)) {
141                 gpio_set_value(lp->slp_tr, 0);
142                 usleep_range(lp->data->t_sleep_to_off,
143                              lp->data->t_sleep_to_off + 100);
144                 lp->sleep = false;
145         }
146 }
147
148 static inline int
149 __at86rf230_write(struct at86rf230_local *lp,
150                   unsigned int addr, unsigned int data)
151 {
152         bool sleep = lp->sleep;
153         int ret;
154
155         /* awake for register setting if sleep */
156         if (sleep)
157                 at86rf230_awake(lp);
158
159         ret = regmap_write(lp->regmap, addr, data);
160
161         /* sleep again if was sleeping */
162         if (sleep)
163                 at86rf230_sleep(lp);
164
165         return ret;
166 }
167
168 static inline int
169 __at86rf230_read(struct at86rf230_local *lp,
170                  unsigned int addr, unsigned int *data)
171 {
172         bool sleep = lp->sleep;
173         int ret;
174
175         /* awake for register setting if sleep */
176         if (sleep)
177                 at86rf230_awake(lp);
178
179         ret = regmap_read(lp->regmap, addr, data);
180
181         /* sleep again if was sleeping */
182         if (sleep)
183                 at86rf230_sleep(lp);
184
185         return ret;
186 }
187
188 static inline int
189 at86rf230_read_subreg(struct at86rf230_local *lp,
190                       unsigned int addr, unsigned int mask,
191                       unsigned int shift, unsigned int *data)
192 {
193         int rc;
194
195         rc = __at86rf230_read(lp, addr, data);
196         if (!rc)
197                 *data = (*data & mask) >> shift;
198
199         return rc;
200 }
201
202 static inline int
203 at86rf230_write_subreg(struct at86rf230_local *lp,
204                        unsigned int addr, unsigned int mask,
205                        unsigned int shift, unsigned int data)
206 {
207         bool sleep = lp->sleep;
208         int ret;
209
210         /* awake for register setting if sleep */
211         if (sleep)
212                 at86rf230_awake(lp);
213
214         ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
215
216         /* sleep again if was sleeping */
217         if (sleep)
218                 at86rf230_sleep(lp);
219
220         return ret;
221 }
222
223 static inline void
224 at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
225 {
226         gpio_set_value(lp->slp_tr, 1);
227         udelay(1);
228         gpio_set_value(lp->slp_tr, 0);
229 }
230
231 static bool
232 at86rf230_reg_writeable(struct device *dev, unsigned int reg)
233 {
234         switch (reg) {
235         case RG_TRX_STATE:
236         case RG_TRX_CTRL_0:
237         case RG_TRX_CTRL_1:
238         case RG_PHY_TX_PWR:
239         case RG_PHY_ED_LEVEL:
240         case RG_PHY_CC_CCA:
241         case RG_CCA_THRES:
242         case RG_RX_CTRL:
243         case RG_SFD_VALUE:
244         case RG_TRX_CTRL_2:
245         case RG_ANT_DIV:
246         case RG_IRQ_MASK:
247         case RG_VREG_CTRL:
248         case RG_BATMON:
249         case RG_XOSC_CTRL:
250         case RG_RX_SYN:
251         case RG_XAH_CTRL_1:
252         case RG_FTN_CTRL:
253         case RG_PLL_CF:
254         case RG_PLL_DCU:
255         case RG_SHORT_ADDR_0:
256         case RG_SHORT_ADDR_1:
257         case RG_PAN_ID_0:
258         case RG_PAN_ID_1:
259         case RG_IEEE_ADDR_0:
260         case RG_IEEE_ADDR_1:
261         case RG_IEEE_ADDR_2:
262         case RG_IEEE_ADDR_3:
263         case RG_IEEE_ADDR_4:
264         case RG_IEEE_ADDR_5:
265         case RG_IEEE_ADDR_6:
266         case RG_IEEE_ADDR_7:
267         case RG_XAH_CTRL_0:
268         case RG_CSMA_SEED_0:
269         case RG_CSMA_SEED_1:
270         case RG_CSMA_BE:
271                 return true;
272         default:
273                 return false;
274         }
275 }
276
277 static bool
278 at86rf230_reg_readable(struct device *dev, unsigned int reg)
279 {
280         bool rc;
281
282         /* all writeable are also readable */
283         rc = at86rf230_reg_writeable(dev, reg);
284         if (rc)
285                 return rc;
286
287         /* readonly regs */
288         switch (reg) {
289         case RG_TRX_STATUS:
290         case RG_PHY_RSSI:
291         case RG_IRQ_STATUS:
292         case RG_PART_NUM:
293         case RG_VERSION_NUM:
294         case RG_MAN_ID_1:
295         case RG_MAN_ID_0:
296                 return true;
297         default:
298                 return false;
299         }
300 }
301
302 static bool
303 at86rf230_reg_volatile(struct device *dev, unsigned int reg)
304 {
305         /* can be changed during runtime */
306         switch (reg) {
307         case RG_TRX_STATUS:
308         case RG_TRX_STATE:
309         case RG_PHY_RSSI:
310         case RG_PHY_ED_LEVEL:
311         case RG_IRQ_STATUS:
312         case RG_VREG_CTRL:
313         case RG_PLL_CF:
314         case RG_PLL_DCU:
315                 return true;
316         default:
317                 return false;
318         }
319 }
320
321 static bool
322 at86rf230_reg_precious(struct device *dev, unsigned int reg)
323 {
324         /* don't clear irq line on read */
325         switch (reg) {
326         case RG_IRQ_STATUS:
327                 return true;
328         default:
329                 return false;
330         }
331 }
332
333 static const struct regmap_config at86rf230_regmap_spi_config = {
334         .reg_bits = 8,
335         .val_bits = 8,
336         .write_flag_mask = CMD_REG | CMD_WRITE,
337         .read_flag_mask = CMD_REG,
338         .cache_type = REGCACHE_RBTREE,
339         .max_register = AT86RF2XX_NUMREGS,
340         .writeable_reg = at86rf230_reg_writeable,
341         .readable_reg = at86rf230_reg_readable,
342         .volatile_reg = at86rf230_reg_volatile,
343         .precious_reg = at86rf230_reg_precious,
344 };
345
346 static void
347 at86rf230_async_error_recover_complete(void *context)
348 {
349         struct at86rf230_state_change *ctx = context;
350         struct at86rf230_local *lp = ctx->lp;
351
352         if (ctx->free)
353                 kfree(ctx);
354
355         if (lp->was_tx) {
356                 lp->was_tx = 0;
357                 dev_kfree_skb_any(lp->tx_skb);
358                 ieee802154_wake_queue(lp->hw);
359         }
360 }
361
362 static void
363 at86rf230_async_error_recover(void *context)
364 {
365         struct at86rf230_state_change *ctx = context;
366         struct at86rf230_local *lp = ctx->lp;
367
368         if (lp->is_tx) {
369                 lp->was_tx = 1;
370                 lp->is_tx = 0;
371         }
372
373         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
374                                      at86rf230_async_error_recover_complete);
375 }
376
377 static inline void
378 at86rf230_async_error(struct at86rf230_local *lp,
379                       struct at86rf230_state_change *ctx, int rc)
380 {
381         dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
382
383         at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
384                                      at86rf230_async_error_recover);
385 }
386
387 /* Generic function to get some register value in async mode */
388 static void
389 at86rf230_async_read_reg(struct at86rf230_local *lp, u8 reg,
390                          struct at86rf230_state_change *ctx,
391                          void (*complete)(void *context))
392 {
393         int rc;
394
395         u8 *tx_buf = ctx->buf;
396
397         tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
398         ctx->msg.complete = complete;
399         rc = spi_async(lp->spi, &ctx->msg);
400         if (rc)
401                 at86rf230_async_error(lp, ctx, rc);
402 }
403
404 static void
405 at86rf230_async_write_reg(struct at86rf230_local *lp, u8 reg, u8 val,
406                           struct at86rf230_state_change *ctx,
407                           void (*complete)(void *context))
408 {
409         int rc;
410
411         ctx->buf[0] = (reg & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
412         ctx->buf[1] = val;
413         ctx->msg.complete = complete;
414         rc = spi_async(lp->spi, &ctx->msg);
415         if (rc)
416                 at86rf230_async_error(lp, ctx, rc);
417 }
418
419 static void
420 at86rf230_async_state_assert(void *context)
421 {
422         struct at86rf230_state_change *ctx = context;
423         struct at86rf230_local *lp = ctx->lp;
424         const u8 *buf = ctx->buf;
425         const u8 trx_state = buf[1] & TRX_STATE_MASK;
426
427         /* Assert state change */
428         if (trx_state != ctx->to_state) {
429                 /* Special handling if transceiver state is in
430                  * STATE_BUSY_RX_AACK and a SHR was detected.
431                  */
432                 if  (trx_state == STATE_BUSY_RX_AACK) {
433                         /* Undocumented race condition. If we send a state
434                          * change to STATE_RX_AACK_ON the transceiver could
435                          * change his state automatically to STATE_BUSY_RX_AACK
436                          * if a SHR was detected. This is not an error, but we
437                          * can't assert this.
438                          */
439                         if (ctx->to_state == STATE_RX_AACK_ON)
440                                 goto done;
441
442                         /* If we change to STATE_TX_ON without forcing and
443                          * transceiver state is STATE_BUSY_RX_AACK, we wait
444                          * 'tFrame + tPAck' receiving time. In this time the
445                          * PDU should be received. If the transceiver is still
446                          * in STATE_BUSY_RX_AACK, we run a force state change
447                          * to STATE_TX_ON. This is a timeout handling, if the
448                          * transceiver stucks in STATE_BUSY_RX_AACK.
449                          *
450                          * Additional we do several retries to try to get into
451                          * TX_ON state without forcing. If the retries are
452                          * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
453                          * will do a force change.
454                          */
455                         if (ctx->to_state == STATE_TX_ON ||
456                             ctx->to_state == STATE_TRX_OFF) {
457                                 u8 state = ctx->to_state;
458
459                                 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
460                                         state = STATE_FORCE_TRX_OFF;
461                                 lp->tx_retry++;
462
463                                 at86rf230_async_state_change(lp, ctx, state,
464                                                              ctx->complete);
465                                 return;
466                         }
467                 }
468
469                 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
470                          ctx->from_state, ctx->to_state, trx_state);
471         }
472
473 done:
474         if (ctx->complete)
475                 ctx->complete(context);
476 }
477
478 static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
479 {
480         struct at86rf230_state_change *ctx =
481                 container_of(timer, struct at86rf230_state_change, timer);
482         struct at86rf230_local *lp = ctx->lp;
483
484         at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
485                                  at86rf230_async_state_assert);
486
487         return HRTIMER_NORESTART;
488 }
489
490 /* Do state change timing delay. */
491 static void
492 at86rf230_async_state_delay(void *context)
493 {
494         struct at86rf230_state_change *ctx = context;
495         struct at86rf230_local *lp = ctx->lp;
496         struct at86rf2xx_chip_data *c = lp->data;
497         bool force = false;
498         ktime_t tim;
499
500         /* The force state changes are will show as normal states in the
501          * state status subregister. We change the to_state to the
502          * corresponding one and remember if it was a force change, this
503          * differs if we do a state change from STATE_BUSY_RX_AACK.
504          */
505         switch (ctx->to_state) {
506         case STATE_FORCE_TX_ON:
507                 ctx->to_state = STATE_TX_ON;
508                 force = true;
509                 break;
510         case STATE_FORCE_TRX_OFF:
511                 ctx->to_state = STATE_TRX_OFF;
512                 force = true;
513                 break;
514         default:
515                 break;
516         }
517
518         switch (ctx->from_state) {
519         case STATE_TRX_OFF:
520                 switch (ctx->to_state) {
521                 case STATE_RX_AACK_ON:
522                         tim = c->t_off_to_aack * NSEC_PER_USEC;
523                         /* state change from TRX_OFF to RX_AACK_ON to do a
524                          * calibration, we need to reset the timeout for the
525                          * next one.
526                          */
527                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
528                         goto change;
529                 case STATE_TX_ARET_ON:
530                 case STATE_TX_ON:
531                         tim = c->t_off_to_tx_on * NSEC_PER_USEC;
532                         /* state change from TRX_OFF to TX_ON or ARET_ON to do
533                          * a calibration, we need to reset the timeout for the
534                          * next one.
535                          */
536                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
537                         goto change;
538                 default:
539                         break;
540                 }
541                 break;
542         case STATE_BUSY_RX_AACK:
543                 switch (ctx->to_state) {
544                 case STATE_TRX_OFF:
545                 case STATE_TX_ON:
546                         /* Wait for worst case receiving time if we
547                          * didn't make a force change from BUSY_RX_AACK
548                          * to TX_ON or TRX_OFF.
549                          */
550                         if (!force) {
551                                 tim = (c->t_frame + c->t_p_ack) * NSEC_PER_USEC;
552                                 goto change;
553                         }
554                         break;
555                 default:
556                         break;
557                 }
558                 break;
559         /* Default value, means RESET state */
560         case STATE_P_ON:
561                 switch (ctx->to_state) {
562                 case STATE_TRX_OFF:
563                         tim = c->t_reset_to_off * NSEC_PER_USEC;
564                         goto change;
565                 default:
566                         break;
567                 }
568                 break;
569         default:
570                 break;
571         }
572
573         /* Default delay is 1us in the most cases */
574         udelay(1);
575         at86rf230_async_state_timer(&ctx->timer);
576         return;
577
578 change:
579         hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
580 }
581
582 static void
583 at86rf230_async_state_change_start(void *context)
584 {
585         struct at86rf230_state_change *ctx = context;
586         struct at86rf230_local *lp = ctx->lp;
587         u8 *buf = ctx->buf;
588         const u8 trx_state = buf[1] & TRX_STATE_MASK;
589
590         /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
591         if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
592                 udelay(1);
593                 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
594                                          at86rf230_async_state_change_start);
595                 return;
596         }
597
598         /* Check if we already are in the state which we change in */
599         if (trx_state == ctx->to_state) {
600                 if (ctx->complete)
601                         ctx->complete(context);
602                 return;
603         }
604
605         /* Set current state to the context of state change */
606         ctx->from_state = trx_state;
607
608         /* Going into the next step for a state change which do a timing
609          * relevant delay.
610          */
611         at86rf230_async_write_reg(lp, RG_TRX_STATE, ctx->to_state, ctx,
612                                   at86rf230_async_state_delay);
613 }
614
615 static void
616 at86rf230_async_state_change(struct at86rf230_local *lp,
617                              struct at86rf230_state_change *ctx,
618                              const u8 state, void (*complete)(void *context))
619 {
620         /* Initialization for the state change context */
621         ctx->to_state = state;
622         ctx->complete = complete;
623         at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
624                                  at86rf230_async_state_change_start);
625 }
626
627 static void
628 at86rf230_sync_state_change_complete(void *context)
629 {
630         struct at86rf230_state_change *ctx = context;
631         struct at86rf230_local *lp = ctx->lp;
632
633         complete(&lp->state_complete);
634 }
635
636 /* This function do a sync framework above the async state change.
637  * Some callbacks of the IEEE 802.15.4 driver interface need to be
638  * handled synchronously.
639  */
640 static int
641 at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
642 {
643         unsigned long rc;
644
645         at86rf230_async_state_change(lp, &lp->state, state,
646                                      at86rf230_sync_state_change_complete);
647
648         rc = wait_for_completion_timeout(&lp->state_complete,
649                                          msecs_to_jiffies(100));
650         if (!rc) {
651                 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
652                 return -ETIMEDOUT;
653         }
654
655         return 0;
656 }
657
658 static void
659 at86rf230_tx_complete(void *context)
660 {
661         struct at86rf230_state_change *ctx = context;
662         struct at86rf230_local *lp = ctx->lp;
663
664         ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
665         kfree(ctx);
666 }
667
668 static void
669 at86rf230_tx_on(void *context)
670 {
671         struct at86rf230_state_change *ctx = context;
672         struct at86rf230_local *lp = ctx->lp;
673
674         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
675                                      at86rf230_tx_complete);
676 }
677
678 static void
679 at86rf230_tx_trac_check(void *context)
680 {
681         struct at86rf230_state_change *ctx = context;
682         struct at86rf230_local *lp = ctx->lp;
683
684         if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
685                 u8 trac = TRAC_MASK(ctx->buf[1]);
686
687                 switch (trac) {
688                 case TRAC_SUCCESS:
689                         lp->trac.success++;
690                         break;
691                 case TRAC_SUCCESS_DATA_PENDING:
692                         lp->trac.success_data_pending++;
693                         break;
694                 case TRAC_CHANNEL_ACCESS_FAILURE:
695                         lp->trac.channel_access_failure++;
696                         break;
697                 case TRAC_NO_ACK:
698                         lp->trac.no_ack++;
699                         break;
700                 case TRAC_INVALID:
701                         lp->trac.invalid++;
702                         break;
703                 default:
704                         WARN_ONCE(1, "received tx trac status %d\n", trac);
705                         break;
706                 }
707         }
708
709         at86rf230_async_state_change(lp, ctx, STATE_TX_ON, at86rf230_tx_on);
710 }
711
712 static void
713 at86rf230_rx_read_frame_complete(void *context)
714 {
715         struct at86rf230_state_change *ctx = context;
716         struct at86rf230_local *lp = ctx->lp;
717         const u8 *buf = ctx->buf;
718         struct sk_buff *skb;
719         u8 len, lqi;
720
721         len = buf[1];
722         if (!ieee802154_is_valid_psdu_len(len)) {
723                 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
724                 len = IEEE802154_MTU;
725         }
726         lqi = buf[2 + len];
727
728         skb = dev_alloc_skb(IEEE802154_MTU);
729         if (!skb) {
730                 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
731                 kfree(ctx);
732                 return;
733         }
734
735         skb_put_data(skb, buf + 2, len);
736         ieee802154_rx_irqsafe(lp->hw, skb, lqi);
737         kfree(ctx);
738 }
739
740 static void
741 at86rf230_rx_trac_check(void *context)
742 {
743         struct at86rf230_state_change *ctx = context;
744         struct at86rf230_local *lp = ctx->lp;
745         u8 *buf = ctx->buf;
746         int rc;
747
748         if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
749                 u8 trac = TRAC_MASK(buf[1]);
750
751                 switch (trac) {
752                 case TRAC_SUCCESS:
753                         lp->trac.success++;
754                         break;
755                 case TRAC_SUCCESS_WAIT_FOR_ACK:
756                         lp->trac.success_wait_for_ack++;
757                         break;
758                 case TRAC_INVALID:
759                         lp->trac.invalid++;
760                         break;
761                 default:
762                         WARN_ONCE(1, "received rx trac status %d\n", trac);
763                         break;
764                 }
765         }
766
767         buf[0] = CMD_FB;
768         ctx->trx.len = AT86RF2XX_MAX_BUF;
769         ctx->msg.complete = at86rf230_rx_read_frame_complete;
770         rc = spi_async(lp->spi, &ctx->msg);
771         if (rc) {
772                 ctx->trx.len = 2;
773                 at86rf230_async_error(lp, ctx, rc);
774         }
775 }
776
777 static void
778 at86rf230_irq_trx_end(void *context)
779 {
780         struct at86rf230_state_change *ctx = context;
781         struct at86rf230_local *lp = ctx->lp;
782
783         if (lp->is_tx) {
784                 lp->is_tx = 0;
785                 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
786                                          at86rf230_tx_trac_check);
787         } else {
788                 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
789                                          at86rf230_rx_trac_check);
790         }
791 }
792
793 static void
794 at86rf230_irq_status(void *context)
795 {
796         struct at86rf230_state_change *ctx = context;
797         struct at86rf230_local *lp = ctx->lp;
798         const u8 *buf = ctx->buf;
799         u8 irq = buf[1];
800
801         enable_irq(lp->spi->irq);
802
803         if (irq & IRQ_TRX_END) {
804                 at86rf230_irq_trx_end(ctx);
805         } else {
806                 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
807                         irq);
808                 kfree(ctx);
809         }
810 }
811
812 static void
813 at86rf230_setup_spi_messages(struct at86rf230_local *lp,
814                              struct at86rf230_state_change *state)
815 {
816         state->lp = lp;
817         state->irq = lp->spi->irq;
818         spi_message_init(&state->msg);
819         state->msg.context = state;
820         state->trx.len = 2;
821         state->trx.tx_buf = state->buf;
822         state->trx.rx_buf = state->buf;
823         spi_message_add_tail(&state->trx, &state->msg);
824         hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
825         state->timer.function = at86rf230_async_state_timer;
826 }
827
828 static irqreturn_t at86rf230_isr(int irq, void *data)
829 {
830         struct at86rf230_local *lp = data;
831         struct at86rf230_state_change *ctx;
832         int rc;
833
834         disable_irq_nosync(irq);
835
836         ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
837         if (!ctx) {
838                 enable_irq(irq);
839                 return IRQ_NONE;
840         }
841
842         at86rf230_setup_spi_messages(lp, ctx);
843         /* tell on error handling to free ctx */
844         ctx->free = true;
845
846         ctx->buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
847         ctx->msg.complete = at86rf230_irq_status;
848         rc = spi_async(lp->spi, &ctx->msg);
849         if (rc) {
850                 at86rf230_async_error(lp, ctx, rc);
851                 enable_irq(irq);
852                 return IRQ_NONE;
853         }
854
855         return IRQ_HANDLED;
856 }
857
858 static void
859 at86rf230_write_frame_complete(void *context)
860 {
861         struct at86rf230_state_change *ctx = context;
862         struct at86rf230_local *lp = ctx->lp;
863
864         ctx->trx.len = 2;
865
866         if (gpio_is_valid(lp->slp_tr))
867                 at86rf230_slp_tr_rising_edge(lp);
868         else
869                 at86rf230_async_write_reg(lp, RG_TRX_STATE, STATE_BUSY_TX, ctx,
870                                           NULL);
871 }
872
873 static void
874 at86rf230_write_frame(void *context)
875 {
876         struct at86rf230_state_change *ctx = context;
877         struct at86rf230_local *lp = ctx->lp;
878         struct sk_buff *skb = lp->tx_skb;
879         u8 *buf = ctx->buf;
880         int rc;
881
882         lp->is_tx = 1;
883
884         buf[0] = CMD_FB | CMD_WRITE;
885         buf[1] = skb->len + 2;
886         memcpy(buf + 2, skb->data, skb->len);
887         ctx->trx.len = skb->len + 2;
888         ctx->msg.complete = at86rf230_write_frame_complete;
889         rc = spi_async(lp->spi, &ctx->msg);
890         if (rc) {
891                 ctx->trx.len = 2;
892                 at86rf230_async_error(lp, ctx, rc);
893         }
894 }
895
896 static void
897 at86rf230_xmit_tx_on(void *context)
898 {
899         struct at86rf230_state_change *ctx = context;
900         struct at86rf230_local *lp = ctx->lp;
901
902         at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
903                                      at86rf230_write_frame);
904 }
905
906 static void
907 at86rf230_xmit_start(void *context)
908 {
909         struct at86rf230_state_change *ctx = context;
910         struct at86rf230_local *lp = ctx->lp;
911
912         /* check if we change from off state */
913         if (lp->is_tx_from_off)
914                 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
915                                              at86rf230_write_frame);
916         else
917                 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
918                                              at86rf230_xmit_tx_on);
919 }
920
921 static int
922 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
923 {
924         struct at86rf230_local *lp = hw->priv;
925         struct at86rf230_state_change *ctx = &lp->tx;
926
927         lp->tx_skb = skb;
928         lp->tx_retry = 0;
929
930         /* After 5 minutes in PLL and the same frequency we run again the
931          * calibration loops which is recommended by at86rf2xx datasheets.
932          *
933          * The calibration is initiate by a state change from TRX_OFF
934          * to TX_ON, the lp->cal_timeout should be reinit by state_delay
935          * function then to start in the next 5 minutes.
936          */
937         if (time_is_before_jiffies(lp->cal_timeout)) {
938                 lp->is_tx_from_off = true;
939                 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
940                                              at86rf230_xmit_start);
941         } else {
942                 lp->is_tx_from_off = false;
943                 at86rf230_xmit_start(ctx);
944         }
945
946         return 0;
947 }
948
949 static int
950 at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
951 {
952         WARN_ON(!level);
953         *level = 0xbe;
954         return 0;
955 }
956
957 static int
958 at86rf230_start(struct ieee802154_hw *hw)
959 {
960         struct at86rf230_local *lp = hw->priv;
961
962         /* reset trac stats on start */
963         if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS))
964                 memset(&lp->trac, 0, sizeof(struct at86rf230_trac));
965
966         at86rf230_awake(lp);
967         enable_irq(lp->spi->irq);
968
969         return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
970 }
971
972 static void
973 at86rf230_stop(struct ieee802154_hw *hw)
974 {
975         struct at86rf230_local *lp = hw->priv;
976         u8 csma_seed[2];
977
978         at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
979
980         disable_irq(lp->spi->irq);
981
982         /* It's recommended to set random new csma_seeds before sleep state.
983          * Makes only sense in the stop callback, not doing this inside of
984          * at86rf230_sleep, this is also used when we don't transmit afterwards
985          * when calling start callback again.
986          */
987         get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
988         at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
989         at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
990
991         at86rf230_sleep(lp);
992 }
993
994 static int
995 at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
996 {
997         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
998 }
999
1000 #define AT86RF2XX_MAX_ED_LEVELS 0xF
1001 static const s32 at86rf233_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1002         -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, -7800, -7600,
1003         -7400, -7200, -7000, -6800, -6600, -6400,
1004 };
1005
1006 static const s32 at86rf231_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1007         -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
1008         -7100, -6900, -6700, -6500, -6300, -6100,
1009 };
1010
1011 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1012         -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
1013         -8000, -7800, -7600, -7400, -7200, -7000,
1014 };
1015
1016 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1017         -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
1018         -7800, -7600, -7400, -7200, -7000, -6800,
1019 };
1020
1021 static inline int
1022 at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
1023 {
1024         unsigned int cca_ed_thres;
1025         int rc;
1026
1027         rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
1028         if (rc < 0)
1029                 return rc;
1030
1031         switch (rssi_base_val) {
1032         case -98:
1033                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
1034                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
1035                 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
1036                 break;
1037         case -100:
1038                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1039                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1040                 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
1041                 break;
1042         default:
1043                 WARN_ON(1);
1044         }
1045
1046         return 0;
1047 }
1048
1049 static int
1050 at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
1051 {
1052         int rc;
1053
1054         if (channel == 0)
1055                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1056         else
1057                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1058         if (rc < 0)
1059                 return rc;
1060
1061         if (page == 0) {
1062                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
1063                 lp->data->rssi_base_val = -100;
1064         } else {
1065                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
1066                 lp->data->rssi_base_val = -98;
1067         }
1068         if (rc < 0)
1069                 return rc;
1070
1071         rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
1072         if (rc < 0)
1073                 return rc;
1074
1075         /* This sets the symbol_duration according frequency on the 212.
1076          * TODO move this handling while set channel and page in cfg802154.
1077          * We can do that, this timings are according 802.15.4 standard.
1078          * If we do that in cfg802154, this is a more generic calculation.
1079          *
1080          * This should also protected from ifs_timer. Means cancel timer and
1081          * init with a new value. For now, this is okay.
1082          */
1083         if (channel == 0) {
1084                 if (page == 0) {
1085                         /* SUB:0 and BPSK:0 -> BPSK-20 */
1086                         lp->hw->phy->symbol_duration = 50;
1087                 } else {
1088                         /* SUB:1 and BPSK:0 -> BPSK-40 */
1089                         lp->hw->phy->symbol_duration = 25;
1090                 }
1091         } else {
1092                 if (page == 0)
1093                         /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
1094                         lp->hw->phy->symbol_duration = 40;
1095                 else
1096                         /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1097                         lp->hw->phy->symbol_duration = 16;
1098         }
1099
1100         lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1101                                    lp->hw->phy->symbol_duration;
1102         lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1103                                    lp->hw->phy->symbol_duration;
1104
1105         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1106 }
1107
1108 static int
1109 at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1110 {
1111         struct at86rf230_local *lp = hw->priv;
1112         int rc;
1113
1114         rc = lp->data->set_channel(lp, page, channel);
1115         /* Wait for PLL */
1116         usleep_range(lp->data->t_channel_switch,
1117                      lp->data->t_channel_switch + 10);
1118
1119         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
1120         return rc;
1121 }
1122
1123 static int
1124 at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1125                            struct ieee802154_hw_addr_filt *filt,
1126                            unsigned long changed)
1127 {
1128         struct at86rf230_local *lp = hw->priv;
1129
1130         if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1131                 u16 addr = le16_to_cpu(filt->short_addr);
1132
1133                 dev_vdbg(&lp->spi->dev, "%s called for saddr\n", __func__);
1134                 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1135                 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1136         }
1137
1138         if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1139                 u16 pan = le16_to_cpu(filt->pan_id);
1140
1141                 dev_vdbg(&lp->spi->dev, "%s called for pan id\n", __func__);
1142                 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1143                 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1144         }
1145
1146         if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1147                 u8 i, addr[8];
1148
1149                 memcpy(addr, &filt->ieee_addr, 8);
1150                 dev_vdbg(&lp->spi->dev, "%s called for IEEE addr\n", __func__);
1151                 for (i = 0; i < 8; i++)
1152                         __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1153         }
1154
1155         if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1156                 dev_vdbg(&lp->spi->dev, "%s called for panc change\n", __func__);
1157                 if (filt->pan_coord)
1158                         at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1159                 else
1160                         at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1161         }
1162
1163         return 0;
1164 }
1165
1166 #define AT86RF23X_MAX_TX_POWERS 0xF
1167 static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1168         400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1169         -800, -1200, -1700,
1170 };
1171
1172 static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1173         300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1174         -900, -1200, -1700,
1175 };
1176
1177 #define AT86RF212_MAX_TX_POWERS 0x1F
1178 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1179         500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1180         -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1181         -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1182 };
1183
1184 static int
1185 at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1186 {
1187         u32 i;
1188
1189         for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1190                 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1191                         return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1192         }
1193
1194         return -EINVAL;
1195 }
1196
1197 static int
1198 at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1199 {
1200         u32 i;
1201
1202         for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1203                 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1204                         return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1205         }
1206
1207         return -EINVAL;
1208 }
1209
1210 static int
1211 at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
1212 {
1213         struct at86rf230_local *lp = hw->priv;
1214
1215         return lp->data->set_txpower(lp, mbm);
1216 }
1217
1218 static int
1219 at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1220 {
1221         struct at86rf230_local *lp = hw->priv;
1222
1223         return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1224 }
1225
1226 static int
1227 at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1228                        const struct wpan_phy_cca *cca)
1229 {
1230         struct at86rf230_local *lp = hw->priv;
1231         u8 val;
1232
1233         /* mapping 802.15.4 to driver spec */
1234         switch (cca->mode) {
1235         case NL802154_CCA_ENERGY:
1236                 val = 1;
1237                 break;
1238         case NL802154_CCA_CARRIER:
1239                 val = 2;
1240                 break;
1241         case NL802154_CCA_ENERGY_CARRIER:
1242                 switch (cca->opt) {
1243                 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1244                         val = 3;
1245                         break;
1246                 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1247                         val = 0;
1248                         break;
1249                 default:
1250                         return -EINVAL;
1251                 }
1252                 break;
1253         default:
1254                 return -EINVAL;
1255         }
1256
1257         return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1258 }
1259
1260 static int
1261 at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
1262 {
1263         struct at86rf230_local *lp = hw->priv;
1264         u32 i;
1265
1266         for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1267                 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1268                         return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1269         }
1270
1271         return -EINVAL;
1272 }
1273
1274 static int
1275 at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1276                           u8 retries)
1277 {
1278         struct at86rf230_local *lp = hw->priv;
1279         int rc;
1280
1281         rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1282         if (rc)
1283                 return rc;
1284
1285         rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1286         if (rc)
1287                 return rc;
1288
1289         return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1290 }
1291
1292 static int
1293 at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1294 {
1295         struct at86rf230_local *lp = hw->priv;
1296
1297         return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1298 }
1299
1300 static int
1301 at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1302 {
1303         struct at86rf230_local *lp = hw->priv;
1304         int rc;
1305
1306         if (on) {
1307                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1308                 if (rc < 0)
1309                         return rc;
1310
1311                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1312                 if (rc < 0)
1313                         return rc;
1314         } else {
1315                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1316                 if (rc < 0)
1317                         return rc;
1318
1319                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1320                 if (rc < 0)
1321                         return rc;
1322         }
1323
1324         return 0;
1325 }
1326
1327 static const struct ieee802154_ops at86rf230_ops = {
1328         .owner = THIS_MODULE,
1329         .xmit_async = at86rf230_xmit,
1330         .ed = at86rf230_ed,
1331         .set_channel = at86rf230_channel,
1332         .start = at86rf230_start,
1333         .stop = at86rf230_stop,
1334         .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
1335         .set_txpower = at86rf230_set_txpower,
1336         .set_lbt = at86rf230_set_lbt,
1337         .set_cca_mode = at86rf230_set_cca_mode,
1338         .set_cca_ed_level = at86rf230_set_cca_ed_level,
1339         .set_csma_params = at86rf230_set_csma_params,
1340         .set_frame_retries = at86rf230_set_frame_retries,
1341         .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
1342 };
1343
1344 static struct at86rf2xx_chip_data at86rf233_data = {
1345         .t_sleep_cycle = 330,
1346         .t_channel_switch = 11,
1347         .t_reset_to_off = 26,
1348         .t_off_to_aack = 80,
1349         .t_off_to_tx_on = 80,
1350         .t_off_to_sleep = 35,
1351         .t_sleep_to_off = 1000,
1352         .t_frame = 4096,
1353         .t_p_ack = 545,
1354         .rssi_base_val = -94,
1355         .set_channel = at86rf23x_set_channel,
1356         .set_txpower = at86rf23x_set_txpower,
1357 };
1358
1359 static struct at86rf2xx_chip_data at86rf231_data = {
1360         .t_sleep_cycle = 330,
1361         .t_channel_switch = 24,
1362         .t_reset_to_off = 37,
1363         .t_off_to_aack = 110,
1364         .t_off_to_tx_on = 110,
1365         .t_off_to_sleep = 35,
1366         .t_sleep_to_off = 1000,
1367         .t_frame = 4096,
1368         .t_p_ack = 545,
1369         .rssi_base_val = -91,
1370         .set_channel = at86rf23x_set_channel,
1371         .set_txpower = at86rf23x_set_txpower,
1372 };
1373
1374 static struct at86rf2xx_chip_data at86rf212_data = {
1375         .t_sleep_cycle = 330,
1376         .t_channel_switch = 11,
1377         .t_reset_to_off = 26,
1378         .t_off_to_aack = 200,
1379         .t_off_to_tx_on = 200,
1380         .t_off_to_sleep = 35,
1381         .t_sleep_to_off = 1000,
1382         .t_frame = 4096,
1383         .t_p_ack = 545,
1384         .rssi_base_val = -100,
1385         .set_channel = at86rf212_set_channel,
1386         .set_txpower = at86rf212_set_txpower,
1387 };
1388
1389 static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1390 {
1391         int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
1392         unsigned int dvdd;
1393         u8 csma_seed[2];
1394
1395         rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1396         if (rc)
1397                 return rc;
1398
1399         irq_type = irq_get_trigger_type(lp->spi->irq);
1400         if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1401             irq_type == IRQ_TYPE_LEVEL_LOW)
1402                 irq_pol = IRQ_ACTIVE_LOW;
1403
1404         rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1405         if (rc)
1406                 return rc;
1407
1408         rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1409         if (rc)
1410                 return rc;
1411
1412         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1413         if (rc)
1414                 return rc;
1415
1416         /* reset values differs in at86rf231 and at86rf233 */
1417         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1418         if (rc)
1419                 return rc;
1420
1421         get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1422         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1423         if (rc)
1424                 return rc;
1425         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1426         if (rc)
1427                 return rc;
1428
1429         /* CLKM changes are applied immediately */
1430         rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1431         if (rc)
1432                 return rc;
1433
1434         /* Turn CLKM Off */
1435         rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1436         if (rc)
1437                 return rc;
1438         /* Wait the next SLEEP cycle */
1439         usleep_range(lp->data->t_sleep_cycle,
1440                      lp->data->t_sleep_cycle + 100);
1441
1442         /* xtal_trim value is calculated by:
1443          * CL = 0.5 * (CX + CTRIM + CPAR)
1444          *
1445          * whereas:
1446          * CL = capacitor of used crystal
1447          * CX = connected capacitors at xtal pins
1448          * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1449          *        but this is different on each board setup. You need to fine
1450          *        tuning this value via CTRIM.
1451          * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1452          *         0 pF upto 4.5 pF.
1453          *
1454          * Examples:
1455          * atben transceiver:
1456          *
1457          * CL = 8 pF
1458          * CX = 12 pF
1459          * CPAR = 3 pF (We assume the magic constant from datasheet)
1460          * CTRIM = 0.9 pF
1461          *
1462          * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1463          *
1464          * xtal_trim = 0x3
1465          *
1466          * openlabs transceiver:
1467          *
1468          * CL = 16 pF
1469          * CX = 22 pF
1470          * CPAR = 3 pF (We assume the magic constant from datasheet)
1471          * CTRIM = 4.5 pF
1472          *
1473          * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1474          *
1475          * xtal_trim = 0xf
1476          */
1477         rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1478         if (rc)
1479                 return rc;
1480
1481         rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1482         if (rc)
1483                 return rc;
1484         if (!dvdd) {
1485                 dev_err(&lp->spi->dev, "DVDD error\n");
1486                 return -EINVAL;
1487         }
1488
1489         /* Force setting slotted operation bit to 0. Sometimes the atben
1490          * sets this bit and I don't know why. We set this always force
1491          * to zero while probing.
1492          */
1493         return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1494 }
1495
1496 static int
1497 at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1498                     u8 *xtal_trim)
1499 {
1500         struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1501         int ret;
1502
1503         if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1504                 if (!pdata)
1505                         return -ENOENT;
1506
1507                 *rstn = pdata->rstn;
1508                 *slp_tr = pdata->slp_tr;
1509                 *xtal_trim = pdata->xtal_trim;
1510                 return 0;
1511         }
1512
1513         *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1514         *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1515         ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1516         if (ret < 0 && ret != -EINVAL)
1517                 return ret;
1518
1519         return 0;
1520 }
1521
1522 static int
1523 at86rf230_detect_device(struct at86rf230_local *lp)
1524 {
1525         unsigned int part, version, val;
1526         u16 man_id = 0;
1527         const char *chip;
1528         int rc;
1529
1530         rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1531         if (rc)
1532                 return rc;
1533         man_id |= val;
1534
1535         rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1536         if (rc)
1537                 return rc;
1538         man_id |= (val << 8);
1539
1540         rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1541         if (rc)
1542                 return rc;
1543
1544         rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1545         if (rc)
1546                 return rc;
1547
1548         if (man_id != 0x001f) {
1549                 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1550                         man_id >> 8, man_id & 0xFF);
1551                 return -EINVAL;
1552         }
1553
1554         lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
1555                         IEEE802154_HW_CSMA_PARAMS |
1556                         IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1557                         IEEE802154_HW_PROMISCUOUS;
1558
1559         lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1560                              WPAN_PHY_FLAG_CCA_ED_LEVEL |
1561                              WPAN_PHY_FLAG_CCA_MODE;
1562
1563         lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1564                 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1565         lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1566                 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1567
1568         lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1569
1570         switch (part) {
1571         case 2:
1572                 chip = "at86rf230";
1573                 rc = -ENOTSUPP;
1574                 goto not_supp;
1575         case 3:
1576                 chip = "at86rf231";
1577                 lp->data = &at86rf231_data;
1578                 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1579                 lp->hw->phy->current_channel = 11;
1580                 lp->hw->phy->symbol_duration = 16;
1581                 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1582                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
1583                 lp->hw->phy->supported.cca_ed_levels = at86rf231_ed_levels;
1584                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf231_ed_levels);
1585                 break;
1586         case 7:
1587                 chip = "at86rf212";
1588                 lp->data = &at86rf212_data;
1589                 lp->hw->flags |= IEEE802154_HW_LBT;
1590                 lp->hw->phy->supported.channels[0] = 0x00007FF;
1591                 lp->hw->phy->supported.channels[2] = 0x00007FF;
1592                 lp->hw->phy->current_channel = 5;
1593                 lp->hw->phy->symbol_duration = 25;
1594                 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
1595                 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1596                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
1597                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1598                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1599                 break;
1600         case 11:
1601                 chip = "at86rf233";
1602                 lp->data = &at86rf233_data;
1603                 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1604                 lp->hw->phy->current_channel = 13;
1605                 lp->hw->phy->symbol_duration = 16;
1606                 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1607                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
1608                 lp->hw->phy->supported.cca_ed_levels = at86rf233_ed_levels;
1609                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf233_ed_levels);
1610                 break;
1611         default:
1612                 chip = "unknown";
1613                 rc = -ENOTSUPP;
1614                 goto not_supp;
1615         }
1616
1617         lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
1618         lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
1619
1620 not_supp:
1621         dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1622
1623         return rc;
1624 }
1625
1626 #ifdef CONFIG_IEEE802154_AT86RF230_DEBUGFS
1627 static struct dentry *at86rf230_debugfs_root;
1628
1629 static int at86rf230_stats_show(struct seq_file *file, void *offset)
1630 {
1631         struct at86rf230_local *lp = file->private;
1632
1633         seq_printf(file, "SUCCESS:\t\t%8llu\n", lp->trac.success);
1634         seq_printf(file, "SUCCESS_DATA_PENDING:\t%8llu\n",
1635                    lp->trac.success_data_pending);
1636         seq_printf(file, "SUCCESS_WAIT_FOR_ACK:\t%8llu\n",
1637                    lp->trac.success_wait_for_ack);
1638         seq_printf(file, "CHANNEL_ACCESS_FAILURE:\t%8llu\n",
1639                    lp->trac.channel_access_failure);
1640         seq_printf(file, "NO_ACK:\t\t\t%8llu\n", lp->trac.no_ack);
1641         seq_printf(file, "INVALID:\t\t%8llu\n", lp->trac.invalid);
1642         return 0;
1643 }
1644
1645 static int at86rf230_stats_open(struct inode *inode, struct file *file)
1646 {
1647         return single_open(file, at86rf230_stats_show, inode->i_private);
1648 }
1649
1650 static const struct file_operations at86rf230_stats_fops = {
1651         .open           = at86rf230_stats_open,
1652         .read           = seq_read,
1653         .llseek         = seq_lseek,
1654         .release        = single_release,
1655 };
1656
1657 static int at86rf230_debugfs_init(struct at86rf230_local *lp)
1658 {
1659         char debugfs_dir_name[DNAME_INLINE_LEN + 1] = "at86rf230-";
1660         struct dentry *stats;
1661
1662         strncat(debugfs_dir_name, dev_name(&lp->spi->dev), DNAME_INLINE_LEN);
1663
1664         at86rf230_debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
1665         if (!at86rf230_debugfs_root)
1666                 return -ENOMEM;
1667
1668         stats = debugfs_create_file("trac_stats", 0444,
1669                                     at86rf230_debugfs_root, lp,
1670                                     &at86rf230_stats_fops);
1671         if (!stats)
1672                 return -ENOMEM;
1673
1674         return 0;
1675 }
1676
1677 static void at86rf230_debugfs_remove(void)
1678 {
1679         debugfs_remove_recursive(at86rf230_debugfs_root);
1680 }
1681 #else
1682 static int at86rf230_debugfs_init(struct at86rf230_local *lp) { return 0; }
1683 static void at86rf230_debugfs_remove(void) { }
1684 #endif
1685
1686 static int at86rf230_probe(struct spi_device *spi)
1687 {
1688         struct ieee802154_hw *hw;
1689         struct at86rf230_local *lp;
1690         unsigned int status;
1691         int rc, irq_type, rstn, slp_tr;
1692         u8 xtal_trim = 0;
1693
1694         if (!spi->irq) {
1695                 dev_err(&spi->dev, "no IRQ specified\n");
1696                 return -EINVAL;
1697         }
1698
1699         rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
1700         if (rc < 0) {
1701                 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1702                 return rc;
1703         }
1704
1705         if (gpio_is_valid(rstn)) {
1706                 rc = devm_gpio_request_one(&spi->dev, rstn,
1707                                            GPIOF_OUT_INIT_HIGH, "rstn");
1708                 if (rc)
1709                         return rc;
1710         }
1711
1712         if (gpio_is_valid(slp_tr)) {
1713                 rc = devm_gpio_request_one(&spi->dev, slp_tr,
1714                                            GPIOF_OUT_INIT_LOW, "slp_tr");
1715                 if (rc)
1716                         return rc;
1717         }
1718
1719         /* Reset */
1720         if (gpio_is_valid(rstn)) {
1721                 udelay(1);
1722                 gpio_set_value_cansleep(rstn, 0);
1723                 udelay(1);
1724                 gpio_set_value_cansleep(rstn, 1);
1725                 usleep_range(120, 240);
1726         }
1727
1728         hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1729         if (!hw)
1730                 return -ENOMEM;
1731
1732         lp = hw->priv;
1733         lp->hw = hw;
1734         lp->spi = spi;
1735         lp->slp_tr = slp_tr;
1736         hw->parent = &spi->dev;
1737         ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1738
1739         lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1740         if (IS_ERR(lp->regmap)) {
1741                 rc = PTR_ERR(lp->regmap);
1742                 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1743                         rc);
1744                 goto free_dev;
1745         }
1746
1747         at86rf230_setup_spi_messages(lp, &lp->state);
1748         at86rf230_setup_spi_messages(lp, &lp->tx);
1749
1750         rc = at86rf230_detect_device(lp);
1751         if (rc < 0)
1752                 goto free_dev;
1753
1754         init_completion(&lp->state_complete);
1755
1756         spi_set_drvdata(spi, lp);
1757
1758         rc = at86rf230_hw_init(lp, xtal_trim);
1759         if (rc)
1760                 goto free_dev;
1761
1762         /* Read irq status register to reset irq line */
1763         rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1764         if (rc)
1765                 goto free_dev;
1766
1767         irq_type = irq_get_trigger_type(spi->irq);
1768         if (!irq_type)
1769                 irq_type = IRQF_TRIGGER_HIGH;
1770
1771         rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1772                               IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1773         if (rc)
1774                 goto free_dev;
1775
1776         /* disable_irq by default and wait for starting hardware */
1777         disable_irq(spi->irq);
1778
1779         /* going into sleep by default */
1780         at86rf230_sleep(lp);
1781
1782         rc = at86rf230_debugfs_init(lp);
1783         if (rc)
1784                 goto free_dev;
1785
1786         rc = ieee802154_register_hw(lp->hw);
1787         if (rc)
1788                 goto free_debugfs;
1789
1790         return rc;
1791
1792 free_debugfs:
1793         at86rf230_debugfs_remove();
1794 free_dev:
1795         ieee802154_free_hw(lp->hw);
1796
1797         return rc;
1798 }
1799
1800 static int at86rf230_remove(struct spi_device *spi)
1801 {
1802         struct at86rf230_local *lp = spi_get_drvdata(spi);
1803
1804         /* mask all at86rf230 irq's */
1805         at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
1806         ieee802154_unregister_hw(lp->hw);
1807         ieee802154_free_hw(lp->hw);
1808         at86rf230_debugfs_remove();
1809         dev_dbg(&spi->dev, "unregistered at86rf230\n");
1810
1811         return 0;
1812 }
1813
1814 static const struct of_device_id at86rf230_of_match[] = {
1815         { .compatible = "atmel,at86rf230", },
1816         { .compatible = "atmel,at86rf231", },
1817         { .compatible = "atmel,at86rf233", },
1818         { .compatible = "atmel,at86rf212", },
1819         { },
1820 };
1821 MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1822
1823 static const struct spi_device_id at86rf230_device_id[] = {
1824         { .name = "at86rf230", },
1825         { .name = "at86rf231", },
1826         { .name = "at86rf233", },
1827         { .name = "at86rf212", },
1828         { },
1829 };
1830 MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1831
1832 static struct spi_driver at86rf230_driver = {
1833         .id_table = at86rf230_device_id,
1834         .driver = {
1835                 .of_match_table = of_match_ptr(at86rf230_of_match),
1836                 .name   = "at86rf230",
1837         },
1838         .probe      = at86rf230_probe,
1839         .remove     = at86rf230_remove,
1840 };
1841
1842 module_spi_driver(at86rf230_driver);
1843
1844 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1845 MODULE_LICENSE("GPL v2");