tpm_tis_lpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <dm.h>
  16. #include <mapmem.h>
  17. #include <tis.h>
  18. #include <tpm.h>
  19. #include <asm/io.h>
  20. #define PREFIX "lpc_tpm: "
  21. struct tpm_locality {
  22. u32 access;
  23. u8 padding0[4];
  24. u32 int_enable;
  25. u8 vector;
  26. u8 padding1[3];
  27. u32 int_status;
  28. u32 int_capability;
  29. u32 tpm_status;
  30. u8 padding2[8];
  31. u8 data;
  32. u8 padding3[3803];
  33. u32 did_vid;
  34. u8 rid;
  35. u8 padding4[251];
  36. };
  37. struct tpm_tis_lpc_priv {
  38. struct tpm_locality *regs;
  39. };
  40. /*
  41. * This pointer refers to the TPM chip, 5 of its localities are mapped as an
  42. * array.
  43. */
  44. #define TPM_TOTAL_LOCALITIES 5
  45. /* Some registers' bit field definitions */
  46. #define TIS_STS_VALID (1 << 7) /* 0x80 */
  47. #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
  48. #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
  49. #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
  50. #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
  51. #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
  52. #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
  53. #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
  54. #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
  55. #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
  56. #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
  57. #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
  58. #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
  59. #define TIS_STS_BURST_COUNT_MASK (0xffff)
  60. #define TIS_STS_BURST_COUNT_SHIFT (8)
  61. /* 1 second is plenty for anything TPM does. */
  62. #define MAX_DELAY_US (1000 * 1000)
  63. /* Retrieve burst count value out of the status register contents. */
  64. static u16 burst_count(u32 status)
  65. {
  66. return (status >> TIS_STS_BURST_COUNT_SHIFT) &
  67. TIS_STS_BURST_COUNT_MASK;
  68. }
  69. /* TPM access wrappers to support tracing */
  70. static u8 tpm_read_byte(struct tpm_tis_lpc_priv *priv, const u8 *ptr)
  71. {
  72. u8 ret = readb(ptr);
  73. debug(PREFIX "Read reg 0x%4.4x returns 0x%2.2x\n",
  74. (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
  75. return ret;
  76. }
  77. static u32 tpm_read_word(struct tpm_tis_lpc_priv *priv, const u32 *ptr)
  78. {
  79. u32 ret = readl(ptr);
  80. debug(PREFIX "Read reg 0x%4.4x returns 0x%8.8x\n",
  81. (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
  82. return ret;
  83. }
  84. static void tpm_write_byte(struct tpm_tis_lpc_priv *priv, u8 value, u8 *ptr)
  85. {
  86. debug(PREFIX "Write reg 0x%4.4x with 0x%2.2x\n",
  87. (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
  88. writeb(value, ptr);
  89. }
  90. static void tpm_write_word(struct tpm_tis_lpc_priv *priv, u32 value,
  91. u32 *ptr)
  92. {
  93. debug(PREFIX "Write reg 0x%4.4x with 0x%8.8x\n",
  94. (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
  95. writel(value, ptr);
  96. }
  97. /*
  98. * tis_wait_reg()
  99. *
  100. * Wait for at least a second for a register to change its state to match the
  101. * expected state. Normally the transition happens within microseconds.
  102. *
  103. * @reg - pointer to the TPM register
  104. * @mask - bitmask for the bitfield(s) to watch
  105. * @expected - value the field(s) are supposed to be set to
  106. *
  107. * Returns the register contents in case the expected value was found in the
  108. * appropriate register bits, or -ETIMEDOUT on timeout.
  109. */
  110. static int tis_wait_reg(struct tpm_tis_lpc_priv *priv, u32 *reg, u8 mask,
  111. u8 expected)
  112. {
  113. u32 time_us = MAX_DELAY_US;
  114. while (time_us > 0) {
  115. u32 value = tpm_read_word(priv, reg);
  116. if ((value & mask) == expected)
  117. return value;
  118. udelay(1); /* 1 us */
  119. time_us--;
  120. }
  121. return -ETIMEDOUT;
  122. }
  123. /*
  124. * Probe the TPM device and try determining its manufacturer/device name.
  125. *
  126. * Returns 0 on success, -ve on error
  127. */
  128. static int tpm_tis_lpc_probe(struct udevice *dev)
  129. {
  130. struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
  131. u32 vid, did;
  132. fdt_addr_t addr;
  133. u32 didvid;
  134. addr = dev_get_addr(dev);
  135. if (addr == FDT_ADDR_T_NONE)
  136. return -EINVAL;
  137. priv->regs = map_sysmem(addr, 0);
  138. didvid = tpm_read_word(priv, &priv->regs[0].did_vid);
  139. vid = didvid & 0xffff;
  140. did = (didvid >> 16) & 0xffff;
  141. if (vid != 0x15d1 || did != 0xb) {
  142. debug("Invalid vendor/device ID %04x/%04x\n", vid, did);
  143. return -ENOSYS;
  144. }
  145. debug("Found TPM %s by %s\n", "SLB9635 TT 1.2", "Infineon");
  146. return 0;
  147. }
  148. /*
  149. * tis_senddata()
  150. *
  151. * send the passed in data to the TPM device.
  152. *
  153. * @data - address of the data to send, byte by byte
  154. * @len - length of the data to send
  155. *
  156. * Returns 0 on success, -ve on error (in case the device does not accept
  157. * the entire command).
  158. */
  159. static int tis_senddata(struct udevice *dev, const u8 *data, size_t len)
  160. {
  161. struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
  162. struct tpm_locality *regs = priv->regs;
  163. u32 offset = 0;
  164. u16 burst = 0;
  165. u32 max_cycles = 0;
  166. u8 locality = 0;
  167. u32 value;
  168. value = tis_wait_reg(priv, &regs[locality].tpm_status,
  169. TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
  170. if (value == -ETIMEDOUT) {
  171. printf("%s:%d - failed to get 'command_ready' status\n",
  172. __FILE__, __LINE__);
  173. return value;
  174. }
  175. burst = burst_count(value);
  176. while (1) {
  177. unsigned count;
  178. /* Wait till the device is ready to accept more data. */
  179. while (!burst) {
  180. if (max_cycles++ == MAX_DELAY_US) {
  181. printf("%s:%d failed to feed %d bytes of %d\n",
  182. __FILE__, __LINE__, len - offset, len);
  183. return -ETIMEDOUT;
  184. }
  185. udelay(1);
  186. burst = burst_count(tpm_read_word(priv,
  187. &regs[locality].tpm_status));
  188. }
  189. max_cycles = 0;
  190. /*
  191. * Calculate number of bytes the TPM is ready to accept in one
  192. * shot.
  193. *
  194. * We want to send the last byte outside of the loop (hence
  195. * the -1 below) to make sure that the 'expected' status bit
  196. * changes to zero exactly after the last byte is fed into the
  197. * FIFO.
  198. */
  199. count = min((u32)burst, len - offset - 1);
  200. while (count--)
  201. tpm_write_byte(priv, data[offset++],
  202. &regs[locality].data);
  203. value = tis_wait_reg(priv, &regs[locality].tpm_status,
  204. TIS_STS_VALID, TIS_STS_VALID);
  205. if ((value == -ETIMEDOUT) || !(value & TIS_STS_EXPECT)) {
  206. printf("%s:%d TPM command feed overflow\n",
  207. __FILE__, __LINE__);
  208. return value == -ETIMEDOUT ? value : -EIO;
  209. }
  210. burst = burst_count(value);
  211. if ((offset == (len - 1)) && burst) {
  212. /*
  213. * We need to be able to send the last byte to the
  214. * device, so burst size must be nonzero before we
  215. * break out.
  216. */
  217. break;
  218. }
  219. }
  220. /* Send the last byte. */
  221. tpm_write_byte(priv, data[offset++], &regs[locality].data);
  222. /*
  223. * Verify that TPM does not expect any more data as part of this
  224. * command.
  225. */
  226. value = tis_wait_reg(priv, &regs[locality].tpm_status,
  227. TIS_STS_VALID, TIS_STS_VALID);
  228. if ((value == -ETIMEDOUT) || (value & TIS_STS_EXPECT)) {
  229. printf("%s:%d unexpected TPM status 0x%x\n",
  230. __FILE__, __LINE__, value);
  231. return value == -ETIMEDOUT ? value : -EIO;
  232. }
  233. /* OK, sitting pretty, let's start the command execution. */
  234. tpm_write_word(priv, TIS_STS_TPM_GO, &regs[locality].tpm_status);
  235. return 0;
  236. }
  237. /*
  238. * tis_readresponse()
  239. *
  240. * read the TPM device response after a command was issued.
  241. *
  242. * @buffer - address where to read the response, byte by byte.
  243. * @len - pointer to the size of buffer
  244. *
  245. * On success stores the number of received bytes to len and returns 0. On
  246. * errors (misformatted TPM data or synchronization problems) returns
  247. * -ve value.
  248. */
  249. static int tis_readresponse(struct udevice *dev, u8 *buffer, size_t len)
  250. {
  251. struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
  252. struct tpm_locality *regs = priv->regs;
  253. u16 burst;
  254. u32 value;
  255. u32 offset = 0;
  256. u8 locality = 0;
  257. const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
  258. u32 expected_count = len;
  259. int max_cycles = 0;
  260. /* Wait for the TPM to process the command. */
  261. value = tis_wait_reg(priv, &regs[locality].tpm_status,
  262. has_data, has_data);
  263. if (value == -ETIMEDOUT) {
  264. printf("%s:%d failed processing command\n",
  265. __FILE__, __LINE__);
  266. return value;
  267. }
  268. do {
  269. while ((burst = burst_count(value)) == 0) {
  270. if (max_cycles++ == MAX_DELAY_US) {
  271. printf("%s:%d TPM stuck on read\n",
  272. __FILE__, __LINE__);
  273. return -EIO;
  274. }
  275. udelay(1);
  276. value = tpm_read_word(priv, &regs[locality].tpm_status);
  277. }
  278. max_cycles = 0;
  279. while (burst-- && (offset < expected_count)) {
  280. buffer[offset++] = tpm_read_byte(priv,
  281. &regs[locality].data);
  282. if (offset == 6) {
  283. /*
  284. * We got the first six bytes of the reply,
  285. * let's figure out how many bytes to expect
  286. * total - it is stored as a 4 byte number in
  287. * network order, starting with offset 2 into
  288. * the body of the reply.
  289. */
  290. u32 real_length;
  291. memcpy(&real_length,
  292. buffer + 2,
  293. sizeof(real_length));
  294. expected_count = be32_to_cpu(real_length);
  295. if ((expected_count < offset) ||
  296. (expected_count > len)) {
  297. printf("%s:%d bad response size %d\n",
  298. __FILE__, __LINE__,
  299. expected_count);
  300. return -ENOSPC;
  301. }
  302. }
  303. }
  304. /* Wait for the next portion. */
  305. value = tis_wait_reg(priv, &regs[locality].tpm_status,
  306. TIS_STS_VALID, TIS_STS_VALID);
  307. if (value == -ETIMEDOUT) {
  308. printf("%s:%d failed to read response\n",
  309. __FILE__, __LINE__);
  310. return value;
  311. }
  312. if (offset == expected_count)
  313. break; /* We got all we needed. */
  314. } while ((value & has_data) == has_data);
  315. /*
  316. * Make sure we indeed read all there was. The TIS_STS_VALID bit is
  317. * known to be set.
  318. */
  319. if (value & TIS_STS_DATA_AVAILABLE) {
  320. printf("%s:%d wrong receive status %x\n",
  321. __FILE__, __LINE__, value);
  322. return -EBADMSG;
  323. }
  324. /* Tell the TPM that we are done. */
  325. tpm_write_word(priv, TIS_STS_COMMAND_READY,
  326. &regs[locality].tpm_status);
  327. return offset;
  328. }
  329. static int tpm_tis_lpc_open(struct udevice *dev)
  330. {
  331. struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
  332. struct tpm_locality *regs = priv->regs;
  333. u8 locality = 0; /* we use locality zero for everything. */
  334. int ret;
  335. /* now request access to locality. */
  336. tpm_write_word(priv, TIS_ACCESS_REQUEST_USE, &regs[locality].access);
  337. /* did we get a lock? */
  338. ret = tis_wait_reg(priv, &regs[locality].access,
  339. TIS_ACCESS_ACTIVE_LOCALITY,
  340. TIS_ACCESS_ACTIVE_LOCALITY);
  341. if (ret == -ETIMEDOUT) {
  342. printf("%s:%d - failed to lock locality %d\n",
  343. __FILE__, __LINE__, locality);
  344. return ret;
  345. }
  346. tpm_write_word(priv, TIS_STS_COMMAND_READY,
  347. &regs[locality].tpm_status);
  348. return 0;
  349. }
  350. static int tpm_tis_lpc_close(struct udevice *dev)
  351. {
  352. struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
  353. struct tpm_locality *regs = priv->regs;
  354. u8 locality = 0;
  355. if (tpm_read_word(priv, &regs[locality].access) &
  356. TIS_ACCESS_ACTIVE_LOCALITY) {
  357. tpm_write_word(priv, TIS_ACCESS_ACTIVE_LOCALITY,
  358. &regs[locality].access);
  359. if (tis_wait_reg(priv, &regs[locality].access,
  360. TIS_ACCESS_ACTIVE_LOCALITY, 0) == -ETIMEDOUT) {
  361. printf("%s:%d - failed to release locality %d\n",
  362. __FILE__, __LINE__, locality);
  363. return -ETIMEDOUT;
  364. }
  365. }
  366. return 0;
  367. }
  368. static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
  369. {
  370. if (size < 50)
  371. return -ENOSPC;
  372. return snprintf(buf, size, "1.2 TPM (vendor %s, chip %s)",
  373. "Infineon", "SLB9635 TT 1.2");
  374. }
  375. static const struct tpm_ops tpm_tis_lpc_ops = {
  376. .open = tpm_tis_lpc_open,
  377. .close = tpm_tis_lpc_close,
  378. .get_desc = tpm_tis_get_desc,
  379. .send = tis_senddata,
  380. .recv = tis_readresponse,
  381. };
  382. static const struct udevice_id tpm_tis_lpc_ids[] = {
  383. { .compatible = "infineon,slb9635lpc" },
  384. { }
  385. };
  386. U_BOOT_DRIVER(tpm_tis_lpc) = {
  387. .name = "tpm_tis_lpc",
  388. .id = UCLASS_TPM,
  389. .of_match = tpm_tis_lpc_ids,
  390. .ops = &tpm_tis_lpc_ops,
  391. .probe = tpm_tis_lpc_probe,
  392. .priv_auto_alloc_size = sizeof(struct tpm_tis_lpc_priv),
  393. };