2018-02-07 02:00:41 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-08-04 06:41:12 +08:00
|
|
|
#define DT_DRV_COMPAT altr_nios2_i2c
|
2020-03-26 00:38:41 +08:00
|
|
|
|
2018-02-07 02:00:41 +08:00
|
|
|
#include <errno.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/i2c.h>
|
2022-10-04 21:33:53 +08:00
|
|
|
#include <zephyr/irq.h>
|
2022-10-04 22:34:24 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2018-02-07 02:00:41 +08:00
|
|
|
#include <soc.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/sys/util.h>
|
2018-02-07 02:00:41 +08:00
|
|
|
#include <altera_common.h>
|
|
|
|
#include "altera_avalon_i2c.h"
|
|
|
|
|
2018-09-18 02:24:08 +08:00
|
|
|
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_MODULE_REGISTER(i2c_nios2);
|
2018-02-09 11:42:28 +08:00
|
|
|
|
2018-02-07 02:00:41 +08:00
|
|
|
#define NIOS2_I2C_TIMEOUT_USEC 1000
|
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
struct i2c_nios2_data {
|
|
|
|
ALT_AVALON_I2C_DEV_t i2c_dev;
|
|
|
|
IRQ_DATA_t irq_data;
|
|
|
|
struct k_sem sem_lock;
|
2018-02-07 02:00:41 +08:00
|
|
|
};
|
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
static int
|
|
|
|
i2c_nios2_configure(const struct device *dev, uint32_t dev_config)
|
2018-02-07 02:00:41 +08:00
|
|
|
{
|
2022-02-23 06:05:14 +08:00
|
|
|
struct i2c_nios2_data *data = (struct i2c_nios2_data *)dev->data;
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
int32_t rc = 0;
|
2018-02-07 02:00:41 +08:00
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
k_sem_take(&data->sem_lock, K_FOREVER);
|
2022-05-24 01:15:02 +08:00
|
|
|
if (!(I2C_MODE_CONTROLLER & dev_config)) {
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_ERR("i2c config mode error\n");
|
2018-02-07 02:00:41 +08:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto i2c_cfg_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I2C_ADDR_10_BITS & dev_config) {
|
2019-06-19 02:45:40 +08:00
|
|
|
LOG_ERR("i2c config addressing error\n");
|
2018-02-07 02:00:41 +08:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto i2c_cfg_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I2C_SPEED_GET(dev_config) != I2C_SPEED_STANDARD) {
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_ERR("i2c config speed error\n");
|
2018-02-07 02:00:41 +08:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto i2c_cfg_err;
|
|
|
|
}
|
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
alt_avalon_i2c_init(&data->i2c_dev);
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
i2c_cfg_err:
|
2022-02-23 06:05:14 +08:00
|
|
|
k_sem_give(&data->sem_lock);
|
2018-02-07 02:00:41 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int i2c_nios2_transfer(const struct device *dev, struct i2c_msg *msgs,
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t num_msgs, uint16_t addr)
|
2018-02-07 02:00:41 +08:00
|
|
|
{
|
2022-02-23 06:05:14 +08:00
|
|
|
struct i2c_nios2_data *data = (struct i2c_nios2_data *)dev->data;
|
2018-02-07 02:00:41 +08:00
|
|
|
ALT_AVALON_I2C_STATUS_CODE status;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t restart, stop;
|
|
|
|
int32_t i, timeout, rc = 0;
|
2018-02-07 02:00:41 +08:00
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
k_sem_take(&data->sem_lock, K_FOREVER);
|
2018-02-07 02:00:41 +08:00
|
|
|
/* register the optional interrupt callback */
|
|
|
|
alt_avalon_i2c_register_optional_irq_handler(
|
2022-02-23 06:05:14 +08:00
|
|
|
&data->i2c_dev, &data->irq_data);
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
/* Iterate over all the messages */
|
|
|
|
for (i = 0; i < num_msgs; i++) {
|
|
|
|
|
|
|
|
/* convert restart flag */
|
|
|
|
if (msgs->flags & I2C_MSG_RESTART) {
|
|
|
|
restart = ALT_AVALON_I2C_RESTART;
|
|
|
|
} else {
|
|
|
|
restart = ALT_AVALON_I2C_NO_RESTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert stop flag */
|
|
|
|
if (msgs->flags & I2C_MSG_STOP) {
|
|
|
|
stop = ALT_AVALON_I2C_STOP;
|
|
|
|
} else {
|
|
|
|
stop = ALT_AVALON_I2C_NO_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the slave device address */
|
2022-02-23 06:05:14 +08:00
|
|
|
alt_avalon_i2c_master_target_set(&data->i2c_dev, addr);
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
/* Start the transfer */
|
|
|
|
if (msgs->flags & I2C_MSG_READ) {
|
|
|
|
status = alt_avalon_i2c_master_receive_using_interrupts(
|
2022-02-23 06:05:14 +08:00
|
|
|
&data->i2c_dev,
|
2018-02-07 02:00:41 +08:00
|
|
|
msgs->buf, msgs->len,
|
|
|
|
restart, stop);
|
|
|
|
} else {
|
|
|
|
status = alt_avalon_i2c_master_transmit_using_interrupts
|
2022-02-23 06:05:14 +08:00
|
|
|
(&data->i2c_dev,
|
2018-02-07 02:00:41 +08:00
|
|
|
msgs->buf, msgs->len,
|
|
|
|
restart, stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return an error if the transfer didn't
|
|
|
|
* start successfully e.g., if the bus was busy
|
|
|
|
*/
|
|
|
|
if (status != ALT_AVALON_I2C_SUCCESS) {
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_ERR("i2c transfer error %lu\n", status);
|
2018-02-07 02:00:41 +08:00
|
|
|
rc = -EIO;
|
|
|
|
goto i2c_transfer_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = NIOS2_I2C_TIMEOUT_USEC;
|
|
|
|
while (timeout) {
|
|
|
|
k_busy_wait(1);
|
|
|
|
status = alt_avalon_i2c_interrupt_transaction_status(
|
2022-02-23 06:05:14 +08:00
|
|
|
&data->i2c_dev);
|
2018-02-07 02:00:41 +08:00
|
|
|
if (status == ALT_AVALON_I2C_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_ERR("i2c busy or timeout error %lu\n", status);
|
2018-02-07 02:00:41 +08:00
|
|
|
rc = -EIO;
|
|
|
|
goto i2c_transfer_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move to the next message */
|
|
|
|
msgs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c_transfer_err:
|
2022-02-23 06:05:14 +08:00
|
|
|
alt_avalon_i2c_disable(&data->i2c_dev);
|
|
|
|
k_sem_give(&data->sem_lock);
|
2018-02-07 02:00:41 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 20:58:56 +08:00
|
|
|
static void i2c_nios2_isr(const struct device *dev)
|
2018-02-07 02:00:41 +08:00
|
|
|
{
|
2022-02-23 06:05:14 +08:00
|
|
|
struct i2c_nios2_data *data = (struct i2c_nios2_data *)dev->data;
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
/* Call Altera HAL driver ISR */
|
2022-02-23 06:05:14 +08:00
|
|
|
alt_handle_irq(&data->i2c_dev, DT_INST_IRQN(0));
|
2018-02-07 02:00:41 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int i2c_nios2_init(const struct device *dev);
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
static struct i2c_driver_api i2c_nios2_driver_api = {
|
|
|
|
.configure = i2c_nios2_configure,
|
|
|
|
.transfer = i2c_nios2_transfer,
|
|
|
|
};
|
|
|
|
|
2022-02-23 06:05:14 +08:00
|
|
|
static struct i2c_nios2_data i2c_nios2_dev_data = {
|
2018-02-07 02:00:41 +08:00
|
|
|
.i2c_dev = {
|
2020-03-26 00:38:41 +08:00
|
|
|
.i2c_base = (alt_u32 *)DT_INST_REG_ADDR(0),
|
2018-02-07 02:00:41 +08:00
|
|
|
.irq_controller_ID = I2C_0_IRQ_INTERRUPT_CONTROLLER_ID,
|
2021-03-03 09:58:46 +08:00
|
|
|
.irq_ID = DT_INST_IRQN(0),
|
2020-03-26 00:38:41 +08:00
|
|
|
.ip_freq_in_hz = DT_INST_PROP(0, clock_frequency),
|
2018-02-07 02:00:41 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int i2c_nios2_init(const struct device *dev)
|
2018-02-07 02:00:41 +08:00
|
|
|
{
|
2022-02-23 06:05:14 +08:00
|
|
|
struct i2c_nios2_data *data = (struct i2c_nios2_data *)dev->data;
|
2018-02-07 02:00:41 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* initialize semaphore */
|
2022-02-23 06:05:14 +08:00
|
|
|
k_sem_init(&data->sem_lock, 1, 1);
|
2018-02-07 02:00:41 +08:00
|
|
|
|
|
|
|
rc = i2c_nios2_configure(dev,
|
2022-05-24 01:15:02 +08:00
|
|
|
I2C_MODE_CONTROLLER |
|
2018-02-07 02:00:41 +08:00
|
|
|
I2C_SPEED_SET(I2C_SPEED_STANDARD));
|
|
|
|
if (rc) {
|
2018-09-18 02:24:08 +08:00
|
|
|
LOG_ERR("i2c configure failed %d\n", rc);
|
2018-02-07 02:00:41 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear ISR register content */
|
2022-02-23 06:05:14 +08:00
|
|
|
alt_avalon_i2c_int_clear(&data->i2c_dev,
|
2018-02-07 02:00:41 +08:00
|
|
|
ALT_AVALON_I2C_ISR_ALL_CLEARABLE_INTS_MSK);
|
2021-03-03 09:58:46 +08:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
|
2020-12-10 01:00:35 +08:00
|
|
|
i2c_nios2_isr, DEVICE_DT_INST_GET(0), 0);
|
2021-03-03 09:58:46 +08:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-02-07 02:00:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-02-23 06:05:14 +08:00
|
|
|
|
|
|
|
I2C_DEVICE_DT_INST_DEFINE(0, i2c_nios2_init, NULL,
|
|
|
|
&i2c_nios2_dev_data, NULL,
|
2022-03-12 05:32:34 +08:00
|
|
|
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY,
|
2022-02-23 06:05:14 +08:00
|
|
|
&i2c_nios2_driver_api);
|