cc2520.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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 <net/mac802154.h>
  25. #include <net/cfg802154.h>
  26. #define SPI_COMMAND_BUFFER 3
  27. #define HIGH 1
  28. #define LOW 0
  29. #define STATE_IDLE 0
  30. #define RSSI_VALID 0
  31. #define RSSI_OFFSET 78
  32. #define CC2520_RAM_SIZE 640
  33. #define CC2520_FIFO_SIZE 128
  34. #define CC2520RAM_TXFIFO 0x100
  35. #define CC2520RAM_RXFIFO 0x180
  36. #define CC2520RAM_IEEEADDR 0x3EA
  37. #define CC2520RAM_PANID 0x3F2
  38. #define CC2520RAM_SHORTADDR 0x3F4
  39. #define CC2520_FREG_MASK 0x3F
  40. /* status byte values */
  41. #define CC2520_STATUS_XOSC32M_STABLE BIT(7)
  42. #define CC2520_STATUS_RSSI_VALID BIT(6)
  43. #define CC2520_STATUS_TX_UNDERFLOW BIT(3)
  44. /* IEEE-802.15.4 defined constants (2.4 GHz logical channels) */
  45. #define CC2520_MINCHANNEL 11
  46. #define CC2520_MAXCHANNEL 26
  47. #define CC2520_CHANNEL_SPACING 5
  48. /* command strobes */
  49. #define CC2520_CMD_SNOP 0x00
  50. #define CC2520_CMD_IBUFLD 0x02
  51. #define CC2520_CMD_SIBUFEX 0x03
  52. #define CC2520_CMD_SSAMPLECCA 0x04
  53. #define CC2520_CMD_SRES 0x0f
  54. #define CC2520_CMD_MEMORY_MASK 0x0f
  55. #define CC2520_CMD_MEMORY_READ 0x10
  56. #define CC2520_CMD_MEMORY_WRITE 0x20
  57. #define CC2520_CMD_RXBUF 0x30
  58. #define CC2520_CMD_RXBUFCP 0x38
  59. #define CC2520_CMD_RXBUFMOV 0x32
  60. #define CC2520_CMD_TXBUF 0x3A
  61. #define CC2520_CMD_TXBUFCP 0x3E
  62. #define CC2520_CMD_RANDOM 0x3C
  63. #define CC2520_CMD_SXOSCON 0x40
  64. #define CC2520_CMD_STXCAL 0x41
  65. #define CC2520_CMD_SRXON 0x42
  66. #define CC2520_CMD_STXON 0x43
  67. #define CC2520_CMD_STXONCCA 0x44
  68. #define CC2520_CMD_SRFOFF 0x45
  69. #define CC2520_CMD_SXOSCOFF 0x46
  70. #define CC2520_CMD_SFLUSHRX 0x47
  71. #define CC2520_CMD_SFLUSHTX 0x48
  72. #define CC2520_CMD_SACK 0x49
  73. #define CC2520_CMD_SACKPEND 0x4A
  74. #define CC2520_CMD_SNACK 0x4B
  75. #define CC2520_CMD_SRXMASKBITSET 0x4C
  76. #define CC2520_CMD_SRXMASKBITCLR 0x4D
  77. #define CC2520_CMD_RXMASKAND 0x4E
  78. #define CC2520_CMD_RXMASKOR 0x4F
  79. #define CC2520_CMD_MEMCP 0x50
  80. #define CC2520_CMD_MEMCPR 0x52
  81. #define CC2520_CMD_MEMXCP 0x54
  82. #define CC2520_CMD_MEMXWR 0x56
  83. #define CC2520_CMD_BCLR 0x58
  84. #define CC2520_CMD_BSET 0x59
  85. #define CC2520_CMD_CTR_UCTR 0x60
  86. #define CC2520_CMD_CBCMAC 0x64
  87. #define CC2520_CMD_UCBCMAC 0x66
  88. #define CC2520_CMD_CCM 0x68
  89. #define CC2520_CMD_UCCM 0x6A
  90. #define CC2520_CMD_ECB 0x70
  91. #define CC2520_CMD_ECBO 0x72
  92. #define CC2520_CMD_ECBX 0x74
  93. #define CC2520_CMD_INC 0x78
  94. #define CC2520_CMD_ABORT 0x7F
  95. #define CC2520_CMD_REGISTER_READ 0x80
  96. #define CC2520_CMD_REGISTER_WRITE 0xC0
  97. /* status registers */
  98. #define CC2520_CHIPID 0x40
  99. #define CC2520_VERSION 0x42
  100. #define CC2520_EXTCLOCK 0x44
  101. #define CC2520_MDMCTRL0 0x46
  102. #define CC2520_MDMCTRL1 0x47
  103. #define CC2520_FREQEST 0x48
  104. #define CC2520_RXCTRL 0x4A
  105. #define CC2520_FSCTRL 0x4C
  106. #define CC2520_FSCAL0 0x4E
  107. #define CC2520_FSCAL1 0x4F
  108. #define CC2520_FSCAL2 0x50
  109. #define CC2520_FSCAL3 0x51
  110. #define CC2520_AGCCTRL0 0x52
  111. #define CC2520_AGCCTRL1 0x53
  112. #define CC2520_AGCCTRL2 0x54
  113. #define CC2520_AGCCTRL3 0x55
  114. #define CC2520_ADCTEST0 0x56
  115. #define CC2520_ADCTEST1 0x57
  116. #define CC2520_ADCTEST2 0x58
  117. #define CC2520_MDMTEST0 0x5A
  118. #define CC2520_MDMTEST1 0x5B
  119. #define CC2520_DACTEST0 0x5C
  120. #define CC2520_DACTEST1 0x5D
  121. #define CC2520_ATEST 0x5E
  122. #define CC2520_DACTEST2 0x5F
  123. #define CC2520_PTEST0 0x60
  124. #define CC2520_PTEST1 0x61
  125. #define CC2520_RESERVED 0x62
  126. #define CC2520_DPUBIST 0x7A
  127. #define CC2520_ACTBIST 0x7C
  128. #define CC2520_RAMBIST 0x7E
  129. /* frame registers */
  130. #define CC2520_FRMFILT0 0x00
  131. #define CC2520_FRMFILT1 0x01
  132. #define CC2520_SRCMATCH 0x02
  133. #define CC2520_SRCSHORTEN0 0x04
  134. #define CC2520_SRCSHORTEN1 0x05
  135. #define CC2520_SRCSHORTEN2 0x06
  136. #define CC2520_SRCEXTEN0 0x08
  137. #define CC2520_SRCEXTEN1 0x09
  138. #define CC2520_SRCEXTEN2 0x0A
  139. #define CC2520_FRMCTRL0 0x0C
  140. #define CC2520_FRMCTRL1 0x0D
  141. #define CC2520_RXENABLE0 0x0E
  142. #define CC2520_RXENABLE1 0x0F
  143. #define CC2520_EXCFLAG0 0x10
  144. #define CC2520_EXCFLAG1 0x11
  145. #define CC2520_EXCFLAG2 0x12
  146. #define CC2520_EXCMASKA0 0x14
  147. #define CC2520_EXCMASKA1 0x15
  148. #define CC2520_EXCMASKA2 0x16
  149. #define CC2520_EXCMASKB0 0x18
  150. #define CC2520_EXCMASKB1 0x19
  151. #define CC2520_EXCMASKB2 0x1A
  152. #define CC2520_EXCBINDX0 0x1C
  153. #define CC2520_EXCBINDX1 0x1D
  154. #define CC2520_EXCBINDY0 0x1E
  155. #define CC2520_EXCBINDY1 0x1F
  156. #define CC2520_GPIOCTRL0 0x20
  157. #define CC2520_GPIOCTRL1 0x21
  158. #define CC2520_GPIOCTRL2 0x22
  159. #define CC2520_GPIOCTRL3 0x23
  160. #define CC2520_GPIOCTRL4 0x24
  161. #define CC2520_GPIOCTRL5 0x25
  162. #define CC2520_GPIOPOLARITY 0x26
  163. #define CC2520_GPIOCTRL 0x28
  164. #define CC2520_DPUCON 0x2A
  165. #define CC2520_DPUSTAT 0x2C
  166. #define CC2520_FREQCTRL 0x2E
  167. #define CC2520_FREQTUNE 0x2F
  168. #define CC2520_TXPOWER 0x30
  169. #define CC2520_TXCTRL 0x31
  170. #define CC2520_FSMSTAT0 0x32
  171. #define CC2520_FSMSTAT1 0x33
  172. #define CC2520_FIFOPCTRL 0x34
  173. #define CC2520_FSMCTRL 0x35
  174. #define CC2520_CCACTRL0 0x36
  175. #define CC2520_CCACTRL1 0x37
  176. #define CC2520_RSSI 0x38
  177. #define CC2520_RSSISTAT 0x39
  178. #define CC2520_RXFIRST 0x3C
  179. #define CC2520_RXFIFOCNT 0x3E
  180. #define CC2520_TXFIFOCNT 0x3F
  181. /* Driver private information */
  182. struct cc2520_private {
  183. struct spi_device *spi; /* SPI device structure */
  184. struct ieee802154_hw *hw; /* IEEE-802.15.4 device */
  185. u8 *buf; /* SPI TX/Rx data buffer */
  186. struct mutex buffer_mutex; /* SPI buffer mutex */
  187. bool is_tx; /* Flag for sync b/w Tx and Rx */
  188. bool amplified; /* Flag for CC2591 */
  189. int fifo_pin; /* FIFO GPIO pin number */
  190. struct work_struct fifop_irqwork;/* Workqueue for FIFOP */
  191. spinlock_t lock; /* Lock for is_tx*/
  192. struct completion tx_complete; /* Work completion for Tx */
  193. };
  194. /* Generic Functions */
  195. static int
  196. cc2520_cmd_strobe(struct cc2520_private *priv, u8 cmd)
  197. {
  198. int ret;
  199. u8 status = 0xff;
  200. struct spi_message msg;
  201. struct spi_transfer xfer = {
  202. .len = 0,
  203. .tx_buf = priv->buf,
  204. .rx_buf = priv->buf,
  205. };
  206. spi_message_init(&msg);
  207. spi_message_add_tail(&xfer, &msg);
  208. mutex_lock(&priv->buffer_mutex);
  209. priv->buf[xfer.len++] = cmd;
  210. dev_vdbg(&priv->spi->dev,
  211. "command strobe buf[0] = %02x\n",
  212. priv->buf[0]);
  213. ret = spi_sync(priv->spi, &msg);
  214. if (!ret)
  215. status = priv->buf[0];
  216. dev_vdbg(&priv->spi->dev,
  217. "buf[0] = %02x\n", priv->buf[0]);
  218. mutex_unlock(&priv->buffer_mutex);
  219. return ret;
  220. }
  221. static int
  222. cc2520_get_status(struct cc2520_private *priv, u8 *status)
  223. {
  224. int ret;
  225. struct spi_message msg;
  226. struct spi_transfer xfer = {
  227. .len = 0,
  228. .tx_buf = priv->buf,
  229. .rx_buf = priv->buf,
  230. };
  231. spi_message_init(&msg);
  232. spi_message_add_tail(&xfer, &msg);
  233. mutex_lock(&priv->buffer_mutex);
  234. priv->buf[xfer.len++] = CC2520_CMD_SNOP;
  235. dev_vdbg(&priv->spi->dev,
  236. "get status command buf[0] = %02x\n", priv->buf[0]);
  237. ret = spi_sync(priv->spi, &msg);
  238. if (!ret)
  239. *status = priv->buf[0];
  240. dev_vdbg(&priv->spi->dev,
  241. "buf[0] = %02x\n", priv->buf[0]);
  242. mutex_unlock(&priv->buffer_mutex);
  243. return ret;
  244. }
  245. static int
  246. cc2520_write_register(struct cc2520_private *priv, u8 reg, u8 value)
  247. {
  248. int status;
  249. struct spi_message msg;
  250. struct spi_transfer xfer = {
  251. .len = 0,
  252. .tx_buf = priv->buf,
  253. .rx_buf = priv->buf,
  254. };
  255. spi_message_init(&msg);
  256. spi_message_add_tail(&xfer, &msg);
  257. mutex_lock(&priv->buffer_mutex);
  258. if (reg <= CC2520_FREG_MASK) {
  259. priv->buf[xfer.len++] = CC2520_CMD_REGISTER_WRITE | reg;
  260. priv->buf[xfer.len++] = value;
  261. } else {
  262. priv->buf[xfer.len++] = CC2520_CMD_MEMORY_WRITE;
  263. priv->buf[xfer.len++] = reg;
  264. priv->buf[xfer.len++] = value;
  265. }
  266. status = spi_sync(priv->spi, &msg);
  267. if (msg.status)
  268. status = msg.status;
  269. mutex_unlock(&priv->buffer_mutex);
  270. return status;
  271. }
  272. static int
  273. cc2520_write_ram(struct cc2520_private *priv, u16 reg, u8 len, u8 *data)
  274. {
  275. int status;
  276. struct spi_message msg;
  277. struct spi_transfer xfer_head = {
  278. .len = 0,
  279. .tx_buf = priv->buf,
  280. .rx_buf = priv->buf,
  281. };
  282. struct spi_transfer xfer_buf = {
  283. .len = len,
  284. .tx_buf = data,
  285. };
  286. mutex_lock(&priv->buffer_mutex);
  287. priv->buf[xfer_head.len++] = (CC2520_CMD_MEMORY_WRITE |
  288. ((reg >> 8) & 0xff));
  289. priv->buf[xfer_head.len++] = reg & 0xff;
  290. spi_message_init(&msg);
  291. spi_message_add_tail(&xfer_head, &msg);
  292. spi_message_add_tail(&xfer_buf, &msg);
  293. status = spi_sync(priv->spi, &msg);
  294. dev_dbg(&priv->spi->dev, "spi status = %d\n", status);
  295. if (msg.status)
  296. status = msg.status;
  297. mutex_unlock(&priv->buffer_mutex);
  298. return status;
  299. }
  300. static int
  301. cc2520_read_register(struct cc2520_private *priv, u8 reg, u8 *data)
  302. {
  303. int status;
  304. struct spi_message msg;
  305. struct spi_transfer xfer1 = {
  306. .len = 0,
  307. .tx_buf = priv->buf,
  308. .rx_buf = priv->buf,
  309. };
  310. struct spi_transfer xfer2 = {
  311. .len = 1,
  312. .rx_buf = data,
  313. };
  314. spi_message_init(&msg);
  315. spi_message_add_tail(&xfer1, &msg);
  316. spi_message_add_tail(&xfer2, &msg);
  317. mutex_lock(&priv->buffer_mutex);
  318. priv->buf[xfer1.len++] = CC2520_CMD_MEMORY_READ;
  319. priv->buf[xfer1.len++] = reg;
  320. status = spi_sync(priv->spi, &msg);
  321. dev_dbg(&priv->spi->dev,
  322. "spi status = %d\n", status);
  323. if (msg.status)
  324. status = msg.status;
  325. mutex_unlock(&priv->buffer_mutex);
  326. return status;
  327. }
  328. static int
  329. cc2520_write_txfifo(struct cc2520_private *priv, u8 *data, u8 len)
  330. {
  331. int status;
  332. /* length byte must include FCS even
  333. * if it is calculated in the hardware
  334. */
  335. int len_byte = len + 2;
  336. struct spi_message msg;
  337. struct spi_transfer xfer_head = {
  338. .len = 0,
  339. .tx_buf = priv->buf,
  340. .rx_buf = priv->buf,
  341. };
  342. struct spi_transfer xfer_len = {
  343. .len = 1,
  344. .tx_buf = &len_byte,
  345. };
  346. struct spi_transfer xfer_buf = {
  347. .len = len,
  348. .tx_buf = data,
  349. };
  350. spi_message_init(&msg);
  351. spi_message_add_tail(&xfer_head, &msg);
  352. spi_message_add_tail(&xfer_len, &msg);
  353. spi_message_add_tail(&xfer_buf, &msg);
  354. mutex_lock(&priv->buffer_mutex);
  355. priv->buf[xfer_head.len++] = CC2520_CMD_TXBUF;
  356. dev_vdbg(&priv->spi->dev,
  357. "TX_FIFO cmd buf[0] = %02x\n", priv->buf[0]);
  358. status = spi_sync(priv->spi, &msg);
  359. dev_vdbg(&priv->spi->dev, "status = %d\n", status);
  360. if (msg.status)
  361. status = msg.status;
  362. dev_vdbg(&priv->spi->dev, "status = %d\n", status);
  363. dev_vdbg(&priv->spi->dev, "buf[0] = %02x\n", priv->buf[0]);
  364. mutex_unlock(&priv->buffer_mutex);
  365. return status;
  366. }
  367. static int
  368. cc2520_read_rxfifo(struct cc2520_private *priv, u8 *data, u8 len, u8 *lqi)
  369. {
  370. int status;
  371. struct spi_message msg;
  372. struct spi_transfer xfer_head = {
  373. .len = 0,
  374. .tx_buf = priv->buf,
  375. .rx_buf = priv->buf,
  376. };
  377. struct spi_transfer xfer_buf = {
  378. .len = len,
  379. .rx_buf = data,
  380. };
  381. spi_message_init(&msg);
  382. spi_message_add_tail(&xfer_head, &msg);
  383. spi_message_add_tail(&xfer_buf, &msg);
  384. mutex_lock(&priv->buffer_mutex);
  385. priv->buf[xfer_head.len++] = CC2520_CMD_RXBUF;
  386. dev_vdbg(&priv->spi->dev, "read rxfifo buf[0] = %02x\n", priv->buf[0]);
  387. dev_vdbg(&priv->spi->dev, "buf[1] = %02x\n", priv->buf[1]);
  388. status = spi_sync(priv->spi, &msg);
  389. dev_vdbg(&priv->spi->dev, "status = %d\n", status);
  390. if (msg.status)
  391. status = msg.status;
  392. dev_vdbg(&priv->spi->dev, "status = %d\n", status);
  393. dev_vdbg(&priv->spi->dev,
  394. "return status buf[0] = %02x\n", priv->buf[0]);
  395. dev_vdbg(&priv->spi->dev, "length buf[1] = %02x\n", priv->buf[1]);
  396. mutex_unlock(&priv->buffer_mutex);
  397. return status;
  398. }
  399. static int cc2520_start(struct ieee802154_hw *hw)
  400. {
  401. return cc2520_cmd_strobe(hw->priv, CC2520_CMD_SRXON);
  402. }
  403. static void cc2520_stop(struct ieee802154_hw *hw)
  404. {
  405. cc2520_cmd_strobe(hw->priv, CC2520_CMD_SRFOFF);
  406. }
  407. static int
  408. cc2520_tx(struct ieee802154_hw *hw, struct sk_buff *skb)
  409. {
  410. struct cc2520_private *priv = hw->priv;
  411. unsigned long flags;
  412. int rc;
  413. u8 status = 0;
  414. rc = cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHTX);
  415. if (rc)
  416. goto err_tx;
  417. rc = cc2520_write_txfifo(priv, skb->data, skb->len);
  418. if (rc)
  419. goto err_tx;
  420. rc = cc2520_get_status(priv, &status);
  421. if (rc)
  422. goto err_tx;
  423. if (status & CC2520_STATUS_TX_UNDERFLOW) {
  424. dev_err(&priv->spi->dev, "cc2520 tx underflow exception\n");
  425. goto err_tx;
  426. }
  427. spin_lock_irqsave(&priv->lock, flags);
  428. BUG_ON(priv->is_tx);
  429. priv->is_tx = 1;
  430. spin_unlock_irqrestore(&priv->lock, flags);
  431. rc = cc2520_cmd_strobe(priv, CC2520_CMD_STXONCCA);
  432. if (rc)
  433. goto err;
  434. rc = wait_for_completion_interruptible(&priv->tx_complete);
  435. if (rc < 0)
  436. goto err;
  437. cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHTX);
  438. cc2520_cmd_strobe(priv, CC2520_CMD_SRXON);
  439. return rc;
  440. err:
  441. spin_lock_irqsave(&priv->lock, flags);
  442. priv->is_tx = 0;
  443. spin_unlock_irqrestore(&priv->lock, flags);
  444. err_tx:
  445. return rc;
  446. }
  447. static int cc2520_rx(struct cc2520_private *priv)
  448. {
  449. u8 len = 0, lqi = 0, bytes = 1;
  450. struct sk_buff *skb;
  451. cc2520_read_rxfifo(priv, &len, bytes, &lqi);
  452. if (len < 2 || len > IEEE802154_MTU)
  453. return -EINVAL;
  454. skb = dev_alloc_skb(len);
  455. if (!skb)
  456. return -ENOMEM;
  457. if (cc2520_read_rxfifo(priv, skb_put(skb, len), len, &lqi)) {
  458. dev_dbg(&priv->spi->dev, "frame reception failed\n");
  459. kfree_skb(skb);
  460. return -EINVAL;
  461. }
  462. skb_trim(skb, skb->len - 2);
  463. ieee802154_rx_irqsafe(priv->hw, skb, lqi);
  464. dev_vdbg(&priv->spi->dev, "RXFIFO: %x %x\n", len, lqi);
  465. return 0;
  466. }
  467. static int
  468. cc2520_ed(struct ieee802154_hw *hw, u8 *level)
  469. {
  470. struct cc2520_private *priv = hw->priv;
  471. u8 status = 0xff;
  472. u8 rssi;
  473. int ret;
  474. ret = cc2520_read_register(priv, CC2520_RSSISTAT, &status);
  475. if (ret)
  476. return ret;
  477. if (status != RSSI_VALID)
  478. return -EINVAL;
  479. ret = cc2520_read_register(priv, CC2520_RSSI, &rssi);
  480. if (ret)
  481. return ret;
  482. /* level = RSSI(rssi) - OFFSET [dBm] : offset is 76dBm */
  483. *level = rssi - RSSI_OFFSET;
  484. return 0;
  485. }
  486. static int
  487. cc2520_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  488. {
  489. struct cc2520_private *priv = hw->priv;
  490. int ret;
  491. dev_dbg(&priv->spi->dev, "trying to set channel\n");
  492. BUG_ON(page != 0);
  493. BUG_ON(channel < CC2520_MINCHANNEL);
  494. BUG_ON(channel > CC2520_MAXCHANNEL);
  495. ret = cc2520_write_register(priv, CC2520_FREQCTRL,
  496. 11 + 5*(channel - 11));
  497. return ret;
  498. }
  499. static int
  500. cc2520_filter(struct ieee802154_hw *hw,
  501. struct ieee802154_hw_addr_filt *filt, unsigned long changed)
  502. {
  503. struct cc2520_private *priv = hw->priv;
  504. int ret = 0;
  505. if (changed & IEEE802154_AFILT_PANID_CHANGED) {
  506. u16 panid = le16_to_cpu(filt->pan_id);
  507. dev_vdbg(&priv->spi->dev,
  508. "cc2520_filter called for pan id\n");
  509. ret = cc2520_write_ram(priv, CC2520RAM_PANID,
  510. sizeof(panid), (u8 *)&panid);
  511. }
  512. if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
  513. dev_vdbg(&priv->spi->dev,
  514. "cc2520_filter called for IEEE addr\n");
  515. ret = cc2520_write_ram(priv, CC2520RAM_IEEEADDR,
  516. sizeof(filt->ieee_addr),
  517. (u8 *)&filt->ieee_addr);
  518. }
  519. if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
  520. u16 addr = le16_to_cpu(filt->short_addr);
  521. dev_vdbg(&priv->spi->dev,
  522. "cc2520_filter called for saddr\n");
  523. ret = cc2520_write_ram(priv, CC2520RAM_SHORTADDR,
  524. sizeof(addr), (u8 *)&addr);
  525. }
  526. if (changed & IEEE802154_AFILT_PANC_CHANGED) {
  527. dev_vdbg(&priv->spi->dev,
  528. "cc2520_filter called for panc change\n");
  529. if (filt->pan_coord)
  530. ret = cc2520_write_register(priv, CC2520_FRMFILT0,
  531. 0x02);
  532. else
  533. ret = cc2520_write_register(priv, CC2520_FRMFILT0,
  534. 0x00);
  535. }
  536. return ret;
  537. }
  538. static inline int cc2520_set_tx_power(struct cc2520_private *priv, s32 mbm)
  539. {
  540. u8 power;
  541. switch (mbm) {
  542. case 500:
  543. power = 0xF7;
  544. break;
  545. case 300:
  546. power = 0xF2;
  547. break;
  548. case 200:
  549. power = 0xAB;
  550. break;
  551. case 100:
  552. power = 0x13;
  553. break;
  554. case 0:
  555. power = 0x32;
  556. break;
  557. case -200:
  558. power = 0x81;
  559. break;
  560. case -400:
  561. power = 0x88;
  562. break;
  563. case -700:
  564. power = 0x2C;
  565. break;
  566. case -1800:
  567. power = 0x03;
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. return cc2520_write_register(priv, CC2520_TXPOWER, power);
  573. }
  574. static inline int cc2520_cc2591_set_tx_power(struct cc2520_private *priv,
  575. s32 mbm)
  576. {
  577. u8 power;
  578. switch (mbm) {
  579. case 1700:
  580. power = 0xF9;
  581. break;
  582. case 1600:
  583. power = 0xF0;
  584. break;
  585. case 1400:
  586. power = 0xA0;
  587. break;
  588. case 1100:
  589. power = 0x2C;
  590. break;
  591. case -100:
  592. power = 0x03;
  593. break;
  594. case -800:
  595. power = 0x01;
  596. break;
  597. default:
  598. return -EINVAL;
  599. }
  600. return cc2520_write_register(priv, CC2520_TXPOWER, power);
  601. }
  602. #define CC2520_MAX_TX_POWERS 0x8
  603. static const s32 cc2520_powers[CC2520_MAX_TX_POWERS + 1] = {
  604. 500, 300, 200, 100, 0, -200, -400, -700, -1800,
  605. };
  606. #define CC2520_CC2591_MAX_TX_POWERS 0x5
  607. static const s32 cc2520_cc2591_powers[CC2520_CC2591_MAX_TX_POWERS + 1] = {
  608. 1700, 1600, 1400, 1100, -100, -800,
  609. };
  610. static int
  611. cc2520_set_txpower(struct ieee802154_hw *hw, s32 mbm)
  612. {
  613. struct cc2520_private *priv = hw->priv;
  614. if (!priv->amplified)
  615. return cc2520_set_tx_power(priv, mbm);
  616. return cc2520_cc2591_set_tx_power(priv, mbm);
  617. }
  618. static const struct ieee802154_ops cc2520_ops = {
  619. .owner = THIS_MODULE,
  620. .start = cc2520_start,
  621. .stop = cc2520_stop,
  622. .xmit_sync = cc2520_tx,
  623. .ed = cc2520_ed,
  624. .set_channel = cc2520_set_channel,
  625. .set_hw_addr_filt = cc2520_filter,
  626. .set_txpower = cc2520_set_txpower,
  627. };
  628. static int cc2520_register(struct cc2520_private *priv)
  629. {
  630. int ret = -ENOMEM;
  631. priv->hw = ieee802154_alloc_hw(sizeof(*priv), &cc2520_ops);
  632. if (!priv->hw)
  633. goto err_ret;
  634. priv->hw->priv = priv;
  635. priv->hw->parent = &priv->spi->dev;
  636. priv->hw->extra_tx_headroom = 0;
  637. ieee802154_random_extended_addr(&priv->hw->phy->perm_extended_addr);
  638. /* We do support only 2.4 Ghz */
  639. priv->hw->phy->supported.channels[0] = 0x7FFF800;
  640. priv->hw->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AFILT;
  641. priv->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER;
  642. if (!priv->amplified) {
  643. priv->hw->phy->supported.tx_powers = cc2520_powers;
  644. priv->hw->phy->supported.tx_powers_size = ARRAY_SIZE(cc2520_powers);
  645. priv->hw->phy->transmit_power = priv->hw->phy->supported.tx_powers[4];
  646. } else {
  647. priv->hw->phy->supported.tx_powers = cc2520_cc2591_powers;
  648. priv->hw->phy->supported.tx_powers_size = ARRAY_SIZE(cc2520_cc2591_powers);
  649. priv->hw->phy->transmit_power = priv->hw->phy->supported.tx_powers[0];
  650. }
  651. priv->hw->phy->current_channel = 11;
  652. dev_vdbg(&priv->spi->dev, "registered cc2520\n");
  653. ret = ieee802154_register_hw(priv->hw);
  654. if (ret)
  655. goto err_free_device;
  656. return 0;
  657. err_free_device:
  658. ieee802154_free_hw(priv->hw);
  659. err_ret:
  660. return ret;
  661. }
  662. static void cc2520_fifop_irqwork(struct work_struct *work)
  663. {
  664. struct cc2520_private *priv
  665. = container_of(work, struct cc2520_private, fifop_irqwork);
  666. dev_dbg(&priv->spi->dev, "fifop interrupt received\n");
  667. if (gpio_get_value(priv->fifo_pin))
  668. cc2520_rx(priv);
  669. else
  670. dev_dbg(&priv->spi->dev, "rxfifo overflow\n");
  671. cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHRX);
  672. cc2520_cmd_strobe(priv, CC2520_CMD_SFLUSHRX);
  673. }
  674. static irqreturn_t cc2520_fifop_isr(int irq, void *data)
  675. {
  676. struct cc2520_private *priv = data;
  677. schedule_work(&priv->fifop_irqwork);
  678. return IRQ_HANDLED;
  679. }
  680. static irqreturn_t cc2520_sfd_isr(int irq, void *data)
  681. {
  682. struct cc2520_private *priv = data;
  683. unsigned long flags;
  684. spin_lock_irqsave(&priv->lock, flags);
  685. if (priv->is_tx) {
  686. priv->is_tx = 0;
  687. spin_unlock_irqrestore(&priv->lock, flags);
  688. dev_dbg(&priv->spi->dev, "SFD for TX\n");
  689. complete(&priv->tx_complete);
  690. } else {
  691. spin_unlock_irqrestore(&priv->lock, flags);
  692. dev_dbg(&priv->spi->dev, "SFD for RX\n");
  693. }
  694. return IRQ_HANDLED;
  695. }
  696. static int cc2520_get_platform_data(struct spi_device *spi,
  697. struct cc2520_platform_data *pdata)
  698. {
  699. struct device_node *np = spi->dev.of_node;
  700. struct cc2520_private *priv = spi_get_drvdata(spi);
  701. if (!np) {
  702. struct cc2520_platform_data *spi_pdata = spi->dev.platform_data;
  703. if (!spi_pdata)
  704. return -ENOENT;
  705. *pdata = *spi_pdata;
  706. return 0;
  707. }
  708. pdata->fifo = of_get_named_gpio(np, "fifo-gpio", 0);
  709. priv->fifo_pin = pdata->fifo;
  710. pdata->fifop = of_get_named_gpio(np, "fifop-gpio", 0);
  711. pdata->sfd = of_get_named_gpio(np, "sfd-gpio", 0);
  712. pdata->cca = of_get_named_gpio(np, "cca-gpio", 0);
  713. pdata->vreg = of_get_named_gpio(np, "vreg-gpio", 0);
  714. pdata->reset = of_get_named_gpio(np, "reset-gpio", 0);
  715. /* CC2591 front end for CC2520 */
  716. if (of_property_read_bool(np, "amplified"))
  717. priv->amplified = true;
  718. return 0;
  719. }
  720. static int cc2520_hw_init(struct cc2520_private *priv)
  721. {
  722. u8 status = 0, state = 0xff;
  723. int ret;
  724. int timeout = 100;
  725. struct cc2520_platform_data pdata;
  726. ret = cc2520_get_platform_data(priv->spi, &pdata);
  727. if (ret)
  728. goto err_ret;
  729. ret = cc2520_read_register(priv, CC2520_FSMSTAT1, &state);
  730. if (ret)
  731. goto err_ret;
  732. if (state != STATE_IDLE)
  733. return -EINVAL;
  734. do {
  735. ret = cc2520_get_status(priv, &status);
  736. if (ret)
  737. goto err_ret;
  738. if (timeout-- <= 0) {
  739. dev_err(&priv->spi->dev, "oscillator start failed!\n");
  740. return ret;
  741. }
  742. udelay(1);
  743. } while (!(status & CC2520_STATUS_XOSC32M_STABLE));
  744. dev_vdbg(&priv->spi->dev, "oscillator brought up\n");
  745. /* If the CC2520 is connected to a CC2591 amplifier, we must both
  746. * configure GPIOs on the CC2520 to correctly configure the CC2591
  747. * and change a couple settings of the CC2520 to work with the
  748. * amplifier. See section 8 page 17 of TI application note AN065.
  749. * http://www.ti.com/lit/an/swra229a/swra229a.pdf
  750. */
  751. if (priv->amplified) {
  752. ret = cc2520_write_register(priv, CC2520_AGCCTRL1, 0x16);
  753. if (ret)
  754. goto err_ret;
  755. ret = cc2520_write_register(priv, CC2520_GPIOCTRL0, 0x46);
  756. if (ret)
  757. goto err_ret;
  758. ret = cc2520_write_register(priv, CC2520_GPIOCTRL5, 0x47);
  759. if (ret)
  760. goto err_ret;
  761. ret = cc2520_write_register(priv, CC2520_GPIOPOLARITY, 0x1e);
  762. if (ret)
  763. goto err_ret;
  764. ret = cc2520_write_register(priv, CC2520_TXCTRL, 0xc1);
  765. if (ret)
  766. goto err_ret;
  767. } else {
  768. ret = cc2520_write_register(priv, CC2520_AGCCTRL1, 0x11);
  769. if (ret)
  770. goto err_ret;
  771. }
  772. /* Registers default value: section 28.1 in Datasheet */
  773. ret = cc2520_write_register(priv, CC2520_CCACTRL0, 0x1A);
  774. if (ret)
  775. goto err_ret;
  776. ret = cc2520_write_register(priv, CC2520_MDMCTRL0, 0x85);
  777. if (ret)
  778. goto err_ret;
  779. ret = cc2520_write_register(priv, CC2520_MDMCTRL1, 0x14);
  780. if (ret)
  781. goto err_ret;
  782. ret = cc2520_write_register(priv, CC2520_RXCTRL, 0x3f);
  783. if (ret)
  784. goto err_ret;
  785. ret = cc2520_write_register(priv, CC2520_FSCTRL, 0x5a);
  786. if (ret)
  787. goto err_ret;
  788. ret = cc2520_write_register(priv, CC2520_FSCAL1, 0x2b);
  789. if (ret)
  790. goto err_ret;
  791. ret = cc2520_write_register(priv, CC2520_ADCTEST0, 0x10);
  792. if (ret)
  793. goto err_ret;
  794. ret = cc2520_write_register(priv, CC2520_ADCTEST1, 0x0e);
  795. if (ret)
  796. goto err_ret;
  797. ret = cc2520_write_register(priv, CC2520_ADCTEST2, 0x03);
  798. if (ret)
  799. goto err_ret;
  800. ret = cc2520_write_register(priv, CC2520_FRMCTRL0, 0x60);
  801. if (ret)
  802. goto err_ret;
  803. ret = cc2520_write_register(priv, CC2520_FRMCTRL1, 0x03);
  804. if (ret)
  805. goto err_ret;
  806. ret = cc2520_write_register(priv, CC2520_FRMFILT0, 0x00);
  807. if (ret)
  808. goto err_ret;
  809. ret = cc2520_write_register(priv, CC2520_FIFOPCTRL, 127);
  810. if (ret)
  811. goto err_ret;
  812. return 0;
  813. err_ret:
  814. return ret;
  815. }
  816. static int cc2520_probe(struct spi_device *spi)
  817. {
  818. struct cc2520_private *priv;
  819. struct cc2520_platform_data pdata;
  820. int ret;
  821. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  822. if (!priv)
  823. return -ENOMEM;
  824. spi_set_drvdata(spi, priv);
  825. ret = cc2520_get_platform_data(spi, &pdata);
  826. if (ret < 0) {
  827. dev_err(&spi->dev, "no platform data\n");
  828. return -EINVAL;
  829. }
  830. priv->spi = spi;
  831. priv->buf = devm_kzalloc(&spi->dev,
  832. SPI_COMMAND_BUFFER, GFP_KERNEL);
  833. if (!priv->buf)
  834. return -ENOMEM;
  835. mutex_init(&priv->buffer_mutex);
  836. INIT_WORK(&priv->fifop_irqwork, cc2520_fifop_irqwork);
  837. spin_lock_init(&priv->lock);
  838. init_completion(&priv->tx_complete);
  839. /* Assumption that CC2591 is not connected */
  840. priv->amplified = false;
  841. /* Request all the gpio's */
  842. if (!gpio_is_valid(pdata.fifo)) {
  843. dev_err(&spi->dev, "fifo gpio is not valid\n");
  844. ret = -EINVAL;
  845. goto err_hw_init;
  846. }
  847. ret = devm_gpio_request_one(&spi->dev, pdata.fifo,
  848. GPIOF_IN, "fifo");
  849. if (ret)
  850. goto err_hw_init;
  851. if (!gpio_is_valid(pdata.cca)) {
  852. dev_err(&spi->dev, "cca gpio is not valid\n");
  853. ret = -EINVAL;
  854. goto err_hw_init;
  855. }
  856. ret = devm_gpio_request_one(&spi->dev, pdata.cca,
  857. GPIOF_IN, "cca");
  858. if (ret)
  859. goto err_hw_init;
  860. if (!gpio_is_valid(pdata.fifop)) {
  861. dev_err(&spi->dev, "fifop gpio is not valid\n");
  862. ret = -EINVAL;
  863. goto err_hw_init;
  864. }
  865. ret = devm_gpio_request_one(&spi->dev, pdata.fifop,
  866. GPIOF_IN, "fifop");
  867. if (ret)
  868. goto err_hw_init;
  869. if (!gpio_is_valid(pdata.sfd)) {
  870. dev_err(&spi->dev, "sfd gpio is not valid\n");
  871. ret = -EINVAL;
  872. goto err_hw_init;
  873. }
  874. ret = devm_gpio_request_one(&spi->dev, pdata.sfd,
  875. GPIOF_IN, "sfd");
  876. if (ret)
  877. goto err_hw_init;
  878. if (!gpio_is_valid(pdata.reset)) {
  879. dev_err(&spi->dev, "reset gpio is not valid\n");
  880. ret = -EINVAL;
  881. goto err_hw_init;
  882. }
  883. ret = devm_gpio_request_one(&spi->dev, pdata.reset,
  884. GPIOF_OUT_INIT_LOW, "reset");
  885. if (ret)
  886. goto err_hw_init;
  887. if (!gpio_is_valid(pdata.vreg)) {
  888. dev_err(&spi->dev, "vreg gpio is not valid\n");
  889. ret = -EINVAL;
  890. goto err_hw_init;
  891. }
  892. ret = devm_gpio_request_one(&spi->dev, pdata.vreg,
  893. GPIOF_OUT_INIT_LOW, "vreg");
  894. if (ret)
  895. goto err_hw_init;
  896. gpio_set_value(pdata.vreg, HIGH);
  897. usleep_range(100, 150);
  898. gpio_set_value(pdata.reset, HIGH);
  899. usleep_range(200, 250);
  900. ret = cc2520_hw_init(priv);
  901. if (ret)
  902. goto err_hw_init;
  903. /* Set up fifop interrupt */
  904. ret = devm_request_irq(&spi->dev,
  905. gpio_to_irq(pdata.fifop),
  906. cc2520_fifop_isr,
  907. IRQF_TRIGGER_RISING,
  908. dev_name(&spi->dev),
  909. priv);
  910. if (ret) {
  911. dev_err(&spi->dev, "could not get fifop irq\n");
  912. goto err_hw_init;
  913. }
  914. /* Set up sfd interrupt */
  915. ret = devm_request_irq(&spi->dev,
  916. gpio_to_irq(pdata.sfd),
  917. cc2520_sfd_isr,
  918. IRQF_TRIGGER_FALLING,
  919. dev_name(&spi->dev),
  920. priv);
  921. if (ret) {
  922. dev_err(&spi->dev, "could not get sfd irq\n");
  923. goto err_hw_init;
  924. }
  925. ret = cc2520_register(priv);
  926. if (ret)
  927. goto err_hw_init;
  928. return 0;
  929. err_hw_init:
  930. mutex_destroy(&priv->buffer_mutex);
  931. flush_work(&priv->fifop_irqwork);
  932. return ret;
  933. }
  934. static int cc2520_remove(struct spi_device *spi)
  935. {
  936. struct cc2520_private *priv = spi_get_drvdata(spi);
  937. mutex_destroy(&priv->buffer_mutex);
  938. flush_work(&priv->fifop_irqwork);
  939. ieee802154_unregister_hw(priv->hw);
  940. ieee802154_free_hw(priv->hw);
  941. return 0;
  942. }
  943. static const struct spi_device_id cc2520_ids[] = {
  944. {"cc2520", },
  945. {},
  946. };
  947. MODULE_DEVICE_TABLE(spi, cc2520_ids);
  948. static const struct of_device_id cc2520_of_ids[] = {
  949. {.compatible = "ti,cc2520", },
  950. {},
  951. };
  952. MODULE_DEVICE_TABLE(of, cc2520_of_ids);
  953. /* SPI driver structure */
  954. static struct spi_driver cc2520_driver = {
  955. .driver = {
  956. .name = "cc2520",
  957. .bus = &spi_bus_type,
  958. .owner = THIS_MODULE,
  959. .of_match_table = of_match_ptr(cc2520_of_ids),
  960. },
  961. .id_table = cc2520_ids,
  962. .probe = cc2520_probe,
  963. .remove = cc2520_remove,
  964. };
  965. module_spi_driver(cc2520_driver);
  966. MODULE_AUTHOR("Varka Bhadram <varkab@cdac.in>");
  967. MODULE_DESCRIPTION("CC2520 Transceiver Driver");
  968. MODULE_LICENSE("GPL v2");