blob: fd5d9577a3ab7b9f55c86a4c7a048b82b62ee56a [file] [log] [blame]
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
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 "config.h"
20#include "cpu.h"
21#include <stdbool.h>
22#include "kernel.h"
23#include "system.h"
24#include "hwcompat.h"
25#include "logf.h"
26#include "debug.h"
27#include "string.h"
28#include "generic_i2c.h"
29
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000030static void i2c_sda_output(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000031{
32 GPECON |= (1 << 30);
33}
34
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000035static void i2c_sda_input(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000036{
37 GPECON &= ~(3 << 30);
38}
39
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000040static void i2c_sda_lo(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000041{
42 GPEDAT &= ~(1 << 15);
43}
44
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000045static void i2c_sda_hi(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000046{
47 GPEDAT |= (1 << 15);
48}
49
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000050static int i2c_sda(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000051{
52 return GPEDAT & (1 << 15);
53}
54
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000055static void i2c_scl_output(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000056{
57 GPECON |= (1 << 28);
58}
59
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000060static void i2c_scl_input(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000061{
62 GPECON &= ~(3 << 28);
63}
64
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000065static void i2c_scl_lo(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000066{
67 GPEDAT &= ~(1 << 14);
68}
69
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000070static int i2c_scl(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000071{
72 return GPEDAT & (1 << 14);
73}
74
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000075static void i2c_scl_hi(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000076{
77 i2c_scl_input();
78 while(!i2c_scl());
79 GPEDAT |= (1 << 14);
80 i2c_scl_output();
81}
82
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000083
Steve Gotthardtf97d24f2007-01-20 22:16:01 +000084
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000085static void i2c_delay(void)
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000086{
Steve Gotthardtf97d24f2007-01-20 22:16:01 +000087 unsigned _x;
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000088
89 /* The i2c can clock at 500KHz: 2uS period -> 1uS half period */
Steve Gotthardtf97d24f2007-01-20 22:16:01 +000090 /* about 30 cycles overhead + X * 7 */
91 /* 300MHz: 1000nS @3.36nS/cyc = 297cyc: X = 38*/
92 /* 100MHz: 1000nS @10nS/cyc = 100cyc : X = 10 */
93 for (_x = get_cpu_boost_counter() ? 38 : 10; _x; _x--)
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000094 {
95 /* burn CPU cycles */
Steve Gotthardtf97d24f2007-01-20 22:16:01 +000096 /* gcc makes it an inc loop - check with objdump for asm timing */
Steve Gotthardt2aa3e3f2007-01-04 03:31:03 +000097 }
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +000098}
99
Steve Gotthardtf97d24f2007-01-20 22:16:01 +0000100
101
Marcoen Hirschberg8e1e6f92006-09-29 10:54:01 +0000102struct i2c_interface s3c2440_i2c = {
103 0x34, /* Address */
104
105 /* Bit-banged interface definitions */
106 i2c_scl_hi, /* Drive SCL high, might sleep on clk stretch */
107 i2c_scl_lo, /* Drive SCL low */
108 i2c_sda_hi, /* Drive SDA high */
109 i2c_sda_lo, /* Drive SDA low */
110 i2c_sda_input, /* Set SDA as input */
111 i2c_sda_output, /* Set SDA as output */
112 i2c_scl_input, /* Set SCL as input */
113 i2c_scl_output, /* Set SCL as output */
114 i2c_scl, /* Read SCL, returns 0 or nonzero */
115 i2c_sda, /* Read SDA, returns 0 or nonzero */
116
117 i2c_delay, /* START SDA hold time (tHD:SDA) */
118 i2c_delay, /* SDA hold time (tHD:DAT) */
119 i2c_delay, /* SDA setup time (tSU:DAT) */
120 i2c_delay, /* STOP setup time (tSU:STO) */
121 i2c_delay, /* Rep. START setup time (tSU:STA) */
122 i2c_delay, /* SCL high period (tHIGH) */
123};
124
125void i2c_init(void)
126{
127 /* Set GPE15 (SDA) and GPE14 (SCL) to 1 */
128 GPECON = (GPECON & ~(0xF<<28)) | 5<<28;
129 i2c_add_node(&s3c2440_i2c);
130}
131
132void i2c_send(int bus_address, int reg_address, const unsigned char buf)
133{
134 i2c_write_data(bus_address, reg_address, &buf, 1);
135}