Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for Samsung S5PV210 HW acceleration. |
| 5 | * |
| 6 | * Copyright (C) 2011 NetUP Inc. All rights reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/scatterlist.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/io.h> |
Naveen Krishna Chatradhi | 6b9f16e | 2014-05-08 21:58:13 +0800 | [diff] [blame] | 25 | #include <linux/of.h> |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 26 | #include <linux/crypto.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | |
| 29 | #include <crypto/algapi.h> |
| 30 | #include <crypto/aes.h> |
| 31 | #include <crypto/ctr.h> |
| 32 | |
| 33 | #include <plat/cpu.h> |
Sachin Kamat | a465348 | 2012-11-08 11:52:00 +0530 | [diff] [blame] | 34 | #include <mach/dma.h> |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 35 | |
| 36 | #define _SBF(s, v) ((v) << (s)) |
| 37 | #define _BIT(b) _SBF(b, 1) |
| 38 | |
| 39 | /* Feed control registers */ |
| 40 | #define SSS_REG_FCINTSTAT 0x0000 |
| 41 | #define SSS_FCINTSTAT_BRDMAINT _BIT(3) |
| 42 | #define SSS_FCINTSTAT_BTDMAINT _BIT(2) |
| 43 | #define SSS_FCINTSTAT_HRDMAINT _BIT(1) |
| 44 | #define SSS_FCINTSTAT_PKDMAINT _BIT(0) |
| 45 | |
| 46 | #define SSS_REG_FCINTENSET 0x0004 |
| 47 | #define SSS_FCINTENSET_BRDMAINTENSET _BIT(3) |
| 48 | #define SSS_FCINTENSET_BTDMAINTENSET _BIT(2) |
| 49 | #define SSS_FCINTENSET_HRDMAINTENSET _BIT(1) |
| 50 | #define SSS_FCINTENSET_PKDMAINTENSET _BIT(0) |
| 51 | |
| 52 | #define SSS_REG_FCINTENCLR 0x0008 |
| 53 | #define SSS_FCINTENCLR_BRDMAINTENCLR _BIT(3) |
| 54 | #define SSS_FCINTENCLR_BTDMAINTENCLR _BIT(2) |
| 55 | #define SSS_FCINTENCLR_HRDMAINTENCLR _BIT(1) |
| 56 | #define SSS_FCINTENCLR_PKDMAINTENCLR _BIT(0) |
| 57 | |
| 58 | #define SSS_REG_FCINTPEND 0x000C |
| 59 | #define SSS_FCINTPEND_BRDMAINTP _BIT(3) |
| 60 | #define SSS_FCINTPEND_BTDMAINTP _BIT(2) |
| 61 | #define SSS_FCINTPEND_HRDMAINTP _BIT(1) |
| 62 | #define SSS_FCINTPEND_PKDMAINTP _BIT(0) |
| 63 | |
| 64 | #define SSS_REG_FCFIFOSTAT 0x0010 |
| 65 | #define SSS_FCFIFOSTAT_BRFIFOFUL _BIT(7) |
| 66 | #define SSS_FCFIFOSTAT_BRFIFOEMP _BIT(6) |
| 67 | #define SSS_FCFIFOSTAT_BTFIFOFUL _BIT(5) |
| 68 | #define SSS_FCFIFOSTAT_BTFIFOEMP _BIT(4) |
| 69 | #define SSS_FCFIFOSTAT_HRFIFOFUL _BIT(3) |
| 70 | #define SSS_FCFIFOSTAT_HRFIFOEMP _BIT(2) |
| 71 | #define SSS_FCFIFOSTAT_PKFIFOFUL _BIT(1) |
| 72 | #define SSS_FCFIFOSTAT_PKFIFOEMP _BIT(0) |
| 73 | |
| 74 | #define SSS_REG_FCFIFOCTRL 0x0014 |
| 75 | #define SSS_FCFIFOCTRL_DESSEL _BIT(2) |
| 76 | #define SSS_HASHIN_INDEPENDENT _SBF(0, 0x00) |
| 77 | #define SSS_HASHIN_CIPHER_INPUT _SBF(0, 0x01) |
| 78 | #define SSS_HASHIN_CIPHER_OUTPUT _SBF(0, 0x02) |
| 79 | |
| 80 | #define SSS_REG_FCBRDMAS 0x0020 |
| 81 | #define SSS_REG_FCBRDMAL 0x0024 |
| 82 | #define SSS_REG_FCBRDMAC 0x0028 |
| 83 | #define SSS_FCBRDMAC_BYTESWAP _BIT(1) |
| 84 | #define SSS_FCBRDMAC_FLUSH _BIT(0) |
| 85 | |
| 86 | #define SSS_REG_FCBTDMAS 0x0030 |
| 87 | #define SSS_REG_FCBTDMAL 0x0034 |
| 88 | #define SSS_REG_FCBTDMAC 0x0038 |
| 89 | #define SSS_FCBTDMAC_BYTESWAP _BIT(1) |
| 90 | #define SSS_FCBTDMAC_FLUSH _BIT(0) |
| 91 | |
| 92 | #define SSS_REG_FCHRDMAS 0x0040 |
| 93 | #define SSS_REG_FCHRDMAL 0x0044 |
| 94 | #define SSS_REG_FCHRDMAC 0x0048 |
| 95 | #define SSS_FCHRDMAC_BYTESWAP _BIT(1) |
| 96 | #define SSS_FCHRDMAC_FLUSH _BIT(0) |
| 97 | |
| 98 | #define SSS_REG_FCPKDMAS 0x0050 |
| 99 | #define SSS_REG_FCPKDMAL 0x0054 |
| 100 | #define SSS_REG_FCPKDMAC 0x0058 |
| 101 | #define SSS_FCPKDMAC_BYTESWAP _BIT(3) |
| 102 | #define SSS_FCPKDMAC_DESCEND _BIT(2) |
| 103 | #define SSS_FCPKDMAC_TRANSMIT _BIT(1) |
| 104 | #define SSS_FCPKDMAC_FLUSH _BIT(0) |
| 105 | |
| 106 | #define SSS_REG_FCPKDMAO 0x005C |
| 107 | |
| 108 | /* AES registers */ |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 109 | #define SSS_REG_AES_CONTROL 0x00 |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 110 | #define SSS_AES_BYTESWAP_DI _BIT(11) |
| 111 | #define SSS_AES_BYTESWAP_DO _BIT(10) |
| 112 | #define SSS_AES_BYTESWAP_IV _BIT(9) |
| 113 | #define SSS_AES_BYTESWAP_CNT _BIT(8) |
| 114 | #define SSS_AES_BYTESWAP_KEY _BIT(7) |
| 115 | #define SSS_AES_KEY_CHANGE_MODE _BIT(6) |
| 116 | #define SSS_AES_KEY_SIZE_128 _SBF(4, 0x00) |
| 117 | #define SSS_AES_KEY_SIZE_192 _SBF(4, 0x01) |
| 118 | #define SSS_AES_KEY_SIZE_256 _SBF(4, 0x02) |
| 119 | #define SSS_AES_FIFO_MODE _BIT(3) |
| 120 | #define SSS_AES_CHAIN_MODE_ECB _SBF(1, 0x00) |
| 121 | #define SSS_AES_CHAIN_MODE_CBC _SBF(1, 0x01) |
| 122 | #define SSS_AES_CHAIN_MODE_CTR _SBF(1, 0x02) |
| 123 | #define SSS_AES_MODE_DECRYPT _BIT(0) |
| 124 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 125 | #define SSS_REG_AES_STATUS 0x04 |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 126 | #define SSS_AES_BUSY _BIT(2) |
| 127 | #define SSS_AES_INPUT_READY _BIT(1) |
| 128 | #define SSS_AES_OUTPUT_READY _BIT(0) |
| 129 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 130 | #define SSS_REG_AES_IN_DATA(s) (0x10 + (s << 2)) |
| 131 | #define SSS_REG_AES_OUT_DATA(s) (0x20 + (s << 2)) |
| 132 | #define SSS_REG_AES_IV_DATA(s) (0x30 + (s << 2)) |
| 133 | #define SSS_REG_AES_CNT_DATA(s) (0x40 + (s << 2)) |
| 134 | #define SSS_REG_AES_KEY_DATA(s) (0x80 + (s << 2)) |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 135 | |
| 136 | #define SSS_REG(dev, reg) ((dev)->ioaddr + (SSS_REG_##reg)) |
| 137 | #define SSS_READ(dev, reg) __raw_readl(SSS_REG(dev, reg)) |
| 138 | #define SSS_WRITE(dev, reg, val) __raw_writel((val), SSS_REG(dev, reg)) |
| 139 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 140 | #define SSS_AES_REG(dev, reg) ((dev)->aes_ioaddr + SSS_REG_##reg) |
| 141 | #define SSS_AES_WRITE(dev, reg, val) __raw_writel((val), \ |
| 142 | SSS_AES_REG(dev, reg)) |
| 143 | |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 144 | /* HW engine modes */ |
| 145 | #define FLAGS_AES_DECRYPT _BIT(0) |
| 146 | #define FLAGS_AES_MODE_MASK _SBF(1, 0x03) |
| 147 | #define FLAGS_AES_CBC _SBF(1, 0x01) |
| 148 | #define FLAGS_AES_CTR _SBF(1, 0x02) |
| 149 | |
| 150 | #define AES_KEY_LEN 16 |
| 151 | #define CRYPTO_QUEUE_LEN 1 |
| 152 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 153 | /** |
| 154 | * struct samsung_aes_variant - platform specific SSS driver data |
| 155 | * @has_hash_irq: true if SSS module uses hash interrupt, false otherwise |
| 156 | * @aes_offset: AES register offset from SSS module's base. |
| 157 | * |
| 158 | * Specifies platform specific configuration of SSS module. |
| 159 | * Note: A structure for driver specific platform data is used for future |
| 160 | * expansion of its usage. |
| 161 | */ |
| 162 | struct samsung_aes_variant { |
| 163 | bool has_hash_irq; |
| 164 | unsigned int aes_offset; |
| 165 | }; |
| 166 | |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 167 | struct s5p_aes_reqctx { |
| 168 | unsigned long mode; |
| 169 | }; |
| 170 | |
| 171 | struct s5p_aes_ctx { |
| 172 | struct s5p_aes_dev *dev; |
| 173 | |
| 174 | uint8_t aes_key[AES_MAX_KEY_SIZE]; |
| 175 | uint8_t nonce[CTR_RFC3686_NONCE_SIZE]; |
| 176 | int keylen; |
| 177 | }; |
| 178 | |
| 179 | struct s5p_aes_dev { |
| 180 | struct device *dev; |
| 181 | struct clk *clk; |
| 182 | void __iomem *ioaddr; |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 183 | void __iomem *aes_ioaddr; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 184 | int irq_hash; |
| 185 | int irq_fc; |
| 186 | |
| 187 | struct ablkcipher_request *req; |
| 188 | struct s5p_aes_ctx *ctx; |
| 189 | struct scatterlist *sg_src; |
| 190 | struct scatterlist *sg_dst; |
| 191 | |
| 192 | struct tasklet_struct tasklet; |
| 193 | struct crypto_queue queue; |
| 194 | bool busy; |
| 195 | spinlock_t lock; |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 196 | |
| 197 | struct samsung_aes_variant *variant; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static struct s5p_aes_dev *s5p_dev; |
| 201 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 202 | static const struct samsung_aes_variant s5p_aes_data = { |
| 203 | .has_hash_irq = true, |
| 204 | .aes_offset = 0x4000, |
| 205 | }; |
| 206 | |
| 207 | static const struct samsung_aes_variant exynos_aes_data = { |
| 208 | .has_hash_irq = false, |
| 209 | .aes_offset = 0x200, |
| 210 | }; |
| 211 | |
Naveen Krishna Chatradhi | 6b9f16e | 2014-05-08 21:58:13 +0800 | [diff] [blame] | 212 | static const struct of_device_id s5p_sss_dt_match[] = { |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 213 | { |
| 214 | .compatible = "samsung,s5pv210-secss", |
| 215 | .data = &s5p_aes_data, |
| 216 | }, |
| 217 | { |
| 218 | .compatible = "samsung,exynos4210-secss", |
| 219 | .data = &exynos_aes_data, |
| 220 | }, |
Naveen Krishna Chatradhi | 6b9f16e | 2014-05-08 21:58:13 +0800 | [diff] [blame] | 221 | { }, |
| 222 | }; |
| 223 | MODULE_DEVICE_TABLE(of, s5p_sss_dt_match); |
| 224 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 225 | static inline struct samsung_aes_variant *find_s5p_sss_version |
| 226 | (struct platform_device *pdev) |
| 227 | { |
| 228 | if (IS_ENABLED(CONFIG_OF) && (pdev->dev.of_node)) { |
| 229 | const struct of_device_id *match; |
| 230 | match = of_match_node(s5p_sss_dt_match, |
| 231 | pdev->dev.of_node); |
| 232 | return (struct samsung_aes_variant *)match->data; |
| 233 | } |
| 234 | return (struct samsung_aes_variant *) |
| 235 | platform_get_device_id(pdev)->driver_data; |
| 236 | } |
| 237 | |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 238 | static void s5p_set_dma_indata(struct s5p_aes_dev *dev, struct scatterlist *sg) |
| 239 | { |
| 240 | SSS_WRITE(dev, FCBRDMAS, sg_dma_address(sg)); |
| 241 | SSS_WRITE(dev, FCBRDMAL, sg_dma_len(sg)); |
| 242 | } |
| 243 | |
| 244 | static void s5p_set_dma_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg) |
| 245 | { |
| 246 | SSS_WRITE(dev, FCBTDMAS, sg_dma_address(sg)); |
| 247 | SSS_WRITE(dev, FCBTDMAL, sg_dma_len(sg)); |
| 248 | } |
| 249 | |
| 250 | static void s5p_aes_complete(struct s5p_aes_dev *dev, int err) |
| 251 | { |
| 252 | /* holding a lock outside */ |
| 253 | dev->req->base.complete(&dev->req->base, err); |
| 254 | dev->busy = false; |
| 255 | } |
| 256 | |
| 257 | static void s5p_unset_outdata(struct s5p_aes_dev *dev) |
| 258 | { |
| 259 | dma_unmap_sg(dev->dev, dev->sg_dst, 1, DMA_FROM_DEVICE); |
| 260 | } |
| 261 | |
| 262 | static void s5p_unset_indata(struct s5p_aes_dev *dev) |
| 263 | { |
| 264 | dma_unmap_sg(dev->dev, dev->sg_src, 1, DMA_TO_DEVICE); |
| 265 | } |
| 266 | |
| 267 | static int s5p_set_outdata(struct s5p_aes_dev *dev, struct scatterlist *sg) |
| 268 | { |
| 269 | int err; |
| 270 | |
| 271 | if (!IS_ALIGNED(sg_dma_len(sg), AES_BLOCK_SIZE)) { |
| 272 | err = -EINVAL; |
| 273 | goto exit; |
| 274 | } |
| 275 | if (!sg_dma_len(sg)) { |
| 276 | err = -EINVAL; |
| 277 | goto exit; |
| 278 | } |
| 279 | |
| 280 | err = dma_map_sg(dev->dev, sg, 1, DMA_FROM_DEVICE); |
| 281 | if (!err) { |
| 282 | err = -ENOMEM; |
| 283 | goto exit; |
| 284 | } |
| 285 | |
| 286 | dev->sg_dst = sg; |
| 287 | err = 0; |
| 288 | |
| 289 | exit: |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | static int s5p_set_indata(struct s5p_aes_dev *dev, struct scatterlist *sg) |
| 294 | { |
| 295 | int err; |
| 296 | |
| 297 | if (!IS_ALIGNED(sg_dma_len(sg), AES_BLOCK_SIZE)) { |
| 298 | err = -EINVAL; |
| 299 | goto exit; |
| 300 | } |
| 301 | if (!sg_dma_len(sg)) { |
| 302 | err = -EINVAL; |
| 303 | goto exit; |
| 304 | } |
| 305 | |
| 306 | err = dma_map_sg(dev->dev, sg, 1, DMA_TO_DEVICE); |
| 307 | if (!err) { |
| 308 | err = -ENOMEM; |
| 309 | goto exit; |
| 310 | } |
| 311 | |
| 312 | dev->sg_src = sg; |
| 313 | err = 0; |
| 314 | |
| 315 | exit: |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | static void s5p_aes_tx(struct s5p_aes_dev *dev) |
| 320 | { |
| 321 | int err = 0; |
| 322 | |
| 323 | s5p_unset_outdata(dev); |
| 324 | |
| 325 | if (!sg_is_last(dev->sg_dst)) { |
| 326 | err = s5p_set_outdata(dev, sg_next(dev->sg_dst)); |
| 327 | if (err) { |
| 328 | s5p_aes_complete(dev, err); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | s5p_set_dma_outdata(dev, dev->sg_dst); |
| 333 | } else |
| 334 | s5p_aes_complete(dev, err); |
| 335 | } |
| 336 | |
| 337 | static void s5p_aes_rx(struct s5p_aes_dev *dev) |
| 338 | { |
| 339 | int err; |
| 340 | |
| 341 | s5p_unset_indata(dev); |
| 342 | |
| 343 | if (!sg_is_last(dev->sg_src)) { |
| 344 | err = s5p_set_indata(dev, sg_next(dev->sg_src)); |
| 345 | if (err) { |
| 346 | s5p_aes_complete(dev, err); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | s5p_set_dma_indata(dev, dev->sg_src); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static irqreturn_t s5p_aes_interrupt(int irq, void *dev_id) |
| 355 | { |
| 356 | struct platform_device *pdev = dev_id; |
| 357 | struct s5p_aes_dev *dev = platform_get_drvdata(pdev); |
| 358 | uint32_t status; |
| 359 | unsigned long flags; |
| 360 | |
| 361 | spin_lock_irqsave(&dev->lock, flags); |
| 362 | |
| 363 | if (irq == dev->irq_fc) { |
| 364 | status = SSS_READ(dev, FCINTSTAT); |
| 365 | if (status & SSS_FCINTSTAT_BRDMAINT) |
| 366 | s5p_aes_rx(dev); |
| 367 | if (status & SSS_FCINTSTAT_BTDMAINT) |
| 368 | s5p_aes_tx(dev); |
| 369 | |
| 370 | SSS_WRITE(dev, FCINTPEND, status); |
| 371 | } |
| 372 | |
| 373 | spin_unlock_irqrestore(&dev->lock, flags); |
| 374 | |
| 375 | return IRQ_HANDLED; |
| 376 | } |
| 377 | |
| 378 | static void s5p_set_aes(struct s5p_aes_dev *dev, |
| 379 | uint8_t *key, uint8_t *iv, unsigned int keylen) |
| 380 | { |
| 381 | void __iomem *keystart; |
| 382 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 383 | memcpy(dev->aes_ioaddr + SSS_REG_AES_IV_DATA(0), iv, 0x10); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 384 | |
| 385 | if (keylen == AES_KEYSIZE_256) |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 386 | keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(0); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 387 | else if (keylen == AES_KEYSIZE_192) |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 388 | keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(2); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 389 | else |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 390 | keystart = dev->aes_ioaddr + SSS_REG_AES_KEY_DATA(4); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 391 | |
| 392 | memcpy(keystart, key, keylen); |
| 393 | } |
| 394 | |
| 395 | static void s5p_aes_crypt_start(struct s5p_aes_dev *dev, unsigned long mode) |
| 396 | { |
| 397 | struct ablkcipher_request *req = dev->req; |
| 398 | |
| 399 | uint32_t aes_control; |
| 400 | int err; |
| 401 | unsigned long flags; |
| 402 | |
| 403 | aes_control = SSS_AES_KEY_CHANGE_MODE; |
| 404 | if (mode & FLAGS_AES_DECRYPT) |
| 405 | aes_control |= SSS_AES_MODE_DECRYPT; |
| 406 | |
| 407 | if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CBC) |
| 408 | aes_control |= SSS_AES_CHAIN_MODE_CBC; |
| 409 | else if ((mode & FLAGS_AES_MODE_MASK) == FLAGS_AES_CTR) |
| 410 | aes_control |= SSS_AES_CHAIN_MODE_CTR; |
| 411 | |
| 412 | if (dev->ctx->keylen == AES_KEYSIZE_192) |
| 413 | aes_control |= SSS_AES_KEY_SIZE_192; |
| 414 | else if (dev->ctx->keylen == AES_KEYSIZE_256) |
| 415 | aes_control |= SSS_AES_KEY_SIZE_256; |
| 416 | |
| 417 | aes_control |= SSS_AES_FIFO_MODE; |
| 418 | |
| 419 | /* as a variant it is possible to use byte swapping on DMA side */ |
| 420 | aes_control |= SSS_AES_BYTESWAP_DI |
| 421 | | SSS_AES_BYTESWAP_DO |
| 422 | | SSS_AES_BYTESWAP_IV |
| 423 | | SSS_AES_BYTESWAP_KEY |
| 424 | | SSS_AES_BYTESWAP_CNT; |
| 425 | |
| 426 | spin_lock_irqsave(&dev->lock, flags); |
| 427 | |
| 428 | SSS_WRITE(dev, FCINTENCLR, |
| 429 | SSS_FCINTENCLR_BTDMAINTENCLR | SSS_FCINTENCLR_BRDMAINTENCLR); |
| 430 | SSS_WRITE(dev, FCFIFOCTRL, 0x00); |
| 431 | |
| 432 | err = s5p_set_indata(dev, req->src); |
| 433 | if (err) |
| 434 | goto indata_error; |
| 435 | |
| 436 | err = s5p_set_outdata(dev, req->dst); |
| 437 | if (err) |
| 438 | goto outdata_error; |
| 439 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 440 | SSS_AES_WRITE(dev, AES_CONTROL, aes_control); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 441 | s5p_set_aes(dev, dev->ctx->aes_key, req->info, dev->ctx->keylen); |
| 442 | |
| 443 | s5p_set_dma_indata(dev, req->src); |
| 444 | s5p_set_dma_outdata(dev, req->dst); |
| 445 | |
| 446 | SSS_WRITE(dev, FCINTENSET, |
| 447 | SSS_FCINTENSET_BTDMAINTENSET | SSS_FCINTENSET_BRDMAINTENSET); |
| 448 | |
| 449 | spin_unlock_irqrestore(&dev->lock, flags); |
| 450 | |
| 451 | return; |
| 452 | |
| 453 | outdata_error: |
| 454 | s5p_unset_indata(dev); |
| 455 | |
| 456 | indata_error: |
| 457 | s5p_aes_complete(dev, err); |
| 458 | spin_unlock_irqrestore(&dev->lock, flags); |
| 459 | } |
| 460 | |
| 461 | static void s5p_tasklet_cb(unsigned long data) |
| 462 | { |
| 463 | struct s5p_aes_dev *dev = (struct s5p_aes_dev *)data; |
| 464 | struct crypto_async_request *async_req, *backlog; |
| 465 | struct s5p_aes_reqctx *reqctx; |
| 466 | unsigned long flags; |
| 467 | |
| 468 | spin_lock_irqsave(&dev->lock, flags); |
| 469 | backlog = crypto_get_backlog(&dev->queue); |
| 470 | async_req = crypto_dequeue_request(&dev->queue); |
| 471 | spin_unlock_irqrestore(&dev->lock, flags); |
| 472 | |
| 473 | if (!async_req) |
| 474 | return; |
| 475 | |
| 476 | if (backlog) |
| 477 | backlog->complete(backlog, -EINPROGRESS); |
| 478 | |
| 479 | dev->req = ablkcipher_request_cast(async_req); |
| 480 | dev->ctx = crypto_tfm_ctx(dev->req->base.tfm); |
| 481 | reqctx = ablkcipher_request_ctx(dev->req); |
| 482 | |
| 483 | s5p_aes_crypt_start(dev, reqctx->mode); |
| 484 | } |
| 485 | |
| 486 | static int s5p_aes_handle_req(struct s5p_aes_dev *dev, |
| 487 | struct ablkcipher_request *req) |
| 488 | { |
| 489 | unsigned long flags; |
| 490 | int err; |
| 491 | |
| 492 | spin_lock_irqsave(&dev->lock, flags); |
| 493 | if (dev->busy) { |
| 494 | err = -EAGAIN; |
| 495 | spin_unlock_irqrestore(&dev->lock, flags); |
| 496 | goto exit; |
| 497 | } |
| 498 | dev->busy = true; |
| 499 | |
| 500 | err = ablkcipher_enqueue_request(&dev->queue, req); |
| 501 | spin_unlock_irqrestore(&dev->lock, flags); |
| 502 | |
| 503 | tasklet_schedule(&dev->tasklet); |
| 504 | |
| 505 | exit: |
| 506 | return err; |
| 507 | } |
| 508 | |
| 509 | static int s5p_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 510 | { |
| 511 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
| 512 | struct s5p_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
| 513 | struct s5p_aes_reqctx *reqctx = ablkcipher_request_ctx(req); |
| 514 | struct s5p_aes_dev *dev = ctx->dev; |
| 515 | |
| 516 | if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) { |
| 517 | pr_err("request size is not exact amount of AES blocks\n"); |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | reqctx->mode = mode; |
| 522 | |
| 523 | return s5p_aes_handle_req(dev, req); |
| 524 | } |
| 525 | |
| 526 | static int s5p_aes_setkey(struct crypto_ablkcipher *cipher, |
| 527 | const uint8_t *key, unsigned int keylen) |
| 528 | { |
| 529 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 530 | struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 531 | |
| 532 | if (keylen != AES_KEYSIZE_128 && |
| 533 | keylen != AES_KEYSIZE_192 && |
| 534 | keylen != AES_KEYSIZE_256) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | memcpy(ctx->aes_key, key, keylen); |
| 538 | ctx->keylen = keylen; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int s5p_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 544 | { |
| 545 | return s5p_aes_crypt(req, 0); |
| 546 | } |
| 547 | |
| 548 | static int s5p_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 549 | { |
| 550 | return s5p_aes_crypt(req, FLAGS_AES_DECRYPT); |
| 551 | } |
| 552 | |
| 553 | static int s5p_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 554 | { |
| 555 | return s5p_aes_crypt(req, FLAGS_AES_CBC); |
| 556 | } |
| 557 | |
| 558 | static int s5p_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 559 | { |
| 560 | return s5p_aes_crypt(req, FLAGS_AES_DECRYPT | FLAGS_AES_CBC); |
| 561 | } |
| 562 | |
| 563 | static int s5p_aes_cra_init(struct crypto_tfm *tfm) |
| 564 | { |
| 565 | struct s5p_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 566 | |
| 567 | ctx->dev = s5p_dev; |
| 568 | tfm->crt_ablkcipher.reqsize = sizeof(struct s5p_aes_reqctx); |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static struct crypto_alg algs[] = { |
| 574 | { |
| 575 | .cra_name = "ecb(aes)", |
| 576 | .cra_driver_name = "ecb-aes-s5p", |
| 577 | .cra_priority = 100, |
| 578 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 579 | CRYPTO_ALG_ASYNC | |
| 580 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 581 | .cra_blocksize = AES_BLOCK_SIZE, |
| 582 | .cra_ctxsize = sizeof(struct s5p_aes_ctx), |
| 583 | .cra_alignmask = 0x0f, |
| 584 | .cra_type = &crypto_ablkcipher_type, |
| 585 | .cra_module = THIS_MODULE, |
| 586 | .cra_init = s5p_aes_cra_init, |
| 587 | .cra_u.ablkcipher = { |
| 588 | .min_keysize = AES_MIN_KEY_SIZE, |
| 589 | .max_keysize = AES_MAX_KEY_SIZE, |
| 590 | .setkey = s5p_aes_setkey, |
| 591 | .encrypt = s5p_aes_ecb_encrypt, |
| 592 | .decrypt = s5p_aes_ecb_decrypt, |
| 593 | } |
| 594 | }, |
| 595 | { |
| 596 | .cra_name = "cbc(aes)", |
| 597 | .cra_driver_name = "cbc-aes-s5p", |
| 598 | .cra_priority = 100, |
| 599 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 600 | CRYPTO_ALG_ASYNC | |
| 601 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 602 | .cra_blocksize = AES_BLOCK_SIZE, |
| 603 | .cra_ctxsize = sizeof(struct s5p_aes_ctx), |
| 604 | .cra_alignmask = 0x0f, |
| 605 | .cra_type = &crypto_ablkcipher_type, |
| 606 | .cra_module = THIS_MODULE, |
| 607 | .cra_init = s5p_aes_cra_init, |
| 608 | .cra_u.ablkcipher = { |
| 609 | .min_keysize = AES_MIN_KEY_SIZE, |
| 610 | .max_keysize = AES_MAX_KEY_SIZE, |
| 611 | .ivsize = AES_BLOCK_SIZE, |
| 612 | .setkey = s5p_aes_setkey, |
| 613 | .encrypt = s5p_aes_cbc_encrypt, |
| 614 | .decrypt = s5p_aes_cbc_decrypt, |
| 615 | } |
| 616 | }, |
| 617 | }; |
| 618 | |
| 619 | static int s5p_aes_probe(struct platform_device *pdev) |
| 620 | { |
| 621 | int i, j, err = -ENODEV; |
| 622 | struct s5p_aes_dev *pdata; |
| 623 | struct device *dev = &pdev->dev; |
| 624 | struct resource *res; |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 625 | struct samsung_aes_variant *variant; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 626 | |
| 627 | if (s5p_dev) |
| 628 | return -EEXIST; |
| 629 | |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 630 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 631 | if (!pdata) |
| 632 | return -ENOMEM; |
| 633 | |
Jingoo Han | 0fdefe2 | 2014-02-12 13:24:57 +0900 | [diff] [blame] | 634 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 635 | pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res); |
| 636 | if (IS_ERR(pdata->ioaddr)) |
| 637 | return PTR_ERR(pdata->ioaddr); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 638 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 639 | variant = find_s5p_sss_version(pdev); |
| 640 | |
Jingoo Han | 5c22ba6 | 2013-01-10 11:05:30 +0900 | [diff] [blame] | 641 | pdata->clk = devm_clk_get(dev, "secss"); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 642 | if (IS_ERR(pdata->clk)) { |
| 643 | dev_err(dev, "failed to find secss clock source\n"); |
| 644 | return -ENOENT; |
| 645 | } |
| 646 | |
| 647 | clk_enable(pdata->clk); |
| 648 | |
| 649 | spin_lock_init(&pdata->lock); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 650 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 651 | pdata->aes_ioaddr = pdata->ioaddr + variant->aes_offset; |
| 652 | |
Naveen Krishna Chatradhi | 96fc70b | 2014-05-08 21:58:12 +0800 | [diff] [blame] | 653 | pdata->irq_fc = platform_get_irq(pdev, 0); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 654 | if (pdata->irq_fc < 0) { |
| 655 | err = pdata->irq_fc; |
| 656 | dev_warn(dev, "feed control interrupt is not available.\n"); |
| 657 | goto err_irq; |
| 658 | } |
| 659 | err = devm_request_irq(dev, pdata->irq_fc, s5p_aes_interrupt, |
| 660 | IRQF_SHARED, pdev->name, pdev); |
| 661 | if (err < 0) { |
| 662 | dev_warn(dev, "feed control interrupt is not available.\n"); |
| 663 | goto err_irq; |
| 664 | } |
| 665 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 666 | if (variant->has_hash_irq) { |
| 667 | pdata->irq_hash = platform_get_irq(pdev, 1); |
| 668 | if (pdata->irq_hash < 0) { |
| 669 | err = pdata->irq_hash; |
| 670 | dev_warn(dev, "hash interrupt is not available.\n"); |
| 671 | goto err_irq; |
| 672 | } |
| 673 | err = devm_request_irq(dev, pdata->irq_hash, s5p_aes_interrupt, |
| 674 | IRQF_SHARED, pdev->name, pdev); |
| 675 | if (err < 0) { |
| 676 | dev_warn(dev, "hash interrupt is not available.\n"); |
| 677 | goto err_irq; |
| 678 | } |
Naveen Krishna Chatradhi | 96fc70b | 2014-05-08 21:58:12 +0800 | [diff] [blame] | 679 | } |
| 680 | |
Naveen Krishna Chatradhi | 8924510 | 2014-05-08 21:58:14 +0800 | [diff] [blame^] | 681 | pdata->variant = variant; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 682 | pdata->dev = dev; |
| 683 | platform_set_drvdata(pdev, pdata); |
| 684 | s5p_dev = pdata; |
| 685 | |
| 686 | tasklet_init(&pdata->tasklet, s5p_tasklet_cb, (unsigned long)pdata); |
| 687 | crypto_init_queue(&pdata->queue, CRYPTO_QUEUE_LEN); |
| 688 | |
| 689 | for (i = 0; i < ARRAY_SIZE(algs); i++) { |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 690 | err = crypto_register_alg(&algs[i]); |
| 691 | if (err) |
| 692 | goto err_algs; |
| 693 | } |
| 694 | |
| 695 | pr_info("s5p-sss driver registered\n"); |
| 696 | |
| 697 | return 0; |
| 698 | |
| 699 | err_algs: |
| 700 | dev_err(dev, "can't register '%s': %d\n", algs[i].cra_name, err); |
| 701 | |
| 702 | for (j = 0; j < i; j++) |
| 703 | crypto_unregister_alg(&algs[j]); |
| 704 | |
| 705 | tasklet_kill(&pdata->tasklet); |
| 706 | |
| 707 | err_irq: |
| 708 | clk_disable(pdata->clk); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 709 | |
| 710 | s5p_dev = NULL; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 711 | |
| 712 | return err; |
| 713 | } |
| 714 | |
| 715 | static int s5p_aes_remove(struct platform_device *pdev) |
| 716 | { |
| 717 | struct s5p_aes_dev *pdata = platform_get_drvdata(pdev); |
| 718 | int i; |
| 719 | |
| 720 | if (!pdata) |
| 721 | return -ENODEV; |
| 722 | |
| 723 | for (i = 0; i < ARRAY_SIZE(algs); i++) |
| 724 | crypto_unregister_alg(&algs[i]); |
| 725 | |
| 726 | tasklet_kill(&pdata->tasklet); |
| 727 | |
| 728 | clk_disable(pdata->clk); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 729 | |
| 730 | s5p_dev = NULL; |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static struct platform_driver s5p_aes_crypto = { |
| 736 | .probe = s5p_aes_probe, |
| 737 | .remove = s5p_aes_remove, |
| 738 | .driver = { |
| 739 | .owner = THIS_MODULE, |
| 740 | .name = "s5p-secss", |
Naveen Krishna Chatradhi | 6b9f16e | 2014-05-08 21:58:13 +0800 | [diff] [blame] | 741 | .of_match_table = s5p_sss_dt_match, |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 742 | }, |
| 743 | }; |
| 744 | |
Axel Lin | 741e8c2 | 2011-11-26 21:26:19 +0800 | [diff] [blame] | 745 | module_platform_driver(s5p_aes_crypto); |
Vladimir Zapolskiy | a49e490 | 2011-04-08 20:40:51 +0800 | [diff] [blame] | 746 | |
| 747 | MODULE_DESCRIPTION("S5PV210 AES hw acceleration support."); |
| 748 | MODULE_LICENSE("GPL v2"); |
| 749 | MODULE_AUTHOR("Vladimir Zapolskiy <vzapolskiy@gmail.com>"); |