phy_common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_B43_PHY_COMMON_H_
  3. #define LINUX_B43_PHY_COMMON_H_
  4. #include <linux/types.h>
  5. #include <linux/nl80211.h>
  6. struct b43_wldev;
  7. /* Complex number using 2 32-bit signed integers */
  8. struct b43_c32 { s32 i, q; };
  9. #define CORDIC_CONVERT(value) (((value) >= 0) ? \
  10. ((((value) >> 15) + 1) >> 1) : \
  11. -((((-(value)) >> 15) + 1) >> 1))
  12. /* PHY register routing bits */
  13. #define B43_PHYROUTE 0x0C00 /* PHY register routing bits mask */
  14. #define B43_PHYROUTE_BASE 0x0000 /* Base registers */
  15. #define B43_PHYROUTE_OFDM_GPHY 0x0400 /* OFDM register routing for G-PHYs */
  16. #define B43_PHYROUTE_EXT_GPHY 0x0800 /* Extended G-PHY registers */
  17. #define B43_PHYROUTE_N_BMODE 0x0C00 /* N-PHY BMODE registers */
  18. /* CCK (B-PHY) registers. */
  19. #define B43_PHY_CCK(reg) ((reg) | B43_PHYROUTE_BASE)
  20. /* N-PHY registers. */
  21. #define B43_PHY_N(reg) ((reg) | B43_PHYROUTE_BASE)
  22. /* N-PHY BMODE registers. */
  23. #define B43_PHY_N_BMODE(reg) ((reg) | B43_PHYROUTE_N_BMODE)
  24. /* OFDM (A-PHY) registers. */
  25. #define B43_PHY_OFDM(reg) ((reg) | B43_PHYROUTE_OFDM_GPHY)
  26. /* Extended G-PHY registers. */
  27. #define B43_PHY_EXTG(reg) ((reg) | B43_PHYROUTE_EXT_GPHY)
  28. /* Masks for the PHY versioning registers. */
  29. #define B43_PHYVER_ANALOG 0xF000
  30. #define B43_PHYVER_ANALOG_SHIFT 12
  31. #define B43_PHYVER_TYPE 0x0F00
  32. #define B43_PHYVER_TYPE_SHIFT 8
  33. #define B43_PHYVER_VERSION 0x00FF
  34. /* PHY writes need to be flushed if we reach limit */
  35. #define B43_MAX_WRITES_IN_ROW 24
  36. /**
  37. * enum b43_interference_mitigation - Interference Mitigation mode
  38. *
  39. * @B43_INTERFMODE_NONE: Disabled
  40. * @B43_INTERFMODE_NONWLAN: Non-WLAN Interference Mitigation
  41. * @B43_INTERFMODE_MANUALWLAN: WLAN Interference Mitigation
  42. * @B43_INTERFMODE_AUTOWLAN: Automatic WLAN Interference Mitigation
  43. */
  44. enum b43_interference_mitigation {
  45. B43_INTERFMODE_NONE,
  46. B43_INTERFMODE_NONWLAN,
  47. B43_INTERFMODE_MANUALWLAN,
  48. B43_INTERFMODE_AUTOWLAN,
  49. };
  50. /* Antenna identifiers */
  51. enum {
  52. B43_ANTENNA0 = 0, /* Antenna 0 */
  53. B43_ANTENNA1 = 1, /* Antenna 1 */
  54. B43_ANTENNA_AUTO0 = 2, /* Automatic, starting with antenna 0 */
  55. B43_ANTENNA_AUTO1 = 3, /* Automatic, starting with antenna 1 */
  56. B43_ANTENNA2 = 4,
  57. B43_ANTENNA3 = 8,
  58. B43_ANTENNA_AUTO = B43_ANTENNA_AUTO0,
  59. B43_ANTENNA_DEFAULT = B43_ANTENNA_AUTO,
  60. };
  61. /**
  62. * enum b43_txpwr_result - Return value for the recalc_txpower PHY op.
  63. *
  64. * @B43_TXPWR_RES_NEED_ADJUST: Values changed. Hardware adjustment is needed.
  65. * @B43_TXPWR_RES_DONE: No more work to do. Everything is done.
  66. */
  67. enum b43_txpwr_result {
  68. B43_TXPWR_RES_NEED_ADJUST,
  69. B43_TXPWR_RES_DONE,
  70. };
  71. /**
  72. * struct b43_phy_operations - Function pointers for PHY ops.
  73. *
  74. * @allocate: Allocate and initialise the PHY data structures.
  75. * Must not be NULL.
  76. * @free: Destroy and free the PHY data structures.
  77. * Must not be NULL.
  78. *
  79. * @prepare_structs: Prepare the PHY data structures.
  80. * The data structures allocated in @allocate are
  81. * initialized here.
  82. * Must not be NULL.
  83. * @prepare_hardware: Prepare the PHY. This is called before b43_chip_init to
  84. * do some early early PHY hardware init.
  85. * Can be NULL, if not required.
  86. * @init: Initialize the PHY.
  87. * Must not be NULL.
  88. * @exit: Shutdown the PHY.
  89. * Can be NULL, if not required.
  90. *
  91. * @phy_read: Read from a PHY register.
  92. * Must not be NULL.
  93. * @phy_write: Write to a PHY register.
  94. * Must not be NULL.
  95. * @phy_maskset: Maskset a PHY register, taking shortcuts.
  96. * If it is NULL, a generic algorithm is used.
  97. * @radio_read: Read from a Radio register.
  98. * Must not be NULL.
  99. * @radio_write: Write to a Radio register.
  100. * Must not be NULL.
  101. *
  102. * @supports_hwpctl: Returns a boolean whether Hardware Power Control
  103. * is supported or not.
  104. * If NULL, hwpctl is assumed to be never supported.
  105. * @software_rfkill: Turn the radio ON or OFF.
  106. * Possible state values are
  107. * RFKILL_STATE_SOFT_BLOCKED or
  108. * RFKILL_STATE_UNBLOCKED
  109. * Must not be NULL.
  110. * @switch_analog: Turn the Analog on/off.
  111. * Must not be NULL.
  112. * @switch_channel: Switch the radio to another channel.
  113. * Must not be NULL.
  114. * @get_default_chan: Just returns the default channel number.
  115. * Must not be NULL.
  116. * @set_rx_antenna: Set the antenna used for RX.
  117. * Can be NULL, if not supported.
  118. * @interf_mitigation: Switch the Interference Mitigation mode.
  119. * Can be NULL, if not supported.
  120. *
  121. * @recalc_txpower: Recalculate the transmission power parameters.
  122. * This callback has to recalculate the TX power settings,
  123. * but does not need to write them to the hardware, yet.
  124. * Returns enum b43_txpwr_result to indicate whether the hardware
  125. * needs to be adjusted.
  126. * If B43_TXPWR_NEED_ADJUST is returned, @adjust_txpower
  127. * will be called later.
  128. * If the parameter "ignore_tssi" is true, the TSSI values should
  129. * be ignored and a recalculation of the power settings should be
  130. * done even if the TSSI values did not change.
  131. * This function may sleep, but should not.
  132. * Must not be NULL.
  133. * @adjust_txpower: Write the previously calculated TX power settings
  134. * (from @recalc_txpower) to the hardware.
  135. * This function may sleep.
  136. * Can be NULL, if (and ONLY if) @recalc_txpower _always_
  137. * returns B43_TXPWR_RES_DONE.
  138. *
  139. * @pwork_15sec: Periodic work. Called every 15 seconds.
  140. * Can be NULL, if not required.
  141. * @pwork_60sec: Periodic work. Called every 60 seconds.
  142. * Can be NULL, if not required.
  143. */
  144. struct b43_phy_operations {
  145. /* Initialisation */
  146. int (*allocate)(struct b43_wldev *dev);
  147. void (*free)(struct b43_wldev *dev);
  148. void (*prepare_structs)(struct b43_wldev *dev);
  149. int (*prepare_hardware)(struct b43_wldev *dev);
  150. int (*init)(struct b43_wldev *dev);
  151. void (*exit)(struct b43_wldev *dev);
  152. /* Register access */
  153. u16 (*phy_read)(struct b43_wldev *dev, u16 reg);
  154. void (*phy_write)(struct b43_wldev *dev, u16 reg, u16 value);
  155. void (*phy_maskset)(struct b43_wldev *dev, u16 reg, u16 mask, u16 set);
  156. u16 (*radio_read)(struct b43_wldev *dev, u16 reg);
  157. void (*radio_write)(struct b43_wldev *dev, u16 reg, u16 value);
  158. /* Radio */
  159. bool (*supports_hwpctl)(struct b43_wldev *dev);
  160. void (*software_rfkill)(struct b43_wldev *dev, bool blocked);
  161. void (*switch_analog)(struct b43_wldev *dev, bool on);
  162. int (*switch_channel)(struct b43_wldev *dev, unsigned int new_channel);
  163. unsigned int (*get_default_chan)(struct b43_wldev *dev);
  164. void (*set_rx_antenna)(struct b43_wldev *dev, int antenna);
  165. int (*interf_mitigation)(struct b43_wldev *dev,
  166. enum b43_interference_mitigation new_mode);
  167. /* Transmission power adjustment */
  168. enum b43_txpwr_result (*recalc_txpower)(struct b43_wldev *dev,
  169. bool ignore_tssi);
  170. void (*adjust_txpower)(struct b43_wldev *dev);
  171. /* Misc */
  172. void (*pwork_15sec)(struct b43_wldev *dev);
  173. void (*pwork_60sec)(struct b43_wldev *dev);
  174. };
  175. struct b43_phy_g;
  176. struct b43_phy_n;
  177. struct b43_phy_lp;
  178. struct b43_phy_ht;
  179. struct b43_phy_lcn;
  180. struct b43_phy {
  181. /* Hardware operation callbacks. */
  182. const struct b43_phy_operations *ops;
  183. /* Most hardware context information is stored in the standard-
  184. * specific data structures pointed to by the pointers below.
  185. * Only one of them is valid (the currently enabled PHY). */
  186. #ifdef CONFIG_B43_DEBUG
  187. /* No union for debug build to force NULL derefs in buggy code. */
  188. struct {
  189. #else
  190. union {
  191. #endif
  192. /* G-PHY specific information */
  193. struct b43_phy_g *g;
  194. /* N-PHY specific information */
  195. struct b43_phy_n *n;
  196. /* LP-PHY specific information */
  197. struct b43_phy_lp *lp;
  198. /* HT-PHY specific information */
  199. struct b43_phy_ht *ht;
  200. /* LCN-PHY specific information */
  201. struct b43_phy_lcn *lcn;
  202. /* AC-PHY specific information */
  203. struct b43_phy_ac *ac;
  204. };
  205. /* Band support flags. */
  206. bool supports_2ghz;
  207. bool supports_5ghz;
  208. /* Is GMODE (2 GHz mode) bit enabled? */
  209. bool gmode;
  210. /* After power reset full init has to be performed */
  211. bool do_full_init;
  212. /* Analog Type */
  213. u8 analog;
  214. /* B43_PHYTYPE_ */
  215. u8 type;
  216. /* PHY revision number. */
  217. u8 rev;
  218. /* Count writes since last read */
  219. u8 writes_counter;
  220. /* Radio versioning */
  221. u16 radio_manuf; /* Radio manufacturer */
  222. u16 radio_ver; /* Radio version */
  223. u8 radio_rev; /* Radio revision */
  224. /* Software state of the radio */
  225. bool radio_on;
  226. /* Desired TX power level (in dBm).
  227. * This is set by the user and adjusted in b43_phy_xmitpower(). */
  228. int desired_txpower;
  229. /* Hardware Power Control enabled? */
  230. bool hardware_power_control;
  231. /* The time (in absolute jiffies) when the next TX power output
  232. * check is needed. */
  233. unsigned long next_txpwr_check_time;
  234. /* Current channel */
  235. struct cfg80211_chan_def *chandef;
  236. unsigned int channel;
  237. /* PHY TX errors counter. */
  238. atomic_t txerr_cnt;
  239. #ifdef CONFIG_B43_DEBUG
  240. /* PHY registers locked (w.r.t. firmware) */
  241. bool phy_locked;
  242. /* Radio registers locked (w.r.t. firmware) */
  243. bool radio_locked;
  244. #endif /* B43_DEBUG */
  245. };
  246. /**
  247. * b43_phy_allocate - Allocate PHY structs
  248. * Allocate the PHY data structures, based on the current dev->phy.type
  249. */
  250. int b43_phy_allocate(struct b43_wldev *dev);
  251. /**
  252. * b43_phy_free - Free PHY structs
  253. */
  254. void b43_phy_free(struct b43_wldev *dev);
  255. /**
  256. * b43_phy_init - Initialise the PHY
  257. */
  258. int b43_phy_init(struct b43_wldev *dev);
  259. /**
  260. * b43_phy_exit - Cleanup PHY
  261. */
  262. void b43_phy_exit(struct b43_wldev *dev);
  263. /**
  264. * b43_has_hardware_pctl - Hardware Power Control supported?
  265. * Returns a boolean, whether hardware power control is supported.
  266. */
  267. bool b43_has_hardware_pctl(struct b43_wldev *dev);
  268. /**
  269. * b43_phy_read - 16bit PHY register read access
  270. */
  271. u16 b43_phy_read(struct b43_wldev *dev, u16 reg);
  272. /**
  273. * b43_phy_write - 16bit PHY register write access
  274. */
  275. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value);
  276. /**
  277. * b43_phy_copy - copy contents of 16bit PHY register to another
  278. */
  279. void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg);
  280. /**
  281. * b43_phy_mask - Mask a PHY register with a mask
  282. */
  283. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask);
  284. /**
  285. * b43_phy_set - OR a PHY register with a bitmap
  286. */
  287. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set);
  288. /**
  289. * b43_phy_maskset - Mask and OR a PHY register with a mask and bitmap
  290. */
  291. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set);
  292. /**
  293. * b43_radio_read - 16bit Radio register read access
  294. */
  295. u16 b43_radio_read(struct b43_wldev *dev, u16 reg);
  296. #define b43_radio_read16 b43_radio_read /* DEPRECATED */
  297. /**
  298. * b43_radio_write - 16bit Radio register write access
  299. */
  300. void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value);
  301. #define b43_radio_write16 b43_radio_write /* DEPRECATED */
  302. /**
  303. * b43_radio_mask - Mask a 16bit radio register with a mask
  304. */
  305. void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask);
  306. /**
  307. * b43_radio_set - OR a 16bit radio register with a bitmap
  308. */
  309. void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set);
  310. /**
  311. * b43_radio_maskset - Mask and OR a radio register with a mask and bitmap
  312. */
  313. void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set);
  314. /**
  315. * b43_radio_wait_value - Waits for a given value in masked register read
  316. */
  317. bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
  318. u16 value, int delay, int timeout);
  319. /**
  320. * b43_radio_lock - Lock firmware radio register access
  321. */
  322. void b43_radio_lock(struct b43_wldev *dev);
  323. /**
  324. * b43_radio_unlock - Unlock firmware radio register access
  325. */
  326. void b43_radio_unlock(struct b43_wldev *dev);
  327. /**
  328. * b43_phy_lock - Lock firmware PHY register access
  329. */
  330. void b43_phy_lock(struct b43_wldev *dev);
  331. /**
  332. * b43_phy_unlock - Unlock firmware PHY register access
  333. */
  334. void b43_phy_unlock(struct b43_wldev *dev);
  335. void b43_phy_put_into_reset(struct b43_wldev *dev);
  336. void b43_phy_take_out_of_reset(struct b43_wldev *dev);
  337. /**
  338. * b43_switch_channel - Switch to another channel
  339. */
  340. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel);
  341. /**
  342. * b43_software_rfkill - Turn the radio ON or OFF in software.
  343. */
  344. void b43_software_rfkill(struct b43_wldev *dev, bool blocked);
  345. /**
  346. * b43_phy_txpower_check - Check TX power output.
  347. *
  348. * Compare the current TX power output to the desired power emission
  349. * and schedule an adjustment in case it mismatches.
  350. *
  351. * @flags: OR'ed enum b43_phy_txpower_check_flags flags.
  352. * See the docs below.
  353. */
  354. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags);
  355. /**
  356. * enum b43_phy_txpower_check_flags - Flags for b43_phy_txpower_check()
  357. *
  358. * @B43_TXPWR_IGNORE_TIME: Ignore the schedule time and force-redo
  359. * the check now.
  360. * @B43_TXPWR_IGNORE_TSSI: Redo the recalculation, even if the average
  361. * TSSI did not change.
  362. */
  363. enum b43_phy_txpower_check_flags {
  364. B43_TXPWR_IGNORE_TIME = (1 << 0),
  365. B43_TXPWR_IGNORE_TSSI = (1 << 1),
  366. };
  367. struct work_struct;
  368. void b43_phy_txpower_adjust_work(struct work_struct *work);
  369. /**
  370. * b43_phy_shm_tssi_read - Read the average of the last 4 TSSI from SHM.
  371. *
  372. * @shm_offset: The SHM address to read the values from.
  373. *
  374. * Returns the average of the 4 TSSI values, or a negative error code.
  375. */
  376. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset);
  377. /**
  378. * b43_phy_switch_analog_generic - Generic PHY operation for switching the Analog.
  379. *
  380. * It does the switching based on the PHY0 core register.
  381. * Do _not_ call this directly. Only use it as a switch_analog callback
  382. * for struct b43_phy_operations.
  383. */
  384. void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on);
  385. bool b43_is_40mhz(struct b43_wldev *dev);
  386. void b43_phy_force_clock(struct b43_wldev *dev, bool force);
  387. struct b43_c32 b43_cordic(int theta);
  388. #endif /* LINUX_B43_PHY_COMMON_H_ */