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