blob: c6210373b1be43df131cd85d9eee1ca1c6d8504d [file] [log] [blame]
Kim Phillips8e8ec592011-03-13 16:54:26 +08001/*
2 * CAAM control-plane driver backend
3 * Controller-level driver, kernel property detection, initialization
4 *
Kim Phillips281922a2012-06-22 19:48:52 -05005 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kim Phillips8e8ec592011-03-13 16:54:26 +08006 */
7
Himangi Saraogi4776d382014-05-27 23:55:48 +05308#include <linux/device.h>
Rob Herring5af50732013-09-17 14:28:33 -05009#include <linux/of_address.h>
10#include <linux/of_irq.h>
11
Kim Phillips8e8ec592011-03-13 16:54:26 +080012#include "compat.h"
13#include "regs.h"
14#include "intern.h"
15#include "jr.h"
Kim Phillips281922a2012-06-22 19:48:52 -050016#include "desc_constr.h"
17#include "error.h"
Kim Phillips8e8ec592011-03-13 16:54:26 +080018
Kim Phillips281922a2012-06-22 19:48:52 -050019/*
20 * Descriptor to instantiate RNG State Handle 0 in normal mode and
21 * load the JDKEK, TDKEK and TDSK registers
22 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030023static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
Kim Phillips281922a2012-06-22 19:48:52 -050024{
Alex Porosanu1005bcc2013-09-09 18:56:34 +030025 u32 *jump_cmd, op_flags;
Kim Phillips281922a2012-06-22 19:48:52 -050026
27 init_job_desc(desc, 0);
28
Alex Porosanu1005bcc2013-09-09 18:56:34 +030029 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
30 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
31
Kim Phillips281922a2012-06-22 19:48:52 -050032 /* INIT RNG in non-test mode */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030033 append_operation(desc, op_flags);
Kim Phillips281922a2012-06-22 19:48:52 -050034
Alex Porosanu1005bcc2013-09-09 18:56:34 +030035 if (!handle && do_sk) {
36 /*
37 * For SH0, Secure Keys must be generated as well
38 */
Kim Phillips281922a2012-06-22 19:48:52 -050039
Alex Porosanu1005bcc2013-09-09 18:56:34 +030040 /* wait for done */
41 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
42 set_jump_tgt_here(desc, jump_cmd);
Kim Phillips281922a2012-06-22 19:48:52 -050043
Alex Porosanu1005bcc2013-09-09 18:56:34 +030044 /*
45 * load 1 to clear written reg:
46 * resets the done interrrupt and returns the RNG to idle.
47 */
48 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
49
50 /* Initialize State Handle */
51 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
52 OP_ALG_AAI_RNG4_SK);
53 }
Alex Porosanud5e4e992013-09-09 18:56:28 +030054
55 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
Kim Phillips281922a2012-06-22 19:48:52 -050056}
57
Alex Porosanub1f996e02013-09-09 18:56:32 +030058/* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030059static void build_deinstantiation_desc(u32 *desc, int handle)
Alex Porosanub1f996e02013-09-09 18:56:32 +030060{
61 init_job_desc(desc, 0);
62
63 /* Uninstantiate State Handle 0 */
64 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
Alex Porosanu1005bcc2013-09-09 18:56:34 +030065 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
Alex Porosanub1f996e02013-09-09 18:56:32 +030066
67 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
68}
Alex Porosanu04cddbf2013-09-09 18:56:31 +030069
70/*
71 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
72 * the software (no JR/QI used).
73 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +030074 * @status - descriptor status, after being run
75 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +030076 * Return: - 0 if no error occurred
77 * - -ENODEV if the DECO couldn't be acquired
78 * - -EAGAIN if an error occurred while executing the descriptor
79 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030080static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
81 u32 *status)
Kim Phillips281922a2012-06-22 19:48:52 -050082{
Ruchika Gupta997ad292013-07-04 11:26:03 +053083 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
84 struct caam_full __iomem *topregs;
85 unsigned int timeout = 100000;
Alex Porosanu04cddbf2013-09-09 18:56:31 +030086 u32 deco_dbg_reg, flags;
Alex Porosanub1f996e02013-09-09 18:56:32 +030087 int i;
Ruchika Gupta997ad292013-07-04 11:26:03 +053088
89 /* Set the bit to request direct access to DECO0 */
90 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
Ruchika Gupta17157c92014-06-23 17:42:33 +053091
Horia Geanta8f1da7b2014-07-21 16:03:21 +030092 if (ctrlpriv->virt_en == 1) {
Ruchika Gupta17157c92014-06-23 17:42:33 +053093 setbits32(&topregs->ctrl.deco_rsr, DECORSR_JR0);
94
Horia Geanta8f1da7b2014-07-21 16:03:21 +030095 while (!(rd_reg32(&topregs->ctrl.deco_rsr) & DECORSR_VALID) &&
96 --timeout)
97 cpu_relax();
98
99 timeout = 100000;
100 }
Ruchika Gupta17157c92014-06-23 17:42:33 +0530101
Ruchika Gupta997ad292013-07-04 11:26:03 +0530102 setbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
103
104 while (!(rd_reg32(&topregs->ctrl.deco_rq) & DECORR_DEN0) &&
105 --timeout)
106 cpu_relax();
107
108 if (!timeout) {
109 dev_err(ctrldev, "failed to acquire DECO 0\n");
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300110 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
111 return -ENODEV;
Kim Phillips281922a2012-06-22 19:48:52 -0500112 }
113
Ruchika Gupta997ad292013-07-04 11:26:03 +0530114 for (i = 0; i < desc_len(desc); i++)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300115 wr_reg32(&topregs->deco.descbuf[i], *(desc + i));
Kim Phillips281922a2012-06-22 19:48:52 -0500116
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300117 flags = DECO_JQCR_WHL;
118 /*
119 * If the descriptor length is longer than 4 words, then the
120 * FOUR bit in JRCTRL register must be set.
121 */
122 if (desc_len(desc) >= 4)
123 flags |= DECO_JQCR_FOUR;
124
125 /* Instruct the DECO to execute it */
126 wr_reg32(&topregs->deco.jr_ctl_hi, flags);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530127
128 timeout = 10000000;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300129 do {
130 deco_dbg_reg = rd_reg32(&topregs->deco.desc_dbg);
131 /*
132 * If an error occured in the descriptor, then
133 * the DECO status field will be set to 0x0D
134 */
135 if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
136 DESC_DBG_DECO_STAT_HOST_ERR)
137 break;
Ruchika Gupta997ad292013-07-04 11:26:03 +0530138 cpu_relax();
Alex Porosanu84cf4822013-09-09 18:56:30 +0300139 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530140
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300141 *status = rd_reg32(&topregs->deco.op_status_hi) &
142 DECO_OP_STATUS_HI_ERR_MASK;
143
Ruchika Gupta17157c92014-06-23 17:42:33 +0530144 if (ctrlpriv->virt_en == 1)
145 clrbits32(&topregs->ctrl.deco_rsr, DECORSR_JR0);
146
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300147 /* Mark the DECO as free */
Ruchika Gupta997ad292013-07-04 11:26:03 +0530148 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300149
150 if (!timeout)
151 return -EAGAIN;
152
153 return 0;
154}
155
156/*
157 * instantiate_rng - builds and executes a descriptor on DECO0,
158 * which initializes the RNG block.
159 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300160 * @state_handle_mask - bitmask containing the instantiation status
161 * for the RNG4 state handles which exist in
162 * the RNG4 block: 1 if it's been instantiated
163 * by an external entry, 0 otherwise.
164 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
165 * Caution: this can be done only once; if the keys need to be
166 * regenerated, a POR is required
167 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300168 * Return: - 0 if no error occurred
169 * - -ENOMEM if there isn't enough memory to allocate the descriptor
170 * - -ENODEV if DECO0 couldn't be acquired
171 * - -EAGAIN if an error occurred when executing the descriptor
172 * f.i. there was a RNG hardware error due to not "good enough"
173 * entropy being aquired.
174 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300175static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
176 int gen_sk)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300177{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300178 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
179 struct caam_full __iomem *topregs;
180 struct rng4tst __iomem *r4tst;
181 u32 *desc, status, rdsta_val;
182 int ret = 0, sh_idx;
183
184 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
185 r4tst = &topregs->ctrl.r4tst[0];
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300186
187 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
188 if (!desc)
189 return -ENOMEM;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300190
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300191 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
192 /*
193 * If the corresponding bit is set, this state handle
194 * was initialized by somebody else, so it's left alone.
195 */
196 if ((1 << sh_idx) & state_handle_mask)
197 continue;
198
199 /* Create the descriptor for instantiating RNG State Handle */
200 build_instantiation_desc(desc, sh_idx, gen_sk);
201
202 /* Try to run it through DECO0 */
203 ret = run_descriptor_deco0(ctrldev, desc, &status);
204
205 /*
206 * If ret is not 0, or descriptor status is not 0, then
207 * something went wrong. No need to try the next state
208 * handle (if available), bail out here.
209 * Also, if for some reason, the State Handle didn't get
210 * instantiated although the descriptor has finished
211 * without any error (HW optimizations for later
212 * CAAM eras), then try again.
213 */
214 rdsta_val =
215 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) & RDSTA_IFMASK;
216 if (status || !(rdsta_val & (1 << sh_idx)))
217 ret = -EAGAIN;
218 if (ret)
219 break;
220
221 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
222 /* Clear the contents before recreating the descriptor */
223 memset(desc, 0x00, CAAM_CMD_SZ * 7);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530224 }
225
Kim Phillips281922a2012-06-22 19:48:52 -0500226 kfree(desc);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300227
Kim Phillips281922a2012-06-22 19:48:52 -0500228 return ret;
229}
230
231/*
Alex Porosanub1f996e02013-09-09 18:56:32 +0300232 * deinstantiate_rng - builds and executes a descriptor on DECO0,
233 * which deinitializes the RNG block.
234 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300235 * @state_handle_mask - bitmask containing the instantiation status
236 * for the RNG4 state handles which exist in
237 * the RNG4 block: 1 if it's been instantiated
Alex Porosanub1f996e02013-09-09 18:56:32 +0300238 *
239 * Return: - 0 if no error occurred
240 * - -ENOMEM if there isn't enough memory to allocate the descriptor
241 * - -ENODEV if DECO0 couldn't be acquired
242 * - -EAGAIN if an error occurred when executing the descriptor
Kim Phillips281922a2012-06-22 19:48:52 -0500243 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300244static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
Alex Porosanub1f996e02013-09-09 18:56:32 +0300245{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300246 u32 *desc, status;
247 int sh_idx, ret = 0;
Alex Porosanub1f996e02013-09-09 18:56:32 +0300248
249 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
250 if (!desc)
251 return -ENOMEM;
252
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300253 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
254 /*
255 * If the corresponding bit is set, then it means the state
256 * handle was initialized by us, and thus it needs to be
257 * deintialized as well
258 */
259 if ((1 << sh_idx) & state_handle_mask) {
260 /*
261 * Create the descriptor for deinstantating this state
262 * handle
263 */
264 build_deinstantiation_desc(desc, sh_idx);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300265
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300266 /* Try to run it through DECO0 */
267 ret = run_descriptor_deco0(ctrldev, desc, &status);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300268
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300269 if (ret || status) {
270 dev_err(ctrldev,
271 "Failed to deinstantiate RNG4 SH%d\n",
272 sh_idx);
273 break;
274 }
275 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
276 }
277 }
Alex Porosanub1f996e02013-09-09 18:56:32 +0300278
279 kfree(desc);
280
281 return ret;
282}
283
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300284static int caam_remove(struct platform_device *pdev)
285{
286 struct device *ctrldev;
287 struct caam_drv_private *ctrlpriv;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300288 struct caam_full __iomem *topregs;
289 int ring, ret = 0;
290
291 ctrldev = &pdev->dev;
292 ctrlpriv = dev_get_drvdata(ctrldev);
293 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
294
Ruchika Gupta313ea292013-10-25 12:01:01 +0530295 /* Remove platform devices for JobRs */
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300296 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530297 if (ctrlpriv->jrpdev[ring])
298 of_device_unregister(ctrlpriv->jrpdev[ring]);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300299 }
300
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300301 /* De-initialize RNG state handles initialized by this driver. */
302 if (ctrlpriv->rng4_sh_init)
303 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300304
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300305 /* Shut down debug views */
306#ifdef CONFIG_DEBUG_FS
307 debugfs_remove_recursive(ctrlpriv->dfs_root);
308#endif
309
310 /* Unmap controller region */
311 iounmap(&topregs->ctrl);
312
Kim Phillips281922a2012-06-22 19:48:52 -0500313 return ret;
314}
315
316/*
Alex Porosanu84cf4822013-09-09 18:56:30 +0300317 * kick_trng - sets the various parameters for enabling the initialization
318 * of the RNG4 block in CAAM
319 * @pdev - pointer to the platform device
320 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
Kim Phillips281922a2012-06-22 19:48:52 -0500321 */
Alex Porosanu84cf4822013-09-09 18:56:30 +0300322static void kick_trng(struct platform_device *pdev, int ent_delay)
Kim Phillips281922a2012-06-22 19:48:52 -0500323{
324 struct device *ctrldev = &pdev->dev;
325 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
326 struct caam_full __iomem *topregs;
327 struct rng4tst __iomem *r4tst;
328 u32 val;
329
330 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
331 r4tst = &topregs->ctrl.r4tst[0];
332
333 /* put RNG4 into program mode */
334 setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300335
336 /*
337 * Performance-wise, it does not make sense to
338 * set the delay to a value that is lower
339 * than the last one that worked (i.e. the state handles
340 * were instantiated properly. Thus, instead of wasting
341 * time trying to set the values controlling the sample
342 * frequency, the function simply returns.
343 */
344 val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
345 >> RTSDCTL_ENT_DLY_SHIFT;
346 if (ent_delay <= val) {
347 /* put RNG4 into run mode */
348 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
349 return;
350 }
351
Kim Phillips281922a2012-06-22 19:48:52 -0500352 val = rd_reg32(&r4tst->rtsdctl);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300353 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
354 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
Kim Phillips281922a2012-06-22 19:48:52 -0500355 wr_reg32(&r4tst->rtsdctl, val);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300356 /* min. freq. count, equal to 1/4 of the entropy sample length */
357 wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
Alex Porosanub061f3f2014-08-11 11:40:15 +0300358 /* disable maximum frequency count */
359 wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
Alex Porosanue5ffbfc2014-08-11 11:40:17 +0300360 /* read the control register */
361 val = rd_reg32(&r4tst->rtmctl);
362 /*
363 * select raw sampling in both entropy shifter
364 * and statistical checker
365 */
366 setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC);
Kim Phillips281922a2012-06-22 19:48:52 -0500367 /* put RNG4 into run mode */
Alex Porosanue5ffbfc2014-08-11 11:40:17 +0300368 clrbits32(&val, RTMCTL_PRGM);
369 /* write back the control register */
370 wr_reg32(&r4tst->rtmctl, val);
Kim Phillips281922a2012-06-22 19:48:52 -0500371}
372
Alex Porosanu82c2f962012-07-11 11:06:11 +0800373/**
374 * caam_get_era() - Return the ERA of the SEC on SoC, based
Alex Porosanu883619a2014-02-06 10:27:19 +0200375 * on "sec-era" propery in the DTS. This property is updated by u-boot.
Alex Porosanu82c2f962012-07-11 11:06:11 +0800376 **/
Alex Porosanu883619a2014-02-06 10:27:19 +0200377int caam_get_era(void)
Alex Porosanu82c2f962012-07-11 11:06:11 +0800378{
Alex Porosanu883619a2014-02-06 10:27:19 +0200379 struct device_node *caam_node;
380 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
381 const uint32_t *prop = (uint32_t *)of_get_property(caam_node,
382 "fsl,sec-era",
383 NULL);
384 return prop ? *prop : -ENOTSUPP;
385 }
Alex Porosanu82c2f962012-07-11 11:06:11 +0800386
387 return -ENOTSUPP;
388}
389EXPORT_SYMBOL(caam_get_era);
390
Kim Phillips8e8ec592011-03-13 16:54:26 +0800391/* Probe routine for CAAM top (controller) level */
Kim Phillips2930d492011-05-14 22:07:55 -0500392static int caam_probe(struct platform_device *pdev)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800393{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300394 int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
Alex Porosanu82c2f962012-07-11 11:06:11 +0800395 u64 caam_id;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800396 struct device *dev;
397 struct device_node *nprop, *np;
398 struct caam_ctrl __iomem *ctrl;
399 struct caam_full __iomem *topregs;
400 struct caam_drv_private *ctrlpriv;
Kim Phillips23457bc2011-06-05 16:42:54 -0500401#ifdef CONFIG_DEBUG_FS
402 struct caam_perfmon *perfmon;
403#endif
Ruchika Gupta17157c92014-06-23 17:42:33 +0530404 u32 scfgr, comp_params;
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530405 u32 cha_vid_ls;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800406
Himangi Saraogi4776d382014-05-27 23:55:48 +0530407 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(struct caam_drv_private),
408 GFP_KERNEL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800409 if (!ctrlpriv)
410 return -ENOMEM;
411
412 dev = &pdev->dev;
413 dev_set_drvdata(dev, ctrlpriv);
414 ctrlpriv->pdev = pdev;
415 nprop = pdev->dev.of_node;
416
417 /* Get configuration properties from device tree */
418 /* First, get register page */
419 ctrl = of_iomap(nprop, 0);
420 if (ctrl == NULL) {
421 dev_err(dev, "caam: of_iomap() failed\n");
422 return -ENOMEM;
423 }
424 ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
425
426 /* topregs used to derive pointers to CAAM sub-blocks only */
427 topregs = (struct caam_full __iomem *)ctrl;
428
429 /* Get the IRQ of the controller (for security violations only) */
Thierry Redingf7578492013-09-18 15:24:44 +0200430 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800431
432 /*
433 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
Kim Phillipse13af182012-06-22 19:48:51 -0500434 * long pointers in master configuration register
Kim Phillips8e8ec592011-03-13 16:54:26 +0800435 */
436 setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
437 (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
438
Ruchika Gupta17157c92014-06-23 17:42:33 +0530439 /*
440 * Read the Compile Time paramters and SCFGR to determine
441 * if Virtualization is enabled for this platform
442 */
443 comp_params = rd_reg32(&topregs->ctrl.perfmon.comp_parms_ms);
444 scfgr = rd_reg32(&topregs->ctrl.scfgr);
445
446 ctrlpriv->virt_en = 0;
447 if (comp_params & CTPR_MS_VIRT_EN_INCL) {
448 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
449 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
450 */
451 if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
452 (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
453 (scfgr & SCFGR_VIRT_EN)))
454 ctrlpriv->virt_en = 1;
455 } else {
456 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
457 if (comp_params & CTPR_MS_VIRT_EN_POR)
458 ctrlpriv->virt_en = 1;
459 }
460
461 if (ctrlpriv->virt_en == 1)
462 setbits32(&topregs->ctrl.jrstart, JRSTART_JR0_START |
463 JRSTART_JR1_START | JRSTART_JR2_START |
464 JRSTART_JR3_START);
465
Kim Phillips8e8ec592011-03-13 16:54:26 +0800466 if (sizeof(dma_addr_t) == sizeof(u64))
Kim Phillipse13af182012-06-22 19:48:51 -0500467 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
Horia Geantaa2ac2872014-07-11 15:34:47 +0300468 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
Kim Phillipse13af182012-06-22 19:48:51 -0500469 else
Horia Geantaa2ac2872014-07-11 15:34:47 +0300470 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
Kim Phillipse13af182012-06-22 19:48:51 -0500471 else
Horia Geantaa2ac2872014-07-11 15:34:47 +0300472 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800473
Kim Phillips8e8ec592011-03-13 16:54:26 +0800474 /*
475 * Detect and enable JobRs
476 * First, find out how many ring spec'ed, allocate references
477 * for all, then go probe each one.
478 */
479 rspec = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800480 for_each_available_child_of_node(nprop, np)
481 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
482 of_device_is_compatible(np, "fsl,sec4.0-job-ring"))
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800483 rspec++;
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800484
Himangi Saraogi4776d382014-05-27 23:55:48 +0530485 ctrlpriv->jrpdev = devm_kzalloc(&pdev->dev,
486 sizeof(struct platform_device *) * rspec,
487 GFP_KERNEL);
Ruchika Gupta313ea292013-10-25 12:01:01 +0530488 if (ctrlpriv->jrpdev == NULL) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800489 iounmap(&topregs->ctrl);
490 return -ENOMEM;
491 }
492
493 ring = 0;
494 ctrlpriv->total_jobrs = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800495 for_each_available_child_of_node(nprop, np)
496 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
497 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530498 ctrlpriv->jrpdev[ring] =
499 of_platform_device_create(np, NULL, dev);
500 if (!ctrlpriv->jrpdev[ring]) {
501 pr_warn("JR%d Platform device creation error\n",
502 ring);
503 continue;
504 }
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800505 ctrlpriv->total_jobrs++;
506 ring++;
507 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800508
509 /* Check to see if QI present. If so, enable */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530510 ctrlpriv->qi_present =
511 !!(rd_reg32(&topregs->ctrl.perfmon.comp_parms_ms) &
512 CTPR_MS_QI_MASK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800513 if (ctrlpriv->qi_present) {
514 ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
515 /* This is all that's required to physically enable QI */
516 wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
517 }
518
519 /* If no QI and no rings specified, quit and go home */
520 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
521 dev_err(dev, "no queues configured, terminating\n");
522 caam_remove(pdev);
523 return -ENOMEM;
524 }
525
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530526 cha_vid_ls = rd_reg32(&topregs->ctrl.perfmon.cha_id_ls);
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530527
Kim Phillips281922a2012-06-22 19:48:52 -0500528 /*
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530529 * If SEC has RNG version >= 4 and RNG state handle has not been
Alex Porosanu84cf4822013-09-09 18:56:30 +0300530 * already instantiated, do RNG instantiation
Kim Phillips281922a2012-06-22 19:48:52 -0500531 */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530532 if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300533 ctrlpriv->rng4_sh_init =
534 rd_reg32(&topregs->ctrl.r4tst[0].rdsta);
535 /*
536 * If the secure keys (TDKEK, JDKEK, TDSK), were already
537 * generated, signal this to the function that is instantiating
538 * the state handles. An error would occur if RNG4 attempts
539 * to regenerate these keys before the next POR.
540 */
541 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
542 ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300543 do {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300544 int inst_handles =
545 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) &
546 RDSTA_IFMASK;
547 /*
548 * If either SH were instantiated by somebody else
549 * (e.g. u-boot) then it is assumed that the entropy
550 * parameters are properly set and thus the function
551 * setting these (kick_trng(...)) is skipped.
552 * Also, if a handle was instantiated, do not change
553 * the TRNG parameters.
554 */
555 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
Alex Porosanueeaa1722014-08-11 11:40:16 +0300556 dev_info(dev,
557 "Entropy delay = %u\n",
558 ent_delay);
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300559 kick_trng(pdev, ent_delay);
560 ent_delay += 400;
561 }
562 /*
563 * if instantiate_rng(...) fails, the loop will rerun
564 * and the kick_trng(...) function will modfiy the
565 * upper and lower limits of the entropy sampling
566 * interval, leading to a sucessful initialization of
567 * the RNG.
568 */
569 ret = instantiate_rng(dev, inst_handles,
570 gen_sk);
Alex Porosanueeaa1722014-08-11 11:40:16 +0300571 if (ret == -EAGAIN)
572 /*
573 * if here, the loop will rerun,
574 * so don't hog the CPU
575 */
576 cpu_relax();
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300577 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
Kim Phillips281922a2012-06-22 19:48:52 -0500578 if (ret) {
Alex Porosanu84cf4822013-09-09 18:56:30 +0300579 dev_err(dev, "failed to instantiate RNG");
Kim Phillips281922a2012-06-22 19:48:52 -0500580 caam_remove(pdev);
581 return ret;
582 }
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300583 /*
584 * Set handles init'ed by this module as the complement of the
585 * already initialized ones
586 */
587 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
Vakul Garg575c1bd2013-03-12 13:55:21 +0530588
589 /* Enable RDB bit so that RNG works faster */
590 setbits32(&topregs->ctrl.scfgr, SCFGR_RDBENABLE);
Kim Phillips281922a2012-06-22 19:48:52 -0500591 }
592
Kim Phillips8e8ec592011-03-13 16:54:26 +0800593 /* NOTE: RTIC detection ought to go here, around Si time */
594
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530595 caam_id = (u64)rd_reg32(&topregs->ctrl.perfmon.caam_id_ms) << 32 |
596 (u64)rd_reg32(&topregs->ctrl.perfmon.caam_id_ls);
Alex Porosanu82c2f962012-07-11 11:06:11 +0800597
Kim Phillips8e8ec592011-03-13 16:54:26 +0800598 /* Report "alive" for developer to see */
Alex Porosanu82c2f962012-07-11 11:06:11 +0800599 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
Alex Porosanu883619a2014-02-06 10:27:19 +0200600 caam_get_era());
Kim Phillips8e8ec592011-03-13 16:54:26 +0800601 dev_info(dev, "job rings = %d, qi = %d\n",
602 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
603
604#ifdef CONFIG_DEBUG_FS
605 /*
606 * FIXME: needs better naming distinction, as some amalgamation of
607 * "caam" and nprop->full_name. The OF name isn't distinctive,
608 * but does separate instances
609 */
610 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
611
Nitesh Narayan Lal178f8272014-07-01 19:54:54 +0530612 ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800613 ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
614
615 /* Controller-level - performance monitor counters */
616 ctrlpriv->ctl_rq_dequeued =
617 debugfs_create_u64("rq_dequeued",
Al Viroeda65cc2011-07-24 04:32:53 -0400618 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800619 ctrlpriv->ctl, &perfmon->req_dequeued);
620 ctrlpriv->ctl_ob_enc_req =
621 debugfs_create_u64("ob_rq_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400622 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800623 ctrlpriv->ctl, &perfmon->ob_enc_req);
624 ctrlpriv->ctl_ib_dec_req =
625 debugfs_create_u64("ib_rq_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400626 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800627 ctrlpriv->ctl, &perfmon->ib_dec_req);
628 ctrlpriv->ctl_ob_enc_bytes =
629 debugfs_create_u64("ob_bytes_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400630 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800631 ctrlpriv->ctl, &perfmon->ob_enc_bytes);
632 ctrlpriv->ctl_ob_prot_bytes =
633 debugfs_create_u64("ob_bytes_protected",
Al Viroeda65cc2011-07-24 04:32:53 -0400634 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800635 ctrlpriv->ctl, &perfmon->ob_prot_bytes);
636 ctrlpriv->ctl_ib_dec_bytes =
637 debugfs_create_u64("ib_bytes_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400638 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800639 ctrlpriv->ctl, &perfmon->ib_dec_bytes);
640 ctrlpriv->ctl_ib_valid_bytes =
641 debugfs_create_u64("ib_bytes_validated",
Al Viroeda65cc2011-07-24 04:32:53 -0400642 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800643 ctrlpriv->ctl, &perfmon->ib_valid_bytes);
644
645 /* Controller level - global status values */
646 ctrlpriv->ctl_faultaddr =
647 debugfs_create_u64("fault_addr",
Al Viroeda65cc2011-07-24 04:32:53 -0400648 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800649 ctrlpriv->ctl, &perfmon->faultaddr);
650 ctrlpriv->ctl_faultdetail =
651 debugfs_create_u32("fault_detail",
Al Viroeda65cc2011-07-24 04:32:53 -0400652 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800653 ctrlpriv->ctl, &perfmon->faultdetail);
654 ctrlpriv->ctl_faultstatus =
655 debugfs_create_u32("fault_status",
Al Viroeda65cc2011-07-24 04:32:53 -0400656 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800657 ctrlpriv->ctl, &perfmon->status);
658
659 /* Internal covering keys (useful in non-secure mode only) */
660 ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
661 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
662 ctrlpriv->ctl_kek = debugfs_create_blob("kek",
Al Viroeda65cc2011-07-24 04:32:53 -0400663 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800664 S_IRGRP | S_IROTH,
665 ctrlpriv->ctl,
666 &ctrlpriv->ctl_kek_wrap);
667
668 ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
669 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
670 ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
Al Viroeda65cc2011-07-24 04:32:53 -0400671 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800672 S_IRGRP | S_IROTH,
673 ctrlpriv->ctl,
674 &ctrlpriv->ctl_tkek_wrap);
675
676 ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
677 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
678 ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
Al Viroeda65cc2011-07-24 04:32:53 -0400679 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800680 S_IRGRP | S_IROTH,
681 ctrlpriv->ctl,
682 &ctrlpriv->ctl_tdsk_wrap);
683#endif
684 return 0;
685}
686
687static struct of_device_id caam_match[] = {
688 {
Kim Phillips54e198d2011-03-23 21:15:44 +0800689 .compatible = "fsl,sec-v4.0",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800690 },
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800691 {
692 .compatible = "fsl,sec4.0",
693 },
Kim Phillips8e8ec592011-03-13 16:54:26 +0800694 {},
695};
696MODULE_DEVICE_TABLE(of, caam_match);
697
Kim Phillips2930d492011-05-14 22:07:55 -0500698static struct platform_driver caam_driver = {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800699 .driver = {
700 .name = "caam",
701 .owner = THIS_MODULE,
702 .of_match_table = caam_match,
703 },
704 .probe = caam_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800705 .remove = caam_remove,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800706};
707
Axel Lin741e8c22011-11-26 21:26:19 +0800708module_platform_driver(caam_driver);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800709
710MODULE_LICENSE("GPL");
711MODULE_DESCRIPTION("FSL CAAM request backend");
712MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");