tpm_tis.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (C) 2005, 2006 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. *
  8. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  9. *
  10. * Device driver for TCG/TCPA TPM (trusted platform module).
  11. * Specifications at www.trustedcomputinggroup.org
  12. *
  13. * This device driver implements the TPM interface as defined in
  14. * the TCG TPM Interface Spec version 1.2, revision 1.0.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/pnp.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/wait.h>
  28. #include <linux/acpi.h>
  29. #include <linux/freezer.h>
  30. #include "tpm.h"
  31. enum tis_access {
  32. TPM_ACCESS_VALID = 0x80,
  33. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  34. TPM_ACCESS_REQUEST_PENDING = 0x04,
  35. TPM_ACCESS_REQUEST_USE = 0x02,
  36. };
  37. enum tis_status {
  38. TPM_STS_VALID = 0x80,
  39. TPM_STS_COMMAND_READY = 0x40,
  40. TPM_STS_GO = 0x20,
  41. TPM_STS_DATA_AVAIL = 0x10,
  42. TPM_STS_DATA_EXPECT = 0x08,
  43. };
  44. enum tis_int_flags {
  45. TPM_GLOBAL_INT_ENABLE = 0x80000000,
  46. TPM_INTF_BURST_COUNT_STATIC = 0x100,
  47. TPM_INTF_CMD_READY_INT = 0x080,
  48. TPM_INTF_INT_EDGE_FALLING = 0x040,
  49. TPM_INTF_INT_EDGE_RISING = 0x020,
  50. TPM_INTF_INT_LEVEL_LOW = 0x010,
  51. TPM_INTF_INT_LEVEL_HIGH = 0x008,
  52. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  53. TPM_INTF_STS_VALID_INT = 0x002,
  54. TPM_INTF_DATA_AVAIL_INT = 0x001,
  55. };
  56. enum tis_defaults {
  57. TIS_MEM_BASE = 0xFED40000,
  58. TIS_MEM_LEN = 0x5000,
  59. TIS_SHORT_TIMEOUT = 750, /* ms */
  60. TIS_LONG_TIMEOUT = 2000, /* 2 sec */
  61. };
  62. #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
  63. #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
  64. #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
  65. #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
  66. #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
  67. #define TPM_STS(l) (0x0018 | ((l) << 12))
  68. #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
  69. #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
  70. #define TPM_RID(l) (0x0F04 | ((l) << 12))
  71. struct priv_data {
  72. bool irq_tested;
  73. };
  74. static LIST_HEAD(tis_chips);
  75. static DEFINE_MUTEX(tis_lock);
  76. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  77. static int is_itpm(struct pnp_dev *dev)
  78. {
  79. struct acpi_device *acpi = pnp_acpi_device(dev);
  80. struct acpi_hardware_id *id;
  81. if (!acpi)
  82. return 0;
  83. list_for_each_entry(id, &acpi->pnp.ids, list) {
  84. if (!strcmp("INTC0102", id->id))
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. #else
  90. static inline int is_itpm(struct pnp_dev *dev)
  91. {
  92. return 0;
  93. }
  94. #endif
  95. /* Before we attempt to access the TPM we must see that the valid bit is set.
  96. * The specification says that this bit is 0 at reset and remains 0 until the
  97. * 'TPM has gone through its self test and initialization and has established
  98. * correct values in the other bits.' */
  99. static int wait_startup(struct tpm_chip *chip, int l)
  100. {
  101. unsigned long stop = jiffies + chip->vendor.timeout_a;
  102. do {
  103. if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  104. TPM_ACCESS_VALID)
  105. return 0;
  106. msleep(TPM_TIMEOUT);
  107. } while (time_before(jiffies, stop));
  108. return -1;
  109. }
  110. static int check_locality(struct tpm_chip *chip, int l)
  111. {
  112. if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  113. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  114. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  115. return chip->vendor.locality = l;
  116. return -1;
  117. }
  118. static void release_locality(struct tpm_chip *chip, int l, int force)
  119. {
  120. if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  121. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
  122. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
  123. iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
  124. chip->vendor.iobase + TPM_ACCESS(l));
  125. }
  126. static int request_locality(struct tpm_chip *chip, int l)
  127. {
  128. unsigned long stop, timeout;
  129. long rc;
  130. if (check_locality(chip, l) >= 0)
  131. return l;
  132. iowrite8(TPM_ACCESS_REQUEST_USE,
  133. chip->vendor.iobase + TPM_ACCESS(l));
  134. stop = jiffies + chip->vendor.timeout_a;
  135. if (chip->vendor.irq) {
  136. again:
  137. timeout = stop - jiffies;
  138. if ((long)timeout <= 0)
  139. return -1;
  140. rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
  141. (check_locality
  142. (chip, l) >= 0),
  143. timeout);
  144. if (rc > 0)
  145. return l;
  146. if (rc == -ERESTARTSYS && freezing(current)) {
  147. clear_thread_flag(TIF_SIGPENDING);
  148. goto again;
  149. }
  150. } else {
  151. /* wait for burstcount */
  152. do {
  153. if (check_locality(chip, l) >= 0)
  154. return l;
  155. msleep(TPM_TIMEOUT);
  156. }
  157. while (time_before(jiffies, stop));
  158. }
  159. return -1;
  160. }
  161. static u8 tpm_tis_status(struct tpm_chip *chip)
  162. {
  163. return ioread8(chip->vendor.iobase +
  164. TPM_STS(chip->vendor.locality));
  165. }
  166. static void tpm_tis_ready(struct tpm_chip *chip)
  167. {
  168. /* this causes the current command to be aborted */
  169. iowrite8(TPM_STS_COMMAND_READY,
  170. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  171. }
  172. static int get_burstcount(struct tpm_chip *chip)
  173. {
  174. unsigned long stop;
  175. int burstcnt;
  176. /* wait for burstcount */
  177. /* which timeout value, spec has 2 answers (c & d) */
  178. stop = jiffies + chip->vendor.timeout_d;
  179. do {
  180. burstcnt = ioread8(chip->vendor.iobase +
  181. TPM_STS(chip->vendor.locality) + 1);
  182. burstcnt += ioread8(chip->vendor.iobase +
  183. TPM_STS(chip->vendor.locality) +
  184. 2) << 8;
  185. if (burstcnt)
  186. return burstcnt;
  187. msleep(TPM_TIMEOUT);
  188. } while (time_before(jiffies, stop));
  189. return -EBUSY;
  190. }
  191. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  192. {
  193. int size = 0, burstcnt;
  194. while (size < count &&
  195. wait_for_tpm_stat(chip,
  196. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  197. chip->vendor.timeout_c,
  198. &chip->vendor.read_queue, true)
  199. == 0) {
  200. burstcnt = get_burstcount(chip);
  201. for (; burstcnt > 0 && size < count; burstcnt--)
  202. buf[size++] = ioread8(chip->vendor.iobase +
  203. TPM_DATA_FIFO(chip->vendor.
  204. locality));
  205. }
  206. return size;
  207. }
  208. static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  209. {
  210. int size = 0;
  211. int expected, status;
  212. if (count < TPM_HEADER_SIZE) {
  213. size = -EIO;
  214. goto out;
  215. }
  216. /* read first 10 bytes, including tag, paramsize, and result */
  217. if ((size =
  218. recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
  219. dev_err(chip->dev, "Unable to read header\n");
  220. goto out;
  221. }
  222. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  223. if (expected > count) {
  224. size = -EIO;
  225. goto out;
  226. }
  227. if ((size +=
  228. recv_data(chip, &buf[TPM_HEADER_SIZE],
  229. expected - TPM_HEADER_SIZE)) < expected) {
  230. dev_err(chip->dev, "Unable to read remainder of result\n");
  231. size = -ETIME;
  232. goto out;
  233. }
  234. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  235. &chip->vendor.int_queue, false);
  236. status = tpm_tis_status(chip);
  237. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  238. dev_err(chip->dev, "Error left over data\n");
  239. size = -EIO;
  240. goto out;
  241. }
  242. out:
  243. tpm_tis_ready(chip);
  244. release_locality(chip, chip->vendor.locality, 0);
  245. return size;
  246. }
  247. static bool itpm;
  248. module_param(itpm, bool, 0444);
  249. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  250. /*
  251. * If interrupts are used (signaled by an irq set in the vendor structure)
  252. * tpm.c can skip polling for the data to be available as the interrupt is
  253. * waited for here
  254. */
  255. static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
  256. {
  257. int rc, status, burstcnt;
  258. size_t count = 0;
  259. if (request_locality(chip, 0) < 0)
  260. return -EBUSY;
  261. status = tpm_tis_status(chip);
  262. if ((status & TPM_STS_COMMAND_READY) == 0) {
  263. tpm_tis_ready(chip);
  264. if (wait_for_tpm_stat
  265. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  266. &chip->vendor.int_queue, false) < 0) {
  267. rc = -ETIME;
  268. goto out_err;
  269. }
  270. }
  271. while (count < len - 1) {
  272. burstcnt = get_burstcount(chip);
  273. for (; burstcnt > 0 && count < len - 1; burstcnt--) {
  274. iowrite8(buf[count], chip->vendor.iobase +
  275. TPM_DATA_FIFO(chip->vendor.locality));
  276. count++;
  277. }
  278. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  279. &chip->vendor.int_queue, false);
  280. status = tpm_tis_status(chip);
  281. if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
  282. rc = -EIO;
  283. goto out_err;
  284. }
  285. }
  286. /* write last byte */
  287. iowrite8(buf[count],
  288. chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
  289. wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  290. &chip->vendor.int_queue, false);
  291. status = tpm_tis_status(chip);
  292. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  293. rc = -EIO;
  294. goto out_err;
  295. }
  296. return 0;
  297. out_err:
  298. tpm_tis_ready(chip);
  299. release_locality(chip, chip->vendor.locality, 0);
  300. return rc;
  301. }
  302. static void disable_interrupts(struct tpm_chip *chip)
  303. {
  304. u32 intmask;
  305. intmask =
  306. ioread32(chip->vendor.iobase +
  307. TPM_INT_ENABLE(chip->vendor.locality));
  308. intmask &= ~TPM_GLOBAL_INT_ENABLE;
  309. iowrite32(intmask,
  310. chip->vendor.iobase +
  311. TPM_INT_ENABLE(chip->vendor.locality));
  312. free_irq(chip->vendor.irq, chip);
  313. chip->vendor.irq = 0;
  314. }
  315. /*
  316. * If interrupts are used (signaled by an irq set in the vendor structure)
  317. * tpm.c can skip polling for the data to be available as the interrupt is
  318. * waited for here
  319. */
  320. static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
  321. {
  322. int rc;
  323. u32 ordinal;
  324. rc = tpm_tis_send_data(chip, buf, len);
  325. if (rc < 0)
  326. return rc;
  327. /* go and do it */
  328. iowrite8(TPM_STS_GO,
  329. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  330. if (chip->vendor.irq) {
  331. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  332. if (wait_for_tpm_stat
  333. (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  334. tpm_calc_ordinal_duration(chip, ordinal),
  335. &chip->vendor.read_queue, false) < 0) {
  336. rc = -ETIME;
  337. goto out_err;
  338. }
  339. }
  340. return len;
  341. out_err:
  342. tpm_tis_ready(chip);
  343. release_locality(chip, chip->vendor.locality, 0);
  344. return rc;
  345. }
  346. static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
  347. {
  348. int rc, irq;
  349. struct priv_data *priv = chip->vendor.priv;
  350. if (!chip->vendor.irq || priv->irq_tested)
  351. return tpm_tis_send_main(chip, buf, len);
  352. /* Verify receipt of the expected IRQ */
  353. irq = chip->vendor.irq;
  354. chip->vendor.irq = 0;
  355. rc = tpm_tis_send_main(chip, buf, len);
  356. chip->vendor.irq = irq;
  357. if (!priv->irq_tested)
  358. msleep(1);
  359. if (!priv->irq_tested) {
  360. disable_interrupts(chip);
  361. dev_err(chip->dev,
  362. FW_BUG "TPM interrupt not working, polling instead\n");
  363. }
  364. priv->irq_tested = true;
  365. return rc;
  366. }
  367. struct tis_vendor_timeout_override {
  368. u32 did_vid;
  369. unsigned long timeout_us[4];
  370. };
  371. static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
  372. /* Atmel 3204 */
  373. { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
  374. (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
  375. };
  376. static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
  377. unsigned long *timeout_cap)
  378. {
  379. int i;
  380. u32 did_vid;
  381. did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  382. for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
  383. if (vendor_timeout_overrides[i].did_vid != did_vid)
  384. continue;
  385. memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
  386. sizeof(vendor_timeout_overrides[i].timeout_us));
  387. return true;
  388. }
  389. return false;
  390. }
  391. /*
  392. * Early probing for iTPM with STS_DATA_EXPECT flaw.
  393. * Try sending command without itpm flag set and if that
  394. * fails, repeat with itpm flag set.
  395. */
  396. static int probe_itpm(struct tpm_chip *chip)
  397. {
  398. int rc = 0;
  399. u8 cmd_getticks[] = {
  400. 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
  401. 0x00, 0x00, 0x00, 0xf1
  402. };
  403. size_t len = sizeof(cmd_getticks);
  404. bool rem_itpm = itpm;
  405. u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
  406. /* probe only iTPMS */
  407. if (vendor != TPM_VID_INTEL)
  408. return 0;
  409. itpm = false;
  410. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  411. if (rc == 0)
  412. goto out;
  413. tpm_tis_ready(chip);
  414. release_locality(chip, chip->vendor.locality, 0);
  415. itpm = true;
  416. rc = tpm_tis_send_data(chip, cmd_getticks, len);
  417. if (rc == 0) {
  418. dev_info(chip->dev, "Detected an iTPM.\n");
  419. rc = 1;
  420. } else
  421. rc = -EFAULT;
  422. out:
  423. itpm = rem_itpm;
  424. tpm_tis_ready(chip);
  425. release_locality(chip, chip->vendor.locality, 0);
  426. return rc;
  427. }
  428. static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
  429. {
  430. switch (chip->vendor.manufacturer_id) {
  431. case TPM_VID_WINBOND:
  432. return ((status == TPM_STS_VALID) ||
  433. (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
  434. case TPM_VID_STM:
  435. return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
  436. default:
  437. return (status == TPM_STS_COMMAND_READY);
  438. }
  439. }
  440. static const struct tpm_class_ops tpm_tis = {
  441. .status = tpm_tis_status,
  442. .recv = tpm_tis_recv,
  443. .send = tpm_tis_send,
  444. .cancel = tpm_tis_ready,
  445. .update_timeouts = tpm_tis_update_timeouts,
  446. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  447. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  448. .req_canceled = tpm_tis_req_canceled,
  449. };
  450. static irqreturn_t tis_int_probe(int irq, void *dev_id)
  451. {
  452. struct tpm_chip *chip = dev_id;
  453. u32 interrupt;
  454. interrupt = ioread32(chip->vendor.iobase +
  455. TPM_INT_STATUS(chip->vendor.locality));
  456. if (interrupt == 0)
  457. return IRQ_NONE;
  458. chip->vendor.probed_irq = irq;
  459. /* Clear interrupts handled with TPM_EOI */
  460. iowrite32(interrupt,
  461. chip->vendor.iobase +
  462. TPM_INT_STATUS(chip->vendor.locality));
  463. return IRQ_HANDLED;
  464. }
  465. static irqreturn_t tis_int_handler(int dummy, void *dev_id)
  466. {
  467. struct tpm_chip *chip = dev_id;
  468. u32 interrupt;
  469. int i;
  470. interrupt = ioread32(chip->vendor.iobase +
  471. TPM_INT_STATUS(chip->vendor.locality));
  472. if (interrupt == 0)
  473. return IRQ_NONE;
  474. ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
  475. if (interrupt & TPM_INTF_DATA_AVAIL_INT)
  476. wake_up_interruptible(&chip->vendor.read_queue);
  477. if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
  478. for (i = 0; i < 5; i++)
  479. if (check_locality(chip, i) >= 0)
  480. break;
  481. if (interrupt &
  482. (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
  483. TPM_INTF_CMD_READY_INT))
  484. wake_up_interruptible(&chip->vendor.int_queue);
  485. /* Clear interrupts handled with TPM_EOI */
  486. iowrite32(interrupt,
  487. chip->vendor.iobase +
  488. TPM_INT_STATUS(chip->vendor.locality));
  489. ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
  490. return IRQ_HANDLED;
  491. }
  492. static bool interrupts = true;
  493. module_param(interrupts, bool, 0444);
  494. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  495. static int tpm_tis_init(struct device *dev, resource_size_t start,
  496. resource_size_t len, unsigned int irq)
  497. {
  498. u32 vendor, intfcaps, intmask;
  499. int rc, i, irq_s, irq_e, probe;
  500. struct tpm_chip *chip;
  501. struct priv_data *priv;
  502. priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
  503. if (priv == NULL)
  504. return -ENOMEM;
  505. if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
  506. return -ENODEV;
  507. chip->vendor.priv = priv;
  508. chip->vendor.iobase = ioremap(start, len);
  509. if (!chip->vendor.iobase) {
  510. rc = -EIO;
  511. goto out_err;
  512. }
  513. /* Default timeouts */
  514. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  515. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  516. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  517. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  518. if (wait_startup(chip, 0) != 0) {
  519. rc = -ENODEV;
  520. goto out_err;
  521. }
  522. if (request_locality(chip, 0) != 0) {
  523. rc = -ENODEV;
  524. goto out_err;
  525. }
  526. vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  527. chip->vendor.manufacturer_id = vendor;
  528. dev_info(dev,
  529. "1.2 TPM (device-id 0x%X, rev-id %d)\n",
  530. vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
  531. if (!itpm) {
  532. probe = probe_itpm(chip);
  533. if (probe < 0) {
  534. rc = -ENODEV;
  535. goto out_err;
  536. }
  537. itpm = !!probe;
  538. }
  539. if (itpm)
  540. dev_info(dev, "Intel iTPM workaround enabled\n");
  541. /* Figure out the capabilities */
  542. intfcaps =
  543. ioread32(chip->vendor.iobase +
  544. TPM_INTF_CAPS(chip->vendor.locality));
  545. dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  546. intfcaps);
  547. if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
  548. dev_dbg(dev, "\tBurst Count Static\n");
  549. if (intfcaps & TPM_INTF_CMD_READY_INT)
  550. dev_dbg(dev, "\tCommand Ready Int Support\n");
  551. if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
  552. dev_dbg(dev, "\tInterrupt Edge Falling\n");
  553. if (intfcaps & TPM_INTF_INT_EDGE_RISING)
  554. dev_dbg(dev, "\tInterrupt Edge Rising\n");
  555. if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
  556. dev_dbg(dev, "\tInterrupt Level Low\n");
  557. if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
  558. dev_dbg(dev, "\tInterrupt Level High\n");
  559. if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
  560. dev_dbg(dev, "\tLocality Change Int Support\n");
  561. if (intfcaps & TPM_INTF_STS_VALID_INT)
  562. dev_dbg(dev, "\tSts Valid Int Support\n");
  563. if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
  564. dev_dbg(dev, "\tData Avail Int Support\n");
  565. /* INTERRUPT Setup */
  566. init_waitqueue_head(&chip->vendor.read_queue);
  567. init_waitqueue_head(&chip->vendor.int_queue);
  568. intmask =
  569. ioread32(chip->vendor.iobase +
  570. TPM_INT_ENABLE(chip->vendor.locality));
  571. intmask |= TPM_INTF_CMD_READY_INT
  572. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  573. | TPM_INTF_STS_VALID_INT;
  574. iowrite32(intmask,
  575. chip->vendor.iobase +
  576. TPM_INT_ENABLE(chip->vendor.locality));
  577. if (interrupts)
  578. chip->vendor.irq = irq;
  579. if (interrupts && !chip->vendor.irq) {
  580. irq_s =
  581. ioread8(chip->vendor.iobase +
  582. TPM_INT_VECTOR(chip->vendor.locality));
  583. if (irq_s) {
  584. irq_e = irq_s;
  585. } else {
  586. irq_s = 3;
  587. irq_e = 15;
  588. }
  589. for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
  590. iowrite8(i, chip->vendor.iobase +
  591. TPM_INT_VECTOR(chip->vendor.locality));
  592. if (request_irq
  593. (i, tis_int_probe, IRQF_SHARED,
  594. chip->vendor.miscdev.name, chip) != 0) {
  595. dev_info(chip->dev,
  596. "Unable to request irq: %d for probe\n",
  597. i);
  598. continue;
  599. }
  600. /* Clear all existing */
  601. iowrite32(ioread32
  602. (chip->vendor.iobase +
  603. TPM_INT_STATUS(chip->vendor.locality)),
  604. chip->vendor.iobase +
  605. TPM_INT_STATUS(chip->vendor.locality));
  606. /* Turn on */
  607. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  608. chip->vendor.iobase +
  609. TPM_INT_ENABLE(chip->vendor.locality));
  610. chip->vendor.probed_irq = 0;
  611. /* Generate Interrupts */
  612. tpm_gen_interrupt(chip);
  613. chip->vendor.irq = chip->vendor.probed_irq;
  614. /* free_irq will call into tis_int_probe;
  615. clear all irqs we haven't seen while doing
  616. tpm_gen_interrupt */
  617. iowrite32(ioread32
  618. (chip->vendor.iobase +
  619. TPM_INT_STATUS(chip->vendor.locality)),
  620. chip->vendor.iobase +
  621. TPM_INT_STATUS(chip->vendor.locality));
  622. /* Turn off */
  623. iowrite32(intmask,
  624. chip->vendor.iobase +
  625. TPM_INT_ENABLE(chip->vendor.locality));
  626. free_irq(i, chip);
  627. }
  628. }
  629. if (chip->vendor.irq) {
  630. iowrite8(chip->vendor.irq,
  631. chip->vendor.iobase +
  632. TPM_INT_VECTOR(chip->vendor.locality));
  633. if (request_irq
  634. (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
  635. chip->vendor.miscdev.name, chip) != 0) {
  636. dev_info(chip->dev,
  637. "Unable to request irq: %d for use\n",
  638. chip->vendor.irq);
  639. chip->vendor.irq = 0;
  640. } else {
  641. /* Clear all existing */
  642. iowrite32(ioread32
  643. (chip->vendor.iobase +
  644. TPM_INT_STATUS(chip->vendor.locality)),
  645. chip->vendor.iobase +
  646. TPM_INT_STATUS(chip->vendor.locality));
  647. /* Turn on */
  648. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  649. chip->vendor.iobase +
  650. TPM_INT_ENABLE(chip->vendor.locality));
  651. }
  652. }
  653. if (tpm_get_timeouts(chip)) {
  654. dev_err(dev, "Could not get TPM timeouts and durations\n");
  655. rc = -ENODEV;
  656. goto out_err;
  657. }
  658. if (tpm_do_selftest(chip)) {
  659. dev_err(dev, "TPM self test failed\n");
  660. rc = -ENODEV;
  661. goto out_err;
  662. }
  663. INIT_LIST_HEAD(&chip->vendor.list);
  664. mutex_lock(&tis_lock);
  665. list_add(&chip->vendor.list, &tis_chips);
  666. mutex_unlock(&tis_lock);
  667. return 0;
  668. out_err:
  669. if (chip->vendor.iobase)
  670. iounmap(chip->vendor.iobase);
  671. tpm_remove_hardware(chip->dev);
  672. return rc;
  673. }
  674. #ifdef CONFIG_PM_SLEEP
  675. static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
  676. {
  677. u32 intmask;
  678. /* reenable interrupts that device may have lost or
  679. BIOS/firmware may have disabled */
  680. iowrite8(chip->vendor.irq, chip->vendor.iobase +
  681. TPM_INT_VECTOR(chip->vendor.locality));
  682. intmask =
  683. ioread32(chip->vendor.iobase +
  684. TPM_INT_ENABLE(chip->vendor.locality));
  685. intmask |= TPM_INTF_CMD_READY_INT
  686. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  687. | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
  688. iowrite32(intmask,
  689. chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
  690. }
  691. static int tpm_tis_resume(struct device *dev)
  692. {
  693. struct tpm_chip *chip = dev_get_drvdata(dev);
  694. int ret;
  695. if (chip->vendor.irq)
  696. tpm_tis_reenable_interrupts(chip);
  697. ret = tpm_pm_resume(dev);
  698. if (!ret)
  699. tpm_do_selftest(chip);
  700. return ret;
  701. }
  702. #endif
  703. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  704. #ifdef CONFIG_PNP
  705. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  706. const struct pnp_device_id *pnp_id)
  707. {
  708. resource_size_t start, len;
  709. unsigned int irq = 0;
  710. start = pnp_mem_start(pnp_dev, 0);
  711. len = pnp_mem_len(pnp_dev, 0);
  712. if (pnp_irq_valid(pnp_dev, 0))
  713. irq = pnp_irq(pnp_dev, 0);
  714. else
  715. interrupts = false;
  716. if (is_itpm(pnp_dev))
  717. itpm = true;
  718. return tpm_tis_init(&pnp_dev->dev, start, len, irq);
  719. }
  720. static struct pnp_device_id tpm_pnp_tbl[] = {
  721. {"PNP0C31", 0}, /* TPM */
  722. {"ATM1200", 0}, /* Atmel */
  723. {"IFX0102", 0}, /* Infineon */
  724. {"BCM0101", 0}, /* Broadcom */
  725. {"BCM0102", 0}, /* Broadcom */
  726. {"NSC1200", 0}, /* National */
  727. {"ICO0102", 0}, /* Intel */
  728. /* Add new here */
  729. {"", 0}, /* User Specified */
  730. {"", 0} /* Terminator */
  731. };
  732. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  733. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  734. {
  735. struct tpm_chip *chip = pnp_get_drvdata(dev);
  736. tpm_dev_vendor_release(chip);
  737. kfree(chip);
  738. }
  739. static struct pnp_driver tis_pnp_driver = {
  740. .name = "tpm_tis",
  741. .id_table = tpm_pnp_tbl,
  742. .probe = tpm_tis_pnp_init,
  743. .remove = tpm_tis_pnp_remove,
  744. .driver = {
  745. .pm = &tpm_tis_pm,
  746. },
  747. };
  748. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  749. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  750. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  751. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  752. #endif
  753. static struct platform_driver tis_drv = {
  754. .driver = {
  755. .name = "tpm_tis",
  756. .owner = THIS_MODULE,
  757. .pm = &tpm_tis_pm,
  758. },
  759. };
  760. static struct platform_device *pdev;
  761. static bool force;
  762. module_param(force, bool, 0444);
  763. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  764. static int __init init_tis(void)
  765. {
  766. int rc;
  767. #ifdef CONFIG_PNP
  768. if (!force)
  769. return pnp_register_driver(&tis_pnp_driver);
  770. #endif
  771. rc = platform_driver_register(&tis_drv);
  772. if (rc < 0)
  773. return rc;
  774. pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
  775. if (IS_ERR(pdev)) {
  776. rc = PTR_ERR(pdev);
  777. goto err_dev;
  778. }
  779. rc = tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0);
  780. if (rc)
  781. goto err_init;
  782. return 0;
  783. err_init:
  784. platform_device_unregister(pdev);
  785. err_dev:
  786. platform_driver_unregister(&tis_drv);
  787. return rc;
  788. }
  789. static void __exit cleanup_tis(void)
  790. {
  791. struct tpm_vendor_specific *i, *j;
  792. struct tpm_chip *chip;
  793. mutex_lock(&tis_lock);
  794. list_for_each_entry_safe(i, j, &tis_chips, list) {
  795. chip = to_tpm_chip(i);
  796. tpm_remove_hardware(chip->dev);
  797. iowrite32(~TPM_GLOBAL_INT_ENABLE &
  798. ioread32(chip->vendor.iobase +
  799. TPM_INT_ENABLE(chip->vendor.
  800. locality)),
  801. chip->vendor.iobase +
  802. TPM_INT_ENABLE(chip->vendor.locality));
  803. release_locality(chip, chip->vendor.locality, 1);
  804. if (chip->vendor.irq)
  805. free_irq(chip->vendor.irq, chip);
  806. iounmap(i->iobase);
  807. list_del(&i->list);
  808. }
  809. mutex_unlock(&tis_lock);
  810. #ifdef CONFIG_PNP
  811. if (!force) {
  812. pnp_unregister_driver(&tis_pnp_driver);
  813. return;
  814. }
  815. #endif
  816. platform_device_unregister(pdev);
  817. platform_driver_unregister(&tis_drv);
  818. }
  819. module_init(init_tis);
  820. module_exit(cleanup_tis);
  821. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  822. MODULE_DESCRIPTION("TPM Driver");
  823. MODULE_VERSION("2.0");
  824. MODULE_LICENSE("GPL");