Skip to content

Commit

Permalink
drivers: k230: add security drivers
Browse files Browse the repository at this point in the history
Signed-off-by: 黄子懿 <[email protected]>
  • Loading branch information
MrThanlon committed Oct 15, 2024
1 parent b5c8e48 commit 8918317
Show file tree
Hide file tree
Showing 21 changed files with 3,915 additions and 0 deletions.
47 changes: 47 additions & 0 deletions arch/riscv/boot/dts/canaan/k230.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,53 @@
reg = <0x0 0x80400c00 0x0 0x00000400>;
interrupts = <191 IRQ_TYPE_LEVEL_HIGH>;
};

tsensor: tsensor@91107000 {
status = "okay";
compatible = "canaan,k230-tsensor";
reg = <0x0 0x91107000 0x0 0x800>;

Check failure on line 637 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: line length of 101 exceeds 100 columns
hardlock = <2>;
};

security: security {
status = "okay";

Check failure on line 642 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: please, no space before tabs
compatible = "simple-bus";

Check failure on line 643 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: please, no space before tabs
#address-cells = <2>;

Check failure on line 644 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: please, no space before tabs
#size-cells = <2>;

Check failure on line 645 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: please, no space before tabs
ranges;

aes: aes@91210200 {
status = "okay";
compatible = "canaan,k230-crypto";
reg = <0x0 0x91210200 0x0 0x0100>;

Check failure on line 651 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: line length of 102 exceeds 100 columns
hardlock = <7>;
};

hash: hash@91210800 {
status = "okay";
compatible = "canaan,k230-hash";
reg = <0x0 0x91210800 0x0 0x0100>;

Check failure on line 658 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: line length of 102 exceeds 100 columns
hardlock = <6>;
};

rsa: rsa@91211000 {
status = "okay";
compatible = "canaan,k230-rsa";

Check failure on line 664 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: please, no space before tabs
reg = <0x0 0x91211000 0x0 0x1000>;

Check failure on line 665 in arch/riscv/boot/dts/canaan/k230.dtsi

View workflow job for this annotation

GitHub Actions / checkpatch

WARNING: line length of 102 exceeds 100 columns
};

trng: trng@91213000 {
status = "okay";
compatible = "canaan,k230-rng";
reg = <0x0 0x91213000 0x0 0x2AC>;
};

otp: otp@91213500 {
status = "okay";
compatible = "canaan,k230-otp";
reg = <0x0 0x91213500 0x0 0x300>;
};
};
};
};
#include "k230_clock_provider.dtsi"
1 change: 1 addition & 0 deletions arch/riscv/configs/k230_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ CONFIG_GPIO_K230=y
CONFIG_SENSORS_MR75203=m
CONFIG_SENSORS_PWM_FAN=m
CONFIG_CPU_THERMAL=y
CONFIG_THERMAL=y
CONFIG_THERMAL_EMULATION=y
CONFIG_WATCHDOG=y
CONFIG_SUNXI_WATCHDOG=y
Expand Down
11 changes: 11 additions & 0 deletions drivers/char/hw_random/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,17 @@ config HW_RANDOM_JH7110
To compile this driver as a module, choose M here.
The module will be called jh7110-trng.

config HW_RANDOM_K230
tristate "Canaan K230 True Random Number Generator support"
depends on HW_RANDOM || ARCH_RV64I
default ARCH_CANAAN
help
This driver provides support for the True Random Number
Generator available in K230 SOC.

To compile this driver as a module, choose M here: the module
will be called k230-rng.

endif # HW_RANDOM

config UML_RANDOM
Expand Down
1 change: 1 addition & 0 deletions drivers/char/hw_random/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ obj-$(CONFIG_HW_RANDOM_ARM_SMCCC_TRNG) += arm_smccc_trng.o
obj-$(CONFIG_HW_RANDOM_CN10K) += cn10k-rng.o
obj-$(CONFIG_HW_RANDOM_POLARFIRE_SOC) += mpfs-rng.o
obj-$(CONFIG_HW_RANDOM_JH7110) += jh7110-trng.o
obj-$(CONFIG_HW_RANDOM_K230) += k230-rng.o
95 changes: 95 additions & 0 deletions drivers/char/hw_random/k230-rng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/hw_random.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/random.h>
#include <linux/delay.h>
#include <linux/pm_runtime.h>

#define PUF_REG_TRNG_OFFSET 0x2a0

struct k230_rng {
void __iomem *base;
struct hwrng rng;
struct device *dev;
};

#define to_k230_rng(p) container_of(p, struct k230_rng, rng)


static int k230_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
struct k230_rng *k230rng = to_k230_rng(rng);
int retval = 0;

while (max >= sizeof(u32)) {
*(u32 *)buf = readl(k230rng->base + PUF_REG_TRNG_OFFSET);

retval += sizeof(u32);
buf += sizeof(u32);
max -= sizeof(u32);
}

return retval;
}

static int k230_rng_probe(struct platform_device *pdev)
{
struct k230_rng *rng;
struct resource *res;
int ret;

rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
if (!rng)
return -ENOMEM;

rng->dev = &pdev->dev;
platform_set_drvdata(pdev, rng);

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
rng->base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(rng->base))
return PTR_ERR(rng->base);

rng->rng.name = pdev->name;
rng->rng.read = k230_rng_read;

ret = devm_hwrng_register(&pdev->dev, &rng->rng);
if (ret) {
dev_err(&pdev->dev, "failed to register hwrng\n");
return ret;
}

dev_info(&pdev->dev, "K230 TRNG driver register!\n");

return 0;
}

static const struct of_device_id k230_rng_dt_ids[] = {
{ .compatible = "canaan,k230-rng" },
{ }
};
MODULE_DEVICE_TABLE(of, k230_rng_dt_ids);

static struct platform_driver k230_rng_driver = {
.probe = k230_rng_probe,
.driver = {
.name = "k230-rng",
.of_match_table = of_match_ptr(k230_rng_dt_ids),
},
};

module_platform_driver(k230_rng_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Canaan K230 random number generator driver");
1 change: 1 addition & 0 deletions drivers/crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -796,5 +796,6 @@ config CRYPTO_DEV_SA2UL

source "drivers/crypto/aspeed/Kconfig"
source "drivers/crypto/starfive/Kconfig"
source "drivers/crypto/canaan/Kconfig"

endif # CRYPTO_HW
1 change: 1 addition & 0 deletions drivers/crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ obj-y += hisilicon/
obj-$(CONFIG_CRYPTO_DEV_AMLOGIC_GXL) += amlogic/
obj-y += intel/
obj-y += starfive/
obj-y += canaan/
35 changes: 35 additions & 0 deletions drivers/crypto/canaan/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-License-Identifier: GPL-2.0
#
# k230 AES-GCM configuration
#
config CRYPTO_DEV_KENDRYTE_CRYP
tristate "Support for K230 crypto accelerators"
select CRYPTO_AES
select CRYPTO_BLKCIPHER
default ARCH_CANAAN
help
This driver interfaces with the hardware crypto
accelerator for K230.
Supporting AES-GCM encryption and
decryption.

config CRYPTO_DEV_KENDRYTE_HASH
tristate "Support for K230 sha256 accelerators"
select CRYPTO_HASH
select CRYPTO_SHA256
default ARCH_CANAAN
help
This driver interfaces with the hardware
hash accelerator for K230.
Supporting sha256 hashing
algo.

config CRYPTO_DEV_KENDRYTE_RSA
tristate "Support for k230 rsa-2048 accelerator"
select CRYPTO_SKCIPHER
default ARCH_CANAAN
help
This driver interfaces with the hardware
rsa accelerator for K230.
Supporting rsa-1024, rsa-2048, rsa-3072,
rsa-4096 encryption and decryption algo.
12 changes: 12 additions & 0 deletions drivers/crypto/canaan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-2.0
#
# Canaan AES-GCM Makefile
#
obj-$(CONFIG_CRYPTO_DEV_KENDRYTE_CRYP) += kendryte_crypto.o
kendryte_crypto-objs := kendryte-aes.o

obj-$(CONFIG_CRYPTO_DEV_KENDRYTE_HASH) += kendryte_hash.o
kendryte_hash-objs := kendryte-hash.o

obj-$(CONFIG_CRYPTO_DEV_KENDRYTE_RSA) += kendryte_rsa.o
kendryte_rsa-objs := kendryte-rsa.o
Loading

0 comments on commit 8918317

Please sign in to comment.