i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #include "hardware.h"
  27. #include "i2c.h"
  28. static void i2c_start (void);
  29. static void i2c_stop (void);
  30. static int i2c_write (u8 data);
  31. static void i2c_read (u8 * data);
  32. static inline void i2c_port_start (void);
  33. static inline void i2c_clock (unsigned int val);
  34. static inline void i2c_data (unsigned int val);
  35. static inline unsigned int
  36. i2c_in (void);
  37. static inline void i2c_write_bit (unsigned int val);
  38. static inline unsigned int
  39. i2c_read_bit (void);
  40. static inline void i2c_udelay (unsigned int time);
  41. int i2c_read_byte (
  42. u8 * data,
  43. u8 dev,
  44. u8 offset)
  45. {
  46. int err = 0;
  47. i2c_start();
  48. err = ! i2c_write(dev);
  49. if (! err)
  50. {
  51. err = ! i2c_write(offset);
  52. }
  53. if (! err)
  54. {
  55. i2c_start();
  56. }
  57. if (! err)
  58. {
  59. err = ! i2c_write(dev | 0x01);
  60. }
  61. if (! err)
  62. {
  63. i2c_read(data);
  64. }
  65. i2c_stop();
  66. return ! err;
  67. }
  68. static inline void i2c_udelay (
  69. unsigned int time)
  70. {
  71. int v;
  72. asm volatile("mtdec %0" : : "r" (time * ((CONFIG_SYS_BUS_CLK / 4) / 1000000)));
  73. do
  74. {
  75. asm volatile("isync; mfdec %0" : "=r" (v));
  76. } while (v >= 0);
  77. }
  78. /* Low-level hardware access
  79. */
  80. #define BIT_GPDATA 0x80000000
  81. #define BIT_GPCLK 0x40000000
  82. static inline void i2c_port_start (void)
  83. {
  84. out32(REG(CPC0, GPDIR), in32(REG(CPC0, GPDIR)) & ~(BIT_GPCLK | BIT_GPDATA));
  85. out32(REG(CPC0, GPOUT), in32(REG(CPC0, GPOUT)) & ~(BIT_GPCLK | BIT_GPDATA));
  86. iobarrier_rw();
  87. i2c_udelay(1);
  88. }
  89. static inline void i2c_clock (
  90. unsigned int val)
  91. {
  92. if (val)
  93. {
  94. out32(REG(CPC0, GPDIR), in32(REG(CPC0, GPDIR)) & ~BIT_GPCLK);
  95. }
  96. else
  97. {
  98. out32(REG(CPC0, GPDIR), in32(REG(CPC0, GPDIR)) | BIT_GPCLK);
  99. }
  100. iobarrier_rw();
  101. i2c_udelay(1);
  102. }
  103. static inline void i2c_data (
  104. unsigned int val)
  105. {
  106. if (val)
  107. {
  108. out32(REG(CPC0, GPDIR), in32(REG(CPC0, GPDIR)) & ~BIT_GPDATA);
  109. }
  110. else
  111. {
  112. out32(REG(CPC0, GPDIR), in32(REG(CPC0, GPDIR)) | BIT_GPDATA);
  113. }
  114. iobarrier_rw();
  115. i2c_udelay(1);
  116. }
  117. static inline unsigned int i2c_in (void)
  118. {
  119. unsigned int val = ((in32(REG(CPC0, GPIN)) & BIT_GPDATA) != 0)?1:0;
  120. iobarrier_rw();
  121. return val;
  122. }
  123. /* Protocol implementation
  124. */
  125. static inline void i2c_write_bit (
  126. unsigned int val)
  127. {
  128. i2c_data(val);
  129. i2c_udelay(10);
  130. i2c_clock(1);
  131. i2c_udelay(10);
  132. i2c_clock(0);
  133. i2c_udelay(10);
  134. }
  135. static inline unsigned int i2c_read_bit (void)
  136. {
  137. unsigned int val;
  138. i2c_data(1);
  139. i2c_udelay(10);
  140. i2c_clock(1);
  141. i2c_udelay(10);
  142. val = i2c_in();
  143. i2c_clock(0);
  144. i2c_udelay(10);
  145. return val;
  146. }
  147. unsigned int i2c_reset (void)
  148. {
  149. unsigned int val;
  150. int i;
  151. i2c_port_start();
  152. i=0;
  153. do {
  154. i2c_udelay(10);
  155. i2c_clock(0);
  156. i2c_udelay(10);
  157. i2c_clock(1);
  158. i2c_udelay(10);
  159. val = i2c_in();
  160. i++;
  161. } while ((i<9)&&(val==0));
  162. return (val);
  163. }
  164. static void i2c_start (void)
  165. {
  166. i2c_data(1);
  167. i2c_clock(1);
  168. i2c_udelay(10);
  169. i2c_data(0);
  170. i2c_udelay(10);
  171. i2c_clock(0);
  172. i2c_udelay(10);
  173. }
  174. static void i2c_stop (void)
  175. {
  176. i2c_data(0);
  177. i2c_udelay(10);
  178. i2c_clock(1);
  179. i2c_udelay(10);
  180. i2c_data(1);
  181. i2c_udelay(10);
  182. }
  183. static int i2c_write (
  184. u8 data)
  185. {
  186. unsigned int i;
  187. for (i = 0; i < 8; i++)
  188. {
  189. i2c_write_bit(data >> 7);
  190. data <<= 1;
  191. }
  192. return i2c_read_bit() == 0;
  193. }
  194. static void i2c_read (
  195. u8 * data)
  196. {
  197. unsigned int i;
  198. u8 val = 0;
  199. for (i = 0; i < 8; i++)
  200. {
  201. val <<= 1;
  202. val |= i2c_read_bit();
  203. }
  204. *data = val;
  205. i2c_write_bit(1); /* NoAck */
  206. }