max2820.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Radio tuning for Maxim max2820 on RTL8180
  3. *
  4. * Copyright 2007 Andrea Merello <andrea.merello@gmail.com>
  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/pci.h>
  21. #include <linux/delay.h>
  22. #include <net/mac80211.h>
  23. #include "rtl8180.h"
  24. #include "max2820.h"
  25. static const u32 max2820_chan[] = {
  26. 12, /* CH 1 */
  27. 17,
  28. 22,
  29. 27,
  30. 32,
  31. 37,
  32. 42,
  33. 47,
  34. 52,
  35. 57,
  36. 62,
  37. 67,
  38. 72,
  39. 84, /* CH 14 */
  40. };
  41. static void write_max2820(struct ieee80211_hw *dev, u8 addr, u32 data)
  42. {
  43. struct rtl8180_priv *priv = dev->priv;
  44. u32 phy_config;
  45. phy_config = 0x90 + (data & 0xf);
  46. phy_config <<= 16;
  47. phy_config += addr;
  48. phy_config <<= 8;
  49. phy_config += (data >> 4) & 0xff;
  50. rtl818x_iowrite32(priv,
  51. (__le32 __iomem *) &priv->map->RFPinsOutput, phy_config);
  52. msleep(1);
  53. }
  54. static void max2820_write_phy_antenna(struct ieee80211_hw *dev, short chan)
  55. {
  56. struct rtl8180_priv *priv = dev->priv;
  57. u8 ant;
  58. ant = MAXIM_ANTENNA;
  59. if (priv->rfparam & RF_PARAM_ANTBDEFAULT)
  60. ant |= BB_ANTENNA_B;
  61. if (chan == 14)
  62. ant |= BB_ANTATTEN_CHAN14;
  63. rtl8180_write_phy(dev, 0x10, ant);
  64. }
  65. static u8 max2820_rf_calc_rssi(u8 agc, u8 sq)
  66. {
  67. bool odd;
  68. odd = !!(agc & 1);
  69. agc >>= 1;
  70. if (odd)
  71. agc += 76;
  72. else
  73. agc += 66;
  74. /* TODO: change addends above to avoid mult / div below */
  75. return 65 * agc / 100;
  76. }
  77. static void max2820_rf_set_channel(struct ieee80211_hw *dev,
  78. struct ieee80211_conf *conf)
  79. {
  80. struct rtl8180_priv *priv = dev->priv;
  81. int channel = conf ?
  82. ieee80211_frequency_to_channel(conf->chandef.chan->center_freq) : 1;
  83. unsigned int chan_idx = channel - 1;
  84. u32 txpw = priv->channels[chan_idx].hw_value & 0xFF;
  85. u32 chan = max2820_chan[chan_idx];
  86. /* While philips SA2400 drive the PA bias from
  87. * sa2400, for MAXIM we do this directly from BB */
  88. rtl8180_write_phy(dev, 3, txpw);
  89. max2820_write_phy_antenna(dev, channel);
  90. write_max2820(dev, 3, chan);
  91. }
  92. static void max2820_rf_stop(struct ieee80211_hw *dev)
  93. {
  94. rtl8180_write_phy(dev, 3, 0x8);
  95. write_max2820(dev, 1, 0);
  96. }
  97. static void max2820_rf_init(struct ieee80211_hw *dev)
  98. {
  99. struct rtl8180_priv *priv = dev->priv;
  100. /* MAXIM from netbsd driver */
  101. write_max2820(dev, 0, 0x007); /* test mode as indicated in datasheet */
  102. write_max2820(dev, 1, 0x01e); /* enable register */
  103. write_max2820(dev, 2, 0x001); /* synt register */
  104. max2820_rf_set_channel(dev, NULL);
  105. write_max2820(dev, 4, 0x313); /* rx register */
  106. /* PA is driven directly by the BB, we keep the MAXIM bias
  107. * at the highest value in case that setting it to lower
  108. * values may introduce some further attenuation somewhere..
  109. */
  110. write_max2820(dev, 5, 0x00f);
  111. /* baseband configuration */
  112. rtl8180_write_phy(dev, 0, 0x88); /* sys1 */
  113. rtl8180_write_phy(dev, 3, 0x08); /* txagc */
  114. rtl8180_write_phy(dev, 4, 0xf8); /* lnadet */
  115. rtl8180_write_phy(dev, 5, 0x90); /* ifagcinit */
  116. rtl8180_write_phy(dev, 6, 0x1a); /* ifagclimit */
  117. rtl8180_write_phy(dev, 7, 0x64); /* ifagcdet */
  118. max2820_write_phy_antenna(dev, 1);
  119. rtl8180_write_phy(dev, 0x11, 0x88); /* trl */
  120. if (rtl818x_ioread8(priv, &priv->map->CONFIG2) &
  121. RTL818X_CONFIG2_ANTENNA_DIV)
  122. rtl8180_write_phy(dev, 0x12, 0xc7);
  123. else
  124. rtl8180_write_phy(dev, 0x12, 0x47);
  125. rtl8180_write_phy(dev, 0x13, 0x9b);
  126. rtl8180_write_phy(dev, 0x19, 0x0); /* CHESTLIM */
  127. rtl8180_write_phy(dev, 0x1a, 0x9f); /* CHSQLIM */
  128. max2820_rf_set_channel(dev, NULL);
  129. }
  130. const struct rtl818x_rf_ops max2820_rf_ops = {
  131. .name = "Maxim",
  132. .init = max2820_rf_init,
  133. .stop = max2820_rf_stop,
  134. .set_chan = max2820_rf_set_channel,
  135. .calc_rssi = max2820_rf_calc_rssi,
  136. };