2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
5 * SPDX-License-Identifier: GPL-2.0+
13 static unsigned long mpc107_eumb_addr = 0;
15 /*----------------------------------------------------------------------------*/
18 * calculate checksum for ELTEC revision srom
20 unsigned long el_srom_checksum (ptr, size)
21 register unsigned char *ptr;
33 f = ((byte & 1) ^ (accu & 1)) ? 0x84083001 : 0;
34 accu >>= 1; accu ^= f;
41 /*----------------------------------------------------------------------------*/
43 static int mpc107_i2c_wait ( unsigned long timeout )
47 while (((x = in32r(MPC107_I2CSR)) & 0x82) != 0x82)
57 out32r(MPC107_I2CSR, 0);
62 /*----------------------------------------------------------------------------*/
64 static int mpc107_i2c_wait_idle ( unsigned long timeout )
66 while (in32r(MPC107_I2CSR) & 0x20)
75 /*----------------------------------------------------------------------------*/
77 int mpc107_i2c_read_byte (
80 unsigned char offset )
82 unsigned long timeout = MPC107_I2C_TIMEOUT;
85 if (!mpc107_eumb_addr)
88 mpc107_i2c_wait_idle (timeout);
91 out32r(MPC107_I2CCR, 0x80);
94 out32r(MPC107_I2CCR, 0xB0);
95 out32r(MPC107_I2CDR, (0xA0 | device | block));
97 if (mpc107_i2c_wait(timeout) < 0)
99 printf("mpc107_i2c_read Error 1\n");
103 if (in32r(MPC107_I2CSR)&0x1)
105 /* Generate STOP condition; device busy or not existing */
106 out32r(MPC107_I2CCR, 0x80);
111 out32r(MPC107_I2CDR, offset);
113 if (mpc107_i2c_wait(timeout) < 0)
115 printf("mpc107_i2c_read Error 2\n");
119 /* Switch to read - restart */
120 out32r(MPC107_I2CCR, 0xB4);
121 out32r(MPC107_I2CDR, (0xA1 | device | block));
123 if (mpc107_i2c_wait(timeout) < 0)
125 printf("mpc107_i2c_read Error 3\n");
129 out32r(MPC107_I2CCR, 0xA8); /* no ACK */
132 if (mpc107_i2c_wait(timeout) < 0)
134 printf("mpc107_i2c_read Error 4\n");
137 /* Generate STOP condition */
138 out32r(MPC107_I2CCR, 0x88);
141 data = in32r(MPC107_I2CDR);
146 /*----------------------------------------------------------------------------*/
148 int mpc107_i2c_write_byte (
149 unsigned char device,
151 unsigned char offset,
155 unsigned long timeout = MPC107_I2C_TIMEOUT;
157 if (!mpc107_eumb_addr)
160 mpc107_i2c_wait_idle(timeout);
163 out32r(MPC107_I2CCR, 0x80);
165 /* Start as master */
166 out32r(MPC107_I2CCR, 0xB0);
167 out32r(MPC107_I2CDR, (0xA0 | device | block));
169 if (mpc107_i2c_wait(timeout) < 0)
171 printf("mpc107_i2c_write Error 1\n");
176 out32r(MPC107_I2CDR, offset);
178 if (mpc107_i2c_wait(timeout) < 0)
180 printf("mpc107_i2c_write Error 2\n");
185 out32r(MPC107_I2CDR, val);
186 if (mpc107_i2c_wait(timeout) < 0)
188 printf("mpc107_i2c_write Error 3\n");
192 /* Generate Stop Condition */
193 out32r(MPC107_I2CCR, 0x80);
195 /* Return ACK or no ACK */
196 return (in32r(MPC107_I2CSR) & 0x01);
199 /*----------------------------------------------------------------------------*/
201 int mpc107_srom_load (
205 unsigned char device,
206 unsigned char block )
212 for (i = 0; i < cnt; i++)
217 val = mpc107_i2c_read_byte (device, block, addr);
220 printf("i2c_read_error %d at dev %x block %x addr %x\n",
221 val, device, block, addr);
226 printf ("i2c_read_error: timeout at dev %x block %x addr %x\n",
227 device, block, addr);
231 } while (val == -1); /* if no ack: try again! */
233 *pBuf++ = (unsigned char)val;
236 if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
238 if (block == FIRST_BLOCK)
239 block = SECOND_BLOCK;
242 printf ("ic2_read_error: read beyond 2. block !\n");
251 /*----------------------------------------------------------------------------*/
253 int mpc107_srom_store (
257 unsigned char device,
258 unsigned char block )
262 for (i = 0; i < cnt; i++)
264 while (mpc107_i2c_write_byte (device,block,addr,*pBuf) == 1);
268 if ((addr == 0) && (i != cnt-1)) /* is it the same block ? */
270 if (block == FIRST_BLOCK)
271 block = SECOND_BLOCK;
274 printf ("ic2_write_error: write beyond 2. block !\n");
283 /*----------------------------------------------------------------------------*/
285 int mpc107_i2c_init ( unsigned long eumb_addr, unsigned long divider )
290 mpc107_eumb_addr = eumb_addr;
295 x = in32r(MPC107_I2CFDR) & 0xffffff00;
296 out32r(MPC107_I2CFDR, (x | divider));
298 /* Clear arbitration */
299 out32r(MPC107_I2CSR, 0);
301 return mpc107_eumb_addr;
304 /*----------------------------------------------------------------------------*/