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