]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ieee802154/at86rf230.c
04a995dad593d0d3fcea6447d8a7d62121acdacb
[karo-tx-linux.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_RESERVED_17_2        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 struct ieee802154_ops at86rf230_ops = {
790         .owner = THIS_MODULE,
791         .xmit = at86rf230_xmit,
792         .ed = at86rf230_ed,
793         .set_channel = at86rf230_channel,
794         .start = at86rf230_start,
795         .stop = at86rf230_stop,
796         .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
797 };
798
799 static struct ieee802154_ops at86rf212_ops = {
800         .owner = THIS_MODULE,
801         .xmit = at86rf230_xmit,
802         .ed = at86rf230_ed,
803         .set_channel = at86rf230_channel,
804         .start = at86rf230_start,
805         .stop = at86rf230_stop,
806         .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
807         .set_txpower = at86rf212_set_txpower,
808 };
809
810 static void at86rf230_irqwork(struct work_struct *work)
811 {
812         struct at86rf230_local *lp =
813                 container_of(work, struct at86rf230_local, irqwork);
814         u8 status = 0, val;
815         int rc;
816         unsigned long flags;
817
818         rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &val);
819         status |= val;
820
821         status &= ~IRQ_PLL_LOCK; /* ignore */
822         status &= ~IRQ_RX_START; /* ignore */
823         status &= ~IRQ_AMI; /* ignore */
824         status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/
825
826         if (status & IRQ_TRX_END) {
827                 spin_lock_irqsave(&lp->lock, flags);
828                 status &= ~IRQ_TRX_END;
829                 if (lp->is_tx) {
830                         lp->is_tx = 0;
831                         spin_unlock_irqrestore(&lp->lock, flags);
832                         complete(&lp->tx_complete);
833                 } else {
834                         spin_unlock_irqrestore(&lp->lock, flags);
835                         at86rf230_rx(lp);
836                 }
837         }
838
839         spin_lock_irqsave(&lp->lock, flags);
840         lp->irq_busy = 0;
841         spin_unlock_irqrestore(&lp->lock, flags);
842 }
843
844 static void at86rf230_irqwork_level(struct work_struct *work)
845 {
846         struct at86rf230_local *lp =
847                 container_of(work, struct at86rf230_local, irqwork);
848
849         at86rf230_irqwork(work);
850
851         enable_irq(lp->spi->irq);
852 }
853
854 static irqreturn_t at86rf230_isr(int irq, void *data)
855 {
856         struct at86rf230_local *lp = data;
857
858         spin_lock(&lp->lock);
859         lp->irq_busy = 1;
860         spin_unlock(&lp->lock);
861
862         schedule_work(&lp->irqwork);
863
864         return IRQ_HANDLED;
865 }
866
867 static irqreturn_t at86rf230_isr_level(int irq, void *data)
868 {
869         disable_irq_nosync(irq);
870
871         return at86rf230_isr(irq, data);
872 }
873
874 static int at86rf230_irq_polarity(struct at86rf230_local *lp, int pol)
875 {
876         return at86rf230_write_subreg(lp, SR_IRQ_POLARITY, pol);
877 }
878
879 static int at86rf230_hw_init(struct at86rf230_local *lp)
880 {
881         struct at86rf230_platform_data *pdata = lp->spi->dev.platform_data;
882         int rc, irq_pol;
883         u8 status;
884
885         rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
886         if (rc)
887                 return rc;
888
889         dev_info(&lp->spi->dev, "Status: %02x\n", status);
890         if (status == STATE_P_ON) {
891                 rc = at86rf230_write_subreg(lp, SR_TRX_CMD,
892                                             STATE_FORCE_TRX_OFF);
893                 if (rc)
894                         return rc;
895                 msleep(1);
896                 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
897                 if (rc)
898                         return rc;
899                 dev_info(&lp->spi->dev, "Status: %02x\n", status);
900         }
901
902         /* configure irq polarity, defaults to high active */
903         if (pdata->irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
904                 irq_pol = IRQ_ACTIVE_LOW;
905         else
906                 irq_pol = IRQ_ACTIVE_HIGH;
907
908         rc = at86rf230_irq_polarity(lp, irq_pol);
909         if (rc)
910                 return rc;
911
912         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
913         if (rc)
914                 return rc;
915
916         /* CLKM changes are applied immediately */
917         rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
918         if (rc)
919                 return rc;
920
921         /* Turn CLKM Off */
922         rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
923         if (rc)
924                 return rc;
925         /* Wait the next SLEEP cycle */
926         msleep(100);
927
928         rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ON);
929         if (rc)
930                 return rc;
931         msleep(1);
932
933         rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status);
934         if (rc)
935                 return rc;
936         dev_info(&lp->spi->dev, "Status: %02x\n", status);
937
938         rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &status);
939         if (rc)
940                 return rc;
941         if (!status) {
942                 dev_err(&lp->spi->dev, "DVDD error\n");
943                 return -EINVAL;
944         }
945
946         rc = at86rf230_read_subreg(lp, SR_AVDD_OK, &status);
947         if (rc)
948                 return rc;
949         if (!status) {
950                 dev_err(&lp->spi->dev, "AVDD error\n");
951                 return -EINVAL;
952         }
953
954         return 0;
955 }
956
957 static int at86rf230_probe(struct spi_device *spi)
958 {
959         struct at86rf230_platform_data *pdata;
960         struct ieee802154_dev *dev;
961         struct at86rf230_local *lp;
962         u16 man_id = 0;
963         u8 part = 0, version = 0, status;
964         irq_handler_t irq_handler;
965         work_func_t irq_worker;
966         int rc;
967         const char *chip;
968         struct ieee802154_ops *ops = NULL;
969
970         if (!spi->irq) {
971                 dev_err(&spi->dev, "no IRQ specified\n");
972                 return -EINVAL;
973         }
974
975         pdata = spi->dev.platform_data;
976         if (!pdata) {
977                 dev_err(&spi->dev, "no platform_data\n");
978                 return -EINVAL;
979         }
980
981         rc = gpio_request(pdata->rstn, "rstn");
982         if (rc)
983                 return rc;
984
985         if (gpio_is_valid(pdata->slp_tr)) {
986                 rc = gpio_request(pdata->slp_tr, "slp_tr");
987                 if (rc)
988                         goto err_slp_tr;
989         }
990
991         rc = gpio_direction_output(pdata->rstn, 1);
992         if (rc)
993                 goto err_gpio_dir;
994
995         if (gpio_is_valid(pdata->slp_tr)) {
996                 rc = gpio_direction_output(pdata->slp_tr, 0);
997                 if (rc)
998                         goto err_gpio_dir;
999         }
1000
1001         /* Reset */
1002         msleep(1);
1003         gpio_set_value(pdata->rstn, 0);
1004         msleep(1);
1005         gpio_set_value(pdata->rstn, 1);
1006         msleep(1);
1007
1008         rc = __at86rf230_detect_device(spi, &man_id, &part, &version);
1009         if (rc < 0)
1010                 goto err_gpio_dir;
1011
1012         if (man_id != 0x001f) {
1013                 dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1014                         man_id >> 8, man_id & 0xFF);
1015                 rc = -EINVAL;
1016                 goto err_gpio_dir;
1017         }
1018
1019         switch (part) {
1020         case 2:
1021                 chip = "at86rf230";
1022                 /* FIXME: should be easy to support; */
1023                 break;
1024         case 3:
1025                 chip = "at86rf231";
1026                 ops = &at86rf230_ops;
1027                 break;
1028         case 7:
1029                 chip = "at86rf212";
1030                 if (version == 1)
1031                         ops = &at86rf212_ops;
1032                 break;
1033         default:
1034                 chip = "UNKNOWN";
1035                 break;
1036         }
1037
1038         dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version);
1039         if (!ops) {
1040                 rc = -ENOTSUPP;
1041                 goto err_gpio_dir;
1042         }
1043
1044         dev = ieee802154_alloc_device(sizeof(*lp), ops);
1045         if (!dev) {
1046                 rc = -ENOMEM;
1047                 goto err_gpio_dir;
1048         }
1049
1050         lp = dev->priv;
1051         lp->dev = dev;
1052         lp->part = part;
1053         lp->vers = version;
1054
1055         lp->spi = spi;
1056
1057         dev->parent = &spi->dev;
1058         dev->extra_tx_headroom = 0;
1059         dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK;
1060
1061         if (pdata->irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
1062                 irq_worker = at86rf230_irqwork;
1063                 irq_handler = at86rf230_isr;
1064         } else {
1065                 irq_worker = at86rf230_irqwork_level;
1066                 irq_handler = at86rf230_isr_level;
1067         }
1068
1069         mutex_init(&lp->bmux);
1070         INIT_WORK(&lp->irqwork, irq_worker);
1071         spin_lock_init(&lp->lock);
1072         init_completion(&lp->tx_complete);
1073
1074         spi_set_drvdata(spi, lp);
1075
1076         if (is_rf212(lp)) {
1077                 dev->phy->channels_supported[0] = 0x00007FF;
1078                 dev->phy->channels_supported[2] = 0x00007FF;
1079         } else {
1080                 dev->phy->channels_supported[0] = 0x7FFF800;
1081         }
1082
1083         rc = at86rf230_hw_init(lp);
1084         if (rc)
1085                 goto err_hw_init;
1086
1087         rc = request_irq(spi->irq, irq_handler,
1088                          IRQF_SHARED | pdata->irq_type,
1089                          dev_name(&spi->dev), lp);
1090         if (rc)
1091                 goto err_hw_init;
1092
1093         /* Read irq status register to reset irq line */
1094         rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1095         if (rc)
1096                 goto err_irq;
1097
1098         rc = ieee802154_register_device(lp->dev);
1099         if (rc)
1100                 goto err_irq;
1101
1102         return rc;
1103
1104 err_irq:
1105         free_irq(spi->irq, lp);
1106 err_hw_init:
1107         flush_work(&lp->irqwork);
1108         spi_set_drvdata(spi, NULL);
1109         mutex_destroy(&lp->bmux);
1110         ieee802154_free_device(lp->dev);
1111
1112 err_gpio_dir:
1113         if (gpio_is_valid(pdata->slp_tr))
1114                 gpio_free(pdata->slp_tr);
1115 err_slp_tr:
1116         gpio_free(pdata->rstn);
1117         return rc;
1118 }
1119
1120 static int at86rf230_remove(struct spi_device *spi)
1121 {
1122         struct at86rf230_local *lp = spi_get_drvdata(spi);
1123         struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1124
1125         ieee802154_unregister_device(lp->dev);
1126
1127         free_irq(spi->irq, lp);
1128         flush_work(&lp->irqwork);
1129
1130         if (gpio_is_valid(pdata->slp_tr))
1131                 gpio_free(pdata->slp_tr);
1132         gpio_free(pdata->rstn);
1133
1134         mutex_destroy(&lp->bmux);
1135         ieee802154_free_device(lp->dev);
1136
1137         dev_dbg(&spi->dev, "unregistered at86rf230\n");
1138         return 0;
1139 }
1140
1141 static struct spi_driver at86rf230_driver = {
1142         .driver = {
1143                 .name   = "at86rf230",
1144                 .owner  = THIS_MODULE,
1145         },
1146         .probe      = at86rf230_probe,
1147         .remove     = at86rf230_remove,
1148 };
1149
1150 module_spi_driver(at86rf230_driver);
1151
1152 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1153 MODULE_LICENSE("GPL v2");