@@ -188,22 +188,24 @@ struct lpi2c_imx_struct {
188
188
struct i2c_client * target ;
189
189
};
190
190
191
- #define lpi2c_imx_read_msr_poll_timeout (val , cond ) \
191
+ #define lpi2c_imx_read_msr_poll_timeout (atomic , val , cond ) \
192
+ (atomic ? readl_poll_timeout_atomic(lpi2c_imx->base + LPI2C_MSR, val, \
193
+ cond, 0, 500000) : \
192
194
readl_poll_timeout(lpi2c_imx->base + LPI2C_MSR, val, cond, \
193
- 0, 500000)
195
+ 0, 500000))
194
196
195
197
static void lpi2c_imx_intctrl (struct lpi2c_imx_struct * lpi2c_imx ,
196
198
unsigned int enable )
197
199
{
198
200
writel (enable , lpi2c_imx -> base + LPI2C_MIER );
199
201
}
200
202
201
- static int lpi2c_imx_bus_busy (struct lpi2c_imx_struct * lpi2c_imx )
203
+ static int lpi2c_imx_bus_busy (struct lpi2c_imx_struct * lpi2c_imx , bool atomic )
202
204
{
203
205
unsigned int temp ;
204
206
int err ;
205
207
206
- err = lpi2c_imx_read_msr_poll_timeout (temp ,
208
+ err = lpi2c_imx_read_msr_poll_timeout (atomic , temp ,
207
209
temp & (MSR_ALF | MSR_BBF | MSR_MBF ));
208
210
209
211
/* check for arbitration lost, clear if set */
@@ -248,7 +250,7 @@ static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx)
248
250
}
249
251
250
252
static int lpi2c_imx_start (struct lpi2c_imx_struct * lpi2c_imx ,
251
- struct i2c_msg * msgs )
253
+ struct i2c_msg * msgs , bool atomic )
252
254
{
253
255
unsigned int temp ;
254
256
@@ -260,17 +262,17 @@ static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx,
260
262
temp = i2c_8bit_addr_from_msg (msgs ) | (GEN_START << 8 );
261
263
writel (temp , lpi2c_imx -> base + LPI2C_MTDR );
262
264
263
- return lpi2c_imx_bus_busy (lpi2c_imx );
265
+ return lpi2c_imx_bus_busy (lpi2c_imx , atomic );
264
266
}
265
267
266
- static void lpi2c_imx_stop (struct lpi2c_imx_struct * lpi2c_imx )
268
+ static void lpi2c_imx_stop (struct lpi2c_imx_struct * lpi2c_imx , bool atomic )
267
269
{
268
270
unsigned int temp ;
269
271
int err ;
270
272
271
273
writel (GEN_STOP << 8 , lpi2c_imx -> base + LPI2C_MTDR );
272
274
273
- err = lpi2c_imx_read_msr_poll_timeout (temp , temp & MSR_SDF );
275
+ err = lpi2c_imx_read_msr_poll_timeout (atomic , temp , temp & MSR_SDF );
274
276
275
277
if (err ) {
276
278
dev_dbg (& lpi2c_imx -> adapter .dev , "stop timeout\n" );
@@ -390,12 +392,12 @@ static int lpi2c_imx_pio_msg_complete(struct lpi2c_imx_struct *lpi2c_imx)
390
392
return time_left ? 0 : - ETIMEDOUT ;
391
393
}
392
394
393
- static int lpi2c_imx_txfifo_empty (struct lpi2c_imx_struct * lpi2c_imx )
395
+ static int lpi2c_imx_txfifo_empty (struct lpi2c_imx_struct * lpi2c_imx , bool atomic )
394
396
{
395
397
unsigned int temp ;
396
398
int err ;
397
399
398
- err = lpi2c_imx_read_msr_poll_timeout (temp ,
400
+ err = lpi2c_imx_read_msr_poll_timeout (atomic , temp ,
399
401
(temp & MSR_NDF ) || !lpi2c_imx_txfifo_cnt (lpi2c_imx ));
400
402
401
403
if (temp & MSR_NDF ) {
@@ -432,7 +434,7 @@ static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct *lpi2c_imx)
432
434
writel (temp << 16 , lpi2c_imx -> base + LPI2C_MFCR );
433
435
}
434
436
435
- static void lpi2c_imx_write_txfifo (struct lpi2c_imx_struct * lpi2c_imx )
437
+ static bool lpi2c_imx_write_txfifo (struct lpi2c_imx_struct * lpi2c_imx , bool atomic )
436
438
{
437
439
unsigned int data , txcnt ;
438
440
@@ -447,13 +449,19 @@ static void lpi2c_imx_write_txfifo(struct lpi2c_imx_struct *lpi2c_imx)
447
449
txcnt ++ ;
448
450
}
449
451
450
- if (lpi2c_imx -> delivered < lpi2c_imx -> msglen )
451
- lpi2c_imx_intctrl (lpi2c_imx , MIER_TDIE | MIER_NDIE );
452
- else
452
+ if (lpi2c_imx -> delivered < lpi2c_imx -> msglen ) {
453
+ if (!atomic )
454
+ lpi2c_imx_intctrl (lpi2c_imx , MIER_TDIE | MIER_NDIE );
455
+ return false;
456
+ }
457
+
458
+ if (!atomic )
453
459
complete (& lpi2c_imx -> complete );
460
+
461
+ return true;
454
462
}
455
463
456
- static void lpi2c_imx_read_rxfifo (struct lpi2c_imx_struct * lpi2c_imx )
464
+ static bool lpi2c_imx_read_rxfifo (struct lpi2c_imx_struct * lpi2c_imx , bool atomic )
457
465
{
458
466
unsigned int blocklen , remaining ;
459
467
unsigned int temp , data ;
@@ -478,8 +486,9 @@ static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx)
478
486
remaining = lpi2c_imx -> msglen - lpi2c_imx -> delivered ;
479
487
480
488
if (!remaining ) {
481
- complete (& lpi2c_imx -> complete );
482
- return ;
489
+ if (!atomic )
490
+ complete (& lpi2c_imx -> complete );
491
+ return true;
483
492
}
484
493
485
494
/* not finished, still waiting for rx data */
@@ -497,19 +506,40 @@ static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx)
497
506
writel (temp , lpi2c_imx -> base + LPI2C_MTDR );
498
507
}
499
508
500
- lpi2c_imx_intctrl (lpi2c_imx , MIER_RDIE );
509
+ if (!atomic )
510
+ lpi2c_imx_intctrl (lpi2c_imx , MIER_RDIE );
511
+
512
+ return false;
501
513
}
502
514
503
515
static void lpi2c_imx_write (struct lpi2c_imx_struct * lpi2c_imx ,
504
516
struct i2c_msg * msgs )
505
517
{
506
518
lpi2c_imx -> tx_buf = msgs -> buf ;
507
519
lpi2c_imx_set_tx_watermark (lpi2c_imx );
508
- lpi2c_imx_write_txfifo (lpi2c_imx );
520
+ lpi2c_imx_write_txfifo (lpi2c_imx , false );
509
521
}
510
522
511
- static void lpi2c_imx_read (struct lpi2c_imx_struct * lpi2c_imx ,
512
- struct i2c_msg * msgs )
523
+ static int lpi2c_imx_write_atomic (struct lpi2c_imx_struct * lpi2c_imx ,
524
+ struct i2c_msg * msgs )
525
+ {
526
+ u32 temp ;
527
+ int err ;
528
+
529
+ lpi2c_imx -> tx_buf = msgs -> buf ;
530
+
531
+ err = lpi2c_imx_read_msr_poll_timeout (true, temp ,
532
+ (temp & MSR_NDF ) ||
533
+ lpi2c_imx_write_txfifo (lpi2c_imx , true));
534
+
535
+ if (temp & MSR_NDF )
536
+ return - EIO ;
537
+
538
+ return err ;
539
+ }
540
+
541
+ static void lpi2c_imx_read_init (struct lpi2c_imx_struct * lpi2c_imx ,
542
+ struct i2c_msg * msgs )
513
543
{
514
544
unsigned int temp ;
515
545
@@ -520,8 +550,43 @@ static void lpi2c_imx_read(struct lpi2c_imx_struct *lpi2c_imx,
520
550
temp = msgs -> len > CHUNK_DATA ? CHUNK_DATA - 1 : msgs -> len - 1 ;
521
551
temp |= (RECV_DATA << 8 );
522
552
writel (temp , lpi2c_imx -> base + LPI2C_MTDR );
553
+ }
523
554
524
- lpi2c_imx_intctrl (lpi2c_imx , MIER_RDIE | MIER_NDIE );
555
+ static bool lpi2c_imx_read_chunk_atomic (struct lpi2c_imx_struct * lpi2c_imx )
556
+ {
557
+ u32 rxcnt ;
558
+
559
+ rxcnt = (readl (lpi2c_imx -> base + LPI2C_MFSR ) >> 16 ) & 0xFF ;
560
+ if (!rxcnt )
561
+ return false;
562
+
563
+ if (!lpi2c_imx_read_rxfifo (lpi2c_imx , true))
564
+ return false;
565
+
566
+ return true;
567
+ }
568
+
569
+ static int lpi2c_imx_read_atomic (struct lpi2c_imx_struct * lpi2c_imx ,
570
+ struct i2c_msg * msgs )
571
+ {
572
+ u32 temp ;
573
+ int tmo_us ;
574
+
575
+ tmo_us = 1000000 ;
576
+ do {
577
+ if (lpi2c_imx_read_chunk_atomic (lpi2c_imx ))
578
+ return 0 ;
579
+
580
+ temp = readl (lpi2c_imx -> base + LPI2C_MSR );
581
+
582
+ if (temp & MSR_NDF )
583
+ return - EIO ;
584
+
585
+ udelay (100 );
586
+ tmo_us -= 100 ;
587
+ } while (tmo_us > 0 );
588
+
589
+ return - ETIMEDOUT ;
525
590
}
526
591
527
592
static bool is_use_dma (struct lpi2c_imx_struct * lpi2c_imx , struct i2c_msg * msg )
@@ -541,14 +606,27 @@ static int lpi2c_imx_pio_xfer(struct lpi2c_imx_struct *lpi2c_imx,
541
606
{
542
607
reinit_completion (& lpi2c_imx -> complete );
543
608
544
- if (msg -> flags & I2C_M_RD )
545
- lpi2c_imx_read (lpi2c_imx , msg );
546
- else
609
+ if (msg -> flags & I2C_M_RD ) {
610
+ lpi2c_imx_read_init (lpi2c_imx , msg );
611
+ lpi2c_imx_intctrl (lpi2c_imx , MIER_RDIE | MIER_NDIE );
612
+ } else {
547
613
lpi2c_imx_write (lpi2c_imx , msg );
614
+ }
548
615
549
616
return lpi2c_imx_pio_msg_complete (lpi2c_imx );
550
617
}
551
618
619
+ static int lpi2c_imx_pio_xfer_atomic (struct lpi2c_imx_struct * lpi2c_imx ,
620
+ struct i2c_msg * msg )
621
+ {
622
+ if (msg -> flags & I2C_M_RD ) {
623
+ lpi2c_imx_read_init (lpi2c_imx , msg );
624
+ return lpi2c_imx_read_atomic (lpi2c_imx , msg );
625
+ }
626
+
627
+ return lpi2c_imx_write_atomic (lpi2c_imx , msg );
628
+ }
629
+
552
630
static int lpi2c_imx_dma_timeout_calculate (struct lpi2c_imx_struct * lpi2c_imx )
553
631
{
554
632
unsigned long time = 0 ;
@@ -943,8 +1021,8 @@ static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,
943
1021
return ret ;
944
1022
}
945
1023
946
- static int lpi2c_imx_xfer (struct i2c_adapter * adapter ,
947
- struct i2c_msg * msgs , int num )
1024
+ static int lpi2c_imx_xfer_common (struct i2c_adapter * adapter ,
1025
+ struct i2c_msg * msgs , int num , bool atomic )
948
1026
{
949
1027
struct lpi2c_imx_struct * lpi2c_imx = i2c_get_adapdata (adapter );
950
1028
unsigned int temp ;
@@ -955,7 +1033,7 @@ static int lpi2c_imx_xfer(struct i2c_adapter *adapter,
955
1033
return result ;
956
1034
957
1035
for (i = 0 ; i < num ; i ++ ) {
958
- result = lpi2c_imx_start (lpi2c_imx , & msgs [i ]);
1036
+ result = lpi2c_imx_start (lpi2c_imx , & msgs [i ], atomic );
959
1037
if (result )
960
1038
goto disable ;
961
1039
@@ -967,28 +1045,33 @@ static int lpi2c_imx_xfer(struct i2c_adapter *adapter,
967
1045
lpi2c_imx -> tx_buf = NULL ;
968
1046
lpi2c_imx -> delivered = 0 ;
969
1047
lpi2c_imx -> msglen = msgs [i ].len ;
970
- init_completion (& lpi2c_imx -> complete );
971
1048
972
- if (is_use_dma (lpi2c_imx , & msgs [i ])) {
973
- result = lpi2c_imx_dma_xfer (lpi2c_imx , & msgs [i ]);
974
- if (result && lpi2c_imx -> dma -> using_pio_mode )
975
- result = lpi2c_imx_pio_xfer (lpi2c_imx , & msgs [i ]);
1049
+ if (atomic ) {
1050
+ result = lpi2c_imx_pio_xfer_atomic (lpi2c_imx , & msgs [i ]);
976
1051
} else {
977
- result = lpi2c_imx_pio_xfer (lpi2c_imx , & msgs [i ]);
1052
+ init_completion (& lpi2c_imx -> complete );
1053
+
1054
+ if (is_use_dma (lpi2c_imx , & msgs [i ])) {
1055
+ result = lpi2c_imx_dma_xfer (lpi2c_imx , & msgs [i ]);
1056
+ if (result && lpi2c_imx -> dma -> using_pio_mode )
1057
+ result = lpi2c_imx_pio_xfer (lpi2c_imx , & msgs [i ]);
1058
+ } else {
1059
+ result = lpi2c_imx_pio_xfer (lpi2c_imx , & msgs [i ]);
1060
+ }
978
1061
}
979
1062
980
1063
if (result )
981
1064
goto stop ;
982
1065
983
1066
if (!(msgs [i ].flags & I2C_M_RD )) {
984
- result = lpi2c_imx_txfifo_empty (lpi2c_imx );
1067
+ result = lpi2c_imx_txfifo_empty (lpi2c_imx , atomic );
985
1068
if (result )
986
1069
goto stop ;
987
1070
}
988
1071
}
989
1072
990
1073
stop :
991
- lpi2c_imx_stop (lpi2c_imx );
1074
+ lpi2c_imx_stop (lpi2c_imx , atomic );
992
1075
993
1076
temp = readl (lpi2c_imx -> base + LPI2C_MSR );
994
1077
if ((temp & MSR_NDF ) && !result )
@@ -1004,6 +1087,16 @@ static int lpi2c_imx_xfer(struct i2c_adapter *adapter,
1004
1087
return (result < 0 ) ? result : num ;
1005
1088
}
1006
1089
1090
+ static int lpi2c_imx_xfer (struct i2c_adapter * adapter , struct i2c_msg * msgs , int num )
1091
+ {
1092
+ return lpi2c_imx_xfer_common (adapter , msgs , num , false);
1093
+ }
1094
+
1095
+ static int lpi2c_imx_xfer_atomic (struct i2c_adapter * adapter , struct i2c_msg * msgs , int num )
1096
+ {
1097
+ return lpi2c_imx_xfer_common (adapter , msgs , num , true);
1098
+ }
1099
+
1007
1100
static irqreturn_t lpi2c_imx_target_isr (struct lpi2c_imx_struct * lpi2c_imx ,
1008
1101
u32 ssr , u32 sier_filter )
1009
1102
{
@@ -1066,9 +1159,9 @@ static irqreturn_t lpi2c_imx_master_isr(struct lpi2c_imx_struct *lpi2c_imx)
1066
1159
if (temp & MSR_NDF )
1067
1160
complete (& lpi2c_imx -> complete );
1068
1161
else if (temp & MSR_RDF )
1069
- lpi2c_imx_read_rxfifo (lpi2c_imx );
1162
+ lpi2c_imx_read_rxfifo (lpi2c_imx , false );
1070
1163
else if (temp & MSR_TDF )
1071
- lpi2c_imx_write_txfifo (lpi2c_imx );
1164
+ lpi2c_imx_write_txfifo (lpi2c_imx , false );
1072
1165
1073
1166
return IRQ_HANDLED ;
1074
1167
}
@@ -1265,6 +1358,7 @@ static u32 lpi2c_imx_func(struct i2c_adapter *adapter)
1265
1358
1266
1359
static const struct i2c_algorithm lpi2c_imx_algo = {
1267
1360
.xfer = lpi2c_imx_xfer ,
1361
+ .xfer_atomic = lpi2c_imx_xfer_atomic ,
1268
1362
.functionality = lpi2c_imx_func ,
1269
1363
.reg_target = lpi2c_imx_register_target ,
1270
1364
.unreg_target = lpi2c_imx_unregister_target ,
0 commit comments