rtw_eeprom.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. *
  19. ******************************************************************************/
  20. #define _RTW_EEPROM_C_
  21. #include <drv_conf.h>
  22. #include <osdep_service.h>
  23. #include <drv_types.h>
  24. void up_clk(_adapter *padapter, u16 *x)
  25. {
  26. *x = *x | _EESK;
  27. rtw_write8(padapter, EE_9346CR, (u8)*x);
  28. rtw_udelay_os(CLOCK_RATE);
  29. }
  30. void down_clk(_adapter *padapter, u16 *x)
  31. {
  32. *x = *x & ~_EESK;
  33. rtw_write8(padapter, EE_9346CR, (u8)*x);
  34. rtw_udelay_os(CLOCK_RATE);
  35. }
  36. void shift_out_bits(_adapter *padapter, u16 data, u16 count)
  37. {
  38. u16 x, mask;
  39. if (rtw_is_surprise_removed(padapter)) {
  40. goto out;
  41. }
  42. mask = 0x01 << (count - 1);
  43. x = rtw_read8(padapter, EE_9346CR);
  44. x &= ~(_EEDO | _EEDI);
  45. do {
  46. x &= ~_EEDI;
  47. if (data & mask)
  48. x |= _EEDI;
  49. if (rtw_is_surprise_removed(padapter)) {
  50. goto out;
  51. }
  52. rtw_write8(padapter, EE_9346CR, (u8)x);
  53. rtw_udelay_os(CLOCK_RATE);
  54. up_clk(padapter, &x);
  55. down_clk(padapter, &x);
  56. mask = mask >> 1;
  57. } while (mask);
  58. if (rtw_is_surprise_removed(padapter)) {
  59. goto out;
  60. }
  61. x &= ~_EEDI;
  62. rtw_write8(padapter, EE_9346CR, (u8)x);
  63. out:
  64. return;
  65. }
  66. u16 shift_in_bits(_adapter *padapter)
  67. {
  68. u16 x, d = 0, i;
  69. if (rtw_is_surprise_removed(padapter)) {
  70. goto out;
  71. }
  72. x = rtw_read8(padapter, EE_9346CR);
  73. x &= ~(_EEDO | _EEDI);
  74. d = 0;
  75. for (i = 0; i < 16; i++) {
  76. d = d << 1;
  77. up_clk(padapter, &x);
  78. if (rtw_is_surprise_removed(padapter)) {
  79. goto out;
  80. }
  81. x = rtw_read8(padapter, EE_9346CR);
  82. x &= ~(_EEDI);
  83. if (x & _EEDO)
  84. d |= 1;
  85. down_clk(padapter, &x);
  86. }
  87. out:
  88. return d;
  89. }
  90. void standby(_adapter *padapter)
  91. {
  92. u8 x;
  93. x = rtw_read8(padapter, EE_9346CR);
  94. x &= ~(_EECS | _EESK);
  95. rtw_write8(padapter, EE_9346CR, x);
  96. rtw_udelay_os(CLOCK_RATE);
  97. x |= _EECS;
  98. rtw_write8(padapter, EE_9346CR, x);
  99. rtw_udelay_os(CLOCK_RATE);
  100. }
  101. u16 wait_eeprom_cmd_done(_adapter *padapter)
  102. {
  103. u8 x;
  104. u16 i, res = _FALSE;
  105. standby(padapter);
  106. for (i = 0; i < 200; i++) {
  107. x = rtw_read8(padapter, EE_9346CR);
  108. if (x & _EEDO) {
  109. res = _TRUE;
  110. goto exit;
  111. }
  112. rtw_udelay_os(CLOCK_RATE);
  113. }
  114. exit:
  115. return res;
  116. }
  117. void eeprom_clean(_adapter *padapter)
  118. {
  119. u16 x;
  120. if (rtw_is_surprise_removed(padapter)) {
  121. goto out;
  122. }
  123. x = rtw_read8(padapter, EE_9346CR);
  124. if (rtw_is_surprise_removed(padapter)) {
  125. goto out;
  126. }
  127. x &= ~(_EECS | _EEDI);
  128. rtw_write8(padapter, EE_9346CR, (u8)x);
  129. if (rtw_is_surprise_removed(padapter)) {
  130. goto out;
  131. }
  132. up_clk(padapter, &x);
  133. if (rtw_is_surprise_removed(padapter)) {
  134. goto out;
  135. }
  136. down_clk(padapter, &x);
  137. out:
  138. return;
  139. }
  140. void eeprom_write16(_adapter *padapter, u16 reg, u16 data)
  141. {
  142. u8 x;
  143. #ifdef CONFIG_RTL8712
  144. u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
  145. tmp8_ori = rtw_read8(padapter, 0x102502f1);
  146. tmp8_new = tmp8_ori & 0xf7;
  147. if (tmp8_ori != tmp8_new) {
  148. rtw_write8(padapter, 0x102502f1, tmp8_new);
  149. }
  150. tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
  151. tmp8_clk_new = tmp8_clk_ori | 0x20;
  152. if (tmp8_clk_new != tmp8_clk_ori) {
  153. rtw_write8(padapter, 0x10250003, tmp8_clk_new);
  154. }
  155. #endif
  156. x = rtw_read8(padapter, EE_9346CR);
  157. x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
  158. x |= _EEM1 | _EECS;
  159. rtw_write8(padapter, EE_9346CR, x);
  160. shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
  161. if (padapter->EepromAddressSize == 8) /* CF+ and SDIO */
  162. shift_out_bits(padapter, 0, 6);
  163. else /* USB */
  164. shift_out_bits(padapter, 0, 4);
  165. standby(padapter);
  166. /* Commented out by rcnjko, 2004.0
  167. * Erase this particular word. Write the erase opcode and register
  168. * number in that order. The opcode is 3bits in length; reg is 6 bits long. */
  169. /* shift_out_bits(Adapter, EEPROM_ERASE_OPCODE, 3);
  170. * shift_out_bits(Adapter, reg, Adapter->EepromAddressSize);
  171. *
  172. * if (wait_eeprom_cmd_done(Adapter ) == FALSE)
  173. * {
  174. * return;
  175. * } */
  176. standby(padapter);
  177. /* write the new word to the EEPROM */
  178. /* send the write opcode the EEPORM */
  179. shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
  180. /* select which word in the EEPROM that we are writing to. */
  181. shift_out_bits(padapter, reg, padapter->EepromAddressSize);
  182. /* write the data to the selected EEPROM word. */
  183. shift_out_bits(padapter, data, 16);
  184. if (wait_eeprom_cmd_done(padapter) == _FALSE)
  185. goto exit;
  186. standby(padapter);
  187. shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
  188. shift_out_bits(padapter, reg, 4);
  189. eeprom_clean(padapter);
  190. exit:
  191. #ifdef CONFIG_RTL8712
  192. if (tmp8_clk_new != tmp8_clk_ori)
  193. rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
  194. if (tmp8_new != tmp8_ori)
  195. rtw_write8(padapter, 0x102502f1, tmp8_ori);
  196. #endif
  197. return;
  198. }
  199. u16 eeprom_read16(_adapter *padapter, u16 reg) /* ReadEEprom */
  200. {
  201. u16 x;
  202. u16 data = 0;
  203. #ifdef CONFIG_RTL8712
  204. u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
  205. tmp8_ori = rtw_read8(padapter, 0x102502f1);
  206. tmp8_new = tmp8_ori & 0xf7;
  207. if (tmp8_ori != tmp8_new) {
  208. rtw_write8(padapter, 0x102502f1, tmp8_new);
  209. }
  210. tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
  211. tmp8_clk_new = tmp8_clk_ori | 0x20;
  212. if (tmp8_clk_new != tmp8_clk_ori) {
  213. rtw_write8(padapter, 0x10250003, tmp8_clk_new);
  214. }
  215. #endif
  216. if (rtw_is_surprise_removed(padapter)) {
  217. goto out;
  218. }
  219. /* select EEPROM, reset bits, set _EECS */
  220. x = rtw_read8(padapter, EE_9346CR);
  221. if (rtw_is_surprise_removed(padapter)) {
  222. goto out;
  223. }
  224. x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
  225. x |= _EEM1 | _EECS;
  226. rtw_write8(padapter, EE_9346CR, (unsigned char)x);
  227. /* write the read opcode and register number in that order */
  228. /* The opcode is 3bits in length, reg is 6 bits long */
  229. shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
  230. shift_out_bits(padapter, reg, padapter->EepromAddressSize);
  231. /* Now read the data (16 bits) in from the selected EEPROM word */
  232. data = shift_in_bits(padapter);
  233. eeprom_clean(padapter);
  234. out:
  235. #ifdef CONFIG_RTL8712
  236. if (tmp8_clk_new != tmp8_clk_ori)
  237. rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
  238. if (tmp8_new != tmp8_ori)
  239. rtw_write8(padapter, 0x102502f1, tmp8_ori);
  240. #endif
  241. return data;
  242. }
  243. /* From even offset */
  244. void eeprom_read_sz(_adapter *padapter, u16 reg, u8 *data, u32 sz)
  245. {
  246. u16 x, data16;
  247. u32 i;
  248. if (rtw_is_surprise_removed(padapter)) {
  249. goto out;
  250. }
  251. /* select EEPROM, reset bits, set _EECS */
  252. x = rtw_read8(padapter, EE_9346CR);
  253. if (rtw_is_surprise_removed(padapter)) {
  254. goto out;
  255. }
  256. x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
  257. x |= _EEM1 | _EECS;
  258. rtw_write8(padapter, EE_9346CR, (unsigned char)x);
  259. /* write the read opcode and register number in that order */
  260. /* The opcode is 3bits in length, reg is 6 bits long */
  261. shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
  262. shift_out_bits(padapter, reg, padapter->EepromAddressSize);
  263. for (i = 0; i < sz; i += 2) {
  264. data16 = shift_in_bits(padapter);
  265. data[i] = data16 & 0xff;
  266. data[i + 1] = data16 >> 8;
  267. }
  268. eeprom_clean(padapter);
  269. out:
  270. return;
  271. }
  272. /* addr_off : address offset of the entry in eeprom (not the tuple number of eeprom (reg); that is addr_off !=reg) */
  273. u8 eeprom_read(_adapter *padapter, u32 addr_off, u8 sz, u8 *rbuf)
  274. {
  275. u8 quotient, remainder, addr_2align_odd;
  276. u16 reg, stmp , i = 0, idx = 0;
  277. reg = (u16)(addr_off >> 1);
  278. addr_2align_odd = (u8)(addr_off & 0x1);
  279. if (addr_2align_odd) { /* read that start at high part: e.g 1,3,5,7,9,... */
  280. stmp = eeprom_read16(padapter, reg);
  281. rbuf[idx++] = (u8)((stmp >> 8) & 0xff); /* return hogh-part of the short */
  282. reg++;
  283. sz--;
  284. }
  285. quotient = sz >> 1;
  286. remainder = sz & 0x1;
  287. for (i = 0 ; i < quotient; i++) {
  288. stmp = eeprom_read16(padapter, reg + i);
  289. rbuf[idx++] = (u8)(stmp & 0xff);
  290. rbuf[idx++] = (u8)((stmp >> 8) & 0xff);
  291. }
  292. reg = reg + i;
  293. if (remainder) { /* end of read at lower part of short : 0,2,4,6,... */
  294. stmp = eeprom_read16(padapter, reg);
  295. rbuf[idx] = (u8)(stmp & 0xff);
  296. }
  297. return _TRUE;
  298. }
  299. VOID read_eeprom_content(_adapter *padapter)
  300. {
  301. }