summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/gigabeat/meg-fx/sc606-meg-fx.c
blob: 9e9fcb35479e47b9eedb02747c58cf534ba39ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "config.h"
#include "cpu.h"
#include <stdbool.h>
#include "kernel.h"
#include "system.h"
#include "hwcompat.h"
#include "logf.h"
#include "debug.h"
#include "string.h"

#define SLAVE_ADDRESS 0xCC

#define SDA_LO     GPHDAT &= ~(1 << 9)
#define SDA_HI     GPHDAT |= (1 << 9)
#define SDA_INPUT  GPHCON &= ~(3 << 18)
#define SDA_OUTPUT GPHCON |= (1 << 18)
#define SDA        GPHDAT & (1 << 9)

#define SCL_LO     GPHDAT &= ~(1 << 10)
#define SCL_HI     GPHDAT |= (1 << 10)
#define SCL_INPUT  GPHCON &= ~(3 << 20)
#define SCL_OUTPUT GPHCON |= (1 << 20)
#define SCL        GPHDAT & (1 << 10)

#define SCL_SDA_HI    GPHDAT |= (3 << 9)

/* The SC606 can clock at 400KHz: 2.5uS period -> 1.25uS half period */
/* At 300Mhz - if loop takes 10 cycles @ 3.3nS each -> 1.25uS / 30nS -> 40 */
#define DELAY   do { volatile int _x; for(_x=0;_x<40;_x++);} while (0)

static void sc606_i2c_start(void)
{
    SCL_SDA_HI;
    DELAY;
    SDA_LO;
    DELAY;
    SCL_LO;
}

static void sc606_i2c_restart(void)
{
    SCL_SDA_HI;
    DELAY;
    SDA_LO;
    DELAY;
    SCL_LO;
}

static void sc606_i2c_stop(void)
{
    SDA_LO;
    DELAY;
    SCL_HI;
    DELAY;
    SDA_HI;
    DELAY;
}

static void sc606_i2c_ack(void)
{

    SDA_LO;
    SCL_HI;
    DELAY;
    SCL_LO;
}

static int sc606_i2c_getack(void)
{
    int ret = 0;

    /* Don't need a delay since follows a data bit with a delay on the end */
    SDA_INPUT;   /* And set to input */
    SCL_HI;
    DELAY;

    if (SDA) /* ack failed */
        ret = 1;

    DELAY;
    SCL_LO;
    DELAY;
    SDA_OUTPUT;
    return ret;
}

static int sc606_i2c_outb(unsigned char byte)
{
    int i;

    /* clock out each bit, MSB first */
    for (i = 0x80; i; i >>= 1) {

       if (i & byte) {
           SDA_HI;
       } else {
           SDA_LO;
       }

       DELAY;
       SCL_HI;
       DELAY;
       SCL_LO;
       DELAY;
    }

    return sc606_i2c_getack();
}

static unsigned char sc606_i2c_inb(void)
{
   int i;
   unsigned char byte = 0;

   SDA_INPUT;   /* And set to input */
   /* clock in each bit, MSB first */
   for (i = 0x80; i; i >>= 1) {
       SCL_HI;

       if (SDA)
           byte |= i;

       SCL_LO;
   }
   SDA_OUTPUT;

   sc606_i2c_ack();

   return byte;
}



int sc606_write(unsigned char reg, unsigned char data)
{
    int x = 0;

    sc606_i2c_start();
    x += sc606_i2c_outb(SLAVE_ADDRESS);
    x += 0x10 * sc606_i2c_outb(reg);
    sc606_i2c_start();
/*    sc606_i2c_restart(); */
    x += 0x100 * sc606_i2c_outb(SLAVE_ADDRESS);
    x += 0x1000 *sc606_i2c_outb(data);
    sc606_i2c_stop();

    return x;
}

int sc606_read(unsigned char reg, unsigned char* data)
{
    int x = 0;

    sc606_i2c_start();
    sc606_i2c_outb(SLAVE_ADDRESS);
    sc606_i2c_outb(reg);
    sc606_i2c_restart();
    sc606_i2c_outb(SLAVE_ADDRESS | 1);
    *data = sc606_i2c_inb();
    sc606_i2c_stop();

    return x;
}

void sc606_init(void)
{
    volatile int i;
    
    /* Set GPB2 (EN) to 1 */
    GPBCON = (GPBCON & ~(3<<4)) | 1<<4;

    /* Turn enable line on */
    GPBDAT |= 1<<2;
    /* OFF GPBDAT &= ~(1 << 2); */

    /* About 400us - needs 350us */
    for (i = 200; i; i--)
    {
        DELAY;
    }

    /* Set GPH9 (SDA) and GPH10 (SCL) to 1 */
    GPHUP &= ~(3<<9);
    GPHCON = (GPHCON & ~(0xF<<18)) | 5<<18;
}