diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2015-04-15 14:08:27 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-04-15 14:08:27 -0400 |
commit | d488d3a4ce08e96dad5cb3b6117517d57ccec98f (patch) | |
tree | 169b09c589e38f6d5f2ea0a9e25c6a9fb3ebf783 /drivers/char | |
parent | cb906953d2c3fd450655d9fa833f03690ad50c23 (diff) | |
parent | 5deeb5cece3f9b30c8129786726b9d02c412c8ca (diff) |
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem updates from James Morris:
"Highlights for this window:
- improved AVC hashing for SELinux by John Brooks and Stephen Smalley
- addition of an unconfined label to Smack
- Smack documentation update
- TPM driver updates"
* 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (28 commits)
lsm: copy comm before calling audit_log to avoid race in string printing
tomoyo: Do not generate empty policy files
tomoyo: Use if_changed when generating builtin-policy.h
tomoyo: Use bin2c to generate builtin-policy.h
selinux: increase avtab max buckets
selinux: Use a better hash function for avtab
selinux: convert avtab hash table to flex_array
selinux: reconcile security_netlbl_secattr_to_sid() and mls_import_netlbl_cat()
selinux: remove unnecessary pointer reassignment
Smack: Updates for Smack documentation
tpm/st33zp24/spi: Add missing device table for spi phy.
tpm/st33zp24: Add proper wait for ordinal duration in case of irq mode
smack: Fix gcc warning from unused smack_syslog_lock mutex in smackfs.c
Smack: Allow an unconfined label in bringup mode
Smack: getting the Smack security context of keys
Smack: Assign smack_known_web as default smk_in label for kernel thread's socket
tpm/tpm_infineon: Use struct dev_pm_ops for power management
MAINTAINERS: Add Jason as designated reviewer for TPM
tpm: Update KConfig text to include TPM2.0 FIFO chips
tpm/st33zp24/dts/st33zp24-spi: Add dts documentation for st33zp24 spi phy
...
Diffstat (limited to 'drivers/char')
-rw-r--r-- | drivers/char/tpm/Kconfig | 20 | ||||
-rw-r--r-- | drivers/char/tpm/Makefile | 2 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/Kconfig | 30 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/Makefile | 12 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/i2c.c | 276 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/spi.c | 399 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/st33zp24.c | 698 | ||||
-rw-r--r-- | drivers/char/tpm/st33zp24/st33zp24.h | 37 | ||||
-rw-r--r-- | drivers/char/tpm/tpm-chip.c | 66 | ||||
-rw-r--r-- | drivers/char/tpm/tpm_i2c_stm_st33.c | 911 | ||||
-rw-r--r-- | drivers/char/tpm/tpm_infineon.c | 34 |
11 files changed, 1510 insertions, 975 deletions
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index 9d4e37549eb2..3b84a8b1bfbe 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig | |||
@@ -25,13 +25,14 @@ menuconfig TCG_TPM | |||
25 | if TCG_TPM | 25 | if TCG_TPM |
26 | 26 | ||
27 | config TCG_TIS | 27 | config TCG_TIS |
28 | tristate "TPM Interface Specification 1.2 Interface" | 28 | tristate "TPM Interface Specification 1.2 Interface / TPM 2.0 FIFO Interface" |
29 | depends on X86 | 29 | depends on X86 |
30 | ---help--- | 30 | ---help--- |
31 | If you have a TPM security chip that is compliant with the | 31 | If you have a TPM security chip that is compliant with the |
32 | TCG TIS 1.2 TPM specification say Yes and it will be accessible | 32 | TCG TIS 1.2 TPM specification (TPM1.2) or the TCG PTP FIFO |
33 | from within Linux. To compile this driver as a module, choose | 33 | specification (TPM2.0) say Yes and it will be accessible from |
34 | M here; the module will be called tpm_tis. | 34 | within Linux. To compile this driver as a module, choose M here; |
35 | the module will be called tpm_tis. | ||
35 | 36 | ||
36 | config TCG_TIS_I2C_ATMEL | 37 | config TCG_TIS_I2C_ATMEL |
37 | tristate "TPM Interface Specification 1.2 Interface (I2C - Atmel)" | 38 | tristate "TPM Interface Specification 1.2 Interface (I2C - Atmel)" |
@@ -100,16 +101,6 @@ config TCG_IBMVTPM | |||
100 | will be accessible from within Linux. To compile this driver | 101 | will be accessible from within Linux. To compile this driver |
101 | as a module, choose M here; the module will be called tpm_ibmvtpm. | 102 | as a module, choose M here; the module will be called tpm_ibmvtpm. |
102 | 103 | ||
103 | config TCG_TIS_I2C_ST33 | ||
104 | tristate "TPM Interface Specification 1.2 Interface (I2C - STMicroelectronics)" | ||
105 | depends on I2C | ||
106 | depends on GPIOLIB | ||
107 | ---help--- | ||
108 | If you have a TPM security chip from STMicroelectronics working with | ||
109 | an I2C bus say Yes and it will be accessible from within Linux. | ||
110 | To compile this driver as a module, choose M here; the module will be | ||
111 | called tpm_i2c_stm_st33. | ||
112 | |||
113 | config TCG_XEN | 104 | config TCG_XEN |
114 | tristate "XEN TPM Interface" | 105 | tristate "XEN TPM Interface" |
115 | depends on TCG_TPM && XEN | 106 | depends on TCG_TPM && XEN |
@@ -131,4 +122,5 @@ config TCG_CRB | |||
131 | from within Linux. To compile this driver as a module, choose | 122 | from within Linux. To compile this driver as a module, choose |
132 | M here; the module will be called tpm_crb. | 123 | M here; the module will be called tpm_crb. |
133 | 124 | ||
125 | source "drivers/char/tpm/st33zp24/Kconfig" | ||
134 | endif # TCG_TPM | 126 | endif # TCG_TPM |
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile index 990cf183931d..56e8f1f3dc7e 100644 --- a/drivers/char/tpm/Makefile +++ b/drivers/char/tpm/Makefile | |||
@@ -20,6 +20,6 @@ obj-$(CONFIG_TCG_NSC) += tpm_nsc.o | |||
20 | obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o | 20 | obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o |
21 | obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o | 21 | obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o |
22 | obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o | 22 | obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o |
23 | obj-$(CONFIG_TCG_TIS_I2C_ST33) += tpm_i2c_stm_st33.o | 23 | obj-$(CONFIG_TCG_TIS_ST33ZP24) += st33zp24/ |
24 | obj-$(CONFIG_TCG_XEN) += xen-tpmfront.o | 24 | obj-$(CONFIG_TCG_XEN) += xen-tpmfront.o |
25 | obj-$(CONFIG_TCG_CRB) += tpm_crb.o | 25 | obj-$(CONFIG_TCG_CRB) += tpm_crb.o |
diff --git a/drivers/char/tpm/st33zp24/Kconfig b/drivers/char/tpm/st33zp24/Kconfig new file mode 100644 index 000000000000..09cb727864f0 --- /dev/null +++ b/drivers/char/tpm/st33zp24/Kconfig | |||
@@ -0,0 +1,30 @@ | |||
1 | config TCG_TIS_ST33ZP24 | ||
2 | tristate "STMicroelectronics TPM Interface Specification 1.2 Interface" | ||
3 | depends on GPIOLIB | ||
4 | ---help--- | ||
5 | STMicroelectronics ST33ZP24 core driver. It implements the core | ||
6 | TPM1.2 logic and hooks into the TPM kernel APIs. Physical layers will | ||
7 | register against it. | ||
8 | |||
9 | To compile this driver as a module, choose m here. The module will be called | ||
10 | tpm_st33zp24. | ||
11 | |||
12 | config TCG_TIS_ST33ZP24_I2C | ||
13 | tristate "TPM 1.2 ST33ZP24 I2C support" | ||
14 | depends on TCG_TIS_ST33ZP24 | ||
15 | depends on I2C | ||
16 | ---help--- | ||
17 | This module adds support for the STMicroelectronics TPM security chip | ||
18 | ST33ZP24 with i2c interface. | ||
19 | To compile this driver as a module, choose M here; the module will be | ||
20 | called tpm_st33zp24_i2c. | ||
21 | |||
22 | config TCG_TIS_ST33ZP24_SPI | ||
23 | tristate "TPM 1.2 ST33ZP24 SPI support" | ||
24 | depends on TCG_TIS_ST33ZP24 | ||
25 | depends on SPI | ||
26 | ---help--- | ||
27 | This module adds support for the STMicroelectronics TPM security chip | ||
28 | ST33ZP24 with spi interface. | ||
29 | To compile this driver as a module, choose M here; the module will be | ||
30 | called tpm_st33zp24_spi. | ||
diff --git a/drivers/char/tpm/st33zp24/Makefile b/drivers/char/tpm/st33zp24/Makefile new file mode 100644 index 000000000000..74a722e5e068 --- /dev/null +++ b/drivers/char/tpm/st33zp24/Makefile | |||
@@ -0,0 +1,12 @@ | |||
1 | # | ||
2 | # Makefile for ST33ZP24 TPM 1.2 driver | ||
3 | # | ||
4 | |||
5 | tpm_st33zp24-objs = st33zp24.o | ||
6 | obj-$(CONFIG_TCG_TIS_ST33ZP24) += tpm_st33zp24.o | ||
7 | |||
8 | tpm_st33zp24_i2c-objs = i2c.o | ||
9 | obj-$(CONFIG_TCG_TIS_ST33ZP24_I2C) += tpm_st33zp24_i2c.o | ||
10 | |||
11 | tpm_st33zp24_spi-objs = spi.o | ||
12 | obj-$(CONFIG_TCG_TIS_ST33ZP24_SPI) += tpm_st33zp24_spi.o | ||
diff --git a/drivers/char/tpm/st33zp24/i2c.c b/drivers/char/tpm/st33zp24/i2c.c new file mode 100644 index 000000000000..ad1ee180e0c2 --- /dev/null +++ b/drivers/char/tpm/st33zp24/i2c.c | |||
@@ -0,0 +1,276 @@ | |||
1 | /* | ||
2 | * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24 | ||
3 | * Copyright (C) 2009 - 2015 STMicroelectronics | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/i2c.h> | ||
21 | #include <linux/gpio.h> | ||
22 | #include <linux/of_irq.h> | ||
23 | #include <linux/of_gpio.h> | ||
24 | #include <linux/tpm.h> | ||
25 | #include <linux/platform_data/st33zp24.h> | ||
26 | |||
27 | #include "st33zp24.h" | ||
28 | |||
29 | #define TPM_DUMMY_BYTE 0xAA | ||
30 | |||
31 | struct st33zp24_i2c_phy { | ||
32 | struct i2c_client *client; | ||
33 | u8 buf[TPM_BUFSIZE + 1]; | ||
34 | int io_lpcpd; | ||
35 | }; | ||
36 | |||
37 | /* | ||
38 | * write8_reg | ||
39 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | ||
40 | * @param: tpm_register, the tpm tis register where the data should be written | ||
41 | * @param: tpm_data, the tpm_data to write inside the tpm_register | ||
42 | * @param: tpm_size, The length of the data | ||
43 | * @return: Returns negative errno, or else the number of bytes written. | ||
44 | */ | ||
45 | static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size) | ||
46 | { | ||
47 | struct st33zp24_i2c_phy *phy = phy_id; | ||
48 | |||
49 | phy->buf[0] = tpm_register; | ||
50 | memcpy(phy->buf + 1, tpm_data, tpm_size); | ||
51 | return i2c_master_send(phy->client, phy->buf, tpm_size + 1); | ||
52 | } /* write8_reg() */ | ||
53 | |||
54 | /* | ||
55 | * read8_reg | ||
56 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | ||
57 | * @param: tpm_register, the tpm tis register where the data should be read | ||
58 | * @param: tpm_data, the TPM response | ||
59 | * @param: tpm_size, tpm TPM response size to read. | ||
60 | * @return: number of byte read successfully: should be one if success. | ||
61 | */ | ||
62 | static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size) | ||
63 | { | ||
64 | struct st33zp24_i2c_phy *phy = phy_id; | ||
65 | u8 status = 0; | ||
66 | u8 data; | ||
67 | |||
68 | data = TPM_DUMMY_BYTE; | ||
69 | status = write8_reg(phy, tpm_register, &data, 1); | ||
70 | if (status == 2) | ||
71 | status = i2c_master_recv(phy->client, tpm_data, tpm_size); | ||
72 | return status; | ||
73 | } /* read8_reg() */ | ||
74 | |||
75 | /* | ||
76 | * st33zp24_i2c_send | ||
77 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | ||
78 | * @param: phy_id, the phy description | ||
79 | * @param: tpm_register, the tpm tis register where the data should be written | ||
80 | * @param: tpm_data, the tpm_data to write inside the tpm_register | ||
81 | * @param: tpm_size, the length of the data | ||
82 | * @return: number of byte written successfully: should be one if success. | ||
83 | */ | ||
84 | static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data, | ||
85 | int tpm_size) | ||
86 | { | ||
87 | return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data, | ||
88 | tpm_size); | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * st33zp24_i2c_recv | ||
93 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | ||
94 | * @param: phy_id, the phy description | ||
95 | * @param: tpm_register, the tpm tis register where the data should be read | ||
96 | * @param: tpm_data, the TPM response | ||
97 | * @param: tpm_size, tpm TPM response size to read. | ||
98 | * @return: number of byte read successfully: should be one if success. | ||
99 | */ | ||
100 | static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data, | ||
101 | int tpm_size) | ||
102 | { | ||
103 | return read8_reg(phy_id, tpm_register, tpm_data, tpm_size); | ||
104 | } | ||
105 | |||
106 | static const struct st33zp24_phy_ops i2c_phy_ops = { | ||
107 | .send = st33zp24_i2c_send, | ||
108 | .recv = st33zp24_i2c_recv, | ||
109 | }; | ||
110 | |||
111 | #ifdef CONFIG_OF | ||
112 | static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy) | ||
113 | { | ||
114 | struct device_node *pp; | ||
115 | struct i2c_client *client = phy->client; | ||
116 | int gpio; | ||
117 | int ret; | ||
118 | |||
119 | pp = client->dev.of_node; | ||
120 | if (!pp) { | ||
121 | dev_err(&client->dev, "No platform data\n"); | ||
122 | return -ENODEV; | ||
123 | } | ||
124 | |||
125 | /* Get GPIO from device tree */ | ||
126 | gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0); | ||
127 | if (gpio < 0) { | ||
128 | dev_err(&client->dev, | ||
129 | "Failed to retrieve lpcpd-gpios from dts.\n"); | ||
130 | phy->io_lpcpd = -1; | ||
131 | /* | ||
132 | * lpcpd pin is not specified. This is not an issue as | ||
133 | * power management can be also managed by TPM specific | ||
134 | * commands. So leave with a success status code. | ||
135 | */ | ||
136 | return 0; | ||
137 | } | ||
138 | /* GPIO request and configuration */ | ||
139 | ret = devm_gpio_request_one(&client->dev, gpio, | ||
140 | GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD"); | ||
141 | if (ret) { | ||
142 | dev_err(&client->dev, "Failed to request lpcpd pin\n"); | ||
143 | return -ENODEV; | ||
144 | } | ||
145 | phy->io_lpcpd = gpio; | ||
146 | |||
147 | return 0; | ||
148 | } | ||
149 | #else | ||
150 | static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy) | ||
151 | { | ||
152 | return -ENODEV; | ||
153 | } | ||
154 | #endif | ||
155 | |||
156 | static int st33zp24_i2c_request_resources(struct i2c_client *client, | ||
157 | struct st33zp24_i2c_phy *phy) | ||
158 | { | ||
159 | struct st33zp24_platform_data *pdata; | ||
160 | int ret; | ||
161 | |||
162 | pdata = client->dev.platform_data; | ||
163 | if (!pdata) { | ||
164 | dev_err(&client->dev, "No platform data\n"); | ||
165 | return -ENODEV; | ||
166 | } | ||
167 | |||
168 | /* store for late use */ | ||
169 | phy->io_lpcpd = pdata->io_lpcpd; | ||
170 | |||
171 | if (gpio_is_valid(pdata->io_lpcpd)) { | ||
172 | ret = devm_gpio_request_one(&client->dev, | ||
173 | pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH, | ||
174 | "TPM IO_LPCPD"); | ||
175 | if (ret) { | ||
176 | dev_err(&client->dev, "Failed to request lpcpd pin\n"); | ||
177 | return ret; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | /* | ||
185 | * st33zp24_i2c_probe initialize the TPM device | ||
186 | * @param: client, the i2c_client drescription (TPM I2C description). | ||
187 | * @param: id, the i2c_device_id struct. | ||
188 | * @return: 0 in case of success. | ||
189 | * -1 in other case. | ||
190 | */ | ||
191 | static int st33zp24_i2c_probe(struct i2c_client *client, | ||
192 | const struct i2c_device_id *id) | ||
193 | { | ||
194 | int ret; | ||
195 | struct st33zp24_platform_data *pdata; | ||
196 | struct st33zp24_i2c_phy *phy; | ||
197 | |||
198 | if (!client) { | ||
199 | pr_info("%s: i2c client is NULL. Device not accessible.\n", | ||
200 | __func__); | ||
201 | return -ENODEV; | ||
202 | } | ||
203 | |||
204 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
205 | dev_info(&client->dev, "client not i2c capable\n"); | ||
206 | return -ENODEV; | ||
207 | } | ||
208 | |||
209 | phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy), | ||
210 | GFP_KERNEL); | ||
211 | if (!phy) | ||
212 | return -ENOMEM; | ||
213 | |||
214 | phy->client = client; | ||
215 | pdata = client->dev.platform_data; | ||
216 | if (!pdata && client->dev.of_node) { | ||
217 | ret = st33zp24_i2c_of_request_resources(phy); | ||
218 | if (ret) | ||
219 | return ret; | ||
220 | } else if (pdata) { | ||
221 | ret = st33zp24_i2c_request_resources(client, phy); | ||
222 | if (ret) | ||
223 | return ret; | ||
224 | } | ||
225 | |||
226 | return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq, | ||
227 | phy->io_lpcpd); | ||
228 | } | ||
229 | |||
230 | /* | ||
231 | * st33zp24_i2c_remove remove the TPM device | ||
232 | * @param: client, the i2c_client description (TPM I2C description). | ||
233 | * @return: 0 in case of success. | ||
234 | */ | ||
235 | static int st33zp24_i2c_remove(struct i2c_client *client) | ||
236 | { | ||
237 | struct tpm_chip *chip = i2c_get_clientdata(client); | ||
238 | |||
239 | return st33zp24_remove(chip); | ||
240 | } | ||
241 | |||
242 | static const struct i2c_device_id st33zp24_i2c_id[] = { | ||
243 | {TPM_ST33_I2C, 0}, | ||
244 | {} | ||
245 | }; | ||
246 | MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id); | ||
247 | |||
248 | #ifdef CONFIG_OF | ||
249 | static const struct of_device_id of_st33zp24_i2c_match[] = { | ||
250 | { .compatible = "st,st33zp24-i2c", }, | ||
251 | {} | ||
252 | }; | ||
253 | MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match); | ||
254 | #endif | ||
255 | |||
256 | static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend, | ||
257 | st33zp24_pm_resume); | ||
258 | |||
259 | static struct i2c_driver st33zp24_i2c_driver = { | ||
260 | .driver = { | ||
261 | .owner = THIS_MODULE, | ||
262 | .name = TPM_ST33_I2C, | ||
263 | .pm = &st33zp24_i2c_ops, | ||
264 | .of_match_table = of_match_ptr(of_st33zp24_i2c_match), | ||
265 | }, | ||
266 | .probe = st33zp24_i2c_probe, | ||
267 | .remove = st33zp24_i2c_remove, | ||
268 | .id_table = st33zp24_i2c_id | ||
269 | }; | ||
270 | |||
271 | module_i2c_driver(st33zp24_i2c_driver); | ||
272 | |||
273 | MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)"); | ||
274 | MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver"); | ||
275 | MODULE_VERSION("1.3.0"); | ||
276 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/char/tpm/st33zp24/spi.c b/drivers/char/tpm/st33zp24/spi.c new file mode 100644 index 000000000000..f0184a1b0c1c --- /dev/null +++ b/drivers/char/tpm/st33zp24/spi.c | |||
@@ -0,0 +1,399 @@ | |||
1 | /* | ||
2 | * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24 | ||
3 | * Copyright (C) 2009 - 2015 STMicroelectronics | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/spi/spi.h> | ||
21 | #include <linux/gpio.h> | ||
22 | #include <linux/of_irq.h> | ||
23 | #include <linux/of_gpio.h> | ||
24 | #include <linux/tpm.h> | ||
25 | #include <linux/platform_data/st33zp24.h> | ||
26 | |||
27 | #include "st33zp24.h" | ||
28 | |||
29 | #define TPM_DATA_FIFO 0x24 | ||
30 | #define TPM_INTF_CAPABILITY 0x14 | ||
31 | |||
32 | #define TPM_DUMMY_BYTE 0x00 | ||
33 | |||
34 | #define MAX_SPI_LATENCY 15 | ||
35 | #define LOCALITY0 0 | ||
36 | |||
37 | #define ST33ZP24_OK 0x5A | ||
38 | #define ST33ZP24_UNDEFINED_ERR 0x80 | ||
39 | #define ST33ZP24_BADLOCALITY 0x81 | ||
40 | #define ST33ZP24_TISREGISTER_UKNOWN 0x82 | ||
41 | #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83 | ||
42 | #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84 | ||
43 | #define ST33ZP24_BAD_COMMAND_ORDER 0x85 | ||
44 | #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86 | ||
45 | #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89 | ||
46 | #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A | ||
47 | #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B | ||
48 | #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90 | ||
49 | #define ST33ZP24_DUMMY_BYTES 0x00 | ||
50 | |||
51 | /* | ||
52 | * TPM command can be up to 2048 byte, A TPM response can be up to | ||
53 | * 1024 byte. | ||
54 | * Between command and response, there are latency byte (up to 15 | ||
55 | * usually on st33zp24 2 are enough). | ||
56 | * | ||
57 | * Overall when sending a command and expecting an answer we need if | ||
58 | * worst case: | ||
59 | * 2048 (for the TPM command) + 1024 (for the TPM answer). We need | ||
60 | * some latency byte before the answer is available (max 15). | ||
61 | * We have 2048 + 1024 + 15. | ||
62 | */ | ||
63 | #define ST33ZP24_SPI_BUFFER_SIZE (TPM_BUFSIZE + (TPM_BUFSIZE / 2) +\ | ||
64 | MAX_SPI_LATENCY) | ||
65 | |||
66 | |||
67 | struct st33zp24_spi_phy { | ||
68 | struct spi_device *spi_device; | ||
69 | struct spi_transfer spi_xfer; | ||
70 | u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE]; | ||
71 | u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE]; | ||
72 | |||
73 | int io_lpcpd; | ||
74 | int latency; | ||
75 | }; | ||
76 | |||
77 | static int st33zp24_status_to_errno(u8 code) | ||
78 | { | ||
79 | switch (code) { | ||
80 | case ST33ZP24_OK: | ||
81 | return 0; | ||
82 | case ST33ZP24_UNDEFINED_ERR: | ||
83 | case ST33ZP24_BADLOCALITY: | ||
84 | case ST33ZP24_TISREGISTER_UKNOWN: | ||
85 | case ST33ZP24_LOCALITY_NOT_ACTIVATED: | ||
86 | case ST33ZP24_HASH_END_BEFORE_HASH_START: | ||
87 | case ST33ZP24_BAD_COMMAND_ORDER: | ||
88 | case ST33ZP24_UNEXPECTED_READ_FIFO: | ||
89 | case ST33ZP24_UNEXPECTED_WRITE_FIFO: | ||
90 | case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END: | ||
91 | return -EPROTO; | ||
92 | case ST33ZP24_INCORECT_RECEIVED_LENGTH: | ||
93 | case ST33ZP24_TPM_FIFO_OVERFLOW: | ||
94 | return -EMSGSIZE; | ||
95 | case ST33ZP24_DUMMY_BYTES: | ||
96 | return -ENOSYS; | ||
97 | } | ||
98 | return code; | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * st33zp24_spi_send | ||
103 | * Send byte to the TIS register according to the ST33ZP24 SPI protocol. | ||
104 | * @param: phy_id, the phy description | ||
105 | * @param: tpm_register, the tpm tis register where the data should be written | ||
106 | * @param: tpm_data, the tpm_data to write inside the tpm_register | ||
107 | * @param: tpm_size, The length of the data | ||
108 | * @return: should be zero if success else a negative error code. | ||
109 | */ | ||
110 | static int st33zp24_spi_send(void *phy_id, u8 tpm_register, u8 *tpm_data, | ||
111 | int tpm_size) | ||
112 | { | ||
113 | u8 data = 0; | ||
114 | int total_length = 0, nbr_dummy_bytes = 0, ret = 0; | ||
115 | struct st33zp24_spi_phy *phy = phy_id; | ||
116 | struct spi_device *dev = phy->spi_device; | ||
117 | u8 *tx_buf = (u8 *)phy->spi_xfer.tx_buf; | ||
118 | u8 *rx_buf = phy->spi_xfer.rx_buf; | ||
119 | |||
120 | /* Pre-Header */ | ||
121 | data = TPM_WRITE_DIRECTION | LOCALITY0; | ||
122 | memcpy(tx_buf + total_length, &data, sizeof(data)); | ||
123 | total_length++; | ||
124 | data = tpm_register; | ||
125 | memcpy(tx_buf + total_length, &data, sizeof(data)); | ||
126 | total_length++; | ||
127 | |||
128 | if (tpm_size > 0 && tpm_register == TPM_DATA_FIFO) { | ||
129 | tx_buf[total_length++] = tpm_size >> 8; | ||
130 | tx_buf[total_length++] = tpm_size; | ||
131 | } | ||
132 | |||
133 | memcpy(&tx_buf[total_length], tpm_data, tpm_size); | ||
134 | total_length += tpm_size; | ||
135 | |||
136 | nbr_dummy_bytes = phy->latency; | ||
137 | memset(&tx_buf[total_length], TPM_DUMMY_BYTE, nbr_dummy_bytes); | ||
138 | |||
139 | phy->spi_xfer.len = total_length + nbr_dummy_bytes; | ||
140 | |||
141 | ret = spi_sync_transfer(dev, &phy->spi_xfer, 1); | ||
142 | if (ret == 0) | ||
143 | ret = rx_buf[total_length + nbr_dummy_bytes - 1]; | ||
144 | |||
145 | return st33zp24_status_to_errno(ret); | ||
146 | } /* st33zp24_spi_send() */ | ||
147 | |||
148 | /* | ||
149 | * read8_recv | ||
150 | * Recv byte from the TIS register according to the ST33ZP24 SPI protocol. | ||
151 | * @param: phy_id, the phy description | ||
152 | * @param: tpm_register, the tpm tis register where the data should be read | ||
153 | * @param: tpm_data, the TPM response | ||
154 | * @param: tpm_size, tpm TPM response size to read. | ||
155 | * @return: should be zero if success else a negative error code. | ||
156 | */ | ||
157 | static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size) | ||
158 | { | ||
159 | u8 data = 0; | ||
160 | int total_length = 0, nbr_dummy_bytes, ret; | ||
161 | struct st33zp24_spi_phy *phy = phy_id; | ||
162 | struct spi_device *dev = phy->spi_device; | ||
163 | u8 *tx_buf = (u8 *)phy->spi_xfer.tx_buf; | ||
164 | u8 *rx_buf = phy->spi_xfer.rx_buf; | ||
165 | |||
166 | /* Pre-Header */ | ||
167 | data = LOCALITY0; | ||
168 | memcpy(tx_buf + total_length, &data, sizeof(data)); | ||
169 | total_length++; | ||
170 | data = tpm_register; | ||
171 | memcpy(tx_buf + total_length, &data, sizeof(data)); | ||
172 | total_length++; | ||
173 | |||
174 | nbr_dummy_bytes = phy->latency; | ||
175 | memset(&tx_buf[total_length], TPM_DUMMY_BYTE, | ||
176 | nbr_dummy_bytes + tpm_size); | ||
177 | |||
178 | phy->spi_xfer.len = total_length + nbr_dummy_bytes + tpm_size; | ||
179 | |||
180 | /* header + status byte + size of the data + status byte */ | ||
181 | ret = spi_sync_transfer(dev, &phy->spi_xfer, 1); | ||
182 | if (tpm_size > 0 && ret == 0) { | ||
183 | ret = rx_buf[total_length + nbr_dummy_bytes - 1]; | ||
184 | |||
185 | memcpy(tpm_data, rx_buf + total_length + nbr_dummy_bytes, | ||
186 | tpm_size); | ||
187 | } | ||
188 | |||
189 | return ret; | ||
190 | } /* read8_reg() */ | ||
191 | |||
192 | /* | ||
193 | * st33zp24_spi_recv | ||
194 | * Recv byte from the TIS register according to the ST33ZP24 SPI protocol. | ||
195 | * @param: phy_id, the phy description | ||
196 | * @param: tpm_register, the tpm tis register where the data should be read | ||
197 | * @param: tpm_data, the TPM response | ||
198 | * @param: tpm_size, tpm TPM response size to read. | ||
199 | * @return: number of byte read successfully: should be one if success. | ||
200 | */ | ||
201 | static int st33zp24_spi_recv(void *phy_id, u8 tpm_register, u8 *tpm_data, | ||
202 | int tpm_size) | ||
203 | { | ||
204 | int ret; | ||
205 | |||
206 | ret = read8_reg(phy_id, tpm_register, tpm_data, tpm_size); | ||
207 | if (!st33zp24_status_to_errno(ret)) | ||
208 | return tpm_size; | ||
209 | return ret; | ||
210 | } /* st33zp24_spi_recv() */ | ||
211 | |||
212 | static int evaluate_latency(void *phy_id) | ||
213 | { | ||
214 | struct st33zp24_spi_phy *phy = phy_id; | ||
215 | int latency = 1, status = 0; | ||
216 | u8 data = 0; | ||
217 | |||
218 | while (!status && latency < MAX_SPI_LATENCY) { | ||
219 | phy->latency = latency; | ||
220 | status = read8_reg(phy_id, TPM_INTF_CAPABILITY, &data, 1); | ||
221 | latency++; | ||
222 | } | ||
223 | return latency - 1; | ||
224 | } /* evaluate_latency() */ | ||
225 | |||
226 | static const struct st33zp24_phy_ops spi_phy_ops = { | ||
227 | .send = st33zp24_spi_send, | ||
228 | .recv = st33zp24_spi_recv, | ||
229 | }; | ||
230 | |||
231 | #ifdef CONFIG_OF | ||
232 | static int tpm_stm_spi_of_request_resources(struct st33zp24_spi_phy *phy) | ||
233 | { | ||
234 | struct device_node *pp; | ||
235 | struct spi_device *dev = phy->spi_device; | ||
236 | int gpio; | ||
237 | int ret; | ||
238 | |||
239 | pp = dev->dev.of_node; | ||
240 | if (!pp) { | ||
241 | dev_err(&dev->dev, "No platform data\n"); | ||
242 | return -ENODEV; | ||
243 | } | ||
244 | |||
245 | /* Get GPIO from device tree */ | ||
246 | gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0); | ||
247 | if (gpio < 0) { | ||
248 | dev_err(&dev->dev, | ||
249 | "Failed to retrieve lpcpd-gpios from dts.\n"); | ||
250 | phy->io_lpcpd = -1; | ||
251 | /* | ||
252 | * lpcpd pin is not specified. This is not an issue as | ||
253 | * power management can be also managed by TPM specific | ||
254 | * commands. So leave with a success status code. | ||
255 | */ | ||
256 | return 0; | ||
257 | } | ||
258 | /* GPIO request and configuration */ | ||
259 | ret = devm_gpio_request_one(&dev->dev, gpio, | ||
260 | GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD"); | ||
261 | if (ret) { | ||
262 | dev_err(&dev->dev, "Failed to request lpcpd pin\n"); | ||
263 | return -ENODEV; | ||
264 | } | ||
265 | phy->io_lpcpd = gpio; | ||
266 | |||
267 | return 0; | ||
268 | } | ||
269 | #else | ||
270 | static int tpm_stm_spi_of_request_resources(struct st33zp24_spi_phy *phy) | ||
271 | { | ||
272 | return -ENODEV; | ||
273 | } | ||
274 | #endif | ||
275 | |||
276 | static int tpm_stm_spi_request_resources(struct spi_device *dev, | ||
277 | struct st33zp24_spi_phy *phy) | ||
278 | { | ||
279 | struct st33zp24_platform_data *pdata; | ||
280 | int ret; | ||
281 | |||
282 | pdata = dev->dev.platform_data; | ||
283 | if (!pdata) { | ||
284 | dev_err(&dev->dev, "No platform data\n"); | ||
285 | return -ENODEV; | ||
286 | } | ||
287 | |||
288 | /* store for late use */ | ||
289 | phy->io_lpcpd = pdata->io_lpcpd; | ||
290 | |||
291 | if (gpio_is_valid(pdata->io_lpcpd)) { | ||
292 | ret = devm_gpio_request_one(&dev->dev, | ||
293 | pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH, | ||
294 | "TPM IO_LPCPD"); | ||
295 | if (ret) { | ||
296 | dev_err(&dev->dev, "%s : reset gpio_request failed\n", | ||
297 | __FILE__); | ||
298 | return ret; | ||
299 | } | ||
300 | } | ||
301 | |||
302 | return 0; | ||
303 | } | ||
304 | |||
305 | /* | ||
306 | * tpm_st33_spi_probe initialize the TPM device | ||
307 | * @param: dev, the spi_device drescription (TPM SPI description). | ||
308 | * @return: 0 in case of success. | ||
309 | * or a negative value describing the error. | ||
310 | */ | ||
311 | static int | ||
312 | tpm_st33_spi_probe(struct spi_device *dev) | ||
313 | { | ||
314 | int ret; | ||
315 | struct st33zp24_platform_data *pdata; | ||
316 | struct st33zp24_spi_phy *phy; | ||
317 | |||
318 | /* Check SPI platform functionnalities */ | ||
319 | if (!dev) { | ||
320 | pr_info("%s: dev is NULL. Device is not accessible.\n", | ||
321 | __func__); | ||
322 | return -ENODEV; | ||
323 | } | ||
324 | |||
325 | phy = devm_kzalloc(&dev->dev, sizeof(struct st33zp24_spi_phy), | ||
326 | GFP_KERNEL); | ||
327 | if (!phy) | ||
328 | return -ENOMEM; | ||
329 | |||
330 | phy->spi_device = dev; | ||
331 | pdata = dev->dev.platform_data; | ||
332 | if (!pdata && dev->dev.of_node) { | ||
333 | ret = tpm_stm_spi_of_request_resources(phy); | ||
334 | if (ret) | ||
335 | return ret; | ||
336 | } else if (pdata) { | ||
337 | ret = tpm_stm_spi_request_resources(dev, phy); | ||
338 | if (ret) | ||
339 | return ret; | ||
340 | } | ||
341 | |||
342 | phy->spi_xfer.tx_buf = phy->tx_buf; | ||
343 | phy->spi_xfer.rx_buf = phy->rx_buf; | ||
344 | |||
345 | phy->latency = evaluate_latency(phy); | ||
346 | if (phy->latency <= 0) | ||
347 | return -ENODEV; | ||
348 | |||
349 | return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq, | ||
350 | phy->io_lpcpd); | ||
351 | } | ||
352 | |||
353 | /* | ||
354 | * tpm_st33_spi_remove remove the TPM device | ||
355 | * @param: client, the spi_device drescription (TPM SPI description). | ||
356 | * @return: 0 in case of success. | ||
357 | */ | ||
358 | static int tpm_st33_spi_remove(struct spi_device *dev) | ||
359 | { | ||
360 | struct tpm_chip *chip = spi_get_drvdata(dev); | ||
361 | |||
362 | return st33zp24_remove(chip); | ||
363 | } | ||
364 | |||
365 | static const struct spi_device_id st33zp24_spi_id[] = { | ||
366 | {TPM_ST33_SPI, 0}, | ||
367 | {} | ||
368 | }; | ||
369 | MODULE_DEVICE_TABLE(spi, st33zp24_spi_id); | ||
370 | |||
371 | #ifdef CONFIG_OF | ||
372 | static const struct of_device_id of_st33zp24_spi_match[] = { | ||
373 | { .compatible = "st,st33zp24-spi", }, | ||
374 | {} | ||
375 | }; | ||
376 | MODULE_DEVICE_TABLE(of, of_st33zp24_spi_match); | ||
377 | #endif | ||
378 | |||
379 | static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops, st33zp24_pm_suspend, | ||
380 | st33zp24_pm_resume); | ||
381 | |||
382 | static struct spi_driver tpm_st33_spi_driver = { | ||
383 | .driver = { | ||
384 | .owner = THIS_MODULE, | ||
385 | .name = TPM_ST33_SPI, | ||
386 | .pm = &st33zp24_spi_ops, | ||
387 | .of_match_table = of_match_ptr(of_st33zp24_spi_match), | ||
388 | }, | ||
389 | .probe = tpm_st33_spi_probe, | ||
390 | .remove = tpm_st33_spi_remove, | ||
391 | .id_table = st33zp24_spi_id, | ||
392 | }; | ||
393 | |||
394 | module_spi_driver(tpm_st33_spi_driver); | ||
395 | |||
396 | MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)"); | ||
397 | MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver"); | ||
398 | MODULE_VERSION("1.3.0"); | ||
399 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/char/tpm/st33zp24/st33zp24.c b/drivers/char/tpm/st33zp24/st33zp24.c new file mode 100644 index 000000000000..8d626784cd8d --- /dev/null +++ b/drivers/char/tpm/st33zp24/st33zp24.c | |||
@@ -0,0 +1,698 @@ | |||
1 | /* | ||
2 | * STMicroelectronics TPM Linux driver for TPM ST33ZP24 | ||
3 | * Copyright (C) 2009 - 2015 STMicroelectronics | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #include <linux/module.h> | ||
20 | #include <linux/fs.h> | ||
21 | #include <linux/miscdevice.h> | ||
22 | #include <linux/kernel.h> | ||
23 | #include <linux/delay.h> | ||
24 | #include <linux/wait.h> | ||
25 | #include <linux/freezer.h> | ||
26 | #include <linux/string.h> | ||
27 | #include <linux/interrupt.h> | ||
28 | #include <linux/gpio.h> | ||
29 | #include <linux/sched.h> | ||
30 | #include <linux/uaccess.h> | ||
31 | #include <linux/io.h> | ||
32 | #include <linux/slab.h> | ||
33 | |||
34 | #include "../tpm.h" | ||
35 | #include "st33zp24.h" | ||
36 | |||
37 | #define TPM_ACCESS 0x0 | ||
38 | #define TPM_STS 0x18 | ||
39 | #define TPM_DATA_FIFO 0x24 | ||
40 | #define TPM_INTF_CAPABILITY 0x14 | ||
41 | #define TPM_INT_STATUS 0x10 | ||
42 | #define TPM_INT_ENABLE 0x08 | ||
43 | |||
44 | #define LOCALITY0 0 | ||
45 | |||
46 | enum st33zp24_access { | ||
47 | TPM_ACCESS_VALID = 0x80, | ||
48 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, | ||
49 | TPM_ACCESS_REQUEST_PENDING = 0x04, | ||
50 | TPM_ACCESS_REQUEST_USE = 0x02, | ||
51 | }; | ||
52 | |||
53 | enum st33zp24_status { | ||
54 | TPM_STS_VALID = 0x80, | ||
55 | TPM_STS_COMMAND_READY = 0x40, | ||
56 | TPM_STS_GO = 0x20, | ||
57 | TPM_STS_DATA_AVAIL = 0x10, | ||
58 | TPM_STS_DATA_EXPECT = 0x08, | ||
59 | }; | ||
60 | |||
61 | enum st33zp24_int_flags { | ||
62 | TPM_GLOBAL_INT_ENABLE = 0x80, | ||
63 | TPM_INTF_CMD_READY_INT = 0x080, | ||
64 | TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, | ||
65 | TPM_INTF_WAKE_UP_READY_INT = 0x020, | ||
66 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, | ||
67 | TPM_INTF_STS_VALID_INT = 0x002, | ||
68 | TPM_INTF_DATA_AVAIL_INT = 0x001, | ||
69 | }; | ||
70 | |||
71 | enum tis_defaults { | ||
72 | TIS_SHORT_TIMEOUT = 750, | ||
73 | TIS_LONG_TIMEOUT = 2000, | ||
74 | }; | ||
75 | |||
76 | struct st33zp24_dev { | ||
77 | struct tpm_chip *chip; | ||
78 | void *phy_id; | ||
79 | const struct st33zp24_phy_ops *ops; | ||
80 | u32 intrs; | ||
81 | int io_lpcpd; | ||
82 | }; | ||
83 | |||
84 | /* | ||
85 | * clear_interruption clear the pending interrupt. | ||
86 | * @param: tpm_dev, the tpm device device. | ||
87 | * @return: the interrupt status value. | ||
88 | */ | ||
89 | static u8 clear_interruption(struct st33zp24_dev *tpm_dev) | ||
90 | { | ||
91 | u8 interrupt; | ||
92 | |||
93 | tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); | ||
94 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); | ||
95 | return interrupt; | ||
96 | } /* clear_interruption() */ | ||
97 | |||
98 | /* | ||
99 | * st33zp24_cancel, cancel the current command execution or | ||
100 | * set STS to COMMAND READY. | ||
101 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | ||
102 | */ | ||
103 | static void st33zp24_cancel(struct tpm_chip *chip) | ||
104 | { | ||
105 | struct st33zp24_dev *tpm_dev; | ||
106 | u8 data; | ||
107 | |||
108 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
109 | |||
110 | data = TPM_STS_COMMAND_READY; | ||
111 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); | ||
112 | } /* st33zp24_cancel() */ | ||
113 | |||
114 | /* | ||
115 | * st33zp24_status return the TPM_STS register | ||
116 | * @param: chip, the tpm chip description | ||
117 | * @return: the TPM_STS register value. | ||
118 | */ | ||
119 | static u8 st33zp24_status(struct tpm_chip *chip) | ||
120 | { | ||
121 | struct st33zp24_dev *tpm_dev; | ||
122 | u8 data; | ||
123 | |||
124 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
125 | |||
126 | tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1); | ||
127 | return data; | ||
128 | } /* st33zp24_status() */ | ||
129 | |||
130 | /* | ||
131 | * check_locality if the locality is active | ||
132 | * @param: chip, the tpm chip description | ||
133 | * @return: the active locality or -EACCESS. | ||
134 | */ | ||
135 | static int check_locality(struct tpm_chip *chip) | ||
136 | { | ||
137 | struct st33zp24_dev *tpm_dev; | ||
138 | u8 data; | ||
139 | u8 status; | ||
140 | |||
141 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
142 | |||
143 | status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1); | ||
144 | if (status && (data & | ||
145 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == | ||
146 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) | ||
147 | return chip->vendor.locality; | ||
148 | |||
149 | return -EACCES; | ||
150 | } /* check_locality() */ | ||
151 | |||
152 | /* | ||
153 | * request_locality request the TPM locality | ||
154 | * @param: chip, the chip description | ||
155 | * @return: the active locality or negative value. | ||
156 | */ | ||
157 | static int request_locality(struct tpm_chip *chip) | ||
158 | { | ||
159 | unsigned long stop; | ||
160 | long ret; | ||
161 | struct st33zp24_dev *tpm_dev; | ||
162 | u8 data; | ||
163 | |||
164 | if (check_locality(chip) == chip->vendor.locality) | ||
165 | return chip->vendor.locality; | ||
166 | |||
167 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
168 | |||
169 | data = TPM_ACCESS_REQUEST_USE; | ||
170 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); | ||
171 | if (ret < 0) | ||
172 | return ret; | ||
173 | |||
174 | stop = jiffies + chip->vendor.timeout_a; | ||
175 | |||
176 | /* Request locality is usually effective after the request */ | ||
177 | do { | ||
178 | if (check_locality(chip) >= 0) | ||
179 | return chip->vendor.locality; | ||
180 | msleep(TPM_TIMEOUT); | ||
181 | } while (time_before(jiffies, stop)); | ||
182 | |||
183 | /* could not get locality */ | ||
184 | return -EACCES; | ||
185 | } /* request_locality() */ | ||
186 | |||
187 | /* | ||
188 | * release_locality release the active locality | ||
189 | * @param: chip, the tpm chip description. | ||
190 | */ | ||
191 | static void release_locality(struct tpm_chip *chip) | ||
192 | { | ||
193 | struct st33zp24_dev *tpm_dev; | ||
194 | u8 data; | ||
195 | |||
196 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
197 | data = TPM_ACCESS_ACTIVE_LOCALITY; | ||
198 | |||
199 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); | ||
200 | } | ||
201 | |||
202 | /* | ||
203 | * get_burstcount return the burstcount value | ||
204 | * @param: chip, the chip description | ||
205 | * return: the burstcount or negative value. | ||
206 | */ | ||
207 | static int get_burstcount(struct tpm_chip *chip) | ||
208 | { | ||
209 | unsigned long stop; | ||
210 | int burstcnt, status; | ||
211 | u8 tpm_reg, temp; | ||
212 | struct st33zp24_dev *tpm_dev; | ||
213 | |||
214 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
215 | |||
216 | stop = jiffies + chip->vendor.timeout_d; | ||
217 | do { | ||
218 | tpm_reg = TPM_STS + 1; | ||
219 | status = tpm_dev->ops->recv(tpm_dev->phy_id, tpm_reg, &temp, 1); | ||
220 | if (status < 0) | ||
221 | return -EBUSY; | ||
222 | |||
223 | tpm_reg = TPM_STS + 2; | ||
224 | burstcnt = temp; | ||
225 | status = tpm_dev->ops->recv(tpm_dev->phy_id, tpm_reg, &temp, 1); | ||
226 | if (status < 0) | ||
227 | return -EBUSY; | ||
228 | |||
229 | burstcnt |= temp << 8; | ||
230 | if (burstcnt) | ||
231 | return burstcnt; | ||
232 | msleep(TPM_TIMEOUT); | ||
233 | } while (time_before(jiffies, stop)); | ||
234 | return -EBUSY; | ||
235 | } /* get_burstcount() */ | ||
236 | |||
237 | |||
238 | /* | ||
239 | * wait_for_tpm_stat_cond | ||
240 | * @param: chip, chip description | ||
241 | * @param: mask, expected mask value | ||
242 | * @param: check_cancel, does the command expected to be canceled ? | ||
243 | * @param: canceled, did we received a cancel request ? | ||
244 | * @return: true if status == mask or if the command is canceled. | ||
245 | * false in other cases. | ||
246 | */ | ||
247 | static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, | ||
248 | bool check_cancel, bool *canceled) | ||
249 | { | ||
250 | u8 status = chip->ops->status(chip); | ||
251 | |||
252 | *canceled = false; | ||
253 | if ((status & mask) == mask) | ||
254 | return true; | ||
255 | if (check_cancel && chip->ops->req_canceled(chip, status)) { | ||
256 | *canceled = true; | ||
257 | return true; | ||
258 | } | ||
259 | return false; | ||
260 | } | ||
261 | |||
262 | /* | ||
263 | * wait_for_stat wait for a TPM_STS value | ||
264 | * @param: chip, the tpm chip description | ||
265 | * @param: mask, the value mask to wait | ||
266 | * @param: timeout, the timeout | ||
267 | * @param: queue, the wait queue. | ||
268 | * @param: check_cancel, does the command can be cancelled ? | ||
269 | * @return: the tpm status, 0 if success, -ETIME if timeout is reached. | ||
270 | */ | ||
271 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, | ||
272 | wait_queue_head_t *queue, bool check_cancel) | ||
273 | { | ||
274 | unsigned long stop; | ||
275 | int ret = 0; | ||
276 | bool canceled = false; | ||
277 | bool condition; | ||
278 | u32 cur_intrs; | ||
279 | u8 status; | ||
280 | struct st33zp24_dev *tpm_dev; | ||
281 | |||
282 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
283 | |||
284 | /* check current status */ | ||
285 | status = st33zp24_status(chip); | ||
286 | if ((status & mask) == mask) | ||
287 | return 0; | ||
288 | |||
289 | stop = jiffies + timeout; | ||
290 | |||
291 | if (chip->vendor.irq) { | ||
292 | cur_intrs = tpm_dev->intrs; | ||
293 | clear_interruption(tpm_dev); | ||
294 | enable_irq(chip->vendor.irq); | ||
295 | |||
296 | do { | ||
297 | if (ret == -ERESTARTSYS && freezing(current)) | ||
298 | clear_thread_flag(TIF_SIGPENDING); | ||
299 | |||
300 | timeout = stop - jiffies; | ||
301 | if ((long) timeout <= 0) | ||
302 | return -1; | ||
303 | |||
304 | ret = wait_event_interruptible_timeout(*queue, | ||
305 | cur_intrs != tpm_dev->intrs, | ||
306 | timeout); | ||
307 | clear_interruption(tpm_dev); | ||
308 | condition = wait_for_tpm_stat_cond(chip, mask, | ||
309 | check_cancel, &canceled); | ||
310 | if (ret >= 0 && condition) { | ||
311 | if (canceled) | ||
312 | return -ECANCELED; | ||
313 | return 0; | ||
314 | } | ||
315 | } while (ret == -ERESTARTSYS && freezing(current)); | ||
316 | |||
317 | disable_irq_nosync(chip->vendor.irq); | ||
318 | |||
319 | } else { | ||
320 | do { | ||
321 | msleep(TPM_TIMEOUT); | ||
322 | status = chip->ops->status(chip); | ||
323 | if ((status & mask) == mask) | ||
324 | return 0; | ||
325 | } while (time_before(jiffies, stop)); | ||
326 | } | ||
327 | |||
328 | return -ETIME; | ||
329 | } /* wait_for_stat() */ | ||
330 | |||
331 | /* | ||
332 | * recv_data receive data | ||
333 | * @param: chip, the tpm chip description | ||
334 | * @param: buf, the buffer where the data are received | ||
335 | * @param: count, the number of data to receive | ||
336 | * @return: the number of bytes read from TPM FIFO. | ||
337 | */ | ||
338 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) | ||
339 | { | ||
340 | int size = 0, burstcnt, len, ret; | ||
341 | struct st33zp24_dev *tpm_dev; | ||
342 | |||
343 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
344 | |||
345 | while (size < count && | ||
346 | wait_for_stat(chip, | ||
347 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
348 | chip->vendor.timeout_c, | ||
349 | &chip->vendor.read_queue, true) == 0) { | ||
350 | burstcnt = get_burstcount(chip); | ||
351 | if (burstcnt < 0) | ||
352 | return burstcnt; | ||
353 | len = min_t(int, burstcnt, count - size); | ||
354 | ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO, | ||
355 | buf + size, len); | ||
356 | if (ret < 0) | ||
357 | return ret; | ||
358 | |||
359 | size += len; | ||
360 | } | ||
361 | return size; | ||
362 | } | ||
363 | |||
364 | /* | ||
365 | * tpm_ioserirq_handler the serirq irq handler | ||
366 | * @param: irq, the tpm chip description | ||
367 | * @param: dev_id, the description of the chip | ||
368 | * @return: the status of the handler. | ||
369 | */ | ||
370 | static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) | ||
371 | { | ||
372 | struct tpm_chip *chip = dev_id; | ||
373 | struct st33zp24_dev *tpm_dev; | ||
374 | |||
375 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
376 | |||
377 | tpm_dev->intrs++; | ||
378 | wake_up_interruptible(&chip->vendor.read_queue); | ||
379 | disable_irq_nosync(chip->vendor.irq); | ||
380 | |||
381 | return IRQ_HANDLED; | ||
382 | } /* tpm_ioserirq_handler() */ | ||
383 | |||
384 | /* | ||
385 | * st33zp24_send send TPM commands through the I2C bus. | ||
386 | * | ||
387 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | ||
388 | * @param: buf, the buffer to send. | ||
389 | * @param: count, the number of bytes to send. | ||
390 | * @return: In case of success the number of bytes sent. | ||
391 | * In other case, a < 0 value describing the issue. | ||
392 | */ | ||
393 | static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf, | ||
394 | size_t len) | ||
395 | { | ||
396 | u32 status, i, size, ordinal; | ||
397 | int burstcnt = 0; | ||
398 | int ret; | ||
399 | u8 data; | ||
400 | struct st33zp24_dev *tpm_dev; | ||
401 | |||
402 | if (!chip) | ||
403 | return -EBUSY; | ||
404 | if (len < TPM_HEADER_SIZE) | ||
405 | return -EBUSY; | ||
406 | |||
407 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
408 | |||
409 | ret = request_locality(chip); | ||
410 | if (ret < 0) | ||
411 | return ret; | ||
412 | |||
413 | status = st33zp24_status(chip); | ||
414 | if ((status & TPM_STS_COMMAND_READY) == 0) { | ||
415 | st33zp24_cancel(chip); | ||
416 | if (wait_for_stat | ||
417 | (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, | ||
418 | &chip->vendor.read_queue, false) < 0) { | ||
419 | ret = -ETIME; | ||
420 | goto out_err; | ||
421 | } | ||
422 | } | ||
423 | |||
424 | for (i = 0; i < len - 1;) { | ||
425 | burstcnt = get_burstcount(chip); | ||
426 | if (burstcnt < 0) | ||
427 | return burstcnt; | ||
428 | size = min_t(int, len - i - 1, burstcnt); | ||
429 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, | ||
430 | buf + i, size); | ||
431 | if (ret < 0) | ||
432 | goto out_err; | ||
433 | |||
434 | i += size; | ||
435 | } | ||
436 | |||
437 | status = st33zp24_status(chip); | ||
438 | if ((status & TPM_STS_DATA_EXPECT) == 0) { | ||
439 | ret = -EIO; | ||
440 | goto out_err; | ||
441 | } | ||
442 | |||
443 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, | ||
444 | buf + len - 1, 1); | ||
445 | if (ret < 0) | ||
446 | goto out_err; | ||
447 | |||
448 | status = st33zp24_status(chip); | ||
449 | if ((status & TPM_STS_DATA_EXPECT) != 0) { | ||
450 | ret = -EIO; | ||
451 | goto out_err; | ||
452 | } | ||
453 | |||
454 | data = TPM_STS_GO; | ||
455 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); | ||
456 | if (ret < 0) | ||
457 | goto out_err; | ||
458 | |||
459 | if (chip->vendor.irq) { | ||
460 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); | ||
461 | |||
462 | ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
463 | tpm_calc_ordinal_duration(chip, ordinal), | ||
464 | &chip->vendor.read_queue, false); | ||
465 | if (ret < 0) | ||
466 | goto out_err; | ||
467 | } | ||
468 | |||
469 | return len; | ||
470 | out_err: | ||
471 | st33zp24_cancel(chip); | ||
472 | release_locality(chip); | ||
473 | return ret; | ||
474 | } | ||
475 | |||
476 | /* | ||
477 | * st33zp24_recv received TPM response through TPM phy. | ||
478 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. | ||
479 | * @param: buf, the buffer to store datas. | ||
480 | * @param: count, the number of bytes to send. | ||
481 | * @return: In case of success the number of bytes received. | ||
482 | * In other case, a < 0 value describing the issue. | ||
483 | */ | ||
484 | static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf, | ||
485 | size_t count) | ||
486 | { | ||
487 | int size = 0; | ||
488 | int expected; | ||
489 | |||
490 | if (!chip) | ||
491 | return -EBUSY; | ||
492 | |||
493 | if (count < TPM_HEADER_SIZE) { | ||
494 | size = -EIO; | ||
495 | goto out; | ||
496 | } | ||
497 | |||
498 | size = recv_data(chip, buf, TPM_HEADER_SIZE); | ||
499 | if (size < TPM_HEADER_SIZE) { | ||
500 | dev_err(&chip->dev, "Unable to read header\n"); | ||
501 | goto out; | ||
502 | } | ||
503 | |||
504 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); | ||
505 | if (expected > count) { | ||
506 | size = -EIO; | ||
507 | goto out; | ||
508 | } | ||
509 | |||
510 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], | ||
511 | expected - TPM_HEADER_SIZE); | ||
512 | if (size < expected) { | ||
513 | dev_err(&chip->dev, "Unable to read remainder of result\n"); | ||
514 | size = -ETIME; | ||
515 | } | ||
516 | |||
517 | out: | ||
518 | st33zp24_cancel(chip); | ||
519 | release_locality(chip); | ||
520 | return size; | ||
521 | } | ||
522 | |||
523 | /* | ||
524 | * st33zp24_req_canceled | ||
525 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. | ||
526 | * @param: status, the TPM status. | ||
527 | * @return: Does TPM ready to compute a new command ? true. | ||
528 | */ | ||
529 | static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status) | ||
530 | { | ||
531 | return (status == TPM_STS_COMMAND_READY); | ||
532 | } | ||
533 | |||
534 | static const struct tpm_class_ops st33zp24_tpm = { | ||
535 | .send = st33zp24_send, | ||
536 | .recv = st33zp24_recv, | ||
537 | .cancel = st33zp24_cancel, | ||
538 | .status = st33zp24_status, | ||
539 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
540 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
541 | .req_canceled = st33zp24_req_canceled, | ||
542 | }; | ||
543 | |||
544 | /* | ||
545 | * st33zp24_probe initialize the TPM device | ||
546 | * @param: client, the i2c_client drescription (TPM I2C description). | ||
547 | * @param: id, the i2c_device_id struct. | ||
548 | * @return: 0 in case of success. | ||
549 | * -1 in other case. | ||
550 | */ | ||
551 | int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops, | ||
552 | struct device *dev, int irq, int io_lpcpd) | ||
553 | { | ||
554 | int ret; | ||
555 | u8 intmask = 0; | ||
556 | struct tpm_chip *chip; | ||
557 | struct st33zp24_dev *tpm_dev; | ||
558 | |||
559 | chip = tpmm_chip_alloc(dev, &st33zp24_tpm); | ||
560 | if (IS_ERR(chip)) | ||
561 | return PTR_ERR(chip); | ||
562 | |||
563 | tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev), | ||
564 | GFP_KERNEL); | ||
565 | if (!tpm_dev) | ||
566 | return -ENOMEM; | ||
567 | |||
568 | TPM_VPRIV(chip) = tpm_dev; | ||
569 | tpm_dev->phy_id = phy_id; | ||
570 | tpm_dev->ops = ops; | ||
571 | |||
572 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
573 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); | ||
574 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
575 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
576 | |||
577 | chip->vendor.locality = LOCALITY0; | ||
578 | |||
579 | if (irq) { | ||
580 | /* INTERRUPT Setup */ | ||
581 | init_waitqueue_head(&chip->vendor.read_queue); | ||
582 | tpm_dev->intrs = 0; | ||
583 | |||
584 | if (request_locality(chip) != LOCALITY0) { | ||
585 | ret = -ENODEV; | ||
586 | goto _tpm_clean_answer; | ||
587 | } | ||
588 | |||
589 | clear_interruption(tpm_dev); | ||
590 | ret = devm_request_irq(dev, irq, tpm_ioserirq_handler, | ||
591 | IRQF_TRIGGER_HIGH, "TPM SERIRQ management", | ||
592 | chip); | ||
593 | if (ret < 0) { | ||
594 | dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n", | ||
595 | irq); | ||
596 | goto _tpm_clean_answer; | ||
597 | } | ||
598 | |||
599 | intmask |= TPM_INTF_CMD_READY_INT | ||
600 | | TPM_INTF_STS_VALID_INT | ||
601 | | TPM_INTF_DATA_AVAIL_INT; | ||
602 | |||
603 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE, | ||
604 | &intmask, 1); | ||
605 | if (ret < 0) | ||
606 | goto _tpm_clean_answer; | ||
607 | |||
608 | intmask = TPM_GLOBAL_INT_ENABLE; | ||
609 | ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3), | ||
610 | &intmask, 1); | ||
611 | if (ret < 0) | ||
612 | goto _tpm_clean_answer; | ||
613 | |||
614 | chip->vendor.irq = irq; | ||
615 | |||
616 | disable_irq_nosync(chip->vendor.irq); | ||
617 | |||
618 | tpm_gen_interrupt(chip); | ||
619 | } | ||
620 | |||
621 | tpm_get_timeouts(chip); | ||
622 | tpm_do_selftest(chip); | ||
623 | |||
624 | return tpm_chip_register(chip); | ||
625 | _tpm_clean_answer: | ||
626 | dev_info(&chip->dev, "TPM initialization fail\n"); | ||
627 | return ret; | ||
628 | } | ||
629 | EXPORT_SYMBOL(st33zp24_probe); | ||
630 | |||
631 | /* | ||
632 | * st33zp24_remove remove the TPM device | ||
633 | * @param: tpm_data, the tpm phy. | ||
634 | * @return: 0 in case of success. | ||
635 | */ | ||
636 | int st33zp24_remove(struct tpm_chip *chip) | ||
637 | { | ||
638 | tpm_chip_unregister(chip); | ||
639 | return 0; | ||
640 | } | ||
641 | EXPORT_SYMBOL(st33zp24_remove); | ||
642 | |||
643 | #ifdef CONFIG_PM_SLEEP | ||
644 | /* | ||
645 | * st33zp24_pm_suspend suspend the TPM device | ||
646 | * @param: tpm_data, the tpm phy. | ||
647 | * @param: mesg, the power management message. | ||
648 | * @return: 0 in case of success. | ||
649 | */ | ||
650 | int st33zp24_pm_suspend(struct device *dev) | ||
651 | { | ||
652 | struct tpm_chip *chip = dev_get_drvdata(dev); | ||
653 | struct st33zp24_dev *tpm_dev; | ||
654 | int ret = 0; | ||
655 | |||
656 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
657 | |||
658 | if (gpio_is_valid(tpm_dev->io_lpcpd)) | ||
659 | gpio_set_value(tpm_dev->io_lpcpd, 0); | ||
660 | else | ||
661 | ret = tpm_pm_suspend(dev); | ||
662 | |||
663 | return ret; | ||
664 | } /* st33zp24_pm_suspend() */ | ||
665 | EXPORT_SYMBOL(st33zp24_pm_suspend); | ||
666 | |||
667 | /* | ||
668 | * st33zp24_pm_resume resume the TPM device | ||
669 | * @param: tpm_data, the tpm phy. | ||
670 | * @return: 0 in case of success. | ||
671 | */ | ||
672 | int st33zp24_pm_resume(struct device *dev) | ||
673 | { | ||
674 | struct tpm_chip *chip = dev_get_drvdata(dev); | ||
675 | struct st33zp24_dev *tpm_dev; | ||
676 | int ret = 0; | ||
677 | |||
678 | tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); | ||
679 | |||
680 | if (gpio_is_valid(tpm_dev->io_lpcpd)) { | ||
681 | gpio_set_value(tpm_dev->io_lpcpd, 1); | ||
682 | ret = wait_for_stat(chip, | ||
683 | TPM_STS_VALID, chip->vendor.timeout_b, | ||
684 | &chip->vendor.read_queue, false); | ||
685 | } else { | ||
686 | ret = tpm_pm_resume(dev); | ||
687 | if (!ret) | ||
688 | tpm_do_selftest(chip); | ||
689 | } | ||
690 | return ret; | ||
691 | } /* st33zp24_pm_resume() */ | ||
692 | EXPORT_SYMBOL(st33zp24_pm_resume); | ||
693 | #endif | ||
694 | |||
695 | MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)"); | ||
696 | MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver"); | ||
697 | MODULE_VERSION("1.3.0"); | ||
698 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/char/tpm/st33zp24/st33zp24.h b/drivers/char/tpm/st33zp24/st33zp24.h new file mode 100644 index 000000000000..c207cebf67dd --- /dev/null +++ b/drivers/char/tpm/st33zp24/st33zp24.h | |||
@@ -0,0 +1,37 @@ | |||
1 | /* | ||
2 | * STMicroelectronics TPM Linux driver for TPM ST33ZP24 | ||
3 | * Copyright (C) 2009 - 2015 STMicroelectronics | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify it | ||
6 | * under the terms and conditions of the GNU General Public License, | ||
7 | * version 2, as published by the Free Software Foundation. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | #ifndef __LOCAL_ST33ZP24_H__ | ||
19 | #define __LOCAL_ST33ZP24_H__ | ||
20 | |||
21 | #define TPM_WRITE_DIRECTION 0x80 | ||
22 | #define TPM_BUFSIZE 2048 | ||
23 | |||
24 | struct st33zp24_phy_ops { | ||
25 | int (*send)(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size); | ||
26 | int (*recv)(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size); | ||
27 | }; | ||
28 | |||
29 | #ifdef CONFIG_PM_SLEEP | ||
30 | int st33zp24_pm_suspend(struct device *dev); | ||
31 | int st33zp24_pm_resume(struct device *dev); | ||
32 | #endif | ||
33 | |||
34 | int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops, | ||
35 | struct device *dev, int irq, int io_lpcpd); | ||
36 | int st33zp24_remove(struct tpm_chip *chip); | ||
37 | #endif /* __LOCAL_ST33ZP24_H__ */ | ||
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index e096e9cddb40..283f00a7f036 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c | |||
@@ -170,6 +170,41 @@ static void tpm_dev_del_device(struct tpm_chip *chip) | |||
170 | device_unregister(&chip->dev); | 170 | device_unregister(&chip->dev); |
171 | } | 171 | } |
172 | 172 | ||
173 | static int tpm1_chip_register(struct tpm_chip *chip) | ||
174 | { | ||
175 | int rc; | ||
176 | |||
177 | if (chip->flags & TPM_CHIP_FLAG_TPM2) | ||
178 | return 0; | ||
179 | |||
180 | rc = tpm_sysfs_add_device(chip); | ||
181 | if (rc) | ||
182 | return rc; | ||
183 | |||
184 | rc = tpm_add_ppi(chip); | ||
185 | if (rc) { | ||
186 | tpm_sysfs_del_device(chip); | ||
187 | return rc; | ||
188 | } | ||
189 | |||
190 | chip->bios_dir = tpm_bios_log_setup(chip->devname); | ||
191 | |||
192 | return 0; | ||
193 | } | ||
194 | |||
195 | static void tpm1_chip_unregister(struct tpm_chip *chip) | ||
196 | { | ||
197 | if (chip->flags & TPM_CHIP_FLAG_TPM2) | ||
198 | return; | ||
199 | |||
200 | if (chip->bios_dir) | ||
201 | tpm_bios_log_teardown(chip->bios_dir); | ||
202 | |||
203 | tpm_remove_ppi(chip); | ||
204 | |||
205 | tpm_sysfs_del_device(chip); | ||
206 | } | ||
207 | |||
173 | /* | 208 | /* |
174 | * tpm_chip_register() - create a character device for the TPM chip | 209 | * tpm_chip_register() - create a character device for the TPM chip |
175 | * @chip: TPM chip to use. | 210 | * @chip: TPM chip to use. |
@@ -185,22 +220,13 @@ int tpm_chip_register(struct tpm_chip *chip) | |||
185 | { | 220 | { |
186 | int rc; | 221 | int rc; |
187 | 222 | ||
188 | /* Populate sysfs for TPM1 devices. */ | 223 | rc = tpm1_chip_register(chip); |
189 | if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { | 224 | if (rc) |
190 | rc = tpm_sysfs_add_device(chip); | 225 | return rc; |
191 | if (rc) | ||
192 | goto del_misc; | ||
193 | |||
194 | rc = tpm_add_ppi(chip); | ||
195 | if (rc) | ||
196 | goto del_sysfs; | ||
197 | |||
198 | chip->bios_dir = tpm_bios_log_setup(chip->devname); | ||
199 | } | ||
200 | 226 | ||
201 | rc = tpm_dev_add_device(chip); | 227 | rc = tpm_dev_add_device(chip); |
202 | if (rc) | 228 | if (rc) |
203 | return rc; | 229 | goto out_err; |
204 | 230 | ||
205 | /* Make the chip available. */ | 231 | /* Make the chip available. */ |
206 | spin_lock(&driver_lock); | 232 | spin_lock(&driver_lock); |
@@ -210,10 +236,8 @@ int tpm_chip_register(struct tpm_chip *chip) | |||
210 | chip->flags |= TPM_CHIP_FLAG_REGISTERED; | 236 | chip->flags |= TPM_CHIP_FLAG_REGISTERED; |
211 | 237 | ||
212 | return 0; | 238 | return 0; |
213 | del_sysfs: | 239 | out_err: |
214 | tpm_sysfs_del_device(chip); | 240 | tpm1_chip_unregister(chip); |
215 | del_misc: | ||
216 | tpm_dev_del_device(chip); | ||
217 | return rc; | 241 | return rc; |
218 | } | 242 | } |
219 | EXPORT_SYMBOL_GPL(tpm_chip_register); | 243 | EXPORT_SYMBOL_GPL(tpm_chip_register); |
@@ -238,13 +262,7 @@ void tpm_chip_unregister(struct tpm_chip *chip) | |||
238 | spin_unlock(&driver_lock); | 262 | spin_unlock(&driver_lock); |
239 | synchronize_rcu(); | 263 | synchronize_rcu(); |
240 | 264 | ||
241 | if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { | 265 | tpm1_chip_unregister(chip); |
242 | if (chip->bios_dir) | ||
243 | tpm_bios_log_teardown(chip->bios_dir); | ||
244 | tpm_remove_ppi(chip); | ||
245 | tpm_sysfs_del_device(chip); | ||
246 | } | ||
247 | |||
248 | tpm_dev_del_device(chip); | 266 | tpm_dev_del_device(chip); |
249 | } | 267 | } |
250 | EXPORT_SYMBOL_GPL(tpm_chip_unregister); | 268 | EXPORT_SYMBOL_GPL(tpm_chip_unregister); |
diff --git a/drivers/char/tpm/tpm_i2c_stm_st33.c b/drivers/char/tpm/tpm_i2c_stm_st33.c deleted file mode 100644 index 612845b36c29..000000000000 --- a/drivers/char/tpm/tpm_i2c_stm_st33.c +++ /dev/null | |||
@@ -1,911 +0,0 @@ | |||
1 | /* | ||
2 | * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24 | ||
3 | * Copyright (C) 2009, 2010, 2014 STMicroelectronics | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * STMicroelectronics version 1.2.1, Copyright (C) 2014 | ||
19 | * STMicroelectronics comes with ABSOLUTELY NO WARRANTY. | ||
20 | * This is free software, and you are welcome to redistribute it | ||
21 | * under certain conditions. | ||
22 | * | ||
23 | * @Author: Christophe RICARD tpmsupport@st.com | ||
24 | * | ||
25 | * @File: tpm_stm_st33_i2c.c | ||
26 | * | ||
27 | * @Synopsis: | ||
28 | * 09/15/2010: First shot driver tpm_tis driver for | ||
29 | * lpc is used as model. | ||
30 | */ | ||
31 | |||
32 | #include <linux/pci.h> | ||
33 | #include <linux/module.h> | ||
34 | #include <linux/platform_device.h> | ||
35 | #include <linux/i2c.h> | ||
36 | #include <linux/fs.h> | ||
37 | #include <linux/miscdevice.h> | ||
38 | #include <linux/kernel.h> | ||
39 | #include <linux/delay.h> | ||
40 | #include <linux/wait.h> | ||
41 | #include <linux/freezer.h> | ||
42 | #include <linux/string.h> | ||
43 | #include <linux/interrupt.h> | ||
44 | #include <linux/sysfs.h> | ||
45 | #include <linux/gpio.h> | ||
46 | #include <linux/sched.h> | ||
47 | #include <linux/uaccess.h> | ||
48 | #include <linux/io.h> | ||
49 | #include <linux/slab.h> | ||
50 | #include <linux/of_irq.h> | ||
51 | #include <linux/of_gpio.h> | ||
52 | |||
53 | #include <linux/platform_data/tpm_stm_st33.h> | ||
54 | #include "tpm.h" | ||
55 | |||
56 | #define TPM_ACCESS 0x0 | ||
57 | #define TPM_STS 0x18 | ||
58 | #define TPM_HASH_END 0x20 | ||
59 | #define TPM_DATA_FIFO 0x24 | ||
60 | #define TPM_HASH_DATA 0x24 | ||
61 | #define TPM_HASH_START 0x28 | ||
62 | #define TPM_INTF_CAPABILITY 0x14 | ||
63 | #define TPM_INT_STATUS 0x10 | ||
64 | #define TPM_INT_ENABLE 0x08 | ||
65 | |||
66 | #define TPM_DUMMY_BYTE 0xAA | ||
67 | #define TPM_WRITE_DIRECTION 0x80 | ||
68 | #define TPM_HEADER_SIZE 10 | ||
69 | #define TPM_BUFSIZE 2048 | ||
70 | |||
71 | #define LOCALITY0 0 | ||
72 | |||
73 | |||
74 | enum stm33zp24_access { | ||
75 | TPM_ACCESS_VALID = 0x80, | ||
76 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, | ||
77 | TPM_ACCESS_REQUEST_PENDING = 0x04, | ||
78 | TPM_ACCESS_REQUEST_USE = 0x02, | ||
79 | }; | ||
80 | |||
81 | enum stm33zp24_status { | ||
82 | TPM_STS_VALID = 0x80, | ||
83 | TPM_STS_COMMAND_READY = 0x40, | ||
84 | TPM_STS_GO = 0x20, | ||
85 | TPM_STS_DATA_AVAIL = 0x10, | ||
86 | TPM_STS_DATA_EXPECT = 0x08, | ||
87 | }; | ||
88 | |||
89 | enum stm33zp24_int_flags { | ||
90 | TPM_GLOBAL_INT_ENABLE = 0x80, | ||
91 | TPM_INTF_CMD_READY_INT = 0x080, | ||
92 | TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, | ||
93 | TPM_INTF_WAKE_UP_READY_INT = 0x020, | ||
94 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, | ||
95 | TPM_INTF_STS_VALID_INT = 0x002, | ||
96 | TPM_INTF_DATA_AVAIL_INT = 0x001, | ||
97 | }; | ||
98 | |||
99 | enum tis_defaults { | ||
100 | TIS_SHORT_TIMEOUT = 750, | ||
101 | TIS_LONG_TIMEOUT = 2000, | ||
102 | }; | ||
103 | |||
104 | struct tpm_stm_dev { | ||
105 | struct i2c_client *client; | ||
106 | struct tpm_chip *chip; | ||
107 | u8 buf[TPM_BUFSIZE + 1]; | ||
108 | u32 intrs; | ||
109 | int io_lpcpd; | ||
110 | }; | ||
111 | |||
112 | /* | ||
113 | * write8_reg | ||
114 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | ||
115 | * @param: tpm_register, the tpm tis register where the data should be written | ||
116 | * @param: tpm_data, the tpm_data to write inside the tpm_register | ||
117 | * @param: tpm_size, The length of the data | ||
118 | * @return: Returns negative errno, or else the number of bytes written. | ||
119 | */ | ||
120 | static int write8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register, | ||
121 | u8 *tpm_data, u16 tpm_size) | ||
122 | { | ||
123 | tpm_dev->buf[0] = tpm_register; | ||
124 | memcpy(tpm_dev->buf + 1, tpm_data, tpm_size); | ||
125 | return i2c_master_send(tpm_dev->client, tpm_dev->buf, tpm_size + 1); | ||
126 | } /* write8_reg() */ | ||
127 | |||
128 | /* | ||
129 | * read8_reg | ||
130 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | ||
131 | * @param: tpm_register, the tpm tis register where the data should be read | ||
132 | * @param: tpm_data, the TPM response | ||
133 | * @param: tpm_size, tpm TPM response size to read. | ||
134 | * @return: number of byte read successfully: should be one if success. | ||
135 | */ | ||
136 | static int read8_reg(struct tpm_stm_dev *tpm_dev, u8 tpm_register, | ||
137 | u8 *tpm_data, int tpm_size) | ||
138 | { | ||
139 | u8 status = 0; | ||
140 | u8 data; | ||
141 | |||
142 | data = TPM_DUMMY_BYTE; | ||
143 | status = write8_reg(tpm_dev, tpm_register, &data, 1); | ||
144 | if (status == 2) | ||
145 | status = i2c_master_recv(tpm_dev->client, tpm_data, tpm_size); | ||
146 | return status; | ||
147 | } /* read8_reg() */ | ||
148 | |||
149 | /* | ||
150 | * I2C_WRITE_DATA | ||
151 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | ||
152 | * @param: tpm_dev, the chip description | ||
153 | * @param: tpm_register, the tpm tis register where the data should be written | ||
154 | * @param: tpm_data, the tpm_data to write inside the tpm_register | ||
155 | * @param: tpm_size, The length of the data | ||
156 | * @return: number of byte written successfully: should be one if success. | ||
157 | */ | ||
158 | #define I2C_WRITE_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \ | ||
159 | (write8_reg(tpm_dev, tpm_register | \ | ||
160 | TPM_WRITE_DIRECTION, tpm_data, tpm_size)) | ||
161 | |||
162 | /* | ||
163 | * I2C_READ_DATA | ||
164 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | ||
165 | * @param: tpm_dev, the chip description | ||
166 | * @param: tpm_register, the tpm tis register where the data should be read | ||
167 | * @param: tpm_data, the TPM response | ||
168 | * @param: tpm_size, tpm TPM response size to read. | ||
169 | * @return: number of byte read successfully: should be one if success. | ||
170 | */ | ||
171 | #define I2C_READ_DATA(tpm_dev, tpm_register, tpm_data, tpm_size) \ | ||
172 | (read8_reg(tpm_dev, tpm_register, tpm_data, tpm_size)) | ||
173 | |||
174 | /* | ||
175 | * clear_interruption | ||
176 | * clear the TPM interrupt register. | ||
177 | * @param: tpm, the chip description | ||
178 | * @return: the TPM_INT_STATUS value | ||
179 | */ | ||
180 | static u8 clear_interruption(struct tpm_stm_dev *tpm_dev) | ||
181 | { | ||
182 | u8 interrupt; | ||
183 | |||
184 | I2C_READ_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1); | ||
185 | I2C_WRITE_DATA(tpm_dev, TPM_INT_STATUS, &interrupt, 1); | ||
186 | return interrupt; | ||
187 | } /* clear_interruption() */ | ||
188 | |||
189 | /* | ||
190 | * tpm_stm_i2c_cancel, cancel is not implemented. | ||
191 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | ||
192 | */ | ||
193 | static void tpm_stm_i2c_cancel(struct tpm_chip *chip) | ||
194 | { | ||
195 | struct tpm_stm_dev *tpm_dev; | ||
196 | u8 data; | ||
197 | |||
198 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
199 | |||
200 | data = TPM_STS_COMMAND_READY; | ||
201 | I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1); | ||
202 | } /* tpm_stm_i2c_cancel() */ | ||
203 | |||
204 | /* | ||
205 | * tpm_stm_spi_status return the TPM_STS register | ||
206 | * @param: chip, the tpm chip description | ||
207 | * @return: the TPM_STS register value. | ||
208 | */ | ||
209 | static u8 tpm_stm_i2c_status(struct tpm_chip *chip) | ||
210 | { | ||
211 | struct tpm_stm_dev *tpm_dev; | ||
212 | u8 data; | ||
213 | |||
214 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
215 | |||
216 | I2C_READ_DATA(tpm_dev, TPM_STS, &data, 1); | ||
217 | return data; | ||
218 | } /* tpm_stm_i2c_status() */ | ||
219 | |||
220 | |||
221 | /* | ||
222 | * check_locality if the locality is active | ||
223 | * @param: chip, the tpm chip description | ||
224 | * @return: the active locality or -EACCESS. | ||
225 | */ | ||
226 | static int check_locality(struct tpm_chip *chip) | ||
227 | { | ||
228 | struct tpm_stm_dev *tpm_dev; | ||
229 | u8 data; | ||
230 | u8 status; | ||
231 | |||
232 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
233 | |||
234 | status = I2C_READ_DATA(tpm_dev, TPM_ACCESS, &data, 1); | ||
235 | if (status && (data & | ||
236 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == | ||
237 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) | ||
238 | return chip->vendor.locality; | ||
239 | |||
240 | return -EACCES; | ||
241 | } /* check_locality() */ | ||
242 | |||
243 | /* | ||
244 | * request_locality request the TPM locality | ||
245 | * @param: chip, the chip description | ||
246 | * @return: the active locality or EACCESS. | ||
247 | */ | ||
248 | static int request_locality(struct tpm_chip *chip) | ||
249 | { | ||
250 | unsigned long stop; | ||
251 | long ret; | ||
252 | struct tpm_stm_dev *tpm_dev; | ||
253 | u8 data; | ||
254 | |||
255 | if (check_locality(chip) == chip->vendor.locality) | ||
256 | return chip->vendor.locality; | ||
257 | |||
258 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
259 | |||
260 | data = TPM_ACCESS_REQUEST_USE; | ||
261 | ret = I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1); | ||
262 | if (ret < 0) | ||
263 | goto end; | ||
264 | |||
265 | stop = jiffies + chip->vendor.timeout_a; | ||
266 | |||
267 | /* Request locality is usually effective after the request */ | ||
268 | do { | ||
269 | if (check_locality(chip) >= 0) | ||
270 | return chip->vendor.locality; | ||
271 | msleep(TPM_TIMEOUT); | ||
272 | } while (time_before(jiffies, stop)); | ||
273 | ret = -EACCES; | ||
274 | end: | ||
275 | return ret; | ||
276 | } /* request_locality() */ | ||
277 | |||
278 | /* | ||
279 | * release_locality release the active locality | ||
280 | * @param: chip, the tpm chip description. | ||
281 | */ | ||
282 | static void release_locality(struct tpm_chip *chip) | ||
283 | { | ||
284 | struct tpm_stm_dev *tpm_dev; | ||
285 | u8 data; | ||
286 | |||
287 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
288 | data = TPM_ACCESS_ACTIVE_LOCALITY; | ||
289 | |||
290 | I2C_WRITE_DATA(tpm_dev, TPM_ACCESS, &data, 1); | ||
291 | } | ||
292 | |||
293 | /* | ||
294 | * get_burstcount return the burstcount address 0x19 0x1A | ||
295 | * @param: chip, the chip description | ||
296 | * return: the burstcount. | ||
297 | */ | ||
298 | static int get_burstcount(struct tpm_chip *chip) | ||
299 | { | ||
300 | unsigned long stop; | ||
301 | int burstcnt, status; | ||
302 | u8 tpm_reg, temp; | ||
303 | struct tpm_stm_dev *tpm_dev; | ||
304 | |||
305 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
306 | |||
307 | stop = jiffies + chip->vendor.timeout_d; | ||
308 | do { | ||
309 | tpm_reg = TPM_STS + 1; | ||
310 | status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1); | ||
311 | if (status < 0) | ||
312 | goto end; | ||
313 | |||
314 | tpm_reg = tpm_reg + 1; | ||
315 | burstcnt = temp; | ||
316 | status = I2C_READ_DATA(tpm_dev, tpm_reg, &temp, 1); | ||
317 | if (status < 0) | ||
318 | goto end; | ||
319 | |||
320 | burstcnt |= temp << 8; | ||
321 | if (burstcnt) | ||
322 | return burstcnt; | ||
323 | msleep(TPM_TIMEOUT); | ||
324 | } while (time_before(jiffies, stop)); | ||
325 | |||
326 | end: | ||
327 | return -EBUSY; | ||
328 | } /* get_burstcount() */ | ||
329 | |||
330 | static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, | ||
331 | bool check_cancel, bool *canceled) | ||
332 | { | ||
333 | u8 status = chip->ops->status(chip); | ||
334 | |||
335 | *canceled = false; | ||
336 | if ((status & mask) == mask) | ||
337 | return true; | ||
338 | if (check_cancel && chip->ops->req_canceled(chip, status)) { | ||
339 | *canceled = true; | ||
340 | return true; | ||
341 | } | ||
342 | return false; | ||
343 | } | ||
344 | |||
345 | /* | ||
346 | * interrupt_to_status | ||
347 | * @param: irq_mask, the irq mask value to wait | ||
348 | * @return: the corresponding tpm_sts value | ||
349 | */ | ||
350 | static u8 interrupt_to_status(u8 irq_mask) | ||
351 | { | ||
352 | u8 status = 0; | ||
353 | |||
354 | if ((irq_mask & TPM_INTF_STS_VALID_INT) == TPM_INTF_STS_VALID_INT) | ||
355 | status |= TPM_STS_VALID; | ||
356 | if ((irq_mask & TPM_INTF_DATA_AVAIL_INT) == TPM_INTF_DATA_AVAIL_INT) | ||
357 | status |= TPM_STS_DATA_AVAIL; | ||
358 | if ((irq_mask & TPM_INTF_CMD_READY_INT) == TPM_INTF_CMD_READY_INT) | ||
359 | status |= TPM_STS_COMMAND_READY; | ||
360 | |||
361 | return status; | ||
362 | } /* status_to_interrupt() */ | ||
363 | |||
364 | /* | ||
365 | * wait_for_stat wait for a TPM_STS value | ||
366 | * @param: chip, the tpm chip description | ||
367 | * @param: mask, the value mask to wait | ||
368 | * @param: timeout, the timeout | ||
369 | * @param: queue, the wait queue. | ||
370 | * @param: check_cancel, does the command can be cancelled ? | ||
371 | * @return: the tpm status, 0 if success, -ETIME if timeout is reached. | ||
372 | */ | ||
373 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, | ||
374 | wait_queue_head_t *queue, bool check_cancel) | ||
375 | { | ||
376 | unsigned long stop; | ||
377 | int ret; | ||
378 | bool canceled = false; | ||
379 | bool condition; | ||
380 | u32 cur_intrs; | ||
381 | u8 interrupt, status; | ||
382 | struct tpm_stm_dev *tpm_dev; | ||
383 | |||
384 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
385 | |||
386 | /* check current status */ | ||
387 | status = tpm_stm_i2c_status(chip); | ||
388 | if ((status & mask) == mask) | ||
389 | return 0; | ||
390 | |||
391 | stop = jiffies + timeout; | ||
392 | |||
393 | if (chip->vendor.irq) { | ||
394 | cur_intrs = tpm_dev->intrs; | ||
395 | interrupt = clear_interruption(tpm_dev); | ||
396 | enable_irq(chip->vendor.irq); | ||
397 | |||
398 | again: | ||
399 | timeout = stop - jiffies; | ||
400 | if ((long) timeout <= 0) | ||
401 | return -1; | ||
402 | |||
403 | ret = wait_event_interruptible_timeout(*queue, | ||
404 | cur_intrs != tpm_dev->intrs, timeout); | ||
405 | |||
406 | interrupt |= clear_interruption(tpm_dev); | ||
407 | status = interrupt_to_status(interrupt); | ||
408 | condition = wait_for_tpm_stat_cond(chip, mask, | ||
409 | check_cancel, &canceled); | ||
410 | |||
411 | if (ret >= 0 && condition) { | ||
412 | if (canceled) | ||
413 | return -ECANCELED; | ||
414 | return 0; | ||
415 | } | ||
416 | if (ret == -ERESTARTSYS && freezing(current)) { | ||
417 | clear_thread_flag(TIF_SIGPENDING); | ||
418 | goto again; | ||
419 | } | ||
420 | disable_irq_nosync(chip->vendor.irq); | ||
421 | |||
422 | } else { | ||
423 | do { | ||
424 | msleep(TPM_TIMEOUT); | ||
425 | status = chip->ops->status(chip); | ||
426 | if ((status & mask) == mask) | ||
427 | return 0; | ||
428 | } while (time_before(jiffies, stop)); | ||
429 | } | ||
430 | |||
431 | return -ETIME; | ||
432 | } /* wait_for_stat() */ | ||
433 | |||
434 | /* | ||
435 | * recv_data receive data | ||
436 | * @param: chip, the tpm chip description | ||
437 | * @param: buf, the buffer where the data are received | ||
438 | * @param: count, the number of data to receive | ||
439 | * @return: the number of bytes read from TPM FIFO. | ||
440 | */ | ||
441 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) | ||
442 | { | ||
443 | int size = 0, burstcnt, len, ret; | ||
444 | struct tpm_stm_dev *tpm_dev; | ||
445 | |||
446 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
447 | |||
448 | while (size < count && | ||
449 | wait_for_stat(chip, | ||
450 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
451 | chip->vendor.timeout_c, | ||
452 | &chip->vendor.read_queue, true) == 0) { | ||
453 | burstcnt = get_burstcount(chip); | ||
454 | if (burstcnt < 0) | ||
455 | return burstcnt; | ||
456 | len = min_t(int, burstcnt, count - size); | ||
457 | ret = I2C_READ_DATA(tpm_dev, TPM_DATA_FIFO, buf + size, len); | ||
458 | if (ret < 0) | ||
459 | return ret; | ||
460 | |||
461 | size += len; | ||
462 | } | ||
463 | return size; | ||
464 | } | ||
465 | |||
466 | /* | ||
467 | * tpm_ioserirq_handler the serirq irq handler | ||
468 | * @param: irq, the tpm chip description | ||
469 | * @param: dev_id, the description of the chip | ||
470 | * @return: the status of the handler. | ||
471 | */ | ||
472 | static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) | ||
473 | { | ||
474 | struct tpm_chip *chip = dev_id; | ||
475 | struct tpm_stm_dev *tpm_dev; | ||
476 | |||
477 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
478 | |||
479 | tpm_dev->intrs++; | ||
480 | wake_up_interruptible(&chip->vendor.read_queue); | ||
481 | disable_irq_nosync(chip->vendor.irq); | ||
482 | |||
483 | return IRQ_HANDLED; | ||
484 | } /* tpm_ioserirq_handler() */ | ||
485 | |||
486 | |||
487 | /* | ||
488 | * tpm_stm_i2c_send send TPM commands through the I2C bus. | ||
489 | * | ||
490 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | ||
491 | * @param: buf, the buffer to send. | ||
492 | * @param: count, the number of bytes to send. | ||
493 | * @return: In case of success the number of bytes sent. | ||
494 | * In other case, a < 0 value describing the issue. | ||
495 | */ | ||
496 | static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, | ||
497 | size_t len) | ||
498 | { | ||
499 | u32 status, i, size; | ||
500 | int burstcnt = 0; | ||
501 | int ret; | ||
502 | u8 data; | ||
503 | struct i2c_client *client; | ||
504 | struct tpm_stm_dev *tpm_dev; | ||
505 | |||
506 | if (!chip) | ||
507 | return -EBUSY; | ||
508 | if (len < TPM_HEADER_SIZE) | ||
509 | return -EBUSY; | ||
510 | |||
511 | tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
512 | client = tpm_dev->client; | ||
513 | |||
514 | client->flags = 0; | ||
515 | |||
516 | ret = request_locality(chip); | ||
517 | if (ret < 0) | ||
518 | return ret; | ||
519 | |||
520 | status = tpm_stm_i2c_status(chip); | ||
521 | if ((status & TPM_STS_COMMAND_READY) == 0) { | ||
522 | tpm_stm_i2c_cancel(chip); | ||
523 | if (wait_for_stat | ||
524 | (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, | ||
525 | &chip->vendor.read_queue, false) < 0) { | ||
526 | ret = -ETIME; | ||
527 | goto out_err; | ||
528 | } | ||
529 | } | ||
530 | |||
531 | for (i = 0; i < len - 1;) { | ||
532 | burstcnt = get_burstcount(chip); | ||
533 | if (burstcnt < 0) | ||
534 | return burstcnt; | ||
535 | size = min_t(int, len - i - 1, burstcnt); | ||
536 | ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + i, size); | ||
537 | if (ret < 0) | ||
538 | goto out_err; | ||
539 | |||
540 | i += size; | ||
541 | } | ||
542 | |||
543 | status = tpm_stm_i2c_status(chip); | ||
544 | if ((status & TPM_STS_DATA_EXPECT) == 0) { | ||
545 | ret = -EIO; | ||
546 | goto out_err; | ||
547 | } | ||
548 | |||
549 | ret = I2C_WRITE_DATA(tpm_dev, TPM_DATA_FIFO, buf + len - 1, 1); | ||
550 | if (ret < 0) | ||
551 | goto out_err; | ||
552 | |||
553 | status = tpm_stm_i2c_status(chip); | ||
554 | if ((status & TPM_STS_DATA_EXPECT) != 0) { | ||
555 | ret = -EIO; | ||
556 | goto out_err; | ||
557 | } | ||
558 | |||
559 | data = TPM_STS_GO; | ||
560 | I2C_WRITE_DATA(tpm_dev, TPM_STS, &data, 1); | ||
561 | |||
562 | return len; | ||
563 | out_err: | ||
564 | tpm_stm_i2c_cancel(chip); | ||
565 | release_locality(chip); | ||
566 | return ret; | ||
567 | } | ||
568 | |||
569 | /* | ||
570 | * tpm_stm_i2c_recv received TPM response through the I2C bus. | ||
571 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. | ||
572 | * @param: buf, the buffer to store datas. | ||
573 | * @param: count, the number of bytes to send. | ||
574 | * @return: In case of success the number of bytes received. | ||
575 | * In other case, a < 0 value describing the issue. | ||
576 | */ | ||
577 | static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, | ||
578 | size_t count) | ||
579 | { | ||
580 | int size = 0; | ||
581 | int expected; | ||
582 | |||
583 | if (!chip) | ||
584 | return -EBUSY; | ||
585 | |||
586 | if (count < TPM_HEADER_SIZE) { | ||
587 | size = -EIO; | ||
588 | goto out; | ||
589 | } | ||
590 | |||
591 | size = recv_data(chip, buf, TPM_HEADER_SIZE); | ||
592 | if (size < TPM_HEADER_SIZE) { | ||
593 | dev_err(chip->pdev, "Unable to read header\n"); | ||
594 | goto out; | ||
595 | } | ||
596 | |||
597 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); | ||
598 | if (expected > count) { | ||
599 | size = -EIO; | ||
600 | goto out; | ||
601 | } | ||
602 | |||
603 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], | ||
604 | expected - TPM_HEADER_SIZE); | ||
605 | if (size < expected) { | ||
606 | dev_err(chip->pdev, "Unable to read remainder of result\n"); | ||
607 | size = -ETIME; | ||
608 | goto out; | ||
609 | } | ||
610 | |||
611 | out: | ||
612 | chip->ops->cancel(chip); | ||
613 | release_locality(chip); | ||
614 | return size; | ||
615 | } | ||
616 | |||
617 | static bool tpm_stm_i2c_req_canceled(struct tpm_chip *chip, u8 status) | ||
618 | { | ||
619 | return (status == TPM_STS_COMMAND_READY); | ||
620 | } | ||
621 | |||
622 | static const struct tpm_class_ops st_i2c_tpm = { | ||
623 | .send = tpm_stm_i2c_send, | ||
624 | .recv = tpm_stm_i2c_recv, | ||
625 | .cancel = tpm_stm_i2c_cancel, | ||
626 | .status = tpm_stm_i2c_status, | ||
627 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
628 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | ||
629 | .req_canceled = tpm_stm_i2c_req_canceled, | ||
630 | }; | ||
631 | |||
632 | #ifdef CONFIG_OF | ||
633 | static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip) | ||
634 | { | ||
635 | struct device_node *pp; | ||
636 | struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
637 | struct i2c_client *client = tpm_dev->client; | ||
638 | int gpio; | ||
639 | int ret; | ||
640 | |||
641 | pp = client->dev.of_node; | ||
642 | if (!pp) { | ||
643 | dev_err(chip->pdev, "No platform data\n"); | ||
644 | return -ENODEV; | ||
645 | } | ||
646 | |||
647 | /* Get GPIO from device tree */ | ||
648 | gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0); | ||
649 | if (gpio < 0) { | ||
650 | dev_err(chip->pdev, "Failed to retrieve lpcpd-gpios from dts.\n"); | ||
651 | tpm_dev->io_lpcpd = -1; | ||
652 | /* | ||
653 | * lpcpd pin is not specified. This is not an issue as | ||
654 | * power management can be also managed by TPM specific | ||
655 | * commands. So leave with a success status code. | ||
656 | */ | ||
657 | return 0; | ||
658 | } | ||
659 | /* GPIO request and configuration */ | ||
660 | ret = devm_gpio_request_one(&client->dev, gpio, | ||
661 | GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD"); | ||
662 | if (ret) { | ||
663 | dev_err(chip->pdev, "Failed to request lpcpd pin\n"); | ||
664 | return -ENODEV; | ||
665 | } | ||
666 | tpm_dev->io_lpcpd = gpio; | ||
667 | |||
668 | return 0; | ||
669 | } | ||
670 | #else | ||
671 | static int tpm_stm_i2c_of_request_resources(struct tpm_chip *chip) | ||
672 | { | ||
673 | return -ENODEV; | ||
674 | } | ||
675 | #endif | ||
676 | |||
677 | static int tpm_stm_i2c_request_resources(struct i2c_client *client, | ||
678 | struct tpm_chip *chip) | ||
679 | { | ||
680 | struct st33zp24_platform_data *pdata; | ||
681 | struct tpm_stm_dev *tpm_dev = (struct tpm_stm_dev *)TPM_VPRIV(chip); | ||
682 | int ret; | ||
683 | |||
684 | pdata = client->dev.platform_data; | ||
685 | if (!pdata) { | ||
686 | dev_err(chip->pdev, "No platform data\n"); | ||
687 | return -ENODEV; | ||
688 | } | ||
689 | |||
690 | /* store for late use */ | ||
691 | tpm_dev->io_lpcpd = pdata->io_lpcpd; | ||
692 | |||
693 | if (gpio_is_valid(pdata->io_lpcpd)) { | ||
694 | ret = devm_gpio_request_one(&client->dev, | ||
695 | pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH, | ||
696 | "TPM IO_LPCPD"); | ||
697 | if (ret) { | ||
698 | dev_err(chip->pdev, "%s : reset gpio_request failed\n", | ||
699 | __FILE__); | ||
700 | return ret; | ||
701 | } | ||
702 | } | ||
703 | |||
704 | return 0; | ||
705 | } | ||
706 | |||
707 | /* | ||
708 | * tpm_stm_i2c_probe initialize the TPM device | ||
709 | * @param: client, the i2c_client drescription (TPM I2C description). | ||
710 | * @param: id, the i2c_device_id struct. | ||
711 | * @return: 0 in case of success. | ||
712 | * -1 in other case. | ||
713 | */ | ||
714 | static int | ||
715 | tpm_stm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) | ||
716 | { | ||
717 | int ret; | ||
718 | u8 intmask = 0; | ||
719 | struct tpm_chip *chip; | ||
720 | struct st33zp24_platform_data *platform_data; | ||
721 | struct tpm_stm_dev *tpm_dev; | ||
722 | |||
723 | if (!client) { | ||
724 | pr_info("%s: i2c client is NULL. Device not accessible.\n", | ||
725 | __func__); | ||
726 | return -ENODEV; | ||
727 | } | ||
728 | |||
729 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
730 | dev_info(&client->dev, "client not i2c capable\n"); | ||
731 | return -ENODEV; | ||
732 | } | ||
733 | |||
734 | tpm_dev = devm_kzalloc(&client->dev, sizeof(struct tpm_stm_dev), | ||
735 | GFP_KERNEL); | ||
736 | if (!tpm_dev) | ||
737 | return -ENOMEM; | ||
738 | |||
739 | chip = tpmm_chip_alloc(&client->dev, &st_i2c_tpm); | ||
740 | if (IS_ERR(chip)) | ||
741 | return PTR_ERR(chip); | ||
742 | |||
743 | TPM_VPRIV(chip) = tpm_dev; | ||
744 | tpm_dev->client = client; | ||
745 | |||
746 | platform_data = client->dev.platform_data; | ||
747 | if (!platform_data && client->dev.of_node) { | ||
748 | ret = tpm_stm_i2c_of_request_resources(chip); | ||
749 | if (ret) | ||
750 | goto _tpm_clean_answer; | ||
751 | } else if (platform_data) { | ||
752 | ret = tpm_stm_i2c_request_resources(client, chip); | ||
753 | if (ret) | ||
754 | goto _tpm_clean_answer; | ||
755 | } | ||
756 | |||
757 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
758 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); | ||
759 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
760 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | ||
761 | |||
762 | chip->vendor.locality = LOCALITY0; | ||
763 | |||
764 | if (client->irq) { | ||
765 | /* INTERRUPT Setup */ | ||
766 | init_waitqueue_head(&chip->vendor.read_queue); | ||
767 | tpm_dev->intrs = 0; | ||
768 | |||
769 | if (request_locality(chip) != LOCALITY0) { | ||
770 | ret = -ENODEV; | ||
771 | goto _tpm_clean_answer; | ||
772 | } | ||
773 | |||
774 | clear_interruption(tpm_dev); | ||
775 | ret = devm_request_irq(&client->dev, client->irq, | ||
776 | tpm_ioserirq_handler, | ||
777 | IRQF_TRIGGER_HIGH, | ||
778 | "TPM SERIRQ management", chip); | ||
779 | if (ret < 0) { | ||
780 | dev_err(chip->pdev, "TPM SERIRQ signals %d not available\n", | ||
781 | client->irq); | ||
782 | goto _tpm_clean_answer; | ||
783 | } | ||
784 | |||
785 | intmask |= TPM_INTF_CMD_READY_INT | ||
786 | | TPM_INTF_STS_VALID_INT | ||
787 | | TPM_INTF_DATA_AVAIL_INT; | ||
788 | |||
789 | ret = I2C_WRITE_DATA(tpm_dev, TPM_INT_ENABLE, &intmask, 1); | ||
790 | if (ret < 0) | ||
791 | goto _tpm_clean_answer; | ||
792 | |||
793 | intmask = TPM_GLOBAL_INT_ENABLE; | ||
794 | ret = I2C_WRITE_DATA(tpm_dev, (TPM_INT_ENABLE + 3), | ||
795 | &intmask, 1); | ||
796 | if (ret < 0) | ||
797 | goto _tpm_clean_answer; | ||
798 | |||
799 | chip->vendor.irq = client->irq; | ||
800 | |||
801 | disable_irq_nosync(chip->vendor.irq); | ||
802 | |||
803 | tpm_gen_interrupt(chip); | ||
804 | } | ||
805 | |||
806 | tpm_get_timeouts(chip); | ||
807 | tpm_do_selftest(chip); | ||
808 | |||
809 | return tpm_chip_register(chip); | ||
810 | _tpm_clean_answer: | ||
811 | dev_info(chip->pdev, "TPM I2C initialisation fail\n"); | ||
812 | return ret; | ||
813 | } | ||
814 | |||
815 | /* | ||
816 | * tpm_stm_i2c_remove remove the TPM device | ||
817 | * @param: client, the i2c_client description (TPM I2C description). | ||
818 | * @return: 0 in case of success. | ||
819 | */ | ||
820 | static int tpm_stm_i2c_remove(struct i2c_client *client) | ||
821 | { | ||
822 | struct tpm_chip *chip = | ||
823 | (struct tpm_chip *) i2c_get_clientdata(client); | ||
824 | |||
825 | if (chip) | ||
826 | tpm_chip_unregister(chip); | ||
827 | |||
828 | return 0; | ||
829 | } | ||
830 | |||
831 | #ifdef CONFIG_PM_SLEEP | ||
832 | /* | ||
833 | * tpm_stm_i2c_pm_suspend suspend the TPM device | ||
834 | * @param: client, the i2c_client drescription (TPM I2C description). | ||
835 | * @param: mesg, the power management message. | ||
836 | * @return: 0 in case of success. | ||
837 | */ | ||
838 | static int tpm_stm_i2c_pm_suspend(struct device *dev) | ||
839 | { | ||
840 | struct st33zp24_platform_data *pin_infos = dev->platform_data; | ||
841 | int ret = 0; | ||
842 | |||
843 | if (gpio_is_valid(pin_infos->io_lpcpd)) | ||
844 | gpio_set_value(pin_infos->io_lpcpd, 0); | ||
845 | else | ||
846 | ret = tpm_pm_suspend(dev); | ||
847 | |||
848 | return ret; | ||
849 | } /* tpm_stm_i2c_suspend() */ | ||
850 | |||
851 | /* | ||
852 | * tpm_stm_i2c_pm_resume resume the TPM device | ||
853 | * @param: client, the i2c_client drescription (TPM I2C description). | ||
854 | * @return: 0 in case of success. | ||
855 | */ | ||
856 | static int tpm_stm_i2c_pm_resume(struct device *dev) | ||
857 | { | ||
858 | struct tpm_chip *chip = dev_get_drvdata(dev); | ||
859 | struct st33zp24_platform_data *pin_infos = dev->platform_data; | ||
860 | |||
861 | int ret = 0; | ||
862 | |||
863 | if (gpio_is_valid(pin_infos->io_lpcpd)) { | ||
864 | gpio_set_value(pin_infos->io_lpcpd, 1); | ||
865 | ret = wait_for_stat(chip, | ||
866 | TPM_STS_VALID, chip->vendor.timeout_b, | ||
867 | &chip->vendor.read_queue, false); | ||
868 | } else { | ||
869 | ret = tpm_pm_resume(dev); | ||
870 | if (!ret) | ||
871 | tpm_do_selftest(chip); | ||
872 | } | ||
873 | return ret; | ||
874 | } /* tpm_stm_i2c_pm_resume() */ | ||
875 | #endif | ||
876 | |||
877 | static const struct i2c_device_id tpm_stm_i2c_id[] = { | ||
878 | {TPM_ST33_I2C, 0}, | ||
879 | {} | ||
880 | }; | ||
881 | MODULE_DEVICE_TABLE(i2c, tpm_stm_i2c_id); | ||
882 | |||
883 | #ifdef CONFIG_OF | ||
884 | static const struct of_device_id of_st33zp24_i2c_match[] = { | ||
885 | { .compatible = "st,st33zp24-i2c", }, | ||
886 | {} | ||
887 | }; | ||
888 | MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match); | ||
889 | #endif | ||
890 | |||
891 | static SIMPLE_DEV_PM_OPS(tpm_stm_i2c_ops, tpm_stm_i2c_pm_suspend, | ||
892 | tpm_stm_i2c_pm_resume); | ||
893 | |||
894 | static struct i2c_driver tpm_stm_i2c_driver = { | ||
895 | .driver = { | ||
896 | .owner = THIS_MODULE, | ||
897 | .name = TPM_ST33_I2C, | ||
898 | .pm = &tpm_stm_i2c_ops, | ||
899 | .of_match_table = of_match_ptr(of_st33zp24_i2c_match), | ||
900 | }, | ||
901 | .probe = tpm_stm_i2c_probe, | ||
902 | .remove = tpm_stm_i2c_remove, | ||
903 | .id_table = tpm_stm_i2c_id | ||
904 | }; | ||
905 | |||
906 | module_i2c_driver(tpm_stm_i2c_driver); | ||
907 | |||
908 | MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)"); | ||
909 | MODULE_DESCRIPTION("STM TPM I2C ST33 Driver"); | ||
910 | MODULE_VERSION("1.2.1"); | ||
911 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/char/tpm/tpm_infineon.c b/drivers/char/tpm/tpm_infineon.c index 29ba520ac24d..6c488e635fdd 100644 --- a/drivers/char/tpm/tpm_infineon.c +++ b/drivers/char/tpm/tpm_infineon.c | |||
@@ -591,27 +591,8 @@ static void tpm_inf_pnp_remove(struct pnp_dev *dev) | |||
591 | } | 591 | } |
592 | } | 592 | } |
593 | 593 | ||
594 | static int tpm_inf_pnp_suspend(struct pnp_dev *dev, pm_message_t pm_state) | 594 | #ifdef CONFIG_PM_SLEEP |
595 | { | 595 | static int tpm_inf_resume(struct device *dev) |
596 | struct tpm_chip *chip = pnp_get_drvdata(dev); | ||
597 | int rc; | ||
598 | if (chip) { | ||
599 | u8 savestate[] = { | ||
600 | 0, 193, /* TPM_TAG_RQU_COMMAND */ | ||
601 | 0, 0, 0, 10, /* blob length (in bytes) */ | ||
602 | 0, 0, 0, 152 /* TPM_ORD_SaveState */ | ||
603 | }; | ||
604 | dev_info(&dev->dev, "saving TPM state\n"); | ||
605 | rc = tpm_inf_send(chip, savestate, sizeof(savestate)); | ||
606 | if (rc < 0) { | ||
607 | dev_err(&dev->dev, "error while saving TPM state\n"); | ||
608 | return rc; | ||
609 | } | ||
610 | } | ||
611 | return 0; | ||
612 | } | ||
613 | |||
614 | static int tpm_inf_pnp_resume(struct pnp_dev *dev) | ||
615 | { | 596 | { |
616 | /* Re-configure TPM after suspending */ | 597 | /* Re-configure TPM after suspending */ |
617 | tpm_config_out(ENABLE_REGISTER_PAIR, TPM_INF_ADDR); | 598 | tpm_config_out(ENABLE_REGISTER_PAIR, TPM_INF_ADDR); |
@@ -625,16 +606,19 @@ static int tpm_inf_pnp_resume(struct pnp_dev *dev) | |||
625 | tpm_config_out(DISABLE_REGISTER_PAIR, TPM_INF_ADDR); | 606 | tpm_config_out(DISABLE_REGISTER_PAIR, TPM_INF_ADDR); |
626 | /* disable RESET, LP and IRQC */ | 607 | /* disable RESET, LP and IRQC */ |
627 | tpm_data_out(RESET_LP_IRQC_DISABLE, CMD); | 608 | tpm_data_out(RESET_LP_IRQC_DISABLE, CMD); |
628 | return tpm_pm_resume(&dev->dev); | 609 | return tpm_pm_resume(dev); |
629 | } | 610 | } |
611 | #endif | ||
612 | static SIMPLE_DEV_PM_OPS(tpm_inf_pm, tpm_pm_suspend, tpm_inf_resume); | ||
630 | 613 | ||
631 | static struct pnp_driver tpm_inf_pnp_driver = { | 614 | static struct pnp_driver tpm_inf_pnp_driver = { |
632 | .name = "tpm_inf_pnp", | 615 | .name = "tpm_inf_pnp", |
633 | .id_table = tpm_inf_pnp_tbl, | 616 | .id_table = tpm_inf_pnp_tbl, |
634 | .probe = tpm_inf_pnp_probe, | 617 | .probe = tpm_inf_pnp_probe, |
635 | .suspend = tpm_inf_pnp_suspend, | 618 | .remove = tpm_inf_pnp_remove, |
636 | .resume = tpm_inf_pnp_resume, | 619 | .driver = { |
637 | .remove = tpm_inf_pnp_remove | 620 | .pm = &tpm_inf_pm, |
621 | } | ||
638 | }; | 622 | }; |
639 | 623 | ||
640 | module_pnp_driver(tpm_inf_pnp_driver); | 624 | module_pnp_driver(tpm_inf_pnp_driver); |