2005-04-17 02:20:36 +04:00
/*
* Copyright ( C ) 2004 IBM Corporation
*
* Authors :
* Leendert van Doorn < leendert @ watson . ibm . com >
* Dave Safford < safford @ watson . ibm . com >
* Reiner Sailer < sailer @ watson . ibm . com >
* Kylene Hall < kjhall @ us . ibm . com >
*
* Maintained by : < tpmdd_devel @ lists . sourceforge . net >
*
* Device driver for TCG / TCPA TPM ( trusted platform module ) .
* Specifications at www . trustedcomputinggroup . org
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation , version 2 of the
* License .
*
*/
# include "tpm.h"
2005-11-14 03:07:41 +03:00
# include "tpm_atmel.h"
2005-04-17 02:20:36 +04:00
/* write status bits */
2005-06-24 09:01:48 +04:00
enum tpm_atmel_write_status {
ATML_STATUS_ABORT = 0x01 ,
ATML_STATUS_LASTBYTE = 0x04
} ;
2005-04-17 02:20:36 +04:00
/* read status bits */
2005-06-24 09:01:48 +04:00
enum tpm_atmel_read_status {
ATML_STATUS_BUSY = 0x01 ,
ATML_STATUS_DATA_AVAIL = 0x02 ,
ATML_STATUS_REWRITE = 0x04 ,
ATML_STATUS_READY = 0x08
} ;
2005-04-17 02:20:36 +04:00
2005-10-31 02:03:28 +03:00
static int tpm_atml_recv ( struct tpm_chip * chip , u8 * buf , size_t count )
2005-04-17 02:20:36 +04:00
{
u8 status , * hdr = buf ;
u32 size ;
int i ;
__be32 * native_size ;
/* start reading header */
if ( count < 6 )
return - EIO ;
for ( i = 0 ; i < 6 ; i + + ) {
2006-04-22 13:37:15 +04:00
status = ioread8 ( chip - > vendor . iobase + 1 ) ;
2005-04-17 02:20:36 +04:00
if ( ( status & ATML_STATUS_DATA_AVAIL ) = = 0 ) {
2005-11-18 12:10:58 +03:00
dev_err ( chip - > dev , " error reading header \n " ) ;
2005-04-17 02:20:36 +04:00
return - EIO ;
}
2006-04-22 13:37:15 +04:00
* buf + + = ioread8 ( chip - > vendor . iobase ) ;
2005-04-17 02:20:36 +04:00
}
/* size of the data received */
native_size = ( __force __be32 * ) ( hdr + 2 ) ;
size = be32_to_cpu ( * native_size ) ;
if ( count < size ) {
2005-10-31 02:03:24 +03:00
dev_err ( chip - > dev ,
2005-04-17 02:20:36 +04:00
" Recv size(%d) less than available space \n " , size ) ;
for ( ; i < size ; i + + ) { /* clear the waiting data anyway */
2006-04-22 13:37:15 +04:00
status = ioread8 ( chip - > vendor . iobase + 1 ) ;
2005-04-17 02:20:36 +04:00
if ( ( status & ATML_STATUS_DATA_AVAIL ) = = 0 ) {
2005-11-18 12:10:58 +03:00
dev_err ( chip - > dev , " error reading data \n " ) ;
2005-04-17 02:20:36 +04:00
return - EIO ;
}
}
return - EIO ;
}
/* read all the data available */
for ( ; i < size ; i + + ) {
2006-04-22 13:37:15 +04:00
status = ioread8 ( chip - > vendor . iobase + 1 ) ;
2005-04-17 02:20:36 +04:00
if ( ( status & ATML_STATUS_DATA_AVAIL ) = = 0 ) {
2005-11-18 12:10:58 +03:00
dev_err ( chip - > dev , " error reading data \n " ) ;
2005-04-17 02:20:36 +04:00
return - EIO ;
}
2006-04-22 13:37:15 +04:00
* buf + + = ioread8 ( chip - > vendor . iobase ) ;
2005-04-17 02:20:36 +04:00
}
/* make sure data available is gone */
2006-04-22 13:37:15 +04:00
status = ioread8 ( chip - > vendor . iobase + 1 ) ;
2005-11-18 12:10:58 +03:00
2005-04-17 02:20:36 +04:00
if ( status & ATML_STATUS_DATA_AVAIL ) {
2005-10-31 02:03:24 +03:00
dev_err ( chip - > dev , " data available is stuck \n " ) ;
2005-04-17 02:20:36 +04:00
return - EIO ;
}
return size ;
}
2005-10-31 02:03:28 +03:00
static int tpm_atml_send ( struct tpm_chip * chip , u8 * buf , size_t count )
2005-04-17 02:20:36 +04:00
{
int i ;
2005-10-31 02:03:24 +03:00
dev_dbg ( chip - > dev , " tpm_atml_send: \n " ) ;
2005-04-17 02:20:36 +04:00
for ( i = 0 ; i < count ; i + + ) {
2005-10-31 02:03:24 +03:00
dev_dbg ( chip - > dev , " %d 0x%x(%d) \n " , i , buf [ i ] , buf [ i ] ) ;
2006-04-22 13:37:15 +04:00
iowrite8 ( buf [ i ] , chip - > vendor . iobase ) ;
2005-04-17 02:20:36 +04:00
}
return count ;
}
static void tpm_atml_cancel ( struct tpm_chip * chip )
{
2006-04-22 13:37:15 +04:00
iowrite8 ( ATML_STATUS_ABORT , chip - > vendor . iobase + 1 ) ;
2005-04-17 02:20:36 +04:00
}
2005-10-31 02:03:23 +03:00
static u8 tpm_atml_status ( struct tpm_chip * chip )
{
2006-04-22 13:37:15 +04:00
return ioread8 ( chip - > vendor . iobase + 1 ) ;
2005-10-31 02:03:23 +03:00
}
2006-07-03 11:24:21 +04:00
static const struct file_operations atmel_ops = {
2005-04-17 02:20:36 +04:00
. owner = THIS_MODULE ,
. llseek = no_llseek ,
. open = tpm_open ,
. read = tpm_read ,
. write = tpm_write ,
. release = tpm_release ,
} ;
2005-06-24 09:02:00 +04:00
static DEVICE_ATTR ( pubek , S_IRUGO , tpm_show_pubek , NULL ) ;
static DEVICE_ATTR ( pcrs , S_IRUGO , tpm_show_pcrs , NULL ) ;
static DEVICE_ATTR ( caps , S_IRUGO , tpm_show_caps , NULL ) ;
static DEVICE_ATTR ( cancel , S_IWUSR | S_IWGRP , NULL , tpm_store_cancel ) ;
static struct attribute * atmel_attrs [ ] = {
& dev_attr_pubek . attr ,
& dev_attr_pcrs . attr ,
& dev_attr_caps . attr ,
& dev_attr_cancel . attr ,
2005-10-31 02:03:29 +03:00
NULL ,
2005-06-24 09:02:00 +04:00
} ;
static struct attribute_group atmel_attr_grp = { . attrs = atmel_attrs } ;
2006-04-22 13:37:26 +04:00
static const struct tpm_vendor_specific tpm_atmel = {
2005-04-17 02:20:36 +04:00
. recv = tpm_atml_recv ,
. send = tpm_atml_send ,
. cancel = tpm_atml_cancel ,
2005-10-31 02:03:23 +03:00
. status = tpm_atml_status ,
2005-04-17 02:20:36 +04:00
. req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL ,
. req_complete_val = ATML_STATUS_DATA_AVAIL ,
2005-06-24 09:02:02 +04:00
. req_canceled = ATML_STATUS_READY ,
2005-06-24 09:02:00 +04:00
. attr_group = & atmel_attr_grp ,
2005-04-17 02:20:36 +04:00
. miscdev = { . fops = & atmel_ops , } ,
} ;
2005-10-31 02:03:28 +03:00
static struct platform_device * pdev ;
2005-10-31 02:03:25 +03:00
2005-11-14 03:07:41 +03:00
static void atml_plat_remove ( void )
2005-10-31 02:03:25 +03:00
{
2005-11-14 03:07:41 +03:00
struct tpm_chip * chip = dev_get_drvdata ( & pdev - > dev ) ;
2005-10-31 02:03:28 +03:00
if ( chip ) {
2006-04-22 13:37:15 +04:00
if ( chip - > vendor . have_region )
atmel_release_region ( chip - > vendor . base ,
chip - > vendor . region_size ) ;
atmel_put_base_addr ( chip - > vendor . iobase ) ;
2005-10-31 02:03:25 +03:00
tpm_remove_hardware ( chip - > dev ) ;
2005-11-14 03:07:41 +03:00
platform_device_unregister ( pdev ) ;
2005-10-31 02:03:25 +03:00
}
}
static struct device_driver atml_drv = {
. name = " tpm_atmel " ,
. bus = & platform_bus_type ,
. owner = THIS_MODULE ,
. suspend = tpm_pm_suspend ,
. resume = tpm_pm_resume ,
} ;
static int __init init_atmel ( void )
2005-04-17 02:20:36 +04:00
{
int rc = 0 ;
2006-04-22 13:37:26 +04:00
void __iomem * iobase = NULL ;
int have_region , region_size ;
unsigned long base ;
struct tpm_chip * chip ;
2005-04-17 02:20:36 +04:00
2006-10-11 12:21:51 +04:00
rc = driver_register ( & atml_drv ) ;
if ( rc )
return rc ;
2005-04-17 02:20:36 +04:00
2006-04-22 13:37:26 +04:00
if ( ( iobase = atmel_get_base_addr ( & base , & region_size ) ) = = NULL ) {
2005-11-14 03:07:41 +03:00
rc = - ENODEV ;
goto err_unreg_drv ;
2005-10-31 02:03:25 +03:00
}
2005-04-17 02:20:36 +04:00
2006-04-22 13:37:26 +04:00
have_region =
2005-11-18 12:10:58 +03:00
( atmel_request_region
2006-04-22 13:37:26 +04:00
( tpm_atmel . base , region_size , " tpm_atmel0 " ) = = NULL ) ? 0 : 1 ;
2006-10-11 12:21:51 +04:00
pdev = platform_device_register_simple ( " tpm_atmel " , - 1 , NULL , 0 ) ;
if ( IS_ERR ( pdev ) ) {
2005-11-14 03:07:41 +03:00
rc = PTR_ERR ( pdev ) ;
goto err_rel_reg ;
2005-10-31 02:03:25 +03:00
}
2005-04-17 02:20:36 +04:00
2006-04-22 13:37:26 +04:00
if ( ! ( chip = tpm_register_hardware ( & pdev - > dev , & tpm_atmel ) ) ) {
rc = - ENODEV ;
2005-11-14 03:07:41 +03:00
goto err_unreg_dev ;
2006-04-22 13:37:26 +04:00
}
chip - > vendor . iobase = iobase ;
chip - > vendor . base = base ;
chip - > vendor . have_region = have_region ;
chip - > vendor . region_size = region_size ;
2005-10-31 02:03:25 +03:00
return 0 ;
2005-11-14 03:07:41 +03:00
err_unreg_dev :
platform_device_unregister ( pdev ) ;
err_rel_reg :
2006-04-22 13:37:26 +04:00
atmel_put_base_addr ( iobase ) ;
if ( have_region )
atmel_release_region ( base ,
region_size ) ;
2005-11-14 03:07:41 +03:00
err_unreg_drv :
driver_unregister ( & atml_drv ) ;
return rc ;
2005-04-17 02:20:36 +04:00
}
static void __exit cleanup_atmel ( void )
{
2005-10-31 02:03:25 +03:00
driver_unregister ( & atml_drv ) ;
2005-11-14 03:07:41 +03:00
atml_plat_remove ( ) ;
2005-04-17 02:20:36 +04:00
}
module_init ( init_atmel ) ;
module_exit ( cleanup_atmel ) ;
MODULE_AUTHOR ( " Leendert van Doorn (leendert@watson.ibm.com) " ) ;
MODULE_DESCRIPTION ( " TPM Driver " ) ;
MODULE_VERSION ( " 2.0 " ) ;
MODULE_LICENSE ( " GPL " ) ;