17
17
*/
18
18
19
19
#include <linux/acpi.h>
20
+ #include <linux/bug.h>
20
21
#include <linux/completion.h>
21
22
#include <linux/i2c.h>
22
23
#include <linux/interrupt.h>
35
36
#define TPM_CR50_I2C_MAX_RETRIES 3 /* Max retries due to I2C errors */
36
37
#define TPM_CR50_I2C_RETRY_DELAY_LO 55 /* Min usecs between retries on I2C */
37
38
#define TPM_CR50_I2C_RETRY_DELAY_HI 65 /* Max usecs between retries on I2C */
39
+ #define TPM_CR50_I2C_DEFAULT_LOC 0
38
40
39
41
#define TPM_I2C_ACCESS (l ) (0x0000 | ((l) << 4))
40
42
#define TPM_I2C_STS (l ) (0x0001 | ((l) << 4))
@@ -285,74 +287,84 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
285
287
}
286
288
287
289
/**
288
- * tpm_cr50_check_locality() - Verify TPM locality 0 is active.
290
+ * tpm_cr50_check_locality() - Verify if required TPM locality is active.
289
291
* @chip: A TPM chip.
292
+ * @loc: Locality to be verified
290
293
*
291
294
* Return:
292
- * - 0: Success.
295
+ * - loc: Success.
293
296
* - -errno: A POSIX error code.
294
297
*/
295
- static int tpm_cr50_check_locality (struct tpm_chip * chip )
298
+ static int tpm_cr50_check_locality (struct tpm_chip * chip , int loc )
296
299
{
297
300
u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY ;
298
301
u8 buf ;
299
302
int rc ;
300
303
301
- rc = tpm_cr50_i2c_read (chip , TPM_I2C_ACCESS (0 ), & buf , sizeof (buf ));
304
+ rc = tpm_cr50_i2c_read (chip , TPM_I2C_ACCESS (loc ), & buf , sizeof (buf ));
302
305
if (rc < 0 )
303
306
return rc ;
304
307
305
308
if ((buf & mask ) == mask )
306
- return 0 ;
309
+ return loc ;
307
310
308
311
return - EIO ;
309
312
}
310
313
311
314
/**
312
315
* tpm_cr50_release_locality() - Release TPM locality.
313
316
* @chip: A TPM chip.
314
- * @force: Flag to force release if set.
317
+ * @loc: Locality to be released
318
+ *
319
+ * Return:
320
+ * - 0: Success.
321
+ * - -errno: A POSIX error code.
315
322
*/
316
- static void tpm_cr50_release_locality (struct tpm_chip * chip , bool force )
323
+ static int tpm_cr50_release_locality (struct tpm_chip * chip , int loc )
317
324
{
318
325
u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING ;
319
- u8 addr = TPM_I2C_ACCESS (0 );
326
+ u8 addr = TPM_I2C_ACCESS (loc );
320
327
u8 buf ;
328
+ int rc ;
321
329
322
- if (tpm_cr50_i2c_read (chip , addr , & buf , sizeof (buf )) < 0 )
323
- return ;
330
+ rc = tpm_cr50_i2c_read (chip , addr , & buf , sizeof (buf ));
331
+ if (rc < 0 )
332
+ return rc ;
324
333
325
- if (force || (buf & mask ) == mask ) {
334
+ if ((buf & mask ) == mask ) {
326
335
buf = TPM_ACCESS_ACTIVE_LOCALITY ;
327
- tpm_cr50_i2c_write (chip , addr , & buf , sizeof (buf ));
336
+ rc = tpm_cr50_i2c_write (chip , addr , & buf , sizeof (buf ));
328
337
}
338
+
339
+ return rc ;
329
340
}
330
341
331
342
/**
332
- * tpm_cr50_request_locality() - Request TPM locality 0 .
343
+ * tpm_cr50_request_locality() - Request TPM locality.
333
344
* @chip: A TPM chip.
345
+ * @loc: Locality to be requested.
334
346
*
335
347
* Return:
336
- * - 0: Success.
348
+ * - loc: Success.
337
349
* - -errno: A POSIX error code.
338
350
*/
339
- static int tpm_cr50_request_locality (struct tpm_chip * chip )
351
+ static int tpm_cr50_request_locality (struct tpm_chip * chip , int loc )
340
352
{
341
353
u8 buf = TPM_ACCESS_REQUEST_USE ;
342
354
unsigned long stop ;
343
355
int rc ;
344
356
345
- if (! tpm_cr50_check_locality (chip ) )
346
- return 0 ;
357
+ if (tpm_cr50_check_locality (chip , loc ) == loc )
358
+ return loc ;
347
359
348
- rc = tpm_cr50_i2c_write (chip , TPM_I2C_ACCESS (0 ), & buf , sizeof (buf ));
360
+ rc = tpm_cr50_i2c_write (chip , TPM_I2C_ACCESS (loc ), & buf , sizeof (buf ));
349
361
if (rc < 0 )
350
362
return rc ;
351
363
352
364
stop = jiffies + chip -> timeout_a ;
353
365
do {
354
- if (! tpm_cr50_check_locality (chip ) )
355
- return 0 ;
366
+ if (tpm_cr50_check_locality (chip , loc ) == loc )
367
+ return loc ;
356
368
357
369
msleep (TPM_CR50_TIMEOUT_SHORT_MS );
358
370
} while (time_before (jiffies , stop ));
@@ -373,7 +385,7 @@ static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
373
385
{
374
386
u8 buf [4 ];
375
387
376
- if (tpm_cr50_i2c_read (chip , TPM_I2C_STS (0 ), buf , sizeof (buf )) < 0 )
388
+ if (tpm_cr50_i2c_read (chip , TPM_I2C_STS (chip -> locality ), buf , sizeof (buf )) < 0 )
377
389
return 0 ;
378
390
379
391
return buf [0 ];
@@ -389,7 +401,7 @@ static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
389
401
{
390
402
u8 buf [4 ] = { TPM_STS_COMMAND_READY };
391
403
392
- tpm_cr50_i2c_write (chip , TPM_I2C_STS (0 ), buf , sizeof (buf ));
404
+ tpm_cr50_i2c_write (chip , TPM_I2C_STS (chip -> locality ), buf , sizeof (buf ));
393
405
msleep (TPM_CR50_TIMEOUT_SHORT_MS );
394
406
}
395
407
@@ -419,7 +431,7 @@ static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
419
431
stop = jiffies + chip -> timeout_b ;
420
432
421
433
do {
422
- if (tpm_cr50_i2c_read (chip , TPM_I2C_STS (0 ), buf , sizeof (buf )) < 0 ) {
434
+ if (tpm_cr50_i2c_read (chip , TPM_I2C_STS (chip -> locality ), buf , sizeof (buf )) < 0 ) {
423
435
msleep (TPM_CR50_TIMEOUT_SHORT_MS );
424
436
continue ;
425
437
}
@@ -453,7 +465,7 @@ static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
453
465
454
466
u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL ;
455
467
size_t burstcnt , cur , len , expected ;
456
- u8 addr = TPM_I2C_DATA_FIFO (0 );
468
+ u8 addr = TPM_I2C_DATA_FIFO (chip -> locality );
457
469
u32 status ;
458
470
int rc ;
459
471
@@ -515,15 +527,13 @@ static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
515
527
goto out_err ;
516
528
}
517
529
518
- tpm_cr50_release_locality (chip , false);
519
530
return cur ;
520
531
521
532
out_err :
522
533
/* Abort current transaction if still pending */
523
534
if (tpm_cr50_i2c_tis_status (chip ) & TPM_STS_COMMAND_READY )
524
535
tpm_cr50_i2c_tis_set_ready (chip );
525
536
526
- tpm_cr50_release_locality (chip , false);
527
537
return rc ;
528
538
}
529
539
@@ -545,10 +555,6 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
545
555
u32 status ;
546
556
int rc ;
547
557
548
- rc = tpm_cr50_request_locality (chip );
549
- if (rc < 0 )
550
- return rc ;
551
-
552
558
/* Wait until TPM is ready for a command */
553
559
stop = jiffies + chip -> timeout_b ;
554
560
while (!(tpm_cr50_i2c_tis_status (chip ) & TPM_STS_COMMAND_READY )) {
@@ -577,7 +583,8 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
577
583
* that is inserted by tpm_cr50_i2c_write()
578
584
*/
579
585
limit = min_t (size_t , burstcnt - 1 , len );
580
- rc = tpm_cr50_i2c_write (chip , TPM_I2C_DATA_FIFO (0 ), & buf [sent ], limit );
586
+ rc = tpm_cr50_i2c_write (chip , TPM_I2C_DATA_FIFO (chip -> locality ),
587
+ & buf [sent ], limit );
581
588
if (rc < 0 ) {
582
589
dev_err (& chip -> dev , "Write failed\n" );
583
590
goto out_err ;
@@ -598,7 +605,7 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
598
605
}
599
606
600
607
/* Start the TPM command */
601
- rc = tpm_cr50_i2c_write (chip , TPM_I2C_STS (0 ), tpm_go ,
608
+ rc = tpm_cr50_i2c_write (chip , TPM_I2C_STS (chip -> locality ), tpm_go ,
602
609
sizeof (tpm_go ));
603
610
if (rc < 0 ) {
604
611
dev_err (& chip -> dev , "Start command failed\n" );
@@ -611,7 +618,6 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
611
618
if (tpm_cr50_i2c_tis_status (chip ) & TPM_STS_COMMAND_READY )
612
619
tpm_cr50_i2c_tis_set_ready (chip );
613
620
614
- tpm_cr50_release_locality (chip , false);
615
621
return rc ;
616
622
}
617
623
@@ -650,6 +656,8 @@ static const struct tpm_class_ops cr50_i2c = {
650
656
.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID ,
651
657
.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID ,
652
658
.req_canceled = & tpm_cr50_i2c_req_canceled ,
659
+ .request_locality = & tpm_cr50_request_locality ,
660
+ .relinquish_locality = & tpm_cr50_release_locality ,
653
661
};
654
662
655
663
#ifdef CONFIG_ACPI
@@ -684,6 +692,7 @@ static int tpm_cr50_i2c_probe(struct i2c_client *client)
684
692
u32 vendor ;
685
693
u8 buf [4 ];
686
694
int rc ;
695
+ int loc ;
687
696
688
697
if (!i2c_check_functionality (client -> adapter , I2C_FUNC_I2C ))
689
698
return - ENODEV ;
@@ -726,24 +735,30 @@ static int tpm_cr50_i2c_probe(struct i2c_client *client)
726
735
TPM_CR50_TIMEOUT_NOIRQ_MS );
727
736
}
728
737
729
- rc = tpm_cr50_request_locality (chip );
730
- if (rc < 0 ) {
738
+ loc = tpm_cr50_request_locality (chip , TPM_CR50_I2C_DEFAULT_LOC );
739
+ if (loc < 0 ) {
731
740
dev_err (dev , "Could not request locality\n" );
732
- return rc ;
741
+ return loc ;
733
742
}
734
743
735
744
/* Read four bytes from DID_VID register */
736
- rc = tpm_cr50_i2c_read (chip , TPM_I2C_DID_VID (0 ), buf , sizeof (buf ));
745
+ rc = tpm_cr50_i2c_read (chip , TPM_I2C_DID_VID (loc ), buf , sizeof (buf ));
737
746
if (rc < 0 ) {
738
747
dev_err (dev , "Could not read vendor id\n" );
739
- tpm_cr50_release_locality (chip , true);
748
+ if (tpm_cr50_release_locality (chip , loc ))
749
+ dev_err (dev , "Could not release locality\n" );
750
+ return rc ;
751
+ }
752
+
753
+ rc = tpm_cr50_release_locality (chip , loc );
754
+ if (rc ) {
755
+ dev_err (dev , "Could not release locality\n" );
740
756
return rc ;
741
757
}
742
758
743
759
vendor = le32_to_cpup ((__le32 * )buf );
744
760
if (vendor != TPM_CR50_I2C_DID_VID && vendor != TPM_TI50_I2C_DID_VID ) {
745
761
dev_err (dev , "Vendor ID did not match! ID was %08x\n" , vendor );
746
- tpm_cr50_release_locality (chip , true);
747
762
return - ENODEV ;
748
763
}
749
764
@@ -772,7 +787,6 @@ static void tpm_cr50_i2c_remove(struct i2c_client *client)
772
787
}
773
788
774
789
tpm_chip_unregister (chip );
775
- tpm_cr50_release_locality (chip , true);
776
790
}
777
791
778
792
static SIMPLE_DEV_PM_OPS (cr50_i2c_pm , tpm_pm_suspend , tpm_pm_resume ) ;
0 commit comments