xref: /linux/drivers/net/pse-pd/pd692x0.c (revision a6a6a98094116b60e5523a571d9443c53325f5b1)
19a993845SKory Maincent (Dent Project) // SPDX-License-Identifier: GPL-2.0-only
29a993845SKory Maincent (Dent Project) /*
39a993845SKory Maincent (Dent Project)  * Driver for the Microchip PD692X0 PoE PSE Controller driver (I2C bus)
49a993845SKory Maincent (Dent Project)  *
59a993845SKory Maincent (Dent Project)  * Copyright (c) 2023 Bootlin, Kory Maincent <kory.maincent@bootlin.com>
69a993845SKory Maincent (Dent Project)  */
79a993845SKory Maincent (Dent Project) 
89a993845SKory Maincent (Dent Project) #include <linux/delay.h>
99a993845SKory Maincent (Dent Project) #include <linux/firmware.h>
109a993845SKory Maincent (Dent Project) #include <linux/i2c.h>
119a993845SKory Maincent (Dent Project) #include <linux/module.h>
129a993845SKory Maincent (Dent Project) #include <linux/of.h>
139a993845SKory Maincent (Dent Project) #include <linux/platform_device.h>
149a993845SKory Maincent (Dent Project) #include <linux/pse-pd/pse.h>
159a993845SKory Maincent (Dent Project) 
169a993845SKory Maincent (Dent Project) #define PD692X0_PSE_NAME "pd692x0_pse"
179a993845SKory Maincent (Dent Project) 
189a993845SKory Maincent (Dent Project) #define PD692X0_MAX_PIS	48
199a993845SKory Maincent (Dent Project) #define PD692X0_MAX_MANAGERS		12
209a993845SKory Maincent (Dent Project) #define PD692X0_MAX_MANAGER_PORTS	8
219a993845SKory Maincent (Dent Project) #define PD692X0_MAX_HW_PORTS	(PD692X0_MAX_MANAGERS * PD692X0_MAX_MANAGER_PORTS)
229a993845SKory Maincent (Dent Project) 
239a993845SKory Maincent (Dent Project) #define PD69200_BT_PROD_VER	24
249a993845SKory Maincent (Dent Project) #define PD69210_BT_PROD_VER	26
259a993845SKory Maincent (Dent Project) #define PD69220_BT_PROD_VER	29
269a993845SKory Maincent (Dent Project) 
279a993845SKory Maincent (Dent Project) #define PD692X0_FW_MAJ_VER	3
289a993845SKory Maincent (Dent Project) #define PD692X0_FW_MIN_VER	5
299a993845SKory Maincent (Dent Project) #define PD692X0_FW_PATCH_VER	5
309a993845SKory Maincent (Dent Project) 
319a993845SKory Maincent (Dent Project) enum pd692x0_fw_state {
329a993845SKory Maincent (Dent Project) 	PD692X0_FW_UNKNOWN,
339a993845SKory Maincent (Dent Project) 	PD692X0_FW_OK,
349a993845SKory Maincent (Dent Project) 	PD692X0_FW_BROKEN,
359a993845SKory Maincent (Dent Project) 	PD692X0_FW_NEED_UPDATE,
369a993845SKory Maincent (Dent Project) 	PD692X0_FW_PREPARE,
379a993845SKory Maincent (Dent Project) 	PD692X0_FW_WRITE,
389a993845SKory Maincent (Dent Project) 	PD692X0_FW_COMPLETE,
399a993845SKory Maincent (Dent Project) };
409a993845SKory Maincent (Dent Project) 
419a993845SKory Maincent (Dent Project) struct pd692x0_msg {
429a993845SKory Maincent (Dent Project) 	u8 key;
439a993845SKory Maincent (Dent Project) 	u8 echo;
449a993845SKory Maincent (Dent Project) 	u8 sub[3];
459a993845SKory Maincent (Dent Project) 	u8 data[8];
469a993845SKory Maincent (Dent Project) 	__be16 chksum;
479a993845SKory Maincent (Dent Project) } __packed;
489a993845SKory Maincent (Dent Project) 
499a993845SKory Maincent (Dent Project) struct pd692x0_msg_ver {
509a993845SKory Maincent (Dent Project) 	u8 prod;
519a993845SKory Maincent (Dent Project) 	u8 maj_sw_ver;
529a993845SKory Maincent (Dent Project) 	u8 min_sw_ver;
539a993845SKory Maincent (Dent Project) 	u8 pa_sw_ver;
549a993845SKory Maincent (Dent Project) 	u8 param;
559a993845SKory Maincent (Dent Project) 	u8 build;
569a993845SKory Maincent (Dent Project) };
579a993845SKory Maincent (Dent Project) 
589a993845SKory Maincent (Dent Project) enum {
599a993845SKory Maincent (Dent Project) 	PD692X0_KEY_CMD,
609a993845SKory Maincent (Dent Project) 	PD692X0_KEY_PRG,
619a993845SKory Maincent (Dent Project) 	PD692X0_KEY_REQ,
629a993845SKory Maincent (Dent Project) 	PD692X0_KEY_TLM,
639a993845SKory Maincent (Dent Project) 	PD692X0_KEY_TEST,
649a993845SKory Maincent (Dent Project) 	PD692X0_KEY_REPORT = 0x52
659a993845SKory Maincent (Dent Project) };
669a993845SKory Maincent (Dent Project) 
679a993845SKory Maincent (Dent Project) enum {
689a993845SKory Maincent (Dent Project) 	PD692X0_MSG_RESET,
699a993845SKory Maincent (Dent Project) 	PD692X0_MSG_GET_SYS_STATUS,
709a993845SKory Maincent (Dent Project) 	PD692X0_MSG_GET_SW_VER,
719a993845SKory Maincent (Dent Project) 	PD692X0_MSG_SET_TMP_PORT_MATRIX,
729a993845SKory Maincent (Dent Project) 	PD692X0_MSG_PRG_PORT_MATRIX,
739a993845SKory Maincent (Dent Project) 	PD692X0_MSG_SET_PORT_PARAM,
749a993845SKory Maincent (Dent Project) 	PD692X0_MSG_GET_PORT_STATUS,
759a993845SKory Maincent (Dent Project) 	PD692X0_MSG_DOWNLOAD_CMD,
769a993845SKory Maincent (Dent Project) 
779a993845SKory Maincent (Dent Project) 	/* add new message above here */
789a993845SKory Maincent (Dent Project) 	PD692X0_MSG_CNT
799a993845SKory Maincent (Dent Project) };
809a993845SKory Maincent (Dent Project) 
819a993845SKory Maincent (Dent Project) struct pd692x0_priv {
829a993845SKory Maincent (Dent Project) 	struct i2c_client *client;
839a993845SKory Maincent (Dent Project) 	struct pse_controller_dev pcdev;
849a993845SKory Maincent (Dent Project) 	struct device_node *np;
859a993845SKory Maincent (Dent Project) 
869a993845SKory Maincent (Dent Project) 	enum pd692x0_fw_state fw_state;
879a993845SKory Maincent (Dent Project) 	struct fw_upload *fwl;
889a993845SKory Maincent (Dent Project) 	bool cancel_request;
899a993845SKory Maincent (Dent Project) 
909a993845SKory Maincent (Dent Project) 	u8 msg_id;
919a993845SKory Maincent (Dent Project) 	bool last_cmd_key;
929a993845SKory Maincent (Dent Project) 	unsigned long last_cmd_key_time;
939a993845SKory Maincent (Dent Project) 
949a993845SKory Maincent (Dent Project) 	enum ethtool_c33_pse_admin_state admin_state[PD692X0_MAX_PIS];
959a993845SKory Maincent (Dent Project) };
969a993845SKory Maincent (Dent Project) 
979a993845SKory Maincent (Dent Project) /* Template list of communication messages. The non-null bytes defined here
989a993845SKory Maincent (Dent Project)  * constitute the fixed portion of the messages. The remaining bytes will
999a993845SKory Maincent (Dent Project)  * be configured later within the functions. Refer to the "PD692x0 BT Serial
1009a993845SKory Maincent (Dent Project)  * Communication Protocol User Guide" for comprehensive details on messages
1019a993845SKory Maincent (Dent Project)  * content.
1029a993845SKory Maincent (Dent Project)  */
1039a993845SKory Maincent (Dent Project) static const struct pd692x0_msg pd692x0_msg_template_list[PD692X0_MSG_CNT] = {
1049a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_RESET] = {
1059a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_CMD,
1069a993845SKory Maincent (Dent Project) 		.sub = {0x07, 0x55, 0x00},
1079a993845SKory Maincent (Dent Project) 		.data = {0x55, 0x00, 0x55, 0x4e,
1089a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1099a993845SKory Maincent (Dent Project) 	},
1109a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_GET_SYS_STATUS] = {
1119a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_REQ,
1129a993845SKory Maincent (Dent Project) 		.sub = {0x07, 0xd0, 0x4e},
1139a993845SKory Maincent (Dent Project) 		.data = {0x4e, 0x4e, 0x4e, 0x4e,
1149a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1159a993845SKory Maincent (Dent Project) 	},
1169a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_GET_SW_VER] = {
1179a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_REQ,
1189a993845SKory Maincent (Dent Project) 		.sub = {0x07, 0x1e, 0x21},
1199a993845SKory Maincent (Dent Project) 		.data = {0x4e, 0x4e, 0x4e, 0x4e,
1209a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1219a993845SKory Maincent (Dent Project) 	},
1229a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_SET_TMP_PORT_MATRIX] = {
1239a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_CMD,
1249a993845SKory Maincent (Dent Project) 		.sub	 = {0x05, 0x43},
1259a993845SKory Maincent (Dent Project) 		.data = {   0, 0x4e, 0x4e, 0x4e,
1269a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1279a993845SKory Maincent (Dent Project) 	},
1289a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_PRG_PORT_MATRIX] = {
1299a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_CMD,
1309a993845SKory Maincent (Dent Project) 		.sub = {0x07, 0x43, 0x4e},
1319a993845SKory Maincent (Dent Project) 		.data = {0x4e, 0x4e, 0x4e, 0x4e,
1329a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1339a993845SKory Maincent (Dent Project) 	},
1349a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_SET_PORT_PARAM] = {
1359a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_CMD,
1369a993845SKory Maincent (Dent Project) 		.sub = {0x05, 0xc0},
1379a993845SKory Maincent (Dent Project) 		.data = {   0, 0xff, 0xff, 0xff,
1389a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1399a993845SKory Maincent (Dent Project) 	},
1409a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_GET_PORT_STATUS] = {
1419a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_REQ,
1429a993845SKory Maincent (Dent Project) 		.sub = {0x05, 0xc1},
1439a993845SKory Maincent (Dent Project) 		.data = {0x4e, 0x4e, 0x4e, 0x4e,
1449a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1459a993845SKory Maincent (Dent Project) 	},
1469a993845SKory Maincent (Dent Project) 	[PD692X0_MSG_DOWNLOAD_CMD] = {
1479a993845SKory Maincent (Dent Project) 		.key = PD692X0_KEY_PRG,
1489a993845SKory Maincent (Dent Project) 		.sub = {0xff, 0x99, 0x15},
1499a993845SKory Maincent (Dent Project) 		.data = {0x16, 0x16, 0x99, 0x4e,
1509a993845SKory Maincent (Dent Project) 			 0x4e, 0x4e, 0x4e, 0x4e},
1519a993845SKory Maincent (Dent Project) 	},
1529a993845SKory Maincent (Dent Project) };
1539a993845SKory Maincent (Dent Project) 
1549a993845SKory Maincent (Dent Project) static u8 pd692x0_build_msg(struct pd692x0_msg *msg, u8 echo)
1559a993845SKory Maincent (Dent Project) {
1569a993845SKory Maincent (Dent Project) 	u8 *data = (u8 *)msg;
1579a993845SKory Maincent (Dent Project) 	u16 chksum = 0;
1589a993845SKory Maincent (Dent Project) 	int i;
1599a993845SKory Maincent (Dent Project) 
1609a993845SKory Maincent (Dent Project) 	msg->echo = echo++;
1619a993845SKory Maincent (Dent Project) 	if (echo == 0xff)
1629a993845SKory Maincent (Dent Project) 		echo = 0;
1639a993845SKory Maincent (Dent Project) 
1649a993845SKory Maincent (Dent Project) 	for (i = 0; i < sizeof(*msg) - sizeof(msg->chksum); i++)
1659a993845SKory Maincent (Dent Project) 		chksum += data[i];
1669a993845SKory Maincent (Dent Project) 
1679a993845SKory Maincent (Dent Project) 	msg->chksum = cpu_to_be16(chksum);
1689a993845SKory Maincent (Dent Project) 
1699a993845SKory Maincent (Dent Project) 	return echo;
1709a993845SKory Maincent (Dent Project) }
1719a993845SKory Maincent (Dent Project) 
1729a993845SKory Maincent (Dent Project) static int pd692x0_send_msg(struct pd692x0_priv *priv, struct pd692x0_msg *msg)
1739a993845SKory Maincent (Dent Project) {
1749a993845SKory Maincent (Dent Project) 	const struct i2c_client *client = priv->client;
1759a993845SKory Maincent (Dent Project) 	int ret;
1769a993845SKory Maincent (Dent Project) 
1779a993845SKory Maincent (Dent Project) 	if (msg->key == PD692X0_KEY_CMD && priv->last_cmd_key) {
1789a993845SKory Maincent (Dent Project) 		int cmd_msleep;
1799a993845SKory Maincent (Dent Project) 
1809a993845SKory Maincent (Dent Project) 		cmd_msleep = 30 - jiffies_to_msecs(jiffies - priv->last_cmd_key_time);
1819a993845SKory Maincent (Dent Project) 		if (cmd_msleep > 0)
1829a993845SKory Maincent (Dent Project) 			msleep(cmd_msleep);
1839a993845SKory Maincent (Dent Project) 	}
1849a993845SKory Maincent (Dent Project) 
1859a993845SKory Maincent (Dent Project) 	/* Add echo and checksum bytes to the message */
1869a993845SKory Maincent (Dent Project) 	priv->msg_id = pd692x0_build_msg(msg, priv->msg_id);
1879a993845SKory Maincent (Dent Project) 
1889a993845SKory Maincent (Dent Project) 	ret = i2c_master_send(client, (u8 *)msg, sizeof(*msg));
1899a993845SKory Maincent (Dent Project) 	if (ret != sizeof(*msg))
1909a993845SKory Maincent (Dent Project) 		return -EIO;
1919a993845SKory Maincent (Dent Project) 
1929a993845SKory Maincent (Dent Project) 	return 0;
1939a993845SKory Maincent (Dent Project) }
1949a993845SKory Maincent (Dent Project) 
1959a993845SKory Maincent (Dent Project) static int pd692x0_reset(struct pd692x0_priv *priv)
1969a993845SKory Maincent (Dent Project) {
1979a993845SKory Maincent (Dent Project) 	const struct i2c_client *client = priv->client;
1989a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
1999a993845SKory Maincent (Dent Project) 	int ret;
2009a993845SKory Maincent (Dent Project) 
2019a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_RESET];
2029a993845SKory Maincent (Dent Project) 	ret = pd692x0_send_msg(priv, &msg);
2039a993845SKory Maincent (Dent Project) 	if (ret) {
2049a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
2059a993845SKory Maincent (Dent Project) 			"Failed to reset the controller (%pe)\n", ERR_PTR(ret));
2069a993845SKory Maincent (Dent Project) 		return ret;
2079a993845SKory Maincent (Dent Project) 	}
2089a993845SKory Maincent (Dent Project) 
2099a993845SKory Maincent (Dent Project) 	msleep(30);
2109a993845SKory Maincent (Dent Project) 
2119a993845SKory Maincent (Dent Project) 	ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
2129a993845SKory Maincent (Dent Project) 	if (ret != sizeof(buf))
2139a993845SKory Maincent (Dent Project) 		return ret < 0 ? ret : -EIO;
2149a993845SKory Maincent (Dent Project) 
2159a993845SKory Maincent (Dent Project) 	/* Is the reply a successful report message */
2169a993845SKory Maincent (Dent Project) 	if (buf.key != PD692X0_KEY_REPORT || buf.sub[0] || buf.sub[1])
2179a993845SKory Maincent (Dent Project) 		return -EIO;
2189a993845SKory Maincent (Dent Project) 
2199a993845SKory Maincent (Dent Project) 	msleep(300);
2209a993845SKory Maincent (Dent Project) 
2219a993845SKory Maincent (Dent Project) 	ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
2229a993845SKory Maincent (Dent Project) 	if (ret != sizeof(buf))
2239a993845SKory Maincent (Dent Project) 		return ret < 0 ? ret : -EIO;
2249a993845SKory Maincent (Dent Project) 
2259a993845SKory Maincent (Dent Project) 	/* Is the boot status without error */
2269a993845SKory Maincent (Dent Project) 	if (buf.key != 0x03 || buf.echo != 0xff || buf.sub[0] & 0x1) {
2279a993845SKory Maincent (Dent Project) 		dev_err(&client->dev, "PSE controller error\n");
2289a993845SKory Maincent (Dent Project) 		return -EIO;
2299a993845SKory Maincent (Dent Project) 	}
2309a993845SKory Maincent (Dent Project) 
2319a993845SKory Maincent (Dent Project) 	return 0;
2329a993845SKory Maincent (Dent Project) }
2339a993845SKory Maincent (Dent Project) 
2349a993845SKory Maincent (Dent Project) static bool pd692x0_try_recv_msg(const struct i2c_client *client,
2359a993845SKory Maincent (Dent Project) 				 struct pd692x0_msg *msg,
2369a993845SKory Maincent (Dent Project) 				 struct pd692x0_msg *buf)
2379a993845SKory Maincent (Dent Project) {
2389a993845SKory Maincent (Dent Project) 	/* Wait 30ms before readback as mandated by the protocol */
2399a993845SKory Maincent (Dent Project) 	msleep(30);
2409a993845SKory Maincent (Dent Project) 
2419a993845SKory Maincent (Dent Project) 	memset(buf, 0, sizeof(*buf));
2429a993845SKory Maincent (Dent Project) 	i2c_master_recv(client, (u8 *)buf, sizeof(*buf));
2439a993845SKory Maincent (Dent Project) 	if (buf->key)
2449a993845SKory Maincent (Dent Project) 		return 0;
2459a993845SKory Maincent (Dent Project) 
2469a993845SKory Maincent (Dent Project) 	msleep(100);
2479a993845SKory Maincent (Dent Project) 
2489a993845SKory Maincent (Dent Project) 	memset(buf, 0, sizeof(*buf));
2499a993845SKory Maincent (Dent Project) 	i2c_master_recv(client, (u8 *)buf, sizeof(*buf));
2509a993845SKory Maincent (Dent Project) 	if (buf->key)
2519a993845SKory Maincent (Dent Project) 		return 0;
2529a993845SKory Maincent (Dent Project) 
2539a993845SKory Maincent (Dent Project) 	return 1;
2549a993845SKory Maincent (Dent Project) }
2559a993845SKory Maincent (Dent Project) 
2569a993845SKory Maincent (Dent Project) /* Implementation of I2C communication, specifically addressing scenarios
2579a993845SKory Maincent (Dent Project)  * involving communication loss. Refer to the "Synchronization During
2589a993845SKory Maincent (Dent Project)  * Communication Loss" section in the Communication Protocol document for
2599a993845SKory Maincent (Dent Project)  * further details.
2609a993845SKory Maincent (Dent Project)  */
2619a993845SKory Maincent (Dent Project) static int pd692x0_recv_msg(struct pd692x0_priv *priv,
2629a993845SKory Maincent (Dent Project) 			    struct pd692x0_msg *msg,
2639a993845SKory Maincent (Dent Project) 			    struct pd692x0_msg *buf)
2649a993845SKory Maincent (Dent Project) {
2659a993845SKory Maincent (Dent Project) 	const struct i2c_client *client = priv->client;
2669a993845SKory Maincent (Dent Project) 	int ret;
2679a993845SKory Maincent (Dent Project) 
2689a993845SKory Maincent (Dent Project) 	ret = pd692x0_try_recv_msg(client, msg, buf);
2699a993845SKory Maincent (Dent Project) 	if (!ret)
2709a993845SKory Maincent (Dent Project) 		goto out_success;
2719a993845SKory Maincent (Dent Project) 
2729a993845SKory Maincent (Dent Project) 	dev_warn(&client->dev,
2739a993845SKory Maincent (Dent Project) 		 "Communication lost, rtnl is locked until communication is back!");
2749a993845SKory Maincent (Dent Project) 
2759a993845SKory Maincent (Dent Project) 	ret = pd692x0_send_msg(priv, msg);
2769a993845SKory Maincent (Dent Project) 	if (ret)
2779a993845SKory Maincent (Dent Project) 		return ret;
2789a993845SKory Maincent (Dent Project) 
2799a993845SKory Maincent (Dent Project) 	ret = pd692x0_try_recv_msg(client, msg, buf);
2809a993845SKory Maincent (Dent Project) 	if (!ret)
2819a993845SKory Maincent (Dent Project) 		goto out_success2;
2829a993845SKory Maincent (Dent Project) 
2839a993845SKory Maincent (Dent Project) 	msleep(10000);
2849a993845SKory Maincent (Dent Project) 
2859a993845SKory Maincent (Dent Project) 	ret = pd692x0_send_msg(priv, msg);
2869a993845SKory Maincent (Dent Project) 	if (ret)
2879a993845SKory Maincent (Dent Project) 		return ret;
2889a993845SKory Maincent (Dent Project) 
2899a993845SKory Maincent (Dent Project) 	ret = pd692x0_try_recv_msg(client, msg, buf);
2909a993845SKory Maincent (Dent Project) 	if (!ret)
2919a993845SKory Maincent (Dent Project) 		goto out_success2;
2929a993845SKory Maincent (Dent Project) 
2939a993845SKory Maincent (Dent Project) 	return pd692x0_reset(priv);
2949a993845SKory Maincent (Dent Project) 
2959a993845SKory Maincent (Dent Project) out_success2:
2969a993845SKory Maincent (Dent Project) 	dev_warn(&client->dev, "Communication is back, rtnl is unlocked!");
2979a993845SKory Maincent (Dent Project) out_success:
2989a993845SKory Maincent (Dent Project) 	if (msg->key == PD692X0_KEY_CMD) {
2999a993845SKory Maincent (Dent Project) 		priv->last_cmd_key = true;
3009a993845SKory Maincent (Dent Project) 		priv->last_cmd_key_time = jiffies;
3019a993845SKory Maincent (Dent Project) 	} else {
3029a993845SKory Maincent (Dent Project) 		priv->last_cmd_key = false;
3039a993845SKory Maincent (Dent Project) 	}
3049a993845SKory Maincent (Dent Project) 
3059a993845SKory Maincent (Dent Project) 	return 0;
3069a993845SKory Maincent (Dent Project) }
3079a993845SKory Maincent (Dent Project) 
3089a993845SKory Maincent (Dent Project) static int pd692x0_sendrecv_msg(struct pd692x0_priv *priv,
3099a993845SKory Maincent (Dent Project) 				struct pd692x0_msg *msg,
3109a993845SKory Maincent (Dent Project) 				struct pd692x0_msg *buf)
3119a993845SKory Maincent (Dent Project) {
3129a993845SKory Maincent (Dent Project) 	struct device *dev = &priv->client->dev;
3139a993845SKory Maincent (Dent Project) 	int ret;
3149a993845SKory Maincent (Dent Project) 
3159a993845SKory Maincent (Dent Project) 	ret = pd692x0_send_msg(priv, msg);
3169a993845SKory Maincent (Dent Project) 	if (ret)
3179a993845SKory Maincent (Dent Project) 		return ret;
3189a993845SKory Maincent (Dent Project) 
3199a993845SKory Maincent (Dent Project) 	ret = pd692x0_recv_msg(priv, msg, buf);
3209a993845SKory Maincent (Dent Project) 	if (ret)
3219a993845SKory Maincent (Dent Project) 		return ret;
3229a993845SKory Maincent (Dent Project) 
3239a993845SKory Maincent (Dent Project) 	if (msg->echo != buf->echo) {
3249a993845SKory Maincent (Dent Project) 		dev_err(dev,
3259a993845SKory Maincent (Dent Project) 			"Wrong match in message ID, expect %d received %d.\n",
3269a993845SKory Maincent (Dent Project) 			msg->echo, buf->echo);
3279a993845SKory Maincent (Dent Project) 		return -EIO;
3289a993845SKory Maincent (Dent Project) 	}
3299a993845SKory Maincent (Dent Project) 
3309a993845SKory Maincent (Dent Project) 	/* If the reply is a report message is it successful */
3319a993845SKory Maincent (Dent Project) 	if (buf->key == PD692X0_KEY_REPORT &&
3329a993845SKory Maincent (Dent Project) 	    (buf->sub[0] || buf->sub[1])) {
3339a993845SKory Maincent (Dent Project) 		return -EIO;
3349a993845SKory Maincent (Dent Project) 	}
3359a993845SKory Maincent (Dent Project) 
3369a993845SKory Maincent (Dent Project) 	return 0;
3379a993845SKory Maincent (Dent Project) }
3389a993845SKory Maincent (Dent Project) 
3399a993845SKory Maincent (Dent Project) static struct pd692x0_priv *to_pd692x0_priv(struct pse_controller_dev *pcdev)
3409a993845SKory Maincent (Dent Project) {
3419a993845SKory Maincent (Dent Project) 	return container_of(pcdev, struct pd692x0_priv, pcdev);
3429a993845SKory Maincent (Dent Project) }
3439a993845SKory Maincent (Dent Project) 
3449a993845SKory Maincent (Dent Project) static int pd692x0_fw_unavailable(struct pd692x0_priv *priv)
3459a993845SKory Maincent (Dent Project) {
3469a993845SKory Maincent (Dent Project) 	switch (priv->fw_state) {
3479a993845SKory Maincent (Dent Project) 	case PD692X0_FW_OK:
3489a993845SKory Maincent (Dent Project) 		return 0;
3499a993845SKory Maincent (Dent Project) 	case PD692X0_FW_PREPARE:
3509a993845SKory Maincent (Dent Project) 	case PD692X0_FW_WRITE:
3519a993845SKory Maincent (Dent Project) 	case PD692X0_FW_COMPLETE:
3529a993845SKory Maincent (Dent Project) 		dev_err(&priv->client->dev, "Firmware update in progress!\n");
3539a993845SKory Maincent (Dent Project) 		return -EBUSY;
3549a993845SKory Maincent (Dent Project) 	case PD692X0_FW_BROKEN:
3559a993845SKory Maincent (Dent Project) 	case PD692X0_FW_NEED_UPDATE:
3569a993845SKory Maincent (Dent Project) 	default:
3579a993845SKory Maincent (Dent Project) 		dev_err(&priv->client->dev,
3589a993845SKory Maincent (Dent Project) 			"Firmware issue. Please update it!\n");
3599a993845SKory Maincent (Dent Project) 		return -EOPNOTSUPP;
3609a993845SKory Maincent (Dent Project) 	}
3619a993845SKory Maincent (Dent Project) }
3629a993845SKory Maincent (Dent Project) 
3639a993845SKory Maincent (Dent Project) static int pd692x0_pi_enable(struct pse_controller_dev *pcdev, int id)
3649a993845SKory Maincent (Dent Project) {
3659a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
3669a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
3679a993845SKory Maincent (Dent Project) 	int ret;
3689a993845SKory Maincent (Dent Project) 
3699a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_unavailable(priv);
3709a993845SKory Maincent (Dent Project) 	if (ret)
3719a993845SKory Maincent (Dent Project) 		return ret;
3729a993845SKory Maincent (Dent Project) 
3739a993845SKory Maincent (Dent Project) 	if (priv->admin_state[id] == ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED)
3749a993845SKory Maincent (Dent Project) 		return 0;
3759a993845SKory Maincent (Dent Project) 
3769a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_SET_PORT_PARAM];
3779a993845SKory Maincent (Dent Project) 	msg.data[0] = 0x1;
3789a993845SKory Maincent (Dent Project) 	msg.sub[2] = id;
3799a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
3809a993845SKory Maincent (Dent Project) 	if (ret < 0)
3819a993845SKory Maincent (Dent Project) 		return ret;
3829a993845SKory Maincent (Dent Project) 
3839a993845SKory Maincent (Dent Project) 	priv->admin_state[id] = ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
3849a993845SKory Maincent (Dent Project) 
3859a993845SKory Maincent (Dent Project) 	return 0;
3869a993845SKory Maincent (Dent Project) }
3879a993845SKory Maincent (Dent Project) 
3889a993845SKory Maincent (Dent Project) static int pd692x0_pi_disable(struct pse_controller_dev *pcdev, int id)
3899a993845SKory Maincent (Dent Project) {
3909a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
3919a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
3929a993845SKory Maincent (Dent Project) 	int ret;
3939a993845SKory Maincent (Dent Project) 
3949a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_unavailable(priv);
3959a993845SKory Maincent (Dent Project) 	if (ret)
3969a993845SKory Maincent (Dent Project) 		return ret;
3979a993845SKory Maincent (Dent Project) 
3989a993845SKory Maincent (Dent Project) 	if (priv->admin_state[id] == ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED)
3999a993845SKory Maincent (Dent Project) 		return 0;
4009a993845SKory Maincent (Dent Project) 
4019a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_SET_PORT_PARAM];
4029a993845SKory Maincent (Dent Project) 	msg.data[0] = 0x0;
4039a993845SKory Maincent (Dent Project) 	msg.sub[2] = id;
4049a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
4059a993845SKory Maincent (Dent Project) 	if (ret < 0)
4069a993845SKory Maincent (Dent Project) 		return ret;
4079a993845SKory Maincent (Dent Project) 
4089a993845SKory Maincent (Dent Project) 	priv->admin_state[id] = ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
4099a993845SKory Maincent (Dent Project) 
4109a993845SKory Maincent (Dent Project) 	return 0;
4119a993845SKory Maincent (Dent Project) }
4129a993845SKory Maincent (Dent Project) 
4139a993845SKory Maincent (Dent Project) static int pd692x0_pi_is_enabled(struct pse_controller_dev *pcdev, int id)
4149a993845SKory Maincent (Dent Project) {
4159a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
4169a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
4179a993845SKory Maincent (Dent Project) 	int ret;
4189a993845SKory Maincent (Dent Project) 
4199a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_unavailable(priv);
4209a993845SKory Maincent (Dent Project) 	if (ret)
4219a993845SKory Maincent (Dent Project) 		return ret;
4229a993845SKory Maincent (Dent Project) 
4239a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_GET_PORT_STATUS];
4249a993845SKory Maincent (Dent Project) 	msg.sub[2] = id;
4259a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
4269a993845SKory Maincent (Dent Project) 	if (ret < 0)
4279a993845SKory Maincent (Dent Project) 		return ret;
4289a993845SKory Maincent (Dent Project) 
4299a993845SKory Maincent (Dent Project) 	if (buf.sub[1]) {
4309a993845SKory Maincent (Dent Project) 		priv->admin_state[id] = ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
4319a993845SKory Maincent (Dent Project) 		return 1;
4329a993845SKory Maincent (Dent Project) 	} else {
4339a993845SKory Maincent (Dent Project) 		priv->admin_state[id] = ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
4349a993845SKory Maincent (Dent Project) 		return 0;
4359a993845SKory Maincent (Dent Project) 	}
4369a993845SKory Maincent (Dent Project) }
4379a993845SKory Maincent (Dent Project) 
4389a993845SKory Maincent (Dent Project) static int pd692x0_ethtool_get_status(struct pse_controller_dev *pcdev,
4399a993845SKory Maincent (Dent Project) 				      unsigned long id,
4409a993845SKory Maincent (Dent Project) 				      struct netlink_ext_ack *extack,
4419a993845SKory Maincent (Dent Project) 				      struct pse_control_status *status)
4429a993845SKory Maincent (Dent Project) {
4439a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
4449a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
4459a993845SKory Maincent (Dent Project) 	int ret;
4469a993845SKory Maincent (Dent Project) 
4479a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_unavailable(priv);
4489a993845SKory Maincent (Dent Project) 	if (ret)
4499a993845SKory Maincent (Dent Project) 		return ret;
4509a993845SKory Maincent (Dent Project) 
4519a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_GET_PORT_STATUS];
4529a993845SKory Maincent (Dent Project) 	msg.sub[2] = id;
4539a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
4549a993845SKory Maincent (Dent Project) 	if (ret < 0)
4559a993845SKory Maincent (Dent Project) 		return ret;
4569a993845SKory Maincent (Dent Project) 
4579a993845SKory Maincent (Dent Project) 	/* Compare Port Status (Communication Protocol Document par. 7.1) */
4589a993845SKory Maincent (Dent Project) 	if ((buf.sub[0] & 0xf0) == 0x80 || (buf.sub[0] & 0xf0) == 0x90)
4599a993845SKory Maincent (Dent Project) 		status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING;
4609a993845SKory Maincent (Dent Project) 	else if (buf.sub[0] == 0x1b || buf.sub[0] == 0x22)
4619a993845SKory Maincent (Dent Project) 		status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_SEARCHING;
4629a993845SKory Maincent (Dent Project) 	else if (buf.sub[0] == 0x12)
4639a993845SKory Maincent (Dent Project) 		status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_FAULT;
4649a993845SKory Maincent (Dent Project) 	else
4659a993845SKory Maincent (Dent Project) 		status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_DISABLED;
4669a993845SKory Maincent (Dent Project) 
4679a993845SKory Maincent (Dent Project) 	if (buf.sub[1])
4689a993845SKory Maincent (Dent Project) 		status->c33_admin_state = ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
4699a993845SKory Maincent (Dent Project) 	else
4709a993845SKory Maincent (Dent Project) 		status->c33_admin_state = ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
4719a993845SKory Maincent (Dent Project) 
4729a993845SKory Maincent (Dent Project) 	priv->admin_state[id] = status->c33_admin_state;
4739a993845SKory Maincent (Dent Project) 
4749a993845SKory Maincent (Dent Project) 	return 0;
4759a993845SKory Maincent (Dent Project) }
4769a993845SKory Maincent (Dent Project) 
4779a993845SKory Maincent (Dent Project) static struct pd692x0_msg_ver pd692x0_get_sw_version(struct pd692x0_priv *priv)
4789a993845SKory Maincent (Dent Project) {
4799a993845SKory Maincent (Dent Project) 	struct device *dev = &priv->client->dev;
4809a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0};
4819a993845SKory Maincent (Dent Project) 	struct pd692x0_msg_ver ver = {0};
4829a993845SKory Maincent (Dent Project) 	int ret;
4839a993845SKory Maincent (Dent Project) 
4849a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_GET_SW_VER];
4859a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
4869a993845SKory Maincent (Dent Project) 	if (ret < 0) {
4879a993845SKory Maincent (Dent Project) 		dev_err(dev, "Failed to get PSE version (%pe)\n", ERR_PTR(ret));
4889a993845SKory Maincent (Dent Project) 		return ver;
4899a993845SKory Maincent (Dent Project) 	}
4909a993845SKory Maincent (Dent Project) 
4919a993845SKory Maincent (Dent Project) 	/* Extract version from the message */
4929a993845SKory Maincent (Dent Project) 	ver.prod = buf.sub[2];
4939a993845SKory Maincent (Dent Project) 	ver.maj_sw_ver = (buf.data[0] << 8 | buf.data[1]) / 100;
4949a993845SKory Maincent (Dent Project) 	ver.min_sw_ver = ((buf.data[0] << 8 | buf.data[1]) / 10) % 10;
4959a993845SKory Maincent (Dent Project) 	ver.pa_sw_ver = (buf.data[0] << 8 | buf.data[1]) % 10;
4969a993845SKory Maincent (Dent Project) 	ver.param = buf.data[2];
4979a993845SKory Maincent (Dent Project) 	ver.build = buf.data[3];
4989a993845SKory Maincent (Dent Project) 
4999a993845SKory Maincent (Dent Project) 	return ver;
5009a993845SKory Maincent (Dent Project) }
5019a993845SKory Maincent (Dent Project) 
5029a993845SKory Maincent (Dent Project) struct pd692x0_manager {
5039a993845SKory Maincent (Dent Project) 	struct device_node *port_node[PD692X0_MAX_MANAGER_PORTS];
5049a993845SKory Maincent (Dent Project) 	int nports;
5059a993845SKory Maincent (Dent Project) };
5069a993845SKory Maincent (Dent Project) 
5079a993845SKory Maincent (Dent Project) struct pd692x0_matrix {
5089a993845SKory Maincent (Dent Project) 	u8 hw_port_a;
5099a993845SKory Maincent (Dent Project) 	u8 hw_port_b;
5109a993845SKory Maincent (Dent Project) };
5119a993845SKory Maincent (Dent Project) 
5129a993845SKory Maincent (Dent Project) static int
5139a993845SKory Maincent (Dent Project) pd692x0_of_get_ports_manager(struct pd692x0_priv *priv,
5149a993845SKory Maincent (Dent Project) 			     struct pd692x0_manager *manager,
5159a993845SKory Maincent (Dent Project) 			     struct device_node *np)
5169a993845SKory Maincent (Dent Project) {
5179a993845SKory Maincent (Dent Project) 	struct device_node *node;
5189a993845SKory Maincent (Dent Project) 	int ret, nports, i;
5199a993845SKory Maincent (Dent Project) 
5209a993845SKory Maincent (Dent Project) 	nports = 0;
5219a993845SKory Maincent (Dent Project) 	for_each_child_of_node(np, node) {
5229a993845SKory Maincent (Dent Project) 		u32 port;
5239a993845SKory Maincent (Dent Project) 
5249a993845SKory Maincent (Dent Project) 		if (!of_node_name_eq(node, "port"))
5259a993845SKory Maincent (Dent Project) 			continue;
5269a993845SKory Maincent (Dent Project) 
5279a993845SKory Maincent (Dent Project) 		ret = of_property_read_u32(node, "reg", &port);
5289a993845SKory Maincent (Dent Project) 		if (ret)
5299a993845SKory Maincent (Dent Project) 			goto out;
5309a993845SKory Maincent (Dent Project) 
5319a993845SKory Maincent (Dent Project) 		if (port >= PD692X0_MAX_MANAGER_PORTS || port != nports) {
5329a993845SKory Maincent (Dent Project) 			dev_err(&priv->client->dev,
5339a993845SKory Maincent (Dent Project) 				"wrong number or order of manager ports (%d)\n",
5349a993845SKory Maincent (Dent Project) 				port);
5359a993845SKory Maincent (Dent Project) 			ret = -EINVAL;
5369a993845SKory Maincent (Dent Project) 			goto out;
5379a993845SKory Maincent (Dent Project) 		}
5389a993845SKory Maincent (Dent Project) 
5399a993845SKory Maincent (Dent Project) 		of_node_get(node);
5409a993845SKory Maincent (Dent Project) 		manager->port_node[port] = node;
5419a993845SKory Maincent (Dent Project) 		nports++;
5429a993845SKory Maincent (Dent Project) 	}
5439a993845SKory Maincent (Dent Project) 
5449a993845SKory Maincent (Dent Project) 	manager->nports = nports;
5459a993845SKory Maincent (Dent Project) 	return 0;
5469a993845SKory Maincent (Dent Project) 
5479a993845SKory Maincent (Dent Project) out:
5489a993845SKory Maincent (Dent Project) 	for (i = 0; i < nports; i++) {
5499a993845SKory Maincent (Dent Project) 		of_node_put(manager->port_node[i]);
5509a993845SKory Maincent (Dent Project) 		manager->port_node[i] = NULL;
5519a993845SKory Maincent (Dent Project) 	}
5529a993845SKory Maincent (Dent Project) 	of_node_put(node);
5539a993845SKory Maincent (Dent Project) 	return ret;
5549a993845SKory Maincent (Dent Project) }
5559a993845SKory Maincent (Dent Project) 
5569a993845SKory Maincent (Dent Project) static int
5579a993845SKory Maincent (Dent Project) pd692x0_of_get_managers(struct pd692x0_priv *priv,
5589a993845SKory Maincent (Dent Project) 			struct pd692x0_manager manager[PD692X0_MAX_MANAGERS])
5599a993845SKory Maincent (Dent Project) {
5609a993845SKory Maincent (Dent Project) 	struct device_node *managers_node, *node;
5619a993845SKory Maincent (Dent Project) 	int ret, nmanagers, i, j;
5629a993845SKory Maincent (Dent Project) 
5639a993845SKory Maincent (Dent Project) 	if (!priv->np)
5649a993845SKory Maincent (Dent Project) 		return -EINVAL;
5659a993845SKory Maincent (Dent Project) 
5669a993845SKory Maincent (Dent Project) 	nmanagers = 0;
5679a993845SKory Maincent (Dent Project) 	managers_node = of_get_child_by_name(priv->np, "managers");
5689a993845SKory Maincent (Dent Project) 	if (!managers_node)
5699a993845SKory Maincent (Dent Project) 		return -EINVAL;
5709a993845SKory Maincent (Dent Project) 
5719a993845SKory Maincent (Dent Project) 	for_each_child_of_node(managers_node, node) {
5729a993845SKory Maincent (Dent Project) 		u32 manager_id;
5739a993845SKory Maincent (Dent Project) 
5749a993845SKory Maincent (Dent Project) 		if (!of_node_name_eq(node, "manager"))
5759a993845SKory Maincent (Dent Project) 			continue;
5769a993845SKory Maincent (Dent Project) 
5779a993845SKory Maincent (Dent Project) 		ret = of_property_read_u32(node, "reg", &manager_id);
5789a993845SKory Maincent (Dent Project) 		if (ret)
5799a993845SKory Maincent (Dent Project) 			goto out;
5809a993845SKory Maincent (Dent Project) 
5819a993845SKory Maincent (Dent Project) 		if (manager_id >= PD692X0_MAX_MANAGERS ||
5829a993845SKory Maincent (Dent Project) 		    manager_id != nmanagers) {
5839a993845SKory Maincent (Dent Project) 			dev_err(&priv->client->dev,
5849a993845SKory Maincent (Dent Project) 				"wrong number or order of managers (%d)\n",
5859a993845SKory Maincent (Dent Project) 				manager_id);
5869a993845SKory Maincent (Dent Project) 			ret = -EINVAL;
5879a993845SKory Maincent (Dent Project) 			goto out;
5889a993845SKory Maincent (Dent Project) 		}
5899a993845SKory Maincent (Dent Project) 
5909a993845SKory Maincent (Dent Project) 		ret = pd692x0_of_get_ports_manager(priv, &manager[manager_id],
5919a993845SKory Maincent (Dent Project) 						   node);
5929a993845SKory Maincent (Dent Project) 		if (ret)
5939a993845SKory Maincent (Dent Project) 			goto out;
5949a993845SKory Maincent (Dent Project) 
5959a993845SKory Maincent (Dent Project) 		nmanagers++;
5969a993845SKory Maincent (Dent Project) 	}
5979a993845SKory Maincent (Dent Project) 
5989a993845SKory Maincent (Dent Project) 	of_node_put(managers_node);
5999a993845SKory Maincent (Dent Project) 	return nmanagers;
6009a993845SKory Maincent (Dent Project) 
6019a993845SKory Maincent (Dent Project) out:
6029a993845SKory Maincent (Dent Project) 	for (i = 0; i < nmanagers; i++) {
6039a993845SKory Maincent (Dent Project) 		for (j = 0; j < manager[i].nports; j++) {
6049a993845SKory Maincent (Dent Project) 			of_node_put(manager[i].port_node[j]);
6059a993845SKory Maincent (Dent Project) 			manager[i].port_node[j] = NULL;
6069a993845SKory Maincent (Dent Project) 		}
6079a993845SKory Maincent (Dent Project) 	}
6089a993845SKory Maincent (Dent Project) 
6099a993845SKory Maincent (Dent Project) 	of_node_put(node);
6109a993845SKory Maincent (Dent Project) 	of_node_put(managers_node);
6119a993845SKory Maincent (Dent Project) 	return ret;
6129a993845SKory Maincent (Dent Project) }
6139a993845SKory Maincent (Dent Project) 
6149a993845SKory Maincent (Dent Project) static int
6159a993845SKory Maincent (Dent Project) pd692x0_set_port_matrix(const struct pse_pi_pairset *pairset,
6169a993845SKory Maincent (Dent Project) 			const struct pd692x0_manager *manager,
6179a993845SKory Maincent (Dent Project) 			int nmanagers, struct pd692x0_matrix *port_matrix)
6189a993845SKory Maincent (Dent Project) {
6199a993845SKory Maincent (Dent Project) 	int i, j, port_cnt;
6209a993845SKory Maincent (Dent Project) 	bool found = false;
6219a993845SKory Maincent (Dent Project) 
6229a993845SKory Maincent (Dent Project) 	if (!pairset->np)
6239a993845SKory Maincent (Dent Project) 		return 0;
6249a993845SKory Maincent (Dent Project) 
6259a993845SKory Maincent (Dent Project) 	/* Look on every managers */
6269a993845SKory Maincent (Dent Project) 	port_cnt = 0;
6279a993845SKory Maincent (Dent Project) 	for (i = 0; i < nmanagers; i++) {
6289a993845SKory Maincent (Dent Project) 		/* Look on every ports of the manager */
6299a993845SKory Maincent (Dent Project) 		for (j = 0; j < manager[i].nports; j++) {
6309a993845SKory Maincent (Dent Project) 			if (pairset->np == manager[i].port_node[j]) {
6319a993845SKory Maincent (Dent Project) 				found = true;
6329a993845SKory Maincent (Dent Project) 				break;
6339a993845SKory Maincent (Dent Project) 			}
6349a993845SKory Maincent (Dent Project) 		}
6359a993845SKory Maincent (Dent Project) 		port_cnt += j;
6369a993845SKory Maincent (Dent Project) 
6379a993845SKory Maincent (Dent Project) 		if (found)
6389a993845SKory Maincent (Dent Project) 			break;
6399a993845SKory Maincent (Dent Project) 	}
6409a993845SKory Maincent (Dent Project) 
6419a993845SKory Maincent (Dent Project) 	if (!found)
6429a993845SKory Maincent (Dent Project) 		return -ENODEV;
6439a993845SKory Maincent (Dent Project) 
6449a993845SKory Maincent (Dent Project) 	if (pairset->pinout == ALTERNATIVE_A)
6459a993845SKory Maincent (Dent Project) 		port_matrix->hw_port_a = port_cnt;
6469a993845SKory Maincent (Dent Project) 	else if (pairset->pinout == ALTERNATIVE_B)
6479a993845SKory Maincent (Dent Project) 		port_matrix->hw_port_b = port_cnt;
6489a993845SKory Maincent (Dent Project) 
6499a993845SKory Maincent (Dent Project) 	return 0;
6509a993845SKory Maincent (Dent Project) }
6519a993845SKory Maincent (Dent Project) 
6529a993845SKory Maincent (Dent Project) static int
6539a993845SKory Maincent (Dent Project) pd692x0_set_ports_matrix(struct pd692x0_priv *priv,
6549a993845SKory Maincent (Dent Project) 			 const struct pd692x0_manager *manager,
6559a993845SKory Maincent (Dent Project) 			 int nmanagers,
6569a993845SKory Maincent (Dent Project) 			 struct pd692x0_matrix port_matrix[PD692X0_MAX_PIS])
6579a993845SKory Maincent (Dent Project) {
6589a993845SKory Maincent (Dent Project) 	struct pse_controller_dev *pcdev = &priv->pcdev;
6599a993845SKory Maincent (Dent Project) 	int i, ret;
6609a993845SKory Maincent (Dent Project) 
6619a993845SKory Maincent (Dent Project) 	/* Init Matrix */
6629a993845SKory Maincent (Dent Project) 	for (i = 0; i < PD692X0_MAX_PIS; i++) {
6639a993845SKory Maincent (Dent Project) 		port_matrix[i].hw_port_a = 0xff;
6649a993845SKory Maincent (Dent Project) 		port_matrix[i].hw_port_b = 0xff;
6659a993845SKory Maincent (Dent Project) 	}
6669a993845SKory Maincent (Dent Project) 
6679a993845SKory Maincent (Dent Project) 	/* Update with values for every PSE PIs */
6689a993845SKory Maincent (Dent Project) 	for (i = 0; i < pcdev->nr_lines; i++) {
6699a993845SKory Maincent (Dent Project) 		ret = pd692x0_set_port_matrix(&pcdev->pi[i].pairset[0],
6709a993845SKory Maincent (Dent Project) 					      manager, nmanagers,
6719a993845SKory Maincent (Dent Project) 					      &port_matrix[i]);
6729a993845SKory Maincent (Dent Project) 		if (ret) {
6739a993845SKory Maincent (Dent Project) 			dev_err(&priv->client->dev,
6749a993845SKory Maincent (Dent Project) 				"unable to configure pi %d pairset 0", i);
6759a993845SKory Maincent (Dent Project) 			return ret;
6769a993845SKory Maincent (Dent Project) 		}
6779a993845SKory Maincent (Dent Project) 
6789a993845SKory Maincent (Dent Project) 		ret = pd692x0_set_port_matrix(&pcdev->pi[i].pairset[1],
6799a993845SKory Maincent (Dent Project) 					      manager, nmanagers,
6809a993845SKory Maincent (Dent Project) 					      &port_matrix[i]);
6819a993845SKory Maincent (Dent Project) 		if (ret) {
6829a993845SKory Maincent (Dent Project) 			dev_err(&priv->client->dev,
6839a993845SKory Maincent (Dent Project) 				"unable to configure pi %d pairset 1", i);
6849a993845SKory Maincent (Dent Project) 			return ret;
6859a993845SKory Maincent (Dent Project) 		}
6869a993845SKory Maincent (Dent Project) 	}
6879a993845SKory Maincent (Dent Project) 
6889a993845SKory Maincent (Dent Project) 	return 0;
6899a993845SKory Maincent (Dent Project) }
6909a993845SKory Maincent (Dent Project) 
6919a993845SKory Maincent (Dent Project) static int
6929a993845SKory Maincent (Dent Project) pd692x0_write_ports_matrix(struct pd692x0_priv *priv,
6939a993845SKory Maincent (Dent Project) 			   const struct pd692x0_matrix port_matrix[PD692X0_MAX_PIS])
6949a993845SKory Maincent (Dent Project) {
6959a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf;
6969a993845SKory Maincent (Dent Project) 	int ret, i;
6979a993845SKory Maincent (Dent Project) 
6989a993845SKory Maincent (Dent Project) 	/* Write temporary Matrix */
6999a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_SET_TMP_PORT_MATRIX];
7009a993845SKory Maincent (Dent Project) 	for (i = 0; i < PD692X0_MAX_PIS; i++) {
7019a993845SKory Maincent (Dent Project) 		msg.sub[2] = i;
7029a993845SKory Maincent (Dent Project) 		msg.data[0] = port_matrix[i].hw_port_b;
7039a993845SKory Maincent (Dent Project) 		msg.data[1] = port_matrix[i].hw_port_a;
7049a993845SKory Maincent (Dent Project) 
7059a993845SKory Maincent (Dent Project) 		ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
7069a993845SKory Maincent (Dent Project) 		if (ret < 0)
7079a993845SKory Maincent (Dent Project) 			return ret;
7089a993845SKory Maincent (Dent Project) 	}
7099a993845SKory Maincent (Dent Project) 
7109a993845SKory Maincent (Dent Project) 	/* Program Matrix */
7119a993845SKory Maincent (Dent Project) 	msg = pd692x0_msg_template_list[PD692X0_MSG_PRG_PORT_MATRIX];
7129a993845SKory Maincent (Dent Project) 	ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
7139a993845SKory Maincent (Dent Project) 	if (ret < 0)
7149a993845SKory Maincent (Dent Project) 		return ret;
7159a993845SKory Maincent (Dent Project) 
7169a993845SKory Maincent (Dent Project) 	return 0;
7179a993845SKory Maincent (Dent Project) }
7189a993845SKory Maincent (Dent Project) 
7199a993845SKory Maincent (Dent Project) static int pd692x0_setup_pi_matrix(struct pse_controller_dev *pcdev)
7209a993845SKory Maincent (Dent Project) {
7219a993845SKory Maincent (Dent Project) 	struct pd692x0_manager manager[PD692X0_MAX_MANAGERS] = {0};
7229a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = to_pd692x0_priv(pcdev);
7239a993845SKory Maincent (Dent Project) 	struct pd692x0_matrix port_matrix[PD692X0_MAX_PIS];
7249a993845SKory Maincent (Dent Project) 	int ret, i, j, nmanagers;
7259a993845SKory Maincent (Dent Project) 
7269a993845SKory Maincent (Dent Project) 	/* Should we flash the port matrix */
7279a993845SKory Maincent (Dent Project) 	if (priv->fw_state != PD692X0_FW_OK &&
7289a993845SKory Maincent (Dent Project) 	    priv->fw_state != PD692X0_FW_COMPLETE)
7299a993845SKory Maincent (Dent Project) 		return 0;
7309a993845SKory Maincent (Dent Project) 
7319a993845SKory Maincent (Dent Project) 	ret = pd692x0_of_get_managers(priv, manager);
7329a993845SKory Maincent (Dent Project) 	if (ret < 0)
7339a993845SKory Maincent (Dent Project) 		return ret;
7349a993845SKory Maincent (Dent Project) 
7359a993845SKory Maincent (Dent Project) 	nmanagers = ret;
7369a993845SKory Maincent (Dent Project) 	ret = pd692x0_set_ports_matrix(priv, manager, nmanagers, port_matrix);
7379a993845SKory Maincent (Dent Project) 	if (ret)
7389a993845SKory Maincent (Dent Project) 		goto out;
7399a993845SKory Maincent (Dent Project) 
7409a993845SKory Maincent (Dent Project) 	ret = pd692x0_write_ports_matrix(priv, port_matrix);
7419a993845SKory Maincent (Dent Project) 	if (ret)
7429a993845SKory Maincent (Dent Project) 		goto out;
7439a993845SKory Maincent (Dent Project) 
7449a993845SKory Maincent (Dent Project) out:
7459a993845SKory Maincent (Dent Project) 	for (i = 0; i < nmanagers; i++) {
7469a993845SKory Maincent (Dent Project) 		for (j = 0; j < manager[i].nports; j++)
7479a993845SKory Maincent (Dent Project) 			of_node_put(manager[i].port_node[j]);
7489a993845SKory Maincent (Dent Project) 	}
7499a993845SKory Maincent (Dent Project) 	return ret;
7509a993845SKory Maincent (Dent Project) }
7519a993845SKory Maincent (Dent Project) 
7529a993845SKory Maincent (Dent Project) static const struct pse_controller_ops pd692x0_ops = {
7539a993845SKory Maincent (Dent Project) 	.setup_pi_matrix = pd692x0_setup_pi_matrix,
7549a993845SKory Maincent (Dent Project) 	.ethtool_get_status = pd692x0_ethtool_get_status,
7559a993845SKory Maincent (Dent Project) 	.pi_enable = pd692x0_pi_enable,
7569a993845SKory Maincent (Dent Project) 	.pi_disable = pd692x0_pi_disable,
7579a993845SKory Maincent (Dent Project) 	.pi_is_enabled = pd692x0_pi_is_enabled,
7589a993845SKory Maincent (Dent Project) };
7599a993845SKory Maincent (Dent Project) 
7609a993845SKory Maincent (Dent Project) #define PD692X0_FW_LINE_MAX_SZ 0xff
7619a993845SKory Maincent (Dent Project) static int pd692x0_fw_get_next_line(const u8 *data,
7629a993845SKory Maincent (Dent Project) 				    char *line, size_t size)
7639a993845SKory Maincent (Dent Project) {
7649a993845SKory Maincent (Dent Project) 	size_t line_size;
7659a993845SKory Maincent (Dent Project) 	int i;
7669a993845SKory Maincent (Dent Project) 
7679a993845SKory Maincent (Dent Project) 	line_size = min_t(size_t, size, PD692X0_FW_LINE_MAX_SZ);
7689a993845SKory Maincent (Dent Project) 
7699a993845SKory Maincent (Dent Project) 	memset(line, 0, PD692X0_FW_LINE_MAX_SZ);
7709a993845SKory Maincent (Dent Project) 	for (i = 0; i < line_size - 1; i++) {
7719a993845SKory Maincent (Dent Project) 		if (*data == '\r' && *(data + 1) == '\n') {
7729a993845SKory Maincent (Dent Project) 			line[i] = '\r';
7739a993845SKory Maincent (Dent Project) 			line[i + 1] = '\n';
7749a993845SKory Maincent (Dent Project) 			return i + 2;
7759a993845SKory Maincent (Dent Project) 		}
7769a993845SKory Maincent (Dent Project) 		line[i] = *data;
7779a993845SKory Maincent (Dent Project) 		data++;
7789a993845SKory Maincent (Dent Project) 	}
7799a993845SKory Maincent (Dent Project) 
7809a993845SKory Maincent (Dent Project) 	return -EIO;
7819a993845SKory Maincent (Dent Project) }
7829a993845SKory Maincent (Dent Project) 
7839a993845SKory Maincent (Dent Project) static enum fw_upload_err
7849a993845SKory Maincent (Dent Project) pd692x0_fw_recv_resp(const struct i2c_client *client, unsigned long ms_timeout,
7859a993845SKory Maincent (Dent Project) 		     const char *msg_ok, unsigned int msg_size)
7869a993845SKory Maincent (Dent Project) {
7879a993845SKory Maincent (Dent Project) 	/* Maximum controller response size */
7889a993845SKory Maincent (Dent Project) 	char fw_msg_buf[5] = {0};
7899a993845SKory Maincent (Dent Project) 	unsigned long timeout;
7909a993845SKory Maincent (Dent Project) 	int ret;
7919a993845SKory Maincent (Dent Project) 
7929a993845SKory Maincent (Dent Project) 	if (msg_size > sizeof(fw_msg_buf))
7939a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_RW_ERROR;
7949a993845SKory Maincent (Dent Project) 
7959a993845SKory Maincent (Dent Project) 	/* Read until we get something */
7969a993845SKory Maincent (Dent Project) 	timeout = msecs_to_jiffies(ms_timeout) + jiffies;
7979a993845SKory Maincent (Dent Project) 	while (true) {
7989a993845SKory Maincent (Dent Project) 		if (time_is_before_jiffies(timeout))
7999a993845SKory Maincent (Dent Project) 			return FW_UPLOAD_ERR_TIMEOUT;
8009a993845SKory Maincent (Dent Project) 
8019a993845SKory Maincent (Dent Project) 		ret = i2c_master_recv(client, fw_msg_buf, 1);
8029a993845SKory Maincent (Dent Project) 		if (ret < 0 || *fw_msg_buf == 0) {
8039a993845SKory Maincent (Dent Project) 			usleep_range(1000, 2000);
8049a993845SKory Maincent (Dent Project) 			continue;
8059a993845SKory Maincent (Dent Project) 		} else {
8069a993845SKory Maincent (Dent Project) 			break;
8079a993845SKory Maincent (Dent Project) 		}
8089a993845SKory Maincent (Dent Project) 	}
8099a993845SKory Maincent (Dent Project) 
8109a993845SKory Maincent (Dent Project) 	/* Read remaining characters */
8119a993845SKory Maincent (Dent Project) 	ret = i2c_master_recv(client, fw_msg_buf + 1, msg_size - 1);
8129a993845SKory Maincent (Dent Project) 	if (strncmp(fw_msg_buf, msg_ok, msg_size)) {
8139a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
8149a993845SKory Maincent (Dent Project) 			"Wrong FW download process answer (%*pE)\n",
8159a993845SKory Maincent (Dent Project) 			msg_size, fw_msg_buf);
8169a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_HW_ERROR;
8179a993845SKory Maincent (Dent Project) 	}
8189a993845SKory Maincent (Dent Project) 
8199a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
8209a993845SKory Maincent (Dent Project) }
8219a993845SKory Maincent (Dent Project) 
8229a993845SKory Maincent (Dent Project) static int pd692x0_fw_write_line(const struct i2c_client *client,
8239a993845SKory Maincent (Dent Project) 				 const char line[PD692X0_FW_LINE_MAX_SZ],
8249a993845SKory Maincent (Dent Project) 				 const bool last_line)
8259a993845SKory Maincent (Dent Project) {
8269a993845SKory Maincent (Dent Project) 	int ret;
8279a993845SKory Maincent (Dent Project) 
8289a993845SKory Maincent (Dent Project) 	while (*line != 0) {
8299a993845SKory Maincent (Dent Project) 		ret = i2c_master_send(client, line, 1);
8309a993845SKory Maincent (Dent Project) 		if (ret < 0)
8319a993845SKory Maincent (Dent Project) 			return FW_UPLOAD_ERR_RW_ERROR;
8329a993845SKory Maincent (Dent Project) 		line++;
8339a993845SKory Maincent (Dent Project) 	}
8349a993845SKory Maincent (Dent Project) 
8359a993845SKory Maincent (Dent Project) 	if (last_line) {
8369a993845SKory Maincent (Dent Project) 		ret = pd692x0_fw_recv_resp(client, 100, "TP\r\n",
8379a993845SKory Maincent (Dent Project) 					   sizeof("TP\r\n") - 1);
8389a993845SKory Maincent (Dent Project) 		if (ret)
8399a993845SKory Maincent (Dent Project) 			return ret;
8409a993845SKory Maincent (Dent Project) 	} else {
8419a993845SKory Maincent (Dent Project) 		ret = pd692x0_fw_recv_resp(client, 100, "T*\r\n",
8429a993845SKory Maincent (Dent Project) 					   sizeof("T*\r\n") - 1);
8439a993845SKory Maincent (Dent Project) 		if (ret)
8449a993845SKory Maincent (Dent Project) 			return ret;
8459a993845SKory Maincent (Dent Project) 	}
8469a993845SKory Maincent (Dent Project) 
8479a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
8489a993845SKory Maincent (Dent Project) }
8499a993845SKory Maincent (Dent Project) 
8509a993845SKory Maincent (Dent Project) static enum fw_upload_err pd692x0_fw_reset(const struct i2c_client *client)
8519a993845SKory Maincent (Dent Project) {
8529a993845SKory Maincent (Dent Project) 	const struct pd692x0_msg zero = {0};
8539a993845SKory Maincent (Dent Project) 	struct pd692x0_msg buf = {0};
8549a993845SKory Maincent (Dent Project) 	unsigned long timeout;
8559a993845SKory Maincent (Dent Project) 	char cmd[] = "RST";
8569a993845SKory Maincent (Dent Project) 	int ret;
8579a993845SKory Maincent (Dent Project) 
8589a993845SKory Maincent (Dent Project) 	ret = i2c_master_send(client, cmd, strlen(cmd));
8599a993845SKory Maincent (Dent Project) 	if (ret < 0) {
8609a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
8619a993845SKory Maincent (Dent Project) 			"Failed to reset the controller (%pe)\n",
8629a993845SKory Maincent (Dent Project) 			ERR_PTR(ret));
8639a993845SKory Maincent (Dent Project) 		return ret;
8649a993845SKory Maincent (Dent Project) 	}
8659a993845SKory Maincent (Dent Project) 
8669a993845SKory Maincent (Dent Project) 	timeout = msecs_to_jiffies(10000) + jiffies;
8679a993845SKory Maincent (Dent Project) 	while (true) {
8689a993845SKory Maincent (Dent Project) 		if (time_is_before_jiffies(timeout))
8699a993845SKory Maincent (Dent Project) 			return FW_UPLOAD_ERR_TIMEOUT;
8709a993845SKory Maincent (Dent Project) 
8719a993845SKory Maincent (Dent Project) 		ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
8729a993845SKory Maincent (Dent Project) 		if (ret < 0 ||
8739a993845SKory Maincent (Dent Project) 		    !memcmp(&buf, &zero, sizeof(buf)))
8749a993845SKory Maincent (Dent Project) 			usleep_range(1000, 2000);
8759a993845SKory Maincent (Dent Project) 		else
8769a993845SKory Maincent (Dent Project) 			break;
8779a993845SKory Maincent (Dent Project) 	}
8789a993845SKory Maincent (Dent Project) 
8799a993845SKory Maincent (Dent Project) 	/* Is the reply a successful report message */
8809a993845SKory Maincent (Dent Project) 	if (buf.key != PD692X0_KEY_TLM || buf.echo != 0xff ||
8819a993845SKory Maincent (Dent Project) 	    buf.sub[0] & 0x01) {
8829a993845SKory Maincent (Dent Project) 		dev_err(&client->dev, "PSE controller error\n");
8839a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_HW_ERROR;
8849a993845SKory Maincent (Dent Project) 	}
8859a993845SKory Maincent (Dent Project) 
8869a993845SKory Maincent (Dent Project) 	/* Is the firmware operational */
8879a993845SKory Maincent (Dent Project) 	if (buf.sub[0] & 0x02) {
8889a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
8899a993845SKory Maincent (Dent Project) 			"PSE firmware error. Please update it.\n");
8909a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_HW_ERROR;
8919a993845SKory Maincent (Dent Project) 	}
8929a993845SKory Maincent (Dent Project) 
8939a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
8949a993845SKory Maincent (Dent Project) }
8959a993845SKory Maincent (Dent Project) 
8969a993845SKory Maincent (Dent Project) static enum fw_upload_err pd692x0_fw_prepare(struct fw_upload *fwl,
8979a993845SKory Maincent (Dent Project) 					     const u8 *data, u32 size)
8989a993845SKory Maincent (Dent Project) {
8999a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = fwl->dd_handle;
9009a993845SKory Maincent (Dent Project) 	const struct i2c_client *client = priv->client;
9019a993845SKory Maincent (Dent Project) 	enum pd692x0_fw_state last_fw_state;
9029a993845SKory Maincent (Dent Project) 	int ret;
9039a993845SKory Maincent (Dent Project) 
9049a993845SKory Maincent (Dent Project) 	priv->cancel_request = false;
9059a993845SKory Maincent (Dent Project) 	last_fw_state = priv->fw_state;
9069a993845SKory Maincent (Dent Project) 
9079a993845SKory Maincent (Dent Project) 	priv->fw_state = PD692X0_FW_PREPARE;
9089a993845SKory Maincent (Dent Project) 
9099a993845SKory Maincent (Dent Project) 	/* Enter program mode */
9109a993845SKory Maincent (Dent Project) 	if (last_fw_state == PD692X0_FW_BROKEN) {
9119a993845SKory Maincent (Dent Project) 		const char *msg = "ENTR";
9129a993845SKory Maincent (Dent Project) 		const char *c;
9139a993845SKory Maincent (Dent Project) 
9149a993845SKory Maincent (Dent Project) 		c = msg;
9159a993845SKory Maincent (Dent Project) 		do {
9169a993845SKory Maincent (Dent Project) 			ret = i2c_master_send(client, c, 1);
9179a993845SKory Maincent (Dent Project) 			if (ret < 0)
9189a993845SKory Maincent (Dent Project) 				return FW_UPLOAD_ERR_RW_ERROR;
9199a993845SKory Maincent (Dent Project) 			if (*(c + 1))
9209a993845SKory Maincent (Dent Project) 				usleep_range(10000, 20000);
9219a993845SKory Maincent (Dent Project) 		} while (*(++c));
9229a993845SKory Maincent (Dent Project) 	} else {
9239a993845SKory Maincent (Dent Project) 		struct pd692x0_msg msg, buf;
9249a993845SKory Maincent (Dent Project) 
9259a993845SKory Maincent (Dent Project) 		msg = pd692x0_msg_template_list[PD692X0_MSG_DOWNLOAD_CMD];
9269a993845SKory Maincent (Dent Project) 		ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
9279a993845SKory Maincent (Dent Project) 		if (ret < 0) {
9289a993845SKory Maincent (Dent Project) 			dev_err(&client->dev,
9299a993845SKory Maincent (Dent Project) 				"Failed to enter programming mode (%pe)\n",
9309a993845SKory Maincent (Dent Project) 				ERR_PTR(ret));
9319a993845SKory Maincent (Dent Project) 			return FW_UPLOAD_ERR_RW_ERROR;
9329a993845SKory Maincent (Dent Project) 		}
9339a993845SKory Maincent (Dent Project) 	}
9349a993845SKory Maincent (Dent Project) 
9359a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_recv_resp(client, 100, "TPE\r\n", sizeof("TPE\r\n") - 1);
9369a993845SKory Maincent (Dent Project) 	if (ret)
9379a993845SKory Maincent (Dent Project) 		goto err_out;
9389a993845SKory Maincent (Dent Project) 
9399a993845SKory Maincent (Dent Project) 	if (priv->cancel_request) {
9409a993845SKory Maincent (Dent Project) 		ret = FW_UPLOAD_ERR_CANCELED;
9419a993845SKory Maincent (Dent Project) 		goto err_out;
9429a993845SKory Maincent (Dent Project) 	}
9439a993845SKory Maincent (Dent Project) 
9449a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
9459a993845SKory Maincent (Dent Project) 
9469a993845SKory Maincent (Dent Project) err_out:
9479a993845SKory Maincent (Dent Project) 	pd692x0_fw_reset(priv->client);
9489a993845SKory Maincent (Dent Project) 	priv->fw_state = last_fw_state;
9499a993845SKory Maincent (Dent Project) 	return ret;
9509a993845SKory Maincent (Dent Project) }
9519a993845SKory Maincent (Dent Project) 
9529a993845SKory Maincent (Dent Project) static enum fw_upload_err pd692x0_fw_write(struct fw_upload *fwl,
9539a993845SKory Maincent (Dent Project) 					   const u8 *data, u32 offset,
9549a993845SKory Maincent (Dent Project) 					   u32 size, u32 *written)
9559a993845SKory Maincent (Dent Project) {
9569a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = fwl->dd_handle;
9579a993845SKory Maincent (Dent Project) 	char line[PD692X0_FW_LINE_MAX_SZ];
9589a993845SKory Maincent (Dent Project) 	const struct i2c_client *client;
9599a993845SKory Maincent (Dent Project) 	int ret, i;
9609a993845SKory Maincent (Dent Project) 	char cmd;
9619a993845SKory Maincent (Dent Project) 
9629a993845SKory Maincent (Dent Project) 	client = priv->client;
9639a993845SKory Maincent (Dent Project) 	priv->fw_state = PD692X0_FW_WRITE;
9649a993845SKory Maincent (Dent Project) 
9659a993845SKory Maincent (Dent Project) 	/* Erase */
9669a993845SKory Maincent (Dent Project) 	cmd = 'E';
9679a993845SKory Maincent (Dent Project) 	ret = i2c_master_send(client, &cmd, 1);
9689a993845SKory Maincent (Dent Project) 	if (ret < 0) {
9699a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
9709a993845SKory Maincent (Dent Project) 			"Failed to boot programming mode (%pe)\n",
9719a993845SKory Maincent (Dent Project) 			ERR_PTR(ret));
9729a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_RW_ERROR;
9739a993845SKory Maincent (Dent Project) 	}
9749a993845SKory Maincent (Dent Project) 
9759a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_recv_resp(client, 100, "TOE\r\n", sizeof("TOE\r\n") - 1);
9769a993845SKory Maincent (Dent Project) 	if (ret)
9779a993845SKory Maincent (Dent Project) 		return ret;
9789a993845SKory Maincent (Dent Project) 
9799a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_recv_resp(client, 5000, "TE\r\n", sizeof("TE\r\n") - 1);
9809a993845SKory Maincent (Dent Project) 	if (ret)
9819a993845SKory Maincent (Dent Project) 		dev_warn(&client->dev,
9829a993845SKory Maincent (Dent Project) 			 "Failed to erase internal memory, however still try to write Firmware\n");
9839a993845SKory Maincent (Dent Project) 
9849a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_recv_resp(client, 100, "TPE\r\n", sizeof("TPE\r\n") - 1);
9859a993845SKory Maincent (Dent Project) 	if (ret)
9869a993845SKory Maincent (Dent Project) 		dev_warn(&client->dev,
9879a993845SKory Maincent (Dent Project) 			 "Failed to erase internal memory, however still try to write Firmware\n");
9889a993845SKory Maincent (Dent Project) 
9899a993845SKory Maincent (Dent Project) 	if (priv->cancel_request)
9909a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_CANCELED;
9919a993845SKory Maincent (Dent Project) 
9929a993845SKory Maincent (Dent Project) 	/* Program */
9939a993845SKory Maincent (Dent Project) 	cmd = 'P';
9949a993845SKory Maincent (Dent Project) 	ret = i2c_master_send(client, &cmd, sizeof(char));
9959a993845SKory Maincent (Dent Project) 	if (ret < 0) {
9969a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
9979a993845SKory Maincent (Dent Project) 			"Failed to boot programming mode (%pe)\n",
9989a993845SKory Maincent (Dent Project) 			ERR_PTR(ret));
9999a993845SKory Maincent (Dent Project) 		return ret;
10009a993845SKory Maincent (Dent Project) 	}
10019a993845SKory Maincent (Dent Project) 
10029a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_recv_resp(client, 100, "TOP\r\n", sizeof("TOP\r\n") - 1);
10039a993845SKory Maincent (Dent Project) 	if (ret)
10049a993845SKory Maincent (Dent Project) 		return ret;
10059a993845SKory Maincent (Dent Project) 
10069a993845SKory Maincent (Dent Project) 	i = 0;
10079a993845SKory Maincent (Dent Project) 	while (i < size) {
10089a993845SKory Maincent (Dent Project) 		ret = pd692x0_fw_get_next_line(data, line, size - i);
10099a993845SKory Maincent (Dent Project) 		if (ret < 0) {
10109a993845SKory Maincent (Dent Project) 			ret = FW_UPLOAD_ERR_FW_INVALID;
10119a993845SKory Maincent (Dent Project) 			goto err;
10129a993845SKory Maincent (Dent Project) 		}
10139a993845SKory Maincent (Dent Project) 
10149a993845SKory Maincent (Dent Project) 		i += ret;
10159a993845SKory Maincent (Dent Project) 		data += ret;
10169a993845SKory Maincent (Dent Project) 		if (line[0] == 'S' && line[1] == '0') {
10179a993845SKory Maincent (Dent Project) 			continue;
10189a993845SKory Maincent (Dent Project) 		} else if (line[0] == 'S' && line[1] == '7') {
10199a993845SKory Maincent (Dent Project) 			ret = pd692x0_fw_write_line(client, line, true);
10209a993845SKory Maincent (Dent Project) 			if (ret)
10219a993845SKory Maincent (Dent Project) 				goto err;
10229a993845SKory Maincent (Dent Project) 		} else {
10239a993845SKory Maincent (Dent Project) 			ret = pd692x0_fw_write_line(client, line, false);
10249a993845SKory Maincent (Dent Project) 			if (ret)
10259a993845SKory Maincent (Dent Project) 				goto err;
10269a993845SKory Maincent (Dent Project) 		}
10279a993845SKory Maincent (Dent Project) 
10289a993845SKory Maincent (Dent Project) 		if (priv->cancel_request) {
10299a993845SKory Maincent (Dent Project) 			ret = FW_UPLOAD_ERR_CANCELED;
10309a993845SKory Maincent (Dent Project) 			goto err;
10319a993845SKory Maincent (Dent Project) 		}
10329a993845SKory Maincent (Dent Project) 	}
10339a993845SKory Maincent (Dent Project) 	*written = i;
10349a993845SKory Maincent (Dent Project) 
10359a993845SKory Maincent (Dent Project) 	msleep(400);
10369a993845SKory Maincent (Dent Project) 
10379a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
10389a993845SKory Maincent (Dent Project) 
10399a993845SKory Maincent (Dent Project) err:
10409a993845SKory Maincent (Dent Project) 	strscpy_pad(line, "S7\r\n", sizeof(line));
10419a993845SKory Maincent (Dent Project) 	pd692x0_fw_write_line(client, line, true);
10429a993845SKory Maincent (Dent Project) 	return ret;
10439a993845SKory Maincent (Dent Project) }
10449a993845SKory Maincent (Dent Project) 
10459a993845SKory Maincent (Dent Project) static enum fw_upload_err pd692x0_fw_poll_complete(struct fw_upload *fwl)
10469a993845SKory Maincent (Dent Project) {
10479a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = fwl->dd_handle;
10489a993845SKory Maincent (Dent Project) 	const struct i2c_client *client = priv->client;
10499a993845SKory Maincent (Dent Project) 	struct pd692x0_msg_ver ver;
10509a993845SKory Maincent (Dent Project) 	int ret;
10519a993845SKory Maincent (Dent Project) 
10529a993845SKory Maincent (Dent Project) 	priv->fw_state = PD692X0_FW_COMPLETE;
10539a993845SKory Maincent (Dent Project) 
10549a993845SKory Maincent (Dent Project) 	ret = pd692x0_fw_reset(client);
10559a993845SKory Maincent (Dent Project) 	if (ret)
10569a993845SKory Maincent (Dent Project) 		return ret;
10579a993845SKory Maincent (Dent Project) 
10589a993845SKory Maincent (Dent Project) 	ver = pd692x0_get_sw_version(priv);
10599a993845SKory Maincent (Dent Project) 	if (ver.maj_sw_ver < PD692X0_FW_MAJ_VER) {
10609a993845SKory Maincent (Dent Project) 		dev_err(&client->dev,
10619a993845SKory Maincent (Dent Project) 			"Too old firmware version. Please update it\n");
10629a993845SKory Maincent (Dent Project) 		priv->fw_state = PD692X0_FW_NEED_UPDATE;
10639a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_FW_INVALID;
10649a993845SKory Maincent (Dent Project) 	}
10659a993845SKory Maincent (Dent Project) 
10669a993845SKory Maincent (Dent Project) 	ret = pd692x0_setup_pi_matrix(&priv->pcdev);
10679a993845SKory Maincent (Dent Project) 	if (ret < 0) {
10689a993845SKory Maincent (Dent Project) 		dev_err(&client->dev, "Error configuring ports matrix (%pe)\n",
10699a993845SKory Maincent (Dent Project) 			ERR_PTR(ret));
10709a993845SKory Maincent (Dent Project) 		priv->fw_state = PD692X0_FW_NEED_UPDATE;
10719a993845SKory Maincent (Dent Project) 		return FW_UPLOAD_ERR_HW_ERROR;
10729a993845SKory Maincent (Dent Project) 	}
10739a993845SKory Maincent (Dent Project) 
10749a993845SKory Maincent (Dent Project) 	priv->fw_state = PD692X0_FW_OK;
10759a993845SKory Maincent (Dent Project) 	return FW_UPLOAD_ERR_NONE;
10769a993845SKory Maincent (Dent Project) }
10779a993845SKory Maincent (Dent Project) 
10789a993845SKory Maincent (Dent Project) static void pd692x0_fw_cancel(struct fw_upload *fwl)
10799a993845SKory Maincent (Dent Project) {
10809a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = fwl->dd_handle;
10819a993845SKory Maincent (Dent Project) 
10829a993845SKory Maincent (Dent Project) 	priv->cancel_request = true;
10839a993845SKory Maincent (Dent Project) }
10849a993845SKory Maincent (Dent Project) 
10859a993845SKory Maincent (Dent Project) static void pd692x0_fw_cleanup(struct fw_upload *fwl)
10869a993845SKory Maincent (Dent Project) {
10879a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = fwl->dd_handle;
10889a993845SKory Maincent (Dent Project) 
10899a993845SKory Maincent (Dent Project) 	switch (priv->fw_state) {
10909a993845SKory Maincent (Dent Project) 	case PD692X0_FW_WRITE:
10919a993845SKory Maincent (Dent Project) 		pd692x0_fw_reset(priv->client);
10929a993845SKory Maincent (Dent Project) 		fallthrough;
10939a993845SKory Maincent (Dent Project) 	case PD692X0_FW_COMPLETE:
10949a993845SKory Maincent (Dent Project) 		priv->fw_state = PD692X0_FW_BROKEN;
10959a993845SKory Maincent (Dent Project) 		break;
10969a993845SKory Maincent (Dent Project) 	default:
10979a993845SKory Maincent (Dent Project) 		break;
10989a993845SKory Maincent (Dent Project) 	}
10999a993845SKory Maincent (Dent Project) }
11009a993845SKory Maincent (Dent Project) 
11019a993845SKory Maincent (Dent Project) static const struct fw_upload_ops pd692x0_fw_ops = {
11029a993845SKory Maincent (Dent Project) 	.prepare = pd692x0_fw_prepare,
11039a993845SKory Maincent (Dent Project) 	.write = pd692x0_fw_write,
11049a993845SKory Maincent (Dent Project) 	.poll_complete = pd692x0_fw_poll_complete,
11059a993845SKory Maincent (Dent Project) 	.cancel = pd692x0_fw_cancel,
11069a993845SKory Maincent (Dent Project) 	.cleanup = pd692x0_fw_cleanup,
11079a993845SKory Maincent (Dent Project) };
11089a993845SKory Maincent (Dent Project) 
11099a993845SKory Maincent (Dent Project) static int pd692x0_i2c_probe(struct i2c_client *client)
11109a993845SKory Maincent (Dent Project) {
11119a993845SKory Maincent (Dent Project) 	struct pd692x0_msg msg, buf = {0}, zero = {0};
11129a993845SKory Maincent (Dent Project) 	struct device *dev = &client->dev;
11139a993845SKory Maincent (Dent Project) 	struct pd692x0_msg_ver ver;
11149a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv;
11159a993845SKory Maincent (Dent Project) 	struct fw_upload *fwl;
11169a993845SKory Maincent (Dent Project) 	int ret;
11179a993845SKory Maincent (Dent Project) 
11189a993845SKory Maincent (Dent Project) 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
11199a993845SKory Maincent (Dent Project) 		dev_err(dev, "i2c check functionality failed\n");
11209a993845SKory Maincent (Dent Project) 		return -ENXIO;
11219a993845SKory Maincent (Dent Project) 	}
11229a993845SKory Maincent (Dent Project) 
11239a993845SKory Maincent (Dent Project) 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
11249a993845SKory Maincent (Dent Project) 	if (!priv)
11259a993845SKory Maincent (Dent Project) 		return -ENOMEM;
11269a993845SKory Maincent (Dent Project) 
11279a993845SKory Maincent (Dent Project) 	priv->client = client;
11289a993845SKory Maincent (Dent Project) 	i2c_set_clientdata(client, priv);
11299a993845SKory Maincent (Dent Project) 
11309a993845SKory Maincent (Dent Project) 	ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
11319a993845SKory Maincent (Dent Project) 	if (ret != sizeof(buf)) {
11329a993845SKory Maincent (Dent Project) 		dev_err(dev, "Failed to get device status\n");
11339a993845SKory Maincent (Dent Project) 		return -EIO;
11349a993845SKory Maincent (Dent Project) 	}
11359a993845SKory Maincent (Dent Project) 
11369a993845SKory Maincent (Dent Project) 	/* Probe has been already run and the status dumped */
11379a993845SKory Maincent (Dent Project) 	if (!memcmp(&buf, &zero, sizeof(buf))) {
11389a993845SKory Maincent (Dent Project) 		/* Ask again the controller status */
11399a993845SKory Maincent (Dent Project) 		msg = pd692x0_msg_template_list[PD692X0_MSG_GET_SYS_STATUS];
11409a993845SKory Maincent (Dent Project) 		ret = pd692x0_sendrecv_msg(priv, &msg, &buf);
11419a993845SKory Maincent (Dent Project) 		if (ret < 0) {
11429a993845SKory Maincent (Dent Project) 			dev_err(dev, "Failed to get device status\n");
11439a993845SKory Maincent (Dent Project) 			return ret;
11449a993845SKory Maincent (Dent Project) 		}
11459a993845SKory Maincent (Dent Project) 	}
11469a993845SKory Maincent (Dent Project) 
11479a993845SKory Maincent (Dent Project) 	if (buf.key != 0x03 || buf.sub[0] & 0x01) {
11489a993845SKory Maincent (Dent Project) 		dev_err(dev, "PSE controller error\n");
11499a993845SKory Maincent (Dent Project) 		return -EIO;
11509a993845SKory Maincent (Dent Project) 	}
11519a993845SKory Maincent (Dent Project) 	if (buf.sub[0] & 0x02) {
11529a993845SKory Maincent (Dent Project) 		dev_err(dev, "PSE firmware error. Please update it.\n");
11539a993845SKory Maincent (Dent Project) 		priv->fw_state = PD692X0_FW_BROKEN;
11549a993845SKory Maincent (Dent Project) 	} else {
11559a993845SKory Maincent (Dent Project) 		ver = pd692x0_get_sw_version(priv);
11569a993845SKory Maincent (Dent Project) 		dev_info(&client->dev, "Software version %d.%02d.%d.%d\n",
11579a993845SKory Maincent (Dent Project) 			 ver.prod, ver.maj_sw_ver, ver.min_sw_ver,
11589a993845SKory Maincent (Dent Project) 			 ver.pa_sw_ver);
11599a993845SKory Maincent (Dent Project) 
11609a993845SKory Maincent (Dent Project) 		if (ver.maj_sw_ver < PD692X0_FW_MAJ_VER) {
11619a993845SKory Maincent (Dent Project) 			dev_err(dev, "Too old firmware version. Please update it\n");
11629a993845SKory Maincent (Dent Project) 			priv->fw_state = PD692X0_FW_NEED_UPDATE;
11639a993845SKory Maincent (Dent Project) 		} else {
11649a993845SKory Maincent (Dent Project) 			priv->fw_state = PD692X0_FW_OK;
11659a993845SKory Maincent (Dent Project) 		}
11669a993845SKory Maincent (Dent Project) 	}
11679a993845SKory Maincent (Dent Project) 
11689a993845SKory Maincent (Dent Project) 	priv->np = dev->of_node;
11699a993845SKory Maincent (Dent Project) 	priv->pcdev.nr_lines = PD692X0_MAX_PIS;
11709a993845SKory Maincent (Dent Project) 	priv->pcdev.owner = THIS_MODULE;
11719a993845SKory Maincent (Dent Project) 	priv->pcdev.ops = &pd692x0_ops;
11729a993845SKory Maincent (Dent Project) 	priv->pcdev.dev = dev;
11739a993845SKory Maincent (Dent Project) 	priv->pcdev.types = ETHTOOL_PSE_C33;
11749a993845SKory Maincent (Dent Project) 	ret = devm_pse_controller_register(dev, &priv->pcdev);
11759a993845SKory Maincent (Dent Project) 	if (ret)
11769a993845SKory Maincent (Dent Project) 		return dev_err_probe(dev, ret,
11779a993845SKory Maincent (Dent Project) 				     "failed to register PSE controller\n");
11789a993845SKory Maincent (Dent Project) 
11799a993845SKory Maincent (Dent Project) 	fwl = firmware_upload_register(THIS_MODULE, dev, dev_name(dev),
11809a993845SKory Maincent (Dent Project) 				       &pd692x0_fw_ops, priv);
11819a993845SKory Maincent (Dent Project) 	if (IS_ERR(fwl))
11829a993845SKory Maincent (Dent Project) 		return dev_err_probe(dev, PTR_ERR(fwl),
11839a993845SKory Maincent (Dent Project) 				     "failed to register to the Firmware Upload API\n");
11849a993845SKory Maincent (Dent Project) 	priv->fwl = fwl;
11859a993845SKory Maincent (Dent Project) 
11869a993845SKory Maincent (Dent Project) 	return 0;
11879a993845SKory Maincent (Dent Project) }
11889a993845SKory Maincent (Dent Project) 
11899a993845SKory Maincent (Dent Project) static void pd692x0_i2c_remove(struct i2c_client *client)
11909a993845SKory Maincent (Dent Project) {
11919a993845SKory Maincent (Dent Project) 	struct pd692x0_priv *priv = i2c_get_clientdata(client);
11929a993845SKory Maincent (Dent Project) 
11939a993845SKory Maincent (Dent Project) 	firmware_upload_unregister(priv->fwl);
11949a993845SKory Maincent (Dent Project) }
11959a993845SKory Maincent (Dent Project) 
11969a993845SKory Maincent (Dent Project) static const struct i2c_device_id pd692x0_id[] = {
1197*a6a6a980SUwe Kleine-König 	{ PD692X0_PSE_NAME },
1198*a6a6a980SUwe Kleine-König 	{ }
11999a993845SKory Maincent (Dent Project) };
12009a993845SKory Maincent (Dent Project) MODULE_DEVICE_TABLE(i2c, pd692x0_id);
12019a993845SKory Maincent (Dent Project) 
12029a993845SKory Maincent (Dent Project) static const struct of_device_id pd692x0_of_match[] = {
12039a993845SKory Maincent (Dent Project) 	{ .compatible = "microchip,pd69200", },
12049a993845SKory Maincent (Dent Project) 	{ .compatible = "microchip,pd69210", },
12059a993845SKory Maincent (Dent Project) 	{ .compatible = "microchip,pd69220", },
12069a993845SKory Maincent (Dent Project) 	{ },
12079a993845SKory Maincent (Dent Project) };
12089a993845SKory Maincent (Dent Project) MODULE_DEVICE_TABLE(of, pd692x0_of_match);
12099a993845SKory Maincent (Dent Project) 
12109a993845SKory Maincent (Dent Project) static struct i2c_driver pd692x0_driver = {
12119a993845SKory Maincent (Dent Project) 	.probe		= pd692x0_i2c_probe,
12129a993845SKory Maincent (Dent Project) 	.remove		= pd692x0_i2c_remove,
12139a993845SKory Maincent (Dent Project) 	.id_table	= pd692x0_id,
12149a993845SKory Maincent (Dent Project) 	.driver		= {
12159a993845SKory Maincent (Dent Project) 		.name		= PD692X0_PSE_NAME,
12169a993845SKory Maincent (Dent Project) 		.of_match_table = pd692x0_of_match,
12179a993845SKory Maincent (Dent Project) 	},
12189a993845SKory Maincent (Dent Project) };
12199a993845SKory Maincent (Dent Project) module_i2c_driver(pd692x0_driver);
12209a993845SKory Maincent (Dent Project) 
12219a993845SKory Maincent (Dent Project) MODULE_AUTHOR("Kory Maincent <kory.maincent@bootlin.com>");
12229a993845SKory Maincent (Dent Project) MODULE_DESCRIPTION("Microchip PD692x0 PoE PSE Controller driver");
12239a993845SKory Maincent (Dent Project) MODULE_LICENSE("GPL");
1224