max2820.c 4.0 KB

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