|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Phytium I2C adapter driver. |
| 4 | + * |
| 5 | + * Derived from Synopysys I2C driver. |
| 6 | + * Copyright (C) 2006 Texas Instruments. |
| 7 | + * Copyright (C) 2007 MontaVista Software Inc. |
| 8 | + * Copyright (C) 2009 Provigent Ltd. |
| 9 | + * |
| 10 | + * Copyright (C) 2021-2023, Phytium Technology Co., Ltd. |
| 11 | + */ |
| 12 | +#include <linux/clk.h> |
| 13 | +#include <linux/delay.h> |
| 14 | +#include <linux/export.h> |
| 15 | +#include <linux/errno.h> |
| 16 | +#include <linux/err.h> |
| 17 | +#include <linux/i2c.h> |
| 18 | +#include <linux/interrupt.h> |
| 19 | +#include <linux/io.h> |
| 20 | +#include <linux/module.h> |
| 21 | +#include <linux/pm_runtime.h> |
| 22 | +#include <linux/swab.h> |
| 23 | + |
| 24 | +#include "i2c-phytium-core.h" |
| 25 | + |
| 26 | +static char *abort_sources[] = { |
| 27 | + [ABRT_7B_ADDR_NOACK] = |
| 28 | + "slave address not acknowledged (7bit mode)", |
| 29 | + [ABRT_10ADDR1_NOACK] = |
| 30 | + "first address byte not acknowledged (10bit mode)", |
| 31 | + [ABRT_10ADDR2_NOACK] = |
| 32 | + "second address byte not acknowledged (10bit mode)", |
| 33 | + [ABRT_TXDATA_NOACK] = |
| 34 | + "data not acknowledged", |
| 35 | + [ABRT_GCALL_NOACK] = |
| 36 | + "no acknowledgment for a general call", |
| 37 | + [ABRT_GCALL_READ] = |
| 38 | + "read after general call", |
| 39 | + [ABRT_SBYTE_ACKDET] = |
| 40 | + "start byte acknowledged", |
| 41 | + [ABRT_SBYTE_NORSTRT] = |
| 42 | + "trying to send start byte when restart is disabled", |
| 43 | + [ABRT_10B_RD_NORSTRT] = |
| 44 | + "trying to read when restart is disabled (10bit mode)", |
| 45 | + [ABRT_MASTER_DIS] = |
| 46 | + "trying to use disabled adapter", |
| 47 | + [ARB_LOST] = |
| 48 | + "lost arbitration", |
| 49 | + [ABRT_SLAVE_FLUSH_TXFIFO] = |
| 50 | + "read command so flush old data in the TX FIFO", |
| 51 | + [ABRT_SLAVE_ARBLOST] = |
| 52 | + "slave lost the bus while transmitting data to a remote master", |
| 53 | + [ABRT_SLAVE_RD_INTX] = |
| 54 | + "incorrect slave-transmitter mode configuration", |
| 55 | +}; |
| 56 | + |
| 57 | +u32 phytium_readl(struct phytium_i2c_dev *dev, int offset) |
| 58 | +{ |
| 59 | + return readl_relaxed(dev->base + offset); |
| 60 | +} |
| 61 | + |
| 62 | +void phytium_writel(struct phytium_i2c_dev *dev, u32 b, int offset) |
| 63 | +{ |
| 64 | + writel_relaxed(b, dev->base + offset); |
| 65 | +} |
| 66 | + |
| 67 | +u32 i2c_phytium_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) |
| 68 | +{ |
| 69 | + if (cond) |
| 70 | + return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; |
| 71 | + else |
| 72 | + return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 - 3 + offset; |
| 73 | +} |
| 74 | + |
| 75 | +u32 i2c_phytium_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) |
| 76 | +{ |
| 77 | + return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; |
| 78 | +} |
| 79 | + |
| 80 | +int i2c_phytium_set_sda_hold(struct phytium_i2c_dev *dev) |
| 81 | +{ |
| 82 | + if (!dev->sda_hold_time) { |
| 83 | + /* Keep previous hold time setting if no one set it */ |
| 84 | + dev->sda_hold_time = phytium_readl(dev, IC_SDA_HOLD); |
| 85 | + } |
| 86 | + |
| 87 | + if (!(dev->sda_hold_time & IC_SDA_HOLD_RX_MASK)) |
| 88 | + dev->sda_hold_time |= 1 << IC_SDA_HOLD_RX_SHIFT; |
| 89 | + |
| 90 | + dev_dbg(dev->dev, "SDA Hold Time TX:RX = %d:%d\n", |
| 91 | + dev->sda_hold_time & ~(u32)IC_SDA_HOLD_RX_MASK, |
| 92 | + dev->sda_hold_time >> IC_SDA_HOLD_RX_SHIFT); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +void __i2c_phytium_disable(struct phytium_i2c_dev *dev) |
| 98 | +{ |
| 99 | + int timeout = 100; |
| 100 | + |
| 101 | + do { |
| 102 | + __i2c_phytium_disable_nowait(dev); |
| 103 | + if ((phytium_readl(dev, IC_ENABLE_STATUS) & 1) == 0) |
| 104 | + return; |
| 105 | + |
| 106 | + /* |
| 107 | + * Wait 10 times the signaling period of the highest I2C |
| 108 | + * transfer supported by the driver (for 400KHz this is |
| 109 | + * 25us). |
| 110 | + */ |
| 111 | + usleep_range(25, 250); |
| 112 | + } while (timeout--); |
| 113 | + |
| 114 | + dev_warn(dev->dev, "timeout in disabling adapter\n"); |
| 115 | +} |
| 116 | + |
| 117 | +unsigned long i2c_phytium_clk_rate(struct phytium_i2c_dev *dev) |
| 118 | +{ |
| 119 | + if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) |
| 120 | + return 0; |
| 121 | + return dev->get_clk_rate_khz(dev); |
| 122 | +} |
| 123 | + |
| 124 | +int i2c_phytium_prepare_clk(struct phytium_i2c_dev *dev, bool prepare) |
| 125 | +{ |
| 126 | + if (IS_ERR(dev->clk)) |
| 127 | + return PTR_ERR(dev->clk); |
| 128 | + |
| 129 | + if (prepare) |
| 130 | + return clk_prepare_enable(dev->clk); |
| 131 | + |
| 132 | + clk_disable_unprepare(dev->clk); |
| 133 | + return 0; |
| 134 | +} |
| 135 | +EXPORT_SYMBOL_GPL(i2c_phytium_prepare_clk); |
| 136 | + |
| 137 | +int i2c_phytium_wait_bus_not_busy(struct phytium_i2c_dev *dev) |
| 138 | +{ |
| 139 | + int timeout = 20; /* 20 ms */ |
| 140 | + |
| 141 | + while (phytium_readl(dev, IC_STATUS) & IC_STATUS_ACTIVITY) { |
| 142 | + if (timeout <= 0) { |
| 143 | + dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 144 | + i2c_recover_bus(&dev->adapter); |
| 145 | + |
| 146 | + if (phytium_readl(dev, IC_STATUS) & IC_STATUS_ACTIVITY) |
| 147 | + return -ETIMEDOUT; |
| 148 | + return 0; |
| 149 | + } |
| 150 | + timeout--; |
| 151 | + usleep_range(1000, 1100); |
| 152 | + } |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +int i2c_phytium_handle_tx_abort(struct phytium_i2c_dev *dev) |
| 158 | +{ |
| 159 | + unsigned long abort_source = dev->abort_source; |
| 160 | + int i; |
| 161 | + |
| 162 | + if (abort_source & IC_TX_ABRT_NOACK) { |
| 163 | + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 164 | + dev_dbg(dev->dev, |
| 165 | + "%s: %s\n", __func__, abort_sources[i]); |
| 166 | + return -EREMOTEIO; |
| 167 | + } |
| 168 | + |
| 169 | + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 170 | + dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); |
| 171 | + |
| 172 | + if (abort_source & IC_TX_ARB_LOST) |
| 173 | + return -EAGAIN; |
| 174 | + else if (abort_source & IC_TX_ABRT_GCALL_READ) |
| 175 | + return -EINVAL; |
| 176 | + else |
| 177 | + return -EIO; |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
| 182 | +u32 i2c_phytium_func(struct i2c_adapter *adapter) |
| 183 | +{ |
| 184 | + struct phytium_i2c_dev *dev = i2c_get_adapdata(adapter); |
| 185 | + |
| 186 | + return dev->functionality; |
| 187 | +} |
| 188 | + |
| 189 | +void i2c_phytium_disable(struct phytium_i2c_dev *dev) |
| 190 | +{ |
| 191 | + /* Disable controller */ |
| 192 | + __i2c_phytium_disable(dev); |
| 193 | + |
| 194 | + /* Disable all interrupts */ |
| 195 | + phytium_writel(dev, 0, IC_INTR_MASK); |
| 196 | + phytium_readl(dev, IC_CLR_INTR); |
| 197 | +} |
| 198 | + |
| 199 | +void i2c_phytium_disable_int(struct phytium_i2c_dev *dev) |
| 200 | +{ |
| 201 | + phytium_writel(dev, 0, IC_INTR_MASK); |
| 202 | +} |
| 203 | + |
| 204 | +MODULE_AUTHOR( "Cheng Quan <[email protected]>"); |
| 205 | +MODULE_DESCRIPTION("Phytium I2C bus adapter core"); |
| 206 | +MODULE_LICENSE("GPL"); |
0 commit comments