12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2524feb79SPatrick Venture /*
3524feb79SPatrick Venture  * Copyright 2017 IBM Corporation
4524feb79SPatrick Venture  */
5524feb79SPatrick Venture 
6524feb79SPatrick Venture #include <linux/clk.h>
76bf4ddbeSAndrew Jeffery #include <linux/log2.h>
8524feb79SPatrick Venture #include <linux/mfd/syscon.h>
9524feb79SPatrick Venture #include <linux/miscdevice.h>
10524feb79SPatrick Venture #include <linux/mm.h>
11524feb79SPatrick Venture #include <linux/module.h>
12524feb79SPatrick Venture #include <linux/of_address.h>
13524feb79SPatrick Venture #include <linux/platform_device.h>
14524feb79SPatrick Venture #include <linux/poll.h>
15524feb79SPatrick Venture #include <linux/regmap.h>
16524feb79SPatrick Venture 
17524feb79SPatrick Venture #include <linux/aspeed-lpc-ctrl.h>
18524feb79SPatrick Venture 
19524feb79SPatrick Venture #define DEVICE_NAME	"aspeed-lpc-ctrl"
20524feb79SPatrick Venture 
21524feb79SPatrick Venture #define HICR5 0x0
22524feb79SPatrick Venture #define HICR5_ENL2H	BIT(8)
23524feb79SPatrick Venture #define HICR5_ENFWH	BIT(10)
24524feb79SPatrick Venture 
255042d3f2SJoel Stanley #define HICR6 0x4
265042d3f2SJoel Stanley #define SW_FWH2AHB	BIT(17)
275042d3f2SJoel Stanley 
28524feb79SPatrick Venture #define HICR7 0x8
29524feb79SPatrick Venture #define HICR8 0xc
30524feb79SPatrick Venture 
31524feb79SPatrick Venture struct aspeed_lpc_ctrl {
32524feb79SPatrick Venture 	struct miscdevice	miscdev;
33524feb79SPatrick Venture 	struct regmap		*regmap;
34524feb79SPatrick Venture 	struct clk		*clk;
35524feb79SPatrick Venture 	phys_addr_t		mem_base;
36524feb79SPatrick Venture 	resource_size_t		mem_size;
37524feb79SPatrick Venture 	u32			pnor_size;
38524feb79SPatrick Venture 	u32			pnor_base;
395042d3f2SJoel Stanley 	bool			fwh2ahb;
40524feb79SPatrick Venture };
41524feb79SPatrick Venture 
42524feb79SPatrick Venture static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file)
43524feb79SPatrick Venture {
44524feb79SPatrick Venture 	return container_of(file->private_data, struct aspeed_lpc_ctrl,
45524feb79SPatrick Venture 			miscdev);
46524feb79SPatrick Venture }
47524feb79SPatrick Venture 
48524feb79SPatrick Venture static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma)
49524feb79SPatrick Venture {
50524feb79SPatrick Venture 	struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
51524feb79SPatrick Venture 	unsigned long vsize = vma->vm_end - vma->vm_start;
52524feb79SPatrick Venture 	pgprot_t prot = vma->vm_page_prot;
53524feb79SPatrick Venture 
54524feb79SPatrick Venture 	if (vma->vm_pgoff + vsize > lpc_ctrl->mem_base + lpc_ctrl->mem_size)
55524feb79SPatrick Venture 		return -EINVAL;
56524feb79SPatrick Venture 
57524feb79SPatrick Venture 	/* ast2400/2500 AHB accesses are not cache coherent */
58524feb79SPatrick Venture 	prot = pgprot_noncached(prot);
59524feb79SPatrick Venture 
60524feb79SPatrick Venture 	if (remap_pfn_range(vma, vma->vm_start,
61524feb79SPatrick Venture 		(lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff,
62524feb79SPatrick Venture 		vsize, prot))
63524feb79SPatrick Venture 		return -EAGAIN;
64524feb79SPatrick Venture 
65524feb79SPatrick Venture 	return 0;
66524feb79SPatrick Venture }
67524feb79SPatrick Venture 
68524feb79SPatrick Venture static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd,
69524feb79SPatrick Venture 		unsigned long param)
70524feb79SPatrick Venture {
71524feb79SPatrick Venture 	struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
72e4272af4SVijay Khemka 	struct device *dev = file->private_data;
73524feb79SPatrick Venture 	void __user *p = (void __user *)param;
74524feb79SPatrick Venture 	struct aspeed_lpc_ctrl_mapping map;
75524feb79SPatrick Venture 	u32 addr;
76524feb79SPatrick Venture 	u32 size;
77524feb79SPatrick Venture 	long rc;
78524feb79SPatrick Venture 
79524feb79SPatrick Venture 	if (copy_from_user(&map, p, sizeof(map)))
80524feb79SPatrick Venture 		return -EFAULT;
81524feb79SPatrick Venture 
82524feb79SPatrick Venture 	if (map.flags != 0)
83524feb79SPatrick Venture 		return -EINVAL;
84524feb79SPatrick Venture 
85524feb79SPatrick Venture 	switch (cmd) {
86524feb79SPatrick Venture 	case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
87524feb79SPatrick Venture 		/* The flash windows don't report their size */
88524feb79SPatrick Venture 		if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY)
89524feb79SPatrick Venture 			return -EINVAL;
90524feb79SPatrick Venture 
91524feb79SPatrick Venture 		/* Support more than one window id in the future */
92524feb79SPatrick Venture 		if (map.window_id != 0)
93524feb79SPatrick Venture 			return -EINVAL;
94524feb79SPatrick Venture 
95e4272af4SVijay Khemka 		/* If memory-region is not described in device tree */
96e4272af4SVijay Khemka 		if (!lpc_ctrl->mem_size) {
97e4272af4SVijay Khemka 			dev_dbg(dev, "Didn't find reserved memory\n");
98e4272af4SVijay Khemka 			return -ENXIO;
99e4272af4SVijay Khemka 		}
100e4272af4SVijay Khemka 
101524feb79SPatrick Venture 		map.size = lpc_ctrl->mem_size;
102524feb79SPatrick Venture 
103524feb79SPatrick Venture 		return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0;
104524feb79SPatrick Venture 	case ASPEED_LPC_CTRL_IOCTL_MAP:
105524feb79SPatrick Venture 
106524feb79SPatrick Venture 		/*
107524feb79SPatrick Venture 		 * The top half of HICR7 is the MSB of the BMC address of the
108524feb79SPatrick Venture 		 * mapping.
109524feb79SPatrick Venture 		 * The bottom half of HICR7 is the MSB of the HOST LPC
110524feb79SPatrick Venture 		 * firmware space address of the mapping.
111524feb79SPatrick Venture 		 *
112524feb79SPatrick Venture 		 * The 1 bits in the top of half of HICR8 represent the bits
113524feb79SPatrick Venture 		 * (in the requested address) that should be ignored and
114524feb79SPatrick Venture 		 * replaced with those from the top half of HICR7.
115524feb79SPatrick Venture 		 * The 1 bits in the bottom half of HICR8 represent the bits
116524feb79SPatrick Venture 		 * (in the requested address) that should be kept and pass
117524feb79SPatrick Venture 		 * into the BMC address space.
118524feb79SPatrick Venture 		 */
119524feb79SPatrick Venture 
120524feb79SPatrick Venture 		/*
121524feb79SPatrick Venture 		 * It doesn't make sense to talk about a size or offset with
122524feb79SPatrick Venture 		 * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
123524feb79SPatrick Venture 		 * bits of addresses and sizes.
124524feb79SPatrick Venture 		 */
125524feb79SPatrick Venture 
126524feb79SPatrick Venture 		if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff))
127524feb79SPatrick Venture 			return -EINVAL;
128524feb79SPatrick Venture 
129524feb79SPatrick Venture 		/*
130524feb79SPatrick Venture 		 * Because of the way the masks work in HICR8 offset has to
131524feb79SPatrick Venture 		 * be a multiple of size.
132524feb79SPatrick Venture 		 */
133524feb79SPatrick Venture 		if (map.offset & (map.size - 1))
134524feb79SPatrick Venture 			return -EINVAL;
135524feb79SPatrick Venture 
136524feb79SPatrick Venture 		if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) {
137e4272af4SVijay Khemka 			if (!lpc_ctrl->pnor_size) {
138e4272af4SVijay Khemka 				dev_dbg(dev, "Didn't find host pnor flash\n");
139e4272af4SVijay Khemka 				return -ENXIO;
140e4272af4SVijay Khemka 			}
141524feb79SPatrick Venture 			addr = lpc_ctrl->pnor_base;
142524feb79SPatrick Venture 			size = lpc_ctrl->pnor_size;
143524feb79SPatrick Venture 		} else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) {
144e4272af4SVijay Khemka 			/* If memory-region is not described in device tree */
145e4272af4SVijay Khemka 			if (!lpc_ctrl->mem_size) {
146e4272af4SVijay Khemka 				dev_dbg(dev, "Didn't find reserved memory\n");
147e4272af4SVijay Khemka 				return -ENXIO;
148e4272af4SVijay Khemka 			}
149524feb79SPatrick Venture 			addr = lpc_ctrl->mem_base;
150524feb79SPatrick Venture 			size = lpc_ctrl->mem_size;
151524feb79SPatrick Venture 		} else {
152524feb79SPatrick Venture 			return -EINVAL;
153524feb79SPatrick Venture 		}
154524feb79SPatrick Venture 
155524feb79SPatrick Venture 		/* Check overflow first! */
156524feb79SPatrick Venture 		if (map.offset + map.size < map.offset ||
157524feb79SPatrick Venture 			map.offset + map.size > size)
158524feb79SPatrick Venture 			return -EINVAL;
159524feb79SPatrick Venture 
160524feb79SPatrick Venture 		if (map.size == 0 || map.size > size)
161524feb79SPatrick Venture 			return -EINVAL;
162524feb79SPatrick Venture 
163524feb79SPatrick Venture 		addr += map.offset;
164524feb79SPatrick Venture 
165524feb79SPatrick Venture 		/*
166524feb79SPatrick Venture 		 * addr (host lpc address) is safe regardless of values. This
167524feb79SPatrick Venture 		 * simply changes the address the host has to request on its
168524feb79SPatrick Venture 		 * side of the LPC bus. This cannot impact the hosts own
169524feb79SPatrick Venture 		 * memory space by surprise as LPC specific accessors are
170524feb79SPatrick Venture 		 * required. The only strange thing that could be done is
171524feb79SPatrick Venture 		 * setting the lower 16 bits but the shift takes care of that.
172524feb79SPatrick Venture 		 */
173524feb79SPatrick Venture 
174524feb79SPatrick Venture 		rc = regmap_write(lpc_ctrl->regmap, HICR7,
175524feb79SPatrick Venture 				(addr | (map.addr >> 16)));
176524feb79SPatrick Venture 		if (rc)
177524feb79SPatrick Venture 			return rc;
178524feb79SPatrick Venture 
179524feb79SPatrick Venture 		rc = regmap_write(lpc_ctrl->regmap, HICR8,
180524feb79SPatrick Venture 				(~(map.size - 1)) | ((map.size >> 16) - 1));
181524feb79SPatrick Venture 		if (rc)
182524feb79SPatrick Venture 			return rc;
183524feb79SPatrick Venture 
184524feb79SPatrick Venture 		/*
1855042d3f2SJoel Stanley 		 * Switch to FWH2AHB mode, AST2600 only.
1865042d3f2SJoel Stanley 		 *
1875042d3f2SJoel Stanley 		 * The other bits in this register are interrupt status bits
1885042d3f2SJoel Stanley 		 * that are cleared by writing 1. As we don't want to clear
1895042d3f2SJoel Stanley 		 * them, set only the bit of interest.
1905042d3f2SJoel Stanley 		 */
1915042d3f2SJoel Stanley 		if (lpc_ctrl->fwh2ahb)
1925042d3f2SJoel Stanley 			regmap_write(lpc_ctrl->regmap, HICR6, SW_FWH2AHB);
1935042d3f2SJoel Stanley 
1945042d3f2SJoel Stanley 		/*
195524feb79SPatrick Venture 		 * Enable LPC FHW cycles. This is required for the host to
196524feb79SPatrick Venture 		 * access the regions specified.
197524feb79SPatrick Venture 		 */
198524feb79SPatrick Venture 		return regmap_update_bits(lpc_ctrl->regmap, HICR5,
199524feb79SPatrick Venture 				HICR5_ENFWH | HICR5_ENL2H,
200524feb79SPatrick Venture 				HICR5_ENFWH | HICR5_ENL2H);
201524feb79SPatrick Venture 	}
202524feb79SPatrick Venture 
203524feb79SPatrick Venture 	return -EINVAL;
204524feb79SPatrick Venture }
205524feb79SPatrick Venture 
206524feb79SPatrick Venture static const struct file_operations aspeed_lpc_ctrl_fops = {
207524feb79SPatrick Venture 	.owner		= THIS_MODULE,
208524feb79SPatrick Venture 	.mmap		= aspeed_lpc_ctrl_mmap,
209524feb79SPatrick Venture 	.unlocked_ioctl	= aspeed_lpc_ctrl_ioctl,
210524feb79SPatrick Venture };
211524feb79SPatrick Venture 
212524feb79SPatrick Venture static int aspeed_lpc_ctrl_probe(struct platform_device *pdev)
213524feb79SPatrick Venture {
214524feb79SPatrick Venture 	struct aspeed_lpc_ctrl *lpc_ctrl;
215524feb79SPatrick Venture 	struct device_node *node;
216524feb79SPatrick Venture 	struct resource resm;
217524feb79SPatrick Venture 	struct device *dev;
218524feb79SPatrick Venture 	int rc;
219524feb79SPatrick Venture 
220524feb79SPatrick Venture 	dev = &pdev->dev;
221524feb79SPatrick Venture 
222524feb79SPatrick Venture 	lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL);
223524feb79SPatrick Venture 	if (!lpc_ctrl)
224524feb79SPatrick Venture 		return -ENOMEM;
225524feb79SPatrick Venture 
226e4272af4SVijay Khemka 	/* If flash is described in device tree then store */
227524feb79SPatrick Venture 	node = of_parse_phandle(dev->of_node, "flash", 0);
228524feb79SPatrick Venture 	if (!node) {
229e4272af4SVijay Khemka 		dev_dbg(dev, "Didn't find host pnor flash node\n");
230e4272af4SVijay Khemka 	} else {
231524feb79SPatrick Venture 		rc = of_address_to_resource(node, 1, &resm);
232524feb79SPatrick Venture 		of_node_put(node);
233524feb79SPatrick Venture 		if (rc) {
234524feb79SPatrick Venture 			dev_err(dev, "Couldn't address to resource for flash\n");
235524feb79SPatrick Venture 			return rc;
236524feb79SPatrick Venture 		}
237524feb79SPatrick Venture 
238524feb79SPatrick Venture 		lpc_ctrl->pnor_size = resource_size(&resm);
239524feb79SPatrick Venture 		lpc_ctrl->pnor_base = resm.start;
240c8a3b9b5SJoel Stanley 	}
241c8a3b9b5SJoel Stanley 
242524feb79SPatrick Venture 
243524feb79SPatrick Venture 	dev_set_drvdata(&pdev->dev, lpc_ctrl);
244524feb79SPatrick Venture 
245e4272af4SVijay Khemka 	/* If memory-region is described in device tree then store */
246524feb79SPatrick Venture 	node = of_parse_phandle(dev->of_node, "memory-region", 0);
247524feb79SPatrick Venture 	if (!node) {
248e4272af4SVijay Khemka 		dev_dbg(dev, "Didn't find reserved memory\n");
249e4272af4SVijay Khemka 	} else {
250524feb79SPatrick Venture 		rc = of_address_to_resource(node, 0, &resm);
251524feb79SPatrick Venture 		of_node_put(node);
252524feb79SPatrick Venture 		if (rc) {
253524feb79SPatrick Venture 			dev_err(dev, "Couldn't address to resource for reserved memory\n");
254e4272af4SVijay Khemka 			return -ENXIO;
255524feb79SPatrick Venture 		}
256524feb79SPatrick Venture 
257524feb79SPatrick Venture 		lpc_ctrl->mem_size = resource_size(&resm);
258524feb79SPatrick Venture 		lpc_ctrl->mem_base = resm.start;
2596bf4ddbeSAndrew Jeffery 
2606bf4ddbeSAndrew Jeffery 		if (!is_power_of_2(lpc_ctrl->mem_size)) {
2616bf4ddbeSAndrew Jeffery 			dev_err(dev, "Reserved memory size must be a power of 2, got %u\n",
2626bf4ddbeSAndrew Jeffery 			       (unsigned int)lpc_ctrl->mem_size);
2636bf4ddbeSAndrew Jeffery 			return -EINVAL;
2646bf4ddbeSAndrew Jeffery 		}
2656bf4ddbeSAndrew Jeffery 
2666bf4ddbeSAndrew Jeffery 		if (!IS_ALIGNED(lpc_ctrl->mem_base, lpc_ctrl->mem_size)) {
2676bf4ddbeSAndrew Jeffery 			dev_err(dev, "Reserved memory must be naturally aligned for size %u\n",
2686bf4ddbeSAndrew Jeffery 			       (unsigned int)lpc_ctrl->mem_size);
2696bf4ddbeSAndrew Jeffery 			return -EINVAL;
2706bf4ddbeSAndrew Jeffery 		}
271e4272af4SVijay Khemka 	}
272524feb79SPatrick Venture 
273524feb79SPatrick Venture 	lpc_ctrl->regmap = syscon_node_to_regmap(
274524feb79SPatrick Venture 			pdev->dev.parent->of_node);
275524feb79SPatrick Venture 	if (IS_ERR(lpc_ctrl->regmap)) {
276524feb79SPatrick Venture 		dev_err(dev, "Couldn't get regmap\n");
277524feb79SPatrick Venture 		return -ENODEV;
278524feb79SPatrick Venture 	}
279524feb79SPatrick Venture 
280524feb79SPatrick Venture 	lpc_ctrl->clk = devm_clk_get(dev, NULL);
281524feb79SPatrick Venture 	if (IS_ERR(lpc_ctrl->clk)) {
282524feb79SPatrick Venture 		dev_err(dev, "couldn't get clock\n");
283524feb79SPatrick Venture 		return PTR_ERR(lpc_ctrl->clk);
284524feb79SPatrick Venture 	}
285524feb79SPatrick Venture 	rc = clk_prepare_enable(lpc_ctrl->clk);
286524feb79SPatrick Venture 	if (rc) {
287524feb79SPatrick Venture 		dev_err(dev, "couldn't enable clock\n");
288524feb79SPatrick Venture 		return rc;
289524feb79SPatrick Venture 	}
290524feb79SPatrick Venture 
2915042d3f2SJoel Stanley 	if (of_device_is_compatible(dev->of_node, "aspeed,ast2600-lpc-ctrl"))
2925042d3f2SJoel Stanley 		lpc_ctrl->fwh2ahb = true;
2935042d3f2SJoel Stanley 
294524feb79SPatrick Venture 	lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR;
295524feb79SPatrick Venture 	lpc_ctrl->miscdev.name = DEVICE_NAME;
296524feb79SPatrick Venture 	lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops;
297524feb79SPatrick Venture 	lpc_ctrl->miscdev.parent = dev;
298524feb79SPatrick Venture 	rc = misc_register(&lpc_ctrl->miscdev);
299524feb79SPatrick Venture 	if (rc) {
300524feb79SPatrick Venture 		dev_err(dev, "Unable to register device\n");
301524feb79SPatrick Venture 		goto err;
302524feb79SPatrick Venture 	}
303524feb79SPatrick Venture 
304524feb79SPatrick Venture 	return 0;
305524feb79SPatrick Venture 
306524feb79SPatrick Venture err:
307524feb79SPatrick Venture 	clk_disable_unprepare(lpc_ctrl->clk);
308524feb79SPatrick Venture 	return rc;
309524feb79SPatrick Venture }
310524feb79SPatrick Venture 
311524feb79SPatrick Venture static int aspeed_lpc_ctrl_remove(struct platform_device *pdev)
312524feb79SPatrick Venture {
313524feb79SPatrick Venture 	struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev);
314524feb79SPatrick Venture 
315524feb79SPatrick Venture 	misc_deregister(&lpc_ctrl->miscdev);
316524feb79SPatrick Venture 	clk_disable_unprepare(lpc_ctrl->clk);
317524feb79SPatrick Venture 
318524feb79SPatrick Venture 	return 0;
319524feb79SPatrick Venture }
320524feb79SPatrick Venture 
321524feb79SPatrick Venture static const struct of_device_id aspeed_lpc_ctrl_match[] = {
322524feb79SPatrick Venture 	{ .compatible = "aspeed,ast2400-lpc-ctrl" },
323524feb79SPatrick Venture 	{ .compatible = "aspeed,ast2500-lpc-ctrl" },
32444ddc4deSBrad Bishop 	{ .compatible = "aspeed,ast2600-lpc-ctrl" },
325524feb79SPatrick Venture 	{ },
326524feb79SPatrick Venture };
327524feb79SPatrick Venture 
328524feb79SPatrick Venture static struct platform_driver aspeed_lpc_ctrl_driver = {
329524feb79SPatrick Venture 	.driver = {
330524feb79SPatrick Venture 		.name		= DEVICE_NAME,
331524feb79SPatrick Venture 		.of_match_table = aspeed_lpc_ctrl_match,
332524feb79SPatrick Venture 	},
333524feb79SPatrick Venture 	.probe = aspeed_lpc_ctrl_probe,
334524feb79SPatrick Venture 	.remove = aspeed_lpc_ctrl_remove,
335524feb79SPatrick Venture };
336524feb79SPatrick Venture 
337524feb79SPatrick Venture module_platform_driver(aspeed_lpc_ctrl_driver);
338524feb79SPatrick Venture 
339524feb79SPatrick Venture MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match);
340524feb79SPatrick Venture MODULE_LICENSE("GPL");
341524feb79SPatrick Venture MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
342*cd460be0SJoel Stanley MODULE_DESCRIPTION("Control for ASPEED LPC HOST to BMC mappings");
343