blob: cbfc8c5b6a343761c6934ca8f1962da6b067f3bc [file] [log] [blame]
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001/*
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 *
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000015 * Written by:
16 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Alexander Aring01ebd602014-07-03 00:20:55 +020018 * Alexander Aring <aar@pengutronix.de>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000019 */
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020023#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000024#include <linux/gpio.h>
25#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000026#include <linux/spinlock.h>
27#include <linux/spi/spi.h>
28#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020029#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000030#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010031#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020032#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000033
34#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020035#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000036
Alexander Aringa53d1f72014-07-03 00:20:46 +020037struct at86rf230_local;
38/* at86rf2xx chip depend data.
39 * All timings are in us.
40 */
41struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020042 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020043 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020044 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020045 u16 t_off_to_aack;
46 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020047 u16 t_frame;
48 u16 t_p_ack;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020049 /* completion timeout for tx in msecs */
50 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020051 int rssi_base_val;
52
Alexander Aringe37d2ec2014-10-28 18:21:19 +010053 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020054 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020055};
56
Alexander Aring1d15d6b2014-07-03 00:20:48 +020057#define AT86RF2XX_MAX_BUF (127 + 3)
58
59struct at86rf230_state_change {
60 struct at86rf230_local *lp;
61
62 struct spi_message msg;
63 struct spi_transfer trx;
64 u8 buf[AT86RF2XX_MAX_BUF];
65
66 void (*complete)(void *context);
67 u8 from_state;
68 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020069
70 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020071};
72
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000073struct at86rf230_local {
74 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000075
Alexander Aring5a504392014-10-25 17:16:34 +020076 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020077 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020078 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
Alexander Aring2e0571c2014-07-03 00:20:51 +020080 struct completion state_complete;
81 struct at86rf230_state_change state;
82
Alexander Aring1d15d6b2014-07-03 00:20:48 +020083 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020084
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010085 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020086 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 bool is_tx;
88 /* spinlock for is_tx protection */
89 spinlock_t lock;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020090 struct sk_buff *tx_skb;
91 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000092};
93
94#define RG_TRX_STATUS (0x01)
95#define SR_TRX_STATUS 0x01, 0x1f, 0
96#define SR_RESERVED_01_3 0x01, 0x20, 5
97#define SR_CCA_STATUS 0x01, 0x40, 6
98#define SR_CCA_DONE 0x01, 0x80, 7
99#define RG_TRX_STATE (0x02)
100#define SR_TRX_CMD 0x02, 0x1f, 0
101#define SR_TRAC_STATUS 0x02, 0xe0, 5
102#define RG_TRX_CTRL_0 (0x03)
103#define SR_CLKM_CTRL 0x03, 0x07, 0
104#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
105#define SR_PAD_IO_CLKM 0x03, 0x30, 4
106#define SR_PAD_IO 0x03, 0xc0, 6
107#define RG_TRX_CTRL_1 (0x04)
108#define SR_IRQ_POLARITY 0x04, 0x01, 0
109#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
110#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
111#define SR_RX_BL_CTRL 0x04, 0x10, 4
112#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
113#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
114#define SR_PA_EXT_EN 0x04, 0x80, 7
115#define RG_PHY_TX_PWR (0x05)
116#define SR_TX_PWR 0x05, 0x0f, 0
117#define SR_PA_LT 0x05, 0x30, 4
118#define SR_PA_BUF_LT 0x05, 0xc0, 6
119#define RG_PHY_RSSI (0x06)
120#define SR_RSSI 0x06, 0x1f, 0
121#define SR_RND_VALUE 0x06, 0x60, 5
122#define SR_RX_CRC_VALID 0x06, 0x80, 7
123#define RG_PHY_ED_LEVEL (0x07)
124#define SR_ED_LEVEL 0x07, 0xff, 0
125#define RG_PHY_CC_CCA (0x08)
126#define SR_CHANNEL 0x08, 0x1f, 0
127#define SR_CCA_MODE 0x08, 0x60, 5
128#define SR_CCA_REQUEST 0x08, 0x80, 7
129#define RG_CCA_THRES (0x09)
130#define SR_CCA_ED_THRES 0x09, 0x0f, 0
131#define SR_RESERVED_09_1 0x09, 0xf0, 4
132#define RG_RX_CTRL (0x0a)
133#define SR_PDT_THRES 0x0a, 0x0f, 0
134#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
135#define RG_SFD_VALUE (0x0b)
136#define SR_SFD_VALUE 0x0b, 0xff, 0
137#define RG_TRX_CTRL_2 (0x0c)
138#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100139#define SR_SUB_MODE 0x0c, 0x04, 2
140#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100141#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
142#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000143#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
144#define RG_ANT_DIV (0x0d)
145#define SR_ANT_CTRL 0x0d, 0x03, 0
146#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
147#define SR_ANT_DIV_EN 0x0d, 0x08, 3
148#define SR_RESERVED_0d_2 0x0d, 0x70, 4
149#define SR_ANT_SEL 0x0d, 0x80, 7
150#define RG_IRQ_MASK (0x0e)
151#define SR_IRQ_MASK 0x0e, 0xff, 0
152#define RG_IRQ_STATUS (0x0f)
153#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
154#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
155#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
156#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
157#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
158#define SR_IRQ_5_AMI 0x0f, 0x20, 5
159#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
160#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
161#define RG_VREG_CTRL (0x10)
162#define SR_RESERVED_10_6 0x10, 0x03, 0
163#define SR_DVDD_OK 0x10, 0x04, 2
164#define SR_DVREG_EXT 0x10, 0x08, 3
165#define SR_RESERVED_10_3 0x10, 0x30, 4
166#define SR_AVDD_OK 0x10, 0x40, 6
167#define SR_AVREG_EXT 0x10, 0x80, 7
168#define RG_BATMON (0x11)
169#define SR_BATMON_VTH 0x11, 0x0f, 0
170#define SR_BATMON_HR 0x11, 0x10, 4
171#define SR_BATMON_OK 0x11, 0x20, 5
172#define SR_RESERVED_11_1 0x11, 0xc0, 6
173#define RG_XOSC_CTRL (0x12)
174#define SR_XTAL_TRIM 0x12, 0x0f, 0
175#define SR_XTAL_MODE 0x12, 0xf0, 4
176#define RG_RX_SYN (0x15)
177#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
178#define SR_RESERVED_15_2 0x15, 0x70, 4
179#define SR_RX_PDT_DIS 0x15, 0x80, 7
180#define RG_XAH_CTRL_1 (0x17)
181#define SR_RESERVED_17_8 0x17, 0x01, 0
182#define SR_AACK_PROM_MODE 0x17, 0x02, 1
183#define SR_AACK_ACK_TIME 0x17, 0x04, 2
184#define SR_RESERVED_17_5 0x17, 0x08, 3
185#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
186#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100187#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000188#define SR_RESERVED_17_1 0x17, 0x80, 7
189#define RG_FTN_CTRL (0x18)
190#define SR_RESERVED_18_2 0x18, 0x7f, 0
191#define SR_FTN_START 0x18, 0x80, 7
192#define RG_PLL_CF (0x1a)
193#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
194#define SR_PLL_CF_START 0x1a, 0x80, 7
195#define RG_PLL_DCU (0x1b)
196#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
197#define SR_RESERVED_1b_2 0x1b, 0x40, 6
198#define SR_PLL_DCU_START 0x1b, 0x80, 7
199#define RG_PART_NUM (0x1c)
200#define SR_PART_NUM 0x1c, 0xff, 0
201#define RG_VERSION_NUM (0x1d)
202#define SR_VERSION_NUM 0x1d, 0xff, 0
203#define RG_MAN_ID_0 (0x1e)
204#define SR_MAN_ID_0 0x1e, 0xff, 0
205#define RG_MAN_ID_1 (0x1f)
206#define SR_MAN_ID_1 0x1f, 0xff, 0
207#define RG_SHORT_ADDR_0 (0x20)
208#define SR_SHORT_ADDR_0 0x20, 0xff, 0
209#define RG_SHORT_ADDR_1 (0x21)
210#define SR_SHORT_ADDR_1 0x21, 0xff, 0
211#define RG_PAN_ID_0 (0x22)
212#define SR_PAN_ID_0 0x22, 0xff, 0
213#define RG_PAN_ID_1 (0x23)
214#define SR_PAN_ID_1 0x23, 0xff, 0
215#define RG_IEEE_ADDR_0 (0x24)
216#define SR_IEEE_ADDR_0 0x24, 0xff, 0
217#define RG_IEEE_ADDR_1 (0x25)
218#define SR_IEEE_ADDR_1 0x25, 0xff, 0
219#define RG_IEEE_ADDR_2 (0x26)
220#define SR_IEEE_ADDR_2 0x26, 0xff, 0
221#define RG_IEEE_ADDR_3 (0x27)
222#define SR_IEEE_ADDR_3 0x27, 0xff, 0
223#define RG_IEEE_ADDR_4 (0x28)
224#define SR_IEEE_ADDR_4 0x28, 0xff, 0
225#define RG_IEEE_ADDR_5 (0x29)
226#define SR_IEEE_ADDR_5 0x29, 0xff, 0
227#define RG_IEEE_ADDR_6 (0x2a)
228#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
229#define RG_IEEE_ADDR_7 (0x2b)
230#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
231#define RG_XAH_CTRL_0 (0x2c)
232#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
233#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
234#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
235#define RG_CSMA_SEED_0 (0x2d)
236#define SR_CSMA_SEED_0 0x2d, 0xff, 0
237#define RG_CSMA_SEED_1 (0x2e)
238#define SR_CSMA_SEED_1 0x2e, 0x07, 0
239#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
240#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
241#define SR_AACK_SET_PD 0x2e, 0x20, 5
242#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
243#define RG_CSMA_BE (0x2f)
244#define SR_MIN_BE 0x2f, 0x0f, 0
245#define SR_MAX_BE 0x2f, 0xf0, 4
246
247#define CMD_REG 0x80
248#define CMD_REG_MASK 0x3f
249#define CMD_WRITE 0x40
250#define CMD_FB 0x20
251
252#define IRQ_BAT_LOW (1 << 7)
253#define IRQ_TRX_UR (1 << 6)
254#define IRQ_AMI (1 << 5)
255#define IRQ_CCA_ED (1 << 4)
256#define IRQ_TRX_END (1 << 3)
257#define IRQ_RX_START (1 << 2)
258#define IRQ_PLL_UNL (1 << 1)
259#define IRQ_PLL_LOCK (1 << 0)
260
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000261#define IRQ_ACTIVE_HIGH 0
262#define IRQ_ACTIVE_LOW 1
263
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000264#define STATE_P_ON 0x00 /* BUSY */
265#define STATE_BUSY_RX 0x01
266#define STATE_BUSY_TX 0x02
267#define STATE_FORCE_TRX_OFF 0x03
268#define STATE_FORCE_TX_ON 0x04 /* IDLE */
269/* 0x05 */ /* INVALID_PARAMETER */
270#define STATE_RX_ON 0x06
271/* 0x07 */ /* SUCCESS */
272#define STATE_TRX_OFF 0x08
273#define STATE_TX_ON 0x09
274/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
275#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500276#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000277#define STATE_BUSY_RX_AACK 0x11
278#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000279#define STATE_RX_AACK_ON 0x16
280#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000281#define STATE_RX_ON_NOCLK 0x1C
282#define STATE_RX_AACK_ON_NOCLK 0x1D
283#define STATE_BUSY_RX_AACK_NOCLK 0x1E
284#define STATE_TRANSITION_IN_PROGRESS 0x1F
285
Alexander Aringf76014f772014-07-03 00:20:44 +0200286#define AT86RF2XX_NUMREGS 0x3F
287
Alexander Aring97fed792014-10-07 10:38:32 +0200288static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200289at86rf230_async_state_change(struct at86rf230_local *lp,
290 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200291 const u8 state, void (*complete)(void *context),
292 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200293
Alexander Aringf76014f772014-07-03 00:20:44 +0200294static inline int
295__at86rf230_write(struct at86rf230_local *lp,
296 unsigned int addr, unsigned int data)
297{
298 return regmap_write(lp->regmap, addr, data);
299}
300
301static inline int
302__at86rf230_read(struct at86rf230_local *lp,
303 unsigned int addr, unsigned int *data)
304{
305 return regmap_read(lp->regmap, addr, data);
306}
307
308static inline int
309at86rf230_read_subreg(struct at86rf230_local *lp,
310 unsigned int addr, unsigned int mask,
311 unsigned int shift, unsigned int *data)
312{
313 int rc;
314
315 rc = __at86rf230_read(lp, addr, data);
316 if (rc > 0)
317 *data = (*data & mask) >> shift;
318
319 return rc;
320}
321
322static inline int
323at86rf230_write_subreg(struct at86rf230_local *lp,
324 unsigned int addr, unsigned int mask,
325 unsigned int shift, unsigned int data)
326{
327 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
328}
329
330static bool
331at86rf230_reg_writeable(struct device *dev, unsigned int reg)
332{
333 switch (reg) {
334 case RG_TRX_STATE:
335 case RG_TRX_CTRL_0:
336 case RG_TRX_CTRL_1:
337 case RG_PHY_TX_PWR:
338 case RG_PHY_ED_LEVEL:
339 case RG_PHY_CC_CCA:
340 case RG_CCA_THRES:
341 case RG_RX_CTRL:
342 case RG_SFD_VALUE:
343 case RG_TRX_CTRL_2:
344 case RG_ANT_DIV:
345 case RG_IRQ_MASK:
346 case RG_VREG_CTRL:
347 case RG_BATMON:
348 case RG_XOSC_CTRL:
349 case RG_RX_SYN:
350 case RG_XAH_CTRL_1:
351 case RG_FTN_CTRL:
352 case RG_PLL_CF:
353 case RG_PLL_DCU:
354 case RG_SHORT_ADDR_0:
355 case RG_SHORT_ADDR_1:
356 case RG_PAN_ID_0:
357 case RG_PAN_ID_1:
358 case RG_IEEE_ADDR_0:
359 case RG_IEEE_ADDR_1:
360 case RG_IEEE_ADDR_2:
361 case RG_IEEE_ADDR_3:
362 case RG_IEEE_ADDR_4:
363 case RG_IEEE_ADDR_5:
364 case RG_IEEE_ADDR_6:
365 case RG_IEEE_ADDR_7:
366 case RG_XAH_CTRL_0:
367 case RG_CSMA_SEED_0:
368 case RG_CSMA_SEED_1:
369 case RG_CSMA_BE:
370 return true;
371 default:
372 return false;
373 }
374}
375
376static bool
377at86rf230_reg_readable(struct device *dev, unsigned int reg)
378{
379 bool rc;
380
381 /* all writeable are also readable */
382 rc = at86rf230_reg_writeable(dev, reg);
383 if (rc)
384 return rc;
385
386 /* readonly regs */
387 switch (reg) {
388 case RG_TRX_STATUS:
389 case RG_PHY_RSSI:
390 case RG_IRQ_STATUS:
391 case RG_PART_NUM:
392 case RG_VERSION_NUM:
393 case RG_MAN_ID_1:
394 case RG_MAN_ID_0:
395 return true;
396 default:
397 return false;
398 }
399}
400
401static bool
402at86rf230_reg_volatile(struct device *dev, unsigned int reg)
403{
404 /* can be changed during runtime */
405 switch (reg) {
406 case RG_TRX_STATUS:
407 case RG_TRX_STATE:
408 case RG_PHY_RSSI:
409 case RG_PHY_ED_LEVEL:
410 case RG_IRQ_STATUS:
411 case RG_VREG_CTRL:
412 return true;
413 default:
414 return false;
415 }
416}
417
418static bool
419at86rf230_reg_precious(struct device *dev, unsigned int reg)
420{
421 /* don't clear irq line on read */
422 switch (reg) {
423 case RG_IRQ_STATUS:
424 return true;
425 default:
426 return false;
427 }
428}
429
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100430static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200431 .reg_bits = 8,
432 .val_bits = 8,
433 .write_flag_mask = CMD_REG | CMD_WRITE,
434 .read_flag_mask = CMD_REG,
435 .cache_type = REGCACHE_RBTREE,
436 .max_register = AT86RF2XX_NUMREGS,
437 .writeable_reg = at86rf230_reg_writeable,
438 .readable_reg = at86rf230_reg_readable,
439 .volatile_reg = at86rf230_reg_volatile,
440 .precious_reg = at86rf230_reg_precious,
441};
442
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200443static void
444at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000445{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200446 struct at86rf230_state_change *ctx = context;
447 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000448
Alexander Aring97fed792014-10-07 10:38:32 +0200449 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100450 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200451}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000452
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100453static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200454at86rf230_async_error(struct at86rf230_local *lp,
455 struct at86rf230_state_change *ctx, int rc)
456{
457 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000458
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200459 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200460 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200461}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000462
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200463/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200464static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
466 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200467 void (*complete)(void *context),
468 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200469{
Alexander Aring97fed792014-10-07 10:38:32 +0200470 int rc;
471
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200472 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000473
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200474 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
475 ctx->trx.len = 2;
476 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200477 ctx->irq_enable = irq_enable;
478 rc = spi_async(lp->spi, &ctx->msg);
479 if (rc) {
480 if (irq_enable)
481 enable_irq(lp->spi->irq);
482
483 at86rf230_async_error(lp, ctx, rc);
484 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200485}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000486
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200487static void
488at86rf230_async_state_assert(void *context)
489{
490 struct at86rf230_state_change *ctx = context;
491 struct at86rf230_local *lp = ctx->lp;
492 const u8 *buf = ctx->buf;
493 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000494
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200495 /* Assert state change */
496 if (trx_state != ctx->to_state) {
497 /* Special handling if transceiver state is in
498 * STATE_BUSY_RX_AACK and a SHR was detected.
499 */
500 if (trx_state == STATE_BUSY_RX_AACK) {
501 /* Undocumented race condition. If we send a state
502 * change to STATE_RX_AACK_ON the transceiver could
503 * change his state automatically to STATE_BUSY_RX_AACK
504 * if a SHR was detected. This is not an error, but we
505 * can't assert this.
506 */
507 if (ctx->to_state == STATE_RX_AACK_ON)
508 goto done;
509
510 /* If we change to STATE_TX_ON without forcing and
511 * transceiver state is STATE_BUSY_RX_AACK, we wait
512 * 'tFrame + tPAck' receiving time. In this time the
513 * PDU should be received. If the transceiver is still
514 * in STATE_BUSY_RX_AACK, we run a force state change
515 * to STATE_TX_ON. This is a timeout handling, if the
516 * transceiver stucks in STATE_BUSY_RX_AACK.
517 */
518 if (ctx->to_state == STATE_TX_ON) {
519 at86rf230_async_state_change(lp, ctx,
520 STATE_FORCE_TX_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200521 ctx->complete,
522 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200523 return;
524 }
525 }
526
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200527 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
528 ctx->from_state, ctx->to_state, trx_state);
529 }
530
531done:
532 if (ctx->complete)
533 ctx->complete(context);
534}
535
536/* Do state change timing delay. */
537static void
538at86rf230_async_state_delay(void *context)
539{
540 struct at86rf230_state_change *ctx = context;
541 struct at86rf230_local *lp = ctx->lp;
542 struct at86rf2xx_chip_data *c = lp->data;
543 bool force = false;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200544
545 /* The force state changes are will show as normal states in the
546 * state status subregister. We change the to_state to the
547 * corresponding one and remember if it was a force change, this
548 * differs if we do a state change from STATE_BUSY_RX_AACK.
549 */
550 switch (ctx->to_state) {
551 case STATE_FORCE_TX_ON:
552 ctx->to_state = STATE_TX_ON;
553 force = true;
554 break;
555 case STATE_FORCE_TRX_OFF:
556 ctx->to_state = STATE_TRX_OFF;
557 force = true;
558 break;
559 default:
560 break;
561 }
562
563 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200564 case STATE_TRX_OFF:
565 switch (ctx->to_state) {
566 case STATE_RX_AACK_ON:
567 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
568 goto change;
569 case STATE_TX_ON:
570 usleep_range(c->t_off_to_tx_on,
571 c->t_off_to_tx_on + 10);
572 goto change;
573 default:
574 break;
575 }
576 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200577 case STATE_BUSY_RX_AACK:
578 switch (ctx->to_state) {
579 case STATE_TX_ON:
580 /* Wait for worst case receiving time if we
581 * didn't make a force change from BUSY_RX_AACK
582 * to TX_ON.
583 */
584 if (!force) {
585 usleep_range(c->t_frame + c->t_p_ack,
586 c->t_frame + c->t_p_ack + 1000);
587 goto change;
588 }
589 break;
590 default:
591 break;
592 }
593 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200594 /* Default value, means RESET state */
595 case STATE_P_ON:
596 switch (ctx->to_state) {
597 case STATE_TRX_OFF:
598 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
599 goto change;
600 default:
601 break;
602 }
603 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200604 default:
605 break;
606 }
607
608 /* Default delay is 1us in the most cases */
609 udelay(1);
610
611change:
Alexander Aring97fed792014-10-07 10:38:32 +0200612 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
613 at86rf230_async_state_assert,
614 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200615}
616
617static void
618at86rf230_async_state_change_start(void *context)
619{
620 struct at86rf230_state_change *ctx = context;
621 struct at86rf230_local *lp = ctx->lp;
622 u8 *buf = ctx->buf;
623 const u8 trx_state = buf[1] & 0x1f;
624 int rc;
625
626 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
627 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
628 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200629 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
630 at86rf230_async_state_change_start,
631 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200632 return;
633 }
634
635 /* Check if we already are in the state which we change in */
636 if (trx_state == ctx->to_state) {
637 if (ctx->complete)
638 ctx->complete(context);
639 return;
640 }
641
642 /* Set current state to the context of state change */
643 ctx->from_state = trx_state;
644
645 /* Going into the next step for a state change which do a timing
646 * relevant delay.
647 */
648 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
649 buf[1] = ctx->to_state;
650 ctx->trx.len = 2;
651 ctx->msg.complete = at86rf230_async_state_delay;
652 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200653 if (rc) {
654 if (ctx->irq_enable)
655 enable_irq(lp->spi->irq);
656
Alexander Aring4fef7d32014-12-15 10:25:55 +0100657 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200658 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000659}
660
Alexander Aring97fed792014-10-07 10:38:32 +0200661static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200662at86rf230_async_state_change(struct at86rf230_local *lp,
663 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200664 const u8 state, void (*complete)(void *context),
665 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000666{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200667 /* Initialization for the state change context */
668 ctx->to_state = state;
669 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200670 ctx->irq_enable = irq_enable;
671 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
672 at86rf230_async_state_change_start,
673 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200674}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000675
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200676static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200677at86rf230_sync_state_change_complete(void *context)
678{
679 struct at86rf230_state_change *ctx = context;
680 struct at86rf230_local *lp = ctx->lp;
681
682 complete(&lp->state_complete);
683}
684
685/* This function do a sync framework above the async state change.
686 * Some callbacks of the IEEE 802.15.4 driver interface need to be
687 * handled synchronously.
688 */
689static int
690at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
691{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100692 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200693
Alexander Aring97fed792014-10-07 10:38:32 +0200694 at86rf230_async_state_change(lp, &lp->state, state,
695 at86rf230_sync_state_change_complete,
696 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200697
698 rc = wait_for_completion_timeout(&lp->state_complete,
699 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200700 if (!rc) {
701 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200702 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200703 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200704
705 return 0;
706}
707
708static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200709at86rf230_tx_complete(void *context)
710{
711 struct at86rf230_state_change *ctx = context;
712 struct at86rf230_local *lp = ctx->lp;
Alexander Aring955aee82014-10-26 09:37:15 +0100713 struct sk_buff *skb = lp->tx_skb;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000714
Alexander Aring35e92a82014-10-07 10:38:31 +0200715 enable_irq(lp->spi->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100716
Alexander Aring5f5c5c22014-12-15 10:25:53 +0100717 ieee802154_xmit_complete(lp->hw, skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200718}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000719
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200720static void
721at86rf230_tx_on(void *context)
722{
723 struct at86rf230_state_change *ctx = context;
724 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000725
Alexander Aring97fed792014-10-07 10:38:32 +0200726 at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
727 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200728}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000729
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200730static void
731at86rf230_tx_trac_error(void *context)
732{
733 struct at86rf230_state_change *ctx = context;
734 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000735
Alexander Aring97fed792014-10-07 10:38:32 +0200736 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
737 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200738}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000739
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200740static void
741at86rf230_tx_trac_check(void *context)
742{
743 struct at86rf230_state_change *ctx = context;
744 struct at86rf230_local *lp = ctx->lp;
745 const u8 *buf = ctx->buf;
746 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000747
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200748 /* If trac status is different than zero we need to do a state change
749 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
750 * state to TX_ON.
751 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100752 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200753 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
754 at86rf230_tx_trac_error, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100755 else
756 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200757}
758
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200759static void
760at86rf230_tx_trac_status(void *context)
761{
762 struct at86rf230_state_change *ctx = context;
763 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200764
Alexander Aring97fed792014-10-07 10:38:32 +0200765 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
766 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200767}
768
769static void
770at86rf230_rx(struct at86rf230_local *lp,
Alexander Aringb89c3342014-10-27 17:13:42 +0100771 const u8 *data, const u8 len, const u8 lqi)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200772{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200773 struct sk_buff *skb;
774 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
775
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200776 memcpy(rx_local_buf, data, len);
777 enable_irq(lp->spi->irq);
778
Alexander Aring61a22812014-10-27 17:13:29 +0100779 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200780 if (!skb) {
781 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
782 return;
783 }
784
785 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100786 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200787}
788
789static void
790at86rf230_rx_read_frame_complete(void *context)
791{
792 struct at86rf230_state_change *ctx = context;
793 struct at86rf230_local *lp = ctx->lp;
794 const u8 *buf = lp->irq.buf;
Alexander Aringd0e73c42014-10-27 17:13:41 +0100795 u8 len = buf[1];
796
797 if (!ieee802154_is_valid_psdu_len(len)) {
798 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
799 len = IEEE802154_MTU;
800 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200801
Alexander Aring2ac0f3a2014-10-29 21:34:43 +0100802 at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200803}
804
Alexander Aring97fed792014-10-07 10:38:32 +0200805static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200806at86rf230_rx_read_frame(struct at86rf230_local *lp)
807{
Alexander Aring97fed792014-10-07 10:38:32 +0200808 int rc;
809
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200810 u8 *buf = lp->irq.buf;
811
812 buf[0] = CMD_FB;
813 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
814 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200815 rc = spi_async(lp->spi, &lp->irq.msg);
816 if (rc) {
817 enable_irq(lp->spi->irq);
818 at86rf230_async_error(lp, &lp->irq, rc);
819 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200820}
821
822static void
823at86rf230_rx_trac_check(void *context)
824{
825 struct at86rf230_state_change *ctx = context;
826 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200827
828 /* Possible check on trac status here. This could be useful to make
829 * some stats why receive is failed. Not used at the moment, but it's
830 * maybe timing relevant. Datasheet doesn't say anything about this.
831 * The programming guide say do it so.
832 */
833
Alexander Aring97fed792014-10-07 10:38:32 +0200834 at86rf230_rx_read_frame(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200835}
836
Alexander Aring97fed792014-10-07 10:38:32 +0200837static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200838at86rf230_irq_trx_end(struct at86rf230_local *lp)
839{
840 spin_lock(&lp->lock);
841 if (lp->is_tx) {
842 lp->is_tx = 0;
843 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200844
845 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200846 at86rf230_async_state_change(lp, &lp->irq,
847 STATE_FORCE_TX_ON,
848 at86rf230_tx_trac_status,
849 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200850 else
Alexander Aring97fed792014-10-07 10:38:32 +0200851 at86rf230_async_state_change(lp, &lp->irq,
852 STATE_RX_AACK_ON,
853 at86rf230_tx_complete,
854 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855 } else {
856 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200857 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
858 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200859 }
860}
861
862static void
863at86rf230_irq_status(void *context)
864{
865 struct at86rf230_state_change *ctx = context;
866 struct at86rf230_local *lp = ctx->lp;
867 const u8 *buf = lp->irq.buf;
868 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200869
870 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200871 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200872 } else {
873 enable_irq(lp->spi->irq);
874 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
875 irq);
876 }
877}
878
879static irqreturn_t at86rf230_isr(int irq, void *data)
880{
881 struct at86rf230_local *lp = data;
882 struct at86rf230_state_change *ctx = &lp->irq;
883 u8 *buf = ctx->buf;
884 int rc;
885
Alexander Aring90566362014-10-07 10:38:29 +0200886 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200887
888 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
889 ctx->trx.len = 2;
890 ctx->msg.complete = at86rf230_irq_status;
891 rc = spi_async(lp->spi, &ctx->msg);
892 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200893 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200894 at86rf230_async_error(lp, ctx, rc);
895 return IRQ_NONE;
896 }
897
898 return IRQ_HANDLED;
899}
900
901static void
902at86rf230_write_frame_complete(void *context)
903{
904 struct at86rf230_state_change *ctx = context;
905 struct at86rf230_local *lp = ctx->lp;
906 u8 *buf = ctx->buf;
907 int rc;
908
909 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
910 buf[1] = STATE_BUSY_TX;
911 ctx->trx.len = 2;
912 ctx->msg.complete = NULL;
913 rc = spi_async(lp->spi, &ctx->msg);
914 if (rc)
915 at86rf230_async_error(lp, ctx, rc);
916}
917
918static void
919at86rf230_write_frame(void *context)
920{
921 struct at86rf230_state_change *ctx = context;
922 struct at86rf230_local *lp = ctx->lp;
923 struct sk_buff *skb = lp->tx_skb;
924 u8 *buf = lp->tx.buf;
925 int rc;
926
927 spin_lock(&lp->lock);
928 lp->is_tx = 1;
929 spin_unlock(&lp->lock);
930
931 buf[0] = CMD_FB | CMD_WRITE;
932 buf[1] = skb->len + 2;
933 memcpy(buf + 2, skb->data, skb->len);
934 lp->tx.trx.len = skb->len + 2;
935 lp->tx.msg.complete = at86rf230_write_frame_complete;
936 rc = spi_async(lp->spi, &lp->tx.msg);
937 if (rc)
938 at86rf230_async_error(lp, ctx, rc);
939}
940
941static void
942at86rf230_xmit_tx_on(void *context)
943{
944 struct at86rf230_state_change *ctx = context;
945 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200946
Alexander Aring97fed792014-10-07 10:38:32 +0200947 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
948 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200949}
950
951static int
Alexander Aring5a504392014-10-25 17:16:34 +0200952at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200953{
Alexander Aring5a504392014-10-25 17:16:34 +0200954 struct at86rf230_local *lp = hw->priv;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200955 struct at86rf230_state_change *ctx = &lp->tx;
956
957 void (*tx_complete)(void *context) = at86rf230_write_frame;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200958
959 lp->tx_skb = skb;
960
961 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
962 * are in STATE_TX_ON. The pfad differs here, so we change
963 * the complete handler.
964 */
965 if (lp->tx_aret)
966 tx_complete = at86rf230_xmit_tx_on;
967
Alexander Aring97fed792014-10-07 10:38:32 +0200968 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
969
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200970 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000971}
972
973static int
Alexander Aring5a504392014-10-25 17:16:34 +0200974at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000975{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000976 BUG_ON(!level);
977 *level = 0xbe;
978 return 0;
979}
980
981static int
Alexander Aring5a504392014-10-25 17:16:34 +0200982at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000983{
Alexander Aring5a504392014-10-25 17:16:34 +0200984 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000985}
986
987static void
Alexander Aring5a504392014-10-25 17:16:34 +0200988at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000989{
Alexander Aring5a504392014-10-25 17:16:34 +0200990 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000991}
992
993static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100994at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100995{
996 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
997}
998
999static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001000at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001001{
1002 int rc;
1003
1004 if (channel == 0)
1005 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1006 else
1007 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1008 if (rc < 0)
1009 return rc;
1010
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001011 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001012 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001013 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001014 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001015 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001016 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001017 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001018 if (rc < 0)
1019 return rc;
1020
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001021 /* This sets the symbol_duration according frequency on the 212.
1022 * TODO move this handling while set channel and page in cfg802154.
1023 * We can do that, this timings are according 802.15.4 standard.
1024 * If we do that in cfg802154, this is a more generic calculation.
1025 *
1026 * This should also protected from ifs_timer. Means cancel timer and
1027 * init with a new value. For now, this is okay.
1028 */
1029 if (channel == 0) {
1030 if (page == 0) {
1031 /* SUB:0 and BPSK:0 -> BPSK-20 */
1032 lp->hw->phy->symbol_duration = 50;
1033 } else {
1034 /* SUB:1 and BPSK:0 -> BPSK-40 */
1035 lp->hw->phy->symbol_duration = 25;
1036 }
1037 } else {
1038 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001039 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001040 lp->hw->phy->symbol_duration = 40;
1041 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001042 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001043 lp->hw->phy->symbol_duration = 16;
1044 }
1045
1046 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1047 lp->hw->phy->symbol_duration;
1048 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1049 lp->hw->phy->symbol_duration;
1050
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001051 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1052}
1053
1054static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001055at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001056{
Alexander Aring5a504392014-10-25 17:16:34 +02001057 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001058 int rc;
1059
Alexander Aringa53d1f72014-07-03 00:20:46 +02001060 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001061 /* Wait for PLL */
1062 usleep_range(lp->data->t_channel_switch,
1063 lp->data->t_channel_switch + 10);
Alexander Aring820bd662014-11-12 03:36:56 +01001064 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001065}
1066
1067static int
Alexander Aring5a504392014-10-25 17:16:34 +02001068at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001069 struct ieee802154_hw_addr_filt *filt,
1070 unsigned long changed)
1071{
Alexander Aring5a504392014-10-25 17:16:34 +02001072 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001073
Alexander Aring57205c12014-10-25 05:25:09 +02001074 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001075 u16 addr = le16_to_cpu(filt->short_addr);
1076
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001077 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001078 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001079 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1080 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001081 }
1082
Alexander Aring57205c12014-10-25 05:25:09 +02001083 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001084 u16 pan = le16_to_cpu(filt->pan_id);
1085
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001086 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001087 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001088 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1089 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001090 }
1091
Alexander Aring57205c12014-10-25 05:25:09 +02001092 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001093 u8 i, addr[8];
1094
1095 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001096 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001097 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001098 for (i = 0; i < 8; i++)
1099 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001100 }
1101
Alexander Aring57205c12014-10-25 05:25:09 +02001102 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001103 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001104 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001105 if (filt->pan_coord)
1106 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1107 else
1108 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1109 }
1110
1111 return 0;
1112}
1113
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001114static int
Alexander Aring5a504392014-10-25 17:16:34 +02001115at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001116{
Alexander Aring5a504392014-10-25 17:16:34 +02001117 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001118
1119 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1120 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1121 * 0dB.
1122 * thus, supported values for db range from -26 to 5, for 31dB of
1123 * reduction to 0dB of reduction.
1124 */
1125 if (db > 5 || db < -26)
1126 return -EINVAL;
1127
1128 db = -(db - 5);
1129
Jean Sacren677676c2014-03-01 15:54:36 -07001130 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001131}
1132
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001133static int
Alexander Aring5a504392014-10-25 17:16:34 +02001134at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001135{
Alexander Aring5a504392014-10-25 17:16:34 +02001136 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001137
1138 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1139}
1140
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001141static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001142at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1143 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001144{
Alexander Aring5a504392014-10-25 17:16:34 +02001145 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001146 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001147
Alexander Aring7fe9a382014-12-10 15:33:12 +01001148 /* mapping 802.15.4 to driver spec */
1149 switch (cca->mode) {
1150 case NL802154_CCA_ENERGY:
1151 val = 1;
1152 break;
1153 case NL802154_CCA_CARRIER:
1154 val = 2;
1155 break;
1156 case NL802154_CCA_ENERGY_CARRIER:
1157 switch (cca->opt) {
1158 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1159 val = 3;
1160 break;
1161 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1162 val = 0;
1163 break;
1164 default:
1165 return -EINVAL;
1166 }
1167 break;
1168 default:
1169 return -EINVAL;
1170 }
1171
1172 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001173}
1174
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001175static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001176at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1177{
1178 return (level - lp->data->rssi_base_val) * 100 / 207;
1179}
1180
1181static int
1182at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1183{
1184 return (level - lp->data->rssi_base_val) / 2;
1185}
1186
1187static int
Alexander Aring5a504392014-10-25 17:16:34 +02001188at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001189{
Alexander Aring5a504392014-10-25 17:16:34 +02001190 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001191
Alexander Aringa53d1f72014-07-03 00:20:46 +02001192 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001193 return -EINVAL;
1194
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001195 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1196 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001197}
1198
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001199static int
Alexander Aring5a504392014-10-25 17:16:34 +02001200at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001201 u8 retries)
1202{
Alexander Aring5a504392014-10-25 17:16:34 +02001203 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001204 int rc;
1205
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001206 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1207 if (rc)
1208 return rc;
1209
1210 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1211 if (rc)
1212 return rc;
1213
Alexander Aring39d7f322014-04-05 13:49:26 +02001214 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001215}
1216
1217static int
Alexander Aring5a504392014-10-25 17:16:34 +02001218at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001219{
Alexander Aring5a504392014-10-25 17:16:34 +02001220 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001221 int rc = 0;
1222
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001223 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001224 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001225
1226 if (retries >= 0)
1227 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1228
1229 return rc;
1230}
1231
Alexander Aring92f45f52014-10-29 21:34:33 +01001232static int
1233at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1234{
1235 struct at86rf230_local *lp = hw->priv;
1236 int rc;
1237
1238 if (on) {
1239 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1240 if (rc < 0)
1241 return rc;
1242
1243 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1244 if (rc < 0)
1245 return rc;
1246 } else {
1247 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1248 if (rc < 0)
1249 return rc;
1250
1251 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1252 if (rc < 0)
1253 return rc;
1254 }
1255
1256 return 0;
1257}
1258
Alexander Aring16301862014-10-28 18:21:18 +01001259static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001260 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001261 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001262 .ed = at86rf230_ed,
1263 .set_channel = at86rf230_channel,
1264 .start = at86rf230_start,
1265 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001266 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001267 .set_txpower = at86rf230_set_txpower,
1268 .set_lbt = at86rf230_set_lbt,
1269 .set_cca_mode = at86rf230_set_cca_mode,
1270 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1271 .set_csma_params = at86rf230_set_csma_params,
1272 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f52014-10-29 21:34:33 +01001273 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001274};
1275
Alexander Aringa53d1f72014-07-03 00:20:46 +02001276static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001277 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001278 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001279 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001280 .t_off_to_aack = 80,
1281 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001282 .t_frame = 4096,
1283 .t_p_ack = 545,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001284 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001285 .rssi_base_val = -91,
1286 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001287 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001288};
1289
1290static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001291 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001292 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001293 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001294 .t_off_to_aack = 110,
1295 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001296 .t_frame = 4096,
1297 .t_p_ack = 545,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001298 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001299 .rssi_base_val = -91,
1300 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001301 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001302};
1303
1304static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001305 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001306 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001307 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001308 .t_off_to_aack = 200,
1309 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001310 .t_frame = 4096,
1311 .t_p_ack = 545,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001312 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001313 .rssi_base_val = -100,
1314 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001315 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001316};
1317
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001318static int at86rf230_hw_init(struct at86rf230_local *lp)
1319{
Alexander Aring1db05582014-07-03 00:20:50 +02001320 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001321 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001322 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001323
Alexander Aring09e536c2014-07-03 00:20:52 +02001324 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001325 if (rc)
1326 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001327
Alexander Aring4af619a2014-04-24 19:09:05 +02001328 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001329 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001330 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001331
Alexander Aring18c65042014-04-24 19:09:18 +02001332 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001333 if (rc)
1334 return rc;
1335
Alexander Aring6bd2b132014-07-03 00:20:49 +02001336 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1337 if (rc)
1338 return rc;
1339
Sascha Herrmann057dad62013-04-14 22:33:29 +00001340 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001341 if (rc)
1342 return rc;
1343
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001344 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1345 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1346 if (rc)
1347 return rc;
1348 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1349 if (rc)
1350 return rc;
1351
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001352 /* CLKM changes are applied immediately */
1353 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1354 if (rc)
1355 return rc;
1356
1357 /* Turn CLKM Off */
1358 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1359 if (rc)
1360 return rc;
1361 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001362 usleep_range(lp->data->t_sleep_cycle,
1363 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001364
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001365 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001366 if (rc)
1367 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001368 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001369 dev_err(&lp->spi->dev, "DVDD error\n");
1370 return -EINVAL;
1371 }
1372
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001373 /* Force setting slotted operation bit to 0. Sometimes the atben
1374 * sets this bit and I don't know why. We set this always force
1375 * to zero while probing.
1376 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001377 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001378}
1379
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001380static struct at86rf230_platform_data *
1381at86rf230_get_pdata(struct spi_device *spi)
1382{
1383 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001384
1385 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1386 return spi->dev.platform_data;
1387
1388 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1389 if (!pdata)
1390 goto done;
1391
1392 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1393 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1394
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001395 spi->dev.platform_data = pdata;
1396done:
1397 return pdata;
1398}
1399
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001400static int
1401at86rf230_detect_device(struct at86rf230_local *lp)
1402{
1403 unsigned int part, version, val;
1404 u16 man_id = 0;
1405 const char *chip;
1406 int rc;
1407
1408 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1409 if (rc)
1410 return rc;
1411 man_id |= val;
1412
1413 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1414 if (rc)
1415 return rc;
1416 man_id |= (val << 8);
1417
1418 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1419 if (rc)
1420 return rc;
1421
Andrey Yurovsky75989682014-12-17 13:14:42 -08001422 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001423 if (rc)
1424 return rc;
1425
1426 if (man_id != 0x001f) {
1427 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1428 man_id >> 8, man_id & 0xFF);
1429 return -EINVAL;
1430 }
1431
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001432 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8fc84e2014-10-29 21:34:31 +01001433 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
Alexander Aring92f45f52014-10-29 21:34:33 +01001434 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001435
Alexander Aringb48a7c12014-12-10 15:33:14 +01001436 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1437
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001438 switch (part) {
1439 case 2:
1440 chip = "at86rf230";
1441 rc = -ENOTSUPP;
1442 break;
1443 case 3:
1444 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001445 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001446 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001447 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001448 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001449 break;
1450 case 7:
1451 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001452 lp->data = &at86rf212_data;
1453 lp->hw->flags |= IEEE802154_HW_LBT;
1454 lp->hw->phy->channels_supported[0] = 0x00007FF;
1455 lp->hw->phy->channels_supported[2] = 0x00007FF;
1456 lp->hw->phy->current_channel = 5;
1457 lp->hw->phy->symbol_duration = 25;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001458 break;
1459 case 11:
1460 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001461 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001462 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001463 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001464 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001465 break;
1466 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001467 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001468 rc = -ENOTSUPP;
1469 break;
1470 }
1471
1472 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1473
1474 return rc;
1475}
1476
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001477static void
1478at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1479{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001480 lp->state.lp = lp;
1481 spi_message_init(&lp->state.msg);
1482 lp->state.msg.context = &lp->state;
1483 lp->state.trx.tx_buf = lp->state.buf;
1484 lp->state.trx.rx_buf = lp->state.buf;
1485 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1486
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001487 lp->irq.lp = lp;
1488 spi_message_init(&lp->irq.msg);
1489 lp->irq.msg.context = &lp->irq;
1490 lp->irq.trx.tx_buf = lp->irq.buf;
1491 lp->irq.trx.rx_buf = lp->irq.buf;
1492 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1493
1494 lp->tx.lp = lp;
1495 spi_message_init(&lp->tx.msg);
1496 lp->tx.msg.context = &lp->tx;
1497 lp->tx.trx.tx_buf = lp->tx.buf;
1498 lp->tx.trx.rx_buf = lp->tx.buf;
1499 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1500}
1501
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001502static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001503{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001504 struct at86rf230_platform_data *pdata;
Alexander Aring5a504392014-10-25 17:16:34 +02001505 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001506 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001507 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001508 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001509
1510 if (!spi->irq) {
1511 dev_err(&spi->dev, "no IRQ specified\n");
1512 return -EINVAL;
1513 }
1514
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001515 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001516 if (!pdata) {
1517 dev_err(&spi->dev, "no platform_data\n");
1518 return -EINVAL;
1519 }
1520
Alexander Aring3fa27572014-03-15 09:29:06 +01001521 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001522 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1523 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001524 if (rc)
1525 return rc;
1526 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001527
1528 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001529 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1530 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001531 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001532 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001533 }
1534
1535 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001536 if (gpio_is_valid(pdata->rstn)) {
1537 udelay(1);
1538 gpio_set_value(pdata->rstn, 0);
1539 udelay(1);
1540 gpio_set_value(pdata->rstn, 1);
1541 usleep_range(120, 240);
1542 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001543
Alexander Aring5a504392014-10-25 17:16:34 +02001544 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1545 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001546 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001547
Alexander Aring5a504392014-10-25 17:16:34 +02001548 lp = hw->priv;
1549 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001550 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001551 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001552 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001553 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001554
Alexander Aringf76014f772014-07-03 00:20:44 +02001555 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1556 if (IS_ERR(lp->regmap)) {
1557 rc = PTR_ERR(lp->regmap);
1558 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1559 rc);
1560 goto free_dev;
1561 }
1562
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001563 at86rf230_setup_spi_messages(lp);
1564
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001565 rc = at86rf230_detect_device(lp);
1566 if (rc < 0)
1567 goto free_dev;
1568
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001569 spin_lock_init(&lp->lock);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001570 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001571
1572 spi_set_drvdata(spi, lp);
1573
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001574 rc = at86rf230_hw_init(lp);
1575 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001576 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001577
Alexander Aring19626942014-04-24 19:09:15 +02001578 /* Read irq status register to reset irq line */
1579 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001580 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001581 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001582
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001583 irq_type = irq_get_trigger_type(spi->irq);
1584 if (!irq_type)
1585 irq_type = IRQF_TRIGGER_RISING;
1586
1587 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1588 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001589 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001590 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001591
Alexander Aring5a504392014-10-25 17:16:34 +02001592 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001593 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001594 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001595
1596 return rc;
1597
Alexander Aring640985e2014-07-03 00:20:43 +02001598free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001599 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001600
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001601 return rc;
1602}
1603
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001604static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001605{
1606 struct at86rf230_local *lp = spi_get_drvdata(spi);
1607
Alexander Aring17e84a92014-03-31 03:26:51 +02001608 /* mask all at86rf230 irq's */
1609 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001610 ieee802154_unregister_hw(lp->hw);
1611 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001612 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001613
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001614 return 0;
1615}
1616
Alexander Aring1086b4f2014-04-24 19:09:11 +02001617static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001618 { .compatible = "atmel,at86rf230", },
1619 { .compatible = "atmel,at86rf231", },
1620 { .compatible = "atmel,at86rf233", },
1621 { .compatible = "atmel,at86rf212", },
1622 { },
1623};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001624MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001625
Alexander Aring90b15522014-04-24 19:09:12 +02001626static const struct spi_device_id at86rf230_device_id[] = {
1627 { .name = "at86rf230", },
1628 { .name = "at86rf231", },
1629 { .name = "at86rf233", },
1630 { .name = "at86rf212", },
1631 { },
1632};
1633MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1634
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001635static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001636 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001637 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001638 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001639 .name = "at86rf230",
1640 .owner = THIS_MODULE,
1641 },
1642 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001643 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001644};
1645
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001646module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001647
1648MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1649MODULE_LICENSE("GPL v2");