atmel_read_eeprom.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * arch/mips/pmc-sierra/yosemite/atmel_read_eeprom.c
  3. *
  4. * Copyright (C) 2003 PMC-Sierra Inc.
  5. * Author: Manish Lachwani (lachwani@pmc-sierra.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  13. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  15. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  18. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. /*
  28. * Description:
  29. *
  30. * This code reads the ATMEL 24CXX EEPROM. The PMC-Sierra Yosemite board uses the ATMEL
  31. * 24C32/24C64 which uses two byte addressing as compared to 24C16. Note that this program
  32. * uses the serial port like /dev/ttyS0, to communicate with the EEPROM. Hence, you are
  33. * expected to have a connectivity from the EEPROM to the serial port. This program does
  34. * __not__ communicate using the I2C protocol
  35. */
  36. #include "atmel_read_eeprom.h"
  37. static void delay(int delay)
  38. {
  39. while (delay--);
  40. }
  41. static void send_bit(unsigned char bit)
  42. {
  43. scl_lo;
  44. delay(TXX);
  45. if (bit)
  46. sda_hi;
  47. else
  48. sda_lo;
  49. delay(TXX);
  50. scl_hi;
  51. delay(TXX);
  52. }
  53. static void send_ack(void)
  54. {
  55. send_bit(0);
  56. }
  57. static void send_byte(unsigned char byte)
  58. {
  59. int i = 0;
  60. for (i = 7; i >= 0; i--)
  61. send_bit((byte >> i) & 0x01);
  62. }
  63. static void send_start(void)
  64. {
  65. sda_hi;
  66. delay(TXX);
  67. scl_hi;
  68. delay(TXX);
  69. sda_lo;
  70. delay(TXX);
  71. }
  72. static void send_stop(void)
  73. {
  74. sda_lo;
  75. delay(TXX);
  76. scl_hi;
  77. delay(TXX);
  78. sda_hi;
  79. delay(TXX);
  80. }
  81. static void do_idle(void)
  82. {
  83. sda_hi;
  84. scl_hi;
  85. vcc_off;
  86. }
  87. static int recv_bit(void)
  88. {
  89. int status;
  90. scl_lo;
  91. delay(TXX);
  92. sda_hi;
  93. delay(TXX);
  94. scl_hi;
  95. delay(TXX);
  96. return 1;
  97. }
  98. static unsigned char recv_byte(void) {
  99. int i;
  100. unsigned char byte=0;
  101. for (i=7;i>=0;i--)
  102. byte |= (recv_bit() << i);
  103. return byte;
  104. }
  105. static int recv_ack(void)
  106. {
  107. unsigned int ack;
  108. ack = (unsigned int)recv_bit();
  109. scl_lo;
  110. if (ack) {
  111. do_idle();
  112. printk(KERN_ERR "Error reading the Atmel 24C32/24C64 EEPROM \n");
  113. return -1;
  114. }
  115. return ack;
  116. }
  117. /*
  118. * This function does the actual read of the EEPROM. It needs the buffer into which the
  119. * read data is copied, the size of the EEPROM being read and the buffer size
  120. */
  121. int read_eeprom(char *buffer, int eeprom_size, int size)
  122. {
  123. int i = 0, err;
  124. send_start();
  125. send_byte(W_HEADER);
  126. recv_ack();
  127. /* EEPROM with size of more then 2K need two byte addressing */
  128. if (eeprom_size > 2048) {
  129. send_byte(0x00);
  130. recv_ack();
  131. }
  132. send_start();
  133. send_byte(R_HEADER);
  134. err = recv_ack();
  135. if (err == -1)
  136. return err;
  137. for (i = 0; i < size; i++) {
  138. *buffer++ = recv_byte();
  139. send_ack();
  140. }
  141. /* Note : We should do some check if the buffer contains correct information */
  142. send_stop();
  143. }