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. static int debug = 0;
  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(100);
  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. /* only one address byte is supported */
  61. if (alen > 0)
  62. twi->TWI_IADR = addr & 0xff;
  63. length = len;
  64. buf = buffer;
  65. if (length && buf) { /* sanity check */
  66. if (rw) {
  67. twi->TWI_CR = AT91C_TWI_START;
  68. while (length--) {
  69. if (!length)
  70. twi->TWI_CR = AT91C_TWI_STOP;
  71. /* Wait until transfer is finished */
  72. if (!at91_poll_status(twi, AT91C_TWI_RXRDY)) {
  73. if (debug)
  74. printf("at91_i2c: timeout 1\n");
  75. return 1;
  76. }
  77. *buf++ = twi->TWI_RHR;
  78. }
  79. if (!at91_poll_status(twi, AT91C_TWI_TXCOMP)) {
  80. if (debug)
  81. printf("at91_i2c: timeout 2\n");
  82. return 1;
  83. }
  84. } else {
  85. twi->TWI_CR = AT91C_TWI_START;
  86. while (length--) {
  87. twi->TWI_THR = *buf++;
  88. if (!length)
  89. twi->TWI_CR = AT91C_TWI_STOP;
  90. if (!at91_poll_status(twi, AT91C_TWI_TXRDY)) {
  91. if (debug)
  92. printf("at91_i2c: timeout 3\n");
  93. return 1;
  94. }
  95. }
  96. /* Wait until transfer is finished */
  97. if (!at91_poll_status(twi, AT91C_TWI_TXCOMP)) {
  98. if (debug)
  99. printf("at91_i2c: timeout 4\n");
  100. return 1;
  101. }
  102. }
  103. }
  104. return 0;
  105. }
  106. int
  107. i2c_probe(unsigned char chip)
  108. {
  109. char buffer[1];
  110. return at91_xfer(chip, 0, 0, buffer, 1, 1);
  111. }
  112. int
  113. i2c_read(unsigned char chip, unsigned int addr, int alen,
  114. unsigned char *buffer, int len)
  115. {
  116. /* we only allow one address byte */
  117. if (alen > 1)
  118. return 1;
  119. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  120. /* XXX assume an ATMEL AT24C16 */
  121. if (alen == 1) {
  122. chip |= (addr >> 8) & 0xff;
  123. addr = addr & 0xff;
  124. }
  125. #endif
  126. return at91_xfer(chip, addr, alen, buffer, len, 1);
  127. }
  128. int
  129. i2c_write(unsigned char chip, unsigned int addr, int alen,
  130. unsigned char *buffer, int len)
  131. {
  132. int i;
  133. unsigned char *buf;
  134. /* we only allow one address byte */
  135. if (alen > 1)
  136. return 1;
  137. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  138. /* XXX assume an ATMEL AT24C16 */
  139. if (alen == 1) {
  140. buf = buffer;
  141. /* do single byte writes */
  142. for (i = 0; i < len; i++) {
  143. chip |= (addr >> 8) & 0xff;
  144. addr = addr & 0xff;
  145. if (at91_xfer(chip, addr, alen, buf++, 1, 0))
  146. return 1;
  147. }
  148. }
  149. return 0;
  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. printf("Found AT91 i2c\n");
  170. return;
  171. }
  172. #endif /* CONFIG_HARD_I2C */