summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-kernel/linux/linux-aspeed/0040-i2c-Add-mux-hold-unhold-msg-types.patch
blob: 85b2f45a1b0b523e3b52a309e3288c51188e99c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
From 38ba0a960fcd17f7b3480fe3025c261fd60fe979 Mon Sep 17 00:00:00 2001
From: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Date: Fri, 15 Feb 2019 16:05:09 -0800
Subject: [PATCH] i2c: Add mux hold/unhold msg types

This commit adds mux hold/unhold message types to support extended
mux control for IPMB and MCTP devices. A hold or an unhold message
can be added at the end of I2C message stream wrapped by
repeated-start, also can be used as a single message independantly.

This mux hold/unhold message will be delivered throughout all mux
levels in the path. Means that if it goes to multi-level mux path,
all muxes will be held/unheld by this message.

1. Hold message
   struct i2c_msg msg;
   uint16_t timeout = 5000; // timeout in ms. 5 secs in this example.

   msg.addr = 0x0; // any value can be used. addr will be ignored in this packet.
   msg.flags = I2C_M_HOLD; // set this flag to indicate it's a hold message.
   msg.len = sizeof(uint16_t); // timeout value will be delivered using two bytes buffer.
   msg.buf = (uint8_t *)&timeout; // set timeout value.

2. Unhold message
   struct i2c_msg msg;
   uint16_t timeout = 0; // set 0 for an unhold message.

   msg.addr = 0x0; // any value can be used. addr will be ignored in this packet.
   msg.flags = I2C_M_HOLD; // set this flag to indicate it's an unhold message.
   msg.len = sizeof(uint16_t); // timeout value will be delivered using two bytes buffer.
   msg.buf = (uint8_t *)&timeout; // set timeout value.

   This unhold message can be delivered to a mux adapter even when
   a bus is locked so that any holding state can be unheld
   immediately by invoking this unhold message.

This patch would not be welcomed from upstream so it should be kept
in downstream only.

Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
---
 drivers/i2c/i2c-core-base.c  |  79 +++++++++++++++++++++++++++----
 drivers/i2c/i2c-core-smbus.c |  17 ++++++-
 drivers/i2c/i2c-mux.c        | 109 +++++++++++++++++++++++++++++++++++++++----
 include/linux/i2c-mux.h      |   3 ++
 include/linux/i2c.h          |  25 ++++++++++
 include/uapi/linux/i2c.h     |   1 +
 6 files changed, 215 insertions(+), 19 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 9200e349f29e..728b818501b1 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1211,6 +1211,25 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
 }
 EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
 
+static void i2c_adapter_hold(struct i2c_adapter *adapter, unsigned long timeout)
+{
+	mutex_lock(&adapter->hold_lock);
+	mod_timer(&adapter->hold_timer, jiffies + timeout);
+}
+
+static void i2c_adapter_unhold(struct i2c_adapter *adapter)
+{
+	del_timer_sync(&adapter->hold_timer);
+	mutex_unlock(&adapter->hold_lock);
+}
+
+static void i2c_adapter_hold_timer_callback(struct timer_list *t)
+{
+	struct i2c_adapter *adapter = from_timer(adapter, t, hold_timer);
+
+	i2c_adapter_unhold(adapter);
+}
+
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
 	int res = -EINVAL;
@@ -1292,6 +1311,9 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	mutex_init(&adap->hold_lock);
+	timer_setup(&adap->hold_timer, i2c_adapter_hold_timer_callback, 0);
+
 	return 0;
 
 out_reg:
@@ -1512,6 +1534,8 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 	idr_remove(&i2c_adapter_idr, adap->nr);
 	mutex_unlock(&core_lock);
 
+	i2c_adapter_unhold(adap);
+
 	/* Clear the device structure in case this adapter is ever going to be
 	   added again */
 	memset(&adap->dev, 0, sizeof(adap->dev));
@@ -1861,7 +1885,9 @@ static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs,
  */
 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
+	enum i2c_hold_msg_type hold_msg;
 	unsigned long orig_jiffies;
+	unsigned long timeout;
 	int ret, try;
 
 	if (WARN_ON(!msgs || num < 1))
@@ -1870,6 +1896,25 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 	if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
 		return -EOPNOTSUPP;
 
+	/* Do not deliver a mux hold msg to root bus adapter */
+	if (!i2c_parent_is_i2c_adapter(adap)) {
+	    hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+					   msgs[num - 1].len,
+					  (u16 *)msgs[num - 1].buf);
+		if (hold_msg == I2C_HOLD_MSG_SET) {
+			timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+			i2c_adapter_hold(adap, timeout);
+
+			if (--num == 0)
+				return 0;
+		} else if (hold_msg == I2C_HOLD_MSG_RESET) {
+			i2c_adapter_unhold(adap);
+			return 0;
+		} else if (hold_msg == I2C_HOLD_MSG_NONE) {
+			mutex_lock(&adap->hold_lock);
+		}
+	}
+
 	/*
 	 * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
 	 * enabled.  This is an efficient way of keeping the for-loop from
@@ -1902,6 +1947,9 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 		trace_i2c_result(adap, num, ret);
 	}
 
+	if (!i2c_parent_is_i2c_adapter(adap) && hold_msg == I2C_HOLD_MSG_NONE)
+		mutex_unlock(&adap->hold_lock);
+
 	return ret;
 }
 EXPORT_SYMBOL(__i2c_transfer);
@@ -1920,6 +1968,7 @@ EXPORT_SYMBOL(__i2c_transfer);
  */
 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
+	bool do_bus_lock = true;
 	int ret;
 
 	/* REVISIT the fault reporting model here is weak:
@@ -1949,18 +1998,30 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 				(msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
 		}
 #endif
-
-		if (in_atomic() || irqs_disabled()) {
-			ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
-			if (!ret)
-				/* I2C activity is ongoing. */
-				return -EAGAIN;
-		} else {
-			i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
+		/*
+		 * Do not lock a bus for delivering an unhold msg to a mux
+		 * adpater. This is just for a single length unhold msg case.
+		 */
+		if (num == 1 && i2c_parent_is_i2c_adapter(adap) &&
+		    i2c_check_hold_msg(msgs[0].flags, msgs[0].len,
+				       (u16 *)msgs[0].buf) ==
+				       I2C_HOLD_MSG_RESET)
+			do_bus_lock = false;
+
+		if (do_bus_lock) {
+			if (in_atomic() || irqs_disabled()) {
+				ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
+				if (!ret)
+					/* I2C activity is ongoing. */
+					return -EAGAIN;
+			} else {
+				i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
+			}
 		}
 
 		ret = __i2c_transfer(adap, msgs, num);
-		i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
+		if (do_bus_lock)
+			i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
 
 		return ret;
 	} else {
diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
index 9cd66cabb84f..64c58911bf21 100644
--- a/drivers/i2c/i2c-core-smbus.c
+++ b/drivers/i2c/i2c-core-smbus.c
@@ -528,12 +528,25 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
 		   unsigned short flags, char read_write,
 		   u8 command, int protocol, union i2c_smbus_data *data)
 {
+	bool do_bus_lock = true;
 	s32 res;
 
-	i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
+	/*
+	 * Do not lock a bus for delivering an unhold msg to a mux adpater.
+	 * This is just for a single length unhold msg case.
+	 */
+	if (i2c_parent_is_i2c_adapter(adapter) &&
+	    i2c_check_hold_msg(flags,
+			       protocol == I2C_SMBUS_WORD_DATA ? 2 : 0,
+			       &data->word) == I2C_HOLD_MSG_RESET)
+		do_bus_lock = false;
+
+	if (do_bus_lock)
+		i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
 	res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
 			       command, protocol, data);
-	i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
+	if (do_bus_lock)
+		i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
 
 	return res;
 }
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index f330690b4125..4d8909a0f90a 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/slab.h>
+#include <linux/timer.h>
 
 /* multiplexer per channel data */
 struct i2c_mux_priv {
@@ -35,21 +36,57 @@ struct i2c_mux_priv {
 	u32 chan_id;
 };
 
+static void i2c_mux_hold(struct i2c_mux_core *muxc, unsigned long timeout)
+{
+	mutex_lock(&muxc->hold_lock);
+	mod_timer(&muxc->hold_timer, jiffies + timeout);
+}
+
+static void i2c_mux_unhold(struct i2c_mux_core *muxc)
+{
+	del_timer_sync(&muxc->hold_timer);
+	mutex_unlock(&muxc->hold_lock);
+}
+
+static void i2c_mux_hold_timer_callback(struct timer_list *t)
+{
+	struct i2c_mux_core *muxc = from_timer(muxc, t, hold_timer);
+
+	i2c_mux_unhold(muxc);
+}
+
 static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
 				 struct i2c_msg msgs[], int num)
 {
 	struct i2c_mux_priv *priv = adap->algo_data;
 	struct i2c_mux_core *muxc = priv->muxc;
 	struct i2c_adapter *parent = muxc->parent;
+	enum i2c_hold_msg_type hold_msg;
+	unsigned long timeout;
 	int ret;
 
 	/* Switch to the right mux port and perform the transfer. */
 
+	hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+				      msgs[num - 1].len,
+				      (u16 *)msgs[num - 1].buf);
+	if (hold_msg == I2C_HOLD_MSG_SET) {
+		timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+		i2c_mux_hold(muxc, timeout);
+	} else if (hold_msg == I2C_HOLD_MSG_NONE) {
+		mutex_lock(&muxc->hold_lock);
+	}
 	ret = muxc->select(muxc, priv->chan_id);
 	if (ret >= 0)
 		ret = __i2c_transfer(parent, msgs, num);
-	if (muxc->deselect)
-		muxc->deselect(muxc, priv->chan_id);
+	if (hold_msg != I2C_HOLD_MSG_SET) {
+		if (muxc->deselect)
+			muxc->deselect(muxc, priv->chan_id);
+		if (hold_msg == I2C_HOLD_MSG_RESET)
+			i2c_mux_unhold(muxc);
+		else
+			mutex_unlock(&muxc->hold_lock);
+	}
 
 	return ret;
 }
@@ -60,15 +97,32 @@ static int i2c_mux_master_xfer(struct i2c_adapter *adap,
 	struct i2c_mux_priv *priv = adap->algo_data;
 	struct i2c_mux_core *muxc = priv->muxc;
 	struct i2c_adapter *parent = muxc->parent;
+	enum i2c_hold_msg_type hold_msg;
+	unsigned long timeout;
 	int ret;
 
 	/* Switch to the right mux port and perform the transfer. */
 
+	hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+				      msgs[num - 1].len,
+				      (u16 *)msgs[num - 1].buf);
+	if (hold_msg == I2C_HOLD_MSG_SET) {
+		timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+		i2c_mux_hold(muxc, timeout);
+	} else if (hold_msg == I2C_HOLD_MSG_NONE) {
+		mutex_lock(&muxc->hold_lock);
+	}
 	ret = muxc->select(muxc, priv->chan_id);
 	if (ret >= 0)
 		ret = i2c_transfer(parent, msgs, num);
-	if (muxc->deselect)
-		muxc->deselect(muxc, priv->chan_id);
+	if (hold_msg != I2C_HOLD_MSG_SET) {
+		if (muxc->deselect)
+			muxc->deselect(muxc, priv->chan_id);
+		if (hold_msg == I2C_HOLD_MSG_RESET)
+			i2c_mux_unhold(muxc);
+		else
+			mutex_unlock(&muxc->hold_lock);
+	}
 
 	return ret;
 }
@@ -81,16 +135,33 @@ static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
 	struct i2c_mux_priv *priv = adap->algo_data;
 	struct i2c_mux_core *muxc = priv->muxc;
 	struct i2c_adapter *parent = muxc->parent;
+	enum i2c_hold_msg_type hold_msg;
+	unsigned long timeout;
 	int ret;
 
 	/* Select the right mux port and perform the transfer. */
 
+	hold_msg = i2c_check_hold_msg(flags,
+				      size == I2C_SMBUS_WORD_DATA ? 2 : 0,
+				      &data->word);
+	if (hold_msg == I2C_HOLD_MSG_SET) {
+		timeout = msecs_to_jiffies(data->word);
+		i2c_mux_hold(muxc, timeout);
+	} else if (hold_msg == I2C_HOLD_MSG_NONE) {
+		mutex_lock(&muxc->hold_lock);
+	}
 	ret = muxc->select(muxc, priv->chan_id);
 	if (ret >= 0)
 		ret = __i2c_smbus_xfer(parent, addr, flags,
 				       read_write, command, size, data);
-	if (muxc->deselect)
-		muxc->deselect(muxc, priv->chan_id);
+	if (hold_msg != I2C_HOLD_MSG_SET) {
+		if (muxc->deselect)
+			muxc->deselect(muxc, priv->chan_id);
+		if (hold_msg == I2C_HOLD_MSG_RESET)
+			i2c_mux_unhold(muxc);
+		else
+			mutex_unlock(&muxc->hold_lock);
+	}
 
 	return ret;
 }
@@ -103,16 +174,33 @@ static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
 	struct i2c_mux_priv *priv = adap->algo_data;
 	struct i2c_mux_core *muxc = priv->muxc;
 	struct i2c_adapter *parent = muxc->parent;
+	enum i2c_hold_msg_type hold_msg;
+	unsigned long timeout;
 	int ret;
 
 	/* Select the right mux port and perform the transfer. */
 
+	hold_msg = i2c_check_hold_msg(flags,
+				      size == I2C_SMBUS_WORD_DATA ? 2 : 0,
+				      &data->word);
+	if (hold_msg == I2C_HOLD_MSG_SET) {
+		timeout = msecs_to_jiffies(data->word);
+		i2c_mux_hold(muxc, timeout);
+	} else if (hold_msg == I2C_HOLD_MSG_NONE) {
+		mutex_lock(&muxc->hold_lock);
+	}
 	ret = muxc->select(muxc, priv->chan_id);
 	if (ret >= 0)
 		ret = i2c_smbus_xfer(parent, addr, flags,
 				     read_write, command, size, data);
-	if (muxc->deselect)
-		muxc->deselect(muxc, priv->chan_id);
+	if (hold_msg != I2C_HOLD_MSG_SET) {
+		if (muxc->deselect)
+			muxc->deselect(muxc, priv->chan_id);
+		if (hold_msg == I2C_HOLD_MSG_RESET)
+			i2c_mux_unhold(muxc);
+		else
+			mutex_unlock(&muxc->hold_lock);
+	}
 
 	return ret;
 }
@@ -263,6 +351,9 @@ struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
 	muxc->deselect = deselect;
 	muxc->max_adapters = max_adapters;
 
+	mutex_init(&muxc->hold_lock);
+	timer_setup(&muxc->hold_timer, i2c_mux_hold_timer_callback, 0);
+
 	return muxc;
 }
 EXPORT_SYMBOL_GPL(i2c_mux_alloc);
@@ -435,6 +526,8 @@ void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
 {
 	char symlink_name[20];
 
+	i2c_mux_unhold(muxc);
+
 	while (muxc->num_adapters) {
 		struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
 		struct i2c_mux_priv *priv = adap->algo_data;
diff --git a/include/linux/i2c-mux.h b/include/linux/i2c-mux.h
index bd74d5706f3b..bc6f778eaf9d 100644
--- a/include/linux/i2c-mux.h
+++ b/include/linux/i2c-mux.h
@@ -41,6 +41,9 @@ struct i2c_mux_core {
 	int (*select)(struct i2c_mux_core *, u32 chan_id);
 	int (*deselect)(struct i2c_mux_core *, u32 chan_id);
 
+	struct mutex hold_lock; /* mutex for channel holding */
+	struct timer_list hold_timer;
+
 	int num_adapters;
 	int max_adapters;
 	struct i2c_adapter *adapter[0];
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 65b4eaed1d96..eadde70c0d4a 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -692,6 +692,13 @@ struct i2c_adapter {
 	const struct i2c_adapter_quirks *quirks;
 
 	struct irq_domain *host_notify_domain;
+
+	/*
+	 * These will be used by root adpaters only. For muxes, each mux core
+	 * has these individually.
+	 */
+	struct mutex hold_lock; /* mutex for bus holding */
+	struct timer_list hold_timer;
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
@@ -949,4 +956,22 @@ static inline struct i2c_client *i2c_acpi_new_device(struct device *dev,
 }
 #endif /* CONFIG_ACPI */
 
+enum i2c_hold_msg_type {
+	I2C_HOLD_MSG_NONE,
+	I2C_HOLD_MSG_SET,
+	I2C_HOLD_MSG_RESET
+};
+
+static inline enum i2c_hold_msg_type i2c_check_hold_msg(u16 flags, u16 len, u16 *buf)
+{
+	if (flags & I2C_M_HOLD && len == sizeof(u16)) {
+		if (*buf)
+			return I2C_HOLD_MSG_SET;
+
+		return I2C_HOLD_MSG_RESET;
+	}
+
+	return I2C_HOLD_MSG_NONE;
+}
+
 #endif /* _LINUX_I2C_H */
diff --git a/include/uapi/linux/i2c.h b/include/uapi/linux/i2c.h
index f71a1751cacf..a1db9b17ed36 100644
--- a/include/uapi/linux/i2c.h
+++ b/include/uapi/linux/i2c.h
@@ -72,6 +72,7 @@ struct i2c_msg {
 #define I2C_M_RD		0x0001	/* read data, from slave to master */
 					/* I2C_M_RD is guaranteed to be 0x0001! */
 #define I2C_M_TEN		0x0010	/* this is a ten bit chip address */
+#define I2C_M_HOLD		0x0100	/* for holding a mux path */
 #define I2C_M_DMA_SAFE		0x0200	/* the buffer of this message is DMA safe */
 					/* makes only sense in kernelspace */
 					/* userspace buffers are copied anyway */
-- 
2.7.4