tpm_tis.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 "tpm.h"
  29. #define TPM_HEADER_SIZE 10
  30. enum tis_access {
  31. TPM_ACCESS_VALID = 0x80,
  32. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  33. TPM_ACCESS_REQUEST_PENDING = 0x04,
  34. TPM_ACCESS_REQUEST_USE = 0x02,
  35. };
  36. enum tis_status {
  37. TPM_STS_VALID = 0x80,
  38. TPM_STS_COMMAND_READY = 0x40,
  39. TPM_STS_GO = 0x20,
  40. TPM_STS_DATA_AVAIL = 0x10,
  41. TPM_STS_DATA_EXPECT = 0x08,
  42. };
  43. enum tis_int_flags {
  44. TPM_GLOBAL_INT_ENABLE = 0x80000000,
  45. TPM_INTF_BURST_COUNT_STATIC = 0x100,
  46. TPM_INTF_CMD_READY_INT = 0x080,
  47. TPM_INTF_INT_EDGE_FALLING = 0x040,
  48. TPM_INTF_INT_EDGE_RISING = 0x020,
  49. TPM_INTF_INT_LEVEL_LOW = 0x010,
  50. TPM_INTF_INT_LEVEL_HIGH = 0x008,
  51. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  52. TPM_INTF_STS_VALID_INT = 0x002,
  53. TPM_INTF_DATA_AVAIL_INT = 0x001,
  54. };
  55. enum tis_defaults {
  56. TIS_MEM_BASE = 0xFED40000,
  57. TIS_MEM_LEN = 0x5000,
  58. TIS_SHORT_TIMEOUT = 750, /* ms */
  59. TIS_LONG_TIMEOUT = 2000, /* 2 sec */
  60. };
  61. #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
  62. #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
  63. #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
  64. #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
  65. #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
  66. #define TPM_STS(l) (0x0018 | ((l) << 12))
  67. #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
  68. #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
  69. #define TPM_RID(l) (0x0F04 | ((l) << 12))
  70. static LIST_HEAD(tis_chips);
  71. static DEFINE_SPINLOCK(tis_lock);
  72. static int check_locality(struct tpm_chip *chip, int l)
  73. {
  74. if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  75. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  76. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  77. return chip->vendor.locality = l;
  78. return -1;
  79. }
  80. static void release_locality(struct tpm_chip *chip, int l, int force)
  81. {
  82. if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
  83. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
  84. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
  85. iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
  86. chip->vendor.iobase + TPM_ACCESS(l));
  87. }
  88. static int request_locality(struct tpm_chip *chip, int l)
  89. {
  90. unsigned long stop;
  91. long rc;
  92. if (check_locality(chip, l) >= 0)
  93. return l;
  94. iowrite8(TPM_ACCESS_REQUEST_USE,
  95. chip->vendor.iobase + TPM_ACCESS(l));
  96. if (chip->vendor.irq) {
  97. rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
  98. (check_locality
  99. (chip, l) >= 0),
  100. chip->vendor.timeout_a);
  101. if (rc > 0)
  102. return l;
  103. } else {
  104. /* wait for burstcount */
  105. stop = jiffies + chip->vendor.timeout_a;
  106. do {
  107. if (check_locality(chip, l) >= 0)
  108. return l;
  109. msleep(TPM_TIMEOUT);
  110. }
  111. while (time_before(jiffies, stop));
  112. }
  113. return -1;
  114. }
  115. static u8 tpm_tis_status(struct tpm_chip *chip)
  116. {
  117. return ioread8(chip->vendor.iobase +
  118. TPM_STS(chip->vendor.locality));
  119. }
  120. static void tpm_tis_ready(struct tpm_chip *chip)
  121. {
  122. /* this causes the current command to be aborted */
  123. iowrite8(TPM_STS_COMMAND_READY,
  124. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  125. }
  126. static int get_burstcount(struct tpm_chip *chip)
  127. {
  128. unsigned long stop;
  129. int burstcnt;
  130. /* wait for burstcount */
  131. /* which timeout value, spec has 2 answers (c & d) */
  132. stop = jiffies + chip->vendor.timeout_d;
  133. do {
  134. burstcnt = ioread8(chip->vendor.iobase +
  135. TPM_STS(chip->vendor.locality) + 1);
  136. burstcnt += ioread8(chip->vendor.iobase +
  137. TPM_STS(chip->vendor.locality) +
  138. 2) << 8;
  139. if (burstcnt)
  140. return burstcnt;
  141. msleep(TPM_TIMEOUT);
  142. } while (time_before(jiffies, stop));
  143. return -EBUSY;
  144. }
  145. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  146. wait_queue_head_t *queue)
  147. {
  148. unsigned long stop;
  149. long rc;
  150. u8 status;
  151. /* check current status */
  152. status = tpm_tis_status(chip);
  153. if ((status & mask) == mask)
  154. return 0;
  155. if (chip->vendor.irq) {
  156. rc = wait_event_interruptible_timeout(*queue,
  157. ((tpm_tis_status
  158. (chip) & mask) ==
  159. mask), timeout);
  160. if (rc > 0)
  161. return 0;
  162. } else {
  163. stop = jiffies + timeout;
  164. do {
  165. msleep(TPM_TIMEOUT);
  166. status = tpm_tis_status(chip);
  167. if ((status & mask) == mask)
  168. return 0;
  169. } while (time_before(jiffies, stop));
  170. }
  171. return -ETIME;
  172. }
  173. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  174. {
  175. int size = 0, burstcnt;
  176. while (size < count &&
  177. wait_for_stat(chip,
  178. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  179. chip->vendor.timeout_c,
  180. &chip->vendor.read_queue)
  181. == 0) {
  182. burstcnt = get_burstcount(chip);
  183. for (; burstcnt > 0 && size < count; burstcnt--)
  184. buf[size++] = ioread8(chip->vendor.iobase +
  185. TPM_DATA_FIFO(chip->vendor.
  186. locality));
  187. }
  188. return size;
  189. }
  190. static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  191. {
  192. int size = 0;
  193. int expected, status;
  194. if (count < TPM_HEADER_SIZE) {
  195. size = -EIO;
  196. goto out;
  197. }
  198. /* read first 10 bytes, including tag, paramsize, and result */
  199. if ((size =
  200. recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
  201. dev_err(chip->dev, "Unable to read header\n");
  202. goto out;
  203. }
  204. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  205. if (expected > count) {
  206. size = -EIO;
  207. goto out;
  208. }
  209. if ((size +=
  210. recv_data(chip, &buf[TPM_HEADER_SIZE],
  211. expected - TPM_HEADER_SIZE)) < expected) {
  212. dev_err(chip->dev, "Unable to read remainder of result\n");
  213. size = -ETIME;
  214. goto out;
  215. }
  216. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  217. &chip->vendor.int_queue);
  218. status = tpm_tis_status(chip);
  219. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  220. dev_err(chip->dev, "Error left over data\n");
  221. size = -EIO;
  222. goto out;
  223. }
  224. out:
  225. tpm_tis_ready(chip);
  226. release_locality(chip, chip->vendor.locality, 0);
  227. return size;
  228. }
  229. static int itpm;
  230. module_param(itpm, bool, 0444);
  231. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  232. /*
  233. * If interrupts are used (signaled by an irq set in the vendor structure)
  234. * tpm.c can skip polling for the data to be available as the interrupt is
  235. * waited for here
  236. */
  237. static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
  238. {
  239. int rc, status, burstcnt;
  240. size_t count = 0;
  241. u32 ordinal;
  242. if (request_locality(chip, 0) < 0)
  243. return -EBUSY;
  244. status = tpm_tis_status(chip);
  245. if ((status & TPM_STS_COMMAND_READY) == 0) {
  246. tpm_tis_ready(chip);
  247. if (wait_for_stat
  248. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  249. &chip->vendor.int_queue) < 0) {
  250. rc = -ETIME;
  251. goto out_err;
  252. }
  253. }
  254. while (count < len - 1) {
  255. burstcnt = get_burstcount(chip);
  256. for (; burstcnt > 0 && count < len - 1; burstcnt--) {
  257. iowrite8(buf[count], chip->vendor.iobase +
  258. TPM_DATA_FIFO(chip->vendor.locality));
  259. count++;
  260. }
  261. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  262. &chip->vendor.int_queue);
  263. status = tpm_tis_status(chip);
  264. if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
  265. rc = -EIO;
  266. goto out_err;
  267. }
  268. }
  269. /* write last byte */
  270. iowrite8(buf[count],
  271. chip->vendor.iobase +
  272. TPM_DATA_FIFO(chip->vendor.locality));
  273. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
  274. &chip->vendor.int_queue);
  275. status = tpm_tis_status(chip);
  276. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  277. rc = -EIO;
  278. goto out_err;
  279. }
  280. /* go and do it */
  281. iowrite8(TPM_STS_GO,
  282. chip->vendor.iobase + TPM_STS(chip->vendor.locality));
  283. if (chip->vendor.irq) {
  284. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  285. if (wait_for_stat
  286. (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  287. tpm_calc_ordinal_duration(chip, ordinal),
  288. &chip->vendor.read_queue) < 0) {
  289. rc = -ETIME;
  290. goto out_err;
  291. }
  292. }
  293. return len;
  294. out_err:
  295. tpm_tis_ready(chip);
  296. release_locality(chip, chip->vendor.locality, 0);
  297. return rc;
  298. }
  299. static const struct file_operations tis_ops = {
  300. .owner = THIS_MODULE,
  301. .llseek = no_llseek,
  302. .open = tpm_open,
  303. .read = tpm_read,
  304. .write = tpm_write,
  305. .release = tpm_release,
  306. };
  307. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  308. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  309. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  310. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  311. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  312. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
  313. NULL);
  314. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
  315. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  316. static struct attribute *tis_attrs[] = {
  317. &dev_attr_pubek.attr,
  318. &dev_attr_pcrs.attr,
  319. &dev_attr_enabled.attr,
  320. &dev_attr_active.attr,
  321. &dev_attr_owned.attr,
  322. &dev_attr_temp_deactivated.attr,
  323. &dev_attr_caps.attr,
  324. &dev_attr_cancel.attr, NULL,
  325. };
  326. static struct attribute_group tis_attr_grp = {
  327. .attrs = tis_attrs
  328. };
  329. static struct tpm_vendor_specific tpm_tis = {
  330. .status = tpm_tis_status,
  331. .recv = tpm_tis_recv,
  332. .send = tpm_tis_send,
  333. .cancel = tpm_tis_ready,
  334. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  335. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  336. .req_canceled = TPM_STS_COMMAND_READY,
  337. .attr_group = &tis_attr_grp,
  338. .miscdev = {
  339. .fops = &tis_ops,},
  340. };
  341. static irqreturn_t tis_int_probe(int irq, void *dev_id)
  342. {
  343. struct tpm_chip *chip = dev_id;
  344. u32 interrupt;
  345. interrupt = ioread32(chip->vendor.iobase +
  346. TPM_INT_STATUS(chip->vendor.locality));
  347. if (interrupt == 0)
  348. return IRQ_NONE;
  349. chip->vendor.irq = irq;
  350. /* Clear interrupts handled with TPM_EOI */
  351. iowrite32(interrupt,
  352. chip->vendor.iobase +
  353. TPM_INT_STATUS(chip->vendor.locality));
  354. return IRQ_HANDLED;
  355. }
  356. static irqreturn_t tis_int_handler(int dummy, void *dev_id)
  357. {
  358. struct tpm_chip *chip = dev_id;
  359. u32 interrupt;
  360. int i;
  361. interrupt = ioread32(chip->vendor.iobase +
  362. TPM_INT_STATUS(chip->vendor.locality));
  363. if (interrupt == 0)
  364. return IRQ_NONE;
  365. if (interrupt & TPM_INTF_DATA_AVAIL_INT)
  366. wake_up_interruptible(&chip->vendor.read_queue);
  367. if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
  368. for (i = 0; i < 5; i++)
  369. if (check_locality(chip, i) >= 0)
  370. break;
  371. if (interrupt &
  372. (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
  373. TPM_INTF_CMD_READY_INT))
  374. wake_up_interruptible(&chip->vendor.int_queue);
  375. /* Clear interrupts handled with TPM_EOI */
  376. iowrite32(interrupt,
  377. chip->vendor.iobase +
  378. TPM_INT_STATUS(chip->vendor.locality));
  379. ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
  380. return IRQ_HANDLED;
  381. }
  382. static int interrupts = 1;
  383. module_param(interrupts, bool, 0444);
  384. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  385. static int tpm_tis_init(struct device *dev, resource_size_t start,
  386. resource_size_t len, unsigned int irq)
  387. {
  388. u32 vendor, intfcaps, intmask;
  389. int rc, i;
  390. struct tpm_chip *chip;
  391. if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
  392. return -ENODEV;
  393. chip->vendor.iobase = ioremap(start, len);
  394. if (!chip->vendor.iobase) {
  395. rc = -EIO;
  396. goto out_err;
  397. }
  398. /* Default timeouts */
  399. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  400. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  401. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  402. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  403. if (request_locality(chip, 0) != 0) {
  404. rc = -ENODEV;
  405. goto out_err;
  406. }
  407. vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
  408. dev_info(dev,
  409. "1.2 TPM (device-id 0x%X, rev-id %d)\n",
  410. vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
  411. if (itpm)
  412. dev_info(dev, "Intel iTPM workaround enabled\n");
  413. /* Figure out the capabilities */
  414. intfcaps =
  415. ioread32(chip->vendor.iobase +
  416. TPM_INTF_CAPS(chip->vendor.locality));
  417. dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  418. intfcaps);
  419. if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
  420. dev_dbg(dev, "\tBurst Count Static\n");
  421. if (intfcaps & TPM_INTF_CMD_READY_INT)
  422. dev_dbg(dev, "\tCommand Ready Int Support\n");
  423. if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
  424. dev_dbg(dev, "\tInterrupt Edge Falling\n");
  425. if (intfcaps & TPM_INTF_INT_EDGE_RISING)
  426. dev_dbg(dev, "\tInterrupt Edge Rising\n");
  427. if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
  428. dev_dbg(dev, "\tInterrupt Level Low\n");
  429. if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
  430. dev_dbg(dev, "\tInterrupt Level High\n");
  431. if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
  432. dev_dbg(dev, "\tLocality Change Int Support\n");
  433. if (intfcaps & TPM_INTF_STS_VALID_INT)
  434. dev_dbg(dev, "\tSts Valid Int Support\n");
  435. if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
  436. dev_dbg(dev, "\tData Avail Int Support\n");
  437. /* INTERRUPT Setup */
  438. init_waitqueue_head(&chip->vendor.read_queue);
  439. init_waitqueue_head(&chip->vendor.int_queue);
  440. intmask =
  441. ioread32(chip->vendor.iobase +
  442. TPM_INT_ENABLE(chip->vendor.locality));
  443. intmask |= TPM_INTF_CMD_READY_INT
  444. | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
  445. | TPM_INTF_STS_VALID_INT;
  446. iowrite32(intmask,
  447. chip->vendor.iobase +
  448. TPM_INT_ENABLE(chip->vendor.locality));
  449. if (interrupts)
  450. chip->vendor.irq = irq;
  451. if (interrupts && !chip->vendor.irq) {
  452. chip->vendor.irq =
  453. ioread8(chip->vendor.iobase +
  454. TPM_INT_VECTOR(chip->vendor.locality));
  455. for (i = 3; i < 16 && chip->vendor.irq == 0; i++) {
  456. iowrite8(i, chip->vendor.iobase +
  457. TPM_INT_VECTOR(chip->vendor.locality));
  458. if (request_irq
  459. (i, tis_int_probe, IRQF_SHARED,
  460. chip->vendor.miscdev.name, chip) != 0) {
  461. dev_info(chip->dev,
  462. "Unable to request irq: %d for probe\n",
  463. i);
  464. continue;
  465. }
  466. /* Clear all existing */
  467. iowrite32(ioread32
  468. (chip->vendor.iobase +
  469. TPM_INT_STATUS(chip->vendor.locality)),
  470. chip->vendor.iobase +
  471. TPM_INT_STATUS(chip->vendor.locality));
  472. /* Turn on */
  473. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  474. chip->vendor.iobase +
  475. TPM_INT_ENABLE(chip->vendor.locality));
  476. /* Generate Interrupts */
  477. tpm_gen_interrupt(chip);
  478. /* Turn off */
  479. iowrite32(intmask,
  480. chip->vendor.iobase +
  481. TPM_INT_ENABLE(chip->vendor.locality));
  482. free_irq(i, chip);
  483. }
  484. }
  485. if (chip->vendor.irq) {
  486. iowrite8(chip->vendor.irq,
  487. chip->vendor.iobase +
  488. TPM_INT_VECTOR(chip->vendor.locality));
  489. if (request_irq
  490. (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
  491. chip->vendor.miscdev.name, chip) != 0) {
  492. dev_info(chip->dev,
  493. "Unable to request irq: %d for use\n",
  494. chip->vendor.irq);
  495. chip->vendor.irq = 0;
  496. } else {
  497. /* Clear all existing */
  498. iowrite32(ioread32
  499. (chip->vendor.iobase +
  500. TPM_INT_STATUS(chip->vendor.locality)),
  501. chip->vendor.iobase +
  502. TPM_INT_STATUS(chip->vendor.locality));
  503. /* Turn on */
  504. iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
  505. chip->vendor.iobase +
  506. TPM_INT_ENABLE(chip->vendor.locality));
  507. }
  508. }
  509. INIT_LIST_HEAD(&chip->vendor.list);
  510. spin_lock(&tis_lock);
  511. list_add(&chip->vendor.list, &tis_chips);
  512. spin_unlock(&tis_lock);
  513. tpm_get_timeouts(chip);
  514. tpm_continue_selftest(chip);
  515. return 0;
  516. out_err:
  517. if (chip->vendor.iobase)
  518. iounmap(chip->vendor.iobase);
  519. tpm_remove_hardware(chip->dev);
  520. return rc;
  521. }
  522. #ifdef CONFIG_PNP
  523. static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  524. const struct pnp_device_id *pnp_id)
  525. {
  526. resource_size_t start, len;
  527. unsigned int irq = 0;
  528. start = pnp_mem_start(pnp_dev, 0);
  529. len = pnp_mem_len(pnp_dev, 0);
  530. if (pnp_irq_valid(pnp_dev, 0))
  531. irq = pnp_irq(pnp_dev, 0);
  532. else
  533. interrupts = 0;
  534. return tpm_tis_init(&pnp_dev->dev, start, len, irq);
  535. }
  536. static int tpm_tis_pnp_suspend(struct pnp_dev *dev, pm_message_t msg)
  537. {
  538. return tpm_pm_suspend(&dev->dev, msg);
  539. }
  540. static int tpm_tis_pnp_resume(struct pnp_dev *dev)
  541. {
  542. struct tpm_chip *chip = pnp_get_drvdata(dev);
  543. int ret;
  544. ret = tpm_pm_resume(&dev->dev);
  545. if (!ret)
  546. tpm_continue_selftest(chip);
  547. return ret;
  548. }
  549. static struct pnp_device_id tpm_pnp_tbl[] __devinitdata = {
  550. {"PNP0C31", 0}, /* TPM */
  551. {"ATM1200", 0}, /* Atmel */
  552. {"IFX0102", 0}, /* Infineon */
  553. {"BCM0101", 0}, /* Broadcom */
  554. {"BCM0102", 0}, /* Broadcom */
  555. {"NSC1200", 0}, /* National */
  556. {"ICO0102", 0}, /* Intel */
  557. /* Add new here */
  558. {"", 0}, /* User Specified */
  559. {"", 0} /* Terminator */
  560. };
  561. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  562. static __devexit void tpm_tis_pnp_remove(struct pnp_dev *dev)
  563. {
  564. struct tpm_chip *chip = pnp_get_drvdata(dev);
  565. tpm_dev_vendor_release(chip);
  566. kfree(chip);
  567. }
  568. static struct pnp_driver tis_pnp_driver = {
  569. .name = "tpm_tis",
  570. .id_table = tpm_pnp_tbl,
  571. .probe = tpm_tis_pnp_init,
  572. .suspend = tpm_tis_pnp_suspend,
  573. .resume = tpm_tis_pnp_resume,
  574. .remove = tpm_tis_pnp_remove,
  575. };
  576. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  577. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  578. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  579. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  580. #endif
  581. static int tpm_tis_suspend(struct platform_device *dev, pm_message_t msg)
  582. {
  583. return tpm_pm_suspend(&dev->dev, msg);
  584. }
  585. static int tpm_tis_resume(struct platform_device *dev)
  586. {
  587. return tpm_pm_resume(&dev->dev);
  588. }
  589. static struct platform_driver tis_drv = {
  590. .driver = {
  591. .name = "tpm_tis",
  592. .owner = THIS_MODULE,
  593. },
  594. .suspend = tpm_tis_suspend,
  595. .resume = tpm_tis_resume,
  596. };
  597. static struct platform_device *pdev;
  598. static int force;
  599. module_param(force, bool, 0444);
  600. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  601. static int __init init_tis(void)
  602. {
  603. int rc;
  604. #ifdef CONFIG_PNP
  605. if (!force)
  606. return pnp_register_driver(&tis_pnp_driver);
  607. #endif
  608. rc = platform_driver_register(&tis_drv);
  609. if (rc < 0)
  610. return rc;
  611. if (IS_ERR(pdev=platform_device_register_simple("tpm_tis", -1, NULL, 0)))
  612. return PTR_ERR(pdev);
  613. if((rc=tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0)) != 0) {
  614. platform_device_unregister(pdev);
  615. platform_driver_unregister(&tis_drv);
  616. }
  617. return rc;
  618. }
  619. static void __exit cleanup_tis(void)
  620. {
  621. struct tpm_vendor_specific *i, *j;
  622. struct tpm_chip *chip;
  623. spin_lock(&tis_lock);
  624. list_for_each_entry_safe(i, j, &tis_chips, list) {
  625. chip = to_tpm_chip(i);
  626. tpm_remove_hardware(chip->dev);
  627. iowrite32(~TPM_GLOBAL_INT_ENABLE &
  628. ioread32(chip->vendor.iobase +
  629. TPM_INT_ENABLE(chip->vendor.
  630. locality)),
  631. chip->vendor.iobase +
  632. TPM_INT_ENABLE(chip->vendor.locality));
  633. release_locality(chip, chip->vendor.locality, 1);
  634. if (chip->vendor.irq)
  635. free_irq(chip->vendor.irq, chip);
  636. iounmap(i->iobase);
  637. list_del(&i->list);
  638. }
  639. spin_unlock(&tis_lock);
  640. #ifdef CONFIG_PNP
  641. if (!force) {
  642. pnp_unregister_driver(&tis_pnp_driver);
  643. return;
  644. }
  645. #endif
  646. platform_device_unregister(pdev);
  647. platform_driver_unregister(&tis_drv);
  648. }
  649. module_init(init_tis);
  650. module_exit(cleanup_tis);
  651. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  652. MODULE_DESCRIPTION("TPM Driver");
  653. MODULE_VERSION("2.0");
  654. MODULE_LICENSE("GPL");