]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/net/ieee802154/at86rf230.c
ieee802154: add support for listen-before-talk in wpan_phy
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ieee802154 / at86rf230.c
1 /*
2 * AT86RF230/RF231 driver
3 *
4 * Copyright (C) 2009-2012 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22 */
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/gpio.h>
27 #include <linux/delay.h>
28 #include <linux/mutex.h>
29 #include <linux/workqueue.h>
30 #include <linux/spinlock.h>
31 #include <linux/spi/spi.h>
32 #include <linux/spi/at86rf230.h>
33 #include <linux/skbuff.h>
34
35 #include <net/mac802154.h>
36 #include <net/wpan-phy.h>
37
38 struct at86rf230_local {
39 struct spi_device *spi;
40
41 u8 part;
42 u8 vers;
43
44 u8 buf[2];
45 struct mutex bmux;
46
47 struct work_struct irqwork;
48 struct completion tx_complete;
49
50 struct ieee802154_dev *dev;
51
52 spinlock_t lock;
53 bool irq_busy;
54 bool is_tx;
55 };
56
57 static inline int is_rf212(struct at86rf230_local *local)
58 {
59 return local->part == 7;
60 }
61
62 #define RG_TRX_STATUS (0x01)
63 #define SR_TRX_STATUS 0x01, 0x1f, 0
64 #define SR_RESERVED_01_3 0x01, 0x20, 5
65 #define SR_CCA_STATUS 0x01, 0x40, 6
66 #define SR_CCA_DONE 0x01, 0x80, 7
67 #define RG_TRX_STATE (0x02)
68 #define SR_TRX_CMD 0x02, 0x1f, 0
69 #define SR_TRAC_STATUS 0x02, 0xe0, 5
70 #define RG_TRX_CTRL_0 (0x03)
71 #define SR_CLKM_CTRL 0x03, 0x07, 0
72 #define SR_CLKM_SHA_SEL 0x03, 0x08, 3
73 #define SR_PAD_IO_CLKM 0x03, 0x30, 4
74 #define SR_PAD_IO 0x03, 0xc0, 6
75 #define RG_TRX_CTRL_1 (0x04)
76 #define SR_IRQ_POLARITY 0x04, 0x01, 0
77 #define SR_IRQ_MASK_MODE 0x04, 0x02, 1
78 #define SR_SPI_CMD_MODE 0x04, 0x0c, 2
79 #define SR_RX_BL_CTRL 0x04, 0x10, 4
80 #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
81 #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
82 #define SR_PA_EXT_EN 0x04, 0x80, 7
83 #define RG_PHY_TX_PWR (0x05)
84 #define SR_TX_PWR 0x05, 0x0f, 0
85 #define SR_PA_LT 0x05, 0x30, 4
86 #define SR_PA_BUF_LT 0x05, 0xc0, 6
87 #define RG_PHY_RSSI (0x06)
88 #define SR_RSSI 0x06, 0x1f, 0
89 #define SR_RND_VALUE 0x06, 0x60, 5
90 #define SR_RX_CRC_VALID 0x06, 0x80, 7
91 #define RG_PHY_ED_LEVEL (0x07)
92 #define SR_ED_LEVEL 0x07, 0xff, 0
93 #define RG_PHY_CC_CCA (0x08)
94 #define SR_CHANNEL 0x08, 0x1f, 0
95 #define SR_CCA_MODE 0x08, 0x60, 5
96 #define SR_CCA_REQUEST 0x08, 0x80, 7
97 #define RG_CCA_THRES (0x09)
98 #define SR_CCA_ED_THRES 0x09, 0x0f, 0
99 #define SR_RESERVED_09_1 0x09, 0xf0, 4
100 #define RG_RX_CTRL (0x0a)
101 #define SR_PDT_THRES 0x0a, 0x0f, 0
102 #define SR_RESERVED_0a_1 0x0a, 0xf0, 4
103 #define RG_SFD_VALUE (0x0b)
104 #define SR_SFD_VALUE 0x0b, 0xff, 0
105 #define RG_TRX_CTRL_2 (0x0c)
106 #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
107 #define SR_SUB_MODE 0x0c, 0x04, 2
108 #define SR_BPSK_QPSK 0x0c, 0x08, 3
109 #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
110 #define SR_RESERVED_0c_5 0x0c, 0x60, 5
111 #define SR_RX_SAFE_MODE 0x0c, 0x80, 7
112 #define RG_ANT_DIV (0x0d)
113 #define SR_ANT_CTRL 0x0d, 0x03, 0
114 #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
115 #define SR_ANT_DIV_EN 0x0d, 0x08, 3
116 #define SR_RESERVED_0d_2 0x0d, 0x70, 4
117 #define SR_ANT_SEL 0x0d, 0x80, 7
118 #define RG_IRQ_MASK (0x0e)
119 #define SR_IRQ_MASK 0x0e, 0xff, 0
120 #define RG_IRQ_STATUS (0x0f)
121 #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
122 #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
123 #define SR_IRQ_2_RX_START 0x0f, 0x04, 2
124 #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
125 #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
126 #define SR_IRQ_5_AMI 0x0f, 0x20, 5
127 #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
128 #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
129 #define RG_VREG_CTRL (0x10)
130 #define SR_RESERVED_10_6 0x10, 0x03, 0
131 #define SR_DVDD_OK 0x10, 0x04, 2
132 #define SR_DVREG_EXT 0x10, 0x08, 3
133 #define SR_RESERVED_10_3 0x10, 0x30, 4
134 #define SR_AVDD_OK 0x10, 0x40, 6
135 #define SR_AVREG_EXT 0x10, 0x80, 7
136 #define RG_BATMON (0x11)
137 #define SR_BATMON_VTH 0x11, 0x0f, 0
138 #define SR_BATMON_HR 0x11, 0x10, 4
139 #define SR_BATMON_OK 0x11, 0x20, 5
140 #define SR_RESERVED_11_1 0x11, 0xc0, 6
141 #define RG_XOSC_CTRL (0x12)
142 #define SR_XTAL_TRIM 0x12, 0x0f, 0
143 #define SR_XTAL_MODE 0x12, 0xf0, 4
144 #define RG_RX_SYN (0x15)
145 #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
146 #define SR_RESERVED_15_2 0x15, 0x70, 4
147 #define SR_RX_PDT_DIS 0x15, 0x80, 7
148 #define RG_XAH_CTRL_1 (0x17)
149 #define SR_RESERVED_17_8 0x17, 0x01, 0
150 #define SR_AACK_PROM_MODE 0x17, 0x02, 1
151 #define SR_AACK_ACK_TIME 0x17, 0x04, 2
152 #define SR_RESERVED_17_5 0x17, 0x08, 3
153 #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
154 #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
155 #define SR_CSMA_LBT_MODE 0x17, 0x40, 6
156 #define SR_RESERVED_17_1 0x17, 0x80, 7
157 #define RG_FTN_CTRL (0x18)
158 #define SR_RESERVED_18_2 0x18, 0x7f, 0
159 #define SR_FTN_START 0x18, 0x80, 7
160 #define RG_PLL_CF (0x1a)
161 #define SR_RESERVED_1a_2 0x1a, 0x7f, 0
162 #define SR_PLL_CF_START 0x1a, 0x80, 7
163 #define RG_PLL_DCU (0x1b)
164 #define SR_RESERVED_1b_3 0x1b, 0x3f, 0
165 #define SR_RESERVED_1b_2 0x1b, 0x40, 6
166 #define SR_PLL_DCU_START 0x1b, 0x80, 7
167 #define RG_PART_NUM (0x1c)
168 #define SR_PART_NUM 0x1c, 0xff, 0
169 #define RG_VERSION_NUM (0x1d)
170 #define SR_VERSION_NUM 0x1d, 0xff, 0
171 #define RG_MAN_ID_0 (0x1e)
172 #define SR_MAN_ID_0 0x1e, 0xff, 0
173 #define RG_MAN_ID_1 (0x1f)
174 #define SR_MAN_ID_1 0x1f, 0xff, 0
175 #define RG_SHORT_ADDR_0 (0x20)
176 #define SR_SHORT_ADDR_0 0x20, 0xff, 0
177 #define RG_SHORT_ADDR_1 (0x21)
178 #define SR_SHORT_ADDR_1 0x21, 0xff, 0
179 #define RG_PAN_ID_0 (0x22)
180 #define SR_PAN_ID_0 0x22, 0xff, 0
181 #define RG_PAN_ID_1 (0x23)
182 #define SR_PAN_ID_1 0x23, 0xff, 0
183 #define RG_IEEE_ADDR_0 (0x24)
184 #define SR_IEEE_ADDR_0 0x24, 0xff, 0
185 #define RG_IEEE_ADDR_1 (0x25)
186 #define SR_IEEE_ADDR_1 0x25, 0xff, 0
187 #define RG_IEEE_ADDR_2 (0x26)
188 #define SR_IEEE_ADDR_2 0x26, 0xff, 0
189 #define RG_IEEE_ADDR_3 (0x27)
190 #define SR_IEEE_ADDR_3 0x27, 0xff, 0
191 #define RG_IEEE_ADDR_4 (0x28)
192 #define SR_IEEE_ADDR_4 0x28, 0xff, 0
193 #define RG_IEEE_ADDR_5 (0x29)
194 #define SR_IEEE_ADDR_5 0x29, 0xff, 0
195 #define RG_IEEE_ADDR_6 (0x2a)
196 #define SR_IEEE_ADDR_6 0x2a, 0xff, 0
197 #define RG_IEEE_ADDR_7 (0x2b)
198 #define SR_IEEE_ADDR_7 0x2b, 0xff, 0
199 #define RG_XAH_CTRL_0 (0x2c)
200 #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
201 #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
202 #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
203 #define RG_CSMA_SEED_0 (0x2d)
204 #define SR_CSMA_SEED_0 0x2d, 0xff, 0
205 #define RG_CSMA_SEED_1 (0x2e)
206 #define SR_CSMA_SEED_1 0x2e, 0x07, 0
207 #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
208 #define SR_AACK_DIS_ACK 0x2e, 0x10, 4
209 #define SR_AACK_SET_PD 0x2e, 0x20, 5
210 #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
211 #define RG_CSMA_BE (0x2f)
212 #define SR_MIN_BE 0x2f, 0x0f, 0
213 #define SR_MAX_BE 0x2f, 0xf0, 4
214
215 #define CMD_REG 0x80
216 #define CMD_REG_MASK 0x3f
217 #define CMD_WRITE 0x40
218 #define CMD_FB 0x20
219
220 #define IRQ_BAT_LOW (1 << 7)
221 #define IRQ_TRX_UR (1 << 6)
222 #define IRQ_AMI (1 << 5)
223 #define IRQ_CCA_ED (1 << 4)
224 #define IRQ_TRX_END (1 << 3)
225 #define IRQ_RX_START (1 << 2)
226 #define IRQ_PLL_UNL (1 << 1)
227 #define IRQ_PLL_LOCK (1 << 0)
228
229 #define IRQ_ACTIVE_HIGH 0
230 #define IRQ_ACTIVE_LOW 1
231
232 #define STATE_P_ON 0x00 /* BUSY */
233 #define STATE_BUSY_RX 0x01
234 #define STATE_BUSY_TX 0x02
235 #define STATE_FORCE_TRX_OFF 0x03
236 #define STATE_FORCE_TX_ON 0x04 /* IDLE */
237 /* 0x05 */ /* INVALID_PARAMETER */
238 #define STATE_RX_ON 0x06
239 /* 0x07 */ /* SUCCESS */
240 #define STATE_TRX_OFF 0x08
241 #define STATE_TX_ON 0x09
242 /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
243 #define STATE_SLEEP 0x0F
244 #define STATE_BUSY_RX_AACK 0x11
245 #define STATE_BUSY_TX_ARET 0x12
246 #define STATE_RX_AACK_ON 0x16
247 #define STATE_TX_ARET_ON 0x19
248 #define STATE_RX_ON_NOCLK 0x1C
249 #define STATE_RX_AACK_ON_NOCLK 0x1D
250 #define STATE_BUSY_RX_AACK_NOCLK 0x1E
251 #define STATE_TRANSITION_IN_PROGRESS 0x1F
252
253 static int
254 __at86rf230_detect_device(struct spi_device *spi, u16 *man_id, u8 *part,
255 u8 *version)
256 {
257 u8 data[4];
258 u8 *buf = kmalloc(2, GFP_KERNEL);
259 int status;
260 struct spi_message msg;
261 struct spi_transfer xfer = {
262 .len = 2,
263 .tx_buf = buf,
264 .rx_buf = buf,
265 };
266 u8 reg;
267
268 if (!buf)
269 return -ENOMEM;
270
271 for (reg = RG_PART_NUM; reg <= RG_MAN_ID_1; reg++) {
272 buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
273 buf[1] = 0xff;
274 dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
275 spi_message_init(&msg);
276 spi_message_add_tail(&xfer, &msg);
277
278 status = spi_sync(spi, &msg);
279 dev_vdbg(&spi->dev, "status = %d\n", status);
280 if (msg.status)
281 status = msg.status;
282
283 dev_vdbg(&spi->dev, "status = %d\n", status);
284 dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
285 dev_vdbg(&spi->dev, "buf[1] = %02x\n", buf[1]);
286
287 if (status == 0)
288 data[reg - RG_PART_NUM] = buf[1];
289 else
290 break;
291 }
292
293 if (status == 0) {
294 *part = data[0];
295 *version = data[1];
296 *man_id = (data[3] << 8) | data[2];
297 }
298
299 kfree(buf);
300
301 return status;
302 }
303
304 static int
305 __at86rf230_write(struct at86rf230_local *lp, u8 addr, u8 data)
306 {
307 u8 *buf = lp->buf;
308 int status;
309 struct spi_message msg;
310 struct spi_transfer xfer = {
311 .len = 2,
312 .tx_buf = buf,
313 };
314
315 buf[0] = (addr & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
316 buf[1] = data;
317 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
318 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
319 spi_message_init(&msg);
320 spi_message_add_tail(&xfer, &msg);
321
322 status = spi_sync(lp->spi, &msg);
323 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
324 if (msg.status)
325 status = msg.status;
326
327 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
328 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
329 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
330
331 return status;
332 }
333
334 static int
335 __at86rf230_read_subreg(struct at86rf230_local *lp,
336 u8 addr, u8 mask, int shift, u8 *data)
337 {
338 u8 *buf = lp->buf;
339 int status;
340 struct spi_message msg;
341 struct spi_transfer xfer = {
342 .len = 2,
343 .tx_buf = buf,
344 .rx_buf = buf,
345 };
346
347 buf[0] = (addr & CMD_REG_MASK) | CMD_REG;
348 buf[1] = 0xff;
349 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
350 spi_message_init(&msg);
351 spi_message_add_tail(&xfer, &msg);
352
353 status = spi_sync(lp->spi, &msg);
354 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
355 if (msg.status)
356 status = msg.status;
357
358 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
359 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
360 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
361
362 if (status == 0)
363 *data = buf[1];
364
365 return status;
366 }
367
368 static int
369 at86rf230_read_subreg(struct at86rf230_local *lp,
370 u8 addr, u8 mask, int shift, u8 *data)
371 {
372 int status;
373
374 mutex_lock(&lp->bmux);
375 status = __at86rf230_read_subreg(lp, addr, mask, shift, data);
376 mutex_unlock(&lp->bmux);
377
378 return status;
379 }
380
381 static int
382 at86rf230_write_subreg(struct at86rf230_local *lp,
383 u8 addr, u8 mask, int shift, u8 data)
384 {
385 int status;
386 u8 val;
387
388 mutex_lock(&lp->bmux);
389 status = __at86rf230_read_subreg(lp, addr, 0xff, 0, &val);
390 if (status)
391 goto out;
392
393 val &= ~mask;
394 val |= (data << shift) & mask;
395
396 status = __at86rf230_write(lp, addr, val);
397 out:
398 mutex_unlock(&lp->bmux);
399
400 return status;
401 }
402
403 static int
404 at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len)
405 {
406 u8 *buf = lp->buf;
407 int status;
408 struct spi_message msg;
409 struct spi_transfer xfer_head = {
410 .len = 2,
411 .tx_buf = buf,
412
413 };
414 struct spi_transfer xfer_buf = {
415 .len = len,
416 .tx_buf = data,
417 };
418
419 mutex_lock(&lp->bmux);
420 buf[0] = CMD_WRITE | CMD_FB;
421 buf[1] = len + 2; /* 2 bytes for CRC that isn't written */
422
423 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
424 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
425
426 spi_message_init(&msg);
427 spi_message_add_tail(&xfer_head, &msg);
428 spi_message_add_tail(&xfer_buf, &msg);
429
430 status = spi_sync(lp->spi, &msg);
431 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
432 if (msg.status)
433 status = msg.status;
434
435 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
436 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
437 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
438
439 mutex_unlock(&lp->bmux);
440 return status;
441 }
442
443 static int
444 at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi)
445 {
446 u8 *buf = lp->buf;
447 int status;
448 struct spi_message msg;
449 struct spi_transfer xfer_head = {
450 .len = 2,
451 .tx_buf = buf,
452 .rx_buf = buf,
453 };
454 struct spi_transfer xfer_head1 = {
455 .len = 2,
456 .tx_buf = buf,
457 .rx_buf = buf,
458 };
459 struct spi_transfer xfer_buf = {
460 .len = 0,
461 .rx_buf = data,
462 };
463
464 mutex_lock(&lp->bmux);
465
466 buf[0] = CMD_FB;
467 buf[1] = 0x00;
468
469 spi_message_init(&msg);
470 spi_message_add_tail(&xfer_head, &msg);
471
472 status = spi_sync(lp->spi, &msg);
473 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
474
475 xfer_buf.len = *(buf + 1) + 1;
476 *len = buf[1];
477
478 buf[0] = CMD_FB;
479 buf[1] = 0x00;
480
481 spi_message_init(&msg);
482 spi_message_add_tail(&xfer_head1, &msg);
483 spi_message_add_tail(&xfer_buf, &msg);
484
485 status = spi_sync(lp->spi, &msg);
486
487 if (msg.status)
488 status = msg.status;
489
490 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
491 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
492 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
493
494 if (status) {
495 if (lqi && (*len > lp->buf[1]))
496 *lqi = data[lp->buf[1]];
497 }
498 mutex_unlock(&lp->bmux);
499
500 return status;
501 }
502
503 static int
504 at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
505 {
506 might_sleep();
507 BUG_ON(!level);
508 *level = 0xbe;
509 return 0;
510 }
511
512 static int
513 at86rf230_state(struct ieee802154_dev *dev, int state)
514 {
515 struct at86rf230_local *lp = dev->priv;
516 int rc;
517 u8 val;
518 u8 desired_status;
519
520 might_sleep();
521
522 if (state == STATE_FORCE_TX_ON)
523 desired_status = STATE_TX_ON;
524 else if (state == STATE_FORCE_TRX_OFF)
525 desired_status = STATE_TRX_OFF;
526 else
527 desired_status = state;
528
529 do {
530 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
531 if (rc)
532 goto err;
533 } while (val == STATE_TRANSITION_IN_PROGRESS);
534
535 if (val == desired_status)
536 return 0;
537
538 /* state is equal to phy states */
539 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state);
540 if (rc)
541 goto err;
542
543 do {
544 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
545 if (rc)
546 goto err;
547 } while (val == STATE_TRANSITION_IN_PROGRESS);
548
549
550 if (val == desired_status)
551 return 0;
552
553 pr_err("unexpected state change: %d, asked for %d\n", val, state);
554 return -EBUSY;
555
556 err:
557 pr_err("error: %d\n", rc);
558 return rc;
559 }
560
561 static int
562 at86rf230_start(struct ieee802154_dev *dev)
563 {
564 struct at86rf230_local *lp = dev->priv;
565 u8 rc;
566
567 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
568 if (rc)
569 return rc;
570
571 return at86rf230_state(dev, STATE_RX_AACK_ON);
572 }
573
574 static void
575 at86rf230_stop(struct ieee802154_dev *dev)
576 {
577 at86rf230_state(dev, STATE_FORCE_TRX_OFF);
578 }
579
580 static int
581 at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel)
582 {
583 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
584 }
585
586 static int
587 at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
588 {
589 int rc;
590
591 if (channel == 0)
592 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
593 else
594 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
595 if (rc < 0)
596 return rc;
597
598 if (page == 0)
599 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
600 else
601 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
602 if (rc < 0)
603 return rc;
604
605 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
606 }
607
608 static int
609 at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
610 {
611 struct at86rf230_local *lp = dev->priv;
612 int rc;
613
614 might_sleep();
615
616 if (page < 0 || page > 31 ||
617 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
618 WARN_ON(1);
619 return -EINVAL;
620 }
621
622 if (is_rf212(lp))
623 rc = at86rf212_set_channel(lp, page, channel);
624 else
625 rc = at86rf230_set_channel(lp, page, channel);
626 if (rc < 0)
627 return rc;
628
629 msleep(1); /* Wait for PLL */
630 dev->phy->current_channel = channel;
631 dev->phy->current_page = page;
632
633 return 0;
634 }
635
636 static int
637 at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
638 {
639 struct at86rf230_local *lp = dev->priv;
640 int rc;
641 unsigned long flags;
642
643 spin_lock(&lp->lock);
644 if (lp->irq_busy) {
645 spin_unlock(&lp->lock);
646 return -EBUSY;
647 }
648 spin_unlock(&lp->lock);
649
650 might_sleep();
651
652 rc = at86rf230_state(dev, STATE_FORCE_TX_ON);
653 if (rc)
654 goto err;
655
656 spin_lock_irqsave(&lp->lock, flags);
657 lp->is_tx = 1;
658 reinit_completion(&lp->tx_complete);
659 spin_unlock_irqrestore(&lp->lock, flags);
660
661 rc = at86rf230_write_fbuf(lp, skb->data, skb->len);
662 if (rc)
663 goto err_rx;
664
665 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX);
666 if (rc)
667 goto err_rx;
668
669 rc = wait_for_completion_interruptible(&lp->tx_complete);
670 if (rc < 0)
671 goto err_rx;
672
673 rc = at86rf230_start(dev);
674
675 return rc;
676
677 err_rx:
678 at86rf230_start(dev);
679 err:
680 pr_err("error: %d\n", rc);
681
682 spin_lock_irqsave(&lp->lock, flags);
683 lp->is_tx = 0;
684 spin_unlock_irqrestore(&lp->lock, flags);
685
686 return rc;
687 }
688
689 static int at86rf230_rx(struct at86rf230_local *lp)
690 {
691 u8 len = 128, lqi = 0;
692 struct sk_buff *skb;
693
694 skb = alloc_skb(len, GFP_KERNEL);
695
696 if (!skb)
697 return -ENOMEM;
698
699 if (at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi))
700 goto err;
701
702 if (len < 2)
703 goto err;
704
705 skb_trim(skb, len - 2); /* We do not put CRC into the frame */
706
707 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
708
709 dev_dbg(&lp->spi->dev, "READ_FBUF: %d %x\n", len, lqi);
710
711 return 0;
712 err:
713 pr_debug("received frame is too small\n");
714
715 kfree_skb(skb);
716 return -EINVAL;
717 }
718
719 static int
720 at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
721 struct ieee802154_hw_addr_filt *filt,
722 unsigned long changed)
723 {
724 struct at86rf230_local *lp = dev->priv;
725
726 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
727 dev_vdbg(&lp->spi->dev,
728 "at86rf230_set_hw_addr_filt called for saddr\n");
729 __at86rf230_write(lp, RG_SHORT_ADDR_0, filt->short_addr);
730 __at86rf230_write(lp, RG_SHORT_ADDR_1, filt->short_addr >> 8);
731 }
732
733 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
734 dev_vdbg(&lp->spi->dev,
735 "at86rf230_set_hw_addr_filt called for pan id\n");
736 __at86rf230_write(lp, RG_PAN_ID_0, filt->pan_id);
737 __at86rf230_write(lp, RG_PAN_ID_1, filt->pan_id >> 8);
738 }
739
740 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
741 dev_vdbg(&lp->spi->dev,
742 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
743 at86rf230_write_subreg(lp, SR_IEEE_ADDR_0, filt->ieee_addr[7]);
744 at86rf230_write_subreg(lp, SR_IEEE_ADDR_1, filt->ieee_addr[6]);
745 at86rf230_write_subreg(lp, SR_IEEE_ADDR_2, filt->ieee_addr[5]);
746 at86rf230_write_subreg(lp, SR_IEEE_ADDR_3, filt->ieee_addr[4]);
747 at86rf230_write_subreg(lp, SR_IEEE_ADDR_4, filt->ieee_addr[3]);
748 at86rf230_write_subreg(lp, SR_IEEE_ADDR_5, filt->ieee_addr[2]);
749 at86rf230_write_subreg(lp, SR_IEEE_ADDR_6, filt->ieee_addr[1]);
750 at86rf230_write_subreg(lp, SR_IEEE_ADDR_7, filt->ieee_addr[0]);
751 }
752
753 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
754 dev_vdbg(&lp->spi->dev,
755 "at86rf230_set_hw_addr_filt called for panc change\n");
756 if (filt->pan_coord)
757 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
758 else
759 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
760 }
761
762 return 0;
763 }
764
765 static int
766 at86rf212_set_txpower(struct ieee802154_dev *dev, int db)
767 {
768 struct at86rf230_local *lp = dev->priv;
769 int rc;
770
771 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
772 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
773 * 0dB.
774 * thus, supported values for db range from -26 to 5, for 31dB of
775 * reduction to 0dB of reduction.
776 */
777 if (db > 5 || db < -26)
778 return -EINVAL;
779
780 db = -(db - 5);
781
782 rc = __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
783 if (rc)
784 return rc;
785
786 return 0;
787 }
788
789 static int
790 at86rf212_set_lbt(struct ieee802154_dev *dev, bool on)
791 {
792 struct at86rf230_local *lp = dev->priv;
793
794 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
795 }
796
797 static struct ieee802154_ops at86rf230_ops = {
798 .owner = THIS_MODULE,
799 .xmit = at86rf230_xmit,
800 .ed = at86rf230_ed,
801 .set_channel = at86rf230_channel,
802 .start = at86rf230_start,
803 .stop = at86rf230_stop,
804 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
805 };
806
807 static struct ieee802154_ops at86rf212_ops = {
808 .owner = THIS_MODULE,
809 .xmit = at86rf230_xmit,
810 .ed = at86rf230_ed,
811 .set_channel = at86rf230_channel,
812 .start = at86rf230_start,
813 .stop = at86rf230_stop,
814 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
815 .set_txpower = at86rf212_set_txpower,
816 .set_lbt = at86rf212_set_lbt,
817 };
818
819 static void at86rf230_irqwork(struct work_struct *work)
820 {
821 struct at86rf230_local *lp =
822 container_of(work, struct at86rf230_local, irqwork);
823 u8 status = 0, val;
824 int rc;
825 unsigned long flags;
826
827 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &val);
828 status |= val;
829
830 status &= ~IRQ_PLL_LOCK; /* ignore */
831 status &= ~IRQ_RX_START; /* ignore */
832 status &= ~IRQ_AMI; /* ignore */
833 status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/
834
835 if (status & IRQ_TRX_END) {
836 spin_lock_irqsave(&lp->lock, flags);
837 status &= ~IRQ_TRX_END;
838 if (lp->is_tx) {
839 lp->is_tx = 0;
840 spin_unlock_irqrestore(&lp->lock, flags);
841 complete(&lp->tx_complete);
842 } else {
843 spin_unlock_irqrestore(&lp->lock, flags);
844 at86rf230_rx(lp);
845 }
846 }
847
848 spin_lock_irqsave(&lp->lock, flags);
849 lp->irq_busy = 0;
850 spin_unlock_irqrestore(&lp->lock, flags);
851 }
852
853 static void at86rf230_irqwork_level(struct work_struct *work)
854 {
855 struct at86rf230_local *lp =
856 container_of(work, struct at86rf230_local, irqwork);
857
858 at86rf230_irqwork(work);
859
860 enable_irq(lp->spi->irq);
861 }
862
863 static irqreturn_t at86rf230_isr(int irq, void *data)
864 {
865 struct at86rf230_local *lp = data;
866
867 spin_lock(&lp->lock);
868 lp->irq_busy = 1;
869 spin_unlock(&lp->lock);
870
871 schedule_work(&lp->irqwork);
872
873 return IRQ_HANDLED;
874 }
875
876 static irqreturn_t at86rf230_isr_level(int irq, void *data)
877 {
878 disable_irq_nosync(irq);
879
880 return at86rf230_isr(irq, data);
881 }
882
883 static int at86rf230_irq_polarity(struct at86rf230_local *lp, int pol)
884 {
885 return at86rf230_write_subreg(lp, SR_IRQ_POLARITY, pol);
886 }
887
888 static int at86rf230_hw_init(struct at86rf230_local *lp)
889 {
890 struct at86rf230_platform_data *pdata = lp->spi->dev.platform_data;
891 int rc, irq_pol;
892 u8 status;
893
894 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
895 if (rc)
896 return rc;
897
898 dev_info(&lp->spi->dev, "Status: %02x\n", status);
899 if (status == STATE_P_ON) {
900 rc = at86rf230_write_subreg(lp, SR_TRX_CMD,
901 STATE_FORCE_TRX_OFF);
902 if (rc)
903 return rc;
904 msleep(1);
905 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
906 if (rc)
907 return rc;
908 dev_info(&lp->spi->dev, "Status: %02x\n", status);
909 }
910
911 /* configure irq polarity, defaults to high active */
912 if (pdata->irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
913 irq_pol = IRQ_ACTIVE_LOW;
914 else
915 irq_pol = IRQ_ACTIVE_HIGH;
916
917 rc = at86rf230_irq_polarity(lp, irq_pol);
918 if (rc)
919 return rc;
920
921 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
922 if (rc)
923 return rc;
924
925 /* CLKM changes are applied immediately */
926 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
927 if (rc)
928 return rc;
929
930 /* Turn CLKM Off */
931 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
932 if (rc)
933 return rc;
934 /* Wait the next SLEEP cycle */
935 msleep(100);
936
937 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ON);
938 if (rc)
939 return rc;
940 msleep(1);
941
942 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
943 if (rc)
944 return rc;
945 dev_info(&lp->spi->dev, "Status: %02x\n", status);
946
947 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &status);
948 if (rc)
949 return rc;
950 if (!status) {
951 dev_err(&lp->spi->dev, "DVDD error\n");
952 return -EINVAL;
953 }
954
955 rc = at86rf230_read_subreg(lp, SR_AVDD_OK, &status);
956 if (rc)
957 return rc;
958 if (!status) {
959 dev_err(&lp->spi->dev, "AVDD error\n");
960 return -EINVAL;
961 }
962
963 return 0;
964 }
965
966 static int at86rf230_probe(struct spi_device *spi)
967 {
968 struct at86rf230_platform_data *pdata;
969 struct ieee802154_dev *dev;
970 struct at86rf230_local *lp;
971 u16 man_id = 0;
972 u8 part = 0, version = 0, status;
973 irq_handler_t irq_handler;
974 work_func_t irq_worker;
975 int rc;
976 const char *chip;
977 struct ieee802154_ops *ops = NULL;
978
979 if (!spi->irq) {
980 dev_err(&spi->dev, "no IRQ specified\n");
981 return -EINVAL;
982 }
983
984 pdata = spi->dev.platform_data;
985 if (!pdata) {
986 dev_err(&spi->dev, "no platform_data\n");
987 return -EINVAL;
988 }
989
990 rc = gpio_request(pdata->rstn, "rstn");
991 if (rc)
992 return rc;
993
994 if (gpio_is_valid(pdata->slp_tr)) {
995 rc = gpio_request(pdata->slp_tr, "slp_tr");
996 if (rc)
997 goto err_slp_tr;
998 }
999
1000 rc = gpio_direction_output(pdata->rstn, 1);
1001 if (rc)
1002 goto err_gpio_dir;
1003
1004 if (gpio_is_valid(pdata->slp_tr)) {
1005 rc = gpio_direction_output(pdata->slp_tr, 0);
1006 if (rc)
1007 goto err_gpio_dir;
1008 }
1009
1010 /* Reset */
1011 msleep(1);
1012 gpio_set_value(pdata->rstn, 0);
1013 msleep(1);
1014 gpio_set_value(pdata->rstn, 1);
1015 msleep(1);
1016
1017 rc = __at86rf230_detect_device(spi, &man_id, &part, &version);
1018 if (rc < 0)
1019 goto err_gpio_dir;
1020
1021 if (man_id != 0x001f) {
1022 dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1023 man_id >> 8, man_id & 0xFF);
1024 rc = -EINVAL;
1025 goto err_gpio_dir;
1026 }
1027
1028 switch (part) {
1029 case 2:
1030 chip = "at86rf230";
1031 /* FIXME: should be easy to support; */
1032 break;
1033 case 3:
1034 chip = "at86rf231";
1035 ops = &at86rf230_ops;
1036 break;
1037 case 7:
1038 chip = "at86rf212";
1039 if (version == 1)
1040 ops = &at86rf212_ops;
1041 break;
1042 default:
1043 chip = "UNKNOWN";
1044 break;
1045 }
1046
1047 dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version);
1048 if (!ops) {
1049 rc = -ENOTSUPP;
1050 goto err_gpio_dir;
1051 }
1052
1053 dev = ieee802154_alloc_device(sizeof(*lp), ops);
1054 if (!dev) {
1055 rc = -ENOMEM;
1056 goto err_gpio_dir;
1057 }
1058
1059 lp = dev->priv;
1060 lp->dev = dev;
1061 lp->part = part;
1062 lp->vers = version;
1063
1064 lp->spi = spi;
1065
1066 dev->parent = &spi->dev;
1067 dev->extra_tx_headroom = 0;
1068 dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK;
1069
1070 if (pdata->irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
1071 irq_worker = at86rf230_irqwork;
1072 irq_handler = at86rf230_isr;
1073 } else {
1074 irq_worker = at86rf230_irqwork_level;
1075 irq_handler = at86rf230_isr_level;
1076 }
1077
1078 mutex_init(&lp->bmux);
1079 INIT_WORK(&lp->irqwork, irq_worker);
1080 spin_lock_init(&lp->lock);
1081 init_completion(&lp->tx_complete);
1082
1083 spi_set_drvdata(spi, lp);
1084
1085 if (is_rf212(lp)) {
1086 dev->phy->channels_supported[0] = 0x00007FF;
1087 dev->phy->channels_supported[2] = 0x00007FF;
1088 } else {
1089 dev->phy->channels_supported[0] = 0x7FFF800;
1090 }
1091
1092 rc = at86rf230_hw_init(lp);
1093 if (rc)
1094 goto err_hw_init;
1095
1096 rc = request_irq(spi->irq, irq_handler,
1097 IRQF_SHARED | pdata->irq_type,
1098 dev_name(&spi->dev), lp);
1099 if (rc)
1100 goto err_hw_init;
1101
1102 /* Read irq status register to reset irq line */
1103 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1104 if (rc)
1105 goto err_irq;
1106
1107 rc = ieee802154_register_device(lp->dev);
1108 if (rc)
1109 goto err_irq;
1110
1111 return rc;
1112
1113 err_irq:
1114 free_irq(spi->irq, lp);
1115 err_hw_init:
1116 flush_work(&lp->irqwork);
1117 spi_set_drvdata(spi, NULL);
1118 mutex_destroy(&lp->bmux);
1119 ieee802154_free_device(lp->dev);
1120
1121 err_gpio_dir:
1122 if (gpio_is_valid(pdata->slp_tr))
1123 gpio_free(pdata->slp_tr);
1124 err_slp_tr:
1125 gpio_free(pdata->rstn);
1126 return rc;
1127 }
1128
1129 static int at86rf230_remove(struct spi_device *spi)
1130 {
1131 struct at86rf230_local *lp = spi_get_drvdata(spi);
1132 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1133
1134 ieee802154_unregister_device(lp->dev);
1135
1136 free_irq(spi->irq, lp);
1137 flush_work(&lp->irqwork);
1138
1139 if (gpio_is_valid(pdata->slp_tr))
1140 gpio_free(pdata->slp_tr);
1141 gpio_free(pdata->rstn);
1142
1143 mutex_destroy(&lp->bmux);
1144 ieee802154_free_device(lp->dev);
1145
1146 dev_dbg(&spi->dev, "unregistered at86rf230\n");
1147 return 0;
1148 }
1149
1150 static struct spi_driver at86rf230_driver = {
1151 .driver = {
1152 .name = "at86rf230",
1153 .owner = THIS_MODULE,
1154 },
1155 .probe = at86rf230_probe,
1156 .remove = at86rf230_remove,
1157 };
1158
1159 module_spi_driver(at86rf230_driver);
1160
1161 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1162 MODULE_LICENSE("GPL v2");