blob: 5068c1bd004669f83c97e702ef5fb3744970a48d [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;
49 /* short interframe spacing time */
50 u16 t_sifs;
51 /* long interframe spacing time */
52 u16 t_lifs;
53 /* completion timeout for tx in msecs */
54 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020055 int rssi_base_val;
56
Alexander Aringe37d2ec2014-10-28 18:21:19 +010057 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020058 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020059};
60
Alexander Aring1d15d6b2014-07-03 00:20:48 +020061#define AT86RF2XX_MAX_BUF (127 + 3)
62
63struct at86rf230_state_change {
64 struct at86rf230_local *lp;
65
66 struct spi_message msg;
67 struct spi_transfer trx;
68 u8 buf[AT86RF2XX_MAX_BUF];
69
70 void (*complete)(void *context);
71 u8 from_state;
72 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020073
74 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020075};
76
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000077struct at86rf230_local {
78 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
Alexander Aring5a504392014-10-25 17:16:34 +020080 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020081 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020082 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000083
Alexander Aring2e0571c2014-07-03 00:20:51 +020084 struct completion state_complete;
85 struct at86rf230_state_change state;
86
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020088
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010089 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020090 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020091 bool is_tx;
92 /* spinlock for is_tx protection */
93 spinlock_t lock;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020094 struct sk_buff *tx_skb;
95 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000096};
97
98#define RG_TRX_STATUS (0x01)
99#define SR_TRX_STATUS 0x01, 0x1f, 0
100#define SR_RESERVED_01_3 0x01, 0x20, 5
101#define SR_CCA_STATUS 0x01, 0x40, 6
102#define SR_CCA_DONE 0x01, 0x80, 7
103#define RG_TRX_STATE (0x02)
104#define SR_TRX_CMD 0x02, 0x1f, 0
105#define SR_TRAC_STATUS 0x02, 0xe0, 5
106#define RG_TRX_CTRL_0 (0x03)
107#define SR_CLKM_CTRL 0x03, 0x07, 0
108#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
109#define SR_PAD_IO_CLKM 0x03, 0x30, 4
110#define SR_PAD_IO 0x03, 0xc0, 6
111#define RG_TRX_CTRL_1 (0x04)
112#define SR_IRQ_POLARITY 0x04, 0x01, 0
113#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
114#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
115#define SR_RX_BL_CTRL 0x04, 0x10, 4
116#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
117#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
118#define SR_PA_EXT_EN 0x04, 0x80, 7
119#define RG_PHY_TX_PWR (0x05)
120#define SR_TX_PWR 0x05, 0x0f, 0
121#define SR_PA_LT 0x05, 0x30, 4
122#define SR_PA_BUF_LT 0x05, 0xc0, 6
123#define RG_PHY_RSSI (0x06)
124#define SR_RSSI 0x06, 0x1f, 0
125#define SR_RND_VALUE 0x06, 0x60, 5
126#define SR_RX_CRC_VALID 0x06, 0x80, 7
127#define RG_PHY_ED_LEVEL (0x07)
128#define SR_ED_LEVEL 0x07, 0xff, 0
129#define RG_PHY_CC_CCA (0x08)
130#define SR_CHANNEL 0x08, 0x1f, 0
131#define SR_CCA_MODE 0x08, 0x60, 5
132#define SR_CCA_REQUEST 0x08, 0x80, 7
133#define RG_CCA_THRES (0x09)
134#define SR_CCA_ED_THRES 0x09, 0x0f, 0
135#define SR_RESERVED_09_1 0x09, 0xf0, 4
136#define RG_RX_CTRL (0x0a)
137#define SR_PDT_THRES 0x0a, 0x0f, 0
138#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
139#define RG_SFD_VALUE (0x0b)
140#define SR_SFD_VALUE 0x0b, 0xff, 0
141#define RG_TRX_CTRL_2 (0x0c)
142#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100143#define SR_SUB_MODE 0x0c, 0x04, 2
144#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100145#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
146#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000147#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
148#define RG_ANT_DIV (0x0d)
149#define SR_ANT_CTRL 0x0d, 0x03, 0
150#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
151#define SR_ANT_DIV_EN 0x0d, 0x08, 3
152#define SR_RESERVED_0d_2 0x0d, 0x70, 4
153#define SR_ANT_SEL 0x0d, 0x80, 7
154#define RG_IRQ_MASK (0x0e)
155#define SR_IRQ_MASK 0x0e, 0xff, 0
156#define RG_IRQ_STATUS (0x0f)
157#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
158#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
159#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
160#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
161#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
162#define SR_IRQ_5_AMI 0x0f, 0x20, 5
163#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
164#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
165#define RG_VREG_CTRL (0x10)
166#define SR_RESERVED_10_6 0x10, 0x03, 0
167#define SR_DVDD_OK 0x10, 0x04, 2
168#define SR_DVREG_EXT 0x10, 0x08, 3
169#define SR_RESERVED_10_3 0x10, 0x30, 4
170#define SR_AVDD_OK 0x10, 0x40, 6
171#define SR_AVREG_EXT 0x10, 0x80, 7
172#define RG_BATMON (0x11)
173#define SR_BATMON_VTH 0x11, 0x0f, 0
174#define SR_BATMON_HR 0x11, 0x10, 4
175#define SR_BATMON_OK 0x11, 0x20, 5
176#define SR_RESERVED_11_1 0x11, 0xc0, 6
177#define RG_XOSC_CTRL (0x12)
178#define SR_XTAL_TRIM 0x12, 0x0f, 0
179#define SR_XTAL_MODE 0x12, 0xf0, 4
180#define RG_RX_SYN (0x15)
181#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
182#define SR_RESERVED_15_2 0x15, 0x70, 4
183#define SR_RX_PDT_DIS 0x15, 0x80, 7
184#define RG_XAH_CTRL_1 (0x17)
185#define SR_RESERVED_17_8 0x17, 0x01, 0
186#define SR_AACK_PROM_MODE 0x17, 0x02, 1
187#define SR_AACK_ACK_TIME 0x17, 0x04, 2
188#define SR_RESERVED_17_5 0x17, 0x08, 3
189#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
190#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100191#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000192#define SR_RESERVED_17_1 0x17, 0x80, 7
193#define RG_FTN_CTRL (0x18)
194#define SR_RESERVED_18_2 0x18, 0x7f, 0
195#define SR_FTN_START 0x18, 0x80, 7
196#define RG_PLL_CF (0x1a)
197#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
198#define SR_PLL_CF_START 0x1a, 0x80, 7
199#define RG_PLL_DCU (0x1b)
200#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
201#define SR_RESERVED_1b_2 0x1b, 0x40, 6
202#define SR_PLL_DCU_START 0x1b, 0x80, 7
203#define RG_PART_NUM (0x1c)
204#define SR_PART_NUM 0x1c, 0xff, 0
205#define RG_VERSION_NUM (0x1d)
206#define SR_VERSION_NUM 0x1d, 0xff, 0
207#define RG_MAN_ID_0 (0x1e)
208#define SR_MAN_ID_0 0x1e, 0xff, 0
209#define RG_MAN_ID_1 (0x1f)
210#define SR_MAN_ID_1 0x1f, 0xff, 0
211#define RG_SHORT_ADDR_0 (0x20)
212#define SR_SHORT_ADDR_0 0x20, 0xff, 0
213#define RG_SHORT_ADDR_1 (0x21)
214#define SR_SHORT_ADDR_1 0x21, 0xff, 0
215#define RG_PAN_ID_0 (0x22)
216#define SR_PAN_ID_0 0x22, 0xff, 0
217#define RG_PAN_ID_1 (0x23)
218#define SR_PAN_ID_1 0x23, 0xff, 0
219#define RG_IEEE_ADDR_0 (0x24)
220#define SR_IEEE_ADDR_0 0x24, 0xff, 0
221#define RG_IEEE_ADDR_1 (0x25)
222#define SR_IEEE_ADDR_1 0x25, 0xff, 0
223#define RG_IEEE_ADDR_2 (0x26)
224#define SR_IEEE_ADDR_2 0x26, 0xff, 0
225#define RG_IEEE_ADDR_3 (0x27)
226#define SR_IEEE_ADDR_3 0x27, 0xff, 0
227#define RG_IEEE_ADDR_4 (0x28)
228#define SR_IEEE_ADDR_4 0x28, 0xff, 0
229#define RG_IEEE_ADDR_5 (0x29)
230#define SR_IEEE_ADDR_5 0x29, 0xff, 0
231#define RG_IEEE_ADDR_6 (0x2a)
232#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
233#define RG_IEEE_ADDR_7 (0x2b)
234#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
235#define RG_XAH_CTRL_0 (0x2c)
236#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
237#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
238#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
239#define RG_CSMA_SEED_0 (0x2d)
240#define SR_CSMA_SEED_0 0x2d, 0xff, 0
241#define RG_CSMA_SEED_1 (0x2e)
242#define SR_CSMA_SEED_1 0x2e, 0x07, 0
243#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
244#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
245#define SR_AACK_SET_PD 0x2e, 0x20, 5
246#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
247#define RG_CSMA_BE (0x2f)
248#define SR_MIN_BE 0x2f, 0x0f, 0
249#define SR_MAX_BE 0x2f, 0xf0, 4
250
251#define CMD_REG 0x80
252#define CMD_REG_MASK 0x3f
253#define CMD_WRITE 0x40
254#define CMD_FB 0x20
255
256#define IRQ_BAT_LOW (1 << 7)
257#define IRQ_TRX_UR (1 << 6)
258#define IRQ_AMI (1 << 5)
259#define IRQ_CCA_ED (1 << 4)
260#define IRQ_TRX_END (1 << 3)
261#define IRQ_RX_START (1 << 2)
262#define IRQ_PLL_UNL (1 << 1)
263#define IRQ_PLL_LOCK (1 << 0)
264
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000265#define IRQ_ACTIVE_HIGH 0
266#define IRQ_ACTIVE_LOW 1
267
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000268#define STATE_P_ON 0x00 /* BUSY */
269#define STATE_BUSY_RX 0x01
270#define STATE_BUSY_TX 0x02
271#define STATE_FORCE_TRX_OFF 0x03
272#define STATE_FORCE_TX_ON 0x04 /* IDLE */
273/* 0x05 */ /* INVALID_PARAMETER */
274#define STATE_RX_ON 0x06
275/* 0x07 */ /* SUCCESS */
276#define STATE_TRX_OFF 0x08
277#define STATE_TX_ON 0x09
278/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
279#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500280#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000281#define STATE_BUSY_RX_AACK 0x11
282#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000283#define STATE_RX_AACK_ON 0x16
284#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000285#define STATE_RX_ON_NOCLK 0x1C
286#define STATE_RX_AACK_ON_NOCLK 0x1D
287#define STATE_BUSY_RX_AACK_NOCLK 0x1E
288#define STATE_TRANSITION_IN_PROGRESS 0x1F
289
Alexander Aringf76014f772014-07-03 00:20:44 +0200290#define AT86RF2XX_NUMREGS 0x3F
291
Alexander Aring97fed792014-10-07 10:38:32 +0200292static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200293at86rf230_async_state_change(struct at86rf230_local *lp,
294 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200295 const u8 state, void (*complete)(void *context),
296 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200297
Alexander Aringf76014f772014-07-03 00:20:44 +0200298static inline int
299__at86rf230_write(struct at86rf230_local *lp,
300 unsigned int addr, unsigned int data)
301{
302 return regmap_write(lp->regmap, addr, data);
303}
304
305static inline int
306__at86rf230_read(struct at86rf230_local *lp,
307 unsigned int addr, unsigned int *data)
308{
309 return regmap_read(lp->regmap, addr, data);
310}
311
312static inline int
313at86rf230_read_subreg(struct at86rf230_local *lp,
314 unsigned int addr, unsigned int mask,
315 unsigned int shift, unsigned int *data)
316{
317 int rc;
318
319 rc = __at86rf230_read(lp, addr, data);
320 if (rc > 0)
321 *data = (*data & mask) >> shift;
322
323 return rc;
324}
325
326static inline int
327at86rf230_write_subreg(struct at86rf230_local *lp,
328 unsigned int addr, unsigned int mask,
329 unsigned int shift, unsigned int data)
330{
331 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
332}
333
334static bool
335at86rf230_reg_writeable(struct device *dev, unsigned int reg)
336{
337 switch (reg) {
338 case RG_TRX_STATE:
339 case RG_TRX_CTRL_0:
340 case RG_TRX_CTRL_1:
341 case RG_PHY_TX_PWR:
342 case RG_PHY_ED_LEVEL:
343 case RG_PHY_CC_CCA:
344 case RG_CCA_THRES:
345 case RG_RX_CTRL:
346 case RG_SFD_VALUE:
347 case RG_TRX_CTRL_2:
348 case RG_ANT_DIV:
349 case RG_IRQ_MASK:
350 case RG_VREG_CTRL:
351 case RG_BATMON:
352 case RG_XOSC_CTRL:
353 case RG_RX_SYN:
354 case RG_XAH_CTRL_1:
355 case RG_FTN_CTRL:
356 case RG_PLL_CF:
357 case RG_PLL_DCU:
358 case RG_SHORT_ADDR_0:
359 case RG_SHORT_ADDR_1:
360 case RG_PAN_ID_0:
361 case RG_PAN_ID_1:
362 case RG_IEEE_ADDR_0:
363 case RG_IEEE_ADDR_1:
364 case RG_IEEE_ADDR_2:
365 case RG_IEEE_ADDR_3:
366 case RG_IEEE_ADDR_4:
367 case RG_IEEE_ADDR_5:
368 case RG_IEEE_ADDR_6:
369 case RG_IEEE_ADDR_7:
370 case RG_XAH_CTRL_0:
371 case RG_CSMA_SEED_0:
372 case RG_CSMA_SEED_1:
373 case RG_CSMA_BE:
374 return true;
375 default:
376 return false;
377 }
378}
379
380static bool
381at86rf230_reg_readable(struct device *dev, unsigned int reg)
382{
383 bool rc;
384
385 /* all writeable are also readable */
386 rc = at86rf230_reg_writeable(dev, reg);
387 if (rc)
388 return rc;
389
390 /* readonly regs */
391 switch (reg) {
392 case RG_TRX_STATUS:
393 case RG_PHY_RSSI:
394 case RG_IRQ_STATUS:
395 case RG_PART_NUM:
396 case RG_VERSION_NUM:
397 case RG_MAN_ID_1:
398 case RG_MAN_ID_0:
399 return true;
400 default:
401 return false;
402 }
403}
404
405static bool
406at86rf230_reg_volatile(struct device *dev, unsigned int reg)
407{
408 /* can be changed during runtime */
409 switch (reg) {
410 case RG_TRX_STATUS:
411 case RG_TRX_STATE:
412 case RG_PHY_RSSI:
413 case RG_PHY_ED_LEVEL:
414 case RG_IRQ_STATUS:
415 case RG_VREG_CTRL:
416 return true;
417 default:
418 return false;
419 }
420}
421
422static bool
423at86rf230_reg_precious(struct device *dev, unsigned int reg)
424{
425 /* don't clear irq line on read */
426 switch (reg) {
427 case RG_IRQ_STATUS:
428 return true;
429 default:
430 return false;
431 }
432}
433
434static struct regmap_config at86rf230_regmap_spi_config = {
435 .reg_bits = 8,
436 .val_bits = 8,
437 .write_flag_mask = CMD_REG | CMD_WRITE,
438 .read_flag_mask = CMD_REG,
439 .cache_type = REGCACHE_RBTREE,
440 .max_register = AT86RF2XX_NUMREGS,
441 .writeable_reg = at86rf230_reg_writeable,
442 .readable_reg = at86rf230_reg_readable,
443 .volatile_reg = at86rf230_reg_volatile,
444 .precious_reg = at86rf230_reg_precious,
445};
446
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200447static void
448at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000449{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200450 struct at86rf230_state_change *ctx = context;
451 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000452
Alexander Aring97fed792014-10-07 10:38:32 +0200453 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100454 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200455}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000456
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200457static void
458at86rf230_async_error(struct at86rf230_local *lp,
459 struct at86rf230_state_change *ctx, int rc)
460{
461 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000462
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200463 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200464 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000466
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200467/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200468static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200469at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
470 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200471 void (*complete)(void *context),
472 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200473{
Alexander Aring97fed792014-10-07 10:38:32 +0200474 int rc;
475
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200476 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000477
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200478 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
479 ctx->trx.len = 2;
480 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200481 ctx->irq_enable = irq_enable;
482 rc = spi_async(lp->spi, &ctx->msg);
483 if (rc) {
484 if (irq_enable)
485 enable_irq(lp->spi->irq);
486
487 at86rf230_async_error(lp, ctx, rc);
488 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200489}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000490
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200491static void
492at86rf230_async_state_assert(void *context)
493{
494 struct at86rf230_state_change *ctx = context;
495 struct at86rf230_local *lp = ctx->lp;
496 const u8 *buf = ctx->buf;
497 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000498
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200499 /* Assert state change */
500 if (trx_state != ctx->to_state) {
501 /* Special handling if transceiver state is in
502 * STATE_BUSY_RX_AACK and a SHR was detected.
503 */
504 if (trx_state == STATE_BUSY_RX_AACK) {
505 /* Undocumented race condition. If we send a state
506 * change to STATE_RX_AACK_ON the transceiver could
507 * change his state automatically to STATE_BUSY_RX_AACK
508 * if a SHR was detected. This is not an error, but we
509 * can't assert this.
510 */
511 if (ctx->to_state == STATE_RX_AACK_ON)
512 goto done;
513
514 /* If we change to STATE_TX_ON without forcing and
515 * transceiver state is STATE_BUSY_RX_AACK, we wait
516 * 'tFrame + tPAck' receiving time. In this time the
517 * PDU should be received. If the transceiver is still
518 * in STATE_BUSY_RX_AACK, we run a force state change
519 * to STATE_TX_ON. This is a timeout handling, if the
520 * transceiver stucks in STATE_BUSY_RX_AACK.
521 */
522 if (ctx->to_state == STATE_TX_ON) {
523 at86rf230_async_state_change(lp, ctx,
524 STATE_FORCE_TX_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200525 ctx->complete,
526 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200527 return;
528 }
529 }
530
531
532 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
533 ctx->from_state, ctx->to_state, trx_state);
534 }
535
536done:
537 if (ctx->complete)
538 ctx->complete(context);
539}
540
541/* Do state change timing delay. */
542static void
543at86rf230_async_state_delay(void *context)
544{
545 struct at86rf230_state_change *ctx = context;
546 struct at86rf230_local *lp = ctx->lp;
547 struct at86rf2xx_chip_data *c = lp->data;
548 bool force = false;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200549
550 /* The force state changes are will show as normal states in the
551 * state status subregister. We change the to_state to the
552 * corresponding one and remember if it was a force change, this
553 * differs if we do a state change from STATE_BUSY_RX_AACK.
554 */
555 switch (ctx->to_state) {
556 case STATE_FORCE_TX_ON:
557 ctx->to_state = STATE_TX_ON;
558 force = true;
559 break;
560 case STATE_FORCE_TRX_OFF:
561 ctx->to_state = STATE_TRX_OFF;
562 force = true;
563 break;
564 default:
565 break;
566 }
567
568 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200569 case STATE_TRX_OFF:
570 switch (ctx->to_state) {
571 case STATE_RX_AACK_ON:
572 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
573 goto change;
574 case STATE_TX_ON:
575 usleep_range(c->t_off_to_tx_on,
576 c->t_off_to_tx_on + 10);
577 goto change;
578 default:
579 break;
580 }
581 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200582 case STATE_BUSY_RX_AACK:
583 switch (ctx->to_state) {
584 case STATE_TX_ON:
585 /* Wait for worst case receiving time if we
586 * didn't make a force change from BUSY_RX_AACK
587 * to TX_ON.
588 */
589 if (!force) {
590 usleep_range(c->t_frame + c->t_p_ack,
591 c->t_frame + c->t_p_ack + 1000);
592 goto change;
593 }
594 break;
595 default:
596 break;
597 }
598 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200599 /* Default value, means RESET state */
600 case STATE_P_ON:
601 switch (ctx->to_state) {
602 case STATE_TRX_OFF:
603 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
604 goto change;
605 default:
606 break;
607 }
608 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200609 default:
610 break;
611 }
612
613 /* Default delay is 1us in the most cases */
614 udelay(1);
615
616change:
Alexander Aring97fed792014-10-07 10:38:32 +0200617 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
618 at86rf230_async_state_assert,
619 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200620}
621
622static void
623at86rf230_async_state_change_start(void *context)
624{
625 struct at86rf230_state_change *ctx = context;
626 struct at86rf230_local *lp = ctx->lp;
627 u8 *buf = ctx->buf;
628 const u8 trx_state = buf[1] & 0x1f;
629 int rc;
630
631 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
632 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
633 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200634 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
635 at86rf230_async_state_change_start,
636 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200637 return;
638 }
639
640 /* Check if we already are in the state which we change in */
641 if (trx_state == ctx->to_state) {
642 if (ctx->complete)
643 ctx->complete(context);
644 return;
645 }
646
647 /* Set current state to the context of state change */
648 ctx->from_state = trx_state;
649
650 /* Going into the next step for a state change which do a timing
651 * relevant delay.
652 */
653 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
654 buf[1] = ctx->to_state;
655 ctx->trx.len = 2;
656 ctx->msg.complete = at86rf230_async_state_delay;
657 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200658 if (rc) {
659 if (ctx->irq_enable)
660 enable_irq(lp->spi->irq);
661
662 at86rf230_async_error(lp, &lp->state, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200663 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000664}
665
Alexander Aring97fed792014-10-07 10:38:32 +0200666static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200667at86rf230_async_state_change(struct at86rf230_local *lp,
668 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200669 const u8 state, void (*complete)(void *context),
670 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000671{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200672 /* Initialization for the state change context */
673 ctx->to_state = state;
674 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200675 ctx->irq_enable = irq_enable;
676 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
677 at86rf230_async_state_change_start,
678 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200679}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000680
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200681static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200682at86rf230_sync_state_change_complete(void *context)
683{
684 struct at86rf230_state_change *ctx = context;
685 struct at86rf230_local *lp = ctx->lp;
686
687 complete(&lp->state_complete);
688}
689
690/* This function do a sync framework above the async state change.
691 * Some callbacks of the IEEE 802.15.4 driver interface need to be
692 * handled synchronously.
693 */
694static int
695at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
696{
697 int rc;
698
Alexander Aring97fed792014-10-07 10:38:32 +0200699 at86rf230_async_state_change(lp, &lp->state, state,
700 at86rf230_sync_state_change_complete,
701 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200702
703 rc = wait_for_completion_timeout(&lp->state_complete,
704 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200705 if (!rc) {
706 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200707 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200708 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200709
710 return 0;
711}
712
713static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200714at86rf230_tx_complete(void *context)
715{
716 struct at86rf230_state_change *ctx = context;
717 struct at86rf230_local *lp = ctx->lp;
Alexander Aring955aee82014-10-26 09:37:15 +0100718 struct sk_buff *skb = lp->tx_skb;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000719
Alexander Aring35e92a82014-10-07 10:38:31 +0200720 enable_irq(lp->spi->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100721
722 if (lp->max_frame_retries <= 0) {
723 /* Interfame spacing time, which is phy depend.
724 * TODO
725 * Move this handling in MAC 802.15.4 layer.
726 * This is currently a workaround to avoid fragmenation issues.
727 */
728 if (skb->len > 18)
729 udelay(lp->data->t_lifs);
730 else
731 udelay(lp->data->t_sifs);
732 }
733
734 ieee802154_xmit_complete(lp->hw, skb);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200735}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000736
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200737static void
738at86rf230_tx_on(void *context)
739{
740 struct at86rf230_state_change *ctx = context;
741 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000742
Alexander Aring97fed792014-10-07 10:38:32 +0200743 at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
744 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200745}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000746
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200747static void
748at86rf230_tx_trac_error(void *context)
749{
750 struct at86rf230_state_change *ctx = context;
751 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000752
Alexander Aring97fed792014-10-07 10:38:32 +0200753 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
754 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200755}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000756
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200757static void
758at86rf230_tx_trac_check(void *context)
759{
760 struct at86rf230_state_change *ctx = context;
761 struct at86rf230_local *lp = ctx->lp;
762 const u8 *buf = ctx->buf;
763 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000764
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200765 /* If trac status is different than zero we need to do a state change
766 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
767 * state to TX_ON.
768 */
769 if (trac) {
Alexander Aring97fed792014-10-07 10:38:32 +0200770 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
771 at86rf230_tx_trac_error, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200772 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000773 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000774
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200775 at86rf230_tx_on(context);
776}
777
778
779static void
780at86rf230_tx_trac_status(void *context)
781{
782 struct at86rf230_state_change *ctx = context;
783 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200784
Alexander Aring97fed792014-10-07 10:38:32 +0200785 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
786 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200787}
788
789static void
790at86rf230_rx(struct at86rf230_local *lp,
Alexander Aringb89c3342014-10-27 17:13:42 +0100791 const u8 *data, const u8 len, const u8 lqi)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200792{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200793 struct sk_buff *skb;
794 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
795
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200796 memcpy(rx_local_buf, data, len);
797 enable_irq(lp->spi->irq);
798
Alexander Aring61a22812014-10-27 17:13:29 +0100799 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200800 if (!skb) {
801 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
802 return;
803 }
804
805 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100806 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200807}
808
809static void
810at86rf230_rx_read_frame_complete(void *context)
811{
812 struct at86rf230_state_change *ctx = context;
813 struct at86rf230_local *lp = ctx->lp;
814 const u8 *buf = lp->irq.buf;
Alexander Aringd0e73c42014-10-27 17:13:41 +0100815 u8 len = buf[1];
816
817 if (!ieee802154_is_valid_psdu_len(len)) {
818 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
819 len = IEEE802154_MTU;
820 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200821
Alexander Aringb89c3342014-10-27 17:13:42 +0100822 at86rf230_rx(lp, buf + 2, len - 2, buf[2 + len]);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200823}
824
Alexander Aring97fed792014-10-07 10:38:32 +0200825static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200826at86rf230_rx_read_frame(struct at86rf230_local *lp)
827{
Alexander Aring97fed792014-10-07 10:38:32 +0200828 int rc;
829
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200830 u8 *buf = lp->irq.buf;
831
832 buf[0] = CMD_FB;
833 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
834 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200835 rc = spi_async(lp->spi, &lp->irq.msg);
836 if (rc) {
837 enable_irq(lp->spi->irq);
838 at86rf230_async_error(lp, &lp->irq, rc);
839 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200840}
841
842static void
843at86rf230_rx_trac_check(void *context)
844{
845 struct at86rf230_state_change *ctx = context;
846 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200847
848 /* Possible check on trac status here. This could be useful to make
849 * some stats why receive is failed. Not used at the moment, but it's
850 * maybe timing relevant. Datasheet doesn't say anything about this.
851 * The programming guide say do it so.
852 */
853
Alexander Aring97fed792014-10-07 10:38:32 +0200854 at86rf230_rx_read_frame(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855}
856
Alexander Aring97fed792014-10-07 10:38:32 +0200857static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200858at86rf230_irq_trx_end(struct at86rf230_local *lp)
859{
860 spin_lock(&lp->lock);
861 if (lp->is_tx) {
862 lp->is_tx = 0;
863 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200864
865 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200866 at86rf230_async_state_change(lp, &lp->irq,
867 STATE_FORCE_TX_ON,
868 at86rf230_tx_trac_status,
869 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200870 else
Alexander Aring97fed792014-10-07 10:38:32 +0200871 at86rf230_async_state_change(lp, &lp->irq,
872 STATE_RX_AACK_ON,
873 at86rf230_tx_complete,
874 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200875 } else {
876 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200877 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
878 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200879 }
880}
881
882static void
883at86rf230_irq_status(void *context)
884{
885 struct at86rf230_state_change *ctx = context;
886 struct at86rf230_local *lp = ctx->lp;
887 const u8 *buf = lp->irq.buf;
888 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200889
890 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200891 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200892 } else {
893 enable_irq(lp->spi->irq);
894 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
895 irq);
896 }
897}
898
899static irqreturn_t at86rf230_isr(int irq, void *data)
900{
901 struct at86rf230_local *lp = data;
902 struct at86rf230_state_change *ctx = &lp->irq;
903 u8 *buf = ctx->buf;
904 int rc;
905
Alexander Aring90566362014-10-07 10:38:29 +0200906 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200907
908 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
909 ctx->trx.len = 2;
910 ctx->msg.complete = at86rf230_irq_status;
911 rc = spi_async(lp->spi, &ctx->msg);
912 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200913 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200914 at86rf230_async_error(lp, ctx, rc);
915 return IRQ_NONE;
916 }
917
918 return IRQ_HANDLED;
919}
920
921static void
922at86rf230_write_frame_complete(void *context)
923{
924 struct at86rf230_state_change *ctx = context;
925 struct at86rf230_local *lp = ctx->lp;
926 u8 *buf = ctx->buf;
927 int rc;
928
929 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
930 buf[1] = STATE_BUSY_TX;
931 ctx->trx.len = 2;
932 ctx->msg.complete = NULL;
933 rc = spi_async(lp->spi, &ctx->msg);
934 if (rc)
935 at86rf230_async_error(lp, ctx, rc);
936}
937
938static void
939at86rf230_write_frame(void *context)
940{
941 struct at86rf230_state_change *ctx = context;
942 struct at86rf230_local *lp = ctx->lp;
943 struct sk_buff *skb = lp->tx_skb;
944 u8 *buf = lp->tx.buf;
945 int rc;
946
947 spin_lock(&lp->lock);
948 lp->is_tx = 1;
949 spin_unlock(&lp->lock);
950
951 buf[0] = CMD_FB | CMD_WRITE;
952 buf[1] = skb->len + 2;
953 memcpy(buf + 2, skb->data, skb->len);
954 lp->tx.trx.len = skb->len + 2;
955 lp->tx.msg.complete = at86rf230_write_frame_complete;
956 rc = spi_async(lp->spi, &lp->tx.msg);
957 if (rc)
958 at86rf230_async_error(lp, ctx, rc);
959}
960
961static void
962at86rf230_xmit_tx_on(void *context)
963{
964 struct at86rf230_state_change *ctx = context;
965 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200966
Alexander Aring97fed792014-10-07 10:38:32 +0200967 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
968 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200969}
970
971static int
Alexander Aring5a504392014-10-25 17:16:34 +0200972at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200973{
Alexander Aring5a504392014-10-25 17:16:34 +0200974 struct at86rf230_local *lp = hw->priv;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200975 struct at86rf230_state_change *ctx = &lp->tx;
976
977 void (*tx_complete)(void *context) = at86rf230_write_frame;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200978
979 lp->tx_skb = skb;
980
981 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
982 * are in STATE_TX_ON. The pfad differs here, so we change
983 * the complete handler.
984 */
985 if (lp->tx_aret)
986 tx_complete = at86rf230_xmit_tx_on;
987
Alexander Aring97fed792014-10-07 10:38:32 +0200988 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
989
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200990 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000991}
992
993static int
Alexander Aring5a504392014-10-25 17:16:34 +0200994at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000995{
996 might_sleep();
997 BUG_ON(!level);
998 *level = 0xbe;
999 return 0;
1000}
1001
1002static int
Alexander Aring5a504392014-10-25 17:16:34 +02001003at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001004{
Alexander Aring5a504392014-10-25 17:16:34 +02001005 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001006}
1007
1008static void
Alexander Aring5a504392014-10-25 17:16:34 +02001009at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001010{
Alexander Aring5a504392014-10-25 17:16:34 +02001011 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001012}
1013
1014static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001015at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001016{
1017 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1018}
1019
1020static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001021at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001022{
1023 int rc;
1024
1025 if (channel == 0)
1026 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1027 else
1028 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1029 if (rc < 0)
1030 return rc;
1031
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001032 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001033 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001034 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001035 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001036 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001037 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001038 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001039 if (rc < 0)
1040 return rc;
1041
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001042 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1043}
1044
1045static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001046at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001047{
Alexander Aring5a504392014-10-25 17:16:34 +02001048 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001049 int rc;
1050
1051 might_sleep();
1052
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001053 if (page < 0 || page > 31 ||
Alexander Aring5a504392014-10-25 17:16:34 +02001054 !(lp->hw->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001055 WARN_ON(1);
1056 return -EINVAL;
1057 }
1058
Alexander Aringa53d1f72014-07-03 00:20:46 +02001059 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001060 if (rc < 0)
1061 return rc;
1062
Alexander Aring984e0c62014-07-03 00:20:53 +02001063 /* Wait for PLL */
1064 usleep_range(lp->data->t_channel_switch,
1065 lp->data->t_channel_switch + 10);
Alexander Aring5a504392014-10-25 17:16:34 +02001066 hw->phy->current_channel = channel;
1067 hw->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001068
1069 return 0;
1070}
1071
1072static int
Alexander Aring5a504392014-10-25 17:16:34 +02001073at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001074 struct ieee802154_hw_addr_filt *filt,
1075 unsigned long changed)
1076{
Alexander Aring5a504392014-10-25 17:16:34 +02001077 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001078
Alexander Aring57205c12014-10-25 05:25:09 +02001079 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001080 u16 addr = le16_to_cpu(filt->short_addr);
1081
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001082 dev_vdbg(&lp->spi->dev,
1083 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001084 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1085 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001086 }
1087
Alexander Aring57205c12014-10-25 05:25:09 +02001088 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001089 u16 pan = le16_to_cpu(filt->pan_id);
1090
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001091 dev_vdbg(&lp->spi->dev,
1092 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001093 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1094 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001095 }
1096
Alexander Aring57205c12014-10-25 05:25:09 +02001097 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001098 u8 i, addr[8];
1099
1100 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001101 dev_vdbg(&lp->spi->dev,
1102 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001103 for (i = 0; i < 8; i++)
1104 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001105 }
1106
Alexander Aring57205c12014-10-25 05:25:09 +02001107 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001108 dev_vdbg(&lp->spi->dev,
1109 "at86rf230_set_hw_addr_filt called for panc change\n");
1110 if (filt->pan_coord)
1111 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1112 else
1113 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1114 }
1115
1116 return 0;
1117}
1118
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001119static int
Alexander Aring5a504392014-10-25 17:16:34 +02001120at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001121{
Alexander Aring5a504392014-10-25 17:16:34 +02001122 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001123
1124 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1125 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1126 * 0dB.
1127 * thus, supported values for db range from -26 to 5, for 31dB of
1128 * reduction to 0dB of reduction.
1129 */
1130 if (db > 5 || db < -26)
1131 return -EINVAL;
1132
1133 db = -(db - 5);
1134
Jean Sacren677676c2014-03-01 15:54:36 -07001135 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001136}
1137
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001138static int
Alexander Aring5a504392014-10-25 17:16:34 +02001139at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001140{
Alexander Aring5a504392014-10-25 17:16:34 +02001141 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001142
1143 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1144}
1145
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001146static int
Alexander Aring5a504392014-10-25 17:16:34 +02001147at86rf230_set_cca_mode(struct ieee802154_hw *hw, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001148{
Alexander Aring5a504392014-10-25 17:16:34 +02001149 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001150
1151 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1152}
1153
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001154static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001155at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1156{
1157 return (level - lp->data->rssi_base_val) * 100 / 207;
1158}
1159
1160static int
1161at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1162{
1163 return (level - lp->data->rssi_base_val) / 2;
1164}
1165
1166static int
Alexander Aring5a504392014-10-25 17:16:34 +02001167at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001168{
Alexander Aring5a504392014-10-25 17:16:34 +02001169 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001170
Alexander Aringa53d1f72014-07-03 00:20:46 +02001171 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001172 return -EINVAL;
1173
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001174 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1175 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001176}
1177
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001178static int
Alexander Aring5a504392014-10-25 17:16:34 +02001179at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001180 u8 retries)
1181{
Alexander Aring5a504392014-10-25 17:16:34 +02001182 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001183 int rc;
1184
1185 if (min_be > max_be || max_be > 8 || retries > 5)
1186 return -EINVAL;
1187
1188 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1189 if (rc)
1190 return rc;
1191
1192 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1193 if (rc)
1194 return rc;
1195
Alexander Aring39d7f322014-04-05 13:49:26 +02001196 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001197}
1198
1199static int
Alexander Aring5a504392014-10-25 17:16:34 +02001200at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001201{
Alexander Aring5a504392014-10-25 17:16:34 +02001202 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001203 int rc = 0;
1204
1205 if (retries < -1 || retries > 15)
1206 return -EINVAL;
1207
1208 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001209 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001210
1211 if (retries >= 0)
1212 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1213
1214 return rc;
1215}
1216
Alexander Aring16301862014-10-28 18:21:18 +01001217static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001218 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001219 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001220 .ed = at86rf230_ed,
1221 .set_channel = at86rf230_channel,
1222 .start = at86rf230_start,
1223 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001224 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001225 .set_txpower = at86rf230_set_txpower,
1226 .set_lbt = at86rf230_set_lbt,
1227 .set_cca_mode = at86rf230_set_cca_mode,
1228 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1229 .set_csma_params = at86rf230_set_csma_params,
1230 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001231};
1232
Alexander Aringa53d1f72014-07-03 00:20:46 +02001233static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001234 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001235 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001236 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001237 .t_off_to_aack = 80,
1238 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001239 .t_frame = 4096,
1240 .t_p_ack = 545,
1241 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001242 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001243 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001244 .rssi_base_val = -91,
1245 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001246 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001247};
1248
1249static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001250 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001251 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001252 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001253 .t_off_to_aack = 110,
1254 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001255 .t_frame = 4096,
1256 .t_p_ack = 545,
1257 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001258 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001259 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001260 .rssi_base_val = -91,
1261 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001262 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001263};
1264
1265static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001266 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001267 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001268 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001269 .t_off_to_aack = 200,
1270 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001271 .t_frame = 4096,
1272 .t_p_ack = 545,
1273 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001274 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001275 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001276 .rssi_base_val = -100,
1277 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001278 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001279};
1280
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001281static int at86rf230_hw_init(struct at86rf230_local *lp)
1282{
Alexander Aring1db05582014-07-03 00:20:50 +02001283 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001284 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001285 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001286
Alexander Aring09e536c2014-07-03 00:20:52 +02001287 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001288 if (rc)
1289 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001290
Alexander Aring4af619a2014-04-24 19:09:05 +02001291 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001292 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001293 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001294
Alexander Aring18c65042014-04-24 19:09:18 +02001295 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001296 if (rc)
1297 return rc;
1298
Alexander Aring6bd2b132014-07-03 00:20:49 +02001299 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1300 if (rc)
1301 return rc;
1302
Sascha Herrmann057dad62013-04-14 22:33:29 +00001303 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001304 if (rc)
1305 return rc;
1306
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001307 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1308 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1309 if (rc)
1310 return rc;
1311 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1312 if (rc)
1313 return rc;
1314
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001315 /* CLKM changes are applied immediately */
1316 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1317 if (rc)
1318 return rc;
1319
1320 /* Turn CLKM Off */
1321 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1322 if (rc)
1323 return rc;
1324 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001325 usleep_range(lp->data->t_sleep_cycle,
1326 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001327
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001328 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001329 if (rc)
1330 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001331 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001332 dev_err(&lp->spi->dev, "DVDD error\n");
1333 return -EINVAL;
1334 }
1335
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001336 return 0;
1337}
1338
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001339static struct at86rf230_platform_data *
1340at86rf230_get_pdata(struct spi_device *spi)
1341{
1342 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001343
1344 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1345 return spi->dev.platform_data;
1346
1347 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1348 if (!pdata)
1349 goto done;
1350
1351 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1352 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1353
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001354 spi->dev.platform_data = pdata;
1355done:
1356 return pdata;
1357}
1358
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001359static int
1360at86rf230_detect_device(struct at86rf230_local *lp)
1361{
1362 unsigned int part, version, val;
1363 u16 man_id = 0;
1364 const char *chip;
1365 int rc;
1366
1367 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1368 if (rc)
1369 return rc;
1370 man_id |= val;
1371
1372 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1373 if (rc)
1374 return rc;
1375 man_id |= (val << 8);
1376
1377 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1378 if (rc)
1379 return rc;
1380
1381 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1382 if (rc)
1383 return rc;
1384
1385 if (man_id != 0x001f) {
1386 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1387 man_id >> 8, man_id & 0xFF);
1388 return -EINVAL;
1389 }
1390
Alexander Aring5a504392014-10-25 17:16:34 +02001391 lp->hw->extra_tx_headroom = 0;
1392 lp->hw->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001393 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1394
1395 switch (part) {
1396 case 2:
1397 chip = "at86rf230";
1398 rc = -ENOTSUPP;
1399 break;
1400 case 3:
1401 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001402 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001403 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001404 break;
1405 case 7:
1406 chip = "at86rf212";
1407 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001408 lp->data = &at86rf212_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001409 lp->hw->flags |= IEEE802154_HW_LBT;
1410 lp->hw->phy->channels_supported[0] = 0x00007FF;
1411 lp->hw->phy->channels_supported[2] = 0x00007FF;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001412 } else {
1413 rc = -ENOTSUPP;
1414 }
1415 break;
1416 case 11:
1417 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001418 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001419 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001420 break;
1421 default:
1422 chip = "unkown";
1423 rc = -ENOTSUPP;
1424 break;
1425 }
1426
1427 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1428
1429 return rc;
1430}
1431
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001432static void
1433at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1434{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001435 lp->state.lp = lp;
1436 spi_message_init(&lp->state.msg);
1437 lp->state.msg.context = &lp->state;
1438 lp->state.trx.tx_buf = lp->state.buf;
1439 lp->state.trx.rx_buf = lp->state.buf;
1440 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1441
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001442 lp->irq.lp = lp;
1443 spi_message_init(&lp->irq.msg);
1444 lp->irq.msg.context = &lp->irq;
1445 lp->irq.trx.tx_buf = lp->irq.buf;
1446 lp->irq.trx.rx_buf = lp->irq.buf;
1447 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1448
1449 lp->tx.lp = lp;
1450 spi_message_init(&lp->tx.msg);
1451 lp->tx.msg.context = &lp->tx;
1452 lp->tx.trx.tx_buf = lp->tx.buf;
1453 lp->tx.trx.rx_buf = lp->tx.buf;
1454 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1455}
1456
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001457static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001458{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001459 struct at86rf230_platform_data *pdata;
Alexander Aring5a504392014-10-25 17:16:34 +02001460 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001461 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001462 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001463 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001464
1465 if (!spi->irq) {
1466 dev_err(&spi->dev, "no IRQ specified\n");
1467 return -EINVAL;
1468 }
1469
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001470 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001471 if (!pdata) {
1472 dev_err(&spi->dev, "no platform_data\n");
1473 return -EINVAL;
1474 }
1475
Alexander Aring3fa27572014-03-15 09:29:06 +01001476 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001477 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1478 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001479 if (rc)
1480 return rc;
1481 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001482
1483 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001484 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1485 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001486 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001487 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001488 }
1489
1490 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001491 if (gpio_is_valid(pdata->rstn)) {
1492 udelay(1);
1493 gpio_set_value(pdata->rstn, 0);
1494 udelay(1);
1495 gpio_set_value(pdata->rstn, 1);
1496 usleep_range(120, 240);
1497 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001498
Alexander Aring5a504392014-10-25 17:16:34 +02001499 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1500 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001501 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001502
Alexander Aring5a504392014-10-25 17:16:34 +02001503 lp = hw->priv;
1504 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001505 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001506 hw->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001507
Alexander Aringf76014f772014-07-03 00:20:44 +02001508 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1509 if (IS_ERR(lp->regmap)) {
1510 rc = PTR_ERR(lp->regmap);
1511 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1512 rc);
1513 goto free_dev;
1514 }
1515
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001516 at86rf230_setup_spi_messages(lp);
1517
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001518 rc = at86rf230_detect_device(lp);
1519 if (rc < 0)
1520 goto free_dev;
1521
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001522 spin_lock_init(&lp->lock);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001523 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001524
1525 spi_set_drvdata(spi, lp);
1526
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001527 rc = at86rf230_hw_init(lp);
1528 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001529 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001530
Alexander Aring19626942014-04-24 19:09:15 +02001531 /* Read irq status register to reset irq line */
1532 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001533 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001534 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001535
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001536 irq_type = irq_get_trigger_type(spi->irq);
1537 if (!irq_type)
1538 irq_type = IRQF_TRIGGER_RISING;
1539
1540 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1541 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001542 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001543 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001544
Alexander Aring5a504392014-10-25 17:16:34 +02001545 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001546 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001547 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001548
1549 return rc;
1550
Alexander Aring640985e2014-07-03 00:20:43 +02001551free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001552 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001553
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001554 return rc;
1555}
1556
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001557static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001558{
1559 struct at86rf230_local *lp = spi_get_drvdata(spi);
1560
Alexander Aring17e84a92014-03-31 03:26:51 +02001561 /* mask all at86rf230 irq's */
1562 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001563 ieee802154_unregister_hw(lp->hw);
1564 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001565 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001566
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001567 return 0;
1568}
1569
Alexander Aring1086b4f2014-04-24 19:09:11 +02001570static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001571 { .compatible = "atmel,at86rf230", },
1572 { .compatible = "atmel,at86rf231", },
1573 { .compatible = "atmel,at86rf233", },
1574 { .compatible = "atmel,at86rf212", },
1575 { },
1576};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001577MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001578
Alexander Aring90b15522014-04-24 19:09:12 +02001579static const struct spi_device_id at86rf230_device_id[] = {
1580 { .name = "at86rf230", },
1581 { .name = "at86rf231", },
1582 { .name = "at86rf233", },
1583 { .name = "at86rf212", },
1584 { },
1585};
1586MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1587
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001588static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001589 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001590 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001591 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001592 .name = "at86rf230",
1593 .owner = THIS_MODULE,
1594 },
1595 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001596 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001597};
1598
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001599module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001600
1601MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1602MODULE_LICENSE("GPL v2");