]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/misc/aspeed-lpc-ctrl.c
Merge branch 'core-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / misc / aspeed-lpc-ctrl.c
1 /*
2  * Copyright 2017 IBM Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/mfd/syscon.h>
11 #include <linux/miscdevice.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/poll.h>
17 #include <linux/regmap.h>
18
19 #include <linux/aspeed-lpc-ctrl.h>
20
21 #define DEVICE_NAME     "aspeed-lpc-ctrl"
22
23 #define HICR7 0x8
24 #define HICR8 0xc
25
26 struct aspeed_lpc_ctrl {
27         struct miscdevice       miscdev;
28         struct regmap           *regmap;
29         phys_addr_t             mem_base;
30         resource_size_t         mem_size;
31         u32             pnor_size;
32         u32             pnor_base;
33 };
34
35 static struct aspeed_lpc_ctrl *file_aspeed_lpc_ctrl(struct file *file)
36 {
37         return container_of(file->private_data, struct aspeed_lpc_ctrl,
38                         miscdev);
39 }
40
41 static int aspeed_lpc_ctrl_mmap(struct file *file, struct vm_area_struct *vma)
42 {
43         struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
44         unsigned long vsize = vma->vm_end - vma->vm_start;
45         pgprot_t prot = vma->vm_page_prot;
46
47         if (vma->vm_pgoff + vsize > lpc_ctrl->mem_base + lpc_ctrl->mem_size)
48                 return -EINVAL;
49
50         /* ast2400/2500 AHB accesses are not cache coherent */
51         prot = pgprot_noncached(prot);
52
53         if (remap_pfn_range(vma, vma->vm_start,
54                 (lpc_ctrl->mem_base >> PAGE_SHIFT) + vma->vm_pgoff,
55                 vsize, prot))
56                 return -EAGAIN;
57
58         return 0;
59 }
60
61 static long aspeed_lpc_ctrl_ioctl(struct file *file, unsigned int cmd,
62                 unsigned long param)
63 {
64         struct aspeed_lpc_ctrl *lpc_ctrl = file_aspeed_lpc_ctrl(file);
65         void __user *p = (void __user *)param;
66         struct aspeed_lpc_ctrl_mapping map;
67         u32 addr;
68         u32 size;
69         long rc;
70
71         if (copy_from_user(&map, p, sizeof(map)))
72                 return -EFAULT;
73
74         if (map.flags != 0)
75                 return -EINVAL;
76
77         switch (cmd) {
78         case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
79                 /* The flash windows don't report their size */
80                 if (map.window_type != ASPEED_LPC_CTRL_WINDOW_MEMORY)
81                         return -EINVAL;
82
83                 /* Support more than one window id in the future */
84                 if (map.window_id != 0)
85                         return -EINVAL;
86
87                 map.size = lpc_ctrl->mem_size;
88
89                 return copy_to_user(p, &map, sizeof(map)) ? -EFAULT : 0;
90         case ASPEED_LPC_CTRL_IOCTL_MAP:
91
92                 /*
93                  * The top half of HICR7 is the MSB of the BMC address of the
94                  * mapping.
95                  * The bottom half of HICR7 is the MSB of the HOST LPC
96                  * firmware space address of the mapping.
97                  *
98                  * The 1 bits in the top of half of HICR8 represent the bits
99                  * (in the requested address) that should be ignored and
100                  * replaced with those from the top half of HICR7.
101                  * The 1 bits in the bottom half of HICR8 represent the bits
102                  * (in the requested address) that should be kept and pass
103                  * into the BMC address space.
104                  */
105
106                 /*
107                  * It doesn't make sense to talk about a size or offset with
108                  * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
109                  * bits of addresses and sizes.
110                  */
111
112                 if ((map.size & 0x0000ffff) || (map.offset & 0x0000ffff))
113                         return -EINVAL;
114
115                 /*
116                  * Because of the way the masks work in HICR8 offset has to
117                  * be a multiple of size.
118                  */
119                 if (map.offset & (map.size - 1))
120                         return -EINVAL;
121
122                 if (map.window_type == ASPEED_LPC_CTRL_WINDOW_FLASH) {
123                         addr = lpc_ctrl->pnor_base;
124                         size = lpc_ctrl->pnor_size;
125                 } else if (map.window_type == ASPEED_LPC_CTRL_WINDOW_MEMORY) {
126                         addr = lpc_ctrl->mem_base;
127                         size = lpc_ctrl->mem_size;
128                 } else {
129                         return -EINVAL;
130                 }
131
132                 /* Check overflow first! */
133                 if (map.offset + map.size < map.offset ||
134                         map.offset + map.size > size)
135                         return -EINVAL;
136
137                 if (map.size == 0 || map.size > size)
138                         return -EINVAL;
139
140                 addr += map.offset;
141
142                 /*
143                  * addr (host lpc address) is safe regardless of values. This
144                  * simply changes the address the host has to request on its
145                  * side of the LPC bus. This cannot impact the hosts own
146                  * memory space by surprise as LPC specific accessors are
147                  * required. The only strange thing that could be done is
148                  * setting the lower 16 bits but the shift takes care of that.
149                  */
150
151                 rc = regmap_write(lpc_ctrl->regmap, HICR7,
152                                 (addr | (map.addr >> 16)));
153                 if (rc)
154                         return rc;
155
156                 return regmap_write(lpc_ctrl->regmap, HICR8,
157                         (~(map.size - 1)) | ((map.size >> 16) - 1));
158         }
159
160         return -EINVAL;
161 }
162
163 static const struct file_operations aspeed_lpc_ctrl_fops = {
164         .owner          = THIS_MODULE,
165         .mmap           = aspeed_lpc_ctrl_mmap,
166         .unlocked_ioctl = aspeed_lpc_ctrl_ioctl,
167 };
168
169 static int aspeed_lpc_ctrl_probe(struct platform_device *pdev)
170 {
171         struct aspeed_lpc_ctrl *lpc_ctrl;
172         struct device_node *node;
173         struct resource resm;
174         struct device *dev;
175         int rc;
176
177         dev = &pdev->dev;
178
179         lpc_ctrl = devm_kzalloc(dev, sizeof(*lpc_ctrl), GFP_KERNEL);
180         if (!lpc_ctrl)
181                 return -ENOMEM;
182
183         node = of_parse_phandle(dev->of_node, "flash", 0);
184         if (!node) {
185                 dev_err(dev, "Didn't find host pnor flash node\n");
186                 return -ENODEV;
187         }
188
189         rc = of_address_to_resource(node, 1, &resm);
190         of_node_put(node);
191         if (rc) {
192                 dev_err(dev, "Couldn't address to resource for flash\n");
193                 return rc;
194         }
195
196         lpc_ctrl->pnor_size = resource_size(&resm);
197         lpc_ctrl->pnor_base = resm.start;
198
199         dev_set_drvdata(&pdev->dev, lpc_ctrl);
200
201         node = of_parse_phandle(dev->of_node, "memory-region", 0);
202         if (!node) {
203                 dev_err(dev, "Didn't find reserved memory\n");
204                 return -EINVAL;
205         }
206
207         rc = of_address_to_resource(node, 0, &resm);
208         of_node_put(node);
209         if (rc) {
210                 dev_err(dev, "Couldn't address to resource for reserved memory\n");
211                 return -ENOMEM;
212         }
213
214         lpc_ctrl->mem_size = resource_size(&resm);
215         lpc_ctrl->mem_base = resm.start;
216
217         lpc_ctrl->regmap = syscon_node_to_regmap(
218                         pdev->dev.parent->of_node);
219         if (IS_ERR(lpc_ctrl->regmap)) {
220                 dev_err(dev, "Couldn't get regmap\n");
221                 return -ENODEV;
222         }
223
224         lpc_ctrl->miscdev.minor = MISC_DYNAMIC_MINOR;
225         lpc_ctrl->miscdev.name = DEVICE_NAME;
226         lpc_ctrl->miscdev.fops = &aspeed_lpc_ctrl_fops;
227         lpc_ctrl->miscdev.parent = dev;
228         rc = misc_register(&lpc_ctrl->miscdev);
229         if (rc)
230                 dev_err(dev, "Unable to register device\n");
231         else
232                 dev_info(dev, "Loaded at %pr\n", &resm);
233
234         return rc;
235 }
236
237 static int aspeed_lpc_ctrl_remove(struct platform_device *pdev)
238 {
239         struct aspeed_lpc_ctrl *lpc_ctrl = dev_get_drvdata(&pdev->dev);
240
241         misc_deregister(&lpc_ctrl->miscdev);
242
243         return 0;
244 }
245
246 static const struct of_device_id aspeed_lpc_ctrl_match[] = {
247         { .compatible = "aspeed,ast2400-lpc-ctrl" },
248         { .compatible = "aspeed,ast2500-lpc-ctrl" },
249         { },
250 };
251
252 static struct platform_driver aspeed_lpc_ctrl_driver = {
253         .driver = {
254                 .name           = DEVICE_NAME,
255                 .of_match_table = aspeed_lpc_ctrl_match,
256         },
257         .probe = aspeed_lpc_ctrl_probe,
258         .remove = aspeed_lpc_ctrl_remove,
259 };
260
261 module_platform_driver(aspeed_lpc_ctrl_driver);
262
263 MODULE_DEVICE_TABLE(of, aspeed_lpc_ctrl_match);
264 MODULE_LICENSE("GPL");
265 MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
266 MODULE_DESCRIPTION("Control for aspeed 2400/2500 LPC HOST to BMC mappings");