solo6x10-eeprom.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include "solo6x10.h"
  23. /* Control */
  24. #define EE_SHIFT_CLK 0x04
  25. #define EE_CS 0x08
  26. #define EE_DATA_WRITE 0x02
  27. #define EE_DATA_READ 0x01
  28. #define EE_ENB (0x80 | EE_CS)
  29. #define eeprom_delay() udelay(100)
  30. #if 0
  31. #define eeprom_delay() solo_reg_read(solo_dev, SOLO_EEPROM_CTRL)
  32. #define eeprom_delay() ({ \
  33. int i, ret; \
  34. udelay(100); \
  35. for (i = ret = 0; i < 1000 && !ret; i++) \
  36. ret = solo_eeprom_reg_read(solo_dev); \
  37. })
  38. #endif
  39. #define ADDR_LEN 6
  40. /* Commands */
  41. #define EE_EWEN_CMD 4
  42. #define EE_EWDS_CMD 4
  43. #define EE_WRITE_CMD 5
  44. #define EE_READ_CMD 6
  45. #define EE_ERASE_CMD 7
  46. static unsigned int solo_eeprom_reg_read(struct solo_dev *solo_dev)
  47. {
  48. return solo_reg_read(solo_dev, SOLO_EEPROM_CTRL) & EE_DATA_READ;
  49. }
  50. static void solo_eeprom_reg_write(struct solo_dev *solo_dev, u32 data)
  51. {
  52. solo_reg_write(solo_dev, SOLO_EEPROM_CTRL, data);
  53. eeprom_delay();
  54. }
  55. static void solo_eeprom_cmd(struct solo_dev *solo_dev, int cmd)
  56. {
  57. int i;
  58. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ACCESS_EN);
  59. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
  60. for (i = 4 + ADDR_LEN; i >= 0; i--) {
  61. int dataval = (cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  62. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE | dataval);
  63. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
  64. EE_SHIFT_CLK | dataval);
  65. }
  66. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
  67. }
  68. unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en)
  69. {
  70. int ewen_cmd = (w_en ? 0x3f : 0) | (EE_EWEN_CMD << ADDR_LEN);
  71. unsigned int retval = 0;
  72. int i;
  73. solo_eeprom_cmd(solo_dev, ewen_cmd);
  74. for (i = 0; i < 16; i++) {
  75. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
  76. EE_SHIFT_CLK);
  77. retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
  78. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
  79. retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
  80. }
  81. solo_eeprom_reg_write(solo_dev, ~EE_CS);
  82. retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
  83. return retval;
  84. }
  85. __be16 solo_eeprom_read(struct solo_dev *solo_dev, int loc)
  86. {
  87. int read_cmd = loc | (EE_READ_CMD << ADDR_LEN);
  88. u16 retval = 0;
  89. int i;
  90. solo_eeprom_cmd(solo_dev, read_cmd);
  91. for (i = 0; i < 16; i++) {
  92. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
  93. EE_SHIFT_CLK);
  94. retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
  95. solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
  96. }
  97. solo_eeprom_reg_write(solo_dev, ~EE_CS);
  98. return (__force __be16)retval;
  99. }
  100. int solo_eeprom_write(struct solo_dev *solo_dev, int loc,
  101. __be16 data)
  102. {
  103. int write_cmd = loc | (EE_WRITE_CMD << ADDR_LEN);
  104. unsigned int retval;
  105. int i;
  106. solo_eeprom_cmd(solo_dev, write_cmd);
  107. for (i = 15; i >= 0; i--) {
  108. unsigned int dataval = ((__force unsigned)data >> i) & 1;
  109. solo_eeprom_reg_write(solo_dev, EE_ENB);
  110. solo_eeprom_reg_write(solo_dev,
  111. EE_ENB | (dataval << 1) | EE_SHIFT_CLK);
  112. }
  113. solo_eeprom_reg_write(solo_dev, EE_ENB);
  114. solo_eeprom_reg_write(solo_dev, ~EE_CS);
  115. solo_eeprom_reg_write(solo_dev, EE_ENB);
  116. for (i = retval = 0; i < 10000 && !retval; i++)
  117. retval = solo_eeprom_reg_read(solo_dev);
  118. solo_eeprom_reg_write(solo_dev, ~EE_CS);
  119. return !retval;
  120. }