blob: d6b533497ce1a5334acd6c647e1d87c15cc579af [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Carlo Caionead855ea2016-08-27 15:43:46 +02002/*
Martin Blumenstingl9593ad32017-10-09 15:26:40 +02003 * Amlogic Meson GX eFuse Driver
Carlo Caionead855ea2016-08-27 15:43:46 +02004 *
5 * Copyright (c) 2016 Endless Computers, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
Carlo Caionead855ea2016-08-27 15:43:46 +02007 */
8
Jerome Brunet611fbca2018-11-30 11:53:23 +00009#include <linux/clk.h>
Carlo Caionead855ea2016-08-27 15:43:46 +020010#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
17static int meson_efuse_read(void *context, unsigned int offset,
18 void *val, size_t bytes)
19{
Carlo Caione8cde3c22019-07-31 09:23:39 +010020 struct meson_sm_firmware *fw = context;
21
22 return meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
Jerome Bruneta29a63b2018-05-11 12:07:00 +010023 bytes, 0, 0, 0);
Carlo Caionead855ea2016-08-27 15:43:46 +020024}
25
Jerome Brunete5507732018-05-11 12:07:01 +010026static int meson_efuse_write(void *context, unsigned int offset,
27 void *val, size_t bytes)
28{
Carlo Caione8cde3c22019-07-31 09:23:39 +010029 struct meson_sm_firmware *fw = context;
30
31 return meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset,
Jerome Brunete5507732018-05-11 12:07:01 +010032 bytes, 0, 0, 0);
33}
34
Carlo Caionead855ea2016-08-27 15:43:46 +020035static const struct of_device_id meson_efuse_match[] = {
36 { .compatible = "amlogic,meson-gxbb-efuse", },
37 { /* sentinel */ },
38};
39MODULE_DEVICE_TABLE(of, meson_efuse_match);
40
41static int meson_efuse_probe(struct platform_device *pdev)
42{
Jerome Brunet401488d2018-05-11 12:06:59 +010043 struct device *dev = &pdev->dev;
Carlo Caione8cde3c22019-07-31 09:23:39 +010044 struct meson_sm_firmware *fw;
45 struct device_node *sm_np;
Carlo Caionead855ea2016-08-27 15:43:46 +020046 struct nvmem_device *nvmem;
Jerome Brunet401488d2018-05-11 12:06:59 +010047 struct nvmem_config *econfig;
Jerome Brunet611fbca2018-11-30 11:53:23 +000048 struct clk *clk;
Carlo Caionead855ea2016-08-27 15:43:46 +020049 unsigned int size;
Jerome Brunet611fbca2018-11-30 11:53:23 +000050 int ret;
51
Carlo Caione8cde3c22019-07-31 09:23:39 +010052 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 Brunet611fbca2018-11-30 11:53:23 +000063 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 Caionead855ea2016-08-27 15:43:46 +020084
Carlo Caione8cde3c22019-07-31 09:23:39 +010085 if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
Jerome Brunet8649dbe2018-11-30 11:53:21 +000086 dev_err(dev, "failed to get max user");
Carlo Caionead855ea2016-08-27 15:43:46 +020087 return -EINVAL;
Jerome Brunet8649dbe2018-11-30 11:53:21 +000088 }
Carlo Caionead855ea2016-08-27 15:43:46 +020089
Jerome Brunet401488d2018-05-11 12:06:59 +010090 econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL);
91 if (!econfig)
92 return -ENOMEM;
Carlo Caionead855ea2016-08-27 15:43:46 +020093
Jerome Brunet401488d2018-05-11 12:06:59 +010094 econfig->dev = dev;
95 econfig->name = dev_name(dev);
96 econfig->stride = 1;
97 econfig->word_size = 1;
Jerome Brunet401488d2018-05-11 12:06:59 +010098 econfig->reg_read = meson_efuse_read;
Jerome Brunete5507732018-05-11 12:07:01 +010099 econfig->reg_write = meson_efuse_write;
Jerome Brunet401488d2018-05-11 12:06:59 +0100100 econfig->size = size;
Carlo Caione8cde3c22019-07-31 09:23:39 +0100101 econfig->priv = fw;
Jerome Brunet401488d2018-05-11 12:06:59 +0100102
103 nvmem = devm_nvmem_register(&pdev->dev, econfig);
Carlo Caionead855ea2016-08-27 15:43:46 +0200104
Andrey Smirnov90696a42018-03-09 14:47:05 +0000105 return PTR_ERR_OR_ZERO(nvmem);
Carlo Caionead855ea2016-08-27 15:43:46 +0200106}
107
108static struct platform_driver meson_efuse_driver = {
109 .probe = meson_efuse_probe,
Carlo Caionead855ea2016-08-27 15:43:46 +0200110 .driver = {
111 .name = "meson-efuse",
112 .of_match_table = meson_efuse_match,
113 },
114};
115
116module_platform_driver(meson_efuse_driver);
117
118MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
Martin Blumenstingl9593ad32017-10-09 15:26:40 +0200119MODULE_DESCRIPTION("Amlogic Meson GX NVMEM driver");
Carlo Caionead855ea2016-08-27 15:43:46 +0200120MODULE_LICENSE("GPL v2");