summaryrefslogtreecommitdiff
path: root/firmware/target
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target')
-rw-r--r--firmware/target/arm/i2c-pp5020.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/firmware/target/arm/i2c-pp5020.c b/firmware/target/arm/i2c-pp5020.c
new file mode 100644
index 0000000000..95a677ea01
--- /dev/null
+++ b/firmware/target/arm/i2c-pp5020.c
@@ -0,0 +1,198 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * PP5020 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#include "i2c-pp5020.h"
32
33/* Local functions definitions */
34
35#define POLL_TIMEOUT (HZ)
36
37static int pp_i2c_wait_not_busy(void)
38{
39 unsigned long timeout;
40 timeout = current_tick + POLL_TIMEOUT;
41 while (TIME_BEFORE(current_tick, timeout)) {
42 if (!(I2C_STATUS & I2C_BUSY)) {
43 return 0;
44 }
45 yield();
46 }
47
48 return -1;
49}
50
51static int pp_i2c_read_byte(unsigned int addr, unsigned int *data)
52{
53 if (pp_i2c_wait_not_busy() < 0)
54 {
55 return -1;
56 }
57
58 {
59 unsigned int byte;
60 int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
61
62 /* clear top 15 bits, left shift 1, or in 0x1 for a read */
63 I2C_ADDR = ((addr << 17) >> 16) | 0x1 ;
64
65 I2C_CTRL |= 0x20;
66
67 I2C_CTRL |= I2C_SEND;
68
69 set_irq_level(old_irq_level);
70 if (pp_i2c_wait_not_busy() < 0)
71 {
72 return -1;
73 }
74 old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
75
76 byte = I2C_DATA(0);
77
78 if (data)
79 *data = byte;
80
81 set_irq_level(old_irq_level);
82 }
83
84 return 0;
85}
86
87static int pp_i2c_send_bytes(unsigned int addr, unsigned int len, unsigned char *data)
88{
89 unsigned int i;
90
91 if (len < 1 || len > 4)
92 {
93 return -1;
94 }
95
96 if (pp_i2c_wait_not_busy() < 0)
97 {
98 return -2;
99 }
100
101 {
102 int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
103
104 /* clear top 15 bits, left shift 1 */
105 I2C_ADDR = (addr << 17) >> 16;
106
107 I2C_CTRL &= ~0x20;
108
109 for ( i = 0; i < len; i++ )
110 {
111 I2C_DATA(i) = *data++;
112 }
113
114 I2C_CTRL = (I2C_CTRL & ~0x26) | ((len-1) << 1);
115
116 I2C_CTRL |= I2C_SEND;
117
118 set_irq_level(old_irq_level);
119 }
120
121 return 0x0;
122}
123
124static int pp_i2c_send_byte(unsigned int addr, int data0)
125{
126 unsigned char data[1];
127
128 data[0] = data0;
129
130 return pp_i2c_send_bytes(addr, 1, data);
131}
132
133/* Public functions */
134static struct mutex i2c_mutex;
135
136int i2c_readbytes(unsigned int dev_addr, int addr, int len, unsigned char *data) {
137 unsigned int temp;
138 int i;
139 mutex_lock(&i2c_mutex);
140 pp_i2c_send_byte(dev_addr, addr);
141 for (i = 0; i < len; i++) {
142 pp_i2c_read_byte(dev_addr, &temp);
143 data[i] = temp;
144 }
145 mutex_unlock(&i2c_mutex);
146 return i;
147}
148
149int i2c_readbyte(unsigned int dev_addr, int addr)
150{
151 int data;
152
153 mutex_lock(&i2c_mutex);
154 pp_i2c_send_byte(dev_addr, addr);
155 pp_i2c_read_byte(dev_addr, &data);
156 mutex_unlock(&i2c_mutex);
157
158 return data;
159}
160
161int pp_i2c_send(unsigned int addr, int data0, int data1)
162{
163 int retval;
164 unsigned char data[2];
165
166 data[0] = data0;
167 data[1] = data1;
168
169 mutex_lock(&i2c_mutex);
170 retval = pp_i2c_send_bytes(addr, 2, data);
171 mutex_unlock(&i2c_mutex);
172
173 return retval;
174}
175
176void i2c_init(void)
177{
178 /* From ipodlinux */
179
180#ifdef IPOD_MINI
181 /* GPIO port C disable port 0x10 */
182 GPIOC_ENABLE &= ~0x10;
183
184 /* GPIO port C disable port 0x20 */
185 GPIOC_ENABLE &= ~0x20;
186#endif
187
188 DEV_EN |= DEV_I2C;
189 DEV_RS |= DEV_I2C;
190 DEV_RS &=~DEV_I2C;
191
192 outl(0x0, 0x600060a4);
193 outl(0x80 | (0 << 8), 0x600060a4);
194
195 mutex_init(&i2c_mutex);
196
197 i2c_readbyte(0x8, 0);
198}