1 /* Copyright 2013-2016 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <skiboot.h>
18 #include <opal-api.h>
19 #include <i2c.h>
20 
21 #include "tpm_i2c_interface.h"
22 #include "../status_codes.h"
23 
24 #define I2C_BYTE_TIMEOUT_MS		30  /* 30ms/byte timeout */
25 
26 /**
27  * tpm_i2c_request_send - send request to i2c bus
28  * @tpm_bus_id: i2c bus id
29  * @tpm_dev_addr: address of the tpm device
30  * @read_write: SMBUS_READ or SMBUS_WRITE
31  * @offset: any of the I2C interface offset defined
32  * @offset_bytes: offset size in bytes
33  * @buf: data to be read or written
34  * @buflen: buf length
35  *
36  * This interacts with skiboot i2c API to send an I2C request to the tpm
37  * device
38  *
39  * Returns: Zero on success otherwise a negative error code
40  */
tpm_i2c_request_send(int bus_id,int dev_addr,int read_write,uint32_t offset,uint32_t offset_bytes,void * buf,size_t buflen)41 int tpm_i2c_request_send(int bus_id, int dev_addr, int read_write,
42 			 uint32_t offset, uint32_t offset_bytes, void* buf,
43 			 size_t buflen)
44 {
45 	int rc, timeout;
46 
47 	/*
48 	 * Set the request timeout to 30ms per byte. Otherwise, we get
49 	 * an I2C master timeout for all requests sent to the device
50 	 * since the I2C master's timeout is too short (1ms per byte).
51 	 */
52 	timeout = (buflen + offset_bytes + 2) * I2C_BYTE_TIMEOUT_MS;
53 
54 	rc = i2c_request_send(bus_id, dev_addr, read_write, offset,
55 			      offset_bytes, buf, buflen, timeout);
56 	if (rc == OPAL_PARAMETER)
57 		return STB_ARG_ERROR;
58 	else if (rc < 0)
59 		return STB_DRIVER_ERROR;
60 	return 0;
61 }
62