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