1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2017 IBM Corporation
7 #include <linux/log2.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/poll.h>
15 #include <linux/regmap.h>
17 #include <linux/aspeed-lpc-ctrl.h>
19 #define DEVICE_NAME "aspeed-lpc-ctrl"
22 #define HICR5_ENL2H BIT(8)
23 #define HICR5_ENFWH BIT(10)
26 #define SW_FWH2AHB BIT(17)
31 struct aspeed_lpc_ctrl {
32 struct miscdevice miscdev;
33 struct regmap *regmap;
36 resource_size_t mem_size;
42 static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file)
44 return container_of(file->private_data, struct aspeed_lpc_ctrl,
48 static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma)
50 struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
51 unsigned long vsize = vma->vm_end - vma->vm_start;
52 pgprot_t prot = vma->vm_page_prot;
54 if (vma->vm_pgoff + vsize > lpc_ctrl->mem_base + lpc_ctrl->mem_size)
57 /* ast2400/2500 AHB accesses are not cache coherent */
58 prot = pgprot_noncached(prot);
60 if (remap_pfn_range(vma, vma->vm_start,
61 (lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff,
68 static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd,
71 struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
72 struct device *dev = file->private_data;
73 void __user *p = (void __user *)param;
74 struct aspeed_lpc_ctrl_mapping map;
79 if (copy_from_user(&map, p, sizeof(map)))
86 case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
87 /* The flash windows don't report their size */
88 if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY)
91 /* Support more than one window id in the future */
92 if (map.window_id != 0)
95 /* If memory-region is not described in device tree */
96 if (!lpc_ctrl->mem_size) {
97 dev_dbg(dev, "Didn't find reserved memory\n");
101 map.size = lpc_ctrl->mem_size;
103 return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0;
104 case ASPEED_LPC_CTRL_IOCTL_MAP:
107 * The top half of HICR7 is the MSB of the BMC address of the
109 * The bottom half of HICR7 is the MSB of the HOST LPC
110 * firmware space address of the mapping.
112 * The 1 bits in the top of half of HICR8 represent the bits
113 * (in the requested address) that should be ignored and
114 * replaced with those from the top half of HICR7.
115 * The 1 bits in the bottom half of HICR8 represent the bits
116 * (in the requested address) that should be kept and pass
117 * into the BMC address space.
121 * It doesn't make sense to talk about a size or offset with
122 * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
123 * bits of addresses and sizes.
126 if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff))
130 * Because of the way the masks work in HICR8 offset has to
131 * be a multiple of size.
133 if (map.offset & (map.size - 1))
136 if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) {
137 if (!lpc_ctrl->pnor_size) {
138 dev_dbg(dev, "Didn't find host pnor flash\n");
141 addr = lpc_ctrl->pnor_base;
142 size = lpc_ctrl->pnor_size;
143 } else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) {
144 /* If memory-region is not described in device tree */
145 if (!lpc_ctrl->mem_size) {
146 dev_dbg(dev, "Didn't find reserved memory\n");
149 addr = lpc_ctrl->mem_base;
150 size = lpc_ctrl->mem_size;
155 /* Check overflow first! */
156 if (map.offset + map.size < map.offset ||
157 map.offset + map.size > size)
160 if (map.size == 0 || map.size > size)
166 * addr (host lpc address) is safe regardless of values. This
167 * simply changes the address the host has to request on its
168 * side of the LPC bus. This cannot impact the hosts own
169 * memory space by surprise as LPC specific accessors are
170 * required. The only strange thing that could be done is
171 * setting the lower 16 bits but the shift takes care of that.
174 rc = regmap_write(lpc_ctrl->regmap, HICR7,
175 (addr | (map.addr >> 16)));
179 rc = regmap_write(lpc_ctrl->regmap, HICR8,
180 (~(map.size - 1)) | ((map.size >> 16) - 1));
185 * Switch to FWH2AHB mode, AST2600 only.
187 * The other bits in this register are interrupt status bits
188 * that are cleared by writing 1. As we don't want to clear
189 * them, set only the bit of interest.
191 if (lpc_ctrl->fwh2ahb)
192 regmap_write(lpc_ctrl->regmap, HICR6, SW_FWH2AHB);
195 * Enable LPC FHW cycles. This is required for the host to
196 * access the regions specified.
198 return regmap_update_bits(lpc_ctrl->regmap, HICR5,
199 HICR5_ENFWH | HICR5_ENL2H,
200 HICR5_ENFWH | HICR5_ENL2H);
206 static const struct file_operations aspeed_lpc_ctrl_fops = {
207 .owner = THIS_MODULE,
208 .mmap = aspeed_lpc_ctrl_mmap,
209 .unlocked_ioctl = aspeed_lpc_ctrl_ioctl,
212 static int aspeed_lpc_ctrl_probe(struct platform_device *pdev)
214 struct aspeed_lpc_ctrl *lpc_ctrl;
215 struct device_node *node;
216 struct resource resm;
218 struct device_node *np;
223 lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL);
227 /* If flash is described in device tree then store */
228 node = of_parse_phandle(dev->of_node, "flash", 0);
230 dev_dbg(dev, "Didn't find host pnor flash node\n");
232 rc = of_address_to_resource(node, 1, &resm);
235 dev_err(dev, "Couldn't address to resource for flash\n");
239 lpc_ctrl->pnor_size = resource_size(&resm);
240 lpc_ctrl->pnor_base = resm.start;
244 dev_set_drvdata(&pdev->dev, lpc_ctrl);
246 /* If memory-region is described in device tree then store */
247 node = of_parse_phandle(dev->of_node, "memory-region", 0);
249 dev_dbg(dev, "Didn't find reserved memory\n");
251 rc = of_address_to_resource(node, 0, &resm);
254 dev_err(dev, "Couldn't address to resource for reserved memory\n");
258 lpc_ctrl->mem_size = resource_size(&resm);
259 lpc_ctrl->mem_base = resm.start;
261 if (!is_power_of_2(lpc_ctrl->mem_size)) {
262 dev_err(dev, "Reserved memory size must be a power of 2, got %u\n",
263 (unsigned int)lpc_ctrl->mem_size);
267 if (!IS_ALIGNED(lpc_ctrl->mem_base, lpc_ctrl->mem_size)) {
268 dev_err(dev, "Reserved memory must be naturally aligned for size %u\n",
269 (unsigned int)lpc_ctrl->mem_size);
274 np = pdev->dev.parent->of_node;
275 if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
276 !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") &&
277 !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) {
278 dev_err(dev, "unsupported LPC device binding\n");
282 lpc_ctrl->regmap = syscon_node_to_regmap(np);
283 if (IS_ERR(lpc_ctrl->regmap)) {
284 dev_err(dev, "Couldn't get regmap\n");
288 lpc_ctrl->clk = devm_clk_get(dev, NULL);
289 if (IS_ERR(lpc_ctrl->clk)) {
290 dev_err(dev, "couldn't get clock\n");
291 return PTR_ERR(lpc_ctrl->clk);
293 rc = clk_prepare_enable(lpc_ctrl->clk);
295 dev_err(dev, "couldn't enable clock\n");
299 if (of_device_is_compatible(dev->of_node, "aspeed,ast2600-lpc-ctrl"))
300 lpc_ctrl->fwh2ahb = true;
302 lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR;
303 lpc_ctrl->miscdev.name = DEVICE_NAME;
304 lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops;
305 lpc_ctrl->miscdev.parent = dev;
306 rc = misc_register(&lpc_ctrl->miscdev);
308 dev_err(dev, "Unable to register device\n");
315 clk_disable_unprepare(lpc_ctrl->clk);
319 static int aspeed_lpc_ctrl_remove(struct platform_device *pdev)
321 struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev);
323 misc_deregister(&lpc_ctrl->miscdev);
324 clk_disable_unprepare(lpc_ctrl->clk);
329 static const struct of_device_id aspeed_lpc_ctrl_match[] = {
330 { .compatible = "aspeed,ast2400-lpc-ctrl" },
331 { .compatible = "aspeed,ast2500-lpc-ctrl" },
332 { .compatible = "aspeed,ast2600-lpc-ctrl" },
336 static struct platform_driver aspeed_lpc_ctrl_driver = {
339 .of_match_table = aspeed_lpc_ctrl_match,
341 .probe = aspeed_lpc_ctrl_probe,
342 .remove = aspeed_lpc_ctrl_remove,
345 module_platform_driver(aspeed_lpc_ctrl_driver);
347 MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match);
348 MODULE_LICENSE("GPL");
349 MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
350 MODULE_DESCRIPTION("Control for ASPEED LPC HOST to BMC mappings");