at86rf230.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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. * Written by:
  16. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. * Alexander Aring <aar@pengutronix.de>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/gpio.h>
  27. #include <linux/delay.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/at86rf230.h>
  30. #include <linux/regmap.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/ieee802154.h>
  34. #include <linux/debugfs.h>
  35. #include <net/mac802154.h>
  36. #include <net/cfg802154.h>
  37. #include "at86rf230.h"
  38. struct at86rf230_local;
  39. /* at86rf2xx chip depend data.
  40. * All timings are in us.
  41. */
  42. struct at86rf2xx_chip_data {
  43. u16 t_sleep_cycle;
  44. u16 t_channel_switch;
  45. u16 t_reset_to_off;
  46. u16 t_off_to_aack;
  47. u16 t_off_to_tx_on;
  48. u16 t_off_to_sleep;
  49. u16 t_sleep_to_off;
  50. u16 t_frame;
  51. u16 t_p_ack;
  52. int rssi_base_val;
  53. int (*set_channel)(struct at86rf230_local *, u8, u8);
  54. int (*set_txpower)(struct at86rf230_local *, s32);
  55. };
  56. #define AT86RF2XX_MAX_BUF (127 + 3)
  57. /* tx retries to access the TX_ON state
  58. * if it's above then force change will be started.
  59. *
  60. * We assume the max_frame_retries (7) value of 802.15.4 here.
  61. */
  62. #define AT86RF2XX_MAX_TX_RETRIES 7
  63. /* We use the recommended 5 minutes timeout to recalibrate */
  64. #define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
  65. struct at86rf230_state_change {
  66. struct at86rf230_local *lp;
  67. int irq;
  68. struct hrtimer timer;
  69. struct spi_message msg;
  70. struct spi_transfer trx;
  71. u8 buf[AT86RF2XX_MAX_BUF];
  72. void (*complete)(void *context);
  73. u8 from_state;
  74. u8 to_state;
  75. bool free;
  76. };
  77. struct at86rf230_trac {
  78. u64 success;
  79. u64 success_data_pending;
  80. u64 success_wait_for_ack;
  81. u64 channel_access_failure;
  82. u64 no_ack;
  83. u64 invalid;
  84. };
  85. struct at86rf230_local {
  86. struct spi_device *spi;
  87. struct ieee802154_hw *hw;
  88. struct at86rf2xx_chip_data *data;
  89. struct regmap *regmap;
  90. int slp_tr;
  91. bool sleep;
  92. struct completion state_complete;
  93. struct at86rf230_state_change state;
  94. unsigned long cal_timeout;
  95. bool is_tx;
  96. bool is_tx_from_off;
  97. u8 tx_retry;
  98. struct sk_buff *tx_skb;
  99. struct at86rf230_state_change tx;
  100. struct at86rf230_trac trac;
  101. };
  102. #define AT86RF2XX_NUMREGS 0x3F
  103. static void
  104. at86rf230_async_state_change(struct at86rf230_local *lp,
  105. struct at86rf230_state_change *ctx,
  106. const u8 state, void (*complete)(void *context));
  107. static inline void
  108. at86rf230_sleep(struct at86rf230_local *lp)
  109. {
  110. if (gpio_is_valid(lp->slp_tr)) {
  111. gpio_set_value(lp->slp_tr, 1);
  112. usleep_range(lp->data->t_off_to_sleep,
  113. lp->data->t_off_to_sleep + 10);
  114. lp->sleep = true;
  115. }
  116. }
  117. static inline void
  118. at86rf230_awake(struct at86rf230_local *lp)
  119. {
  120. if (gpio_is_valid(lp->slp_tr)) {
  121. gpio_set_value(lp->slp_tr, 0);
  122. usleep_range(lp->data->t_sleep_to_off,
  123. lp->data->t_sleep_to_off + 100);
  124. lp->sleep = false;
  125. }
  126. }
  127. static inline int
  128. __at86rf230_write(struct at86rf230_local *lp,
  129. unsigned int addr, unsigned int data)
  130. {
  131. bool sleep = lp->sleep;
  132. int ret;
  133. /* awake for register setting if sleep */
  134. if (sleep)
  135. at86rf230_awake(lp);
  136. ret = regmap_write(lp->regmap, addr, data);
  137. /* sleep again if was sleeping */
  138. if (sleep)
  139. at86rf230_sleep(lp);
  140. return ret;
  141. }
  142. static inline int
  143. __at86rf230_read(struct at86rf230_local *lp,
  144. unsigned int addr, unsigned int *data)
  145. {
  146. bool sleep = lp->sleep;
  147. int ret;
  148. /* awake for register setting if sleep */
  149. if (sleep)
  150. at86rf230_awake(lp);
  151. ret = regmap_read(lp->regmap, addr, data);
  152. /* sleep again if was sleeping */
  153. if (sleep)
  154. at86rf230_sleep(lp);
  155. return ret;
  156. }
  157. static inline int
  158. at86rf230_read_subreg(struct at86rf230_local *lp,
  159. unsigned int addr, unsigned int mask,
  160. unsigned int shift, unsigned int *data)
  161. {
  162. int rc;
  163. rc = __at86rf230_read(lp, addr, data);
  164. if (!rc)
  165. *data = (*data & mask) >> shift;
  166. return rc;
  167. }
  168. static inline int
  169. at86rf230_write_subreg(struct at86rf230_local *lp,
  170. unsigned int addr, unsigned int mask,
  171. unsigned int shift, unsigned int data)
  172. {
  173. bool sleep = lp->sleep;
  174. int ret;
  175. /* awake for register setting if sleep */
  176. if (sleep)
  177. at86rf230_awake(lp);
  178. ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
  179. /* sleep again if was sleeping */
  180. if (sleep)
  181. at86rf230_sleep(lp);
  182. return ret;
  183. }
  184. static inline void
  185. at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
  186. {
  187. gpio_set_value(lp->slp_tr, 1);
  188. udelay(1);
  189. gpio_set_value(lp->slp_tr, 0);
  190. }
  191. static bool
  192. at86rf230_reg_writeable(struct device *dev, unsigned int reg)
  193. {
  194. switch (reg) {
  195. case RG_TRX_STATE:
  196. case RG_TRX_CTRL_0:
  197. case RG_TRX_CTRL_1:
  198. case RG_PHY_TX_PWR:
  199. case RG_PHY_ED_LEVEL:
  200. case RG_PHY_CC_CCA:
  201. case RG_CCA_THRES:
  202. case RG_RX_CTRL:
  203. case RG_SFD_VALUE:
  204. case RG_TRX_CTRL_2:
  205. case RG_ANT_DIV:
  206. case RG_IRQ_MASK:
  207. case RG_VREG_CTRL:
  208. case RG_BATMON:
  209. case RG_XOSC_CTRL:
  210. case RG_RX_SYN:
  211. case RG_XAH_CTRL_1:
  212. case RG_FTN_CTRL:
  213. case RG_PLL_CF:
  214. case RG_PLL_DCU:
  215. case RG_SHORT_ADDR_0:
  216. case RG_SHORT_ADDR_1:
  217. case RG_PAN_ID_0:
  218. case RG_PAN_ID_1:
  219. case RG_IEEE_ADDR_0:
  220. case RG_IEEE_ADDR_1:
  221. case RG_IEEE_ADDR_2:
  222. case RG_IEEE_ADDR_3:
  223. case RG_IEEE_ADDR_4:
  224. case RG_IEEE_ADDR_5:
  225. case RG_IEEE_ADDR_6:
  226. case RG_IEEE_ADDR_7:
  227. case RG_XAH_CTRL_0:
  228. case RG_CSMA_SEED_0:
  229. case RG_CSMA_SEED_1:
  230. case RG_CSMA_BE:
  231. return true;
  232. default:
  233. return false;
  234. }
  235. }
  236. static bool
  237. at86rf230_reg_readable(struct device *dev, unsigned int reg)
  238. {
  239. bool rc;
  240. /* all writeable are also readable */
  241. rc = at86rf230_reg_writeable(dev, reg);
  242. if (rc)
  243. return rc;
  244. /* readonly regs */
  245. switch (reg) {
  246. case RG_TRX_STATUS:
  247. case RG_PHY_RSSI:
  248. case RG_IRQ_STATUS:
  249. case RG_PART_NUM:
  250. case RG_VERSION_NUM:
  251. case RG_MAN_ID_1:
  252. case RG_MAN_ID_0:
  253. return true;
  254. default:
  255. return false;
  256. }
  257. }
  258. static bool
  259. at86rf230_reg_volatile(struct device *dev, unsigned int reg)
  260. {
  261. /* can be changed during runtime */
  262. switch (reg) {
  263. case RG_TRX_STATUS:
  264. case RG_TRX_STATE:
  265. case RG_PHY_RSSI:
  266. case RG_PHY_ED_LEVEL:
  267. case RG_IRQ_STATUS:
  268. case RG_VREG_CTRL:
  269. case RG_PLL_CF:
  270. case RG_PLL_DCU:
  271. return true;
  272. default:
  273. return false;
  274. }
  275. }
  276. static bool
  277. at86rf230_reg_precious(struct device *dev, unsigned int reg)
  278. {
  279. /* don't clear irq line on read */
  280. switch (reg) {
  281. case RG_IRQ_STATUS:
  282. return true;
  283. default:
  284. return false;
  285. }
  286. }
  287. static const struct regmap_config at86rf230_regmap_spi_config = {
  288. .reg_bits = 8,
  289. .val_bits = 8,
  290. .write_flag_mask = CMD_REG | CMD_WRITE,
  291. .read_flag_mask = CMD_REG,
  292. .cache_type = REGCACHE_RBTREE,
  293. .max_register = AT86RF2XX_NUMREGS,
  294. .writeable_reg = at86rf230_reg_writeable,
  295. .readable_reg = at86rf230_reg_readable,
  296. .volatile_reg = at86rf230_reg_volatile,
  297. .precious_reg = at86rf230_reg_precious,
  298. };
  299. static void
  300. at86rf230_async_error_recover_complete(void *context)
  301. {
  302. struct at86rf230_state_change *ctx = context;
  303. struct at86rf230_local *lp = ctx->lp;
  304. if (ctx->free)
  305. kfree(ctx);
  306. ieee802154_wake_queue(lp->hw);
  307. }
  308. static void
  309. at86rf230_async_error_recover(void *context)
  310. {
  311. struct at86rf230_state_change *ctx = context;
  312. struct at86rf230_local *lp = ctx->lp;
  313. lp->is_tx = 0;
  314. at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
  315. at86rf230_async_error_recover_complete);
  316. }
  317. static inline void
  318. at86rf230_async_error(struct at86rf230_local *lp,
  319. struct at86rf230_state_change *ctx, int rc)
  320. {
  321. dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
  322. at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
  323. at86rf230_async_error_recover);
  324. }
  325. /* Generic function to get some register value in async mode */
  326. static void
  327. at86rf230_async_read_reg(struct at86rf230_local *lp, u8 reg,
  328. struct at86rf230_state_change *ctx,
  329. void (*complete)(void *context))
  330. {
  331. int rc;
  332. u8 *tx_buf = ctx->buf;
  333. tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
  334. ctx->msg.complete = complete;
  335. rc = spi_async(lp->spi, &ctx->msg);
  336. if (rc)
  337. at86rf230_async_error(lp, ctx, rc);
  338. }
  339. static void
  340. at86rf230_async_write_reg(struct at86rf230_local *lp, u8 reg, u8 val,
  341. struct at86rf230_state_change *ctx,
  342. void (*complete)(void *context))
  343. {
  344. int rc;
  345. ctx->buf[0] = (reg & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  346. ctx->buf[1] = val;
  347. ctx->msg.complete = complete;
  348. rc = spi_async(lp->spi, &ctx->msg);
  349. if (rc)
  350. at86rf230_async_error(lp, ctx, rc);
  351. }
  352. static void
  353. at86rf230_async_state_assert(void *context)
  354. {
  355. struct at86rf230_state_change *ctx = context;
  356. struct at86rf230_local *lp = ctx->lp;
  357. const u8 *buf = ctx->buf;
  358. const u8 trx_state = buf[1] & TRX_STATE_MASK;
  359. /* Assert state change */
  360. if (trx_state != ctx->to_state) {
  361. /* Special handling if transceiver state is in
  362. * STATE_BUSY_RX_AACK and a SHR was detected.
  363. */
  364. if (trx_state == STATE_BUSY_RX_AACK) {
  365. /* Undocumented race condition. If we send a state
  366. * change to STATE_RX_AACK_ON the transceiver could
  367. * change his state automatically to STATE_BUSY_RX_AACK
  368. * if a SHR was detected. This is not an error, but we
  369. * can't assert this.
  370. */
  371. if (ctx->to_state == STATE_RX_AACK_ON)
  372. goto done;
  373. /* If we change to STATE_TX_ON without forcing and
  374. * transceiver state is STATE_BUSY_RX_AACK, we wait
  375. * 'tFrame + tPAck' receiving time. In this time the
  376. * PDU should be received. If the transceiver is still
  377. * in STATE_BUSY_RX_AACK, we run a force state change
  378. * to STATE_TX_ON. This is a timeout handling, if the
  379. * transceiver stucks in STATE_BUSY_RX_AACK.
  380. *
  381. * Additional we do several retries to try to get into
  382. * TX_ON state without forcing. If the retries are
  383. * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
  384. * will do a force change.
  385. */
  386. if (ctx->to_state == STATE_TX_ON ||
  387. ctx->to_state == STATE_TRX_OFF) {
  388. u8 state = ctx->to_state;
  389. if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
  390. state = STATE_FORCE_TRX_OFF;
  391. lp->tx_retry++;
  392. at86rf230_async_state_change(lp, ctx, state,
  393. ctx->complete);
  394. return;
  395. }
  396. }
  397. dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
  398. ctx->from_state, ctx->to_state, trx_state);
  399. }
  400. done:
  401. if (ctx->complete)
  402. ctx->complete(context);
  403. }
  404. static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
  405. {
  406. struct at86rf230_state_change *ctx =
  407. container_of(timer, struct at86rf230_state_change, timer);
  408. struct at86rf230_local *lp = ctx->lp;
  409. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  410. at86rf230_async_state_assert);
  411. return HRTIMER_NORESTART;
  412. }
  413. /* Do state change timing delay. */
  414. static void
  415. at86rf230_async_state_delay(void *context)
  416. {
  417. struct at86rf230_state_change *ctx = context;
  418. struct at86rf230_local *lp = ctx->lp;
  419. struct at86rf2xx_chip_data *c = lp->data;
  420. bool force = false;
  421. ktime_t tim;
  422. /* The force state changes are will show as normal states in the
  423. * state status subregister. We change the to_state to the
  424. * corresponding one and remember if it was a force change, this
  425. * differs if we do a state change from STATE_BUSY_RX_AACK.
  426. */
  427. switch (ctx->to_state) {
  428. case STATE_FORCE_TX_ON:
  429. ctx->to_state = STATE_TX_ON;
  430. force = true;
  431. break;
  432. case STATE_FORCE_TRX_OFF:
  433. ctx->to_state = STATE_TRX_OFF;
  434. force = true;
  435. break;
  436. default:
  437. break;
  438. }
  439. switch (ctx->from_state) {
  440. case STATE_TRX_OFF:
  441. switch (ctx->to_state) {
  442. case STATE_RX_AACK_ON:
  443. tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
  444. /* state change from TRX_OFF to RX_AACK_ON to do a
  445. * calibration, we need to reset the timeout for the
  446. * next one.
  447. */
  448. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  449. goto change;
  450. case STATE_TX_ARET_ON:
  451. case STATE_TX_ON:
  452. tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
  453. /* state change from TRX_OFF to TX_ON or ARET_ON to do
  454. * a calibration, we need to reset the timeout for the
  455. * next one.
  456. */
  457. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  458. goto change;
  459. default:
  460. break;
  461. }
  462. break;
  463. case STATE_BUSY_RX_AACK:
  464. switch (ctx->to_state) {
  465. case STATE_TRX_OFF:
  466. case STATE_TX_ON:
  467. /* Wait for worst case receiving time if we
  468. * didn't make a force change from BUSY_RX_AACK
  469. * to TX_ON or TRX_OFF.
  470. */
  471. if (!force) {
  472. tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
  473. NSEC_PER_USEC);
  474. goto change;
  475. }
  476. break;
  477. default:
  478. break;
  479. }
  480. break;
  481. /* Default value, means RESET state */
  482. case STATE_P_ON:
  483. switch (ctx->to_state) {
  484. case STATE_TRX_OFF:
  485. tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
  486. goto change;
  487. default:
  488. break;
  489. }
  490. break;
  491. default:
  492. break;
  493. }
  494. /* Default delay is 1us in the most cases */
  495. udelay(1);
  496. at86rf230_async_state_timer(&ctx->timer);
  497. return;
  498. change:
  499. hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
  500. }
  501. static void
  502. at86rf230_async_state_change_start(void *context)
  503. {
  504. struct at86rf230_state_change *ctx = context;
  505. struct at86rf230_local *lp = ctx->lp;
  506. u8 *buf = ctx->buf;
  507. const u8 trx_state = buf[1] & TRX_STATE_MASK;
  508. /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
  509. if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
  510. udelay(1);
  511. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  512. at86rf230_async_state_change_start);
  513. return;
  514. }
  515. /* Check if we already are in the state which we change in */
  516. if (trx_state == ctx->to_state) {
  517. if (ctx->complete)
  518. ctx->complete(context);
  519. return;
  520. }
  521. /* Set current state to the context of state change */
  522. ctx->from_state = trx_state;
  523. /* Going into the next step for a state change which do a timing
  524. * relevant delay.
  525. */
  526. at86rf230_async_write_reg(lp, RG_TRX_STATE, ctx->to_state, ctx,
  527. at86rf230_async_state_delay);
  528. }
  529. static void
  530. at86rf230_async_state_change(struct at86rf230_local *lp,
  531. struct at86rf230_state_change *ctx,
  532. const u8 state, void (*complete)(void *context))
  533. {
  534. /* Initialization for the state change context */
  535. ctx->to_state = state;
  536. ctx->complete = complete;
  537. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  538. at86rf230_async_state_change_start);
  539. }
  540. static void
  541. at86rf230_sync_state_change_complete(void *context)
  542. {
  543. struct at86rf230_state_change *ctx = context;
  544. struct at86rf230_local *lp = ctx->lp;
  545. complete(&lp->state_complete);
  546. }
  547. /* This function do a sync framework above the async state change.
  548. * Some callbacks of the IEEE 802.15.4 driver interface need to be
  549. * handled synchronously.
  550. */
  551. static int
  552. at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
  553. {
  554. unsigned long rc;
  555. at86rf230_async_state_change(lp, &lp->state, state,
  556. at86rf230_sync_state_change_complete);
  557. rc = wait_for_completion_timeout(&lp->state_complete,
  558. msecs_to_jiffies(100));
  559. if (!rc) {
  560. at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
  561. return -ETIMEDOUT;
  562. }
  563. return 0;
  564. }
  565. static void
  566. at86rf230_tx_complete(void *context)
  567. {
  568. struct at86rf230_state_change *ctx = context;
  569. struct at86rf230_local *lp = ctx->lp;
  570. ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
  571. kfree(ctx);
  572. }
  573. static void
  574. at86rf230_tx_on(void *context)
  575. {
  576. struct at86rf230_state_change *ctx = context;
  577. struct at86rf230_local *lp = ctx->lp;
  578. at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
  579. at86rf230_tx_complete);
  580. }
  581. static void
  582. at86rf230_tx_trac_check(void *context)
  583. {
  584. struct at86rf230_state_change *ctx = context;
  585. struct at86rf230_local *lp = ctx->lp;
  586. if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
  587. u8 trac = TRAC_MASK(ctx->buf[1]);
  588. switch (trac) {
  589. case TRAC_SUCCESS:
  590. lp->trac.success++;
  591. break;
  592. case TRAC_SUCCESS_DATA_PENDING:
  593. lp->trac.success_data_pending++;
  594. break;
  595. case TRAC_CHANNEL_ACCESS_FAILURE:
  596. lp->trac.channel_access_failure++;
  597. break;
  598. case TRAC_NO_ACK:
  599. lp->trac.no_ack++;
  600. break;
  601. case TRAC_INVALID:
  602. lp->trac.invalid++;
  603. break;
  604. default:
  605. WARN_ONCE(1, "received tx trac status %d\n", trac);
  606. break;
  607. }
  608. }
  609. at86rf230_async_state_change(lp, ctx, STATE_TX_ON, at86rf230_tx_on);
  610. }
  611. static void
  612. at86rf230_rx_read_frame_complete(void *context)
  613. {
  614. struct at86rf230_state_change *ctx = context;
  615. struct at86rf230_local *lp = ctx->lp;
  616. const u8 *buf = ctx->buf;
  617. struct sk_buff *skb;
  618. u8 len, lqi;
  619. len = buf[1];
  620. if (!ieee802154_is_valid_psdu_len(len)) {
  621. dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
  622. len = IEEE802154_MTU;
  623. }
  624. lqi = buf[2 + len];
  625. skb = dev_alloc_skb(IEEE802154_MTU);
  626. if (!skb) {
  627. dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
  628. kfree(ctx);
  629. return;
  630. }
  631. memcpy(skb_put(skb, len), buf + 2, len);
  632. ieee802154_rx_irqsafe(lp->hw, skb, lqi);
  633. kfree(ctx);
  634. }
  635. static void
  636. at86rf230_rx_trac_check(void *context)
  637. {
  638. struct at86rf230_state_change *ctx = context;
  639. struct at86rf230_local *lp = ctx->lp;
  640. u8 *buf = ctx->buf;
  641. int rc;
  642. if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
  643. u8 trac = TRAC_MASK(buf[1]);
  644. switch (trac) {
  645. case TRAC_SUCCESS:
  646. lp->trac.success++;
  647. break;
  648. case TRAC_SUCCESS_WAIT_FOR_ACK:
  649. lp->trac.success_wait_for_ack++;
  650. break;
  651. case TRAC_INVALID:
  652. lp->trac.invalid++;
  653. break;
  654. default:
  655. WARN_ONCE(1, "received rx trac status %d\n", trac);
  656. break;
  657. }
  658. }
  659. buf[0] = CMD_FB;
  660. ctx->trx.len = AT86RF2XX_MAX_BUF;
  661. ctx->msg.complete = at86rf230_rx_read_frame_complete;
  662. rc = spi_async(lp->spi, &ctx->msg);
  663. if (rc) {
  664. ctx->trx.len = 2;
  665. at86rf230_async_error(lp, ctx, rc);
  666. }
  667. }
  668. static void
  669. at86rf230_irq_trx_end(void *context)
  670. {
  671. struct at86rf230_state_change *ctx = context;
  672. struct at86rf230_local *lp = ctx->lp;
  673. if (lp->is_tx) {
  674. lp->is_tx = 0;
  675. at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
  676. at86rf230_tx_trac_check);
  677. } else {
  678. at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
  679. at86rf230_rx_trac_check);
  680. }
  681. }
  682. static void
  683. at86rf230_irq_status(void *context)
  684. {
  685. struct at86rf230_state_change *ctx = context;
  686. struct at86rf230_local *lp = ctx->lp;
  687. const u8 *buf = ctx->buf;
  688. u8 irq = buf[1];
  689. enable_irq(lp->spi->irq);
  690. if (irq & IRQ_TRX_END) {
  691. at86rf230_irq_trx_end(ctx);
  692. } else {
  693. dev_err(&lp->spi->dev, "not supported irq %02x received\n",
  694. irq);
  695. kfree(ctx);
  696. }
  697. }
  698. static void
  699. at86rf230_setup_spi_messages(struct at86rf230_local *lp,
  700. struct at86rf230_state_change *state)
  701. {
  702. state->lp = lp;
  703. state->irq = lp->spi->irq;
  704. spi_message_init(&state->msg);
  705. state->msg.context = state;
  706. state->trx.len = 2;
  707. state->trx.tx_buf = state->buf;
  708. state->trx.rx_buf = state->buf;
  709. spi_message_add_tail(&state->trx, &state->msg);
  710. hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  711. state->timer.function = at86rf230_async_state_timer;
  712. }
  713. static irqreturn_t at86rf230_isr(int irq, void *data)
  714. {
  715. struct at86rf230_local *lp = data;
  716. struct at86rf230_state_change *ctx;
  717. int rc;
  718. disable_irq_nosync(irq);
  719. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  720. if (!ctx) {
  721. enable_irq(irq);
  722. return IRQ_NONE;
  723. }
  724. at86rf230_setup_spi_messages(lp, ctx);
  725. /* tell on error handling to free ctx */
  726. ctx->free = true;
  727. ctx->buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
  728. ctx->msg.complete = at86rf230_irq_status;
  729. rc = spi_async(lp->spi, &ctx->msg);
  730. if (rc) {
  731. at86rf230_async_error(lp, ctx, rc);
  732. enable_irq(irq);
  733. return IRQ_NONE;
  734. }
  735. return IRQ_HANDLED;
  736. }
  737. static void
  738. at86rf230_write_frame_complete(void *context)
  739. {
  740. struct at86rf230_state_change *ctx = context;
  741. struct at86rf230_local *lp = ctx->lp;
  742. ctx->trx.len = 2;
  743. if (gpio_is_valid(lp->slp_tr))
  744. at86rf230_slp_tr_rising_edge(lp);
  745. else
  746. at86rf230_async_write_reg(lp, RG_TRX_STATE, STATE_BUSY_TX, ctx,
  747. NULL);
  748. }
  749. static void
  750. at86rf230_write_frame(void *context)
  751. {
  752. struct at86rf230_state_change *ctx = context;
  753. struct at86rf230_local *lp = ctx->lp;
  754. struct sk_buff *skb = lp->tx_skb;
  755. u8 *buf = ctx->buf;
  756. int rc;
  757. lp->is_tx = 1;
  758. buf[0] = CMD_FB | CMD_WRITE;
  759. buf[1] = skb->len + 2;
  760. memcpy(buf + 2, skb->data, skb->len);
  761. ctx->trx.len = skb->len + 2;
  762. ctx->msg.complete = at86rf230_write_frame_complete;
  763. rc = spi_async(lp->spi, &ctx->msg);
  764. if (rc) {
  765. ctx->trx.len = 2;
  766. at86rf230_async_error(lp, ctx, rc);
  767. }
  768. }
  769. static void
  770. at86rf230_xmit_tx_on(void *context)
  771. {
  772. struct at86rf230_state_change *ctx = context;
  773. struct at86rf230_local *lp = ctx->lp;
  774. at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
  775. at86rf230_write_frame);
  776. }
  777. static void
  778. at86rf230_xmit_start(void *context)
  779. {
  780. struct at86rf230_state_change *ctx = context;
  781. struct at86rf230_local *lp = ctx->lp;
  782. /* check if we change from off state */
  783. if (lp->is_tx_from_off)
  784. at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
  785. at86rf230_write_frame);
  786. else
  787. at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
  788. at86rf230_xmit_tx_on);
  789. }
  790. static int
  791. at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  792. {
  793. struct at86rf230_local *lp = hw->priv;
  794. struct at86rf230_state_change *ctx = &lp->tx;
  795. lp->tx_skb = skb;
  796. lp->tx_retry = 0;
  797. /* After 5 minutes in PLL and the same frequency we run again the
  798. * calibration loops which is recommended by at86rf2xx datasheets.
  799. *
  800. * The calibration is initiate by a state change from TRX_OFF
  801. * to TX_ON, the lp->cal_timeout should be reinit by state_delay
  802. * function then to start in the next 5 minutes.
  803. */
  804. if (time_is_before_jiffies(lp->cal_timeout)) {
  805. lp->is_tx_from_off = true;
  806. at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
  807. at86rf230_xmit_start);
  808. } else {
  809. lp->is_tx_from_off = false;
  810. at86rf230_xmit_start(ctx);
  811. }
  812. return 0;
  813. }
  814. static int
  815. at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
  816. {
  817. BUG_ON(!level);
  818. *level = 0xbe;
  819. return 0;
  820. }
  821. static int
  822. at86rf230_start(struct ieee802154_hw *hw)
  823. {
  824. struct at86rf230_local *lp = hw->priv;
  825. /* reset trac stats on start */
  826. if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS))
  827. memset(&lp->trac, 0, sizeof(struct at86rf230_trac));
  828. at86rf230_awake(lp);
  829. enable_irq(lp->spi->irq);
  830. return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
  831. }
  832. static void
  833. at86rf230_stop(struct ieee802154_hw *hw)
  834. {
  835. struct at86rf230_local *lp = hw->priv;
  836. u8 csma_seed[2];
  837. at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
  838. disable_irq(lp->spi->irq);
  839. /* It's recommended to set random new csma_seeds before sleep state.
  840. * Makes only sense in the stop callback, not doing this inside of
  841. * at86rf230_sleep, this is also used when we don't transmit afterwards
  842. * when calling start callback again.
  843. */
  844. get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
  845. at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
  846. at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
  847. at86rf230_sleep(lp);
  848. }
  849. static int
  850. at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  851. {
  852. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  853. }
  854. #define AT86RF2XX_MAX_ED_LEVELS 0xF
  855. static const s32 at86rf23x_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
  856. -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
  857. -7100, -6900, -6700, -6500, -6300, -6100,
  858. };
  859. static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
  860. -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
  861. -8000, -7800, -7600, -7400, -7200, -7000,
  862. };
  863. static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
  864. -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
  865. -7800, -7600, -7400, -7200, -7000, -6800,
  866. };
  867. static inline int
  868. at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
  869. {
  870. unsigned int cca_ed_thres;
  871. int rc;
  872. rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
  873. if (rc < 0)
  874. return rc;
  875. switch (rssi_base_val) {
  876. case -98:
  877. lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
  878. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
  879. lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
  880. break;
  881. case -100:
  882. lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
  883. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
  884. lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
  885. break;
  886. default:
  887. WARN_ON(1);
  888. }
  889. return 0;
  890. }
  891. static int
  892. at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  893. {
  894. int rc;
  895. if (channel == 0)
  896. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
  897. else
  898. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
  899. if (rc < 0)
  900. return rc;
  901. if (page == 0) {
  902. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
  903. lp->data->rssi_base_val = -100;
  904. } else {
  905. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
  906. lp->data->rssi_base_val = -98;
  907. }
  908. if (rc < 0)
  909. return rc;
  910. rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
  911. if (rc < 0)
  912. return rc;
  913. /* This sets the symbol_duration according frequency on the 212.
  914. * TODO move this handling while set channel and page in cfg802154.
  915. * We can do that, this timings are according 802.15.4 standard.
  916. * If we do that in cfg802154, this is a more generic calculation.
  917. *
  918. * This should also protected from ifs_timer. Means cancel timer and
  919. * init with a new value. For now, this is okay.
  920. */
  921. if (channel == 0) {
  922. if (page == 0) {
  923. /* SUB:0 and BPSK:0 -> BPSK-20 */
  924. lp->hw->phy->symbol_duration = 50;
  925. } else {
  926. /* SUB:1 and BPSK:0 -> BPSK-40 */
  927. lp->hw->phy->symbol_duration = 25;
  928. }
  929. } else {
  930. if (page == 0)
  931. /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
  932. lp->hw->phy->symbol_duration = 40;
  933. else
  934. /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
  935. lp->hw->phy->symbol_duration = 16;
  936. }
  937. lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
  938. lp->hw->phy->symbol_duration;
  939. lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
  940. lp->hw->phy->symbol_duration;
  941. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  942. }
  943. static int
  944. at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  945. {
  946. struct at86rf230_local *lp = hw->priv;
  947. int rc;
  948. rc = lp->data->set_channel(lp, page, channel);
  949. /* Wait for PLL */
  950. usleep_range(lp->data->t_channel_switch,
  951. lp->data->t_channel_switch + 10);
  952. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  953. return rc;
  954. }
  955. static int
  956. at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
  957. struct ieee802154_hw_addr_filt *filt,
  958. unsigned long changed)
  959. {
  960. struct at86rf230_local *lp = hw->priv;
  961. if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
  962. u16 addr = le16_to_cpu(filt->short_addr);
  963. dev_vdbg(&lp->spi->dev,
  964. "at86rf230_set_hw_addr_filt called for saddr\n");
  965. __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
  966. __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
  967. }
  968. if (changed & IEEE802154_AFILT_PANID_CHANGED) {
  969. u16 pan = le16_to_cpu(filt->pan_id);
  970. dev_vdbg(&lp->spi->dev,
  971. "at86rf230_set_hw_addr_filt called for pan id\n");
  972. __at86rf230_write(lp, RG_PAN_ID_0, pan);
  973. __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
  974. }
  975. if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
  976. u8 i, addr[8];
  977. memcpy(addr, &filt->ieee_addr, 8);
  978. dev_vdbg(&lp->spi->dev,
  979. "at86rf230_set_hw_addr_filt called for IEEE addr\n");
  980. for (i = 0; i < 8; i++)
  981. __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
  982. }
  983. if (changed & IEEE802154_AFILT_PANC_CHANGED) {
  984. dev_vdbg(&lp->spi->dev,
  985. "at86rf230_set_hw_addr_filt called for panc change\n");
  986. if (filt->pan_coord)
  987. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
  988. else
  989. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
  990. }
  991. return 0;
  992. }
  993. #define AT86RF23X_MAX_TX_POWERS 0xF
  994. static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
  995. 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
  996. -800, -1200, -1700,
  997. };
  998. static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
  999. 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
  1000. -900, -1200, -1700,
  1001. };
  1002. #define AT86RF212_MAX_TX_POWERS 0x1F
  1003. static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
  1004. 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
  1005. -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
  1006. -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
  1007. };
  1008. static int
  1009. at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
  1010. {
  1011. u32 i;
  1012. for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
  1013. if (lp->hw->phy->supported.tx_powers[i] == mbm)
  1014. return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
  1015. }
  1016. return -EINVAL;
  1017. }
  1018. static int
  1019. at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
  1020. {
  1021. u32 i;
  1022. for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
  1023. if (lp->hw->phy->supported.tx_powers[i] == mbm)
  1024. return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
  1025. }
  1026. return -EINVAL;
  1027. }
  1028. static int
  1029. at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
  1030. {
  1031. struct at86rf230_local *lp = hw->priv;
  1032. return lp->data->set_txpower(lp, mbm);
  1033. }
  1034. static int
  1035. at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
  1036. {
  1037. struct at86rf230_local *lp = hw->priv;
  1038. return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
  1039. }
  1040. static int
  1041. at86rf230_set_cca_mode(struct ieee802154_hw *hw,
  1042. const struct wpan_phy_cca *cca)
  1043. {
  1044. struct at86rf230_local *lp = hw->priv;
  1045. u8 val;
  1046. /* mapping 802.15.4 to driver spec */
  1047. switch (cca->mode) {
  1048. case NL802154_CCA_ENERGY:
  1049. val = 1;
  1050. break;
  1051. case NL802154_CCA_CARRIER:
  1052. val = 2;
  1053. break;
  1054. case NL802154_CCA_ENERGY_CARRIER:
  1055. switch (cca->opt) {
  1056. case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
  1057. val = 3;
  1058. break;
  1059. case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
  1060. val = 0;
  1061. break;
  1062. default:
  1063. return -EINVAL;
  1064. }
  1065. break;
  1066. default:
  1067. return -EINVAL;
  1068. }
  1069. return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
  1070. }
  1071. static int
  1072. at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
  1073. {
  1074. struct at86rf230_local *lp = hw->priv;
  1075. u32 i;
  1076. for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
  1077. if (hw->phy->supported.cca_ed_levels[i] == mbm)
  1078. return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
  1079. }
  1080. return -EINVAL;
  1081. }
  1082. static int
  1083. at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
  1084. u8 retries)
  1085. {
  1086. struct at86rf230_local *lp = hw->priv;
  1087. int rc;
  1088. rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
  1089. if (rc)
  1090. return rc;
  1091. rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
  1092. if (rc)
  1093. return rc;
  1094. return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
  1095. }
  1096. static int
  1097. at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
  1098. {
  1099. struct at86rf230_local *lp = hw->priv;
  1100. return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
  1101. }
  1102. static int
  1103. at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  1104. {
  1105. struct at86rf230_local *lp = hw->priv;
  1106. int rc;
  1107. if (on) {
  1108. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
  1109. if (rc < 0)
  1110. return rc;
  1111. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
  1112. if (rc < 0)
  1113. return rc;
  1114. } else {
  1115. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
  1116. if (rc < 0)
  1117. return rc;
  1118. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
  1119. if (rc < 0)
  1120. return rc;
  1121. }
  1122. return 0;
  1123. }
  1124. static const struct ieee802154_ops at86rf230_ops = {
  1125. .owner = THIS_MODULE,
  1126. .xmit_async = at86rf230_xmit,
  1127. .ed = at86rf230_ed,
  1128. .set_channel = at86rf230_channel,
  1129. .start = at86rf230_start,
  1130. .stop = at86rf230_stop,
  1131. .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
  1132. .set_txpower = at86rf230_set_txpower,
  1133. .set_lbt = at86rf230_set_lbt,
  1134. .set_cca_mode = at86rf230_set_cca_mode,
  1135. .set_cca_ed_level = at86rf230_set_cca_ed_level,
  1136. .set_csma_params = at86rf230_set_csma_params,
  1137. .set_frame_retries = at86rf230_set_frame_retries,
  1138. .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
  1139. };
  1140. static struct at86rf2xx_chip_data at86rf233_data = {
  1141. .t_sleep_cycle = 330,
  1142. .t_channel_switch = 11,
  1143. .t_reset_to_off = 26,
  1144. .t_off_to_aack = 80,
  1145. .t_off_to_tx_on = 80,
  1146. .t_off_to_sleep = 35,
  1147. .t_sleep_to_off = 1000,
  1148. .t_frame = 4096,
  1149. .t_p_ack = 545,
  1150. .rssi_base_val = -91,
  1151. .set_channel = at86rf23x_set_channel,
  1152. .set_txpower = at86rf23x_set_txpower,
  1153. };
  1154. static struct at86rf2xx_chip_data at86rf231_data = {
  1155. .t_sleep_cycle = 330,
  1156. .t_channel_switch = 24,
  1157. .t_reset_to_off = 37,
  1158. .t_off_to_aack = 110,
  1159. .t_off_to_tx_on = 110,
  1160. .t_off_to_sleep = 35,
  1161. .t_sleep_to_off = 1000,
  1162. .t_frame = 4096,
  1163. .t_p_ack = 545,
  1164. .rssi_base_val = -91,
  1165. .set_channel = at86rf23x_set_channel,
  1166. .set_txpower = at86rf23x_set_txpower,
  1167. };
  1168. static struct at86rf2xx_chip_data at86rf212_data = {
  1169. .t_sleep_cycle = 330,
  1170. .t_channel_switch = 11,
  1171. .t_reset_to_off = 26,
  1172. .t_off_to_aack = 200,
  1173. .t_off_to_tx_on = 200,
  1174. .t_off_to_sleep = 35,
  1175. .t_sleep_to_off = 1000,
  1176. .t_frame = 4096,
  1177. .t_p_ack = 545,
  1178. .rssi_base_val = -100,
  1179. .set_channel = at86rf212_set_channel,
  1180. .set_txpower = at86rf212_set_txpower,
  1181. };
  1182. static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
  1183. {
  1184. int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
  1185. unsigned int dvdd;
  1186. u8 csma_seed[2];
  1187. rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
  1188. if (rc)
  1189. return rc;
  1190. irq_type = irq_get_trigger_type(lp->spi->irq);
  1191. if (irq_type == IRQ_TYPE_EDGE_FALLING ||
  1192. irq_type == IRQ_TYPE_LEVEL_LOW)
  1193. irq_pol = IRQ_ACTIVE_LOW;
  1194. rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
  1195. if (rc)
  1196. return rc;
  1197. rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
  1198. if (rc)
  1199. return rc;
  1200. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
  1201. if (rc)
  1202. return rc;
  1203. /* reset values differs in at86rf231 and at86rf233 */
  1204. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
  1205. if (rc)
  1206. return rc;
  1207. get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
  1208. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
  1209. if (rc)
  1210. return rc;
  1211. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
  1212. if (rc)
  1213. return rc;
  1214. /* CLKM changes are applied immediately */
  1215. rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
  1216. if (rc)
  1217. return rc;
  1218. /* Turn CLKM Off */
  1219. rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
  1220. if (rc)
  1221. return rc;
  1222. /* Wait the next SLEEP cycle */
  1223. usleep_range(lp->data->t_sleep_cycle,
  1224. lp->data->t_sleep_cycle + 100);
  1225. /* xtal_trim value is calculated by:
  1226. * CL = 0.5 * (CX + CTRIM + CPAR)
  1227. *
  1228. * whereas:
  1229. * CL = capacitor of used crystal
  1230. * CX = connected capacitors at xtal pins
  1231. * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
  1232. * but this is different on each board setup. You need to fine
  1233. * tuning this value via CTRIM.
  1234. * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
  1235. * 0 pF upto 4.5 pF.
  1236. *
  1237. * Examples:
  1238. * atben transceiver:
  1239. *
  1240. * CL = 8 pF
  1241. * CX = 12 pF
  1242. * CPAR = 3 pF (We assume the magic constant from datasheet)
  1243. * CTRIM = 0.9 pF
  1244. *
  1245. * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
  1246. *
  1247. * xtal_trim = 0x3
  1248. *
  1249. * openlabs transceiver:
  1250. *
  1251. * CL = 16 pF
  1252. * CX = 22 pF
  1253. * CPAR = 3 pF (We assume the magic constant from datasheet)
  1254. * CTRIM = 4.5 pF
  1255. *
  1256. * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
  1257. *
  1258. * xtal_trim = 0xf
  1259. */
  1260. rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
  1261. if (rc)
  1262. return rc;
  1263. rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
  1264. if (rc)
  1265. return rc;
  1266. if (!dvdd) {
  1267. dev_err(&lp->spi->dev, "DVDD error\n");
  1268. return -EINVAL;
  1269. }
  1270. /* Force setting slotted operation bit to 0. Sometimes the atben
  1271. * sets this bit and I don't know why. We set this always force
  1272. * to zero while probing.
  1273. */
  1274. return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
  1275. }
  1276. static int
  1277. at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
  1278. u8 *xtal_trim)
  1279. {
  1280. struct at86rf230_platform_data *pdata = spi->dev.platform_data;
  1281. int ret;
  1282. if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
  1283. if (!pdata)
  1284. return -ENOENT;
  1285. *rstn = pdata->rstn;
  1286. *slp_tr = pdata->slp_tr;
  1287. *xtal_trim = pdata->xtal_trim;
  1288. return 0;
  1289. }
  1290. *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
  1291. *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
  1292. ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
  1293. if (ret < 0 && ret != -EINVAL)
  1294. return ret;
  1295. return 0;
  1296. }
  1297. static int
  1298. at86rf230_detect_device(struct at86rf230_local *lp)
  1299. {
  1300. unsigned int part, version, val;
  1301. u16 man_id = 0;
  1302. const char *chip;
  1303. int rc;
  1304. rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
  1305. if (rc)
  1306. return rc;
  1307. man_id |= val;
  1308. rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
  1309. if (rc)
  1310. return rc;
  1311. man_id |= (val << 8);
  1312. rc = __at86rf230_read(lp, RG_PART_NUM, &part);
  1313. if (rc)
  1314. return rc;
  1315. rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
  1316. if (rc)
  1317. return rc;
  1318. if (man_id != 0x001f) {
  1319. dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
  1320. man_id >> 8, man_id & 0xFF);
  1321. return -EINVAL;
  1322. }
  1323. lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
  1324. IEEE802154_HW_CSMA_PARAMS |
  1325. IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
  1326. IEEE802154_HW_PROMISCUOUS;
  1327. lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
  1328. WPAN_PHY_FLAG_CCA_ED_LEVEL |
  1329. WPAN_PHY_FLAG_CCA_MODE;
  1330. lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
  1331. BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
  1332. lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
  1333. BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
  1334. lp->hw->phy->supported.cca_ed_levels = at86rf23x_ed_levels;
  1335. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf23x_ed_levels);
  1336. lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
  1337. switch (part) {
  1338. case 2:
  1339. chip = "at86rf230";
  1340. rc = -ENOTSUPP;
  1341. goto not_supp;
  1342. case 3:
  1343. chip = "at86rf231";
  1344. lp->data = &at86rf231_data;
  1345. lp->hw->phy->supported.channels[0] = 0x7FFF800;
  1346. lp->hw->phy->current_channel = 11;
  1347. lp->hw->phy->symbol_duration = 16;
  1348. lp->hw->phy->supported.tx_powers = at86rf231_powers;
  1349. lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
  1350. break;
  1351. case 7:
  1352. chip = "at86rf212";
  1353. lp->data = &at86rf212_data;
  1354. lp->hw->flags |= IEEE802154_HW_LBT;
  1355. lp->hw->phy->supported.channels[0] = 0x00007FF;
  1356. lp->hw->phy->supported.channels[2] = 0x00007FF;
  1357. lp->hw->phy->current_channel = 5;
  1358. lp->hw->phy->symbol_duration = 25;
  1359. lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
  1360. lp->hw->phy->supported.tx_powers = at86rf212_powers;
  1361. lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
  1362. lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
  1363. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
  1364. break;
  1365. case 11:
  1366. chip = "at86rf233";
  1367. lp->data = &at86rf233_data;
  1368. lp->hw->phy->supported.channels[0] = 0x7FFF800;
  1369. lp->hw->phy->current_channel = 13;
  1370. lp->hw->phy->symbol_duration = 16;
  1371. lp->hw->phy->supported.tx_powers = at86rf233_powers;
  1372. lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
  1373. break;
  1374. default:
  1375. chip = "unknown";
  1376. rc = -ENOTSUPP;
  1377. goto not_supp;
  1378. }
  1379. lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
  1380. lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
  1381. not_supp:
  1382. dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
  1383. return rc;
  1384. }
  1385. #ifdef CONFIG_IEEE802154_AT86RF230_DEBUGFS
  1386. static struct dentry *at86rf230_debugfs_root;
  1387. static int at86rf230_stats_show(struct seq_file *file, void *offset)
  1388. {
  1389. struct at86rf230_local *lp = file->private;
  1390. seq_printf(file, "SUCCESS:\t\t%8llu\n", lp->trac.success);
  1391. seq_printf(file, "SUCCESS_DATA_PENDING:\t%8llu\n",
  1392. lp->trac.success_data_pending);
  1393. seq_printf(file, "SUCCESS_WAIT_FOR_ACK:\t%8llu\n",
  1394. lp->trac.success_wait_for_ack);
  1395. seq_printf(file, "CHANNEL_ACCESS_FAILURE:\t%8llu\n",
  1396. lp->trac.channel_access_failure);
  1397. seq_printf(file, "NO_ACK:\t\t\t%8llu\n", lp->trac.no_ack);
  1398. seq_printf(file, "INVALID:\t\t%8llu\n", lp->trac.invalid);
  1399. return 0;
  1400. }
  1401. static int at86rf230_stats_open(struct inode *inode, struct file *file)
  1402. {
  1403. return single_open(file, at86rf230_stats_show, inode->i_private);
  1404. }
  1405. static const struct file_operations at86rf230_stats_fops = {
  1406. .open = at86rf230_stats_open,
  1407. .read = seq_read,
  1408. .llseek = seq_lseek,
  1409. .release = single_release,
  1410. };
  1411. static int at86rf230_debugfs_init(struct at86rf230_local *lp)
  1412. {
  1413. char debugfs_dir_name[DNAME_INLINE_LEN + 1] = "at86rf230-";
  1414. struct dentry *stats;
  1415. strncat(debugfs_dir_name, dev_name(&lp->spi->dev), DNAME_INLINE_LEN);
  1416. at86rf230_debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
  1417. if (!at86rf230_debugfs_root)
  1418. return -ENOMEM;
  1419. stats = debugfs_create_file("trac_stats", S_IRUGO,
  1420. at86rf230_debugfs_root, lp,
  1421. &at86rf230_stats_fops);
  1422. if (!stats)
  1423. return -ENOMEM;
  1424. return 0;
  1425. }
  1426. static void at86rf230_debugfs_remove(void)
  1427. {
  1428. debugfs_remove_recursive(at86rf230_debugfs_root);
  1429. }
  1430. #else
  1431. static int at86rf230_debugfs_init(struct at86rf230_local *lp) { return 0; }
  1432. static void at86rf230_debugfs_remove(void) { }
  1433. #endif
  1434. static int at86rf230_probe(struct spi_device *spi)
  1435. {
  1436. struct ieee802154_hw *hw;
  1437. struct at86rf230_local *lp;
  1438. unsigned int status;
  1439. int rc, irq_type, rstn, slp_tr;
  1440. u8 xtal_trim = 0;
  1441. if (!spi->irq) {
  1442. dev_err(&spi->dev, "no IRQ specified\n");
  1443. return -EINVAL;
  1444. }
  1445. rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
  1446. if (rc < 0) {
  1447. dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
  1448. return rc;
  1449. }
  1450. if (gpio_is_valid(rstn)) {
  1451. rc = devm_gpio_request_one(&spi->dev, rstn,
  1452. GPIOF_OUT_INIT_HIGH, "rstn");
  1453. if (rc)
  1454. return rc;
  1455. }
  1456. if (gpio_is_valid(slp_tr)) {
  1457. rc = devm_gpio_request_one(&spi->dev, slp_tr,
  1458. GPIOF_OUT_INIT_LOW, "slp_tr");
  1459. if (rc)
  1460. return rc;
  1461. }
  1462. /* Reset */
  1463. if (gpio_is_valid(rstn)) {
  1464. udelay(1);
  1465. gpio_set_value(rstn, 0);
  1466. udelay(1);
  1467. gpio_set_value(rstn, 1);
  1468. usleep_range(120, 240);
  1469. }
  1470. hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
  1471. if (!hw)
  1472. return -ENOMEM;
  1473. lp = hw->priv;
  1474. lp->hw = hw;
  1475. lp->spi = spi;
  1476. lp->slp_tr = slp_tr;
  1477. hw->parent = &spi->dev;
  1478. ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
  1479. lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
  1480. if (IS_ERR(lp->regmap)) {
  1481. rc = PTR_ERR(lp->regmap);
  1482. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  1483. rc);
  1484. goto free_dev;
  1485. }
  1486. at86rf230_setup_spi_messages(lp, &lp->state);
  1487. at86rf230_setup_spi_messages(lp, &lp->tx);
  1488. rc = at86rf230_detect_device(lp);
  1489. if (rc < 0)
  1490. goto free_dev;
  1491. init_completion(&lp->state_complete);
  1492. spi_set_drvdata(spi, lp);
  1493. rc = at86rf230_hw_init(lp, xtal_trim);
  1494. if (rc)
  1495. goto free_dev;
  1496. /* Read irq status register to reset irq line */
  1497. rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
  1498. if (rc)
  1499. goto free_dev;
  1500. irq_type = irq_get_trigger_type(spi->irq);
  1501. if (!irq_type)
  1502. irq_type = IRQF_TRIGGER_HIGH;
  1503. rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
  1504. IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
  1505. if (rc)
  1506. goto free_dev;
  1507. /* disable_irq by default and wait for starting hardware */
  1508. disable_irq(spi->irq);
  1509. /* going into sleep by default */
  1510. at86rf230_sleep(lp);
  1511. rc = at86rf230_debugfs_init(lp);
  1512. if (rc)
  1513. goto free_dev;
  1514. rc = ieee802154_register_hw(lp->hw);
  1515. if (rc)
  1516. goto free_debugfs;
  1517. return rc;
  1518. free_debugfs:
  1519. at86rf230_debugfs_remove();
  1520. free_dev:
  1521. ieee802154_free_hw(lp->hw);
  1522. return rc;
  1523. }
  1524. static int at86rf230_remove(struct spi_device *spi)
  1525. {
  1526. struct at86rf230_local *lp = spi_get_drvdata(spi);
  1527. /* mask all at86rf230 irq's */
  1528. at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
  1529. ieee802154_unregister_hw(lp->hw);
  1530. ieee802154_free_hw(lp->hw);
  1531. at86rf230_debugfs_remove();
  1532. dev_dbg(&spi->dev, "unregistered at86rf230\n");
  1533. return 0;
  1534. }
  1535. static const struct of_device_id at86rf230_of_match[] = {
  1536. { .compatible = "atmel,at86rf230", },
  1537. { .compatible = "atmel,at86rf231", },
  1538. { .compatible = "atmel,at86rf233", },
  1539. { .compatible = "atmel,at86rf212", },
  1540. { },
  1541. };
  1542. MODULE_DEVICE_TABLE(of, at86rf230_of_match);
  1543. static const struct spi_device_id at86rf230_device_id[] = {
  1544. { .name = "at86rf230", },
  1545. { .name = "at86rf231", },
  1546. { .name = "at86rf233", },
  1547. { .name = "at86rf212", },
  1548. { },
  1549. };
  1550. MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
  1551. static struct spi_driver at86rf230_driver = {
  1552. .id_table = at86rf230_device_id,
  1553. .driver = {
  1554. .of_match_table = of_match_ptr(at86rf230_of_match),
  1555. .name = "at86rf230",
  1556. },
  1557. .probe = at86rf230_probe,
  1558. .remove = at86rf230_remove,
  1559. };
  1560. module_spi_driver(at86rf230_driver);
  1561. MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
  1562. MODULE_LICENSE("GPL v2");