Blame view

arch/cris/arch-v32/drivers/sync_serial.c 47.7 KB
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1
  /*
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
2
   * Simple synchronous serial port driver for ETRAX FS and ARTPEC-3.
51533b615   Mikael Starvik   [PATCH] CRIS upda...
3
   *
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
4
   * Copyright (c) 2005, 2008 Axis Communications AB
51533b615   Mikael Starvik   [PATCH] CRIS upda...
5
6
7
8
9
10
   * Author: Mikael Starvik
   *
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
51533b615   Mikael Starvik   [PATCH] CRIS upda...
11
12
13
14
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/major.h>
  #include <linux/sched.h>
0890b5880   Arnd Bergmann   cris: autoconvert...
15
  #include <linux/mutex.h>
51533b615   Mikael Starvik   [PATCH] CRIS upda...
16
17
  #include <linux/interrupt.h>
  #include <linux/poll.h>
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
18
19
20
  #include <linux/fs.h>
  #include <linux/cdev.h>
  #include <linux/device.h>
329fddd7b   Arnd Bergmann   cris: sync_serial...
21
  #include <linux/wait.h>
51533b615   Mikael Starvik   [PATCH] CRIS upda...
22
23
  
  #include <asm/io.h>
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
24
  #include <mach/dma.h>
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
25
26
27
  #include <pinmux.h>
  #include <hwregs/reg_rdwr.h>
  #include <hwregs/sser_defs.h>
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
28
  #include <hwregs/timer_defs.h>
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
29
30
31
32
33
  #include <hwregs/dma_defs.h>
  #include <hwregs/dma.h>
  #include <hwregs/intr_vect_defs.h>
  #include <hwregs/intr_vect.h>
  #include <hwregs/reg_map.h>
51533b615   Mikael Starvik   [PATCH] CRIS upda...
34
  #include <asm/sync_serial.h>
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
35

25985edce   Lucas De Marchi   Fix common misspe...
36
  /* The receiver is a bit tricky because of the continuous stream of data.*/
51533b615   Mikael Starvik   [PATCH] CRIS upda...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  /*                                                                       */
  /* Three DMA descriptors are linked together. Each DMA descriptor is     */
  /* responsible for port->bufchunk of a common buffer.                    */
  /*                                                                       */
  /* +---------------------------------------------+                       */
  /* |   +----------+   +----------+   +----------+ |                      */
  /* +-> | Descr[0] |-->| Descr[1] |-->| Descr[2] |-+                      */
  /*     +----------+   +----------+   +----------+                        */
  /*         |            |              |                                 */
  /*         v            v              v                                 */
  /*   +-------------------------------------+                             */
  /*   |        BUFFER                       |                             */
  /*   +-------------------------------------+                             */
  /*      |<- data_avail ->|                                               */
  /*    readp          writep                                              */
  /*                                                                       */
  /* If the application keeps up the pace readp will be right after writep.*/
  /* If the application can't keep the pace we have to throw away data.    */
  /* The idea is that readp should be ready with the data pointed out by	 */
  /* Descr[i] when the DMA has filled in Descr[i+1].                       */
  /* Otherwise we will discard	                                         */
  /* the rest of the data pointed out by Descr1 and set readp to the start */
  /* of Descr2                                                             */
51533b615   Mikael Starvik   [PATCH] CRIS upda...
60
61
  /* IN_BUFFER_SIZE should be a multiple of 6 to make sure that 24 bit */
  /* words can be handled */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
62
63
64
  #define IN_DESCR_SIZE SSP_INPUT_CHUNK_SIZE
  #define NBR_IN_DESCR (8*6)
  #define IN_BUFFER_SIZE (IN_DESCR_SIZE * NBR_IN_DESCR)
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
65

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
66
  #define NBR_OUT_DESCR 8
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
67
  #define OUT_BUFFER_SIZE (1024 * NBR_OUT_DESCR)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
68
69
70
  
  #define DEFAULT_FRAME_RATE 0
  #define DEFAULT_WORD_RATE 7
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
71
72
  /* To be removed when we move to pure udev. */
  #define SYNC_SERIAL_MAJOR 125
51533b615   Mikael Starvik   [PATCH] CRIS upda...
73
  /* NOTE: Enabling some debug will likely cause overrun or underrun,
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
74
   * especially if manual mode is used.
51533b615   Mikael Starvik   [PATCH] CRIS upda...
75
76
77
78
79
80
81
   */
  #define DEBUG(x)
  #define DEBUGREAD(x)
  #define DEBUGWRITE(x)
  #define DEBUGPOLL(x)
  #define DEBUGRXINT(x)
  #define DEBUGTXINT(x)
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
82
83
  #define DEBUGTRDMA(x)
  #define DEBUGOUTBUF(x)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
84

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  enum syncser_irq_setup {
  	no_irq_setup = 0,
  	dma_irq_setup = 1,
  	manual_irq_setup = 2,
  };
  
  struct sync_port {
  	unsigned long regi_sser;
  	unsigned long regi_dmain;
  	unsigned long regi_dmaout;
  
  	/* Interrupt vectors. */
  	unsigned long dma_in_intr_vect; /* Used for DMA in. */
  	unsigned long dma_out_intr_vect; /* Used for DMA out. */
  	unsigned long syncser_intr_vect; /* Used when no DMA. */
  
  	/* DMA number for in and out. */
  	unsigned int dma_in_nbr;
  	unsigned int dma_out_nbr;
  
  	/* DMA owner. */
  	enum dma_owner req_dma;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
107
108
109
110
111
112
113
114
  
  	char started; /* 1 if port has been started */
  	char port_nbr; /* Port 0 or 1 */
  	char busy; /* 1 if port is busy */
  
  	char enabled;  /* 1 if port is enabled */
  	char use_dma;  /* 1 if port uses dma */
  	char tr_running;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
115
  	enum syncser_irq_setup init_irqs;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
116
117
  	int output;
  	int input;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
118
  	/* Next byte to be read by application */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
119
  	unsigned char *readp;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
120
  	/* Next byte to be written by etrax */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
121
  	unsigned char *writep;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
122

51533b615   Mikael Starvik   [PATCH] CRIS upda...
123
  	unsigned int in_buffer_size;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
124
  	unsigned int in_buffer_len;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
125
  	unsigned int inbufchunk;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
126
127
128
129
130
131
132
133
134
135
136
  	/* Data buffers for in and output. */
  	unsigned char out_buffer[OUT_BUFFER_SIZE] __aligned(32);
  	unsigned char in_buffer[IN_BUFFER_SIZE] __aligned(32);
  	unsigned char flip[IN_BUFFER_SIZE] __aligned(32);
  	struct timespec timestamp[NBR_IN_DESCR];
  	struct dma_descr_data *next_rx_desc;
  	struct dma_descr_data *prev_rx_desc;
  
  	struct timeval last_timestamp;
  	int read_ts_idx;
  	int write_ts_idx;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
137
138
139
140
141
142
143
144
145
146
147
  
  	/* Pointer to the first available descriptor in the ring,
  	 * unless active_tr_descr == catch_tr_descr and a dma
  	 * transfer is active */
  	struct dma_descr_data *active_tr_descr;
  
  	/* Pointer to the first allocated descriptor in the ring */
  	struct dma_descr_data *catch_tr_descr;
  
  	/* Pointer to the descriptor with the current end-of-list */
  	struct dma_descr_data *prev_tr_descr;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
148
  	int full;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
149
150
151
152
153
154
  	/* Pointer to the first byte being read by DMA
  	 * or current position in out_buffer if not using DMA. */
  	unsigned char *out_rd_ptr;
  
  	/* Number of bytes currently locked for being read by DMA */
  	int out_buf_count;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
155
156
157
158
  	dma_descr_context in_context __aligned(32);
  	dma_descr_context out_context __aligned(32);
  	dma_descr_data in_descr[NBR_IN_DESCR] __aligned(16);
  	dma_descr_data out_descr[NBR_OUT_DESCR] __aligned(16);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
159
160
161
162
  	wait_queue_head_t out_wait_q;
  	wait_queue_head_t in_wait_q;
  
  	spinlock_t lock;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
163
  };
51533b615   Mikael Starvik   [PATCH] CRIS upda...
164

0890b5880   Arnd Bergmann   cris: autoconvert...
165
  static DEFINE_MUTEX(sync_serial_mutex);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
166
167
168
  static int etrax_sync_serial_init(void);
  static void initialize_port(int portnbr);
  static inline int sync_data_avail(struct sync_port *port);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
169
170
  static int sync_serial_open(struct inode *, struct file *);
  static int sync_serial_release(struct inode *, struct file *);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
171
  static unsigned int sync_serial_poll(struct file *filp, poll_table *wait);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
172
173
174
175
176
  static long sync_serial_ioctl(struct file *file,
  			      unsigned int cmd, unsigned long arg);
  static int sync_serial_ioctl_unlocked(struct file *file,
  				      unsigned int cmd, unsigned long arg);
  static ssize_t sync_serial_write(struct file *file, const char __user *buf,
51533b615   Mikael Starvik   [PATCH] CRIS upda...
177
  				 size_t count, loff_t *ppos);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
178
  static ssize_t sync_serial_read(struct file *file, char __user *buf,
51533b615   Mikael Starvik   [PATCH] CRIS upda...
179
  				size_t count, loff_t *ppos);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
180
181
182
183
  #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
  	defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
  	(defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
  	defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
51533b615   Mikael Starvik   [PATCH] CRIS upda...
184
  #define SYNC_SER_DMA
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
185
186
  #else
  #define SYNC_SER_MANUAL
51533b615   Mikael Starvik   [PATCH] CRIS upda...
187
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
188
  #ifdef SYNC_SER_DMA
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
189
190
  static void start_dma_out(struct sync_port *port, const char *data, int count);
  static void start_dma_in(struct sync_port *port);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
191
192
  static irqreturn_t tr_interrupt(int irq, void *dev_id);
  static irqreturn_t rx_interrupt(int irq, void *dev_id);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
193
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
194
  #ifdef SYNC_SER_MANUAL
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
195
  static void send_word(struct sync_port *port);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
196
197
  static irqreturn_t manual_interrupt(int irq, void *dev_id);
  #endif
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
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
  #define artpec_pinmux_alloc_fixed crisv32_pinmux_alloc_fixed
  #define artpec_request_dma crisv32_request_dma
  #define artpec_free_dma crisv32_free_dma
  
  #ifdef CONFIG_ETRAXFS
  /* ETRAX FS */
  #define DMA_OUT_NBR0		SYNC_SER0_TX_DMA_NBR
  #define DMA_IN_NBR0		SYNC_SER0_RX_DMA_NBR
  #define DMA_OUT_NBR1		SYNC_SER1_TX_DMA_NBR
  #define DMA_IN_NBR1		SYNC_SER1_RX_DMA_NBR
  #define PINMUX_SSER0		pinmux_sser0
  #define PINMUX_SSER1		pinmux_sser1
  #define SYNCSER_INST0		regi_sser0
  #define SYNCSER_INST1		regi_sser1
  #define SYNCSER_INTR_VECT0	SSER0_INTR_VECT
  #define SYNCSER_INTR_VECT1	SSER1_INTR_VECT
  #define OUT_DMA_INST0		regi_dma4
  #define IN_DMA_INST0		regi_dma5
  #define DMA_OUT_INTR_VECT0	DMA4_INTR_VECT
  #define DMA_OUT_INTR_VECT1	DMA7_INTR_VECT
  #define DMA_IN_INTR_VECT0	DMA5_INTR_VECT
  #define DMA_IN_INTR_VECT1	DMA6_INTR_VECT
  #define REQ_DMA_SYNCSER0	dma_sser0
  #define REQ_DMA_SYNCSER1	dma_sser1
  #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
  #define PORT1_DMA 1
  #else
  #define PORT1_DMA 0
  #endif
  #elif defined(CONFIG_CRIS_MACH_ARTPEC3)
  /* ARTPEC-3 */
  #define DMA_OUT_NBR0		SYNC_SER_TX_DMA_NBR
  #define DMA_IN_NBR0		SYNC_SER_RX_DMA_NBR
  #define PINMUX_SSER0		pinmux_sser
  #define SYNCSER_INST0		regi_sser
  #define SYNCSER_INTR_VECT0	SSER_INTR_VECT
  #define OUT_DMA_INST0		regi_dma6
  #define IN_DMA_INST0		regi_dma7
  #define DMA_OUT_INTR_VECT0	DMA6_INTR_VECT
  #define DMA_IN_INTR_VECT0	DMA7_INTR_VECT
  #define REQ_DMA_SYNCSER0	dma_sser
  #define REQ_DMA_SYNCSER1	dma_sser
51533b615   Mikael Starvik   [PATCH] CRIS upda...
240
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
241
  #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
242
  #define PORT0_DMA 1
51533b615   Mikael Starvik   [PATCH] CRIS upda...
243
  #else
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
244
  #define PORT0_DMA 0
51533b615   Mikael Starvik   [PATCH] CRIS upda...
245
  #endif
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
246

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
247
248
  /* The ports */
  static struct sync_port ports[] = {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
249
  	{
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  		.regi_sser		= SYNCSER_INST0,
  		.regi_dmaout		= OUT_DMA_INST0,
  		.regi_dmain		= IN_DMA_INST0,
  		.use_dma		= PORT0_DMA,
  		.dma_in_intr_vect	= DMA_IN_INTR_VECT0,
  		.dma_out_intr_vect	= DMA_OUT_INTR_VECT0,
  		.dma_in_nbr		= DMA_IN_NBR0,
  		.dma_out_nbr		= DMA_OUT_NBR0,
  		.req_dma		= REQ_DMA_SYNCSER0,
  		.syncser_intr_vect	= SYNCSER_INTR_VECT0,
  	},
  #ifdef CONFIG_ETRAXFS
  	{
  		.regi_sser		= SYNCSER_INST1,
  		.regi_dmaout		= regi_dma6,
  		.regi_dmain		= regi_dma7,
  		.use_dma		= PORT1_DMA,
  		.dma_in_intr_vect	= DMA_IN_INTR_VECT1,
  		.dma_out_intr_vect	= DMA_OUT_INTR_VECT1,
  		.dma_in_nbr		= DMA_IN_NBR1,
  		.dma_out_nbr		= DMA_OUT_NBR1,
  		.req_dma		= REQ_DMA_SYNCSER1,
  		.syncser_intr_vect	= SYNCSER_INTR_VECT1,
  	},
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
274
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
275
  };
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
276
  #define NBR_PORTS ARRAY_SIZE(ports)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
277

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
278
  static const struct file_operations syncser_fops = {
90276a1a6   Jesper Nilsson   cris: Pushdown th...
279
280
281
282
283
284
  	.owner		= THIS_MODULE,
  	.write		= sync_serial_write,
  	.read		= sync_serial_read,
  	.poll		= sync_serial_poll,
  	.unlocked_ioctl	= sync_serial_ioctl,
  	.open		= sync_serial_open,
6038f373a   Arnd Bergmann   llseek: automatic...
285
286
  	.release	= sync_serial_release,
  	.llseek		= noop_llseek,
51533b615   Mikael Starvik   [PATCH] CRIS upda...
287
  };
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
288
289
290
291
292
  static dev_t syncser_first;
  static int minor_count = NBR_PORTS;
  #define SYNCSER_NAME "syncser"
  static struct cdev *syncser_cdev;
  static struct class *syncser_class;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
293

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  static void sync_serial_start_port(struct sync_port *port)
  {
  	reg_sser_rw_cfg cfg = REG_RD(sser, port->regi_sser, rw_cfg);
  	reg_sser_rw_tr_cfg tr_cfg =
  		REG_RD(sser, port->regi_sser, rw_tr_cfg);
  	reg_sser_rw_rec_cfg rec_cfg =
  		REG_RD(sser, port->regi_sser, rw_rec_cfg);
  	cfg.en = regk_sser_yes;
  	tr_cfg.tr_en = regk_sser_yes;
  	rec_cfg.rec_en = regk_sser_yes;
  	REG_WR(sser, port->regi_sser, rw_cfg, cfg);
  	REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  	REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  	port->started = 1;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
308
309
310
311
  }
  
  static void __init initialize_port(int portnbr)
  {
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
312
  	struct sync_port *port = &ports[portnbr];
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
313
314
315
316
  	reg_sser_rw_cfg cfg = { 0 };
  	reg_sser_rw_frm_cfg frm_cfg = { 0 };
  	reg_sser_rw_tr_cfg tr_cfg = { 0 };
  	reg_sser_rw_rec_cfg rec_cfg = { 0 };
51533b615   Mikael Starvik   [PATCH] CRIS upda...
317

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
318
319
  	DEBUG(pr_info("Init sync serial port %d
  ", portnbr));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
320
321
  
  	port->port_nbr = portnbr;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
322
  	port->init_irqs = no_irq_setup;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
323

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
324
325
  	port->out_rd_ptr = port->out_buffer;
  	port->out_buf_count = 0;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
326
327
328
329
330
331
  	port->output = 1;
  	port->input = 0;
  
  	port->readp = port->flip;
  	port->writep = port->flip;
  	port->in_buffer_size = IN_BUFFER_SIZE;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
332
  	port->in_buffer_len = 0;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
333
  	port->inbufchunk = IN_DESCR_SIZE;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
334
335
336
  
  	port->read_ts_idx = 0;
  	port->write_ts_idx = 0;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  
  	init_waitqueue_head(&port->out_wait_q);
  	init_waitqueue_head(&port->in_wait_q);
  
  	spin_lock_init(&port->lock);
  
  	cfg.out_clk_src = regk_sser_intern_clk;
  	cfg.out_clk_pol = regk_sser_pos;
  	cfg.clk_od_mode = regk_sser_no;
  	cfg.clk_dir = regk_sser_out;
  	cfg.gate_clk = regk_sser_no;
  	cfg.base_freq = regk_sser_f29_493;
  	cfg.clk_div = 256;
  	REG_WR(sser, port->regi_sser, rw_cfg, cfg);
  
  	frm_cfg.wordrate = DEFAULT_WORD_RATE;
  	frm_cfg.type = regk_sser_edge;
  	frm_cfg.frame_pin_dir = regk_sser_out;
  	frm_cfg.frame_pin_use = regk_sser_frm;
  	frm_cfg.status_pin_dir = regk_sser_in;
  	frm_cfg.status_pin_use = regk_sser_hold;
  	frm_cfg.out_on = regk_sser_tr;
  	frm_cfg.tr_delay = 1;
  	REG_WR(sser, port->regi_sser, rw_frm_cfg, frm_cfg);
  
  	tr_cfg.urun_stop = regk_sser_no;
  	tr_cfg.sample_size = 7;
  	tr_cfg.sh_dir = regk_sser_msbfirst;
  	tr_cfg.use_dma = port->use_dma ? regk_sser_yes : regk_sser_no;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
366
  #if 0
51533b615   Mikael Starvik   [PATCH] CRIS upda...
367
368
  	tr_cfg.rate_ctrl = regk_sser_bulk;
  	tr_cfg.data_pin_use = regk_sser_dout;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
369
370
371
372
  #else
  	tr_cfg.rate_ctrl = regk_sser_iso;
  	tr_cfg.data_pin_use = regk_sser_dout;
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
373
374
375
376
377
378
379
380
  	tr_cfg.bulk_wspace = 1;
  	REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  
  	rec_cfg.sample_size = 7;
  	rec_cfg.sh_dir = regk_sser_msbfirst;
  	rec_cfg.use_dma = port->use_dma ? regk_sser_yes : regk_sser_no;
  	rec_cfg.fifo_thr = regk_sser_inf;
  	REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
381
382
  
  #ifdef SYNC_SER_DMA
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
383
384
385
386
387
388
389
390
391
392
393
394
  	{
  		int i;
  		/* Setup the descriptor ring for dma out/transmit. */
  		for (i = 0; i < NBR_OUT_DESCR; i++) {
  			dma_descr_data *descr = &port->out_descr[i];
  			descr->wait = 0;
  			descr->intr = 1;
  			descr->eol = 0;
  			descr->out_eop = 0;
  			descr->next =
  				(dma_descr_data *)virt_to_phys(&descr[i+1]);
  		}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
395
396
397
398
399
400
401
402
403
404
405
  	}
  
  	/* Create a ring from the list. */
  	port->out_descr[NBR_OUT_DESCR-1].next =
  		(dma_descr_data *)virt_to_phys(&port->out_descr[0]);
  
  	/* Setup context for traversing the ring. */
  	port->active_tr_descr = &port->out_descr[0];
  	port->prev_tr_descr = &port->out_descr[NBR_OUT_DESCR-1];
  	port->catch_tr_descr = &port->out_descr[0];
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
406
407
408
409
  }
  
  static inline int sync_data_avail(struct sync_port *port)
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
410
  	return port->in_buffer_len;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
411
412
413
414
  }
  
  static int sync_serial_open(struct inode *inode, struct file *file)
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
415
  	int ret = 0;
32ea086b7   Eric Sesterhenn   [PATCH] cris: swi...
416
  	int dev = iminor(inode);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
417
418
419
420
421
  	struct sync_port *port;
  #ifdef SYNC_SER_DMA
  	reg_dma_rw_cfg cfg = { .en = regk_dma_yes };
  	reg_dma_rw_intr_mask intr_mask = { .data = regk_dma_yes };
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
422

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
423
424
  	DEBUG(pr_debug("Open sync serial port %d
  ", dev));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
425

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
426
427
428
429
  	if (dev < 0 || dev >= NBR_PORTS || !ports[dev].enabled) {
  		DEBUG(pr_info("Invalid minor %d
  ", dev));
  		return -ENODEV;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
430
431
432
  	}
  	port = &ports[dev];
  	/* Allow open this device twice (assuming one reader and one writer) */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
433
434
435
436
  	if (port->busy == 2) {
  		DEBUG(pr_info("syncser%d is busy
  ", dev));
  		return -EBUSY;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
437
  	}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
438

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
439
  	mutex_lock(&sync_serial_mutex);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
440

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
441
442
443
444
445
446
447
448
449
450
451
452
453
  	/* Clear any stale date left in the flip buffer */
  	port->readp = port->writep = port->flip;
  	port->in_buffer_len = 0;
  	port->read_ts_idx = 0;
  	port->write_ts_idx = 0;
  
  	if (port->init_irqs != no_irq_setup) {
  		/* Init only on first call. */
  		port->busy++;
  		mutex_unlock(&sync_serial_mutex);
  		return 0;
  	}
  	if (port->use_dma) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
454
  #ifdef SYNC_SER_DMA
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  		const char *tmp;
  		DEBUG(pr_info("Using DMA for syncser%d
  ", dev));
  
  		tmp = dev == 0 ? "syncser0 tx" : "syncser1 tx";
  		if (request_irq(port->dma_out_intr_vect, tr_interrupt, 0,
  				tmp, port)) {
  			pr_err("Can't alloc syncser%d TX IRQ", dev);
  			ret = -EBUSY;
  			goto unlock_and_exit;
  		}
  		if (artpec_request_dma(port->dma_out_nbr, tmp,
  				DMA_VERBOSE_ON_ERROR, 0, port->req_dma)) {
  			free_irq(port->dma_out_intr_vect, port);
  			pr_err("Can't alloc syncser%d TX DMA", dev);
  			ret = -EBUSY;
  			goto unlock_and_exit;
  		}
  		tmp = dev == 0 ? "syncser0 rx" : "syncser1 rx";
  		if (request_irq(port->dma_in_intr_vect, rx_interrupt, 0,
  				tmp, port)) {
  			artpec_free_dma(port->dma_out_nbr);
  			free_irq(port->dma_out_intr_vect, port);
  			pr_err("Can't alloc syncser%d RX IRQ", dev);
  			ret = -EBUSY;
  			goto unlock_and_exit;
  		}
  		if (artpec_request_dma(port->dma_in_nbr, tmp,
  				DMA_VERBOSE_ON_ERROR, 0, port->req_dma)) {
  			artpec_free_dma(port->dma_out_nbr);
  			free_irq(port->dma_out_intr_vect, port);
  			free_irq(port->dma_in_intr_vect, port);
  			pr_err("Can't alloc syncser%d RX DMA", dev);
  			ret = -EBUSY;
  			goto unlock_and_exit;
  		}
  		/* Enable DMAs */
  		REG_WR(dma, port->regi_dmain, rw_cfg, cfg);
  		REG_WR(dma, port->regi_dmaout, rw_cfg, cfg);
  		/* Enable DMA IRQs */
  		REG_WR(dma, port->regi_dmain, rw_intr_mask, intr_mask);
  		REG_WR(dma, port->regi_dmaout, rw_intr_mask, intr_mask);
  		/* Set up wordsize = 1 for DMAs. */
  		DMA_WR_CMD(port->regi_dmain, regk_dma_set_w_size1);
  		DMA_WR_CMD(port->regi_dmaout, regk_dma_set_w_size1);
  
  		start_dma_in(port);
  		port->init_irqs = dma_irq_setup;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
503
  #endif
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
504
  	} else { /* !port->use_dma */
51533b615   Mikael Starvik   [PATCH] CRIS upda...
505
  #ifdef SYNC_SER_MANUAL
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
506
507
508
509
510
511
512
513
514
515
  		const char *tmp = dev == 0 ? "syncser0 manual irq" :
  					     "syncser1 manual irq";
  		if (request_irq(port->syncser_intr_vect, manual_interrupt,
  				0, tmp, port)) {
  			pr_err("Can't alloc syncser%d manual irq",
  				dev);
  			ret = -EBUSY;
  			goto unlock_and_exit;
  		}
  		port->init_irqs = manual_irq_setup;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
516
  #else
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
517
518
  		panic("sync_serial: Manual mode not supported
  ");
51533b615   Mikael Starvik   [PATCH] CRIS upda...
519
  #endif /* SYNC_SER_MANUAL */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
520
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
521
  	port->busy++;
0c401df37   Jonathan Corbet   cris: cdev lock_k...
522
  	ret = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
523
524
  
  unlock_and_exit:
0890b5880   Arnd Bergmann   cris: autoconvert...
525
  	mutex_unlock(&sync_serial_mutex);
0c401df37   Jonathan Corbet   cris: cdev lock_k...
526
  	return ret;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
527
528
529
530
  }
  
  static int sync_serial_release(struct inode *inode, struct file *file)
  {
32ea086b7   Eric Sesterhenn   [PATCH] cris: swi...
531
  	int dev = iminor(inode);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
532
  	struct sync_port *port;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
533

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
534
535
536
  	if (dev < 0 || dev >= NBR_PORTS || !ports[dev].enabled) {
  		DEBUG(pr_info("Invalid minor %d
  ", dev));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
537
538
539
540
541
542
  		return -ENODEV;
  	}
  	port = &ports[dev];
  	if (port->busy)
  		port->busy--;
  	if (!port->busy)
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
543
  		/* XXX */;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
544
545
546
547
548
  	return 0;
  }
  
  static unsigned int sync_serial_poll(struct file *file, poll_table *wait)
  {
496ad9aa8   Al Viro   new helper: file_...
549
  	int dev = iminor(file_inode(file));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
550
  	unsigned int mask = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
551
552
553
554
  	struct sync_port *port;
  	DEBUGPOLL(
  	static unsigned int prev_mask;
  	);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
555
556
  
  	port = &ports[dev];
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
557

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
558
559
  	if (!port->started)
  		sync_serial_start_port(port);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
560

51533b615   Mikael Starvik   [PATCH] CRIS upda...
561
562
  	poll_wait(file, &port->out_wait_q, wait);
  	poll_wait(file, &port->in_wait_q, wait);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
563
564
565
566
567
568
569
570
571
  
  	/* No active transfer, descriptors are available */
  	if (port->output && !port->tr_running)
  		mask |= POLLOUT | POLLWRNORM;
  
  	/* Descriptor and buffer space available. */
  	if (port->output &&
  	    port->active_tr_descr != port->catch_tr_descr &&
  	    port->out_buf_count < OUT_BUFFER_SIZE)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
572
  		mask |=  POLLOUT | POLLWRNORM;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
573

51533b615   Mikael Starvik   [PATCH] CRIS upda...
574
  	/* At least an inbufchunk of data */
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
575
  	if (port->input && sync_data_avail(port) >= port->inbufchunk)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
576
  		mask |= POLLIN | POLLRDNORM;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
577
578
579
580
581
582
583
584
585
  	DEBUGPOLL(
  	if (mask != prev_mask)
  		pr_info("sync_serial_poll: mask 0x%08X %s %s
  ",
  			mask,
  			mask & POLLOUT ? "POLLOUT" : "",
  			mask & POLLIN ? "POLLIN" : "");
  		prev_mask = mask;
  	);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
586
587
  	return mask;
  }
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
588
589
590
591
592
593
594
  static ssize_t __sync_serial_read(struct file *file,
  				  char __user *buf,
  				  size_t count,
  				  loff_t *ppos,
  				  struct timespec *ts)
  {
  	unsigned long flags;
fb32c76d1   David Howells   VFS: Convert file...
595
  	int dev = MINOR(file_inode(file)->i_rdev);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  	int avail;
  	struct sync_port *port;
  	unsigned char *start;
  	unsigned char *end;
  
  	if (dev < 0 || dev >= NBR_PORTS || !ports[dev].enabled) {
  		DEBUG(pr_info("Invalid minor %d
  ", dev));
  		return -ENODEV;
  	}
  	port = &ports[dev];
  
  	if (!port->started)
  		sync_serial_start_port(port);
  
  	/* Calculate number of available bytes */
  	/* Save pointers to avoid that they are modified by interrupt */
  	spin_lock_irqsave(&port->lock, flags);
  	start = port->readp;
  	end = port->writep;
  	spin_unlock_irqrestore(&port->lock, flags);
  
  	while ((start == end) && !port->in_buffer_len) {
  		if (file->f_flags & O_NONBLOCK)
  			return -EAGAIN;
  
  		wait_event_interruptible(port->in_wait_q,
  					 !(start == end && !port->full));
  
  		if (signal_pending(current))
  			return -EINTR;
  
  		spin_lock_irqsave(&port->lock, flags);
  		start = port->readp;
  		end = port->writep;
  		spin_unlock_irqrestore(&port->lock, flags);
  	}
  
  	DEBUGREAD(pr_info("R%d c %d ri %u wi %u /%u
  ",
  			  dev, count,
  			  start - port->flip, end - port->flip,
  			  port->in_buffer_size));
  
  	/* Lazy read, never return wrapped data. */
  	if (end > start)
  		avail = end - start;
  	else
  		avail = port->flip + port->in_buffer_size - start;
  
  	count = count > avail ? avail : count;
  	if (copy_to_user(buf, start, count))
  		return -EFAULT;
  
  	/* If timestamp requested, find timestamp of first returned byte
  	 * and copy it.
  	 * N.B: Applications that request timstamps MUST read data in
  	 * chunks that are multiples of IN_DESCR_SIZE.
  	 * Otherwise the timestamps will not be aligned to the data read.
  	 */
  	if (ts != NULL) {
  		int idx = port->read_ts_idx;
  		memcpy(ts, &port->timestamp[idx], sizeof(struct timespec));
  		port->read_ts_idx += count / IN_DESCR_SIZE;
  		if (port->read_ts_idx >= NBR_IN_DESCR)
  			port->read_ts_idx = 0;
  	}
  
  	spin_lock_irqsave(&port->lock, flags);
  	port->readp += count;
  	/* Check for wrap */
  	if (port->readp >= port->flip + port->in_buffer_size)
  		port->readp = port->flip;
  	port->in_buffer_len -= count;
  	port->full = 0;
  	spin_unlock_irqrestore(&port->lock, flags);
  
  	DEBUGREAD(pr_info("r %d
  ", count));
  
  	return count;
  }
  
  static ssize_t sync_serial_input(struct file *file, unsigned long arg)
  {
  	struct ssp_request req;
  	int count;
  	int ret;
  
  	/* Copy the request structure from user-mode. */
  	ret = copy_from_user(&req, (struct ssp_request __user *)arg,
  		sizeof(struct ssp_request));
  
  	if (ret) {
  		DEBUG(pr_info("sync_serial_input copy from user failed
  "));
  		return -EFAULT;
  	}
  
  	/* To get the timestamps aligned, make sure that 'len'
  	 * is a multiple of IN_DESCR_SIZE.
  	 */
  	if ((req.len % IN_DESCR_SIZE) != 0) {
  		DEBUG(pr_info("sync_serial: req.len %x, IN_DESCR_SIZE %x
  ",
  			      req.len, IN_DESCR_SIZE));
  		return -EFAULT;
  	}
  
  	/* Do the actual read. */
  	/* Note that req.buf is actually a pointer to user space. */
  	count = __sync_serial_read(file, req.buf, req.len,
  				   NULL, &req.ts);
  
  	if (count < 0) {
  		DEBUG(pr_info("sync_serial_input read failed
  "));
  		return count;
  	}
  
  	/* Copy the request back to user-mode. */
  	ret = copy_to_user((struct ssp_request __user *)arg, &req,
  		sizeof(struct ssp_request));
  
  	if (ret) {
  		DEBUG(pr_info("syncser input copy2user failed
  "));
  		return -EFAULT;
  	}
  
  	/* Return the number of bytes read. */
  	return count;
  }
  
  
  static int sync_serial_ioctl_unlocked(struct file *file,
  				      unsigned int cmd, unsigned long arg)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
733
734
  {
  	int return_val = 0;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
735
  	int dma_w_size = regk_dma_set_w_size1;
496ad9aa8   Al Viro   new helper: file_...
736
  	int dev = iminor(file_inode(file));
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
737
  	struct sync_port *port;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
738
739
740
741
742
  	reg_sser_rw_tr_cfg tr_cfg;
  	reg_sser_rw_rec_cfg rec_cfg;
  	reg_sser_rw_frm_cfg frm_cfg;
  	reg_sser_rw_cfg gen_cfg;
  	reg_sser_rw_intr_mask intr_mask;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
743
744
745
  	if (dev < 0 || dev >= NBR_PORTS || !ports[dev].enabled) {
  		DEBUG(pr_info("Invalid minor %d
  ", dev));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
746
747
  		return -1;
  	}
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
748
749
750
751
752
  
  	if (cmd == SSP_INPUT)
  		return sync_serial_input(file, arg);
  
  	port = &ports[dev];
51533b615   Mikael Starvik   [PATCH] CRIS upda...
753
754
755
756
757
758
759
  	spin_lock_irq(&port->lock);
  
  	tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  	rec_cfg = REG_RD(sser, port->regi_sser, rw_rec_cfg);
  	frm_cfg = REG_RD(sser, port->regi_sser, rw_frm_cfg);
  	gen_cfg = REG_RD(sser, port->regi_sser, rw_cfg);
  	intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
760
  	switch (cmd) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
761
  	case SSP_SPEED:
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
762
  		if (GET_SPEED(arg) == CODEC) {
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
763
  			unsigned int freq;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
764
  			gen_cfg.base_freq = regk_sser_f32;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
765
766
767
768
769
770
771
772
773
774
775
776
777
  
  			/* Clock divider will internally be
  			 * gen_cfg.clk_div + 1.
  			 */
  
  			freq = GET_FREQ(arg);
  			switch (freq) {
  			case FREQ_32kHz:
  			case FREQ_64kHz:
  			case FREQ_128kHz:
  			case FREQ_256kHz:
  				gen_cfg.clk_div = 125 *
  					(1 << (freq - FREQ_256kHz)) - 1;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
778
  				break;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
779
780
  			case FREQ_512kHz:
  				gen_cfg.clk_div = 62;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
781
  				break;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
782
783
784
785
  			case FREQ_1MHz:
  			case FREQ_2MHz:
  			case FREQ_4MHz:
  				gen_cfg.clk_div = 8 * (1 << freq) - 1;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
786
787
788
789
790
791
792
793
794
795
796
  				break;
  			}
  		} else if (GET_SPEED(arg) == CODEC_f32768) {
  			gen_cfg.base_freq = regk_sser_f32_768;
  			switch (GET_FREQ(arg)) {
  			case FREQ_4096kHz:
  				gen_cfg.clk_div = 7;
  				break;
  			default:
  				spin_unlock_irq(&port->lock);
  				return -EINVAL;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
797
798
  			}
  		} else {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
799
  			gen_cfg.base_freq = regk_sser_f29_493;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
  			switch (GET_SPEED(arg)) {
  			case SSP150:
  				gen_cfg.clk_div = 29493000 / (150 * 8) - 1;
  				break;
  			case SSP300:
  				gen_cfg.clk_div = 29493000 / (300 * 8) - 1;
  				break;
  			case SSP600:
  				gen_cfg.clk_div = 29493000 / (600 * 8) - 1;
  				break;
  			case SSP1200:
  				gen_cfg.clk_div = 29493000 / (1200 * 8) - 1;
  				break;
  			case SSP2400:
  				gen_cfg.clk_div = 29493000 / (2400 * 8) - 1;
  				break;
  			case SSP4800:
  				gen_cfg.clk_div = 29493000 / (4800 * 8) - 1;
  				break;
  			case SSP9600:
  				gen_cfg.clk_div = 29493000 / (9600 * 8) - 1;
  				break;
  			case SSP19200:
  				gen_cfg.clk_div = 29493000 / (19200 * 8) - 1;
  				break;
  			case SSP28800:
  				gen_cfg.clk_div = 29493000 / (28800 * 8) - 1;
  				break;
  			case SSP57600:
  				gen_cfg.clk_div = 29493000 / (57600 * 8) - 1;
  				break;
  			case SSP115200:
  				gen_cfg.clk_div = 29493000 / (115200 * 8) - 1;
  				break;
  			case SSP230400:
  				gen_cfg.clk_div = 29493000 / (230400 * 8) - 1;
  				break;
  			case SSP460800:
  				gen_cfg.clk_div = 29493000 / (460800 * 8) - 1;
  				break;
  			case SSP921600:
  				gen_cfg.clk_div = 29493000 / (921600 * 8) - 1;
  				break;
  			case SSP3125000:
  				gen_cfg.base_freq = regk_sser_f100;
  				gen_cfg.clk_div = 100000000 / (3125000 * 8) - 1;
  				break;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
847
848
849
850
851
852
853
  
  			}
  		}
  		frm_cfg.wordrate = GET_WORD_RATE(arg);
  
  		break;
  	case SSP_MODE:
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
  		switch (arg) {
  		case MASTER_OUTPUT:
  			port->output = 1;
  			port->input = 0;
  			frm_cfg.out_on = regk_sser_tr;
  			frm_cfg.frame_pin_dir = regk_sser_out;
  			gen_cfg.clk_dir = regk_sser_out;
  			break;
  		case SLAVE_OUTPUT:
  			port->output = 1;
  			port->input = 0;
  			frm_cfg.frame_pin_dir = regk_sser_in;
  			gen_cfg.clk_dir = regk_sser_in;
  			break;
  		case MASTER_INPUT:
  			port->output = 0;
  			port->input = 1;
  			frm_cfg.frame_pin_dir = regk_sser_out;
  			frm_cfg.out_on = regk_sser_intern_tb;
  			gen_cfg.clk_dir = regk_sser_out;
  			break;
  		case SLAVE_INPUT:
  			port->output = 0;
  			port->input = 1;
  			frm_cfg.frame_pin_dir = regk_sser_in;
  			gen_cfg.clk_dir = regk_sser_in;
  			break;
  		case MASTER_BIDIR:
  			port->output = 1;
  			port->input = 1;
  			frm_cfg.frame_pin_dir = regk_sser_out;
  			frm_cfg.out_on = regk_sser_intern_tb;
  			gen_cfg.clk_dir = regk_sser_out;
  			break;
  		case SLAVE_BIDIR:
  			port->output = 1;
  			port->input = 1;
  			frm_cfg.frame_pin_dir = regk_sser_in;
  			gen_cfg.clk_dir = regk_sser_in;
  			break;
  		default:
  			spin_unlock_irq(&port->lock);
  			return -EINVAL;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
897
  		}
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
898
899
  		if (!port->use_dma || arg == MASTER_OUTPUT ||
  				arg == SLAVE_OUTPUT)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
900
901
902
  			intr_mask.rdav = regk_sser_yes;
  		break;
  	case SSP_FRAME_SYNC:
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
903
904
  		if (arg & NORMAL_SYNC) {
  			frm_cfg.rec_delay = 1;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
905
  			frm_cfg.tr_delay = 1;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
906
  		} else if (arg & EARLY_SYNC)
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
907
  			frm_cfg.rec_delay = frm_cfg.tr_delay = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
908
909
910
911
  		else if (arg & LATE_SYNC) {
  			frm_cfg.tr_delay = 2;
  			frm_cfg.rec_delay = 2;
  		} else if (arg & SECOND_WORD_SYNC) {
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
912
913
914
  			frm_cfg.rec_delay = 7;
  			frm_cfg.tr_delay = 1;
  		}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
915
916
917
918
919
920
921
922
923
924
925
926
927
928
  
  		tr_cfg.bulk_wspace = frm_cfg.tr_delay;
  		frm_cfg.early_wend = regk_sser_yes;
  		if (arg & BIT_SYNC)
  			frm_cfg.type = regk_sser_edge;
  		else if (arg & WORD_SYNC)
  			frm_cfg.type = regk_sser_level;
  		else if (arg & EXTENDED_SYNC)
  			frm_cfg.early_wend = regk_sser_no;
  
  		if (arg & SYNC_ON)
  			frm_cfg.frame_pin_use = regk_sser_frm;
  		else if (arg & SYNC_OFF)
  			frm_cfg.frame_pin_use = regk_sser_gio0;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
929
930
  		dma_w_size = regk_dma_set_w_size2;
  		if (arg & WORD_SIZE_8) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
931
  			rec_cfg.sample_size = tr_cfg.sample_size = 7;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
932
933
  			dma_w_size = regk_dma_set_w_size1;
  		} else if (arg & WORD_SIZE_12)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
934
935
936
937
938
939
940
941
942
943
944
945
  			rec_cfg.sample_size = tr_cfg.sample_size = 11;
  		else if (arg & WORD_SIZE_16)
  			rec_cfg.sample_size = tr_cfg.sample_size = 15;
  		else if (arg & WORD_SIZE_24)
  			rec_cfg.sample_size = tr_cfg.sample_size = 23;
  		else if (arg & WORD_SIZE_32)
  			rec_cfg.sample_size = tr_cfg.sample_size = 31;
  
  		if (arg & BIT_ORDER_MSB)
  			rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_msbfirst;
  		else if (arg & BIT_ORDER_LSB)
  			rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_lsbfirst;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
946
947
  		if (arg & FLOW_CONTROL_ENABLE) {
  			frm_cfg.status_pin_use = regk_sser_frm;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
948
  			rec_cfg.fifo_thr = regk_sser_thr16;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
949
950
  		} else if (arg & FLOW_CONTROL_DISABLE) {
  			frm_cfg.status_pin_use = regk_sser_gio0;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
951
  			rec_cfg.fifo_thr = regk_sser_inf;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
952
  		}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
  
  		if (arg & CLOCK_NOT_GATED)
  			gen_cfg.gate_clk = regk_sser_no;
  		else if (arg & CLOCK_GATED)
  			gen_cfg.gate_clk = regk_sser_yes;
  
  		break;
  	case SSP_IPOLARITY:
  		/* NOTE!! negedge is considered NORMAL */
  		if (arg & CLOCK_NORMAL)
  			rec_cfg.clk_pol = regk_sser_neg;
  		else if (arg & CLOCK_INVERT)
  			rec_cfg.clk_pol = regk_sser_pos;
  
  		if (arg & FRAME_NORMAL)
  			frm_cfg.level = regk_sser_pos_hi;
  		else if (arg & FRAME_INVERT)
  			frm_cfg.level = regk_sser_neg_lo;
  
  		if (arg & STATUS_NORMAL)
  			gen_cfg.hold_pol = regk_sser_pos;
  		else if (arg & STATUS_INVERT)
  			gen_cfg.hold_pol = regk_sser_neg;
  		break;
  	case SSP_OPOLARITY:
  		if (arg & CLOCK_NORMAL)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
979
  			gen_cfg.out_clk_pol = regk_sser_pos;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
980
981
  		else if (arg & CLOCK_INVERT)
  			gen_cfg.out_clk_pol = regk_sser_neg;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  
  		if (arg & FRAME_NORMAL)
  			frm_cfg.level = regk_sser_pos_hi;
  		else if (arg & FRAME_INVERT)
  			frm_cfg.level = regk_sser_neg_lo;
  
  		if (arg & STATUS_NORMAL)
  			gen_cfg.hold_pol = regk_sser_pos;
  		else if (arg & STATUS_INVERT)
  			gen_cfg.hold_pol = regk_sser_neg;
  		break;
  	case SSP_SPI:
  		rec_cfg.fifo_thr = regk_sser_inf;
  		rec_cfg.sh_dir = tr_cfg.sh_dir = regk_sser_msbfirst;
  		rec_cfg.sample_size = tr_cfg.sample_size = 7;
  		frm_cfg.frame_pin_use = regk_sser_frm;
  		frm_cfg.type = regk_sser_level;
  		frm_cfg.tr_delay = 1;
  		frm_cfg.level = regk_sser_neg_lo;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1001
  		if (arg & SPI_SLAVE) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1002
1003
1004
1005
  			rec_cfg.clk_pol = regk_sser_neg;
  			gen_cfg.clk_dir = regk_sser_in;
  			port->input = 1;
  			port->output = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1006
  		} else {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  			gen_cfg.out_clk_pol = regk_sser_pos;
  			port->input = 0;
  			port->output = 1;
  			gen_cfg.clk_dir = regk_sser_out;
  		}
  		break;
  	case SSP_INBUFCHUNK:
  		break;
  	default:
  		return_val = -1;
  	}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1018
  	if (port->started) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1019
  		rec_cfg.rec_en = port->input;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1020
  		gen_cfg.en = (port->output | port->input);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1021
1022
1023
1024
1025
1026
1027
  	}
  
  	REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
  	REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  	REG_WR(sser, port->regi_sser, rw_frm_cfg, frm_cfg);
  	REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
  	REG_WR(sser, port->regi_sser, rw_cfg, gen_cfg);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  
  	if (cmd == SSP_FRAME_SYNC && (arg & (WORD_SIZE_8 | WORD_SIZE_12 |
  			WORD_SIZE_16 | WORD_SIZE_24 | WORD_SIZE_32))) {
  		int en = gen_cfg.en;
  		gen_cfg.en = 0;
  		REG_WR(sser, port->regi_sser, rw_cfg, gen_cfg);
  		/* ##### Should DMA be stoped before we change dma size? */
  		DMA_WR_CMD(port->regi_dmain, dma_w_size);
  		DMA_WR_CMD(port->regi_dmaout, dma_w_size);
  		gen_cfg.en = en;
  		REG_WR(sser, port->regi_sser, rw_cfg, gen_cfg);
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1040
1041
1042
  	spin_unlock_irq(&port->lock);
  	return return_val;
  }
90276a1a6   Jesper Nilsson   cris: Pushdown th...
1043
  static long sync_serial_ioctl(struct file *file,
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1044
  		unsigned int cmd, unsigned long arg)
90276a1a6   Jesper Nilsson   cris: Pushdown th...
1045
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1046
  	long ret;
90276a1a6   Jesper Nilsson   cris: Pushdown th...
1047

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1048
1049
1050
  	mutex_lock(&sync_serial_mutex);
  	ret = sync_serial_ioctl_unlocked(file, cmd, arg);
  	mutex_unlock(&sync_serial_mutex);
90276a1a6   Jesper Nilsson   cris: Pushdown th...
1051

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1052
  	return ret;
90276a1a6   Jesper Nilsson   cris: Pushdown th...
1053
  }
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1054
  /* NOTE: sync_serial_write does not support concurrency */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1055
  static ssize_t sync_serial_write(struct file *file, const char __user *buf,
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1056
  				 size_t count, loff_t *ppos)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1057
  {
496ad9aa8   Al Viro   new helper: file_...
1058
  	int dev = iminor(file_inode(file));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1059
  	DECLARE_WAITQUEUE(wait, current);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1060
1061
  	struct sync_port *port;
  	int trunc_count;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1062
  	unsigned long flags;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1063
1064
  	int bytes_free;
  	int out_buf_count;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1065

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1066
1067
1068
1069
1070
  	unsigned char *rd_ptr;       /* First allocated byte in the buffer */
  	unsigned char *wr_ptr;       /* First free byte in the buffer */
  	unsigned char *buf_stop_ptr; /* Last byte + 1 */
  
  	if (dev < 0 || dev >= NBR_PORTS || !ports[dev].enabled) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1071
1072
  		DEBUG(pr_info("Invalid minor %d
  ", dev));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1073
1074
1075
  		return -ENODEV;
  	}
  	port = &ports[dev];
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1076
1077
1078
1079
1080
1081
1082
  	/* |<-         OUT_BUFFER_SIZE                          ->|
  	 *           |<- out_buf_count ->|
  	 *                               |<- trunc_count ->| ...->|
  	 *  ______________________________________________________
  	 * |  free   |   data            | free                   |
  	 * |_________|___________________|________________________|
  	 *           ^ rd_ptr            ^ wr_ptr
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1083
  	 */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1084
1085
1086
1087
  	DEBUGWRITE(pr_info("W d%d c %u a: %p c: %p
  ",
  			   port->port_nbr, count, port->active_tr_descr,
  			   port->catch_tr_descr));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1088
1089
1090
  
  	/* Read variables that may be updated by interrupts */
  	spin_lock_irqsave(&port->lock, flags);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1091
1092
  	rd_ptr = port->out_rd_ptr;
  	out_buf_count = port->out_buf_count;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1093
  	spin_unlock_irqrestore(&port->lock, flags);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1094

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1095
1096
1097
1098
  	/* Check if resources are available */
  	if (port->tr_running &&
  	    ((port->use_dma && port->active_tr_descr == port->catch_tr_descr) ||
  	     out_buf_count >= OUT_BUFFER_SIZE)) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1099
1100
  		DEBUGWRITE(pr_info("sser%d full
  ", dev));
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
  		return -EAGAIN;
  	}
  
  	buf_stop_ptr = port->out_buffer + OUT_BUFFER_SIZE;
  
  	/* Determine pointer to the first free byte, before copying. */
  	wr_ptr = rd_ptr + out_buf_count;
  	if (wr_ptr >= buf_stop_ptr)
  		wr_ptr -= OUT_BUFFER_SIZE;
  
  	/* If we wrap the ring buffer, let the user space program handle it by
  	 * truncating the data. This could be more elegant, small buffer
  	 * fragments may occur.
  	 */
  	bytes_free = OUT_BUFFER_SIZE - out_buf_count;
  	if (wr_ptr + bytes_free > buf_stop_ptr)
  		bytes_free = buf_stop_ptr - wr_ptr;
  	trunc_count = (count < bytes_free) ? count : bytes_free;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1119

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1120
  	if (copy_from_user(wr_ptr, buf, trunc_count))
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1121
  		return -EFAULT;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1122
1123
1124
1125
1126
  	DEBUGOUTBUF(pr_info("%-4d + %-4d = %-4d     %p %p %p
  ",
  			    out_buf_count, trunc_count,
  			    port->out_buf_count, port->out_buffer,
  			    wr_ptr, buf_stop_ptr));
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1127
1128
  
  	/* Make sure transmitter/receiver is running */
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1129
  	if (!port->started) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1130
  		reg_sser_rw_cfg cfg = REG_RD(sser, port->regi_sser, rw_cfg);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1131
1132
  		reg_sser_rw_rec_cfg rec_cfg =
  			REG_RD(sser, port->regi_sser, rw_rec_cfg);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1133
  		cfg.en = regk_sser_yes;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1134
1135
  		rec_cfg.rec_en = port->input;
  		REG_WR(sser, port->regi_sser, rw_cfg, cfg);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1136
1137
1138
  		REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
  		port->started = 1;
  	}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1139
1140
1141
1142
  	/* Setup wait if blocking */
  	if (!(file->f_flags & O_NONBLOCK)) {
  		add_wait_queue(&port->out_wait_q, &wait);
  		set_current_state(TASK_INTERRUPTIBLE);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1143
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1144
  	spin_lock_irqsave(&port->lock, flags);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1145
1146
  	port->out_buf_count += trunc_count;
  	if (port->use_dma) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1147
  #ifdef SYNC_SER_DMA
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1148
  		start_dma_out(port, wr_ptr, trunc_count);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1149
  #endif
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1150
  	} else if (!port->tr_running) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1151
  #ifdef SYNC_SER_MANUAL
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1152
1153
1154
1155
1156
1157
1158
  		reg_sser_rw_intr_mask intr_mask;
  		intr_mask = REG_RD(sser, port->regi_sser, rw_intr_mask);
  		/* Start sender by writing data */
  		send_word(port);
  		/* and enable transmitter ready IRQ */
  		intr_mask.trdy = 1;
  		REG_WR(sser, port->regi_sser, rw_intr_mask, intr_mask);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1159
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1160
1161
  	}
  	spin_unlock_irqrestore(&port->lock, flags);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1162
1163
1164
  
  	/* Exit if non blocking */
  	if (file->f_flags & O_NONBLOCK) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1165
1166
1167
1168
  		DEBUGWRITE(pr_info("w d%d c %u  %08x
  ",
  				   port->port_nbr, trunc_count,
  				   REG_RD_INT(dma, port->regi_dmaout, r_intr)));
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1169
1170
  		return trunc_count;
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1171
  	schedule();
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1172
  	remove_wait_queue(&port->out_wait_q, &wait);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1173

51533b615   Mikael Starvik   [PATCH] CRIS upda...
1174
  	if (signal_pending(current))
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1175
  		return -EINTR;
cbca66348   Jesper Nilsson   CRIS v32: Remove ...
1176

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1177
1178
  	DEBUGWRITE(pr_info("w d%d c %u
  ", port->port_nbr, trunc_count));
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1179
  	return trunc_count;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1180
  }
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1181
  static ssize_t sync_serial_read(struct file *file, char __user *buf,
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1182
1183
  				size_t count, loff_t *ppos)
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1184
  	return __sync_serial_read(file, buf, count, ppos, NULL);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1185
  }
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1186
1187
  #ifdef SYNC_SER_MANUAL
  static void send_word(struct sync_port *port)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1188
1189
1190
  {
  	reg_sser_rw_tr_cfg tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  	reg_sser_rw_tr_data tr_data =  {0};
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1191
1192
1193
1194
1195
1196
1197
1198
1199
  	switch (tr_cfg.sample_size) {
  	case 8:
  		port->out_buf_count--;
  		tr_data.data = *port->out_rd_ptr++;
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
  		if (port->out_rd_ptr >= port->out_buffer + OUT_BUFFER_SIZE)
  			port->out_rd_ptr = port->out_buffer;
  		break;
  	case 12:
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1200
  	{
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1201
1202
1203
  		int data = (*port->out_rd_ptr++) << 8;
  		data |= *port->out_rd_ptr++;
  		port->out_buf_count -= 2;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1204
1205
  		tr_data.data = data;
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1206
1207
  		if (port->out_rd_ptr >= port->out_buffer + OUT_BUFFER_SIZE)
  			port->out_rd_ptr = port->out_buffer;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1208
  		break;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1209
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1210
  	case 16:
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1211
1212
  		port->out_buf_count -= 2;
  		tr_data.data = *(unsigned short *)port->out_rd_ptr;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1213
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1214
1215
1216
  		port->out_rd_ptr += 2;
  		if (port->out_rd_ptr >= port->out_buffer + OUT_BUFFER_SIZE)
  			port->out_rd_ptr = port->out_buffer;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1217
1218
  		break;
  	case 24:
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1219
1220
  		port->out_buf_count -= 3;
  		tr_data.data = *(unsigned short *)port->out_rd_ptr;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1221
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1222
1223
  		port->out_rd_ptr += 2;
  		tr_data.data = *port->out_rd_ptr++;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1224
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1225
1226
  		if (port->out_rd_ptr >= port->out_buffer + OUT_BUFFER_SIZE)
  			port->out_rd_ptr = port->out_buffer;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1227
1228
  		break;
  	case 32:
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1229
1230
  		port->out_buf_count -= 4;
  		tr_data.data = *(unsigned short *)port->out_rd_ptr;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1231
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1232
1233
  		port->out_rd_ptr += 2;
  		tr_data.data = *(unsigned short *)port->out_rd_ptr;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1234
  		REG_WR(sser, port->regi_sser, rw_tr_data, tr_data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1235
1236
1237
  		port->out_rd_ptr += 2;
  		if (port->out_rd_ptr >= port->out_buffer + OUT_BUFFER_SIZE)
  			port->out_rd_ptr = port->out_buffer;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1238
1239
1240
  		break;
  	}
  }
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1241
  #endif
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1242

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1243
1244
  #ifdef SYNC_SER_DMA
  static void start_dma_out(struct sync_port *port, const char *data, int count)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1245
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1246
  	port->active_tr_descr->buf = (char *)virt_to_phys((char *)data);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1247
1248
1249
1250
1251
  	port->active_tr_descr->after = port->active_tr_descr->buf + count;
  	port->active_tr_descr->intr = 1;
  
  	port->active_tr_descr->eol = 1;
  	port->prev_tr_descr->eol = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1252
1253
  	DEBUGTRDMA(pr_info("Inserting eolr:%p eol@:%p
  ",
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1254
1255
  		port->prev_tr_descr, port->active_tr_descr));
  	port->prev_tr_descr = port->active_tr_descr;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1256
  	port->active_tr_descr = phys_to_virt((int)port->active_tr_descr->next);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1257
1258
1259
1260
  
  	if (!port->tr_running) {
  		reg_sser_rw_tr_cfg tr_cfg = REG_RD(sser, port->regi_sser,
  			rw_tr_cfg);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1261

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1262
  		port->out_context.next = NULL;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1263
1264
1265
1266
1267
1268
1269
1270
1271
  		port->out_context.saved_data =
  			(dma_descr_data *)virt_to_phys(port->prev_tr_descr);
  		port->out_context.saved_data_buf = port->prev_tr_descr->buf;
  
  		DMA_START_CONTEXT(port->regi_dmaout,
  			virt_to_phys((char *)&port->out_context));
  
  		tr_cfg.tr_en = regk_sser_yes;
  		REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1272
1273
  		DEBUGTRDMA(pr_info(KERN_INFO "dma s
  "););
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1274
1275
  	} else {
  		DMA_CONTINUE_DATA(port->regi_dmaout);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1276
1277
  		DEBUGTRDMA(pr_info("dma c
  "););
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1278
  	}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1279

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1280
  	port->tr_running = 1;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1281
  }
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1282
  static void start_dma_in(struct sync_port *port)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1283
1284
  {
  	int i;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1285
  	char *buf;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1286
1287
  	unsigned long flags;
  	spin_lock_irqsave(&port->lock, flags);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1288
  	port->writep = port->flip;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1289
  	spin_unlock_irqrestore(&port->lock, flags);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1290

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1291
  	buf = (char *)virt_to_phys(port->in_buffer);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1292
  	for (i = 0; i < NBR_IN_DESCR; i++) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1293
1294
1295
  		port->in_descr[i].buf = buf;
  		port->in_descr[i].after = buf + port->inbufchunk;
  		port->in_descr[i].intr = 1;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1296
1297
  		port->in_descr[i].next =
  			(dma_descr_data *)virt_to_phys(&port->in_descr[i+1]);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1298
1299
1300
1301
  		port->in_descr[i].buf = buf;
  		buf += port->inbufchunk;
  	}
  	/* Link the last descriptor to the first */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1302
1303
  	port->in_descr[i-1].next =
  		(dma_descr_data *)virt_to_phys(&port->in_descr[0]);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1304
1305
  	port->in_descr[i-1].eol = regk_sser_yes;
  	port->next_rx_desc = &port->in_descr[0];
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1306
  	port->prev_rx_desc = &port->in_descr[NBR_IN_DESCR - 1];
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1307
1308
  	port->in_context.saved_data =
  		(dma_descr_data *)virt_to_phys(&port->in_descr[0]);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1309
1310
1311
  	port->in_context.saved_data_buf = port->in_descr[0].buf;
  	DMA_START_CONTEXT(port->regi_dmain, virt_to_phys(&port->in_context));
  }
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1312
  static irqreturn_t tr_interrupt(int irq, void *dev_id)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1313
1314
  {
  	reg_dma_r_masked_intr masked;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1315
  	reg_dma_rw_ack_intr ack_intr = { .data = regk_dma_yes };
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1316
  	reg_dma_rw_stat stat;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1317
  	int i;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1318
  	int found = 0;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1319
  	int stop_sser = 0;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1320

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1321
  	for (i = 0; i < NBR_PORTS; i++) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1322
1323
  		struct sync_port *port = &ports[i];
  		if (!port->enabled || !port->use_dma)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1324
  			continue;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1325
  		/* IRQ active for the port? */
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1326
  		masked = REG_RD(dma, port->regi_dmaout, r_masked_intr);
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1327
1328
  		if (!masked.data)
  			continue;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1329

e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
  		found = 1;
  
  		/* Check if we should stop the DMA transfer */
  		stat = REG_RD(dma, port->regi_dmaout, rw_stat);
  		if (stat.list_state == regk_dma_data_at_eol)
  			stop_sser = 1;
  
  		/* Clear IRQ */
  		REG_WR(dma, port->regi_dmaout, rw_ack_intr, ack_intr);
  
  		if (!stop_sser) {
  			/* The DMA has completed a descriptor, EOL was not
  			 * encountered, so step relevant descriptor and
  			 * datapointers forward. */
  			int sent;
  			sent = port->catch_tr_descr->after -
  				port->catch_tr_descr->buf;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1347
1348
1349
1350
1351
1352
1353
  			DEBUGTXINT(pr_info("%-4d - %-4d = %-4d\t"
  					   "in descr %p (ac: %p)
  ",
  					   port->out_buf_count, sent,
  					   port->out_buf_count - sent,
  					   port->catch_tr_descr,
  					   port->active_tr_descr););
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1354
1355
1356
1357
1358
1359
  			port->out_buf_count -= sent;
  			port->catch_tr_descr =
  				phys_to_virt((int) port->catch_tr_descr->next);
  			port->out_rd_ptr =
  				phys_to_virt((int) port->catch_tr_descr->buf);
  		} else {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1360
1361
  			reg_sser_rw_tr_cfg tr_cfg;
  			int j, sent;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1362
1363
1364
1365
1366
1367
1368
  			/* EOL handler.
  			 * Note that if an EOL was encountered during the irq
  			 * locked section of sync_ser_write the DMA will be
  			 * restarted and the eol flag will be cleared.
  			 * The remaining descriptors will be traversed by
  			 * the descriptor interrupts as usual.
  			 */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1369
  			j = 0;
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1370
1371
1372
  			while (!port->catch_tr_descr->eol) {
  				sent = port->catch_tr_descr->after -
  					port->catch_tr_descr->buf;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1373
  				DEBUGOUTBUF(pr_info(
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1374
1375
1376
1377
1378
1379
1380
1381
  					"traversing descr %p -%d (%d)
  ",
  					port->catch_tr_descr,
  					sent,
  					port->out_buf_count));
  				port->out_buf_count -= sent;
  				port->catch_tr_descr = phys_to_virt(
  					(int)port->catch_tr_descr->next);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1382
1383
  				j++;
  				if (j >= NBR_OUT_DESCR) {
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1384
1385
1386
  					/* TODO: Reset and recover */
  					panic("sync_serial: missing eol");
  				}
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1387
  			}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1388
1389
  			sent = port->catch_tr_descr->after -
  				port->catch_tr_descr->buf;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1390
1391
  			DEBUGOUTBUF(pr_info("eol at descr %p -%d (%d)
  ",
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
  				port->catch_tr_descr,
  				sent,
  				port->out_buf_count));
  
  			port->out_buf_count -= sent;
  
  			/* Update read pointer to first free byte, we
  			 * may already be writing data there. */
  			port->out_rd_ptr =
  				phys_to_virt((int) port->catch_tr_descr->after);
  			if (port->out_rd_ptr > port->out_buffer +
  					OUT_BUFFER_SIZE)
  				port->out_rd_ptr = port->out_buffer;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1405
1406
  			tr_cfg = REG_RD(sser, port->regi_sser, rw_tr_cfg);
  			DEBUGTXINT(pr_info(
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1407
1408
1409
1410
1411
  				"tr_int DMA stop %d, set catch @ %p
  ",
  				port->out_buf_count,
  				port->active_tr_descr));
  			if (port->out_buf_count != 0)
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1412
1413
  				pr_err("sync_ser: buf not empty after eol
  ");
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1414
1415
1416
1417
  			port->catch_tr_descr = port->active_tr_descr;
  			port->tr_running = 0;
  			tr_cfg.tr_en = regk_sser_no;
  			REG_WR(sser, port->regi_sser, rw_tr_cfg, tr_cfg);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1418
  		}
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1419
1420
  		/* wake up the waiting process */
  		wake_up_interruptible(&port->out_wait_q);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1421
1422
1423
  	}
  	return IRQ_RETVAL(found);
  } /* tr_interrupt */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
  
  static inline void handle_rx_packet(struct sync_port *port)
  {
  	int idx;
  	reg_dma_rw_ack_intr ack_intr = { .data = regk_dma_yes };
  	unsigned long flags;
  
  	DEBUGRXINT(pr_info(KERN_INFO "!"));
  	spin_lock_irqsave(&port->lock, flags);
  
  	/* If we overrun the user experience is crap regardless if we
  	 * drop new or old data. Its much easier to get it right when
  	 * dropping new data so lets do that.
  	 */
  	if ((port->writep + port->inbufchunk <=
  	     port->flip + port->in_buffer_size) &&
  	    (port->in_buffer_len + port->inbufchunk < IN_BUFFER_SIZE)) {
  		memcpy(port->writep,
  		       phys_to_virt((unsigned)port->next_rx_desc->buf),
  		       port->inbufchunk);
  		port->writep += port->inbufchunk;
  		if (port->writep >= port->flip + port->in_buffer_size)
  			port->writep = port->flip;
  
  		/* Timestamp the new data chunk. */
  		if (port->write_ts_idx == NBR_IN_DESCR)
  			port->write_ts_idx = 0;
  		idx = port->write_ts_idx++;
  		do_posix_clock_monotonic_gettime(&port->timestamp[idx]);
  		port->in_buffer_len += port->inbufchunk;
  	}
  	spin_unlock_irqrestore(&port->lock, flags);
  
  	port->next_rx_desc->eol = 1;
  	port->prev_rx_desc->eol = 0;
  	/* Cache bug workaround */
  	flush_dma_descr(port->prev_rx_desc, 0);
  	port->prev_rx_desc = port->next_rx_desc;
  	port->next_rx_desc = phys_to_virt((unsigned)port->next_rx_desc->next);
  	/* Cache bug workaround */
  	flush_dma_descr(port->prev_rx_desc, 1);
  	/* wake up the waiting process */
  	wake_up_interruptible(&port->in_wait_q);
  	DMA_CONTINUE(port->regi_dmain);
  	REG_WR(dma, port->regi_dmain, rw_ack_intr, ack_intr);
  
  }
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1471
  static irqreturn_t rx_interrupt(int irq, void *dev_id)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1472
1473
  {
  	reg_dma_r_masked_intr masked;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1474
1475
1476
  
  	int i;
  	int found = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1477
1478
1479
1480
1481
  	DEBUG(pr_info("rx_interrupt
  "));
  
  	for (i = 0; i < NBR_PORTS; i++) {
  		struct sync_port *port = &ports[i];
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1482

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1483
  		if (!port->enabled || !port->use_dma)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1484
1485
1486
  			continue;
  
  		masked = REG_RD(dma, port->regi_dmain, r_masked_intr);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1487
1488
  		if (!masked.data)
  			continue;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1489

3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1490
1491
1492
1493
1494
  		/* Descriptor interrupt */
  		found = 1;
  		while (REG_RD(dma, port->regi_dmain, rw_data) !=
  				virt_to_phys(port->next_rx_desc))
  			handle_rx_packet(port);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1495
1496
1497
1498
1499
1500
  	}
  	return IRQ_RETVAL(found);
  } /* rx_interrupt */
  #endif /* SYNC_SER_DMA */
  
  #ifdef SYNC_SER_MANUAL
e908dfc3c   Jesper Nilsson   CRIS v32: Update ...
1501
  static irqreturn_t manual_interrupt(int irq, void *dev_id)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1502
  {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1503
  	unsigned long flags;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1504
1505
1506
  	int i;
  	int found = 0;
  	reg_sser_r_masked_intr masked;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1507
1508
  	for (i = 0; i < NBR_PORTS; i++) {
  		struct sync_port *port = &ports[i];
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1509
1510
  
  		if (!port->enabled || port->use_dma)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1511
  			continue;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1512
1513
  
  		masked = REG_RD(sser, port->regi_sser, r_masked_intr);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1514
1515
1516
1517
1518
1519
  		/* Data received? */
  		if (masked.rdav) {
  			reg_sser_rw_rec_cfg rec_cfg =
  				REG_RD(sser, port->regi_sser, rw_rec_cfg);
  			reg_sser_r_rec_data data = REG_RD(sser,
  				port->regi_sser, r_rec_data);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1520
1521
  			found = 1;
  			/* Read data */
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1522
1523
  			spin_lock_irqsave(&port->lock, flags);
  			switch (rec_cfg.sample_size) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1524
1525
1526
1527
1528
1529
  			case 8:
  				*port->writep++ = data.data & 0xff;
  				break;
  			case 12:
  				*port->writep = (data.data & 0x0ff0) >> 4;
  				*(port->writep + 1) = data.data & 0x0f;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1530
  				port->writep += 2;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1531
1532
  				break;
  			case 16:
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1533
1534
  				*(unsigned short *)port->writep = data.data;
  				port->writep += 2;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1535
1536
  				break;
  			case 24:
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1537
1538
  				*(unsigned int *)port->writep = data.data;
  				port->writep += 3;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1539
1540
  				break;
  			case 32:
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1541
1542
  				*(unsigned int *)port->writep = data.data;
  				port->writep += 4;
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1543
1544
  				break;
  			}
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1545
1546
  			/* Wrap? */
  			if (port->writep >= port->flip + port->in_buffer_size)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1547
1548
  				port->writep = port->flip;
  			if (port->writep == port->readp) {
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1549
  				/* Receive buf overrun, discard oldest data */
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1550
  				port->readp++;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1551
1552
1553
  				/* Wrap? */
  				if (port->readp >= port->flip +
  						port->in_buffer_size)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1554
1555
  					port->readp = port->flip;
  			}
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1556
  			spin_unlock_irqrestore(&port->lock, flags);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1557
  			if (sync_data_avail(port) >= port->inbufchunk)
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1558
1559
  				/* Wake up application */
  				wake_up_interruptible(&port->in_wait_q);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1560
  		}
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1561
1562
  		/* Transmitter ready? */
  		if (masked.trdy) {
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1563
  			found = 1;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1564
1565
  			/* More data to send */
  			if (port->out_buf_count > 0)
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1566
  				send_word(port);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1567
1568
  			else {
  				/* Transmission finished */
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1569
  				reg_sser_rw_intr_mask intr_mask;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1570
1571
  				intr_mask = REG_RD(sser, port->regi_sser,
  					rw_intr_mask);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1572
  				intr_mask.trdy = 0;
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1573
1574
1575
1576
  				REG_WR(sser, port->regi_sser,
  					rw_intr_mask, intr_mask);
  				/* Wake up application */
  				wake_up_interruptible(&port->out_wait_q);
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1577
1578
1579
1580
1581
1582
  			}
  		}
  	}
  	return IRQ_RETVAL(found);
  }
  #endif
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
  static int __init etrax_sync_serial_init(void)
  {
  #if 1
  	/* This code will be removed when we move to udev for all devices. */
  	syncser_first = MKDEV(SYNC_SERIAL_MAJOR, 0);
  	if (register_chrdev_region(syncser_first, minor_count, SYNCSER_NAME)) {
  		pr_err("Failed to register major %d
  ", SYNC_SERIAL_MAJOR);
  		return -1;
  	}
  #else
  	/* Allocate dynamic major number. */
  	if (alloc_chrdev_region(&syncser_first, 0, minor_count, SYNCSER_NAME)) {
  		pr_err("Failed to allocate character device region
  ");
  		return -1;
  	}
  #endif
  	syncser_cdev = cdev_alloc();
  	if (!syncser_cdev) {
  		pr_err("Failed to allocate cdev for syncser
  ");
  		unregister_chrdev_region(syncser_first, minor_count);
  		return -1;
  	}
  	cdev_init(syncser_cdev, &syncser_fops);
  
  	/* Create a sysfs class for syncser */
  	syncser_class = class_create(THIS_MODULE, "syncser_class");
  
  	/* Initialize Ports */
  #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
  	if (artpec_pinmux_alloc_fixed(PINMUX_SSER0)) {
  		pr_warn("Unable to alloc pins for synchronous serial port 0
  ");
  		unregister_chrdev_region(syncser_first, minor_count);
  		return -EIO;
  	}
  	initialize_port(0);
  	ports[0].enabled = 1;
  	/* Register with sysfs so udev can pick it up. */
  	device_create(syncser_class, NULL, syncser_first, NULL,
  		      "%s%d", SYNCSER_NAME, 0);
  #endif
  
  #if defined(CONFIG_ETRAXFS) && defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
  	if (artpec_pinmux_alloc_fixed(PINMUX_SSER1)) {
  		pr_warn("Unable to alloc pins for synchronous serial port 1
  ");
  		unregister_chrdev_region(syncser_first, minor_count);
  		class_destroy(syncser_class);
  		return -EIO;
  	}
  	initialize_port(1);
  	ports[1].enabled = 1;
  	/* Register with sysfs so udev can pick it up. */
  	device_create(syncser_class, NULL, syncser_first, NULL,
  		      "%s%d", SYNCSER_NAME, 0);
  #endif
  
  	/* Add it to system */
  	if (cdev_add(syncser_cdev, syncser_first, minor_count) < 0) {
  		pr_err("Failed to add syncser as char device
  ");
  		device_destroy(syncser_class, syncser_first);
  		class_destroy(syncser_class);
  		cdev_del(syncser_cdev);
  		unregister_chrdev_region(syncser_first, minor_count);
  		return -1;
  	}
  
  
  	pr_info("ARTPEC synchronous serial port (%s: %d, %d)
  ",
  		SYNCSER_NAME, MAJOR(syncser_first), MINOR(syncser_first));
  
  	return 0;
  }
  
  static void __exit etrax_sync_serial_exit(void)
  {
  	int i;
  	device_destroy(syncser_class, syncser_first);
  	class_destroy(syncser_class);
  
  	if (syncser_cdev) {
  		cdev_del(syncser_cdev);
  		unregister_chrdev_region(syncser_first, minor_count);
  	}
  	for (i = 0; i < NBR_PORTS; i++) {
  		struct sync_port *port = &ports[i];
  		if (port->init_irqs == dma_irq_setup) {
  			/* Free dma irqs and dma channels. */
  #ifdef SYNC_SER_DMA
  			artpec_free_dma(port->dma_in_nbr);
  			artpec_free_dma(port->dma_out_nbr);
  			free_irq(port->dma_out_intr_vect, port);
  			free_irq(port->dma_in_intr_vect, port);
  #endif
  		} else if (port->init_irqs == manual_irq_setup) {
  			/* Free manual irq. */
  			free_irq(port->syncser_intr_vect, port);
  		}
  	}
  
  	pr_info("ARTPEC synchronous serial port unregistered
  ");
  }
51533b615   Mikael Starvik   [PATCH] CRIS upda...
1691
  module_init(etrax_sync_serial_init);
3f10462f2   Jesper Nilsson   CRISv32: Rewrite ...
1692
1693
1694
  module_exit(etrax_sync_serial_exit);
  
  MODULE_LICENSE("GPL");