GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / net / ieee802154 / cc2520.c
1 /* Driver for TI CC2520 802.15.4 Wireless-PAN Networking controller
2  *
3  * Copyright (C) 2014 Varka Bhadram <varkab@cdac.in>
4  *                    Md.Jamal Mohiuddin <mjmohiuddin@cdac.in>
5  *                    P Sowjanya <sowjanyap@cdac.in>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  */
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/gpio.h>
16 #include <linux/delay.h>
17 #include <linux/spi/spi.h>
18 #include <linux/spi/cc2520.h>
19 #include <linux/workqueue.h>
20 #include <linux/interrupt.h>
21 #include <linux/skbuff.h>
22 #include <linux/of_gpio.h>
23 #include <linux/ieee802154.h>
24 #include <linux/crc-ccitt.h>
25 #include <asm/unaligned.h>
26
27 #include <net/mac802154.h>
28 #include <net/cfg802154.h>
29
30 #define SPI_COMMAND_BUFFER      3
31 #define HIGH                    1
32 #define LOW                     0
33 #define STATE_IDLE              0
34 #define RSSI_VALID              0
35 #define RSSI_OFFSET             78
36
37 #define CC2520_RAM_SIZE         640
38 #define CC2520_FIFO_SIZE        128
39
40 #define CC2520RAM_TXFIFO        0x100
41 #define CC2520RAM_RXFIFO        0x180
42 #define CC2520RAM_IEEEADDR      0x3EA
43 #define CC2520RAM_PANID         0x3F2
44 #define CC2520RAM_SHORTADDR     0x3F4
45
46 #define CC2520_FREG_MASK        0x3F
47
48 /* status byte values */
49 #define CC2520_STATUS_XOSC32M_STABLE    BIT(7)
50 #define CC2520_STATUS_RSSI_VALID        BIT(6)
51 #define CC2520_STATUS_TX_UNDERFLOW      BIT(3)
52
53 /* IEEE-802.15.4 defined constants (2.4 GHz logical channels) */
54 #define CC2520_MINCHANNEL               11
55 #define CC2520_MAXCHANNEL               26
56 #define CC2520_CHANNEL_SPACING          5
57
58 /* command strobes */
59 #define CC2520_CMD_SNOP                 0x00
60 #define CC2520_CMD_IBUFLD               0x02
61 #define CC2520_CMD_SIBUFEX              0x03
62 #define CC2520_CMD_SSAMPLECCA           0x04
63 #define CC2520_CMD_SRES                 0x0f
64 #define CC2520_CMD_MEMORY_MASK          0x0f
65 #define CC2520_CMD_MEMORY_READ          0x10
66 #define CC2520_CMD_MEMORY_WRITE         0x20
67 #define CC2520_CMD_RXBUF                0x30
68 #define CC2520_CMD_RXBUFCP              0x38
69 #define CC2520_CMD_RXBUFMOV             0x32
70 #define CC2520_CMD_TXBUF                0x3A
71 #define CC2520_CMD_TXBUFCP              0x3E
72 #define CC2520_CMD_RANDOM               0x3C
73 #define CC2520_CMD_SXOSCON              0x40
74 #define CC2520_CMD_STXCAL               0x41
75 #define CC2520_CMD_SRXON                0x42
76 #define CC2520_CMD_STXON                0x43
77 #define CC2520_CMD_STXONCCA             0x44
78 #define CC2520_CMD_SRFOFF               0x45
79 #define CC2520_CMD_SXOSCOFF             0x46
80 #define CC2520_CMD_SFLUSHRX             0x47
81 #define CC2520_CMD_SFLUSHTX             0x48
82 #define CC2520_CMD_SACK                 0x49
83 #define CC2520_CMD_SACKPEND             0x4A
84 #define CC2520_CMD_SNACK                0x4B
85 #define CC2520_CMD_SRXMASKBITSET        0x4C
86 #define CC2520_CMD_SRXMASKBITCLR        0x4D
87 #define CC2520_CMD_RXMASKAND            0x4E
88 #define CC2520_CMD_RXMASKOR             0x4F
89 #define CC2520_CMD_MEMCP                0x50
90 #define CC2520_CMD_MEMCPR               0x52
91 #define CC2520_CMD_MEMXCP               0x54
92 #define CC2520_CMD_MEMXWR               0x56
93 #define CC2520_CMD_BCLR                 0x58
94 #define CC2520_CMD_BSET                 0x59
95 #define CC2520_CMD_CTR_UCTR             0x60
96 #define CC2520_CMD_CBCMAC               0x64
97 #define CC2520_CMD_UCBCMAC              0x66
98 #define CC2520_CMD_CCM                  0x68
99 #define CC2520_CMD_UCCM                 0x6A
100 #define CC2520_CMD_ECB                  0x70
101 #define CC2520_CMD_ECBO                 0x72
102 #define CC2520_CMD_ECBX                 0x74
103 #define CC2520_CMD_INC                  0x78
104 #define CC2520_CMD_ABORT                0x7F
105 #define CC2520_CMD_REGISTER_READ        0x80
106 #define CC2520_CMD_REGISTER_WRITE       0xC0
107
108 /* status registers */
109 #define CC2520_CHIPID                   0x40
110 #define CC2520_VERSION                  0x42
111 #define CC2520_EXTCLOCK                 0x44
112 #define CC2520_MDMCTRL0                 0x46
113 #define CC2520_MDMCTRL1                 0x47
114 #define CC2520_FREQEST                  0x48
115 #define CC2520_RXCTRL                   0x4A
116 #define CC2520_FSCTRL                   0x4C
117 #define CC2520_FSCAL0                   0x4E
118 #define CC2520_FSCAL1                   0x4F
119 #define CC2520_FSCAL2                   0x50
120 #define CC2520_FSCAL3                   0x51
121 #define CC2520_AGCCTRL0                 0x52
122 #define CC2520_AGCCTRL1                 0x53
123 #define CC2520_AGCCTRL2                 0x54
124 #define CC2520_AGCCTRL3                 0x55
125 #define CC2520_ADCTEST0                 0x56
126 #define CC2520_ADCTEST1                 0x57
127 #define CC2520_ADCTEST2                 0x58
128 #define CC2520_MDMTEST0                 0x5A
129 #define CC2520_MDMTEST1                 0x5B
130 #define CC2520_DACTEST0                 0x5C
131 #define CC2520_DACTEST1                 0x5D
132 #define CC2520_ATEST                    0x5E
133 #define CC2520_DACTEST2                 0x5F
134 #define CC2520_PTEST0                   0x60
135 #define CC2520_PTEST1                   0x61
136 #define CC2520_RESERVED                 0x62
137 #define CC2520_DPUBIST                  0x7A
138 #define CC2520_ACTBIST                  0x7C
139 #define CC2520_RAMBIST                  0x7E
140
141 /* frame registers */
142 #define CC2520_FRMFILT0                 0x00
143 #define CC2520_FRMFILT1                 0x01
144 #define CC2520_SRCMATCH                 0x02
145 #define CC2520_SRCSHORTEN0              0x04
146 #define CC2520_SRCSHORTEN1              0x05
147 #define CC2520_SRCSHORTEN2              0x06
148 #define CC2520_SRCEXTEN0                0x08
149 #define CC2520_SRCEXTEN1                0x09
150 #define CC2520_SRCEXTEN2                0x0A
151 #define CC2520_FRMCTRL0                 0x0C
152 #define CC2520_FRMCTRL1                 0x0D
153 #define CC2520_RXENABLE0                0x0E
154 #define CC2520_RXENABLE1                0x0F
155 #define CC2520_EXCFLAG0                 0x10
156 #define CC2520_EXCFLAG1                 0x11
157 #define CC2520_EXCFLAG2                 0x12
158 #define CC2520_EXCMASKA0                0x14
159 #define CC2520_EXCMASKA1                0x15
160 #define CC2520_EXCMASKA2                0x16
161 #define CC2520_EXCMASKB0                0x18
162 #define CC2520_EXCMASKB1                0x19
163 #define CC2520_EXCMASKB2                0x1A
164 #define CC2520_EXCBINDX0                0x1C
165 #define CC2520_EXCBINDX1                0x1D
166 #define CC2520_EXCBINDY0                0x1E
167 #define CC2520_EXCBINDY1                0x1F
168 #define CC2520_GPIOCTRL0                0x20
169 #define CC2520_GPIOCTRL1                0x21
170 #define CC2520_GPIOCTRL2                0x22
171 #define CC2520_GPIOCTRL3                0x23
172 #define CC2520_GPIOCTRL4                0x24
173 #define CC2520_GPIOCTRL5                0x25
174 #define CC2520_GPIOPOLARITY             0x26
175 #define CC2520_GPIOCTRL                 0x28
176 #define CC2520_DPUCON                   0x2A
177 #define CC2520_DPUSTAT                  0x2C
178 #define CC2520_FREQCTRL                 0x2E
179 #define CC2520_FREQTUNE                 0x2F
180 #define CC2520_TXPOWER                  0x30
181 #define CC2520_TXCTRL                   0x31
182 #define CC2520_FSMSTAT0                 0x32
183 #define CC2520_FSMSTAT1                 0x33
184 #define CC2520_FIFOPCTRL                0x34
185 #define CC2520_FSMCTRL                  0x35
186 #define CC2520_CCACTRL0                 0x36
187 #define CC2520_CCACTRL1                 0x37
188 #define CC2520_RSSI                     0x38
189 #define CC2520_RSSISTAT                 0x39
190 #define CC2520_RXFIRST                  0x3C
191 #define CC2520_RXFIFOCNT                0x3E
192 #define CC2520_TXFIFOCNT                0x3F
193
194 /* CC2520_FRMFILT0 */
195 #define FRMFILT0_FRAME_FILTER_EN        BIT(0)
196 #define FRMFILT0_PAN_COORDINATOR        BIT(1)
197
198 /* CC2520_FRMCTRL0 */
199 #define FRMCTRL0_AUTOACK                BIT(5)
200 #define FRMCTRL0_AUTOCRC                BIT(6)
201
202 /* CC2520_FRMCTRL1 */
203 #define FRMCTRL1_SET_RXENMASK_ON_TX     BIT(0)
204 #define FRMCTRL1_IGNORE_TX_UNDERF       BIT(1)
205
206 /* Driver private information */
207 struct cc2520_private {
208         struct spi_device *spi;         /* SPI device structure */
209         struct ieee802154_hw *hw;       /* IEEE-802.15.4 device */
210         u8 *buf;                        /* SPI TX/Rx data buffer */
211         struct mutex buffer_mutex;      /* SPI buffer mutex */
212         bool is_tx;                     /* Flag for sync b/w Tx and Rx */
213         bool amplified;                 /* Flag for CC2591 */
214         int fifo_pin;                   /* FIFO GPIO pin number */
215         struct work_struct fifop_irqwork;/* Workqueue for FIFOP */
216         spinlock_t lock;                /* Lock for is_tx*/
217         struct completion tx_complete;  /* Work completion for Tx */
218         bool promiscuous;               /* Flag for promiscuous mode */
219 };
220
221 /* Generic Functions */
222 static int
223 cc2520_cmd_strobe(struct cc2520_private *priv, u8 cmd)
224 {
225         int ret;
226         u8 status = 0xff;
227         struct spi_message msg;
228         struct spi_transfer xfer = {
229                 .len = 0,
230                 .tx_buf = priv->buf,
231                 .rx_buf = priv->buf,
232         };
233
234         spi_message_init(&msg);
235         spi_message_add_tail(&xfer, &msg);
236
237         mutex_lock(&priv->buffer_mutex);
238         priv->buf[xfer.len++] = cmd;
239         dev_vdbg(&priv->spi->dev,
240                  "command strobe buf[0] = %02x\n",
241                  priv->buf[0]);
242
243         ret = spi_sync(priv->spi, &msg);
244         if (!ret)
245                 status = priv->buf[0];
246         dev_vdbg(&priv->spi->dev,
247                  "buf[0] = %02x\n", priv->buf[0]);
248         mutex_unlock(&priv->buffer_mutex);
249
250         return ret;
251 }
252
253 static int
254 cc2520_get_status(struct cc2520_private *priv, u8 *status)
255 {
256         int ret;
257         struct spi_message msg;
258         struct spi_transfer xfer = {
259                 .len = 0,
260                 .tx_buf = priv->buf,
261                 .rx_buf = priv->buf,
262         };
263
264         spi_message_init(&msg);
265         spi_message_add_tail(&xfer, &msg);
266
267         mutex_lock(&priv->buffer_mutex);
268         priv->buf[xfer.len++] = CC2520_CMD_SNOP;
269         dev_vdbg(&priv->spi->dev,
270                  "get status command buf[0] = %02x\n", priv->buf[0]);
271
272         ret = spi_sync(priv->spi, &msg);
273         if (!ret)
274                 *status = priv->buf[0];
275         dev_vdbg(&priv->spi->dev,
276                  "buf[0] = %02x\n", priv->buf[0]);
277         mutex_unlock(&priv->buffer_mutex);
278
279         return ret;
280 }
281
282 static int
283 cc2520_write_register(struct cc2520_private *priv, u8 reg, u8 value)
284 {
285         int status;
286         struct spi_message msg;
287         struct spi_transfer xfer = {
288                 .len = 0,
289                 .tx_buf = priv->buf,
290                 .rx_buf = priv->buf,
291         };
292
293         spi_message_init(&msg);
294         spi_message_add_tail(&xfer, &msg);
295
296         mutex_lock(&priv->buffer_mutex);
297
298         if (reg <= CC2520_FREG_MASK) {
299                 priv->buf[xfer.len++] = CC2520_CMD_REGISTER_WRITE | reg;
300                 priv->buf[xfer.len++] = value;
301         } else {
302                 priv->buf[xfer.len++] = CC2520_CMD_MEMORY_WRITE;
303                 priv->buf[xfer.len++] = reg;
304                 priv->buf[xfer.len++] = value;
305         }
306         status = spi_sync(priv->spi, &msg);
307         if (msg.status)
308                 status = msg.status;
309
310         mutex_unlock(&priv->buffer_mutex);
311
312         return status;
313 }
314
315 static int
316 cc2520_write_ram(struct cc2520_private *priv, u16 reg, u8 len, u8 *data)
317 {
318         int status;
319         struct spi_message msg;
320         struct spi_transfer xfer_head = {
321                 .len        = 0,
322                 .tx_buf        = priv->buf,
323                 .rx_buf        = priv->buf,
324         };
325
326         struct spi_transfer xfer_buf = {
327                 .len = len,
328                 .tx_buf = data,
329         };
330
331         mutex_lock(&priv->buffer_mutex);
332         priv->buf[xfer_head.len++] = (CC2520_CMD_MEMORY_WRITE |
333                                                 ((reg >> 8) & 0xff));
334         priv->buf[xfer_head.len++] = reg & 0xff;
335
336         spi_message_init(&msg);
337         spi_message_add_tail(&xfer_head, &msg);
338         spi_message_add_tail(&xfer_buf, &msg);
339
340         status = spi_sync(priv->spi, &msg);
341         dev_dbg(&priv->spi->dev, "spi status = %d\n", status);
342         if (msg.status)
343                 status = msg.status;
344
345         mutex_unlock(&priv->buffer_mutex);
346         return status;
347 }
348
349 static int
350 cc2520_read_register(struct cc2520_private *priv, u8 reg, u8 *data)
351 {
352         int status;
353         struct spi_message msg;
354         struct spi_transfer xfer1 = {
355                 .len = 0,
356                 .tx_buf = priv->buf,
357                 .rx_buf = priv->buf,
358         };
359
360         struct spi_transfer xfer2 = {
361                 .len = 1,
362                 .rx_buf = data,
363         };
364
365         spi_message_init(&msg);
366         spi_message_add_tail(&xfer1, &msg);
367         spi_message_add_tail(&xfer2, &msg);
368
369         mutex_lock(&priv->buffer_mutex);
370         priv->buf[xfer1.len++] = CC2520_CMD_MEMORY_READ;
371         priv->buf[xfer1.len++] = reg;
372
373         status = spi_sync(priv->spi, &msg);
374         dev_dbg(&priv->spi->dev,
375                 "spi status = %d\n", status);
376         if (msg.status)
377                 status = msg.status;
378
379         mutex_unlock(&priv->buffer_mutex);
380
381         return status;
382 }
383
384 static int
385 cc2520_write_txfifo(struct cc2520_private *priv, u8 pkt_len, u8 *data, u8 len)
386 {
387         int status;
388
389         /* length byte must include FCS even
390          * if it is calculated in the hardware
391          */
392         int len_byte = pkt_len;
393
394         struct spi_message msg;
395
396         struct spi_transfer xfer_head = {
397                 .len = 0,
398                 .tx_buf = priv->buf,
399                 .rx_buf = priv->buf,
400         };
401         struct spi_transfer xfer_len = {
402                 .len = 1,
403                 .tx_buf = &len_byte,
404         };
405         struct spi_transfer xfer_buf = {
406                 .len = len,
407                 .tx_buf = data,
408         };
409
410         spi_message_init(&msg);
411         spi_message_add_tail(&xfer_head, &msg);
412         spi_message_add_tail(&xfer_len, &msg);
413         spi_message_add_tail(&xfer_buf, &msg);
414
415         mutex_lock(&priv->buffer_mutex);
416         priv->buf[xfer_head.len++] = CC2520_CMD_TXBUF;
417         dev_vdbg(&priv->spi->dev,
418                  "TX_FIFO cmd buf[0] = %02x\n", priv->buf[0]);
419
420         status = spi_sync(priv->spi, &msg);
421         dev_vdbg(&priv->spi->dev, "status = %d\n", status);
422         if (msg.status)
423                 status = msg.status;
424         dev_vdbg(&priv->spi->dev, "status = %d\n", status);
425         dev_vdbg(&priv->spi->dev, "buf[0] = %02x\n", priv->buf[0]);
426         mutex_unlock(&priv->buffer_mutex);
427
428         return status;
429 }
430
431 static int
432 cc2520_read_rxfifo(struct cc2520_private *priv, u8 *data, u8 len)
433 {
434         int status;
435         struct spi_message msg;
436
437         struct spi_transfer xfer_head = {
438                 .len = 0,
439                 .tx_buf = priv->buf,
440                 .rx_buf = priv->buf,
441         };
442         struct spi_transfer xfer_buf = {
443                 .len = len,
444                 .rx_buf = data,
445         };
446
447         spi_message_init(&msg);
448         spi_message_add_tail(&xfer_head, &msg);
449         spi_message_add_tail(&xfer_buf, &msg);
450
451         mutex_lock(&priv->buffer_mutex);
452         priv->buf[xfer_head.len++] = CC2520_CMD_RXBUF;
453
454         dev_vdbg(&priv->spi->dev, "read rxfifo buf[0] = %02x\n", priv->buf[0]);
455         dev_vdbg(&priv->spi->dev, "buf[1] = %02x\n", priv->buf[1]);
456
457         status = spi_sync(priv->spi, &msg);
458         dev_vdbg(&priv->spi->dev, "status = %d\n", status);
459         if (msg.status)
460                 status = msg.status;
461         dev_vdbg(&priv->spi->dev, "status = %d\n", status);
462         dev_vdbg(&priv->spi->dev,
463                  "return status buf[0] = %02x\n", priv->buf[0]);
464         dev_vdbg(&priv->spi->dev, "length buf[1] = %02x\n", priv->buf[1]);
465
466         mutex_unlock(&priv->buffer_mutex);
467
468         return status;
469 }
470
471 static int cc2520_start(struct ieee802154_hw *hw)
472 {
473         return cc2520_cmd_strobe(hw->priv, CC2520_CMD_SRXON);
474 }
475
476 static void cc2520_stop(struct ieee802154_hw *hw)
477 {
478         cc2520_cmd_strobe(hw->priv, CC2520_CMD_SRFOFF);
479 }
480
481 static int
482 cc2520_tx(struct ieee802154_hw *hw, struct sk_buff *skb)
483 {
484         struct cc2520_private *priv = hw->priv;
485         unsigned long flags;
486         int rc;
487         u8 status = 0;
488         u8 pkt_len;
489
490         /* In promiscuous mode we disable AUTOCRC so we can get the raw CRC
491          * values on RX. This means we need to manually add the CRC on TX.
492          */
493         if (priv->promiscuous) {
494                 u16 crc = crc_ccitt(0, skb->data, skb->len);
495
496                 put_unaligned_le16(crc, skb_put(skb, 2));
497                 pkt_len = skb->len;
498         } else {
499                 pkt_len = skb->len + 2;
500         }
501
502         rc = cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHTX);
503         if (rc)
504                 goto err_tx;
505
506         rc = cc2520_write_txfifo(priv, pkt_len, skb->data, skb->len);
507         if (rc)
508                 goto err_tx;
509
510         rc = cc2520_get_status(priv, &status);
511         if (rc)
512                 goto err_tx;
513
514         if (status & CC2520_STATUS_TX_UNDERFLOW) {
515                 rc = -EINVAL;
516                 dev_err(&priv->spi->dev, "cc2520 tx underflow exception\n");
517                 goto err_tx;
518         }
519
520         spin_lock_irqsave(&priv->lock, flags);
521         BUG_ON(priv->is_tx);
522         priv->is_tx = 1;
523         spin_unlock_irqrestore(&priv->lock, flags);
524
525         rc = cc2520_cmd_strobe(priv, CC2520_CMD_STXONCCA);
526         if (rc)
527                 goto err;
528
529         rc = wait_for_completion_interruptible(&priv->tx_complete);
530         if (rc < 0)
531                 goto err;
532
533         cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHTX);
534         cc2520_cmd_strobe(priv, CC2520_CMD_SRXON);
535
536         return rc;
537 err:
538         spin_lock_irqsave(&priv->lock, flags);
539         priv->is_tx = 0;
540         spin_unlock_irqrestore(&priv->lock, flags);
541 err_tx:
542         return rc;
543 }
544
545 static int cc2520_rx(struct cc2520_private *priv)
546 {
547         u8 len = 0, lqi = 0, bytes = 1;
548         struct sk_buff *skb;
549
550         /* Read single length byte from the radio. */
551         cc2520_read_rxfifo(priv, &len, bytes);
552
553         if (!ieee802154_is_valid_psdu_len(len)) {
554                 /* Corrupted frame received, clear frame buffer by
555                  * reading entire buffer.
556                  */
557                 dev_dbg(&priv->spi->dev, "corrupted frame received\n");
558                 len = IEEE802154_MTU;
559         }
560
561         skb = dev_alloc_skb(len);
562         if (!skb)
563                 return -ENOMEM;
564
565         if (cc2520_read_rxfifo(priv, skb_put(skb, len), len)) {
566                 dev_dbg(&priv->spi->dev, "frame reception failed\n");
567                 kfree_skb(skb);
568                 return -EINVAL;
569         }
570
571         /* In promiscuous mode, we configure the radio to include the
572          * CRC (AUTOCRC==0) and we pass on the packet unconditionally. If not
573          * in promiscuous mode, we check the CRC here, but leave the
574          * RSSI/LQI/CRC_OK bytes as they will get removed in the mac layer.
575          */
576         if (!priv->promiscuous) {
577                 bool crc_ok;
578
579                 /* Check if the CRC is valid. With AUTOCRC set, the most
580                  * significant bit of the last byte returned from the CC2520
581                  * is CRC_OK flag. See section 20.3.4 of the datasheet.
582                  */
583                 crc_ok = skb->data[len - 1] & BIT(7);
584
585                 /* If we failed CRC drop the packet in the driver layer. */
586                 if (!crc_ok) {
587                         dev_dbg(&priv->spi->dev, "CRC check failed\n");
588                         kfree_skb(skb);
589                         return -EINVAL;
590                 }
591
592                 /* To calculate LQI, the lower 7 bits of the last byte (the
593                  * correlation value provided by the radio) must be scaled to
594                  * the range 0-255. According to section 20.6, the correlation
595                  * value ranges from 50-110. Ideally this would be calibrated
596                  * per hardware design, but we use roughly the datasheet values
597                  * to get close enough while avoiding floating point.
598                  */
599                 lqi = skb->data[len - 1] & 0x7f;
600                 if (lqi < 50)
601                         lqi = 50;
602                 else if (lqi > 113)
603                         lqi = 113;
604                 lqi = (lqi - 50) * 4;
605         }
606
607         ieee802154_rx_irqsafe(priv->hw, skb, lqi);
608
609         dev_vdbg(&priv->spi->dev, "RXFIFO: %x %x\n", len, lqi);
610
611         return 0;
612 }
613
614 static int
615 cc2520_ed(struct ieee802154_hw *hw, u8 *level)
616 {
617         struct cc2520_private *priv = hw->priv;
618         u8 status = 0xff;
619         u8 rssi;
620         int ret;
621
622         ret = cc2520_read_register(priv, CC2520_RSSISTAT, &status);
623         if (ret)
624                 return ret;
625
626         if (status != RSSI_VALID)
627                 return -EINVAL;
628
629         ret = cc2520_read_register(priv, CC2520_RSSI, &rssi);
630         if (ret)
631                 return ret;
632
633         /* level = RSSI(rssi) - OFFSET [dBm] : offset is 76dBm */
634         *level = rssi - RSSI_OFFSET;
635
636         return 0;
637 }
638
639 static int
640 cc2520_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
641 {
642         struct cc2520_private *priv = hw->priv;
643         int ret;
644
645         dev_dbg(&priv->spi->dev, "trying to set channel\n");
646
647         BUG_ON(page != 0);
648         BUG_ON(channel < CC2520_MINCHANNEL);
649         BUG_ON(channel > CC2520_MAXCHANNEL);
650
651         ret = cc2520_write_register(priv, CC2520_FREQCTRL,
652                                     11 + 5*(channel - 11));
653
654         return ret;
655 }
656
657 static int
658 cc2520_filter(struct ieee802154_hw *hw,
659               struct ieee802154_hw_addr_filt *filt, unsigned long changed)
660 {
661         struct cc2520_private *priv = hw->priv;
662         int ret = 0;
663
664         if (changed & IEEE802154_AFILT_PANID_CHANGED) {
665                 u16 panid = le16_to_cpu(filt->pan_id);
666
667                 dev_vdbg(&priv->spi->dev,
668                          "cc2520_filter called for pan id\n");
669                 ret = cc2520_write_ram(priv, CC2520RAM_PANID,
670                                        sizeof(panid), (u8 *)&panid);
671         }
672
673         if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
674                 dev_vdbg(&priv->spi->dev,
675                          "cc2520_filter called for IEEE addr\n");
676                 ret = cc2520_write_ram(priv, CC2520RAM_IEEEADDR,
677                                        sizeof(filt->ieee_addr),
678                                        (u8 *)&filt->ieee_addr);
679         }
680
681         if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
682                 u16 addr = le16_to_cpu(filt->short_addr);
683
684                 dev_vdbg(&priv->spi->dev,
685                          "cc2520_filter called for saddr\n");
686                 ret = cc2520_write_ram(priv, CC2520RAM_SHORTADDR,
687                                        sizeof(addr), (u8 *)&addr);
688         }
689
690         if (changed & IEEE802154_AFILT_PANC_CHANGED) {
691                 u8 frmfilt0;
692
693                 dev_vdbg(&priv->spi->dev,
694                          "cc2520_filter called for panc change\n");
695
696                 cc2520_read_register(priv, CC2520_FRMFILT0, &frmfilt0);
697
698                 if (filt->pan_coord)
699                         frmfilt0 |= FRMFILT0_PAN_COORDINATOR;
700                 else
701                         frmfilt0 &= ~FRMFILT0_PAN_COORDINATOR;
702
703                 ret = cc2520_write_register(priv, CC2520_FRMFILT0, frmfilt0);
704         }
705
706         return ret;
707 }
708
709 static inline int cc2520_set_tx_power(struct cc2520_private *priv, s32 mbm)
710 {
711         u8 power;
712
713         switch (mbm) {
714         case 500:
715                 power = 0xF7;
716                 break;
717         case 300:
718                 power = 0xF2;
719                 break;
720         case 200:
721                 power = 0xAB;
722                 break;
723         case 100:
724                 power = 0x13;
725                 break;
726         case 0:
727                 power = 0x32;
728                 break;
729         case -200:
730                 power = 0x81;
731                 break;
732         case -400:
733                 power = 0x88;
734                 break;
735         case -700:
736                 power = 0x2C;
737                 break;
738         case -1800:
739                 power = 0x03;
740                 break;
741         default:
742                 return -EINVAL;
743         }
744
745         return cc2520_write_register(priv, CC2520_TXPOWER, power);
746 }
747
748 static inline int cc2520_cc2591_set_tx_power(struct cc2520_private *priv,
749                                              s32 mbm)
750 {
751         u8 power;
752
753         switch (mbm) {
754         case 1700:
755                 power = 0xF9;
756                 break;
757         case 1600:
758                 power = 0xF0;
759                 break;
760         case 1400:
761                 power = 0xA0;
762                 break;
763         case 1100:
764                 power = 0x2C;
765                 break;
766         case -100:
767                 power = 0x03;
768                 break;
769         case -800:
770                 power = 0x01;
771                 break;
772         default:
773                 return -EINVAL;
774         }
775
776         return cc2520_write_register(priv, CC2520_TXPOWER, power);
777 }
778
779 #define CC2520_MAX_TX_POWERS 0x8
780 static const s32 cc2520_powers[CC2520_MAX_TX_POWERS + 1] = {
781         500, 300, 200, 100, 0, -200, -400, -700, -1800,
782 };
783
784 #define CC2520_CC2591_MAX_TX_POWERS 0x5
785 static const s32 cc2520_cc2591_powers[CC2520_CC2591_MAX_TX_POWERS + 1] = {
786         1700, 1600, 1400, 1100, -100, -800,
787 };
788
789 static int
790 cc2520_set_txpower(struct ieee802154_hw *hw, s32 mbm)
791 {
792         struct cc2520_private *priv = hw->priv;
793
794         if (!priv->amplified)
795                 return cc2520_set_tx_power(priv, mbm);
796
797         return cc2520_cc2591_set_tx_power(priv, mbm);
798 }
799
800 static int
801 cc2520_set_promiscuous_mode(struct ieee802154_hw *hw, bool on)
802 {
803         struct cc2520_private *priv = hw->priv;
804         u8 frmfilt0;
805
806         dev_dbg(&priv->spi->dev, "%s : mode %d\n", __func__, on);
807
808         priv->promiscuous = on;
809
810         cc2520_read_register(priv, CC2520_FRMFILT0, &frmfilt0);
811
812         if (on) {
813                 /* Disable automatic ACK, automatic CRC, and frame filtering. */
814                 cc2520_write_register(priv, CC2520_FRMCTRL0, 0);
815                 frmfilt0 &= ~FRMFILT0_FRAME_FILTER_EN;
816         } else {
817                 cc2520_write_register(priv, CC2520_FRMCTRL0, FRMCTRL0_AUTOACK |
818                                                              FRMCTRL0_AUTOCRC);
819                 frmfilt0 |= FRMFILT0_FRAME_FILTER_EN;
820         }
821         return cc2520_write_register(priv, CC2520_FRMFILT0, frmfilt0);
822 }
823
824 static const struct ieee802154_ops cc2520_ops = {
825         .owner = THIS_MODULE,
826         .start = cc2520_start,
827         .stop = cc2520_stop,
828         .xmit_sync = cc2520_tx,
829         .ed = cc2520_ed,
830         .set_channel = cc2520_set_channel,
831         .set_hw_addr_filt = cc2520_filter,
832         .set_txpower = cc2520_set_txpower,
833         .set_promiscuous_mode = cc2520_set_promiscuous_mode,
834 };
835
836 static int cc2520_register(struct cc2520_private *priv)
837 {
838         int ret = -ENOMEM;
839
840         priv->hw = ieee802154_alloc_hw(sizeof(*priv), &cc2520_ops);
841         if (!priv->hw)
842                 goto err_ret;
843
844         priv->hw->priv = priv;
845         priv->hw->parent = &priv->spi->dev;
846         priv->hw->extra_tx_headroom = 0;
847         ieee802154_random_extended_addr(&priv->hw->phy->perm_extended_addr);
848
849         /* We do support only 2.4 Ghz */
850         priv->hw->phy->supported.channels[0] = 0x7FFF800;
851         priv->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
852                           IEEE802154_HW_PROMISCUOUS;
853
854         priv->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER;
855
856         if (!priv->amplified) {
857                 priv->hw->phy->supported.tx_powers = cc2520_powers;
858                 priv->hw->phy->supported.tx_powers_size = ARRAY_SIZE(cc2520_powers);
859                 priv->hw->phy->transmit_power = priv->hw->phy->supported.tx_powers[4];
860         } else {
861                 priv->hw->phy->supported.tx_powers = cc2520_cc2591_powers;
862                 priv->hw->phy->supported.tx_powers_size = ARRAY_SIZE(cc2520_cc2591_powers);
863                 priv->hw->phy->transmit_power = priv->hw->phy->supported.tx_powers[0];
864         }
865
866         priv->hw->phy->current_channel = 11;
867
868         dev_vdbg(&priv->spi->dev, "registered cc2520\n");
869         ret = ieee802154_register_hw(priv->hw);
870         if (ret)
871                 goto err_free_device;
872
873         return 0;
874
875 err_free_device:
876         ieee802154_free_hw(priv->hw);
877 err_ret:
878         return ret;
879 }
880
881 static void cc2520_fifop_irqwork(struct work_struct *work)
882 {
883         struct cc2520_private *priv
884                 = container_of(work, struct cc2520_private, fifop_irqwork);
885
886         dev_dbg(&priv->spi->dev, "fifop interrupt received\n");
887
888         if (gpio_get_value(priv->fifo_pin))
889                 cc2520_rx(priv);
890         else
891                 dev_dbg(&priv->spi->dev, "rxfifo overflow\n");
892
893         cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHRX);
894         cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHRX);
895 }
896
897 static irqreturn_t cc2520_fifop_isr(int irq, void *data)
898 {
899         struct cc2520_private *priv = data;
900
901         schedule_work(&priv->fifop_irqwork);
902
903         return IRQ_HANDLED;
904 }
905
906 static irqreturn_t cc2520_sfd_isr(int irq, void *data)
907 {
908         struct cc2520_private *priv = data;
909         unsigned long flags;
910
911         spin_lock_irqsave(&priv->lock, flags);
912         if (priv->is_tx) {
913                 priv->is_tx = 0;
914                 spin_unlock_irqrestore(&priv->lock, flags);
915                 dev_dbg(&priv->spi->dev, "SFD for TX\n");
916                 complete(&priv->tx_complete);
917         } else {
918                 spin_unlock_irqrestore(&priv->lock, flags);
919                 dev_dbg(&priv->spi->dev, "SFD for RX\n");
920         }
921
922         return IRQ_HANDLED;
923 }
924
925 static int cc2520_get_platform_data(struct spi_device *spi,
926                                     struct cc2520_platform_data *pdata)
927 {
928         struct device_node *np = spi->dev.of_node;
929         struct cc2520_private *priv = spi_get_drvdata(spi);
930
931         if (!np) {
932                 struct cc2520_platform_data *spi_pdata = spi->dev.platform_data;
933                 if (!spi_pdata)
934                         return -ENOENT;
935                 *pdata = *spi_pdata;
936                 priv->fifo_pin = pdata->fifo;
937                 return 0;
938         }
939
940         pdata->fifo = of_get_named_gpio(np, "fifo-gpio", 0);
941         priv->fifo_pin = pdata->fifo;
942
943         pdata->fifop = of_get_named_gpio(np, "fifop-gpio", 0);
944
945         pdata->sfd = of_get_named_gpio(np, "sfd-gpio", 0);
946         pdata->cca = of_get_named_gpio(np, "cca-gpio", 0);
947         pdata->vreg = of_get_named_gpio(np, "vreg-gpio", 0);
948         pdata->reset = of_get_named_gpio(np, "reset-gpio", 0);
949
950         /* CC2591 front end for CC2520 */
951         if (of_property_read_bool(np, "amplified"))
952                 priv->amplified = true;
953
954         return 0;
955 }
956
957 static int cc2520_hw_init(struct cc2520_private *priv)
958 {
959         u8 status = 0, state = 0xff;
960         int ret;
961         int timeout = 100;
962         struct cc2520_platform_data pdata;
963
964         ret = cc2520_get_platform_data(priv->spi, &pdata);
965         if (ret)
966                 goto err_ret;
967
968         ret = cc2520_read_register(priv, CC2520_FSMSTAT1, &state);
969         if (ret)
970                 goto err_ret;
971
972         if (state != STATE_IDLE)
973                 return -EINVAL;
974
975         do {
976                 ret = cc2520_get_status(priv, &status);
977                 if (ret)
978                         goto err_ret;
979
980                 if (timeout-- <= 0) {
981                         dev_err(&priv->spi->dev, "oscillator start failed!\n");
982                         return -ETIMEDOUT;
983                 }
984                 udelay(1);
985         } while (!(status & CC2520_STATUS_XOSC32M_STABLE));
986
987         dev_vdbg(&priv->spi->dev, "oscillator brought up\n");
988
989         /* If the CC2520 is connected to a CC2591 amplifier, we must both
990          * configure GPIOs on the CC2520 to correctly configure the CC2591
991          * and change a couple settings of the CC2520 to work with the
992          * amplifier. See section 8 page 17 of TI application note AN065.
993          * http://www.ti.com/lit/an/swra229a/swra229a.pdf
994          */
995         if (priv->amplified) {
996                 ret = cc2520_write_register(priv, CC2520_AGCCTRL1, 0x16);
997                 if (ret)
998                         goto err_ret;
999
1000                 ret = cc2520_write_register(priv, CC2520_GPIOCTRL0, 0x46);
1001                 if (ret)
1002                         goto err_ret;
1003
1004                 ret = cc2520_write_register(priv, CC2520_GPIOCTRL5, 0x47);
1005                 if (ret)
1006                         goto err_ret;
1007
1008                 ret = cc2520_write_register(priv, CC2520_GPIOPOLARITY, 0x1e);
1009                 if (ret)
1010                         goto err_ret;
1011
1012                 ret = cc2520_write_register(priv, CC2520_TXCTRL, 0xc1);
1013                 if (ret)
1014                         goto err_ret;
1015         } else {
1016                 ret = cc2520_write_register(priv, CC2520_AGCCTRL1, 0x11);
1017                 if (ret)
1018                         goto err_ret;
1019         }
1020
1021         /* Registers default value: section 28.1 in Datasheet */
1022
1023         /* Set the CCA threshold to -50 dBm. This seems to have been copied
1024          * from the TinyOS CC2520 driver and is much higher than the -84 dBm
1025          * threshold suggested in the datasheet.
1026          */
1027         ret = cc2520_write_register(priv, CC2520_CCACTRL0, 0x1A);
1028         if (ret)
1029                 goto err_ret;
1030
1031         ret = cc2520_write_register(priv, CC2520_MDMCTRL0, 0x85);
1032         if (ret)
1033                 goto err_ret;
1034
1035         ret = cc2520_write_register(priv, CC2520_MDMCTRL1, 0x14);
1036         if (ret)
1037                 goto err_ret;
1038
1039         ret = cc2520_write_register(priv, CC2520_RXCTRL, 0x3f);
1040         if (ret)
1041                 goto err_ret;
1042
1043         ret = cc2520_write_register(priv, CC2520_FSCTRL, 0x5a);
1044         if (ret)
1045                 goto err_ret;
1046
1047         ret = cc2520_write_register(priv, CC2520_FSCAL1, 0x2b);
1048         if (ret)
1049                 goto err_ret;
1050
1051         ret = cc2520_write_register(priv, CC2520_ADCTEST0, 0x10);
1052         if (ret)
1053                 goto err_ret;
1054
1055         ret = cc2520_write_register(priv, CC2520_ADCTEST1, 0x0e);
1056         if (ret)
1057                 goto err_ret;
1058
1059         ret = cc2520_write_register(priv, CC2520_ADCTEST2, 0x03);
1060         if (ret)
1061                 goto err_ret;
1062
1063         /* Configure registers correctly for this driver. */
1064         ret = cc2520_write_register(priv, CC2520_FRMCTRL1,
1065                                     FRMCTRL1_SET_RXENMASK_ON_TX |
1066                                     FRMCTRL1_IGNORE_TX_UNDERF);
1067         if (ret)
1068                 goto err_ret;
1069
1070         ret = cc2520_write_register(priv, CC2520_FIFOPCTRL, 127);
1071         if (ret)
1072                 goto err_ret;
1073
1074         return 0;
1075
1076 err_ret:
1077         return ret;
1078 }
1079
1080 static int cc2520_probe(struct spi_device *spi)
1081 {
1082         struct cc2520_private *priv;
1083         struct cc2520_platform_data pdata;
1084         int ret;
1085
1086         priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
1087         if (!priv)
1088                 return -ENOMEM;
1089
1090         spi_set_drvdata(spi, priv);
1091
1092         ret = cc2520_get_platform_data(spi, &pdata);
1093         if (ret < 0) {
1094                 dev_err(&spi->dev, "no platform data\n");
1095                 return -EINVAL;
1096         }
1097
1098         priv->spi = spi;
1099
1100         priv->buf = devm_kzalloc(&spi->dev,
1101                                  SPI_COMMAND_BUFFER, GFP_KERNEL);
1102         if (!priv->buf)
1103                 return -ENOMEM;
1104
1105         mutex_init(&priv->buffer_mutex);
1106         INIT_WORK(&priv->fifop_irqwork, cc2520_fifop_irqwork);
1107         spin_lock_init(&priv->lock);
1108         init_completion(&priv->tx_complete);
1109
1110         /* Assumption that CC2591 is not connected */
1111         priv->amplified = false;
1112
1113         /* Request all the gpio's */
1114         if (!gpio_is_valid(pdata.fifo)) {
1115                 dev_err(&spi->dev, "fifo gpio is not valid\n");
1116                 ret = -EINVAL;
1117                 goto err_hw_init;
1118         }
1119
1120         ret = devm_gpio_request_one(&spi->dev, pdata.fifo,
1121                                     GPIOF_IN, "fifo");
1122         if (ret)
1123                 goto err_hw_init;
1124
1125         if (!gpio_is_valid(pdata.cca)) {
1126                 dev_err(&spi->dev, "cca gpio is not valid\n");
1127                 ret = -EINVAL;
1128                 goto err_hw_init;
1129         }
1130
1131         ret = devm_gpio_request_one(&spi->dev, pdata.cca,
1132                                     GPIOF_IN, "cca");
1133         if (ret)
1134                 goto err_hw_init;
1135
1136         if (!gpio_is_valid(pdata.fifop)) {
1137                 dev_err(&spi->dev, "fifop gpio is not valid\n");
1138                 ret = -EINVAL;
1139                 goto err_hw_init;
1140         }
1141
1142         ret = devm_gpio_request_one(&spi->dev, pdata.fifop,
1143                                     GPIOF_IN, "fifop");
1144         if (ret)
1145                 goto err_hw_init;
1146
1147         if (!gpio_is_valid(pdata.sfd)) {
1148                 dev_err(&spi->dev, "sfd gpio is not valid\n");
1149                 ret = -EINVAL;
1150                 goto err_hw_init;
1151         }
1152
1153         ret = devm_gpio_request_one(&spi->dev, pdata.sfd,
1154                                     GPIOF_IN, "sfd");
1155         if (ret)
1156                 goto err_hw_init;
1157
1158         if (!gpio_is_valid(pdata.reset)) {
1159                 dev_err(&spi->dev, "reset gpio is not valid\n");
1160                 ret = -EINVAL;
1161                 goto err_hw_init;
1162         }
1163
1164         ret = devm_gpio_request_one(&spi->dev, pdata.reset,
1165                                     GPIOF_OUT_INIT_LOW, "reset");
1166         if (ret)
1167                 goto err_hw_init;
1168
1169         if (!gpio_is_valid(pdata.vreg)) {
1170                 dev_err(&spi->dev, "vreg gpio is not valid\n");
1171                 ret = -EINVAL;
1172                 goto err_hw_init;
1173         }
1174
1175         ret = devm_gpio_request_one(&spi->dev, pdata.vreg,
1176                                     GPIOF_OUT_INIT_LOW, "vreg");
1177         if (ret)
1178                 goto err_hw_init;
1179
1180         gpio_set_value(pdata.vreg, HIGH);
1181         usleep_range(100, 150);
1182
1183         gpio_set_value(pdata.reset, HIGH);
1184         usleep_range(200, 250);
1185
1186         ret = cc2520_hw_init(priv);
1187         if (ret)
1188                 goto err_hw_init;
1189
1190         /* Set up fifop interrupt */
1191         ret = devm_request_irq(&spi->dev,
1192                                gpio_to_irq(pdata.fifop),
1193                                cc2520_fifop_isr,
1194                                IRQF_TRIGGER_RISING,
1195                                dev_name(&spi->dev),
1196                                priv);
1197         if (ret) {
1198                 dev_err(&spi->dev, "could not get fifop irq\n");
1199                 goto err_hw_init;
1200         }
1201
1202         /* Set up sfd interrupt */
1203         ret = devm_request_irq(&spi->dev,
1204                                gpio_to_irq(pdata.sfd),
1205                                cc2520_sfd_isr,
1206                                IRQF_TRIGGER_FALLING,
1207                                dev_name(&spi->dev),
1208                                priv);
1209         if (ret) {
1210                 dev_err(&spi->dev, "could not get sfd irq\n");
1211                 goto err_hw_init;
1212         }
1213
1214         ret = cc2520_register(priv);
1215         if (ret)
1216                 goto err_hw_init;
1217
1218         return 0;
1219
1220 err_hw_init:
1221         mutex_destroy(&priv->buffer_mutex);
1222         flush_work(&priv->fifop_irqwork);
1223         return ret;
1224 }
1225
1226 static int cc2520_remove(struct spi_device *spi)
1227 {
1228         struct cc2520_private *priv = spi_get_drvdata(spi);
1229
1230         mutex_destroy(&priv->buffer_mutex);
1231         flush_work(&priv->fifop_irqwork);
1232
1233         ieee802154_unregister_hw(priv->hw);
1234         ieee802154_free_hw(priv->hw);
1235
1236         return 0;
1237 }
1238
1239 static const struct spi_device_id cc2520_ids[] = {
1240         {"cc2520", },
1241         {},
1242 };
1243 MODULE_DEVICE_TABLE(spi, cc2520_ids);
1244
1245 static const struct of_device_id cc2520_of_ids[] = {
1246         {.compatible = "ti,cc2520", },
1247         {},
1248 };
1249 MODULE_DEVICE_TABLE(of, cc2520_of_ids);
1250
1251 /* SPI driver structure */
1252 static struct spi_driver cc2520_driver = {
1253         .driver = {
1254                 .name = "cc2520",
1255                 .of_match_table = of_match_ptr(cc2520_of_ids),
1256         },
1257         .id_table = cc2520_ids,
1258         .probe = cc2520_probe,
1259         .remove = cc2520_remove,
1260 };
1261 module_spi_driver(cc2520_driver);
1262
1263 MODULE_AUTHOR("Varka Bhadram <varkab@cdac.in>");
1264 MODULE_DESCRIPTION("CC2520 Transceiver Driver");
1265 MODULE_LICENSE("GPL v2");