diff options
Diffstat (limited to 'firmware/target/arm/pp/i2c-pp.c')
-rw-r--r-- | firmware/target/arm/pp/i2c-pp.c | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/firmware/target/arm/pp/i2c-pp.c b/firmware/target/arm/pp/i2c-pp.c new file mode 100644 index 0000000000..58740b5c66 --- /dev/null +++ b/firmware/target/arm/pp/i2c-pp.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * PP502X and PP5002 I2C driver | ||
11 | * | ||
12 | * Based on code from the ipodlinux project - http://ipodlinux.org/ | ||
13 | * Adapted for Rockbox in November 2005 | ||
14 | * | ||
15 | * Original file: linux/arch/armnommu/mach-ipod/hardware.c | ||
16 | * | ||
17 | * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org) | ||
18 | * | ||
19 | * This program is free software; you can redistribute it and/or | ||
20 | * modify it under the terms of the GNU General Public License | ||
21 | * as published by the Free Software Foundation; either version 2 | ||
22 | * of the License, or (at your option) any later version. | ||
23 | * | ||
24 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
25 | * KIND, either express or implied. | ||
26 | * | ||
27 | ****************************************************************************/ | ||
28 | |||
29 | #include "cpu.h" | ||
30 | #include "kernel.h" | ||
31 | #include "thread.h" | ||
32 | #include "logf.h" | ||
33 | #include "system.h" | ||
34 | #include "i2c.h" | ||
35 | #include "i2c-pp.h" | ||
36 | #include "ascodec.h" | ||
37 | #include "as3514.h" | ||
38 | |||
39 | #define I2C_CTRL (*(volatile unsigned char*)(I2C_BASE+0x00)) | ||
40 | #define I2C_ADDR (*(volatile unsigned char*)(I2C_BASE+0x04)) | ||
41 | #define I2C_DATA(X) (*(volatile unsigned char*)(I2C_BASE+0xc+(4*X))) | ||
42 | #define I2C_STATUS (*(volatile unsigned char*)(I2C_BASE+0x1c)) | ||
43 | |||
44 | /* I2C_CTRL bit definitions */ | ||
45 | #define I2C_SEND 0x80 | ||
46 | |||
47 | /* I2C_STATUS bit definitions */ | ||
48 | #define I2C_BUSY (1<<6) | ||
49 | |||
50 | /* Local functions definitions */ | ||
51 | static struct mutex i2c_mtx SHAREDBSS_ATTR; | ||
52 | |||
53 | #define POLL_TIMEOUT (HZ) | ||
54 | |||
55 | static int pp_i2c_wait_not_busy(void) | ||
56 | { | ||
57 | unsigned long timeout; | ||
58 | timeout = current_tick + POLL_TIMEOUT; | ||
59 | while (TIME_BEFORE(current_tick, timeout)) { | ||
60 | if (!(I2C_STATUS & I2C_BUSY)) { | ||
61 | return 0; | ||
62 | } | ||
63 | yield(); | ||
64 | } | ||
65 | |||
66 | return -1; | ||
67 | } | ||
68 | |||
69 | static int pp_i2c_read_bytes(unsigned int addr, int len, unsigned char *data) | ||
70 | { | ||
71 | int i; | ||
72 | |||
73 | if (len < 1 || len > 4) | ||
74 | { | ||
75 | return -1; | ||
76 | } | ||
77 | |||
78 | if (pp_i2c_wait_not_busy() < 0) | ||
79 | { | ||
80 | return -2; | ||
81 | } | ||
82 | |||
83 | { | ||
84 | int old_irq_level = disable_irq_save(); | ||
85 | |||
86 | /* clear top 15 bits, left shift 1, or in 0x1 for a read */ | ||
87 | I2C_ADDR = ((addr << 17) >> 16) | 0x1; | ||
88 | |||
89 | I2C_CTRL |= 0x20; | ||
90 | |||
91 | I2C_CTRL = (I2C_CTRL & ~0x6) | ((len-1) << 1); | ||
92 | |||
93 | I2C_CTRL |= I2C_SEND; | ||
94 | |||
95 | restore_irq(old_irq_level); | ||
96 | |||
97 | if (pp_i2c_wait_not_busy() < 0) | ||
98 | { | ||
99 | return -2; | ||
100 | } | ||
101 | |||
102 | old_irq_level = disable_irq_save(); | ||
103 | |||
104 | if (data) | ||
105 | { | ||
106 | for ( i = 0; i < len; i++ ) | ||
107 | *data++ = I2C_DATA(i); | ||
108 | } | ||
109 | |||
110 | restore_irq(old_irq_level); | ||
111 | } | ||
112 | |||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | static int pp_i2c_send_bytes(unsigned int addr, int len, unsigned char *data) | ||
117 | { | ||
118 | int i; | ||
119 | |||
120 | if (len < 1 || len > 4) | ||
121 | { | ||
122 | return -1; | ||
123 | } | ||
124 | |||
125 | if (pp_i2c_wait_not_busy() < 0) | ||
126 | { | ||
127 | return -2; | ||
128 | } | ||
129 | |||
130 | { | ||
131 | int old_irq_level = disable_irq_save(); | ||
132 | |||
133 | /* clear top 15 bits, left shift 1 */ | ||
134 | I2C_ADDR = (addr << 17) >> 16; | ||
135 | |||
136 | I2C_CTRL &= ~0x20; | ||
137 | |||
138 | for ( i = 0; i < len; i++ ) | ||
139 | { | ||
140 | I2C_DATA(i) = *data++; | ||
141 | } | ||
142 | |||
143 | I2C_CTRL = (I2C_CTRL & ~0x6) | ((len-1) << 1); | ||
144 | |||
145 | I2C_CTRL |= I2C_SEND; | ||
146 | |||
147 | restore_irq(old_irq_level); | ||
148 | } | ||
149 | |||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | static int pp_i2c_send_byte(unsigned int addr, int data0) | ||
154 | { | ||
155 | unsigned char data[1]; | ||
156 | |||
157 | data[0] = data0; | ||
158 | |||
159 | return pp_i2c_send_bytes(addr, 1, data); | ||
160 | } | ||
161 | |||
162 | /* Public functions */ | ||
163 | void i2c_lock(void) | ||
164 | { | ||
165 | mutex_lock(&i2c_mtx); | ||
166 | } | ||
167 | |||
168 | void i2c_unlock(void) | ||
169 | { | ||
170 | mutex_unlock(&i2c_mtx); | ||
171 | } | ||
172 | |||
173 | int i2c_readbytes(unsigned int dev_addr, int addr, int len, unsigned char *data) | ||
174 | { | ||
175 | int i, n; | ||
176 | |||
177 | mutex_lock(&i2c_mtx); | ||
178 | |||
179 | if (addr >= 0) | ||
180 | pp_i2c_send_byte(dev_addr, addr); | ||
181 | |||
182 | i = 0; | ||
183 | while (len > 0) | ||
184 | { | ||
185 | n = (len < 4) ? len : 4; | ||
186 | |||
187 | if (pp_i2c_read_bytes(dev_addr, n, data + i) < 0) | ||
188 | break; | ||
189 | |||
190 | len -= n; | ||
191 | i += n; | ||
192 | } | ||
193 | |||
194 | mutex_unlock(&i2c_mtx); | ||
195 | |||
196 | return i; | ||
197 | } | ||
198 | |||
199 | int i2c_readbyte(unsigned int dev_addr, int addr) | ||
200 | { | ||
201 | unsigned char data; | ||
202 | |||
203 | mutex_lock(&i2c_mtx); | ||
204 | pp_i2c_send_byte(dev_addr, addr); | ||
205 | pp_i2c_read_bytes(dev_addr, 1, &data); | ||
206 | mutex_unlock(&i2c_mtx); | ||
207 | |||
208 | return (int)data; | ||
209 | } | ||
210 | |||
211 | int i2c_sendbytes(unsigned int addr, int len, const unsigned char *data) | ||
212 | { | ||
213 | int i, n; | ||
214 | |||
215 | mutex_lock(&i2c_mtx); | ||
216 | |||
217 | i = 0; | ||
218 | while (len > 0) | ||
219 | { | ||
220 | n = (len < 4) ? len : 4; | ||
221 | |||
222 | if (pp_i2c_send_bytes(addr, n, (unsigned char *)(data + i)) < 0) | ||
223 | break; | ||
224 | |||
225 | len -= n; | ||
226 | i += n; | ||
227 | } | ||
228 | |||
229 | mutex_unlock(&i2c_mtx); | ||
230 | |||
231 | return i; | ||
232 | } | ||
233 | |||
234 | int pp_i2c_send(unsigned int addr, int data0, int data1) | ||
235 | { | ||
236 | int retval; | ||
237 | unsigned char data[2]; | ||
238 | |||
239 | data[0] = data0; | ||
240 | data[1] = data1; | ||
241 | |||
242 | mutex_lock(&i2c_mtx); | ||
243 | retval = pp_i2c_send_bytes(addr, 2, data); | ||
244 | mutex_unlock(&i2c_mtx); | ||
245 | |||
246 | return retval; | ||
247 | } | ||
248 | |||
249 | void i2c_init(void) | ||
250 | { | ||
251 | /* From ipodlinux */ | ||
252 | mutex_init(&i2c_mtx); | ||
253 | |||
254 | #ifdef IPOD_MINI | ||
255 | /* GPIO port C disable port 0x10 */ | ||
256 | GPIOC_ENABLE &= ~0x10; | ||
257 | |||
258 | /* GPIO port C disable port 0x20 */ | ||
259 | GPIOC_ENABLE &= ~0x20; | ||
260 | #endif | ||
261 | |||
262 | #if CONFIG_I2C == I2C_PP5002 | ||
263 | DEV_EN |= 0x2; | ||
264 | #else | ||
265 | DEV_EN |= DEV_I2C; /* Enable I2C */ | ||
266 | #endif | ||
267 | DEV_RS |= DEV_I2C; /* Start I2C Reset */ | ||
268 | DEV_RS &=~DEV_I2C; /* End I2C Reset */ | ||
269 | |||
270 | #if CONFIG_I2C == I2C_PP5020 | ||
271 | outl(0x0, 0x600060a4); | ||
272 | #if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || \ | ||
273 | defined(SAMSUNG_YH820) || defined(SAMSUNG_YH920) || \ | ||
274 | defined(SAMSUNG_YH925) || defined(PBELL_VIBE500) | ||
275 | outl(inl(0x600060a4) | 0x20, 0x600060a4); | ||
276 | outl(inl(0x7000c020) | 0x3, 0x7000c020); | ||
277 | outl(0x55, 0x7000c02c); | ||
278 | outl(0x54, 0x7000c030); | ||
279 | #else | ||
280 | outl(0x80 | (0 << 8), 0x600060a4); | ||
281 | #endif | ||
282 | #elif CONFIG_I2C == I2C_PP5024 | ||
283 | #if defined(SANSA_E200) || defined(PHILIPS_SA9200) | ||
284 | /* Sansa OF sets this to 0x20 first, communicates with the AS3514 | ||
285 | then sets it to 0x23 - this still works fine though */ | ||
286 | outl(0x0, 0x600060a4); | ||
287 | outl(0x23, 0x600060a4); | ||
288 | #elif defined(SANSA_C200) | ||
289 | /* This is the init sequence from the Sansa c200 bootloader. | ||
290 | I'm not sure what's really necessary. */ | ||
291 | pp_i2c_wait_not_busy(); | ||
292 | |||
293 | outl(0, 0x600060a4); | ||
294 | outl(0x64, 0x600060a4); | ||
295 | |||
296 | outl(0x55, 0x7000c02c); | ||
297 | outl(0x54, 0x7000c030); | ||
298 | |||
299 | outl(0, 0x600060a4); | ||
300 | outl(0x1e, 0x600060a4); | ||
301 | |||
302 | ascodec_write(AS3514_SUPERVISOR, 5); | ||
303 | #elif defined(PHILIPS_SA9200) | ||
304 | outl(0x0, 0x600060a4); | ||
305 | outl(inl(0x600060a4) | 0x20, 0x600060a4); | ||
306 | |||
307 | outl(inl(0x7000c020) | 0x3, 0x7000c020); | ||
308 | outl(0x55, 0x7000c02c); | ||
309 | outl(0x54, 0x7000c030); | ||
310 | #endif | ||
311 | #endif | ||
312 | |||
313 | i2c_readbyte(0x8, 0); | ||
314 | } | ||