i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * i2c Support for Atmel's AT91RM9200 Two-Wire Interface
  3. *
  4. * (c) Rick Bronson
  5. *
  6. * Borrowed heavily from original work by:
  7. * Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  8. *
  9. * Modified to work with u-boot by (C) 2004 Gary Jennejohn garyj@denx.de
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. #include <common.h>
  27. #ifdef CONFIG_HARD_I2C
  28. #include <i2c.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/hardware.h>
  31. #include <at91rm9200_i2c.h>
  32. /* define DEBUG */
  33. /*
  34. * Poll the i2c status register until the specified bit is set.
  35. * Returns 0 if timed out (100 msec)
  36. */
  37. static short at91_poll_status(AT91PS_TWI twi, unsigned long bit) {
  38. int loop_cntr = 10000;
  39. do {
  40. udelay(10);
  41. } while (!(twi->TWI_SR & bit) && (--loop_cntr > 0));
  42. return (loop_cntr > 0);
  43. }
  44. /*
  45. * Generic i2c master transfer entrypoint
  46. *
  47. * rw == 1 means that this is a read
  48. */
  49. static int
  50. at91_xfer(unsigned char chip, unsigned int addr, int alen,
  51. unsigned char *buffer, int len, int rw)
  52. {
  53. AT91PS_TWI twi = (AT91PS_TWI) AT91_TWI_BASE;
  54. int length;
  55. unsigned char *buf;
  56. /* Set the TWI Master Mode Register */
  57. twi->TWI_MMR = (chip << 16) | (alen << 8)
  58. | ((rw == 1) ? AT91C_TWI_MREAD : 0);
  59. /* Set TWI Internal Address Register with first messages data field */
  60. if (alen > 0)
  61. twi->TWI_IADR = addr;
  62. length = len;
  63. buf = buffer;
  64. if (length && buf) { /* sanity check */
  65. if (rw) {
  66. twi->TWI_CR = AT91C_TWI_START;
  67. while (length--) {
  68. if (!length)
  69. twi->TWI_CR = AT91C_TWI_STOP;
  70. /* Wait until transfer is finished */
  71. if (!at91_poll_status(twi, AT91C_TWI_RXRDY)) {
  72. debug ("at91_i2c: timeout 1\n");
  73. return 1;
  74. }
  75. *buf++ = twi->TWI_RHR;
  76. }
  77. if (!at91_poll_status(twi, AT91C_TWI_TXCOMP)) {
  78. debug ("at91_i2c: timeout 2\n");
  79. return 1;
  80. }
  81. } else {
  82. twi->TWI_CR = AT91C_TWI_START;
  83. while (length--) {
  84. twi->TWI_THR = *buf++;
  85. if (!length)
  86. twi->TWI_CR = AT91C_TWI_STOP;
  87. if (!at91_poll_status(twi, AT91C_TWI_TXRDY)) {
  88. debug ("at91_i2c: timeout 3\n");
  89. return 1;
  90. }
  91. }
  92. /* Wait until transfer is finished */
  93. if (!at91_poll_status(twi, AT91C_TWI_TXCOMP)) {
  94. debug ("at91_i2c: timeout 4\n");
  95. return 1;
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. int
  102. i2c_probe(unsigned char chip)
  103. {
  104. unsigned char buffer[1];
  105. return at91_xfer(chip, 0, 0, buffer, 1, 1);
  106. }
  107. int
  108. i2c_read (unsigned char chip, unsigned int addr, int alen,
  109. unsigned char *buffer, int len)
  110. {
  111. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  112. /* we only allow one address byte */
  113. if (alen > 1)
  114. return 1;
  115. /* XXX assume an ATMEL AT24C16 */
  116. if (alen == 1) {
  117. #if 0 /* EEPROM code already sets this correctly */
  118. chip |= (addr >> 8) & 0xff;
  119. #endif
  120. addr = addr & 0xff;
  121. }
  122. #endif
  123. return at91_xfer(chip, addr, alen, buffer, len, 1);
  124. }
  125. int
  126. i2c_write(unsigned char chip, unsigned int addr, int alen,
  127. unsigned char *buffer, int len)
  128. {
  129. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  130. int i;
  131. unsigned char *buf;
  132. /* we only allow one address byte */
  133. if (alen > 1)
  134. return 1;
  135. /* XXX assume an ATMEL AT24C16 */
  136. if (alen == 1) {
  137. buf = buffer;
  138. /* do single byte writes */
  139. for (i = 0; i < len; i++) {
  140. #if 0 /* EEPROM code already sets this correctly */
  141. chip |= (addr >> 8) & 0xff;
  142. #endif
  143. addr = addr & 0xff;
  144. if (at91_xfer(chip, addr, alen, buf++, 1, 0))
  145. return 1;
  146. addr++;
  147. }
  148. return 0;
  149. }
  150. #endif
  151. return at91_xfer(chip, addr, alen, buffer, len, 0);
  152. }
  153. /*
  154. * Main initialization routine
  155. */
  156. void
  157. i2c_init(int speed, int slaveaddr)
  158. {
  159. AT91PS_TWI twi = (AT91PS_TWI) AT91_TWI_BASE;
  160. *AT91C_PIOA_PDR = AT91C_PA25_TWD | AT91C_PA26_TWCK;
  161. *AT91C_PIOA_ASR = AT91C_PA25_TWD | AT91C_PA26_TWCK;
  162. *AT91C_PIOA_MDER = AT91C_PA25_TWD | AT91C_PA26_TWCK;
  163. *AT91C_PMC_PCER = 1 << AT91C_ID_TWI; /* enable peripheral clock */
  164. twi->TWI_IDR = 0x3ff; /* Disable all interrupts */
  165. twi->TWI_CR = AT91C_TWI_SWRST; /* Reset peripheral */
  166. twi->TWI_CR = AT91C_TWI_MSEN | AT91C_TWI_SVDIS; /* Set Master mode */
  167. /* Here, CKDIV = 1 and CHDIV=CLDIV ==> CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6) */
  168. twi->TWI_CWGR = AT91C_TWI_CKDIV1 | AT91C_TWI_CLDIV3 | (AT91C_TWI_CLDIV3 << 8);
  169. debug ("Found AT91 i2c\n");
  170. return;
  171. }
  172. #endif /* CONFIG_HARD_I2C */