aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-lh7a40x/ssp-cpld.c
blob: 4cd31bb8a8b87f69cf1ec814abb42c68db5e8554 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* arch/arm/mach-lh7a40x/ssp-cpld.c
 *
 *  Copyright (C) 2004,2005 Marc Singer
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  version 2 as published by the Free Software Foundation.
 *
 * SSP/SPI driver for the CardEngine CPLD.
 *
 */

/* NOTES
   -----

   o *** This driver is cribbed from the 7952x implementation.
	 Some comments may not apply.

   o This driver contains sufficient logic to control either the
     serial EEPROMs or the audio codec.  It is included in the kernel
     to support the codec.  The EEPROMs are really the responsibility
     of the boot loader and should probably be left alone.

   o The code must be augmented to cope with multiple, simultaneous
     clients.
     o The audio codec writes to the codec chip whenever playback
       starts.
     o The touchscreen driver writes to the ads chip every time it
       samples.
     o The audio codec must write 16 bits, but the touch chip writes
       are 8 bits long.
     o We need to be able to keep these configurations separate while
       simultaneously active.

 */

#include <linux/module.h>
#include <linux/kernel.h>
//#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
//#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/spinlock.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/arch/hardware.h>

#include <asm/arch/ssp.h>

//#define TALK

#if defined (TALK)
#define PRINTK(f...)		printk (f)
#else
#define PRINTK(f...)		do {} while (0)
#endif

#if defined (CONFIG_ARCH_LH7A400)
# define CPLD_SPID		__REGP16(CPLD06_VIRT) /* SPI data */
# define CPLD_SPIC		__REGP16(CPLD08_VIRT) /* SPI control */
# define CPLD_SPIC_CS_CODEC	(1<<0)
# define CPLD_SPIC_CS_TOUCH	(1<<1)
# define CPLD_SPIC_WRITE	(0<<2)
# define CPLD_SPIC_READ		(1<<2)
# define CPLD_SPIC_DONE		(1<<3) /* r/o */
# define CPLD_SPIC_LOAD		(1<<4)
# define CPLD_SPIC_START	(1<<4)
# define CPLD_SPIC_LOADED	(1<<5) /* r/o */
#endif

#define CPLD_SPI		__REGP16(CPLD0A_VIRT) /* SPI operation */
#define CPLD_SPI_CS_EEPROM	(1<<3)
#define CPLD_SPI_SCLK		(1<<2)
#define CPLD_SPI_TX_SHIFT	(1)
#define CPLD_SPI_TX		(1<<CPLD_SPI_TX_SHIFT)
#define CPLD_SPI_RX_SHIFT	(0)
#define CPLD_SPI_RX		(1<<CPLD_SPI_RX_SHIFT)

/* *** FIXME: these timing values are substantially larger than the
   *** chip requires. We may implement an nsleep () function. */
#define T_SKH	1		/* Clock time high (us) */
#define T_SKL	1		/* Clock time low (us) */
#define T_CS	1		/* Minimum chip select low time (us)  */
#define T_CSS	1		/* Minimum chip select setup time (us)  */
#define T_DIS	1		/* Data setup time (us) */

	 /* EEPROM SPI bits */
#define P_START		(1<<9)
#define P_WRITE		(1<<7)
#define P_READ		(2<<7)
#define P_ERASE		(3<<7)
#define P_EWDS		(0<<7)
#define P_WRAL		(0<<7)
#define P_ERAL		(0<<7)
#define P_EWEN		(0<<7)
#define P_A_EWDS	(0<<5)
#define P_A_WRAL	(1<<5)
#define P_A_ERAL	(2<<5)
#define P_A_EWEN	(3<<5)

struct ssp_configuration {
	int device;
	int mode;
	int speed;
	int frame_size_write;
	int frame_size_read;
};

static struct ssp_configuration ssp_configuration;
static spinlock_t ssp_lock;

static void enable_cs (void)
{
	switch (ssp_configuration.device) {
	case DEVICE_EEPROM:
		CPLD_SPI |= CPLD_SPI_CS_EEPROM;
		break;
	}
	udelay (T_CSS);
}

static void disable_cs (void)
{
	switch (ssp_configuration.device) {
	case DEVICE_EEPROM:
		CPLD_SPI &= ~CPLD_SPI_CS_EEPROM;
		break;
	}
	udelay (T_CS);
}

static void pulse_clock (void)
{
	CPLD_SPI |=  CPLD_SPI_SCLK;
	udelay (T_SKH);
	CPLD_SPI &= ~CPLD_SPI_SCLK;
	udelay (T_SKL);
}


/* execute_spi_command

   sends an spi command to a device.  It first sends cwrite bits from
   v.  If cread is greater than zero it will read cread bits
   (discarding the leading 0 bit) and return them.  If cread is less
   than zero it will check for completetion status and return 0 on
   success or -1 on timeout.  If cread is zero it does nothing other
   than sending the command.

   On the LPD7A400, we can only read or write multiples of 8 bits on
   the codec and the touch screen device.  Here, we round up.

*/

static int execute_spi_command (int v, int cwrite, int cread)
{
	unsigned long l = 0;

#if defined (CONFIG_MACH_LPD7A400)
	/* The codec and touch devices cannot be bit-banged.  Instead,
	 * the CPLD provides an eight-bit shift register and a crude
	 * interface.  */
	if (   ssp_configuration.device == DEVICE_CODEC
	    || ssp_configuration.device == DEVICE_TOUCH) {
		int select = 0;

		PRINTK ("spi(%d %d.%d) 0x%04x",
			ssp_configuration.device, cwrite, cread,
			v);
#if defined (TALK)
		if (ssp_configuration.device == DEVICE_CODEC)
			PRINTK (" 0x%03x -> %2d", v & 0x1ff, (v >> 9) & 0x7f);
#endif
		PRINTK ("\n");

		if (ssp_configuration.device == DEVICE_CODEC)
			select = CPLD_SPIC_CS_CODEC;
		if (ssp_configuration.device == DEVICE_TOUCH)
			select = CPLD_SPIC_CS_TOUCH;
		if (cwrite) {
			for (cwrite = (cwrite + 7)/8; cwrite-- > 0; ) {
				CPLD_SPID = (v >> (8*cwrite)) & 0xff;
				CPLD_SPIC = select | CPLD_SPIC_LOAD;
				while (!(CPLD_SPIC & CPLD_SPIC_LOADED))
					;
				CPLD_SPIC = select;
				while (!(CPLD_SPIC & CPLD_SPIC_DONE))
					;
			}
			v = 0;
		}
		if (cread) {
			mdelay (2);	/* *** FIXME: required by ads7843? */
			v = 0;
			for (cread = (cread + 7)/8; cread-- > 0;) {
				CPLD_SPID = 0;
				CPLD_SPIC = select | CPLD_SPIC_READ
					| CPLD_SPIC_START;
				while (!(CPLD_SPIC & CPLD_SPIC_LOADED))
					;
				CPLD_SPIC = select | CPLD_SPIC_READ;
				while (!(CPLD_SPIC & CPLD_SPIC_DONE))
					;
				v = (v << 8) | CPLD_SPID;
			}
		}
		return v;
	}
#endif

	PRINTK ("spi(%d) 0x%04x -> 0x%x\r\n", ssp_configuration.device,
		v & 0x1ff, (v >> 9) & 0x7f);

	enable_cs ();

	v <<= CPLD_SPI_TX_SHIFT; /* Correction for position of SPI_TX bit */
	while (cwrite--) {
		CPLD_SPI
			= (CPLD_SPI & ~CPLD_SPI_TX)
			| ((v >> cwrite) & CPLD_SPI_TX);
		udelay (T_DIS);
		pulse_clock ();
	}

	if (cread < 0) {
		int delay = 10;
		disable_cs ();
		udelay (1);
		enable_cs ();

		l = -1;
		do {
			if (CPLD_SPI & CPLD_SPI_RX) {
				l = 0;
				break;
			}
		} while (udelay (1), --delay);
	}
	else
	/* We pulse the clock before the data to skip the leading zero. */
		while (cread-- > 0) {
			pulse_clock ();
			l = (l<<1)
				| (((CPLD_SPI & CPLD_SPI_RX)
				    >> CPLD_SPI_RX_SHIFT) & 0x1);
		}

	disable_cs ();
	return l;
}

static int ssp_init (void)
{
	spin_lock_init (&ssp_lock);
	memset (&ssp_configuration, 0, sizeof (ssp_configuration));
	return 0;
}


/* ssp_chip_select

   drops the chip select line for the CPLD shift-register controlled
   devices.  It doesn't enable chip

*/

static void ssp_chip_select (int enable)
{
#if defined (CONFIG_MACH_LPD7A400)
	int select;

	if (ssp_configuration.device == DEVICE_CODEC)
		select = CPLD_SPIC_CS_CODEC;
	else if (ssp_configuration.device == DEVICE_TOUCH)
		select = CPLD_SPIC_CS_TOUCH;
	else
		return;

	if (enable)
		CPLD_SPIC = select;
	else
		CPLD_SPIC = 0;
#endif
}

static void ssp_acquire (void)
{
	spin_lock (&ssp_lock);
}

static void ssp_release (void)
{
	ssp_chip_select (0);	/* just in case */
	spin_unlock (&ssp_lock);
}

static int ssp_configure (int device, int mode, int speed,
			   int frame_size_write, int frame_size_read)
{
	ssp_configuration.device		= device;
	ssp_configuration.mode			= mode;
	ssp_configuration.speed			= speed;
	ssp_configuration.frame_size_write	= frame_size_write;
	ssp_configuration.frame_size_read	= frame_size_read;

	return 0;
}

static int ssp_read (void)
{
	return execute_spi_command (0, 0, ssp_configuration.frame_size_read);
}

static int ssp_write (u16 data)
{
	execute_spi_command (data, ssp_configuration.frame_size_write, 0);
	return 0;
}

static int ssp_write_read (u16 data)
{
	return execute_spi_command (data, ssp_configuration.frame_size_write,
				    ssp_configuration.frame_size_read);
}

struct ssp_driver lh7a40x_cpld_ssp_driver = {
	.init		= ssp_init,
	.acquire	= ssp_acquire,
	.release	= ssp_release,
	.configure	= ssp_configure,
	.chip_select	= ssp_chip_select,
	.read		= ssp_read,
	.write		= ssp_write,
	.write_read	= ssp_write_read,
};


MODULE_AUTHOR("Marc Singer");
MODULE_DESCRIPTION("LPD7A40X CPLD SPI driver");
MODULE_LICENSE("GPL");