1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for NXP MCR20A 802.15.4 Wireless-PAN Networking controller
5 * Copyright (C) 2018 Xue Liu <liuxuenetmail@gmail.com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/spi/spi.h>
11 #include <linux/workqueue.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/skbuff.h>
15 #include <linux/of_gpio.h>
16 #include <linux/regmap.h>
17 #include <linux/ieee802154.h>
18 #include <linux/debugfs.h>
20 #include <net/mac802154.h>
21 #include <net/cfg802154.h>
23 #include <linux/device.h>
27 #define SPI_COMMAND_BUFFER 3
29 #define REGISTER_READ BIT(7)
30 #define REGISTER_WRITE (0 << 7)
31 #define REGISTER_ACCESS (0 << 6)
32 #define PACKET_BUFF_BURST_ACCESS BIT(6)
33 #define PACKET_BUFF_BYTE_ACCESS BIT(5)
35 #define MCR20A_WRITE_REG(x) (x)
36 #define MCR20A_READ_REG(x) (REGISTER_READ | (x))
37 #define MCR20A_BURST_READ_PACKET_BUF (0xC0)
38 #define MCR20A_BURST_WRITE_PACKET_BUF (0x40)
40 #define MCR20A_CMD_REG 0x80
41 #define MCR20A_CMD_REG_MASK 0x3f
42 #define MCR20A_CMD_WRITE 0x40
43 #define MCR20A_CMD_FB 0x20
45 /* Number of Interrupt Request Status Register */
46 #define MCR20A_IRQSTS_NUM 2 /* only IRQ_STS1 and IRQ_STS2 */
50 MCR20A_CCA_ED
, // energy detect - CCA bit not active,
51 // not to be used for T and CCCA sequences
52 MCR20A_CCA_MODE1
, // energy detect - CCA bit ACTIVE
53 MCR20A_CCA_MODE2
, // 802.15.4 compliant signal detect - CCA bit ACTIVE
58 MCR20A_XCVSEQ_IDLE
= 0x00,
59 MCR20A_XCVSEQ_RX
= 0x01,
60 MCR20A_XCVSEQ_TX
= 0x02,
61 MCR20A_XCVSEQ_CCA
= 0x03,
62 MCR20A_XCVSEQ_TR
= 0x04,
63 MCR20A_XCVSEQ_CCCA
= 0x05,
66 /* IEEE-802.15.4 defined constants (2.4 GHz logical channels) */
67 #define MCR20A_MIN_CHANNEL (11)
68 #define MCR20A_MAX_CHANNEL (26)
69 #define MCR20A_CHANNEL_SPACING (5)
71 /* MCR20A CCA Threshold constans */
72 #define MCR20A_MIN_CCA_THRESHOLD (0x6EU)
73 #define MCR20A_MAX_CCA_THRESHOLD (0x00U)
76 #define MCR20A_OVERWRITE_VERSION (0x0C)
78 /* MCR20A PLL configurations */
79 static const u8 PLL_INT
[16] = {
80 /* 2405 */ 0x0B, /* 2410 */ 0x0B, /* 2415 */ 0x0B,
81 /* 2420 */ 0x0B, /* 2425 */ 0x0B, /* 2430 */ 0x0B,
82 /* 2435 */ 0x0C, /* 2440 */ 0x0C, /* 2445 */ 0x0C,
83 /* 2450 */ 0x0C, /* 2455 */ 0x0C, /* 2460 */ 0x0C,
84 /* 2465 */ 0x0D, /* 2470 */ 0x0D, /* 2475 */ 0x0D,
88 static const u8 PLL_FRAC
[16] = {
89 /* 2405 */ 0x28, /* 2410 */ 0x50, /* 2415 */ 0x78,
90 /* 2420 */ 0xA0, /* 2425 */ 0xC8, /* 2430 */ 0xF0,
91 /* 2435 */ 0x18, /* 2440 */ 0x40, /* 2445 */ 0x68,
92 /* 2450 */ 0x90, /* 2455 */ 0xB8, /* 2460 */ 0xE0,
93 /* 2465 */ 0x08, /* 2470 */ 0x30, /* 2475 */ 0x58,
97 static const struct reg_sequence mar20a_iar_overwrites
[] = {
98 { IAR_MISC_PAD_CTRL
, 0x02 },
99 { IAR_VCO_CTRL1
, 0xB3 },
100 { IAR_VCO_CTRL2
, 0x07 },
101 { IAR_PA_TUNING
, 0x71 },
102 { IAR_CHF_IBUF
, 0x2F },
103 { IAR_CHF_QBUF
, 0x2F },
104 { IAR_CHF_IRIN
, 0x24 },
105 { IAR_CHF_QRIN
, 0x24 },
106 { IAR_CHF_IL
, 0x24 },
107 { IAR_CHF_QL
, 0x24 },
108 { IAR_CHF_CC1
, 0x32 },
109 { IAR_CHF_CCL
, 0x1D },
110 { IAR_CHF_CC2
, 0x2D },
111 { IAR_CHF_IROUT
, 0x24 },
112 { IAR_CHF_QROUT
, 0x24 },
113 { IAR_PA_CAL
, 0x28 },
114 { IAR_AGC_THR1
, 0x55 },
115 { IAR_AGC_THR2
, 0x2D },
116 { IAR_ATT_RSSI1
, 0x5F },
117 { IAR_ATT_RSSI2
, 0x8F },
118 { IAR_RSSI_OFFSET
, 0x61 },
119 { IAR_CHF_PMA_GAIN
, 0x03 },
120 { IAR_CCA1_THRESH
, 0x50 },
121 { IAR_CORR_NVAL
, 0x13 },
122 { IAR_ACKDELAY
, 0x3D },
125 #define MCR20A_VALID_CHANNELS (0x07FFF800)
126 #define MCR20A_MAX_BUF (127)
128 #define printdev(X) (&X->spi->dev)
130 /* regmap information for Direct Access Register (DAR) access */
131 #define MCR20A_DAR_WRITE 0x01
132 #define MCR20A_DAR_READ 0x00
133 #define MCR20A_DAR_NUMREGS 0x3F
135 /* regmap information for Indirect Access Register (IAR) access */
136 #define MCR20A_IAR_ACCESS 0x80
137 #define MCR20A_IAR_NUMREGS 0xBEFF
139 /* Read/Write SPI Commands for DAR and IAR registers. */
140 #define MCR20A_READSHORT(reg) ((reg) << 1)
141 #define MCR20A_WRITESHORT(reg) ((reg) << 1 | 1)
142 #define MCR20A_READLONG(reg) (1 << 15 | (reg) << 5)
143 #define MCR20A_WRITELONG(reg) (1 << 15 | (reg) << 5 | 1 << 4)
145 /* Type definitions for link configuration of instantiable layers */
146 #define MCR20A_PHY_INDIRECT_QUEUE_SIZE (12)
149 mcr20a_dar_writeable(struct device
*dev
, unsigned int reg
)
160 case DAR_SRC_ADDRS_SUM_LSB
:
161 case DAR_SRC_ADDRS_SUM_MSB
:
165 case DAR_T2PRIMECMP_LSB
:
166 case DAR_T2PRIMECMP_MSB
:
177 case DAR_PLL_FRAC0_LSB
:
178 case DAR_PLL_FRAC0_MSB
:
181 case DAR_OVERWRITE_VER
:
182 case DAR_CLK_OUT_CTRL
:
191 mcr20a_dar_readable(struct device
*dev
, unsigned int reg
)
195 /* all writeable are also readable */
196 rc
= mcr20a_dar_writeable(dev
, reg
);
203 case DAR_CCA1_ED_FNL
:
204 case DAR_EVENT_TMR_LSB
:
205 case DAR_EVENT_TMR_MSB
:
206 case DAR_EVENT_TMR_USB
:
207 case DAR_TIMESTAMP_LSB
:
208 case DAR_TIMESTAMP_MSB
:
209 case DAR_TIMESTAMP_USB
:
212 case DAR_RSSI_CCA_CONT
:
220 mcr20a_dar_volatile(struct device
*dev
, unsigned int reg
)
222 /* can be changed during runtime */
227 /* use them in spi_async and regmap so it's volatile */
235 mcr20a_dar_precious(struct device
*dev
, unsigned int reg
)
237 /* don't clear irq line on read */
248 static const struct regmap_config mcr20a_dar_regmap
= {
249 .name
= "mcr20a_dar",
252 .write_flag_mask
= REGISTER_ACCESS
| REGISTER_WRITE
,
253 .read_flag_mask
= REGISTER_ACCESS
| REGISTER_READ
,
254 .cache_type
= REGCACHE_RBTREE
,
255 .writeable_reg
= mcr20a_dar_writeable
,
256 .readable_reg
= mcr20a_dar_readable
,
257 .volatile_reg
= mcr20a_dar_volatile
,
258 .precious_reg
= mcr20a_dar_precious
,
260 .can_multi_write
= true,
264 mcr20a_iar_writeable(struct device
*dev
, unsigned int reg
)
268 case IAR_PMC_LP_TRIM
:
269 case IAR_MACPANID0_LSB
:
270 case IAR_MACPANID0_MSB
:
271 case IAR_MACSHORTADDRS0_LSB
:
272 case IAR_MACSHORTADDRS0_MSB
:
273 case IAR_MACLONGADDRS0_0
:
274 case IAR_MACLONGADDRS0_8
:
275 case IAR_MACLONGADDRS0_16
:
276 case IAR_MACLONGADDRS0_24
:
277 case IAR_MACLONGADDRS0_32
:
278 case IAR_MACLONGADDRS0_40
:
279 case IAR_MACLONGADDRS0_48
:
280 case IAR_MACLONGADDRS0_56
:
281 case IAR_RX_FRAME_FILTER
:
283 case IAR_PLL_FRAC1_LSB
:
284 case IAR_PLL_FRAC1_MSB
:
285 case IAR_MACPANID1_LSB
:
286 case IAR_MACPANID1_MSB
:
287 case IAR_MACSHORTADDRS1_LSB
:
288 case IAR_MACSHORTADDRS1_MSB
:
289 case IAR_MACLONGADDRS1_0
:
290 case IAR_MACLONGADDRS1_8
:
291 case IAR_MACLONGADDRS1_16
:
292 case IAR_MACLONGADDRS1_24
:
293 case IAR_MACLONGADDRS1_32
:
294 case IAR_MACLONGADDRS1_40
:
295 case IAR_MACLONGADDRS1_48
:
296 case IAR_MACLONGADDRS1_56
:
297 case IAR_DUAL_PAN_CTRL
:
298 case IAR_DUAL_PAN_DWELL
:
299 case IAR_CCA1_THRESH
:
300 case IAR_CCA1_ED_OFFSET_COMP
:
301 case IAR_LQI_OFFSET_COMP
:
303 case IAR_CCA2_CORR_PEAKS
:
304 case IAR_CCA2_CORR_THRESH
:
305 case IAR_TMR_PRESCALE
:
306 case IAR_ANT_PAD_CTRL
:
307 case IAR_MISC_PAD_CTRL
:
310 case IAR_RX_WTR_MARK
:
315 case IAR_ANT_AGC_CTRL
:
321 case IAR_RSSI_OFFSET
:
323 case IAR_CHF_PMA_GAIN
:
345 mcr20a_iar_readable(struct device
*dev
, unsigned int reg
)
349 /* all writeable are also readable */
350 rc
= mcr20a_iar_writeable(dev
, reg
);
357 case IAR_DUAL_PAN_STS
:
358 case IAR_RX_BYTE_COUNT
:
359 case IAR_FILTERFAIL_CODE1
:
360 case IAR_FILTERFAIL_CODE2
:
369 mcr20a_iar_volatile(struct device
*dev
, unsigned int reg
)
371 /* can be changed during runtime */
373 case IAR_DUAL_PAN_STS
:
374 case IAR_RX_BYTE_COUNT
:
375 case IAR_FILTERFAIL_CODE1
:
376 case IAR_FILTERFAIL_CODE2
:
384 static const struct regmap_config mcr20a_iar_regmap
= {
385 .name
= "mcr20a_iar",
388 .write_flag_mask
= REGISTER_ACCESS
| REGISTER_WRITE
| IAR_INDEX
,
389 .read_flag_mask
= REGISTER_ACCESS
| REGISTER_READ
| IAR_INDEX
,
390 .cache_type
= REGCACHE_RBTREE
,
391 .writeable_reg
= mcr20a_iar_writeable
,
392 .readable_reg
= mcr20a_iar_readable
,
393 .volatile_reg
= mcr20a_iar_volatile
,
397 struct mcr20a_local
{
398 struct spi_device
*spi
;
400 struct ieee802154_hw
*hw
;
401 struct regmap
*regmap_dar
;
402 struct regmap
*regmap_iar
;
408 /* for writing tx buffer */
409 struct spi_message tx_buf_msg
;
411 /* burst buffer write command */
412 struct spi_transfer tx_xfer_header
;
414 /* len of tx packet */
415 struct spi_transfer tx_xfer_len
;
416 /* data of tx packet */
417 struct spi_transfer tx_xfer_buf
;
418 struct sk_buff
*tx_skb
;
420 /* for read length rxfifo */
421 struct spi_message reg_msg
;
423 u8 reg_data
[MCR20A_IRQSTS_NUM
];
424 struct spi_transfer reg_xfer_cmd
;
425 struct spi_transfer reg_xfer_data
;
427 /* receive handling */
428 struct spi_message rx_buf_msg
;
430 struct spi_transfer rx_xfer_header
;
432 struct spi_transfer rx_xfer_lqi
;
433 u8 rx_buf
[MCR20A_MAX_BUF
];
434 struct spi_transfer rx_xfer_buf
;
436 /* isr handling for reading intstat */
437 struct spi_message irq_msg
;
439 u8 irq_data
[MCR20A_IRQSTS_NUM
];
440 struct spi_transfer irq_xfer_data
;
441 struct spi_transfer irq_xfer_header
;
445 mcr20a_write_tx_buf_complete(void *context
)
447 struct mcr20a_local
*lp
= context
;
450 dev_dbg(printdev(lp
), "%s\n", __func__
);
452 lp
->reg_msg
.complete
= NULL
;
453 lp
->reg_cmd
[0] = MCR20A_WRITE_REG(DAR_PHY_CTRL1
);
454 lp
->reg_data
[0] = MCR20A_XCVSEQ_TX
;
455 lp
->reg_xfer_data
.len
= 1;
457 ret
= spi_async(lp
->spi
, &lp
->reg_msg
);
459 dev_err(printdev(lp
), "failed to set SEQ TX\n");
463 mcr20a_xmit(struct ieee802154_hw
*hw
, struct sk_buff
*skb
)
465 struct mcr20a_local
*lp
= hw
->priv
;
467 dev_dbg(printdev(lp
), "%s\n", __func__
);
471 print_hex_dump_debug("mcr20a tx: ", DUMP_PREFIX_OFFSET
, 16, 1,
472 skb
->data
, skb
->len
, 0);
476 lp
->reg_msg
.complete
= NULL
;
477 lp
->reg_cmd
[0] = MCR20A_WRITE_REG(DAR_PHY_CTRL1
);
478 lp
->reg_data
[0] = MCR20A_XCVSEQ_IDLE
;
479 lp
->reg_xfer_data
.len
= 1;
481 return spi_async(lp
->spi
, &lp
->reg_msg
);
485 mcr20a_ed(struct ieee802154_hw
*hw
, u8
*level
)
493 mcr20a_set_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
495 struct mcr20a_local
*lp
= hw
->priv
;
498 dev_dbg(printdev(lp
), "%s\n", __func__
);
500 /* freqency = ((PLL_INT+64) + (PLL_FRAC/65536)) * 32 MHz */
501 ret
= regmap_write(lp
->regmap_dar
, DAR_PLL_INT0
, PLL_INT
[channel
- 11]);
504 ret
= regmap_write(lp
->regmap_dar
, DAR_PLL_FRAC0_LSB
, 0x00);
507 ret
= regmap_write(lp
->regmap_dar
, DAR_PLL_FRAC0_MSB
,
508 PLL_FRAC
[channel
- 11]);
516 mcr20a_start(struct ieee802154_hw
*hw
)
518 struct mcr20a_local
*lp
= hw
->priv
;
521 dev_dbg(printdev(lp
), "%s\n", __func__
);
523 /* No slotted operation */
524 dev_dbg(printdev(lp
), "no slotted operation\n");
525 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL1
,
526 DAR_PHY_CTRL1_SLOTTED
, 0x0);
531 enable_irq(lp
->spi
->irq
);
533 /* Unmask SEQ interrupt */
534 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL2
,
535 DAR_PHY_CTRL2_SEQMSK
, 0x0);
539 /* Start the RX sequence */
540 dev_dbg(printdev(lp
), "start the RX sequence\n");
541 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL1
,
542 DAR_PHY_CTRL1_XCVSEQ_MASK
, MCR20A_XCVSEQ_RX
);
550 mcr20a_stop(struct ieee802154_hw
*hw
)
552 struct mcr20a_local
*lp
= hw
->priv
;
554 dev_dbg(printdev(lp
), "%s\n", __func__
);
556 /* stop all running sequence */
557 regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL1
,
558 DAR_PHY_CTRL1_XCVSEQ_MASK
, MCR20A_XCVSEQ_IDLE
);
561 disable_irq(lp
->spi
->irq
);
565 mcr20a_set_hw_addr_filt(struct ieee802154_hw
*hw
,
566 struct ieee802154_hw_addr_filt
*filt
,
567 unsigned long changed
)
569 struct mcr20a_local
*lp
= hw
->priv
;
571 dev_dbg(printdev(lp
), "%s\n", __func__
);
573 if (changed
& IEEE802154_AFILT_SADDR_CHANGED
) {
574 u16 addr
= le16_to_cpu(filt
->short_addr
);
576 regmap_write(lp
->regmap_iar
, IAR_MACSHORTADDRS0_LSB
, addr
);
577 regmap_write(lp
->regmap_iar
, IAR_MACSHORTADDRS0_MSB
, addr
>> 8);
580 if (changed
& IEEE802154_AFILT_PANID_CHANGED
) {
581 u16 pan
= le16_to_cpu(filt
->pan_id
);
583 regmap_write(lp
->regmap_iar
, IAR_MACPANID0_LSB
, pan
);
584 regmap_write(lp
->regmap_iar
, IAR_MACPANID0_MSB
, pan
>> 8);
587 if (changed
& IEEE802154_AFILT_IEEEADDR_CHANGED
) {
590 memcpy(addr
, &filt
->ieee_addr
, 8);
591 for (i
= 0; i
< 8; i
++)
592 regmap_write(lp
->regmap_iar
,
593 IAR_MACLONGADDRS0_0
+ i
, addr
[i
]);
596 if (changed
& IEEE802154_AFILT_PANC_CHANGED
) {
597 if (filt
->pan_coord
) {
598 regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL4
,
599 DAR_PHY_CTRL4_PANCORDNTR0
, 0x10);
601 regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL4
,
602 DAR_PHY_CTRL4_PANCORDNTR0
, 0x00);
609 /* -30 dBm to 10 dBm */
610 #define MCR20A_MAX_TX_POWERS 0x14
611 static const s32 mcr20a_powers
[MCR20A_MAX_TX_POWERS
+ 1] = {
612 -3000, -2800, -2600, -2400, -2200, -2000, -1800, -1600, -1400,
613 -1200, -1000, -800, -600, -400, -200, 0, 200, 400, 600, 800, 1000
617 mcr20a_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
619 struct mcr20a_local
*lp
= hw
->priv
;
622 dev_dbg(printdev(lp
), "%s(%d)\n", __func__
, mbm
);
624 for (i
= 0; i
< lp
->hw
->phy
->supported
.tx_powers_size
; i
++) {
625 if (lp
->hw
->phy
->supported
.tx_powers
[i
] == mbm
)
626 return regmap_write(lp
->regmap_dar
, DAR_PA_PWR
,
633 #define MCR20A_MAX_ED_LEVELS MCR20A_MIN_CCA_THRESHOLD
634 static s32 mcr20a_ed_levels
[MCR20A_MAX_ED_LEVELS
+ 1];
637 mcr20a_set_cca_mode(struct ieee802154_hw
*hw
,
638 const struct wpan_phy_cca
*cca
)
640 struct mcr20a_local
*lp
= hw
->priv
;
641 unsigned int cca_mode
= 0xff;
642 bool cca_mode_and
= false;
645 dev_dbg(printdev(lp
), "%s\n", __func__
);
647 /* mapping 802.15.4 to driver spec */
649 case NL802154_CCA_ENERGY
:
650 cca_mode
= MCR20A_CCA_MODE1
;
652 case NL802154_CCA_CARRIER
:
653 cca_mode
= MCR20A_CCA_MODE2
;
655 case NL802154_CCA_ENERGY_CARRIER
:
657 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
658 cca_mode
= MCR20A_CCA_MODE3
;
661 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
662 cca_mode
= MCR20A_CCA_MODE3
;
663 cca_mode_and
= false;
672 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL4
,
673 DAR_PHY_CTRL4_CCATYPE_MASK
,
674 cca_mode
<< DAR_PHY_CTRL4_CCATYPE_SHIFT
);
678 if (cca_mode
== MCR20A_CCA_MODE3
) {
680 ret
= regmap_update_bits(lp
->regmap_iar
, IAR_CCA_CTRL
,
681 IAR_CCA_CTRL_CCA3_AND_NOT_OR
,
684 ret
= regmap_update_bits(lp
->regmap_iar
,
686 IAR_CCA_CTRL_CCA3_AND_NOT_OR
,
697 mcr20a_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
699 struct mcr20a_local
*lp
= hw
->priv
;
702 dev_dbg(printdev(lp
), "%s\n", __func__
);
704 for (i
= 0; i
< hw
->phy
->supported
.cca_ed_levels_size
; i
++) {
705 if (hw
->phy
->supported
.cca_ed_levels
[i
] == mbm
)
706 return regmap_write(lp
->regmap_iar
, IAR_CCA1_THRESH
, i
);
713 mcr20a_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
715 struct mcr20a_local
*lp
= hw
->priv
;
717 u8 rx_frame_filter_reg
= 0x0;
719 dev_dbg(printdev(lp
), "%s(%d)\n", __func__
, on
);
722 /* All frame types accepted*/
723 rx_frame_filter_reg
&= ~(IAR_RX_FRAME_FLT_FRM_VER
);
724 rx_frame_filter_reg
|= (IAR_RX_FRAME_FLT_ACK_FT
|
725 IAR_RX_FRAME_FLT_NS_FT
);
727 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL4
,
728 DAR_PHY_CTRL4_PROMISCUOUS
,
729 DAR_PHY_CTRL4_PROMISCUOUS
);
733 ret
= regmap_write(lp
->regmap_iar
, IAR_RX_FRAME_FILTER
,
734 rx_frame_filter_reg
);
738 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL4
,
739 DAR_PHY_CTRL4_PROMISCUOUS
, 0x0);
743 ret
= regmap_write(lp
->regmap_iar
, IAR_RX_FRAME_FILTER
,
744 IAR_RX_FRAME_FLT_FRM_VER
|
745 IAR_RX_FRAME_FLT_BEACON_FT
|
746 IAR_RX_FRAME_FLT_DATA_FT
|
747 IAR_RX_FRAME_FLT_CMD_FT
);
755 static const struct ieee802154_ops mcr20a_hw_ops
= {
756 .owner
= THIS_MODULE
,
757 .xmit_async
= mcr20a_xmit
,
759 .set_channel
= mcr20a_set_channel
,
760 .start
= mcr20a_start
,
762 .set_hw_addr_filt
= mcr20a_set_hw_addr_filt
,
763 .set_txpower
= mcr20a_set_txpower
,
764 .set_cca_mode
= mcr20a_set_cca_mode
,
765 .set_cca_ed_level
= mcr20a_set_cca_ed_level
,
766 .set_promiscuous_mode
= mcr20a_set_promiscuous_mode
,
770 mcr20a_request_rx(struct mcr20a_local
*lp
)
772 dev_dbg(printdev(lp
), "%s\n", __func__
);
774 /* Start the RX sequence */
775 regmap_update_bits_async(lp
->regmap_dar
, DAR_PHY_CTRL1
,
776 DAR_PHY_CTRL1_XCVSEQ_MASK
, MCR20A_XCVSEQ_RX
);
782 mcr20a_handle_rx_read_buf_complete(void *context
)
784 struct mcr20a_local
*lp
= context
;
785 u8 len
= lp
->reg_data
[0] & DAR_RX_FRAME_LENGTH_MASK
;
788 dev_dbg(printdev(lp
), "%s\n", __func__
);
790 dev_dbg(printdev(lp
), "RX is done\n");
792 if (!ieee802154_is_valid_psdu_len(len
)) {
793 dev_vdbg(&lp
->spi
->dev
, "corrupted frame received\n");
794 len
= IEEE802154_MTU
;
797 len
= len
- 2; /* get rid of frame check field */
799 skb
= dev_alloc_skb(len
);
803 memcpy(skb_put(skb
, len
), lp
->rx_buf
, len
);
804 ieee802154_rx_irqsafe(lp
->hw
, skb
, lp
->rx_lqi
[0]);
806 print_hex_dump_debug("mcr20a rx: ", DUMP_PREFIX_OFFSET
, 16, 1,
808 pr_debug("mcr20a rx: lqi: %02hhx\n", lp
->rx_lqi
[0]);
810 /* start RX sequence */
811 mcr20a_request_rx(lp
);
815 mcr20a_handle_rx_read_len_complete(void *context
)
817 struct mcr20a_local
*lp
= context
;
821 dev_dbg(printdev(lp
), "%s\n", __func__
);
823 /* get the length of received frame */
824 len
= lp
->reg_data
[0] & DAR_RX_FRAME_LENGTH_MASK
;
825 dev_dbg(printdev(lp
), "frame len : %d\n", len
);
827 /* prepare to read the rx buf */
828 lp
->rx_buf_msg
.complete
= mcr20a_handle_rx_read_buf_complete
;
829 lp
->rx_header
[0] = MCR20A_BURST_READ_PACKET_BUF
;
830 lp
->rx_xfer_buf
.len
= len
;
832 ret
= spi_async(lp
->spi
, &lp
->rx_buf_msg
);
834 dev_err(printdev(lp
), "failed to read rx buffer length\n");
838 mcr20a_handle_rx(struct mcr20a_local
*lp
)
840 dev_dbg(printdev(lp
), "%s\n", __func__
);
841 lp
->reg_msg
.complete
= mcr20a_handle_rx_read_len_complete
;
842 lp
->reg_cmd
[0] = MCR20A_READ_REG(DAR_RX_FRM_LEN
);
843 lp
->reg_xfer_data
.len
= 1;
845 return spi_async(lp
->spi
, &lp
->reg_msg
);
849 mcr20a_handle_tx_complete(struct mcr20a_local
*lp
)
851 dev_dbg(printdev(lp
), "%s\n", __func__
);
853 ieee802154_xmit_complete(lp
->hw
, lp
->tx_skb
, false);
855 return mcr20a_request_rx(lp
);
859 mcr20a_handle_tx(struct mcr20a_local
*lp
)
863 dev_dbg(printdev(lp
), "%s\n", __func__
);
865 /* write tx buffer */
866 lp
->tx_header
[0] = MCR20A_BURST_WRITE_PACKET_BUF
;
867 /* add 2 bytes of FCS */
868 lp
->tx_len
[0] = lp
->tx_skb
->len
+ 2;
869 lp
->tx_xfer_buf
.tx_buf
= lp
->tx_skb
->data
;
870 /* add 1 byte psduLength */
871 lp
->tx_xfer_buf
.len
= lp
->tx_skb
->len
+ 1;
873 ret
= spi_async(lp
->spi
, &lp
->tx_buf_msg
);
875 dev_err(printdev(lp
), "SPI write Failed for TX buf\n");
883 mcr20a_irq_clean_complete(void *context
)
885 struct mcr20a_local
*lp
= context
;
886 u8 seq_state
= lp
->irq_data
[DAR_IRQ_STS1
] & DAR_PHY_CTRL1_XCVSEQ_MASK
;
888 dev_dbg(printdev(lp
), "%s\n", __func__
);
890 enable_irq(lp
->spi
->irq
);
892 dev_dbg(printdev(lp
), "IRQ STA1 (%02x) STA2 (%02x)\n",
893 lp
->irq_data
[DAR_IRQ_STS1
], lp
->irq_data
[DAR_IRQ_STS2
]);
896 /* TX IRQ, RX IRQ and SEQ IRQ */
897 case (DAR_IRQSTS1_TXIRQ
| DAR_IRQSTS1_SEQIRQ
):
900 dev_dbg(printdev(lp
), "TX is done. No ACK\n");
901 mcr20a_handle_tx_complete(lp
);
904 case (DAR_IRQSTS1_RXIRQ
| DAR_IRQSTS1_SEQIRQ
):
906 dev_dbg(printdev(lp
), "RX is starting\n");
907 mcr20a_handle_rx(lp
);
909 case (DAR_IRQSTS1_RXIRQ
| DAR_IRQSTS1_TXIRQ
| DAR_IRQSTS1_SEQIRQ
):
913 dev_dbg(printdev(lp
), "TX is done. Get ACK\n");
914 mcr20a_handle_tx_complete(lp
);
917 dev_dbg(printdev(lp
), "RX is starting\n");
918 mcr20a_handle_rx(lp
);
921 case (DAR_IRQSTS1_SEQIRQ
):
923 dev_dbg(printdev(lp
), "TX is starting\n");
924 mcr20a_handle_tx(lp
);
926 dev_dbg(printdev(lp
), "MCR20A is stop\n");
932 static void mcr20a_irq_status_complete(void *context
)
935 struct mcr20a_local
*lp
= context
;
937 dev_dbg(printdev(lp
), "%s\n", __func__
);
938 regmap_update_bits_async(lp
->regmap_dar
, DAR_PHY_CTRL1
,
939 DAR_PHY_CTRL1_XCVSEQ_MASK
, MCR20A_XCVSEQ_IDLE
);
941 lp
->reg_msg
.complete
= mcr20a_irq_clean_complete
;
942 lp
->reg_cmd
[0] = MCR20A_WRITE_REG(DAR_IRQ_STS1
);
943 memcpy(lp
->reg_data
, lp
->irq_data
, MCR20A_IRQSTS_NUM
);
944 lp
->reg_xfer_data
.len
= MCR20A_IRQSTS_NUM
;
946 ret
= spi_async(lp
->spi
, &lp
->reg_msg
);
949 dev_err(printdev(lp
), "failed to clean irq status\n");
952 static irqreturn_t
mcr20a_irq_isr(int irq
, void *data
)
954 struct mcr20a_local
*lp
= data
;
957 disable_irq_nosync(irq
);
959 lp
->irq_header
[0] = MCR20A_READ_REG(DAR_IRQ_STS1
);
961 ret
= spi_async(lp
->spi
, &lp
->irq_msg
);
970 static void mcr20a_hw_setup(struct mcr20a_local
*lp
)
973 struct ieee802154_hw
*hw
= lp
->hw
;
974 struct wpan_phy
*phy
= lp
->hw
->phy
;
976 dev_dbg(printdev(lp
), "%s\n", __func__
);
978 phy
->symbol_duration
= 16;
979 phy
->lifs_period
= 40;
980 phy
->sifs_period
= 12;
982 hw
->flags
= IEEE802154_HW_TX_OMIT_CKSUM
|
983 IEEE802154_HW_AFILT
|
984 IEEE802154_HW_PROMISCUOUS
;
986 phy
->flags
= WPAN_PHY_FLAG_TXPOWER
| WPAN_PHY_FLAG_CCA_ED_LEVEL
|
987 WPAN_PHY_FLAG_CCA_MODE
;
989 phy
->supported
.cca_modes
= BIT(NL802154_CCA_ENERGY
) |
990 BIT(NL802154_CCA_CARRIER
) | BIT(NL802154_CCA_ENERGY_CARRIER
);
991 phy
->supported
.cca_opts
= BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND
) |
992 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR
);
994 /* initiating cca_ed_levels */
995 for (i
= MCR20A_MAX_CCA_THRESHOLD
; i
< MCR20A_MIN_CCA_THRESHOLD
+ 1;
997 mcr20a_ed_levels
[i
] = -i
* 100;
1000 phy
->supported
.cca_ed_levels
= mcr20a_ed_levels
;
1001 phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(mcr20a_ed_levels
);
1003 phy
->cca
.mode
= NL802154_CCA_ENERGY
;
1005 phy
->supported
.channels
[0] = MCR20A_VALID_CHANNELS
;
1006 phy
->current_page
= 0;
1007 /* MCR20A default reset value */
1008 phy
->current_channel
= 20;
1009 phy
->symbol_duration
= 16;
1010 phy
->supported
.tx_powers
= mcr20a_powers
;
1011 phy
->supported
.tx_powers_size
= ARRAY_SIZE(mcr20a_powers
);
1012 phy
->cca_ed_level
= phy
->supported
.cca_ed_levels
[75];
1013 phy
->transmit_power
= phy
->supported
.tx_powers
[0x0F];
1017 mcr20a_setup_tx_spi_messages(struct mcr20a_local
*lp
)
1019 spi_message_init(&lp
->tx_buf_msg
);
1020 lp
->tx_buf_msg
.context
= lp
;
1021 lp
->tx_buf_msg
.complete
= mcr20a_write_tx_buf_complete
;
1023 lp
->tx_xfer_header
.len
= 1;
1024 lp
->tx_xfer_header
.tx_buf
= lp
->tx_header
;
1026 lp
->tx_xfer_len
.len
= 1;
1027 lp
->tx_xfer_len
.tx_buf
= lp
->tx_len
;
1029 spi_message_add_tail(&lp
->tx_xfer_header
, &lp
->tx_buf_msg
);
1030 spi_message_add_tail(&lp
->tx_xfer_len
, &lp
->tx_buf_msg
);
1031 spi_message_add_tail(&lp
->tx_xfer_buf
, &lp
->tx_buf_msg
);
1035 mcr20a_setup_rx_spi_messages(struct mcr20a_local
*lp
)
1037 spi_message_init(&lp
->reg_msg
);
1038 lp
->reg_msg
.context
= lp
;
1040 lp
->reg_xfer_cmd
.len
= 1;
1041 lp
->reg_xfer_cmd
.tx_buf
= lp
->reg_cmd
;
1042 lp
->reg_xfer_cmd
.rx_buf
= lp
->reg_cmd
;
1044 lp
->reg_xfer_data
.rx_buf
= lp
->reg_data
;
1045 lp
->reg_xfer_data
.tx_buf
= lp
->reg_data
;
1047 spi_message_add_tail(&lp
->reg_xfer_cmd
, &lp
->reg_msg
);
1048 spi_message_add_tail(&lp
->reg_xfer_data
, &lp
->reg_msg
);
1050 spi_message_init(&lp
->rx_buf_msg
);
1051 lp
->rx_buf_msg
.context
= lp
;
1052 lp
->rx_buf_msg
.complete
= mcr20a_handle_rx_read_buf_complete
;
1053 lp
->rx_xfer_header
.len
= 1;
1054 lp
->rx_xfer_header
.tx_buf
= lp
->rx_header
;
1055 lp
->rx_xfer_header
.rx_buf
= lp
->rx_header
;
1057 lp
->rx_xfer_buf
.rx_buf
= lp
->rx_buf
;
1059 lp
->rx_xfer_lqi
.len
= 1;
1060 lp
->rx_xfer_lqi
.rx_buf
= lp
->rx_lqi
;
1062 spi_message_add_tail(&lp
->rx_xfer_header
, &lp
->rx_buf_msg
);
1063 spi_message_add_tail(&lp
->rx_xfer_buf
, &lp
->rx_buf_msg
);
1064 spi_message_add_tail(&lp
->rx_xfer_lqi
, &lp
->rx_buf_msg
);
1068 mcr20a_setup_irq_spi_messages(struct mcr20a_local
*lp
)
1070 spi_message_init(&lp
->irq_msg
);
1071 lp
->irq_msg
.context
= lp
;
1072 lp
->irq_msg
.complete
= mcr20a_irq_status_complete
;
1073 lp
->irq_xfer_header
.len
= 1;
1074 lp
->irq_xfer_header
.tx_buf
= lp
->irq_header
;
1075 lp
->irq_xfer_header
.rx_buf
= lp
->irq_header
;
1077 lp
->irq_xfer_data
.len
= MCR20A_IRQSTS_NUM
;
1078 lp
->irq_xfer_data
.rx_buf
= lp
->irq_data
;
1080 spi_message_add_tail(&lp
->irq_xfer_header
, &lp
->irq_msg
);
1081 spi_message_add_tail(&lp
->irq_xfer_data
, &lp
->irq_msg
);
1085 mcr20a_phy_init(struct mcr20a_local
*lp
)
1088 unsigned int phy_reg
= 0;
1091 dev_dbg(printdev(lp
), "%s\n", __func__
);
1093 /* Disable Tristate on COCO MISO for SPI reads */
1094 ret
= regmap_write(lp
->regmap_iar
, IAR_MISC_PAD_CTRL
, 0x02);
1098 /* Clear all PP IRQ bits in IRQSTS1 to avoid unexpected interrupts
1099 * immediately after init
1101 ret
= regmap_write(lp
->regmap_dar
, DAR_IRQ_STS1
, 0xEF);
1105 /* Clear all PP IRQ bits in IRQSTS2 */
1106 ret
= regmap_write(lp
->regmap_dar
, DAR_IRQ_STS2
,
1107 DAR_IRQSTS2_ASM_IRQ
| DAR_IRQSTS2_PB_ERR_IRQ
|
1108 DAR_IRQSTS2_WAKE_IRQ
);
1112 /* Disable all timer interrupts */
1113 ret
= regmap_write(lp
->regmap_dar
, DAR_IRQ_STS3
, 0xFF);
1117 /* PHY_CTRL1 : default HW settings + AUTOACK enabled */
1118 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PHY_CTRL1
,
1119 DAR_PHY_CTRL1_AUTOACK
, DAR_PHY_CTRL1_AUTOACK
);
1121 /* PHY_CTRL2 : disable all interrupts */
1122 ret
= regmap_write(lp
->regmap_dar
, DAR_PHY_CTRL2
, 0xFF);
1126 /* PHY_CTRL3 : disable all timers and remaining interrupts */
1127 ret
= regmap_write(lp
->regmap_dar
, DAR_PHY_CTRL3
,
1128 DAR_PHY_CTRL3_ASM_MSK
| DAR_PHY_CTRL3_PB_ERR_MSK
|
1129 DAR_PHY_CTRL3_WAKE_MSK
);
1133 /* SRC_CTRL : enable Acknowledge Frame Pending and
1134 * Source Address Matching Enable
1136 ret
= regmap_write(lp
->regmap_dar
, DAR_SRC_CTRL
,
1137 DAR_SRC_CTRL_ACK_FRM_PND
|
1138 (DAR_SRC_CTRL_INDEX
<< DAR_SRC_CTRL_INDEX_SHIFT
));
1142 /* RX_FRAME_FILTER */
1143 /* FRM_VER[1:0] = b11. Accept FrameVersion 0 and 1 packets */
1144 ret
= regmap_write(lp
->regmap_iar
, IAR_RX_FRAME_FILTER
,
1145 IAR_RX_FRAME_FLT_FRM_VER
|
1146 IAR_RX_FRAME_FLT_BEACON_FT
|
1147 IAR_RX_FRAME_FLT_DATA_FT
|
1148 IAR_RX_FRAME_FLT_CMD_FT
);
1152 dev_info(printdev(lp
), "MCR20A DAR overwrites version: 0x%02x\n",
1153 MCR20A_OVERWRITE_VERSION
);
1155 /* Overwrites direct registers */
1156 ret
= regmap_write(lp
->regmap_dar
, DAR_OVERWRITE_VER
,
1157 MCR20A_OVERWRITE_VERSION
);
1161 /* Overwrites indirect registers */
1162 ret
= regmap_multi_reg_write(lp
->regmap_iar
, mar20a_iar_overwrites
,
1163 ARRAY_SIZE(mar20a_iar_overwrites
));
1167 /* Clear HW indirect queue */
1168 dev_dbg(printdev(lp
), "clear HW indirect queue\n");
1169 for (index
= 0; index
< MCR20A_PHY_INDIRECT_QUEUE_SIZE
; index
++) {
1170 phy_reg
= (u8
)(((index
& DAR_SRC_CTRL_INDEX
) <<
1171 DAR_SRC_CTRL_INDEX_SHIFT
)
1172 | (DAR_SRC_CTRL_SRCADDR_EN
)
1173 | (DAR_SRC_CTRL_INDEX_DISABLE
));
1174 ret
= regmap_write(lp
->regmap_dar
, DAR_SRC_CTRL
, phy_reg
);
1180 /* Assign HW Indirect hash table to PAN0 */
1181 ret
= regmap_read(lp
->regmap_iar
, IAR_DUAL_PAN_CTRL
, &phy_reg
);
1185 /* Clear current lvl */
1186 phy_reg
&= ~IAR_DUAL_PAN_CTRL_DUAL_PAN_SAM_LVL_MSK
;
1189 phy_reg
|= MCR20A_PHY_INDIRECT_QUEUE_SIZE
<<
1190 IAR_DUAL_PAN_CTRL_DUAL_PAN_SAM_LVL_SHIFT
;
1191 ret
= regmap_write(lp
->regmap_iar
, IAR_DUAL_PAN_CTRL
, phy_reg
);
1195 /* Set CCA threshold to -75 dBm */
1196 ret
= regmap_write(lp
->regmap_iar
, IAR_CCA1_THRESH
, 0x4B);
1200 /* Set prescaller to obtain 1 symbol (16us) timebase */
1201 ret
= regmap_write(lp
->regmap_iar
, IAR_TMR_PRESCALE
, 0x05);
1205 /* Enable autodoze mode. */
1206 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_PWR_MODES
,
1207 DAR_PWR_MODES_AUTODOZE
,
1208 DAR_PWR_MODES_AUTODOZE
);
1212 /* Disable clk_out */
1213 ret
= regmap_update_bits(lp
->regmap_dar
, DAR_CLK_OUT_CTRL
,
1214 DAR_CLK_OUT_CTRL_EN
, 0x0);
1225 mcr20a_probe(struct spi_device
*spi
)
1227 struct ieee802154_hw
*hw
;
1228 struct mcr20a_local
*lp
;
1229 struct gpio_desc
*rst_b
;
1233 dev_dbg(&spi
->dev
, "%s\n", __func__
);
1236 dev_err(&spi
->dev
, "no IRQ specified\n");
1240 rst_b
= devm_gpiod_get(&spi
->dev
, "rst_b", GPIOD_OUT_HIGH
);
1241 if (IS_ERR(rst_b
)) {
1242 ret
= PTR_ERR(rst_b
);
1243 if (ret
!= -EPROBE_DEFER
)
1244 dev_err(&spi
->dev
, "Failed to get 'rst_b' gpio: %d", ret
);
1249 usleep_range(10, 20);
1250 gpiod_set_value_cansleep(rst_b
, 1);
1251 usleep_range(10, 20);
1252 gpiod_set_value_cansleep(rst_b
, 0);
1253 usleep_range(120, 240);
1255 /* allocate ieee802154_hw and private data */
1256 hw
= ieee802154_alloc_hw(sizeof(*lp
), &mcr20a_hw_ops
);
1258 dev_crit(&spi
->dev
, "ieee802154_alloc_hw failed\n");
1262 /* init mcr20a local data */
1267 /* init ieee802154_hw */
1268 hw
->parent
= &spi
->dev
;
1269 ieee802154_random_extended_addr(&hw
->phy
->perm_extended_addr
);
1272 lp
->buf
= devm_kzalloc(&spi
->dev
, SPI_COMMAND_BUFFER
, GFP_KERNEL
);
1279 mcr20a_setup_tx_spi_messages(lp
);
1280 mcr20a_setup_rx_spi_messages(lp
);
1281 mcr20a_setup_irq_spi_messages(lp
);
1284 lp
->regmap_dar
= devm_regmap_init_spi(spi
, &mcr20a_dar_regmap
);
1285 if (IS_ERR(lp
->regmap_dar
)) {
1286 ret
= PTR_ERR(lp
->regmap_dar
);
1287 dev_err(&spi
->dev
, "Failed to allocate dar map: %d\n",
1292 lp
->regmap_iar
= devm_regmap_init_spi(spi
, &mcr20a_iar_regmap
);
1293 if (IS_ERR(lp
->regmap_iar
)) {
1294 ret
= PTR_ERR(lp
->regmap_iar
);
1295 dev_err(&spi
->dev
, "Failed to allocate iar map: %d\n", ret
);
1299 mcr20a_hw_setup(lp
);
1301 spi_set_drvdata(spi
, lp
);
1303 ret
= mcr20a_phy_init(lp
);
1305 dev_crit(&spi
->dev
, "mcr20a_phy_init failed\n");
1309 irq_type
= irq_get_trigger_type(spi
->irq
);
1311 irq_type
= IRQF_TRIGGER_FALLING
;
1313 ret
= devm_request_irq(&spi
->dev
, spi
->irq
, mcr20a_irq_isr
,
1314 irq_type
, dev_name(&spi
->dev
), lp
);
1316 dev_err(&spi
->dev
, "could not request_irq for mcr20a\n");
1321 /* disable_irq by default and wait for starting hardware */
1322 disable_irq(spi
->irq
);
1324 ret
= ieee802154_register_hw(hw
);
1326 dev_crit(&spi
->dev
, "ieee802154_register_hw failed\n");
1333 ieee802154_free_hw(lp
->hw
);
1338 static int mcr20a_remove(struct spi_device
*spi
)
1340 struct mcr20a_local
*lp
= spi_get_drvdata(spi
);
1342 dev_dbg(&spi
->dev
, "%s\n", __func__
);
1344 ieee802154_unregister_hw(lp
->hw
);
1345 ieee802154_free_hw(lp
->hw
);
1350 static const struct of_device_id mcr20a_of_match
[] = {
1351 { .compatible
= "nxp,mcr20a", },
1354 MODULE_DEVICE_TABLE(of
, mcr20a_of_match
);
1356 static const struct spi_device_id mcr20a_device_id
[] = {
1357 { .name
= "mcr20a", },
1360 MODULE_DEVICE_TABLE(spi
, mcr20a_device_id
);
1362 static struct spi_driver mcr20a_driver
= {
1363 .id_table
= mcr20a_device_id
,
1365 .of_match_table
= of_match_ptr(mcr20a_of_match
),
1368 .probe
= mcr20a_probe
,
1369 .remove
= mcr20a_remove
,
1372 module_spi_driver(mcr20a_driver
);
1374 MODULE_DESCRIPTION("MCR20A Transceiver Driver");
1375 MODULE_LICENSE("GPL v2");
1376 MODULE_AUTHOR("Xue Liu <liuxuenetmail@gmail>");