From: Zev Weiss <zev@bewilderbeest.net>
To: openbmc@lists.ozlabs.org
Cc: "Vignesh Raghavendra" <vigneshr@ti.com>,
"Zev Weiss" <zev@bewilderbeest.net>,
"Tudor Ambarus" <tudor.ambarus@microchip.com>,
"Andrew Jeffery" <andrew@aj.id.au>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org,
"Richard Weinberger" <richard@nod.at>,
"Michael Walle" <michael@walle.cc>,
linux-aspeed@lists.ozlabs.org, "Cédric Le Goater" <clg@kaod.org>,
linux-mtd@lists.infradead.org,
"Miquel Raynal" <miquel.raynal@bootlin.com>,
"Jeremy Kerr" <jk@codeconstruct.com.au>,
"Pratyush Yadav" <p.yadav@ti.com>,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH 4/6] mtd: spi-nor: aspeed: Allow attaching & detaching chips at runtime
Date: Wed, 29 Sep 2021 04:54:06 -0700 [thread overview]
Message-ID: <20210929115409.21254-5-zev@bewilderbeest.net> (raw)
In-Reply-To: <20210929115409.21254-1-zev@bewilderbeest.net>
There are now two new sysfs attributes, attach_chip and detach_chip,
into which a chip select number can be written to attach or detach the
corresponding chip.
Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
---
.../ABI/stable/sysfs-driver-aspeed-smc | 17 +++
drivers/mtd/spi-nor/controllers/aspeed-smc.c | 101 +++++++++++++++++-
2 files changed, 115 insertions(+), 3 deletions(-)
create mode 100644 Documentation/ABI/stable/sysfs-driver-aspeed-smc
diff --git a/Documentation/ABI/stable/sysfs-driver-aspeed-smc b/Documentation/ABI/stable/sysfs-driver-aspeed-smc
new file mode 100644
index 000000000000..871b4d65ccb2
--- /dev/null
+++ b/Documentation/ABI/stable/sysfs-driver-aspeed-smc
@@ -0,0 +1,17 @@
+What: /sys/bus/platform/drivers/aspeed-smc/*/attach_chip
+Date: September 2021
+Contact: Zev Weiss <zev@bewilderbeest.net>
+Description: A chip select number may be written into this file to
+ request that the corresponding chip be attached by the
+ driver.
+Users: OpenBMC. Proposed changes should be mailed to
+ openbmc@lists.ozlabs.org
+
+What: /sys/bus/platform/drivers/aspeed-smc/*/detach_chip
+Date: September 2021
+Contact: Zev Weiss <zev@bewilderbeest.net>
+Description: A chip select number may be written into this file to
+ request that the corresponding chip be detached by the
+ driver.
+Users: OpenBMC. Proposed changes should be mailed to
+ openbmc@lists.ozlabs.org
diff --git a/drivers/mtd/spi-nor/controllers/aspeed-smc.c b/drivers/mtd/spi-nor/controllers/aspeed-smc.c
index 3c153104a905..da49192a8220 100644
--- a/drivers/mtd/spi-nor/controllers/aspeed-smc.c
+++ b/drivers/mtd/spi-nor/controllers/aspeed-smc.c
@@ -91,6 +91,7 @@ struct aspeed_smc_controller;
struct aspeed_smc_chip {
int cs;
+ bool attached;
struct aspeed_smc_controller *controller;
void __iomem *ctl; /* control register */
void __iomem *ahb_base; /* base of chip window */
@@ -402,7 +403,15 @@ static ssize_t aspeed_smc_write_user(struct spi_nor *nor, loff_t to,
static int aspeed_smc_unregister_chip(struct aspeed_smc_chip *chip)
{
- return mtd_device_unregister(&chip->nor.mtd);
+ int ret = -ENOENT;
+ mutex_lock(&chip->controller->mutex);
+ if (chip->attached) {
+ ret = mtd_device_unregister(&chip->nor.mtd);
+ if (!ret)
+ chip->attached = false;
+ }
+ mutex_unlock(&chip->controller->mutex);
+ return ret;
}
static int aspeed_smc_unregister(struct aspeed_smc_controller *controller)
@@ -412,7 +421,7 @@ static int aspeed_smc_unregister(struct aspeed_smc_controller *controller)
for (n = 0; n < controller->info->nce; n++) {
chip = controller->chips[n];
- if (chip) {
+ if (chip && chip->attached) {
ret = aspeed_smc_unregister_chip(chip);
if (ret)
dev_warn(controller->dev, "failed to unregister CS%d: %d\n",
@@ -775,6 +784,13 @@ static int aspeed_smc_register_chip(struct aspeed_smc_chip *chip)
};
int ret;
+ mutex_lock(&chip->controller->mutex);
+
+ if (chip->attached) {
+ ret = -EEXIST;
+ goto out;
+ }
+
ret = aspeed_smc_chip_setup_init(chip, chip->controller->ahb_res);
if (ret)
goto out;
@@ -792,7 +808,12 @@ static int aspeed_smc_register_chip(struct aspeed_smc_chip *chip)
goto out;
ret = mtd_device_register(&chip->nor.mtd, NULL, 0);
+ if (ret)
+ goto out;
+
+ chip->attached = true;
out:
+ mutex_unlock(&chip->controller->mutex);
return ret;
}
@@ -865,6 +886,72 @@ static int aspeed_smc_setup_flash(struct aspeed_smc_controller *controller,
return ret;
}
+static inline struct aspeed_smc_controller *to_aspeed_smc_controller(struct device *dev)
+{
+ struct platform_device *pdev = container_of(dev, struct platform_device, dev);
+ return platform_get_drvdata(pdev);
+}
+
+static ssize_t attach_chip_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long cs;
+ struct aspeed_smc_controller *controller;
+ struct aspeed_smc_chip *chip;
+ ssize_t ret = kstrtoul(buf, 0, &cs);
+ if (ret)
+ return ret;
+
+ controller = to_aspeed_smc_controller(dev);
+ if (cs >= controller->info->nce)
+ return -EINVAL;
+
+ chip = controller->chips[cs];
+
+ if (!chip)
+ return -ENODEV;
+
+ ret = aspeed_smc_register_chip(chip);
+
+ return ret ? ret : count;
+}
+static DEVICE_ATTR_WO(attach_chip);
+
+static ssize_t detach_chip_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long cs;
+ struct aspeed_smc_controller *controller;
+ struct aspeed_smc_chip *chip;
+ ssize_t ret = kstrtoul(buf, 0, &cs);
+ if (ret)
+ return ret;
+
+ controller = to_aspeed_smc_controller(dev);
+ if (cs >= controller->info->nce)
+ return -EINVAL;
+
+ chip = controller->chips[cs];
+
+ if (!chip)
+ return -ENODEV;
+
+ ret = aspeed_smc_unregister_chip(chip);
+
+ return ret ? ret : count;
+}
+static DEVICE_ATTR_WO(detach_chip);
+
+static struct attribute *aspeed_smc_sysfs_attrs[] = {
+ &dev_attr_attach_chip.attr,
+ &dev_attr_detach_chip.attr,
+ NULL,
+};
+
+static const struct attribute_group aspeed_smc_sysfs_attr_group = {
+ .attrs = aspeed_smc_sysfs_attrs,
+};
+
static int aspeed_smc_probe(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
@@ -905,8 +992,16 @@ static int aspeed_smc_probe(struct platform_device *pdev)
controller->ahb_window_size = resource_size(res);
ret = aspeed_smc_setup_flash(controller, np, res);
- if (ret)
+ if (ret) {
dev_err(dev, "Aspeed SMC probe failed %d\n", ret);
+ return ret;
+ }
+
+ ret = devm_device_add_group(dev, &aspeed_smc_sysfs_attr_group);
+ if (ret) {
+ dev_err(dev, "Failed to create sysfs files\n");
+ aspeed_smc_unregister(controller);
+ }
return ret;
}
--
2.33.0
next prev parent reply other threads:[~2021-09-29 11:57 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-29 11:54 [PATCH 0/6] Dynamic aspeed-smc flash chips via "reserved" DT status Zev Weiss
2021-09-29 11:54 ` [PATCH 1/6] of: base: Add function to check for status = "reserved" Zev Weiss
2021-09-29 15:49 ` Rob Herring
2021-09-29 11:54 ` [PATCH 2/6] mtd: core: clear out unregistered devices a bit more Zev Weiss
2021-09-29 11:54 ` [PATCH 3/6] mtd: spi-nor: aspeed: Refactor registration/unregistration Zev Weiss
2021-10-06 0:03 ` Dhananjay Phadke
2021-09-29 11:54 ` Zev Weiss [this message]
2021-09-29 11:54 ` [PATCH 5/6] mtd: spi-nor: aspeed: Don't automatically attach reserved chips Zev Weiss
2021-09-29 11:54 ` [PATCH 6/6] ARM: dts: aspeed: Add e3c246d4i BIOS flash device Zev Weiss
2021-09-29 16:08 ` [PATCH 0/6] Dynamic aspeed-smc flash chips via "reserved" DT status Rob Herring
2021-09-29 22:00 ` Zev Weiss
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210929115409.21254-5-zev@bewilderbeest.net \
--to=zev@bewilderbeest.net \
--cc=andrew@aj.id.au \
--cc=clg@kaod.org \
--cc=gregkh@linuxfoundation.org \
--cc=jk@codeconstruct.com.au \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-aspeed@lists.ozlabs.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=michael@walle.cc \
--cc=miquel.raynal@bootlin.com \
--cc=openbmc@lists.ozlabs.org \
--cc=p.yadav@ti.com \
--cc=richard@nod.at \
--cc=tudor.ambarus@microchip.com \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).