blob: babf11d6b3384a44f5bfb0f9b7c9441073b5cc3a [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery9fc79232017-04-12 14:26:00 +09303
4#include <linux/types.h>
5#include <mtd/mtd-user.h>
6#include <stdarg.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <string.h>
10#include <sys/mman.h>
11
12#include "linux/aspeed-lpc-ctrl.h"
13
Patrick Williams68a24c92023-07-25 12:02:16 -050014static struct aspeed_lpc_ctrl_mapping ctrl = { .size = 0 };
Andrew Jeffery9fc79232017-04-12 14:26:00 +093015
16static struct mtd_info_user mtd = {
17 .type = MTD_NORFLASH,
18 .flags = MTD_WRITEABLE,
19};
20
21void system_set_reserved_size(uint32_t size)
22{
23 ctrl.size = size;
24}
25
26void system_set_mtd_sizes(uint32_t size, uint32_t erasesize)
27{
28 mtd.size = size;
29 mtd.erasesize = erasesize;
30 mtd.writesize = erasesize;
31}
32
33int ioctl(int fd, unsigned long request, ...)
34{
35 int rc = 0;
36 va_list ap;
37
38 switch (request) {
39 case MEMGETINFO:
40 {
41 struct mtd_info_user *info;
42
43 va_start(ap, request);
44 info = va_arg(ap, struct mtd_info_user *);
45 memcpy(info, &mtd, sizeof(mtd));
46 va_end(ap);
47 break;
48 }
49 case MEMERASE:
50 {
51 struct erase_info_user *info;
52 uint8_t *map;
53
54 va_start(ap, request);
55 info = va_arg(ap, struct erase_info_user *);
56
57 if (info->start + info->length > mtd.size)
58 return -1;
59
60 map = mmap(NULL, mtd.size, PROT_WRITE, MAP_SHARED, fd, 0);
61 if (map == MAP_FAILED)
62 return -1;
63
64 memset(&map[info->start], 0xff, info->length);
65 munmap(map, mtd.size);
66
67 va_end(ap);
68 break;
69 }
70 case ASPEED_LPC_CTRL_IOCTL_GET_SIZE:
71 {
72 struct aspeed_lpc_ctrl_mapping *info;
73
74 va_start(ap, request);
75 info = va_arg(ap, struct aspeed_lpc_ctrl_mapping *);
Andrew Jefferyc2c08682018-03-02 10:14:59 +103076 info->size = ctrl.size;
Andrew Jeffery9fc79232017-04-12 14:26:00 +093077 va_end(ap);
78 break;
79 }
80 case ASPEED_LPC_CTRL_IOCTL_MAP:
81 break;
82 default:
83 printf("ioctl() called with unhandled request 0x%08lx\n", request);
84 rc = -1;
85 break;
86 }
87
88 return rc;
89}