summaryrefslogtreecommitdiff
path: root/firmware/target/arm/i2c-pp.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/i2c-pp.c')
-rw-r--r--firmware/target/arm/i2c-pp.c208
1 files changed, 208 insertions, 0 deletions
diff --git a/firmware/target/arm/i2c-pp.c b/firmware/target/arm/i2c-pp.c
new file mode 100644
index 0000000000..0c8aec8f4b
--- /dev/null
+++ b/firmware/target/arm/i2c-pp.c
@@ -0,0 +1,208 @@
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 * All files in this archive are subject to the GNU General Public License.
20 * See the file COPYING in the source tree root for full license agreement.
21 *
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
24 *
25 ****************************************************************************/
26
27#include "cpu.h"
28#include "kernel.h"
29#include "logf.h"
30#include "system.h"
31#if CONFIG_I2C == I2C_PP5002
32#include "i2c-pp5002.h"
33#else
34#include "i2c-pp5020.h"
35#endif
36
37/* Local functions definitions */
38
39#define POLL_TIMEOUT (HZ)
40
41static int pp_i2c_wait_not_busy(void)
42{
43 unsigned long timeout;
44 timeout = current_tick + POLL_TIMEOUT;
45 while (TIME_BEFORE(current_tick, timeout)) {
46 if (!(I2C_STATUS & I2C_BUSY)) {
47 return 0;
48 }
49 yield();
50 }
51
52 return -1;
53}
54
55static int pp_i2c_read_byte(unsigned int addr, unsigned int *data)
56{
57 if (pp_i2c_wait_not_busy() < 0)
58 {
59 return -1;
60 }
61
62 {
63 unsigned int byte;
64 int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
65
66 /* clear top 15 bits, left shift 1, or in 0x1 for a read */
67 I2C_ADDR = ((addr << 17) >> 16) | 0x1 ;
68
69 I2C_CTRL |= 0x20;
70
71 I2C_CTRL |= I2C_SEND;
72
73 set_irq_level(old_irq_level);
74 if (pp_i2c_wait_not_busy() < 0)
75 {
76 return -1;
77 }
78 old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
79
80 byte = I2C_DATA(0);
81
82 if (data)
83 *data = byte;
84
85 set_irq_level(old_irq_level);
86 }
87
88 return 0;
89}
90
91static int pp_i2c_send_bytes(unsigned int addr, unsigned int len, unsigned char *data)
92{
93 unsigned int i;
94
95 if (len < 1 || len > 4)
96 {
97 return -1;
98 }
99
100 if (pp_i2c_wait_not_busy() < 0)
101 {
102 return -2;
103 }
104
105 {
106 int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
107
108 /* clear top 15 bits, left shift 1 */
109 I2C_ADDR = (addr << 17) >> 16;
110
111 I2C_CTRL &= ~0x20;
112
113 for ( i = 0; i < len; i++ )
114 {
115 I2C_DATA(i) = *data++;
116 }
117
118 I2C_CTRL = (I2C_CTRL & ~0x26) | ((len-1) << 1);
119
120 I2C_CTRL |= I2C_SEND;
121
122 set_irq_level(old_irq_level);
123 }
124
125 return 0x0;
126}
127
128static int pp_i2c_send_byte(unsigned int addr, int data0)
129{
130 unsigned char data[1];
131
132 data[0] = data0;
133
134 return pp_i2c_send_bytes(addr, 1, data);
135}
136
137/* Public functions */
138static struct mutex i2c_mutex;
139
140int i2c_readbytes(unsigned int dev_addr, int addr, int len, unsigned char *data) {
141 unsigned int temp;
142 int i;
143 mutex_lock(&i2c_mutex);
144 pp_i2c_send_byte(dev_addr, addr);
145 for (i = 0; i < len; i++) {
146 pp_i2c_read_byte(dev_addr, &temp);
147 data[i] = temp;
148 }
149 mutex_unlock(&i2c_mutex);
150 return i;
151}
152
153int i2c_readbyte(unsigned int dev_addr, int addr)
154{
155 int data;
156
157 mutex_lock(&i2c_mutex);
158 pp_i2c_send_byte(dev_addr, addr);
159 pp_i2c_read_byte(dev_addr, &data);
160 mutex_unlock(&i2c_mutex);
161
162 return data;
163}
164
165int pp_i2c_send(unsigned int addr, int data0, int data1)
166{
167 int retval;
168 unsigned char data[2];
169
170 data[0] = data0;
171 data[1] = data1;
172
173 mutex_lock(&i2c_mutex);
174 retval = pp_i2c_send_bytes(addr, 2, data);
175 mutex_unlock(&i2c_mutex);
176
177 return retval;
178}
179
180void i2c_init(void)
181{
182 /* From ipodlinux */
183
184#ifdef IPOD_MINI
185 /* GPIO port C disable port 0x10 */
186 GPIOC_ENABLE &= ~0x10;
187
188 /* GPIO port C disable port 0x20 */
189 GPIOC_ENABLE &= ~0x20;
190#endif
191
192#if CONFIG_I2C == I2C_PP5002
193 DEV_EN |= 0x2;
194#else
195 DEV_EN |= DEV_I2C; /* Enable I2C */
196#endif
197 DEV_RS |= DEV_I2C; /* Start I2C Reset */
198 DEV_RS &=~DEV_I2C; /* End I2C Reset */
199
200#if CONFIG_I2C == I2C_PP5020
201 outl(0x0, 0x600060a4);
202 outl(0x80 | (0 << 8), 0x600060a4);
203#endif
204
205 mutex_init(&i2c_mutex);
206
207 i2c_readbyte(0x8, 0);
208}