Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 2 | /* |
Martin Blumenstingl | 9593ad3 | 2017-10-09 15:26:40 +0200 | [diff] [blame] | 3 | * Amlogic Meson GX eFuse Driver |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2016 Endless Computers, Inc. |
| 6 | * Author: Carlo Caione <carlo@endlessm.com> |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Jerome Brunet | 611fbca | 2018-11-30 11:53:23 +0000 | [diff] [blame] | 9 | #include <linux/clk.h> |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/nvmem-provider.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | |
| 15 | #include <linux/firmware/meson/meson_sm.h> |
| 16 | |
| 17 | static int meson_efuse_read(void *context, unsigned int offset, |
| 18 | void *val, size_t bytes) |
| 19 | { |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 20 | struct meson_sm_firmware *fw = context; |
| 21 | |
| 22 | return meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset, |
Jerome Brunet | a29a63b | 2018-05-11 12:07:00 +0100 | [diff] [blame] | 23 | bytes, 0, 0, 0); |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Jerome Brunet | e550773 | 2018-05-11 12:07:01 +0100 | [diff] [blame] | 26 | static int meson_efuse_write(void *context, unsigned int offset, |
| 27 | void *val, size_t bytes) |
| 28 | { |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 29 | struct meson_sm_firmware *fw = context; |
| 30 | |
| 31 | return meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset, |
Jerome Brunet | e550773 | 2018-05-11 12:07:01 +0100 | [diff] [blame] | 32 | bytes, 0, 0, 0); |
| 33 | } |
| 34 | |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 35 | static const struct of_device_id meson_efuse_match[] = { |
| 36 | { .compatible = "amlogic,meson-gxbb-efuse", }, |
| 37 | { /* sentinel */ }, |
| 38 | }; |
| 39 | MODULE_DEVICE_TABLE(of, meson_efuse_match); |
| 40 | |
| 41 | static int meson_efuse_probe(struct platform_device *pdev) |
| 42 | { |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 43 | struct device *dev = &pdev->dev; |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 44 | struct meson_sm_firmware *fw; |
| 45 | struct device_node *sm_np; |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 46 | struct nvmem_device *nvmem; |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 47 | struct nvmem_config *econfig; |
Jerome Brunet | 611fbca | 2018-11-30 11:53:23 +0000 | [diff] [blame] | 48 | struct clk *clk; |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 49 | unsigned int size; |
Jerome Brunet | 611fbca | 2018-11-30 11:53:23 +0000 | [diff] [blame] | 50 | int ret; |
| 51 | |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 52 | sm_np = of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0); |
| 53 | if (!sm_np) { |
| 54 | dev_err(&pdev->dev, "no secure-monitor node\n"); |
| 55 | return -ENODEV; |
| 56 | } |
| 57 | |
| 58 | fw = meson_sm_get(sm_np); |
| 59 | of_node_put(sm_np); |
| 60 | if (!fw) |
| 61 | return -EPROBE_DEFER; |
| 62 | |
Jerome Brunet | 611fbca | 2018-11-30 11:53:23 +0000 | [diff] [blame] | 63 | clk = devm_clk_get(dev, NULL); |
| 64 | if (IS_ERR(clk)) { |
| 65 | ret = PTR_ERR(clk); |
| 66 | if (ret != -EPROBE_DEFER) |
| 67 | dev_err(dev, "failed to get efuse gate"); |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | ret = clk_prepare_enable(clk); |
| 72 | if (ret) { |
| 73 | dev_err(dev, "failed to enable gate"); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | ret = devm_add_action_or_reset(dev, |
| 78 | (void(*)(void *))clk_disable_unprepare, |
| 79 | clk); |
| 80 | if (ret) { |
| 81 | dev_err(dev, "failed to add disable callback"); |
| 82 | return ret; |
| 83 | } |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 84 | |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 85 | if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) { |
Jerome Brunet | 8649dbe | 2018-11-30 11:53:21 +0000 | [diff] [blame] | 86 | dev_err(dev, "failed to get max user"); |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 87 | return -EINVAL; |
Jerome Brunet | 8649dbe | 2018-11-30 11:53:21 +0000 | [diff] [blame] | 88 | } |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 89 | |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 90 | econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL); |
| 91 | if (!econfig) |
| 92 | return -ENOMEM; |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 93 | |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 94 | econfig->dev = dev; |
| 95 | econfig->name = dev_name(dev); |
| 96 | econfig->stride = 1; |
| 97 | econfig->word_size = 1; |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 98 | econfig->reg_read = meson_efuse_read; |
Jerome Brunet | e550773 | 2018-05-11 12:07:01 +0100 | [diff] [blame] | 99 | econfig->reg_write = meson_efuse_write; |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 100 | econfig->size = size; |
Carlo Caione | 8cde3c2 | 2019-07-31 09:23:39 +0100 | [diff] [blame] | 101 | econfig->priv = fw; |
Jerome Brunet | 401488d | 2018-05-11 12:06:59 +0100 | [diff] [blame] | 102 | |
| 103 | nvmem = devm_nvmem_register(&pdev->dev, econfig); |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 104 | |
Andrey Smirnov | 90696a4 | 2018-03-09 14:47:05 +0000 | [diff] [blame] | 105 | return PTR_ERR_OR_ZERO(nvmem); |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static struct platform_driver meson_efuse_driver = { |
| 109 | .probe = meson_efuse_probe, |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 110 | .driver = { |
| 111 | .name = "meson-efuse", |
| 112 | .of_match_table = meson_efuse_match, |
| 113 | }, |
| 114 | }; |
| 115 | |
| 116 | module_platform_driver(meson_efuse_driver); |
| 117 | |
| 118 | MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>"); |
Martin Blumenstingl | 9593ad3 | 2017-10-09 15:26:40 +0200 | [diff] [blame] | 119 | MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver"); |
Carlo Caione | ad855ea | 2016-08-27 15:43:46 +0200 | [diff] [blame] | 120 | MODULE_LICENSE("GPL v2"); |