1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* Copyright 2012-15 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: AMD
*
*/
#include "dm_services.h"
/*
* Pre-requisites: headers required by header of this unit
*/
#include "include/i2caux_interface.h"
#include "engine.h"
#include "i2c_engine.h"
#include "i2c_hw_engine.h"
/*
* Header of this unit
*/
#include "i2c_generic_hw_engine.h"
/*
* Post-requisites: headers required by this unit
*/
/*
* This unit
*/
/*
* @brief
* Cast 'struct i2c_hw_engine *'
* to 'struct i2c_generic_hw_engine *'
*/
#define FROM_I2C_HW_ENGINE(ptr) \
container_of((ptr), struct i2c_generic_hw_engine, base)
/*
* @brief
* Cast 'struct i2c_engine *'
* to 'struct i2c_generic_hw_engine *'
*/
#define FROM_I2C_ENGINE(ptr) \
FROM_I2C_HW_ENGINE(container_of((ptr), struct i2c_hw_engine, base))
/*
* @brief
* Cast 'struct engine *'
* to 'struct i2c_generic_hw_engine *'
*/
#define FROM_ENGINE(ptr) \
FROM_I2C_ENGINE(container_of((ptr), struct i2c_engine, base))
enum i2caux_engine_type dal_i2c_generic_hw_engine_get_engine_type(
const struct engine *engine)
{
return I2CAUX_ENGINE_TYPE_I2C_GENERIC_HW;
}
/*
* @brief
* Single transaction handling.
* Since transaction may be bigger than HW buffer size,
* it divides transaction to sub-transactions
* and uses batch transaction feature of the engine.
*/
bool dal_i2c_generic_hw_engine_submit_request(
struct engine *engine,
struct i2caux_transaction_request *i2caux_request,
bool middle_of_transaction)
{
struct i2c_generic_hw_engine *hw_engine = FROM_ENGINE(engine);
struct i2c_hw_engine *base = &hw_engine->base;
uint32_t max_payload_size =
base->funcs->get_hw_buffer_available_size(base);
bool initial_stop_bit = !middle_of_transaction;
struct i2c_generic_transaction_attributes attributes;
enum i2c_channel_operation_result operation_result =
I2C_CHANNEL_OPERATION_FAILED;
bool result = false;
/* setup transaction initial properties */
uint8_t address = i2caux_request->payload.address;
uint8_t *current_payload = i2caux_request->payload.data;
uint32_t remaining_payload_size = i2caux_request->payload.length;
bool first_iteration = true;
if (i2caux_request->operation == I2CAUX_TRANSACTION_READ)
attributes.action = I2CAUX_TRANSACTION_ACTION_I2C_READ;
else if (i2caux_request->operation == I2CAUX_TRANSACTION_WRITE)
attributes.action = I2CAUX_TRANSACTION_ACTION_I2C_WRITE;
else {
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_FAILED_INVALID_OPERATION;
return false;
}
/* Do batch transaction.
* Divide read/write data into payloads which fit HW buffer size.
* 1. Single transaction:
* start_bit = 1, stop_bit depends on session state, ack_on_read = 0;
* 2. Start of batch transaction:
* start_bit = 1, stop_bit = 0, ack_on_read = 1;
* 3. Middle of batch transaction:
* start_bit = 0, stop_bit = 0, ack_on_read = 1;
* 4. End of batch transaction:
* start_bit = 0, stop_bit depends on session state, ack_on_read = 0.
* Session stop bit is set if 'middle_of_transaction' = 0. */
while (remaining_payload_size) {
uint32_t current_transaction_size;
uint32_t current_payload_size;
bool last_iteration;
bool stop_bit;
/* Calculate current transaction size and payload size.
* Transaction size = total number of bytes in transaction,
* including slave's address;
* Payload size = number of data bytes in transaction. */
if (first_iteration) {
/* In the first sub-transaction we send slave's address
* thus we need to reserve one byte for it */
current_transaction_size =
(remaining_payload_size > max_payload_size - 1) ?
max_payload_size :
remaining_payload_size + 1;
current_payload_size = current_transaction_size - 1;
} else {
/* Second and further sub-transactions will have
* entire buffer reserved for data */
current_transaction_size =
(remaining_payload_size > max_payload_size) ?
max_payload_size :
remaining_payload_size;
current_payload_size = current_transaction_size;
}
last_iteration =
(remaining_payload_size == current_payload_size);
stop_bit = last_iteration ? initial_stop_bit : false;
/* write slave device address */
if (first_iteration)
hw_engine->funcs->write_address(hw_engine, address);
/* write current portion of data, if requested */
if (i2caux_request->operation == I2CAUX_TRANSACTION_WRITE)
hw_engine->funcs->write_data(
hw_engine,
current_payload,
current_payload_size);
/* execute transaction */
attributes.start_bit = first_iteration;
attributes.stop_bit = stop_bit;
attributes.last_read = last_iteration;
attributes.transaction_size = current_transaction_size;
hw_engine->funcs->execute_transaction(hw_engine, &attributes);
/* wait until transaction is processed; if it fails - quit */
operation_result = base->funcs->wait_on_operation_result(
base,
base->funcs->get_transaction_timeout(
base, current_transaction_size),
I2C_CHANNEL_OPERATION_ENGINE_BUSY);
if (operation_result != I2C_CHANNEL_OPERATION_SUCCEEDED)
break;
/* read current portion of data, if requested */
/* the read offset should be 1 for first sub-transaction,
* and 0 for any next one */
if (i2caux_request->operation == I2CAUX_TRANSACTION_READ)
hw_engine->funcs->read_data(hw_engine, current_payload,
current_payload_size, first_iteration ? 1 : 0);
/* update loop variables */
first_iteration = false;
current_payload += current_payload_size;
remaining_payload_size -= current_payload_size;
}
/* update transaction status */
switch (operation_result) {
case I2C_CHANNEL_OPERATION_SUCCEEDED:
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_SUCCEEDED;
result = true;
break;
case I2C_CHANNEL_OPERATION_NO_RESPONSE:
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_FAILED_NACK;
break;
case I2C_CHANNEL_OPERATION_TIMEOUT:
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
break;
case I2C_CHANNEL_OPERATION_FAILED:
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_FAILED_INCOMPLETE;
break;
default:
i2caux_request->status =
I2CAUX_TRANSACTION_STATUS_FAILED_OPERATION;
}
return result;
}
/*
* @brief
* Returns number of microseconds to wait until timeout to be considered
*/
uint32_t dal_i2c_generic_hw_engine_get_transaction_timeout(
const struct i2c_hw_engine *engine,
uint32_t length)
{
const struct i2c_engine *base = &engine->base;
uint32_t speed = base->funcs->get_speed(base);
if (!speed)
return 0;
/* total timeout = period_timeout * (start + data bits count + stop) */
return ((1000 * TRANSACTION_TIMEOUT_IN_I2C_CLOCKS) / speed) *
(1 + (length << 3) + 1);
}
void dal_i2c_generic_hw_engine_construct(
struct i2c_generic_hw_engine *engine,
struct dc_context *ctx)
{
dal_i2c_hw_engine_construct(&engine->base, ctx);
}
void dal_i2c_generic_hw_engine_destruct(
struct i2c_generic_hw_engine *engine)
{
dal_i2c_hw_engine_destruct(&engine->base);
}
|