Loading...
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | /*
* Copyright (c) 2018 SiFive Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(i2c_sifive);
#include <device.h>
#include <drivers/i2c.h>
#include <sys/sys_io.h>
#include "i2c-priv.h"
/* Macros */
#define I2C_REG(config, reg) ((mem_addr_t) ((config)->base + reg))
#define IS_SET(config, reg, value) (sys_read8(I2C_REG(config, reg)) & (value))
/* Register Offsets */
#define REG_PRESCALE_LOW 0x00
#define REG_PRESCALE_HIGH 0x04
#define REG_CONTROL 0x08
/* Transmit on write, receive on read */
#define REG_TRANSMIT 0x0c
#define REG_RECEIVE 0x0c
/* Command on write, status on read */
#define REG_COMMAND 0x10
#define REG_STATUS 0x10
/* Values */
#define SF_CONTROL_EN (1 << 7)
#define SF_CONTROL_IE (1 << 6)
#define SF_TX_WRITE (0 << 0)
#define SF_TX_READ (1 << 0)
#define SF_CMD_START (1 << 7)
#define SF_CMD_STOP (1 << 6)
#define SF_CMD_READ (1 << 5)
#define SF_CMD_WRITE (1 << 4)
#define SF_CMD_ACK (1 << 3)
#define SF_CMD_IACK (1 << 0)
#define SF_STATUS_RXACK (1 << 7)
#define SF_STATUS_BUSY (1 << 6)
#define SF_STATUS_AL (1 << 5)
#define SF_STATUS_TIP (1 << 1)
#define SF_STATUS_IP (1 << 0)
/* Structure declarations */
struct i2c_sifive_cfg {
u32_t base;
u32_t f_sys;
u32_t f_bus;
};
/* Helper functions */
static inline bool i2c_sifive_busy(struct device *dev)
{
const struct i2c_sifive_cfg *config = dev->config->config_info;
return IS_SET(config, REG_STATUS, SF_STATUS_TIP);
}
static int i2c_sifive_send_addr(struct device *dev,
u16_t addr,
u16_t rw_flag)
{
const struct i2c_sifive_cfg *config = dev->config->config_info;
u8_t command = 0U;
/* Wait for a previous transfer to complete */
while (i2c_sifive_busy(dev)) {
}
/* Set transmit register to address with read/write flag */
sys_write8((addr | rw_flag), I2C_REG(config, REG_TRANSMIT));
/* Addresses are always written */
command = SF_CMD_WRITE | SF_CMD_START;
/* Write the command register to start the transfer */
sys_write8(command, I2C_REG(config, REG_COMMAND));
while (i2c_sifive_busy(dev)) {
}
if (IS_SET(config, REG_STATUS, SF_STATUS_RXACK)) {
LOG_ERR("I2C Rx failed to acknowledge\n");
return -EIO;
}
return 0;
}
static int i2c_sifive_write_msg(struct device *dev,
struct i2c_msg *msg,
u16_t addr)
{
const struct i2c_sifive_cfg *config = dev->config->config_info;
int rc = 0;
u8_t command = 0U;
rc = i2c_sifive_send_addr(dev, addr, SF_TX_WRITE);
if (rc != 0) {
LOG_ERR("I2C failed to write message\n");
return rc;
}
for (u32_t i = 0; i < msg->len; i++) {
/* Wait for a previous transfer */
while (i2c_sifive_busy(dev)) {
}
/* Put data in transmit reg */
sys_write8((msg->buf)[i], I2C_REG(config, REG_TRANSMIT));
/* Generate command byte */
command = SF_CMD_WRITE;
/* On the last byte of the message */
if (i == (msg->len - 1)) {
/* If the stop bit is requested, set it */
if (msg->flags & I2C_MSG_STOP) {
command |= SF_CMD_STOP;
}
}
/* Write command reg */
sys_write8(command, I2C_REG(config, REG_COMMAND));
/* Wait for a previous transfer */
while (i2c_sifive_busy(dev)) {
}
if (IS_SET(config, REG_STATUS, SF_STATUS_RXACK)) {
LOG_ERR("I2C Rx failed to acknowledge\n");
return -EIO;
}
}
return 0;
}
static int i2c_sifive_read_msg(struct device *dev,
struct i2c_msg *msg,
u16_t addr)
{
const struct i2c_sifive_cfg *config = dev->config->config_info;
u8_t command = 0U;
i2c_sifive_send_addr(dev, addr, SF_TX_READ);
while (i2c_sifive_busy(dev)) {
}
for (int i = 0; i < msg->len; i++) {
/* Generate command byte */
command = SF_CMD_READ;
/* On the last byte of the message */
if (i == (msg->len - 1)) {
/* Set NACK to end read */
command |= SF_CMD_ACK;
/* If the stop bit is requested, set it */
if (msg->flags & I2C_MSG_STOP) {
command |= SF_CMD_STOP;
}
}
/* Write command reg */
sys_write8(command, I2C_REG(config, REG_COMMAND));
/* Wait for the read to complete */
while (i2c_sifive_busy(dev)) {
}
/* Store the received byte */
(msg->buf)[i] = sys_read8(I2C_REG(config, REG_RECEIVE));
}
return 0;
}
/* API Functions */
static int i2c_sifive_configure(struct device *dev, u32_t dev_config)
{
const struct i2c_sifive_cfg *config = NULL;
u32_t i2c_speed = 0U;
u16_t prescale = 0U;
/* Check for NULL pointers */
if (dev == NULL) {
LOG_ERR("Device handle is NULL");
return -EINVAL;
}
if (dev->config == NULL) {
LOG_ERR("Device handle config is NULL");
return -EINVAL;
}
config = dev->config->config_info;
if (config == NULL) {
LOG_ERR("Device config is NULL");
return -EINVAL;
}
/* Disable the I2C peripheral */
sys_write8(0, I2C_REG(config, REG_CONTROL));
/* Configure bus frequency */
switch (I2C_SPEED_GET(dev_config)) {
case I2C_SPEED_STANDARD:
i2c_speed = 100000U; /* 100 KHz */
break;
case I2C_SPEED_FAST:
i2c_speed = 400000U; /* 400 KHz */
break;
case I2C_SPEED_FAST_PLUS:
case I2C_SPEED_HIGH:
case I2C_SPEED_ULTRA:
default:
LOG_ERR("Unsupported I2C speed requested");
return -ENOTSUP;
}
/* Calculate prescale value */
prescale = (config->f_sys / (i2c_speed * 5U)) - 1;
/* Configure peripheral with calculated prescale */
sys_write8((u8_t) (0xFF & prescale), I2C_REG(config, REG_PRESCALE_LOW));
sys_write8((u8_t) (0xFF & (prescale >> 8)),
I2C_REG(config, REG_PRESCALE_HIGH));
/* Support I2C Master mode only */
if (!(dev_config & I2C_MODE_MASTER)) {
LOG_ERR("I2C only supports operation as master");
return -ENOTSUP;
}
/*
* Driver does not support 10-bit addressing. This can be added
* in the future when needed.
*/
if (dev_config & I2C_ADDR_10_BITS) {
LOG_ERR("I2C driver does not support 10-bit addresses");
return -ENOTSUP;
}
/* Enable the I2C peripheral */
sys_write8(SF_CONTROL_EN, I2C_REG(config, REG_CONTROL));
return 0;
}
static int i2c_sifive_transfer(struct device *dev,
struct i2c_msg *msgs,
u8_t num_msgs,
u16_t addr)
{
int rc = 0;
/* Check for NULL pointers */
if (dev == NULL) {
LOG_ERR("Device handle is NULL");
return -EINVAL;
}
if (dev->config == NULL) {
LOG_ERR("Device handle config is NULL");
return -EINVAL;
}
if (dev->config->config_info == NULL) {
LOG_ERR("Device config is NULL");
return -EINVAL;
}
if (msgs == NULL) {
return -EINVAL;
}
for (int i = 0; i < num_msgs; i++) {
if (msgs[i].flags & I2C_MSG_READ) {
rc = i2c_sifive_read_msg(dev, &(msgs[i]), addr);
} else {
rc = i2c_sifive_write_msg(dev, &(msgs[i]), addr);
}
if (rc != 0) {
LOG_ERR("I2C failed to transfer messages\n");
return rc;
}
}
return 0;
};
static int i2c_sifive_init(struct device *dev)
{
const struct i2c_sifive_cfg *config = dev->config->config_info;
u32_t dev_config = 0U;
int rc = 0;
dev_config = (I2C_MODE_MASTER | i2c_map_dt_bitrate(config->f_bus));
rc = i2c_sifive_configure(dev, dev_config);
if (rc != 0) {
LOG_ERR("Failed to configure I2C on init");
return rc;
}
return 0;
}
static struct i2c_driver_api i2c_sifive_api = {
.configure = i2c_sifive_configure,
.transfer = i2c_sifive_transfer,
};
/* Device instantiation */
#define I2C_SIFIVE_INIT(n) \
static struct i2c_sifive_cfg i2c_sifive_cfg_##n = { \
.base = DT_SIFIVE_I2C0_##n##_BASE_ADDRESS, \
.f_sys = DT_SIFIVE_I2C0_##n##_INPUT_FREQUENCY, \
.f_bus = DT_SIFIVE_I2C0_##n##_CLOCK_FREQUENCY, \
}; \
DEVICE_AND_API_INIT(i2c_##n, \
DT_SIFIVE_I2C0_##n##_LABEL, \
i2c_sifive_init, \
NULL, \
&i2c_sifive_cfg_##n, \
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_sifive_api)
#ifdef DT_INST_0_SIFIVE_I2C0_LABEL
I2C_SIFIVE_INIT(0);
#endif
|