tpm_tis_lpc.c 12 KB

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