192-crypto-add-ss.patch 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. From 6298e948215f2a3eb8a9af5c490d025deb66f179 Mon Sep 17 00:00:00 2001
  2. From: LABBE Corentin <clabbe.montjoie@gmail.com>
  3. Date: Fri, 17 Jul 2015 16:39:41 +0200
  4. Subject: [PATCH] crypto: sunxi-ss - Add Allwinner Security System crypto
  5. accelerator
  6. Add support for the Security System included in Allwinner SoC A20.
  7. The Security System is a hardware cryptographic accelerator that support:
  8. - MD5 and SHA1 hash algorithms
  9. - AES block cipher in CBC/ECB mode with 128/196/256bits keys.
  10. - DES and 3DES block cipher in CBC/ECB mode
  11. Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
  12. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  13. ---
  14. drivers/crypto/Kconfig | 17 +
  15. drivers/crypto/Makefile | 1 +
  16. drivers/crypto/sunxi-ss/Makefile | 2 +
  17. drivers/crypto/sunxi-ss/sun4i-ss-cipher.c | 542 ++++++++++++++++++++++++++++++
  18. drivers/crypto/sunxi-ss/sun4i-ss-core.c | 403 ++++++++++++++++++++++
  19. drivers/crypto/sunxi-ss/sun4i-ss-hash.c | 492 +++++++++++++++++++++++++++
  20. drivers/crypto/sunxi-ss/sun4i-ss.h | 199 +++++++++++
  21. 7 files changed, 1656 insertions(+)
  22. create mode 100644 drivers/crypto/sunxi-ss/Makefile
  23. create mode 100644 drivers/crypto/sunxi-ss/sun4i-ss-cipher.c
  24. create mode 100644 drivers/crypto/sunxi-ss/sun4i-ss-core.c
  25. create mode 100644 drivers/crypto/sunxi-ss/sun4i-ss-hash.c
  26. create mode 100644 drivers/crypto/sunxi-ss/sun4i-ss.h
  27. --- a/drivers/crypto/Kconfig
  28. +++ b/drivers/crypto/Kconfig
  29. @@ -460,4 +460,21 @@ config CRYPTO_DEV_IMGTEC_HASH
  30. hardware hash accelerator. Supporting MD5/SHA1/SHA224/SHA256
  31. hashing algorithms.
  32. +config CRYPTO_DEV_SUN4I_SS
  33. + tristate "Support for Allwinner Security System cryptographic accelerator"
  34. + depends on ARCH_SUNXI
  35. + select CRYPTO_MD5
  36. + select CRYPTO_SHA1
  37. + select CRYPTO_AES
  38. + select CRYPTO_DES
  39. + select CRYPTO_BLKCIPHER
  40. + help
  41. + Some Allwinner SoC have a crypto accelerator named
  42. + Security System. Select this if you want to use it.
  43. + The Security System handle AES/DES/3DES ciphers in CBC mode
  44. + and SHA1 and MD5 hash algorithms.
  45. +
  46. + To compile this driver as a module, choose M here: the module
  47. + will be called sun4i-ss.
  48. +
  49. endif # CRYPTO_HW
  50. --- a/drivers/crypto/Makefile
  51. +++ b/drivers/crypto/Makefile
  52. @@ -27,3 +27,4 @@ obj-$(CONFIG_CRYPTO_DEV_UX500) += ux500/
  53. obj-$(CONFIG_CRYPTO_DEV_QAT) += qat/
  54. obj-$(CONFIG_CRYPTO_DEV_QCE) += qce/
  55. obj-$(CONFIG_CRYPTO_DEV_VMX) += vmx/
  56. +obj-$(CONFIG_CRYPTO_DEV_SUN4I_SS) += sunxi-ss/
  57. --- /dev/null
  58. +++ b/drivers/crypto/sunxi-ss/Makefile
  59. @@ -0,0 +1,2 @@
  60. +obj-$(CONFIG_CRYPTO_DEV_SUN4I_SS) += sun4i-ss.o
  61. +sun4i-ss-y += sun4i-ss-core.o sun4i-ss-hash.o sun4i-ss-cipher.o
  62. --- /dev/null
  63. +++ b/drivers/crypto/sunxi-ss/sun4i-ss-cipher.c
  64. @@ -0,0 +1,542 @@
  65. +/*
  66. + * sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
  67. + *
  68. + * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  69. + *
  70. + * This file add support for AES cipher with 128,192,256 bits
  71. + * keysize in CBC and ECB mode.
  72. + * Add support also for DES and 3DES in CBC and ECB mode.
  73. + *
  74. + * You could find the datasheet in Documentation/arm/sunxi/README
  75. + *
  76. + * This program is free software; you can redistribute it and/or modify
  77. + * it under the terms of the GNU General Public License as published by
  78. + * the Free Software Foundation; either version 2 of the License, or
  79. + * (at your option) any later version.
  80. + */
  81. +#include "sun4i-ss.h"
  82. +
  83. +static int sun4i_ss_opti_poll(struct ablkcipher_request *areq)
  84. +{
  85. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  86. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  87. + struct sun4i_ss_ctx *ss = op->ss;
  88. + unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  89. + struct sun4i_cipher_req_ctx *ctx = ablkcipher_request_ctx(areq);
  90. + u32 mode = ctx->mode;
  91. + /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
  92. + u32 rx_cnt = SS_RX_DEFAULT;
  93. + u32 tx_cnt = 0;
  94. + u32 spaces;
  95. + u32 v;
  96. + int i, err = 0;
  97. + unsigned int ileft = areq->nbytes;
  98. + unsigned int oleft = areq->nbytes;
  99. + unsigned int todo;
  100. + struct sg_mapping_iter mi, mo;
  101. + unsigned int oi, oo; /* offset for in and out */
  102. +
  103. + if (areq->nbytes == 0)
  104. + return 0;
  105. +
  106. + if (!areq->info) {
  107. + dev_err_ratelimited(ss->dev, "ERROR: Empty IV\n");
  108. + return -EINVAL;
  109. + }
  110. +
  111. + if (!areq->src || !areq->dst) {
  112. + dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
  113. + return -EINVAL;
  114. + }
  115. +
  116. + spin_lock_bh(&ss->slock);
  117. +
  118. + for (i = 0; i < op->keylen; i += 4)
  119. + writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
  120. +
  121. + if (areq->info) {
  122. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  123. + v = *(u32 *)(areq->info + i * 4);
  124. + writel(v, ss->base + SS_IV0 + i * 4);
  125. + }
  126. + }
  127. + writel(mode, ss->base + SS_CTL);
  128. +
  129. + sg_miter_start(&mi, areq->src, sg_nents(areq->src),
  130. + SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  131. + sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
  132. + SG_MITER_TO_SG | SG_MITER_ATOMIC);
  133. + sg_miter_next(&mi);
  134. + sg_miter_next(&mo);
  135. + if (!mi.addr || !mo.addr) {
  136. + dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
  137. + err = -EINVAL;
  138. + goto release_ss;
  139. + }
  140. +
  141. + ileft = areq->nbytes / 4;
  142. + oleft = areq->nbytes / 4;
  143. + oi = 0;
  144. + oo = 0;
  145. + do {
  146. + todo = min3(rx_cnt, ileft, (mi.length - oi) / 4);
  147. + if (todo > 0) {
  148. + ileft -= todo;
  149. + writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
  150. + oi += todo * 4;
  151. + }
  152. + if (oi == mi.length) {
  153. + sg_miter_next(&mi);
  154. + oi = 0;
  155. + }
  156. +
  157. + spaces = readl(ss->base + SS_FCSR);
  158. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  159. + tx_cnt = SS_TXFIFO_SPACES(spaces);
  160. +
  161. + todo = min3(tx_cnt, oleft, (mo.length - oo) / 4);
  162. + if (todo > 0) {
  163. + oleft -= todo;
  164. + readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
  165. + oo += todo * 4;
  166. + }
  167. + if (oo == mo.length) {
  168. + sg_miter_next(&mo);
  169. + oo = 0;
  170. + }
  171. + } while (mo.length > 0);
  172. +
  173. + if (areq->info) {
  174. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  175. + v = readl(ss->base + SS_IV0 + i * 4);
  176. + *(u32 *)(areq->info + i * 4) = v;
  177. + }
  178. + }
  179. +
  180. +release_ss:
  181. + sg_miter_stop(&mi);
  182. + sg_miter_stop(&mo);
  183. + writel(0, ss->base + SS_CTL);
  184. + spin_unlock_bh(&ss->slock);
  185. + return err;
  186. +}
  187. +
  188. +/* Generic function that support SG with size not multiple of 4 */
  189. +static int sun4i_ss_cipher_poll(struct ablkcipher_request *areq)
  190. +{
  191. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  192. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  193. + struct sun4i_ss_ctx *ss = op->ss;
  194. + int no_chunk = 1;
  195. + struct scatterlist *in_sg = areq->src;
  196. + struct scatterlist *out_sg = areq->dst;
  197. + unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
  198. + struct sun4i_cipher_req_ctx *ctx = ablkcipher_request_ctx(areq);
  199. + u32 mode = ctx->mode;
  200. + /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
  201. + u32 rx_cnt = SS_RX_DEFAULT;
  202. + u32 tx_cnt = 0;
  203. + u32 v;
  204. + u32 spaces;
  205. + int i, err = 0;
  206. + unsigned int ileft = areq->nbytes;
  207. + unsigned int oleft = areq->nbytes;
  208. + unsigned int todo;
  209. + struct sg_mapping_iter mi, mo;
  210. + unsigned int oi, oo; /* offset for in and out */
  211. + char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
  212. + char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
  213. + unsigned int ob = 0; /* offset in buf */
  214. + unsigned int obo = 0; /* offset in bufo*/
  215. + unsigned int obl = 0; /* length of data in bufo */
  216. +
  217. + if (areq->nbytes == 0)
  218. + return 0;
  219. +
  220. + if (!areq->info) {
  221. + dev_err_ratelimited(ss->dev, "ERROR: Empty IV\n");
  222. + return -EINVAL;
  223. + }
  224. +
  225. + if (!areq->src || !areq->dst) {
  226. + dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
  227. + return -EINVAL;
  228. + }
  229. +
  230. + /*
  231. + * if we have only SGs with size multiple of 4,
  232. + * we can use the SS optimized function
  233. + */
  234. + while (in_sg && no_chunk == 1) {
  235. + if ((in_sg->length % 4) != 0)
  236. + no_chunk = 0;
  237. + in_sg = sg_next(in_sg);
  238. + }
  239. + while (out_sg && no_chunk == 1) {
  240. + if ((out_sg->length % 4) != 0)
  241. + no_chunk = 0;
  242. + out_sg = sg_next(out_sg);
  243. + }
  244. +
  245. + if (no_chunk == 1)
  246. + return sun4i_ss_opti_poll(areq);
  247. +
  248. + spin_lock_bh(&ss->slock);
  249. +
  250. + for (i = 0; i < op->keylen; i += 4)
  251. + writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
  252. +
  253. + if (areq->info) {
  254. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  255. + v = *(u32 *)(areq->info + i * 4);
  256. + writel(v, ss->base + SS_IV0 + i * 4);
  257. + }
  258. + }
  259. + writel(mode, ss->base + SS_CTL);
  260. +
  261. + sg_miter_start(&mi, areq->src, sg_nents(areq->src),
  262. + SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  263. + sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
  264. + SG_MITER_TO_SG | SG_MITER_ATOMIC);
  265. + sg_miter_next(&mi);
  266. + sg_miter_next(&mo);
  267. + if (!mi.addr || !mo.addr) {
  268. + dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
  269. + err = -EINVAL;
  270. + goto release_ss;
  271. + }
  272. + ileft = areq->nbytes;
  273. + oleft = areq->nbytes;
  274. + oi = 0;
  275. + oo = 0;
  276. +
  277. + while (oleft > 0) {
  278. + if (ileft > 0) {
  279. + /*
  280. + * todo is the number of consecutive 4byte word that we
  281. + * can read from current SG
  282. + */
  283. + todo = min3(rx_cnt, ileft / 4, (mi.length - oi) / 4);
  284. + if (todo > 0 && ob == 0) {
  285. + writesl(ss->base + SS_RXFIFO, mi.addr + oi,
  286. + todo);
  287. + ileft -= todo * 4;
  288. + oi += todo * 4;
  289. + } else {
  290. + /*
  291. + * not enough consecutive bytes, so we need to
  292. + * linearize in buf. todo is in bytes
  293. + * After that copy, if we have a multiple of 4
  294. + * we need to be able to write all buf in one
  295. + * pass, so it is why we min() with rx_cnt
  296. + */
  297. + todo = min3(rx_cnt * 4 - ob, ileft,
  298. + mi.length - oi);
  299. + memcpy(buf + ob, mi.addr + oi, todo);
  300. + ileft -= todo;
  301. + oi += todo;
  302. + ob += todo;
  303. + if (ob % 4 == 0) {
  304. + writesl(ss->base + SS_RXFIFO, buf,
  305. + ob / 4);
  306. + ob = 0;
  307. + }
  308. + }
  309. + if (oi == mi.length) {
  310. + sg_miter_next(&mi);
  311. + oi = 0;
  312. + }
  313. + }
  314. +
  315. + spaces = readl(ss->base + SS_FCSR);
  316. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  317. + tx_cnt = SS_TXFIFO_SPACES(spaces);
  318. + dev_dbg(ss->dev, "%x %u/%u %u/%u cnt=%u %u/%u %u/%u cnt=%u %u %u\n",
  319. + mode,
  320. + oi, mi.length, ileft, areq->nbytes, rx_cnt,
  321. + oo, mo.length, oleft, areq->nbytes, tx_cnt,
  322. + todo, ob);
  323. +
  324. + if (tx_cnt == 0)
  325. + continue;
  326. + /* todo in 4bytes word */
  327. + todo = min3(tx_cnt, oleft / 4, (mo.length - oo) / 4);
  328. + if (todo > 0) {
  329. + readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
  330. + oleft -= todo * 4;
  331. + oo += todo * 4;
  332. + if (oo == mo.length) {
  333. + sg_miter_next(&mo);
  334. + oo = 0;
  335. + }
  336. + } else {
  337. + /*
  338. + * read obl bytes in bufo, we read at maximum for
  339. + * emptying the device
  340. + */
  341. + readsl(ss->base + SS_TXFIFO, bufo, tx_cnt);
  342. + obl = tx_cnt * 4;
  343. + obo = 0;
  344. + do {
  345. + /*
  346. + * how many bytes we can copy ?
  347. + * no more than remaining SG size
  348. + * no more than remaining buffer
  349. + * no need to test against oleft
  350. + */
  351. + todo = min(mo.length - oo, obl - obo);
  352. + memcpy(mo.addr + oo, bufo + obo, todo);
  353. + oleft -= todo;
  354. + obo += todo;
  355. + oo += todo;
  356. + if (oo == mo.length) {
  357. + sg_miter_next(&mo);
  358. + oo = 0;
  359. + }
  360. + } while (obo < obl);
  361. + /* bufo must be fully used here */
  362. + }
  363. + }
  364. + if (areq->info) {
  365. + for (i = 0; i < 4 && i < ivsize / 4; i++) {
  366. + v = readl(ss->base + SS_IV0 + i * 4);
  367. + *(u32 *)(areq->info + i * 4) = v;
  368. + }
  369. + }
  370. +
  371. +release_ss:
  372. + sg_miter_stop(&mi);
  373. + sg_miter_stop(&mo);
  374. + writel(0, ss->base + SS_CTL);
  375. + spin_unlock_bh(&ss->slock);
  376. +
  377. + return err;
  378. +}
  379. +
  380. +/* CBC AES */
  381. +int sun4i_ss_cbc_aes_encrypt(struct ablkcipher_request *areq)
  382. +{
  383. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  384. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  385. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  386. +
  387. + rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
  388. + op->keymode;
  389. + return sun4i_ss_cipher_poll(areq);
  390. +}
  391. +
  392. +int sun4i_ss_cbc_aes_decrypt(struct ablkcipher_request *areq)
  393. +{
  394. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  395. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  396. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  397. +
  398. + rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
  399. + op->keymode;
  400. + return sun4i_ss_cipher_poll(areq);
  401. +}
  402. +
  403. +/* ECB AES */
  404. +int sun4i_ss_ecb_aes_encrypt(struct ablkcipher_request *areq)
  405. +{
  406. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  407. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  408. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  409. +
  410. + rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
  411. + op->keymode;
  412. + return sun4i_ss_cipher_poll(areq);
  413. +}
  414. +
  415. +int sun4i_ss_ecb_aes_decrypt(struct ablkcipher_request *areq)
  416. +{
  417. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  418. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  419. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  420. +
  421. + rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
  422. + op->keymode;
  423. + return sun4i_ss_cipher_poll(areq);
  424. +}
  425. +
  426. +/* CBC DES */
  427. +int sun4i_ss_cbc_des_encrypt(struct ablkcipher_request *areq)
  428. +{
  429. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  430. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  431. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  432. +
  433. + rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
  434. + op->keymode;
  435. + return sun4i_ss_cipher_poll(areq);
  436. +}
  437. +
  438. +int sun4i_ss_cbc_des_decrypt(struct ablkcipher_request *areq)
  439. +{
  440. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  441. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  442. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  443. +
  444. + rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
  445. + op->keymode;
  446. + return sun4i_ss_cipher_poll(areq);
  447. +}
  448. +
  449. +/* ECB DES */
  450. +int sun4i_ss_ecb_des_encrypt(struct ablkcipher_request *areq)
  451. +{
  452. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  453. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  454. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  455. +
  456. + rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
  457. + op->keymode;
  458. + return sun4i_ss_cipher_poll(areq);
  459. +}
  460. +
  461. +int sun4i_ss_ecb_des_decrypt(struct ablkcipher_request *areq)
  462. +{
  463. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  464. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  465. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  466. +
  467. + rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
  468. + op->keymode;
  469. + return sun4i_ss_cipher_poll(areq);
  470. +}
  471. +
  472. +/* CBC 3DES */
  473. +int sun4i_ss_cbc_des3_encrypt(struct ablkcipher_request *areq)
  474. +{
  475. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  476. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  477. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  478. +
  479. + rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
  480. + op->keymode;
  481. + return sun4i_ss_cipher_poll(areq);
  482. +}
  483. +
  484. +int sun4i_ss_cbc_des3_decrypt(struct ablkcipher_request *areq)
  485. +{
  486. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  487. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  488. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  489. +
  490. + rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
  491. + op->keymode;
  492. + return sun4i_ss_cipher_poll(areq);
  493. +}
  494. +
  495. +/* ECB 3DES */
  496. +int sun4i_ss_ecb_des3_encrypt(struct ablkcipher_request *areq)
  497. +{
  498. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  499. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  500. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  501. +
  502. + rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
  503. + op->keymode;
  504. + return sun4i_ss_cipher_poll(areq);
  505. +}
  506. +
  507. +int sun4i_ss_ecb_des3_decrypt(struct ablkcipher_request *areq)
  508. +{
  509. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
  510. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  511. + struct sun4i_cipher_req_ctx *rctx = ablkcipher_request_ctx(areq);
  512. +
  513. + rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
  514. + op->keymode;
  515. + return sun4i_ss_cipher_poll(areq);
  516. +}
  517. +
  518. +int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
  519. +{
  520. + struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
  521. + struct crypto_alg *alg = tfm->__crt_alg;
  522. + struct sun4i_ss_alg_template *algt;
  523. +
  524. + memset(op, 0, sizeof(struct sun4i_tfm_ctx));
  525. +
  526. + algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
  527. + op->ss = algt->ss;
  528. +
  529. + tfm->crt_ablkcipher.reqsize = sizeof(struct sun4i_cipher_req_ctx);
  530. +
  531. + return 0;
  532. +}
  533. +
  534. +/* check and set the AES key, prepare the mode to be used */
  535. +int sun4i_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  536. + unsigned int keylen)
  537. +{
  538. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  539. + struct sun4i_ss_ctx *ss = op->ss;
  540. +
  541. + switch (keylen) {
  542. + case 128 / 8:
  543. + op->keymode = SS_AES_128BITS;
  544. + break;
  545. + case 192 / 8:
  546. + op->keymode = SS_AES_192BITS;
  547. + break;
  548. + case 256 / 8:
  549. + op->keymode = SS_AES_256BITS;
  550. + break;
  551. + default:
  552. + dev_err(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
  553. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  554. + return -EINVAL;
  555. + }
  556. + op->keylen = keylen;
  557. + memcpy(op->key, key, keylen);
  558. + return 0;
  559. +}
  560. +
  561. +/* check and set the DES key, prepare the mode to be used */
  562. +int sun4i_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  563. + unsigned int keylen)
  564. +{
  565. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  566. + struct sun4i_ss_ctx *ss = op->ss;
  567. + u32 flags;
  568. + u32 tmp[DES_EXPKEY_WORDS];
  569. + int ret;
  570. +
  571. + if (unlikely(keylen != DES_KEY_SIZE)) {
  572. + dev_err(ss->dev, "Invalid keylen %u\n", keylen);
  573. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  574. + return -EINVAL;
  575. + }
  576. +
  577. + flags = crypto_ablkcipher_get_flags(tfm);
  578. +
  579. + ret = des_ekey(tmp, key);
  580. + if (unlikely(ret == 0) && (flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  581. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
  582. + dev_dbg(ss->dev, "Weak key %u\n", keylen);
  583. + return -EINVAL;
  584. + }
  585. +
  586. + op->keylen = keylen;
  587. + memcpy(op->key, key, keylen);
  588. + return 0;
  589. +}
  590. +
  591. +/* check and set the 3DES key, prepare the mode to be used */
  592. +int sun4i_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  593. + unsigned int keylen)
  594. +{
  595. + struct sun4i_tfm_ctx *op = crypto_ablkcipher_ctx(tfm);
  596. + struct sun4i_ss_ctx *ss = op->ss;
  597. +
  598. + if (unlikely(keylen != 3 * DES_KEY_SIZE)) {
  599. + dev_err(ss->dev, "Invalid keylen %u\n", keylen);
  600. + crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  601. + return -EINVAL;
  602. + }
  603. + op->keylen = keylen;
  604. + memcpy(op->key, key, keylen);
  605. + return 0;
  606. +}
  607. --- /dev/null
  608. +++ b/drivers/crypto/sunxi-ss/sun4i-ss-core.c
  609. @@ -0,0 +1,403 @@
  610. +/*
  611. + * sun4i-ss-core.c - hardware cryptographic accelerator for Allwinner A20 SoC
  612. + *
  613. + * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  614. + *
  615. + * Core file which registers crypto algorithms supported by the SS.
  616. + *
  617. + * You could find a link for the datasheet in Documentation/arm/sunxi/README
  618. + *
  619. + * This program is free software; you can redistribute it and/or modify
  620. + * it under the terms of the GNU General Public License as published by
  621. + * the Free Software Foundation; either version 2 of the License, or
  622. + * (at your option) any later version.
  623. + */
  624. +#include <linux/clk.h>
  625. +#include <linux/crypto.h>
  626. +#include <linux/io.h>
  627. +#include <linux/module.h>
  628. +#include <linux/of.h>
  629. +#include <linux/platform_device.h>
  630. +#include <crypto/scatterwalk.h>
  631. +#include <linux/scatterlist.h>
  632. +#include <linux/interrupt.h>
  633. +#include <linux/delay.h>
  634. +
  635. +#include "sun4i-ss.h"
  636. +
  637. +static struct sun4i_ss_alg_template ss_algs[] = {
  638. +{ .type = CRYPTO_ALG_TYPE_AHASH,
  639. + .mode = SS_OP_MD5,
  640. + .alg.hash = {
  641. + .init = sun4i_hash_init,
  642. + .update = sun4i_hash_update,
  643. + .final = sun4i_hash_final,
  644. + .finup = sun4i_hash_finup,
  645. + .digest = sun4i_hash_digest,
  646. + .export = sun4i_hash_export_md5,
  647. + .import = sun4i_hash_import_md5,
  648. + .halg = {
  649. + .digestsize = MD5_DIGEST_SIZE,
  650. + .base = {
  651. + .cra_name = "md5",
  652. + .cra_driver_name = "md5-sun4i-ss",
  653. + .cra_priority = 300,
  654. + .cra_alignmask = 3,
  655. + .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  656. + .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  657. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  658. + .cra_module = THIS_MODULE,
  659. + .cra_type = &crypto_ahash_type,
  660. + .cra_init = sun4i_hash_crainit
  661. + }
  662. + }
  663. + }
  664. +},
  665. +{ .type = CRYPTO_ALG_TYPE_AHASH,
  666. + .mode = SS_OP_SHA1,
  667. + .alg.hash = {
  668. + .init = sun4i_hash_init,
  669. + .update = sun4i_hash_update,
  670. + .final = sun4i_hash_final,
  671. + .finup = sun4i_hash_finup,
  672. + .digest = sun4i_hash_digest,
  673. + .export = sun4i_hash_export_sha1,
  674. + .import = sun4i_hash_import_sha1,
  675. + .halg = {
  676. + .digestsize = SHA1_DIGEST_SIZE,
  677. + .base = {
  678. + .cra_name = "sha1",
  679. + .cra_driver_name = "sha1-sun4i-ss",
  680. + .cra_priority = 300,
  681. + .cra_alignmask = 3,
  682. + .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  683. + .cra_blocksize = SHA1_BLOCK_SIZE,
  684. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  685. + .cra_module = THIS_MODULE,
  686. + .cra_type = &crypto_ahash_type,
  687. + .cra_init = sun4i_hash_crainit
  688. + }
  689. + }
  690. + }
  691. +},
  692. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  693. + .alg.crypto = {
  694. + .cra_name = "cbc(aes)",
  695. + .cra_driver_name = "cbc-aes-sun4i-ss",
  696. + .cra_priority = 300,
  697. + .cra_blocksize = AES_BLOCK_SIZE,
  698. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  699. + .cra_ctxsize = sizeof(struct sun4i_tfm_ctx),
  700. + .cra_module = THIS_MODULE,
  701. + .cra_alignmask = 3,
  702. + .cra_type = &crypto_ablkcipher_type,
  703. + .cra_init = sun4i_ss_cipher_init,
  704. + .cra_ablkcipher = {
  705. + .min_keysize = AES_MIN_KEY_SIZE,
  706. + .max_keysize = AES_MAX_KEY_SIZE,
  707. + .ivsize = AES_BLOCK_SIZE,
  708. + .setkey = sun4i_ss_aes_setkey,
  709. + .encrypt = sun4i_ss_cbc_aes_encrypt,
  710. + .decrypt = sun4i_ss_cbc_aes_decrypt,
  711. + }
  712. + }
  713. +},
  714. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  715. + .alg.crypto = {
  716. + .cra_name = "ecb(aes)",
  717. + .cra_driver_name = "ecb-aes-sun4i-ss",
  718. + .cra_priority = 300,
  719. + .cra_blocksize = AES_BLOCK_SIZE,
  720. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  721. + .cra_ctxsize = sizeof(struct sun4i_tfm_ctx),
  722. + .cra_module = THIS_MODULE,
  723. + .cra_alignmask = 3,
  724. + .cra_type = &crypto_ablkcipher_type,
  725. + .cra_init = sun4i_ss_cipher_init,
  726. + .cra_ablkcipher = {
  727. + .min_keysize = AES_MIN_KEY_SIZE,
  728. + .max_keysize = AES_MAX_KEY_SIZE,
  729. + .ivsize = AES_BLOCK_SIZE,
  730. + .setkey = sun4i_ss_aes_setkey,
  731. + .encrypt = sun4i_ss_ecb_aes_encrypt,
  732. + .decrypt = sun4i_ss_ecb_aes_decrypt,
  733. + }
  734. + }
  735. +},
  736. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  737. + .alg.crypto = {
  738. + .cra_name = "cbc(des)",
  739. + .cra_driver_name = "cbc-des-sun4i-ss",
  740. + .cra_priority = 300,
  741. + .cra_blocksize = DES_BLOCK_SIZE,
  742. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  743. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  744. + .cra_module = THIS_MODULE,
  745. + .cra_alignmask = 3,
  746. + .cra_type = &crypto_ablkcipher_type,
  747. + .cra_init = sun4i_ss_cipher_init,
  748. + .cra_u.ablkcipher = {
  749. + .min_keysize = DES_KEY_SIZE,
  750. + .max_keysize = DES_KEY_SIZE,
  751. + .ivsize = DES_BLOCK_SIZE,
  752. + .setkey = sun4i_ss_des_setkey,
  753. + .encrypt = sun4i_ss_cbc_des_encrypt,
  754. + .decrypt = sun4i_ss_cbc_des_decrypt,
  755. + }
  756. + }
  757. +},
  758. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  759. + .alg.crypto = {
  760. + .cra_name = "ecb(des)",
  761. + .cra_driver_name = "ecb-des-sun4i-ss",
  762. + .cra_priority = 300,
  763. + .cra_blocksize = DES_BLOCK_SIZE,
  764. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  765. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  766. + .cra_module = THIS_MODULE,
  767. + .cra_alignmask = 3,
  768. + .cra_type = &crypto_ablkcipher_type,
  769. + .cra_init = sun4i_ss_cipher_init,
  770. + .cra_u.ablkcipher = {
  771. + .min_keysize = DES_KEY_SIZE,
  772. + .max_keysize = DES_KEY_SIZE,
  773. + .setkey = sun4i_ss_des_setkey,
  774. + .encrypt = sun4i_ss_ecb_des_encrypt,
  775. + .decrypt = sun4i_ss_ecb_des_decrypt,
  776. + }
  777. + }
  778. +},
  779. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  780. + .alg.crypto = {
  781. + .cra_name = "cbc(des3_ede)",
  782. + .cra_driver_name = "cbc-des3-sun4i-ss",
  783. + .cra_priority = 300,
  784. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  785. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  786. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  787. + .cra_module = THIS_MODULE,
  788. + .cra_alignmask = 3,
  789. + .cra_type = &crypto_ablkcipher_type,
  790. + .cra_init = sun4i_ss_cipher_init,
  791. + .cra_u.ablkcipher = {
  792. + .min_keysize = DES3_EDE_KEY_SIZE,
  793. + .max_keysize = DES3_EDE_KEY_SIZE,
  794. + .ivsize = DES3_EDE_BLOCK_SIZE,
  795. + .setkey = sun4i_ss_des3_setkey,
  796. + .encrypt = sun4i_ss_cbc_des3_encrypt,
  797. + .decrypt = sun4i_ss_cbc_des3_decrypt,
  798. + }
  799. + }
  800. +},
  801. +{ .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  802. + .alg.crypto = {
  803. + .cra_name = "ecb(des3_ede)",
  804. + .cra_driver_name = "ecb-des3-sun4i-ss",
  805. + .cra_priority = 300,
  806. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  807. + .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER,
  808. + .cra_ctxsize = sizeof(struct sun4i_req_ctx),
  809. + .cra_module = THIS_MODULE,
  810. + .cra_alignmask = 3,
  811. + .cra_type = &crypto_ablkcipher_type,
  812. + .cra_init = sun4i_ss_cipher_init,
  813. + .cra_u.ablkcipher = {
  814. + .min_keysize = DES3_EDE_KEY_SIZE,
  815. + .max_keysize = DES3_EDE_KEY_SIZE,
  816. + .ivsize = DES3_EDE_BLOCK_SIZE,
  817. + .setkey = sun4i_ss_des3_setkey,
  818. + .encrypt = sun4i_ss_ecb_des3_encrypt,
  819. + .decrypt = sun4i_ss_ecb_des3_decrypt,
  820. + }
  821. + }
  822. +},
  823. +};
  824. +
  825. +static int sun4i_ss_probe(struct platform_device *pdev)
  826. +{
  827. + struct resource *res;
  828. + u32 v;
  829. + int err, i;
  830. + unsigned long cr;
  831. + const unsigned long cr_ahb = 24 * 1000 * 1000;
  832. + const unsigned long cr_mod = 150 * 1000 * 1000;
  833. + struct sun4i_ss_ctx *ss;
  834. +
  835. + if (!pdev->dev.of_node)
  836. + return -ENODEV;
  837. +
  838. + ss = devm_kzalloc(&pdev->dev, sizeof(*ss), GFP_KERNEL);
  839. + if (!ss)
  840. + return -ENOMEM;
  841. +
  842. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  843. + ss->base = devm_ioremap_resource(&pdev->dev, res);
  844. + if (IS_ERR(ss->base)) {
  845. + dev_err(&pdev->dev, "Cannot request MMIO\n");
  846. + return PTR_ERR(ss->base);
  847. + }
  848. +
  849. + ss->ssclk = devm_clk_get(&pdev->dev, "mod");
  850. + if (IS_ERR(ss->ssclk)) {
  851. + err = PTR_ERR(ss->ssclk);
  852. + dev_err(&pdev->dev, "Cannot get SS clock err=%d\n", err);
  853. + return err;
  854. + }
  855. + dev_dbg(&pdev->dev, "clock ss acquired\n");
  856. +
  857. + ss->busclk = devm_clk_get(&pdev->dev, "ahb");
  858. + if (IS_ERR(ss->busclk)) {
  859. + err = PTR_ERR(ss->busclk);
  860. + dev_err(&pdev->dev, "Cannot get AHB SS clock err=%d\n", err);
  861. + return err;
  862. + }
  863. + dev_dbg(&pdev->dev, "clock ahb_ss acquired\n");
  864. +
  865. + /* Enable both clocks */
  866. + err = clk_prepare_enable(ss->busclk);
  867. + if (err != 0) {
  868. + dev_err(&pdev->dev, "Cannot prepare_enable busclk\n");
  869. + return err;
  870. + }
  871. + err = clk_prepare_enable(ss->ssclk);
  872. + if (err != 0) {
  873. + dev_err(&pdev->dev, "Cannot prepare_enable ssclk\n");
  874. + goto error_ssclk;
  875. + }
  876. +
  877. + /*
  878. + * Check that clock have the correct rates given in the datasheet
  879. + * Try to set the clock to the maximum allowed
  880. + */
  881. + err = clk_set_rate(ss->ssclk, cr_mod);
  882. + if (err != 0) {
  883. + dev_err(&pdev->dev, "Cannot set clock rate to ssclk\n");
  884. + goto error_clk;
  885. + }
  886. +
  887. + /*
  888. + * The only impact on clocks below requirement are bad performance,
  889. + * so do not print "errors"
  890. + * warn on Overclocked clocks
  891. + */
  892. + cr = clk_get_rate(ss->busclk);
  893. + if (cr >= cr_ahb)
  894. + dev_dbg(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
  895. + cr, cr / 1000000, cr_ahb);
  896. + else
  897. + dev_warn(&pdev->dev, "Clock bus %lu (%lu MHz) (must be >= %lu)\n",
  898. + cr, cr / 1000000, cr_ahb);
  899. +
  900. + cr = clk_get_rate(ss->ssclk);
  901. + if (cr <= cr_mod)
  902. + if (cr < cr_mod)
  903. + dev_warn(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
  904. + cr, cr / 1000000, cr_mod);
  905. + else
  906. + dev_dbg(&pdev->dev, "Clock ss %lu (%lu MHz) (must be <= %lu)\n",
  907. + cr, cr / 1000000, cr_mod);
  908. + else
  909. + dev_warn(&pdev->dev, "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n",
  910. + cr, cr / 1000000, cr_mod);
  911. +
  912. + /*
  913. + * Datasheet named it "Die Bonding ID"
  914. + * I expect to be a sort of Security System Revision number.
  915. + * Since the A80 seems to have an other version of SS
  916. + * this info could be useful
  917. + */
  918. + writel(SS_ENABLED, ss->base + SS_CTL);
  919. + v = readl(ss->base + SS_CTL);
  920. + v >>= 16;
  921. + v &= 0x07;
  922. + dev_info(&pdev->dev, "Die ID %d\n", v);
  923. + writel(0, ss->base + SS_CTL);
  924. +
  925. + ss->dev = &pdev->dev;
  926. +
  927. + spin_lock_init(&ss->slock);
  928. +
  929. + for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
  930. + ss_algs[i].ss = ss;
  931. + switch (ss_algs[i].type) {
  932. + case CRYPTO_ALG_TYPE_ABLKCIPHER:
  933. + err = crypto_register_alg(&ss_algs[i].alg.crypto);
  934. + if (err != 0) {
  935. + dev_err(ss->dev, "Fail to register %s\n",
  936. + ss_algs[i].alg.crypto.cra_name);
  937. + goto error_alg;
  938. + }
  939. + break;
  940. + case CRYPTO_ALG_TYPE_AHASH:
  941. + err = crypto_register_ahash(&ss_algs[i].alg.hash);
  942. + if (err != 0) {
  943. + dev_err(ss->dev, "Fail to register %s\n",
  944. + ss_algs[i].alg.hash.halg.base.cra_name);
  945. + goto error_alg;
  946. + }
  947. + break;
  948. + }
  949. + }
  950. + platform_set_drvdata(pdev, ss);
  951. + return 0;
  952. +error_alg:
  953. + i--;
  954. + for (; i >= 0; i--) {
  955. + switch (ss_algs[i].type) {
  956. + case CRYPTO_ALG_TYPE_ABLKCIPHER:
  957. + crypto_unregister_alg(&ss_algs[i].alg.crypto);
  958. + break;
  959. + case CRYPTO_ALG_TYPE_AHASH:
  960. + crypto_unregister_ahash(&ss_algs[i].alg.hash);
  961. + break;
  962. + }
  963. + }
  964. +error_clk:
  965. + clk_disable_unprepare(ss->ssclk);
  966. +error_ssclk:
  967. + clk_disable_unprepare(ss->busclk);
  968. + return err;
  969. +}
  970. +
  971. +static int sun4i_ss_remove(struct platform_device *pdev)
  972. +{
  973. + int i;
  974. + struct sun4i_ss_ctx *ss = platform_get_drvdata(pdev);
  975. +
  976. + for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
  977. + switch (ss_algs[i].type) {
  978. + case CRYPTO_ALG_TYPE_ABLKCIPHER:
  979. + crypto_unregister_alg(&ss_algs[i].alg.crypto);
  980. + break;
  981. + case CRYPTO_ALG_TYPE_AHASH:
  982. + crypto_unregister_ahash(&ss_algs[i].alg.hash);
  983. + break;
  984. + }
  985. + }
  986. +
  987. + writel(0, ss->base + SS_CTL);
  988. + clk_disable_unprepare(ss->busclk);
  989. + clk_disable_unprepare(ss->ssclk);
  990. + return 0;
  991. +}
  992. +
  993. +static const struct of_device_id a20ss_crypto_of_match_table[] = {
  994. + { .compatible = "allwinner,sun4i-a10-crypto" },
  995. + {}
  996. +};
  997. +MODULE_DEVICE_TABLE(of, a20ss_crypto_of_match_table);
  998. +
  999. +static struct platform_driver sun4i_ss_driver = {
  1000. + .probe = sun4i_ss_probe,
  1001. + .remove = sun4i_ss_remove,
  1002. + .driver = {
  1003. + .name = "sun4i-ss",
  1004. + .of_match_table = a20ss_crypto_of_match_table,
  1005. + },
  1006. +};
  1007. +
  1008. +module_platform_driver(sun4i_ss_driver);
  1009. +
  1010. +MODULE_DESCRIPTION("Allwinner Security System cryptographic accelerator");
  1011. +MODULE_LICENSE("GPL");
  1012. +MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
  1013. --- /dev/null
  1014. +++ b/drivers/crypto/sunxi-ss/sun4i-ss-hash.c
  1015. @@ -0,0 +1,492 @@
  1016. +/*
  1017. + * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
  1018. + *
  1019. + * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  1020. + *
  1021. + * This file add support for MD5 and SHA1.
  1022. + *
  1023. + * You could find the datasheet in Documentation/arm/sunxi/README
  1024. + *
  1025. + * This program is free software; you can redistribute it and/or modify
  1026. + * it under the terms of the GNU General Public License as published by
  1027. + * the Free Software Foundation; either version 2 of the License, or
  1028. + * (at your option) any later version.
  1029. + */
  1030. +#include "sun4i-ss.h"
  1031. +#include <linux/scatterlist.h>
  1032. +
  1033. +/* This is a totally arbitrary value */
  1034. +#define SS_TIMEOUT 100
  1035. +
  1036. +int sun4i_hash_crainit(struct crypto_tfm *tfm)
  1037. +{
  1038. + crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1039. + sizeof(struct sun4i_req_ctx));
  1040. + return 0;
  1041. +}
  1042. +
  1043. +/* sun4i_hash_init: initialize request context */
  1044. +int sun4i_hash_init(struct ahash_request *areq)
  1045. +{
  1046. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1047. + struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1048. + struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  1049. + struct sun4i_ss_alg_template *algt;
  1050. + struct sun4i_ss_ctx *ss;
  1051. +
  1052. + memset(op, 0, sizeof(struct sun4i_req_ctx));
  1053. +
  1054. + algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
  1055. + ss = algt->ss;
  1056. + op->ss = algt->ss;
  1057. + op->mode = algt->mode;
  1058. +
  1059. + return 0;
  1060. +}
  1061. +
  1062. +int sun4i_hash_export_md5(struct ahash_request *areq, void *out)
  1063. +{
  1064. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1065. + struct md5_state *octx = out;
  1066. + int i;
  1067. +
  1068. + octx->byte_count = op->byte_count + op->len;
  1069. +
  1070. + memcpy(octx->block, op->buf, op->len);
  1071. +
  1072. + if (op->byte_count > 0) {
  1073. + for (i = 0; i < 4; i++)
  1074. + octx->hash[i] = op->hash[i];
  1075. + } else {
  1076. + octx->hash[0] = SHA1_H0;
  1077. + octx->hash[1] = SHA1_H1;
  1078. + octx->hash[2] = SHA1_H2;
  1079. + octx->hash[3] = SHA1_H3;
  1080. + }
  1081. +
  1082. + return 0;
  1083. +}
  1084. +
  1085. +int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)
  1086. +{
  1087. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1088. + const struct md5_state *ictx = in;
  1089. + int i;
  1090. +
  1091. + sun4i_hash_init(areq);
  1092. +
  1093. + op->byte_count = ictx->byte_count & ~0x3F;
  1094. + op->len = ictx->byte_count & 0x3F;
  1095. +
  1096. + memcpy(op->buf, ictx->block, op->len);
  1097. +
  1098. + for (i = 0; i < 4; i++)
  1099. + op->hash[i] = ictx->hash[i];
  1100. +
  1101. + return 0;
  1102. +}
  1103. +
  1104. +int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)
  1105. +{
  1106. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1107. + struct sha1_state *octx = out;
  1108. + int i;
  1109. +
  1110. + octx->count = op->byte_count + op->len;
  1111. +
  1112. + memcpy(octx->buffer, op->buf, op->len);
  1113. +
  1114. + if (op->byte_count > 0) {
  1115. + for (i = 0; i < 5; i++)
  1116. + octx->state[i] = op->hash[i];
  1117. + } else {
  1118. + octx->state[0] = SHA1_H0;
  1119. + octx->state[1] = SHA1_H1;
  1120. + octx->state[2] = SHA1_H2;
  1121. + octx->state[3] = SHA1_H3;
  1122. + octx->state[4] = SHA1_H4;
  1123. + }
  1124. +
  1125. + return 0;
  1126. +}
  1127. +
  1128. +int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)
  1129. +{
  1130. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1131. + const struct sha1_state *ictx = in;
  1132. + int i;
  1133. +
  1134. + sun4i_hash_init(areq);
  1135. +
  1136. + op->byte_count = ictx->count & ~0x3F;
  1137. + op->len = ictx->count & 0x3F;
  1138. +
  1139. + memcpy(op->buf, ictx->buffer, op->len);
  1140. +
  1141. + for (i = 0; i < 5; i++)
  1142. + op->hash[i] = ictx->state[i];
  1143. +
  1144. + return 0;
  1145. +}
  1146. +
  1147. +/*
  1148. + * sun4i_hash_update: update hash engine
  1149. + *
  1150. + * Could be used for both SHA1 and MD5
  1151. + * Write data by step of 32bits and put then in the SS.
  1152. + *
  1153. + * Since we cannot leave partial data and hash state in the engine,
  1154. + * we need to get the hash state at the end of this function.
  1155. + * We can get the hash state every 64 bytes
  1156. + *
  1157. + * So the first work is to get the number of bytes to write to SS modulo 64
  1158. + * The extra bytes will go to a temporary buffer op->buf storing op->len bytes
  1159. + *
  1160. + * So at the begin of update()
  1161. + * if op->len + areq->nbytes < 64
  1162. + * => all data will be written to wait buffer (op->buf) and end=0
  1163. + * if not, write all data from op->buf to the device and position end to
  1164. + * complete to 64bytes
  1165. + *
  1166. + * example 1:
  1167. + * update1 60o => op->len=60
  1168. + * update2 60o => need one more word to have 64 bytes
  1169. + * end=4
  1170. + * so write all data from op->buf and one word of SGs
  1171. + * write remaining data in op->buf
  1172. + * final state op->len=56
  1173. + */
  1174. +int sun4i_hash_update(struct ahash_request *areq)
  1175. +{
  1176. + u32 v, ivmode = 0;
  1177. + unsigned int i = 0;
  1178. + /*
  1179. + * i is the total bytes read from SGs, to be compared to areq->nbytes
  1180. + * i is important because we cannot rely on SG length since the sum of
  1181. + * SG->length could be greater than areq->nbytes
  1182. + */
  1183. +
  1184. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1185. + struct sun4i_ss_ctx *ss = op->ss;
  1186. + struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1187. + unsigned int in_i = 0; /* advancement in the current SG */
  1188. + unsigned int end;
  1189. + /*
  1190. + * end is the position when we need to stop writing to the device,
  1191. + * to be compared to i
  1192. + */
  1193. + int in_r, err = 0;
  1194. + unsigned int todo;
  1195. + u32 spaces, rx_cnt = SS_RX_DEFAULT;
  1196. + size_t copied = 0;
  1197. + struct sg_mapping_iter mi;
  1198. +
  1199. + dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
  1200. + __func__, crypto_tfm_alg_name(areq->base.tfm),
  1201. + op->byte_count, areq->nbytes, op->mode,
  1202. + op->len, op->hash[0]);
  1203. +
  1204. + if (areq->nbytes == 0)
  1205. + return 0;
  1206. +
  1207. + /* protect against overflow */
  1208. + if (areq->nbytes > UINT_MAX - op->len) {
  1209. + dev_err(ss->dev, "Cannot process too large request\n");
  1210. + return -EINVAL;
  1211. + }
  1212. +
  1213. + if (op->len + areq->nbytes < 64) {
  1214. + /* linearize data to op->buf */
  1215. + copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
  1216. + op->buf + op->len, areq->nbytes, 0);
  1217. + op->len += copied;
  1218. + return 0;
  1219. + }
  1220. +
  1221. + end = ((areq->nbytes + op->len) / 64) * 64 - op->len;
  1222. +
  1223. + if (end > areq->nbytes || areq->nbytes - end > 63) {
  1224. + dev_err(ss->dev, "ERROR: Bound error %u %u\n",
  1225. + end, areq->nbytes);
  1226. + return -EINVAL;
  1227. + }
  1228. +
  1229. + spin_lock_bh(&ss->slock);
  1230. +
  1231. + /*
  1232. + * if some data have been processed before,
  1233. + * we need to restore the partial hash state
  1234. + */
  1235. + if (op->byte_count > 0) {
  1236. + ivmode = SS_IV_ARBITRARY;
  1237. + for (i = 0; i < 5; i++)
  1238. + writel(op->hash[i], ss->base + SS_IV0 + i * 4);
  1239. + }
  1240. + /* Enable the device */
  1241. + writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
  1242. +
  1243. + i = 0;
  1244. + sg_miter_start(&mi, areq->src, sg_nents(areq->src),
  1245. + SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  1246. + sg_miter_next(&mi);
  1247. + in_i = 0;
  1248. +
  1249. + do {
  1250. + /*
  1251. + * we need to linearize in two case:
  1252. + * - the buffer is already used
  1253. + * - the SG does not have enough byte remaining ( < 4)
  1254. + */
  1255. + if (op->len > 0 || (mi.length - in_i) < 4) {
  1256. + /*
  1257. + * if we have entered here we have two reason to stop
  1258. + * - the buffer is full
  1259. + * - reach the end
  1260. + */
  1261. + while (op->len < 64 && i < end) {
  1262. + /* how many bytes we can read from current SG */
  1263. + in_r = min3(mi.length - in_i, end - i,
  1264. + 64 - op->len);
  1265. + memcpy(op->buf + op->len, mi.addr + in_i, in_r);
  1266. + op->len += in_r;
  1267. + i += in_r;
  1268. + in_i += in_r;
  1269. + if (in_i == mi.length) {
  1270. + sg_miter_next(&mi);
  1271. + in_i = 0;
  1272. + }
  1273. + }
  1274. + if (op->len > 3 && (op->len % 4) == 0) {
  1275. + /* write buf to the device */
  1276. + writesl(ss->base + SS_RXFIFO, op->buf,
  1277. + op->len / 4);
  1278. + op->byte_count += op->len;
  1279. + op->len = 0;
  1280. + }
  1281. + }
  1282. + if (mi.length - in_i > 3 && i < end) {
  1283. + /* how many bytes we can read from current SG */
  1284. + in_r = min3(mi.length - in_i, areq->nbytes - i,
  1285. + ((mi.length - in_i) / 4) * 4);
  1286. + /* how many bytes we can write in the device*/
  1287. + todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);
  1288. + writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);
  1289. + op->byte_count += todo * 4;
  1290. + i += todo * 4;
  1291. + in_i += todo * 4;
  1292. + rx_cnt -= todo;
  1293. + if (rx_cnt == 0) {
  1294. + spaces = readl(ss->base + SS_FCSR);
  1295. + rx_cnt = SS_RXFIFO_SPACES(spaces);
  1296. + }
  1297. + if (in_i == mi.length) {
  1298. + sg_miter_next(&mi);
  1299. + in_i = 0;
  1300. + }
  1301. + }
  1302. + } while (i < end);
  1303. + /* final linear */
  1304. + if ((areq->nbytes - i) < 64) {
  1305. + while (i < areq->nbytes && in_i < mi.length && op->len < 64) {
  1306. + /* how many bytes we can read from current SG */
  1307. + in_r = min3(mi.length - in_i, areq->nbytes - i,
  1308. + 64 - op->len);
  1309. + memcpy(op->buf + op->len, mi.addr + in_i, in_r);
  1310. + op->len += in_r;
  1311. + i += in_r;
  1312. + in_i += in_r;
  1313. + if (in_i == mi.length) {
  1314. + sg_miter_next(&mi);
  1315. + in_i = 0;
  1316. + }
  1317. + }
  1318. + }
  1319. +
  1320. + sg_miter_stop(&mi);
  1321. +
  1322. + writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  1323. + i = 0;
  1324. + do {
  1325. + v = readl(ss->base + SS_CTL);
  1326. + i++;
  1327. + } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  1328. + if (i >= SS_TIMEOUT) {
  1329. + dev_err_ratelimited(ss->dev,
  1330. + "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
  1331. + i, SS_TIMEOUT, v, areq->nbytes);
  1332. + err = -EIO;
  1333. + goto release_ss;
  1334. + }
  1335. +
  1336. + /* get the partial hash only if something was written */
  1337. + for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
  1338. + op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
  1339. +
  1340. +release_ss:
  1341. + writel(0, ss->base + SS_CTL);
  1342. + spin_unlock_bh(&ss->slock);
  1343. + return err;
  1344. +}
  1345. +
  1346. +/*
  1347. + * sun4i_hash_final: finalize hashing operation
  1348. + *
  1349. + * If we have some remaining bytes, we write them.
  1350. + * Then ask the SS for finalizing the hashing operation
  1351. + *
  1352. + * I do not check RX FIFO size in this function since the size is 32
  1353. + * after each enabling and this function neither write more than 32 words.
  1354. + */
  1355. +int sun4i_hash_final(struct ahash_request *areq)
  1356. +{
  1357. + u32 v, ivmode = 0;
  1358. + unsigned int i;
  1359. + unsigned int j = 0;
  1360. + int zeros, err = 0;
  1361. + unsigned int index, padlen;
  1362. + __be64 bits;
  1363. + struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  1364. + struct sun4i_ss_ctx *ss = op->ss;
  1365. + struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1366. + u32 bf[32];
  1367. + u32 wb = 0;
  1368. + unsigned int nwait, nbw = 0;
  1369. +
  1370. + dev_dbg(ss->dev, "%s: byte=%llu len=%u mode=%x wl=%u h=%x",
  1371. + __func__, op->byte_count, areq->nbytes, op->mode,
  1372. + op->len, op->hash[0]);
  1373. +
  1374. + spin_lock_bh(&ss->slock);
  1375. +
  1376. + /*
  1377. + * if we have already written something,
  1378. + * restore the partial hash state
  1379. + */
  1380. + if (op->byte_count > 0) {
  1381. + ivmode = SS_IV_ARBITRARY;
  1382. + for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
  1383. + writel(op->hash[i], ss->base + SS_IV0 + i * 4);
  1384. + }
  1385. + writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
  1386. +
  1387. + /* write the remaining words of the wait buffer */
  1388. + if (op->len > 0) {
  1389. + nwait = op->len / 4;
  1390. + if (nwait > 0) {
  1391. + writesl(ss->base + SS_RXFIFO, op->buf, nwait);
  1392. + op->byte_count += 4 * nwait;
  1393. + }
  1394. + nbw = op->len - 4 * nwait;
  1395. + wb = *(u32 *)(op->buf + nwait * 4);
  1396. + wb &= (0xFFFFFFFF >> (4 - nbw) * 8);
  1397. + }
  1398. +
  1399. + /* write the remaining bytes of the nbw buffer */
  1400. + if (nbw > 0) {
  1401. + wb |= ((1 << 7) << (nbw * 8));
  1402. + bf[j++] = wb;
  1403. + } else {
  1404. + bf[j++] = 1 << 7;
  1405. + }
  1406. +
  1407. + /*
  1408. + * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
  1409. + * I take the operations from other MD5/SHA1 implementations
  1410. + */
  1411. +
  1412. + /* we have already send 4 more byte of which nbw data */
  1413. + if (op->mode == SS_OP_MD5) {
  1414. + index = (op->byte_count + 4) & 0x3f;
  1415. + op->byte_count += nbw;
  1416. + if (index > 56)
  1417. + zeros = (120 - index) / 4;
  1418. + else
  1419. + zeros = (56 - index) / 4;
  1420. + } else {
  1421. + op->byte_count += nbw;
  1422. + index = op->byte_count & 0x3f;
  1423. + padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
  1424. + zeros = (padlen - 1) / 4;
  1425. + }
  1426. +
  1427. + memset(bf + j, 0, 4 * zeros);
  1428. + j += zeros;
  1429. +
  1430. + /* write the length of data */
  1431. + if (op->mode == SS_OP_SHA1) {
  1432. + bits = cpu_to_be64(op->byte_count << 3);
  1433. + bf[j++] = bits & 0xffffffff;
  1434. + bf[j++] = (bits >> 32) & 0xffffffff;
  1435. + } else {
  1436. + bf[j++] = (op->byte_count << 3) & 0xffffffff;
  1437. + bf[j++] = (op->byte_count >> 29) & 0xffffffff;
  1438. + }
  1439. + writesl(ss->base + SS_RXFIFO, bf, j);
  1440. +
  1441. + /* Tell the SS to stop the hashing */
  1442. + writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  1443. +
  1444. + /*
  1445. + * Wait for SS to finish the hash.
  1446. + * The timeout could happen only in case of bad overcloking
  1447. + * or driver bug.
  1448. + */
  1449. + i = 0;
  1450. + do {
  1451. + v = readl(ss->base + SS_CTL);
  1452. + i++;
  1453. + } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  1454. + if (i >= SS_TIMEOUT) {
  1455. + dev_err_ratelimited(ss->dev,
  1456. + "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
  1457. + i, SS_TIMEOUT, v, areq->nbytes);
  1458. + err = -EIO;
  1459. + goto release_ss;
  1460. + }
  1461. +
  1462. + /* Get the hash from the device */
  1463. + if (op->mode == SS_OP_SHA1) {
  1464. + for (i = 0; i < 5; i++) {
  1465. + v = cpu_to_be32(readl(ss->base + SS_MD0 + i * 4));
  1466. + memcpy(areq->result + i * 4, &v, 4);
  1467. + }
  1468. + } else {
  1469. + for (i = 0; i < 4; i++) {
  1470. + v = readl(ss->base + SS_MD0 + i * 4);
  1471. + memcpy(areq->result + i * 4, &v, 4);
  1472. + }
  1473. + }
  1474. +
  1475. +release_ss:
  1476. + writel(0, ss->base + SS_CTL);
  1477. + spin_unlock_bh(&ss->slock);
  1478. + return err;
  1479. +}
  1480. +
  1481. +/* sun4i_hash_finup: finalize hashing operation after an update */
  1482. +int sun4i_hash_finup(struct ahash_request *areq)
  1483. +{
  1484. + int err;
  1485. +
  1486. + err = sun4i_hash_update(areq);
  1487. + if (err != 0)
  1488. + return err;
  1489. +
  1490. + return sun4i_hash_final(areq);
  1491. +}
  1492. +
  1493. +/* combo of init/update/final functions */
  1494. +int sun4i_hash_digest(struct ahash_request *areq)
  1495. +{
  1496. + int err;
  1497. +
  1498. + err = sun4i_hash_init(areq);
  1499. + if (err != 0)
  1500. + return err;
  1501. +
  1502. + err = sun4i_hash_update(areq);
  1503. + if (err != 0)
  1504. + return err;
  1505. +
  1506. + return sun4i_hash_final(areq);
  1507. +}
  1508. --- /dev/null
  1509. +++ b/drivers/crypto/sunxi-ss/sun4i-ss.h
  1510. @@ -0,0 +1,199 @@
  1511. +/*
  1512. + * sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC
  1513. + *
  1514. + * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  1515. + *
  1516. + * Support AES cipher with 128,192,256 bits keysize.
  1517. + * Support MD5 and SHA1 hash algorithms.
  1518. + * Support DES and 3DES
  1519. + *
  1520. + * You could find the datasheet in Documentation/arm/sunxi/README
  1521. + *
  1522. + * Licensed under the GPL-2.
  1523. + */
  1524. +
  1525. +#include <linux/clk.h>
  1526. +#include <linux/crypto.h>
  1527. +#include <linux/io.h>
  1528. +#include <linux/module.h>
  1529. +#include <linux/of.h>
  1530. +#include <linux/platform_device.h>
  1531. +#include <crypto/scatterwalk.h>
  1532. +#include <linux/scatterlist.h>
  1533. +#include <linux/interrupt.h>
  1534. +#include <linux/delay.h>
  1535. +#include <crypto/md5.h>
  1536. +#include <crypto/sha.h>
  1537. +#include <crypto/hash.h>
  1538. +#include <crypto/internal/hash.h>
  1539. +#include <crypto/aes.h>
  1540. +#include <crypto/des.h>
  1541. +#include <crypto/internal/rng.h>
  1542. +
  1543. +#define SS_CTL 0x00
  1544. +#define SS_KEY0 0x04
  1545. +#define SS_KEY1 0x08
  1546. +#define SS_KEY2 0x0C
  1547. +#define SS_KEY3 0x10
  1548. +#define SS_KEY4 0x14
  1549. +#define SS_KEY5 0x18
  1550. +#define SS_KEY6 0x1C
  1551. +#define SS_KEY7 0x20
  1552. +
  1553. +#define SS_IV0 0x24
  1554. +#define SS_IV1 0x28
  1555. +#define SS_IV2 0x2C
  1556. +#define SS_IV3 0x30
  1557. +
  1558. +#define SS_FCSR 0x44
  1559. +
  1560. +#define SS_MD0 0x4C
  1561. +#define SS_MD1 0x50
  1562. +#define SS_MD2 0x54
  1563. +#define SS_MD3 0x58
  1564. +#define SS_MD4 0x5C
  1565. +
  1566. +#define SS_RXFIFO 0x200
  1567. +#define SS_TXFIFO 0x204
  1568. +
  1569. +/* SS_CTL configuration values */
  1570. +
  1571. +/* PRNG generator mode - bit 15 */
  1572. +#define SS_PRNG_ONESHOT (0 << 15)
  1573. +#define SS_PRNG_CONTINUE (1 << 15)
  1574. +
  1575. +/* IV mode for hash */
  1576. +#define SS_IV_ARBITRARY (1 << 14)
  1577. +
  1578. +/* SS operation mode - bits 12-13 */
  1579. +#define SS_ECB (0 << 12)
  1580. +#define SS_CBC (1 << 12)
  1581. +#define SS_CTS (3 << 12)
  1582. +
  1583. +/* Counter width for CNT mode - bits 10-11 */
  1584. +#define SS_CNT_16BITS (0 << 10)
  1585. +#define SS_CNT_32BITS (1 << 10)
  1586. +#define SS_CNT_64BITS (2 << 10)
  1587. +
  1588. +/* Key size for AES - bits 8-9 */
  1589. +#define SS_AES_128BITS (0 << 8)
  1590. +#define SS_AES_192BITS (1 << 8)
  1591. +#define SS_AES_256BITS (2 << 8)
  1592. +
  1593. +/* Operation direction - bit 7 */
  1594. +#define SS_ENCRYPTION (0 << 7)
  1595. +#define SS_DECRYPTION (1 << 7)
  1596. +
  1597. +/* SS Method - bits 4-6 */
  1598. +#define SS_OP_AES (0 << 4)
  1599. +#define SS_OP_DES (1 << 4)
  1600. +#define SS_OP_3DES (2 << 4)
  1601. +#define SS_OP_SHA1 (3 << 4)
  1602. +#define SS_OP_MD5 (4 << 4)
  1603. +#define SS_OP_PRNG (5 << 4)
  1604. +
  1605. +/* Data end bit - bit 2 */
  1606. +#define SS_DATA_END (1 << 2)
  1607. +
  1608. +/* PRNG start bit - bit 1 */
  1609. +#define SS_PRNG_START (1 << 1)
  1610. +
  1611. +/* SS Enable bit - bit 0 */
  1612. +#define SS_DISABLED (0 << 0)
  1613. +#define SS_ENABLED (1 << 0)
  1614. +
  1615. +/* SS_FCSR configuration values */
  1616. +/* RX FIFO status - bit 30 */
  1617. +#define SS_RXFIFO_FREE (1 << 30)
  1618. +
  1619. +/* RX FIFO empty spaces - bits 24-29 */
  1620. +#define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)
  1621. +
  1622. +/* TX FIFO status - bit 22 */
  1623. +#define SS_TXFIFO_AVAILABLE (1 << 22)
  1624. +
  1625. +/* TX FIFO available spaces - bits 16-21 */
  1626. +#define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)
  1627. +
  1628. +#define SS_RX_MAX 32
  1629. +#define SS_RX_DEFAULT SS_RX_MAX
  1630. +#define SS_TX_MAX 33
  1631. +
  1632. +#define SS_RXFIFO_EMP_INT_PENDING (1 << 10)
  1633. +#define SS_TXFIFO_AVA_INT_PENDING (1 << 8)
  1634. +#define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
  1635. +#define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
  1636. +
  1637. +struct sun4i_ss_ctx {
  1638. + void __iomem *base;
  1639. + int irq;
  1640. + struct clk *busclk;
  1641. + struct clk *ssclk;
  1642. + struct device *dev;
  1643. + struct resource *res;
  1644. + spinlock_t slock; /* control the use of the device */
  1645. +};
  1646. +
  1647. +struct sun4i_ss_alg_template {
  1648. + u32 type;
  1649. + u32 mode;
  1650. + union {
  1651. + struct crypto_alg crypto;
  1652. + struct ahash_alg hash;
  1653. + } alg;
  1654. + struct sun4i_ss_ctx *ss;
  1655. +};
  1656. +
  1657. +struct sun4i_tfm_ctx {
  1658. + u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */
  1659. + u32 keylen;
  1660. + u32 keymode;
  1661. + struct sun4i_ss_ctx *ss;
  1662. +};
  1663. +
  1664. +struct sun4i_cipher_req_ctx {
  1665. + u32 mode;
  1666. +};
  1667. +
  1668. +struct sun4i_req_ctx {
  1669. + u32 mode;
  1670. + u64 byte_count; /* number of bytes "uploaded" to the device */
  1671. + u32 hash[5]; /* for storing SS_IVx register */
  1672. + char buf[64];
  1673. + unsigned int len;
  1674. + struct sun4i_ss_ctx *ss;
  1675. +};
  1676. +
  1677. +int sun4i_hash_crainit(struct crypto_tfm *tfm);
  1678. +int sun4i_hash_init(struct ahash_request *areq);
  1679. +int sun4i_hash_update(struct ahash_request *areq);
  1680. +int sun4i_hash_final(struct ahash_request *areq);
  1681. +int sun4i_hash_finup(struct ahash_request *areq);
  1682. +int sun4i_hash_digest(struct ahash_request *areq);
  1683. +int sun4i_hash_export_md5(struct ahash_request *areq, void *out);
  1684. +int sun4i_hash_import_md5(struct ahash_request *areq, const void *in);
  1685. +int sun4i_hash_export_sha1(struct ahash_request *areq, void *out);
  1686. +int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in);
  1687. +
  1688. +int sun4i_ss_cbc_aes_encrypt(struct ablkcipher_request *areq);
  1689. +int sun4i_ss_cbc_aes_decrypt(struct ablkcipher_request *areq);
  1690. +int sun4i_ss_ecb_aes_encrypt(struct ablkcipher_request *areq);
  1691. +int sun4i_ss_ecb_aes_decrypt(struct ablkcipher_request *areq);
  1692. +
  1693. +int sun4i_ss_cbc_des_encrypt(struct ablkcipher_request *areq);
  1694. +int sun4i_ss_cbc_des_decrypt(struct ablkcipher_request *areq);
  1695. +int sun4i_ss_ecb_des_encrypt(struct ablkcipher_request *areq);
  1696. +int sun4i_ss_ecb_des_decrypt(struct ablkcipher_request *areq);
  1697. +
  1698. +int sun4i_ss_cbc_des3_encrypt(struct ablkcipher_request *areq);
  1699. +int sun4i_ss_cbc_des3_decrypt(struct ablkcipher_request *areq);
  1700. +int sun4i_ss_ecb_des3_encrypt(struct ablkcipher_request *areq);
  1701. +int sun4i_ss_ecb_des3_decrypt(struct ablkcipher_request *areq);
  1702. +
  1703. +int sun4i_ss_cipher_init(struct crypto_tfm *tfm);
  1704. +int sun4i_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1705. + unsigned int keylen);
  1706. +int sun4i_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1707. + unsigned int keylen);
  1708. +int sun4i_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1709. + unsigned int keylen);