1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Google Inc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Provides a simple driver to control the ASPEED P2A interface which allows
11 * the host to read and write to various regions of the BMC's memory.
16 #include <linux/mfd/syscon.h>
17 #include <linux/miscdevice.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/of_address.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
28 #include <linux/aspeed-p2a-ctrl.h>
30 #define DEVICE_NAME "aspeed-p2a-ctrl"
32 /* SCU2C is a Misc. Control Register. */
34 /* SCU180 is the PCIe Configuration Setting Control Register. */
36 /* Bit 1 controls the P2A bridge, while bit 0 controls the entire VGA device
39 #define SCU180_ENP2A BIT(1)
41 /* The ast2400/2500 both have six ranges. */
42 #define P2A_REGION_COUNT 6
50 struct aspeed_p2a_model_data
{
52 struct region regions
[P2A_REGION_COUNT
];
55 struct aspeed_p2a_ctrl
{
56 struct miscdevice miscdev
;
57 struct regmap
*regmap
;
59 const struct aspeed_p2a_model_data
*config
;
61 /* Access to these needs to be locked, held via probe, mapping ioctl,
62 * and release, remove.
64 struct mutex tracking
;
66 u32 readerwriters
[P2A_REGION_COUNT
];
69 resource_size_t mem_size
;
72 struct aspeed_p2a_user
{
74 struct aspeed_p2a_ctrl
*parent
;
76 /* The entire memory space is opened for reading once the bridge is
77 * enabled, therefore this needs only to be tracked once per user.
78 * If any user has it open for read, the bridge must stay enabled.
82 /* Each entry of the array corresponds to a P2A Region. If the user
83 * opens for read or readwrite, the reference goes up here. On
84 * release, this array is walked and references adjusted accordingly.
86 u32 readwrite
[P2A_REGION_COUNT
];
89 static void aspeed_p2a_enable_bridge(struct aspeed_p2a_ctrl
*p2a_ctrl
)
91 regmap_update_bits(p2a_ctrl
->regmap
,
92 SCU180
, SCU180_ENP2A
, SCU180_ENP2A
);
95 static void aspeed_p2a_disable_bridge(struct aspeed_p2a_ctrl
*p2a_ctrl
)
97 regmap_update_bits(p2a_ctrl
->regmap
, SCU180
, SCU180_ENP2A
, 0);
100 static int aspeed_p2a_mmap(struct file
*file
, struct vm_area_struct
*vma
)
104 struct aspeed_p2a_user
*priv
= file
->private_data
;
105 struct aspeed_p2a_ctrl
*ctrl
= priv
->parent
;
107 if (ctrl
->mem_base
== 0 && ctrl
->mem_size
== 0)
110 vsize
= vma
->vm_end
- vma
->vm_start
;
111 prot
= vma
->vm_page_prot
;
113 if (vma
->vm_pgoff
+ vsize
> ctrl
->mem_base
+ ctrl
->mem_size
)
116 /* ast2400/2500 AHB accesses are not cache coherent */
117 prot
= pgprot_noncached(prot
);
119 if (remap_pfn_range(vma
, vma
->vm_start
,
120 (ctrl
->mem_base
>> PAGE_SHIFT
) + vma
->vm_pgoff
,
127 static bool aspeed_p2a_region_acquire(struct aspeed_p2a_user
*priv
,
128 struct aspeed_p2a_ctrl
*ctrl
,
129 struct aspeed_p2a_ctrl_mapping
*map
)
133 bool matched
= false;
136 end
= map
->addr
+ (map
->length
- 1);
138 /* If the value is a legal u32, it will find a match. */
139 for (i
= 0; i
< P2A_REGION_COUNT
; i
++) {
140 const struct region
*curr
= &ctrl
->config
->regions
[i
];
142 /* If the top of this region is lower than your base, skip it.
144 if (curr
->max
< base
)
147 /* If the bottom of this region is higher than your end, bail.
152 /* Lock this and update it, therefore it someone else is
153 * closing their file out, this'll preserve the increment.
155 mutex_lock(&ctrl
->tracking
);
156 ctrl
->readerwriters
[i
] += 1;
157 mutex_unlock(&ctrl
->tracking
);
159 /* Track with the user, so when they close their file, we can
160 * decrement properly.
162 priv
->readwrite
[i
] += 1;
164 /* Enable the region as read-write. */
165 regmap_update_bits(ctrl
->regmap
, SCU2C
, curr
->bit
, 0);
172 static long aspeed_p2a_ioctl(struct file
*file
, unsigned int cmd
,
175 struct aspeed_p2a_user
*priv
= file
->private_data
;
176 struct aspeed_p2a_ctrl
*ctrl
= priv
->parent
;
177 void __user
*arg
= (void __user
*)data
;
178 struct aspeed_p2a_ctrl_mapping map
;
180 if (copy_from_user(&map
, arg
, sizeof(map
)))
184 case ASPEED_P2A_CTRL_IOCTL_SET_WINDOW
:
185 /* If they want a region to be read-only, since the entire
186 * region is read-only once enabled, we just need to track this
187 * user wants to read from the bridge, and if it's not enabled.
190 if (map
.flags
== ASPEED_P2A_CTRL_READ_ONLY
) {
191 mutex_lock(&ctrl
->tracking
);
193 mutex_unlock(&ctrl
->tracking
);
195 /* Track with the user, so when they close their file,
196 * we can decrement properly.
199 } else if (map
.flags
== ASPEED_P2A_CTRL_READWRITE
) {
200 /* If we don't acquire any region return error. */
201 if (!aspeed_p2a_region_acquire(priv
, ctrl
, &map
)) {
205 /* Invalid map flags. */
209 aspeed_p2a_enable_bridge(ctrl
);
211 case ASPEED_P2A_CTRL_IOCTL_GET_MEMORY_CONFIG
:
212 /* This is a request for the memory-region and corresponding
213 * length that is used by the driver for mmap.
217 map
.addr
= ctrl
->mem_base
;
218 map
.length
= ctrl
->mem_size
;
220 return copy_to_user(arg
, &map
, sizeof(map
)) ? -EFAULT
: 0;
228 * When a user opens this file, we create a structure to track their mappings.
230 * A user can map a region as read-only (bridge enabled), or read-write (bit
231 * flipped, and bridge enabled). Either way, this tracking is used, s.t. when
232 * they release the device references are handled.
234 * The bridge is not enabled until a user calls an ioctl to map a region,
235 * simply opening the device does not enable it.
237 static int aspeed_p2a_open(struct inode
*inode
, struct file
*file
)
239 struct aspeed_p2a_user
*priv
;
241 priv
= kmalloc(sizeof(*priv
), GFP_KERNEL
);
247 memset(priv
->readwrite
, 0, sizeof(priv
->readwrite
));
249 /* The file's private_data is initialized to the p2a_ctrl. */
250 priv
->parent
= file
->private_data
;
252 /* Set the file's private_data to the user's data. */
253 file
->private_data
= priv
;
259 * This will close the users mappings. It will go through what they had opened
260 * for readwrite, and decrement those counts. If at the end, this is the last
261 * user, it'll close the bridge.
263 static int aspeed_p2a_release(struct inode
*inode
, struct file
*file
)
267 bool open_regions
= false;
268 struct aspeed_p2a_user
*priv
= file
->private_data
;
270 /* Lock others from changing these values until everything is updated
273 mutex_lock(&priv
->parent
->tracking
);
275 priv
->parent
->readers
-= priv
->read
;
277 for (i
= 0; i
< P2A_REGION_COUNT
; i
++) {
278 priv
->parent
->readerwriters
[i
] -= priv
->readwrite
[i
];
280 if (priv
->parent
->readerwriters
[i
] > 0)
283 bits
|= priv
->parent
->config
->regions
[i
].bit
;
286 /* Setting a bit to 1 disables the region, so let's just OR with the
287 * above to disable any.
290 /* Note, if another user is trying to ioctl, they can't grab tracking,
291 * and therefore can't grab either register mutex.
292 * If another user is trying to close, they can't grab tracking either.
294 regmap_update_bits(priv
->parent
->regmap
, SCU2C
, bits
, bits
);
296 /* If parent->readers is zero and open windows is 0, disable the
299 if (!open_regions
&& priv
->parent
->readers
== 0)
300 aspeed_p2a_disable_bridge(priv
->parent
);
302 mutex_unlock(&priv
->parent
->tracking
);
309 static const struct file_operations aspeed_p2a_ctrl_fops
= {
310 .owner
= THIS_MODULE
,
311 .mmap
= aspeed_p2a_mmap
,
312 .unlocked_ioctl
= aspeed_p2a_ioctl
,
313 .open
= aspeed_p2a_open
,
314 .release
= aspeed_p2a_release
,
317 /* The regions are controlled by SCU2C */
318 static void aspeed_p2a_disable_all(struct aspeed_p2a_ctrl
*p2a_ctrl
)
323 for (i
= 0; i
< P2A_REGION_COUNT
; i
++)
324 value
|= p2a_ctrl
->config
->regions
[i
].bit
;
326 regmap_update_bits(p2a_ctrl
->regmap
, SCU2C
, value
, value
);
328 /* Disable the bridge. */
329 aspeed_p2a_disable_bridge(p2a_ctrl
);
332 static int aspeed_p2a_ctrl_probe(struct platform_device
*pdev
)
334 struct aspeed_p2a_ctrl
*misc_ctrl
;
336 struct resource resm
;
337 struct device_node
*node
;
342 misc_ctrl
= devm_kzalloc(dev
, sizeof(*misc_ctrl
), GFP_KERNEL
);
346 mutex_init(&misc_ctrl
->tracking
);
349 node
= of_parse_phandle(dev
->of_node
, "memory-region", 0);
351 rc
= of_address_to_resource(node
, 0, &resm
);
354 dev_err(dev
, "Couldn't address to resource for reserved memory\n");
358 misc_ctrl
->mem_size
= resource_size(&resm
);
359 misc_ctrl
->mem_base
= resm
.start
;
362 misc_ctrl
->regmap
= syscon_node_to_regmap(pdev
->dev
.parent
->of_node
);
363 if (IS_ERR(misc_ctrl
->regmap
)) {
364 dev_err(dev
, "Couldn't get regmap\n");
368 misc_ctrl
->config
= of_device_get_match_data(dev
);
370 dev_set_drvdata(&pdev
->dev
, misc_ctrl
);
372 aspeed_p2a_disable_all(misc_ctrl
);
374 misc_ctrl
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
375 misc_ctrl
->miscdev
.name
= DEVICE_NAME
;
376 misc_ctrl
->miscdev
.fops
= &aspeed_p2a_ctrl_fops
;
377 misc_ctrl
->miscdev
.parent
= dev
;
379 rc
= misc_register(&misc_ctrl
->miscdev
);
381 dev_err(dev
, "Unable to register device\n");
386 static int aspeed_p2a_ctrl_remove(struct platform_device
*pdev
)
388 struct aspeed_p2a_ctrl
*p2a_ctrl
= dev_get_drvdata(&pdev
->dev
);
390 misc_deregister(&p2a_ctrl
->miscdev
);
395 #define SCU2C_DRAM BIT(25)
396 #define SCU2C_SPI BIT(24)
397 #define SCU2C_SOC BIT(23)
398 #define SCU2C_FLASH BIT(22)
400 static const struct aspeed_p2a_model_data ast2400_model_data
= {
402 {0x00000000, 0x17FFFFFF, SCU2C_FLASH
},
403 {0x18000000, 0x1FFFFFFF, SCU2C_SOC
},
404 {0x20000000, 0x2FFFFFFF, SCU2C_FLASH
},
405 {0x30000000, 0x3FFFFFFF, SCU2C_SPI
},
406 {0x40000000, 0x5FFFFFFF, SCU2C_DRAM
},
407 {0x60000000, 0xFFFFFFFF, SCU2C_SOC
},
411 static const struct aspeed_p2a_model_data ast2500_model_data
= {
413 {0x00000000, 0x0FFFFFFF, SCU2C_FLASH
},
414 {0x10000000, 0x1FFFFFFF, SCU2C_SOC
},
415 {0x20000000, 0x3FFFFFFF, SCU2C_FLASH
},
416 {0x40000000, 0x5FFFFFFF, SCU2C_SOC
},
417 {0x60000000, 0x7FFFFFFF, SCU2C_SPI
},
418 {0x80000000, 0xFFFFFFFF, SCU2C_DRAM
},
422 static const struct of_device_id aspeed_p2a_ctrl_match
[] = {
423 { .compatible
= "aspeed,ast2400-p2a-ctrl",
424 .data
= &ast2400_model_data
},
425 { .compatible
= "aspeed,ast2500-p2a-ctrl",
426 .data
= &ast2500_model_data
},
430 static struct platform_driver aspeed_p2a_ctrl_driver
= {
433 .of_match_table
= aspeed_p2a_ctrl_match
,
435 .probe
= aspeed_p2a_ctrl_probe
,
436 .remove
= aspeed_p2a_ctrl_remove
,
439 module_platform_driver(aspeed_p2a_ctrl_driver
);
441 MODULE_DEVICE_TABLE(of
, aspeed_p2a_ctrl_match
);
442 MODULE_LICENSE("GPL");
443 MODULE_AUTHOR("Patrick Venture <venture@google.com>");
444 MODULE_DESCRIPTION("Control for aspeed 2400/2500 P2A VGA HOST to BMC mappings");