blob: b71e9c9b3ea0f8b3d102e814df49d431e025a340 [file] [log] [blame]
Vadim Bendeburyc7a43d62015-06-10 16:11:47 -07001// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef TRUNKS_TRUNKS_FTDI_SPI_H_
6#define TRUNKS_TRUNKS_FTDI_SPI_H_
7
8#include <string>
9
10#include <base/macros.h>
11
12#include "trunks/command_transceiver.h"
13#include "trunks/trunks_export.h"
14
15#if defined SPI_OVER_FTDI
16
17#include "trunks/ftdi/mpsse.h"
18
19namespace trunks {
20
21// TrunksFtdiSpi is a CommandTransceiver implementation that forwards all
22// commands to the SPI over FTDI interface directly to a TPM chip.
23class TRUNKS_EXPORT TrunksFtdiSpi: public CommandTransceiver {
24 public:
Vadim Bendeburyea14a6e2015-06-12 17:03:57 -070025 TrunksFtdiSpi() : mpsse_(NULL), locality_(0) {}
Vadim Bendeburyc7a43d62015-06-10 16:11:47 -070026 ~TrunksFtdiSpi() override;
27
28 // CommandTransceiver methods.
29 bool Init() override;
30 void SendCommand(const std::string& command,
31 const ResponseCallback& callback) override;
32 std::string SendCommandAndWait(const std::string& command) override;
33
34 private:
35 struct mpsse_context* mpsse_;
Vadim Bendeburyea14a6e2015-06-12 17:03:57 -070036 unsigned locality_; // Set at initialization.
Vadim Bendeburyc7a43d62015-06-10 16:11:47 -070037
Vadim Bendebury95146ce2015-06-11 19:15:15 -070038 // Read a TPM register into the passed in buffer, where 'bytes' the width of
39 // the register. Return true on success, false on failure.
40 bool FtdiReadReg(unsigned reg_number, size_t bytes,
Vadim Bendeburyea14a6e2015-06-12 17:03:57 -070041 void *buffer);
Vadim Bendebury95146ce2015-06-11 19:15:15 -070042 // Write a TPM register from the passed in buffer, where 'bytes' the width of
43 // the register. Return true on success, false on failure.
44 bool FtdiWriteReg(unsigned reg_number, size_t bytes,
Vadim Bendeburyea14a6e2015-06-12 17:03:57 -070045 const void *buffer);
Vadim Bendebury95146ce2015-06-11 19:15:15 -070046 // Generate a proper SPI frame for read/write transaction, read_write set to
47 // true for read transactions, the size of the transaction is passed as
48 // 'bytes', addr is the internal TPM address space address (accounting for
49 // locality).
50 //
51 // Note that this function is expected to be called when the SPI bus is idle
52 // (CS deasserted), and will assert the CS before transmitting.
53 void StartTransaction(bool read_write, size_t bytes, unsigned addr);
Vadim Bendebury6b88fbe2015-06-12 17:02:37 -070054 // TPM Status Register is going to be accessed a lot, let's have dedicated
55 // accessors for it,
56 bool ReadTpmSts(uint32_t *status);
57 bool WriteTpmSts(uint32_t status);
Vadim Bendebury32f46a02015-06-12 17:04:55 -070058 // Poll status register until the required value is read or the timeout
59 // expires.
60 bool WaitForStatus(uint32_t statusMask,
Vadim Bendeburyb21ea122015-08-24 18:09:45 -070061 uint32_t statusExpected, int timeout_ms = 10000);
Vadim Bendeburyc60690b2015-08-24 19:17:32 -070062 // Retrieve current value of the burst count field.
63 size_t GetBurstCount(void);
Vadim Bendebury95146ce2015-06-11 19:15:15 -070064
Vadim Bendeburyc7a43d62015-06-10 16:11:47 -070065 DISALLOW_COPY_AND_ASSIGN(TrunksFtdiSpi);
66};
67
68} // namespace trunks
69
70#else // SPI_OVER_FTDI ^^^^ defined vvvvv NOT defined
71
72namespace trunks {
73
74// A plug to support compilations on platforms where FTDI SPI interface is not
75// available.
76class TRUNKS_EXPORT TrunksFtdiSpi: public CommandTransceiver {
77 public:
78 TrunksFtdiSpi() {}
79 ~TrunksFtdiSpi() {}
80
81 bool Init() { return false; }
82 void SendCommand(const std::string& command,
83 const ResponseCallback& callback) {}
84 std::string SendCommandAndWait(const std::string& command) {
85 return std::string(""); }
86};
87
88} // namespace trunks
89
90#endif // SPI_OVER_FTDI ^^^^ NOT defined
91
92#endif // TRUNKS_TRUNKS_FTDI_SPI_H_