2 * Copyright (c) 2008, 2009 Michael Shalayeff
3 * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
15 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* #define TPM_DEBUG */
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
31 #include <sys/module.h>
36 #include <machine/bus.h>
38 #include <machine/resource.h>
40 #include <machine/md_var.h>
42 #include <isa/isareg.h>
43 #include <isa/isavar.h>
45 #include <sys/device.h>
47 #include <machine/cpu.h>
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50 #include <machine/conf.h>
52 #include <dev/isa/isareg.h>
53 #include <dev/isa/isavar.h>
55 #include <dev/tpm/tpmvar.h>
58 /* XXX horrible hack for tcsd (-lpthread) workaround on OpenBSD */
63 #define TPM_BUFSIZ 1024
65 #define TPM_HDRSIZE 10
67 #define TPM_PARAM_SIZE 0x0001
73 #define TPM_ACCESS 0x0000 /* access register */
74 #define TPM_ACCESS_ESTABLISHMENT 0x01 /* establishment */
75 #define TPM_ACCESS_REQUEST_USE 0x02 /* request using locality */
76 #define TPM_ACCESS_REQUEST_PENDING 0x04 /* pending request */
77 #define TPM_ACCESS_SEIZE 0x08 /* request locality seize */
78 #define TPM_ACCESS_SEIZED 0x10 /* locality has been seized */
79 #define TPM_ACCESS_ACTIVE_LOCALITY 0x20 /* locality is active */
80 #define TPM_ACCESS_VALID 0x80 /* bits are valid */
81 #define TPM_ACCESS_BITS \
82 "\020\01EST\02REQ\03PEND\04SEIZE\05SEIZED\06ACT\010VALID"
84 #define TPM_INTERRUPT_ENABLE 0x0008
85 #define TPM_GLOBAL_INT_ENABLE 0x80000000 /* enable ints */
86 #define TPM_CMD_READY_INT 0x00000080 /* cmd ready enable */
87 #define TPM_INT_EDGE_FALLING 0x00000018
88 #define TPM_INT_EDGE_RISING 0x00000010
89 #define TPM_INT_LEVEL_LOW 0x00000008
90 #define TPM_INT_LEVEL_HIGH 0x00000000
91 #define TPM_LOCALITY_CHANGE_INT 0x00000004 /* locality change enable */
92 #define TPM_STS_VALID_INT 0x00000002 /* int on TPM_STS_VALID is set */
93 #define TPM_DATA_AVAIL_INT 0x00000001 /* int on TPM_STS_DATA_AVAIL is set */
94 #define TPM_INTERRUPT_ENABLE_BITS \
95 "\020\040ENA\010RDY\03LOCH\02STSV\01DRDY"
97 #define TPM_INT_VECTOR 0x000c /* 8 bit reg for 4 bit irq vector */
98 #define TPM_INT_STATUS 0x0010 /* bits are & 0x87 from TPM_INTERRUPT_ENABLE */
100 #define TPM_INTF_CAPABILITIES 0x0014 /* capability register */
101 #define TPM_INTF_BURST_COUNT_STATIC 0x0100 /* TPM_STS_BMASK static */
102 #define TPM_INTF_CMD_READY_INT 0x0080 /* int on ready supported */
103 #define TPM_INTF_INT_EDGE_FALLING 0x0040 /* falling edge ints supported */
104 #define TPM_INTF_INT_EDGE_RISING 0x0020 /* rising edge ints supported */
105 #define TPM_INTF_INT_LEVEL_LOW 0x0010 /* level-low ints supported */
106 #define TPM_INTF_INT_LEVEL_HIGH 0x0008 /* level-high ints supported */
107 #define TPM_INTF_LOCALITY_CHANGE_INT 0x0004 /* locality-change int (mb 1) */
108 #define TPM_INTF_STS_VALID_INT 0x0002 /* TPM_STS_VALID int supported */
109 #define TPM_INTF_DATA_AVAIL_INT 0x0001 /* TPM_STS_DATA_AVAIL int supported (mb 1) */
110 #define TPM_CAPSREQ \
111 (TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT|TPM_INTF_INT_LEVEL_LOW)
112 #define TPM_CAPBITS \
113 "\020\01IDRDY\02ISTSV\03ILOCH\04IHIGH\05ILOW\06IEDGE\07IFALL\010IRDY\011BCST"
115 #define TPM_STS 0x0018 /* status register */
116 #define TPM_STS_MASK 0x000000ff /* status bits */
117 #define TPM_STS_BMASK 0x00ffff00 /* ro io burst size */
118 #define TPM_STS_VALID 0x00000080 /* ro other bits are valid */
119 #define TPM_STS_CMD_READY 0x00000040 /* rw chip/signal ready */
120 #define TPM_STS_GO 0x00000020 /* wo start the command */
121 #define TPM_STS_DATA_AVAIL 0x00000010 /* ro data available */
122 #define TPM_STS_DATA_EXPECT 0x00000008 /* ro more data to be written */
123 #define TPM_STS_RESP_RETRY 0x00000002 /* wo resend the response */
124 #define TPM_STS_BITS "\020\010VALID\07RDY\06GO\05DRDY\04EXPECT\02RETRY"
126 #define TPM_DATA 0x0024
127 #define TPM_ID 0x0f00
128 #define TPM_REV 0x0f04
129 #define TPM_SIZE 0x5000 /* five pages of the above */
131 #define TPM_ACCESS_TMO 2000 /* 2sec */
132 #define TPM_READY_TMO 2000 /* 2sec */
133 #define TPM_READ_TMO 120000 /* 2 minutes */
134 #define TPM_BURST_TMO 2000 /* 2sec */
136 #define TPM_LEGACY_BUSY 0x01
137 #define TPM_LEGACY_ABRT 0x01
138 #define TPM_LEGACY_DA 0x02
139 #define TPM_LEGACY_RE 0x04
140 #define TPM_LEGACY_LAST 0x04
141 #define TPM_LEGACY_BITS "\020\01BUSY\2DA\3RE\4LAST"
142 #define TPM_LEGACY_TMO (2*60) /* sec */
143 #define TPM_LEGACY_SLEEP 5 /* ticks */
144 #define TPM_LEGACY_DELAY 100
146 /* Set when enabling legacy interface in host bridge. */
151 #define TPMSOFTC(dev) \
152 ((struct tpm_softc *)dev->si_drv1)
160 static struct cdevsw tpm_cdevsw = {
161 .d_version = D_VERSION,
162 .d_flags = D_NEEDGIANT,
171 #define TPMSOFTC(dev) \
172 (struct tpm_softc *)device_lookup(&tpm_cd, minor(dev))
174 struct cfdriver tpm_cd = {
178 int tpm_match(device_t , void *, void *);
179 void tpm_attach(device_t , device_t , void *);
181 struct cfattach tpm_ca = {
182 sizeof(struct tpm_softc), tpm_match, tpm_attach
190 #define TPM_DEV_NOINTS 0x0001
192 { 0x000615d1, "IFX SLD 9630 TT 1.1", 0 },
193 { 0x000b15d1, "IFX SLB 9635 TT 1.2", 0 },
194 { 0x100214e4, "Broadcom BCM0102", TPM_DEV_NOINTS },
195 { 0x00fe1050, "WEC WPCT200", 0 },
196 { 0x687119fa, "SNS SSX35", 0 },
197 { 0x2e4d5453, "STM ST19WP18", 0 },
198 { 0x32021114, "ATML 97SC3203", TPM_DEV_NOINTS },
199 { 0x10408086, "INTEL INTC0102", 0 },
200 { 0, "", TPM_DEV_NOINTS },
203 int tpm_tis12_irqinit(struct tpm_softc *, int, int);
204 int tpm_tis12_init(struct tpm_softc *, int, const char *);
205 int tpm_tis12_start(struct tpm_softc *, int);
206 int tpm_tis12_read(struct tpm_softc *, void *, int, size_t *, int);
207 int tpm_tis12_write(struct tpm_softc *, void *, int);
208 int tpm_tis12_end(struct tpm_softc *, int, int);
211 void tpm_intr(void *);
213 int tpm_intr(void *);
214 void tpm_powerhook(int, void *);
215 int tpm_suspend(struct tpm_softc *, int);
216 int tpm_resume(struct tpm_softc *, int);
219 int tpm_waitfor_poll(struct tpm_softc *, u_int8_t, int, void *);
220 int tpm_waitfor_int(struct tpm_softc *, u_int8_t, int, void *, int);
221 int tpm_waitfor(struct tpm_softc *, u_int8_t, int, void *);
222 int tpm_request_locality(struct tpm_softc *, int);
223 int tpm_getburst(struct tpm_softc *);
224 u_int8_t tpm_status(struct tpm_softc *);
225 int tpm_tmotohz(int);
227 int tpm_legacy_probe(bus_space_tag_t, bus_addr_t);
228 int tpm_legacy_init(struct tpm_softc *, int, const char *);
229 int tpm_legacy_start(struct tpm_softc *, int);
230 int tpm_legacy_read(struct tpm_softc *, void *, int, size_t *, int);
231 int tpm_legacy_write(struct tpm_softc *, void *, int);
232 int tpm_legacy_end(struct tpm_softc *, int, int);
237 * FreeBSD specific code for probing and attaching TPM to device tree.
241 tpm_identify(driver_t *driver, device_t parent)
243 BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "tpm", 0);
249 tpm_attach(device_t dev)
251 struct tpm_softc *sc = device_get_softc(dev);
255 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
257 if (sc->mem_res == NULL)
260 sc->sc_bt = rman_get_bustag(sc->mem_res);
261 sc->sc_bh = rman_get_bushandle(sc->mem_res);
264 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
265 RF_ACTIVE | RF_SHAREABLE);
266 if (sc->irq_res != NULL)
267 irq = rman_get_start(sc->irq_res);
271 /* In case PnP probe this may contain some initialization. */
272 tpm_tis12_probe(sc->sc_bt, sc->sc_bh);
274 if (tpm_legacy_probe(sc->sc_bt, sc->sc_bh)) {
275 sc->sc_init = tpm_legacy_init;
276 sc->sc_start = tpm_legacy_start;
277 sc->sc_read = tpm_legacy_read;
278 sc->sc_write = tpm_legacy_write;
279 sc->sc_end = tpm_legacy_end;
281 sc->sc_init = tpm_tis12_init;
282 sc->sc_start = tpm_tis12_start;
283 sc->sc_read = tpm_tis12_read;
284 sc->sc_write = tpm_tis12_write;
285 sc->sc_end = tpm_tis12_end;
288 printf("%s", device_get_name(dev));
289 if ((sc->sc_init)(sc, irq, "tpm")) {
294 if (sc->sc_init == tpm_tis12_init && sc->irq_res != NULL &&
295 bus_setup_intr(dev, sc->irq_res, INTR_TYPE_TTY, NULL,
296 tpm_intr, sc, &sc->intr_cookie) != 0) {
298 printf(": cannot establish interrupt\n");
302 sc->sc_cdev = make_dev(&tpm_cdevsw, device_get_unit(dev),
303 UID_ROOT, GID_WHEEL, 0600, "tpm");
304 sc->sc_cdev->si_drv1 = sc;
310 tpm_detach(device_t dev)
312 struct tpm_softc * sc = device_get_softc(dev);
315 bus_teardown_intr(dev, sc->irq_res, sc->intr_cookie);
319 bus_release_resource(dev, SYS_RES_MEMORY,
320 sc->mem_rid, sc->mem_res);
324 bus_release_resource(dev, SYS_RES_IRQ,
325 sc->irq_rid, sc->irq_res);
328 destroy_dev(sc->sc_cdev);
337 * OpenBSD specific code for probing and attaching TPM to device tree.
340 tpm_match(device_t parent, void *match, void *aux)
342 struct isa_attach_args *ia = aux;
343 struct cfdata *cf = match;
344 bus_space_tag_t bt = ia->ia_memt;
345 bus_space_handle_t bh;
348 /* There can be only one. */
352 if (tpm_legacy_probe(ia->ia_iot, ia->ia_iobase)) {
357 if (ia->ia_maddr == -1)
360 if (bus_space_map(bt, ia->ia_maddr, TPM_SIZE, 0, &bh))
363 if ((rv = tpm_tis12_probe(bt, bh))) {
365 ia->ia_msize = TPM_SIZE;
368 bus_space_unmap(bt, bh, TPM_SIZE);
373 tpm_attach(device_t parent, device_t self, void *aux)
375 struct tpm_softc *sc = (struct tpm_softc *)self;
376 struct isa_attach_args *ia = aux;
381 if (tpm_legacy_probe(ia->ia_iot, ia->ia_iobase)) {
382 sc->sc_bt = ia->ia_iot;
383 iobase = ia->ia_iobase;
384 size = ia->ia_iosize;
385 sc->sc_batm = ia->ia_iot;
386 sc->sc_init = tpm_legacy_init;
387 sc->sc_start = tpm_legacy_start;
388 sc->sc_read = tpm_legacy_read;
389 sc->sc_write = tpm_legacy_write;
390 sc->sc_end = tpm_legacy_end;
392 sc->sc_bt = ia->ia_memt;
393 iobase = ia->ia_maddr;
395 sc->sc_init = tpm_tis12_init;
396 sc->sc_start = tpm_tis12_start;
397 sc->sc_read = tpm_tis12_read;
398 sc->sc_write = tpm_tis12_write;
399 sc->sc_end = tpm_tis12_end;
402 if (bus_space_map(sc->sc_bt, iobase, size, 0, &sc->sc_bh)) {
403 printf(": cannot map registers\n");
407 if ((rv = (sc->sc_init)(sc, ia->ia_irq, sc->sc_dev.dv_xname))) {
408 bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
413 * Only setup interrupt handler when we have a vector and the
414 * chip is TIS 1.2 compliant.
416 if (sc->sc_init == tpm_tis12_init && ia->ia_irq != IRQUNK &&
417 (sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
418 IPL_TTY, tpm_intr, sc, sc->sc_dev.dv_xname)) == NULL) {
419 bus_space_unmap(sc->sc_bt, sc->sc_bh, TPM_SIZE);
420 printf("%s: cannot establish interrupt\n",
421 sc->sc_dev.dv_xname);
425 sc->sc_suspend = PWR_RESUME;
426 sc->sc_powerhook = powerhook_establish(tpm_powerhook, sc);
430 /* Probe TPM using TIS 1.2 interface. */
432 tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
437 r = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITIES);
442 printf("tpm: caps=%b\n", r, TPM_CAPBITS);
444 if ((r & TPM_CAPSREQ) != TPM_CAPSREQ ||
445 !(r & (TPM_INTF_INT_EDGE_RISING | TPM_INTF_INT_LEVEL_LOW))) {
447 printf("tpm: caps too low (caps=%b)\n", r, TPM_CAPBITS);
452 save = bus_space_read_1(bt, bh, TPM_ACCESS);
453 bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
454 reg = bus_space_read_1(bt, bh, TPM_ACCESS);
455 if ((reg & TPM_ACCESS_VALID) && (reg & TPM_ACCESS_ACTIVE_LOCALITY) &&
456 bus_space_read_4(bt, bh, TPM_ID) != 0xffffffff)
459 bus_space_write_1(bt, bh, TPM_ACCESS, save);
464 * Setup interrupt vector if one is provided and interrupts are know to
465 * work on that particular chip.
468 tpm_tis12_irqinit(struct tpm_softc *sc, int irq, int idx)
472 if ((irq == IRQUNK) || (tpm_devs[idx].flags & TPM_DEV_NOINTS)) {
473 sc->sc_vector = IRQUNK;
477 /* Ack and disable all interrupts. */
478 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE,
479 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE) &
480 ~TPM_GLOBAL_INT_ENABLE);
481 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INT_STATUS,
482 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INT_STATUS));
484 /* Program interrupt vector. */
485 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_INT_VECTOR, irq);
488 /* Program interrupt type. */
489 if (sc->sc_capabilities & TPM_INTF_INT_EDGE_RISING)
490 r = TPM_INT_EDGE_RISING;
491 else if (sc->sc_capabilities & TPM_INTF_INT_LEVEL_HIGH)
492 r = TPM_INT_LEVEL_HIGH;
494 r = TPM_INT_LEVEL_LOW;
495 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE, r);
500 /* Setup TPM using TIS 1.2 interface. */
502 tpm_tis12_init(struct tpm_softc *sc, int irq, const char *name)
507 r = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTF_CAPABILITIES);
509 printf(" caps=%b ", r, TPM_CAPBITS);
511 if ((r & TPM_CAPSREQ) != TPM_CAPSREQ ||
512 !(r & (TPM_INTF_INT_EDGE_RISING | TPM_INTF_INT_LEVEL_LOW))) {
513 printf(": capabilities too low (caps=%b)\n", r, TPM_CAPBITS);
516 sc->sc_capabilities = r;
518 sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
519 sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
521 for (i = 0; tpm_devs[i].devid; i++)
522 if (tpm_devs[i].devid == sc->sc_devid)
525 if (tpm_devs[i].devid)
526 printf(": %s rev 0x%x\n", tpm_devs[i].name, sc->sc_rev);
528 printf(": device 0x%08x rev 0x%x\n", sc->sc_devid, sc->sc_rev);
530 if (tpm_tis12_irqinit(sc, irq, i))
533 if (tpm_request_locality(sc, 0))
536 /* Abort whatever it thought it was doing. */
537 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
543 tpm_request_locality(struct tpm_softc *sc, int l)
551 if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
552 (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
553 (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
556 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
557 TPM_ACCESS_REQUEST_USE);
559 to = tpm_tmotohz(TPM_ACCESS_TMO);
561 while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
562 (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
563 (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
564 rv = tsleep(sc->sc_init, PRIBIO | PCATCH, "tpm_locality", 1);
565 if (rv && rv != EWOULDBLOCK) {
567 printf("tpm_request_locality: interrupted %d\n", rv);
573 if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
574 (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
576 printf("tpm_request_locality: access %b\n", r, TPM_ACCESS_BITS);
585 tpm_getburst(struct tpm_softc *sc)
589 to = tpm_tmotohz(TPM_BURST_TMO);
592 while (burst == 0 && to--) {
594 * Burst count has to be read from bits 8 to 23 without
595 * touching any other bits, eg. the actual status bits 0
598 burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
599 burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
602 printf("tpm_getburst: read %d\n", burst);
607 rv = tsleep(sc, PRIBIO | PCATCH, "tpm_getburst", 1);
608 if (rv && rv != EWOULDBLOCK) {
617 tpm_status(struct tpm_softc *sc)
621 status = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
632 tv.tv_sec = tmo / 1000;
633 tv.tv_usec = 1000 * (tmo % 1000);
638 /* Save TPM state on suspend. */
641 tpm_suspend(device_t dev)
643 tpm_suspend(struct tpm_softc *sc, int why)
647 struct tpm_softc *sc = device_get_softc(dev);
650 u_int8_t command[] = {
651 0, 193, /* TPM_TAG_RQU_COMMAND */
652 0, 0, 0, 10, /* Length in bytes */
653 0, 0, 0, 156 /* TPM_ORD_SaveStates */
657 * Power down: We have to issue the SaveStates command.
659 sc->sc_write(sc, &command, sizeof(command));
660 sc->sc_read(sc, &command, sizeof(command), NULL, TPM_HDRSIZE);
662 printf("tpm_suspend: power down: %d -> %d\n", sc->sc_suspend, why);
664 sc->sc_suspend = why;
670 * Handle resume event. Actually nothing to do as the BIOS is supposed
671 * to restore the previously saved state.
675 tpm_resume(device_t dev)
677 tpm_resume(struct tpm_softc *sc, int why)
681 struct tpm_softc *sc = device_get_softc(dev);
685 printf("tpm_resume: resume: %d -> %d\n", sc->sc_suspend, why);
687 sc->sc_suspend = why;
692 /* Dispatch suspend and resume events. */
695 tpm_powerhook(int why, void *self)
697 struct tpm_softc *sc = (struct tpm_softc *)self;
699 if (why != PWR_RESUME)
700 tpm_suspend(sc, why);
704 #endif /* !__FreeBSD__ */
706 /* Wait for given status bits using polling. */
708 tpm_waitfor_poll(struct tpm_softc *sc, u_int8_t mask, int tmo, void *c)
713 * Poll until either the requested condition or a time out is
716 while (((sc->sc_stat = tpm_status(sc)) & mask) != mask && tmo--) {
717 rv = tsleep(c, PRIBIO | PCATCH, "tpm_poll", 1);
718 if (rv && rv != EWOULDBLOCK) {
720 printf("tpm_waitfor_poll: interrupted %d\n", rv);
729 /* Wait for given status bits using interrupts. */
731 tpm_waitfor_int(struct tpm_softc *sc, u_int8_t mask, int tmo, void *c,
736 /* Poll and return when condition is already met. */
737 sc->sc_stat = tpm_status(sc);
738 if ((sc->sc_stat & mask) == mask)
742 * Enable interrupt on tpm chip. Note that interrupts on our
743 * level (SPL_TTY) are disabled (see tpm{read,write} et al) and
744 * will not be delivered to the cpu until we call tsleep(9) below.
746 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE,
747 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE) |
749 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE,
750 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE) |
751 TPM_GLOBAL_INT_ENABLE);
754 * Poll once more to remedy the race between previous polling
755 * and enabling interrupts on the tpm chip.
757 sc->sc_stat = tpm_status(sc);
758 if ((sc->sc_stat & mask) == mask) {
763 to = tpm_tmotohz(tmo);
765 printf("tpm_waitfor_int: sleeping for %d ticks on %p\n", to, c);
768 * tsleep(9) enables interrupts on the cpu and returns after
769 * wake up with interrupts disabled again. Note that interrupts
770 * generated by the tpm chip while being at SPL_TTY are not lost
771 * but held and delivered as soon as the cpu goes below SPL_TTY.
773 rv = tsleep(c, PRIBIO | PCATCH, "tpm_intr", to);
775 sc->sc_stat = tpm_status(sc);
777 printf("tpm_waitfor_int: woke up with rv %d stat %b\n", rv,
778 sc->sc_stat, TPM_STS_BITS);
780 if ((sc->sc_stat & mask) == mask)
783 /* Disable interrupts on tpm chip again. */
784 out: bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE,
785 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE) &
786 ~TPM_GLOBAL_INT_ENABLE);
787 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE,
788 bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INTERRUPT_ENABLE) &
795 * Wait on given status bits, uses interrupts where possible, otherwise polls.
798 tpm_waitfor(struct tpm_softc *sc, u_int8_t b0, int tmo, void *c)
804 printf("tpm_waitfor: b0 %b\n", b0, TPM_STS_BITS);
808 * If possible, use interrupts, otherwise poll.
810 * We use interrupts for TPM_STS_VALID and TPM_STS_DATA_AVAIL (if
811 * the tpm chips supports them) as waiting for those can take
812 * really long. The other TPM_STS* are not needed very often
813 * so we do not support them.
815 if (sc->sc_vector != IRQUNK) {
819 * Wait for data ready. This interrupt only occurs
820 * when both TPM_STS_VALID and TPM_STS_DATA_AVAIL are asserted.
821 * Thus we don't have to bother with TPM_STS_VALID
822 * separately and can just return.
824 * This only holds for interrupts! When using polling
825 * both flags have to be waited for, see below.
827 if ((b & TPM_STS_DATA_AVAIL) && (sc->sc_capabilities &
828 TPM_INTF_DATA_AVAIL_INT))
829 return tpm_waitfor_int(sc, b, tmo, c,
832 /* Wait for status valid bit. */
833 if ((b & TPM_STS_VALID) && (sc->sc_capabilities &
834 TPM_INTF_STS_VALID_INT)) {
835 rv = tpm_waitfor_int(sc, b, tmo, c, TPM_STS_VALID_INT);
839 b = b0 & ~TPM_STS_VALID;
843 * When all flags are taken care of, return. Otherwise
844 * use polling for eg. TPM_STS_CMD_READY.
853 * If requested wait for TPM_STS_VALID before dealing with
854 * any other flag. Eg. when both TPM_STS_DATA_AVAIL and TPM_STS_VALID
855 * are requested, wait for the latter first.
858 if (b0 & TPM_STS_VALID)
861 to = tpm_tmotohz(tmo);
863 if ((rv = tpm_waitfor_poll(sc, b, to, c)) != 0)
866 if ((b & sc->sc_stat) == TPM_STS_VALID) {
867 /* Now wait for other flags. */
868 b = b0 & ~TPM_STS_VALID;
873 if ((sc->sc_stat & b) != b) {
875 printf("tpm_waitfor: timeout: stat=%b b=%b\n",
876 sc->sc_stat, TPM_STS_BITS, b, TPM_STS_BITS);
878 if (re-- && (b0 & TPM_STS_VALID)) {
879 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
889 /* Start transaction. */
891 tpm_tis12_start(struct tpm_softc *sc, int flag)
895 if (flag == UIO_READ) {
896 rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
897 TPM_READ_TMO, sc->sc_read);
901 /* Own our (0th) locality. */
902 if ((rv = tpm_request_locality(sc, 0)) != 0)
905 sc->sc_stat = tpm_status(sc);
906 if (sc->sc_stat & TPM_STS_CMD_READY) {
908 printf("tpm_tis12_start: UIO_WRITE status %b\n", sc->sc_stat,
915 printf("tpm_tis12_start: UIO_WRITE readying chip\n");
918 /* Abort previous and restart. */
919 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
920 if ((rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO,
923 printf("tpm_tis12_start: UIO_WRITE readying failed %d\n", rv);
929 printf("tpm_tis12_start: UIO_WRITE readying done\n");
936 tpm_tis12_read(struct tpm_softc *sc, void *buf, int len, size_t *count,
944 printf("tpm_tis12_read: len %d\n", len);
948 if ((rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
949 TPM_READ_TMO, sc->sc_read)))
952 bcnt = tpm_getburst(sc);
955 printf("tpm_tis12_read: fetching %d, burst is %d\n", n, bcnt);
958 *p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
962 if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
966 printf("tpm_tis12_read: read %zd bytes, len %d\n", cnt, len);
976 tpm_tis12_write(struct tpm_softc *sc, void *buf, int len)
983 printf("tpm_tis12_write: sc %p buf %p len %d\n", sc, buf, len);
986 if ((rv = tpm_request_locality(sc, 0)) != 0)
990 while (cnt < len - 1) {
991 for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
992 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
995 if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
997 printf("tpm_tis12_write: failed burst rv %d\n", rv);
1001 sc->sc_stat = tpm_status(sc);
1002 if (!(sc->sc_stat & TPM_STS_DATA_EXPECT)) {
1004 printf("tpm_tis12_write: failed rv %d stat=%b\n", rv,
1005 sc->sc_stat, TPM_STS_BITS);
1011 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
1014 if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
1016 printf("tpm_tis12_write: failed last byte rv %d\n", rv);
1020 if ((sc->sc_stat & TPM_STS_DATA_EXPECT) != 0) {
1022 printf("tpm_tis12_write: failed rv %d stat=%b\n", rv,
1023 sc->sc_stat, TPM_STS_BITS);
1029 printf("tpm_tis12_write: wrote %d byte\n", cnt);
1035 /* Finish transaction. */
1037 tpm_tis12_end(struct tpm_softc *sc, int flag, int err)
1041 if (flag == UIO_READ) {
1042 if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO,
1046 /* Still more data? */
1047 sc->sc_stat = tpm_status(sc);
1048 if (!err && ((sc->sc_stat & TPM_STS_DATA_AVAIL) == TPM_STS_DATA_AVAIL)) {
1050 printf("tpm_tis12_end: read failed stat=%b\n",
1051 sc->sc_stat, TPM_STS_BITS);
1056 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
1059 /* Release our (0th) locality. */
1060 bus_space_write_1(sc->sc_bt, sc->sc_bh,TPM_ACCESS,
1061 TPM_ACCESS_ACTIVE_LOCALITY);
1063 /* Hungry for more? */
1064 sc->sc_stat = tpm_status(sc);
1065 if (!err && (sc->sc_stat & TPM_STS_DATA_EXPECT)) {
1067 printf("tpm_tis12_end: write failed stat=%b\n",
1068 sc->sc_stat, TPM_STS_BITS);
1073 bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
1074 err ? TPM_STS_CMD_READY : TPM_STS_GO);
1087 struct tpm_softc *sc = v;
1093 r = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_INT_STATUS);
1096 printf("tpm_intr: int=%b (%d)\n", r, TPM_INTERRUPT_ENABLE_BITS,
1101 if (!(r & (TPM_CMD_READY_INT | TPM_LOCALITY_CHANGE_INT |
1102 TPM_STS_VALID_INT | TPM_DATA_AVAIL_INT)))
1108 if (r & TPM_STS_VALID_INT)
1111 if (r & TPM_CMD_READY_INT)
1112 wakeup(sc->sc_write);
1114 if (r & TPM_DATA_AVAIL_INT)
1115 wakeup(sc->sc_read);
1117 if (r & TPM_LOCALITY_CHANGE_INT)
1118 wakeup(sc->sc_init);
1120 bus_space_write_4(sc->sc_bt, sc->sc_bh, TPM_INT_STATUS, r);
1129 /* Read single byte using legacy interface. */
1130 static inline u_int8_t
1131 tpm_legacy_in(bus_space_tag_t iot, bus_space_handle_t ioh, int reg)
1133 bus_space_write_1(iot, ioh, 0, reg);
1134 return bus_space_read_1(iot, ioh, 1);
1138 /* Write single byte using legacy interface. */
1140 tpm_legacy_out(bus_space_tag_t iot, bus_space_handle_t ioh, int reg, u_int8_t v)
1142 bus_space_write_1(iot, ioh, 0, reg);
1143 bus_space_write_1(iot, ioh, 1, v);
1147 /* Probe for TPM using legacy interface. */
1149 tpm_legacy_probe(bus_space_tag_t iot, bus_addr_t iobase)
1151 bus_space_handle_t ioh;
1156 if (!tpm_enabled || iobase == -1)
1159 if (bus_space_map(iot, iobase, 2, 0, &ioh))
1162 v = bus_space_read_1(iot, ioh, 0);
1164 bus_space_unmap(iot, ioh, 2);
1167 r = bus_space_read_1(iot, ioh, 1);
1169 for (i = sizeof(id); i--; )
1170 id[i] = tpm_legacy_in(iot, ioh, TPM_ID + i);
1173 printf("tpm_legacy_probe %.4s %d.%d.%d.%d\n",
1174 &id[4], id[0], id[1], id[2], id[3]);
1177 * The only chips using the legacy interface we are aware of are
1178 * by Atmel. For other chips more signature would have to be added.
1180 if (!bcmp(&id[4], "ATML", 4))
1184 bus_space_write_1(iot, ioh, r, 1);
1185 bus_space_write_1(iot, ioh, v, 0);
1187 bus_space_unmap(iot, ioh, 2);
1192 /* Setup TPM using legacy interface. */
1194 tpm_legacy_init(struct tpm_softc *sc, int irq, const char *name)
1200 if ((i = bus_space_map(sc->sc_batm, tpm_enabled, 2, 0, &sc->sc_bahm))) {
1201 printf(": cannot map tpm registers (%d)\n", i);
1206 for (i = sizeof(id); i--; )
1207 id[i] = tpm_legacy_in(sc->sc_bt, sc->sc_bh, TPM_ID + i);
1209 printf(": %.4s %d.%d @0x%x\n", &id[4], id[0], id[1], tpm_enabled);
1210 iol = tpm_enabled & 0xff;
1211 ioh = tpm_enabled >> 16;
1217 /* Start transaction. */
1219 tpm_legacy_start(struct tpm_softc *sc, int flag)
1225 bits = flag == UIO_READ ? TPM_LEGACY_DA : 0;
1226 tv.tv_sec = TPM_LEGACY_TMO;
1228 to = tvtohz(&tv) / TPM_LEGACY_SLEEP;
1229 while (((r = bus_space_read_1(sc->sc_batm, sc->sc_bahm, 1)) &
1230 (TPM_LEGACY_BUSY|bits)) != bits && to--) {
1231 rv = tsleep(sc, PRIBIO | PCATCH, "legacy_tpm_start",
1233 if (rv && rv != EWOULDBLOCK)
1237 #if defined(TPM_DEBUG) && !defined(__FreeBSD__)
1238 printf("%s: bits %b\n", sc->sc_dev.dv_xname, r, TPM_LEGACY_BITS);
1240 if ((r & (TPM_LEGACY_BUSY|bits)) != bits)
1247 tpm_legacy_read(struct tpm_softc *sc, void *buf, int len, size_t *count,
1255 for (p = buf; !rv && len > 0; len--) {
1257 !(bus_space_read_1(sc->sc_batm, sc->sc_bahm, 1) &
1258 TPM_LEGACY_DA); DELAY(1))
1262 DELAY(TPM_LEGACY_DELAY);
1263 *p++ = bus_space_read_1(sc->sc_batm, sc->sc_bahm, 0);
1272 tpm_legacy_write(struct tpm_softc *sc, void *buf, int len)
1277 for (p = buf, n = len; n--; DELAY(TPM_LEGACY_DELAY)) {
1278 if (!n && len != TPM_BUFSIZ) {
1279 bus_space_write_1(sc->sc_batm, sc->sc_bahm, 1,
1281 DELAY(TPM_LEGACY_DELAY);
1283 bus_space_write_1(sc->sc_batm, sc->sc_bahm, 0, *p++);
1289 /* Finish transaction. */
1291 tpm_legacy_end(struct tpm_softc *sc, int flag, int rv)
1297 if (rv || flag == UIO_READ)
1298 bus_space_write_1(sc->sc_batm, sc->sc_bahm, 1, TPM_LEGACY_ABRT);
1300 tv.tv_sec = TPM_LEGACY_TMO;
1302 to = tvtohz(&tv) / TPM_LEGACY_SLEEP;
1303 while(((r = bus_space_read_1(sc->sc_batm, sc->sc_bahm, 1)) &
1304 TPM_LEGACY_BUSY) && to--) {
1305 rv = tsleep(sc, PRIBIO | PCATCH, "legacy_tpm_end",
1307 if (rv && rv != EWOULDBLOCK)
1311 #if defined(TPM_DEBUG) && !defined(__FreeBSD__)
1312 printf("%s: bits %b\n", sc->sc_dev.dv_xname, r, TPM_LEGACY_BITS);
1314 if (r & TPM_LEGACY_BUSY)
1317 if (r & TPM_LEGACY_RE)
1318 return EIO; /* XXX Retry the loop? */
1326 tpmopen(struct cdev *dev, int flag, int mode, struct thread *td)
1328 tpmopen(dev_t dev, int flag, int mode, struct proc *p)
1331 struct tpm_softc *sc = TPMSOFTC(dev);
1336 if (sc->sc_flags & TPM_OPEN)
1339 sc->sc_flags |= TPM_OPEN;
1346 tpmclose(struct cdev *dev, int flag, int mode, struct thread *td)
1348 tpmclose(dev_t dev, int flag, int mode, struct proc *p)
1351 struct tpm_softc *sc = TPMSOFTC(dev);
1356 if (!(sc->sc_flags & TPM_OPEN))
1359 sc->sc_flags &= ~TPM_OPEN;
1366 tpmread(struct cdev *dev, struct uio *uio, int flags)
1368 tpmread(dev_t dev, struct uio *uio, int flags)
1371 struct tpm_softc *sc = TPMSOFTC(dev);
1372 u_int8_t buf[TPM_BUFSIZ], *p;
1380 if ((rv = (sc->sc_start)(sc, UIO_READ))) {
1386 printf("tpmread: getting header\n");
1388 if ((rv = (sc->sc_read)(sc, buf, TPM_HDRSIZE, &cnt, 0))) {
1389 (sc->sc_end)(sc, UIO_READ, rv);
1394 len = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | buf[5];
1396 printf("tpmread: len %d, io count %d\n", len, uio->uio_resid);
1398 if (len > uio->uio_resid) {
1400 (sc->sc_end)(sc, UIO_READ, rv);
1402 printf("tpmread: bad residual io count 0x%x\n", uio->uio_resid);
1408 /* Copy out header. */
1409 if ((rv = uiomove((caddr_t)buf, cnt, uio))) {
1410 (sc->sc_end)(sc, UIO_READ, rv);
1415 /* Get remaining part of the answer (if anything is left). */
1416 for (len -= cnt, p = buf, n = sizeof(buf); len > 0; p = buf, len -= n,
1420 printf("tpmread: n %d len %d\n", n, len);
1422 if ((rv = (sc->sc_read)(sc, p, n, NULL, TPM_PARAM_SIZE))) {
1423 (sc->sc_end)(sc, UIO_READ, rv);
1428 if ((rv = uiomove((caddr_t)buf, p - buf, uio))) {
1429 (sc->sc_end)(sc, UIO_READ, rv);
1435 rv = (sc->sc_end)(sc, UIO_READ, rv);
1442 tpmwrite(struct cdev *dev, struct uio *uio, int flags)
1444 tpmwrite(dev_t dev, struct uio *uio, int flags)
1447 struct tpm_softc *sc = TPMSOFTC(dev);
1448 u_int8_t buf[TPM_BUFSIZ];
1457 printf("tpmwrite: io count %d\n", uio->uio_resid);
1460 n = MIN(sizeof(buf), uio->uio_resid);
1461 if ((rv = uiomove((caddr_t)buf, n, uio))) {
1466 if ((rv = (sc->sc_start)(sc, UIO_WRITE))) {
1471 if ((rv = (sc->sc_write(sc, buf, n)))) {
1476 rv = (sc->sc_end)(sc, UIO_WRITE, rv);
1483 tpmioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
1486 tpmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)