1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Mellanox BlueField I2C bus driver
4 *
5 * Copyright (C) 2020 Mellanox Technologies, Ltd.
6 */
7
8 #include <linux/acpi.h>
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/i2c.h>
14 #include <linux/io.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/string.h>
22 #include <linux/string_choices.h>
23 #include <linux/units.h>
24
25 /* Defines what functionality is present. */
26 #define MLXBF_I2C_FUNC_SMBUS_BLOCK \
27 (I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
28
29 #define MLXBF_I2C_FUNC_SMBUS_DEFAULT \
30 (I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | \
31 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK | \
32 I2C_FUNC_SMBUS_PROC_CALL)
33
34 #define MLXBF_I2C_FUNC_ALL \
35 (MLXBF_I2C_FUNC_SMBUS_DEFAULT | MLXBF_I2C_FUNC_SMBUS_BLOCK | \
36 I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SLAVE)
37
38 /* Shared resources info in BlueField platforms. */
39
40 #define MLXBF_I2C_COALESCE_TYU_ADDR 0x02801300
41 #define MLXBF_I2C_COALESCE_TYU_SIZE 0x010
42
43 #define MLXBF_I2C_GPIO_TYU_ADDR 0x02802000
44 #define MLXBF_I2C_GPIO_TYU_SIZE 0x100
45
46 #define MLXBF_I2C_COREPLL_TYU_ADDR 0x02800358
47 #define MLXBF_I2C_COREPLL_TYU_SIZE 0x008
48
49 #define MLXBF_I2C_COREPLL_YU_ADDR 0x02800c30
50 #define MLXBF_I2C_COREPLL_YU_SIZE 0x00c
51
52 #define MLXBF_I2C_COREPLL_RSH_YU_ADDR 0x13409824
53 #define MLXBF_I2C_COREPLL_RSH_YU_SIZE 0x00c
54
55 #define MLXBF_I2C_SHARED_RES_MAX 3
56
57 /*
58 * Note that the following SMBus, CAUSE, GPIO and PLL register addresses
59 * refer to their respective offsets relative to the corresponding
60 * memory-mapped region whose addresses are specified in either the DT or
61 * the ACPI tables or above.
62 */
63
64 /*
65 * SMBus Master core clock frequency. Timing configurations are
66 * strongly dependent on the core clock frequency of the SMBus
67 * Master. Default value is set to 400MHz.
68 */
69 #define MLXBF_I2C_TYU_PLL_OUT_FREQ (400 * HZ_PER_MHZ)
70 /* Reference clock for Bluefield - 156 MHz. */
71 #define MLXBF_I2C_PLL_IN_FREQ 156250000ULL
72
73 /* Constant used to determine the PLL frequency. */
74 #define MLNXBF_I2C_COREPLL_CONST 16384ULL
75
76 /* PLL registers. */
77 #define MLXBF_I2C_CORE_PLL_REG1 0x4
78 #define MLXBF_I2C_CORE_PLL_REG2 0x8
79
80 /* OR cause register. */
81 #define MLXBF_I2C_CAUSE_OR_EVTEN0 0x14
82 #define MLXBF_I2C_CAUSE_OR_CLEAR 0x18
83
84 /* Arbiter Cause Register. */
85 #define MLXBF_I2C_CAUSE_ARBITER 0x1c
86
87 /*
88 * Cause Status flags. Note that those bits might be considered
89 * as interrupt enabled bits.
90 */
91
92 /* Transaction ended with STOP. */
93 #define MLXBF_I2C_CAUSE_TRANSACTION_ENDED BIT(0)
94 /* Master arbitration lost. */
95 #define MLXBF_I2C_CAUSE_M_ARBITRATION_LOST BIT(1)
96 /* Unexpected start detected. */
97 #define MLXBF_I2C_CAUSE_UNEXPECTED_START BIT(2)
98 /* Unexpected stop detected. */
99 #define MLXBF_I2C_CAUSE_UNEXPECTED_STOP BIT(3)
100 /* Wait for transfer continuation. */
101 #define MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA BIT(4)
102 /* Failed to generate STOP. */
103 #define MLXBF_I2C_CAUSE_PUT_STOP_FAILED BIT(5)
104 /* Failed to generate START. */
105 #define MLXBF_I2C_CAUSE_PUT_START_FAILED BIT(6)
106 /* Clock toggle completed. */
107 #define MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE BIT(7)
108 /* Transfer timeout occurred. */
109 #define MLXBF_I2C_CAUSE_M_FW_TIMEOUT BIT(8)
110 /* Master busy bit reset. */
111 #define MLXBF_I2C_CAUSE_M_GW_BUSY_FALL BIT(9)
112
113 #define MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK GENMASK(9, 0)
114
115 #define MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR \
116 (MLXBF_I2C_CAUSE_M_ARBITRATION_LOST | \
117 MLXBF_I2C_CAUSE_UNEXPECTED_START | \
118 MLXBF_I2C_CAUSE_UNEXPECTED_STOP | \
119 MLXBF_I2C_CAUSE_PUT_STOP_FAILED | \
120 MLXBF_I2C_CAUSE_PUT_START_FAILED | \
121 MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE | \
122 MLXBF_I2C_CAUSE_M_FW_TIMEOUT)
123
124 /*
125 * Slave cause status flags. Note that those bits might be considered
126 * as interrupt enabled bits.
127 */
128
129 /* Write transaction received successfully. */
130 #define MLXBF_I2C_CAUSE_WRITE_SUCCESS BIT(0)
131 /* Read transaction received, waiting for response. */
132 #define MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE BIT(13)
133 /* Slave busy bit reset. */
134 #define MLXBF_I2C_CAUSE_S_GW_BUSY_FALL BIT(18)
135
136 /* Cause coalesce registers. */
137 #define MLXBF_I2C_CAUSE_COALESCE_0 0x00
138
139 #define MLXBF_I2C_CAUSE_TYU_SLAVE_BIT 3
140 #define MLXBF_I2C_CAUSE_YU_SLAVE_BIT 1
141
142 /* Functional enable register. */
143 #define MLXBF_I2C_GPIO_0_FUNC_EN_0 0x28
144 /* Force OE enable register. */
145 #define MLXBF_I2C_GPIO_0_FORCE_OE_EN 0x30
146 /*
147 * Note that Smbus GWs are on GPIOs 30:25. Two pins are used to control
148 * SDA/SCL lines:
149 *
150 * SMBUS GW0 -> bits[26:25]
151 * SMBUS GW1 -> bits[28:27]
152 * SMBUS GW2 -> bits[30:29]
153 */
154 #define MLXBF_I2C_GPIO_SMBUS_GW_PINS(num) (25 + ((num) << 1))
155
156 /* Note that gw_id can be 0,1 or 2. */
157 #define MLXBF_I2C_GPIO_SMBUS_GW_MASK(num) \
158 (0xffffffff & (~(0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num))))
159
160 #define MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(num, val) \
161 ((val) & MLXBF_I2C_GPIO_SMBUS_GW_MASK(num))
162
163 #define MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(num, val) \
164 ((val) | (0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num)))
165
166 /*
167 * Defines SMBus operating frequency and core clock frequency.
168 * According to ADB files, default values are compliant to 100KHz SMBus
169 * @ 400MHz core clock. The driver should be able to calculate core
170 * frequency based on PLL parameters.
171 */
172 #define MLXBF_I2C_COREPLL_FREQ MLXBF_I2C_TYU_PLL_OUT_FREQ
173
174 /* Core PLL TYU configuration. */
175 #define MLXBF_I2C_COREPLL_CORE_F_TYU_MASK GENMASK(15, 3)
176 #define MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK GENMASK(19, 16)
177 #define MLXBF_I2C_COREPLL_CORE_R_TYU_MASK GENMASK(25, 20)
178
179 /* Core PLL YU configuration. */
180 #define MLXBF_I2C_COREPLL_CORE_F_YU_MASK GENMASK(25, 0)
181 #define MLXBF_I2C_COREPLL_CORE_OD_YU_MASK GENMASK(3, 0)
182 #define MLXBF_I2C_COREPLL_CORE_R_YU_MASK GENMASK(31, 26)
183
184 /* SMBus timing parameters. */
185 #define MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH 0x00
186 #define MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE 0x04
187 #define MLXBF_I2C_SMBUS_TIMER_THOLD 0x08
188 #define MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP 0x0c
189 #define MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA 0x10
190 #define MLXBF_I2C_SMBUS_THIGH_MAX_TBUF 0x14
191 #define MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT 0x18
192
193 #define MLXBF_I2C_SHIFT_0 0
194 #define MLXBF_I2C_SHIFT_8 8
195 #define MLXBF_I2C_SHIFT_16 16
196 #define MLXBF_I2C_SHIFT_24 24
197
198 #define MLXBF_I2C_MASK_8 GENMASK(7, 0)
199 #define MLXBF_I2C_MASK_16 GENMASK(15, 0)
200 #define MLXBF_I2C_MASK_32 GENMASK(31, 0)
201
202 #define MLXBF_I2C_MST_ADDR_OFFSET 0x200
203
204 /* SMBus Master GW. */
205 #define MLXBF_I2C_SMBUS_MASTER_GW 0x0
206 /* Number of bytes received and sent. */
207 #define MLXBF_I2C_YU_SMBUS_RS_BYTES 0x100
208 #define MLXBF_I2C_RSH_YU_SMBUS_RS_BYTES 0x10c
209 /* Packet error check (PEC) value. */
210 #define MLXBF_I2C_SMBUS_MASTER_PEC 0x104
211 /* Status bits (ACK/NACK/FW Timeout). */
212 #define MLXBF_I2C_SMBUS_MASTER_STATUS 0x108
213 /* SMbus Master Finite State Machine. */
214 #define MLXBF_I2C_YU_SMBUS_MASTER_FSM 0x110
215 #define MLXBF_I2C_RSH_YU_SMBUS_MASTER_FSM 0x100
216
217 /* SMBus master GW control bits offset in MLXBF_I2C_SMBUS_MASTER_GW[31:3]. */
218 #define MLXBF_I2C_MASTER_LOCK_BIT BIT(31) /* Lock bit. */
219 #define MLXBF_I2C_MASTER_BUSY_BIT BIT(30) /* Busy bit. */
220 #define MLXBF_I2C_MASTER_START_BIT BIT(29) /* Control start. */
221 #define MLXBF_I2C_MASTER_CTL_WRITE_BIT BIT(28) /* Control write phase. */
222 #define MLXBF_I2C_MASTER_CTL_READ_BIT BIT(19) /* Control read phase. */
223 #define MLXBF_I2C_MASTER_STOP_BIT BIT(3) /* Control stop. */
224
225 #define MLXBF_I2C_MASTER_ENABLE \
226 (MLXBF_I2C_MASTER_LOCK_BIT | MLXBF_I2C_MASTER_BUSY_BIT | \
227 MLXBF_I2C_MASTER_START_BIT)
228
229 #define MLXBF_I2C_MASTER_ENABLE_WRITE \
230 (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_WRITE_BIT)
231
232 #define MLXBF_I2C_MASTER_ENABLE_READ \
233 (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_READ_BIT)
234
235 #define MLXBF_I2C_MASTER_WRITE_SHIFT 21 /* Control write bytes */
236 #define MLXBF_I2C_MASTER_SEND_PEC_SHIFT 20 /* Send PEC byte when set to 1 */
237 #define MLXBF_I2C_MASTER_PARSE_EXP_SHIFT 11 /* Control parse expected bytes */
238 #define MLXBF_I2C_MASTER_SLV_ADDR_SHIFT 12 /* Slave address */
239 #define MLXBF_I2C_MASTER_READ_SHIFT 4 /* Control read bytes */
240
241 /* SMBus master GW Data descriptor. */
242 #define MLXBF_I2C_MASTER_DATA_DESC_ADDR 0x80
243 #define MLXBF_I2C_MASTER_DATA_DESC_SIZE 0x80 /* Size in bytes. */
244
245 /* Maximum bytes to read/write per SMBus transaction. */
246 #define MLXBF_I2C_MASTER_DATA_R_LENGTH MLXBF_I2C_MASTER_DATA_DESC_SIZE
247 #define MLXBF_I2C_MASTER_DATA_W_LENGTH (MLXBF_I2C_MASTER_DATA_DESC_SIZE - 1)
248
249 /* All bytes were transmitted. */
250 #define MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE BIT(0)
251 /* NACK received. */
252 #define MLXBF_I2C_SMBUS_STATUS_NACK_RCV BIT(1)
253 /* Slave's byte count >128 bytes. */
254 #define MLXBF_I2C_SMBUS_STATUS_READ_ERR BIT(2)
255 /* Timeout occurred. */
256 #define MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT BIT(3)
257
258 #define MLXBF_I2C_SMBUS_MASTER_STATUS_MASK GENMASK(3, 0)
259
260 #define MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR \
261 (MLXBF_I2C_SMBUS_STATUS_NACK_RCV | \
262 MLXBF_I2C_SMBUS_STATUS_READ_ERR | \
263 MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT)
264
265 #define MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK BIT(31)
266 #define MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK BIT(15)
267
268 #define MLXBF_I2C_SLV_ADDR_OFFSET 0x400
269
270 /* SMBus slave GW. */
271 #define MLXBF_I2C_SMBUS_SLAVE_GW 0x0
272 /* Number of bytes received and sent from/to master. */
273 #define MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES 0x100
274 /* Packet error check (PEC) value. */
275 #define MLXBF_I2C_SMBUS_SLAVE_PEC 0x104
276 /* SMBus slave Finite State Machine (FSM). */
277 #define MLXBF_I2C_SMBUS_SLAVE_FSM 0x110
278 /*
279 * Should be set when all raised causes handled, and cleared by HW on
280 * every new cause.
281 */
282 #define MLXBF_I2C_SMBUS_SLAVE_READY 0x12c
283
284 /* SMBus slave GW control bits offset in MLXBF_I2C_SMBUS_SLAVE_GW[31:19]. */
285 #define MLXBF_I2C_SLAVE_BUSY_BIT BIT(30) /* Busy bit. */
286 #define MLXBF_I2C_SLAVE_WRITE_BIT BIT(29) /* Control write enable. */
287
288 #define MLXBF_I2C_SLAVE_ENABLE \
289 (MLXBF_I2C_SLAVE_BUSY_BIT | MLXBF_I2C_SLAVE_WRITE_BIT)
290
291 #define MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT 22 /* Number of bytes to write. */
292 #define MLXBF_I2C_SLAVE_SEND_PEC_SHIFT 21 /* Send PEC byte shift. */
293
294 /* SMBus slave GW Data descriptor. */
295 #define MLXBF_I2C_SLAVE_DATA_DESC_ADDR 0x80
296 #define MLXBF_I2C_SLAVE_DATA_DESC_SIZE 0x80 /* Size in bytes. */
297
298 /* SMbus slave configuration registers. */
299 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG 0x114
300 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT 16
301 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT BIT(7)
302 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK GENMASK(6, 0)
303
304 /*
305 * Timeout is given in microsends. Note also that timeout handling is not
306 * exact.
307 */
308 #define MLXBF_I2C_SMBUS_TIMEOUT (300 * 1000) /* 300ms */
309 #define MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT (300 * 1000) /* 300ms */
310
311 /* Polling frequency in microseconds. */
312 #define MLXBF_I2C_POLL_FREQ_IN_USEC 200
313
314 #define MLXBF_I2C_SMBUS_OP_CNT_1 1
315 #define MLXBF_I2C_SMBUS_OP_CNT_2 2
316 #define MLXBF_I2C_SMBUS_OP_CNT_3 3
317 #define MLXBF_I2C_SMBUS_MAX_OP_CNT MLXBF_I2C_SMBUS_OP_CNT_3
318
319 /* Helper macro to define an I2C resource parameters. */
320 #define MLXBF_I2C_RES_PARAMS(addr, size, str) \
321 { \
322 .start = (addr), \
323 .end = (addr) + (size) - 1, \
324 .name = (str) \
325 }
326
327 enum {
328 MLXBF_I2C_F_READ = BIT(0),
329 MLXBF_I2C_F_WRITE = BIT(1),
330 MLXBF_I2C_F_NORESTART = BIT(3),
331 MLXBF_I2C_F_SMBUS_OPERATION = BIT(4),
332 MLXBF_I2C_F_SMBUS_BLOCK = BIT(5),
333 MLXBF_I2C_F_SMBUS_PEC = BIT(6),
334 MLXBF_I2C_F_SMBUS_PROCESS_CALL = BIT(7),
335 MLXBF_I2C_F_WRITE_WITHOUT_STOP = BIT(8),
336 };
337
338 /* Mellanox BlueField chip type. */
339 enum mlxbf_i2c_chip_type {
340 MLXBF_I2C_CHIP_TYPE_1, /* Mellanox BlueField-1 chip. */
341 MLXBF_I2C_CHIP_TYPE_2, /* Mellanox BlueField-2 chip. */
342 MLXBF_I2C_CHIP_TYPE_3 /* Mellanox BlueField-3 chip. */
343 };
344
345 /* List of chip resources that are being accessed by the driver. */
346 enum {
347 MLXBF_I2C_SMBUS_RES,
348 MLXBF_I2C_MST_CAUSE_RES,
349 MLXBF_I2C_SLV_CAUSE_RES,
350 MLXBF_I2C_COALESCE_RES,
351 MLXBF_I2C_SMBUS_TIMER_RES,
352 MLXBF_I2C_SMBUS_MST_RES,
353 MLXBF_I2C_SMBUS_SLV_RES,
354 MLXBF_I2C_COREPLL_RES,
355 MLXBF_I2C_GPIO_RES,
356 MLXBF_I2C_END_RES
357 };
358
359 /* Encapsulates timing parameters. */
360 struct mlxbf_i2c_timings {
361 u16 scl_high; /* Clock high period. */
362 u16 scl_low; /* Clock low period. */
363 u8 sda_rise; /* Data rise time. */
364 u8 sda_fall; /* Data fall time. */
365 u8 scl_rise; /* Clock rise time. */
366 u8 scl_fall; /* Clock fall time. */
367 u16 hold_start; /* Hold time after (REPEATED) START. */
368 u16 hold_data; /* Data hold time. */
369 u16 setup_start; /* REPEATED START condition setup time. */
370 u16 setup_stop; /* STOP condition setup time. */
371 u16 setup_data; /* Data setup time. */
372 u16 pad; /* Padding. */
373 u16 buf; /* Bus free time between STOP and START. */
374 u16 thigh_max; /* Thigh max. */
375 u32 timeout; /* Detect clock low timeout. */
376 };
377
378 struct mlxbf_i2c_smbus_operation {
379 u32 flags;
380 u32 length; /* Buffer length in bytes. */
381 u8 *buffer;
382 };
383
384 struct mlxbf_i2c_smbus_request {
385 u8 slave;
386 u8 operation_cnt;
387 struct mlxbf_i2c_smbus_operation operation[MLXBF_I2C_SMBUS_MAX_OP_CNT];
388 };
389
390 struct mlxbf_i2c_resource {
391 void __iomem *io;
392 struct resource *params;
393 struct mutex *lock; /* Mutex to protect mlxbf_i2c_resource. */
394 u8 type;
395 };
396
397 struct mlxbf_i2c_chip_info {
398 enum mlxbf_i2c_chip_type type;
399 /* Chip shared resources that are being used by the I2C controller. */
400 struct mlxbf_i2c_resource *shared_res[MLXBF_I2C_SHARED_RES_MAX];
401
402 /* Callback to calculate the core PLL frequency. */
403 u64 (*calculate_freq)(struct mlxbf_i2c_resource *corepll_res);
404
405 /* Registers' address offset */
406 u32 smbus_master_rs_bytes_off;
407 u32 smbus_master_fsm_off;
408 };
409
410 struct mlxbf_i2c_priv {
411 const struct mlxbf_i2c_chip_info *chip;
412 struct i2c_adapter adap;
413 struct mlxbf_i2c_resource *smbus;
414 struct mlxbf_i2c_resource *timer;
415 struct mlxbf_i2c_resource *mst;
416 struct mlxbf_i2c_resource *slv;
417 struct mlxbf_i2c_resource *mst_cause;
418 struct mlxbf_i2c_resource *slv_cause;
419 struct mlxbf_i2c_resource *coalesce;
420 u64 frequency; /* Core frequency in Hz. */
421 int bus; /* Physical bus identifier. */
422 int irq;
423 struct i2c_client *slave[MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT];
424 u32 resource_version;
425 };
426
427 /* Core PLL frequency. */
428 static u64 mlxbf_i2c_corepll_frequency;
429
430 static struct resource mlxbf_i2c_coalesce_tyu_params =
431 MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COALESCE_TYU_ADDR,
432 MLXBF_I2C_COALESCE_TYU_SIZE,
433 "COALESCE_MEM");
434 static struct resource mlxbf_i2c_corepll_tyu_params =
435 MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_TYU_ADDR,
436 MLXBF_I2C_COREPLL_TYU_SIZE,
437 "COREPLL_MEM");
438 static struct resource mlxbf_i2c_corepll_yu_params =
439 MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_YU_ADDR,
440 MLXBF_I2C_COREPLL_YU_SIZE,
441 "COREPLL_MEM");
442 static struct resource mlxbf_i2c_corepll_rsh_yu_params =
443 MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_RSH_YU_ADDR,
444 MLXBF_I2C_COREPLL_RSH_YU_SIZE,
445 "COREPLL_MEM");
446 static struct resource mlxbf_i2c_gpio_tyu_params =
447 MLXBF_I2C_RES_PARAMS(MLXBF_I2C_GPIO_TYU_ADDR,
448 MLXBF_I2C_GPIO_TYU_SIZE,
449 "GPIO_MEM");
450
451 static struct mutex mlxbf_i2c_coalesce_lock;
452 static struct mutex mlxbf_i2c_corepll_lock;
453 static struct mutex mlxbf_i2c_gpio_lock;
454
455 static struct mlxbf_i2c_resource mlxbf_i2c_coalesce_res[] = {
456 [MLXBF_I2C_CHIP_TYPE_1] = {
457 .params = &mlxbf_i2c_coalesce_tyu_params,
458 .lock = &mlxbf_i2c_coalesce_lock,
459 .type = MLXBF_I2C_COALESCE_RES
460 },
461 {}
462 };
463
464 static struct mlxbf_i2c_resource mlxbf_i2c_corepll_res[] = {
465 [MLXBF_I2C_CHIP_TYPE_1] = {
466 .params = &mlxbf_i2c_corepll_tyu_params,
467 .lock = &mlxbf_i2c_corepll_lock,
468 .type = MLXBF_I2C_COREPLL_RES
469 },
470 [MLXBF_I2C_CHIP_TYPE_2] = {
471 .params = &mlxbf_i2c_corepll_yu_params,
472 .lock = &mlxbf_i2c_corepll_lock,
473 .type = MLXBF_I2C_COREPLL_RES,
474 },
475 [MLXBF_I2C_CHIP_TYPE_3] = {
476 .params = &mlxbf_i2c_corepll_rsh_yu_params,
477 .lock = &mlxbf_i2c_corepll_lock,
478 .type = MLXBF_I2C_COREPLL_RES,
479 }
480 };
481
482 static struct mlxbf_i2c_resource mlxbf_i2c_gpio_res[] = {
483 [MLXBF_I2C_CHIP_TYPE_1] = {
484 .params = &mlxbf_i2c_gpio_tyu_params,
485 .lock = &mlxbf_i2c_gpio_lock,
486 .type = MLXBF_I2C_GPIO_RES
487 },
488 {}
489 };
490
491 static u8 mlxbf_i2c_bus_count;
492
493 static struct mutex mlxbf_i2c_bus_lock;
494
mlxbf_i2c_smbus_transaction_success(u32 master_status,u32 cause_status)495 static bool mlxbf_i2c_smbus_transaction_success(u32 master_status,
496 u32 cause_status)
497 {
498 /*
499 * When transaction ended with STOP, all bytes were transmitted,
500 * and no NACK received, then the transaction ended successfully.
501 * On the other hand, when the GW is configured with the stop bit
502 * de-asserted then the SMBus expects the following GW configuration
503 * for transfer continuation.
504 */
505 if ((cause_status & MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA) ||
506 ((cause_status & MLXBF_I2C_CAUSE_TRANSACTION_ENDED) &&
507 (master_status & MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE) &&
508 !(master_status & MLXBF_I2C_SMBUS_STATUS_NACK_RCV)))
509 return true;
510
511 return false;
512 }
513
514 /*
515 * Poll SMBus master status and return transaction status,
516 * i.e. whether succeeded or failed. I2C and SMBus fault codes
517 * are returned as negative numbers from most calls, with zero
518 * or some positive number indicating a non-fault return.
519 */
mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv * priv)520 static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
521 {
522 u32 master_status_bits;
523 u32 cause_status_bits;
524 u32 bits;
525
526 /*
527 * GW busy bit is raised by the driver and cleared by the HW
528 * when the transaction is completed. The busy bit is a good
529 * indicator of transaction status. So poll the busy bit, and
530 * then read the cause and master status bits to determine if
531 * errors occurred during the transaction.
532 */
533 readl_poll_timeout_atomic(priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW,
534 bits, !(bits & MLXBF_I2C_MASTER_BUSY_BIT),
535 MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
536
537 /* Read cause status bits. */
538 cause_status_bits = readl(priv->mst_cause->io +
539 MLXBF_I2C_CAUSE_ARBITER);
540 cause_status_bits &= MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK;
541
542 /*
543 * Parse both Cause and Master GW bits, then return transaction status.
544 */
545
546 master_status_bits = readl(priv->mst->io +
547 MLXBF_I2C_SMBUS_MASTER_STATUS);
548 master_status_bits &= MLXBF_I2C_SMBUS_MASTER_STATUS_MASK;
549
550 if (mlxbf_i2c_smbus_transaction_success(master_status_bits,
551 cause_status_bits))
552 return 0;
553
554 /*
555 * In case of timeout on GW busy, the ISR will clear busy bit but
556 * transaction ended bits cause will not be set so the transaction
557 * fails. Then, we must check Master GW status bits.
558 */
559 if ((master_status_bits & MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR) &&
560 (cause_status_bits & (MLXBF_I2C_CAUSE_TRANSACTION_ENDED |
561 MLXBF_I2C_CAUSE_M_GW_BUSY_FALL)))
562 return -EIO;
563
564 if (cause_status_bits & MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR)
565 return -EAGAIN;
566
567 return -ETIMEDOUT;
568 }
569
mlxbf_i2c_smbus_write_data(struct mlxbf_i2c_priv * priv,const u8 * data,u8 length,u32 addr,bool is_master)570 static void mlxbf_i2c_smbus_write_data(struct mlxbf_i2c_priv *priv,
571 const u8 *data, u8 length, u32 addr,
572 bool is_master)
573 {
574 u8 offset, aligned_length;
575 u32 data32;
576
577 aligned_length = round_up(length, 4);
578
579 /*
580 * Copy data bytes from 4-byte aligned source buffer.
581 * Data copied to the Master GW Data Descriptor MUST be shifted
582 * left so the data starts at the MSB of the descriptor registers
583 * as required by the underlying hardware. Enable byte swapping
584 * when writing data bytes to the 32 * 32-bit HW Data registers
585 * a.k.a Master GW Data Descriptor.
586 */
587 for (offset = 0; offset < aligned_length; offset += sizeof(u32)) {
588 data32 = *((u32 *)(data + offset));
589 if (is_master)
590 iowrite32be(data32, priv->mst->io + addr + offset);
591 else
592 iowrite32be(data32, priv->slv->io + addr + offset);
593 }
594 }
595
mlxbf_i2c_smbus_read_data(struct mlxbf_i2c_priv * priv,u8 * data,u8 length,u32 addr,bool is_master)596 static void mlxbf_i2c_smbus_read_data(struct mlxbf_i2c_priv *priv,
597 u8 *data, u8 length, u32 addr,
598 bool is_master)
599 {
600 u32 data32, mask;
601 u8 byte, offset;
602
603 mask = sizeof(u32) - 1;
604
605 /*
606 * Data bytes in the Master GW Data Descriptor are shifted left
607 * so the data starts at the MSB of the descriptor registers as
608 * set by the underlying hardware. Enable byte swapping while
609 * reading data bytes from the 32 * 32-bit HW Data registers
610 * a.k.a Master GW Data Descriptor.
611 */
612
613 for (offset = 0; offset < (length & ~mask); offset += sizeof(u32)) {
614 if (is_master)
615 data32 = ioread32be(priv->mst->io + addr + offset);
616 else
617 data32 = ioread32be(priv->slv->io + addr + offset);
618 *((u32 *)(data + offset)) = data32;
619 }
620
621 if (!(length & mask))
622 return;
623
624 if (is_master)
625 data32 = ioread32be(priv->mst->io + addr + offset);
626 else
627 data32 = ioread32be(priv->slv->io + addr + offset);
628
629 for (byte = 0; byte < (length & mask); byte++) {
630 data[offset + byte] = data32 & GENMASK(7, 0);
631 data32 = ror32(data32, MLXBF_I2C_SHIFT_8);
632 }
633 }
634
mlxbf_i2c_smbus_enable(struct mlxbf_i2c_priv * priv,u8 slave,u8 len,u8 block_en,u8 pec_en,bool read,bool stop)635 static int mlxbf_i2c_smbus_enable(struct mlxbf_i2c_priv *priv, u8 slave,
636 u8 len, u8 block_en, u8 pec_en, bool read,
637 bool stop)
638 {
639 u32 command = 0;
640
641 /* Set Master GW control word. */
642 if (stop)
643 command |= MLXBF_I2C_MASTER_STOP_BIT;
644 if (read) {
645 command |= MLXBF_I2C_MASTER_ENABLE_READ;
646 command |= rol32(len, MLXBF_I2C_MASTER_READ_SHIFT);
647 } else {
648 command |= MLXBF_I2C_MASTER_ENABLE_WRITE;
649 command |= rol32(len, MLXBF_I2C_MASTER_WRITE_SHIFT);
650 }
651 command |= rol32(slave, MLXBF_I2C_MASTER_SLV_ADDR_SHIFT);
652 command |= rol32(block_en, MLXBF_I2C_MASTER_PARSE_EXP_SHIFT);
653 command |= rol32(pec_en, MLXBF_I2C_MASTER_SEND_PEC_SHIFT);
654
655 /* Clear status bits. */
656 writel(0x0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_STATUS);
657 /* Set the cause data. */
658 writel(~0x0, priv->mst_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
659 /* Zero PEC byte. */
660 writel(0x0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_PEC);
661 /* Zero byte count. */
662 writel(0x0, priv->mst->io + priv->chip->smbus_master_rs_bytes_off);
663
664 /* GW activation. */
665 writel(command, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
666
667 /*
668 * Poll master status and check status bits. An ACK is sent when
669 * completing writing data to the bus (Master 'byte_count_done' bit
670 * is set to 1).
671 */
672 return mlxbf_i2c_smbus_check_status(priv);
673 }
674
675 static int
mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv * priv,struct mlxbf_i2c_smbus_request * request)676 mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
677 struct mlxbf_i2c_smbus_request *request)
678 {
679 u8 data_desc[MLXBF_I2C_MASTER_DATA_DESC_SIZE] = { 0 };
680 u8 op_idx, data_idx, data_len, write_len, read_len;
681 struct mlxbf_i2c_smbus_operation *operation;
682 u8 read_en, write_en, block_en, pec_en;
683 bool stop_after_write = true;
684 u8 slave, addr;
685 u8 *read_buf;
686 u32 flags;
687 u32 bits;
688 int ret;
689
690 if (request->operation_cnt > MLXBF_I2C_SMBUS_MAX_OP_CNT)
691 return -EINVAL;
692
693 read_buf = NULL;
694 data_idx = 0;
695 read_en = 0;
696 write_en = 0;
697 write_len = 0;
698 read_len = 0;
699 block_en = 0;
700 pec_en = 0;
701 slave = request->slave & GENMASK(6, 0);
702 addr = slave << 1;
703
704 /*
705 * Try to acquire the smbus gw lock before any reads of the GW register since
706 * a read sets the lock.
707 */
708 ret = readl_poll_timeout_atomic(priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW,
709 bits, !(bits & MLXBF_I2C_MASTER_LOCK_BIT),
710 MLXBF_I2C_POLL_FREQ_IN_USEC,
711 MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT);
712 if (WARN_ON(ret))
713 return -EBUSY;
714
715 /*
716 * SW must make sure that the SMBus Master GW is idle before starting
717 * a transaction. Accordingly, this call polls the Master FSM stop bit;
718 * it returns -ETIMEDOUT when the bit is asserted, 0 if not.
719 */
720 ret = readl_poll_timeout_atomic(priv->mst->io + priv->chip->smbus_master_fsm_off,
721 bits, !(bits & MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK),
722 MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
723 if (WARN_ON(ret)) {
724 ret = -EBUSY;
725 goto out_unlock;
726 }
727
728 /* Set first byte. */
729 data_desc[data_idx++] = addr;
730
731 for (op_idx = 0; op_idx < request->operation_cnt; op_idx++) {
732 operation = &request->operation[op_idx];
733 flags = operation->flags;
734
735 /*
736 * Note that read and write operations might be handled by a
737 * single command. If the MLXBF_I2C_F_SMBUS_OPERATION is set
738 * then write command byte and set the optional SMBus specific
739 * bits such as block_en and pec_en. These bits MUST be
740 * submitted by the first operation only.
741 */
742 if (op_idx == 0 && flags & MLXBF_I2C_F_SMBUS_OPERATION) {
743 block_en = flags & MLXBF_I2C_F_SMBUS_BLOCK;
744 pec_en = flags & MLXBF_I2C_F_SMBUS_PEC;
745 }
746
747 if (flags & MLXBF_I2C_F_WRITE) {
748 write_en = 1;
749 write_len += operation->length;
750 if (data_idx + operation->length >
751 MLXBF_I2C_MASTER_DATA_DESC_SIZE) {
752 ret = -ENOBUFS;
753 goto out_unlock;
754 }
755 memcpy(data_desc + data_idx,
756 operation->buffer, operation->length);
757 data_idx += operation->length;
758
759 /*
760 * The stop condition can be skipped when writing on the bus
761 * to implement a repeated start condition on the next read
762 * as required for several SMBus and I2C operations.
763 */
764 if (flags & MLXBF_I2C_F_WRITE_WITHOUT_STOP)
765 stop_after_write = false;
766 }
767
768 /*
769 * We assume that read operations are performed only once per
770 * SMBus transaction. *TBD* protect this statement so it won't
771 * be executed twice? or return an error if we try to read more
772 * than once?
773 */
774 if (flags & MLXBF_I2C_F_READ) {
775 read_en = 1;
776 /* Subtract 1 as required by HW. */
777 read_len = operation->length - 1;
778 read_buf = operation->buffer;
779 }
780 }
781
782 /* Set Master GW data descriptor. */
783 data_len = write_len + 1; /* Add one byte of the slave address. */
784 /*
785 * Note that data_len cannot be 0. Indeed, the slave address byte
786 * must be written to the data registers.
787 */
788 mlxbf_i2c_smbus_write_data(priv, (const u8 *)data_desc, data_len,
789 MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
790
791 if (write_en) {
792 ret = mlxbf_i2c_smbus_enable(priv, slave, write_len, block_en,
793 pec_en, 0, stop_after_write);
794 if (ret)
795 goto out_unlock;
796 }
797
798 if (read_en) {
799 /* Write slave address to Master GW data descriptor. */
800 mlxbf_i2c_smbus_write_data(priv, (const u8 *)&addr, 1,
801 MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
802 ret = mlxbf_i2c_smbus_enable(priv, slave, read_len, block_en,
803 pec_en, 1, true);
804 if (!ret) {
805 /* Get Master GW data descriptor. */
806 mlxbf_i2c_smbus_read_data(priv, data_desc, read_len + 1,
807 MLXBF_I2C_MASTER_DATA_DESC_ADDR, true);
808
809 /* Get data from Master GW data descriptor. */
810 memcpy(read_buf, data_desc, read_len + 1);
811 }
812
813 /*
814 * After a read operation the SMBus FSM ps (present state)
815 * needs to be 'manually' reset. This should be removed in
816 * next tag integration.
817 */
818 writel(MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK,
819 priv->mst->io + priv->chip->smbus_master_fsm_off);
820 }
821
822 out_unlock:
823 /* Clear the gw to clear the lock */
824 writel(0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
825
826 return ret;
827 }
828
829 /* I2C SMBus protocols. */
830
831 static void
mlxbf_i2c_smbus_quick_command(struct mlxbf_i2c_smbus_request * request,u8 read)832 mlxbf_i2c_smbus_quick_command(struct mlxbf_i2c_smbus_request *request,
833 u8 read)
834 {
835 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
836
837 request->operation[0].length = 0;
838 request->operation[0].flags = MLXBF_I2C_F_WRITE;
839 request->operation[0].flags |= read ? MLXBF_I2C_F_READ : 0;
840 }
841
mlxbf_i2c_smbus_byte_func(struct mlxbf_i2c_smbus_request * request,u8 * data,bool read,bool pec_check)842 static void mlxbf_i2c_smbus_byte_func(struct mlxbf_i2c_smbus_request *request,
843 u8 *data, bool read, bool pec_check)
844 {
845 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
846
847 request->operation[0].length = 1;
848 request->operation[0].length += pec_check;
849
850 request->operation[0].flags = MLXBF_I2C_F_SMBUS_OPERATION;
851 request->operation[0].flags |= read ?
852 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
853 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
854
855 request->operation[0].buffer = data;
856 }
857
858 static void
mlxbf_i2c_smbus_data_byte_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,bool read,bool pec_check)859 mlxbf_i2c_smbus_data_byte_func(struct mlxbf_i2c_smbus_request *request,
860 u8 *command, u8 *data, bool read, bool pec_check)
861 {
862 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
863
864 request->operation[0].length = 1;
865 request->operation[0].flags =
866 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
867 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
868 request->operation[0].buffer = command;
869
870 request->operation[1].length = 1;
871 request->operation[1].length += pec_check;
872 request->operation[1].flags = read ?
873 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
874 request->operation[1].buffer = data;
875 }
876
877 static void
mlxbf_i2c_smbus_data_word_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,bool read,bool pec_check)878 mlxbf_i2c_smbus_data_word_func(struct mlxbf_i2c_smbus_request *request,
879 u8 *command, u8 *data, bool read, bool pec_check)
880 {
881 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
882
883 request->operation[0].length = 1;
884 request->operation[0].flags =
885 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
886 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
887 request->operation[0].buffer = command;
888
889 request->operation[1].length = 2;
890 request->operation[1].length += pec_check;
891 request->operation[1].flags = read ?
892 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
893 request->operation[1].buffer = data;
894 }
895
896 static void
mlxbf_i2c_smbus_i2c_block_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,u8 * data_len,bool read,bool pec_check)897 mlxbf_i2c_smbus_i2c_block_func(struct mlxbf_i2c_smbus_request *request,
898 u8 *command, u8 *data, u8 *data_len, bool read,
899 bool pec_check)
900 {
901 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
902
903 request->operation[0].length = 1;
904 request->operation[0].flags =
905 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
906 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
907 request->operation[0].buffer = command;
908
909 if (read)
910 request->operation[0].flags |= MLXBF_I2C_F_WRITE_WITHOUT_STOP;
911
912 /*
913 * As specified in the standard, the max number of bytes to read/write
914 * per block operation is 32 bytes. In Golan code, the controller can
915 * read up to 128 bytes and write up to 127 bytes.
916 */
917 request->operation[1].length =
918 (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
919 I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
920 request->operation[1].flags = read ?
921 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
922 /*
923 * Skip the first data byte, which corresponds to the number of bytes
924 * to read/write.
925 */
926 request->operation[1].buffer = data + 1;
927
928 *data_len = request->operation[1].length;
929
930 /* Set the number of byte to read. This will be used by userspace. */
931 if (read)
932 data[0] = *data_len;
933 }
934
mlxbf_i2c_smbus_block_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,u8 * data_len,bool read,bool pec_check)935 static void mlxbf_i2c_smbus_block_func(struct mlxbf_i2c_smbus_request *request,
936 u8 *command, u8 *data, u8 *data_len,
937 bool read, bool pec_check)
938 {
939 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
940
941 request->operation[0].length = 1;
942 request->operation[0].flags =
943 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
944 request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
945 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
946 request->operation[0].buffer = command;
947
948 request->operation[1].length =
949 (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
950 I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
951 request->operation[1].flags = read ?
952 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
953 request->operation[1].buffer = data + 1;
954
955 *data_len = request->operation[1].length;
956
957 /* Set the number of bytes to read. This will be used by userspace. */
958 if (read)
959 data[0] = *data_len;
960 }
961
962 static void
mlxbf_i2c_smbus_process_call_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,bool pec_check)963 mlxbf_i2c_smbus_process_call_func(struct mlxbf_i2c_smbus_request *request,
964 u8 *command, u8 *data, bool pec_check)
965 {
966 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
967
968 request->operation[0].length = 1;
969 request->operation[0].flags =
970 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
971 request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
972 request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
973 request->operation[0].buffer = command;
974
975 request->operation[1].length = 2;
976 request->operation[1].flags = MLXBF_I2C_F_WRITE;
977 request->operation[1].buffer = data;
978
979 request->operation[2].length = 3;
980 request->operation[2].flags = MLXBF_I2C_F_READ;
981 request->operation[2].buffer = data;
982 }
983
984 static void
mlxbf_i2c_smbus_blk_process_call_func(struct mlxbf_i2c_smbus_request * request,u8 * command,u8 * data,u8 * data_len,bool pec_check)985 mlxbf_i2c_smbus_blk_process_call_func(struct mlxbf_i2c_smbus_request *request,
986 u8 *command, u8 *data, u8 *data_len,
987 bool pec_check)
988 {
989 u32 length;
990
991 request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
992
993 request->operation[0].length = 1;
994 request->operation[0].flags =
995 MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
996 request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
997 request->operation[0].flags |= (pec_check) ? MLXBF_I2C_F_SMBUS_PEC : 0;
998 request->operation[0].buffer = command;
999
1000 length = (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
1001 I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
1002
1003 request->operation[1].length = length - pec_check;
1004 request->operation[1].flags = MLXBF_I2C_F_WRITE;
1005 request->operation[1].buffer = data;
1006
1007 request->operation[2].length = length;
1008 request->operation[2].flags = MLXBF_I2C_F_READ;
1009 request->operation[2].buffer = data;
1010
1011 *data_len = length; /* including PEC byte. */
1012 }
1013
1014 /* Initialization functions. */
1015
mlxbf_i2c_has_chip_type(struct mlxbf_i2c_priv * priv,u8 type)1016 static bool mlxbf_i2c_has_chip_type(struct mlxbf_i2c_priv *priv, u8 type)
1017 {
1018 return priv->chip->type == type;
1019 }
1020
1021 static struct mlxbf_i2c_resource *
mlxbf_i2c_get_shared_resource(struct mlxbf_i2c_priv * priv,u8 type)1022 mlxbf_i2c_get_shared_resource(struct mlxbf_i2c_priv *priv, u8 type)
1023 {
1024 const struct mlxbf_i2c_chip_info *chip = priv->chip;
1025 struct mlxbf_i2c_resource *res;
1026 u8 res_idx = 0;
1027
1028 for (res_idx = 0; res_idx < MLXBF_I2C_SHARED_RES_MAX; res_idx++) {
1029 res = chip->shared_res[res_idx];
1030 if (res && res->type == type)
1031 return res;
1032 }
1033
1034 return NULL;
1035 }
1036
mlxbf_i2c_init_resource(struct platform_device * pdev,struct mlxbf_i2c_resource ** res,u8 type)1037 static int mlxbf_i2c_init_resource(struct platform_device *pdev,
1038 struct mlxbf_i2c_resource **res,
1039 u8 type)
1040 {
1041 struct mlxbf_i2c_resource *tmp_res;
1042 struct device *dev = &pdev->dev;
1043
1044 if (!res || *res || type >= MLXBF_I2C_END_RES)
1045 return -EINVAL;
1046
1047 tmp_res = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource),
1048 GFP_KERNEL);
1049 if (!tmp_res)
1050 return -ENOMEM;
1051
1052 tmp_res->io = devm_platform_get_and_ioremap_resource(pdev, type, &tmp_res->params);
1053 if (IS_ERR(tmp_res->io)) {
1054 int ret = PTR_ERR(tmp_res->io);
1055
1056 devm_kfree(dev, tmp_res);
1057 return ret;
1058 }
1059
1060 tmp_res->type = type;
1061
1062 *res = tmp_res;
1063
1064 return 0;
1065 }
1066
mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv * priv,u64 nanoseconds,bool minimum)1067 static u32 mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv *priv, u64 nanoseconds,
1068 bool minimum)
1069 {
1070 u64 frequency;
1071 u32 ticks;
1072
1073 /*
1074 * Compute ticks as follow:
1075 *
1076 * Ticks
1077 * Time = --------- x 10^9 => Ticks = Time x Frequency x 10^-9
1078 * Frequency
1079 */
1080 frequency = priv->frequency;
1081 ticks = div_u64(nanoseconds * frequency, HZ_PER_GHZ);
1082 /*
1083 * The number of ticks is rounded down and if minimum is equal to 1
1084 * then add one tick.
1085 */
1086 if (minimum)
1087 ticks++;
1088
1089 return ticks;
1090 }
1091
mlxbf_i2c_set_timer(struct mlxbf_i2c_priv * priv,u64 nsec,bool opt,u32 mask,u8 shift)1092 static u32 mlxbf_i2c_set_timer(struct mlxbf_i2c_priv *priv, u64 nsec, bool opt,
1093 u32 mask, u8 shift)
1094 {
1095 u32 val = (mlxbf_i2c_get_ticks(priv, nsec, opt) & mask) << shift;
1096
1097 return val;
1098 }
1099
mlxbf_i2c_set_timings(struct mlxbf_i2c_priv * priv,const struct mlxbf_i2c_timings * timings)1100 static void mlxbf_i2c_set_timings(struct mlxbf_i2c_priv *priv,
1101 const struct mlxbf_i2c_timings *timings)
1102 {
1103 u32 timer;
1104
1105 timer = mlxbf_i2c_set_timer(priv, timings->scl_high,
1106 false, MLXBF_I2C_MASK_16,
1107 MLXBF_I2C_SHIFT_0);
1108 timer |= mlxbf_i2c_set_timer(priv, timings->scl_low,
1109 false, MLXBF_I2C_MASK_16,
1110 MLXBF_I2C_SHIFT_16);
1111 writel(timer, priv->timer->io +
1112 MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH);
1113
1114 timer = mlxbf_i2c_set_timer(priv, timings->sda_rise, false,
1115 MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_0);
1116 timer |= mlxbf_i2c_set_timer(priv, timings->sda_fall, false,
1117 MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_8);
1118 timer |= mlxbf_i2c_set_timer(priv, timings->scl_rise, false,
1119 MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_16);
1120 timer |= mlxbf_i2c_set_timer(priv, timings->scl_fall, false,
1121 MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_24);
1122 writel(timer, priv->timer->io +
1123 MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE);
1124
1125 timer = mlxbf_i2c_set_timer(priv, timings->hold_start, true,
1126 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1127 timer |= mlxbf_i2c_set_timer(priv, timings->hold_data, true,
1128 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1129 writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_THOLD);
1130
1131 timer = mlxbf_i2c_set_timer(priv, timings->setup_start, true,
1132 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1133 timer |= mlxbf_i2c_set_timer(priv, timings->setup_stop, true,
1134 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1135 writel(timer, priv->timer->io +
1136 MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP);
1137
1138 timer = mlxbf_i2c_set_timer(priv, timings->setup_data, true,
1139 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1140 writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA);
1141
1142 timer = mlxbf_i2c_set_timer(priv, timings->buf, false,
1143 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1144 timer |= mlxbf_i2c_set_timer(priv, timings->thigh_max, false,
1145 MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1146 writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_THIGH_MAX_TBUF);
1147
1148 timer = mlxbf_i2c_set_timer(priv, timings->timeout, false,
1149 MLXBF_I2C_MASK_32, MLXBF_I2C_SHIFT_0);
1150 writel(timer, priv->timer->io + MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT);
1151 }
1152
1153 enum mlxbf_i2c_timings_config {
1154 MLXBF_I2C_TIMING_CONFIG_100KHZ,
1155 MLXBF_I2C_TIMING_CONFIG_400KHZ,
1156 MLXBF_I2C_TIMING_CONFIG_1000KHZ,
1157 };
1158
1159 /* Timing values are in nanoseconds */
1160 static const struct mlxbf_i2c_timings mlxbf_i2c_timings[] = {
1161 [MLXBF_I2C_TIMING_CONFIG_100KHZ] = {
1162 .scl_high = 4810,
1163 .scl_low = 5000,
1164 .hold_start = 4000,
1165 .setup_start = 4800,
1166 .setup_stop = 4000,
1167 .setup_data = 250,
1168 .sda_rise = 50,
1169 .sda_fall = 50,
1170 .scl_rise = 50,
1171 .scl_fall = 50,
1172 .hold_data = 300,
1173 .buf = 20000,
1174 .thigh_max = 50000,
1175 .timeout = 35000000
1176 },
1177 [MLXBF_I2C_TIMING_CONFIG_400KHZ] = {
1178 .scl_high = 1011,
1179 .scl_low = 1300,
1180 .hold_start = 600,
1181 .setup_start = 700,
1182 .setup_stop = 600,
1183 .setup_data = 100,
1184 .sda_rise = 50,
1185 .sda_fall = 50,
1186 .scl_rise = 50,
1187 .scl_fall = 50,
1188 .hold_data = 300,
1189 .buf = 20000,
1190 .thigh_max = 50000,
1191 .timeout = 35000000
1192 },
1193 [MLXBF_I2C_TIMING_CONFIG_1000KHZ] = {
1194 .scl_high = 383,
1195 .scl_low = 460,
1196 .hold_start = 600,
1197 .setup_start = 260,
1198 .setup_stop = 260,
1199 .setup_data = 50,
1200 .sda_rise = 50,
1201 .sda_fall = 50,
1202 .scl_rise = 50,
1203 .scl_fall = 50,
1204 .hold_data = 300,
1205 .buf = 500,
1206 .thigh_max = 50000,
1207 .timeout = 35000000
1208 }
1209 };
1210
mlxbf_i2c_init_timings(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1211 static int mlxbf_i2c_init_timings(struct platform_device *pdev,
1212 struct mlxbf_i2c_priv *priv)
1213 {
1214 enum mlxbf_i2c_timings_config config_idx;
1215 struct device *dev = &pdev->dev;
1216 u32 config_khz;
1217
1218 int ret;
1219
1220 ret = device_property_read_u32(dev, "clock-frequency", &config_khz);
1221 if (ret < 0)
1222 config_khz = I2C_MAX_STANDARD_MODE_FREQ;
1223
1224 switch (config_khz) {
1225 default:
1226 /* Default settings is 100 KHz. */
1227 pr_warn("Illegal value %d: defaulting to 100 KHz\n",
1228 config_khz);
1229 fallthrough;
1230 case I2C_MAX_STANDARD_MODE_FREQ:
1231 config_idx = MLXBF_I2C_TIMING_CONFIG_100KHZ;
1232 break;
1233
1234 case I2C_MAX_FAST_MODE_FREQ:
1235 config_idx = MLXBF_I2C_TIMING_CONFIG_400KHZ;
1236 break;
1237
1238 case I2C_MAX_FAST_MODE_PLUS_FREQ:
1239 config_idx = MLXBF_I2C_TIMING_CONFIG_1000KHZ;
1240 break;
1241 }
1242
1243 mlxbf_i2c_set_timings(priv, &mlxbf_i2c_timings[config_idx]);
1244
1245 return 0;
1246 }
1247
mlxbf_i2c_get_gpio(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1248 static int mlxbf_i2c_get_gpio(struct platform_device *pdev,
1249 struct mlxbf_i2c_priv *priv)
1250 {
1251 struct mlxbf_i2c_resource *gpio_res;
1252 struct device *dev = &pdev->dev;
1253 struct resource *params;
1254 resource_size_t size;
1255
1256 gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1257 if (!gpio_res)
1258 return -EPERM;
1259
1260 /*
1261 * The GPIO region in TYU space is shared among I2C busses.
1262 * This function MUST be serialized to avoid racing when
1263 * claiming the memory region and/or setting up the GPIO.
1264 */
1265 lockdep_assert_held(gpio_res->lock);
1266
1267 /* Check whether the memory map exist. */
1268 if (gpio_res->io)
1269 return 0;
1270
1271 params = gpio_res->params;
1272 size = resource_size(params);
1273
1274 if (!devm_request_mem_region(dev, params->start, size, params->name))
1275 return -EFAULT;
1276
1277 gpio_res->io = devm_ioremap(dev, params->start, size);
1278 if (!gpio_res->io) {
1279 devm_release_mem_region(dev, params->start, size);
1280 return -ENOMEM;
1281 }
1282
1283 return 0;
1284 }
1285
mlxbf_i2c_release_gpio(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1286 static int mlxbf_i2c_release_gpio(struct platform_device *pdev,
1287 struct mlxbf_i2c_priv *priv)
1288 {
1289 struct mlxbf_i2c_resource *gpio_res;
1290 struct device *dev = &pdev->dev;
1291 struct resource *params;
1292
1293 gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1294 if (!gpio_res)
1295 return 0;
1296
1297 mutex_lock(gpio_res->lock);
1298
1299 if (gpio_res->io) {
1300 /* Release the GPIO resource. */
1301 params = gpio_res->params;
1302 devm_iounmap(dev, gpio_res->io);
1303 devm_release_mem_region(dev, params->start,
1304 resource_size(params));
1305 }
1306
1307 mutex_unlock(gpio_res->lock);
1308
1309 return 0;
1310 }
1311
mlxbf_i2c_get_corepll(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1312 static int mlxbf_i2c_get_corepll(struct platform_device *pdev,
1313 struct mlxbf_i2c_priv *priv)
1314 {
1315 struct mlxbf_i2c_resource *corepll_res;
1316 struct device *dev = &pdev->dev;
1317 struct resource *params;
1318 resource_size_t size;
1319
1320 corepll_res = mlxbf_i2c_get_shared_resource(priv,
1321 MLXBF_I2C_COREPLL_RES);
1322 if (!corepll_res)
1323 return -EPERM;
1324
1325 /*
1326 * The COREPLL region in TYU space is shared among I2C busses.
1327 * This function MUST be serialized to avoid racing when
1328 * claiming the memory region.
1329 */
1330 lockdep_assert_held(corepll_res->lock);
1331
1332 /* Check whether the memory map exist. */
1333 if (corepll_res->io)
1334 return 0;
1335
1336 params = corepll_res->params;
1337 size = resource_size(params);
1338
1339 if (!devm_request_mem_region(dev, params->start, size, params->name))
1340 return -EFAULT;
1341
1342 corepll_res->io = devm_ioremap(dev, params->start, size);
1343 if (!corepll_res->io) {
1344 devm_release_mem_region(dev, params->start, size);
1345 return -ENOMEM;
1346 }
1347
1348 return 0;
1349 }
1350
mlxbf_i2c_release_corepll(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1351 static int mlxbf_i2c_release_corepll(struct platform_device *pdev,
1352 struct mlxbf_i2c_priv *priv)
1353 {
1354 struct mlxbf_i2c_resource *corepll_res;
1355 struct device *dev = &pdev->dev;
1356 struct resource *params;
1357
1358 corepll_res = mlxbf_i2c_get_shared_resource(priv,
1359 MLXBF_I2C_COREPLL_RES);
1360
1361 mutex_lock(corepll_res->lock);
1362
1363 if (corepll_res->io) {
1364 /* Release the CorePLL resource. */
1365 params = corepll_res->params;
1366 devm_iounmap(dev, corepll_res->io);
1367 devm_release_mem_region(dev, params->start,
1368 resource_size(params));
1369 }
1370
1371 mutex_unlock(corepll_res->lock);
1372
1373 return 0;
1374 }
1375
mlxbf_i2c_init_master(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1376 static int mlxbf_i2c_init_master(struct platform_device *pdev,
1377 struct mlxbf_i2c_priv *priv)
1378 {
1379 struct mlxbf_i2c_resource *gpio_res;
1380 struct device *dev = &pdev->dev;
1381 u32 config_reg;
1382 int ret;
1383
1384 /* This configuration is only needed for BlueField 1. */
1385 if (!mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1))
1386 return 0;
1387
1388 gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1389 if (!gpio_res)
1390 return -EPERM;
1391
1392 /*
1393 * The GPIO region in TYU space is shared among I2C busses.
1394 * This function MUST be serialized to avoid racing when
1395 * claiming the memory region and/or setting up the GPIO.
1396 */
1397
1398 mutex_lock(gpio_res->lock);
1399
1400 ret = mlxbf_i2c_get_gpio(pdev, priv);
1401 if (ret < 0) {
1402 dev_err(dev, "Failed to get gpio resource");
1403 mutex_unlock(gpio_res->lock);
1404 return ret;
1405 }
1406
1407 /*
1408 * TYU - Configuration for GPIO pins. Those pins must be asserted in
1409 * MLXBF_I2C_GPIO_0_FUNC_EN_0, i.e. GPIO 0 is controlled by HW, and must
1410 * be reset in MLXBF_I2C_GPIO_0_FORCE_OE_EN, i.e. GPIO_OE will be driven
1411 * instead of HW_OE.
1412 * For now, we do not reset the GPIO state when the driver is removed.
1413 * First, it is not necessary to disable the bus since we are using
1414 * the same busses. Then, some busses might be shared among Linux and
1415 * platform firmware; disabling the bus might compromise the system
1416 * functionality.
1417 */
1418 config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
1419 config_reg = MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(priv->bus,
1420 config_reg);
1421 writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
1422
1423 config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
1424 config_reg = MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(priv->bus,
1425 config_reg);
1426 writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
1427
1428 mutex_unlock(gpio_res->lock);
1429
1430 return 0;
1431 }
1432
mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource * corepll_res)1433 static u64 mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res)
1434 {
1435 u64 core_frequency;
1436 u8 core_od, core_r;
1437 u32 corepll_val;
1438 u16 core_f;
1439
1440 corepll_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
1441
1442 /* Get Core PLL configuration bits. */
1443 core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_TYU_MASK, corepll_val);
1444 core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK, corepll_val);
1445 core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_TYU_MASK, corepll_val);
1446
1447 /*
1448 * Compute PLL output frequency as follow:
1449 *
1450 * CORE_F + 1
1451 * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
1452 * (CORE_R + 1) * (CORE_OD + 1)
1453 *
1454 * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
1455 * and PadFrequency, respectively.
1456 */
1457 core_frequency = MLXBF_I2C_PLL_IN_FREQ * (++core_f);
1458
1459 return div_u64(core_frequency, (++core_r) * (++core_od));
1460 }
1461
mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource * corepll_res)1462 static u64 mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res)
1463 {
1464 u32 corepll_reg1_val, corepll_reg2_val;
1465 u64 corepll_frequency;
1466 u8 core_od, core_r;
1467 u32 core_f;
1468
1469 corepll_reg1_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
1470 corepll_reg2_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG2);
1471
1472 /* Get Core PLL configuration bits */
1473 core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_YU_MASK, corepll_reg1_val);
1474 core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_YU_MASK, corepll_reg1_val);
1475 core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_YU_MASK, corepll_reg2_val);
1476
1477 /*
1478 * Compute PLL output frequency as follow:
1479 *
1480 * CORE_F / 16384
1481 * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
1482 * (CORE_R + 1) * (CORE_OD + 1)
1483 *
1484 * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
1485 * and PadFrequency, respectively.
1486 */
1487 corepll_frequency = (MLXBF_I2C_PLL_IN_FREQ * core_f) / MLNXBF_I2C_COREPLL_CONST;
1488
1489 return div_u64(corepll_frequency, (++core_r) * (++core_od));
1490 }
1491
mlxbf_i2c_calculate_corepll_freq(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1492 static int mlxbf_i2c_calculate_corepll_freq(struct platform_device *pdev,
1493 struct mlxbf_i2c_priv *priv)
1494 {
1495 const struct mlxbf_i2c_chip_info *chip = priv->chip;
1496 struct mlxbf_i2c_resource *corepll_res;
1497 struct device *dev = &pdev->dev;
1498 u64 *freq = &priv->frequency;
1499 int ret;
1500
1501 corepll_res = mlxbf_i2c_get_shared_resource(priv,
1502 MLXBF_I2C_COREPLL_RES);
1503 if (!corepll_res)
1504 return -EPERM;
1505
1506 /*
1507 * First, check whether the TYU core Clock frequency is set.
1508 * The TYU core frequency is the same for all I2C busses; when
1509 * the first device gets probed the frequency is determined and
1510 * stored into a globally visible variable. So, first of all,
1511 * check whether the frequency is already set. Here, we assume
1512 * that the frequency is expected to be greater than 0.
1513 */
1514 mutex_lock(corepll_res->lock);
1515 if (!mlxbf_i2c_corepll_frequency) {
1516 if (!chip->calculate_freq) {
1517 mutex_unlock(corepll_res->lock);
1518 return -EPERM;
1519 }
1520
1521 ret = mlxbf_i2c_get_corepll(pdev, priv);
1522 if (ret < 0) {
1523 dev_err(dev, "Failed to get corePLL resource");
1524 mutex_unlock(corepll_res->lock);
1525 return ret;
1526 }
1527
1528 mlxbf_i2c_corepll_frequency = chip->calculate_freq(corepll_res);
1529 }
1530 mutex_unlock(corepll_res->lock);
1531
1532 *freq = mlxbf_i2c_corepll_frequency;
1533
1534 return 0;
1535 }
1536
mlxbf_i2c_slave_enable(struct mlxbf_i2c_priv * priv,struct i2c_client * slave)1537 static int mlxbf_i2c_slave_enable(struct mlxbf_i2c_priv *priv,
1538 struct i2c_client *slave)
1539 {
1540 u8 reg, reg_cnt, byte, addr_tmp;
1541 u32 slave_reg, slave_reg_tmp;
1542
1543 if (!priv)
1544 return -EPERM;
1545
1546 reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
1547
1548 /*
1549 * Read the slave registers. There are 4 * 32-bit slave registers.
1550 * Each slave register can hold up to 4 * 8-bit slave configuration:
1551 * 1) A 7-bit address
1552 * 2) And a status bit (1 if enabled, 0 if not).
1553 * Look for the next available slave register slot.
1554 */
1555 for (reg = 0; reg < reg_cnt; reg++) {
1556 slave_reg = readl(priv->slv->io +
1557 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
1558 /*
1559 * Each register holds 4 slave addresses. So, we have to keep
1560 * the byte order consistent with the value read in order to
1561 * update the register correctly, if needed.
1562 */
1563 slave_reg_tmp = slave_reg;
1564 for (byte = 0; byte < 4; byte++) {
1565 addr_tmp = slave_reg_tmp & GENMASK(7, 0);
1566
1567 /*
1568 * If an enable bit is not set in the
1569 * MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG register, then the
1570 * slave address slot associated with that bit is
1571 * free. So set the enable bit and write the
1572 * slave address bits.
1573 */
1574 if (!(addr_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT)) {
1575 slave_reg &= ~(MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK << (byte * 8));
1576 slave_reg |= (slave->addr << (byte * 8));
1577 slave_reg |= MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT << (byte * 8);
1578 writel(slave_reg, priv->slv->io +
1579 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
1580 (reg * 0x4));
1581
1582 /*
1583 * Set the slave at the corresponding index.
1584 */
1585 priv->slave[(reg * 4) + byte] = slave;
1586
1587 return 0;
1588 }
1589
1590 /* Parse next byte. */
1591 slave_reg_tmp >>= 8;
1592 }
1593 }
1594
1595 return -EBUSY;
1596 }
1597
mlxbf_i2c_slave_disable(struct mlxbf_i2c_priv * priv,u8 addr)1598 static int mlxbf_i2c_slave_disable(struct mlxbf_i2c_priv *priv, u8 addr)
1599 {
1600 u8 addr_tmp, reg, reg_cnt, byte;
1601 u32 slave_reg, slave_reg_tmp;
1602
1603 reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
1604
1605 /*
1606 * Read the slave registers. There are 4 * 32-bit slave registers.
1607 * Each slave register can hold up to 4 * 8-bit slave configuration:
1608 * 1) A 7-bit address
1609 * 2) And a status bit (1 if enabled, 0 if not).
1610 * Check if addr is present in the registers.
1611 */
1612 for (reg = 0; reg < reg_cnt; reg++) {
1613 slave_reg = readl(priv->slv->io +
1614 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
1615
1616 /* Check whether the address slots are empty. */
1617 if (!slave_reg)
1618 continue;
1619
1620 /*
1621 * Check if addr matches any of the 4 slave addresses
1622 * in the register.
1623 */
1624 slave_reg_tmp = slave_reg;
1625 for (byte = 0; byte < 4; byte++) {
1626 addr_tmp = slave_reg_tmp & MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK;
1627 /*
1628 * Parse slave address bytes and check whether the
1629 * slave address already exists.
1630 */
1631 if (addr_tmp == addr) {
1632 /* Clear the slave address slot. */
1633 slave_reg &= ~(GENMASK(7, 0) << (byte * 8));
1634 writel(slave_reg, priv->slv->io +
1635 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
1636 (reg * 0x4));
1637 /* Free slave at the corresponding index */
1638 priv->slave[(reg * 4) + byte] = NULL;
1639
1640 return 0;
1641 }
1642
1643 /* Parse next byte. */
1644 slave_reg_tmp >>= 8;
1645 }
1646 }
1647
1648 return -ENXIO;
1649 }
1650
mlxbf_i2c_init_coalesce(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1651 static int mlxbf_i2c_init_coalesce(struct platform_device *pdev,
1652 struct mlxbf_i2c_priv *priv)
1653 {
1654 struct mlxbf_i2c_resource *coalesce_res;
1655 struct resource *params;
1656 resource_size_t size;
1657 int ret = 0;
1658
1659 /*
1660 * Unlike BlueField-1 platform, the coalesce registers is a dedicated
1661 * resource in the next generations of BlueField.
1662 */
1663 if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
1664 coalesce_res = mlxbf_i2c_get_shared_resource(priv,
1665 MLXBF_I2C_COALESCE_RES);
1666 if (!coalesce_res)
1667 return -EPERM;
1668
1669 /*
1670 * The Cause Coalesce group in TYU space is shared among
1671 * I2C busses. This function MUST be serialized to avoid
1672 * racing when claiming the memory region.
1673 */
1674 lockdep_assert_held(mlxbf_i2c_gpio_res->lock);
1675
1676 /* Check whether the memory map exist. */
1677 if (coalesce_res->io) {
1678 priv->coalesce = coalesce_res;
1679 return 0;
1680 }
1681
1682 params = coalesce_res->params;
1683 size = resource_size(params);
1684
1685 if (!request_mem_region(params->start, size, params->name))
1686 return -EFAULT;
1687
1688 coalesce_res->io = ioremap(params->start, size);
1689 if (!coalesce_res->io) {
1690 release_mem_region(params->start, size);
1691 return -ENOMEM;
1692 }
1693
1694 priv->coalesce = coalesce_res;
1695
1696 } else {
1697 ret = mlxbf_i2c_init_resource(pdev, &priv->coalesce,
1698 MLXBF_I2C_COALESCE_RES);
1699 }
1700
1701 return ret;
1702 }
1703
mlxbf_i2c_release_coalesce(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1704 static int mlxbf_i2c_release_coalesce(struct platform_device *pdev,
1705 struct mlxbf_i2c_priv *priv)
1706 {
1707 struct mlxbf_i2c_resource *coalesce_res;
1708 struct device *dev = &pdev->dev;
1709 struct resource *params;
1710 resource_size_t size;
1711
1712 coalesce_res = priv->coalesce;
1713
1714 if (coalesce_res->io) {
1715 params = coalesce_res->params;
1716 size = resource_size(params);
1717 if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
1718 mutex_lock(coalesce_res->lock);
1719 iounmap(coalesce_res->io);
1720 release_mem_region(params->start, size);
1721 mutex_unlock(coalesce_res->lock);
1722 } else {
1723 devm_release_mem_region(dev, params->start, size);
1724 }
1725 }
1726
1727 return 0;
1728 }
1729
mlxbf_i2c_init_slave(struct platform_device * pdev,struct mlxbf_i2c_priv * priv)1730 static int mlxbf_i2c_init_slave(struct platform_device *pdev,
1731 struct mlxbf_i2c_priv *priv)
1732 {
1733 struct device *dev = &pdev->dev;
1734 u32 int_reg;
1735 int ret;
1736
1737 /* Reset FSM. */
1738 writel(0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_FSM);
1739
1740 /*
1741 * Enable slave cause interrupt bits. Drive
1742 * MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE and
1743 * MLXBF_I2C_CAUSE_WRITE_SUCCESS, these are enabled when an external
1744 * masters issue a Read and Write, respectively. But, clear all
1745 * interrupts first.
1746 */
1747 writel(~0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
1748 int_reg = MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE;
1749 int_reg |= MLXBF_I2C_CAUSE_WRITE_SUCCESS;
1750 writel(int_reg, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_EVTEN0);
1751
1752 /* Finally, set the 'ready' bit to start handling transactions. */
1753 writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1754
1755 /* Initialize the cause coalesce resource. */
1756 ret = mlxbf_i2c_init_coalesce(pdev, priv);
1757 if (ret < 0) {
1758 dev_err(dev, "failed to initialize cause coalesce\n");
1759 return ret;
1760 }
1761
1762 return 0;
1763 }
1764
mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv * priv,bool * read,bool * write)1765 static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
1766 bool *write)
1767 {
1768 const struct mlxbf_i2c_chip_info *chip = priv->chip;
1769 u32 coalesce0_reg, cause_reg;
1770 u8 slave_shift, is_set;
1771
1772 *write = false;
1773 *read = false;
1774
1775 slave_shift = chip->type != MLXBF_I2C_CHIP_TYPE_1 ?
1776 MLXBF_I2C_CAUSE_YU_SLAVE_BIT :
1777 priv->bus + MLXBF_I2C_CAUSE_TYU_SLAVE_BIT;
1778
1779 coalesce0_reg = readl(priv->coalesce->io + MLXBF_I2C_CAUSE_COALESCE_0);
1780 is_set = coalesce0_reg & (1 << slave_shift);
1781
1782 if (!is_set)
1783 return false;
1784
1785 /* Check the source of the interrupt, i.e. whether a Read or Write. */
1786 cause_reg = readl(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER);
1787 if (cause_reg & MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE)
1788 *read = true;
1789 else if (cause_reg & MLXBF_I2C_CAUSE_WRITE_SUCCESS)
1790 *write = true;
1791
1792 /* Clear cause bits. */
1793 writel(~0x0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
1794
1795 return true;
1796 }
1797
mlxbf_i2c_get_slave_from_addr(struct mlxbf_i2c_priv * priv,u8 addr)1798 static struct i2c_client *mlxbf_i2c_get_slave_from_addr(
1799 struct mlxbf_i2c_priv *priv, u8 addr)
1800 {
1801 int i;
1802
1803 for (i = 0; i < MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT; i++) {
1804 if (!priv->slave[i])
1805 continue;
1806
1807 if (priv->slave[i]->addr == addr)
1808 return priv->slave[i];
1809 }
1810
1811 return NULL;
1812 }
1813
1814 /*
1815 * Send byte to 'external' smbus master. This function is executed when
1816 * an external smbus master wants to read data from the BlueField.
1817 */
mlxbf_i2c_irq_send(struct mlxbf_i2c_priv * priv,u8 recv_bytes)1818 static int mlxbf_i2c_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
1819 {
1820 u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
1821 u8 write_size, pec_en, addr, value, byte_cnt;
1822 struct i2c_client *slave;
1823 u32 control32, data32;
1824 int ret = 0;
1825
1826 /*
1827 * Read the first byte received from the external master to
1828 * determine the slave address. This byte is located in the
1829 * first data descriptor register of the slave GW.
1830 */
1831 data32 = ioread32be(priv->slv->io +
1832 MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
1833 addr = (data32 & GENMASK(7, 0)) >> 1;
1834
1835 /*
1836 * Check if the slave address received in the data descriptor register
1837 * matches any of the slave addresses registered. If there is a match,
1838 * set the slave.
1839 */
1840 slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
1841 if (!slave) {
1842 ret = -ENXIO;
1843 goto clear_csr;
1844 }
1845
1846 /*
1847 * An I2C read can consist of a WRITE bit transaction followed by
1848 * a READ bit transaction. Indeed, slave devices often expect
1849 * the slave address to be followed by the internal address.
1850 * So, write the internal address byte first, and then, send the
1851 * requested data to the master.
1852 */
1853 if (recv_bytes > 1) {
1854 i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
1855 value = (data32 >> 8) & GENMASK(7, 0);
1856 ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
1857 &value);
1858 i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1859
1860 if (ret < 0)
1861 goto clear_csr;
1862 }
1863
1864 /*
1865 * Send data to the master. Currently, the driver supports
1866 * READ_BYTE, READ_WORD and BLOCK READ protocols. The
1867 * hardware can send up to 128 bytes per transfer which is
1868 * the total size of the data registers.
1869 */
1870 i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value);
1871
1872 for (byte_cnt = 0; byte_cnt < MLXBF_I2C_SLAVE_DATA_DESC_SIZE; byte_cnt++) {
1873 data_desc[byte_cnt] = value;
1874 i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value);
1875 }
1876
1877 /* Send a stop condition to the backend. */
1878 i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1879
1880 /* Set the number of bytes to write to master. */
1881 write_size = (byte_cnt - 1) & 0x7f;
1882
1883 /* Write data to Slave GW data descriptor. */
1884 mlxbf_i2c_smbus_write_data(priv, data_desc, byte_cnt,
1885 MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
1886
1887 pec_en = 0; /* Disable PEC since it is not supported. */
1888
1889 /* Prepare control word. */
1890 control32 = MLXBF_I2C_SLAVE_ENABLE;
1891 control32 |= rol32(write_size, MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT);
1892 control32 |= rol32(pec_en, MLXBF_I2C_SLAVE_SEND_PEC_SHIFT);
1893
1894 writel(control32, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_GW);
1895
1896 /*
1897 * Wait until the transfer is completed; the driver will wait
1898 * until the GW is idle, a cause will rise on fall of GW busy.
1899 */
1900 readl_poll_timeout_atomic(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER,
1901 data32, data32 & MLXBF_I2C_CAUSE_S_GW_BUSY_FALL,
1902 MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
1903
1904 clear_csr:
1905 /* Release the Slave GW. */
1906 writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
1907 writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
1908 writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1909
1910 return ret;
1911 }
1912
1913 /*
1914 * Receive bytes from 'external' smbus master. This function is executed when
1915 * an external smbus master wants to write data to the BlueField.
1916 */
mlxbf_i2c_irq_recv(struct mlxbf_i2c_priv * priv,u8 recv_bytes)1917 static int mlxbf_i2c_irq_recv(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
1918 {
1919 u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
1920 struct i2c_client *slave;
1921 u8 value, byte, addr;
1922 int ret = 0;
1923
1924 /* Read data from Slave GW data descriptor. */
1925 mlxbf_i2c_smbus_read_data(priv, data_desc, recv_bytes,
1926 MLXBF_I2C_SLAVE_DATA_DESC_ADDR, false);
1927 addr = data_desc[0] >> 1;
1928
1929 /*
1930 * Check if the slave address received in the data descriptor register
1931 * matches any of the slave addresses registered.
1932 */
1933 slave = mlxbf_i2c_get_slave_from_addr(priv, addr);
1934 if (!slave) {
1935 ret = -EINVAL;
1936 goto clear_csr;
1937 }
1938
1939 /*
1940 * Notify the slave backend that an smbus master wants to write data
1941 * to the BlueField.
1942 */
1943 i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
1944
1945 /* Send the received data to the slave backend. */
1946 for (byte = 1; byte < recv_bytes; byte++) {
1947 value = data_desc[byte];
1948 ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
1949 &value);
1950 if (ret < 0)
1951 break;
1952 }
1953
1954 /*
1955 * Send a stop event to the slave backend, to signal
1956 * the end of the write transactions.
1957 */
1958 i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1959
1960 clear_csr:
1961 /* Release the Slave GW. */
1962 writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
1963 writel(0x0, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
1964 writel(0x1, priv->slv->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1965
1966 return ret;
1967 }
1968
mlxbf_i2c_irq(int irq,void * ptr)1969 static irqreturn_t mlxbf_i2c_irq(int irq, void *ptr)
1970 {
1971 struct mlxbf_i2c_priv *priv = ptr;
1972 bool read, write, irq_is_set;
1973 u32 rw_bytes_reg;
1974 u8 recv_bytes;
1975
1976 /*
1977 * Read TYU interrupt register and determine the source of the
1978 * interrupt. Based on the source of the interrupt one of the
1979 * following actions are performed:
1980 * - Receive data and send response to master.
1981 * - Send data and release slave GW.
1982 *
1983 * Handle read/write transaction only. CRmaster and Iarp requests
1984 * are ignored for now.
1985 */
1986 irq_is_set = mlxbf_i2c_has_coalesce(priv, &read, &write);
1987 if (!irq_is_set || (!read && !write)) {
1988 /* Nothing to do here, interrupt was not from this device. */
1989 return IRQ_NONE;
1990 }
1991
1992 /*
1993 * The MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES includes the number of
1994 * bytes from/to master. These are defined by 8-bits each. If the lower
1995 * 8 bits are set, then the master expect to read N bytes from the
1996 * slave, if the higher 8 bits are sent then the slave expect N bytes
1997 * from the master.
1998 */
1999 rw_bytes_reg = readl(priv->slv->io +
2000 MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
2001 recv_bytes = (rw_bytes_reg >> 8) & GENMASK(7, 0);
2002
2003 /*
2004 * For now, the slave supports 128 bytes transfer. Discard remaining
2005 * data bytes if the master wrote more than
2006 * MLXBF_I2C_SLAVE_DATA_DESC_SIZE, i.e, the actual size of the slave
2007 * data descriptor.
2008 *
2009 * Note that we will never expect to transfer more than 128 bytes; as
2010 * specified in the SMBus standard, block transactions cannot exceed
2011 * 32 bytes.
2012 */
2013 recv_bytes = recv_bytes > MLXBF_I2C_SLAVE_DATA_DESC_SIZE ?
2014 MLXBF_I2C_SLAVE_DATA_DESC_SIZE : recv_bytes;
2015
2016 if (read)
2017 mlxbf_i2c_irq_send(priv, recv_bytes);
2018 else
2019 mlxbf_i2c_irq_recv(priv, recv_bytes);
2020
2021 return IRQ_HANDLED;
2022 }
2023
2024 /* Return negative errno on error. */
mlxbf_i2c_smbus_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)2025 static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
2026 unsigned short flags, char read_write,
2027 u8 command, int size,
2028 union i2c_smbus_data *data)
2029 {
2030 struct mlxbf_i2c_smbus_request request = { 0 };
2031 struct mlxbf_i2c_priv *priv;
2032 bool read, pec;
2033 u8 byte_cnt;
2034
2035 request.slave = addr;
2036
2037 read = (read_write == I2C_SMBUS_READ);
2038 pec = flags & I2C_FUNC_SMBUS_PEC;
2039
2040 switch (size) {
2041 case I2C_SMBUS_QUICK:
2042 mlxbf_i2c_smbus_quick_command(&request, read);
2043 dev_dbg(&adap->dev, "smbus quick, slave 0x%02x\n", addr);
2044 break;
2045
2046 case I2C_SMBUS_BYTE:
2047 mlxbf_i2c_smbus_byte_func(&request,
2048 read ? &data->byte : &command, read,
2049 pec);
2050 dev_dbg(&adap->dev, "smbus %s byte, slave 0x%02x.\n",
2051 str_read_write(read), addr);
2052 break;
2053
2054 case I2C_SMBUS_BYTE_DATA:
2055 mlxbf_i2c_smbus_data_byte_func(&request, &command, &data->byte,
2056 read, pec);
2057 dev_dbg(&adap->dev, "smbus %s byte data at 0x%02x, slave 0x%02x.\n",
2058 str_read_write(read), command, addr);
2059 break;
2060
2061 case I2C_SMBUS_WORD_DATA:
2062 mlxbf_i2c_smbus_data_word_func(&request, &command,
2063 (u8 *)&data->word, read, pec);
2064 dev_dbg(&adap->dev, "smbus %s word data at 0x%02x, slave 0x%02x.\n",
2065 str_read_write(read), command, addr);
2066 break;
2067
2068 case I2C_SMBUS_I2C_BLOCK_DATA:
2069 byte_cnt = data->block[0];
2070 mlxbf_i2c_smbus_i2c_block_func(&request, &command, data->block,
2071 &byte_cnt, read, pec);
2072 dev_dbg(&adap->dev, "i2c %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
2073 str_read_write(read), byte_cnt, command, addr);
2074 break;
2075
2076 case I2C_SMBUS_BLOCK_DATA:
2077 byte_cnt = read ? I2C_SMBUS_BLOCK_MAX : data->block[0];
2078 mlxbf_i2c_smbus_block_func(&request, &command, data->block,
2079 &byte_cnt, read, pec);
2080 dev_dbg(&adap->dev, "smbus %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
2081 str_read_write(read), byte_cnt, command, addr);
2082 break;
2083
2084 case I2C_FUNC_SMBUS_PROC_CALL:
2085 mlxbf_i2c_smbus_process_call_func(&request, &command,
2086 (u8 *)&data->word, pec);
2087 dev_dbg(&adap->dev, "process call, wr/rd at 0x%02x, slave 0x%02x.\n",
2088 command, addr);
2089 break;
2090
2091 case I2C_FUNC_SMBUS_BLOCK_PROC_CALL:
2092 byte_cnt = data->block[0];
2093 mlxbf_i2c_smbus_blk_process_call_func(&request, &command,
2094 data->block, &byte_cnt,
2095 pec);
2096 dev_dbg(&adap->dev, "block process call, wr/rd %d bytes, slave 0x%02x.\n",
2097 byte_cnt, addr);
2098 break;
2099
2100 default:
2101 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command %d\n",
2102 size);
2103 return -EOPNOTSUPP;
2104 }
2105
2106 priv = i2c_get_adapdata(adap);
2107
2108 return mlxbf_i2c_smbus_start_transaction(priv, &request);
2109 }
2110
mlxbf_i2c_reg_slave(struct i2c_client * slave)2111 static int mlxbf_i2c_reg_slave(struct i2c_client *slave)
2112 {
2113 struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
2114 struct device *dev = &slave->dev;
2115 int ret;
2116
2117 /*
2118 * Do not support ten bit chip address and do not use Packet Error
2119 * Checking (PEC).
2120 */
2121 if (slave->flags & (I2C_CLIENT_TEN | I2C_CLIENT_PEC)) {
2122 dev_err(dev, "SMBus PEC and 10 bit address not supported\n");
2123 return -EAFNOSUPPORT;
2124 }
2125
2126 ret = mlxbf_i2c_slave_enable(priv, slave);
2127 if (ret)
2128 dev_err(dev, "Surpassed max number of registered slaves allowed\n");
2129
2130 return 0;
2131 }
2132
mlxbf_i2c_unreg_slave(struct i2c_client * slave)2133 static int mlxbf_i2c_unreg_slave(struct i2c_client *slave)
2134 {
2135 struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
2136 struct device *dev = &slave->dev;
2137 int ret;
2138
2139 /*
2140 * Unregister slave by:
2141 * 1) Disabling the slave address in hardware
2142 * 2) Freeing priv->slave at the corresponding index
2143 */
2144 ret = mlxbf_i2c_slave_disable(priv, slave->addr);
2145 if (ret)
2146 dev_err(dev, "Unable to find slave 0x%x\n", slave->addr);
2147
2148 return ret;
2149 }
2150
mlxbf_i2c_functionality(struct i2c_adapter * adap)2151 static u32 mlxbf_i2c_functionality(struct i2c_adapter *adap)
2152 {
2153 return MLXBF_I2C_FUNC_ALL;
2154 }
2155
2156 static struct mlxbf_i2c_chip_info mlxbf_i2c_chip[] = {
2157 [MLXBF_I2C_CHIP_TYPE_1] = {
2158 .type = MLXBF_I2C_CHIP_TYPE_1,
2159 .shared_res = {
2160 [0] = &mlxbf_i2c_coalesce_res[MLXBF_I2C_CHIP_TYPE_1],
2161 [1] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_1],
2162 [2] = &mlxbf_i2c_gpio_res[MLXBF_I2C_CHIP_TYPE_1]
2163 },
2164 .calculate_freq = mlxbf_i2c_calculate_freq_from_tyu,
2165 .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
2166 .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
2167 },
2168 [MLXBF_I2C_CHIP_TYPE_2] = {
2169 .type = MLXBF_I2C_CHIP_TYPE_2,
2170 .shared_res = {
2171 [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_2]
2172 },
2173 .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
2174 .smbus_master_rs_bytes_off = MLXBF_I2C_YU_SMBUS_RS_BYTES,
2175 .smbus_master_fsm_off = MLXBF_I2C_YU_SMBUS_MASTER_FSM
2176 },
2177 [MLXBF_I2C_CHIP_TYPE_3] = {
2178 .type = MLXBF_I2C_CHIP_TYPE_3,
2179 .shared_res = {
2180 [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_3]
2181 },
2182 .calculate_freq = mlxbf_i2c_calculate_freq_from_yu,
2183 .smbus_master_rs_bytes_off = MLXBF_I2C_RSH_YU_SMBUS_RS_BYTES,
2184 .smbus_master_fsm_off = MLXBF_I2C_RSH_YU_SMBUS_MASTER_FSM
2185 }
2186 };
2187
2188 static const struct i2c_algorithm mlxbf_i2c_algo = {
2189 .smbus_xfer = mlxbf_i2c_smbus_xfer,
2190 .functionality = mlxbf_i2c_functionality,
2191 .reg_slave = mlxbf_i2c_reg_slave,
2192 .unreg_slave = mlxbf_i2c_unreg_slave,
2193 };
2194
2195 static struct i2c_adapter_quirks mlxbf_i2c_quirks = {
2196 .max_read_len = MLXBF_I2C_MASTER_DATA_R_LENGTH,
2197 .max_write_len = MLXBF_I2C_MASTER_DATA_W_LENGTH,
2198 };
2199
2200 static const struct acpi_device_id mlxbf_i2c_acpi_ids[] = {
2201 { "MLNXBF03", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_1] },
2202 { "MLNXBF23", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_2] },
2203 { "MLNXBF31", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_3] },
2204 {},
2205 };
2206
2207 MODULE_DEVICE_TABLE(acpi, mlxbf_i2c_acpi_ids);
2208
mlxbf_i2c_acpi_probe(struct device * dev,struct mlxbf_i2c_priv * priv)2209 static int mlxbf_i2c_acpi_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
2210 {
2211 const struct acpi_device_id *aid;
2212 u64 bus_id;
2213 int ret;
2214
2215 if (acpi_disabled)
2216 return -ENOENT;
2217
2218 aid = acpi_match_device(mlxbf_i2c_acpi_ids, dev);
2219 if (!aid)
2220 return -ENODEV;
2221
2222 priv->chip = (struct mlxbf_i2c_chip_info *)aid->driver_data;
2223
2224 ret = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &bus_id);
2225 if (ret) {
2226 dev_err(dev, "Cannot retrieve UID\n");
2227 return ret;
2228 }
2229
2230 priv->bus = bus_id;
2231
2232 return 0;
2233 }
2234
mlxbf_i2c_probe(struct platform_device * pdev)2235 static int mlxbf_i2c_probe(struct platform_device *pdev)
2236 {
2237 struct device *dev = &pdev->dev;
2238 struct mlxbf_i2c_priv *priv;
2239 struct i2c_adapter *adap;
2240 u32 resource_version;
2241 int irq, ret;
2242
2243 priv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_priv), GFP_KERNEL);
2244 if (!priv)
2245 return -ENOMEM;
2246
2247 ret = mlxbf_i2c_acpi_probe(dev, priv);
2248 if (ret < 0)
2249 return ret;
2250
2251 /* This property allows the driver to stay backward compatible with older
2252 * ACPI tables.
2253 * Starting BlueField-3 SoC, the "smbus" resource was broken down into 3
2254 * separate resources "timer", "master" and "slave".
2255 */
2256 if (device_property_read_u32(dev, "resource_version", &resource_version))
2257 resource_version = 0;
2258
2259 priv->resource_version = resource_version;
2260
2261 if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && resource_version == 0) {
2262 priv->timer = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
2263 if (!priv->timer)
2264 return -ENOMEM;
2265
2266 priv->mst = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
2267 if (!priv->mst)
2268 return -ENOMEM;
2269
2270 priv->slv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource), GFP_KERNEL);
2271 if (!priv->slv)
2272 return -ENOMEM;
2273
2274 ret = mlxbf_i2c_init_resource(pdev, &priv->smbus,
2275 MLXBF_I2C_SMBUS_RES);
2276 if (ret < 0)
2277 return dev_err_probe(dev, ret, "Cannot fetch smbus resource info");
2278
2279 priv->timer->io = priv->smbus->io;
2280 priv->mst->io = priv->smbus->io + MLXBF_I2C_MST_ADDR_OFFSET;
2281 priv->slv->io = priv->smbus->io + MLXBF_I2C_SLV_ADDR_OFFSET;
2282 } else {
2283 ret = mlxbf_i2c_init_resource(pdev, &priv->timer,
2284 MLXBF_I2C_SMBUS_TIMER_RES);
2285 if (ret < 0)
2286 return dev_err_probe(dev, ret, "Cannot fetch timer resource info");
2287
2288 ret = mlxbf_i2c_init_resource(pdev, &priv->mst,
2289 MLXBF_I2C_SMBUS_MST_RES);
2290 if (ret < 0)
2291 return dev_err_probe(dev, ret, "Cannot fetch master resource info");
2292
2293 ret = mlxbf_i2c_init_resource(pdev, &priv->slv,
2294 MLXBF_I2C_SMBUS_SLV_RES);
2295 if (ret < 0)
2296 return dev_err_probe(dev, ret, "Cannot fetch slave resource info");
2297 }
2298
2299 ret = mlxbf_i2c_init_resource(pdev, &priv->mst_cause,
2300 MLXBF_I2C_MST_CAUSE_RES);
2301 if (ret < 0)
2302 return dev_err_probe(dev, ret, "Cannot fetch cause master resource info");
2303
2304 ret = mlxbf_i2c_init_resource(pdev, &priv->slv_cause,
2305 MLXBF_I2C_SLV_CAUSE_RES);
2306 if (ret < 0)
2307 return dev_err_probe(dev, ret, "Cannot fetch cause slave resource info");
2308
2309 adap = &priv->adap;
2310 adap->owner = THIS_MODULE;
2311 adap->class = I2C_CLASS_HWMON;
2312 adap->algo = &mlxbf_i2c_algo;
2313 adap->quirks = &mlxbf_i2c_quirks;
2314 adap->dev.parent = dev;
2315 adap->dev.of_node = dev->of_node;
2316 adap->nr = priv->bus;
2317
2318 snprintf(adap->name, sizeof(adap->name), "i2c%d", adap->nr);
2319 i2c_set_adapdata(adap, priv);
2320
2321 /* Read Core PLL frequency. */
2322 ret = mlxbf_i2c_calculate_corepll_freq(pdev, priv);
2323 if (ret < 0) {
2324 dev_err(dev, "cannot get core clock frequency\n");
2325 /* Set to default value. */
2326 priv->frequency = MLXBF_I2C_COREPLL_FREQ;
2327 }
2328
2329 /*
2330 * Initialize master.
2331 * Note that a physical bus might be shared among Linux and firmware
2332 * (e.g., ATF). Thus, the bus should be initialized and ready and
2333 * bus initialization would be unnecessary. This requires additional
2334 * knowledge about physical busses. But, since an extra initialization
2335 * does not really hurt, then keep the code as is.
2336 */
2337 ret = mlxbf_i2c_init_master(pdev, priv);
2338 if (ret < 0)
2339 return dev_err_probe(dev, ret, "failed to initialize smbus master %d",
2340 priv->bus);
2341
2342 mlxbf_i2c_init_timings(pdev, priv);
2343
2344 mlxbf_i2c_init_slave(pdev, priv);
2345
2346 irq = platform_get_irq(pdev, 0);
2347 if (irq < 0)
2348 return irq;
2349 ret = devm_request_irq(dev, irq, mlxbf_i2c_irq,
2350 IRQF_SHARED | IRQF_PROBE_SHARED,
2351 dev_name(dev), priv);
2352 if (ret < 0)
2353 return ret;
2354
2355 priv->irq = irq;
2356
2357 platform_set_drvdata(pdev, priv);
2358
2359 ret = i2c_add_numbered_adapter(adap);
2360 if (ret < 0)
2361 return ret;
2362
2363 mutex_lock(&mlxbf_i2c_bus_lock);
2364 mlxbf_i2c_bus_count++;
2365 mutex_unlock(&mlxbf_i2c_bus_lock);
2366
2367 return 0;
2368 }
2369
mlxbf_i2c_remove(struct platform_device * pdev)2370 static void mlxbf_i2c_remove(struct platform_device *pdev)
2371 {
2372 struct mlxbf_i2c_priv *priv = platform_get_drvdata(pdev);
2373 struct device *dev = &pdev->dev;
2374 struct resource *params;
2375
2376 if (priv->chip->type < MLXBF_I2C_CHIP_TYPE_3 && priv->resource_version == 0) {
2377 params = priv->smbus->params;
2378 devm_release_mem_region(dev, params->start, resource_size(params));
2379 } else {
2380 params = priv->timer->params;
2381 devm_release_mem_region(dev, params->start, resource_size(params));
2382
2383 params = priv->mst->params;
2384 devm_release_mem_region(dev, params->start, resource_size(params));
2385
2386 params = priv->slv->params;
2387 devm_release_mem_region(dev, params->start, resource_size(params));
2388 }
2389
2390 params = priv->mst_cause->params;
2391 devm_release_mem_region(dev, params->start, resource_size(params));
2392
2393 params = priv->slv_cause->params;
2394 devm_release_mem_region(dev, params->start, resource_size(params));
2395
2396 /*
2397 * Release shared resources. This should be done when releasing
2398 * the I2C controller.
2399 */
2400 mutex_lock(&mlxbf_i2c_bus_lock);
2401 if (--mlxbf_i2c_bus_count == 0) {
2402 mlxbf_i2c_release_coalesce(pdev, priv);
2403 mlxbf_i2c_release_corepll(pdev, priv);
2404 mlxbf_i2c_release_gpio(pdev, priv);
2405 }
2406 mutex_unlock(&mlxbf_i2c_bus_lock);
2407
2408 devm_free_irq(dev, priv->irq, priv);
2409
2410 i2c_del_adapter(&priv->adap);
2411 }
2412
2413 static struct platform_driver mlxbf_i2c_driver = {
2414 .probe = mlxbf_i2c_probe,
2415 .remove = mlxbf_i2c_remove,
2416 .driver = {
2417 .name = "i2c-mlxbf",
2418 .acpi_match_table = ACPI_PTR(mlxbf_i2c_acpi_ids),
2419 },
2420 };
2421
mlxbf_i2c_init(void)2422 static int __init mlxbf_i2c_init(void)
2423 {
2424 mutex_init(&mlxbf_i2c_coalesce_lock);
2425 mutex_init(&mlxbf_i2c_corepll_lock);
2426 mutex_init(&mlxbf_i2c_gpio_lock);
2427
2428 mutex_init(&mlxbf_i2c_bus_lock);
2429
2430 return platform_driver_register(&mlxbf_i2c_driver);
2431 }
2432 module_init(mlxbf_i2c_init);
2433
mlxbf_i2c_exit(void)2434 static void __exit mlxbf_i2c_exit(void)
2435 {
2436 platform_driver_unregister(&mlxbf_i2c_driver);
2437
2438 mutex_destroy(&mlxbf_i2c_bus_lock);
2439
2440 mutex_destroy(&mlxbf_i2c_gpio_lock);
2441 mutex_destroy(&mlxbf_i2c_corepll_lock);
2442 mutex_destroy(&mlxbf_i2c_coalesce_lock);
2443 }
2444 module_exit(mlxbf_i2c_exit);
2445
2446 MODULE_DESCRIPTION("Mellanox BlueField I2C bus driver");
2447 MODULE_AUTHOR("Khalil Blaiech <kblaiech@nvidia.com>");
2448 MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
2449 MODULE_LICENSE("GPL v2");
2450