xref: /linux/drivers/vfio/platform/reset/vfio_platform_bcmflexrm.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1*ab78130eSCai Huoqing // SPDX-License-Identifier: GPL-2.0
2e2588341SAnup Patel /*
3e2588341SAnup Patel  * Copyright (C) 2017 Broadcom
4e2588341SAnup Patel  */
5e2588341SAnup Patel 
6e2588341SAnup Patel /*
7e2588341SAnup Patel  * This driver provides reset support for Broadcom FlexRM ring manager
8e2588341SAnup Patel  * to VFIO platform.
9e2588341SAnup Patel  */
10e2588341SAnup Patel 
11e2588341SAnup Patel #include <linux/delay.h>
12e2588341SAnup Patel #include <linux/device.h>
13e2588341SAnup Patel #include <linux/init.h>
14e2588341SAnup Patel #include <linux/io.h>
15e2588341SAnup Patel #include <linux/kernel.h>
16e2588341SAnup Patel #include <linux/module.h>
17e2588341SAnup Patel 
183c8d7ef8SMasahiro Yamada #include "../vfio_platform_private.h"
19e2588341SAnup Patel 
20e2588341SAnup Patel /* FlexRM configuration */
21e2588341SAnup Patel #define RING_REGS_SIZE					0x10000
22e2588341SAnup Patel #define RING_VER_MAGIC					0x76303031
23e2588341SAnup Patel 
24e2588341SAnup Patel /* Per-Ring register offsets */
25e2588341SAnup Patel #define RING_VER					0x000
26e2588341SAnup Patel #define RING_CONTROL					0x034
27e2588341SAnup Patel #define RING_FLUSH_DONE					0x038
28e2588341SAnup Patel 
29e2588341SAnup Patel /* Register RING_CONTROL fields */
30e2588341SAnup Patel #define CONTROL_FLUSH_SHIFT				5
31e2588341SAnup Patel 
32e2588341SAnup Patel /* Register RING_FLUSH_DONE fields */
33e2588341SAnup Patel #define FLUSH_DONE_MASK					0x1
34e2588341SAnup Patel 
vfio_platform_bcmflexrm_shutdown(void __iomem * ring)35e2588341SAnup Patel static int vfio_platform_bcmflexrm_shutdown(void __iomem *ring)
36e2588341SAnup Patel {
37e2588341SAnup Patel 	unsigned int timeout;
38e2588341SAnup Patel 
39e2588341SAnup Patel 	/* Disable/inactivate ring */
40e2588341SAnup Patel 	writel_relaxed(0x0, ring + RING_CONTROL);
41e2588341SAnup Patel 
42e2588341SAnup Patel 	/* Set ring flush state */
43e2588341SAnup Patel 	timeout = 1000; /* timeout of 1s */
44e2588341SAnup Patel 	writel_relaxed(BIT(CONTROL_FLUSH_SHIFT), ring + RING_CONTROL);
45e2588341SAnup Patel 	do {
46e2588341SAnup Patel 		if (readl_relaxed(ring + RING_FLUSH_DONE) &
47e2588341SAnup Patel 		    FLUSH_DONE_MASK)
48e2588341SAnup Patel 			break;
49e2588341SAnup Patel 		mdelay(1);
50e2588341SAnup Patel 	} while (--timeout);
51e2588341SAnup Patel 	if (!timeout)
52e2588341SAnup Patel 		return -ETIMEDOUT;
53e2588341SAnup Patel 
54e2588341SAnup Patel 	/* Clear ring flush state */
55e2588341SAnup Patel 	timeout = 1000; /* timeout of 1s */
56e2588341SAnup Patel 	writel_relaxed(0x0, ring + RING_CONTROL);
57e2588341SAnup Patel 	do {
58e2588341SAnup Patel 		if (!(readl_relaxed(ring + RING_FLUSH_DONE) &
59e2588341SAnup Patel 		      FLUSH_DONE_MASK))
60e2588341SAnup Patel 			break;
61e2588341SAnup Patel 		mdelay(1);
62e2588341SAnup Patel 	} while (--timeout);
63e2588341SAnup Patel 	if (!timeout)
64e2588341SAnup Patel 		return -ETIMEDOUT;
65e2588341SAnup Patel 
66e2588341SAnup Patel 	return 0;
67e2588341SAnup Patel }
68e2588341SAnup Patel 
vfio_platform_bcmflexrm_reset(struct vfio_platform_device * vdev)69e2588341SAnup Patel static int vfio_platform_bcmflexrm_reset(struct vfio_platform_device *vdev)
70e2588341SAnup Patel {
71e2588341SAnup Patel 	void __iomem *ring;
72e2588341SAnup Patel 	int rc = 0, ret = 0, ring_num = 0;
73e2588341SAnup Patel 	struct vfio_platform_region *reg = &vdev->regions[0];
74e2588341SAnup Patel 
75e2588341SAnup Patel 	/* Map FlexRM ring registers if not mapped */
76e2588341SAnup Patel 	if (!reg->ioaddr) {
774bdc0d67SChristoph Hellwig 		reg->ioaddr = ioremap(reg->addr, reg->size);
78e2588341SAnup Patel 		if (!reg->ioaddr)
79e2588341SAnup Patel 			return -ENOMEM;
80e2588341SAnup Patel 	}
81e2588341SAnup Patel 
82e2588341SAnup Patel 	/* Discover and shutdown each FlexRM ring */
83e2588341SAnup Patel 	for (ring = reg->ioaddr;
84e2588341SAnup Patel 	     ring < (reg->ioaddr + reg->size); ring += RING_REGS_SIZE) {
85e2588341SAnup Patel 		if (readl_relaxed(ring + RING_VER) == RING_VER_MAGIC) {
86e2588341SAnup Patel 			rc = vfio_platform_bcmflexrm_shutdown(ring);
87e2588341SAnup Patel 			if (rc) {
88e2588341SAnup Patel 				dev_warn(vdev->device,
89e2588341SAnup Patel 					 "FlexRM ring%d shutdown error %d\n",
90e2588341SAnup Patel 					 ring_num, rc);
91e2588341SAnup Patel 				ret |= rc;
92e2588341SAnup Patel 			}
93e2588341SAnup Patel 			ring_num++;
94e2588341SAnup Patel 		}
95e2588341SAnup Patel 	}
96e2588341SAnup Patel 
97e2588341SAnup Patel 	return ret;
98e2588341SAnup Patel }
99e2588341SAnup Patel 
100e2588341SAnup Patel module_vfio_reset_handler("brcm,iproc-flexrm-mbox",
101e2588341SAnup Patel 			  vfio_platform_bcmflexrm_reset);
102e2588341SAnup Patel 
103e2588341SAnup Patel MODULE_LICENSE("GPL v2");
104e2588341SAnup Patel MODULE_AUTHOR("Anup Patel <anup.patel@broadcom.com>");
105e2588341SAnup Patel MODULE_DESCRIPTION("Reset support for Broadcom FlexRM VFIO platform device");
106