strip the kernel version suffix from target directories, except for brcm-2.4 (the...
[openwrt/staging/florian.git] / target / linux / etrax / files / drivers / spi / spi_crisv32_sser.c
1 /*
2 * SPI port driver for ETRAX FS et al. using a synchronous serial
3 * port, but simplified by using the spi_bitbang framework.
4 *
5 * Copyright (c) 2007 Axis Communications AB
6 *
7 * Author: Hans-Peter Nilsson, though copying parts of
8 * spi_s3c24xx_gpio.c, hence also:
9 * Copyright (c) 2006 Ben Dooks
10 * Copyright (c) 2006 Simtec Electronics
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * This driver restricts frequency, polarity, "word" length and endian
17 * much more than the hardware does. I'm happy to unrestrict it, but
18 * only with what I can test myself (at time of writing, just SD/MMC
19 * SPI) and what people actually test and report.
20 */
21
22 #include <linux/types.h>
23 #include <linux/device.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_bitbang.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29 #include <asm/io.h>
30 #include <asm/arch/board.h>
31 #include <asm/arch/hwregs/reg_map.h>
32 #include <asm/arch/hwregs/reg_rdwr.h>
33 #include <asm/arch/hwregs/sser_defs.h>
34 #include <asm/arch/dma.h>
35 #include <asm/arch/hwregs/dma.h>
36
37 /* A size "not much larger" than the max typical transfer size. */
38 #define DMA_CHUNKSIZ 512
39
40 /*
41 * For a transfer expected to take this long, we busy-wait instead of enabling
42 * interrupts.
43 */
44 #define IRQ_USAGE_THRESHOLD_NS 14000
45
46 /* A few register access macros to avoid verbiage and reduce typos. */
47 #define REG_RD_DI(reg) REG_RD(dma, regi_dmain, reg)
48 #define REG_RD_DO(reg) REG_RD(dma, regi_dmaout, reg)
49 #define REG_RD_SSER(reg) REG_RD(sser, regi_sser, reg)
50 #define REG_WR_DI(reg, val) REG_WR(dma, regi_dmain, reg, val)
51 #define REG_WR_DO(reg, val) REG_WR(dma, regi_dmaout, reg, val)
52 #define REG_WR_SSER(reg, val) REG_WR(sser, regi_sser, reg, val)
53 #define REG_WRINT_DI(reg, val) REG_WR_INT(dma, regi_dmain, reg, val)
54 #define REG_WRINT_DO(reg, val) REG_WR_INT(dma, regi_dmaout, reg, val)
55 #define REG_WRINT_SSER(reg, val) REG_WR_INT(sser, regi_sser, reg, val)
56 #define REG_RDINT_DI(reg) REG_RD_INT(dma, regi_dmain, reg)
57 #define REG_RDINT_DO(reg) REG_RD_INT(dma, regi_dmaout, reg)
58 #define REG_RDINT_SSER(reg) REG_RD_INT(sser, regi_sser, reg)
59
60 #define DMA_WAIT_UNTIL_RESET(inst) \
61 do { \
62 reg_dma_rw_stat r; \
63 do { \
64 r = REG_RD(dma, (inst), rw_stat); \
65 } while (r.mode != regk_dma_rst); \
66 } while (0)
67
68 #define DMA_BUSY(inst) (REG_RD(dma, inst, rw_stream_cmd)).busy
69
70 /* Our main driver state. */
71 struct crisv32_spi_hw_info {
72 struct crisv32_regi_n_int sser;
73 struct crisv32_regi_n_int dmain;
74 struct crisv32_regi_n_int dmaout;
75
76 reg_sser_rw_cfg cfg;
77 reg_sser_rw_frm_cfg frm_cfg;
78 reg_sser_rw_tr_cfg tr_cfg;
79 reg_sser_rw_rec_cfg rec_cfg;
80 reg_sser_rw_extra extra;
81
82 /* We store the speed in kHz, so we can have expressions
83 * multiplying 100MHz by * 4 before dividing by it, and still
84 * keep it in an u32. */
85 u32 effective_speed_kHz;
86
87 /*
88 * The time in 10s of nanoseconds for half a cycles.
89 * For convenience and performance; derived from the above.
90 */
91 u32 half_cycle_delay_ns;
92
93 /* This should be overridable by a module parameter. */
94 u32 max_speed_Hz;
95
96 /* Pre-computed timout for the max transfer chunk-size. */
97 u32 dma_timeout;
98
99 struct completion dma_done;
100
101 /*
102 * If we get a timeout from wait_for_completion_timeout on the
103 * above, first look at this before panicking.
104 */
105 u32 dma_actually_done;
106
107 /*
108 * Resources don't seem available at the remove call, so we
109 * have to save information we get through them.
110 */
111 struct crisv32_spi_sser_controller_data *gc;
112 };
113
114 /*
115 * The driver state hides behind the spi_bitbang state; we're
116 * responsible for allocating that, so we can get a little something
117 * for ourselves.
118 */
119 struct crisv32_spi_sser_devdata {
120 struct spi_bitbang bitbang;
121 struct crisv32_spi_hw_info hw;
122 };
123
124 /* Our DMA descriptors that need alignment. */
125 struct crisv32_spi_dma_descrs {
126 dma_descr_context in_ctxt __attribute__ ((__aligned__(32)));
127 dma_descr_context out_ctxt __attribute__ ((__aligned__(32)));
128
129 /*
130 * The code takes advantage of the fact that in_descr and
131 * out_descr are on the same cache-line when working around
132 * the cache-bug in TR 106.
133 */
134 dma_descr_data in_descr __attribute__ ((__aligned__(16)));
135 dma_descr_data out_descr __attribute__ ((__aligned__(16)));
136 };
137
138 /*
139 * Whatever needs DMA access is here, besides whatever DMA-able memory
140 * comes in transfers.
141 */
142 struct crisv32_spi_dma_cs {
143 struct crisv32_spi_dma_descrs *descrp;
144
145 /* Scratch-buffers when the original was non-DMA. */
146 u8 rx_buf[DMA_CHUNKSIZ];
147 u8 tx_buf[DMA_CHUNKSIZ];
148 };
149
150 /*
151 * Max speed. If set, we won't go faster, promise. May be useful
152 * when dealing with weak hardware; misrouted signal paths or various
153 * debug-situations.
154 */
155 static ulong crisv32_spi_speed_limit_Hz = 0;
156
157 /* Helper function getting the driver state from a spi_device. */
158
159 static inline struct crisv32_spi_hw_info *spidev_to_hw(struct spi_device *spi)
160 {
161 struct crisv32_spi_sser_devdata *dd = spi_master_get_devdata(spi->master);
162 return &dd->hw;
163 }
164
165 /* SPI-bitbang word transmit-function for non-DMA. */
166
167 static u32 crisv32_spi_sser_txrx_mode3(struct spi_device *spi,
168 unsigned nsecs, u32 word, u8 bits)
169 {
170 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
171 u32 regi_sser = hw->sser.regi;
172 reg_sser_rw_ack_intr ack_intr = { .trdy = 1, .rdav = 1 };
173 reg_sser_r_intr intr = {0};
174 reg_sser_rw_tr_data w_data = { .data = (u8) word };
175 reg_sser_r_rec_data r_data;
176 u32 i;
177
178 /*
179 * The timeout reflects one iteration per 10ns (impossible at
180 * 200MHz clock even without the ndelay) and a wait for a full
181 * byte.
182 */
183 u32 timeout = 1000000/10*8/hw->effective_speed_kHz;
184
185 BUG_ON(bits != 8);
186
187 intr = REG_RD_SSER(r_intr);
188
189 /*
190 * We should never get xruns when we control the transmitter
191 * and receiver in register mode. And if we don't have
192 * transmitter-ready and data-ready on entry, something's
193 * seriously fishy.
194 */
195 if (!intr.trdy || !intr.rdav || intr.orun || intr.urun)
196 panic("sser hardware or SPI driver broken (1) 0x%x\n",
197 REG_TYPE_CONV(u32, reg_sser_r_intr, intr));
198
199 REG_WR_SSER(rw_ack_intr, ack_intr);
200 REG_WR_SSER(rw_tr_data, w_data);
201
202 for (i = 0; i < timeout; i++) {
203 intr = REG_RD_SSER(r_intr);
204 /* Wait for received data. */
205 if (intr.rdav)
206 break;
207 ndelay(10);
208 }
209
210 if (!(intr.trdy && intr.rdav) || intr.orun || intr.urun)
211 panic("sser hardware or SPI driver broken (2) 0x%x\n",
212 REG_TYPE_CONV(u32, reg_sser_r_intr, intr));
213
214 r_data = REG_RD_SSER(r_rec_data);
215 return r_data.data & 0xff;
216 }
217
218 /*
219 * Wait for 1/2 bit-time if the transmitter or receiver is enabled.
220 * We need to do this as the data-available indications may arrive
221 * right at the edge, with half the last cycle remaining.
222 */
223 static void inline crisv32_spi_sser_wait_halfabit(struct crisv32_spi_hw_info
224 *hw)
225 {
226 if (hw->cfg.en)
227 ndelay(hw->half_cycle_delay_ns);
228 }
229
230 /*
231 * Assert or de-assert chip-select.
232 * We have two functions, with the active one assigned to the bitbang
233 * slot at setup, to avoid a performance penalty (1% on reads).
234 */
235 static void crisv32_spi_sser_chip_select_active_high(struct spi_device *spi,
236 int value)
237 {
238 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
239 u32 regi_sser = hw->sser.regi;
240
241 /*
242 * We may have received data at the "last producing clock
243 * edge". Thus we delay for another half a clock cycle.
244 */
245 crisv32_spi_sser_wait_halfabit(hw);
246
247 hw->frm_cfg.frame_pin_use
248 = value == BITBANG_CS_ACTIVE ? regk_sser_gio1 : regk_sser_gio0;
249 REG_WR_SSER(rw_frm_cfg, hw->frm_cfg);
250 }
251
252 static void crisv32_spi_sser_chip_select_active_low(struct spi_device *spi,
253 int value)
254 {
255 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
256 u32 regi_sser = hw->sser.regi;
257
258 crisv32_spi_sser_wait_halfabit(hw);
259 hw->frm_cfg.frame_pin_use
260 = value == BITBANG_CS_ACTIVE ? regk_sser_gio0 : regk_sser_gio1;
261 REG_WR_SSER(rw_frm_cfg, hw->frm_cfg);
262 }
263
264 /* Set the transmission speed in Hz. */
265
266 static int crisv32_spi_sser_set_speed_Hz(struct crisv32_spi_hw_info *hw,
267 u32 Hz)
268 {
269 u32 kHz;
270 u32 ns_delay;
271 u32 regi_sser = hw->sser.regi;
272
273 if (Hz > hw->max_speed_Hz)
274 /*
275 * Should we complain? Return error? Current caller
276 * sequences want just the max speed.
277 */
278 Hz = hw->max_speed_Hz;
279
280 kHz = Hz/1000;
281
282 /*
283 * If absolutely needed, we *could* change the base frequency
284 * and go lower. Usually, a frequency set higher than wanted
285 * is a problem but lower isn't.
286 */
287 if (Hz < 100000000 / 65536 + 1) {
288 printk(KERN_ERR "attempt to set invalid sser speed: %u Hz\n",
289 Hz);
290 Hz = 100000000 / 65536 + 1;
291 }
292
293 pr_debug("setting sser speed to %u Hz\n", Hz);
294
295 /*
296 * Avoid going above the requested speed if there's a
297 * remainder for the 100 MHz clock-divider calculation, but
298 * don't unnecessarily go below if it's even.
299 */
300 hw->cfg.clk_div = 100000000/Hz - ((100000000 % Hz) == 0);
301
302 /* Make sure there's no ongoing transmission. */
303 crisv32_spi_sser_wait_halfabit(hw);
304
305 /*
306 * Wait for 3 times max of the old and the new clock before and after
307 * changing the frequency. Not because of documentation or empirical
308 * need, but because it seems sane to do so. The three-bit-times
309 * value is because that's the documented time it takes for a reset to
310 * take effect.
311 */
312 ns_delay = 1000000*3/(kHz > hw->effective_speed_kHz
313 ? kHz : hw->effective_speed_kHz);
314 ndelay(ns_delay);
315 REG_WR_SSER(rw_cfg, hw->cfg);
316 ndelay(ns_delay);
317
318 hw->effective_speed_kHz = kHz;
319
320 /*
321 * A timeout of twice the time for the largest chunk (not
322 * counting DMA overhead) plus one jiffy, should be more than
323 * enough for the transmission.
324 */
325 hw->dma_timeout = 1 + usecs_to_jiffies(1000*2*DMA_CHUNKSIZ*8/kHz);
326
327 hw->half_cycle_delay_ns
328 = 1000000/2/hw->effective_speed_kHz;
329
330 pr_debug(".clk_div %d, half %d, eff %d\n",
331 hw->cfg.clk_div, hw->half_cycle_delay_ns,
332 hw->effective_speed_kHz);
333 return 0;
334 }
335
336 /*
337 * Set up transmitter and receiver for non-DMA access.
338 * Unfortunately, it doesn't seem like hispeed works for this mode
339 * (mea culpa), so we're stuck with lospeed-mode. A little slower,
340 * but that's what you get for not allocating DMA.
341 */
342 static int crisv32_setup_spi_sser_for_reg_access(struct crisv32_spi_hw_info *hw)
343 {
344 u32 regi_sser = hw->sser.regi;
345
346 reg_sser_rw_cfg cfg = {0};
347 reg_sser_rw_frm_cfg frm_cfg = {0};
348 reg_sser_rw_tr_cfg tr_cfg = {0};
349 reg_sser_rw_rec_cfg rec_cfg = {0};
350 reg_sser_rw_intr_mask mask = {0};
351 reg_sser_rw_extra extra = {0};
352 reg_sser_rw_tr_data tr_data = {0};
353 reg_sser_r_intr intr;
354
355 cfg.en = 0;
356 tr_cfg.tr_en = 1;
357 rec_cfg.rec_en = 1;
358 REG_WR_SSER(rw_cfg, cfg);
359 REG_WR_SSER(rw_tr_cfg, tr_cfg);
360 REG_WR_SSER(rw_rec_cfg, rec_cfg);
361 REG_WR_SSER(rw_intr_mask, mask);
362
363 /*
364 * See 23.7.2 SPI in the hardware documentation.
365 * Except our configuration uses bulk mode; MMC/SD-SPI
366 * isn't isochronous in nature.
367 * Step 1.
368 */
369 cfg.gate_clk = regk_sser_yes;
370 cfg.clkgate_in = regk_sser_no;
371 cfg.clkgate_ctrl = regk_sser_tr;
372
373 /* Step 2. */
374 cfg.out_clk_pol = regk_sser_pos;
375 cfg.out_clk_src = regk_sser_intern_clk;
376
377 /* Step 3. */
378 tr_cfg.clk_src = regk_sser_intern;
379 rec_cfg.clk_src = regk_sser_intern;
380 frm_cfg.clk_src = regk_sser_intern;
381
382 /* Step 4. */
383 tr_cfg.clk_pol = regk_sser_neg;
384 rec_cfg.clk_pol = regk_sser_pos;
385 frm_cfg.clk_pol = regk_sser_neg;
386
387 /*
388 * Step 5: frame pin (PC03 or PD03) is frame; the status pin
389 * (PC02, PD02) is configured as input.
390 */
391 frm_cfg.frame_pin_dir = regk_sser_out;
392
393 /*
394 * Contrary to the doc example, we don't generate the frame
395 * signal "automatically". This setting of the frame pin as
396 * constant 1, reflects an inactive /CS setting, for just idle
397 * clocking. When we need to transmit or receive data, we
398 * change it.
399 */
400 frm_cfg.frame_pin_use = regk_sser_gio1;
401 frm_cfg.status_pin_dir = regk_sser_in;
402
403 /*
404 * Step 6. This is probably not necessary, as we don't
405 * generate the frame signal automatically. Nevertheless,
406 * modified for bulk transmission.
407 */
408 frm_cfg.out_on = regk_sser_tr;
409 frm_cfg.out_off = regk_sser_tr;
410
411 /* Step 7. Similarly, maybe not necessary. */
412 frm_cfg.type = regk_sser_level;
413 frm_cfg.level = regk_sser_neg_lo;
414
415 /* Step 8. These we have to set according to the bulk mode,
416 * which for tr_delay is the same as for iso; a value of 1
417 * means in sync with the frame signal. For rec_delay, we
418 * start it at the same time as the transmitter. See figure
419 * 23.7 in the hw documentation. */
420 frm_cfg.tr_delay = 1;
421 frm_cfg.rec_delay = 0;
422
423 /* Step 9. */
424 tr_cfg.sample_size = 7;
425 rec_cfg.sample_size = 7;
426
427 /* Step 10. */
428 frm_cfg.wordrate = 7;
429
430 /* Step 11 (but for bulk). */
431 tr_cfg.rate_ctrl = regk_sser_bulk;
432
433 /*
434 * Step 12. Similarly, maybe not necessary; still, modified
435 * for bulk.
436 */
437 tr_cfg.frm_src = regk_sser_intern;
438 rec_cfg.frm_src = regk_sser_tx_bulk;
439
440 /* Step 13. */
441 tr_cfg.mode = regk_sser_lospeed;
442 rec_cfg.mode = regk_sser_lospeed;
443
444 /* Step 14. */
445 tr_cfg.sh_dir = regk_sser_msbfirst;
446 rec_cfg.sh_dir = regk_sser_msbfirst;
447
448 /*
449 * Extra step for bulk-specific settings and other general
450 * settings not specified in the SPI config example.
451 * It's uncertain whether all of these are needed.
452 */
453 tr_cfg.bulk_wspace = 1;
454 tr_cfg.use_dma = 0;
455
456 tr_cfg.urun_stop = 1;
457 rec_cfg.orun_stop = 1;
458 rec_cfg.use_dma = 0;
459
460 rec_cfg.fifo_thr = regk_sser_inf;
461 frm_cfg.early_wend = regk_sser_yes;
462
463 cfg.clk_dir = regk_sser_out;
464 tr_cfg.data_pin_use = regk_sser_dout;
465 cfg.base_freq = regk_sser_f100;
466
467 /* Setup for the initial frequency given to us. */
468 hw->cfg = cfg;
469 crisv32_spi_sser_set_speed_Hz(hw, hw->max_speed_Hz);
470 cfg = hw->cfg;
471
472 /*
473 * Write it all, except cfg which is already written by
474 * crisv32_spi_sser_set_speed_Hz.
475 */
476 REG_WR_SSER(rw_frm_cfg, frm_cfg);
477 REG_WR_SSER(rw_tr_cfg, tr_cfg);
478 REG_WR_SSER(rw_rec_cfg, rec_cfg);
479 REG_WR_SSER(rw_extra, extra);
480
481 /*
482 * The transmit-register needs to be written before the
483 * transmitter is enabled, and to get a valid trdy signal
484 * waiting for us when we want to transmit a byte. Because
485 * the "frame event" is that the transmitter is written, this
486 * will cause a dummy 0xff-byte to be transmitted, but that's
487 * ok, because /CS is inactive.
488 */
489 tr_data.data = 0xffff;
490 REG_WR_SSER(rw_tr_data, tr_data);
491
492 /*
493 * We ack everything interrupt-wise; left-over indicators don't have
494 * to come from *this* code.
495 */
496 REG_WRINT_SSER(rw_ack_intr, -1);
497
498 /*
499 * Wait 3 cycles before enabling, after the transmit register
500 * has been written. (This'll be just a few microseconds for
501 * e.g. 400 KHz.)
502 */
503 ndelay(3 * 2 * hw->half_cycle_delay_ns);
504 cfg.en = 1;
505
506 REG_WR_SSER(rw_cfg, cfg);
507
508 /*
509 * Now wait for 8 + 3 cycles. The 0xff byte should now have
510 * been transmitted and dummy data received.
511 */
512 ndelay((8 + 3) * 2 * hw->half_cycle_delay_ns);
513
514 /*
515 * Sanity-check that we have data-available and the
516 * transmitter is ready to send new data.
517 */
518 intr = REG_RD_SSER(r_intr);
519 if (!intr.rdav || !intr.trdy)
520 panic("sser hw or SPI driver broken (3) 0x%x",
521 REG_TYPE_CONV(u32, reg_sser_r_intr, intr));
522
523 hw->frm_cfg = frm_cfg;
524 hw->tr_cfg = tr_cfg;
525 hw->rec_cfg = rec_cfg;
526 hw->extra = extra;
527 hw->cfg = cfg;
528 return 0;
529 }
530
531 /* Initialization, maybe fault recovery. */
532
533 static void crisv32_reset_dma_hw(u32 regi)
534 {
535 REG_WR_INT(dma, regi, rw_intr_mask, 0);
536
537 DMA_RESET(regi);
538 DMA_WAIT_UNTIL_RESET(regi);
539 DMA_ENABLE(regi);
540 REG_WR_INT(dma, regi, rw_ack_intr, -1);
541
542 DMA_WR_CMD(regi, regk_dma_set_w_size1);
543 }
544
545 /* Interrupt from SSER, for use with DMA when only the transmitter is used. */
546
547 static irqreturn_t sser_interrupt(int irqno, void *arg)
548 {
549 struct crisv32_spi_hw_info *hw = arg;
550 u32 regi_sser = hw->sser.regi;
551 reg_sser_r_intr intr = REG_RD_SSER(r_intr);
552
553 if (intr.tidle == 0 && intr.urun == 0) {
554 printk(KERN_ERR
555 "sser @0x%x: spurious sser intr, flags: 0x%x\n",
556 regi_sser, REG_TYPE_CONV(u32, reg_sser_r_intr, intr));
557 } else if (intr.urun == 0) {
558 hw->dma_actually_done = 1;
559 complete(&hw->dma_done);
560 } else {
561 /*
562 * Make any reception time out and notice the error,
563 * which it might not otherwise do data was *received*
564 * successfully.
565 */
566 u32 regi_dmain = hw->dmain.regi;
567
568 /*
569 * Recommended practice before acking urun is to turn
570 * off sser. That might not be enough to stop DMA-in
571 * from signalling success if the underrun was late in
572 * the transmission, so we disable the DMA-in
573 * interrupts too.
574 */
575 REG_WRINT_SSER(rw_cfg, 0);
576 REG_WRINT_DI(rw_intr_mask, 0);
577 REG_WRINT_DI(rw_ack_intr, -1);
578 }
579
580 REG_WRINT_SSER(rw_intr_mask, 0);
581
582 /*
583 * We must at least ack urun together with tidle, but keep it
584 * simple and ack them all.
585 */
586 REG_WRINT_SSER(rw_ack_intr, -1);
587
588 return IRQ_HANDLED;
589 }
590
591 /*
592 * Interrupt from receiver DMA connected to SSER, for use when the
593 * receiver is used, with or without the transmitter.
594 */
595 static irqreturn_t rec_dma_interrupt(int irqno, void *arg)
596 {
597 struct crisv32_spi_hw_info *hw = arg;
598 u32 regi_dmain = hw->dmain.regi;
599 u32 regi_sser = hw->sser.regi;
600 reg_dma_r_intr intr = REG_RD_DI(r_intr);
601
602 if (intr.data == 0) {
603 printk(KERN_ERR
604 "sser @0x%x: spurious rec dma intr, flags: 0x%x\n",
605 regi_dmain, REG_TYPE_CONV(u32, reg_dma_r_intr, intr));
606 } else {
607 hw->dma_actually_done = 1;
608 complete(&hw->dma_done);
609 }
610
611 REG_WRINT_DI(rw_intr_mask, 0);
612
613 /* Avoid false underrun indications; stop all sser interrupts. */
614 REG_WRINT_SSER(rw_intr_mask, 0);
615 REG_WRINT_SSER(rw_ack_intr, -1);
616
617 REG_WRINT_DI(rw_ack_intr, -1);
618 return IRQ_HANDLED;
619 }
620
621 /*
622 * Set up transmitter and receiver for DMA access. We use settings
623 * from the "Atmel fast flash" example.
624 */
625 static int crisv32_setup_spi_sser_for_dma_access(struct crisv32_spi_hw_info
626 *hw)
627 {
628 int ret;
629 u32 regi_sser = hw->sser.regi;
630
631 reg_sser_rw_cfg cfg = {0};
632 reg_sser_rw_frm_cfg frm_cfg = {0};
633 reg_sser_rw_tr_cfg tr_cfg = {0};
634 reg_sser_rw_rec_cfg rec_cfg = {0};
635 reg_sser_rw_intr_mask mask = {0};
636 reg_sser_rw_extra extra = {0};
637
638 cfg.en = 0;
639 tr_cfg.tr_en = 1;
640 rec_cfg.rec_en = 1;
641 REG_WR_SSER(rw_cfg, cfg);
642 REG_WR_SSER(rw_tr_cfg, tr_cfg);
643 REG_WR_SSER(rw_rec_cfg, rec_cfg);
644 REG_WR_SSER(rw_intr_mask, mask);
645
646 /*
647 * See 23.7.5.2 (Atmel fast flash) in the hardware documentation.
648 * Step 1.
649 */
650 cfg.gate_clk = regk_sser_no;
651
652 /* Step 2. */
653 cfg.out_clk_pol = regk_sser_pos;
654
655 /* Step 3. */
656 cfg.out_clk_src = regk_sser_intern_clk;
657
658 /* Step 4. */
659 tr_cfg.sample_size = 1;
660 rec_cfg.sample_size = 1;
661
662 /* Step 5. */
663 frm_cfg.wordrate = 7;
664
665 /* Step 6. */
666 tr_cfg.clk_src = regk_sser_intern;
667 rec_cfg.clk_src = regk_sser_intern;
668 frm_cfg.clk_src = regk_sser_intern;
669 tr_cfg.clk_pol = regk_sser_neg;
670 frm_cfg.clk_pol = regk_sser_neg;
671
672 /* Step 7. */
673 rec_cfg.clk_pol = regk_sser_pos;
674
675 /* Step 8. */
676 frm_cfg.tr_delay = 1;
677
678 /* Step 9. */
679 frm_cfg.rec_delay = 1;
680
681 /* Step 10. */
682 tr_cfg.sh_dir = regk_sser_msbfirst;
683 rec_cfg.sh_dir = regk_sser_msbfirst;
684
685 /* Step 11. */
686 tr_cfg.frm_src = regk_sser_intern;
687 rec_cfg.frm_src = regk_sser_intern;
688
689 /* Step 12. */
690 tr_cfg.rate_ctrl = regk_sser_iso;
691
692 /*
693 * Step 13. Note that 0 != tx_null, so we're good regarding
694 * the descriptor .md field.
695 */
696 tr_cfg.eop_stop = 1;
697
698 /* Step 14. */
699 frm_cfg.frame_pin_use = regk_sser_gio1;
700 frm_cfg.frame_pin_dir = regk_sser_out;
701
702 /* Step 15. */
703 extra.clkon_en = 1;
704 extra.clkoff_en = 1;
705
706 /* Step 16. We'll modify this value for each "burst". */
707 extra.clkoff_cycles = 7;
708
709 /* Step 17. */
710 cfg.prepare = 1;
711
712 /*
713 * Things left out from the documented startup procedure.
714 * It's uncertain whether all of these are needed.
715 */
716 frm_cfg.status_pin_dir = regk_sser_in;
717 tr_cfg.mode = regk_sser_hispeed;
718 rec_cfg.mode = regk_sser_hispeed;
719 frm_cfg.out_on = regk_sser_intern_tb;
720 frm_cfg.out_off = regk_sser_rec;
721 frm_cfg.type = regk_sser_level;
722 tr_cfg.use_dma = 1;
723 tr_cfg.urun_stop = 1;
724 rec_cfg.orun_stop = 1;
725 rec_cfg.use_dma = 1;
726 rec_cfg.fifo_thr = regk_sser_inf;
727 frm_cfg.early_wend = regk_sser_yes;
728 cfg.clk_dir = regk_sser_out;
729
730 tr_cfg.data_pin_use = regk_sser_dout;
731 cfg.base_freq = regk_sser_f100;
732
733 REG_WR_SSER(rw_frm_cfg, frm_cfg);
734 REG_WR_SSER(rw_tr_cfg, tr_cfg);
735 REG_WR_SSER(rw_rec_cfg, rec_cfg);
736 REG_WR_SSER(rw_extra, extra);
737 REG_WR_SSER(rw_cfg, cfg);
738 hw->frm_cfg = frm_cfg;
739 hw->tr_cfg = tr_cfg;
740 hw->rec_cfg = rec_cfg;
741 hw->extra = extra;
742 hw->cfg = cfg;
743
744 crisv32_spi_sser_set_speed_Hz(hw, hw->max_speed_Hz);
745
746 ret = request_irq(hw->sser.irq, sser_interrupt, 0, "sser", hw);
747 if (ret != 0)
748 goto noirq;
749
750 ret = request_irq(hw->dmain.irq, rec_dma_interrupt, 0, "sser rec", hw);
751 if (ret != 0)
752 goto free_outirq;
753
754 crisv32_reset_dma_hw(hw->dmain.regi);
755 crisv32_reset_dma_hw(hw->dmaout.regi);
756 return 0;
757
758 free_outirq:
759 free_irq(hw->sser.irq, hw);
760 noirq:
761 return ret;
762 }
763
764 /* SPI-master setup function for non-DMA. */
765
766 static int crisv32_spi_sser_regs_master_setup(struct spi_device *spi)
767 {
768 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
769 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
770 int ret = 0;
771
772 /* Just do a little initial constraining checks. */
773 if (spi->bits_per_word == 0)
774 spi->bits_per_word = 8;
775
776 if (spi->bits_per_word != 8)
777 return -EINVAL;
778
779 bitbang->chipselect = (spi->mode & SPI_CS_HIGH) != 0
780 ? crisv32_spi_sser_chip_select_active_high
781 : crisv32_spi_sser_chip_select_active_low;
782
783 if (hw->max_speed_Hz == 0) {
784 u32 max_speed_Hz;
785
786 /*
787 * At this time; at the first call to the SPI master
788 * setup function, spi->max_speed_hz reflects the
789 * board-init value. It will be changed later on by
790 * the protocol master, but at the master setup call
791 * is the only time we actually get to see the hw max
792 * and thus a reasonable time to init the hw field.
793 */
794
795 /* The module parameter overrides everything. */
796 if (crisv32_spi_speed_limit_Hz != 0)
797 max_speed_Hz = crisv32_spi_speed_limit_Hz;
798 /*
799 * I never could get hispeed mode to work for non-DMA.
800 * We adjust the max speed here (where we could
801 * presumably fix it), not in the board info file.
802 */
803 else if (spi->max_speed_hz > 16667000)
804 max_speed_Hz = 16667000;
805 else
806 max_speed_Hz = spi->max_speed_hz;
807
808 hw->max_speed_Hz = max_speed_Hz;
809 spi->max_speed_hz = max_speed_Hz;
810
811 /*
812 * We also do one-time initialization of the hardware at this
813 * point. We could defer to the return to the probe-function
814 * from spi_bitbang_start, but other hardware setup (like
815 * subsequent calls to this function before that) would have
816 * to be deferred until then too.
817 */
818 ret = crisv32_setup_spi_sser_for_reg_access(hw);
819 if (ret != 0)
820 return ret;
821
822 ret = spi_bitbang_setup(spi);
823 if (ret != 0)
824 return ret;
825
826 dev_info(&spi->dev,
827 "CRIS v32 SPI driver for sser%d\n",
828 spi->master->bus_num);
829 }
830
831 return 0;
832 }
833
834 /*
835 * SPI-master setup_transfer-function used for both DMA and non-DMA
836 * (single function for DMA, together with spi_bitbang_setup_transfer
837 * for non-DMA).
838 */
839
840 static int crisv32_spi_sser_common_setup_transfer(struct spi_device *spi,
841 struct spi_transfer *t)
842 {
843 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
844 u8 bits_per_word;
845 u32 hz;
846 int ret = 0;
847
848 if (t) {
849 bits_per_word = t->bits_per_word;
850 hz = t->speed_hz;
851 } else {
852 bits_per_word = 0;
853 hz = 0;
854 }
855
856 if (bits_per_word == 0)
857 bits_per_word = spi->bits_per_word;
858
859 if (bits_per_word != 8)
860 return -EINVAL;
861
862 if (hz == 0)
863 hz = spi->max_speed_hz;
864
865 if (hz != hw->effective_speed_kHz*1000 && hz != 0)
866 ret = crisv32_spi_sser_set_speed_Hz(hw, hz);
867
868 return ret;
869 }
870
871 /* Helper for a SPI-master setup_transfer function for non-DMA. */
872
873 static int crisv32_spi_sser_regs_setup_transfer(struct spi_device *spi,
874 struct spi_transfer *t)
875 {
876 int ret = crisv32_spi_sser_common_setup_transfer(spi, t);
877
878 if (ret != 0)
879 return ret;
880
881 /* Set up the loop-over-buffer parts. */
882 return spi_bitbang_setup_transfer (spi, t);
883 }
884
885 /* SPI-master setup function for DMA. */
886
887 static int crisv32_spi_sser_dma_master_setup(struct spi_device *spi)
888 {
889 /*
890 * As we don't dispatch to the spi_bitbang default function,
891 * we need to do whatever tests it does; keep it in sync. On
892 * the bright side, we can use the spi->controller_state slot;
893 * we use it for DMA:able memory for the descriptors and
894 * temporary buffers to copy non-DMA:able transfers.
895 */
896 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
897 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
898 struct crisv32_spi_dma_cs *cs;
899 u32 dmasize;
900 int ret = 0;
901
902 if (hw->max_speed_Hz == 0) {
903 struct crisv32_spi_dma_descrs *descrp;
904 u32 descrp_dma;
905 u32 max_speed_Hz;
906
907 /* The module parameter overrides everything. */
908 if (crisv32_spi_speed_limit_Hz != 0)
909 max_speed_Hz = crisv32_spi_speed_limit_Hz;
910 /*
911 * See comment at corresponding statement in
912 * crisv32_spi_sser_regs_master_setup.
913 */
914 else
915 max_speed_Hz = spi->max_speed_hz;
916
917 hw->max_speed_Hz = max_speed_Hz;
918 spi->max_speed_hz = max_speed_Hz;
919
920 ret = crisv32_setup_spi_sser_for_dma_access(hw);
921 if (ret != 0)
922 return ret;
923
924 /* Allocate some extra for necessary alignment. */
925 dmasize = sizeof *cs + 31
926 + sizeof(struct crisv32_spi_dma_descrs);
927
928 cs = kzalloc(dmasize, GFP_KERNEL | GFP_DMA);
929 if (cs == NULL)
930 return -ENOMEM;
931
932 /*
933 * Make descriptors aligned within the allocated area,
934 * some-place after cs.
935 */
936 descrp = (struct crisv32_spi_dma_descrs *)
937 (((u32) (cs + 1) + 31) & ~31);
938 descrp_dma = virt_to_phys(descrp);
939
940 /* Set up the "constant" parts of the descriptors. */
941 descrp->out_descr.eol = 1;
942 descrp->out_descr.intr = 1;
943 descrp->out_descr.out_eop = 1;
944 descrp->out_ctxt.saved_data = (dma_descr_data *)
945 (descrp_dma
946 + offsetof(struct crisv32_spi_dma_descrs, out_descr));
947 descrp->out_ctxt.next = 0;
948
949 descrp->in_descr.eol = 1;
950 descrp->in_descr.intr = 1;
951 descrp->in_ctxt.saved_data = (dma_descr_data *)
952 (descrp_dma
953 + offsetof(struct crisv32_spi_dma_descrs, in_descr));
954 descrp->in_ctxt.next = 0;
955
956 cs->descrp = descrp;
957 spi->controller_state = cs;
958
959 init_completion(&hw->dma_done);
960
961 dev_info(&spi->dev,
962 "CRIS v32 SPI driver for sser%d/DMA\n",
963 spi->master->bus_num);
964 }
965
966 /* Do our extra constraining checks. */
967 if (spi->bits_per_word == 0)
968 spi->bits_per_word = 8;
969
970 if (spi->bits_per_word != 8)
971 return -EINVAL;
972
973 /* SPI_LSB_FIRST deliberately left out, and we only support mode 3. */
974 if ((spi->mode & ~(SPI_TX_1|SPI_CS_HIGH)) != SPI_MODE_3)
975 return -EINVAL;
976
977 bitbang->chipselect = (spi->mode & SPI_CS_HIGH) != 0
978 ? crisv32_spi_sser_chip_select_active_high
979 : crisv32_spi_sser_chip_select_active_low;
980
981 ret = bitbang->setup_transfer(spi, NULL);
982 if (ret != 0)
983 return ret;
984
985 /* Remember to de-assert chip-select before the first transfer. */
986 spin_lock(&bitbang->lock);
987 if (!bitbang->busy) {
988 bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
989 ndelay(hw->half_cycle_delay_ns);
990 }
991 spin_unlock(&bitbang->lock);
992
993 return 0;
994 }
995
996 /* SPI-master cleanup function for DMA. */
997
998 static void crisv32_spi_sser_dma_cleanup(struct spi_device *spi)
999 {
1000 kfree(spi->controller_state);
1001 spi->controller_state = NULL;
1002 }
1003
1004 /*
1005 * Set up DMA transmitter descriptors for a chunk of data.
1006 * The caller is responsible for working around TR 106.
1007 */
1008 static void crisv32_spi_sser_setup_dma_descr_out(u32 regi,
1009 struct crisv32_spi_dma_cs *cs,
1010 u32 out_phys, u32 chunk_len)
1011 {
1012 BUG_ON(chunk_len > DMA_CHUNKSIZ);
1013 struct crisv32_spi_dma_descrs *descrp = cs->descrp;
1014 u32 descrp_dma = virt_to_phys(descrp);
1015
1016 descrp->out_descr.buf = (u8 *) out_phys;
1017 descrp->out_descr.after = (u8 *) out_phys + chunk_len;
1018 descrp->out_ctxt.saved_data_buf = (u8 *) out_phys;
1019
1020 DMA_START_CONTEXT(regi,
1021 descrp_dma
1022 + offsetof(struct crisv32_spi_dma_descrs, out_ctxt));
1023 }
1024
1025 /*
1026 * Set up DMA receiver descriptors for a chunk of data.
1027 * Also, work around TR 106.
1028 */
1029 static void crisv32_spi_sser_setup_dma_descr_in(u32 regi_dmain,
1030 struct crisv32_spi_dma_cs *cs,
1031 u32 in_phys, u32 chunk_len)
1032 {
1033 BUG_ON(chunk_len > DMA_CHUNKSIZ);
1034 struct crisv32_spi_dma_descrs *descrp = cs->descrp;
1035 u32 descrp_dma = virt_to_phys(descrp);
1036
1037 descrp->in_descr.buf = (u8 *) in_phys;
1038 descrp->in_descr.after = (u8 *) in_phys + chunk_len;
1039 descrp->in_ctxt.saved_data_buf = (u8 *) in_phys;
1040
1041 flush_dma_descr(&descrp->in_descr, 1);
1042
1043 DMA_START_CONTEXT(regi_dmain,
1044 descrp_dma
1045 + offsetof(struct crisv32_spi_dma_descrs, in_ctxt));
1046 }
1047
1048 /*
1049 * SPI-bitbang txrx_bufs function for DMA.
1050 * FIXME: We have SG DMA descriptors; use them.
1051 * (Requires abandoning the spi_bitbang framework if done reasonably.)
1052 */
1053 static int crisv32_spi_sser_dma_txrx_bufs(struct spi_device *spi,
1054 struct spi_transfer *t)
1055 {
1056 struct crisv32_spi_dma_cs *cs = spi->controller_state;
1057 struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
1058 u32 len = t->len;
1059 reg_sser_rw_cfg cfg = hw->cfg;
1060 reg_sser_rw_tr_cfg tr_cfg = hw->tr_cfg;
1061 reg_sser_rw_rec_cfg rec_cfg = hw->rec_cfg;
1062 reg_sser_rw_extra extra = hw->extra;
1063 u32 regi_sser = hw->sser.regi;
1064 u32 dmain = 0;
1065 u32 dmaout = 0;
1066 u32 regi_dmain = hw->dmain.regi;
1067 u8 *rx_buf = t->rx_buf;
1068
1069 /*
1070 * Using IRQ+completion is measured to give an overhead of 14
1071 * us, so let's instead busy-wait for the time that would be
1072 * wasted anyway, and get back sooner. We're not counting in
1073 * other overhead such as the DMA descriptor in the
1074 * time-expression, which causes us to use busy-wait for
1075 * data-lengths that actually take a bit longer than
1076 * IRQ_USAGE_THRESHOLD_NS. Still, with IRQ_USAGE_THRESHOLD_NS
1077 * = 14000, the threshold is for 20 MHz => 35 bytes, 25 => 44
1078 * and 50 => 88 and the typical SPI transfer lengths for
1079 * SDcard are { 1, 2, 7, 512 } bytes so a more complicated
1080 * would likely give nothing but worse performance due to
1081 * complexity.
1082 */
1083 int use_irq = len * hw->half_cycle_delay_ns
1084 > IRQ_USAGE_THRESHOLD_NS / 8 / 2;
1085
1086 if (len > DMA_CHUNKSIZ) {
1087 /*
1088 * It should be quite easy to adjust the code if the need
1089 * arises for something much larger than the preallocated
1090 * buffers (which could themselves easily just be increased)
1091 * but still what fits in extra.clkoff_cycles: kmalloc a
1092 * temporary dmaable buffer in this function and free it at
1093 * the end. No need to optimize rare requests. Until then,
1094 * we'll keep the code as simple as performance allows.
1095 * Alternatively or if we need to send even larger data,
1096 * consider calling self with the required number of "faked"
1097 * shorter transfers here.
1098 */
1099 dev_err(&spi->dev,
1100 "Trying to transfer %d > max %d bytes:"
1101 " need to adjust the SPI driver\n",
1102 len, DMA_CHUNKSIZ);
1103 return -EMSGSIZE;
1104 }
1105
1106 /*
1107 * Need to separately tell the hispeed machinery the number of
1108 * bits in this transmission.
1109 */
1110 extra.clkoff_cycles = len * 8 - 1;
1111
1112 if (t->tx_buf != NULL) {
1113 if (t->tx_dma == 0) {
1114 memcpy(cs->tx_buf, t->tx_buf, len);
1115 dmaout = virt_to_phys(cs->tx_buf);
1116 } else
1117 dmaout = t->tx_dma;
1118
1119 crisv32_spi_sser_setup_dma_descr_out(hw->dmaout.regi,
1120 cs, dmaout,
1121 len);
1122
1123 /* No need to do anything for TR 106; this DMA only reads. */
1124 tr_cfg.tr_en = 1;
1125 tr_cfg.data_pin_use = regk_sser_dout;
1126 } else {
1127 tr_cfg.data_pin_use = (spi->mode & SPI_TX_1)
1128 ? regk_sser_gio1 : regk_sser_gio0;
1129 tr_cfg.tr_en = 0;
1130 }
1131
1132 if (rx_buf != 0) {
1133 if (t->rx_dma == 0)
1134 dmain = virt_to_phys(cs->rx_buf);
1135 else
1136 dmain = t->rx_dma;
1137
1138 crisv32_spi_sser_setup_dma_descr_in(regi_dmain, cs,
1139 dmain, len);
1140 rec_cfg.rec_en = 1;
1141
1142 REG_WRINT_SSER(rw_ack_intr, -1);
1143 REG_WRINT_DI(rw_ack_intr, -1);
1144
1145 /*
1146 * If we're receiving, use the rec data interrupt from DMA as
1147 * a signal that the HW is done.
1148 */
1149 if (use_irq) {
1150 reg_sser_rw_intr_mask mask = { .urun = 1 };
1151 reg_dma_rw_intr_mask dmask = { .data = 1 };
1152
1153 REG_WR_DI(rw_intr_mask, dmask);
1154
1155 /*
1156 * Catch transmitter underruns too. We don't
1157 * have to conditionalize that on the
1158 * transmitter being enabled; it's off when
1159 * the transmitter is off. Any overruns will
1160 * be indicated by a timeout, so we don't have
1161 * to check for that specifically.
1162 */
1163 REG_WR_SSER(rw_intr_mask, mask);
1164 }
1165 } else {
1166 rec_cfg.rec_en = 0;
1167
1168 /*
1169 * Ack previous overrun, underrun and tidle interrupts. Or
1170 * why not all. We'll get orun and urun "normally" due to the
1171 * way hispeed is (documented to) work and need to clear them,
1172 * and we'll have a tidle from a previous transmit if we used
1173 * to both receive and transmit, but now only transmit.
1174 */
1175 REG_WRINT_SSER(rw_ack_intr, -1);
1176
1177 if (use_irq) {
1178 reg_sser_rw_intr_mask mask = { .urun = 1, .tidle = 1 };
1179 REG_WR_SSER(rw_intr_mask, mask);
1180 }
1181 }
1182
1183 REG_WR_SSER(rw_rec_cfg, rec_cfg);
1184 REG_WR_SSER(rw_tr_cfg, tr_cfg);
1185 REG_WR_SSER(rw_extra, extra);
1186
1187 /*
1188 * Barriers are needed to make sure that the completion inits don't
1189 * migrate past the register writes due to gcc scheduling.
1190 */
1191 mb();
1192 hw->dma_actually_done = 0;
1193 INIT_COMPLETION(hw->dma_done);
1194 mb();
1195
1196 /*
1197 * Wait until DMA tx FIFO has more than one byte (it reads one
1198 * directly then one "very quickly") before starting sser tx.
1199 */
1200 if (tr_cfg.tr_en) {
1201 u32 regi_dmaout = hw->dmaout.regi;
1202 u32 minlen = len > 2 ? 2 : len;
1203 while ((REG_RD_DO(rw_stat)).buf < minlen)
1204 ;
1205 }
1206
1207 /* Wait until DMA-in is finished reading the descriptors. */
1208 if (rec_cfg.rec_en)
1209 while (DMA_BUSY(regi_dmain))
1210 ;
1211 /*
1212 * Wait 3 cycles before enabling (with .prepare = 1).
1213 * FIXME: Can we cut this by some time already passed?
1214 */
1215 ndelay(3 * 2 * hw->half_cycle_delay_ns);
1216 cfg.en = 1;
1217 REG_WR_SSER(rw_cfg, cfg);
1218
1219 /*
1220 * Wait 3 more cycles plus 30 ns before letting go.
1221 * FIXME: Can we do something else before but after the
1222 * previous cfg write and cut this by the time already passed?
1223 */
1224 cfg.prepare = 0;
1225 hw->cfg = cfg;
1226 ndelay(3 * 2 * hw->half_cycle_delay_ns + 30);
1227
1228 REG_WR_SSER(rw_cfg, cfg);
1229
1230 /*, We'll disable sser next the time we change the configuration. */
1231 cfg.en = 0;
1232 cfg.prepare = 1;
1233 hw->cfg = cfg;
1234
1235 if (!use_irq) {
1236 /*
1237 * We use a timeout corresponding to one iteration per ns,
1238 * which of course is at least five * insns / loop times as
1239 * much as reality, but we'll avoid a need for reading hw
1240 * timers directly.
1241 */
1242 u32 countdown = IRQ_USAGE_THRESHOLD_NS;
1243
1244 do
1245 if (rec_cfg.rec_en == 0) {
1246 /* Using the transmitter only. */
1247 reg_sser_r_intr intr = REG_RD_SSER(r_intr);
1248
1249 if (intr.tidle != 0) {
1250 /*
1251 * Almost done... Just check if we
1252 * had a transmitter underrun too.
1253 */
1254 if (!intr.urun)
1255 goto transmission_done;
1256
1257 /*
1258 * Fall over to the "time is up" case;
1259 * no need to provide a special path
1260 * for the error case.
1261 */
1262 countdown = 1;
1263 }
1264 } else {
1265 /* Using at least the receiver. */
1266 if ((REG_RD_DI(r_intr)).data != 0) {
1267 if ((REG_RD_SSER(r_intr)).urun == 0)
1268 goto transmission_done;
1269 countdown = 1;
1270 }
1271 }
1272 while (--countdown != 0);
1273
1274 /*
1275 * The time is up. Something might be wrong, or perhaps we've
1276 * started using data lengths where the threshold was about a
1277 * magnitude wrong. Fall over to IRQ. Remember not to ack
1278 * interrupts here (but always above, before starting), else
1279 * we'll have a race condition with the interrupt.
1280 */
1281 if (!rec_cfg.rec_en) {
1282 reg_sser_rw_intr_mask mask = { .urun = 1, .tidle = 1 };
1283 REG_WR_SSER(rw_intr_mask, mask);
1284 } else {
1285 reg_dma_rw_intr_mask dmask = { .data = 1 };
1286 reg_sser_rw_intr_mask mask = { .urun = 1 };
1287
1288 /*
1289 * Never mind checking for tr being disabled; urun
1290 * won't happen then.
1291 */
1292 REG_WR_SSER(rw_intr_mask, mask);
1293 REG_WR_DI(rw_intr_mask, dmask);
1294 }
1295 }
1296
1297 if (!wait_for_completion_timeout(&hw->dma_done, hw->dma_timeout)
1298 /*
1299 * Have to keep track manually too, else we'll get a timeout
1300 * indication for being scheduled out too long, while the
1301 * completion will still have trigged.
1302 */
1303 && !hw->dma_actually_done) {
1304 u32 regi_dmaout = hw->dmaout.regi;
1305
1306 /*
1307 * Transfer timed out. Should not happen for a
1308 * working controller, except perhaps if the system is
1309 * badly conditioned, causing DMA memory bandwidth
1310 * starvation. Not much to do afterwards, but perhaps
1311 * reset DMA and sser and hope it works the next time.
1312 */
1313 REG_WRINT_SSER(rw_cfg, 0);
1314 REG_WR_SSER(rw_cfg, cfg);
1315 REG_WRINT_SSER(rw_intr_mask, 0);
1316 REG_WRINT_DI(rw_intr_mask, 0);
1317 REG_WRINT_SSER(rw_ack_intr, -1);
1318 crisv32_reset_dma_hw(hw->dmain.regi);
1319 crisv32_reset_dma_hw(hw->dmaout.regi);
1320
1321 dev_err(&spi->dev, "timeout %u bytes %u kHz\n",
1322 len, hw->effective_speed_kHz);
1323 dev_err(&spi->dev, "sser=(%x,%x,%x,%x,%x)\n",
1324 REG_RDINT_SSER(rw_cfg), REG_RDINT_SSER(rw_tr_cfg),
1325 REG_RDINT_SSER(rw_rec_cfg), REG_RDINT_SSER(rw_extra),
1326 REG_RDINT_SSER(r_intr));
1327 dev_err(&spi->dev, "tx=(%x,%x,%x,%x)\n",
1328 dmaout, REG_RDINT_DO(rw_stat), REG_RDINT_DO(rw_data),
1329 REG_RDINT_DO(r_intr));
1330 dev_err(&spi->dev, "rx=(%x,%x,%x,%x)\n",
1331 dmain, REG_RDINT_DI(rw_stat), REG_RDINT_DI(rw_data),
1332 REG_RDINT_DI(r_intr));
1333 return -EIO;
1334 }
1335
1336 transmission_done:
1337 /* Wait for the last half-cycle of the last cycle. */
1338 crisv32_spi_sser_wait_halfabit(hw);
1339
1340 /* Reset for another call. */
1341 REG_WR_SSER(rw_cfg, cfg);
1342
1343 /*
1344 * If we had to use the temp DMAable rec buffer, copy it to the right
1345 * position.
1346 */
1347 if (t->rx_buf != 0 && t->rx_dma == 0)
1348 memcpy (t->rx_buf, cs->rx_buf, len);
1349
1350 /*
1351 * All clear. The interrupt function disabled the interrupt, we don't
1352 * have to do more.
1353 */
1354 return len;
1355 }
1356
1357 /* Platform-device probe function. */
1358
1359 static int __devinit crisv32_spi_sser_probe(struct platform_device *dev)
1360 {
1361 struct spi_master *master;
1362 struct crisv32_spi_sser_devdata *dd;
1363 struct crisv32_spi_hw_info *hw;
1364 struct resource *res;
1365 struct crisv32_spi_sser_controller_data *gc;
1366 int ret;
1367
1368 /*
1369 * We need to get the controller data as a hardware resource,
1370 * or else it wouldn't be available until *after* the
1371 * spi_bitbang_start call!
1372 */
1373 res = platform_get_resource_byname(dev, 0, "controller_data_ptr");
1374 if (res == NULL) {
1375 dev_err(&dev->dev,
1376 "can't get controller_data resource at probe\n");
1377 return -EIO;
1378 }
1379
1380 gc = (struct crisv32_spi_sser_controller_data *) res->start;
1381
1382 master = spi_alloc_master(&dev->dev, sizeof *dd);
1383 if (master == NULL) {
1384 dev_err(&dev->dev, "failed to allocate spi master\n");
1385 ret = -ENOMEM;
1386 goto err;
1387 }
1388
1389 dd = spi_master_get_devdata(master);
1390 platform_set_drvdata(dev, dd);
1391
1392 /*
1393 * The device data asks for this driver, and holds the id
1394 * number, which must be unique among the same-type devices.
1395 * We use this as the number of this SPI bus.
1396 */
1397 master->bus_num = dev->id;
1398
1399 /* Setup SPI bitbang adapter hooks. */
1400 dd->bitbang.master = spi_master_get(master);
1401 dd->bitbang.chipselect = crisv32_spi_sser_chip_select_active_low;
1402
1403 hw = &dd->hw;
1404 hw->gc = gc;
1405
1406 /* Pre-spi_bitbang_start setup. */
1407 if (gc->using_dma) {
1408 /* Setup DMA and interrupts. */
1409 ret = gc->iface_allocate(&hw->sser, &hw->dmain, &hw->dmaout);
1410 if (ret != 0)
1411 goto err_no_regs;
1412
1413 dd->bitbang.master->setup = crisv32_spi_sser_dma_master_setup;
1414 dd->bitbang.setup_transfer
1415 = crisv32_spi_sser_common_setup_transfer;
1416 dd->bitbang.txrx_bufs = crisv32_spi_sser_dma_txrx_bufs;
1417 dd->bitbang.master->cleanup = crisv32_spi_sser_dma_cleanup;
1418 } else {
1419 /* Just registers, then. */
1420 ret = gc->iface_allocate(&hw->sser, NULL, NULL);
1421 if (ret != 0)
1422 goto err_no_regs;
1423
1424 dd->bitbang.master->setup
1425 = crisv32_spi_sser_regs_master_setup;
1426 dd->bitbang.setup_transfer
1427 = crisv32_spi_sser_regs_setup_transfer;
1428 dd->bitbang.master->cleanup = spi_bitbang_cleanup;
1429
1430 /*
1431 * We can do all modes pretty simply, but I have no
1432 * simple enough way to test them, so I won't.
1433 */
1434 dd->bitbang.txrx_word[SPI_MODE_3]
1435 = crisv32_spi_sser_txrx_mode3;
1436 }
1437
1438 ret = spi_bitbang_start(&dd->bitbang);
1439 if (ret)
1440 goto err_no_bitbang;
1441
1442 /*
1443 * We don't have a dev_info here, as initialization that may fail is
1444 * postponed to the first master->setup call. It's called from
1445 * spi_bitbang_start (above), where the call-chain doesn't look too
1446 * close at error return values; we'll get here successfully anyway,
1447 * so emitting a separate message here is at most confusing.
1448 */
1449 dev_dbg(&dev->dev,
1450 "CRIS v32 SPI driver for sser%d%s present\n",
1451 master->bus_num,
1452 gc->using_dma ? "/DMA" : "");
1453
1454 return 0;
1455
1456 err_no_bitbang:
1457 gc->iface_free();
1458
1459 err_no_regs:
1460 platform_set_drvdata(dev, NULL);
1461 spi_master_put(dd->bitbang.master);
1462
1463 err:
1464 return ret;
1465 }
1466
1467 /* Platform-device remove-function. */
1468
1469 static int __devexit crisv32_spi_sser_remove(struct platform_device *dev)
1470 {
1471 struct crisv32_spi_sser_devdata *dd = platform_get_drvdata(dev);
1472 struct crisv32_spi_hw_info *hw = &dd->hw;
1473 struct crisv32_spi_sser_controller_data *gc = hw->gc;
1474 int ret;
1475
1476 /* We need to stop all bitbanging activity separately. */
1477 ret = spi_bitbang_stop(&dd->bitbang);
1478 if (ret != 0)
1479 return ret;
1480
1481 spi_master_put(dd->bitbang.master);
1482
1483 /*
1484 * If we get here, the queue is empty and there's no activity;
1485 * it's safe to flip the switch on the interfaces.
1486 */
1487 if (gc->using_dma) {
1488 u32 regi_dmain = hw->dmain.regi;
1489 u32 regi_dmaout = hw->dmaout.regi;
1490 u32 regi_sser = hw->sser.regi;
1491
1492 REG_WRINT_SSER(rw_intr_mask, 0);
1493 REG_WRINT_DI(rw_intr_mask, 0);
1494 REG_WRINT_DO(rw_intr_mask, 0);
1495 hw->cfg.en = 0;
1496 REG_WR_SSER(rw_cfg, hw->cfg);
1497 DMA_RESET(regi_dmain);
1498 DMA_RESET(regi_dmaout);
1499 free_irq(hw->sser.irq, hw);
1500 free_irq(hw->dmain.irq, hw);
1501 }
1502
1503 gc->iface_free();
1504
1505 platform_set_drvdata(dev, NULL);
1506 return 0;
1507 }
1508
1509 /*
1510 * For the time being, there's no suspend/resume support to care
1511 * about, so those handlers default to NULL.
1512 */
1513 static struct platform_driver crisv32_spi_sser_drv = {
1514 .probe = crisv32_spi_sser_probe,
1515 .remove = __devexit_p(crisv32_spi_sser_remove),
1516 .driver = {
1517 .name = "spi_crisv32_sser",
1518 .owner = THIS_MODULE,
1519 },
1520 };
1521
1522 /* Module init function. */
1523
1524 static int __devinit crisv32_spi_sser_init(void)
1525 {
1526 return platform_driver_register(&crisv32_spi_sser_drv);
1527 }
1528
1529 /* Module exit function. */
1530
1531 static void __devexit crisv32_spi_sser_exit(void)
1532 {
1533 platform_driver_unregister(&crisv32_spi_sser_drv);
1534 }
1535
1536 /* Setter function for speed limit. */
1537
1538 static int crisv32_spi_speed_limit_Hz_setter(const char *val,
1539 struct kernel_param *kp)
1540 {
1541 char *endp;
1542 ulong num = simple_strtoul(val, &endp, 0);
1543 if (endp == val
1544 || *endp != 0
1545 || num <= 0
1546 /*
1547 * We can't go above 100 MHz speed. Actually we can't go
1548 * above 50 MHz using the sser support but it might make
1549 * sense trying.
1550 */
1551 || num > 100000000)
1552 return -EINVAL;
1553 *(ulong *) kp->arg = num;
1554 return 0;
1555 }
1556
1557 module_param_call(crisv32_spi_max_speed_hz,
1558 crisv32_spi_speed_limit_Hz_setter, param_get_ulong,
1559 &crisv32_spi_speed_limit_Hz, 0644);
1560
1561 module_init(crisv32_spi_sser_init);
1562 module_exit(crisv32_spi_sser_exit);
1563
1564 MODULE_DESCRIPTION("CRIS v32 SPI-SSER Driver");
1565 MODULE_AUTHOR("Hans-Peter Nilsson, <hp@axis.com>");
1566 MODULE_LICENSE("GPL");