summaryrefslogtreecommitdiff
path: root/board/eltec/elppc/mpc107_i2c.c
blob: 4f95703e143144df57dea69fc4cf99024cf4f7f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * (C) Copyright 2002 ELTEC Elektronik AG
 * Frank Gottschling <fgottschling@eltec.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/* includes */
#include <common.h>
#include "srom.h"

/* locals */
static unsigned long mpc107_eumb_addr = 0;

/*----------------------------------------------------------------------------*/

/*
 * calculate checksum for ELTEC revision srom
 */
unsigned long el_srom_checksum (ptr, size)
register unsigned char *ptr;
unsigned long size;
{
    u_long f, accu = 0;
    u_int  i;
    u_char byte;

    for (; size; size--)
    {
	byte = *ptr++;
	for (i = 8; i; i--)
	{
	    f =  ((byte & 1) ^ (accu & 1)) ? 0x84083001 : 0;
	    accu >>= 1; accu ^= f;
	    byte >>= 1;
	}
    }
    return(accu);
}

/*----------------------------------------------------------------------------*/

static int mpc107_i2c_wait ( unsigned long timeout )
{
    unsigned long x;

    while (((x = in32r(MPC107_I2CSR)) & 0x82) != 0x82)
    {
	if (!timeout--)
	    return -1;
    }

    if (x & 0x10)
    {
	return -1;
    }
    out32r(MPC107_I2CSR, 0);

    return 0;
}

/*----------------------------------------------------------------------------*/

static int mpc107_i2c_wait_idle ( unsigned long timeout )
{
    while (in32r(MPC107_I2CSR) & 0x20)
    {
	if (!timeout--)
	    return -1;
    }
    return 0;
}


/*----------------------------------------------------------------------------*/

int mpc107_i2c_read_byte (
    unsigned char device,
    unsigned char block,
    unsigned char offset )
{
    unsigned long timeout = MPC107_I2C_TIMEOUT;
    int data;

    if (!mpc107_eumb_addr)
	return -6;

    mpc107_i2c_wait_idle (timeout);

    /* Start with MEN */
    out32r(MPC107_I2CCR, 0x80);

    /* Start as master */
    out32r(MPC107_I2CCR, 0xB0);
    out32r(MPC107_I2CDR, (0xA0 | device | block));

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_read Error 1\n");
	return -2;
    }

    if (in32r(MPC107_I2CSR)&0x1)
    {
	/* Generate STOP condition; device busy or not existing */
	out32r(MPC107_I2CCR, 0x80);
	return -1;
    }

    /* Data address */
    out32r(MPC107_I2CDR, offset);

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_read Error 2\n");
	return -3;
    }

    /* Switch to read - restart */
    out32r(MPC107_I2CCR, 0xB4);
    out32r(MPC107_I2CDR, (0xA1 | device | block));

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_read Error 3\n");
	return -4;
    }

    out32r(MPC107_I2CCR, 0xA8); /* no ACK */
    in32r(MPC107_I2CDR);

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_read Error 4\n");
	return -5;
    }
    /* Generate STOP condition */
    out32r(MPC107_I2CCR, 0x88);

    /* read */
    data = in32r(MPC107_I2CDR);

    return (data);
}

/*----------------------------------------------------------------------------*/

int mpc107_i2c_write_byte (
    unsigned char device,
    unsigned char block,
    unsigned char offset,
    unsigned char val )
{

    unsigned long timeout = MPC107_I2C_TIMEOUT;

    if (!mpc107_eumb_addr)
	return -6;

    mpc107_i2c_wait_idle(timeout);

    /* Start with MEN */
    out32r(MPC107_I2CCR, 0x80);

    /* Start as master */
    out32r(MPC107_I2CCR, 0xB0);
    out32r(MPC107_I2CDR, (0xA0 | device | block));

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_write Error 1\n");
	return -1;
    }

    /* Data address */
    out32r(MPC107_I2CDR, offset);

    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_write Error 2\n");
	return -1;
    }

    /* Write */
    out32r(MPC107_I2CDR, val);
    if (mpc107_i2c_wait(timeout) < 0)
    {
	printf("mpc107_i2c_write Error 3\n");
	return -1;
    }

    /* Generate Stop Condition */
    out32r(MPC107_I2CCR, 0x80);

    /* Return ACK or no ACK */
    return (in32r(MPC107_I2CSR) & 0x01);
}

/*----------------------------------------------------------------------------*/

int mpc107_srom_load (
    unsigned char addr,
    unsigned char *pBuf,
    int          cnt,
    unsigned char device,
    unsigned char block )
{
    register int i;
    int val;
    int timeout;

    for (i = 0; i < cnt; i++)
    {
	timeout=100;
	do
	{
	    val = mpc107_i2c_read_byte (device, block, addr);
	    if (val < -1)
	    {
	    printf("i2c_read_error %d at dev %x block %x addr %x\n",
		   val, device, block, addr);
	    return -1;
	    }
	    else if (timeout==0)
	    {
		printf ("i2c_read_error: timeout at dev %x block %x addr %x\n",
			device, block, addr);
		return -1;
	    }
	    timeout--;
	} while (val == -1); /* if no ack: try again! */

	*pBuf++ = (unsigned char)val;
	addr++;

	if ((addr == 0) && (i != cnt-1))    /* is it the same block ? */
	{
	    if (block == FIRST_BLOCK)
		block = SECOND_BLOCK;
	    else
	    {
		printf ("ic2_read_error: read beyond 2. block !\n");
		return -1;
	    }
	}
    }
    udelay(100000);
    return (cnt);
}

/*----------------------------------------------------------------------------*/

int mpc107_srom_store (
    unsigned char addr,
    unsigned char *pBuf,
    int          cnt,
    unsigned char device,
    unsigned char block )
{
    register int i;

    for (i = 0; i < cnt; i++)
    {
	while (mpc107_i2c_write_byte (device,block,addr,*pBuf) == 1);
	addr++;
	pBuf++;

	if ((addr == 0) && (i != cnt-1))     /* is it the same block ? */
	{
	    if (block == FIRST_BLOCK)
		block = SECOND_BLOCK;
	    else
	    {
		printf ("ic2_write_error: write beyond 2. block !\n");
		return -1;
	    }
	}
    }
    udelay(100000);
    return(cnt);
}

/*----------------------------------------------------------------------------*/

int mpc107_i2c_init ( unsigned long eumb_addr, unsigned long divider )
{
    unsigned long x;

    if (eumb_addr)
	mpc107_eumb_addr = eumb_addr;
    else
	return -1;

    /* Set I2C clock */
    x = in32r(MPC107_I2CFDR) & 0xffffff00;
    out32r(MPC107_I2CFDR, (x | divider));

    /* Clear arbitration */
    out32r(MPC107_I2CSR, 0);

    return mpc107_eumb_addr;
}

/*----------------------------------------------------------------------------*/