Path: blob/master/drivers/crypto/rockchip/rk3288_crypto.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Crypto acceleration support for Rockchip RK32883*4* Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd5*6* Author: Zain Wang <[email protected]>7*8* Some ideas are from marvell-cesa.c and s5p-sss.c driver.9*/1011#include "rk3288_crypto.h"12#include <crypto/engine.h>13#include <crypto/internal/hash.h>14#include <crypto/internal/skcipher.h>15#include <linux/clk.h>16#include <linux/dma-mapping.h>17#include <linux/debugfs.h>18#include <linux/delay.h>19#include <linux/err.h>20#include <linux/kernel.h>21#include <linux/io.h>22#include <linux/module.h>23#include <linux/platform_device.h>24#include <linux/of.h>25#include <linux/reset.h>26#include <linux/spinlock.h>2728static struct rockchip_ip rocklist = {29.dev_list = LIST_HEAD_INIT(rocklist.dev_list),30.lock = __SPIN_LOCK_UNLOCKED(rocklist.lock),31};3233struct rk_crypto_info *get_rk_crypto(void)34{35struct rk_crypto_info *first;3637spin_lock(&rocklist.lock);38first = list_first_entry_or_null(&rocklist.dev_list,39struct rk_crypto_info, list);40list_rotate_left(&rocklist.dev_list);41spin_unlock(&rocklist.lock);42return first;43}4445static const struct rk_variant rk3288_variant = {46.num_clks = 4,47.rkclks = {48{ "sclk", 150000000},49}50};5152static const struct rk_variant rk3328_variant = {53.num_clks = 3,54};5556static const struct rk_variant rk3399_variant = {57.num_clks = 3,58};5960static int rk_crypto_get_clks(struct rk_crypto_info *dev)61{62int i, j, err;63unsigned long cr;6465dev->num_clks = devm_clk_bulk_get_all(dev->dev, &dev->clks);66if (dev->num_clks < dev->variant->num_clks) {67dev_err(dev->dev, "Missing clocks, got %d instead of %d\n",68dev->num_clks, dev->variant->num_clks);69return -EINVAL;70}7172for (i = 0; i < dev->num_clks; i++) {73cr = clk_get_rate(dev->clks[i].clk);74for (j = 0; j < ARRAY_SIZE(dev->variant->rkclks); j++) {75if (dev->variant->rkclks[j].max == 0)76continue;77if (strcmp(dev->variant->rkclks[j].name, dev->clks[i].id))78continue;79if (cr > dev->variant->rkclks[j].max) {80err = clk_set_rate(dev->clks[i].clk,81dev->variant->rkclks[j].max);82if (err)83dev_err(dev->dev, "Fail downclocking %s from %lu to %lu\n",84dev->variant->rkclks[j].name, cr,85dev->variant->rkclks[j].max);86else87dev_info(dev->dev, "Downclocking %s from %lu to %lu\n",88dev->variant->rkclks[j].name, cr,89dev->variant->rkclks[j].max);90}91}92}93return 0;94}9596static int rk_crypto_enable_clk(struct rk_crypto_info *dev)97{98int err;99100err = clk_bulk_prepare_enable(dev->num_clks, dev->clks);101if (err)102dev_err(dev->dev, "Could not enable clock clks\n");103104return err;105}106107static void rk_crypto_disable_clk(struct rk_crypto_info *dev)108{109clk_bulk_disable_unprepare(dev->num_clks, dev->clks);110}111112/*113* Power management strategy: The device is suspended until a request114* is handled. For avoiding suspend/resume yoyo, the autosuspend is set to 2s.115*/116static int rk_crypto_pm_suspend(struct device *dev)117{118struct rk_crypto_info *rkdev = dev_get_drvdata(dev);119120rk_crypto_disable_clk(rkdev);121reset_control_assert(rkdev->rst);122123return 0;124}125126static int rk_crypto_pm_resume(struct device *dev)127{128struct rk_crypto_info *rkdev = dev_get_drvdata(dev);129int ret;130131ret = rk_crypto_enable_clk(rkdev);132if (ret)133return ret;134135reset_control_deassert(rkdev->rst);136return 0;137138}139140static const struct dev_pm_ops rk_crypto_pm_ops = {141SET_RUNTIME_PM_OPS(rk_crypto_pm_suspend, rk_crypto_pm_resume, NULL)142};143144static int rk_crypto_pm_init(struct rk_crypto_info *rkdev)145{146int err;147148pm_runtime_use_autosuspend(rkdev->dev);149pm_runtime_set_autosuspend_delay(rkdev->dev, 2000);150151err = pm_runtime_set_suspended(rkdev->dev);152if (err)153return err;154pm_runtime_enable(rkdev->dev);155return err;156}157158static void rk_crypto_pm_exit(struct rk_crypto_info *rkdev)159{160pm_runtime_disable(rkdev->dev);161}162163static irqreturn_t rk_crypto_irq_handle(int irq, void *dev_id)164{165struct rk_crypto_info *dev = platform_get_drvdata(dev_id);166u32 interrupt_status;167168interrupt_status = CRYPTO_READ(dev, RK_CRYPTO_INTSTS);169CRYPTO_WRITE(dev, RK_CRYPTO_INTSTS, interrupt_status);170171dev->status = 1;172if (interrupt_status & 0x0a) {173dev_warn(dev->dev, "DMA Error\n");174dev->status = 0;175}176complete(&dev->complete);177178return IRQ_HANDLED;179}180181static struct rk_crypto_tmp *rk_cipher_algs[] = {182&rk_ecb_aes_alg,183&rk_cbc_aes_alg,184&rk_ecb_des_alg,185&rk_cbc_des_alg,186&rk_ecb_des3_ede_alg,187&rk_cbc_des3_ede_alg,188&rk_ahash_sha1,189&rk_ahash_sha256,190&rk_ahash_md5,191};192193static int rk_crypto_debugfs_show(struct seq_file *seq, void *v)194{195struct rk_crypto_info *dd;196unsigned int i;197198spin_lock(&rocklist.lock);199list_for_each_entry(dd, &rocklist.dev_list, list) {200seq_printf(seq, "%s %s requests: %lu\n",201dev_driver_string(dd->dev), dev_name(dd->dev),202dd->nreq);203}204spin_unlock(&rocklist.lock);205206for (i = 0; i < ARRAY_SIZE(rk_cipher_algs); i++) {207if (!rk_cipher_algs[i]->dev)208continue;209switch (rk_cipher_algs[i]->type) {210case CRYPTO_ALG_TYPE_SKCIPHER:211seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",212rk_cipher_algs[i]->alg.skcipher.base.base.cra_driver_name,213rk_cipher_algs[i]->alg.skcipher.base.base.cra_name,214rk_cipher_algs[i]->stat_req, rk_cipher_algs[i]->stat_fb);215seq_printf(seq, "\tfallback due to length: %lu\n",216rk_cipher_algs[i]->stat_fb_len);217seq_printf(seq, "\tfallback due to alignment: %lu\n",218rk_cipher_algs[i]->stat_fb_align);219seq_printf(seq, "\tfallback due to SGs: %lu\n",220rk_cipher_algs[i]->stat_fb_sgdiff);221break;222case CRYPTO_ALG_TYPE_AHASH:223seq_printf(seq, "%s %s reqs=%lu fallback=%lu\n",224rk_cipher_algs[i]->alg.hash.base.halg.base.cra_driver_name,225rk_cipher_algs[i]->alg.hash.base.halg.base.cra_name,226rk_cipher_algs[i]->stat_req, rk_cipher_algs[i]->stat_fb);227break;228}229}230return 0;231}232233DEFINE_SHOW_ATTRIBUTE(rk_crypto_debugfs);234235static void register_debugfs(struct rk_crypto_info *crypto_info)236{237struct dentry *dbgfs_dir __maybe_unused;238struct dentry *dbgfs_stats __maybe_unused;239240/* Ignore error of debugfs */241dbgfs_dir = debugfs_create_dir("rk3288_crypto", NULL);242dbgfs_stats = debugfs_create_file("stats", 0444, dbgfs_dir, &rocklist,243&rk_crypto_debugfs_fops);244245#ifdef CONFIG_CRYPTO_DEV_ROCKCHIP_DEBUG246rocklist.dbgfs_dir = dbgfs_dir;247rocklist.dbgfs_stats = dbgfs_stats;248#endif249}250251static int rk_crypto_register(struct rk_crypto_info *crypto_info)252{253unsigned int i, k;254int err = 0;255256for (i = 0; i < ARRAY_SIZE(rk_cipher_algs); i++) {257rk_cipher_algs[i]->dev = crypto_info;258switch (rk_cipher_algs[i]->type) {259case CRYPTO_ALG_TYPE_SKCIPHER:260dev_info(crypto_info->dev, "Register %s as %s\n",261rk_cipher_algs[i]->alg.skcipher.base.base.cra_name,262rk_cipher_algs[i]->alg.skcipher.base.base.cra_driver_name);263err = crypto_engine_register_skcipher(&rk_cipher_algs[i]->alg.skcipher);264break;265case CRYPTO_ALG_TYPE_AHASH:266dev_info(crypto_info->dev, "Register %s as %s\n",267rk_cipher_algs[i]->alg.hash.base.halg.base.cra_name,268rk_cipher_algs[i]->alg.hash.base.halg.base.cra_driver_name);269err = crypto_engine_register_ahash(&rk_cipher_algs[i]->alg.hash);270break;271default:272dev_err(crypto_info->dev, "unknown algorithm\n");273}274if (err)275goto err_cipher_algs;276}277return 0;278279err_cipher_algs:280for (k = 0; k < i; k++) {281if (rk_cipher_algs[i]->type == CRYPTO_ALG_TYPE_SKCIPHER)282crypto_engine_unregister_skcipher(&rk_cipher_algs[k]->alg.skcipher);283else284crypto_engine_unregister_ahash(&rk_cipher_algs[i]->alg.hash);285}286return err;287}288289static void rk_crypto_unregister(void)290{291unsigned int i;292293for (i = 0; i < ARRAY_SIZE(rk_cipher_algs); i++) {294if (rk_cipher_algs[i]->type == CRYPTO_ALG_TYPE_SKCIPHER)295crypto_engine_unregister_skcipher(&rk_cipher_algs[i]->alg.skcipher);296else297crypto_engine_unregister_ahash(&rk_cipher_algs[i]->alg.hash);298}299}300301static const struct of_device_id crypto_of_id_table[] = {302{ .compatible = "rockchip,rk3288-crypto",303.data = &rk3288_variant,304},305{ .compatible = "rockchip,rk3328-crypto",306.data = &rk3328_variant,307},308{ .compatible = "rockchip,rk3399-crypto",309.data = &rk3399_variant,310},311{}312};313MODULE_DEVICE_TABLE(of, crypto_of_id_table);314315static int rk_crypto_probe(struct platform_device *pdev)316{317struct device *dev = &pdev->dev;318struct rk_crypto_info *crypto_info, *first;319int err = 0;320321crypto_info = devm_kzalloc(&pdev->dev,322sizeof(*crypto_info), GFP_KERNEL);323if (!crypto_info) {324err = -ENOMEM;325goto err_crypto;326}327328crypto_info->dev = &pdev->dev;329platform_set_drvdata(pdev, crypto_info);330331crypto_info->variant = of_device_get_match_data(&pdev->dev);332if (!crypto_info->variant) {333dev_err(&pdev->dev, "Missing variant\n");334return -EINVAL;335}336337crypto_info->rst = devm_reset_control_array_get_exclusive(dev);338if (IS_ERR(crypto_info->rst)) {339err = PTR_ERR(crypto_info->rst);340goto err_crypto;341}342343reset_control_assert(crypto_info->rst);344usleep_range(10, 20);345reset_control_deassert(crypto_info->rst);346347crypto_info->reg = devm_platform_ioremap_resource(pdev, 0);348if (IS_ERR(crypto_info->reg)) {349err = PTR_ERR(crypto_info->reg);350goto err_crypto;351}352353err = rk_crypto_get_clks(crypto_info);354if (err)355goto err_crypto;356357crypto_info->irq = platform_get_irq(pdev, 0);358if (crypto_info->irq < 0) {359err = crypto_info->irq;360goto err_crypto;361}362363err = devm_request_irq(&pdev->dev, crypto_info->irq,364rk_crypto_irq_handle, IRQF_SHARED,365"rk-crypto", pdev);366367if (err) {368dev_err(&pdev->dev, "irq request failed.\n");369goto err_crypto;370}371372crypto_info->engine = crypto_engine_alloc_init(&pdev->dev, true);373if (!crypto_info->engine) {374err = -ENOMEM;375goto err_crypto;376}377378crypto_engine_start(crypto_info->engine);379init_completion(&crypto_info->complete);380381err = rk_crypto_pm_init(crypto_info);382if (err)383goto err_pm;384385spin_lock(&rocklist.lock);386first = list_first_entry_or_null(&rocklist.dev_list,387struct rk_crypto_info, list);388list_add_tail(&crypto_info->list, &rocklist.dev_list);389spin_unlock(&rocklist.lock);390391if (!first) {392err = rk_crypto_register(crypto_info);393if (err) {394dev_err(dev, "Fail to register crypto algorithms");395goto err_register_alg;396}397398register_debugfs(crypto_info);399}400401return 0;402403err_register_alg:404rk_crypto_pm_exit(crypto_info);405err_pm:406crypto_engine_exit(crypto_info->engine);407err_crypto:408dev_err(dev, "Crypto Accelerator not successfully registered\n");409return err;410}411412static void rk_crypto_remove(struct platform_device *pdev)413{414struct rk_crypto_info *crypto_tmp = platform_get_drvdata(pdev);415struct rk_crypto_info *first;416417spin_lock_bh(&rocklist.lock);418list_del(&crypto_tmp->list);419first = list_first_entry_or_null(&rocklist.dev_list,420struct rk_crypto_info, list);421spin_unlock_bh(&rocklist.lock);422423if (!first) {424#ifdef CONFIG_CRYPTO_DEV_ROCKCHIP_DEBUG425debugfs_remove_recursive(rocklist.dbgfs_dir);426#endif427rk_crypto_unregister();428}429rk_crypto_pm_exit(crypto_tmp);430crypto_engine_exit(crypto_tmp->engine);431}432433static struct platform_driver crypto_driver = {434.probe = rk_crypto_probe,435.remove = rk_crypto_remove,436.driver = {437.name = "rk3288-crypto",438.pm = &rk_crypto_pm_ops,439.of_match_table = crypto_of_id_table,440},441};442443module_platform_driver(crypto_driver);444445MODULE_AUTHOR("Zain Wang <[email protected]>");446MODULE_DESCRIPTION("Support for Rockchip's cryptographic engine");447MODULE_LICENSE("GPL");448449450