generic_lpc_tpm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * The code in this file is based on the article "Writing a TPM Device Driver"
  24. * published on http://ptgmedia.pearsoncmg.com.
  25. *
  26. * One principal difference is that in the simplest config the other than 0
  27. * TPM localities do not get mapped by some devices (for instance, by Infineon
  28. * slb9635), so this driver provides access to locality 0 only.
  29. */
  30. #include <common.h>
  31. #include <asm/io.h>
  32. #include <tpm.h>
  33. #define PREFIX "lpc_tpm: "
  34. struct tpm_locality {
  35. u32 access;
  36. u8 padding0[4];
  37. u32 int_enable;
  38. u8 vector;
  39. u8 padding1[3];
  40. u32 int_status;
  41. u32 int_capability;
  42. u32 tpm_status;
  43. u8 padding2[8];
  44. u8 data;
  45. u8 padding3[3803];
  46. u32 did_vid;
  47. u8 rid;
  48. u8 padding4[251];
  49. };
  50. /*
  51. * This pointer refers to the TPM chip, 5 of its localities are mapped as an
  52. * array.
  53. */
  54. #define TPM_TOTAL_LOCALITIES 5
  55. static struct tpm_locality *lpc_tpm_dev =
  56. (struct tpm_locality *)CONFIG_TPM_TIS_BASE_ADDRESS;
  57. /* Some registers' bit field definitions */
  58. #define TIS_STS_VALID (1 << 7) /* 0x80 */
  59. #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
  60. #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
  61. #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
  62. #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
  63. #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
  64. #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
  65. #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
  66. #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
  67. #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
  68. #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
  69. #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
  70. #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
  71. #define TIS_STS_BURST_COUNT_MASK (0xffff)
  72. #define TIS_STS_BURST_COUNT_SHIFT (8)
  73. /*
  74. * Error value returned if a tpm register does not enter the expected state
  75. * after continuous polling. No actual TPM register reading ever returns -1,
  76. * so this value is a safe error indication to be mixed with possible status
  77. * register values.
  78. */
  79. #define TPM_TIMEOUT_ERR (-1)
  80. /* Error value returned on various TPM driver errors. */
  81. #define TPM_DRIVER_ERR (1)
  82. /* 1 second is plenty for anything TPM does. */
  83. #define MAX_DELAY_US (1000 * 1000)
  84. /* Retrieve burst count value out of the status register contents. */
  85. static u16 burst_count(u32 status)
  86. {
  87. return (status >> TIS_STS_BURST_COUNT_SHIFT) & TIS_STS_BURST_COUNT_MASK;
  88. }
  89. /*
  90. * Structures defined below allow creating descriptions of TPM vendor/device
  91. * ID information for run time discovery. The only device the system knows
  92. * about at this time is Infineon slb9635.
  93. */
  94. struct device_name {
  95. u16 dev_id;
  96. const char * const dev_name;
  97. };
  98. struct vendor_name {
  99. u16 vendor_id;
  100. const char *vendor_name;
  101. const struct device_name *dev_names;
  102. };
  103. static const struct device_name infineon_devices[] = {
  104. {0xb, "SLB9635 TT 1.2"},
  105. {0}
  106. };
  107. static const struct vendor_name vendor_names[] = {
  108. {0x15d1, "Infineon", infineon_devices},
  109. };
  110. /*
  111. * Cached vendor/device ID pair to indicate that the device has been already
  112. * discovered.
  113. */
  114. static u32 vendor_dev_id;
  115. /* TPM access wrappers to support tracing */
  116. static u8 tpm_read_byte(const u8 *ptr)
  117. {
  118. u8 ret = readb(ptr);
  119. debug(PREFIX "Read reg 0x%4.4x returns 0x%2.2x\n",
  120. (u32)ptr - (u32)lpc_tpm_dev, ret);
  121. return ret;
  122. }
  123. static u32 tpm_read_word(const u32 *ptr)
  124. {
  125. u32 ret = readl(ptr);
  126. debug(PREFIX "Read reg 0x%4.4x returns 0x%8.8x\n",
  127. (u32)ptr - (u32)lpc_tpm_dev, ret);
  128. return ret;
  129. }
  130. static void tpm_write_byte(u8 value, u8 *ptr)
  131. {
  132. debug(PREFIX "Write reg 0x%4.4x with 0x%2.2x\n",
  133. (u32)ptr - (u32)lpc_tpm_dev, value);
  134. writeb(value, ptr);
  135. }
  136. static void tpm_write_word(u32 value, u32 *ptr)
  137. {
  138. debug(PREFIX "Write reg 0x%4.4x with 0x%8.8x\n",
  139. (u32)ptr - (u32)lpc_tpm_dev, value);
  140. writel(value, ptr);
  141. }
  142. /*
  143. * tis_wait_reg()
  144. *
  145. * Wait for at least a second for a register to change its state to match the
  146. * expected state. Normally the transition happens within microseconds.
  147. *
  148. * @reg - pointer to the TPM register
  149. * @mask - bitmask for the bitfield(s) to watch
  150. * @expected - value the field(s) are supposed to be set to
  151. *
  152. * Returns the register contents in case the expected value was found in the
  153. * appropriate register bits, or TPM_TIMEOUT_ERR on timeout.
  154. */
  155. static u32 tis_wait_reg(u32 *reg, u8 mask, u8 expected)
  156. {
  157. u32 time_us = MAX_DELAY_US;
  158. while (time_us > 0) {
  159. u32 value = tpm_read_word(reg);
  160. if ((value & mask) == expected)
  161. return value;
  162. udelay(1); /* 1 us */
  163. time_us--;
  164. }
  165. return TPM_TIMEOUT_ERR;
  166. }
  167. /*
  168. * Probe the TPM device and try determining its manufacturer/device name.
  169. *
  170. * Returns 0 on success (the device is found or was found during an earlier
  171. * invocation) or TPM_DRIVER_ERR if the device is not found.
  172. */
  173. int tis_init(void)
  174. {
  175. u32 didvid = tpm_read_word(&lpc_tpm_dev[0].did_vid);
  176. int i;
  177. const char *device_name = "unknown";
  178. const char *vendor_name = device_name;
  179. u16 vid, did;
  180. if (vendor_dev_id)
  181. return 0; /* Already probed. */
  182. if (!didvid || (didvid == 0xffffffff)) {
  183. printf("%s: No TPM device found\n", __func__);
  184. return TPM_DRIVER_ERR;
  185. }
  186. vendor_dev_id = didvid;
  187. vid = didvid & 0xffff;
  188. did = (didvid >> 16) & 0xffff;
  189. for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
  190. int j = 0;
  191. u16 known_did;
  192. if (vid == vendor_names[i].vendor_id)
  193. vendor_name = vendor_names[i].vendor_name;
  194. while ((known_did = vendor_names[i].dev_names[j].dev_id) != 0) {
  195. if (known_did == did) {
  196. device_name =
  197. vendor_names[i].dev_names[j].dev_name;
  198. break;
  199. }
  200. j++;
  201. }
  202. break;
  203. }
  204. printf("Found TPM %s by %s\n", device_name, vendor_name);
  205. return 0;
  206. }
  207. /*
  208. * tis_senddata()
  209. *
  210. * send the passed in data to the TPM device.
  211. *
  212. * @data - address of the data to send, byte by byte
  213. * @len - length of the data to send
  214. *
  215. * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
  216. * not accept the entire command).
  217. */
  218. static u32 tis_senddata(const u8 * const data, u32 len)
  219. {
  220. u32 offset = 0;
  221. u16 burst = 0;
  222. u32 max_cycles = 0;
  223. u8 locality = 0;
  224. u32 value;
  225. value = tis_wait_reg(&lpc_tpm_dev[locality].tpm_status,
  226. TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
  227. if (value == TPM_TIMEOUT_ERR) {
  228. printf("%s:%d - failed to get 'command_ready' status\n",
  229. __FILE__, __LINE__);
  230. return TPM_DRIVER_ERR;
  231. }
  232. burst = burst_count(value);
  233. while (1) {
  234. unsigned count;
  235. /* Wait till the device is ready to accept more data. */
  236. while (!burst) {
  237. if (max_cycles++ == MAX_DELAY_US) {
  238. printf("%s:%d failed to feed %d bytes of %d\n",
  239. __FILE__, __LINE__, len - offset, len);
  240. return TPM_DRIVER_ERR;
  241. }
  242. udelay(1);
  243. burst = burst_count(tpm_read_word(&lpc_tpm_dev
  244. [locality].tpm_status));
  245. }
  246. max_cycles = 0;
  247. /*
  248. * Calculate number of bytes the TPM is ready to accept in one
  249. * shot.
  250. *
  251. * We want to send the last byte outside of the loop (hence
  252. * the -1 below) to make sure that the 'expected' status bit
  253. * changes to zero exactly after the last byte is fed into the
  254. * FIFO.
  255. */
  256. count = min(burst, len - offset - 1);
  257. while (count--)
  258. tpm_write_byte(data[offset++],
  259. &lpc_tpm_dev[locality].data);
  260. value = tis_wait_reg(&lpc_tpm_dev[locality].tpm_status,
  261. TIS_STS_VALID, TIS_STS_VALID);
  262. if ((value == TPM_TIMEOUT_ERR) || !(value & TIS_STS_EXPECT)) {
  263. printf("%s:%d TPM command feed overflow\n",
  264. __FILE__, __LINE__);
  265. return TPM_DRIVER_ERR;
  266. }
  267. burst = burst_count(value);
  268. if ((offset == (len - 1)) && burst) {
  269. /*
  270. * We need to be able to send the last byte to the
  271. * device, so burst size must be nonzero before we
  272. * break out.
  273. */
  274. break;
  275. }
  276. }
  277. /* Send the last byte. */
  278. tpm_write_byte(data[offset++], &lpc_tpm_dev[locality].data);
  279. /*
  280. * Verify that TPM does not expect any more data as part of this
  281. * command.
  282. */
  283. value = tis_wait_reg(&lpc_tpm_dev[locality].tpm_status,
  284. TIS_STS_VALID, TIS_STS_VALID);
  285. if ((value == TPM_TIMEOUT_ERR) || (value & TIS_STS_EXPECT)) {
  286. printf("%s:%d unexpected TPM status 0x%x\n",
  287. __FILE__, __LINE__, value);
  288. return TPM_DRIVER_ERR;
  289. }
  290. /* OK, sitting pretty, let's start the command execution. */
  291. tpm_write_word(TIS_STS_TPM_GO, &lpc_tpm_dev[locality].tpm_status);
  292. return 0;
  293. }
  294. /*
  295. * tis_readresponse()
  296. *
  297. * read the TPM device response after a command was issued.
  298. *
  299. * @buffer - address where to read the response, byte by byte.
  300. * @len - pointer to the size of buffer
  301. *
  302. * On success stores the number of received bytes to len and returns 0. On
  303. * errors (misformatted TPM data or synchronization problems) returns
  304. * TPM_DRIVER_ERR.
  305. */
  306. static u32 tis_readresponse(u8 *buffer, u32 *len)
  307. {
  308. u16 burst;
  309. u32 value;
  310. u32 offset = 0;
  311. u8 locality = 0;
  312. const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
  313. u32 expected_count = *len;
  314. int max_cycles = 0;
  315. /* Wait for the TPM to process the command. */
  316. value = tis_wait_reg(&lpc_tpm_dev[locality].tpm_status,
  317. has_data, has_data);
  318. if (value == TPM_TIMEOUT_ERR) {
  319. printf("%s:%d failed processing command\n",
  320. __FILE__, __LINE__);
  321. return TPM_DRIVER_ERR;
  322. }
  323. do {
  324. while ((burst = burst_count(value)) == 0) {
  325. if (max_cycles++ == MAX_DELAY_US) {
  326. printf("%s:%d TPM stuck on read\n",
  327. __FILE__, __LINE__);
  328. return TPM_DRIVER_ERR;
  329. }
  330. udelay(1);
  331. value = tpm_read_word(&lpc_tpm_dev
  332. [locality].tpm_status);
  333. }
  334. max_cycles = 0;
  335. while (burst-- && (offset < expected_count)) {
  336. buffer[offset++] = tpm_read_byte(&lpc_tpm_dev
  337. [locality].data);
  338. if (offset == 6) {
  339. /*
  340. * We got the first six bytes of the reply,
  341. * let's figure out how many bytes to expect
  342. * total - it is stored as a 4 byte number in
  343. * network order, starting with offset 2 into
  344. * the body of the reply.
  345. */
  346. u32 real_length;
  347. memcpy(&real_length,
  348. buffer + 2,
  349. sizeof(real_length));
  350. expected_count = be32_to_cpu(real_length);
  351. if ((expected_count < offset) ||
  352. (expected_count > *len)) {
  353. printf("%s:%d bad response size %d\n",
  354. __FILE__, __LINE__,
  355. expected_count);
  356. return TPM_DRIVER_ERR;
  357. }
  358. }
  359. }
  360. /* Wait for the next portion. */
  361. value = tis_wait_reg(&lpc_tpm_dev[locality].tpm_status,
  362. TIS_STS_VALID, TIS_STS_VALID);
  363. if (value == TPM_TIMEOUT_ERR) {
  364. printf("%s:%d failed to read response\n",
  365. __FILE__, __LINE__);
  366. return TPM_DRIVER_ERR;
  367. }
  368. if (offset == expected_count)
  369. break; /* We got all we needed. */
  370. } while ((value & has_data) == has_data);
  371. /*
  372. * Make sure we indeed read all there was. The TIS_STS_VALID bit is
  373. * known to be set.
  374. */
  375. if (value & TIS_STS_DATA_AVAILABLE) {
  376. printf("%s:%d wrong receive status %x\n",
  377. __FILE__, __LINE__, value);
  378. return TPM_DRIVER_ERR;
  379. }
  380. /* Tell the TPM that we are done. */
  381. tpm_write_word(TIS_STS_COMMAND_READY, &lpc_tpm_dev
  382. [locality].tpm_status);
  383. *len = offset;
  384. return 0;
  385. }
  386. int tis_open(void)
  387. {
  388. u8 locality = 0; /* we use locality zero for everything. */
  389. if (tis_close())
  390. return TPM_DRIVER_ERR;
  391. /* now request access to locality. */
  392. tpm_write_word(TIS_ACCESS_REQUEST_USE, &lpc_tpm_dev[locality].access);
  393. /* did we get a lock? */
  394. if (tis_wait_reg(&lpc_tpm_dev[locality].access,
  395. TIS_ACCESS_ACTIVE_LOCALITY,
  396. TIS_ACCESS_ACTIVE_LOCALITY) == TPM_TIMEOUT_ERR) {
  397. printf("%s:%d - failed to lock locality %d\n",
  398. __FILE__, __LINE__, locality);
  399. return TPM_DRIVER_ERR;
  400. }
  401. tpm_write_word(TIS_STS_COMMAND_READY,
  402. &lpc_tpm_dev[locality].tpm_status);
  403. return 0;
  404. }
  405. int tis_close(void)
  406. {
  407. u8 locality = 0;
  408. if (tpm_read_word(&lpc_tpm_dev[locality].access) &
  409. TIS_ACCESS_ACTIVE_LOCALITY) {
  410. tpm_write_word(TIS_ACCESS_ACTIVE_LOCALITY,
  411. &lpc_tpm_dev[locality].access);
  412. if (tis_wait_reg(&lpc_tpm_dev[locality].access,
  413. TIS_ACCESS_ACTIVE_LOCALITY, 0) ==
  414. TPM_TIMEOUT_ERR) {
  415. printf("%s:%d - failed to release locality %d\n",
  416. __FILE__, __LINE__, locality);
  417. return TPM_DRIVER_ERR;
  418. }
  419. }
  420. return 0;
  421. }
  422. int tis_sendrecv(const u8 *sendbuf, size_t send_size,
  423. u8 *recvbuf, size_t *recv_len)
  424. {
  425. if (tis_senddata(sendbuf, send_size)) {
  426. printf("%s:%d failed sending data to TPM\n",
  427. __FILE__, __LINE__);
  428. return TPM_DRIVER_ERR;
  429. }
  430. return tis_readresponse(recvbuf, recv_len);
  431. }