2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 static unsigned long mpc107_eumb_addr = 0;
31 /*----------------------------------------------------------------------------*/
34 * calculate checksum for ELTEC revision srom
36 unsigned long el_srom_checksum (ptr, size)
37 register unsigned char *ptr;
49 f = ((byte & 1) ^ (accu & 1)) ? 0x84083001 : 0;
50 accu >>= 1; accu ^= f;
57 /*----------------------------------------------------------------------------*/
59 static int mpc107_i2c_wait ( unsigned long timeout )
63 while (((x = in32r(MPC107_I2CSR)) & 0x82) != 0x82)
73 out32r(MPC107_I2CSR, 0);
78 /*----------------------------------------------------------------------------*/
80 static int mpc107_i2c_wait_idle ( unsigned long timeout )
82 while (in32r(MPC107_I2CSR) & 0x20)
91 /*----------------------------------------------------------------------------*/
93 int mpc107_i2c_read_byte (
96 unsigned char offset )
98 unsigned long timeout = MPC107_I2C_TIMEOUT;
101 if (!mpc107_eumb_addr)
104 mpc107_i2c_wait_idle (timeout);
107 out32r(MPC107_I2CCR, 0x80);
109 /* Start as master */
110 out32r(MPC107_I2CCR, 0xB0);
111 out32r(MPC107_I2CDR, (0xA0 | device | block));
113 if (mpc107_i2c_wait(timeout) < 0)
115 printf("mpc107_i2c_read Error 1\n");
119 if (in32r(MPC107_I2CSR)&0x1)
121 /* Generate STOP condition; device busy or not existing */
122 out32r(MPC107_I2CCR, 0x80);
127 out32r(MPC107_I2CDR, offset);
129 if (mpc107_i2c_wait(timeout) < 0)
131 printf("mpc107_i2c_read Error 2\n");
135 /* Switch to read - restart */
136 out32r(MPC107_I2CCR, 0xB4);
137 out32r(MPC107_I2CDR, (0xA1 | device | block));
139 if (mpc107_i2c_wait(timeout) < 0)
141 printf("mpc107_i2c_read Error 3\n");
145 out32r(MPC107_I2CCR, 0xA8); /* no ACK */
148 if (mpc107_i2c_wait(timeout) < 0)
150 printf("mpc107_i2c_read Error 4\n");
153 /* Generate STOP condition */
154 out32r(MPC107_I2CCR, 0x88);
157 data = in32r(MPC107_I2CDR);
162 /*----------------------------------------------------------------------------*/
164 int mpc107_i2c_write_byte (
165 unsigned char device,
167 unsigned char offset,
171 unsigned long timeout = MPC107_I2C_TIMEOUT;
173 if (!mpc107_eumb_addr)
176 mpc107_i2c_wait_idle(timeout);
179 out32r(MPC107_I2CCR, 0x80);
181 /* Start as master */
182 out32r(MPC107_I2CCR, 0xB0);
183 out32r(MPC107_I2CDR, (0xA0 | device | block));
185 if (mpc107_i2c_wait(timeout) < 0)
187 printf("mpc107_i2c_write Error 1\n");
192 out32r(MPC107_I2CDR, offset);
194 if (mpc107_i2c_wait(timeout) < 0)
196 printf("mpc107_i2c_write Error 2\n");
201 out32r(MPC107_I2CDR, val);
202 if (mpc107_i2c_wait(timeout) < 0)
204 printf("mpc107_i2c_write Error 3\n");
208 /* Generate Stop Condition */
209 out32r(MPC107_I2CCR, 0x80);
211 /* Return ACK or no ACK */
212 return (in32r(MPC107_I2CSR) & 0x01);
215 /*----------------------------------------------------------------------------*/
217 int mpc107_srom_load (
221 unsigned char device,
222 unsigned char block )
228 for (i = 0; i < cnt; i++)
233 val = mpc107_i2c_read_byte (device, block, addr);
236 printf("i2c_read_error %d at dev %x block %x addr %x\n",
237 val, device, block, addr);
242 printf ("i2c_read_error: timeout at dev %x block %x addr %x\n",
243 device, block, addr);
247 } while (val == -1); /* if no ack: try again! */
249 *pBuf++ = (unsigned char)val;
252 if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
254 if (block == FIRST_BLOCK)
255 block = SECOND_BLOCK;
258 printf ("ic2_read_error: read beyond 2. block !\n");
267 /*----------------------------------------------------------------------------*/
269 int mpc107_srom_store (
273 unsigned char device,
274 unsigned char block )
278 for (i = 0; i < cnt; i++)
280 while (mpc107_i2c_write_byte (device,block,addr,*pBuf) == 1);
284 if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
286 if (block == FIRST_BLOCK)
287 block = SECOND_BLOCK;
290 printf ("ic2_write_error: write beyond 2. block !\n");
299 /*----------------------------------------------------------------------------*/
301 int mpc107_i2c_init ( unsigned long eumb_addr, unsigned long divider )
306 mpc107_eumb_addr = eumb_addr;
311 x = in32r(MPC107_I2CFDR) & 0xffffff00;
312 out32r(MPC107_I2CFDR, (x | divider));
314 /* Clear arbitration */
315 out32r(MPC107_I2CSR, 0);
317 return mpc107_eumb_addr;
320 /*----------------------------------------------------------------------------*/