summaryrefslogtreecommitdiff
path: root/target/linux/etrax-2.6/files/drivers/spi/spi_crisv32_sser.c
blob: e8d0e4973b0d81777e3431fd54c9b78110ae1889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
847
848
849
850
851
852
853
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
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
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
/*
 * SPI port driver for ETRAX FS et al. using a synchronous serial
 * port, but simplified by using the spi_bitbang framework.
 *
 * Copyright (c) 2007 Axis Communications AB
 *
 * Author: Hans-Peter Nilsson, though copying parts of
 * spi_s3c24xx_gpio.c, hence also:
 * Copyright (c) 2006 Ben Dooks
 * Copyright (c) 2006 Simtec Electronics
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This driver restricts frequency, polarity, "word" length and endian
 * much more than the hardware does.  I'm happy to unrestrict it, but
 * only with what I can test myself (at time of writing, just SD/MMC
 * SPI) and what people actually test and report.
 */

#include <linux/types.h>
#include <linux/device.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <asm/io.h>
#include <asm/arch/board.h>
#include <asm/arch/hwregs/reg_map.h>
#include <asm/arch/hwregs/reg_rdwr.h>
#include <asm/arch/hwregs/sser_defs.h>
#include <asm/arch/dma.h>
#include <asm/arch/hwregs/dma.h>

/* A size "not much larger" than the max typical transfer size.  */
#define DMA_CHUNKSIZ 512

/*
 * For a transfer expected to take this long, we busy-wait instead of enabling
 * interrupts.
 */
#define IRQ_USAGE_THRESHOLD_NS 14000

/* A few register access macros to avoid verbiage and reduce typos.  */
#define REG_RD_DI(reg) REG_RD(dma, regi_dmain, reg)
#define REG_RD_DO(reg) REG_RD(dma, regi_dmaout, reg)
#define REG_RD_SSER(reg) REG_RD(sser, regi_sser, reg)
#define REG_WR_DI(reg, val) REG_WR(dma, regi_dmain, reg, val)
#define REG_WR_DO(reg, val) REG_WR(dma, regi_dmaout, reg, val)
#define REG_WR_SSER(reg, val) REG_WR(sser, regi_sser, reg, val)
#define REG_WRINT_DI(reg, val) REG_WR_INT(dma, regi_dmain, reg, val)
#define REG_WRINT_DO(reg, val) REG_WR_INT(dma, regi_dmaout, reg, val)
#define REG_WRINT_SSER(reg, val) REG_WR_INT(sser, regi_sser, reg, val)
#define REG_RDINT_DI(reg) REG_RD_INT(dma, regi_dmain, reg)
#define REG_RDINT_DO(reg) REG_RD_INT(dma, regi_dmaout, reg)
#define REG_RDINT_SSER(reg) REG_RD_INT(sser, regi_sser, reg)

#define DMA_WAIT_UNTIL_RESET(inst)			\
  do {							\
	reg_dma_rw_stat r;				\
	do {						\
	  	r = REG_RD(dma, (inst), rw_stat);	\
	} while (r.mode != regk_dma_rst);		\
  } while (0)

#define DMA_BUSY(inst) (REG_RD(dma, inst, rw_stream_cmd)).busy

/* Our main driver state.  */
struct crisv32_spi_hw_info {
	struct crisv32_regi_n_int sser;
	struct crisv32_regi_n_int dmain;
	struct crisv32_regi_n_int dmaout;

	reg_sser_rw_cfg cfg;
	reg_sser_rw_frm_cfg frm_cfg;
	reg_sser_rw_tr_cfg tr_cfg;
	reg_sser_rw_rec_cfg rec_cfg;
	reg_sser_rw_extra extra;

	/* We store the speed in kHz, so we can have expressions
	 * multiplying 100MHz by * 4 before dividing by it, and still
	 * keep it in an u32. */
	u32 effective_speed_kHz;

	/*
	 * The time in 10s of nanoseconds for half a cycles.
	 * For convenience and performance; derived from the above.
	 */
	u32 half_cycle_delay_ns;

	/* This should be overridable by a module parameter.  */
	u32 max_speed_Hz;

	/* Pre-computed timout for the max transfer chunk-size.  */
	u32 dma_timeout;

	struct completion dma_done;

	/*
	 * If we get a timeout from wait_for_completion_timeout on the
	 * above, first look at this before panicking.
	 */
	u32 dma_actually_done;

	/*
	 * Resources don't seem available at the remove call, so we
	 * have to save information we get through them.
	 */
	struct crisv32_spi_sser_controller_data *gc;
};

/*
 * The driver state hides behind the spi_bitbang state; we're
 * responsible for allocating that, so we can get a little something
 * for ourselves.
 */
struct crisv32_spi_sser_devdata {
	struct spi_bitbang bitbang;
	struct crisv32_spi_hw_info hw;
};

/* Our DMA descriptors that need alignment.  */
struct crisv32_spi_dma_descrs {
	dma_descr_context in_ctxt __attribute__ ((__aligned__(32)));
	dma_descr_context out_ctxt __attribute__ ((__aligned__(32)));

	/*
	 * The code takes advantage of the fact that in_descr and
	 * out_descr are on the same cache-line when working around
	 * the cache-bug in TR 106.
	 */
	dma_descr_data in_descr __attribute__ ((__aligned__(16)));
	dma_descr_data out_descr __attribute__ ((__aligned__(16)));
};

/*
 * Whatever needs DMA access is here, besides whatever DMA-able memory
 * comes in transfers.
 */
struct crisv32_spi_dma_cs {
	struct crisv32_spi_dma_descrs *descrp;

	/* Scratch-buffers when the original was non-DMA.  */
	u8 rx_buf[DMA_CHUNKSIZ];
	u8 tx_buf[DMA_CHUNKSIZ];
};

/*
 * Max speed.  If set, we won't go faster, promise.  May be useful
 * when dealing with weak hardware; misrouted signal paths or various
 * debug-situations.
 */
static ulong crisv32_spi_speed_limit_Hz = 0;

/* Helper function getting the driver state from a spi_device.  */

static inline struct crisv32_spi_hw_info *spidev_to_hw(struct spi_device *spi)
{
	struct crisv32_spi_sser_devdata *dd = spi_master_get_devdata(spi->master);
	return &dd->hw;
}

/* SPI-bitbang word transmit-function for non-DMA.  */

static u32 crisv32_spi_sser_txrx_mode3(struct spi_device *spi,
				       unsigned nsecs, u32 word, u8 bits)
{
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	u32 regi_sser = hw->sser.regi;
	reg_sser_rw_ack_intr ack_intr = { .trdy = 1, .rdav = 1 };
	reg_sser_r_intr intr = {0};
	reg_sser_rw_tr_data w_data = { .data = (u8) word };
	reg_sser_r_rec_data r_data;
	u32 i;

	/*
	 * The timeout reflects one iteration per 10ns (impossible at
	 * 200MHz clock even without the ndelay) and a wait for a full
	 * byte.
	 */
	u32 timeout = 1000000/10*8/hw->effective_speed_kHz;

	BUG_ON(bits != 8);

	intr = REG_RD_SSER(r_intr);

	/*
	 * We should never get xruns when we control the transmitter
	 * and receiver in register mode.  And if we don't have
	 * transmitter-ready and data-ready on entry, something's
	 * seriously fishy.
	 */
	if (!intr.trdy || !intr.rdav || intr.orun || intr.urun)
		panic("sser hardware or SPI driver broken (1) 0x%x\n",
		      REG_TYPE_CONV(u32, reg_sser_r_intr, intr));

	REG_WR_SSER(rw_ack_intr, ack_intr);
	REG_WR_SSER(rw_tr_data, w_data);

	for (i = 0; i < timeout; i++) {
		intr = REG_RD_SSER(r_intr);
		/* Wait for received data.  */
		if (intr.rdav)
			break;
		ndelay(10);
	}

	if (!(intr.trdy && intr.rdav) || intr.orun || intr.urun)
		panic("sser hardware or SPI driver broken (2) 0x%x\n",
		      REG_TYPE_CONV(u32, reg_sser_r_intr, intr));

	r_data = REG_RD_SSER(r_rec_data);
	return r_data.data & 0xff;
}

/*
 * Wait for 1/2 bit-time if the transmitter or receiver is enabled.
 * We need to do this as the data-available indications may arrive
 * right at the edge, with half the last cycle remaining.
 */
static void inline crisv32_spi_sser_wait_halfabit(struct crisv32_spi_hw_info
						  *hw)
{
	if (hw->cfg.en)
		ndelay(hw->half_cycle_delay_ns);
}

/*
 * Assert or de-assert chip-select.
 * We have two functions, with the active one assigned to the bitbang
 * slot at setup, to avoid a performance penalty (1% on reads).
 */
static void crisv32_spi_sser_chip_select_active_high(struct spi_device *spi,
						     int value)
{
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	u32 regi_sser = hw->sser.regi;

	/*
	 * We may have received data at the "last producing clock
	 * edge".  Thus we delay for another half a clock cycle.
	 */
	crisv32_spi_sser_wait_halfabit(hw);

	hw->frm_cfg.frame_pin_use
		= value == BITBANG_CS_ACTIVE ? regk_sser_gio1 : regk_sser_gio0;
	REG_WR_SSER(rw_frm_cfg, hw->frm_cfg);
}

static void crisv32_spi_sser_chip_select_active_low(struct spi_device *spi,
						    int value)
{
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	u32 regi_sser = hw->sser.regi;

	crisv32_spi_sser_wait_halfabit(hw);
	hw->frm_cfg.frame_pin_use
		= value == BITBANG_CS_ACTIVE ? regk_sser_gio0 : regk_sser_gio1;
	REG_WR_SSER(rw_frm_cfg, hw->frm_cfg);
}

/* Set the transmission speed in Hz.  */

static int crisv32_spi_sser_set_speed_Hz(struct crisv32_spi_hw_info *hw,
					 u32 Hz)
{
	u32 kHz;
	u32 ns_delay;
	u32 regi_sser = hw->sser.regi;

	if (Hz > hw->max_speed_Hz)
		/*
		 * Should we complain?  Return error?  Current caller
		 * sequences want just the max speed.
		 */
		Hz = hw->max_speed_Hz;

	kHz = Hz/1000;

	/*
	 * If absolutely needed, we *could* change the base frequency
	 * and go lower.  Usually, a frequency set higher than wanted
	 * is a problem but lower isn't.
	 */
	if (Hz < 100000000 / 65536 + 1) {
		printk(KERN_ERR "attempt to set invalid sser speed: %u Hz\n",
		       Hz);
		Hz = 100000000 / 65536 + 1;
	}

	pr_debug("setting sser speed to %u Hz\n", Hz);

	/*
	 * Avoid going above the requested speed if there's a
	 * remainder for the 100 MHz clock-divider calculation, but
	 * don't unnecessarily go below if it's even.
	 */
	hw->cfg.clk_div = 100000000/Hz - ((100000000 % Hz) == 0);

	/* Make sure there's no ongoing transmission. */
	crisv32_spi_sser_wait_halfabit(hw);

	/*
	 * Wait for 3 times max of the old and the new clock before and after
	 * changing the frequency.  Not because of documentation or empirical
	 * need, but because it seems sane to do so.  The three-bit-times
	 * value is because that's the documented time it takes for a reset to
	 * take effect.
	 */
	ns_delay = 1000000*3/(kHz > hw->effective_speed_kHz
			      ? kHz : hw->effective_speed_kHz);
	ndelay(ns_delay);
	REG_WR_SSER(rw_cfg, hw->cfg);
	ndelay(ns_delay);

	hw->effective_speed_kHz = kHz;

	/*
	 * A timeout of twice the time for the largest chunk (not
	 * counting DMA overhead) plus one jiffy, should be more than
	 * enough for the transmission.
	 */
	hw->dma_timeout = 1 + usecs_to_jiffies(1000*2*DMA_CHUNKSIZ*8/kHz);

	hw->half_cycle_delay_ns
		= 1000000/2/hw->effective_speed_kHz;

	pr_debug(".clk_div %d, half %d, eff %d\n",
		 hw->cfg.clk_div, hw->half_cycle_delay_ns,
		 hw->effective_speed_kHz);
	return 0;
}

/*
 * Set up transmitter and receiver for non-DMA access.
 * Unfortunately, it doesn't seem like hispeed works for this mode
 * (mea culpa), so we're stuck with lospeed-mode.  A little slower,
 * but that's what you get for not allocating DMA.
 */
static int crisv32_setup_spi_sser_for_reg_access(struct crisv32_spi_hw_info *hw)
{
	u32 regi_sser = hw->sser.regi;

	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};
	reg_sser_rw_intr_mask mask = {0};
	reg_sser_rw_extra extra = {0};
	reg_sser_rw_tr_data tr_data = {0};
	reg_sser_r_intr intr;

	cfg.en = 0;
	tr_cfg.tr_en = 1;
	rec_cfg.rec_en = 1;
	REG_WR_SSER(rw_cfg, cfg);
	REG_WR_SSER(rw_tr_cfg, tr_cfg);
	REG_WR_SSER(rw_rec_cfg, rec_cfg);
	REG_WR_SSER(rw_intr_mask, mask);

	/*
	 * See 23.7.2 SPI in the hardware documentation.
	 * Except our configuration uses bulk mode; MMC/SD-SPI
	 * isn't isochronous in nature.
	 * Step 1.
	 */
	cfg.gate_clk = regk_sser_yes;
	cfg.clkgate_in = regk_sser_no;
	cfg.clkgate_ctrl = regk_sser_tr;

	/* Step 2.  */
	cfg.out_clk_pol = regk_sser_pos;
	cfg.out_clk_src = regk_sser_intern_clk;

	/* Step 3.  */
	tr_cfg.clk_src = regk_sser_intern;
	rec_cfg.clk_src = regk_sser_intern;
	frm_cfg.clk_src = regk_sser_intern;

	/* Step 4.  */
	tr_cfg.clk_pol = regk_sser_neg;
	rec_cfg.clk_pol = regk_sser_pos;
	frm_cfg.clk_pol = regk_sser_neg;

	/*
	 * Step 5: frame pin (PC03 or PD03) is frame; the status pin
	 * (PC02, PD02) is configured as input.
	 */
	frm_cfg.frame_pin_dir = regk_sser_out;

	/*
	 * Contrary to the doc example, we don't generate the frame
	 * signal "automatically".  This setting of the frame pin as
	 * constant 1, reflects an inactive /CS setting, for just idle
	 * clocking.  When we need to transmit or receive data, we
	 * change it.
	 */
	frm_cfg.frame_pin_use = regk_sser_gio1;
	frm_cfg.status_pin_dir = regk_sser_in;

	/*
	 * Step 6.  This is probably not necessary, as we don't
	 * generate the frame signal automatically.  Nevertheless,
	 * modified for bulk transmission.
	 */
	frm_cfg.out_on = regk_sser_tr;
	frm_cfg.out_off = regk_sser_tr;

	/* Step 7.  Similarly, maybe not necessary.  */
	frm_cfg.type = regk_sser_level;
	frm_cfg.level = regk_sser_neg_lo;

	/* Step 8.  These we have to set according to the bulk mode,
	 * which for tr_delay is the same as for iso; a value of 1
	 * means in sync with the frame signal.  For rec_delay, we
	 * start it at the same time as the transmitter.  See figure
	 * 23.7 in the hw documentation.  */
	frm_cfg.tr_delay = 1;
	frm_cfg.rec_delay = 0;

	/* Step 9.  */
	tr_cfg.sample_size = 7;
	rec_cfg.sample_size = 7;

	/* Step 10.  */
	frm_cfg.wordrate = 7;

	/* Step 11 (but for bulk).  */
	tr_cfg.rate_ctrl = regk_sser_bulk;

	/*
	 * Step 12.  Similarly, maybe not necessary; still, modified
	 * for bulk.
	 */
	tr_cfg.frm_src = regk_sser_intern;
	rec_cfg.frm_src = regk_sser_tx_bulk;

	/* Step 13.  */
	tr_cfg.mode = regk_sser_lospeed;
	rec_cfg.mode = regk_sser_lospeed;

	/* Step 14.  */
	tr_cfg.sh_dir = regk_sser_msbfirst;
	rec_cfg.sh_dir = regk_sser_msbfirst;

	/*
	 * Extra step for bulk-specific settings and other general
	 * settings not specified in the SPI config example.
	 * It's uncertain whether all of these are needed.
	 */
	tr_cfg.bulk_wspace = 1;
	tr_cfg.use_dma = 0;

	tr_cfg.urun_stop = 1;
	rec_cfg.orun_stop = 1;
	rec_cfg.use_dma = 0;

	rec_cfg.fifo_thr = regk_sser_inf;
	frm_cfg.early_wend = regk_sser_yes;

	cfg.clk_dir = regk_sser_out;
	tr_cfg.data_pin_use = regk_sser_dout;
	cfg.base_freq = regk_sser_f100;

	/* Setup for the initial frequency given to us.  */
	hw->cfg = cfg;
	crisv32_spi_sser_set_speed_Hz(hw, hw->max_speed_Hz);
	cfg = hw->cfg;

	/*
	 * Write it all, except cfg which is already written by
	 * crisv32_spi_sser_set_speed_Hz.
	 */
	REG_WR_SSER(rw_frm_cfg, frm_cfg);
	REG_WR_SSER(rw_tr_cfg, tr_cfg);
	REG_WR_SSER(rw_rec_cfg, rec_cfg);
	REG_WR_SSER(rw_extra, extra);

	/*
	 * The transmit-register needs to be written before the
	 * transmitter is enabled, and to get a valid trdy signal
	 * waiting for us when we want to transmit a byte.  Because
	 * the "frame event" is that the transmitter is written, this
	 * will cause a dummy 0xff-byte to be transmitted, but that's
	 * ok, because /CS is inactive.
	 */
	tr_data.data = 0xffff;
	REG_WR_SSER(rw_tr_data, tr_data);

	/*
	 * We ack everything interrupt-wise; left-over indicators don't have
	 * to come from *this* code.
	 */
	REG_WRINT_SSER(rw_ack_intr, -1);

	/*
	 * Wait 3 cycles before enabling, after the transmit register
	 * has been written.  (This'll be just a few microseconds for
	 * e.g. 400 KHz.)
	 */
	ndelay(3 * 2 * hw->half_cycle_delay_ns);
	cfg.en = 1;

	REG_WR_SSER(rw_cfg, cfg);

	/*
	 * Now wait for 8 + 3 cycles.  The 0xff byte should now have
	 * been transmitted and dummy data received.
	 */
	ndelay((8 + 3) * 2 * hw->half_cycle_delay_ns);

	/*
	 * Sanity-check that we have data-available and the
	 * transmitter is ready to send new data.
	 */
	intr = REG_RD_SSER(r_intr);
	if (!intr.rdav || !intr.trdy)
		panic("sser hw or SPI driver broken (3) 0x%x",
		      REG_TYPE_CONV(u32, reg_sser_r_intr, intr));

	hw->frm_cfg = frm_cfg;
	hw->tr_cfg = tr_cfg;
	hw->rec_cfg = rec_cfg;
	hw->extra = extra;
	hw->cfg = cfg;
	return 0;
}

/* Initialization, maybe fault recovery.  */

static void crisv32_reset_dma_hw(u32 regi)
{
	REG_WR_INT(dma, regi, rw_intr_mask, 0);

	DMA_RESET(regi);
	DMA_WAIT_UNTIL_RESET(regi);
	DMA_ENABLE(regi);
	REG_WR_INT(dma, regi, rw_ack_intr, -1);

	DMA_WR_CMD(regi, regk_dma_set_w_size1);
}

/* Interrupt from SSER, for use with DMA when only the transmitter is used.  */

static irqreturn_t sser_interrupt(int irqno, void *arg)
{
	struct crisv32_spi_hw_info *hw = arg;
	u32 regi_sser = hw->sser.regi;
	reg_sser_r_intr intr = REG_RD_SSER(r_intr);

	if (intr.tidle == 0 && intr.urun == 0) {
		printk(KERN_ERR
		       "sser @0x%x: spurious sser intr, flags: 0x%x\n",
		       regi_sser, REG_TYPE_CONV(u32, reg_sser_r_intr, intr));
	} else if (intr.urun == 0) {
		hw->dma_actually_done = 1;
		complete(&hw->dma_done);
	} else {
		/*
		 * Make any reception time out and notice the error,
		 * which it might not otherwise do data was *received*
		 * successfully.
		 */
		u32 regi_dmain = hw->dmain.regi;

		/*
		 * Recommended practice before acking urun is to turn
		 * off sser.  That might not be enough to stop DMA-in
		 * from signalling success if the underrun was late in
		 * the transmission, so we disable the DMA-in
		 * interrupts too.
		 */
		REG_WRINT_SSER(rw_cfg, 0);
		REG_WRINT_DI(rw_intr_mask, 0);
		REG_WRINT_DI(rw_ack_intr, -1);
	}

	REG_WRINT_SSER(rw_intr_mask, 0);

	/*
	 * We must at least ack urun together with tidle, but keep it
	 * simple and ack them all.
	 */
	REG_WRINT_SSER(rw_ack_intr, -1);

	return IRQ_HANDLED;
}

/*
 * Interrupt from receiver DMA connected to SSER, for use when the
 * receiver is used, with or without the transmitter.
 */
static irqreturn_t rec_dma_interrupt(int irqno, void *arg)
{
	struct crisv32_spi_hw_info *hw = arg;
	u32 regi_dmain = hw->dmain.regi;
	u32 regi_sser = hw->sser.regi;
	reg_dma_r_intr intr = REG_RD_DI(r_intr);

	if (intr.data == 0) {
		printk(KERN_ERR
		       "sser @0x%x: spurious rec dma intr, flags: 0x%x\n",
		       regi_dmain, REG_TYPE_CONV(u32, reg_dma_r_intr, intr));
	} else {
		hw->dma_actually_done = 1;
		complete(&hw->dma_done);
	}

	REG_WRINT_DI(rw_intr_mask, 0);

	/* Avoid false underrun indications; stop all sser interrupts.   */
	REG_WRINT_SSER(rw_intr_mask, 0);
	REG_WRINT_SSER(rw_ack_intr, -1);

	REG_WRINT_DI(rw_ack_intr, -1);
	return IRQ_HANDLED;
}

/*
 * Set up transmitter and receiver for DMA access.  We use settings
 * from the "Atmel fast flash" example.
 */
static int crisv32_setup_spi_sser_for_dma_access(struct crisv32_spi_hw_info
						 *hw)
{
	int ret;
	u32 regi_sser = hw->sser.regi;

	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};
	reg_sser_rw_intr_mask mask = {0};
	reg_sser_rw_extra extra = {0};

	cfg.en = 0;
	tr_cfg.tr_en = 1;
	rec_cfg.rec_en = 1;
	REG_WR_SSER(rw_cfg, cfg);
	REG_WR_SSER(rw_tr_cfg, tr_cfg);
	REG_WR_SSER(rw_rec_cfg, rec_cfg);
	REG_WR_SSER(rw_intr_mask, mask);

	/*
	 * See 23.7.5.2 (Atmel fast flash) in the hardware documentation.
	 * Step 1.
	 */
	cfg.gate_clk = regk_sser_no;

	/* Step 2.  */
	cfg.out_clk_pol = regk_sser_pos;

	/* Step 3.  */
	cfg.out_clk_src = regk_sser_intern_clk;

	/* Step 4.  */
	tr_cfg.sample_size = 1;
	rec_cfg.sample_size = 1;

	/* Step 5.  */
	frm_cfg.wordrate = 7;

	/* Step 6.  */
	tr_cfg.clk_src = regk_sser_intern;
	rec_cfg.clk_src = regk_sser_intern;
	frm_cfg.clk_src = regk_sser_intern;
	tr_cfg.clk_pol = regk_sser_neg;
	frm_cfg.clk_pol = regk_sser_neg;

	/* Step 7.  */
	rec_cfg.clk_pol = regk_sser_pos;

	/* Step 8.  */
	frm_cfg.tr_delay = 1;

	/* Step 9.  */
	frm_cfg.rec_delay = 1;

	/* Step 10.  */
	tr_cfg.sh_dir = regk_sser_msbfirst;
	rec_cfg.sh_dir = regk_sser_msbfirst;

	/* Step 11.  */
	tr_cfg.frm_src = regk_sser_intern;
	rec_cfg.frm_src = regk_sser_intern;

	/* Step 12.  */
	tr_cfg.rate_ctrl = regk_sser_iso;

	/*
	 * Step 13.  Note that 0 != tx_null, so we're good regarding
	 * the descriptor .md field.
	 */
	tr_cfg.eop_stop = 1;

	/* Step 14.  */
	frm_cfg.frame_pin_use = regk_sser_gio1;
	frm_cfg.frame_pin_dir = regk_sser_out;

	/* Step 15.  */
	extra.clkon_en = 1;
	extra.clkoff_en = 1;

	/* Step 16.  We'll modify this value for each "burst".  */
	extra.clkoff_cycles = 7;

	/* Step 17.  */
	cfg.prepare = 1;

	/*
	 * Things left out from the documented startup procedure.
	 * It's uncertain whether all of these are needed.
	 */
	frm_cfg.status_pin_dir = regk_sser_in;
	tr_cfg.mode = regk_sser_hispeed;
	rec_cfg.mode = regk_sser_hispeed;
	frm_cfg.out_on = regk_sser_intern_tb;
	frm_cfg.out_off = regk_sser_rec;
	frm_cfg.type = regk_sser_level;
	tr_cfg.use_dma = 1;
	tr_cfg.urun_stop = 1;
	rec_cfg.orun_stop = 1;
	rec_cfg.use_dma = 1;
	rec_cfg.fifo_thr = regk_sser_inf;
	frm_cfg.early_wend = regk_sser_yes;
	cfg.clk_dir = regk_sser_out;

	tr_cfg.data_pin_use = regk_sser_dout;
	cfg.base_freq = regk_sser_f100;

	REG_WR_SSER(rw_frm_cfg, frm_cfg);
	REG_WR_SSER(rw_tr_cfg, tr_cfg);
	REG_WR_SSER(rw_rec_cfg, rec_cfg);
	REG_WR_SSER(rw_extra, extra);
	REG_WR_SSER(rw_cfg, cfg);
	hw->frm_cfg = frm_cfg;
	hw->tr_cfg = tr_cfg;
	hw->rec_cfg = rec_cfg;
	hw->extra = extra;
	hw->cfg = cfg;

	crisv32_spi_sser_set_speed_Hz(hw, hw->max_speed_Hz);

	ret = request_irq(hw->sser.irq, sser_interrupt, 0, "sser", hw);
	if (ret != 0)
		goto noirq;

	ret = request_irq(hw->dmain.irq, rec_dma_interrupt, 0, "sser rec", hw);
	if (ret != 0)
		goto free_outirq;

	crisv32_reset_dma_hw(hw->dmain.regi);
	crisv32_reset_dma_hw(hw->dmaout.regi);
	return 0;

  free_outirq:
	free_irq(hw->sser.irq, hw);
  noirq:
	return ret;
}

/* SPI-master setup function for non-DMA.  */

static int crisv32_spi_sser_regs_master_setup(struct spi_device *spi)
{
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
	int ret = 0;

	/* Just do a little initial constraining checks.  */
	if (spi->bits_per_word == 0)
		spi->bits_per_word = 8;

	if (spi->bits_per_word != 8)
		return -EINVAL;

	bitbang->chipselect = (spi->mode & SPI_CS_HIGH) != 0
		? crisv32_spi_sser_chip_select_active_high
		: crisv32_spi_sser_chip_select_active_low;

	if (hw->max_speed_Hz == 0) {
		u32 max_speed_Hz;

		/*
		 * At this time; at the first call to the SPI master
		 * setup function, spi->max_speed_hz reflects the
		 * board-init value.  It will be changed later on by
		 * the protocol master, but at the master setup call
		 * is the only time we actually get to see the hw max
		 * and thus a reasonable time to init the hw field.
		 */

		/* The module parameter overrides everything.  */
		if (crisv32_spi_speed_limit_Hz != 0)
			max_speed_Hz = crisv32_spi_speed_limit_Hz;
		/*
		 * I never could get hispeed mode to work for non-DMA.
		 * We adjust the max speed here (where we could
		 * presumably fix it), not in the board info file.
		 */
		else if (spi->max_speed_hz > 16667000)
			max_speed_Hz = 16667000;
		else
			max_speed_Hz = spi->max_speed_hz;

		hw->max_speed_Hz = max_speed_Hz;
		spi->max_speed_hz = max_speed_Hz;

		/*
		 * We also do one-time initialization of the hardware at this
		 * point.  We could defer to the return to the probe-function
		 * from spi_bitbang_start, but other hardware setup (like
		 * subsequent calls to this function before that) would have
		 * to be deferred until then too.
		 */
		ret = crisv32_setup_spi_sser_for_reg_access(hw);
		if (ret != 0)
			return ret;

		ret = spi_bitbang_setup(spi);
		if (ret != 0)
			return ret;

		dev_info(&spi->dev,
			 "CRIS v32 SPI driver for sser%d\n",
			 spi->master->bus_num);
	}

	return 0;
}

/*
 * SPI-master setup_transfer-function used for both DMA and non-DMA
 * (single function for DMA, together with spi_bitbang_setup_transfer
 * for non-DMA).
 */

static int crisv32_spi_sser_common_setup_transfer(struct spi_device *spi,
						  struct spi_transfer *t)
{
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	u8 bits_per_word;
	u32 hz;
	int ret = 0;

	if (t) {
		bits_per_word = t->bits_per_word;
		hz = t->speed_hz;
	} else {
		bits_per_word = 0;
		hz = 0;
	}

	if (bits_per_word == 0)
		bits_per_word = spi->bits_per_word;

	if (bits_per_word != 8)
		return -EINVAL;

	if (hz == 0)
		hz = spi->max_speed_hz;

	if (hz != hw->effective_speed_kHz*1000 && hz != 0)
		ret = crisv32_spi_sser_set_speed_Hz(hw, hz);

	return ret;
}

/* Helper for a SPI-master setup_transfer function for non-DMA.  */

static int crisv32_spi_sser_regs_setup_transfer(struct spi_device *spi,
						struct spi_transfer *t)
{
	int ret = crisv32_spi_sser_common_setup_transfer(spi, t);

	if (ret != 0)
		return ret;

	/* Set up the loop-over-buffer parts.  */
	return spi_bitbang_setup_transfer (spi, t);
}

/* SPI-master setup function for DMA.  */

static int crisv32_spi_sser_dma_master_setup(struct spi_device *spi)
{
	/*
	 * As we don't dispatch to the spi_bitbang default function,
	 * we need to do whatever tests it does; keep it in sync.  On
	 * the bright side, we can use the spi->controller_state slot;
	 * we use it for DMA:able memory for the descriptors and
	 * temporary buffers to copy non-DMA:able transfers.
	 */
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
	struct crisv32_spi_dma_cs *cs;
	u32 dmasize;
	int ret = 0;

	if (hw->max_speed_Hz == 0) {
		struct crisv32_spi_dma_descrs *descrp;
		u32 descrp_dma;
		u32 max_speed_Hz;

		/* The module parameter overrides everything.  */
		if (crisv32_spi_speed_limit_Hz != 0)
			max_speed_Hz = crisv32_spi_speed_limit_Hz;
		/*
		 * See comment at corresponding statement in
		 * crisv32_spi_sser_regs_master_setup.
		 */
		else
			max_speed_Hz = spi->max_speed_hz;

		hw->max_speed_Hz = max_speed_Hz;
		spi->max_speed_hz = max_speed_Hz;

		ret = crisv32_setup_spi_sser_for_dma_access(hw);
		if (ret != 0)
			return ret;

		/* Allocate some extra for necessary alignment.  */
		dmasize = sizeof *cs + 31
			+ sizeof(struct crisv32_spi_dma_descrs);

		cs = kzalloc(dmasize, GFP_KERNEL | GFP_DMA);
		if (cs == NULL)
			return -ENOMEM;

		/*
		 * Make descriptors aligned within the allocated area,
		 * some-place after cs.
		 */
		descrp = (struct crisv32_spi_dma_descrs *)
			(((u32) (cs + 1) + 31) & ~31);
		descrp_dma = virt_to_phys(descrp);

		/* Set up the "constant" parts of the descriptors.  */
		descrp->out_descr.eol = 1;
		descrp->out_descr.intr = 1;
		descrp->out_descr.out_eop = 1;
		descrp->out_ctxt.saved_data = (dma_descr_data *)
		  (descrp_dma
		   + offsetof(struct crisv32_spi_dma_descrs, out_descr));
		descrp->out_ctxt.next = 0;

		descrp->in_descr.eol = 1;
		descrp->in_descr.intr = 1;
		descrp->in_ctxt.saved_data = (dma_descr_data *)
			(descrp_dma
			 + offsetof(struct crisv32_spi_dma_descrs, in_descr));
		descrp->in_ctxt.next = 0;

		cs->descrp = descrp;
		spi->controller_state = cs;

		init_completion(&hw->dma_done);

		dev_info(&spi->dev,
			 "CRIS v32 SPI driver for sser%d/DMA\n",
			 spi->master->bus_num);
	}

	/* Do our extra constraining checks.  */
	if (spi->bits_per_word == 0)
		spi->bits_per_word = 8;

	if (spi->bits_per_word != 8)
		return -EINVAL;

	/* SPI_LSB_FIRST deliberately left out, and we only support mode 3.  */
	if ((spi->mode & ~(SPI_TX_1|SPI_CS_HIGH)) != SPI_MODE_3)
		return -EINVAL;

	bitbang->chipselect = (spi->mode & SPI_CS_HIGH) != 0
		? crisv32_spi_sser_chip_select_active_high
		: crisv32_spi_sser_chip_select_active_low;

	ret = bitbang->setup_transfer(spi, NULL);
	if (ret != 0)
		return ret;

	/* Remember to de-assert chip-select before the first transfer.  */
	spin_lock(&bitbang->lock);
	if (!bitbang->busy) {
		bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
		ndelay(hw->half_cycle_delay_ns);
	}
	spin_unlock(&bitbang->lock);

	return 0;
}

/* SPI-master cleanup function for DMA.  */

static void crisv32_spi_sser_dma_cleanup(struct spi_device *spi)
{
	kfree(spi->controller_state);
	spi->controller_state = NULL;
}

/*
 * Set up DMA transmitter descriptors for a chunk of data.
 * The caller is responsible for working around TR 106.
 */
static void crisv32_spi_sser_setup_dma_descr_out(u32 regi,
						 struct crisv32_spi_dma_cs *cs,
						 u32 out_phys, u32 chunk_len)
{
	BUG_ON(chunk_len > DMA_CHUNKSIZ);
	struct crisv32_spi_dma_descrs *descrp = cs->descrp;
	u32 descrp_dma = virt_to_phys(descrp);

	descrp->out_descr.buf = (u8 *) out_phys;
	descrp->out_descr.after = (u8 *) out_phys + chunk_len;
	descrp->out_ctxt.saved_data_buf = (u8 *) out_phys;

	DMA_START_CONTEXT(regi,
			  descrp_dma
			  + offsetof(struct crisv32_spi_dma_descrs, out_ctxt));
}

/*
 * Set up DMA receiver descriptors for a chunk of data.
 * Also, work around TR 106.
 */
static void crisv32_spi_sser_setup_dma_descr_in(u32 regi_dmain,
						struct crisv32_spi_dma_cs *cs,
						u32 in_phys, u32 chunk_len)
{
	BUG_ON(chunk_len > DMA_CHUNKSIZ);
	struct crisv32_spi_dma_descrs *descrp = cs->descrp;
	u32 descrp_dma = virt_to_phys(descrp);

	descrp->in_descr.buf = (u8 *) in_phys;
	descrp->in_descr.after = (u8 *) in_phys + chunk_len;
	descrp->in_ctxt.saved_data_buf = (u8 *) in_phys;

	flush_dma_descr(&descrp->in_descr, 1);

	DMA_START_CONTEXT(regi_dmain,
			  descrp_dma
			  + offsetof(struct crisv32_spi_dma_descrs, in_ctxt));
}

/*
 * SPI-bitbang txrx_bufs function for DMA.
 * FIXME: We have SG DMA descriptors; use them.
 * (Requires abandoning the spi_bitbang framework if done reasonably.)
 */
static int crisv32_spi_sser_dma_txrx_bufs(struct spi_device *spi,
					  struct spi_transfer *t)
{
	struct crisv32_spi_dma_cs *cs = spi->controller_state;
	struct crisv32_spi_hw_info *hw = spidev_to_hw(spi);
	u32 len = t->len;
	reg_sser_rw_cfg cfg = hw->cfg;
	reg_sser_rw_tr_cfg tr_cfg = hw->tr_cfg;
	reg_sser_rw_rec_cfg rec_cfg = hw->rec_cfg;
	reg_sser_rw_extra extra = hw->extra;
	u32 regi_sser = hw->sser.regi;
	u32 dmain = 0;
	u32 dmaout = 0;
	u32 regi_dmain = hw->dmain.regi;
	u8 *rx_buf = t->rx_buf;

	/*
	 * Using IRQ+completion is measured to give an overhead of 14
	 * us, so let's instead busy-wait for the time that would be
	 * wasted anyway, and get back sooner.  We're not counting in
	 * other overhead such as the DMA descriptor in the
	 * time-expression, which causes us to use busy-wait for
	 * data-lengths that actually take a bit longer than
	 * IRQ_USAGE_THRESHOLD_NS.  Still, with IRQ_USAGE_THRESHOLD_NS
	 * = 14000, the threshold is for 20 MHz => 35 bytes, 25 => 44
	 * and 50 => 88 and the typical SPI transfer lengths for
	 * SDcard are { 1, 2, 7, 512 } bytes so a more complicated
	 * would likely give nothing but worse performance due to
	 * complexity.
	 */
	int use_irq = len * hw->half_cycle_delay_ns
		> IRQ_USAGE_THRESHOLD_NS / 8 / 2;

	if (len > DMA_CHUNKSIZ) {
		/*
		 * It should be quite easy to adjust the code if the need
		 * arises for something much larger than the preallocated
		 * buffers (which could themselves easily just be increased)
		 * but still what fits in extra.clkoff_cycles: kmalloc a
		 * temporary dmaable buffer in this function and free it at
		 * the end.  No need to optimize rare requests.  Until then,
		 * we'll keep the code as simple as performance allows.
		 * Alternatively or if we need to send even larger data,
		 * consider calling self with the required number of "faked"
		 * shorter transfers here.
		 */
		dev_err(&spi->dev,
			"Trying to transfer %d > max %d bytes:"
			" need to adjust the SPI driver\n",
			len, DMA_CHUNKSIZ);
		return -EMSGSIZE;
	}

	/*
	 * Need to separately tell the hispeed machinery the number of
	 * bits in this transmission.
	 */
	extra.clkoff_cycles = len * 8 - 1;

	if (t->tx_buf != NULL) {
		if (t->tx_dma == 0) {
			memcpy(cs->tx_buf, t->tx_buf, len);
			dmaout = virt_to_phys(cs->tx_buf);
		} else
			dmaout = t->tx_dma;

		crisv32_spi_sser_setup_dma_descr_out(hw->dmaout.regi,
						     cs, dmaout,
						     len);

		/* No need to do anything for TR 106; this DMA only reads.  */
		tr_cfg.tr_en = 1;
		tr_cfg.data_pin_use = regk_sser_dout;
	} else {
		tr_cfg.data_pin_use = (spi->mode & SPI_TX_1)
			? regk_sser_gio1 : regk_sser_gio0;
		tr_cfg.tr_en = 0;
	}

	if (rx_buf != 0) {
		if (t->rx_dma == 0)
			dmain = virt_to_phys(cs->rx_buf);
		else
			dmain = t->rx_dma;

		crisv32_spi_sser_setup_dma_descr_in(regi_dmain, cs,
						    dmain, len);
		rec_cfg.rec_en = 1;

		REG_WRINT_SSER(rw_ack_intr, -1);
		REG_WRINT_DI(rw_ack_intr, -1);

		/*
		 * If we're receiving, use the rec data interrupt from DMA as
		 * a signal that the HW is done.
		 */
		if (use_irq) {
			reg_sser_rw_intr_mask mask = { .urun = 1 };
			reg_dma_rw_intr_mask dmask = { .data = 1 };

			REG_WR_DI(rw_intr_mask, dmask);

			/*
			 * Catch transmitter underruns too.  We don't
			 * have to conditionalize that on the
			 * transmitter being enabled; it's off when
			 * the transmitter is off.  Any overruns will
			 * be indicated by a timeout, so we don't have
			 * to check for that specifically.
			 */
			REG_WR_SSER(rw_intr_mask, mask);
		}
	} else {
		rec_cfg.rec_en = 0;

		/*
		 * Ack previous overrun, underrun and tidle interrupts.  Or
		 * why not all.  We'll get orun and urun "normally" due to the
		 * way hispeed is (documented to) work and need to clear them,
		 * and we'll have a tidle from a previous transmit if we used
		 * to both receive and transmit, but now only transmit.
		 */
		REG_WRINT_SSER(rw_ack_intr, -1);

		if (use_irq) {
			reg_sser_rw_intr_mask mask = { .urun = 1, .tidle = 1 };
			REG_WR_SSER(rw_intr_mask, mask);
		}
	}

	REG_WR_SSER(rw_rec_cfg, rec_cfg);
	REG_WR_SSER(rw_tr_cfg, tr_cfg);
	REG_WR_SSER(rw_extra, extra);

	/*
	 * Barriers are needed to make sure that the completion inits don't
	 * migrate past the register writes due to gcc scheduling.
	 */
	mb();
	hw->dma_actually_done = 0;
	INIT_COMPLETION(hw->dma_done);
	mb();

	/*
	 * Wait until DMA tx FIFO has more than one byte (it reads one
	 * directly then one "very quickly") before starting sser tx.
	 */
	if (tr_cfg.tr_en) {
		u32 regi_dmaout = hw->dmaout.regi;
		u32 minlen = len > 2 ? 2 : len;
		while ((REG_RD_DO(rw_stat)).buf < minlen)
			;
	}

	/* Wait until DMA-in is finished reading the descriptors.  */
	if (rec_cfg.rec_en)
		while (DMA_BUSY(regi_dmain))
			;
	/*
	 * Wait 3 cycles before enabling (with .prepare = 1).
	 * FIXME: Can we cut this by some time already passed?
	 */
	ndelay(3 * 2 * hw->half_cycle_delay_ns);
	cfg.en = 1;
	REG_WR_SSER(rw_cfg, cfg);

	/*
	 * Wait 3 more cycles plus 30 ns before letting go.
	 * FIXME: Can we do something else before but after the
	 * previous cfg write and cut this by the time already passed?
	 */
	cfg.prepare = 0;
	hw->cfg = cfg;
	ndelay(3 * 2 * hw->half_cycle_delay_ns + 30);

	REG_WR_SSER(rw_cfg, cfg);

	/*, We'll disable sser next the time we change the configuration.  */
	cfg.en = 0;
	cfg.prepare = 1;
	hw->cfg = cfg;

	if (!use_irq) {
		/*
		 * We use a timeout corresponding to one iteration per ns,
		 * which of course is at least five * insns / loop times as
		 * much as reality, but we'll avoid a need for reading hw
		 * timers directly.
		 */
		u32 countdown = IRQ_USAGE_THRESHOLD_NS;

		do
			if (rec_cfg.rec_en == 0) {
				/* Using the transmitter only.  */
				reg_sser_r_intr intr = REG_RD_SSER(r_intr);

				if (intr.tidle != 0) {
					/*
					 * Almost done...  Just check if we
					 * had a transmitter underrun too.
					 */
					if (!intr.urun)
						goto transmission_done;

					/*
					 * Fall over to the "time is up" case;
					 * no need to provide a special path
					 * for the error case.
					 */
					countdown = 1;
				}
			} else {
				/* Using at least the receiver.  */
				if ((REG_RD_DI(r_intr)).data != 0) {
					if ((REG_RD_SSER(r_intr)).urun == 0)
						goto transmission_done;
					countdown = 1;
				}
			}
		while (--countdown != 0);

		/*
		 * The time is up.  Something might be wrong, or perhaps we've
		 * started using data lengths where the threshold was about a
		 * magnitude wrong.  Fall over to IRQ.  Remember not to ack
		 * interrupts here (but always above, before starting), else
		 * we'll have a race condition with the interrupt.
		 */
		if (!rec_cfg.rec_en) {
			reg_sser_rw_intr_mask mask = { .urun = 1, .tidle = 1 };
			REG_WR_SSER(rw_intr_mask, mask);
		} else {
			reg_dma_rw_intr_mask dmask = { .data = 1 };
			reg_sser_rw_intr_mask mask = { .urun = 1 };

			/*
			 * Never mind checking for tr being disabled; urun
			 * won't happen then.
			 */
			REG_WR_SSER(rw_intr_mask, mask);
			REG_WR_DI(rw_intr_mask, dmask);
		}
	}

	if (!wait_for_completion_timeout(&hw->dma_done, hw->dma_timeout)
	    /*
	     * Have to keep track manually too, else we'll get a timeout
	     * indication for being scheduled out too long, while the
	     * completion will still have trigged.
	     */
	    && !hw->dma_actually_done) {
		u32 regi_dmaout = hw->dmaout.regi;

		/*
		 * Transfer timed out.  Should not happen for a
		 * working controller, except perhaps if the system is
		 * badly conditioned, causing DMA memory bandwidth
		 * starvation.  Not much to do afterwards, but perhaps
		 * reset DMA and sser and hope it works the next time.
		 */
		REG_WRINT_SSER(rw_cfg, 0);
		REG_WR_SSER(rw_cfg, cfg);
		REG_WRINT_SSER(rw_intr_mask, 0);
		REG_WRINT_DI(rw_intr_mask, 0);
		REG_WRINT_SSER(rw_ack_intr, -1);
		crisv32_reset_dma_hw(hw->dmain.regi);
		crisv32_reset_dma_hw(hw->dmaout.regi);

		dev_err(&spi->dev, "timeout %u bytes %u kHz\n",
			len, hw->effective_speed_kHz);
		dev_err(&spi->dev, "sser=(%x,%x,%x,%x,%x)\n",
			REG_RDINT_SSER(rw_cfg), REG_RDINT_SSER(rw_tr_cfg),
			REG_RDINT_SSER(rw_rec_cfg), REG_RDINT_SSER(rw_extra),
			REG_RDINT_SSER(r_intr));
		dev_err(&spi->dev, "tx=(%x,%x,%x,%x)\n",
			dmaout, REG_RDINT_DO(rw_stat), REG_RDINT_DO(rw_data),
			REG_RDINT_DO(r_intr));
		dev_err(&spi->dev, "rx=(%x,%x,%x,%x)\n",
			dmain, REG_RDINT_DI(rw_stat), REG_RDINT_DI(rw_data),
			REG_RDINT_DI(r_intr));
		return -EIO;
	}

 transmission_done:
	/* Wait for the last half-cycle of the last cycle.  */
	crisv32_spi_sser_wait_halfabit(hw);

	/* Reset for another call.  */
	REG_WR_SSER(rw_cfg, cfg);

	/*
	 * If we had to use the temp DMAable rec buffer, copy it to the right
	 * position.
	 */
	if (t->rx_buf != 0 && t->rx_dma == 0)
		memcpy (t->rx_buf, cs->rx_buf, len);

	/*
	 * All clear.  The interrupt function disabled the interrupt, we don't
	 * have to do more.
	 */
	return len;
}

/* Platform-device probe function.  */

static int __devinit crisv32_spi_sser_probe(struct platform_device *dev)
{
	struct spi_master *master;
	struct crisv32_spi_sser_devdata *dd;
	struct crisv32_spi_hw_info *hw;
	struct resource *res;
	struct crisv32_spi_sser_controller_data *gc;
	int ret;

	/*
	 * We need to get the controller data as a hardware resource,
	 * or else it wouldn't be available until *after* the
	 * spi_bitbang_start call!
	 */
	res = platform_get_resource_byname(dev, 0, "controller_data_ptr");
	if (res == NULL) {
		dev_err(&dev->dev,
			"can't get controller_data resource at probe\n");
		return -EIO;
	}

	gc = (struct crisv32_spi_sser_controller_data *) res->start;

	master = spi_alloc_master(&dev->dev, sizeof *dd);
	if (master == NULL) {
		dev_err(&dev->dev, "failed to allocate spi master\n");
		ret = -ENOMEM;
		goto err;
	}

	dd = spi_master_get_devdata(master);
	platform_set_drvdata(dev, dd);

	/*
	 * The device data asks for this driver, and holds the id
	 * number, which must be unique among the same-type devices.
	 * We use this as the number of this SPI bus.
	 */
	master->bus_num = dev->id;

	/* Setup SPI bitbang adapter hooks.  */
	dd->bitbang.master = spi_master_get(master);
	dd->bitbang.chipselect = crisv32_spi_sser_chip_select_active_low;

	hw = &dd->hw;
	hw->gc = gc;

	/* Pre-spi_bitbang_start setup. */
	if (gc->using_dma) {
		/* Setup DMA and interrupts.  */
		ret = gc->iface_allocate(&hw->sser, &hw->dmain, &hw->dmaout);
		if (ret != 0)
			goto err_no_regs;

		dd->bitbang.master->setup = crisv32_spi_sser_dma_master_setup;
		dd->bitbang.setup_transfer
			= crisv32_spi_sser_common_setup_transfer;
		dd->bitbang.txrx_bufs = crisv32_spi_sser_dma_txrx_bufs;
		dd->bitbang.master->cleanup = crisv32_spi_sser_dma_cleanup;
	} else {
		/* Just registers, then.  */
		ret = gc->iface_allocate(&hw->sser, NULL, NULL);
		if (ret != 0)
			goto err_no_regs;

		dd->bitbang.master->setup
			= crisv32_spi_sser_regs_master_setup;
		dd->bitbang.setup_transfer
			= crisv32_spi_sser_regs_setup_transfer;
		dd->bitbang.master->cleanup = spi_bitbang_cleanup;

		/*
		 * We can do all modes pretty simply, but I have no
		 * simple enough way to test them, so I won't.
		 */
		dd->bitbang.txrx_word[SPI_MODE_3]
			= crisv32_spi_sser_txrx_mode3;
	}

	ret = spi_bitbang_start(&dd->bitbang);
	if (ret)
		goto err_no_bitbang;

	/*
	 * We don't have a dev_info here, as initialization that may fail is
	 * postponed to the first master->setup call.  It's called from
	 * spi_bitbang_start (above), where the call-chain doesn't look too
	 * close at error return values; we'll get here successfully anyway,
	 * so emitting a separate message here is at most confusing.
	 */
	dev_dbg(&dev->dev,
		"CRIS v32 SPI driver for sser%d%s present\n",
		master->bus_num,
		gc->using_dma ? "/DMA" : "");

	return 0;

 err_no_bitbang:
	gc->iface_free();

 err_no_regs:
	platform_set_drvdata(dev, NULL);
	spi_master_put(dd->bitbang.master);

 err:
	return ret;
}

/* Platform-device remove-function.  */

static int __devexit crisv32_spi_sser_remove(struct platform_device *dev)
{
	struct crisv32_spi_sser_devdata *dd = platform_get_drvdata(dev);
	struct crisv32_spi_hw_info *hw = &dd->hw;
	struct crisv32_spi_sser_controller_data *gc = hw->gc;
	int ret;

	/* We need to stop all bitbanging activity separately.  */
	ret = spi_bitbang_stop(&dd->bitbang);
	if (ret != 0)
		return ret;

	spi_master_put(dd->bitbang.master);

	/*
	 * If we get here, the queue is empty and there's no activity;
	 * it's safe to flip the switch on the interfaces.
	 */
	if (gc->using_dma) {
		u32 regi_dmain = hw->dmain.regi;
		u32 regi_dmaout = hw->dmaout.regi;
		u32 regi_sser = hw->sser.regi;

		REG_WRINT_SSER(rw_intr_mask, 0);
		REG_WRINT_DI(rw_intr_mask, 0);
		REG_WRINT_DO(rw_intr_mask, 0);
		hw->cfg.en = 0;
		REG_WR_SSER(rw_cfg, hw->cfg);
		DMA_RESET(regi_dmain);
		DMA_RESET(regi_dmaout);
		free_irq(hw->sser.irq, hw);
		free_irq(hw->dmain.irq, hw);
	}

	gc->iface_free();

	platform_set_drvdata(dev, NULL);
	return 0;
}

/*
 * For the time being, there's no suspend/resume support to care
 * about, so those handlers default to NULL.
 */
static struct platform_driver crisv32_spi_sser_drv = {
	.probe		= crisv32_spi_sser_probe,
	.remove		= __devexit_p(crisv32_spi_sser_remove),
	.driver		= {
		.name	= "spi_crisv32_sser",
		.owner	= THIS_MODULE,
	},
};

/* Module init function.  */

static int __devinit crisv32_spi_sser_init(void)
{
	return platform_driver_register(&crisv32_spi_sser_drv);
}

/* Module exit function.  */

static void __devexit crisv32_spi_sser_exit(void)
{
	platform_driver_unregister(&crisv32_spi_sser_drv);
}

/* Setter function for speed limit.  */

static int crisv32_spi_speed_limit_Hz_setter(const char *val,
					     struct kernel_param *kp)
{
	char *endp;
	ulong num = simple_strtoul(val, &endp, 0);
	if (endp == val
	    || *endp != 0
	    || num <= 0
	    /*
	     * We can't go above 100 MHz speed.  Actually we can't go
	     * above 50 MHz using the sser support but it might make
	     * sense trying.
	     */
	    || num > 100000000)
		return -EINVAL;
	*(ulong *) kp->arg = num;
	return 0;
}

module_param_call(crisv32_spi_max_speed_hz,
		  crisv32_spi_speed_limit_Hz_setter, param_get_ulong,
		  &crisv32_spi_speed_limit_Hz, 0644);

module_init(crisv32_spi_sser_init);
module_exit(crisv32_spi_sser_exit);

MODULE_DESCRIPTION("CRIS v32 SPI-SSER Driver");
MODULE_AUTHOR("Hans-Peter Nilsson, <hp@axis.com>");
MODULE_LICENSE("GPL");