rtl8180_grf5101.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Radio tuning for GCT GRF5101 on RTL8180
  3. *
  4. * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
  5. *
  6. * Code from the BSD driver and the rtl8181 project have been
  7. * very useful to understand certain things
  8. *
  9. * I want to thanks the Authors of such projects and the Ndiswrapper
  10. * project Authors.
  11. *
  12. * A special Big Thanks also is for all people who donated me cards,
  13. * making possible the creation of the original rtl8180 driver
  14. * from which this code is derived!
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <net/mac80211.h>
  24. #include "rtl8180.h"
  25. #include "rtl8180_grf5101.h"
  26. static const int grf5101_encode[] = {
  27. 0x0, 0x8, 0x4, 0xC,
  28. 0x2, 0xA, 0x6, 0xE,
  29. 0x1, 0x9, 0x5, 0xD,
  30. 0x3, 0xB, 0x7, 0xF
  31. };
  32. static void write_grf5101(struct ieee80211_hw *dev, u8 addr, u32 data)
  33. {
  34. struct rtl8180_priv *priv = dev->priv;
  35. u32 phy_config;
  36. phy_config = grf5101_encode[(data >> 8) & 0xF];
  37. phy_config |= grf5101_encode[(data >> 4) & 0xF] << 4;
  38. phy_config |= grf5101_encode[data & 0xF] << 8;
  39. phy_config |= grf5101_encode[(addr >> 1) & 0xF] << 12;
  40. phy_config |= (addr & 1) << 16;
  41. phy_config |= grf5101_encode[(data & 0xf000) >> 12] << 24;
  42. /* MAC will bang bits to the chip */
  43. phy_config |= 0x90000000;
  44. rtl818x_iowrite32(priv,
  45. (__le32 __iomem *) &priv->map->RFPinsOutput, phy_config);
  46. msleep(3);
  47. }
  48. static void grf5101_write_phy_antenna(struct ieee80211_hw *dev, short chan)
  49. {
  50. struct rtl8180_priv *priv = dev->priv;
  51. u8 ant = GRF5101_ANTENNA;
  52. if (priv->rfparam & RF_PARAM_ANTBDEFAULT)
  53. ant |= BB_ANTENNA_B;
  54. if (chan == 14)
  55. ant |= BB_ANTATTEN_CHAN14;
  56. rtl8180_write_phy(dev, 0x10, ant);
  57. }
  58. static u8 grf5101_rf_calc_rssi(u8 agc, u8 sq)
  59. {
  60. if (agc > 60)
  61. return 65;
  62. /* TODO(?): just return agc (or agc + 5) to avoid mult / div */
  63. return 65 * agc / 60;
  64. }
  65. static void grf5101_rf_set_channel(struct ieee80211_hw *dev,
  66. struct ieee80211_conf *conf)
  67. {
  68. struct rtl8180_priv *priv = dev->priv;
  69. int channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  70. u32 txpw = priv->channels[channel - 1].hw_value & 0xFF;
  71. u32 chan = channel - 1;
  72. /* set TX power */
  73. write_grf5101(dev, 0x15, 0x0);
  74. write_grf5101(dev, 0x06, txpw);
  75. write_grf5101(dev, 0x15, 0x10);
  76. write_grf5101(dev, 0x15, 0x0);
  77. /* set frequency */
  78. write_grf5101(dev, 0x07, 0x0);
  79. write_grf5101(dev, 0x0B, chan);
  80. write_grf5101(dev, 0x07, 0x1000);
  81. grf5101_write_phy_antenna(dev, channel);
  82. }
  83. static void grf5101_rf_stop(struct ieee80211_hw *dev)
  84. {
  85. struct rtl8180_priv *priv = dev->priv;
  86. u32 anaparam;
  87. anaparam = priv->anaparam;
  88. anaparam &= 0x000fffff;
  89. anaparam |= 0x3f900000;
  90. rtl8180_set_anaparam(priv, anaparam);
  91. write_grf5101(dev, 0x07, 0x0);
  92. write_grf5101(dev, 0x1f, 0x45);
  93. write_grf5101(dev, 0x1f, 0x5);
  94. write_grf5101(dev, 0x00, 0x8e4);
  95. }
  96. static void grf5101_rf_init(struct ieee80211_hw *dev)
  97. {
  98. struct rtl8180_priv *priv = dev->priv;
  99. rtl8180_set_anaparam(priv, priv->anaparam);
  100. write_grf5101(dev, 0x1f, 0x0);
  101. write_grf5101(dev, 0x1f, 0x0);
  102. write_grf5101(dev, 0x1f, 0x40);
  103. write_grf5101(dev, 0x1f, 0x60);
  104. write_grf5101(dev, 0x1f, 0x61);
  105. write_grf5101(dev, 0x1f, 0x61);
  106. write_grf5101(dev, 0x00, 0xae4);
  107. write_grf5101(dev, 0x1f, 0x1);
  108. write_grf5101(dev, 0x1f, 0x41);
  109. write_grf5101(dev, 0x1f, 0x61);
  110. write_grf5101(dev, 0x01, 0x1a23);
  111. write_grf5101(dev, 0x02, 0x4971);
  112. write_grf5101(dev, 0x03, 0x41de);
  113. write_grf5101(dev, 0x04, 0x2d80);
  114. write_grf5101(dev, 0x05, 0x68ff); /* 0x61ff original value */
  115. write_grf5101(dev, 0x06, 0x0);
  116. write_grf5101(dev, 0x07, 0x0);
  117. write_grf5101(dev, 0x08, 0x7533);
  118. write_grf5101(dev, 0x09, 0xc401);
  119. write_grf5101(dev, 0x0a, 0x0);
  120. write_grf5101(dev, 0x0c, 0x1c7);
  121. write_grf5101(dev, 0x0d, 0x29d3);
  122. write_grf5101(dev, 0x0e, 0x2e8);
  123. write_grf5101(dev, 0x10, 0x192);
  124. write_grf5101(dev, 0x11, 0x248);
  125. write_grf5101(dev, 0x12, 0x0);
  126. write_grf5101(dev, 0x13, 0x20c4);
  127. write_grf5101(dev, 0x14, 0xf4fc);
  128. write_grf5101(dev, 0x15, 0x0);
  129. write_grf5101(dev, 0x16, 0x1500);
  130. write_grf5101(dev, 0x07, 0x1000);
  131. /* baseband configuration */
  132. rtl8180_write_phy(dev, 0, 0xa8);
  133. rtl8180_write_phy(dev, 3, 0x0);
  134. rtl8180_write_phy(dev, 4, 0xc0);
  135. rtl8180_write_phy(dev, 5, 0x90);
  136. rtl8180_write_phy(dev, 6, 0x1e);
  137. rtl8180_write_phy(dev, 7, 0x64);
  138. grf5101_write_phy_antenna(dev, 1);
  139. rtl8180_write_phy(dev, 0x11, 0x88);
  140. if (rtl818x_ioread8(priv, &priv->map->CONFIG2) &
  141. RTL818X_CONFIG2_ANTENNA_DIV)
  142. rtl8180_write_phy(dev, 0x12, 0xc0); /* enable ant diversity */
  143. else
  144. rtl8180_write_phy(dev, 0x12, 0x40); /* disable ant diversity */
  145. rtl8180_write_phy(dev, 0x13, 0x90 | priv->csthreshold);
  146. rtl8180_write_phy(dev, 0x19, 0x0);
  147. rtl8180_write_phy(dev, 0x1a, 0xa0);
  148. rtl8180_write_phy(dev, 0x1b, 0x44);
  149. }
  150. const struct rtl818x_rf_ops grf5101_rf_ops = {
  151. .name = "GCT",
  152. .init = grf5101_rf_init,
  153. .stop = grf5101_rf_stop,
  154. .set_chan = grf5101_rf_set_channel,
  155. .calc_rssi = grf5101_rf_calc_rssi,
  156. };