xref: /openbmc/linux/drivers/i2c/busses/i2c-npcm7xx.c (revision f4284724)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Nuvoton NPCM7xx I2C Controller driver
4  *
5  * Copyright (C) 2020 Nuvoton Technologies tali.perry@nuvoton.com
6  */
7 #include <linux/bitfield.h>
8 #include <linux/clk.h>
9 #include <linux/debugfs.h>
10 #include <linux/errno.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/iopoll.h>
14 #include <linux/irq.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 
23 enum i2c_mode {
24 	I2C_MASTER,
25 	I2C_SLAVE,
26 };
27 
28 /*
29  * External I2C Interface driver xfer indication values, which indicate status
30  * of the bus.
31  */
32 enum i2c_state_ind {
33 	I2C_NO_STATUS_IND = 0,
34 	I2C_SLAVE_RCV_IND,
35 	I2C_SLAVE_XMIT_IND,
36 	I2C_SLAVE_XMIT_MISSING_DATA_IND,
37 	I2C_SLAVE_RESTART_IND,
38 	I2C_SLAVE_DONE_IND,
39 	I2C_MASTER_DONE_IND,
40 	I2C_NACK_IND,
41 	I2C_BUS_ERR_IND,
42 	I2C_WAKE_UP_IND,
43 	I2C_BLOCK_BYTES_ERR_IND,
44 	I2C_SLAVE_RCV_MISSING_DATA_IND,
45 };
46 
47 /*
48  * Operation type values (used to define the operation currently running)
49  * module is interrupt driven, on each interrupt the current operation is
50  * checked to see if the module is currently reading or writing.
51  */
52 enum i2c_oper {
53 	I2C_NO_OPER = 0,
54 	I2C_WRITE_OPER,
55 	I2C_READ_OPER,
56 };
57 
58 /* I2C Bank (module had 2 banks of registers) */
59 enum i2c_bank {
60 	I2C_BANK_0 = 0,
61 	I2C_BANK_1,
62 };
63 
64 /* Internal I2C states values (for the I2C module state machine). */
65 enum i2c_state {
66 	I2C_DISABLE = 0,
67 	I2C_IDLE,
68 	I2C_MASTER_START,
69 	I2C_SLAVE_MATCH,
70 	I2C_OPER_STARTED,
71 	I2C_STOP_PENDING,
72 };
73 
74 #if IS_ENABLED(CONFIG_I2C_SLAVE)
75 /* Module supports setting multiple own slave addresses */
76 enum i2c_addr {
77 	I2C_SLAVE_ADDR1 = 0,
78 	I2C_SLAVE_ADDR2,
79 	I2C_SLAVE_ADDR3,
80 	I2C_SLAVE_ADDR4,
81 	I2C_SLAVE_ADDR5,
82 	I2C_SLAVE_ADDR6,
83 	I2C_SLAVE_ADDR7,
84 	I2C_SLAVE_ADDR8,
85 	I2C_SLAVE_ADDR9,
86 	I2C_SLAVE_ADDR10,
87 	I2C_GC_ADDR,
88 	I2C_ARP_ADDR,
89 };
90 #endif
91 
92 /* init register and default value required to enable module */
93 #define NPCM_I2CSEGCTL			0xE4
94 #define NPCM_I2CSEGCTL_INIT_VAL		0x0333F000
95 
96 /* Common regs */
97 #define NPCM_I2CSDA			0x00
98 #define NPCM_I2CST			0x02
99 #define NPCM_I2CCST			0x04
100 #define NPCM_I2CCTL1			0x06
101 #define NPCM_I2CADDR1			0x08
102 #define NPCM_I2CCTL2			0x0A
103 #define NPCM_I2CADDR2			0x0C
104 #define NPCM_I2CCTL3			0x0E
105 #define NPCM_I2CCST2			0x18
106 #define NPCM_I2CCST3			0x19
107 #define I2C_VER				0x1F
108 
109 /*BANK0 regs*/
110 #define NPCM_I2CADDR3			0x10
111 #define NPCM_I2CADDR7			0x11
112 #define NPCM_I2CADDR4			0x12
113 #define NPCM_I2CADDR8			0x13
114 #define NPCM_I2CADDR5			0x14
115 #define NPCM_I2CADDR9			0x15
116 #define NPCM_I2CADDR6			0x16
117 #define NPCM_I2CADDR10			0x17
118 
119 #if IS_ENABLED(CONFIG_I2C_SLAVE)
120 /*
121  * npcm_i2caddr array:
122  * The module supports having multiple own slave addresses.
123  * Since the addr regs are sprinkled all over the address space,
124  * use this array to get the address or each register.
125  */
126 #define I2C_NUM_OWN_ADDR 10
127 static const int npcm_i2caddr[I2C_NUM_OWN_ADDR] = {
128 	NPCM_I2CADDR1, NPCM_I2CADDR2, NPCM_I2CADDR3, NPCM_I2CADDR4,
129 	NPCM_I2CADDR5, NPCM_I2CADDR6, NPCM_I2CADDR7, NPCM_I2CADDR8,
130 	NPCM_I2CADDR9, NPCM_I2CADDR10,
131 };
132 #endif
133 
134 #define NPCM_I2CCTL4			0x1A
135 #define NPCM_I2CCTL5			0x1B
136 #define NPCM_I2CSCLLT			0x1C /* SCL Low Time */
137 #define NPCM_I2CFIF_CTL			0x1D /* FIFO Control */
138 #define NPCM_I2CSCLHT			0x1E /* SCL High Time */
139 
140 /* BANK 1 regs */
141 #define NPCM_I2CFIF_CTS			0x10 /* Both FIFOs Control and Status */
142 #define NPCM_I2CTXF_CTL			0x12 /* Tx-FIFO Control */
143 #define NPCM_I2CT_OUT			0x14 /* Bus T.O. */
144 #define NPCM_I2CPEC			0x16 /* PEC Data */
145 #define NPCM_I2CTXF_STS			0x1A /* Tx-FIFO Status */
146 #define NPCM_I2CRXF_STS			0x1C /* Rx-FIFO Status */
147 #define NPCM_I2CRXF_CTL			0x1E /* Rx-FIFO Control */
148 
149 /* NPCM_I2CST reg fields */
150 #define NPCM_I2CST_XMIT			BIT(0)
151 #define NPCM_I2CST_MASTER		BIT(1)
152 #define NPCM_I2CST_NMATCH		BIT(2)
153 #define NPCM_I2CST_STASTR		BIT(3)
154 #define NPCM_I2CST_NEGACK		BIT(4)
155 #define NPCM_I2CST_BER			BIT(5)
156 #define NPCM_I2CST_SDAST		BIT(6)
157 #define NPCM_I2CST_SLVSTP		BIT(7)
158 
159 /* NPCM_I2CCST reg fields */
160 #define NPCM_I2CCST_BUSY		BIT(0)
161 #define NPCM_I2CCST_BB			BIT(1)
162 #define NPCM_I2CCST_MATCH		BIT(2)
163 #define NPCM_I2CCST_GCMATCH		BIT(3)
164 #define NPCM_I2CCST_TSDA		BIT(4)
165 #define NPCM_I2CCST_TGSCL		BIT(5)
166 #define NPCM_I2CCST_MATCHAF		BIT(6)
167 #define NPCM_I2CCST_ARPMATCH		BIT(7)
168 
169 /* NPCM_I2CCTL1 reg fields */
170 #define NPCM_I2CCTL1_START		BIT(0)
171 #define NPCM_I2CCTL1_STOP		BIT(1)
172 #define NPCM_I2CCTL1_INTEN		BIT(2)
173 #define NPCM_I2CCTL1_EOBINTE		BIT(3)
174 #define NPCM_I2CCTL1_ACK		BIT(4)
175 #define NPCM_I2CCTL1_GCMEN		BIT(5)
176 #define NPCM_I2CCTL1_NMINTE		BIT(6)
177 #define NPCM_I2CCTL1_STASTRE		BIT(7)
178 
179 /* RW1S fields (inside a RW reg): */
180 #define NPCM_I2CCTL1_RWS   \
181 	(NPCM_I2CCTL1_START | NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_ACK)
182 
183 /* npcm_i2caddr reg fields */
184 #define NPCM_I2CADDR_A			GENMASK(6, 0)
185 #define NPCM_I2CADDR_SAEN		BIT(7)
186 
187 /* NPCM_I2CCTL2 reg fields */
188 #define I2CCTL2_ENABLE			BIT(0)
189 #define I2CCTL2_SCLFRQ6_0		GENMASK(7, 1)
190 
191 /* NPCM_I2CCTL3 reg fields */
192 #define I2CCTL3_SCLFRQ8_7		GENMASK(1, 0)
193 #define I2CCTL3_ARPMEN			BIT(2)
194 #define I2CCTL3_IDL_START		BIT(3)
195 #define I2CCTL3_400K_MODE		BIT(4)
196 #define I2CCTL3_BNK_SEL			BIT(5)
197 #define I2CCTL3_SDA_LVL			BIT(6)
198 #define I2CCTL3_SCL_LVL			BIT(7)
199 
200 /* NPCM_I2CCST2 reg fields */
201 #define NPCM_I2CCST2_MATCHA1F		BIT(0)
202 #define NPCM_I2CCST2_MATCHA2F		BIT(1)
203 #define NPCM_I2CCST2_MATCHA3F		BIT(2)
204 #define NPCM_I2CCST2_MATCHA4F		BIT(3)
205 #define NPCM_I2CCST2_MATCHA5F		BIT(4)
206 #define NPCM_I2CCST2_MATCHA6F		BIT(5)
207 #define NPCM_I2CCST2_MATCHA7F		BIT(5)
208 #define NPCM_I2CCST2_INTSTS		BIT(7)
209 
210 /* NPCM_I2CCST3 reg fields */
211 #define NPCM_I2CCST3_MATCHA8F		BIT(0)
212 #define NPCM_I2CCST3_MATCHA9F		BIT(1)
213 #define NPCM_I2CCST3_MATCHA10F		BIT(2)
214 #define NPCM_I2CCST3_EO_BUSY		BIT(7)
215 
216 /* NPCM_I2CCTL4 reg fields */
217 #define I2CCTL4_HLDT			GENMASK(5, 0)
218 #define I2CCTL4_LVL_WE			BIT(7)
219 
220 /* NPCM_I2CCTL5 reg fields */
221 #define I2CCTL5_DBNCT			GENMASK(3, 0)
222 
223 /* NPCM_I2CFIF_CTS reg fields */
224 #define NPCM_I2CFIF_CTS_RXF_TXE		BIT(1)
225 #define NPCM_I2CFIF_CTS_RFTE_IE		BIT(3)
226 #define NPCM_I2CFIF_CTS_CLR_FIFO	BIT(6)
227 #define NPCM_I2CFIF_CTS_SLVRSTR		BIT(7)
228 
229 /* NPCM_I2CTXF_CTL reg fields */
230 #define NPCM_I2CTXF_CTL_TX_THR		GENMASK(4, 0)
231 #define NPCM_I2CTXF_CTL_THR_TXIE	BIT(6)
232 
233 /* NPCM_I2CT_OUT reg fields */
234 #define NPCM_I2CT_OUT_TO_CKDIV		GENMASK(5, 0)
235 #define NPCM_I2CT_OUT_T_OUTIE		BIT(6)
236 #define NPCM_I2CT_OUT_T_OUTST		BIT(7)
237 
238 /* NPCM_I2CTXF_STS reg fields */
239 #define NPCM_I2CTXF_STS_TX_BYTES	GENMASK(4, 0)
240 #define NPCM_I2CTXF_STS_TX_THST		BIT(6)
241 
242 /* NPCM_I2CRXF_STS reg fields */
243 #define NPCM_I2CRXF_STS_RX_BYTES	GENMASK(4, 0)
244 #define NPCM_I2CRXF_STS_RX_THST		BIT(6)
245 
246 /* NPCM_I2CFIF_CTL reg fields */
247 #define NPCM_I2CFIF_CTL_FIFO_EN		BIT(4)
248 
249 /* NPCM_I2CRXF_CTL reg fields */
250 #define NPCM_I2CRXF_CTL_RX_THR		GENMASK(4, 0)
251 #define NPCM_I2CRXF_CTL_LAST_PEC	BIT(5)
252 #define NPCM_I2CRXF_CTL_THR_RXIE	BIT(6)
253 
254 #define I2C_HW_FIFO_SIZE		16
255 
256 /* I2C_VER reg fields */
257 #define I2C_VER_VERSION			GENMASK(6, 0)
258 #define I2C_VER_FIFO_EN			BIT(7)
259 
260 /* stall/stuck timeout in us */
261 #define DEFAULT_STALL_COUNT		25
262 
263 /* SCLFRQ field position */
264 #define SCLFRQ_0_TO_6			GENMASK(6, 0)
265 #define SCLFRQ_7_TO_8			GENMASK(8, 7)
266 
267 /* supported clk settings. values in Hz. */
268 #define I2C_FREQ_MIN_HZ			10000
269 #define I2C_FREQ_MAX_HZ			I2C_MAX_FAST_MODE_PLUS_FREQ
270 
271 /* Status of one I2C module */
272 struct npcm_i2c {
273 	struct i2c_adapter adap;
274 	struct device *dev;
275 	unsigned char __iomem *reg;
276 	spinlock_t lock;   /* IRQ synchronization */
277 	struct completion cmd_complete;
278 	int cmd_err;
279 	struct i2c_msg *msgs;
280 	int msgs_num;
281 	int num;
282 	u32 apb_clk;
283 	struct i2c_bus_recovery_info rinfo;
284 	enum i2c_state state;
285 	enum i2c_oper operation;
286 	enum i2c_mode master_or_slave;
287 	enum i2c_state_ind stop_ind;
288 	u8 dest_addr;
289 	u8 *rd_buf;
290 	u16 rd_size;
291 	u16 rd_ind;
292 	u8 *wr_buf;
293 	u16 wr_size;
294 	u16 wr_ind;
295 	bool fifo_use;
296 	u16 PEC_mask; /* PEC bit mask per slave address */
297 	bool PEC_use;
298 	bool read_block_use;
299 	unsigned long int_time_stamp;
300 	unsigned long bus_freq; /* in Hz */
301 #if IS_ENABLED(CONFIG_I2C_SLAVE)
302 	u8 own_slave_addr;
303 	struct i2c_client *slave;
304 	int slv_rd_size;
305 	int slv_rd_ind;
306 	int slv_wr_size;
307 	int slv_wr_ind;
308 	u8 slv_rd_buf[I2C_HW_FIFO_SIZE];
309 	u8 slv_wr_buf[I2C_HW_FIFO_SIZE];
310 #endif
311 	struct dentry *debugfs; /* debugfs device directory */
312 	u64 ber_cnt;
313 	u64 rec_succ_cnt;
314 	u64 rec_fail_cnt;
315 	u64 nack_cnt;
316 	u64 timeout_cnt;
317 	u64 tx_complete_cnt;
318 };
319 
320 static inline void npcm_i2c_select_bank(struct npcm_i2c *bus,
321 					enum i2c_bank bank)
322 {
323 	u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3);
324 
325 	if (bank == I2C_BANK_0)
326 		i2cctl3 = i2cctl3 & ~I2CCTL3_BNK_SEL;
327 	else
328 		i2cctl3 = i2cctl3 | I2CCTL3_BNK_SEL;
329 	iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3);
330 }
331 
332 static void npcm_i2c_init_params(struct npcm_i2c *bus)
333 {
334 	bus->stop_ind = I2C_NO_STATUS_IND;
335 	bus->rd_size = 0;
336 	bus->wr_size = 0;
337 	bus->rd_ind = 0;
338 	bus->wr_ind = 0;
339 	bus->read_block_use = false;
340 	bus->int_time_stamp = 0;
341 	bus->PEC_use = false;
342 	bus->PEC_mask = 0;
343 #if IS_ENABLED(CONFIG_I2C_SLAVE)
344 	if (bus->slave)
345 		bus->master_or_slave = I2C_SLAVE;
346 #endif
347 }
348 
349 static inline void npcm_i2c_wr_byte(struct npcm_i2c *bus, u8 data)
350 {
351 	iowrite8(data, bus->reg + NPCM_I2CSDA);
352 }
353 
354 static inline u8 npcm_i2c_rd_byte(struct npcm_i2c *bus)
355 {
356 	return ioread8(bus->reg + NPCM_I2CSDA);
357 }
358 
359 static int npcm_i2c_get_SCL(struct i2c_adapter *_adap)
360 {
361 	struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
362 
363 	return !!(I2CCTL3_SCL_LVL & ioread8(bus->reg + NPCM_I2CCTL3));
364 }
365 
366 static int npcm_i2c_get_SDA(struct i2c_adapter *_adap)
367 {
368 	struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
369 
370 	return !!(I2CCTL3_SDA_LVL & ioread8(bus->reg + NPCM_I2CCTL3));
371 }
372 
373 static inline u16 npcm_i2c_get_index(struct npcm_i2c *bus)
374 {
375 	if (bus->operation == I2C_READ_OPER)
376 		return bus->rd_ind;
377 	if (bus->operation == I2C_WRITE_OPER)
378 		return bus->wr_ind;
379 	return 0;
380 }
381 
382 /* quick protocol (just address) */
383 static inline bool npcm_i2c_is_quick(struct npcm_i2c *bus)
384 {
385 	return bus->wr_size == 0 && bus->rd_size == 0;
386 }
387 
388 static void npcm_i2c_disable(struct npcm_i2c *bus)
389 {
390 	u8 i2cctl2;
391 
392 #if IS_ENABLED(CONFIG_I2C_SLAVE)
393 	int i;
394 
395 	/* select bank 0 for I2C addresses */
396 	npcm_i2c_select_bank(bus, I2C_BANK_0);
397 
398 	/* Slave addresses removal */
399 	for (i = I2C_SLAVE_ADDR1; i < I2C_NUM_OWN_ADDR; i++)
400 		iowrite8(0, bus->reg + npcm_i2caddr[i]);
401 
402 	npcm_i2c_select_bank(bus, I2C_BANK_1);
403 #endif
404 	/* Disable module */
405 	i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2);
406 	i2cctl2 = i2cctl2 & ~I2CCTL2_ENABLE;
407 	iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2);
408 
409 	bus->state = I2C_DISABLE;
410 }
411 
412 static void npcm_i2c_enable(struct npcm_i2c *bus)
413 {
414 	u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2);
415 
416 	i2cctl2 = i2cctl2 | I2CCTL2_ENABLE;
417 	iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2);
418 	bus->state = I2C_IDLE;
419 }
420 
421 /* enable\disable end of busy (EOB) interrupts */
422 static inline void npcm_i2c_eob_int(struct npcm_i2c *bus, bool enable)
423 {
424 	u8 val;
425 
426 	/* Clear EO_BUSY pending bit: */
427 	val = ioread8(bus->reg + NPCM_I2CCST3);
428 	val = val | NPCM_I2CCST3_EO_BUSY;
429 	iowrite8(val, bus->reg + NPCM_I2CCST3);
430 
431 	val = ioread8(bus->reg + NPCM_I2CCTL1);
432 	val &= ~NPCM_I2CCTL1_RWS;
433 	if (enable)
434 		val |= NPCM_I2CCTL1_EOBINTE;
435 	else
436 		val &= ~NPCM_I2CCTL1_EOBINTE;
437 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
438 }
439 
440 static inline bool npcm_i2c_tx_fifo_empty(struct npcm_i2c *bus)
441 {
442 	u8 tx_fifo_sts;
443 
444 	tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS);
445 	/* check if TX FIFO is not empty */
446 	if ((tx_fifo_sts & NPCM_I2CTXF_STS_TX_BYTES) == 0)
447 		return false;
448 
449 	/* check if TX FIFO status bit is set: */
450 	return !!FIELD_GET(NPCM_I2CTXF_STS_TX_THST, tx_fifo_sts);
451 }
452 
453 static inline bool npcm_i2c_rx_fifo_full(struct npcm_i2c *bus)
454 {
455 	u8 rx_fifo_sts;
456 
457 	rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS);
458 	/* check if RX FIFO is not empty: */
459 	if ((rx_fifo_sts & NPCM_I2CRXF_STS_RX_BYTES) == 0)
460 		return false;
461 
462 	/* check if rx fifo full status is set: */
463 	return !!FIELD_GET(NPCM_I2CRXF_STS_RX_THST, rx_fifo_sts);
464 }
465 
466 static inline void npcm_i2c_clear_fifo_int(struct npcm_i2c *bus)
467 {
468 	u8 val;
469 
470 	val = ioread8(bus->reg + NPCM_I2CFIF_CTS);
471 	val = (val & NPCM_I2CFIF_CTS_SLVRSTR) | NPCM_I2CFIF_CTS_RXF_TXE;
472 	iowrite8(val, bus->reg + NPCM_I2CFIF_CTS);
473 }
474 
475 static inline void npcm_i2c_clear_tx_fifo(struct npcm_i2c *bus)
476 {
477 	u8 val;
478 
479 	val = ioread8(bus->reg + NPCM_I2CTXF_STS);
480 	val = val | NPCM_I2CTXF_STS_TX_THST;
481 	iowrite8(val, bus->reg + NPCM_I2CTXF_STS);
482 }
483 
484 static inline void npcm_i2c_clear_rx_fifo(struct npcm_i2c *bus)
485 {
486 	u8 val;
487 
488 	val = ioread8(bus->reg + NPCM_I2CRXF_STS);
489 	val = val | NPCM_I2CRXF_STS_RX_THST;
490 	iowrite8(val, bus->reg + NPCM_I2CRXF_STS);
491 }
492 
493 static void npcm_i2c_int_enable(struct npcm_i2c *bus, bool enable)
494 {
495 	u8 val;
496 
497 	val = ioread8(bus->reg + NPCM_I2CCTL1);
498 	val &= ~NPCM_I2CCTL1_RWS;
499 	if (enable)
500 		val |= NPCM_I2CCTL1_INTEN;
501 	else
502 		val &= ~NPCM_I2CCTL1_INTEN;
503 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
504 }
505 
506 static inline void npcm_i2c_master_start(struct npcm_i2c *bus)
507 {
508 	u8 val;
509 
510 	val = ioread8(bus->reg + NPCM_I2CCTL1);
511 	val &= ~(NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_ACK);
512 	val |= NPCM_I2CCTL1_START;
513 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
514 }
515 
516 static inline void npcm_i2c_master_stop(struct npcm_i2c *bus)
517 {
518 	u8 val;
519 
520 	/*
521 	 * override HW issue: I2C may fail to supply stop condition in Master
522 	 * Write operation.
523 	 * Need to delay at least 5 us from the last int, before issueing a stop
524 	 */
525 	udelay(10); /* function called from interrupt, can't sleep */
526 	val = ioread8(bus->reg + NPCM_I2CCTL1);
527 	val &= ~(NPCM_I2CCTL1_START | NPCM_I2CCTL1_ACK);
528 	val |= NPCM_I2CCTL1_STOP;
529 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
530 
531 	if (!bus->fifo_use)
532 		return;
533 
534 	npcm_i2c_select_bank(bus, I2C_BANK_1);
535 
536 	if (bus->operation == I2C_READ_OPER)
537 		npcm_i2c_clear_rx_fifo(bus);
538 	else
539 		npcm_i2c_clear_tx_fifo(bus);
540 	npcm_i2c_clear_fifo_int(bus);
541 	iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
542 }
543 
544 static inline void npcm_i2c_stall_after_start(struct npcm_i2c *bus, bool stall)
545 {
546 	u8 val;
547 
548 	val = ioread8(bus->reg + NPCM_I2CCTL1);
549 	val &= ~NPCM_I2CCTL1_RWS;
550 	if (stall)
551 		val |= NPCM_I2CCTL1_STASTRE;
552 	else
553 		val &= ~NPCM_I2CCTL1_STASTRE;
554 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
555 }
556 
557 static inline void npcm_i2c_nack(struct npcm_i2c *bus)
558 {
559 	u8 val;
560 
561 	val = ioread8(bus->reg + NPCM_I2CCTL1);
562 	val &= ~(NPCM_I2CCTL1_STOP | NPCM_I2CCTL1_START);
563 	val |= NPCM_I2CCTL1_ACK;
564 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
565 }
566 
567 static inline void npcm_i2c_clear_master_status(struct npcm_i2c *bus)
568 {
569 	u8 val;
570 
571 	/* Clear NEGACK, STASTR and BER bits */
572 	val = NPCM_I2CST_BER | NPCM_I2CST_NEGACK | NPCM_I2CST_STASTR;
573 	iowrite8(val, bus->reg + NPCM_I2CST);
574 }
575 
576 #if IS_ENABLED(CONFIG_I2C_SLAVE)
577 static void npcm_i2c_slave_int_enable(struct npcm_i2c *bus, bool enable)
578 {
579 	u8 i2cctl1;
580 
581 	/* enable interrupt on slave match: */
582 	i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
583 	i2cctl1 &= ~NPCM_I2CCTL1_RWS;
584 	if (enable)
585 		i2cctl1 |= NPCM_I2CCTL1_NMINTE;
586 	else
587 		i2cctl1 &= ~NPCM_I2CCTL1_NMINTE;
588 	iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
589 }
590 
591 static int npcm_i2c_slave_enable(struct npcm_i2c *bus, enum i2c_addr addr_type,
592 				 u8 addr, bool enable)
593 {
594 	u8 i2cctl1;
595 	u8 i2cctl3;
596 	u8 sa_reg;
597 
598 	sa_reg = (addr & 0x7F) | FIELD_PREP(NPCM_I2CADDR_SAEN, enable);
599 	if (addr_type == I2C_GC_ADDR) {
600 		i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
601 		if (enable)
602 			i2cctl1 |= NPCM_I2CCTL1_GCMEN;
603 		else
604 			i2cctl1 &= ~NPCM_I2CCTL1_GCMEN;
605 		iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
606 		return 0;
607 	}
608 	if (addr_type == I2C_ARP_ADDR) {
609 		i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3);
610 		if (enable)
611 			i2cctl3 |= I2CCTL3_ARPMEN;
612 		else
613 			i2cctl3 &= ~I2CCTL3_ARPMEN;
614 		iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3);
615 		return 0;
616 	}
617 	if (addr_type >= I2C_ARP_ADDR)
618 		return -EFAULT;
619 	/* select bank 0 for address 3 to 10 */
620 	if (addr_type > I2C_SLAVE_ADDR2)
621 		npcm_i2c_select_bank(bus, I2C_BANK_0);
622 	/* Set and enable the address */
623 	iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]);
624 	npcm_i2c_slave_int_enable(bus, enable);
625 	if (addr_type > I2C_SLAVE_ADDR2)
626 		npcm_i2c_select_bank(bus, I2C_BANK_1);
627 	return 0;
628 }
629 #endif
630 
631 static void npcm_i2c_reset(struct npcm_i2c *bus)
632 {
633 	/*
634 	 * Save I2CCTL1 relevant bits. It is being cleared when the module
635 	 *  is disabled.
636 	 */
637 	u8 i2cctl1;
638 #if IS_ENABLED(CONFIG_I2C_SLAVE)
639 	u8 addr;
640 #endif
641 
642 	i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1);
643 
644 	npcm_i2c_disable(bus);
645 	npcm_i2c_enable(bus);
646 
647 	/* Restore NPCM_I2CCTL1 Status */
648 	i2cctl1 &= ~NPCM_I2CCTL1_RWS;
649 	iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1);
650 
651 	/* Clear BB (BUS BUSY) bit */
652 	iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
653 	iowrite8(0xFF, bus->reg + NPCM_I2CST);
654 
655 	/* Clear and disable EOB */
656 	npcm_i2c_eob_int(bus, false);
657 
658 	/* Clear all fifo bits: */
659 	iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
660 
661 #if IS_ENABLED(CONFIG_I2C_SLAVE)
662 	if (bus->slave) {
663 		addr = bus->slave->addr;
664 		npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, addr, true);
665 	}
666 #endif
667 
668 	/* clear status bits for spurious interrupts */
669 	npcm_i2c_clear_master_status(bus);
670 
671 	bus->state = I2C_IDLE;
672 }
673 
674 static inline bool npcm_i2c_is_master(struct npcm_i2c *bus)
675 {
676 	return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST));
677 }
678 
679 static void npcm_i2c_callback(struct npcm_i2c *bus,
680 			      enum i2c_state_ind op_status, u16 info)
681 {
682 	struct i2c_msg *msgs;
683 	int msgs_num;
684 
685 	msgs = bus->msgs;
686 	msgs_num = bus->msgs_num;
687 	/*
688 	 * check that transaction was not timed-out, and msgs still
689 	 * holds a valid value.
690 	 */
691 	if (!msgs)
692 		return;
693 
694 	if (completion_done(&bus->cmd_complete))
695 		return;
696 
697 	switch (op_status) {
698 	case I2C_MASTER_DONE_IND:
699 		bus->cmd_err = bus->msgs_num;
700 		if (bus->tx_complete_cnt < ULLONG_MAX)
701 			bus->tx_complete_cnt++;
702 		fallthrough;
703 	case I2C_BLOCK_BYTES_ERR_IND:
704 		/* Master tx finished and all transmit bytes were sent */
705 		if (bus->msgs) {
706 			if (msgs[0].flags & I2C_M_RD)
707 				msgs[0].len = info;
708 			else if (msgs_num == 2 &&
709 				 msgs[1].flags & I2C_M_RD)
710 				msgs[1].len = info;
711 		}
712 		if (completion_done(&bus->cmd_complete) == false)
713 			complete(&bus->cmd_complete);
714 	break;
715 
716 	case I2C_NACK_IND:
717 		/* MASTER transmit got a NACK before tx all bytes */
718 		bus->cmd_err = -ENXIO;
719 		if (bus->master_or_slave == I2C_MASTER)
720 			complete(&bus->cmd_complete);
721 
722 		break;
723 	case I2C_BUS_ERR_IND:
724 		/* Bus error */
725 		bus->cmd_err = -EAGAIN;
726 		if (bus->master_or_slave == I2C_MASTER)
727 			complete(&bus->cmd_complete);
728 
729 		break;
730 	case I2C_WAKE_UP_IND:
731 		/* I2C wake up */
732 		break;
733 	default:
734 		break;
735 	}
736 
737 	bus->operation = I2C_NO_OPER;
738 #if IS_ENABLED(CONFIG_I2C_SLAVE)
739 	if (bus->slave)
740 		bus->master_or_slave = I2C_SLAVE;
741 #endif
742 }
743 
744 static u8 npcm_i2c_fifo_usage(struct npcm_i2c *bus)
745 {
746 	if (bus->operation == I2C_WRITE_OPER)
747 		return FIELD_GET(NPCM_I2CTXF_STS_TX_BYTES,
748 				 ioread8(bus->reg + NPCM_I2CTXF_STS));
749 	if (bus->operation == I2C_READ_OPER)
750 		return FIELD_GET(NPCM_I2CRXF_STS_RX_BYTES,
751 				 ioread8(bus->reg + NPCM_I2CRXF_STS));
752 	return 0;
753 }
754 
755 static void npcm_i2c_write_to_fifo_master(struct npcm_i2c *bus, u16 max_bytes)
756 {
757 	u8 size_free_fifo;
758 
759 	/*
760 	 * Fill the FIFO, while the FIFO is not full and there are more bytes
761 	 * to write
762 	 */
763 	size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus);
764 	while (max_bytes-- && size_free_fifo) {
765 		if (bus->wr_ind < bus->wr_size)
766 			npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]);
767 		else
768 			npcm_i2c_wr_byte(bus, 0xFF);
769 		size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus);
770 	}
771 }
772 
773 /*
774  * npcm_i2c_set_fifo:
775  * configure the FIFO before using it. If nread is -1 RX FIFO will not be
776  * configured. same for nwrite
777  */
778 static void npcm_i2c_set_fifo(struct npcm_i2c *bus, int nread, int nwrite)
779 {
780 	u8 rxf_ctl = 0;
781 
782 	if (!bus->fifo_use)
783 		return;
784 	npcm_i2c_select_bank(bus, I2C_BANK_1);
785 	npcm_i2c_clear_tx_fifo(bus);
786 	npcm_i2c_clear_rx_fifo(bus);
787 
788 	/* configure RX FIFO */
789 	if (nread > 0) {
790 		rxf_ctl = min_t(int, nread, I2C_HW_FIFO_SIZE);
791 
792 		/* set LAST bit. if LAST is set next FIFO packet is nacked */
793 		if (nread <= I2C_HW_FIFO_SIZE)
794 			rxf_ctl |= NPCM_I2CRXF_CTL_LAST_PEC;
795 
796 		/*
797 		 * if we are about to read the first byte in blk rd mode,
798 		 * don't NACK it. If slave returns zero size HW can't NACK
799 		 * it immediately, it will read extra byte and then NACK.
800 		 */
801 		if (bus->rd_ind == 0 && bus->read_block_use) {
802 			/* set fifo to read one byte, no last: */
803 			rxf_ctl = 1;
804 		}
805 
806 		/* set fifo size: */
807 		iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL);
808 	}
809 
810 	/* configure TX FIFO */
811 	if (nwrite > 0) {
812 		if (nwrite > I2C_HW_FIFO_SIZE)
813 			/* data to send is more then FIFO size. */
814 			iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CTXF_CTL);
815 		else
816 			iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL);
817 
818 		npcm_i2c_clear_tx_fifo(bus);
819 	}
820 }
821 
822 static void npcm_i2c_read_fifo(struct npcm_i2c *bus, u8 bytes_in_fifo)
823 {
824 	u8 data;
825 
826 	while (bytes_in_fifo--) {
827 		data = npcm_i2c_rd_byte(bus);
828 		if (bus->rd_ind < bus->rd_size)
829 			bus->rd_buf[bus->rd_ind++] = data;
830 	}
831 }
832 
833 static void npcm_i2c_master_abort(struct npcm_i2c *bus)
834 {
835 	/* Only current master is allowed to issue a stop condition */
836 	if (!npcm_i2c_is_master(bus))
837 		return;
838 
839 	npcm_i2c_eob_int(bus, true);
840 	npcm_i2c_master_stop(bus);
841 	npcm_i2c_clear_master_status(bus);
842 }
843 
844 #if IS_ENABLED(CONFIG_I2C_SLAVE)
845 static u8 npcm_i2c_get_slave_addr(struct npcm_i2c *bus, enum i2c_addr addr_type)
846 {
847 	u8 slave_add;
848 
849 	/* select bank 0 for address 3 to 10 */
850 	if (addr_type > I2C_SLAVE_ADDR2)
851 		npcm_i2c_select_bank(bus, I2C_BANK_0);
852 
853 	slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]);
854 
855 	if (addr_type > I2C_SLAVE_ADDR2)
856 		npcm_i2c_select_bank(bus, I2C_BANK_1);
857 
858 	return slave_add;
859 }
860 
861 static int npcm_i2c_remove_slave_addr(struct npcm_i2c *bus, u8 slave_add)
862 {
863 	int i;
864 
865 	/* Set the enable bit */
866 	slave_add |= 0x80;
867 	npcm_i2c_select_bank(bus, I2C_BANK_0);
868 	for (i = I2C_SLAVE_ADDR1; i < I2C_NUM_OWN_ADDR; i++) {
869 		if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add)
870 			iowrite8(0, bus->reg + npcm_i2caddr[i]);
871 	}
872 	npcm_i2c_select_bank(bus, I2C_BANK_1);
873 	return 0;
874 }
875 
876 static void npcm_i2c_write_fifo_slave(struct npcm_i2c *bus, u16 max_bytes)
877 {
878 	/*
879 	 * Fill the FIFO, while the FIFO is not full and there are more bytes
880 	 * to write
881 	 */
882 	npcm_i2c_clear_fifo_int(bus);
883 	npcm_i2c_clear_tx_fifo(bus);
884 	iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
885 	while (max_bytes-- && I2C_HW_FIFO_SIZE != npcm_i2c_fifo_usage(bus)) {
886 		if (bus->slv_wr_size <= 0)
887 			break;
888 		bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE;
889 		npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]);
890 		bus->slv_wr_ind++;
891 		bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE;
892 		bus->slv_wr_size--;
893 	}
894 }
895 
896 static void npcm_i2c_read_fifo_slave(struct npcm_i2c *bus, u8 bytes_in_fifo)
897 {
898 	u8 data;
899 
900 	if (!bus->slave)
901 		return;
902 
903 	while (bytes_in_fifo--) {
904 		data = npcm_i2c_rd_byte(bus);
905 
906 		bus->slv_rd_ind = bus->slv_rd_ind % I2C_HW_FIFO_SIZE;
907 		bus->slv_rd_buf[bus->slv_rd_ind] = data;
908 		bus->slv_rd_ind++;
909 
910 		/* 1st byte is length in block protocol: */
911 		if (bus->slv_rd_ind == 1 && bus->read_block_use)
912 			bus->slv_rd_size = data + bus->PEC_use + 1;
913 	}
914 }
915 
916 static int npcm_i2c_slave_get_wr_buf(struct npcm_i2c *bus)
917 {
918 	int i;
919 	u8 value;
920 	int ind;
921 	int ret = bus->slv_wr_ind;
922 
923 	/* fill a cyclic buffer */
924 	for (i = 0; i < I2C_HW_FIFO_SIZE; i++) {
925 		if (bus->slv_wr_size >= I2C_HW_FIFO_SIZE)
926 			break;
927 		i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value);
928 		ind = (bus->slv_wr_ind + bus->slv_wr_size) % I2C_HW_FIFO_SIZE;
929 		bus->slv_wr_buf[ind] = value;
930 		bus->slv_wr_size++;
931 		i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value);
932 	}
933 	return I2C_HW_FIFO_SIZE - ret;
934 }
935 
936 static void npcm_i2c_slave_send_rd_buf(struct npcm_i2c *bus)
937 {
938 	int i;
939 
940 	for (i = 0; i < bus->slv_rd_ind; i++)
941 		i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED,
942 				&bus->slv_rd_buf[i]);
943 	/*
944 	 * once we send bytes up, need to reset the counter of the wr buf
945 	 * got data from master (new offset in device), ignore wr fifo:
946 	 */
947 	if (bus->slv_rd_ind) {
948 		bus->slv_wr_size = 0;
949 		bus->slv_wr_ind = 0;
950 	}
951 
952 	bus->slv_rd_ind = 0;
953 	bus->slv_rd_size = bus->adap.quirks->max_read_len;
954 
955 	npcm_i2c_clear_fifo_int(bus);
956 	npcm_i2c_clear_rx_fifo(bus);
957 }
958 
959 static void npcm_i2c_slave_receive(struct npcm_i2c *bus, u16 nread,
960 				   u8 *read_data)
961 {
962 	bus->state = I2C_OPER_STARTED;
963 	bus->operation = I2C_READ_OPER;
964 	bus->slv_rd_size = nread;
965 	bus->slv_rd_ind = 0;
966 
967 	iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
968 	iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL);
969 	npcm_i2c_clear_tx_fifo(bus);
970 	npcm_i2c_clear_rx_fifo(bus);
971 }
972 
973 static void npcm_i2c_slave_xmit(struct npcm_i2c *bus, u16 nwrite,
974 				u8 *write_data)
975 {
976 	if (nwrite == 0)
977 		return;
978 
979 	bus->state = I2C_OPER_STARTED;
980 	bus->operation = I2C_WRITE_OPER;
981 
982 	/* get the next buffer */
983 	npcm_i2c_slave_get_wr_buf(bus);
984 	npcm_i2c_write_fifo_slave(bus, nwrite);
985 }
986 
987 /*
988  * npcm_i2c_slave_wr_buf_sync:
989  * currently slave IF only supports single byte operations.
990  * in order to utilize the npcm HW FIFO, the driver will ask for 16 bytes
991  * at a time, pack them in buffer, and then transmit them all together
992  * to the FIFO and onward to the bus.
993  * NACK on read will be once reached to bus->adap->quirks->max_read_len.
994  * sending a NACK wherever the backend requests for it is not supported.
995  * the next two functions allow reading to local buffer before writing it all
996  * to the HW FIFO.
997  */
998 static void npcm_i2c_slave_wr_buf_sync(struct npcm_i2c *bus)
999 {
1000 	int left_in_fifo;
1001 
1002 	left_in_fifo = FIELD_GET(NPCM_I2CTXF_STS_TX_BYTES,
1003 				 ioread8(bus->reg + NPCM_I2CTXF_STS));
1004 
1005 	/* fifo already full: */
1006 	if (left_in_fifo >= I2C_HW_FIFO_SIZE ||
1007 	    bus->slv_wr_size >= I2C_HW_FIFO_SIZE)
1008 		return;
1009 
1010 	/* update the wr fifo index back to the untransmitted bytes: */
1011 	bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo;
1012 	bus->slv_wr_size = bus->slv_wr_size + left_in_fifo;
1013 
1014 	if (bus->slv_wr_ind < 0)
1015 		bus->slv_wr_ind += I2C_HW_FIFO_SIZE;
1016 }
1017 
1018 static void npcm_i2c_slave_rd_wr(struct npcm_i2c *bus)
1019 {
1020 	if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) {
1021 		/*
1022 		 * Slave got an address match with direction bit 1 so it should
1023 		 * transmit data. Write till the master will NACK
1024 		 */
1025 		bus->operation = I2C_WRITE_OPER;
1026 		npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len,
1027 				    bus->slv_wr_buf);
1028 	} else {
1029 		/*
1030 		 * Slave got an address match with direction bit 0 so it should
1031 		 * receive data.
1032 		 * this module does not support saying no to bytes.
1033 		 * it will always ACK.
1034 		 */
1035 		bus->operation = I2C_READ_OPER;
1036 		npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus));
1037 		bus->stop_ind = I2C_SLAVE_RCV_IND;
1038 		npcm_i2c_slave_send_rd_buf(bus);
1039 		npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len,
1040 				       bus->slv_rd_buf);
1041 	}
1042 }
1043 
1044 static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
1045 {
1046 	u8 val;
1047 	irqreturn_t ret = IRQ_NONE;
1048 	u8 i2cst = ioread8(bus->reg + NPCM_I2CST);
1049 
1050 	/* Slave: A NACK has occurred */
1051 	if (NPCM_I2CST_NEGACK & i2cst) {
1052 		bus->stop_ind = I2C_NACK_IND;
1053 		npcm_i2c_slave_wr_buf_sync(bus);
1054 		if (bus->fifo_use)
1055 			/* clear the FIFO */
1056 			iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO,
1057 				 bus->reg + NPCM_I2CFIF_CTS);
1058 
1059 		/* In slave write, NACK is OK, otherwise it is a problem */
1060 		bus->stop_ind = I2C_NO_STATUS_IND;
1061 		bus->operation = I2C_NO_OPER;
1062 		bus->own_slave_addr = 0xFF;
1063 
1064 		/*
1065 		 * Slave has to wait for STOP to decide this is the end
1066 		 * of the transaction. tx is not yet considered as done
1067 		 */
1068 		iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST);
1069 
1070 		ret = IRQ_HANDLED;
1071 	}
1072 
1073 	/* Slave mode: a Bus Error (BER) has been identified */
1074 	if (NPCM_I2CST_BER & i2cst) {
1075 		/*
1076 		 * Check whether bus arbitration or Start or Stop during data
1077 		 * xfer bus arbitration problem should not result in recovery
1078 		 */
1079 		bus->stop_ind = I2C_BUS_ERR_IND;
1080 
1081 		/* wait for bus busy before clear fifo */
1082 		iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
1083 
1084 		bus->state = I2C_IDLE;
1085 
1086 		/*
1087 		 * in BER case we might get 2 interrupts: one for slave one for
1088 		 * master ( for a channel which is master\slave switching)
1089 		 */
1090 		if (completion_done(&bus->cmd_complete) == false) {
1091 			bus->cmd_err = -EIO;
1092 			complete(&bus->cmd_complete);
1093 		}
1094 		bus->own_slave_addr = 0xFF;
1095 		iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST);
1096 		ret = IRQ_HANDLED;
1097 	}
1098 
1099 	/* A Slave Stop Condition has been identified */
1100 	if (NPCM_I2CST_SLVSTP & i2cst) {
1101 		u8 bytes_in_fifo = npcm_i2c_fifo_usage(bus);
1102 
1103 		bus->stop_ind = I2C_SLAVE_DONE_IND;
1104 
1105 		if (bus->operation == I2C_READ_OPER)
1106 			npcm_i2c_read_fifo_slave(bus, bytes_in_fifo);
1107 
1108 		/* if the buffer is empty nothing will be sent */
1109 		npcm_i2c_slave_send_rd_buf(bus);
1110 
1111 		/* Slave done transmitting or receiving */
1112 		bus->stop_ind = I2C_NO_STATUS_IND;
1113 
1114 		/*
1115 		 * Note, just because we got here, it doesn't mean we through
1116 		 * away the wr buffer.
1117 		 * we keep it until the next received offset.
1118 		 */
1119 		bus->operation = I2C_NO_OPER;
1120 		bus->own_slave_addr = 0xFF;
1121 		i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0);
1122 		iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST);
1123 		if (bus->fifo_use) {
1124 			npcm_i2c_clear_fifo_int(bus);
1125 			npcm_i2c_clear_rx_fifo(bus);
1126 			npcm_i2c_clear_tx_fifo(bus);
1127 
1128 			iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO,
1129 				 bus->reg + NPCM_I2CFIF_CTS);
1130 		}
1131 		bus->state = I2C_IDLE;
1132 		ret = IRQ_HANDLED;
1133 	}
1134 
1135 	/* restart condition occurred and Rx-FIFO was not empty */
1136 	if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR,
1137 				       ioread8(bus->reg + NPCM_I2CFIF_CTS))) {
1138 		bus->stop_ind = I2C_SLAVE_RESTART_IND;
1139 		bus->master_or_slave = I2C_SLAVE;
1140 		if (bus->operation == I2C_READ_OPER)
1141 			npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus));
1142 		bus->operation = I2C_WRITE_OPER;
1143 		iowrite8(0, bus->reg + NPCM_I2CRXF_CTL);
1144 		val = NPCM_I2CFIF_CTS_CLR_FIFO | NPCM_I2CFIF_CTS_SLVRSTR |
1145 		      NPCM_I2CFIF_CTS_RXF_TXE;
1146 		iowrite8(val, bus->reg + NPCM_I2CFIF_CTS);
1147 		npcm_i2c_slave_rd_wr(bus);
1148 		ret = IRQ_HANDLED;
1149 	}
1150 
1151 	/* A Slave Address Match has been identified */
1152 	if (NPCM_I2CST_NMATCH & i2cst) {
1153 		u8 info = 0;
1154 
1155 		/* Address match automatically implies slave mode */
1156 		bus->master_or_slave = I2C_SLAVE;
1157 		npcm_i2c_clear_fifo_int(bus);
1158 		npcm_i2c_clear_rx_fifo(bus);
1159 		npcm_i2c_clear_tx_fifo(bus);
1160 		iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
1161 		iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL);
1162 		if (NPCM_I2CST_XMIT & i2cst) {
1163 			bus->operation = I2C_WRITE_OPER;
1164 		} else {
1165 			i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED,
1166 					&info);
1167 			bus->operation = I2C_READ_OPER;
1168 		}
1169 		if (bus->own_slave_addr == 0xFF) {
1170 			/* Check which type of address match */
1171 			val = ioread8(bus->reg + NPCM_I2CCST);
1172 			if (NPCM_I2CCST_MATCH & val) {
1173 				u16 addr;
1174 				enum i2c_addr eaddr;
1175 				u8 i2ccst2;
1176 				u8 i2ccst3;
1177 
1178 				i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3);
1179 				i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2);
1180 
1181 				/*
1182 				 * the i2c module can response to 10 own SA.
1183 				 * check which one was addressed by the master.
1184 				 * respond to the first one.
1185 				 */
1186 				addr = ((i2ccst3 & 0x07) << 7) |
1187 					(i2ccst2 & 0x7F);
1188 				info = ffs(addr);
1189 				eaddr = (enum i2c_addr)info;
1190 				addr = npcm_i2c_get_slave_addr(bus, eaddr);
1191 				addr &= 0x7F;
1192 				bus->own_slave_addr = addr;
1193 				if (bus->PEC_mask & BIT(info))
1194 					bus->PEC_use = true;
1195 				else
1196 					bus->PEC_use = false;
1197 			} else {
1198 				if (NPCM_I2CCST_GCMATCH & val)
1199 					bus->own_slave_addr = 0;
1200 				if (NPCM_I2CCST_ARPMATCH & val)
1201 					bus->own_slave_addr = 0x61;
1202 			}
1203 		} else {
1204 			/*
1205 			 *  Slave match can happen in two options:
1206 			 *  1. Start, SA, read (slave read without further ado)
1207 			 *  2. Start, SA, read, data, restart, SA, read,  ...
1208 			 *     (slave read in fragmented mode)
1209 			 *  3. Start, SA, write, data, restart, SA, read, ..
1210 			 *     (regular write-read mode)
1211 			 */
1212 			if ((bus->state == I2C_OPER_STARTED &&
1213 			     bus->operation == I2C_READ_OPER &&
1214 			     bus->stop_ind == I2C_SLAVE_XMIT_IND) ||
1215 			     bus->stop_ind == I2C_SLAVE_RCV_IND) {
1216 				/* slave tx after slave rx w/o STOP */
1217 				bus->stop_ind = I2C_SLAVE_RESTART_IND;
1218 			}
1219 		}
1220 
1221 		if (NPCM_I2CST_XMIT & i2cst)
1222 			bus->stop_ind = I2C_SLAVE_XMIT_IND;
1223 		else
1224 			bus->stop_ind = I2C_SLAVE_RCV_IND;
1225 		bus->state = I2C_SLAVE_MATCH;
1226 		npcm_i2c_slave_rd_wr(bus);
1227 		iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST);
1228 		ret = IRQ_HANDLED;
1229 	}
1230 
1231 	/* Slave SDA status is set - tx or rx */
1232 	if ((NPCM_I2CST_SDAST & i2cst) ||
1233 	    (bus->fifo_use &&
1234 	    (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) {
1235 		npcm_i2c_slave_rd_wr(bus);
1236 		iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST);
1237 		ret = IRQ_HANDLED;
1238 	} /* SDAST */
1239 
1240 	/*
1241 	 * if irq is not one of the above, make sure EOB is disabled and all
1242 	 * status bits are cleared.
1243 	 */
1244 	if (ret == IRQ_NONE) {
1245 		npcm_i2c_eob_int(bus, false);
1246 		npcm_i2c_clear_master_status(bus);
1247 	}
1248 
1249 	return IRQ_HANDLED;
1250 }
1251 
1252 static int npcm_i2c_reg_slave(struct i2c_client *client)
1253 {
1254 	unsigned long lock_flags;
1255 	struct npcm_i2c *bus = i2c_get_adapdata(client->adapter);
1256 
1257 	bus->slave = client;
1258 
1259 	if (!bus->slave)
1260 		return -EINVAL;
1261 
1262 	if (client->flags & I2C_CLIENT_TEN)
1263 		return -EAFNOSUPPORT;
1264 
1265 	spin_lock_irqsave(&bus->lock, lock_flags);
1266 
1267 	npcm_i2c_init_params(bus);
1268 	bus->slv_rd_size = 0;
1269 	bus->slv_wr_size = 0;
1270 	bus->slv_rd_ind = 0;
1271 	bus->slv_wr_ind = 0;
1272 	if (client->flags & I2C_CLIENT_PEC)
1273 		bus->PEC_use = true;
1274 
1275 	dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num,
1276 		 client->addr, bus->PEC_use);
1277 
1278 	npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true);
1279 	npcm_i2c_clear_fifo_int(bus);
1280 	npcm_i2c_clear_rx_fifo(bus);
1281 	npcm_i2c_clear_tx_fifo(bus);
1282 	npcm_i2c_slave_int_enable(bus, true);
1283 
1284 	spin_unlock_irqrestore(&bus->lock, lock_flags);
1285 	return 0;
1286 }
1287 
1288 static int npcm_i2c_unreg_slave(struct i2c_client *client)
1289 {
1290 	struct npcm_i2c *bus = client->adapter->algo_data;
1291 	unsigned long lock_flags;
1292 
1293 	spin_lock_irqsave(&bus->lock, lock_flags);
1294 	if (!bus->slave) {
1295 		spin_unlock_irqrestore(&bus->lock, lock_flags);
1296 		return -EINVAL;
1297 	}
1298 	npcm_i2c_slave_int_enable(bus, false);
1299 	npcm_i2c_remove_slave_addr(bus, client->addr);
1300 	bus->slave = NULL;
1301 	spin_unlock_irqrestore(&bus->lock, lock_flags);
1302 	return 0;
1303 }
1304 #endif /* CONFIG_I2C_SLAVE */
1305 
1306 static void npcm_i2c_master_fifo_read(struct npcm_i2c *bus)
1307 {
1308 	int rcount;
1309 	int fifo_bytes;
1310 	enum i2c_state_ind ind = I2C_MASTER_DONE_IND;
1311 
1312 	fifo_bytes = npcm_i2c_fifo_usage(bus);
1313 	rcount = bus->rd_size - bus->rd_ind;
1314 
1315 	/*
1316 	 * In order not to change the RX_TRH during transaction (we found that
1317 	 * this might be problematic if it takes too much time to read the FIFO)
1318 	 * we read the data in the following way. If the number of bytes to
1319 	 * read == FIFO Size + C (where C < FIFO Size)then first read C bytes
1320 	 * and in the next int we read rest of the data.
1321 	 */
1322 	if (rcount < (2 * I2C_HW_FIFO_SIZE) && rcount > I2C_HW_FIFO_SIZE)
1323 		fifo_bytes = rcount - I2C_HW_FIFO_SIZE;
1324 
1325 	if (rcount <= fifo_bytes) {
1326 		/* last bytes are about to be read - end of tx */
1327 		bus->state = I2C_STOP_PENDING;
1328 		bus->stop_ind = ind;
1329 		npcm_i2c_eob_int(bus, true);
1330 		/* Stop should be set before reading last byte. */
1331 		npcm_i2c_master_stop(bus);
1332 		npcm_i2c_read_fifo(bus, fifo_bytes);
1333 	} else {
1334 		npcm_i2c_read_fifo(bus, fifo_bytes);
1335 		rcount = bus->rd_size - bus->rd_ind;
1336 		npcm_i2c_set_fifo(bus, rcount, -1);
1337 	}
1338 }
1339 
1340 static void npcm_i2c_irq_master_handler_write(struct npcm_i2c *bus)
1341 {
1342 	u16 wcount;
1343 
1344 	if (bus->fifo_use)
1345 		npcm_i2c_clear_tx_fifo(bus); /* clear the TX fifo status bit */
1346 
1347 	/* Master write operation - last byte handling */
1348 	if (bus->wr_ind == bus->wr_size) {
1349 		if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0)
1350 			/*
1351 			 * No more bytes to send (to add to the FIFO),
1352 			 * however the FIFO is not empty yet. It is
1353 			 * still in the middle of tx. Currently there's nothing
1354 			 * to do except for waiting to the end of the tx
1355 			 * We will get an int when the FIFO will get empty.
1356 			 */
1357 			return;
1358 
1359 		if (bus->rd_size == 0) {
1360 			/* all bytes have been written, in wr only operation */
1361 			npcm_i2c_eob_int(bus, true);
1362 			bus->state = I2C_STOP_PENDING;
1363 			bus->stop_ind = I2C_MASTER_DONE_IND;
1364 			npcm_i2c_master_stop(bus);
1365 			/* Clear SDA Status bit (by writing dummy byte) */
1366 			npcm_i2c_wr_byte(bus, 0xFF);
1367 
1368 		} else {
1369 			/* last write-byte written on previous int - restart */
1370 			npcm_i2c_set_fifo(bus, bus->rd_size, -1);
1371 			/* Generate repeated start upon next write to SDA */
1372 			npcm_i2c_master_start(bus);
1373 
1374 			/*
1375 			 * Receiving one byte only - stall after successful
1376 			 * completion of send address byte. If we NACK here, and
1377 			 * slave doesn't ACK the address, we might
1378 			 * unintentionally NACK the next multi-byte read.
1379 			 */
1380 			if (bus->rd_size == 1)
1381 				npcm_i2c_stall_after_start(bus, true);
1382 
1383 			/* Next int will occur on read */
1384 			bus->operation = I2C_READ_OPER;
1385 			/* send the slave address in read direction */
1386 			npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1);
1387 		}
1388 	} else {
1389 		/* write next byte not last byte and not slave address */
1390 		if (!bus->fifo_use || bus->wr_size == 1) {
1391 			npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]);
1392 		} else {
1393 			wcount = bus->wr_size - bus->wr_ind;
1394 			npcm_i2c_set_fifo(bus, -1, wcount);
1395 			if (wcount)
1396 				npcm_i2c_write_to_fifo_master(bus, wcount);
1397 		}
1398 	}
1399 }
1400 
1401 static void npcm_i2c_irq_master_handler_read(struct npcm_i2c *bus)
1402 {
1403 	u16 block_extra_bytes_size;
1404 	u8 data;
1405 
1406 	/* added bytes to the packet: */
1407 	block_extra_bytes_size = bus->read_block_use + bus->PEC_use;
1408 
1409 	/*
1410 	 * Perform master read, distinguishing between last byte and the rest of
1411 	 * the bytes. The last byte should be read when the clock is stopped
1412 	 */
1413 	if (bus->rd_ind == 0) { /* first byte handling: */
1414 		if (bus->read_block_use) {
1415 			/* first byte in block protocol is the size: */
1416 			data = npcm_i2c_rd_byte(bus);
1417 			data = clamp_val(data, 1, I2C_SMBUS_BLOCK_MAX);
1418 			bus->rd_size = data + block_extra_bytes_size;
1419 			bus->rd_buf[bus->rd_ind++] = data;
1420 
1421 			/* clear RX FIFO interrupt status: */
1422 			if (bus->fifo_use) {
1423 				data = ioread8(bus->reg + NPCM_I2CFIF_CTS);
1424 				data = data | NPCM_I2CFIF_CTS_RXF_TXE;
1425 				iowrite8(data, bus->reg + NPCM_I2CFIF_CTS);
1426 			}
1427 
1428 			npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1);
1429 			npcm_i2c_stall_after_start(bus, false);
1430 		} else {
1431 			npcm_i2c_clear_tx_fifo(bus);
1432 			npcm_i2c_master_fifo_read(bus);
1433 		}
1434 	} else {
1435 		if (bus->rd_size == block_extra_bytes_size &&
1436 		    bus->read_block_use) {
1437 			bus->state = I2C_STOP_PENDING;
1438 			bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND;
1439 			bus->cmd_err = -EIO;
1440 			npcm_i2c_eob_int(bus, true);
1441 			npcm_i2c_master_stop(bus);
1442 			npcm_i2c_read_fifo(bus, npcm_i2c_fifo_usage(bus));
1443 		} else {
1444 			npcm_i2c_master_fifo_read(bus);
1445 		}
1446 	}
1447 }
1448 
1449 static void npcm_i2c_irq_handle_nmatch(struct npcm_i2c *bus)
1450 {
1451 	iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST);
1452 	npcm_i2c_nack(bus);
1453 	bus->stop_ind = I2C_BUS_ERR_IND;
1454 	npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus));
1455 }
1456 
1457 /* A NACK has occurred */
1458 static void npcm_i2c_irq_handle_nack(struct npcm_i2c *bus)
1459 {
1460 	u8 val;
1461 
1462 	if (bus->nack_cnt < ULLONG_MAX)
1463 		bus->nack_cnt++;
1464 
1465 	if (bus->fifo_use) {
1466 		/*
1467 		 * if there are still untransmitted bytes in TX FIFO
1468 		 * reduce them from wr_ind
1469 		 */
1470 		if (bus->operation == I2C_WRITE_OPER)
1471 			bus->wr_ind -= npcm_i2c_fifo_usage(bus);
1472 
1473 		/* clear the FIFO */
1474 		iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS);
1475 	}
1476 
1477 	/* In master write operation, got unexpected NACK */
1478 	bus->stop_ind = I2C_NACK_IND;
1479 	/* Only current master is allowed to issue Stop Condition */
1480 	if (npcm_i2c_is_master(bus)) {
1481 		/* stopping in the middle */
1482 		npcm_i2c_eob_int(bus, false);
1483 		npcm_i2c_master_stop(bus);
1484 
1485 		/* Clear SDA Status bit (by reading dummy byte) */
1486 		npcm_i2c_rd_byte(bus);
1487 
1488 		/*
1489 		 * The bus is released from stall only after the SW clears
1490 		 * NEGACK bit. Then a Stop condition is sent.
1491 		 */
1492 		npcm_i2c_clear_master_status(bus);
1493 		readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val,
1494 					  !(val & NPCM_I2CCST_BUSY), 10, 200);
1495 		/* verify no status bits are still set after bus is released */
1496 		npcm_i2c_clear_master_status(bus);
1497 	}
1498 	bus->state = I2C_IDLE;
1499 
1500 	/*
1501 	 * In Master mode, NACK should be cleared only after STOP.
1502 	 * In such case, the bus is released from stall only after the
1503 	 * software clears NACK bit. Then a Stop condition is sent.
1504 	 */
1505 	npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind);
1506 }
1507 
1508 	/* Master mode: a Bus Error has been identified */
1509 static void npcm_i2c_irq_handle_ber(struct npcm_i2c *bus)
1510 {
1511 	if (bus->ber_cnt < ULLONG_MAX)
1512 		bus->ber_cnt++;
1513 	bus->stop_ind = I2C_BUS_ERR_IND;
1514 	if (npcm_i2c_is_master(bus)) {
1515 		npcm_i2c_master_abort(bus);
1516 	} else {
1517 		npcm_i2c_clear_master_status(bus);
1518 
1519 		/* Clear BB (BUS BUSY) bit */
1520 		iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
1521 
1522 		bus->cmd_err = -EAGAIN;
1523 		npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus));
1524 	}
1525 	bus->state = I2C_IDLE;
1526 }
1527 
1528 	/* EOB: a master End Of Busy (meaning STOP completed) */
1529 static void npcm_i2c_irq_handle_eob(struct npcm_i2c *bus)
1530 {
1531 	npcm_i2c_eob_int(bus, false);
1532 	bus->state = I2C_IDLE;
1533 	npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind);
1534 }
1535 
1536 /* Address sent and requested stall occurred (Master mode) */
1537 static void npcm_i2c_irq_handle_stall_after_start(struct npcm_i2c *bus)
1538 {
1539 	if (npcm_i2c_is_quick(bus)) {
1540 		bus->state = I2C_STOP_PENDING;
1541 		bus->stop_ind = I2C_MASTER_DONE_IND;
1542 		npcm_i2c_eob_int(bus, true);
1543 		npcm_i2c_master_stop(bus);
1544 	} else if ((bus->rd_size == 1) && !bus->read_block_use) {
1545 		/*
1546 		 * Receiving one byte only - set NACK after ensuring
1547 		 * slave ACKed the address byte.
1548 		 */
1549 		npcm_i2c_nack(bus);
1550 	}
1551 
1552 	/* Reset stall-after-address-byte */
1553 	npcm_i2c_stall_after_start(bus, false);
1554 
1555 	/* Clear stall only after setting STOP */
1556 	iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST);
1557 }
1558 
1559 /* SDA status is set - TX or RX, master */
1560 static void npcm_i2c_irq_handle_sda(struct npcm_i2c *bus, u8 i2cst)
1561 {
1562 	u8 fif_cts;
1563 
1564 	if (!npcm_i2c_is_master(bus))
1565 		return;
1566 
1567 	if (bus->state == I2C_IDLE) {
1568 		bus->stop_ind = I2C_WAKE_UP_IND;
1569 
1570 		if (npcm_i2c_is_quick(bus) || bus->read_block_use)
1571 			/*
1572 			 * Need to stall after successful
1573 			 * completion of sending address byte
1574 			 */
1575 			npcm_i2c_stall_after_start(bus, true);
1576 		else
1577 			npcm_i2c_stall_after_start(bus, false);
1578 
1579 		/*
1580 		 * Receiving one byte only - stall after successful completion
1581 		 * of sending address byte If we NACK here, and slave doesn't
1582 		 * ACK the address, we might unintentionally NACK the next
1583 		 * multi-byte read
1584 		 */
1585 		if (bus->wr_size == 0 && bus->rd_size == 1)
1586 			npcm_i2c_stall_after_start(bus, true);
1587 
1588 		/* Initiate I2C master tx */
1589 
1590 		/* select bank 1 for FIFO regs */
1591 		npcm_i2c_select_bank(bus, I2C_BANK_1);
1592 
1593 		fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
1594 		fif_cts = fif_cts & ~NPCM_I2CFIF_CTS_SLVRSTR;
1595 
1596 		/* clear FIFO and relevant status bits. */
1597 		fif_cts = fif_cts | NPCM_I2CFIF_CTS_CLR_FIFO;
1598 		iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
1599 
1600 		/* re-enable */
1601 		fif_cts = fif_cts | NPCM_I2CFIF_CTS_RXF_TXE;
1602 		iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
1603 
1604 		/*
1605 		 * Configure the FIFO threshold:
1606 		 * according to the needed # of bytes to read.
1607 		 * Note: due to HW limitation can't config the rx fifo before it
1608 		 * got and ACK on the restart. LAST bit will not be reset unless
1609 		 * RX completed. It will stay set on the next tx.
1610 		 */
1611 		if (bus->wr_size)
1612 			npcm_i2c_set_fifo(bus, -1, bus->wr_size);
1613 		else
1614 			npcm_i2c_set_fifo(bus, bus->rd_size, -1);
1615 
1616 		bus->state = I2C_OPER_STARTED;
1617 
1618 		if (npcm_i2c_is_quick(bus) || bus->wr_size)
1619 			npcm_i2c_wr_byte(bus, bus->dest_addr);
1620 		else
1621 			npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0));
1622 	/* SDA interrupt, after start\restart */
1623 	} else {
1624 		if (NPCM_I2CST_XMIT & i2cst) {
1625 			bus->operation = I2C_WRITE_OPER;
1626 			npcm_i2c_irq_master_handler_write(bus);
1627 		} else {
1628 			bus->operation = I2C_READ_OPER;
1629 			npcm_i2c_irq_master_handler_read(bus);
1630 		}
1631 	}
1632 }
1633 
1634 static int npcm_i2c_int_master_handler(struct npcm_i2c *bus)
1635 {
1636 	u8 i2cst;
1637 	int ret = -EIO;
1638 
1639 	i2cst = ioread8(bus->reg + NPCM_I2CST);
1640 
1641 	if (FIELD_GET(NPCM_I2CST_NMATCH, i2cst)) {
1642 		npcm_i2c_irq_handle_nmatch(bus);
1643 		return 0;
1644 	}
1645 	/* A NACK has occurred */
1646 	if (FIELD_GET(NPCM_I2CST_NEGACK, i2cst)) {
1647 		npcm_i2c_irq_handle_nack(bus);
1648 		return 0;
1649 	}
1650 
1651 	/* Master mode: a Bus Error has been identified */
1652 	if (FIELD_GET(NPCM_I2CST_BER, i2cst)) {
1653 		npcm_i2c_irq_handle_ber(bus);
1654 		return 0;
1655 	}
1656 
1657 	/* EOB: a master End Of Busy (meaning STOP completed) */
1658 	if ((FIELD_GET(NPCM_I2CCTL1_EOBINTE,
1659 		       ioread8(bus->reg + NPCM_I2CCTL1)) == 1) &&
1660 	    (FIELD_GET(NPCM_I2CCST3_EO_BUSY,
1661 		       ioread8(bus->reg + NPCM_I2CCST3)))) {
1662 		npcm_i2c_irq_handle_eob(bus);
1663 		return 0;
1664 	}
1665 
1666 	/* Address sent and requested stall occurred (Master mode) */
1667 	if (FIELD_GET(NPCM_I2CST_STASTR, i2cst)) {
1668 		npcm_i2c_irq_handle_stall_after_start(bus);
1669 		ret = 0;
1670 	}
1671 
1672 	/* SDA status is set - TX or RX, master */
1673 	if (FIELD_GET(NPCM_I2CST_SDAST, i2cst) ||
1674 	    (bus->fifo_use &&
1675 	    (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) {
1676 		npcm_i2c_irq_handle_sda(bus, i2cst);
1677 		ret = 0;
1678 	}
1679 
1680 	return ret;
1681 }
1682 
1683 /* recovery using TGCLK functionality of the module */
1684 static int npcm_i2c_recovery_tgclk(struct i2c_adapter *_adap)
1685 {
1686 	u8               val;
1687 	u8               fif_cts;
1688 	bool             done = false;
1689 	int              status = -ENOTRECOVERABLE;
1690 	struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
1691 	/* Allow 3 bytes (27 toggles) to be read from the slave: */
1692 	int              iter = 27;
1693 
1694 	if ((npcm_i2c_get_SDA(_adap) == 1) && (npcm_i2c_get_SCL(_adap) == 1)) {
1695 		dev_dbg(bus->dev, "bus%d-0x%x recovery skipped, bus not stuck",
1696 			bus->num, bus->dest_addr);
1697 		npcm_i2c_reset(bus);
1698 		return 0;
1699 	}
1700 
1701 	npcm_i2c_int_enable(bus, false);
1702 	npcm_i2c_disable(bus);
1703 	npcm_i2c_enable(bus);
1704 	iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
1705 	npcm_i2c_clear_tx_fifo(bus);
1706 	npcm_i2c_clear_rx_fifo(bus);
1707 	iowrite8(0, bus->reg + NPCM_I2CRXF_CTL);
1708 	iowrite8(0, bus->reg + NPCM_I2CTXF_CTL);
1709 	npcm_i2c_stall_after_start(bus, false);
1710 
1711 	/* select bank 1 for FIFO regs */
1712 	npcm_i2c_select_bank(bus, I2C_BANK_1);
1713 
1714 	/* clear FIFO and relevant status bits. */
1715 	fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
1716 	fif_cts &= ~NPCM_I2CFIF_CTS_SLVRSTR;
1717 	fif_cts |= NPCM_I2CFIF_CTS_CLR_FIFO;
1718 	iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS);
1719 	npcm_i2c_set_fifo(bus, -1, 0);
1720 
1721 	/* Repeat the following sequence until SDA is released */
1722 	do {
1723 		/* Issue a single SCL toggle */
1724 		iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST);
1725 		usleep_range(20, 30);
1726 		/* If SDA line is inactive (high), stop */
1727 		if (npcm_i2c_get_SDA(_adap)) {
1728 			done = true;
1729 			status = 0;
1730 		}
1731 	} while (!done && iter--);
1732 
1733 	/* If SDA line is released: send start-addr-stop, to re-sync. */
1734 	if (npcm_i2c_get_SDA(_adap)) {
1735 		/* Send an address byte in write direction: */
1736 		npcm_i2c_wr_byte(bus, bus->dest_addr);
1737 		npcm_i2c_master_start(bus);
1738 		/* Wait until START condition is sent */
1739 		status = readx_poll_timeout(npcm_i2c_get_SCL, _adap, val, !val,
1740 					    20, 200);
1741 		/* If START condition was sent */
1742 		if (npcm_i2c_is_master(bus) > 0) {
1743 			usleep_range(20, 30);
1744 			npcm_i2c_master_stop(bus);
1745 			usleep_range(200, 500);
1746 		}
1747 	}
1748 	npcm_i2c_reset(bus);
1749 	npcm_i2c_int_enable(bus, true);
1750 
1751 	if ((npcm_i2c_get_SDA(_adap) == 1) && (npcm_i2c_get_SCL(_adap) == 1))
1752 		status = 0;
1753 	else
1754 		status = -ENOTRECOVERABLE;
1755 	if (status) {
1756 		if (bus->rec_fail_cnt < ULLONG_MAX)
1757 			bus->rec_fail_cnt++;
1758 	} else {
1759 		if (bus->rec_succ_cnt < ULLONG_MAX)
1760 			bus->rec_succ_cnt++;
1761 	}
1762 	return status;
1763 }
1764 
1765 /* recovery using bit banging functionality of the module */
1766 static void npcm_i2c_recovery_init(struct i2c_adapter *_adap)
1767 {
1768 	struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap);
1769 	struct i2c_bus_recovery_info *rinfo = &bus->rinfo;
1770 
1771 	rinfo->recover_bus = npcm_i2c_recovery_tgclk;
1772 
1773 	/*
1774 	 * npcm i2c HW allows direct reading of SCL and SDA.
1775 	 * However, it does not support setting SCL and SDA directly.
1776 	 * The recovery function can toggle SCL when SDA is low (but not set)
1777 	 * Getter functions used internally, and can be used externally.
1778 	 */
1779 	rinfo->get_scl = npcm_i2c_get_SCL;
1780 	rinfo->get_sda = npcm_i2c_get_SDA;
1781 	_adap->bus_recovery_info = rinfo;
1782 }
1783 
1784 /* SCLFRQ min/max field values */
1785 #define SCLFRQ_MIN  10
1786 #define SCLFRQ_MAX  511
1787 #define clk_coef(freq, mul)	DIV_ROUND_UP((freq) * (mul), 1000000)
1788 
1789 /*
1790  * npcm_i2c_init_clk: init HW timing parameters.
1791  * NPCM7XX i2c module timing parameters are dependent on module core clk (APB)
1792  * and bus frequency.
1793  * 100kHz bus requires tSCL = 4 * SCLFRQ * tCLK. LT and HT are symmetric.
1794  * 400kHz bus requires asymmetric HT and LT. A different equation is recommended
1795  * by the HW designer, given core clock range (equations in comments below).
1796  *
1797  */
1798 static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz)
1799 {
1800 	u32  k1 = 0;
1801 	u32  k2 = 0;
1802 	u8   dbnct = 0;
1803 	u32  sclfrq = 0;
1804 	u8   hldt = 7;
1805 	u8   fast_mode = 0;
1806 	u32  src_clk_khz;
1807 	u32  bus_freq_khz;
1808 
1809 	src_clk_khz = bus->apb_clk / 1000;
1810 	bus_freq_khz = bus_freq_hz / 1000;
1811 	bus->bus_freq = bus_freq_hz;
1812 
1813 	/* 100KHz and below: */
1814 	if (bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ) {
1815 		sclfrq = src_clk_khz / (bus_freq_khz * 4);
1816 
1817 		if (sclfrq < SCLFRQ_MIN || sclfrq > SCLFRQ_MAX)
1818 			return -EDOM;
1819 
1820 		if (src_clk_khz >= 40000)
1821 			hldt = 17;
1822 		else if (src_clk_khz >= 12500)
1823 			hldt = 15;
1824 		else
1825 			hldt = 7;
1826 	}
1827 
1828 	/* 400KHz: */
1829 	else if (bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ) {
1830 		sclfrq = 0;
1831 		fast_mode = I2CCTL3_400K_MODE;
1832 
1833 		if (src_clk_khz < 7500)
1834 			/* 400KHZ cannot be supported for core clock < 7.5MHz */
1835 			return -EDOM;
1836 
1837 		else if (src_clk_khz >= 50000) {
1838 			k1 = 80;
1839 			k2 = 48;
1840 			hldt = 12;
1841 			dbnct = 7;
1842 		}
1843 
1844 		/* Master or Slave with frequency > 25MHz */
1845 		else if (src_clk_khz > 25000) {
1846 			hldt = clk_coef(src_clk_khz, 300) + 7;
1847 			k1 = clk_coef(src_clk_khz, 1600);
1848 			k2 = clk_coef(src_clk_khz, 900);
1849 		}
1850 	}
1851 
1852 	/* 1MHz: */
1853 	else if (bus_freq_hz <= I2C_MAX_FAST_MODE_PLUS_FREQ) {
1854 		sclfrq = 0;
1855 		fast_mode = I2CCTL3_400K_MODE;
1856 
1857 		/* 1MHZ cannot be supported for core clock < 24 MHz */
1858 		if (src_clk_khz < 24000)
1859 			return -EDOM;
1860 
1861 		k1 = clk_coef(src_clk_khz, 620);
1862 		k2 = clk_coef(src_clk_khz, 380);
1863 
1864 		/* Core clk > 40 MHz */
1865 		if (src_clk_khz > 40000) {
1866 			/*
1867 			 * Set HLDT:
1868 			 * SDA hold time:  (HLDT-7) * T(CLK) >= 120
1869 			 * HLDT = 120/T(CLK) + 7 = 120 * FREQ(CLK) + 7
1870 			 */
1871 			hldt = clk_coef(src_clk_khz, 120) + 7;
1872 		} else {
1873 			hldt = 7;
1874 			dbnct = 2;
1875 		}
1876 	}
1877 
1878 	/* Frequency larger than 1 MHz is not supported */
1879 	else
1880 		return -EINVAL;
1881 
1882 	if (bus_freq_hz >= I2C_MAX_FAST_MODE_FREQ) {
1883 		k1 = round_up(k1, 2);
1884 		k2 = round_up(k2 + 1, 2);
1885 		if (k1 < SCLFRQ_MIN || k1 > SCLFRQ_MAX ||
1886 		    k2 < SCLFRQ_MIN || k2 > SCLFRQ_MAX)
1887 			return -EDOM;
1888 	}
1889 
1890 	/* write sclfrq value. bits [6:0] are in I2CCTL2 reg */
1891 	iowrite8(FIELD_PREP(I2CCTL2_SCLFRQ6_0, sclfrq & 0x7F),
1892 		 bus->reg + NPCM_I2CCTL2);
1893 
1894 	/* bits [8:7] are in I2CCTL3 reg */
1895 	iowrite8(fast_mode | FIELD_PREP(I2CCTL3_SCLFRQ8_7, (sclfrq >> 7) & 0x3),
1896 		 bus->reg + NPCM_I2CCTL3);
1897 
1898 	/* Select Bank 0 to access NPCM_I2CCTL4/NPCM_I2CCTL5 */
1899 	npcm_i2c_select_bank(bus, I2C_BANK_0);
1900 
1901 	if (bus_freq_hz >= I2C_MAX_FAST_MODE_FREQ) {
1902 		/*
1903 		 * Set SCL Low/High Time:
1904 		 * k1 = 2 * SCLLT7-0 -> Low Time  = k1 / 2
1905 		 * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2
1906 		 */
1907 		iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT);
1908 		iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT);
1909 
1910 		iowrite8(dbnct, bus->reg + NPCM_I2CCTL5);
1911 	}
1912 
1913 	iowrite8(hldt, bus->reg + NPCM_I2CCTL4);
1914 
1915 	/* Return to Bank 1, and stay there by default: */
1916 	npcm_i2c_select_bank(bus, I2C_BANK_1);
1917 
1918 	return 0;
1919 }
1920 
1921 static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode,
1922 				u32 bus_freq_hz)
1923 {
1924 	u8 val;
1925 	int ret;
1926 
1927 	/* Check whether module already enabled or frequency is out of bounds */
1928 	if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) ||
1929 	    bus_freq_hz < I2C_FREQ_MIN_HZ || bus_freq_hz > I2C_FREQ_MAX_HZ)
1930 		return -EINVAL;
1931 
1932 	npcm_i2c_int_enable(bus, false);
1933 	npcm_i2c_disable(bus);
1934 
1935 	/* Configure FIFO mode : */
1936 	if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) {
1937 		bus->fifo_use = true;
1938 		npcm_i2c_select_bank(bus, I2C_BANK_0);
1939 		val = ioread8(bus->reg + NPCM_I2CFIF_CTL);
1940 		val |= NPCM_I2CFIF_CTL_FIFO_EN;
1941 		iowrite8(val, bus->reg + NPCM_I2CFIF_CTL);
1942 		npcm_i2c_select_bank(bus, I2C_BANK_1);
1943 	} else {
1944 		bus->fifo_use = false;
1945 	}
1946 
1947 	/* Configure I2C module clock frequency */
1948 	ret = npcm_i2c_init_clk(bus, bus_freq_hz);
1949 	if (ret) {
1950 		dev_err(bus->dev, "npcm_i2c_init_clk failed\n");
1951 		return ret;
1952 	}
1953 
1954 	/* Enable module (before configuring CTL1) */
1955 	npcm_i2c_enable(bus);
1956 	bus->state = I2C_IDLE;
1957 	val = ioread8(bus->reg + NPCM_I2CCTL1);
1958 	val = (val | NPCM_I2CCTL1_NMINTE) & ~NPCM_I2CCTL1_RWS;
1959 	iowrite8(val, bus->reg + NPCM_I2CCTL1);
1960 
1961 	npcm_i2c_reset(bus);
1962 
1963 	/* check HW is OK: SDA and SCL should be high at this point. */
1964 	if ((npcm_i2c_get_SDA(&bus->adap) == 0) || (npcm_i2c_get_SCL(&bus->adap) == 0)) {
1965 		dev_err(bus->dev, "I2C%d init fail: lines are low\n", bus->num);
1966 		dev_err(bus->dev, "SDA=%d SCL=%d\n", npcm_i2c_get_SDA(&bus->adap),
1967 			npcm_i2c_get_SCL(&bus->adap));
1968 		return -ENXIO;
1969 	}
1970 
1971 	npcm_i2c_int_enable(bus, true);
1972 	return 0;
1973 }
1974 
1975 static int __npcm_i2c_init(struct npcm_i2c *bus, struct platform_device *pdev)
1976 {
1977 	u32 clk_freq_hz;
1978 	int ret;
1979 
1980 	/* Initialize the internal data structures */
1981 	bus->state = I2C_DISABLE;
1982 	bus->master_or_slave = I2C_SLAVE;
1983 	bus->int_time_stamp = 0;
1984 #if IS_ENABLED(CONFIG_I2C_SLAVE)
1985 	bus->slave = NULL;
1986 #endif
1987 
1988 	ret = device_property_read_u32(&pdev->dev, "clock-frequency",
1989 				       &clk_freq_hz);
1990 	if (ret) {
1991 		dev_info(&pdev->dev, "Could not read clock-frequency property");
1992 		clk_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
1993 	}
1994 
1995 	ret = npcm_i2c_init_module(bus, I2C_MASTER, clk_freq_hz);
1996 	if (ret) {
1997 		dev_err(&pdev->dev, "npcm_i2c_init_module failed\n");
1998 		return ret;
1999 	}
2000 
2001 	return 0;
2002 }
2003 
2004 static irqreturn_t npcm_i2c_bus_irq(int irq, void *dev_id)
2005 {
2006 	struct npcm_i2c *bus = dev_id;
2007 
2008 	if (npcm_i2c_is_master(bus))
2009 		bus->master_or_slave = I2C_MASTER;
2010 
2011 	if (bus->master_or_slave == I2C_MASTER) {
2012 		bus->int_time_stamp = jiffies;
2013 		if (!npcm_i2c_int_master_handler(bus))
2014 			return IRQ_HANDLED;
2015 	}
2016 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2017 	if (bus->slave) {
2018 		bus->master_or_slave = I2C_SLAVE;
2019 		if (npcm_i2c_int_slave_handler(bus))
2020 			return IRQ_HANDLED;
2021 	}
2022 #endif
2023 	/* clear status bits for spurious interrupts */
2024 	npcm_i2c_clear_master_status(bus);
2025 
2026 	return IRQ_HANDLED;
2027 }
2028 
2029 static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus,
2030 				       u8 slave_addr, u16 nwrite, u16 nread,
2031 				       u8 *write_data, u8 *read_data,
2032 				       bool use_PEC, bool use_read_block)
2033 {
2034 	if (bus->state != I2C_IDLE) {
2035 		bus->cmd_err = -EBUSY;
2036 		return false;
2037 	}
2038 	bus->dest_addr = slave_addr << 1;
2039 	bus->wr_buf = write_data;
2040 	bus->wr_size = nwrite;
2041 	bus->wr_ind = 0;
2042 	bus->rd_buf = read_data;
2043 	bus->rd_size = nread;
2044 	bus->rd_ind = 0;
2045 	bus->PEC_use = 0;
2046 
2047 	/* for tx PEC is appended to buffer from i2c IF. PEC flag is ignored */
2048 	if (nread)
2049 		bus->PEC_use = use_PEC;
2050 
2051 	bus->read_block_use = use_read_block;
2052 	if (nread && !nwrite)
2053 		bus->operation = I2C_READ_OPER;
2054 	else
2055 		bus->operation = I2C_WRITE_OPER;
2056 	if (bus->fifo_use) {
2057 		u8 i2cfif_cts;
2058 
2059 		npcm_i2c_select_bank(bus, I2C_BANK_1);
2060 		/* clear FIFO and relevant status bits. */
2061 		i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS);
2062 		i2cfif_cts &= ~NPCM_I2CFIF_CTS_SLVRSTR;
2063 		i2cfif_cts |= NPCM_I2CFIF_CTS_CLR_FIFO;
2064 		iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS);
2065 	}
2066 
2067 	bus->state = I2C_IDLE;
2068 	npcm_i2c_stall_after_start(bus, true);
2069 	npcm_i2c_master_start(bus);
2070 	return true;
2071 }
2072 
2073 static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
2074 				int num)
2075 {
2076 	struct npcm_i2c *bus = container_of(adap, struct npcm_i2c, adap);
2077 	struct i2c_msg *msg0, *msg1;
2078 	unsigned long time_left, flags;
2079 	u16 nwrite, nread;
2080 	u8 *write_data, *read_data;
2081 	u8 slave_addr;
2082 	unsigned long timeout;
2083 	bool read_block = false;
2084 	bool read_PEC = false;
2085 	u8 bus_busy;
2086 	unsigned long timeout_usec;
2087 
2088 	if (bus->state == I2C_DISABLE) {
2089 		dev_err(bus->dev, "I2C%d module is disabled", bus->num);
2090 		return -EINVAL;
2091 	}
2092 
2093 	msg0 = &msgs[0];
2094 	slave_addr = msg0->addr;
2095 	if (msg0->flags & I2C_M_RD) { /* read */
2096 		nwrite = 0;
2097 		write_data = NULL;
2098 		read_data = msg0->buf;
2099 		if (msg0->flags & I2C_M_RECV_LEN) {
2100 			nread = 1;
2101 			read_block = true;
2102 			if (msg0->flags & I2C_CLIENT_PEC)
2103 				read_PEC = true;
2104 		} else {
2105 			nread = msg0->len;
2106 		}
2107 	} else { /* write */
2108 		nwrite = msg0->len;
2109 		write_data = msg0->buf;
2110 		nread = 0;
2111 		read_data = NULL;
2112 		if (num == 2) {
2113 			msg1 = &msgs[1];
2114 			read_data = msg1->buf;
2115 			if (msg1->flags & I2C_M_RECV_LEN) {
2116 				nread = 1;
2117 				read_block = true;
2118 				if (msg1->flags & I2C_CLIENT_PEC)
2119 					read_PEC = true;
2120 			} else {
2121 				nread = msg1->len;
2122 				read_block = false;
2123 			}
2124 		}
2125 	}
2126 
2127 	/*
2128 	 * Adaptive TimeOut: estimated time in usec + 100% margin:
2129 	 * 2: double the timeout for clock stretching case
2130 	 * 9: bits per transaction (including the ack/nack)
2131 	 */
2132 	timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite);
2133 	timeout = max_t(unsigned long, bus->adap.timeout, usecs_to_jiffies(timeout_usec));
2134 	if (nwrite >= 32 * 1024 || nread >= 32 * 1024) {
2135 		dev_err(bus->dev, "i2c%d buffer too big\n", bus->num);
2136 		return -EINVAL;
2137 	}
2138 
2139 	time_left = jiffies + timeout + 1;
2140 	do {
2141 		/*
2142 		 * we must clear slave address immediately when the bus is not
2143 		 * busy, so we spinlock it, but we don't keep the lock for the
2144 		 * entire while since it is too long.
2145 		 */
2146 		spin_lock_irqsave(&bus->lock, flags);
2147 		bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB;
2148 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2149 		if (!bus_busy && bus->slave)
2150 			iowrite8((bus->slave->addr & 0x7F),
2151 				 bus->reg + NPCM_I2CADDR1);
2152 #endif
2153 		spin_unlock_irqrestore(&bus->lock, flags);
2154 
2155 	} while (time_is_after_jiffies(time_left) && bus_busy);
2156 
2157 	if (bus_busy) {
2158 		iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST);
2159 		npcm_i2c_reset(bus);
2160 		i2c_recover_bus(adap);
2161 		return -EAGAIN;
2162 	}
2163 
2164 	npcm_i2c_init_params(bus);
2165 	bus->dest_addr = slave_addr;
2166 	bus->msgs = msgs;
2167 	bus->msgs_num = num;
2168 	bus->cmd_err = 0;
2169 	bus->read_block_use = read_block;
2170 
2171 	reinit_completion(&bus->cmd_complete);
2172 
2173 	npcm_i2c_int_enable(bus, true);
2174 
2175 	if (npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread,
2176 				       write_data, read_data, read_PEC,
2177 				       read_block)) {
2178 		time_left = wait_for_completion_timeout(&bus->cmd_complete,
2179 							timeout);
2180 
2181 		if (time_left == 0) {
2182 			if (bus->timeout_cnt < ULLONG_MAX)
2183 				bus->timeout_cnt++;
2184 			if (bus->master_or_slave == I2C_MASTER) {
2185 				i2c_recover_bus(adap);
2186 				bus->cmd_err = -EIO;
2187 				bus->state = I2C_IDLE;
2188 			}
2189 		}
2190 	}
2191 
2192 	/* if there was BER, check if need to recover the bus: */
2193 	if (bus->cmd_err == -EAGAIN)
2194 		bus->cmd_err = i2c_recover_bus(adap);
2195 
2196 	/*
2197 	 * After any type of error, check if LAST bit is still set,
2198 	 * due to a HW issue.
2199 	 * It cannot be cleared without resetting the module.
2200 	 */
2201 	else if (bus->cmd_err &&
2202 		 (NPCM_I2CRXF_CTL_LAST_PEC & ioread8(bus->reg + NPCM_I2CRXF_CTL)))
2203 		npcm_i2c_reset(bus);
2204 
2205 	/* after any xfer, successful or not, stall and EOB must be disabled */
2206 	npcm_i2c_stall_after_start(bus, false);
2207 	npcm_i2c_eob_int(bus, false);
2208 
2209 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2210 	/* reenable slave if it was enabled */
2211 	if (bus->slave)
2212 		iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN,
2213 			 bus->reg + NPCM_I2CADDR1);
2214 #else
2215 	npcm_i2c_int_enable(bus, false);
2216 #endif
2217 	return bus->cmd_err;
2218 }
2219 
2220 static u32 npcm_i2c_functionality(struct i2c_adapter *adap)
2221 {
2222 	return I2C_FUNC_I2C |
2223 	       I2C_FUNC_SMBUS_EMUL |
2224 	       I2C_FUNC_SMBUS_BLOCK_DATA |
2225 	       I2C_FUNC_SMBUS_PEC |
2226 	       I2C_FUNC_SLAVE;
2227 }
2228 
2229 static const struct i2c_adapter_quirks npcm_i2c_quirks = {
2230 	.max_read_len = 32768,
2231 	.max_write_len = 32768,
2232 	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
2233 };
2234 
2235 static const struct i2c_algorithm npcm_i2c_algo = {
2236 	.master_xfer = npcm_i2c_master_xfer,
2237 	.functionality = npcm_i2c_functionality,
2238 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2239 	.reg_slave	= npcm_i2c_reg_slave,
2240 	.unreg_slave	= npcm_i2c_unreg_slave,
2241 #endif
2242 };
2243 
2244 /* i2c debugfs directory: used to keep health monitor of i2c devices */
2245 static struct dentry *npcm_i2c_debugfs_dir;
2246 
2247 static void npcm_i2c_init_debugfs(struct platform_device *pdev,
2248 				  struct npcm_i2c *bus)
2249 {
2250 	struct dentry *d;
2251 
2252 	if (!npcm_i2c_debugfs_dir)
2253 		return;
2254 	d = debugfs_create_dir(dev_name(&pdev->dev), npcm_i2c_debugfs_dir);
2255 	if (IS_ERR_OR_NULL(d))
2256 		return;
2257 	debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt);
2258 	debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt);
2259 	debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt);
2260 	debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt);
2261 	debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt);
2262 	debugfs_create_u64("tx_complete_cnt", 0444, d, &bus->tx_complete_cnt);
2263 
2264 	bus->debugfs = d;
2265 }
2266 
2267 static int npcm_i2c_probe_bus(struct platform_device *pdev)
2268 {
2269 	struct device_node *np = pdev->dev.of_node;
2270 	static struct regmap *gcr_regmap;
2271 	struct i2c_adapter *adap;
2272 	struct npcm_i2c *bus;
2273 	struct clk *i2c_clk;
2274 	int irq;
2275 	int ret;
2276 
2277 	bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
2278 	if (!bus)
2279 		return -ENOMEM;
2280 
2281 	bus->dev = &pdev->dev;
2282 
2283 	bus->num = of_alias_get_id(pdev->dev.of_node, "i2c");
2284 	/* core clk must be acquired to calculate module timing settings */
2285 	i2c_clk = devm_clk_get(&pdev->dev, NULL);
2286 	if (IS_ERR(i2c_clk))
2287 		return PTR_ERR(i2c_clk);
2288 	bus->apb_clk = clk_get_rate(i2c_clk);
2289 
2290 	gcr_regmap = syscon_regmap_lookup_by_phandle(np, "nuvoton,sys-mgr");
2291 	if (IS_ERR(gcr_regmap))
2292 		gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
2293 
2294 	if (IS_ERR(gcr_regmap))
2295 		return PTR_ERR(gcr_regmap);
2296 	regmap_write(gcr_regmap, NPCM_I2CSEGCTL, NPCM_I2CSEGCTL_INIT_VAL);
2297 
2298 	bus->reg = devm_platform_ioremap_resource(pdev, 0);
2299 	if (IS_ERR(bus->reg))
2300 		return PTR_ERR(bus->reg);
2301 
2302 	spin_lock_init(&bus->lock);
2303 	init_completion(&bus->cmd_complete);
2304 
2305 	adap = &bus->adap;
2306 	adap->owner = THIS_MODULE;
2307 	adap->retries = 3;
2308 	adap->timeout = msecs_to_jiffies(35);
2309 	adap->algo = &npcm_i2c_algo;
2310 	adap->quirks = &npcm_i2c_quirks;
2311 	adap->algo_data = bus;
2312 	adap->dev.parent = &pdev->dev;
2313 	adap->dev.of_node = pdev->dev.of_node;
2314 	adap->nr = pdev->id;
2315 
2316 	irq = platform_get_irq(pdev, 0);
2317 	if (irq < 0)
2318 		return irq;
2319 
2320 	ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0,
2321 			       dev_name(bus->dev), bus);
2322 	if (ret)
2323 		return ret;
2324 
2325 	ret = __npcm_i2c_init(bus, pdev);
2326 	if (ret)
2327 		return ret;
2328 
2329 	npcm_i2c_recovery_init(adap);
2330 
2331 	i2c_set_adapdata(adap, bus);
2332 
2333 	snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d",
2334 		 bus->num);
2335 	ret = i2c_add_numbered_adapter(&bus->adap);
2336 	if (ret)
2337 		return ret;
2338 
2339 	platform_set_drvdata(pdev, bus);
2340 	npcm_i2c_init_debugfs(pdev, bus);
2341 	return 0;
2342 }
2343 
2344 static int npcm_i2c_remove_bus(struct platform_device *pdev)
2345 {
2346 	unsigned long lock_flags;
2347 	struct npcm_i2c *bus = platform_get_drvdata(pdev);
2348 
2349 	debugfs_remove_recursive(bus->debugfs);
2350 	spin_lock_irqsave(&bus->lock, lock_flags);
2351 	npcm_i2c_disable(bus);
2352 	spin_unlock_irqrestore(&bus->lock, lock_flags);
2353 	i2c_del_adapter(&bus->adap);
2354 	return 0;
2355 }
2356 
2357 static const struct of_device_id npcm_i2c_bus_of_table[] = {
2358 	{ .compatible = "nuvoton,npcm750-i2c", },
2359 	{}
2360 };
2361 MODULE_DEVICE_TABLE(of, npcm_i2c_bus_of_table);
2362 
2363 static struct platform_driver npcm_i2c_bus_driver = {
2364 	.probe = npcm_i2c_probe_bus,
2365 	.remove = npcm_i2c_remove_bus,
2366 	.driver = {
2367 		.name = "nuvoton-i2c",
2368 		.of_match_table = npcm_i2c_bus_of_table,
2369 	}
2370 };
2371 
2372 static int __init npcm_i2c_init(void)
2373 {
2374 	npcm_i2c_debugfs_dir = debugfs_create_dir("npcm_i2c", NULL);
2375 	platform_driver_register(&npcm_i2c_bus_driver);
2376 	return 0;
2377 }
2378 module_init(npcm_i2c_init);
2379 
2380 static void __exit npcm_i2c_exit(void)
2381 {
2382 	platform_driver_unregister(&npcm_i2c_bus_driver);
2383 	debugfs_remove_recursive(npcm_i2c_debugfs_dir);
2384 }
2385 module_exit(npcm_i2c_exit);
2386 
2387 MODULE_AUTHOR("Avi Fishman <avi.fishman@gmail.com>");
2388 MODULE_AUTHOR("Tali Perry <tali.perry@nuvoton.com>");
2389 MODULE_AUTHOR("Tyrone Ting <kfting@nuvoton.com>");
2390 MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");
2391 MODULE_LICENSE("GPL v2");
2392