at86rf230.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/gpio.h>
  25. #include <linux/delay.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/at86rf230.h>
  29. #include <linux/regmap.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/ieee802154.h>
  33. #include <net/mac802154.h>
  34. #include <net/cfg802154.h>
  35. struct at86rf230_local;
  36. /* at86rf2xx chip depend data.
  37. * All timings are in us.
  38. */
  39. struct at86rf2xx_chip_data {
  40. u16 t_sleep_cycle;
  41. u16 t_channel_switch;
  42. u16 t_reset_to_off;
  43. u16 t_off_to_aack;
  44. u16 t_off_to_tx_on;
  45. u16 t_frame;
  46. u16 t_p_ack;
  47. /* completion timeout for tx in msecs */
  48. u16 t_tx_timeout;
  49. int rssi_base_val;
  50. int (*set_channel)(struct at86rf230_local *, u8, u8);
  51. int (*get_desense_steps)(struct at86rf230_local *, s32);
  52. };
  53. #define AT86RF2XX_MAX_BUF (127 + 3)
  54. struct at86rf230_state_change {
  55. struct at86rf230_local *lp;
  56. struct spi_message msg;
  57. struct spi_transfer trx;
  58. u8 buf[AT86RF2XX_MAX_BUF];
  59. void (*complete)(void *context);
  60. u8 from_state;
  61. u8 to_state;
  62. bool irq_enable;
  63. };
  64. struct at86rf230_local {
  65. struct spi_device *spi;
  66. struct ieee802154_hw *hw;
  67. struct at86rf2xx_chip_data *data;
  68. struct regmap *regmap;
  69. struct completion state_complete;
  70. struct at86rf230_state_change state;
  71. struct at86rf230_state_change irq;
  72. bool tx_aret;
  73. s8 max_frame_retries;
  74. bool is_tx;
  75. /* spinlock for is_tx protection */
  76. spinlock_t lock;
  77. struct sk_buff *tx_skb;
  78. struct at86rf230_state_change tx;
  79. };
  80. #define RG_TRX_STATUS (0x01)
  81. #define SR_TRX_STATUS 0x01, 0x1f, 0
  82. #define SR_RESERVED_01_3 0x01, 0x20, 5
  83. #define SR_CCA_STATUS 0x01, 0x40, 6
  84. #define SR_CCA_DONE 0x01, 0x80, 7
  85. #define RG_TRX_STATE (0x02)
  86. #define SR_TRX_CMD 0x02, 0x1f, 0
  87. #define SR_TRAC_STATUS 0x02, 0xe0, 5
  88. #define RG_TRX_CTRL_0 (0x03)
  89. #define SR_CLKM_CTRL 0x03, 0x07, 0
  90. #define SR_CLKM_SHA_SEL 0x03, 0x08, 3
  91. #define SR_PAD_IO_CLKM 0x03, 0x30, 4
  92. #define SR_PAD_IO 0x03, 0xc0, 6
  93. #define RG_TRX_CTRL_1 (0x04)
  94. #define SR_IRQ_POLARITY 0x04, 0x01, 0
  95. #define SR_IRQ_MASK_MODE 0x04, 0x02, 1
  96. #define SR_SPI_CMD_MODE 0x04, 0x0c, 2
  97. #define SR_RX_BL_CTRL 0x04, 0x10, 4
  98. #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
  99. #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
  100. #define SR_PA_EXT_EN 0x04, 0x80, 7
  101. #define RG_PHY_TX_PWR (0x05)
  102. #define SR_TX_PWR 0x05, 0x0f, 0
  103. #define SR_PA_LT 0x05, 0x30, 4
  104. #define SR_PA_BUF_LT 0x05, 0xc0, 6
  105. #define RG_PHY_RSSI (0x06)
  106. #define SR_RSSI 0x06, 0x1f, 0
  107. #define SR_RND_VALUE 0x06, 0x60, 5
  108. #define SR_RX_CRC_VALID 0x06, 0x80, 7
  109. #define RG_PHY_ED_LEVEL (0x07)
  110. #define SR_ED_LEVEL 0x07, 0xff, 0
  111. #define RG_PHY_CC_CCA (0x08)
  112. #define SR_CHANNEL 0x08, 0x1f, 0
  113. #define SR_CCA_MODE 0x08, 0x60, 5
  114. #define SR_CCA_REQUEST 0x08, 0x80, 7
  115. #define RG_CCA_THRES (0x09)
  116. #define SR_CCA_ED_THRES 0x09, 0x0f, 0
  117. #define SR_RESERVED_09_1 0x09, 0xf0, 4
  118. #define RG_RX_CTRL (0x0a)
  119. #define SR_PDT_THRES 0x0a, 0x0f, 0
  120. #define SR_RESERVED_0a_1 0x0a, 0xf0, 4
  121. #define RG_SFD_VALUE (0x0b)
  122. #define SR_SFD_VALUE 0x0b, 0xff, 0
  123. #define RG_TRX_CTRL_2 (0x0c)
  124. #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
  125. #define SR_SUB_MODE 0x0c, 0x04, 2
  126. #define SR_BPSK_QPSK 0x0c, 0x08, 3
  127. #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
  128. #define SR_RESERVED_0c_5 0x0c, 0x60, 5
  129. #define SR_RX_SAFE_MODE 0x0c, 0x80, 7
  130. #define RG_ANT_DIV (0x0d)
  131. #define SR_ANT_CTRL 0x0d, 0x03, 0
  132. #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
  133. #define SR_ANT_DIV_EN 0x0d, 0x08, 3
  134. #define SR_RESERVED_0d_2 0x0d, 0x70, 4
  135. #define SR_ANT_SEL 0x0d, 0x80, 7
  136. #define RG_IRQ_MASK (0x0e)
  137. #define SR_IRQ_MASK 0x0e, 0xff, 0
  138. #define RG_IRQ_STATUS (0x0f)
  139. #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
  140. #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
  141. #define SR_IRQ_2_RX_START 0x0f, 0x04, 2
  142. #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
  143. #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
  144. #define SR_IRQ_5_AMI 0x0f, 0x20, 5
  145. #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
  146. #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
  147. #define RG_VREG_CTRL (0x10)
  148. #define SR_RESERVED_10_6 0x10, 0x03, 0
  149. #define SR_DVDD_OK 0x10, 0x04, 2
  150. #define SR_DVREG_EXT 0x10, 0x08, 3
  151. #define SR_RESERVED_10_3 0x10, 0x30, 4
  152. #define SR_AVDD_OK 0x10, 0x40, 6
  153. #define SR_AVREG_EXT 0x10, 0x80, 7
  154. #define RG_BATMON (0x11)
  155. #define SR_BATMON_VTH 0x11, 0x0f, 0
  156. #define SR_BATMON_HR 0x11, 0x10, 4
  157. #define SR_BATMON_OK 0x11, 0x20, 5
  158. #define SR_RESERVED_11_1 0x11, 0xc0, 6
  159. #define RG_XOSC_CTRL (0x12)
  160. #define SR_XTAL_TRIM 0x12, 0x0f, 0
  161. #define SR_XTAL_MODE 0x12, 0xf0, 4
  162. #define RG_RX_SYN (0x15)
  163. #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
  164. #define SR_RESERVED_15_2 0x15, 0x70, 4
  165. #define SR_RX_PDT_DIS 0x15, 0x80, 7
  166. #define RG_XAH_CTRL_1 (0x17)
  167. #define SR_RESERVED_17_8 0x17, 0x01, 0
  168. #define SR_AACK_PROM_MODE 0x17, 0x02, 1
  169. #define SR_AACK_ACK_TIME 0x17, 0x04, 2
  170. #define SR_RESERVED_17_5 0x17, 0x08, 3
  171. #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
  172. #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
  173. #define SR_CSMA_LBT_MODE 0x17, 0x40, 6
  174. #define SR_RESERVED_17_1 0x17, 0x80, 7
  175. #define RG_FTN_CTRL (0x18)
  176. #define SR_RESERVED_18_2 0x18, 0x7f, 0
  177. #define SR_FTN_START 0x18, 0x80, 7
  178. #define RG_PLL_CF (0x1a)
  179. #define SR_RESERVED_1a_2 0x1a, 0x7f, 0
  180. #define SR_PLL_CF_START 0x1a, 0x80, 7
  181. #define RG_PLL_DCU (0x1b)
  182. #define SR_RESERVED_1b_3 0x1b, 0x3f, 0
  183. #define SR_RESERVED_1b_2 0x1b, 0x40, 6
  184. #define SR_PLL_DCU_START 0x1b, 0x80, 7
  185. #define RG_PART_NUM (0x1c)
  186. #define SR_PART_NUM 0x1c, 0xff, 0
  187. #define RG_VERSION_NUM (0x1d)
  188. #define SR_VERSION_NUM 0x1d, 0xff, 0
  189. #define RG_MAN_ID_0 (0x1e)
  190. #define SR_MAN_ID_0 0x1e, 0xff, 0
  191. #define RG_MAN_ID_1 (0x1f)
  192. #define SR_MAN_ID_1 0x1f, 0xff, 0
  193. #define RG_SHORT_ADDR_0 (0x20)
  194. #define SR_SHORT_ADDR_0 0x20, 0xff, 0
  195. #define RG_SHORT_ADDR_1 (0x21)
  196. #define SR_SHORT_ADDR_1 0x21, 0xff, 0
  197. #define RG_PAN_ID_0 (0x22)
  198. #define SR_PAN_ID_0 0x22, 0xff, 0
  199. #define RG_PAN_ID_1 (0x23)
  200. #define SR_PAN_ID_1 0x23, 0xff, 0
  201. #define RG_IEEE_ADDR_0 (0x24)
  202. #define SR_IEEE_ADDR_0 0x24, 0xff, 0
  203. #define RG_IEEE_ADDR_1 (0x25)
  204. #define SR_IEEE_ADDR_1 0x25, 0xff, 0
  205. #define RG_IEEE_ADDR_2 (0x26)
  206. #define SR_IEEE_ADDR_2 0x26, 0xff, 0
  207. #define RG_IEEE_ADDR_3 (0x27)
  208. #define SR_IEEE_ADDR_3 0x27, 0xff, 0
  209. #define RG_IEEE_ADDR_4 (0x28)
  210. #define SR_IEEE_ADDR_4 0x28, 0xff, 0
  211. #define RG_IEEE_ADDR_5 (0x29)
  212. #define SR_IEEE_ADDR_5 0x29, 0xff, 0
  213. #define RG_IEEE_ADDR_6 (0x2a)
  214. #define SR_IEEE_ADDR_6 0x2a, 0xff, 0
  215. #define RG_IEEE_ADDR_7 (0x2b)
  216. #define SR_IEEE_ADDR_7 0x2b, 0xff, 0
  217. #define RG_XAH_CTRL_0 (0x2c)
  218. #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
  219. #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
  220. #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
  221. #define RG_CSMA_SEED_0 (0x2d)
  222. #define SR_CSMA_SEED_0 0x2d, 0xff, 0
  223. #define RG_CSMA_SEED_1 (0x2e)
  224. #define SR_CSMA_SEED_1 0x2e, 0x07, 0
  225. #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
  226. #define SR_AACK_DIS_ACK 0x2e, 0x10, 4
  227. #define SR_AACK_SET_PD 0x2e, 0x20, 5
  228. #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
  229. #define RG_CSMA_BE (0x2f)
  230. #define SR_MIN_BE 0x2f, 0x0f, 0
  231. #define SR_MAX_BE 0x2f, 0xf0, 4
  232. #define CMD_REG 0x80
  233. #define CMD_REG_MASK 0x3f
  234. #define CMD_WRITE 0x40
  235. #define CMD_FB 0x20
  236. #define IRQ_BAT_LOW (1 << 7)
  237. #define IRQ_TRX_UR (1 << 6)
  238. #define IRQ_AMI (1 << 5)
  239. #define IRQ_CCA_ED (1 << 4)
  240. #define IRQ_TRX_END (1 << 3)
  241. #define IRQ_RX_START (1 << 2)
  242. #define IRQ_PLL_UNL (1 << 1)
  243. #define IRQ_PLL_LOCK (1 << 0)
  244. #define IRQ_ACTIVE_HIGH 0
  245. #define IRQ_ACTIVE_LOW 1
  246. #define STATE_P_ON 0x00 /* BUSY */
  247. #define STATE_BUSY_RX 0x01
  248. #define STATE_BUSY_TX 0x02
  249. #define STATE_FORCE_TRX_OFF 0x03
  250. #define STATE_FORCE_TX_ON 0x04 /* IDLE */
  251. /* 0x05 */ /* INVALID_PARAMETER */
  252. #define STATE_RX_ON 0x06
  253. /* 0x07 */ /* SUCCESS */
  254. #define STATE_TRX_OFF 0x08
  255. #define STATE_TX_ON 0x09
  256. /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
  257. #define STATE_SLEEP 0x0F
  258. #define STATE_PREP_DEEP_SLEEP 0x10
  259. #define STATE_BUSY_RX_AACK 0x11
  260. #define STATE_BUSY_TX_ARET 0x12
  261. #define STATE_RX_AACK_ON 0x16
  262. #define STATE_TX_ARET_ON 0x19
  263. #define STATE_RX_ON_NOCLK 0x1C
  264. #define STATE_RX_AACK_ON_NOCLK 0x1D
  265. #define STATE_BUSY_RX_AACK_NOCLK 0x1E
  266. #define STATE_TRANSITION_IN_PROGRESS 0x1F
  267. #define AT86RF2XX_NUMREGS 0x3F
  268. static void
  269. at86rf230_async_state_change(struct at86rf230_local *lp,
  270. struct at86rf230_state_change *ctx,
  271. const u8 state, void (*complete)(void *context),
  272. const bool irq_enable);
  273. static inline int
  274. __at86rf230_write(struct at86rf230_local *lp,
  275. unsigned int addr, unsigned int data)
  276. {
  277. return regmap_write(lp->regmap, addr, data);
  278. }
  279. static inline int
  280. __at86rf230_read(struct at86rf230_local *lp,
  281. unsigned int addr, unsigned int *data)
  282. {
  283. return regmap_read(lp->regmap, addr, data);
  284. }
  285. static inline int
  286. at86rf230_read_subreg(struct at86rf230_local *lp,
  287. unsigned int addr, unsigned int mask,
  288. unsigned int shift, unsigned int *data)
  289. {
  290. int rc;
  291. rc = __at86rf230_read(lp, addr, data);
  292. if (rc > 0)
  293. *data = (*data & mask) >> shift;
  294. return rc;
  295. }
  296. static inline int
  297. at86rf230_write_subreg(struct at86rf230_local *lp,
  298. unsigned int addr, unsigned int mask,
  299. unsigned int shift, unsigned int data)
  300. {
  301. return regmap_update_bits(lp->regmap, addr, mask, data << shift);
  302. }
  303. static bool
  304. at86rf230_reg_writeable(struct device *dev, unsigned int reg)
  305. {
  306. switch (reg) {
  307. case RG_TRX_STATE:
  308. case RG_TRX_CTRL_0:
  309. case RG_TRX_CTRL_1:
  310. case RG_PHY_TX_PWR:
  311. case RG_PHY_ED_LEVEL:
  312. case RG_PHY_CC_CCA:
  313. case RG_CCA_THRES:
  314. case RG_RX_CTRL:
  315. case RG_SFD_VALUE:
  316. case RG_TRX_CTRL_2:
  317. case RG_ANT_DIV:
  318. case RG_IRQ_MASK:
  319. case RG_VREG_CTRL:
  320. case RG_BATMON:
  321. case RG_XOSC_CTRL:
  322. case RG_RX_SYN:
  323. case RG_XAH_CTRL_1:
  324. case RG_FTN_CTRL:
  325. case RG_PLL_CF:
  326. case RG_PLL_DCU:
  327. case RG_SHORT_ADDR_0:
  328. case RG_SHORT_ADDR_1:
  329. case RG_PAN_ID_0:
  330. case RG_PAN_ID_1:
  331. case RG_IEEE_ADDR_0:
  332. case RG_IEEE_ADDR_1:
  333. case RG_IEEE_ADDR_2:
  334. case RG_IEEE_ADDR_3:
  335. case RG_IEEE_ADDR_4:
  336. case RG_IEEE_ADDR_5:
  337. case RG_IEEE_ADDR_6:
  338. case RG_IEEE_ADDR_7:
  339. case RG_XAH_CTRL_0:
  340. case RG_CSMA_SEED_0:
  341. case RG_CSMA_SEED_1:
  342. case RG_CSMA_BE:
  343. return true;
  344. default:
  345. return false;
  346. }
  347. }
  348. static bool
  349. at86rf230_reg_readable(struct device *dev, unsigned int reg)
  350. {
  351. bool rc;
  352. /* all writeable are also readable */
  353. rc = at86rf230_reg_writeable(dev, reg);
  354. if (rc)
  355. return rc;
  356. /* readonly regs */
  357. switch (reg) {
  358. case RG_TRX_STATUS:
  359. case RG_PHY_RSSI:
  360. case RG_IRQ_STATUS:
  361. case RG_PART_NUM:
  362. case RG_VERSION_NUM:
  363. case RG_MAN_ID_1:
  364. case RG_MAN_ID_0:
  365. return true;
  366. default:
  367. return false;
  368. }
  369. }
  370. static bool
  371. at86rf230_reg_volatile(struct device *dev, unsigned int reg)
  372. {
  373. /* can be changed during runtime */
  374. switch (reg) {
  375. case RG_TRX_STATUS:
  376. case RG_TRX_STATE:
  377. case RG_PHY_RSSI:
  378. case RG_PHY_ED_LEVEL:
  379. case RG_IRQ_STATUS:
  380. case RG_VREG_CTRL:
  381. return true;
  382. default:
  383. return false;
  384. }
  385. }
  386. static bool
  387. at86rf230_reg_precious(struct device *dev, unsigned int reg)
  388. {
  389. /* don't clear irq line on read */
  390. switch (reg) {
  391. case RG_IRQ_STATUS:
  392. return true;
  393. default:
  394. return false;
  395. }
  396. }
  397. static const struct regmap_config at86rf230_regmap_spi_config = {
  398. .reg_bits = 8,
  399. .val_bits = 8,
  400. .write_flag_mask = CMD_REG | CMD_WRITE,
  401. .read_flag_mask = CMD_REG,
  402. .cache_type = REGCACHE_RBTREE,
  403. .max_register = AT86RF2XX_NUMREGS,
  404. .writeable_reg = at86rf230_reg_writeable,
  405. .readable_reg = at86rf230_reg_readable,
  406. .volatile_reg = at86rf230_reg_volatile,
  407. .precious_reg = at86rf230_reg_precious,
  408. };
  409. static void
  410. at86rf230_async_error_recover(void *context)
  411. {
  412. struct at86rf230_state_change *ctx = context;
  413. struct at86rf230_local *lp = ctx->lp;
  414. at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
  415. ieee802154_wake_queue(lp->hw);
  416. }
  417. static inline void
  418. at86rf230_async_error(struct at86rf230_local *lp,
  419. struct at86rf230_state_change *ctx, int rc)
  420. {
  421. dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
  422. at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
  423. at86rf230_async_error_recover, false);
  424. }
  425. /* Generic function to get some register value in async mode */
  426. static void
  427. at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
  428. struct at86rf230_state_change *ctx,
  429. void (*complete)(void *context),
  430. const bool irq_enable)
  431. {
  432. int rc;
  433. u8 *tx_buf = ctx->buf;
  434. tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
  435. ctx->trx.len = 2;
  436. ctx->msg.complete = complete;
  437. ctx->irq_enable = irq_enable;
  438. rc = spi_async(lp->spi, &ctx->msg);
  439. if (rc) {
  440. if (irq_enable)
  441. enable_irq(lp->spi->irq);
  442. at86rf230_async_error(lp, ctx, rc);
  443. }
  444. }
  445. static void
  446. at86rf230_async_state_assert(void *context)
  447. {
  448. struct at86rf230_state_change *ctx = context;
  449. struct at86rf230_local *lp = ctx->lp;
  450. const u8 *buf = ctx->buf;
  451. const u8 trx_state = buf[1] & 0x1f;
  452. /* Assert state change */
  453. if (trx_state != ctx->to_state) {
  454. /* Special handling if transceiver state is in
  455. * STATE_BUSY_RX_AACK and a SHR was detected.
  456. */
  457. if (trx_state == STATE_BUSY_RX_AACK) {
  458. /* Undocumented race condition. If we send a state
  459. * change to STATE_RX_AACK_ON the transceiver could
  460. * change his state automatically to STATE_BUSY_RX_AACK
  461. * if a SHR was detected. This is not an error, but we
  462. * can't assert this.
  463. */
  464. if (ctx->to_state == STATE_RX_AACK_ON)
  465. goto done;
  466. /* If we change to STATE_TX_ON without forcing and
  467. * transceiver state is STATE_BUSY_RX_AACK, we wait
  468. * 'tFrame + tPAck' receiving time. In this time the
  469. * PDU should be received. If the transceiver is still
  470. * in STATE_BUSY_RX_AACK, we run a force state change
  471. * to STATE_TX_ON. This is a timeout handling, if the
  472. * transceiver stucks in STATE_BUSY_RX_AACK.
  473. */
  474. if (ctx->to_state == STATE_TX_ON) {
  475. at86rf230_async_state_change(lp, ctx,
  476. STATE_FORCE_TX_ON,
  477. ctx->complete,
  478. ctx->irq_enable);
  479. return;
  480. }
  481. }
  482. dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
  483. ctx->from_state, ctx->to_state, trx_state);
  484. }
  485. done:
  486. if (ctx->complete)
  487. ctx->complete(context);
  488. }
  489. /* Do state change timing delay. */
  490. static void
  491. at86rf230_async_state_delay(void *context)
  492. {
  493. struct at86rf230_state_change *ctx = context;
  494. struct at86rf230_local *lp = ctx->lp;
  495. struct at86rf2xx_chip_data *c = lp->data;
  496. bool force = false;
  497. /* The force state changes are will show as normal states in the
  498. * state status subregister. We change the to_state to the
  499. * corresponding one and remember if it was a force change, this
  500. * differs if we do a state change from STATE_BUSY_RX_AACK.
  501. */
  502. switch (ctx->to_state) {
  503. case STATE_FORCE_TX_ON:
  504. ctx->to_state = STATE_TX_ON;
  505. force = true;
  506. break;
  507. case STATE_FORCE_TRX_OFF:
  508. ctx->to_state = STATE_TRX_OFF;
  509. force = true;
  510. break;
  511. default:
  512. break;
  513. }
  514. switch (ctx->from_state) {
  515. case STATE_TRX_OFF:
  516. switch (ctx->to_state) {
  517. case STATE_RX_AACK_ON:
  518. usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
  519. goto change;
  520. case STATE_TX_ON:
  521. usleep_range(c->t_off_to_tx_on,
  522. c->t_off_to_tx_on + 10);
  523. goto change;
  524. default:
  525. break;
  526. }
  527. break;
  528. case STATE_BUSY_RX_AACK:
  529. switch (ctx->to_state) {
  530. case STATE_TX_ON:
  531. /* Wait for worst case receiving time if we
  532. * didn't make a force change from BUSY_RX_AACK
  533. * to TX_ON.
  534. */
  535. if (!force) {
  536. usleep_range(c->t_frame + c->t_p_ack,
  537. c->t_frame + c->t_p_ack + 1000);
  538. goto change;
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. break;
  545. /* Default value, means RESET state */
  546. case STATE_P_ON:
  547. switch (ctx->to_state) {
  548. case STATE_TRX_OFF:
  549. usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
  550. goto change;
  551. default:
  552. break;
  553. }
  554. break;
  555. default:
  556. break;
  557. }
  558. /* Default delay is 1us in the most cases */
  559. udelay(1);
  560. change:
  561. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  562. at86rf230_async_state_assert,
  563. ctx->irq_enable);
  564. }
  565. static void
  566. at86rf230_async_state_change_start(void *context)
  567. {
  568. struct at86rf230_state_change *ctx = context;
  569. struct at86rf230_local *lp = ctx->lp;
  570. u8 *buf = ctx->buf;
  571. const u8 trx_state = buf[1] & 0x1f;
  572. int rc;
  573. /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
  574. if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
  575. udelay(1);
  576. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  577. at86rf230_async_state_change_start,
  578. ctx->irq_enable);
  579. return;
  580. }
  581. /* Check if we already are in the state which we change in */
  582. if (trx_state == ctx->to_state) {
  583. if (ctx->complete)
  584. ctx->complete(context);
  585. return;
  586. }
  587. /* Set current state to the context of state change */
  588. ctx->from_state = trx_state;
  589. /* Going into the next step for a state change which do a timing
  590. * relevant delay.
  591. */
  592. buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  593. buf[1] = ctx->to_state;
  594. ctx->trx.len = 2;
  595. ctx->msg.complete = at86rf230_async_state_delay;
  596. rc = spi_async(lp->spi, &ctx->msg);
  597. if (rc) {
  598. if (ctx->irq_enable)
  599. enable_irq(lp->spi->irq);
  600. at86rf230_async_error(lp, ctx, rc);
  601. }
  602. }
  603. static void
  604. at86rf230_async_state_change(struct at86rf230_local *lp,
  605. struct at86rf230_state_change *ctx,
  606. const u8 state, void (*complete)(void *context),
  607. const bool irq_enable)
  608. {
  609. /* Initialization for the state change context */
  610. ctx->to_state = state;
  611. ctx->complete = complete;
  612. ctx->irq_enable = irq_enable;
  613. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  614. at86rf230_async_state_change_start,
  615. irq_enable);
  616. }
  617. static void
  618. at86rf230_sync_state_change_complete(void *context)
  619. {
  620. struct at86rf230_state_change *ctx = context;
  621. struct at86rf230_local *lp = ctx->lp;
  622. complete(&lp->state_complete);
  623. }
  624. /* This function do a sync framework above the async state change.
  625. * Some callbacks of the IEEE 802.15.4 driver interface need to be
  626. * handled synchronously.
  627. */
  628. static int
  629. at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
  630. {
  631. int rc;
  632. at86rf230_async_state_change(lp, &lp->state, state,
  633. at86rf230_sync_state_change_complete,
  634. false);
  635. rc = wait_for_completion_timeout(&lp->state_complete,
  636. msecs_to_jiffies(100));
  637. if (!rc) {
  638. at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
  639. return -ETIMEDOUT;
  640. }
  641. return 0;
  642. }
  643. static void
  644. at86rf230_tx_complete(void *context)
  645. {
  646. struct at86rf230_state_change *ctx = context;
  647. struct at86rf230_local *lp = ctx->lp;
  648. struct sk_buff *skb = lp->tx_skb;
  649. enable_irq(lp->spi->irq);
  650. ieee802154_xmit_complete(lp->hw, skb, !lp->tx_aret);
  651. }
  652. static void
  653. at86rf230_tx_on(void *context)
  654. {
  655. struct at86rf230_state_change *ctx = context;
  656. struct at86rf230_local *lp = ctx->lp;
  657. at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
  658. at86rf230_tx_complete, true);
  659. }
  660. static void
  661. at86rf230_tx_trac_error(void *context)
  662. {
  663. struct at86rf230_state_change *ctx = context;
  664. struct at86rf230_local *lp = ctx->lp;
  665. at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
  666. at86rf230_tx_on, true);
  667. }
  668. static void
  669. at86rf230_tx_trac_check(void *context)
  670. {
  671. struct at86rf230_state_change *ctx = context;
  672. struct at86rf230_local *lp = ctx->lp;
  673. const u8 *buf = ctx->buf;
  674. const u8 trac = (buf[1] & 0xe0) >> 5;
  675. /* If trac status is different than zero we need to do a state change
  676. * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
  677. * state to TX_ON.
  678. */
  679. if (trac)
  680. at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
  681. at86rf230_tx_trac_error, true);
  682. else
  683. at86rf230_tx_on(context);
  684. }
  685. static void
  686. at86rf230_tx_trac_status(void *context)
  687. {
  688. struct at86rf230_state_change *ctx = context;
  689. struct at86rf230_local *lp = ctx->lp;
  690. at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
  691. at86rf230_tx_trac_check, true);
  692. }
  693. static void
  694. at86rf230_rx(struct at86rf230_local *lp,
  695. const u8 *data, const u8 len, const u8 lqi)
  696. {
  697. struct sk_buff *skb;
  698. u8 rx_local_buf[AT86RF2XX_MAX_BUF];
  699. memcpy(rx_local_buf, data, len);
  700. enable_irq(lp->spi->irq);
  701. skb = dev_alloc_skb(IEEE802154_MTU);
  702. if (!skb) {
  703. dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
  704. return;
  705. }
  706. memcpy(skb_put(skb, len), rx_local_buf, len);
  707. ieee802154_rx_irqsafe(lp->hw, skb, lqi);
  708. }
  709. static void
  710. at86rf230_rx_read_frame_complete(void *context)
  711. {
  712. struct at86rf230_state_change *ctx = context;
  713. struct at86rf230_local *lp = ctx->lp;
  714. const u8 *buf = lp->irq.buf;
  715. u8 len = buf[1];
  716. if (!ieee802154_is_valid_psdu_len(len)) {
  717. dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
  718. len = IEEE802154_MTU;
  719. }
  720. at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
  721. }
  722. static void
  723. at86rf230_rx_read_frame(struct at86rf230_local *lp)
  724. {
  725. int rc;
  726. u8 *buf = lp->irq.buf;
  727. buf[0] = CMD_FB;
  728. lp->irq.trx.len = AT86RF2XX_MAX_BUF;
  729. lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
  730. rc = spi_async(lp->spi, &lp->irq.msg);
  731. if (rc) {
  732. enable_irq(lp->spi->irq);
  733. at86rf230_async_error(lp, &lp->irq, rc);
  734. }
  735. }
  736. static void
  737. at86rf230_rx_trac_check(void *context)
  738. {
  739. struct at86rf230_state_change *ctx = context;
  740. struct at86rf230_local *lp = ctx->lp;
  741. /* Possible check on trac status here. This could be useful to make
  742. * some stats why receive is failed. Not used at the moment, but it's
  743. * maybe timing relevant. Datasheet doesn't say anything about this.
  744. * The programming guide say do it so.
  745. */
  746. at86rf230_rx_read_frame(lp);
  747. }
  748. static void
  749. at86rf230_irq_trx_end(struct at86rf230_local *lp)
  750. {
  751. spin_lock(&lp->lock);
  752. if (lp->is_tx) {
  753. lp->is_tx = 0;
  754. spin_unlock(&lp->lock);
  755. if (lp->tx_aret)
  756. at86rf230_async_state_change(lp, &lp->irq,
  757. STATE_FORCE_TX_ON,
  758. at86rf230_tx_trac_status,
  759. true);
  760. else
  761. at86rf230_async_state_change(lp, &lp->irq,
  762. STATE_RX_AACK_ON,
  763. at86rf230_tx_complete,
  764. true);
  765. } else {
  766. spin_unlock(&lp->lock);
  767. at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
  768. at86rf230_rx_trac_check, true);
  769. }
  770. }
  771. static void
  772. at86rf230_irq_status(void *context)
  773. {
  774. struct at86rf230_state_change *ctx = context;
  775. struct at86rf230_local *lp = ctx->lp;
  776. const u8 *buf = lp->irq.buf;
  777. const u8 irq = buf[1];
  778. if (irq & IRQ_TRX_END) {
  779. at86rf230_irq_trx_end(lp);
  780. } else {
  781. enable_irq(lp->spi->irq);
  782. dev_err(&lp->spi->dev, "not supported irq %02x received\n",
  783. irq);
  784. }
  785. }
  786. static irqreturn_t at86rf230_isr(int irq, void *data)
  787. {
  788. struct at86rf230_local *lp = data;
  789. struct at86rf230_state_change *ctx = &lp->irq;
  790. u8 *buf = ctx->buf;
  791. int rc;
  792. disable_irq_nosync(irq);
  793. buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
  794. ctx->trx.len = 2;
  795. ctx->msg.complete = at86rf230_irq_status;
  796. rc = spi_async(lp->spi, &ctx->msg);
  797. if (rc) {
  798. enable_irq(irq);
  799. at86rf230_async_error(lp, ctx, rc);
  800. return IRQ_NONE;
  801. }
  802. return IRQ_HANDLED;
  803. }
  804. static void
  805. at86rf230_write_frame_complete(void *context)
  806. {
  807. struct at86rf230_state_change *ctx = context;
  808. struct at86rf230_local *lp = ctx->lp;
  809. u8 *buf = ctx->buf;
  810. int rc;
  811. buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  812. buf[1] = STATE_BUSY_TX;
  813. ctx->trx.len = 2;
  814. ctx->msg.complete = NULL;
  815. rc = spi_async(lp->spi, &ctx->msg);
  816. if (rc)
  817. at86rf230_async_error(lp, ctx, rc);
  818. }
  819. static void
  820. at86rf230_write_frame(void *context)
  821. {
  822. struct at86rf230_state_change *ctx = context;
  823. struct at86rf230_local *lp = ctx->lp;
  824. struct sk_buff *skb = lp->tx_skb;
  825. u8 *buf = lp->tx.buf;
  826. int rc;
  827. spin_lock(&lp->lock);
  828. lp->is_tx = 1;
  829. spin_unlock(&lp->lock);
  830. buf[0] = CMD_FB | CMD_WRITE;
  831. buf[1] = skb->len + 2;
  832. memcpy(buf + 2, skb->data, skb->len);
  833. lp->tx.trx.len = skb->len + 2;
  834. lp->tx.msg.complete = at86rf230_write_frame_complete;
  835. rc = spi_async(lp->spi, &lp->tx.msg);
  836. if (rc)
  837. at86rf230_async_error(lp, ctx, rc);
  838. }
  839. static void
  840. at86rf230_xmit_tx_on(void *context)
  841. {
  842. struct at86rf230_state_change *ctx = context;
  843. struct at86rf230_local *lp = ctx->lp;
  844. at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
  845. at86rf230_write_frame, false);
  846. }
  847. static int
  848. at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  849. {
  850. struct at86rf230_local *lp = hw->priv;
  851. struct at86rf230_state_change *ctx = &lp->tx;
  852. void (*tx_complete)(void *context) = at86rf230_write_frame;
  853. lp->tx_skb = skb;
  854. /* In ARET mode we need to go into STATE_TX_ARET_ON after we
  855. * are in STATE_TX_ON. The pfad differs here, so we change
  856. * the complete handler.
  857. */
  858. if (lp->tx_aret)
  859. tx_complete = at86rf230_xmit_tx_on;
  860. at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
  861. return 0;
  862. }
  863. static int
  864. at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
  865. {
  866. BUG_ON(!level);
  867. *level = 0xbe;
  868. return 0;
  869. }
  870. static int
  871. at86rf230_start(struct ieee802154_hw *hw)
  872. {
  873. return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
  874. }
  875. static void
  876. at86rf230_stop(struct ieee802154_hw *hw)
  877. {
  878. at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
  879. }
  880. static int
  881. at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  882. {
  883. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  884. }
  885. static int
  886. at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  887. {
  888. int rc;
  889. if (channel == 0)
  890. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
  891. else
  892. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
  893. if (rc < 0)
  894. return rc;
  895. if (page == 0) {
  896. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
  897. lp->data->rssi_base_val = -100;
  898. } else {
  899. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
  900. lp->data->rssi_base_val = -98;
  901. }
  902. if (rc < 0)
  903. return rc;
  904. /* This sets the symbol_duration according frequency on the 212.
  905. * TODO move this handling while set channel and page in cfg802154.
  906. * We can do that, this timings are according 802.15.4 standard.
  907. * If we do that in cfg802154, this is a more generic calculation.
  908. *
  909. * This should also protected from ifs_timer. Means cancel timer and
  910. * init with a new value. For now, this is okay.
  911. */
  912. if (channel == 0) {
  913. if (page == 0) {
  914. /* SUB:0 and BPSK:0 -> BPSK-20 */
  915. lp->hw->phy->symbol_duration = 50;
  916. } else {
  917. /* SUB:1 and BPSK:0 -> BPSK-40 */
  918. lp->hw->phy->symbol_duration = 25;
  919. }
  920. } else {
  921. if (page == 0)
  922. /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
  923. lp->hw->phy->symbol_duration = 40;
  924. else
  925. /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
  926. lp->hw->phy->symbol_duration = 16;
  927. }
  928. lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
  929. lp->hw->phy->symbol_duration;
  930. lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
  931. lp->hw->phy->symbol_duration;
  932. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  933. }
  934. static int
  935. at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  936. {
  937. struct at86rf230_local *lp = hw->priv;
  938. int rc;
  939. rc = lp->data->set_channel(lp, page, channel);
  940. /* Wait for PLL */
  941. usleep_range(lp->data->t_channel_switch,
  942. lp->data->t_channel_switch + 10);
  943. return rc;
  944. }
  945. static int
  946. at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
  947. struct ieee802154_hw_addr_filt *filt,
  948. unsigned long changed)
  949. {
  950. struct at86rf230_local *lp = hw->priv;
  951. if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
  952. u16 addr = le16_to_cpu(filt->short_addr);
  953. dev_vdbg(&lp->spi->dev,
  954. "at86rf230_set_hw_addr_filt called for saddr\n");
  955. __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
  956. __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
  957. }
  958. if (changed & IEEE802154_AFILT_PANID_CHANGED) {
  959. u16 pan = le16_to_cpu(filt->pan_id);
  960. dev_vdbg(&lp->spi->dev,
  961. "at86rf230_set_hw_addr_filt called for pan id\n");
  962. __at86rf230_write(lp, RG_PAN_ID_0, pan);
  963. __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
  964. }
  965. if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
  966. u8 i, addr[8];
  967. memcpy(addr, &filt->ieee_addr, 8);
  968. dev_vdbg(&lp->spi->dev,
  969. "at86rf230_set_hw_addr_filt called for IEEE addr\n");
  970. for (i = 0; i < 8; i++)
  971. __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
  972. }
  973. if (changed & IEEE802154_AFILT_PANC_CHANGED) {
  974. dev_vdbg(&lp->spi->dev,
  975. "at86rf230_set_hw_addr_filt called for panc change\n");
  976. if (filt->pan_coord)
  977. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
  978. else
  979. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
  980. }
  981. return 0;
  982. }
  983. static int
  984. at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
  985. {
  986. struct at86rf230_local *lp = hw->priv;
  987. /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
  988. * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
  989. * 0dB.
  990. * thus, supported values for db range from -26 to 5, for 31dB of
  991. * reduction to 0dB of reduction.
  992. */
  993. if (db > 5 || db < -26)
  994. return -EINVAL;
  995. db = -(db - 5);
  996. return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
  997. }
  998. static int
  999. at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
  1000. {
  1001. struct at86rf230_local *lp = hw->priv;
  1002. return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
  1003. }
  1004. static int
  1005. at86rf230_set_cca_mode(struct ieee802154_hw *hw,
  1006. const struct wpan_phy_cca *cca)
  1007. {
  1008. struct at86rf230_local *lp = hw->priv;
  1009. u8 val;
  1010. /* mapping 802.15.4 to driver spec */
  1011. switch (cca->mode) {
  1012. case NL802154_CCA_ENERGY:
  1013. val = 1;
  1014. break;
  1015. case NL802154_CCA_CARRIER:
  1016. val = 2;
  1017. break;
  1018. case NL802154_CCA_ENERGY_CARRIER:
  1019. switch (cca->opt) {
  1020. case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
  1021. val = 3;
  1022. break;
  1023. case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
  1024. val = 0;
  1025. break;
  1026. default:
  1027. return -EINVAL;
  1028. }
  1029. break;
  1030. default:
  1031. return -EINVAL;
  1032. }
  1033. return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
  1034. }
  1035. static int
  1036. at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
  1037. {
  1038. return (level - lp->data->rssi_base_val) * 100 / 207;
  1039. }
  1040. static int
  1041. at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
  1042. {
  1043. return (level - lp->data->rssi_base_val) / 2;
  1044. }
  1045. static int
  1046. at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
  1047. {
  1048. struct at86rf230_local *lp = hw->priv;
  1049. if (level < lp->data->rssi_base_val || level > 30)
  1050. return -EINVAL;
  1051. return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
  1052. lp->data->get_desense_steps(lp, level));
  1053. }
  1054. static int
  1055. at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
  1056. u8 retries)
  1057. {
  1058. struct at86rf230_local *lp = hw->priv;
  1059. int rc;
  1060. rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
  1061. if (rc)
  1062. return rc;
  1063. rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
  1064. if (rc)
  1065. return rc;
  1066. return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
  1067. }
  1068. static int
  1069. at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
  1070. {
  1071. struct at86rf230_local *lp = hw->priv;
  1072. int rc = 0;
  1073. lp->tx_aret = retries >= 0;
  1074. lp->max_frame_retries = retries;
  1075. if (retries >= 0)
  1076. rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
  1077. return rc;
  1078. }
  1079. static int
  1080. at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  1081. {
  1082. struct at86rf230_local *lp = hw->priv;
  1083. int rc;
  1084. if (on) {
  1085. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
  1086. if (rc < 0)
  1087. return rc;
  1088. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
  1089. if (rc < 0)
  1090. return rc;
  1091. } else {
  1092. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
  1093. if (rc < 0)
  1094. return rc;
  1095. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
  1096. if (rc < 0)
  1097. return rc;
  1098. }
  1099. return 0;
  1100. }
  1101. static const struct ieee802154_ops at86rf230_ops = {
  1102. .owner = THIS_MODULE,
  1103. .xmit_async = at86rf230_xmit,
  1104. .ed = at86rf230_ed,
  1105. .set_channel = at86rf230_channel,
  1106. .start = at86rf230_start,
  1107. .stop = at86rf230_stop,
  1108. .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
  1109. .set_txpower = at86rf230_set_txpower,
  1110. .set_lbt = at86rf230_set_lbt,
  1111. .set_cca_mode = at86rf230_set_cca_mode,
  1112. .set_cca_ed_level = at86rf230_set_cca_ed_level,
  1113. .set_csma_params = at86rf230_set_csma_params,
  1114. .set_frame_retries = at86rf230_set_frame_retries,
  1115. .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
  1116. };
  1117. static struct at86rf2xx_chip_data at86rf233_data = {
  1118. .t_sleep_cycle = 330,
  1119. .t_channel_switch = 11,
  1120. .t_reset_to_off = 26,
  1121. .t_off_to_aack = 80,
  1122. .t_off_to_tx_on = 80,
  1123. .t_frame = 4096,
  1124. .t_p_ack = 545,
  1125. .t_tx_timeout = 2000,
  1126. .rssi_base_val = -91,
  1127. .set_channel = at86rf23x_set_channel,
  1128. .get_desense_steps = at86rf23x_get_desens_steps
  1129. };
  1130. static struct at86rf2xx_chip_data at86rf231_data = {
  1131. .t_sleep_cycle = 330,
  1132. .t_channel_switch = 24,
  1133. .t_reset_to_off = 37,
  1134. .t_off_to_aack = 110,
  1135. .t_off_to_tx_on = 110,
  1136. .t_frame = 4096,
  1137. .t_p_ack = 545,
  1138. .t_tx_timeout = 2000,
  1139. .rssi_base_val = -91,
  1140. .set_channel = at86rf23x_set_channel,
  1141. .get_desense_steps = at86rf23x_get_desens_steps
  1142. };
  1143. static struct at86rf2xx_chip_data at86rf212_data = {
  1144. .t_sleep_cycle = 330,
  1145. .t_channel_switch = 11,
  1146. .t_reset_to_off = 26,
  1147. .t_off_to_aack = 200,
  1148. .t_off_to_tx_on = 200,
  1149. .t_frame = 4096,
  1150. .t_p_ack = 545,
  1151. .t_tx_timeout = 2000,
  1152. .rssi_base_val = -100,
  1153. .set_channel = at86rf212_set_channel,
  1154. .get_desense_steps = at86rf212_get_desens_steps
  1155. };
  1156. static int at86rf230_hw_init(struct at86rf230_local *lp)
  1157. {
  1158. int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
  1159. unsigned int dvdd;
  1160. u8 csma_seed[2];
  1161. rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
  1162. if (rc)
  1163. return rc;
  1164. irq_type = irq_get_trigger_type(lp->spi->irq);
  1165. if (irq_type == IRQ_TYPE_EDGE_FALLING)
  1166. irq_pol = IRQ_ACTIVE_LOW;
  1167. rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
  1168. if (rc)
  1169. return rc;
  1170. rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
  1171. if (rc)
  1172. return rc;
  1173. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
  1174. if (rc)
  1175. return rc;
  1176. get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
  1177. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
  1178. if (rc)
  1179. return rc;
  1180. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
  1181. if (rc)
  1182. return rc;
  1183. /* CLKM changes are applied immediately */
  1184. rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
  1185. if (rc)
  1186. return rc;
  1187. /* Turn CLKM Off */
  1188. rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
  1189. if (rc)
  1190. return rc;
  1191. /* Wait the next SLEEP cycle */
  1192. usleep_range(lp->data->t_sleep_cycle,
  1193. lp->data->t_sleep_cycle + 100);
  1194. rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
  1195. if (rc)
  1196. return rc;
  1197. if (!dvdd) {
  1198. dev_err(&lp->spi->dev, "DVDD error\n");
  1199. return -EINVAL;
  1200. }
  1201. /* Force setting slotted operation bit to 0. Sometimes the atben
  1202. * sets this bit and I don't know why. We set this always force
  1203. * to zero while probing.
  1204. */
  1205. return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
  1206. }
  1207. static struct at86rf230_platform_data *
  1208. at86rf230_get_pdata(struct spi_device *spi)
  1209. {
  1210. struct at86rf230_platform_data *pdata;
  1211. if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
  1212. return spi->dev.platform_data;
  1213. pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
  1214. if (!pdata)
  1215. goto done;
  1216. pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
  1217. pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
  1218. spi->dev.platform_data = pdata;
  1219. done:
  1220. return pdata;
  1221. }
  1222. static int
  1223. at86rf230_detect_device(struct at86rf230_local *lp)
  1224. {
  1225. unsigned int part, version, val;
  1226. u16 man_id = 0;
  1227. const char *chip;
  1228. int rc;
  1229. rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
  1230. if (rc)
  1231. return rc;
  1232. man_id |= val;
  1233. rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
  1234. if (rc)
  1235. return rc;
  1236. man_id |= (val << 8);
  1237. rc = __at86rf230_read(lp, RG_PART_NUM, &part);
  1238. if (rc)
  1239. return rc;
  1240. rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
  1241. if (rc)
  1242. return rc;
  1243. if (man_id != 0x001f) {
  1244. dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
  1245. man_id >> 8, man_id & 0xFF);
  1246. return -EINVAL;
  1247. }
  1248. lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
  1249. IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
  1250. IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
  1251. lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
  1252. switch (part) {
  1253. case 2:
  1254. chip = "at86rf230";
  1255. rc = -ENOTSUPP;
  1256. break;
  1257. case 3:
  1258. chip = "at86rf231";
  1259. lp->data = &at86rf231_data;
  1260. lp->hw->phy->channels_supported[0] = 0x7FFF800;
  1261. lp->hw->phy->current_channel = 11;
  1262. lp->hw->phy->symbol_duration = 16;
  1263. break;
  1264. case 7:
  1265. chip = "at86rf212";
  1266. lp->data = &at86rf212_data;
  1267. lp->hw->flags |= IEEE802154_HW_LBT;
  1268. lp->hw->phy->channels_supported[0] = 0x00007FF;
  1269. lp->hw->phy->channels_supported[2] = 0x00007FF;
  1270. lp->hw->phy->current_channel = 5;
  1271. lp->hw->phy->symbol_duration = 25;
  1272. break;
  1273. case 11:
  1274. chip = "at86rf233";
  1275. lp->data = &at86rf233_data;
  1276. lp->hw->phy->channels_supported[0] = 0x7FFF800;
  1277. lp->hw->phy->current_channel = 13;
  1278. lp->hw->phy->symbol_duration = 16;
  1279. break;
  1280. default:
  1281. chip = "unknown";
  1282. rc = -ENOTSUPP;
  1283. break;
  1284. }
  1285. dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
  1286. return rc;
  1287. }
  1288. static void
  1289. at86rf230_setup_spi_messages(struct at86rf230_local *lp)
  1290. {
  1291. lp->state.lp = lp;
  1292. spi_message_init(&lp->state.msg);
  1293. lp->state.msg.context = &lp->state;
  1294. lp->state.trx.tx_buf = lp->state.buf;
  1295. lp->state.trx.rx_buf = lp->state.buf;
  1296. spi_message_add_tail(&lp->state.trx, &lp->state.msg);
  1297. lp->irq.lp = lp;
  1298. spi_message_init(&lp->irq.msg);
  1299. lp->irq.msg.context = &lp->irq;
  1300. lp->irq.trx.tx_buf = lp->irq.buf;
  1301. lp->irq.trx.rx_buf = lp->irq.buf;
  1302. spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
  1303. lp->tx.lp = lp;
  1304. spi_message_init(&lp->tx.msg);
  1305. lp->tx.msg.context = &lp->tx;
  1306. lp->tx.trx.tx_buf = lp->tx.buf;
  1307. lp->tx.trx.rx_buf = lp->tx.buf;
  1308. spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
  1309. }
  1310. static int at86rf230_probe(struct spi_device *spi)
  1311. {
  1312. struct at86rf230_platform_data *pdata;
  1313. struct ieee802154_hw *hw;
  1314. struct at86rf230_local *lp;
  1315. unsigned int status;
  1316. int rc, irq_type;
  1317. if (!spi->irq) {
  1318. dev_err(&spi->dev, "no IRQ specified\n");
  1319. return -EINVAL;
  1320. }
  1321. pdata = at86rf230_get_pdata(spi);
  1322. if (!pdata) {
  1323. dev_err(&spi->dev, "no platform_data\n");
  1324. return -EINVAL;
  1325. }
  1326. if (gpio_is_valid(pdata->rstn)) {
  1327. rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
  1328. GPIOF_OUT_INIT_HIGH, "rstn");
  1329. if (rc)
  1330. return rc;
  1331. }
  1332. if (gpio_is_valid(pdata->slp_tr)) {
  1333. rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
  1334. GPIOF_OUT_INIT_LOW, "slp_tr");
  1335. if (rc)
  1336. return rc;
  1337. }
  1338. /* Reset */
  1339. if (gpio_is_valid(pdata->rstn)) {
  1340. udelay(1);
  1341. gpio_set_value(pdata->rstn, 0);
  1342. udelay(1);
  1343. gpio_set_value(pdata->rstn, 1);
  1344. usleep_range(120, 240);
  1345. }
  1346. hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
  1347. if (!hw)
  1348. return -ENOMEM;
  1349. lp = hw->priv;
  1350. lp->hw = hw;
  1351. lp->spi = spi;
  1352. hw->parent = &spi->dev;
  1353. hw->vif_data_size = sizeof(*lp);
  1354. ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
  1355. lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
  1356. if (IS_ERR(lp->regmap)) {
  1357. rc = PTR_ERR(lp->regmap);
  1358. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  1359. rc);
  1360. goto free_dev;
  1361. }
  1362. at86rf230_setup_spi_messages(lp);
  1363. rc = at86rf230_detect_device(lp);
  1364. if (rc < 0)
  1365. goto free_dev;
  1366. spin_lock_init(&lp->lock);
  1367. init_completion(&lp->state_complete);
  1368. spi_set_drvdata(spi, lp);
  1369. rc = at86rf230_hw_init(lp);
  1370. if (rc)
  1371. goto free_dev;
  1372. /* Read irq status register to reset irq line */
  1373. rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
  1374. if (rc)
  1375. goto free_dev;
  1376. irq_type = irq_get_trigger_type(spi->irq);
  1377. if (!irq_type)
  1378. irq_type = IRQF_TRIGGER_RISING;
  1379. rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
  1380. IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
  1381. if (rc)
  1382. goto free_dev;
  1383. rc = ieee802154_register_hw(lp->hw);
  1384. if (rc)
  1385. goto free_dev;
  1386. return rc;
  1387. free_dev:
  1388. ieee802154_free_hw(lp->hw);
  1389. return rc;
  1390. }
  1391. static int at86rf230_remove(struct spi_device *spi)
  1392. {
  1393. struct at86rf230_local *lp = spi_get_drvdata(spi);
  1394. /* mask all at86rf230 irq's */
  1395. at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
  1396. ieee802154_unregister_hw(lp->hw);
  1397. ieee802154_free_hw(lp->hw);
  1398. dev_dbg(&spi->dev, "unregistered at86rf230\n");
  1399. return 0;
  1400. }
  1401. static const struct of_device_id at86rf230_of_match[] = {
  1402. { .compatible = "atmel,at86rf230", },
  1403. { .compatible = "atmel,at86rf231", },
  1404. { .compatible = "atmel,at86rf233", },
  1405. { .compatible = "atmel,at86rf212", },
  1406. { },
  1407. };
  1408. MODULE_DEVICE_TABLE(of, at86rf230_of_match);
  1409. static const struct spi_device_id at86rf230_device_id[] = {
  1410. { .name = "at86rf230", },
  1411. { .name = "at86rf231", },
  1412. { .name = "at86rf233", },
  1413. { .name = "at86rf212", },
  1414. { },
  1415. };
  1416. MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
  1417. static struct spi_driver at86rf230_driver = {
  1418. .id_table = at86rf230_device_id,
  1419. .driver = {
  1420. .of_match_table = of_match_ptr(at86rf230_of_match),
  1421. .name = "at86rf230",
  1422. .owner = THIS_MODULE,
  1423. },
  1424. .probe = at86rf230_probe,
  1425. .remove = at86rf230_remove,
  1426. };
  1427. module_spi_driver(at86rf230_driver);
  1428. MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
  1429. MODULE_LICENSE("GPL v2");