atmel_read_eeprom.c 3.5 KB

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