diff options
Diffstat (limited to 'firmware/target/coldfire')
-rw-r--r-- | firmware/target/coldfire/iriver/h100/sw_i2c-h100.c | 269 |
1 files changed, 0 insertions, 269 deletions
diff --git a/firmware/target/coldfire/iriver/h100/sw_i2c-h100.c b/firmware/target/coldfire/iriver/h100/sw_i2c-h100.c deleted file mode 100644 index 3b2cdc4042..0000000000 --- a/firmware/target/coldfire/iriver/h100/sw_i2c-h100.c +++ /dev/null | |||
@@ -1,269 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006 by Miika Pekkarinen | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include "system.h" | ||
20 | #include "logf.h" | ||
21 | #include "inttypes.h" | ||
22 | |||
23 | #include "sw_i2c.h" | ||
24 | |||
25 | /** | ||
26 | * I2C-functions are copied and ported from fmradio.c. | ||
27 | * later fixed, adapted and moved to a seperate file so they can be re-used by the rtc-ds1339c code by Robert Kukla | ||
28 | */ | ||
29 | |||
30 | /* cute little functions, atomic read-modify-write */ | ||
31 | |||
32 | /* SCL is GPIO, 12 */ | ||
33 | #define SCL ( 0x00001000 & GPIO_READ) | ||
34 | #define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT) | ||
35 | #define SCL_LO or_l( 0x00001000, &GPIO_ENABLE) | ||
36 | #define SCL_HI and_l(~0x00001000, &GPIO_ENABLE) | ||
37 | |||
38 | /* SDA is GPIO1, 13 */ | ||
39 | #define SDA ( 0x00002000 & GPIO1_READ) | ||
40 | #define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT) | ||
41 | #define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE) | ||
42 | #define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE) | ||
43 | |||
44 | /* delay loop to achieve 400kHz at 120MHz CPU frequency */ | ||
45 | #define DELAY \ | ||
46 | ({ \ | ||
47 | int _x_; \ | ||
48 | asm volatile ( \ | ||
49 | "move.l #21, %[_x_] \r\n" \ | ||
50 | "1: \r\n" \ | ||
51 | "subq.l #1, %[_x_] \r\n" \ | ||
52 | "bhi.b 1b \r\n" \ | ||
53 | : [_x_]"=&d"(_x_) \ | ||
54 | ); \ | ||
55 | }) | ||
56 | |||
57 | void sw_i2c_init(void) | ||
58 | { | ||
59 | or_l(0x00001000, &GPIO_FUNCTION); | ||
60 | or_l(0x00002000, &GPIO1_FUNCTION); | ||
61 | SDA_HI; | ||
62 | SCL_HI; | ||
63 | SDA_OUT_LO; | ||
64 | SCL_OUT_LO; | ||
65 | } | ||
66 | |||
67 | /* in: C=? D=? | ||
68 | * out: C=L D=L | ||
69 | */ | ||
70 | static void sw_i2c_start(void) | ||
71 | { | ||
72 | SCL_LO; | ||
73 | DELAY; | ||
74 | SDA_HI; | ||
75 | DELAY; | ||
76 | SCL_HI; | ||
77 | DELAY; | ||
78 | SDA_LO; | ||
79 | DELAY; | ||
80 | SCL_LO; | ||
81 | } | ||
82 | |||
83 | /* in: C=L D=? | ||
84 | * out: C=H D=H | ||
85 | */ | ||
86 | static void sw_i2c_stop(void) | ||
87 | { | ||
88 | SDA_LO; | ||
89 | DELAY; | ||
90 | SCL_HI; | ||
91 | DELAY; | ||
92 | SDA_HI; | ||
93 | } | ||
94 | |||
95 | /* in: C=L D=H | ||
96 | * out: C=L D=L | ||
97 | */ | ||
98 | static void sw_i2c_ack(void) | ||
99 | { | ||
100 | SDA_LO; | ||
101 | DELAY; | ||
102 | |||
103 | SCL_HI; | ||
104 | DELAY; | ||
105 | SCL_LO; | ||
106 | } | ||
107 | |||
108 | /* in: C=L D=H | ||
109 | * out: C=L D=H | ||
110 | */ | ||
111 | static void sw_i2c_nack(void) | ||
112 | { | ||
113 | SDA_HI; /* redundant */ | ||
114 | DELAY; | ||
115 | |||
116 | SCL_HI; | ||
117 | DELAY; | ||
118 | SCL_LO; | ||
119 | } | ||
120 | |||
121 | /* in: C=L D=? | ||
122 | * out: C=L D=H | ||
123 | */ | ||
124 | static bool sw_i2c_getack(void) | ||
125 | { | ||
126 | bool ret = true; | ||
127 | /* int count = 10; */ | ||
128 | |||
129 | SDA_HI; /* sets to input */ | ||
130 | DELAY; | ||
131 | SCL_HI; | ||
132 | DELAY; | ||
133 | |||
134 | /* while (SDA && count--) */ | ||
135 | /* DELAY; */ | ||
136 | |||
137 | if (SDA) | ||
138 | /* ack failed */ | ||
139 | ret = false; | ||
140 | |||
141 | SCL_LO; | ||
142 | |||
143 | return ret; | ||
144 | } | ||
145 | |||
146 | /* in: C=L D=? | ||
147 | * out: C=L D=? | ||
148 | */ | ||
149 | static void sw_i2c_outb(unsigned char byte) | ||
150 | { | ||
151 | int i; | ||
152 | |||
153 | /* clock out each bit, MSB first */ | ||
154 | for ( i=0x80; i; i>>=1 ) | ||
155 | { | ||
156 | if ( i & byte ) | ||
157 | SDA_HI; | ||
158 | else | ||
159 | SDA_LO; | ||
160 | DELAY; | ||
161 | |||
162 | SCL_HI; | ||
163 | DELAY; | ||
164 | SCL_LO; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /* in: C=L D=? | ||
169 | * out: C=L D=H | ||
170 | */ | ||
171 | static unsigned char sw_i2c_inb(void) | ||
172 | { | ||
173 | int i; | ||
174 | unsigned char byte = 0; | ||
175 | |||
176 | SDA_HI; /* sets to input */ | ||
177 | |||
178 | /* clock in each bit, MSB first */ | ||
179 | for ( i=0x80; i; i>>=1 ) | ||
180 | { | ||
181 | DELAY; | ||
182 | do { | ||
183 | SCL_HI; | ||
184 | DELAY; | ||
185 | } | ||
186 | while(SCL==0); /* wait for any SCL clock stretching */ | ||
187 | if ( SDA ) | ||
188 | byte |= i; | ||
189 | SCL_LO; | ||
190 | } | ||
191 | |||
192 | return byte; | ||
193 | } | ||
194 | |||
195 | int sw_i2c_write(unsigned char chip, unsigned char location, const unsigned char* buf, int count) | ||
196 | { | ||
197 | int i; | ||
198 | |||
199 | sw_i2c_start(); | ||
200 | sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE); | ||
201 | if (!sw_i2c_getack()) | ||
202 | { | ||
203 | sw_i2c_stop(); | ||
204 | return -1; | ||
205 | } | ||
206 | |||
207 | sw_i2c_outb(location); | ||
208 | if (!sw_i2c_getack()) | ||
209 | { | ||
210 | sw_i2c_stop(); | ||
211 | return -2; | ||
212 | } | ||
213 | |||
214 | for (i=0; i<count; i++) | ||
215 | { | ||
216 | sw_i2c_outb(buf[i]); | ||
217 | if (!sw_i2c_getack()) | ||
218 | { | ||
219 | sw_i2c_stop(); | ||
220 | return -3; | ||
221 | } | ||
222 | } | ||
223 | |||
224 | sw_i2c_stop(); | ||
225 | |||
226 | return 0; | ||
227 | } | ||
228 | |||
229 | int sw_i2c_read(unsigned char chip, unsigned char location, unsigned char* buf, int count) | ||
230 | { | ||
231 | int i; | ||
232 | |||
233 | sw_i2c_start(); | ||
234 | sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE); | ||
235 | if (!sw_i2c_getack()) | ||
236 | { | ||
237 | sw_i2c_stop(); | ||
238 | return -1; | ||
239 | } | ||
240 | |||
241 | sw_i2c_outb(location); | ||
242 | if (!sw_i2c_getack()) | ||
243 | { | ||
244 | sw_i2c_stop(); | ||
245 | return -2; | ||
246 | } | ||
247 | |||
248 | sw_i2c_start(); | ||
249 | sw_i2c_outb((chip & 0xfe) | SW_I2C_READ); | ||
250 | if (!sw_i2c_getack()) | ||
251 | { | ||
252 | sw_i2c_stop(); | ||
253 | return -3; | ||
254 | } | ||
255 | |||
256 | for (i=0; i<count-1; i++) | ||
257 | { | ||
258 | buf[i] = sw_i2c_inb(); | ||
259 | sw_i2c_ack(); | ||
260 | } | ||
261 | |||
262 | /* 1byte min */ | ||
263 | buf[i] = sw_i2c_inb(); | ||
264 | sw_i2c_nack(); | ||
265 | |||
266 | sw_i2c_stop(); | ||
267 | |||
268 | return 0; | ||
269 | } | ||