]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/eth/arm/netarm/v2_0/src/eeprom.c
Initial revision
[karo-tx-redboot.git] / packages / devs / eth / arm / netarm / v2_0 / src / eeprom.c
1 // ====================================================================
2 //
3 //      eeprom.c
4 //
5 // ====================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Copyright (C) 2005 eCosCentric LTD
10 //
11 // eCos is free software; you can redistribute it and/or modify it under
12 // the terms of the GNU General Public License as published by the Free
13 // Software Foundation; either version 2 or (at your option) any later version.
14 //
15 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
16 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 // for more details.
19 //
20 // You should have received a copy of the GNU General Public License along
21 // with eCos; if not, write to the Free Software Foundation, Inc.,
22 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 //
24 // As a special exception, if other files instantiate templates or use macros
25 // or inline functions from this file, or you compile this file and link it
26 // with other works to produce a work based on this file, this file does not
27 // by itself cause the resulting work to be covered by the GNU General Public
28 // License. However the source code for this file must still be made available
29 // in accordance with section (3) of the GNU General Public License.
30 //
31 // This exception does not invalidate any other reasons why a work based on
32 // this file might be covered by the GNU General Public License.
33 // ====================================================================
34 //#####DESCRIPTIONBEGIN####
35 //
36 // Author(s):           Harald Brandl (harald.brandl@fh-joanneum.at)
37 // Contributors:        Harald Brandl
38 // Date:                10.03.2005
39 // Purpose:             EEPROM I/O
40 // Description:
41 //
42 //####DESCRIPTIONEND####
43 //
44 // ====================================================================
45
46 #include <cyg/hal/hal_netarm.h>
47 #include <sys/types.h>
48
49 static void wait(int time)
50 {
51   int i;
52   
53   for(i=0; i < time; i++);
54 }
55
56 static void i2cStop(void)
57 {
58   *PORTC |= 0x00c00000;
59   *PORTC &= ~0x000000c0;  // SDA = 0, SLK = 0
60   wait(5);
61   *PORTC |= 0x00000040;   // SLK = 1
62   wait(5);
63   *PORTC |= 0x00000080;   // SDA = 1
64 }
65
66 static void i2cStart(void)
67 {
68   *PORTC |= 0x00c000c0;   // SDA = 1, SLK = 1
69   wait(5);
70   *PORTC &= ~0x00000080;  // SDA = 0
71   wait(5);
72   *PORTC &= ~0x00000040;  // SLK = 0
73 }
74
75 static void i2cPutByte(char byte)
76 {
77   int i, bit;
78   
79   *PORTC |= 0x00c00000;
80   
81   for(i=7; i >= 0; i--)
82     {
83       bit = (byte >> i) & 1;
84       *PORTC = (*PORTC & ~0x80) | (bit << 7); // SDA = data
85       *PORTC |= 0x00000040;   // SLK = 1
86       wait(5);
87       *PORTC &= ~0x00000040;  // SLK = 0
88       wait(5);
89     }
90   *PORTC |= 0x00000080;           // SDA = 1
91 }
92
93 static char i2cGetByte(void)
94 {
95   int i;
96   char byte = 0;
97   
98   *PORTC &= ~0x00800000;
99   for(i=7; i >= 0; i--)
100     {
101       *PORTC |= 0x00000040;   // SLK = 1
102       byte |= ((*PORTC & 0x80) >> 7) << i;    // data = SDA
103       wait(5);
104       *PORTC &= ~0x00000040;  // SLK = 0
105       wait(5);
106     }
107   *PORTC |= 0x00800080;           // SDA = 1
108   return byte;
109 }
110
111 static void i2cGiveAck(void)
112 {
113   *PORTC |= 0x00c00000;
114   *PORTC &= ~0x00000080;  // SDA = 0
115   wait(5);
116   *PORTC |= 0x00000040;   // SLK = 1
117   wait(5);
118   *PORTC &= ~0x00000040;  // SLK = 0
119   wait(5);
120   *PORTC |= 0x00000080;   // SDA = 1
121   wait(5);
122 }
123
124 static void i2cGetAck(void)
125 {
126   *PORTC &= ~0x00800000;  // SDA in
127   *PORTC |= 0x00400040;   // SLK = 1
128   while(*PORTC & 0x80);   // wait for SDA = 1
129   *PORTC &= ~0x00000040;  // SLK = 0
130 }
131
132 void initI2C(void)
133 {
134   *PORTC &= ~0xc0000000;  // mode GPIO
135   i2cStop();
136 }
137
138 void eepromPollAck(int deviceAddr)
139 {
140   deviceAddr <<= 1;
141   
142   *PORTC |= 0x00400040;   // SLK = 1
143   while(1)
144     {
145       i2cStart();
146       i2cPutByte(deviceAddr);
147       *PORTC &= ~0x00800000;          // SDA in
148       *PORTC |= 0x00400040;           // SLK = 1
149       if((*PORTC & 0x80) == 0)
150         {
151           *PORTC &= ~0x00000040;  // SLK = 0
152           break;
153         }
154       *PORTC &= ~0x00000040;          // SLK = 0
155     }
156 }
157
158 void eepromRead(int deviceAddr, int readAddr, char *buf, int numBytes)
159 {
160   int i;
161   
162   deviceAddr <<= 1;
163   i2cStart();
164   i2cPutByte(deviceAddr);
165   i2cGetAck();
166   i2cPutByte(readAddr >> 8);
167   i2cGetAck();
168   i2cPutByte(readAddr & 0xff);
169   i2cGetAck();
170   i2cStart();
171   i2cPutByte(deviceAddr | 1);             // set read flag
172   i2cGetAck();
173   
174   for(i=0;i<numBytes;i++)
175     {
176       buf[i] = i2cGetByte();
177       if(i < numBytes - 1)
178         i2cGiveAck();
179     }
180   
181   i2cStop();
182 }
183
184 void eepromWrite(int deviceAddr, int writeAddr, char *buf, int numBytes)
185 {
186   int i;
187   
188   deviceAddr <<= 1;
189   i2cStart();
190   i2cPutByte(deviceAddr);
191   i2cGetAck();
192   i2cPutByte(writeAddr >> 8);
193   i2cGetAck();
194   i2cPutByte(writeAddr & 0xff);
195   i2cGetAck();
196   
197   for(i=0; i<numBytes; i++)
198     {
199       i2cPutByte(buf[i]);
200       i2cGetAck();
201     }
202   
203   i2cStop();
204 }