Software APIs
dif_i2c.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_I2C_H_
6 #define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_I2C_H_
7 
8 /**
9  * @file
10  * @brief <a href="/hw/ip/i2c/doc/">I2C</a> Device Interface Functions
11  */
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 
19 
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif // __cplusplus
25 
26 /**
27  * Represents a speed setting for an I2C component: standard, fast, and
28  * fast plus, corresponding to 100 kbaud, 400 kbaud, and 1 Mbaud,
29  * respectively.
30  */
31 typedef enum dif_i2c_speed {
32  /**
33  * Standard speed, 100 kilobaud.
34  */
36  /**
37  * Fast speed, 400 kilobaud.
38  */
40  /**
41  * Fast plus speed, 1 megabaud.
42  */
45 
46 /**
47  * Timing configuration parameters for I2C.
48  *
49  * While the I2C device requires ten parameters to describe its timing
50  * configuration, the degrees of freedom of those parameters is constrained to
51  * the ones in this struct.
52  *
53  * See `dif_i2c_compute_timing()`
54  */
55 typedef struct dif_i2c_timing_config {
56  /**
57  * The lowest speed at which an I2C target connected to this host will
58  * operate.
59  *
60  * In other words, this is the maximum speed at which the host can operate
61  * without going over what the target devices can handle.
62  */
64  /**
65  * The period of the clock driving this device, in nanoseconds.
66  *
67  * This value should not be zero, since it is used as a divisor for
68  * division.
69  */
71  /**
72  * The expected time it takes for the I2C bus signal to rise, in nanoseconds.
73  *
74  * This value is dependent on properties of the hardware's interconnect, and
75  * not under actual firmware control.
76  */
77  uint32_t sda_rise_nanos;
78  /**
79  * The expected time for the bus signal to fall, similar to `sda_rise_nanos`.
80  */
81  uint32_t sda_fall_nanos;
82  /**
83  * The desired period of the SCL line, in nanoseconds.
84  *
85  * Normally, this should just be `1'000'000 / lowest_target_device_speed`,
86  * but the period may be larger if desired.
87  *
88  * Setting this value to zero will result in the minimum period being used.
89  */
90  uint32_t scl_period_nanos;
92 
93 /**
94  * Configuration for the addressing behavior of the I2C, can be disabled or
95  * configured to look for multiple addresses by masking certain bits. A mask of
96  * 0x7f will match only a single address.
97  */
98 typedef struct dif_i2c_id {
99  /**
100  * Mask the recieved I2C address before checking for a match. Received Address
101  * & mask must equal the programmed address to activate I2C Device. If Address
102  * & ~mask != 0, this will not match any addresses. A mask of 0x7f will cause
103  * device to only respond to an exact match. The mask is 7 bits and LSB
104  * aligned.
105  */
106  uint8_t mask;
107  /**
108  * The 7-bit I2C address that should be matched after masking to cause the
109  * activated I2C Target device to begin to act in a transaction. Address is
110  * LSB aligned.
111  */
112  uint8_t address;
113 } dif_i2c_id_t;
114 
115 /**
116  * Runtime configuration for I2C.
117  *
118  * This struct describes runtime timing parameters. Computing these values is
119  * somewhat complicated, so these fields should be initialized using the
120  * `dif_i2c_compute_timing()` function. A caller is, however, free to compute
121  * these values themselves if they prefer, so long as the I2C spec is
122  * respected.
123  *
124  * These values correspond to those in Table 10 of the I2C spec, and are given
125  * in units of input clock cycles.
126  */
127 typedef struct dif_i2c_config {
128  uint16_t scl_time_high_cycles;
129  uint16_t scl_time_low_cycles;
130  uint16_t rise_cycles;
131  uint16_t fall_cycles;
132  uint16_t start_signal_setup_cycles;
133  uint16_t start_signal_hold_cycles;
134  uint16_t data_signal_setup_cycles;
135  uint16_t data_signal_hold_cycles;
136  uint16_t stop_signal_setup_cycles;
137  /**
138  * This parameter is referred to in the I2C documents as the
139  * "bus free time".
140  */
143 
144 /**
145  * Represents a watermark or data level for one of the I2C FIFOs.
146  */
147 typedef uint16_t dif_i2c_level_t;
148 
149 /**
150  * Flags for a formatted I2C byte, used by the `dif_i2c_write_byte_raw()`
151  * function.
152  */
153 typedef struct dif_i2c_fmt_flags {
154  /**
155  * Causes a start signal to be sent before the byte.
156  *
157  * If a start has been issued during the current transaction, this will issue
158  * a repeated start.
159  */
160  bool start;
161  /**
162  * Causes a stop signal to be sent after the byte.
163  *
164  * This flag cannot be set when both `read` and `read_cont` are set.
165  */
166  bool stop;
167  /**
168  * Causes the byte to be interpreted as an unsigned number of bytes to read
169  * from the target; 0 is interpreted as 256.
170  */
171  bool read;
172  /**
173  * Requires `read` to be set; if so, once the final byte associated with this
174  * read is received, it will be acknowledged, allowing the read operation to
175  * continue.
176  */
177  bool read_cont;
178  /**
179  * By default, the hardware expects an ACK after every byte sent, and raises
180  * an exception (contributing to the `kDifi2cIrqControllerHalt` interrupt).
181  * This flag disables that behavior.
182  *
183  * This flag cannot be set along with `read` or `read_cont`.
184  */
187 
188 /**
189  * The I2C Target device records the following signals with received data
190  */
191 typedef enum dif_i2c_signal {
192  /**
193  * The associated byte was received with a START signal, and should be the
194  * matching address and R/W bit
195  */
197  /**
198  * The associated byte was received after a STOP signal, so the transaction is
199  * over and the byte is junk
200  */
202  /**
203  * The associated byte was received with a repeated START signal and
204  * represents the address for the subsequent transaction.
205  */
207  /**
208  * The associated data byte was NACK'd.
209  */
211  /**
212  * There was a stretch timeout on the associated address byte, leading to
213  * NACKing all subsequent incoming bytes for the rest of the transaction (and
214  * returning 0xFF bytes on any subsequent reads in that transaction).
215  */
217  /**
218  * A STOP signal was received to end a transaction that experienced a stretch
219  * timeout or other I/O error condition.
220  */
222  /**
223  * There's no associated STOP or START signal this is just a byte that's been
224  * written to the I2C target in an ongoing transaction, and it was ACK'd.
225  */
228 
229 /**
230  * Available formatting codes for `dif_i2c_write_byte_raw()`.
231  *
232  * Each code describes how to interpret the `byte` parameter, referred to below
233  * as "the byte".
234  *
235  * It is the caller's responsibility to observe the state transitions in the
236  * comments below.
237  */
238 typedef enum dif_i2c_fmt {
239  /**
240  * Start a transaction. This sends a START signal followed by the byte.
241  * The byte sent will form (potentially part of) the target address for the
242  * transaction.
243  *
244  * May be followed by any format code.
245  */
247  /**
248  * Transmit byte. This simply sends the byte. It may need to be used in
249  * conjunction with `Start` to send a multi-byte target address.
250  *
251  * May be followed by any format code.
252  */
254  /**
255  * Transmit byte and stop. This sends the byte, and then sends a stop
256  * signal, completing a transaction.
257  *
258  * Only `Start` may follow this code.
259  */
261  /**
262  * Request `n` bytes, where `n` is the byte interpreted as an unsigned
263  * integer; a byte value of `0` will be interpreted as requesting `256`
264  * bytes. This will NAK the last byte.
265  *
266  * Only `Start` may follow this code (this code does not stop a transaction;
267  * see `RxStop`).
268  */
270  /**
271  * Request `n` bytes, same as `Rx`, but ACK the last byte so that more data
272  * can be requested.
273  *
274  * May be followed by `RxContinue`, `Rx`, or `RxStop`.
275  */
277  /**
278  * Request `n` bytes, same as `Rx`, but, after NAKing the last byte, send a
279  * stop signal to end the transaction.
280  *
281  * Only `Start` may follow this code.
282  */
284 } dif_i2c_fmt_t;
285 
286 /**
287  * Flags representing the status of an I2C block
288  */
289 typedef struct dif_i2c_status {
290  /**
291  * Enable Host, I2C block has been initialized and enabled to act as a host,
292  * consuming entries from the FMT FIFO to perform I2C transactions, and
293  * writing the results of I2C Reads to the RX FIFO
294  */
296  /**
297  * Enable Target, I2C block has been initialized and enabled to act as a
298  * target device, using the TX FIFO to respond to I2C Reads and the ACQ FIFO
299  * to store data received from I2C Writes
300  */
302  /**
303  * Line Loopback enabled
304  */
306  /**
307  * ACK Control Mode enabled
308  */
310  /**
311  * Format FIFO is full, SW cannot write commands to transact into the FIFO
312  * until I2C host is able to act on the contents.
313  */
315  /**
316  * RX FIFO is full, I2C cannot continue to read data until SW reads from the
317  * FIFO.
318  */
320  /**
321  * Format FIFO is empty, I2C host will stop transacting until the FIFO is
322  * written to.
323  */
325  /**
326  * RX FIFO Empty, Software has handled all bytes read by the I2C Host.
327  */
329  /**
330  * I2C Host is not carrying out a transaction
331  */
332  bool host_idle;
333  /**
334  * I2C Device is not carrying out a transaction
335  */
337  /**
338  * TX FIFO Full, I2C device must respond to I2C reads or have the FIFO cleared
339  * before SW can write to the FIFO.
340  */
342  /**
343  * Acquire FIFO is full of data from I2C writes to the target device. Software
344  * must handle data before I2C device can continue
345  */
347  /**
348  * TX FIFO is empty, device is unprepared to respond to I2C Reads until SW
349  * writes to FIFO
350  */
352  /**
353  * Acquire FIFO is empty and will remain so until I2C Device receives a Write
354  */
356  /**
357  * Target is stretching due to the Auto ACK Counter expiring.
358  */
361 
362 /**
363  * Get I2C status.
364  *
365  * @param i2c An I2C handle.
366  * @param[out] status I2C status as understood by the block.
367  * @return The result of the operation.
368  */
371 
373  /** Received a NACK from the target. */
375  /** Failed to handle a NACK before the handling timeout. */
378 
379 /**
380  * Get the events that contributed to the controller halting, if any.
381  *
382  * @param i2c handle,
383  * @param[out] events The events causing the controller FSM to halt.
384  * @return The result of the operation.
385  */
388  const dif_i2c_t *i2c, dif_i2c_controller_halt_events_t *events);
389 
390 /**
391  * Clear the selected events that contributed to the controller halting, if any.
392  *
393  * @param i2c handle,
394  * @param events The events to clear.
395  * @return The result of the operation.
396  */
399  const dif_i2c_t *i2c, dif_i2c_controller_halt_events_t events);
400 
401 /**
402  * Computes timing parameters for an I2C host and stores them in `config`.
403  *
404  * Timing is based on requirements for devices attached to OpenTitan
405  *
406  * The values returned may be tweaked by callers that require finer control over
407  * some of the calculations, such as how the allocation of a lengthened SCL
408  * period.
409  *
410  * @param timing_config Configuration values for producing timing parameters.
411  * @param[out] config I2C configuration to which to apply the computed
412  * parameters.
413  * @return The result of the operation.
414  */
417  dif_i2c_config_t *config);
418 
419 /**
420  * Configures I2C with runtime information.
421  *
422  * This function should need to be called once for the lifetime of `handle`.
423  *
424  * @param i2c An I2C handle.
425  * @param config Runtime configuration parameters.
426  * @return The result of the operation.
427  */
430 
431 /**
432  * Resets the state of the RX FIFO, essentially dropping all received bytes for
433  * the host.
434  *
435  * @param i2c An I2c handle.
436  * @return The result of the operation.
437  */
440 
441 /**
442  * Resets the state of the FMT FIFO, essentially dropping all scheduled
443  * operations.
444  *
445  * @param i2c An I2c handle.
446  * @return The result of the operation.
447  */
450 
451 /**
452  * Resets the state of the TX FIFO, essentially dropping all scheduled
453  * responses.
454  *
455  * @param i2c An I2c handle.
456  * @return The result of the operation.
457  */
460 
461 /**
462  * Resets the state of the ACQ FIFO, essentially dropping all received bytes for
463  * the target device.
464  *
465  * @param i2c An I2c handle.
466  * @return The result of the operation.
467  */
470 
471 /**
472  * Sets watermarks for the RX and FMT FIFOs, which will assert the
473  * corresponding interrupts whenever the levels in the FIFOs are above (RX)
474  * and below (FMT) the set levels.
475  *
476  * @param i2c An I2C handle.
477  * @param rx_level The desired watermark level for the RX FIFO.
478  * @param fmt_level The desired watermark level for the FMT FIFO.
479  * @return The result of the operation.
480  */
483  dif_i2c_level_t rx_level,
484  dif_i2c_level_t fmt_level);
485 
486 /**
487  * Sets watermarks for the TX and ACQ FIFOs, which will assert the
488  * corresponding interrupts whenever the levels in the FIFOs are below (TX)
489  * and above (ACQ) the set levels.
490  *
491  * @param i2c An I2C handle.
492  * @param tx_level The desired watermark level for the TX FIFO.
493  * @param acq_level The desired watermark level for the ACQ FIFO.
494  * @return The result of the operation.
495  */
498  dif_i2c_level_t tx_level,
499  dif_i2c_level_t acq_level);
500 
501 /**
502  * Enables or disables the "Host I2C" functionality,
503  * This function should be called to enable the device
504  * once timings, interrupts, and watermarks are all configured.
505  *
506  * @param i2c An I2C handle.
507  * @param state The new toggle state for the host functionality.
508  * @return The result of the operation.
509  */
512 
513 /**
514  * Enables or disables the "Device I2C" functionality,
515  * This function should be called to enable the device
516  * once address, and interrupts are all configured.
517  *
518  * @param i2c An I2C handle.
519  * @param state The new toggle state for the device functionality.
520  * @return The result of the operation.
521  */
524  dif_toggle_t state);
525 
526 /**
527  * Enables or disables the Line Loopback functionality,
528  * This function should be called to assist debugging by setting the i2c block
529  * or host to use received transactions to populate outgoing transactions.
530  *
531  * @param i2c An I2C handle.
532  * @param state The new toggle state for the host functionality.
533  * @return The result of the operation.
534  */
537  dif_toggle_t state);
538 
539 /**
540  * Enables or disables the functionality to NACK when timing out on an address
541  * (N)ACK phase stretch.
542  * This function should be called prior to enabling the i2c target module.
543  *
544  * @param i2c An I2C handle.
545  * @param state The new toggle state for the device functionality.
546  * @return The result of the operation.
547  */
550  dif_toggle_t state);
551 
552 /**
553  * Enables or disables the ACK Control Mode functionality.
554  *
555  * This function should be called prior to enabling the i2c target module.
556  *
557  * @param i2c An I2C handle.
558  * @param state The new toggle state for the device functionality.
559  * @return The result of the operation.
560  */
563  dif_toggle_t state);
564 
565 /**
566  * Enables or disables the "override mode". In override mode, software is able
567  * to directly control the driven values of the SCL and SDA lines using
568  * `dif_i2c_override_drive_pins()`.
569  *
570  * @param i2c An I2C handle.
571  * @param state The new toggle state for override mode.'
572  * @return The result of the operation.
573  */
576  dif_toggle_t state);
577 
578 /**
579  * Drives the SCL and SDA pins to the given values when "override mode" is
580  * enabled.
581  *
582  * @param i2c An I2C handle.
583  * @param scl The value to drive SCL to.
584  * @param sda The value to drive SDA to.
585  * @return The result of the operation.
586  */
589  bool sda);
590 
591 /**
592  * Returns oversampling of the last 16 values of the SCL and SDA pins, with the
593  * zeroth bit being the most recent.
594  *
595  * @param i2c An I2C handle.
596  * @param[out] scl_samples SCL sample bits; may be `NULL`.
597  * @param[out] sda_samples SDA sample bits; may be `NULL`.
598  * @return The result of the operation.
599  */
602  uint16_t *scl_samples,
603  uint16_t *sda_samples);
604 
605 /**
606  * Returns the current levels, i.e., number of entries, in the FMT, RX, TX and
607  * ACQ FIFOs.
608  * These values represent the number of entries pending for send by host
609  * hardware, entries pending for read by host software, entries pending for send
610  * by device hardware, and entries pending for read by device software
611  * respectively.
612  *
613  * @param i2c An I2C handle.
614  * @param[out] fmt_fifo_level The number of unsent FMT bytes; may be `NULL`.
615  * @param[out] rx_fifo_level The number of unread RX bytes; may be `NULL`.
616  * @param[out] tx_fifo_level The number of unread TX bytes; may be `NULL`.
617  * @param[out] acq_fifo_level The number of unread ACQ bytes; may be `NULL`.
618  * @return The result of the operation.
619  */
622  dif_i2c_level_t *fmt_fifo_level,
623  dif_i2c_level_t *rx_fifo_level,
624  dif_i2c_level_t *tx_fifo_level,
625  dif_i2c_level_t *acq_fifo_level);
626 
627 /**
628  * Read the current value of the Auto ACK Counter.
629  *
630  * The counter is only active if ACK Control Mode is enabled.
631  *
632  * The Auto ACK Counter represents the remaining number of bytes the Target
633  * module will ACK automatically, so long as the ACQ FIFO has capacity.
634  *
635  * @param i2c An I2C handle.
636  * @param count[out] The number of additional bytes to ACK in the current
637  * transfer.
638  * @return The result of the operation.
639  */
641 dif_result_t dif_i2c_get_auto_ack_count(const dif_i2c_t *i2c, uint16_t *count);
642 
643 /**
644  * Reloads the Auto ACK Counter with the provided value.
645  *
646  * The count will only be accepted if the I2C target module is currently
647  * stretching the clock, and the count is currently 0. In other words, the
648  * target module is stretching because the Auto ACK Count was exhausted.
649  *
650  * In addition, the counter is only active if ACK Control Mode is enabled.
651  *
652  * Set the value to 1 to ACK only the current pending data byte. Increase the
653  * `count` argument for each additional byte desired to be automatically ACK'd,
654  * assuming the ACQ FIFO has capacity.
655  *
656  * @param i2c An I2C handle.
657  * @param count The number of additional bytes to ACK in the current transfer.
658  * @return The result of the operation.
659  */
661 dif_result_t dif_i2c_set_auto_ack_count(const dif_i2c_t *i2c, uint16_t count);
662 
663 /**
664  * Instruct the I2C Target module to issue a NACK for the current transaction.
665  *
666  * Only takes effect if the Target module is stretching the clock because the
667  * Auto ACK Count has expired. ACK Control Mode must be enabled.
668  *
669  * @param i2c An I2C handle.
670  * @return The result of the operation.
671  */
674 
675 /**
676  * Get the pending data byte when stretching due to Auto Ack Count exhaustion.
677  *
678  * This value is only valid if ACK Control Mode is enabled.
679  *
680  * @param i2c An I2C handle.
681  * @param[out] data The data pending for (N)ACK.
682  * @return The result of the operation.
683  */
685 dif_result_t dif_i2c_get_pending_acq_byte(const dif_i2c_t *i2c, uint8_t *data);
686 
687 /**
688  * Pops an entry (a byte) off of the RX FIFO. Passing in `NULL` to the out-param
689  * will still trigger a byte pop.
690  *
691  * @param i2c An I2C handle.
692  * @param[out] byte The popped byte; may be `NULL`.
693  * @return The result of the operation.
694  */
696 dif_result_t dif_i2c_read_byte(const dif_i2c_t *i2c, uint8_t *byte);
697 
698 /**
699  * Reads off a chunk of bytes from the RX FIFO.
700  *
701  * @param i2c An I2C handle.
702  * @param[out] size The size of the buffer.
703  * @param[out] buffer A buffer to receive the bytes read.
704  * @return The result of the operation.
705  */
707 dif_result_t dif_i2c_read_bytes(const dif_i2c_t *i2c, size_t size,
708  uint8_t *buffer);
709 
710 /**
711  * Pushes a raw write entry onto the FMT FIFO, consisting of a byte and format
712  * flags. This function can be called in sequence to enqueue an I2C
713  * transmission.
714  *
715  * Callers should prefer `dif_i2c_write_byte()` instead, since that function
716  * provides clearer semantics. This function should only really be used for
717  * testing or troubleshooting a device.
718  *
719  * @param i2c An I2C handle.
720  * @param byte The value to push onto the FIFO.
721  * @param flags The flags to use for this write.
722  * @return The result of the operation.
723  */
725 dif_result_t dif_i2c_write_byte_raw(const dif_i2c_t *i2c, uint8_t byte,
726  dif_i2c_fmt_flags_t flags);
727 
728 /**
729  * Writes a chunk of raw bytes and format flags onto the FMT FIFO.
730  *
731  * @param i2c An I2C handle.
732  * @param size The number of bytes to push onto the FIFO.
733  * @param bytes Buffer with the values to push onto the FIFO.
734  * @param flags The format flags to use for this write.
735  * @return The result of the operation.
736  */
738 dif_result_t dif_i2c_write_bytes_raw(const dif_i2c_t *i2c, size_t size,
739  const uint8_t *bytes,
740  dif_i2c_fmt_flags_t flags);
741 
742 /**
743  * Pushes a write entry onto the FMT FIFO, consisting of a byte and a format
744  * code. This function can be called in sequence to enqueue an I2C
745  * transmission.
746  *
747  * @param i2c An I2C handle.
748  * @param byte The value to push onto the FIFO.
749  * @param code The code to use for this write.
750  * @param suppress_nak_irq Whether to supress the NAK IRQ for this one byte.
751  * May not be used in combination with `Rx` codes.
752  * @return The result of the operation.
753  */
755 dif_result_t dif_i2c_write_byte(const dif_i2c_t *i2c, uint8_t byte,
756  dif_i2c_fmt_t code, bool suppress_nak_irq);
757 
758 /**
759  * Pushes a byte into the TX FIFO to make it available when this I2C block
760  * responds to an I2C Read as a target device.
761  *
762  * @param i2c handle.
763  * @param byte to write to FIFO
764  * @return The result of the operation.
765  */
767 dif_result_t dif_i2c_transmit_byte(const dif_i2c_t *i2c, uint8_t byte);
768 
769 /**
770  * Read acquired data from the ACQ FIFO, including record of starts, stops,
771  * address and written data
772  *
773  * @param i2c handle.
774  * @param[out] byte, Data received in the transaction, Could be the address or
775  * junk
776  * @param[out] signal, Signal received in the transaction
777  * @return The result of the operation.
778  */
780 dif_result_t dif_i2c_acquire_byte(const dif_i2c_t *i2c, uint8_t *byte,
781  dif_i2c_signal_t *signal);
782 
783 /**
784  * Enables clock stretching timeout after a number of I2C block clock cycles
785  * when I2C block is configured as host.
786  *
787  * @param i2c An I2C handle,
788  * @param enable the timeout
789  * @param cycles How many cycles to wait before timing out
790  * @return The result of the operation.
791  */
794  dif_toggle_t enable,
795  uint32_t cycles);
796 
797 /**
798  * Sets the I2C device to listen for a pair of masked addresses
799  *
800  * @param i2c handle,
801  * @param id0 address and mask pair to listen for can be null
802  * @param id1 address and mask pair to listen for can be null
803  * @return The result of the operation.
804  */
807  const dif_i2c_id_t *id0,
808  const dif_i2c_id_t *id1);
809 
810 /**
811  * Set host timeout. When OT is acting as target device, set the number of
812  * counts after which to trigger a host_timeout interrupt
813  *
814  * @param i2c handle,
815  * @param duration in clock counts
816  * @return The result of the operation.
817  */
819 dif_result_t dif_i2c_set_host_timeout(const dif_i2c_t *i2c, uint32_t duration);
820 
821 #ifdef __cplusplus
822 } // extern "C"
823 #endif // __cplusplus
824 
825 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_I2C_H_