Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 30 | static void i2c_sda_output(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 31 | { |
| 32 | GPECON |= (1 << 30); |
| 33 | } |
| 34 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 35 | static void i2c_sda_input(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 36 | { |
| 37 | GPECON &= ~(3 << 30); |
| 38 | } |
| 39 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 40 | static void i2c_sda_lo(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 41 | { |
| 42 | GPEDAT &= ~(1 << 15); |
| 43 | } |
| 44 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 45 | static void i2c_sda_hi(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 46 | { |
| 47 | GPEDAT |= (1 << 15); |
| 48 | } |
| 49 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 50 | static int i2c_sda(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 51 | { |
| 52 | return GPEDAT & (1 << 15); |
| 53 | } |
| 54 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 55 | static void i2c_scl_output(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 56 | { |
| 57 | GPECON |= (1 << 28); |
| 58 | } |
| 59 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 60 | static void i2c_scl_input(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 61 | { |
| 62 | GPECON &= ~(3 << 28); |
| 63 | } |
| 64 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 65 | static void i2c_scl_lo(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 66 | { |
| 67 | GPEDAT &= ~(1 << 14); |
| 68 | } |
| 69 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 70 | static int i2c_scl(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 71 | { |
| 72 | return GPEDAT & (1 << 14); |
| 73 | } |
| 74 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 75 | static void i2c_scl_hi(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 76 | { |
| 77 | i2c_scl_input(); |
| 78 | while(!i2c_scl()); |
| 79 | GPEDAT |= (1 << 14); |
| 80 | i2c_scl_output(); |
| 81 | } |
| 82 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 83 | |
Steve Gotthardt | f97d24f | 2007-01-20 22:16:01 +0000 | [diff] [blame] | 84 | |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 85 | static void i2c_delay(void) |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 86 | { |
Steve Gotthardt | f97d24f | 2007-01-20 22:16:01 +0000 | [diff] [blame] | 87 | unsigned _x; |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 88 | |
| 89 | /* The i2c can clock at 500KHz: 2uS period -> 1uS half period */ |
Steve Gotthardt | f97d24f | 2007-01-20 22:16:01 +0000 | [diff] [blame] | 90 | /* 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 Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 94 | { |
| 95 | /* burn CPU cycles */ |
Steve Gotthardt | f97d24f | 2007-01-20 22:16:01 +0000 | [diff] [blame] | 96 | /* gcc makes it an inc loop - check with objdump for asm timing */ |
Steve Gotthardt | 2aa3e3f | 2007-01-04 03:31:03 +0000 | [diff] [blame] | 97 | } |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Steve Gotthardt | f97d24f | 2007-01-20 22:16:01 +0000 | [diff] [blame] | 100 | |
| 101 | |
Marcoen Hirschberg | 8e1e6f9 | 2006-09-29 10:54:01 +0000 | [diff] [blame] | 102 | struct 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 | |
| 125 | void 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 | |
| 132 | void i2c_send(int bus_address, int reg_address, const unsigned char buf) |
| 133 | { |
| 134 | i2c_write_data(bus_address, reg_address, &buf, 1); |
| 135 | } |