rt2800lib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright (C) 2009 Bartlomiej Zolnierkiewicz
  3. Based on the original rt2800pci.c and rt2800usb.c:
  4. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  5. <http://rt2x00.serialmonkey.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the
  16. Free Software Foundation, Inc.,
  17. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. Module: rt2800lib
  21. Abstract: rt2800 generic device routines.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include "rt2x00.h"
  26. #include "rt2800lib.h"
  27. #include "rt2800.h"
  28. MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
  29. MODULE_DESCRIPTION("rt2800 library");
  30. MODULE_LICENSE("GPL");
  31. /*
  32. * Register access.
  33. * All access to the CSR registers will go through the methods
  34. * rt2800_register_read and rt2800_register_write.
  35. * BBP and RF register require indirect register access,
  36. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  37. * These indirect registers work with busy bits,
  38. * and we will try maximal REGISTER_BUSY_COUNT times to access
  39. * the register while taking a REGISTER_BUSY_DELAY us delay
  40. * between each attampt. When the busy bit is still set at that time,
  41. * the access attempt is considered to have failed,
  42. * and we will print an error.
  43. * The _lock versions must be used if you already hold the csr_mutex
  44. */
  45. #define WAIT_FOR_BBP(__dev, __reg) \
  46. rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
  47. #define WAIT_FOR_RFCSR(__dev, __reg) \
  48. rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
  49. #define WAIT_FOR_RF(__dev, __reg) \
  50. rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
  51. #define WAIT_FOR_MCU(__dev, __reg) \
  52. rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  53. H2M_MAILBOX_CSR_OWNER, (__reg))
  54. void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
  55. const unsigned int word, const u8 value)
  56. {
  57. u32 reg;
  58. mutex_lock(&rt2x00dev->csr_mutex);
  59. /*
  60. * Wait until the BBP becomes available, afterwards we
  61. * can safely write the new data into the register.
  62. */
  63. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  64. reg = 0;
  65. rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
  66. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  67. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  68. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
  69. if (rt2x00_intf_is_pci(rt2x00dev))
  70. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  71. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  72. }
  73. mutex_unlock(&rt2x00dev->csr_mutex);
  74. }
  75. EXPORT_SYMBOL_GPL(rt2800_bbp_write);
  76. void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
  77. const unsigned int word, u8 *value)
  78. {
  79. u32 reg;
  80. mutex_lock(&rt2x00dev->csr_mutex);
  81. /*
  82. * Wait until the BBP becomes available, afterwards we
  83. * can safely write the read request into the register.
  84. * After the data has been written, we wait until hardware
  85. * returns the correct value, if at any time the register
  86. * doesn't become available in time, reg will be 0xffffffff
  87. * which means we return 0xff to the caller.
  88. */
  89. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  90. reg = 0;
  91. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  92. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  93. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
  94. if (rt2x00_intf_is_pci(rt2x00dev))
  95. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  96. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  97. WAIT_FOR_BBP(rt2x00dev, &reg);
  98. }
  99. *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
  100. mutex_unlock(&rt2x00dev->csr_mutex);
  101. }
  102. EXPORT_SYMBOL_GPL(rt2800_bbp_read);
  103. void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
  104. const unsigned int word, const u8 value)
  105. {
  106. u32 reg;
  107. mutex_lock(&rt2x00dev->csr_mutex);
  108. /*
  109. * Wait until the RFCSR becomes available, afterwards we
  110. * can safely write the new data into the register.
  111. */
  112. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  113. reg = 0;
  114. rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
  115. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  116. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
  117. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  118. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  119. }
  120. mutex_unlock(&rt2x00dev->csr_mutex);
  121. }
  122. EXPORT_SYMBOL_GPL(rt2800_rfcsr_write);
  123. void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
  124. const unsigned int word, u8 *value)
  125. {
  126. u32 reg;
  127. mutex_lock(&rt2x00dev->csr_mutex);
  128. /*
  129. * Wait until the RFCSR becomes available, afterwards we
  130. * can safely write the read request into the register.
  131. * After the data has been written, we wait until hardware
  132. * returns the correct value, if at any time the register
  133. * doesn't become available in time, reg will be 0xffffffff
  134. * which means we return 0xff to the caller.
  135. */
  136. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  137. reg = 0;
  138. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  139. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
  140. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  141. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  142. WAIT_FOR_RFCSR(rt2x00dev, &reg);
  143. }
  144. *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
  145. mutex_unlock(&rt2x00dev->csr_mutex);
  146. }
  147. EXPORT_SYMBOL_GPL(rt2800_rfcsr_read);
  148. void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
  149. const unsigned int word, const u32 value)
  150. {
  151. u32 reg;
  152. mutex_lock(&rt2x00dev->csr_mutex);
  153. /*
  154. * Wait until the RF becomes available, afterwards we
  155. * can safely write the new data into the register.
  156. */
  157. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  158. reg = 0;
  159. rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
  160. rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
  161. rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
  162. rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
  163. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
  164. rt2x00_rf_write(rt2x00dev, word, value);
  165. }
  166. mutex_unlock(&rt2x00dev->csr_mutex);
  167. }
  168. EXPORT_SYMBOL_GPL(rt2800_rf_write);
  169. void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
  170. const u8 command, const u8 token,
  171. const u8 arg0, const u8 arg1)
  172. {
  173. u32 reg;
  174. if (rt2x00_intf_is_pci(rt2x00dev)) {
  175. /*
  176. * RT2880 and RT3052 don't support MCU requests.
  177. */
  178. if (rt2x00_rt(&rt2x00dev->chip, RT2880) ||
  179. rt2x00_rt(&rt2x00dev->chip, RT3052))
  180. return;
  181. }
  182. mutex_lock(&rt2x00dev->csr_mutex);
  183. /*
  184. * Wait until the MCU becomes available, afterwards we
  185. * can safely write the new data into the register.
  186. */
  187. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  188. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  189. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  190. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  191. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  192. rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
  193. reg = 0;
  194. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  195. rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
  196. }
  197. mutex_unlock(&rt2x00dev->csr_mutex);
  198. }
  199. EXPORT_SYMBOL_GPL(rt2800_mcu_request);