mpc107_i2c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * (C) Copyright 2002 ELTEC Elektronik AG
  3. * Frank Gottschling <fgottschling@eltec.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. /* includes */
  24. #include <common.h>
  25. #include "srom.h"
  26. /* locals */
  27. static unsigned long mpc107_eumb_addr = 0;
  28. /*----------------------------------------------------------------------------*/
  29. /*
  30. * calculate checksum for ELTEC revision srom
  31. */
  32. unsigned long el_srom_checksum (ptr, size)
  33. register unsigned char *ptr;
  34. unsigned long size;
  35. {
  36. u_long f, accu = 0;
  37. u_int i;
  38. u_char byte;
  39. for (; size; size--)
  40. {
  41. byte = *ptr++;
  42. for (i = 8; i; i--)
  43. {
  44. f = ((byte & 1) ^ (accu & 1)) ? 0x84083001 : 0;
  45. accu >>= 1; accu ^= f;
  46. byte >>= 1;
  47. }
  48. }
  49. return(accu);
  50. }
  51. /*----------------------------------------------------------------------------*/
  52. static int mpc107_i2c_wait ( unsigned long timeout )
  53. {
  54. unsigned long x;
  55. while (((x = in32r(MPC107_I2CSR)) & 0x82) != 0x82)
  56. {
  57. if (!timeout--)
  58. return -1;
  59. }
  60. if (x & 0x10)
  61. {
  62. return -1;
  63. }
  64. out32r(MPC107_I2CSR, 0);
  65. return 0;
  66. }
  67. /*----------------------------------------------------------------------------*/
  68. static int mpc107_i2c_wait_idle ( unsigned long timeout )
  69. {
  70. while (in32r(MPC107_I2CSR) & 0x20)
  71. {
  72. if (!timeout--)
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. /*----------------------------------------------------------------------------*/
  78. int mpc107_i2c_read_byte (
  79. unsigned char device,
  80. unsigned char block,
  81. unsigned char offset )
  82. {
  83. unsigned long timeout = MPC107_I2C_TIMEOUT;
  84. int data;
  85. if (!mpc107_eumb_addr)
  86. return -6;
  87. mpc107_i2c_wait_idle (timeout);
  88. /* Start with MEN */
  89. out32r(MPC107_I2CCR, 0x80);
  90. /* Start as master */
  91. out32r(MPC107_I2CCR, 0xB0);
  92. out32r(MPC107_I2CDR, (0xA0 | device | block));
  93. if (mpc107_i2c_wait(timeout) < 0)
  94. {
  95. printf("mpc107_i2c_read Error 1\n");
  96. return -2;
  97. }
  98. if (in32r(MPC107_I2CSR)&0x1)
  99. {
  100. /* Generate STOP condition; device busy or not existing */
  101. out32r(MPC107_I2CCR, 0x80);
  102. return -1;
  103. }
  104. /* Data address */
  105. out32r(MPC107_I2CDR, offset);
  106. if (mpc107_i2c_wait(timeout) < 0)
  107. {
  108. printf("mpc107_i2c_read Error 2\n");
  109. return -3;
  110. }
  111. /* Switch to read - restart */
  112. out32r(MPC107_I2CCR, 0xB4);
  113. out32r(MPC107_I2CDR, (0xA1 | device | block));
  114. if (mpc107_i2c_wait(timeout) < 0)
  115. {
  116. printf("mpc107_i2c_read Error 3\n");
  117. return -4;
  118. }
  119. out32r(MPC107_I2CCR, 0xA8); /* no ACK */
  120. in32r(MPC107_I2CDR);
  121. if (mpc107_i2c_wait(timeout) < 0)
  122. {
  123. printf("mpc107_i2c_read Error 4\n");
  124. return -5;
  125. }
  126. /* Generate STOP condition */
  127. out32r(MPC107_I2CCR, 0x88);
  128. /* read */
  129. data = in32r(MPC107_I2CDR);
  130. return (data);
  131. }
  132. /*----------------------------------------------------------------------------*/
  133. int mpc107_i2c_write_byte (
  134. unsigned char device,
  135. unsigned char block,
  136. unsigned char offset,
  137. unsigned char val )
  138. {
  139. unsigned long timeout = MPC107_I2C_TIMEOUT;
  140. if (!mpc107_eumb_addr)
  141. return -6;
  142. mpc107_i2c_wait_idle(timeout);
  143. /* Start with MEN */
  144. out32r(MPC107_I2CCR, 0x80);
  145. /* Start as master */
  146. out32r(MPC107_I2CCR, 0xB0);
  147. out32r(MPC107_I2CDR, (0xA0 | device | block));
  148. if (mpc107_i2c_wait(timeout) < 0)
  149. {
  150. printf("mpc107_i2c_write Error 1\n");
  151. return -1;
  152. }
  153. /* Data address */
  154. out32r(MPC107_I2CDR, offset);
  155. if (mpc107_i2c_wait(timeout) < 0)
  156. {
  157. printf("mpc107_i2c_write Error 2\n");
  158. return -1;
  159. }
  160. /* Write */
  161. out32r(MPC107_I2CDR, val);
  162. if (mpc107_i2c_wait(timeout) < 0)
  163. {
  164. printf("mpc107_i2c_write Error 3\n");
  165. return -1;
  166. }
  167. /* Generate Stop Condition */
  168. out32r(MPC107_I2CCR, 0x80);
  169. /* Return ACK or no ACK */
  170. return (in32r(MPC107_I2CSR) & 0x01);
  171. }
  172. /*----------------------------------------------------------------------------*/
  173. int mpc107_srom_load (
  174. unsigned char addr,
  175. unsigned char *pBuf,
  176. int cnt,
  177. unsigned char device,
  178. unsigned char block )
  179. {
  180. register int i;
  181. int val;
  182. int timeout;
  183. for (i = 0; i < cnt; i++)
  184. {
  185. timeout=100;
  186. do
  187. {
  188. val = mpc107_i2c_read_byte (device, block, addr);
  189. if (val < -1)
  190. {
  191. printf("i2c_read_error %d at dev %x block %x addr %x\n",
  192. val, device, block, addr);
  193. return -1;
  194. }
  195. else if (timeout==0)
  196. {
  197. printf ("i2c_read_error: timeout at dev %x block %x addr %x\n",
  198. device, block, addr);
  199. return -1;
  200. }
  201. timeout--;
  202. } while (val == -1); /* if no ack: try again! */
  203. *pBuf++ = (unsigned char)val;
  204. addr++;
  205. if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
  206. {
  207. if (block == FIRST_BLOCK)
  208. block = SECOND_BLOCK;
  209. else
  210. {
  211. printf ("ic2_read_error: read beyond 2. block !\n");
  212. return -1;
  213. }
  214. }
  215. }
  216. udelay(100000);
  217. return (cnt);
  218. }
  219. /*----------------------------------------------------------------------------*/
  220. int mpc107_srom_store (
  221. unsigned char addr,
  222. unsigned char *pBuf,
  223. int cnt,
  224. unsigned char device,
  225. unsigned char block )
  226. {
  227. register int i;
  228. for (i = 0; i < cnt; i++)
  229. {
  230. while (mpc107_i2c_write_byte (device,block,addr,*pBuf) == 1);
  231. addr++;
  232. pBuf++;
  233. if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
  234. {
  235. if (block == FIRST_BLOCK)
  236. block = SECOND_BLOCK;
  237. else
  238. {
  239. printf ("ic2_write_error: write beyond 2. block !\n");
  240. return -1;
  241. }
  242. }
  243. }
  244. udelay(100000);
  245. return(cnt);
  246. }
  247. /*----------------------------------------------------------------------------*/
  248. int mpc107_i2c_init ( unsigned long eumb_addr, unsigned long divider )
  249. {
  250. unsigned long x;
  251. if (eumb_addr)
  252. mpc107_eumb_addr = eumb_addr;
  253. else
  254. return -1;
  255. /* Set I2C clock */
  256. x = in32r(MPC107_I2CFDR) & 0xffffff00;
  257. out32r(MPC107_I2CFDR, (x | divider));
  258. /* Clear arbitration */
  259. out32r(MPC107_I2CSR, 0);
  260. return mpc107_eumb_addr;
  261. }
  262. /*----------------------------------------------------------------------------*/