|
| 1 | +/* |
| 2 | + * Copyright 2025, UNSW |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <sddf/i2c/libi2c_raw.h> |
| 8 | + |
| 9 | +#ifdef DEBUG_LIBI2C |
| 10 | +#define LOG_LIBI2C(...) do{ sddf_dprintf("CLIENT|INFO: "); sddf_printf(__VA_ARGS__); }while(0) |
| 11 | +#else |
| 12 | +#define LOG_LIBI2C(...) do{}while(0) |
| 13 | +#endif |
| 14 | + |
| 15 | +/** |
| 16 | + * Initialise libI2C and return the conf struct needed for all operations. |
| 17 | + */ |
| 18 | +int libi2c_init(libi2c_conf_t *conf_struct, i2c_queue_handle_t *queue_handle) |
| 19 | +{ |
| 20 | + conf_struct->handle = queue_handle; |
| 21 | + |
| 22 | + // Allocate bitmask (i.e. zero portion of data region) |
| 23 | + for (int i = 0; i < LIBI2C_BITMASK_SZ; i++) { |
| 24 | + I2C_DATA_REGION[i] = 0; |
| 25 | + } |
| 26 | + |
| 27 | + // Index commands after bytes used for bitmask |
| 28 | + conf_struct->data_start = (void *)(I2C_DATA_REGION + LIBI2C_BITMASK_SZ); |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +// ########### Command allocator functions ############ |
| 33 | +// SDFgen will do a lot of the work for us, but unfortunately all of the variables it generates |
| 34 | +// are considered runtime-context by the C compiler. As a result, we need to do some magic to |
| 35 | +// have a sane allocator which doesn't need a bunch of #defines set based on region sizes. |
| 36 | +// |
| 37 | +// This allocator sets aside a tracking bitmask from the available room in the data region. |
| 38 | +// Commands are 2 bytes, hence a region of size S can store a max of S/2 commands. |
| 39 | +// S/2 commands can be indexed using (S/2)/64 = S/128. We set aside this many bitmask words at |
| 40 | +// the base of the region. The remaining C=S - (S/128)=127/128 * S bytes are used to store |
| 41 | +// ((127/128)S) / 2 commands. |
| 42 | + |
| 43 | +/** |
| 44 | + * Given configuration struct, return first available command from allocation bitmask. |
| 45 | + * Returns NULL if allocator is exhausted. |
| 46 | + */ |
| 47 | +static i2c_cmd_t *__libi2c_alloc_cmd(libi2c_conf_t *conf) |
| 48 | +{ |
| 49 | + // Find first non-zero byte in bitmask range |
| 50 | + int victim_idx = -1; |
| 51 | + for (int i = 0; i < LIBI2C_BITMASK_SZ; i++) { |
| 52 | + uint8_t mask = I2C_DATA_REGION[i]; |
| 53 | + if (I2C_DATA_REGION[i] != 0xFF) { |
| 54 | + victim_idx = i; |
| 55 | + for (int bit = 0; bit < 8; bit++) { |
| 56 | + if (!(mask & (1 << bit))) { |
| 57 | + // Mark this bit as allocated |
| 58 | + I2C_DATA_REGION[i] |= (1 << bit); |
| 59 | + // Calculate command index |
| 60 | + int cmd_idx = (i * 8) + bit; |
| 61 | + return &conf->data_start[cmd_idx]; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return NULL; // No space. |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Given a pointer to a command in the data region, release this command to the allocator. |
| 71 | + */ |
| 72 | +static void __libi2c_free_cmd(libi2c_conf_t *conf, i2c_cmd_t *cmd) |
| 73 | +{ |
| 74 | + // Make sure command is actually in the data region. |
| 75 | + assert((uintptr_t)cmd > (uintptr_t)I2C_DATA_REGION |
| 76 | + && ((uintptr_t)cmd - (uintptr_t)I2C_DATA_REGION) <= i2c_config.data.size); |
| 77 | + |
| 78 | + size_t cmd_idx = cmd - conf->data_start; |
| 79 | + size_t bitmask_byte = cmd_idx / 8; |
| 80 | + uint8_t bitmask_bit = cmd_idx % 8; |
| 81 | + I2C_DATA_REGION[bitmask_byte] &= ~(1 << bitmask_bit); |
| 82 | +} |
| 83 | + |
| 84 | +// ########### I2C interface ########### |
| 85 | + |
| 86 | +static inline int check_meta_buf(void *meta_buf) |
| 87 | +{ |
| 88 | + if ((uintptr_t)meta_buf < (uintptr_t)I2C_META_REGION |
| 89 | + || (uintptr_t)meta_buf > ((uintptr_t)I2C_META_REGION + i2c_config.meta.size)) { |
| 90 | + LOG_LIBI2C_ERR("i2c_write called with meta_buf not in meta region!"); |
| 91 | + return -1; |
| 92 | + } |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Given a buffer pointer from the META region, create an I2C op, dispatch and return when |
| 98 | + * complete. This is a blocking function call. Implements all single-cmd ops. |
| 99 | + * @return -1 if queue ops fail, positive value corresponding to i2c_err_t, or 0 if successful. |
| 100 | + */ |
| 101 | +static int __i2c_dispatch(libi2c_conf_t *conf, i2c_addr_t address, void *buf, uint16_t len, uint8_t flag_mask) |
| 102 | +{ |
| 103 | + // Check that supplied buffer is within bounds of meta region |
| 104 | + if (check_meta_buf(buf)) { |
| 105 | + return -1; |
| 106 | + } |
| 107 | + // Create a write command |
| 108 | + i2c_cmd_t *cmd = __libi2c_alloc_cmd(conf); |
| 109 | + if (cmd == NULL) { |
| 110 | + LOG_LIBI2C_ERR("__i2c_dispatch failed to allocate command!\n"); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + size_t meta_offset = (uint8_t *)buf - I2C_META_REGION; |
| 114 | + i2c_err_t error = 0; |
| 115 | + cmd->offset = meta_offset; |
| 116 | + cmd->len = len; |
| 117 | + cmd->flag_mask = flag_mask; |
| 118 | + |
| 119 | + if (i2c_enqueue_request(*conf->handle, address, (uintptr_t)cmd, (uintptr_t)I2C_META_REGION, 1)) { |
| 120 | + LOG_LIBI2C_ERR("__i2c_dispatch failed to enqueue request!\n"); |
| 121 | + error = -1; |
| 122 | + goto i2c_dispatch_fail; |
| 123 | + } |
| 124 | + microkit_notify(i2c_config.virt.id); |
| 125 | + |
| 126 | + // Await response. |
| 127 | + co_switch(t_event); |
| 128 | + |
| 129 | + i2c_addr_t returned_addr = 0; |
| 130 | + size_t err_cmd = 0; // Irrelevant for single-command runs. |
| 131 | + if (i2c_dequeue_response(*conf->handle, &returned_addr, &error, &err_cmd)) { |
| 132 | + LOG_LIBI2C_ERR("__i2c_dispatch failed to dequeue response!\n"); |
| 133 | + error = -1; |
| 134 | + goto i2c_dispatch_fail; |
| 135 | + } |
| 136 | + assert(returned_addr == address); // If this ever fails, the protocol is broken or misused! |
| 137 | +i2c_dispatch_fail: |
| 138 | + __libi2c_free_cmd(conf, cmd); |
| 139 | + return error; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Perform a simple I2C write given a META region buffer containing data. |
| 144 | + * To perform a write to a device register, ensure the FIRST byte of write_buf contains |
| 145 | + * the register address. |
| 146 | + * This is a blocking function call. |
| 147 | + * @return -1 if queue ops fail, positive value corresponding to i2c_err_t, or 0 if successful. |
| 148 | + */ |
| 149 | +int i2c_write(libi2c_conf_t *conf, i2c_addr_t address, void *write_buf, uint16_t len) |
| 150 | +{ |
| 151 | + return __i2c_dispatch(conf, address, write_buf, len, I2C_FLAG_STOP); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Perform a simple I2C read given a META region buffer to store result. |
| 156 | + * To perform a read to a device register, use i2c_writeread! |
| 157 | + * This is a blocking function call. |
| 158 | + * @return -1 if queue ops fail, positive value corresponding to i2c_err_t, or 0 if successful. |
| 159 | + */ |
| 160 | +int i2c_read(libi2c_conf_t *conf, i2c_addr_t address, void *read_buf, uint16_t len) |
| 161 | +{ |
| 162 | + return __i2c_dispatch(conf, address, read_buf, len, I2C_FLAG_STOP | I2C_FLAG_READ); |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Perform an I2C read given a META region buffer to store result, writing the address of a |
| 167 | + * peripheral register first. |
| 168 | + * This is a blocking function call. |
| 169 | + * @return -1 if queue ops fail, positive value corresponding to i2c_err_t, or 0 if successful. |
| 170 | + */ |
| 171 | +int i2c_writeread(libi2c_conf_t *conf, i2c_addr_t address, i2c_addr_t reg_address, void *read_buf, uint16_t len) |
| 172 | +{ |
| 173 | + // Check that supplied buffer is within bounds of meta region |
| 174 | + if (check_meta_buf(read_buf)) { |
| 175 | + return -1; |
| 176 | + } |
| 177 | + // Inject register address to read buf |
| 178 | + ((i2c_addr_t *)read_buf)[0] = reg_address; |
| 179 | + |
| 180 | + return __i2c_dispatch(conf, address, read_buf, len, I2C_FLAG_STOP | I2C_FLAG_READ | I2C_FLAG_WRRD); |
| 181 | +} |
0 commit comments