Vadim Bendebury | c7a43d6 | 2015-06-10 16:11:47 -0700 | [diff] [blame] | 1 | // 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 | |
| 19 | namespace trunks { |
| 20 | |
| 21 | // TrunksFtdiSpi is a CommandTransceiver implementation that forwards all |
| 22 | // commands to the SPI over FTDI interface directly to a TPM chip. |
| 23 | class TRUNKS_EXPORT TrunksFtdiSpi: public CommandTransceiver { |
| 24 | public: |
Vadim Bendebury | ea14a6e | 2015-06-12 17:03:57 -0700 | [diff] [blame] | 25 | TrunksFtdiSpi() : mpsse_(NULL), locality_(0) {} |
Vadim Bendebury | c7a43d6 | 2015-06-10 16:11:47 -0700 | [diff] [blame] | 26 | ~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 Bendebury | ea14a6e | 2015-06-12 17:03:57 -0700 | [diff] [blame] | 36 | unsigned locality_; // Set at initialization. |
Vadim Bendebury | c7a43d6 | 2015-06-10 16:11:47 -0700 | [diff] [blame] | 37 | |
Vadim Bendebury | 95146ce | 2015-06-11 19:15:15 -0700 | [diff] [blame] | 38 | // 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 Bendebury | ea14a6e | 2015-06-12 17:03:57 -0700 | [diff] [blame] | 41 | void *buffer); |
Vadim Bendebury | 95146ce | 2015-06-11 19:15:15 -0700 | [diff] [blame] | 42 | // 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 Bendebury | ea14a6e | 2015-06-12 17:03:57 -0700 | [diff] [blame] | 45 | const void *buffer); |
Vadim Bendebury | 95146ce | 2015-06-11 19:15:15 -0700 | [diff] [blame] | 46 | // 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 Bendebury | 6b88fbe | 2015-06-12 17:02:37 -0700 | [diff] [blame] | 54 | // 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 Bendebury | 32f46a0 | 2015-06-12 17:04:55 -0700 | [diff] [blame] | 58 | // Poll status register until the required value is read or the timeout |
| 59 | // expires. |
| 60 | bool WaitForStatus(uint32_t statusMask, |
Vadim Bendebury | b21ea12 | 2015-08-24 18:09:45 -0700 | [diff] [blame] | 61 | uint32_t statusExpected, int timeout_ms = 10000); |
Vadim Bendebury | c60690b | 2015-08-24 19:17:32 -0700 | [diff] [blame^] | 62 | // Retrieve current value of the burst count field. |
| 63 | size_t GetBurstCount(void); |
Vadim Bendebury | 95146ce | 2015-06-11 19:15:15 -0700 | [diff] [blame] | 64 | |
Vadim Bendebury | c7a43d6 | 2015-06-10 16:11:47 -0700 | [diff] [blame] | 65 | DISALLOW_COPY_AND_ASSIGN(TrunksFtdiSpi); |
| 66 | }; |
| 67 | |
| 68 | } // namespace trunks |
| 69 | |
| 70 | #else // SPI_OVER_FTDI ^^^^ defined vvvvv NOT defined |
| 71 | |
| 72 | namespace trunks { |
| 73 | |
| 74 | // A plug to support compilations on platforms where FTDI SPI interface is not |
| 75 | // available. |
| 76 | class 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_ |