tpm_tis_lpc.c 12 KB

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