Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/fsi-sbefifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/fsi-occ.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define OCC_SRAM_BYTES		4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define OCC_CMD_DATA_BYTES	4090
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define OCC_RESP_DATA_BYTES	4089
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define OCC_SRAM_CMD_ADDR	0xFFFBE000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define OCC_SRAM_RSP_ADDR	0xFFFBF000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * Assume we don't have much FFDC, if we do we'll overflow and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * fail the command. This needs to be big enough for simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * commands as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define OCC_SBE_STATUS_WORDS	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define OCC_TIMEOUT_MS		1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define OCC_CMD_IN_PRG_WAIT_MS	50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct occ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct device *sbefifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	char name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct miscdevice mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct mutex occ_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define to_occ(x)	container_of((x), struct occ, mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) struct occ_response {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u8 seq_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u8 cmd_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u8 return_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	__be16 data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u8 data[OCC_RESP_DATA_BYTES + 2];	/* two bytes checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct occ_client {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct occ *occ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	size_t data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	size_t read_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define to_client(x)	container_of((x), struct occ_client, xfr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static DEFINE_IDA(occ_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int occ_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct occ_client *client = kzalloc(sizeof(*client), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct miscdevice *mdev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct occ *occ = to_occ(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	client->buffer = (u8 *)__get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!client->buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		kfree(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	client->occ = occ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mutex_init(&client->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	file->private_data = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* We allocate a 1-page buffer, make sure it all fits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	BUILD_BUG_ON((OCC_CMD_DATA_BYTES + 3) > PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	BUILD_BUG_ON((OCC_RESP_DATA_BYTES + 7) > PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static ssize_t occ_read(struct file *file, char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct occ_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (!client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (len > OCC_SRAM_BYTES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	mutex_lock(&client->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* This should not be possible ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (WARN_ON_ONCE(client->read_offset > client->data_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* Grab how much data we have to read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	rc = min(len, client->data_size - client->read_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (copy_to_user(buf, client->buffer + client->read_offset, rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		client->read_offset += rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	mutex_unlock(&client->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static ssize_t occ_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			 size_t len, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct occ_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	size_t rlen, data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	u16 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ssize_t rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	u8 *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (!client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (len > (OCC_CMD_DATA_BYTES + 3) || len < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	mutex_lock(&client->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	/* Construct the command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	cmd = client->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* Sequence number (we could increment and compare with response) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	cmd[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * Copy the user command (assume user data follows the occ command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * byte 0: command type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * bytes 1-2: data length (msb first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * bytes 3-n: data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (copy_from_user(&cmd[1], buf, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* Extract data length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	data_length = (cmd[2] << 8) + cmd[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (data_length > OCC_CMD_DATA_BYTES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* Calculate checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	for (i = 0; i < data_length + 4; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		checksum += cmd[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	cmd[data_length + 4] = checksum >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	cmd[data_length + 5] = checksum & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/* Submit command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	rlen = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	rc = fsi_occ_submit(client->occ->dev, cmd, data_length + 6, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			    &rlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Set read tracking data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	client->data_size = rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	client->read_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* Done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	rc = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	mutex_unlock(&client->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int occ_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct occ_client *client = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	free_page((unsigned long)client->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	kfree(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static const struct file_operations occ_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	.open = occ_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	.read = occ_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	.write = occ_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	.release = occ_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int occ_verify_checksum(struct occ_response *resp, u16 data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* Fetch the two bytes after the data for the checksum. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	u16 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	u16 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	checksum = resp->seq_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	checksum += resp->cmd_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	checksum += resp->return_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	checksum += (data_length >> 8) + (data_length & 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	for (i = 0; i < data_length; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		checksum += resp->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (checksum != checksum_resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int occ_getsram(struct occ *occ, u32 address, void *data, ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	u32 data_len = ((len + 7) / 8) * 8;	/* must be multiples of 8 B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	size_t resp_len, resp_data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	__be32 *resp, cmd[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 * Magic sequence to do SBE getsram command. SBE will fetch data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	 * specified SRAM address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	cmd[0] = cpu_to_be32(0x5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_OCC_SRAM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	cmd[2] = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	cmd[3] = cpu_to_be32(address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	cmd[4] = cpu_to_be32(data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	resp_len = (data_len >> 2) + OCC_SBE_STATUS_WORDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	resp = kzalloc(resp_len << 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	rc = sbefifo_submit(occ->sbefifo, cmd, 5, resp, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_GET_OCC_SRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				  resp, resp_len, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	resp_data_len = be32_to_cpu(resp[resp_len - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (resp_data_len != data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		dev_err(occ->dev, "SRAM read expected %d bytes got %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			data_len, resp_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		memcpy(data, resp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/* Convert positive SBEI status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		dev_err(occ->dev, "SRAM read returned failure status: %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	kfree(resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int occ_putsram(struct occ *occ, u32 address, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		       ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	size_t cmd_len, buf_len, resp_len, resp_data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	u32 data_len = ((len + 7) / 8) * 8;	/* must be multiples of 8 B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	__be32 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 * We use the same buffer for command and response, make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * sure it's big enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	resp_len = OCC_SBE_STATUS_WORDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	cmd_len = (data_len >> 2) + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	buf_len = max(cmd_len, resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	buf = kzalloc(buf_len << 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * Magic sequence to do SBE putsram command. SBE will transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * data to specified SRAM address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	buf[0] = cpu_to_be32(cmd_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	buf[2] = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	buf[3] = cpu_to_be32(address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	buf[4] = cpu_to_be32(data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	memcpy(&buf[5], data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 				  buf, resp_len, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (resp_len != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		dev_err(occ->dev, "SRAM write response length invalid: %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		resp_data_len = be32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (resp_data_len != data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			dev_err(occ->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				"SRAM write expected %d bytes got %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				data_len, resp_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	/* Convert positive SBEI status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		dev_err(occ->dev, "SRAM write returned failure status: %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int occ_trigger_attn(struct occ *occ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	__be32 buf[OCC_SBE_STATUS_WORDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	size_t resp_len, resp_data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	BUILD_BUG_ON(OCC_SBE_STATUS_WORDS < 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	resp_len = OCC_SBE_STATUS_WORDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	buf[0] = cpu_to_be32(0x5 + 0x2);        /* Chip-op length in words */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	buf[2] = cpu_to_be32(0x3);              /* Mode: Circular */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	buf[3] = cpu_to_be32(0x0);              /* Address: ignore in mode 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	buf[4] = cpu_to_be32(0x8);              /* Data length in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	buf[5] = cpu_to_be32(0x20010000);       /* Trigger OCC attention */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	buf[6] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	rc = sbefifo_submit(occ->sbefifo, buf, 7, buf, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 				  buf, resp_len, &resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (resp_len != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		dev_err(occ->dev, "SRAM attn response length invalid: %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		resp_data_len = be32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		if (resp_data_len != 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			dev_err(occ->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				"SRAM attn expected 8 bytes got %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				resp_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/* Convert positive SBEI status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		dev_err(occ->dev, "SRAM attn returned failure status: %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		rc = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int fsi_occ_submit(struct device *dev, const void *request, size_t req_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		   void *response, size_t *resp_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	const unsigned long timeout = msecs_to_jiffies(OCC_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	const unsigned long wait_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		msecs_to_jiffies(OCC_CMD_IN_PRG_WAIT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct occ *occ = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct occ_response *resp = response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	u8 seq_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	u16 resp_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (!occ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (*resp_len < 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		dev_dbg(dev, "Bad resplen %zd\n", *resp_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	mutex_lock(&occ->occ_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/* Extract the seq_no from the command (first byte) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	seq_no = *(const u8 *)request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	rc = occ_putsram(occ, OCC_SRAM_CMD_ADDR, request, req_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	rc = occ_trigger_attn(occ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	/* Read occ response header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		rc = occ_getsram(occ, OCC_SRAM_RSP_ADDR, resp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (resp->return_status == OCC_RESP_CMD_IN_PRG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		    resp->return_status == OCC_RESP_CRIT_INIT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		    resp->seq_no != seq_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			rc = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			if (time_after(jiffies, start + timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 				dev_err(occ->dev, "resp timeout status=%02x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 					"resp seq_no=%d our seq_no=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 					resp->return_status, resp->seq_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 					seq_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			schedule_timeout(wait_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	} while (rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/* Extract size of response data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	resp_data_length = get_unaligned_be16(&resp->data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	/* Message size is data length + 5 bytes header + 2 bytes checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if ((resp_data_length + 7) > *resp_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		rc = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	dev_dbg(dev, "resp_status=%02x resp_data_len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		resp->return_status, resp_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/* Grab the rest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (resp_data_length > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		/* already got 3 bytes resp, also need 2 bytes checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		rc = occ_getsram(occ, OCC_SRAM_RSP_ADDR + 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 				 &resp->data[3], resp_data_length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	*resp_len = resp_data_length + 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	rc = occ_verify_checksum(resp, resp_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	mutex_unlock(&occ->occ_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) EXPORT_SYMBOL_GPL(fsi_occ_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int occ_unregister_child(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct platform_device *hwmon_dev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	platform_device_unregister(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static int occ_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct occ *occ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	struct platform_device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct platform_device_info hwmon_dev_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		.parent = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		.name = "occ-hwmon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	occ = devm_kzalloc(dev, sizeof(*occ), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (!occ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	occ->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	occ->sbefifo = dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	mutex_init(&occ->occ_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		rc = of_property_read_u32(dev->of_node, "reg", &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			/* make sure we don't have a duplicate from dts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			occ->idx = ida_simple_get(&occ_ida, reg, reg + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 						  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			if (occ->idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 				occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 							  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 						  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	platform_set_drvdata(pdev, occ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	snprintf(occ->name, sizeof(occ->name), "occ%d", occ->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	occ->mdev.fops = &occ_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	occ->mdev.minor = MISC_DYNAMIC_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	occ->mdev.name = occ->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	occ->mdev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	rc = misc_register(&occ->mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		dev_err(dev, "failed to register miscdevice: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		ida_simple_remove(&occ_ida, occ->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	hwmon_dev_info.id = occ->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	hwmon_dev = platform_device_register_full(&hwmon_dev_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (IS_ERR(hwmon_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		dev_warn(dev, "failed to create hwmon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int occ_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	struct occ *occ = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	misc_deregister(&occ->mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	device_for_each_child(&pdev->dev, NULL, occ_unregister_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	ida_simple_remove(&occ_ida, occ->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static const struct of_device_id occ_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	{ .compatible = "ibm,p9-occ" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) MODULE_DEVICE_TABLE(of, occ_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static struct platform_driver occ_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		.name = "occ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		.of_match_table	= occ_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	.probe	= occ_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	.remove = occ_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static int occ_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	return platform_driver_register(&occ_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static void occ_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	platform_driver_unregister(&occ_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	ida_destroy(&occ_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) module_init(occ_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) module_exit(occ_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) MODULE_DESCRIPTION("BMC P9 OCC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) MODULE_LICENSE("GPL");