2 * Copyright 2017 IBM Corporation
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.
10 #include <linux/clk.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/miscdevice.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>
20 #include <linux/aspeed-lpc-ctrl.h>
22 #define DEVICE_NAME "aspeed-lpc-ctrl"
25 #define HICR5_ENL2H BIT(8)
26 #define HICR5_ENFWH BIT(10)
31 struct aspeed_lpc_ctrl
{
32 struct miscdevice miscdev
;
33 struct regmap
*regmap
;
36 resource_size_t mem_size
;
41 static struct aspeed_lpc_ctrl
*file_aspeed_lpc_ctrl(struct file
*file
)
43 return container_of(file
->private_data
, struct aspeed_lpc_ctrl
,
47 static int aspeed_lpc_ctrl_mmap(struct file
*file
, struct vm_area_struct
*vma
)
49 struct aspeed_lpc_ctrl
*lpc_ctrl
= file_aspeed_lpc_ctrl(file
);
50 unsigned long vsize
= vma
->vm_end
- vma
->vm_start
;
51 pgprot_t prot
= vma
->vm_page_prot
;
53 if (vma
->vm_pgoff
+ vsize
> lpc_ctrl
->mem_base
+ lpc_ctrl
->mem_size
)
56 /* ast2400/2500 AHB accesses are not cache coherent */
57 prot
= pgprot_noncached(prot
);
59 if (remap_pfn_range(vma
, vma
->vm_start
,
60 (lpc_ctrl
->mem_base
>> PAGE_SHIFT
) + vma
->vm_pgoff
,
67 static long aspeed_lpc_ctrl_ioctl(struct file
*file
, unsigned int cmd
,
70 struct aspeed_lpc_ctrl
*lpc_ctrl
= file_aspeed_lpc_ctrl(file
);
71 void __user
*p
= (void __user
*)param
;
72 struct aspeed_lpc_ctrl_mapping map
;
77 if (copy_from_user(&map
, p
, sizeof(map
)))
84 case ASPEED_LPC_CTRL_IOCTL_GET_SIZE
:
85 /* The flash windows don't report their size */
86 if (map
.window_type
!= ASPEED_LPC_CTRL_WINDOW_MEMORY
)
89 /* Support more than one window id in the future */
90 if (map
.window_id
!= 0)
93 map
.size
= lpc_ctrl
->mem_size
;
95 return copy_to_user(p
, &map
, sizeof(map
)) ? -EFAULT
: 0;
96 case ASPEED_LPC_CTRL_IOCTL_MAP
:
99 * The top half of HICR7 is the MSB of the BMC address of the
101 * The bottom half of HICR7 is the MSB of the HOST LPC
102 * firmware space address of the mapping.
104 * The 1 bits in the top of half of HICR8 represent the bits
105 * (in the requested address) that should be ignored and
106 * replaced with those from the top half of HICR7.
107 * The 1 bits in the bottom half of HICR8 represent the bits
108 * (in the requested address) that should be kept and pass
109 * into the BMC address space.
113 * It doesn't make sense to talk about a size or offset with
114 * low 16 bits set. Both HICR7 and HICR8 talk about the top 16
115 * bits of addresses and sizes.
118 if ((map
.size
& 0x0000ffff) || (map
.offset
& 0x0000ffff))
122 * Because of the way the masks work in HICR8 offset has to
123 * be a multiple of size.
125 if (map
.offset
& (map
.size
- 1))
128 if (map
.window_type
== ASPEED_LPC_CTRL_WINDOW_FLASH
) {
129 addr
= lpc_ctrl
->pnor_base
;
130 size
= lpc_ctrl
->pnor_size
;
131 } else if (map
.window_type
== ASPEED_LPC_CTRL_WINDOW_MEMORY
) {
132 addr
= lpc_ctrl
->mem_base
;
133 size
= lpc_ctrl
->mem_size
;
138 /* Check overflow first! */
139 if (map
.offset
+ map
.size
< map
.offset
||
140 map
.offset
+ map
.size
> size
)
143 if (map
.size
== 0 || map
.size
> size
)
149 * addr (host lpc address) is safe regardless of values. This
150 * simply changes the address the host has to request on its
151 * side of the LPC bus. This cannot impact the hosts own
152 * memory space by surprise as LPC specific accessors are
153 * required. The only strange thing that could be done is
154 * setting the lower 16 bits but the shift takes care of that.
157 rc
= regmap_write(lpc_ctrl
->regmap
, HICR7
,
158 (addr
| (map
.addr
>> 16)));
162 rc
= regmap_write(lpc_ctrl
->regmap
, HICR8
,
163 (~(map
.size
- 1)) | ((map
.size
>> 16) - 1));
168 * Enable LPC FHW cycles. This is required for the host to
169 * access the regions specified.
171 return regmap_update_bits(lpc_ctrl
->regmap
, HICR5
,
172 HICR5_ENFWH
| HICR5_ENL2H
,
173 HICR5_ENFWH
| HICR5_ENL2H
);
179 static const struct file_operations aspeed_lpc_ctrl_fops
= {
180 .owner
= THIS_MODULE
,
181 .mmap
= aspeed_lpc_ctrl_mmap
,
182 .unlocked_ioctl
= aspeed_lpc_ctrl_ioctl
,
185 static int aspeed_lpc_ctrl_probe(struct platform_device
*pdev
)
187 struct aspeed_lpc_ctrl
*lpc_ctrl
;
188 struct device_node
*node
;
189 struct resource resm
;
195 lpc_ctrl
= devm_kzalloc(dev
, sizeof(*lpc_ctrl
), GFP_KERNEL
);
199 node
= of_parse_phandle(dev
->of_node
, "flash", 0);
201 dev_err(dev
, "Didn't find host pnor flash node\n");
205 rc
= of_address_to_resource(node
, 1, &resm
);
208 dev_err(dev
, "Couldn't address to resource for flash\n");
212 lpc_ctrl
->pnor_size
= resource_size(&resm
);
213 lpc_ctrl
->pnor_base
= resm
.start
;
215 dev_set_drvdata(&pdev
->dev
, lpc_ctrl
);
217 node
= of_parse_phandle(dev
->of_node
, "memory-region", 0);
219 dev_err(dev
, "Didn't find reserved memory\n");
223 rc
= of_address_to_resource(node
, 0, &resm
);
226 dev_err(dev
, "Couldn't address to resource for reserved memory\n");
230 lpc_ctrl
->mem_size
= resource_size(&resm
);
231 lpc_ctrl
->mem_base
= resm
.start
;
233 lpc_ctrl
->regmap
= syscon_node_to_regmap(
234 pdev
->dev
.parent
->of_node
);
235 if (IS_ERR(lpc_ctrl
->regmap
)) {
236 dev_err(dev
, "Couldn't get regmap\n");
240 lpc_ctrl
->clk
= devm_clk_get(dev
, NULL
);
241 if (IS_ERR(lpc_ctrl
->clk
)) {
242 dev_err(dev
, "couldn't get clock\n");
243 return PTR_ERR(lpc_ctrl
->clk
);
245 rc
= clk_prepare_enable(lpc_ctrl
->clk
);
247 dev_err(dev
, "couldn't enable clock\n");
251 lpc_ctrl
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
252 lpc_ctrl
->miscdev
.name
= DEVICE_NAME
;
253 lpc_ctrl
->miscdev
.fops
= &aspeed_lpc_ctrl_fops
;
254 lpc_ctrl
->miscdev
.parent
= dev
;
255 rc
= misc_register(&lpc_ctrl
->miscdev
);
257 dev_err(dev
, "Unable to register device\n");
261 dev_info(dev
, "Loaded at %pr\n", &resm
);
266 clk_disable_unprepare(lpc_ctrl
->clk
);
270 static int aspeed_lpc_ctrl_remove(struct platform_device
*pdev
)
272 struct aspeed_lpc_ctrl
*lpc_ctrl
= dev_get_drvdata(&pdev
->dev
);
274 misc_deregister(&lpc_ctrl
->miscdev
);
275 clk_disable_unprepare(lpc_ctrl
->clk
);
280 static const struct of_device_id aspeed_lpc_ctrl_match
[] = {
281 { .compatible
= "aspeed,ast2400-lpc-ctrl" },
282 { .compatible
= "aspeed,ast2500-lpc-ctrl" },
286 static struct platform_driver aspeed_lpc_ctrl_driver
= {
289 .of_match_table
= aspeed_lpc_ctrl_match
,
291 .probe
= aspeed_lpc_ctrl_probe
,
292 .remove
= aspeed_lpc_ctrl_remove
,
295 module_platform_driver(aspeed_lpc_ctrl_driver
);
297 MODULE_DEVICE_TABLE(of
, aspeed_lpc_ctrl_match
);
298 MODULE_LICENSE("GPL");
299 MODULE_AUTHOR("Cyril Bur <cyrilbur@gmail.com>");
300 MODULE_DESCRIPTION("Control for aspeed 2400/2500 LPC HOST to BMC mappings");