18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Aspeed 24XX/25XX I2C Controller. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012-2017 ASPEED Technology Inc. 68c2ecf20Sopenharmony_ci * Copyright 2017 IBM Corporation 78c2ecf20Sopenharmony_ci * Copyright 2017 Google, Inc. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/clk.h> 118c2ecf20Sopenharmony_ci#include <linux/completion.h> 128c2ecf20Sopenharmony_ci#include <linux/err.h> 138c2ecf20Sopenharmony_ci#include <linux/errno.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 178c2ecf20Sopenharmony_ci#include <linux/io.h> 188c2ecf20Sopenharmony_ci#include <linux/irq.h> 198c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h> 208c2ecf20Sopenharmony_ci#include <linux/irqdomain.h> 218c2ecf20Sopenharmony_ci#include <linux/kernel.h> 228c2ecf20Sopenharmony_ci#include <linux/module.h> 238c2ecf20Sopenharmony_ci#include <linux/of_address.h> 248c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 258c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 268c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 278c2ecf20Sopenharmony_ci#include <linux/reset.h> 288c2ecf20Sopenharmony_ci#include <linux/slab.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* I2C Register */ 318c2ecf20Sopenharmony_ci#define ASPEED_I2C_FUN_CTRL_REG 0x00 328c2ecf20Sopenharmony_ci#define ASPEED_I2C_AC_TIMING_REG1 0x04 338c2ecf20Sopenharmony_ci#define ASPEED_I2C_AC_TIMING_REG2 0x08 348c2ecf20Sopenharmony_ci#define ASPEED_I2C_INTR_CTRL_REG 0x0c 358c2ecf20Sopenharmony_ci#define ASPEED_I2C_INTR_STS_REG 0x10 368c2ecf20Sopenharmony_ci#define ASPEED_I2C_CMD_REG 0x14 378c2ecf20Sopenharmony_ci#define ASPEED_I2C_DEV_ADDR_REG 0x18 388c2ecf20Sopenharmony_ci#define ASPEED_I2C_BYTE_BUF_REG 0x20 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* Global Register Definition */ 418c2ecf20Sopenharmony_ci/* 0x00 : I2C Interrupt Status Register */ 428c2ecf20Sopenharmony_ci/* 0x08 : I2C Interrupt Target Assignment */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* Device Register Definition */ 458c2ecf20Sopenharmony_ci/* 0x00 : I2CD Function Control Register */ 468c2ecf20Sopenharmony_ci#define ASPEED_I2CD_MULTI_MASTER_DIS BIT(15) 478c2ecf20Sopenharmony_ci#define ASPEED_I2CD_SDA_DRIVE_1T_EN BIT(8) 488c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_SDA_DRIVE_1T_EN BIT(7) 498c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_HIGH_SPEED_EN BIT(6) 508c2ecf20Sopenharmony_ci#define ASPEED_I2CD_SLAVE_EN BIT(1) 518c2ecf20Sopenharmony_ci#define ASPEED_I2CD_MASTER_EN BIT(0) 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* 0x04 : I2CD Clock and AC Timing Control Register #1 */ 548c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_TBUF_MASK GENMASK(31, 28) 558c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_THDSTA_MASK GENMASK(27, 24) 568c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_TACST_MASK GENMASK(23, 20) 578c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_SCL_HIGH_SHIFT 16 588c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_SCL_HIGH_MASK GENMASK(19, 16) 598c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_SCL_LOW_SHIFT 12 608c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_SCL_LOW_MASK GENMASK(15, 12) 618c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_BASE_DIVISOR_MASK GENMASK(3, 0) 628c2ecf20Sopenharmony_ci#define ASPEED_I2CD_TIME_SCL_REG_MAX GENMASK(3, 0) 638c2ecf20Sopenharmony_ci/* 0x08 : I2CD Clock and AC Timing Control Register #2 */ 648c2ecf20Sopenharmony_ci#define ASPEED_NO_TIMEOUT_CTRL 0 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* 0x0c : I2CD Interrupt Control Register & 678c2ecf20Sopenharmony_ci * 0x10 : I2CD Interrupt Status Register 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * These share bit definitions, so use the same values for the enable & 708c2ecf20Sopenharmony_ci * status bits. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_RECV_MASK 0xf000ffff 738c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_SDA_DL_TIMEOUT BIT(14) 748c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_BUS_RECOVER_DONE BIT(13) 758c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_SLAVE_MATCH BIT(7) 768c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_SCL_TIMEOUT BIT(6) 778c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_ABNORMAL BIT(5) 788c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_NORMAL_STOP BIT(4) 798c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_ARBIT_LOSS BIT(3) 808c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_RX_DONE BIT(2) 818c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_TX_NAK BIT(1) 828c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_TX_ACK BIT(0) 838c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_MASTER_ERRORS \ 848c2ecf20Sopenharmony_ci (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ 858c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_SCL_TIMEOUT | \ 868c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_ABNORMAL | \ 878c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_ARBIT_LOSS) 888c2ecf20Sopenharmony_ci#define ASPEED_I2CD_INTR_ALL \ 898c2ecf20Sopenharmony_ci (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ 908c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \ 918c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_SCL_TIMEOUT | \ 928c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_ABNORMAL | \ 938c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_NORMAL_STOP | \ 948c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_ARBIT_LOSS | \ 958c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_RX_DONE | \ 968c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_TX_NAK | \ 978c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_TX_ACK) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* 0x14 : I2CD Command/Status Register */ 1008c2ecf20Sopenharmony_ci#define ASPEED_I2CD_SCL_LINE_STS BIT(18) 1018c2ecf20Sopenharmony_ci#define ASPEED_I2CD_SDA_LINE_STS BIT(17) 1028c2ecf20Sopenharmony_ci#define ASPEED_I2CD_BUS_BUSY_STS BIT(16) 1038c2ecf20Sopenharmony_ci#define ASPEED_I2CD_BUS_RECOVER_CMD BIT(11) 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* Command Bit */ 1068c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_STOP_CMD BIT(5) 1078c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_S_RX_CMD_LAST BIT(4) 1088c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_RX_CMD BIT(3) 1098c2ecf20Sopenharmony_ci#define ASPEED_I2CD_S_TX_CMD BIT(2) 1108c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_TX_CMD BIT(1) 1118c2ecf20Sopenharmony_ci#define ASPEED_I2CD_M_START_CMD BIT(0) 1128c2ecf20Sopenharmony_ci#define ASPEED_I2CD_MASTER_CMDS_MASK \ 1138c2ecf20Sopenharmony_ci (ASPEED_I2CD_M_STOP_CMD | \ 1148c2ecf20Sopenharmony_ci ASPEED_I2CD_M_S_RX_CMD_LAST | \ 1158c2ecf20Sopenharmony_ci ASPEED_I2CD_M_RX_CMD | \ 1168c2ecf20Sopenharmony_ci ASPEED_I2CD_M_TX_CMD | \ 1178c2ecf20Sopenharmony_ci ASPEED_I2CD_M_START_CMD) 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci/* 0x18 : I2CD Slave Device Address Register */ 1208c2ecf20Sopenharmony_ci#define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0) 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cienum aspeed_i2c_master_state { 1238c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_INACTIVE, 1248c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_PENDING, 1258c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_START, 1268c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_TX_FIRST, 1278c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_TX, 1288c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_RX_FIRST, 1298c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_RX, 1308c2ecf20Sopenharmony_ci ASPEED_I2C_MASTER_STOP, 1318c2ecf20Sopenharmony_ci}; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cienum aspeed_i2c_slave_state { 1348c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_INACTIVE, 1358c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_START, 1368c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_READ_REQUESTED, 1378c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_READ_PROCESSED, 1388c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_WRITE_REQUESTED, 1398c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_WRITE_RECEIVED, 1408c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_STOP, 1418c2ecf20Sopenharmony_ci}; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistruct aspeed_i2c_bus { 1448c2ecf20Sopenharmony_ci struct i2c_adapter adap; 1458c2ecf20Sopenharmony_ci struct device *dev; 1468c2ecf20Sopenharmony_ci void __iomem *base; 1478c2ecf20Sopenharmony_ci struct reset_control *rst; 1488c2ecf20Sopenharmony_ci /* Synchronizes I/O mem access to base. */ 1498c2ecf20Sopenharmony_ci spinlock_t lock; 1508c2ecf20Sopenharmony_ci struct completion cmd_complete; 1518c2ecf20Sopenharmony_ci u32 (*get_clk_reg_val)(struct device *dev, 1528c2ecf20Sopenharmony_ci u32 divisor); 1538c2ecf20Sopenharmony_ci unsigned long parent_clk_frequency; 1548c2ecf20Sopenharmony_ci u32 bus_frequency; 1558c2ecf20Sopenharmony_ci /* Transaction state. */ 1568c2ecf20Sopenharmony_ci enum aspeed_i2c_master_state master_state; 1578c2ecf20Sopenharmony_ci struct i2c_msg *msgs; 1588c2ecf20Sopenharmony_ci size_t buf_index; 1598c2ecf20Sopenharmony_ci size_t msgs_index; 1608c2ecf20Sopenharmony_ci size_t msgs_count; 1618c2ecf20Sopenharmony_ci bool send_stop; 1628c2ecf20Sopenharmony_ci int cmd_err; 1638c2ecf20Sopenharmony_ci /* Protected only by i2c_lock_bus */ 1648c2ecf20Sopenharmony_ci int master_xfer_result; 1658c2ecf20Sopenharmony_ci /* Multi-master */ 1668c2ecf20Sopenharmony_ci bool multi_master; 1678c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 1688c2ecf20Sopenharmony_ci struct i2c_client *slave; 1698c2ecf20Sopenharmony_ci enum aspeed_i2c_slave_state slave_state; 1708c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic int aspeed_i2c_reset(struct aspeed_i2c_bus *bus); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci unsigned long time_left, flags; 1788c2ecf20Sopenharmony_ci int ret = 0; 1798c2ecf20Sopenharmony_ci u32 command; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 1828c2ecf20Sopenharmony_ci command = readl(bus->base + ASPEED_I2C_CMD_REG); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci if (command & ASPEED_I2CD_SDA_LINE_STS) { 1858c2ecf20Sopenharmony_ci /* Bus is idle: no recovery needed. */ 1868c2ecf20Sopenharmony_ci if (command & ASPEED_I2CD_SCL_LINE_STS) 1878c2ecf20Sopenharmony_ci goto out; 1888c2ecf20Sopenharmony_ci dev_dbg(bus->dev, "SCL hung (state %x), attempting recovery\n", 1898c2ecf20Sopenharmony_ci command); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci reinit_completion(&bus->cmd_complete); 1928c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_M_STOP_CMD, bus->base + ASPEED_I2C_CMD_REG); 1938c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci time_left = wait_for_completion_timeout( 1968c2ecf20Sopenharmony_ci &bus->cmd_complete, bus->adap.timeout); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 1998c2ecf20Sopenharmony_ci if (time_left == 0) 2008c2ecf20Sopenharmony_ci goto reset_out; 2018c2ecf20Sopenharmony_ci else if (bus->cmd_err) 2028c2ecf20Sopenharmony_ci goto reset_out; 2038c2ecf20Sopenharmony_ci /* Recovery failed. */ 2048c2ecf20Sopenharmony_ci else if (!(readl(bus->base + ASPEED_I2C_CMD_REG) & 2058c2ecf20Sopenharmony_ci ASPEED_I2CD_SCL_LINE_STS)) 2068c2ecf20Sopenharmony_ci goto reset_out; 2078c2ecf20Sopenharmony_ci /* Bus error. */ 2088c2ecf20Sopenharmony_ci } else { 2098c2ecf20Sopenharmony_ci dev_dbg(bus->dev, "SDA hung (state %x), attempting recovery\n", 2108c2ecf20Sopenharmony_ci command); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci reinit_completion(&bus->cmd_complete); 2138c2ecf20Sopenharmony_ci /* Writes 1 to 8 SCL clock cycles until SDA is released. */ 2148c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_BUS_RECOVER_CMD, 2158c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_CMD_REG); 2168c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci time_left = wait_for_completion_timeout( 2198c2ecf20Sopenharmony_ci &bus->cmd_complete, bus->adap.timeout); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 2228c2ecf20Sopenharmony_ci if (time_left == 0) 2238c2ecf20Sopenharmony_ci goto reset_out; 2248c2ecf20Sopenharmony_ci else if (bus->cmd_err) 2258c2ecf20Sopenharmony_ci goto reset_out; 2268c2ecf20Sopenharmony_ci /* Recovery failed. */ 2278c2ecf20Sopenharmony_ci else if (!(readl(bus->base + ASPEED_I2C_CMD_REG) & 2288c2ecf20Sopenharmony_ci ASPEED_I2CD_SDA_LINE_STS)) 2298c2ecf20Sopenharmony_ci goto reset_out; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ciout: 2338c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci return ret; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cireset_out: 2388c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci return aspeed_i2c_reset(bus); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 2448c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus, u32 irq_status) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci u32 command, irq_handled = 0; 2478c2ecf20Sopenharmony_ci struct i2c_client *slave = bus->slave; 2488c2ecf20Sopenharmony_ci u8 value; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci if (!slave) 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci /* 2548c2ecf20Sopenharmony_ci * Handle stop conditions early, prior to SLAVE_MATCH. Some masters may drive 2558c2ecf20Sopenharmony_ci * transfers with low enough latency between the nak/stop phase of the current 2568c2ecf20Sopenharmony_ci * command and the start/address phase of the following command that the 2578c2ecf20Sopenharmony_ci * interrupts are coalesced by the time we process them. 2588c2ecf20Sopenharmony_ci */ 2598c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_NORMAL_STOP) { 2608c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_NORMAL_STOP; 2618c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_STOP; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_TX_NAK && 2658c2ecf20Sopenharmony_ci bus->slave_state == ASPEED_I2C_SLAVE_READ_PROCESSED) { 2668c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_NAK; 2678c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_STOP; 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* Propagate any stop conditions to the slave implementation. */ 2718c2ecf20Sopenharmony_ci if (bus->slave_state == ASPEED_I2C_SLAVE_STOP) { 2728c2ecf20Sopenharmony_ci i2c_slave_event(slave, I2C_SLAVE_STOP, &value); 2738c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_INACTIVE; 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci /* 2778c2ecf20Sopenharmony_ci * Now that we've dealt with any potentially coalesced stop conditions, 2788c2ecf20Sopenharmony_ci * address any start conditions. 2798c2ecf20Sopenharmony_ci */ 2808c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_SLAVE_MATCH) { 2818c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_SLAVE_MATCH; 2828c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_START; 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci /* 2868c2ecf20Sopenharmony_ci * If the slave has been stopped and not started then slave interrupt 2878c2ecf20Sopenharmony_ci * handling is complete. 2888c2ecf20Sopenharmony_ci */ 2898c2ecf20Sopenharmony_ci if (bus->slave_state == ASPEED_I2C_SLAVE_INACTIVE) 2908c2ecf20Sopenharmony_ci return irq_handled; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci command = readl(bus->base + ASPEED_I2C_CMD_REG); 2938c2ecf20Sopenharmony_ci dev_dbg(bus->dev, "slave irq status 0x%08x, cmd 0x%08x\n", 2948c2ecf20Sopenharmony_ci irq_status, command); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci /* Slave was sent something. */ 2978c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_RX_DONE) { 2988c2ecf20Sopenharmony_ci value = readl(bus->base + ASPEED_I2C_BYTE_BUF_REG) >> 8; 2998c2ecf20Sopenharmony_ci /* Handle address frame. */ 3008c2ecf20Sopenharmony_ci if (bus->slave_state == ASPEED_I2C_SLAVE_START) { 3018c2ecf20Sopenharmony_ci if (value & 0x1) 3028c2ecf20Sopenharmony_ci bus->slave_state = 3038c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_READ_REQUESTED; 3048c2ecf20Sopenharmony_ci else 3058c2ecf20Sopenharmony_ci bus->slave_state = 3068c2ecf20Sopenharmony_ci ASPEED_I2C_SLAVE_WRITE_REQUESTED; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_RX_DONE; 3098c2ecf20Sopenharmony_ci } 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci switch (bus->slave_state) { 3128c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_READ_REQUESTED: 3138c2ecf20Sopenharmony_ci if (unlikely(irq_status & ASPEED_I2CD_INTR_TX_ACK)) 3148c2ecf20Sopenharmony_ci dev_err(bus->dev, "Unexpected ACK on read request.\n"); 3158c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_READ_PROCESSED; 3168c2ecf20Sopenharmony_ci i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value); 3178c2ecf20Sopenharmony_ci writel(value, bus->base + ASPEED_I2C_BYTE_BUF_REG); 3188c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_S_TX_CMD, bus->base + ASPEED_I2C_CMD_REG); 3198c2ecf20Sopenharmony_ci break; 3208c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_READ_PROCESSED: 3218c2ecf20Sopenharmony_ci if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 3228c2ecf20Sopenharmony_ci dev_err(bus->dev, 3238c2ecf20Sopenharmony_ci "Expected ACK after processed read.\n"); 3248c2ecf20Sopenharmony_ci break; 3258c2ecf20Sopenharmony_ci } 3268c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_ACK; 3278c2ecf20Sopenharmony_ci i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value); 3288c2ecf20Sopenharmony_ci writel(value, bus->base + ASPEED_I2C_BYTE_BUF_REG); 3298c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_S_TX_CMD, bus->base + ASPEED_I2C_CMD_REG); 3308c2ecf20Sopenharmony_ci break; 3318c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_WRITE_REQUESTED: 3328c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_WRITE_RECEIVED; 3338c2ecf20Sopenharmony_ci i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value); 3348c2ecf20Sopenharmony_ci break; 3358c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_WRITE_RECEIVED: 3368c2ecf20Sopenharmony_ci i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED, &value); 3378c2ecf20Sopenharmony_ci break; 3388c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_STOP: 3398c2ecf20Sopenharmony_ci /* Stop event handling is done early. Unreachable. */ 3408c2ecf20Sopenharmony_ci break; 3418c2ecf20Sopenharmony_ci case ASPEED_I2C_SLAVE_START: 3428c2ecf20Sopenharmony_ci /* Slave was just started. Waiting for the next event. */; 3438c2ecf20Sopenharmony_ci break; 3448c2ecf20Sopenharmony_ci default: 3458c2ecf20Sopenharmony_ci dev_err(bus->dev, "unknown slave_state: %d\n", 3468c2ecf20Sopenharmony_ci bus->slave_state); 3478c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_INACTIVE; 3488c2ecf20Sopenharmony_ci break; 3498c2ecf20Sopenharmony_ci } 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci return irq_handled; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 3568c2ecf20Sopenharmony_cistatic void aspeed_i2c_do_start(struct aspeed_i2c_bus *bus) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci u32 command = ASPEED_I2CD_M_START_CMD | ASPEED_I2CD_M_TX_CMD; 3598c2ecf20Sopenharmony_ci struct i2c_msg *msg = &bus->msgs[bus->msgs_index]; 3608c2ecf20Sopenharmony_ci u8 slave_addr = i2c_8bit_addr_from_msg(msg); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 3638c2ecf20Sopenharmony_ci /* 3648c2ecf20Sopenharmony_ci * If it's requested in the middle of a slave session, set the master 3658c2ecf20Sopenharmony_ci * state to 'pending' then H/W will continue handling this master 3668c2ecf20Sopenharmony_ci * command when the bus comes back to the idle state. 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_ci if (bus->slave_state != ASPEED_I2C_SLAVE_INACTIVE) { 3698c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_PENDING; 3708c2ecf20Sopenharmony_ci return; 3718c2ecf20Sopenharmony_ci } 3728c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_START; 3758c2ecf20Sopenharmony_ci bus->buf_index = 0; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_RD) { 3788c2ecf20Sopenharmony_ci command |= ASPEED_I2CD_M_RX_CMD; 3798c2ecf20Sopenharmony_ci /* Need to let the hardware know to NACK after RX. */ 3808c2ecf20Sopenharmony_ci if (msg->len == 1 && !(msg->flags & I2C_M_RECV_LEN)) 3818c2ecf20Sopenharmony_ci command |= ASPEED_I2CD_M_S_RX_CMD_LAST; 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci writel(slave_addr, bus->base + ASPEED_I2C_BYTE_BUF_REG); 3858c2ecf20Sopenharmony_ci writel(command, bus->base + ASPEED_I2C_CMD_REG); 3868c2ecf20Sopenharmony_ci} 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 3898c2ecf20Sopenharmony_cistatic void aspeed_i2c_do_stop(struct aspeed_i2c_bus *bus) 3908c2ecf20Sopenharmony_ci{ 3918c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_STOP; 3928c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_M_STOP_CMD, bus->base + ASPEED_I2C_CMD_REG); 3938c2ecf20Sopenharmony_ci} 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 3968c2ecf20Sopenharmony_cistatic void aspeed_i2c_next_msg_or_stop(struct aspeed_i2c_bus *bus) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci if (bus->msgs_index + 1 < bus->msgs_count) { 3998c2ecf20Sopenharmony_ci bus->msgs_index++; 4008c2ecf20Sopenharmony_ci aspeed_i2c_do_start(bus); 4018c2ecf20Sopenharmony_ci } else { 4028c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci} 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_cistatic int aspeed_i2c_is_irq_error(u32 irq_status) 4078c2ecf20Sopenharmony_ci{ 4088c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_ARBIT_LOSS) 4098c2ecf20Sopenharmony_ci return -EAGAIN; 4108c2ecf20Sopenharmony_ci if (irq_status & (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | 4118c2ecf20Sopenharmony_ci ASPEED_I2CD_INTR_SCL_TIMEOUT)) 4128c2ecf20Sopenharmony_ci return -EBUSY; 4138c2ecf20Sopenharmony_ci if (irq_status & (ASPEED_I2CD_INTR_ABNORMAL)) 4148c2ecf20Sopenharmony_ci return -EPROTO; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci return 0; 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status) 4208c2ecf20Sopenharmony_ci{ 4218c2ecf20Sopenharmony_ci u32 irq_handled = 0, command = 0; 4228c2ecf20Sopenharmony_ci struct i2c_msg *msg; 4238c2ecf20Sopenharmony_ci u8 recv_byte; 4248c2ecf20Sopenharmony_ci int ret; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci if (irq_status & ASPEED_I2CD_INTR_BUS_RECOVER_DONE) { 4278c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 4288c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_BUS_RECOVER_DONE; 4298c2ecf20Sopenharmony_ci goto out_complete; 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci /* 4338c2ecf20Sopenharmony_ci * We encountered an interrupt that reports an error: the hardware 4348c2ecf20Sopenharmony_ci * should clear the command queue effectively taking us back to the 4358c2ecf20Sopenharmony_ci * INACTIVE state. 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci ret = aspeed_i2c_is_irq_error(irq_status); 4388c2ecf20Sopenharmony_ci if (ret) { 4398c2ecf20Sopenharmony_ci dev_dbg(bus->dev, "received error interrupt: 0x%08x\n", 4408c2ecf20Sopenharmony_ci irq_status); 4418c2ecf20Sopenharmony_ci irq_handled |= (irq_status & ASPEED_I2CD_INTR_MASTER_ERRORS); 4428c2ecf20Sopenharmony_ci if (bus->master_state != ASPEED_I2C_MASTER_INACTIVE) { 4438c2ecf20Sopenharmony_ci bus->cmd_err = ret; 4448c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 4458c2ecf20Sopenharmony_ci goto out_complete; 4468c2ecf20Sopenharmony_ci } 4478c2ecf20Sopenharmony_ci } 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci /* Master is not currently active, irq was for someone else. */ 4508c2ecf20Sopenharmony_ci if (bus->master_state == ASPEED_I2C_MASTER_INACTIVE || 4518c2ecf20Sopenharmony_ci bus->master_state == ASPEED_I2C_MASTER_PENDING) 4528c2ecf20Sopenharmony_ci goto out_no_complete; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci /* We are in an invalid state; reset bus to a known state. */ 4558c2ecf20Sopenharmony_ci if (!bus->msgs) { 4568c2ecf20Sopenharmony_ci dev_err(bus->dev, "bus in unknown state. irq_status: 0x%x\n", 4578c2ecf20Sopenharmony_ci irq_status); 4588c2ecf20Sopenharmony_ci bus->cmd_err = -EIO; 4598c2ecf20Sopenharmony_ci if (bus->master_state != ASPEED_I2C_MASTER_STOP && 4608c2ecf20Sopenharmony_ci bus->master_state != ASPEED_I2C_MASTER_INACTIVE) 4618c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 4628c2ecf20Sopenharmony_ci goto out_no_complete; 4638c2ecf20Sopenharmony_ci } 4648c2ecf20Sopenharmony_ci msg = &bus->msgs[bus->msgs_index]; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci /* 4678c2ecf20Sopenharmony_ci * START is a special case because we still have to handle a subsequent 4688c2ecf20Sopenharmony_ci * TX or RX immediately after we handle it, so we handle it here and 4698c2ecf20Sopenharmony_ci * then update the state and handle the new state below. 4708c2ecf20Sopenharmony_ci */ 4718c2ecf20Sopenharmony_ci if (bus->master_state == ASPEED_I2C_MASTER_START) { 4728c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 4738c2ecf20Sopenharmony_ci /* 4748c2ecf20Sopenharmony_ci * If a peer master starts a xfer immediately after it queues a 4758c2ecf20Sopenharmony_ci * master command, clear the queued master command and change 4768c2ecf20Sopenharmony_ci * its state to 'pending'. To simplify handling of pending 4778c2ecf20Sopenharmony_ci * cases, it uses S/W solution instead of H/W command queue 4788c2ecf20Sopenharmony_ci * handling. 4798c2ecf20Sopenharmony_ci */ 4808c2ecf20Sopenharmony_ci if (unlikely(irq_status & ASPEED_I2CD_INTR_SLAVE_MATCH)) { 4818c2ecf20Sopenharmony_ci writel(readl(bus->base + ASPEED_I2C_CMD_REG) & 4828c2ecf20Sopenharmony_ci ~ASPEED_I2CD_MASTER_CMDS_MASK, 4838c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_CMD_REG); 4848c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_PENDING; 4858c2ecf20Sopenharmony_ci dev_dbg(bus->dev, 4868c2ecf20Sopenharmony_ci "master goes pending due to a slave start\n"); 4878c2ecf20Sopenharmony_ci goto out_no_complete; 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 4908c2ecf20Sopenharmony_ci if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 4918c2ecf20Sopenharmony_ci if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_NAK))) { 4928c2ecf20Sopenharmony_ci bus->cmd_err = -ENXIO; 4938c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 4948c2ecf20Sopenharmony_ci goto out_complete; 4958c2ecf20Sopenharmony_ci } 4968c2ecf20Sopenharmony_ci pr_devel("no slave present at %02x\n", msg->addr); 4978c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_NAK; 4988c2ecf20Sopenharmony_ci bus->cmd_err = -ENXIO; 4998c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 5008c2ecf20Sopenharmony_ci goto out_no_complete; 5018c2ecf20Sopenharmony_ci } 5028c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_ACK; 5038c2ecf20Sopenharmony_ci if (msg->len == 0) { /* SMBUS_QUICK */ 5048c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 5058c2ecf20Sopenharmony_ci goto out_no_complete; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_RD) 5088c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_RX_FIRST; 5098c2ecf20Sopenharmony_ci else 5108c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_TX_FIRST; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci switch (bus->master_state) { 5148c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_TX: 5158c2ecf20Sopenharmony_ci if (unlikely(irq_status & ASPEED_I2CD_INTR_TX_NAK)) { 5168c2ecf20Sopenharmony_ci dev_dbg(bus->dev, "slave NACKed TX\n"); 5178c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_NAK; 5188c2ecf20Sopenharmony_ci goto error_and_stop; 5198c2ecf20Sopenharmony_ci } else if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 5208c2ecf20Sopenharmony_ci dev_err(bus->dev, "slave failed to ACK TX\n"); 5218c2ecf20Sopenharmony_ci goto error_and_stop; 5228c2ecf20Sopenharmony_ci } 5238c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_TX_ACK; 5248c2ecf20Sopenharmony_ci fallthrough; 5258c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_TX_FIRST: 5268c2ecf20Sopenharmony_ci if (bus->buf_index < msg->len) { 5278c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_TX; 5288c2ecf20Sopenharmony_ci writel(msg->buf[bus->buf_index++], 5298c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_BYTE_BUF_REG); 5308c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_M_TX_CMD, 5318c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_CMD_REG); 5328c2ecf20Sopenharmony_ci } else { 5338c2ecf20Sopenharmony_ci aspeed_i2c_next_msg_or_stop(bus); 5348c2ecf20Sopenharmony_ci } 5358c2ecf20Sopenharmony_ci goto out_no_complete; 5368c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_RX_FIRST: 5378c2ecf20Sopenharmony_ci /* RX may not have completed yet (only address cycle) */ 5388c2ecf20Sopenharmony_ci if (!(irq_status & ASPEED_I2CD_INTR_RX_DONE)) 5398c2ecf20Sopenharmony_ci goto out_no_complete; 5408c2ecf20Sopenharmony_ci fallthrough; 5418c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_RX: 5428c2ecf20Sopenharmony_ci if (unlikely(!(irq_status & ASPEED_I2CD_INTR_RX_DONE))) { 5438c2ecf20Sopenharmony_ci dev_err(bus->dev, "master failed to RX\n"); 5448c2ecf20Sopenharmony_ci goto error_and_stop; 5458c2ecf20Sopenharmony_ci } 5468c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_RX_DONE; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci recv_byte = readl(bus->base + ASPEED_I2C_BYTE_BUF_REG) >> 8; 5498c2ecf20Sopenharmony_ci msg->buf[bus->buf_index++] = recv_byte; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_RECV_LEN) { 5528c2ecf20Sopenharmony_ci if (unlikely(recv_byte > I2C_SMBUS_BLOCK_MAX)) { 5538c2ecf20Sopenharmony_ci bus->cmd_err = -EPROTO; 5548c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 5558c2ecf20Sopenharmony_ci goto out_no_complete; 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci msg->len = recv_byte + 5588c2ecf20Sopenharmony_ci ((msg->flags & I2C_CLIENT_PEC) ? 2 : 1); 5598c2ecf20Sopenharmony_ci msg->flags &= ~I2C_M_RECV_LEN; 5608c2ecf20Sopenharmony_ci } 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci if (bus->buf_index < msg->len) { 5638c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_RX; 5648c2ecf20Sopenharmony_ci command = ASPEED_I2CD_M_RX_CMD; 5658c2ecf20Sopenharmony_ci if (bus->buf_index + 1 == msg->len) 5668c2ecf20Sopenharmony_ci command |= ASPEED_I2CD_M_S_RX_CMD_LAST; 5678c2ecf20Sopenharmony_ci writel(command, bus->base + ASPEED_I2C_CMD_REG); 5688c2ecf20Sopenharmony_ci } else { 5698c2ecf20Sopenharmony_ci aspeed_i2c_next_msg_or_stop(bus); 5708c2ecf20Sopenharmony_ci } 5718c2ecf20Sopenharmony_ci goto out_no_complete; 5728c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_STOP: 5738c2ecf20Sopenharmony_ci if (unlikely(!(irq_status & ASPEED_I2CD_INTR_NORMAL_STOP))) { 5748c2ecf20Sopenharmony_ci dev_err(bus->dev, 5758c2ecf20Sopenharmony_ci "master failed to STOP. irq_status:0x%x\n", 5768c2ecf20Sopenharmony_ci irq_status); 5778c2ecf20Sopenharmony_ci bus->cmd_err = -EIO; 5788c2ecf20Sopenharmony_ci /* Do not STOP as we have already tried. */ 5798c2ecf20Sopenharmony_ci } else { 5808c2ecf20Sopenharmony_ci irq_handled |= ASPEED_I2CD_INTR_NORMAL_STOP; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 5848c2ecf20Sopenharmony_ci goto out_complete; 5858c2ecf20Sopenharmony_ci case ASPEED_I2C_MASTER_INACTIVE: 5868c2ecf20Sopenharmony_ci dev_err(bus->dev, 5878c2ecf20Sopenharmony_ci "master received interrupt 0x%08x, but is inactive\n", 5888c2ecf20Sopenharmony_ci irq_status); 5898c2ecf20Sopenharmony_ci bus->cmd_err = -EIO; 5908c2ecf20Sopenharmony_ci /* Do not STOP as we should be inactive. */ 5918c2ecf20Sopenharmony_ci goto out_complete; 5928c2ecf20Sopenharmony_ci default: 5938c2ecf20Sopenharmony_ci WARN(1, "unknown master state\n"); 5948c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 5958c2ecf20Sopenharmony_ci bus->cmd_err = -EINVAL; 5968c2ecf20Sopenharmony_ci goto out_complete; 5978c2ecf20Sopenharmony_ci } 5988c2ecf20Sopenharmony_cierror_and_stop: 5998c2ecf20Sopenharmony_ci bus->cmd_err = -EIO; 6008c2ecf20Sopenharmony_ci aspeed_i2c_do_stop(bus); 6018c2ecf20Sopenharmony_ci goto out_no_complete; 6028c2ecf20Sopenharmony_ciout_complete: 6038c2ecf20Sopenharmony_ci bus->msgs = NULL; 6048c2ecf20Sopenharmony_ci if (bus->cmd_err) 6058c2ecf20Sopenharmony_ci bus->master_xfer_result = bus->cmd_err; 6068c2ecf20Sopenharmony_ci else 6078c2ecf20Sopenharmony_ci bus->master_xfer_result = bus->msgs_index + 1; 6088c2ecf20Sopenharmony_ci complete(&bus->cmd_complete); 6098c2ecf20Sopenharmony_ciout_no_complete: 6108c2ecf20Sopenharmony_ci return irq_handled; 6118c2ecf20Sopenharmony_ci} 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_cistatic irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus = dev_id; 6168c2ecf20Sopenharmony_ci u32 irq_received, irq_remaining, irq_handled; 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci spin_lock(&bus->lock); 6198c2ecf20Sopenharmony_ci irq_received = readl(bus->base + ASPEED_I2C_INTR_STS_REG); 6208c2ecf20Sopenharmony_ci /* Ack all interrupts except for Rx done */ 6218c2ecf20Sopenharmony_ci writel(irq_received & ~ASPEED_I2CD_INTR_RX_DONE, 6228c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_INTR_STS_REG); 6238c2ecf20Sopenharmony_ci readl(bus->base + ASPEED_I2C_INTR_STS_REG); 6248c2ecf20Sopenharmony_ci irq_received &= ASPEED_I2CD_INTR_RECV_MASK; 6258c2ecf20Sopenharmony_ci irq_remaining = irq_received; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 6288c2ecf20Sopenharmony_ci /* 6298c2ecf20Sopenharmony_ci * In most cases, interrupt bits will be set one by one, although 6308c2ecf20Sopenharmony_ci * multiple interrupt bits could be set at the same time. It's also 6318c2ecf20Sopenharmony_ci * possible that master interrupt bits could be set along with slave 6328c2ecf20Sopenharmony_ci * interrupt bits. Each case needs to be handled using corresponding 6338c2ecf20Sopenharmony_ci * handlers depending on the current state. 6348c2ecf20Sopenharmony_ci */ 6358c2ecf20Sopenharmony_ci if (bus->master_state != ASPEED_I2C_MASTER_INACTIVE && 6368c2ecf20Sopenharmony_ci bus->master_state != ASPEED_I2C_MASTER_PENDING) { 6378c2ecf20Sopenharmony_ci irq_handled = aspeed_i2c_master_irq(bus, irq_remaining); 6388c2ecf20Sopenharmony_ci irq_remaining &= ~irq_handled; 6398c2ecf20Sopenharmony_ci if (irq_remaining) 6408c2ecf20Sopenharmony_ci irq_handled |= aspeed_i2c_slave_irq(bus, irq_remaining); 6418c2ecf20Sopenharmony_ci } else { 6428c2ecf20Sopenharmony_ci irq_handled = aspeed_i2c_slave_irq(bus, irq_remaining); 6438c2ecf20Sopenharmony_ci irq_remaining &= ~irq_handled; 6448c2ecf20Sopenharmony_ci if (irq_remaining) 6458c2ecf20Sopenharmony_ci irq_handled |= aspeed_i2c_master_irq(bus, 6468c2ecf20Sopenharmony_ci irq_remaining); 6478c2ecf20Sopenharmony_ci } 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci /* 6508c2ecf20Sopenharmony_ci * Start a pending master command at here if a slave operation is 6518c2ecf20Sopenharmony_ci * completed. 6528c2ecf20Sopenharmony_ci */ 6538c2ecf20Sopenharmony_ci if (bus->master_state == ASPEED_I2C_MASTER_PENDING && 6548c2ecf20Sopenharmony_ci bus->slave_state == ASPEED_I2C_SLAVE_INACTIVE) 6558c2ecf20Sopenharmony_ci aspeed_i2c_do_start(bus); 6568c2ecf20Sopenharmony_ci#else 6578c2ecf20Sopenharmony_ci irq_handled = aspeed_i2c_master_irq(bus, irq_remaining); 6588c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci irq_remaining &= ~irq_handled; 6618c2ecf20Sopenharmony_ci if (irq_remaining) 6628c2ecf20Sopenharmony_ci dev_err(bus->dev, 6638c2ecf20Sopenharmony_ci "irq handled != irq. expected 0x%08x, but was 0x%08x\n", 6648c2ecf20Sopenharmony_ci irq_received, irq_handled); 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci /* Ack Rx done */ 6678c2ecf20Sopenharmony_ci if (irq_received & ASPEED_I2CD_INTR_RX_DONE) { 6688c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_INTR_RX_DONE, 6698c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_INTR_STS_REG); 6708c2ecf20Sopenharmony_ci readl(bus->base + ASPEED_I2C_INTR_STS_REG); 6718c2ecf20Sopenharmony_ci } 6728c2ecf20Sopenharmony_ci spin_unlock(&bus->lock); 6738c2ecf20Sopenharmony_ci return irq_remaining ? IRQ_NONE : IRQ_HANDLED; 6748c2ecf20Sopenharmony_ci} 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_cistatic int aspeed_i2c_master_xfer(struct i2c_adapter *adap, 6778c2ecf20Sopenharmony_ci struct i2c_msg *msgs, int num) 6788c2ecf20Sopenharmony_ci{ 6798c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus = i2c_get_adapdata(adap); 6808c2ecf20Sopenharmony_ci unsigned long time_left, flags; 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 6838c2ecf20Sopenharmony_ci bus->cmd_err = 0; 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci /* If bus is busy in a single master environment, attempt recovery. */ 6868c2ecf20Sopenharmony_ci if (!bus->multi_master && 6878c2ecf20Sopenharmony_ci (readl(bus->base + ASPEED_I2C_CMD_REG) & 6888c2ecf20Sopenharmony_ci ASPEED_I2CD_BUS_BUSY_STS)) { 6898c2ecf20Sopenharmony_ci int ret; 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 6928c2ecf20Sopenharmony_ci ret = aspeed_i2c_recover_bus(bus); 6938c2ecf20Sopenharmony_ci if (ret) 6948c2ecf20Sopenharmony_ci return ret; 6958c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 6968c2ecf20Sopenharmony_ci } 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci bus->cmd_err = 0; 6998c2ecf20Sopenharmony_ci bus->msgs = msgs; 7008c2ecf20Sopenharmony_ci bus->msgs_index = 0; 7018c2ecf20Sopenharmony_ci bus->msgs_count = num; 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci reinit_completion(&bus->cmd_complete); 7048c2ecf20Sopenharmony_ci aspeed_i2c_do_start(bus); 7058c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci time_left = wait_for_completion_timeout(&bus->cmd_complete, 7088c2ecf20Sopenharmony_ci bus->adap.timeout); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci if (time_left == 0) { 7118c2ecf20Sopenharmony_ci /* 7128c2ecf20Sopenharmony_ci * In a multi-master setup, if a timeout occurs, attempt 7138c2ecf20Sopenharmony_ci * recovery. But if the bus is idle, we still need to reset the 7148c2ecf20Sopenharmony_ci * i2c controller to clear the remaining interrupts. 7158c2ecf20Sopenharmony_ci */ 7168c2ecf20Sopenharmony_ci if (bus->multi_master && 7178c2ecf20Sopenharmony_ci (readl(bus->base + ASPEED_I2C_CMD_REG) & 7188c2ecf20Sopenharmony_ci ASPEED_I2CD_BUS_BUSY_STS)) 7198c2ecf20Sopenharmony_ci aspeed_i2c_recover_bus(bus); 7208c2ecf20Sopenharmony_ci else 7218c2ecf20Sopenharmony_ci aspeed_i2c_reset(bus); 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci /* 7248c2ecf20Sopenharmony_ci * If timed out and the state is still pending, drop the pending 7258c2ecf20Sopenharmony_ci * master command. 7268c2ecf20Sopenharmony_ci */ 7278c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 7288c2ecf20Sopenharmony_ci if (bus->master_state == ASPEED_I2C_MASTER_PENDING) 7298c2ecf20Sopenharmony_ci bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 7308c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci return -ETIMEDOUT; 7338c2ecf20Sopenharmony_ci } 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ci return bus->master_xfer_result; 7368c2ecf20Sopenharmony_ci} 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_functionality(struct i2c_adapter *adap) 7398c2ecf20Sopenharmony_ci{ 7408c2ecf20Sopenharmony_ci return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA; 7418c2ecf20Sopenharmony_ci} 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 7448c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 7458c2ecf20Sopenharmony_cistatic void __aspeed_i2c_reg_slave(struct aspeed_i2c_bus *bus, u16 slave_addr) 7468c2ecf20Sopenharmony_ci{ 7478c2ecf20Sopenharmony_ci u32 addr_reg_val, func_ctrl_reg_val; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci /* Set slave addr. */ 7508c2ecf20Sopenharmony_ci addr_reg_val = readl(bus->base + ASPEED_I2C_DEV_ADDR_REG); 7518c2ecf20Sopenharmony_ci addr_reg_val &= ~ASPEED_I2CD_DEV_ADDR_MASK; 7528c2ecf20Sopenharmony_ci addr_reg_val |= slave_addr & ASPEED_I2CD_DEV_ADDR_MASK; 7538c2ecf20Sopenharmony_ci writel(addr_reg_val, bus->base + ASPEED_I2C_DEV_ADDR_REG); 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci /* Turn on slave mode. */ 7568c2ecf20Sopenharmony_ci func_ctrl_reg_val = readl(bus->base + ASPEED_I2C_FUN_CTRL_REG); 7578c2ecf20Sopenharmony_ci func_ctrl_reg_val |= ASPEED_I2CD_SLAVE_EN; 7588c2ecf20Sopenharmony_ci writel(func_ctrl_reg_val, bus->base + ASPEED_I2C_FUN_CTRL_REG); 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci bus->slave_state = ASPEED_I2C_SLAVE_INACTIVE; 7618c2ecf20Sopenharmony_ci} 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_cistatic int aspeed_i2c_reg_slave(struct i2c_client *client) 7648c2ecf20Sopenharmony_ci{ 7658c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus = i2c_get_adapdata(client->adapter); 7668c2ecf20Sopenharmony_ci unsigned long flags; 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 7698c2ecf20Sopenharmony_ci if (bus->slave) { 7708c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 7718c2ecf20Sopenharmony_ci return -EINVAL; 7728c2ecf20Sopenharmony_ci } 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci __aspeed_i2c_reg_slave(bus, client->addr); 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci bus->slave = client; 7778c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci return 0; 7808c2ecf20Sopenharmony_ci} 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_cistatic int aspeed_i2c_unreg_slave(struct i2c_client *client) 7838c2ecf20Sopenharmony_ci{ 7848c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus = i2c_get_adapdata(client->adapter); 7858c2ecf20Sopenharmony_ci u32 func_ctrl_reg_val; 7868c2ecf20Sopenharmony_ci unsigned long flags; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 7898c2ecf20Sopenharmony_ci if (!bus->slave) { 7908c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 7918c2ecf20Sopenharmony_ci return -EINVAL; 7928c2ecf20Sopenharmony_ci } 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci /* Turn off slave mode. */ 7958c2ecf20Sopenharmony_ci func_ctrl_reg_val = readl(bus->base + ASPEED_I2C_FUN_CTRL_REG); 7968c2ecf20Sopenharmony_ci func_ctrl_reg_val &= ~ASPEED_I2CD_SLAVE_EN; 7978c2ecf20Sopenharmony_ci writel(func_ctrl_reg_val, bus->base + ASPEED_I2C_FUN_CTRL_REG); 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci bus->slave = NULL; 8008c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci return 0; 8038c2ecf20Sopenharmony_ci} 8048c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_cistatic const struct i2c_algorithm aspeed_i2c_algo = { 8078c2ecf20Sopenharmony_ci .master_xfer = aspeed_i2c_master_xfer, 8088c2ecf20Sopenharmony_ci .functionality = aspeed_i2c_functionality, 8098c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 8108c2ecf20Sopenharmony_ci .reg_slave = aspeed_i2c_reg_slave, 8118c2ecf20Sopenharmony_ci .unreg_slave = aspeed_i2c_unreg_slave, 8128c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 8138c2ecf20Sopenharmony_ci}; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_get_clk_reg_val(struct device *dev, 8168c2ecf20Sopenharmony_ci u32 clk_high_low_mask, 8178c2ecf20Sopenharmony_ci u32 divisor) 8188c2ecf20Sopenharmony_ci{ 8198c2ecf20Sopenharmony_ci u32 base_clk_divisor, clk_high_low_max, clk_high, clk_low, tmp; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci /* 8228c2ecf20Sopenharmony_ci * SCL_high and SCL_low represent a value 1 greater than what is stored 8238c2ecf20Sopenharmony_ci * since a zero divider is meaningless. Thus, the max value each can 8248c2ecf20Sopenharmony_ci * store is every bit set + 1. Since SCL_high and SCL_low are added 8258c2ecf20Sopenharmony_ci * together (see below), the max value of both is the max value of one 8268c2ecf20Sopenharmony_ci * them times two. 8278c2ecf20Sopenharmony_ci */ 8288c2ecf20Sopenharmony_ci clk_high_low_max = (clk_high_low_mask + 1) * 2; 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci /* 8318c2ecf20Sopenharmony_ci * The actual clock frequency of SCL is: 8328c2ecf20Sopenharmony_ci * SCL_freq = APB_freq / (base_freq * (SCL_high + SCL_low)) 8338c2ecf20Sopenharmony_ci * = APB_freq / divisor 8348c2ecf20Sopenharmony_ci * where base_freq is a programmable clock divider; its value is 8358c2ecf20Sopenharmony_ci * base_freq = 1 << base_clk_divisor 8368c2ecf20Sopenharmony_ci * SCL_high is the number of base_freq clock cycles that SCL stays high 8378c2ecf20Sopenharmony_ci * and SCL_low is the number of base_freq clock cycles that SCL stays 8388c2ecf20Sopenharmony_ci * low for a period of SCL. 8398c2ecf20Sopenharmony_ci * The actual register has a minimum SCL_high and SCL_low minimum of 1; 8408c2ecf20Sopenharmony_ci * thus, they start counting at zero. So 8418c2ecf20Sopenharmony_ci * SCL_high = clk_high + 1 8428c2ecf20Sopenharmony_ci * SCL_low = clk_low + 1 8438c2ecf20Sopenharmony_ci * Thus, 8448c2ecf20Sopenharmony_ci * SCL_freq = APB_freq / 8458c2ecf20Sopenharmony_ci * ((1 << base_clk_divisor) * (clk_high + 1 + clk_low + 1)) 8468c2ecf20Sopenharmony_ci * The documentation recommends clk_high >= clk_high_max / 2 and 8478c2ecf20Sopenharmony_ci * clk_low >= clk_low_max / 2 - 1 when possible; this last constraint 8488c2ecf20Sopenharmony_ci * gives us the following solution: 8498c2ecf20Sopenharmony_ci */ 8508c2ecf20Sopenharmony_ci base_clk_divisor = divisor > clk_high_low_max ? 8518c2ecf20Sopenharmony_ci ilog2((divisor - 1) / clk_high_low_max) + 1 : 0; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci if (base_clk_divisor > ASPEED_I2CD_TIME_BASE_DIVISOR_MASK) { 8548c2ecf20Sopenharmony_ci base_clk_divisor = ASPEED_I2CD_TIME_BASE_DIVISOR_MASK; 8558c2ecf20Sopenharmony_ci clk_low = clk_high_low_mask; 8568c2ecf20Sopenharmony_ci clk_high = clk_high_low_mask; 8578c2ecf20Sopenharmony_ci dev_err(dev, 8588c2ecf20Sopenharmony_ci "clamping clock divider: divider requested, %u, is greater than largest possible divider, %u.\n", 8598c2ecf20Sopenharmony_ci divisor, (1 << base_clk_divisor) * clk_high_low_max); 8608c2ecf20Sopenharmony_ci } else { 8618c2ecf20Sopenharmony_ci tmp = (divisor + (1 << base_clk_divisor) - 1) 8628c2ecf20Sopenharmony_ci >> base_clk_divisor; 8638c2ecf20Sopenharmony_ci clk_low = tmp / 2; 8648c2ecf20Sopenharmony_ci clk_high = tmp - clk_low; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci if (clk_high) 8678c2ecf20Sopenharmony_ci clk_high--; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci if (clk_low) 8708c2ecf20Sopenharmony_ci clk_low--; 8718c2ecf20Sopenharmony_ci } 8728c2ecf20Sopenharmony_ci 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci return ((clk_high << ASPEED_I2CD_TIME_SCL_HIGH_SHIFT) 8758c2ecf20Sopenharmony_ci & ASPEED_I2CD_TIME_SCL_HIGH_MASK) 8768c2ecf20Sopenharmony_ci | ((clk_low << ASPEED_I2CD_TIME_SCL_LOW_SHIFT) 8778c2ecf20Sopenharmony_ci & ASPEED_I2CD_TIME_SCL_LOW_MASK) 8788c2ecf20Sopenharmony_ci | (base_clk_divisor 8798c2ecf20Sopenharmony_ci & ASPEED_I2CD_TIME_BASE_DIVISOR_MASK); 8808c2ecf20Sopenharmony_ci} 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_24xx_get_clk_reg_val(struct device *dev, u32 divisor) 8838c2ecf20Sopenharmony_ci{ 8848c2ecf20Sopenharmony_ci /* 8858c2ecf20Sopenharmony_ci * clk_high and clk_low are each 3 bits wide, so each can hold a max 8868c2ecf20Sopenharmony_ci * value of 8 giving a clk_high_low_max of 16. 8878c2ecf20Sopenharmony_ci */ 8888c2ecf20Sopenharmony_ci return aspeed_i2c_get_clk_reg_val(dev, GENMASK(2, 0), divisor); 8898c2ecf20Sopenharmony_ci} 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_cistatic u32 aspeed_i2c_25xx_get_clk_reg_val(struct device *dev, u32 divisor) 8928c2ecf20Sopenharmony_ci{ 8938c2ecf20Sopenharmony_ci /* 8948c2ecf20Sopenharmony_ci * clk_high and clk_low are each 4 bits wide, so each can hold a max 8958c2ecf20Sopenharmony_ci * value of 16 giving a clk_high_low_max of 32. 8968c2ecf20Sopenharmony_ci */ 8978c2ecf20Sopenharmony_ci return aspeed_i2c_get_clk_reg_val(dev, GENMASK(3, 0), divisor); 8988c2ecf20Sopenharmony_ci} 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 9018c2ecf20Sopenharmony_cistatic int aspeed_i2c_init_clk(struct aspeed_i2c_bus *bus) 9028c2ecf20Sopenharmony_ci{ 9038c2ecf20Sopenharmony_ci u32 divisor, clk_reg_val; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci divisor = DIV_ROUND_UP(bus->parent_clk_frequency, bus->bus_frequency); 9068c2ecf20Sopenharmony_ci clk_reg_val = readl(bus->base + ASPEED_I2C_AC_TIMING_REG1); 9078c2ecf20Sopenharmony_ci clk_reg_val &= (ASPEED_I2CD_TIME_TBUF_MASK | 9088c2ecf20Sopenharmony_ci ASPEED_I2CD_TIME_THDSTA_MASK | 9098c2ecf20Sopenharmony_ci ASPEED_I2CD_TIME_TACST_MASK); 9108c2ecf20Sopenharmony_ci clk_reg_val |= bus->get_clk_reg_val(bus->dev, divisor); 9118c2ecf20Sopenharmony_ci writel(clk_reg_val, bus->base + ASPEED_I2C_AC_TIMING_REG1); 9128c2ecf20Sopenharmony_ci writel(ASPEED_NO_TIMEOUT_CTRL, bus->base + ASPEED_I2C_AC_TIMING_REG2); 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci return 0; 9158c2ecf20Sopenharmony_ci} 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci/* precondition: bus.lock has been acquired. */ 9188c2ecf20Sopenharmony_cistatic int aspeed_i2c_init(struct aspeed_i2c_bus *bus, 9198c2ecf20Sopenharmony_ci struct platform_device *pdev) 9208c2ecf20Sopenharmony_ci{ 9218c2ecf20Sopenharmony_ci u32 fun_ctrl_reg = ASPEED_I2CD_MASTER_EN; 9228c2ecf20Sopenharmony_ci int ret; 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci /* Disable everything. */ 9258c2ecf20Sopenharmony_ci writel(0, bus->base + ASPEED_I2C_FUN_CTRL_REG); 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci ret = aspeed_i2c_init_clk(bus); 9288c2ecf20Sopenharmony_ci if (ret < 0) 9298c2ecf20Sopenharmony_ci return ret; 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci if (of_property_read_bool(pdev->dev.of_node, "multi-master")) 9328c2ecf20Sopenharmony_ci bus->multi_master = true; 9338c2ecf20Sopenharmony_ci else 9348c2ecf20Sopenharmony_ci fun_ctrl_reg |= ASPEED_I2CD_MULTI_MASTER_DIS; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci /* Enable Master Mode */ 9378c2ecf20Sopenharmony_ci writel(readl(bus->base + ASPEED_I2C_FUN_CTRL_REG) | fun_ctrl_reg, 9388c2ecf20Sopenharmony_ci bus->base + ASPEED_I2C_FUN_CTRL_REG); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_I2C_SLAVE) 9418c2ecf20Sopenharmony_ci /* If slave has already been registered, re-enable it. */ 9428c2ecf20Sopenharmony_ci if (bus->slave) 9438c2ecf20Sopenharmony_ci __aspeed_i2c_reg_slave(bus, bus->slave->addr); 9448c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_SLAVE */ 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci /* Set interrupt generation of I2C controller */ 9478c2ecf20Sopenharmony_ci writel(ASPEED_I2CD_INTR_ALL, bus->base + ASPEED_I2C_INTR_CTRL_REG); 9488c2ecf20Sopenharmony_ci 9498c2ecf20Sopenharmony_ci return 0; 9508c2ecf20Sopenharmony_ci} 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_cistatic int aspeed_i2c_reset(struct aspeed_i2c_bus *bus) 9538c2ecf20Sopenharmony_ci{ 9548c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(bus->dev); 9558c2ecf20Sopenharmony_ci unsigned long flags; 9568c2ecf20Sopenharmony_ci int ret; 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci /* Disable and ack all interrupts. */ 9618c2ecf20Sopenharmony_ci writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 9628c2ecf20Sopenharmony_ci writel(0xffffffff, bus->base + ASPEED_I2C_INTR_STS_REG); 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci ret = aspeed_i2c_init(bus, pdev); 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci return ret; 9698c2ecf20Sopenharmony_ci} 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_cistatic const struct of_device_id aspeed_i2c_bus_of_table[] = { 9728c2ecf20Sopenharmony_ci { 9738c2ecf20Sopenharmony_ci .compatible = "aspeed,ast2400-i2c-bus", 9748c2ecf20Sopenharmony_ci .data = aspeed_i2c_24xx_get_clk_reg_val, 9758c2ecf20Sopenharmony_ci }, 9768c2ecf20Sopenharmony_ci { 9778c2ecf20Sopenharmony_ci .compatible = "aspeed,ast2500-i2c-bus", 9788c2ecf20Sopenharmony_ci .data = aspeed_i2c_25xx_get_clk_reg_val, 9798c2ecf20Sopenharmony_ci }, 9808c2ecf20Sopenharmony_ci { 9818c2ecf20Sopenharmony_ci .compatible = "aspeed,ast2600-i2c-bus", 9828c2ecf20Sopenharmony_ci .data = aspeed_i2c_25xx_get_clk_reg_val, 9838c2ecf20Sopenharmony_ci }, 9848c2ecf20Sopenharmony_ci { }, 9858c2ecf20Sopenharmony_ci}; 9868c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, aspeed_i2c_bus_of_table); 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_cistatic int aspeed_i2c_probe_bus(struct platform_device *pdev) 9898c2ecf20Sopenharmony_ci{ 9908c2ecf20Sopenharmony_ci const struct of_device_id *match; 9918c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus; 9928c2ecf20Sopenharmony_ci struct clk *parent_clk; 9938c2ecf20Sopenharmony_ci struct resource *res; 9948c2ecf20Sopenharmony_ci int irq, ret; 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); 9978c2ecf20Sopenharmony_ci if (!bus) 9988c2ecf20Sopenharmony_ci return -ENOMEM; 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 10018c2ecf20Sopenharmony_ci bus->base = devm_ioremap_resource(&pdev->dev, res); 10028c2ecf20Sopenharmony_ci if (IS_ERR(bus->base)) 10038c2ecf20Sopenharmony_ci return PTR_ERR(bus->base); 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci parent_clk = devm_clk_get(&pdev->dev, NULL); 10068c2ecf20Sopenharmony_ci if (IS_ERR(parent_clk)) 10078c2ecf20Sopenharmony_ci return PTR_ERR(parent_clk); 10088c2ecf20Sopenharmony_ci bus->parent_clk_frequency = clk_get_rate(parent_clk); 10098c2ecf20Sopenharmony_ci /* We just need the clock rate, we don't actually use the clk object. */ 10108c2ecf20Sopenharmony_ci devm_clk_put(&pdev->dev, parent_clk); 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci bus->rst = devm_reset_control_get_shared(&pdev->dev, NULL); 10138c2ecf20Sopenharmony_ci if (IS_ERR(bus->rst)) { 10148c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 10158c2ecf20Sopenharmony_ci "missing or invalid reset controller device tree entry\n"); 10168c2ecf20Sopenharmony_ci return PTR_ERR(bus->rst); 10178c2ecf20Sopenharmony_ci } 10188c2ecf20Sopenharmony_ci reset_control_deassert(bus->rst); 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci ret = of_property_read_u32(pdev->dev.of_node, 10218c2ecf20Sopenharmony_ci "bus-frequency", &bus->bus_frequency); 10228c2ecf20Sopenharmony_ci if (ret < 0) { 10238c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 10248c2ecf20Sopenharmony_ci "Could not read bus-frequency property\n"); 10258c2ecf20Sopenharmony_ci bus->bus_frequency = I2C_MAX_STANDARD_MODE_FREQ; 10268c2ecf20Sopenharmony_ci } 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci match = of_match_node(aspeed_i2c_bus_of_table, pdev->dev.of_node); 10298c2ecf20Sopenharmony_ci if (!match) 10308c2ecf20Sopenharmony_ci bus->get_clk_reg_val = aspeed_i2c_24xx_get_clk_reg_val; 10318c2ecf20Sopenharmony_ci else 10328c2ecf20Sopenharmony_ci bus->get_clk_reg_val = (u32 (*)(struct device *, u32)) 10338c2ecf20Sopenharmony_ci match->data; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci /* Initialize the I2C adapter */ 10368c2ecf20Sopenharmony_ci spin_lock_init(&bus->lock); 10378c2ecf20Sopenharmony_ci init_completion(&bus->cmd_complete); 10388c2ecf20Sopenharmony_ci bus->adap.owner = THIS_MODULE; 10398c2ecf20Sopenharmony_ci bus->adap.retries = 0; 10408c2ecf20Sopenharmony_ci bus->adap.algo = &aspeed_i2c_algo; 10418c2ecf20Sopenharmony_ci bus->adap.dev.parent = &pdev->dev; 10428c2ecf20Sopenharmony_ci bus->adap.dev.of_node = pdev->dev.of_node; 10438c2ecf20Sopenharmony_ci strlcpy(bus->adap.name, pdev->name, sizeof(bus->adap.name)); 10448c2ecf20Sopenharmony_ci i2c_set_adapdata(&bus->adap, bus); 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_ci bus->dev = &pdev->dev; 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci /* Clean up any left over interrupt state. */ 10498c2ecf20Sopenharmony_ci writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 10508c2ecf20Sopenharmony_ci writel(0xffffffff, bus->base + ASPEED_I2C_INTR_STS_REG); 10518c2ecf20Sopenharmony_ci /* 10528c2ecf20Sopenharmony_ci * bus.lock does not need to be held because the interrupt handler has 10538c2ecf20Sopenharmony_ci * not been enabled yet. 10548c2ecf20Sopenharmony_ci */ 10558c2ecf20Sopenharmony_ci ret = aspeed_i2c_init(bus, pdev); 10568c2ecf20Sopenharmony_ci if (ret < 0) 10578c2ecf20Sopenharmony_ci return ret; 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_ci irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 10608c2ecf20Sopenharmony_ci ret = devm_request_irq(&pdev->dev, irq, aspeed_i2c_bus_irq, 10618c2ecf20Sopenharmony_ci 0, dev_name(&pdev->dev), bus); 10628c2ecf20Sopenharmony_ci if (ret < 0) 10638c2ecf20Sopenharmony_ci return ret; 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci ret = i2c_add_adapter(&bus->adap); 10668c2ecf20Sopenharmony_ci if (ret < 0) 10678c2ecf20Sopenharmony_ci return ret; 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, bus); 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci dev_info(bus->dev, "i2c bus %d registered, irq %d\n", 10728c2ecf20Sopenharmony_ci bus->adap.nr, irq); 10738c2ecf20Sopenharmony_ci 10748c2ecf20Sopenharmony_ci return 0; 10758c2ecf20Sopenharmony_ci} 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_cistatic int aspeed_i2c_remove_bus(struct platform_device *pdev) 10788c2ecf20Sopenharmony_ci{ 10798c2ecf20Sopenharmony_ci struct aspeed_i2c_bus *bus = platform_get_drvdata(pdev); 10808c2ecf20Sopenharmony_ci unsigned long flags; 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci spin_lock_irqsave(&bus->lock, flags); 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci /* Disable everything. */ 10858c2ecf20Sopenharmony_ci writel(0, bus->base + ASPEED_I2C_FUN_CTRL_REG); 10868c2ecf20Sopenharmony_ci writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 10878c2ecf20Sopenharmony_ci 10888c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bus->lock, flags); 10898c2ecf20Sopenharmony_ci 10908c2ecf20Sopenharmony_ci reset_control_assert(bus->rst); 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_ci i2c_del_adapter(&bus->adap); 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_ci return 0; 10958c2ecf20Sopenharmony_ci} 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_cistatic struct platform_driver aspeed_i2c_bus_driver = { 10988c2ecf20Sopenharmony_ci .probe = aspeed_i2c_probe_bus, 10998c2ecf20Sopenharmony_ci .remove = aspeed_i2c_remove_bus, 11008c2ecf20Sopenharmony_ci .driver = { 11018c2ecf20Sopenharmony_ci .name = "aspeed-i2c-bus", 11028c2ecf20Sopenharmony_ci .of_match_table = aspeed_i2c_bus_of_table, 11038c2ecf20Sopenharmony_ci }, 11048c2ecf20Sopenharmony_ci}; 11058c2ecf20Sopenharmony_cimodule_platform_driver(aspeed_i2c_bus_driver); 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ciMODULE_AUTHOR("Brendan Higgins <brendanhiggins@google.com>"); 11088c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aspeed I2C Bus Driver"); 11098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1110