87bc46e145be44e68cda7a51e530731d239986a1
[kugel-rb.git] / firmware / target / arm / as3525 / dbop-as3525.c
blob87bc46e145be44e68cda7a51e530731d239986a1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Bertrik Sikken
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include <inttypes.h>
24 #include "as3525.h"
25 #include "dbop-as3525.h"
27 #if defined(SANSA_FUZE) || defined(SANSA_FUZEV2)
28 #define DBOP_PRECHARGE 0x80FF
29 #elif defined(SANSA_E200V2) || defined(SANSA_C200V2)
30 #define DBOP_PRECHARGE 0xF0FF
31 #endif
33 #if CONFIG_CPU == AS3525
34 /* doesn't work with the new ams sansas so far and is not needed */
35 static short int dbop_input_value = 0;
37 #if defined(SANSA_C200V2)
39 * workaround DBOP noise issue cause it's really annoying if your
40 * buttons don't work in the debug menu...
42 static short int input_value_tmp[2];
43 int dbop_denoise_reject = 0;
44 int dbop_denoise_accept = 0;
45 #endif
47 /* read the DBOP data pins */
48 #if defined(SANSA_C200V2)
49 unsigned short dbop_read_input_once(void);
51 unsigned short dbop_read_input(void)
53 int i;
55 while (1) {
56 for (i=0; i<2; i++) {
57 input_value_tmp[i] = dbop_read_input_once();
59 /* noise rejection */
60 if (input_value_tmp[0] == input_value_tmp[1]) {
61 dbop_denoise_accept++;
62 break;
63 } else {
64 dbop_denoise_reject++;
67 if (dbop_denoise_accept + dbop_denoise_reject > 1000) {
68 dbop_denoise_accept /= 2;
69 dbop_denoise_reject /= 2;
72 return dbop_input_value;
75 unsigned short dbop_read_input_once(void)
76 #else
77 unsigned short dbop_read_input(void)
78 #endif
80 unsigned int dbop_ctrl_old = DBOP_CTRL;
81 unsigned int dbop_timpol23_old = DBOP_TIMPOL_23;
83 /* make sure that the DBOP FIFO is empty */
84 while ((DBOP_STAT & (1<<10)) == 0);
86 int delay = 10;
87 while (delay--) asm volatile ("nop\n");
89 /* write DBOP_DOUT to pre-charge DBOP data lines with a defined level */
90 DBOP_TIMPOL_23 = 0xe007e007; /* no strobe towards lcd */
91 DBOP_CTRL = (1 << 19) | /* tri-state output */
92 (1 << 16) | /* enw=1 (enable write) */
93 (1 << 12); /* ow=1 (16-bit data width) */
94 DBOP_DOUT = DBOP_PRECHARGE;
95 while ((DBOP_STAT & (1<<10)) == 0);
97 #if defined(SANSA_FUZE) || defined(SANSA_E200V2)
98 delay = 50;
99 while (delay--) asm volatile ("nop\n");
100 #endif
102 /* perform a DBOP read */
103 DBOP_CTRL = (1 << 19) | /* tri-state output */
104 (1 << 15) | /* strd=1 (start read) */
105 (1 << 12) | /* ow=1 (16-bit data width) */
106 (31 << 0); /* rs_t=31 (read DBOP at end of cycle) */
107 while ((DBOP_STAT & (1<<16)) == 0);
108 dbop_input_value = DBOP_DIN;
110 /* restore previous values */
111 DBOP_TIMPOL_23 = dbop_timpol23_old;
112 DBOP_CTRL = dbop_ctrl_old;
114 return dbop_input_value;
117 /* for the debug menu */
118 unsigned short dbop_debug(void)
120 return dbop_input_value;
123 #endif
125 static inline void dbop_set_mode(int mode)
127 int delay = 10;
128 unsigned long ctrl = DBOP_CTRL;
129 int curr_mode = (DBOP_CTRL >> 13) & 0x3; // bits 14:13
130 #ifdef SANSA_FUZEV2
131 if (mode == 32 && curr_mode != 1<<1)
132 DBOP_CTRL = (ctrl & ~(1<<13)) | (1<<14); // 2 serial half words
133 else if (mode == 16 && curr_mode != 1<<0)
134 DBOP_CTRL = (ctrl & ~(1<<14)) | (1<<13); // 2 serial bytes
135 #else
136 if (mode == 32 && curr_mode == 0)
137 DBOP_CTRL = ctrl | (1<<13|1<<14); /* 2 serial half words */
138 else if (mode == 16 && curr_mode == (1<<1|1<<0))
139 DBOP_CTRL = ctrl & ~(1<<14|1<<13); /* 1 serial half word */
140 #endif
141 else
142 return;
143 while(delay--) asm volatile("nop");
146 void dbop_write_data(const int16_t* p_bytes, int count)
149 const int32_t *data;
150 if ((intptr_t)p_bytes & 0x3 || count == 1)
151 { /* need to do a single 16bit write beforehand if the address is
152 * not word aligned or count is 1, switch to 16bit mode if needed */
153 dbop_set_mode(16);
154 DBOP_DOUT16 = *p_bytes++;
155 if (!(--count))
156 return;
158 /* from here, 32bit transfers are save
159 * set it to transfer 4*(outputwidth) units at a time,
160 * if bit 12 is set it only does 2 halfwords though (we never set it)
161 * switch to 32bit output if needed */
162 dbop_set_mode(32);
163 data = (int32_t*)p_bytes;
164 while (count > 1)
166 DBOP_DOUT32 = *data++;
167 count -= 2;
169 /* Wait if push fifo is full */
170 while ((DBOP_STAT & (1<<6)) != 0);
172 /* While push fifo is not empty */
173 while ((DBOP_STAT & (1<<10)) == 0);
175 /* due to the 32bit alignment requirement or uneven count,
176 * we possibly need to do a 16bit transfer at the end also */
177 if (count > 0)
178 dbop_write_data((int16_t*)data, 1);