at86rf230.c 39 KB

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