Repository navigation
nvmem: raspberrypi: Add nvmem driver for reading customer OTP #6014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause | ||
| /** | ||
| * raspberrypi-otp.c | ||
| * | ||
| * nvmem driver using firmware mailbox to access otp | ||
| * | ||
| * Copyright (c) 2024, Raspberry Pi Ltd. | ||
| */ | ||
|
|
||
| #include <linux/nvmem-provider.h> | ||
| #include <soc/bcm2835/raspberrypi-firmware.h> | ||
|
|
||
| struct rpi_otp_priv { | ||
| struct rpi_firmware *fw; | ||
| u32 block; | ||
| }; | ||
|
|
||
| #define MAX_ROWS 192 | ||
|
|
||
| #define RPI_FIRMWARE_GET_USER_OTP 0x00030024 | ||
| #define RPI_FIRMWARE_SET_USER_OTP 0x00038024 | ||
|
|
||
| static int rpi_otp_read(void *context, unsigned int offset, void *val, | ||
| size_t bytes) | ||
| { | ||
| struct rpi_otp_priv *priv = context; | ||
| int words = bytes / sizeof(u32); | ||
| int index = offset / sizeof(u32); | ||
| u32 data[3 + MAX_ROWS] = {priv->block, index, words}; | ||
| int err = 0; | ||
|
|
||
| if (words > MAX_ROWS) | ||
| return -EINVAL; | ||
|
|
||
| err = rpi_firmware_property(priv->fw, RPI_FIRMWARE_GET_USER_OTP, | ||
| &data, sizeof(data)); | ||
| if (err == 0) | ||
| memcpy(val, data + 3, bytes); | ||
| else | ||
| memset(val, 0xee, bytes); | ||
| return err; | ||
| } | ||
|
|
||
| static int rpi_otp_write(void *context, unsigned int offset, void *val, | ||
| size_t bytes) | ||
| { | ||
| struct rpi_otp_priv *priv = context; | ||
| int words = bytes / sizeof(u32); | ||
| int index = offset / sizeof(u32); | ||
| u32 data[3 + MAX_ROWS] = {priv->block, index, words}; | ||
|
|
||
| if (bytes > MAX_ROWS * sizeof(u32)) | ||
| return -EINVAL; | ||
|
|
||
| memcpy(data + 3, val, bytes); | ||
| return rpi_firmware_property(priv->fw, RPI_FIRMWARE_SET_USER_OTP, | ||
| &data, sizeof(data)); | ||
| } | ||
|
|
||
| static int rpi_otp_probe(struct platform_device *pdev) | ||
| { | ||
| struct rpi_otp_priv *priv; | ||
| struct nvmem_config config = { | ||
| .dev = &pdev->dev, | ||
| .reg_read = rpi_otp_read, | ||
| .reg_write = rpi_otp_write, | ||
| .stride = sizeof(u32), | ||
| .word_size = sizeof(u32), | ||
| .type = NVMEM_TYPE_OTP, | ||
| .root_only = true, | ||
| }; | ||
| struct device *dev = &pdev->dev; | ||
| struct device_node *np = dev->of_node; | ||
| struct device_node *fw_node; | ||
| struct rpi_firmware *fw; | ||
| u32 reg[2]; | ||
| const char *pname; | ||
|
|
||
| if (of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg))) { | ||
| dev_err(dev, "Failed to parse \"reg\" property\n"); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| pname = of_get_property(np, "name", NULL); | ||
| if (!pname) { | ||
| dev_err(dev, "Failed to parse \"name\" property\n"); | ||
| return -ENOENT; | ||
| } | ||
|
|
||
| config.name = pname; | ||
| config.size = reg[1] * sizeof(u32); | ||
| config.read_only = !of_property_read_bool(np, "rw"); | ||
|
|
||
| fw_node = of_parse_phandle(np, "firmware", 0); | ||
| if (!fw_node) { | ||
| dev_err(dev, "Missing firmware node\n"); | ||
| return -ENOENT; | ||
| } | ||
|
|
||
| fw = rpi_firmware_get(fw_node); | ||
| if (!fw) | ||
| return -EPROBE_DEFER; | ||
|
|
||
| priv = devm_kzalloc(config.dev, sizeof(*priv), GFP_KERNEL); | ||
| if (!priv) | ||
| return -ENOMEM; | ||
|
|
||
| priv->fw = fw; | ||
| priv->block = reg[0]; | ||
| config.priv = priv; | ||
|
|
||
| return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config)); | ||
| } | ||
|
|
||
| static const struct of_device_id rpi_otp_of_match[] = { | ||
| { .compatible = "raspberrypi,rpi-otp", }, | ||
| {} | ||
| }; | ||
|
|
||
| MODULE_DEVICE_TABLE(of, rpi_otp_of_match); | ||
|
|
||
| static struct platform_driver rpi_otp_driver = { | ||
| .driver = { | ||
| .name = "rpi_otp", | ||
| .of_match_table = rpi_otp_of_match, | ||
| }, | ||
| .probe = rpi_otp_probe, | ||
| }; | ||
|
|
||
| module_platform_driver(rpi_otp_driver); | ||
|
|
||
| MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>"); | ||
| MODULE_LICENSE("GPL"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for a user to make the MAC addresses visible but read-only via device-tree or module parameters?