^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * one commend line parameter per device, each in the form:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * phram=<name>,<start>,<len>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * <name> may be up to 63 characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * <start> and <len> can be octal, decimal or hexadecimal. If followed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * gigabytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct phram_mtd_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct mtd_info mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static LIST_HEAD(phram_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u_char *start = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) memset(start + instr->addr, 0xff, instr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) size_t *retlen, void **virt, resource_size_t *phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *virt = mtd->priv + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) size_t *retlen, u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u_char *start = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) memcpy(buf, start + from, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) size_t *retlen, const u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u_char *start = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) memcpy(start + to, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void unregister_devices(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct phram_mtd_list *this, *safe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) list_for_each_entry_safe(this, safe, &phram_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mtd_device_unregister(&this->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) iounmap(this->mtd.priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) kfree(this->mtd.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) kfree(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int register_device(char *name, phys_addr_t start, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct phram_mtd_list *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) new = kzalloc(sizeof(*new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) new->mtd.priv = ioremap(start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!new->mtd.priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pr_err("ioremap failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) new->mtd.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) new->mtd.size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) new->mtd.flags = MTD_CAP_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) new->mtd._erase = phram_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) new->mtd._point = phram_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) new->mtd._unpoint = phram_unpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) new->mtd._read = phram_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) new->mtd._write = phram_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) new->mtd.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) new->mtd.type = MTD_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) new->mtd.erasesize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) new->mtd.writesize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (mtd_device_register(&new->mtd, NULL, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pr_err("Failed to register new device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) list_add_tail(&new->list, &phram_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) iounmap(new->mtd.priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) out0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int parse_num64(uint64_t *num64, char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) len = strlen(token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (len > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (token[len - 1] == 'i') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) switch (token[len - 2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case 'G':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) shift += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) case 'M':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) shift += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) shift += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) token[len - 2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = kstrtou64(token, 0, num64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *num64 <<= shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int parse_name(char **pname, const char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) len = strlen(token) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (len > 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) name = kstrdup(token, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *pname = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static inline void kill_final_newline(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) char *newline = strrchr(str, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (newline && !newline[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *newline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #define parse_err(fmt, args...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pr_err(fmt , ## args); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int phram_init_called;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * This shall contain the module parameter if any. It is of the form:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * - phram=<device>,<address>,<size> for module case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * - phram.phram=<device>,<address>,<size> for built-in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * We leave 64 bytes for the device name, 20 for the address and 20 for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Example: phram.phram=rootfs,0xa0000000,512Mi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static char phram_paramline[64 + 20 + 20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int phram_setup(const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) char buf[64 + 20 + 20], *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) char *token[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) uint64_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) uint64_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (strnlen(val, sizeof(buf)) >= sizeof(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) parse_err("parameter too long\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) strcpy(str, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) kill_final_newline(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for (i = 0; i < 3; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) token[i] = strsep(&str, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) parse_err("too many arguments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!token[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) parse_err("not enough arguments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ret = parse_name(&name, token[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ret = parse_num64(&start, token[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) parse_err("illegal start address\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ret = parse_num64(&len, token[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) parse_err("illegal device length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = register_device(name, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) pr_info("%s device: %#llx at %#llx\n", name, len, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int phram_param_call(const char *val, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #ifdef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return phram_setup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * If more parameters are later passed in via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * /sys/module/phram/parameters/phram
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * and init_phram() has already been called,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * we can parse the argument now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (phram_init_called)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return phram_setup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * During early boot stage, we only save the parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * here. We must parse them later: if the param passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * from kernel boot command line, phram_param_call() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * called so early that it is not possible to resolve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * the device (even kmalloc() fails). Defer that work to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * phram_setup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (strlen(val) >= sizeof(phram_paramline))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) strcpy(phram_paramline, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) module_param_call(phram, phram_param_call, NULL, NULL, 0200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int __init init_phram(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #ifndef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (phram_paramline[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = phram_setup(phram_paramline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) phram_init_called = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static void __exit cleanup_phram(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) unregister_devices();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) module_init(init_phram);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) module_exit(cleanup_phram);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) MODULE_DESCRIPTION("MTD driver for physical RAM");