blob: 26438cd126859e75638c893ad6e959c3baf9e9ef [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
8#include "compat.h"
9#include "regs.h"
10#include "intern.h"
11#include "jr.h"
Kim Phillips281922a2012-06-22 19:48:52 -050012#include "desc_constr.h"
13#include "error.h"
Alex Porosanu82c2f962012-07-11 11:06:11 +080014#include "ctrl.h"
Kim Phillips8e8ec592011-03-13 16:54:26 +080015
Kim Phillips281922a2012-06-22 19:48:52 -050016/*
17 * Descriptor to instantiate RNG State Handle 0 in normal mode and
18 * load the JDKEK, TDKEK and TDSK registers
19 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030020static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
Kim Phillips281922a2012-06-22 19:48:52 -050021{
Alex Porosanu1005bcc2013-09-09 18:56:34 +030022 u32 *jump_cmd, op_flags;
Kim Phillips281922a2012-06-22 19:48:52 -050023
24 init_job_desc(desc, 0);
25
Alex Porosanu1005bcc2013-09-09 18:56:34 +030026 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
27 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
28
Kim Phillips281922a2012-06-22 19:48:52 -050029 /* INIT RNG in non-test mode */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030030 append_operation(desc, op_flags);
Kim Phillips281922a2012-06-22 19:48:52 -050031
Alex Porosanu1005bcc2013-09-09 18:56:34 +030032 if (!handle && do_sk) {
33 /*
34 * For SH0, Secure Keys must be generated as well
35 */
Kim Phillips281922a2012-06-22 19:48:52 -050036
Alex Porosanu1005bcc2013-09-09 18:56:34 +030037 /* wait for done */
38 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
39 set_jump_tgt_here(desc, jump_cmd);
Kim Phillips281922a2012-06-22 19:48:52 -050040
Alex Porosanu1005bcc2013-09-09 18:56:34 +030041 /*
42 * load 1 to clear written reg:
43 * resets the done interrrupt and returns the RNG to idle.
44 */
45 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
46
47 /* Initialize State Handle */
48 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
49 OP_ALG_AAI_RNG4_SK);
50 }
Alex Porosanud5e4e992013-09-09 18:56:28 +030051
52 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
Kim Phillips281922a2012-06-22 19:48:52 -050053}
54
Alex Porosanub1f996e02013-09-09 18:56:32 +030055/* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030056static void build_deinstantiation_desc(u32 *desc, int handle)
Alex Porosanub1f996e02013-09-09 18:56:32 +030057{
58 init_job_desc(desc, 0);
59
60 /* Uninstantiate State Handle 0 */
61 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
Alex Porosanu1005bcc2013-09-09 18:56:34 +030062 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
Alex Porosanub1f996e02013-09-09 18:56:32 +030063
64 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
65}
Alex Porosanu04cddbf2013-09-09 18:56:31 +030066
67/*
68 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
69 * the software (no JR/QI used).
70 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +030071 * @status - descriptor status, after being run
72 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +030073 * Return: - 0 if no error occurred
74 * - -ENODEV if the DECO couldn't be acquired
75 * - -EAGAIN if an error occurred while executing the descriptor
76 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030077static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
78 u32 *status)
Kim Phillips281922a2012-06-22 19:48:52 -050079{
Ruchika Gupta997ad292013-07-04 11:26:03 +053080 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
81 struct caam_full __iomem *topregs;
82 unsigned int timeout = 100000;
Alex Porosanu04cddbf2013-09-09 18:56:31 +030083 u32 deco_dbg_reg, flags;
Alex Porosanub1f996e02013-09-09 18:56:32 +030084 int i;
Kim Phillips281922a2012-06-22 19:48:52 -050085
Ruchika Gupta997ad292013-07-04 11:26:03 +053086 /* Set the bit to request direct access to DECO0 */
87 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
88 setbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
89
90 while (!(rd_reg32(&topregs->ctrl.deco_rq) & DECORR_DEN0) &&
91 --timeout)
92 cpu_relax();
93
94 if (!timeout) {
95 dev_err(ctrldev, "failed to acquire DECO 0\n");
Alex Porosanu04cddbf2013-09-09 18:56:31 +030096 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
97 return -ENODEV;
Kim Phillips281922a2012-06-22 19:48:52 -050098 }
99
Ruchika Gupta997ad292013-07-04 11:26:03 +0530100 for (i = 0; i < desc_len(desc); i++)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300101 wr_reg32(&topregs->deco.descbuf[i], *(desc + i));
Kim Phillips281922a2012-06-22 19:48:52 -0500102
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300103 flags = DECO_JQCR_WHL;
104 /*
105 * If the descriptor length is longer than 4 words, then the
106 * FOUR bit in JRCTRL register must be set.
107 */
108 if (desc_len(desc) >= 4)
109 flags |= DECO_JQCR_FOUR;
110
111 /* Instruct the DECO to execute it */
112 wr_reg32(&topregs->deco.jr_ctl_hi, flags);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530113
114 timeout = 10000000;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300115 do {
116 deco_dbg_reg = rd_reg32(&topregs->deco.desc_dbg);
117 /*
118 * If an error occured in the descriptor, then
119 * the DECO status field will be set to 0x0D
120 */
121 if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
122 DESC_DBG_DECO_STAT_HOST_ERR)
123 break;
Ruchika Gupta997ad292013-07-04 11:26:03 +0530124 cpu_relax();
Alex Porosanu84cf4822013-09-09 18:56:30 +0300125 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530126
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300127 *status = rd_reg32(&topregs->deco.op_status_hi) &
128 DECO_OP_STATUS_HI_ERR_MASK;
129
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300130 /* Mark the DECO as free */
Ruchika Gupta997ad292013-07-04 11:26:03 +0530131 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300132
133 if (!timeout)
134 return -EAGAIN;
135
136 return 0;
137}
138
139/*
140 * instantiate_rng - builds and executes a descriptor on DECO0,
141 * which initializes the RNG block.
142 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300143 * @state_handle_mask - bitmask containing the instantiation status
144 * for the RNG4 state handles which exist in
145 * the RNG4 block: 1 if it's been instantiated
146 * by an external entry, 0 otherwise.
147 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
148 * Caution: this can be done only once; if the keys need to be
149 * regenerated, a POR is required
150 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300151 * Return: - 0 if no error occurred
152 * - -ENOMEM if there isn't enough memory to allocate the descriptor
153 * - -ENODEV if DECO0 couldn't be acquired
154 * - -EAGAIN if an error occurred when executing the descriptor
155 * f.i. there was a RNG hardware error due to not "good enough"
156 * entropy being aquired.
157 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300158static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
159 int gen_sk)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300160{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300161 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
162 struct caam_full __iomem *topregs;
163 struct rng4tst __iomem *r4tst;
164 u32 *desc, status, rdsta_val;
165 int ret = 0, sh_idx;
166
167 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
168 r4tst = &topregs->ctrl.r4tst[0];
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300169
170 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
171 if (!desc)
172 return -ENOMEM;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300173
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300174 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
175 /*
176 * If the corresponding bit is set, this state handle
177 * was initialized by somebody else, so it's left alone.
178 */
179 if ((1 << sh_idx) & state_handle_mask)
180 continue;
181
182 /* Create the descriptor for instantiating RNG State Handle */
183 build_instantiation_desc(desc, sh_idx, gen_sk);
184
185 /* Try to run it through DECO0 */
186 ret = run_descriptor_deco0(ctrldev, desc, &status);
187
188 /*
189 * If ret is not 0, or descriptor status is not 0, then
190 * something went wrong. No need to try the next state
191 * handle (if available), bail out here.
192 * Also, if for some reason, the State Handle didn't get
193 * instantiated although the descriptor has finished
194 * without any error (HW optimizations for later
195 * CAAM eras), then try again.
196 */
197 rdsta_val =
198 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) & RDSTA_IFMASK;
199 if (status || !(rdsta_val & (1 << sh_idx)))
200 ret = -EAGAIN;
201 if (ret)
202 break;
203
204 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
205 /* Clear the contents before recreating the descriptor */
206 memset(desc, 0x00, CAAM_CMD_SZ * 7);
207 }
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300208
Kim Phillips281922a2012-06-22 19:48:52 -0500209 kfree(desc);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300210
211 return ret;
212}
213
Alex Porosanub1f996e02013-09-09 18:56:32 +0300214/*
215 * deinstantiate_rng - builds and executes a descriptor on DECO0,
216 * which deinitializes the RNG block.
217 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300218 * @state_handle_mask - bitmask containing the instantiation status
219 * for the RNG4 state handles which exist in
220 * the RNG4 block: 1 if it's been instantiated
Alex Porosanub1f996e02013-09-09 18:56:32 +0300221 *
222 * Return: - 0 if no error occurred
223 * - -ENOMEM if there isn't enough memory to allocate the descriptor
224 * - -ENODEV if DECO0 couldn't be acquired
225 * - -EAGAIN if an error occurred when executing the descriptor
226 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300227static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
Alex Porosanub1f996e02013-09-09 18:56:32 +0300228{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300229 u32 *desc, status;
230 int sh_idx, ret = 0;
Alex Porosanub1f996e02013-09-09 18:56:32 +0300231
232 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
233 if (!desc)
234 return -ENOMEM;
235
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300236 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
237 /*
238 * If the corresponding bit is set, then it means the state
239 * handle was initialized by us, and thus it needs to be
240 * deintialized as well
241 */
242 if ((1 << sh_idx) & state_handle_mask) {
243 /*
244 * Create the descriptor for deinstantating this state
245 * handle
246 */
247 build_deinstantiation_desc(desc, sh_idx);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300248
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300249 /* Try to run it through DECO0 */
250 ret = run_descriptor_deco0(ctrldev, desc, &status);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300251
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300252 if (ret || status) {
253 dev_err(ctrldev,
254 "Failed to deinstantiate RNG4 SH%d\n",
255 sh_idx);
256 break;
257 }
258 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
259 }
260 }
Alex Porosanub1f996e02013-09-09 18:56:32 +0300261
262 kfree(desc);
263
264 return ret;
265}
266
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300267static int caam_remove(struct platform_device *pdev)
268{
269 struct device *ctrldev;
270 struct caam_drv_private *ctrlpriv;
271 struct caam_drv_private_jr *jrpriv;
272 struct caam_full __iomem *topregs;
273 int ring, ret = 0;
274
275 ctrldev = &pdev->dev;
276 ctrlpriv = dev_get_drvdata(ctrldev);
277 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
278
279 /* shut down JobRs */
280 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
281 ret |= caam_jr_shutdown(ctrlpriv->jrdev[ring]);
282 jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
283 irq_dispose_mapping(jrpriv->irq);
284 }
285
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300286 /* De-initialize RNG state handles initialized by this driver. */
287 if (ctrlpriv->rng4_sh_init)
288 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300289
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300290 /* Shut down debug views */
291#ifdef CONFIG_DEBUG_FS
292 debugfs_remove_recursive(ctrlpriv->dfs_root);
293#endif
294
295 /* Unmap controller region */
296 iounmap(&topregs->ctrl);
297
298 kfree(ctrlpriv->jrdev);
299 kfree(ctrlpriv);
300
Kim Phillips281922a2012-06-22 19:48:52 -0500301 return ret;
302}
303
304/*
Alex Porosanu84cf4822013-09-09 18:56:30 +0300305 * kick_trng - sets the various parameters for enabling the initialization
306 * of the RNG4 block in CAAM
307 * @pdev - pointer to the platform device
308 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
Kim Phillips281922a2012-06-22 19:48:52 -0500309 */
Alex Porosanu84cf4822013-09-09 18:56:30 +0300310static void kick_trng(struct platform_device *pdev, int ent_delay)
Kim Phillips281922a2012-06-22 19:48:52 -0500311{
312 struct device *ctrldev = &pdev->dev;
313 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
314 struct caam_full __iomem *topregs;
315 struct rng4tst __iomem *r4tst;
316 u32 val;
317
318 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
319 r4tst = &topregs->ctrl.r4tst[0];
320
321 /* put RNG4 into program mode */
322 setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300323
324 /*
325 * Performance-wise, it does not make sense to
326 * set the delay to a value that is lower
327 * than the last one that worked (i.e. the state handles
328 * were instantiated properly. Thus, instead of wasting
329 * time trying to set the values controlling the sample
330 * frequency, the function simply returns.
331 */
332 val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
333 >> RTSDCTL_ENT_DLY_SHIFT;
334 if (ent_delay <= val) {
335 /* put RNG4 into run mode */
336 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
337 return;
338 }
339
Kim Phillips281922a2012-06-22 19:48:52 -0500340 val = rd_reg32(&r4tst->rtsdctl);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300341 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
342 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
Kim Phillips281922a2012-06-22 19:48:52 -0500343 wr_reg32(&r4tst->rtsdctl, val);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300344 /* min. freq. count, equal to 1/4 of the entropy sample length */
345 wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
346 /* max. freq. count, equal to 8 times the entropy sample length */
347 wr_reg32(&r4tst->rtfrqmax, ent_delay << 3);
Kim Phillips281922a2012-06-22 19:48:52 -0500348 /* put RNG4 into run mode */
349 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
350}
351
Alex Porosanu82c2f962012-07-11 11:06:11 +0800352/**
353 * caam_get_era() - Return the ERA of the SEC on SoC, based
354 * on the SEC_VID register.
355 * Returns the ERA number (1..4) or -ENOTSUPP if the ERA is unknown.
356 * @caam_id - the value of the SEC_VID register
357 **/
358int caam_get_era(u64 caam_id)
359{
360 struct sec_vid *sec_vid = (struct sec_vid *)&caam_id;
361 static const struct {
362 u16 ip_id;
363 u8 maj_rev;
364 u8 era;
365 } caam_eras[] = {
366 {0x0A10, 1, 1},
367 {0x0A10, 2, 2},
368 {0x0A12, 1, 3},
369 {0x0A14, 1, 3},
370 {0x0A14, 2, 4},
371 {0x0A16, 1, 4},
372 {0x0A11, 1, 4}
373 };
374 int i;
375
376 for (i = 0; i < ARRAY_SIZE(caam_eras); i++)
377 if (caam_eras[i].ip_id == sec_vid->ip_id &&
378 caam_eras[i].maj_rev == sec_vid->maj_rev)
379 return caam_eras[i].era;
380
381 return -ENOTSUPP;
382}
383EXPORT_SYMBOL(caam_get_era);
384
Kim Phillips8e8ec592011-03-13 16:54:26 +0800385/* Probe routine for CAAM top (controller) level */
Kim Phillips2930d492011-05-14 22:07:55 -0500386static int caam_probe(struct platform_device *pdev)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800387{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300388 int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
Alex Porosanu82c2f962012-07-11 11:06:11 +0800389 u64 caam_id;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800390 struct device *dev;
391 struct device_node *nprop, *np;
392 struct caam_ctrl __iomem *ctrl;
393 struct caam_full __iomem *topregs;
394 struct caam_drv_private *ctrlpriv;
Kim Phillips23457bc2011-06-05 16:42:54 -0500395#ifdef CONFIG_DEBUG_FS
396 struct caam_perfmon *perfmon;
397#endif
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530398 u64 cha_vid;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800399
400 ctrlpriv = kzalloc(sizeof(struct caam_drv_private), GFP_KERNEL);
401 if (!ctrlpriv)
402 return -ENOMEM;
403
404 dev = &pdev->dev;
405 dev_set_drvdata(dev, ctrlpriv);
406 ctrlpriv->pdev = pdev;
407 nprop = pdev->dev.of_node;
408
409 /* Get configuration properties from device tree */
410 /* First, get register page */
411 ctrl = of_iomap(nprop, 0);
412 if (ctrl == NULL) {
413 dev_err(dev, "caam: of_iomap() failed\n");
414 return -ENOMEM;
415 }
416 ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
417
418 /* topregs used to derive pointers to CAAM sub-blocks only */
419 topregs = (struct caam_full __iomem *)ctrl;
420
421 /* Get the IRQ of the controller (for security violations only) */
422 ctrlpriv->secvio_irq = of_irq_to_resource(nprop, 0, NULL);
423
424 /*
425 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
Kim Phillipse13af182012-06-22 19:48:51 -0500426 * long pointers in master configuration register
Kim Phillips8e8ec592011-03-13 16:54:26 +0800427 */
428 setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
429 (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
430
431 if (sizeof(dma_addr_t) == sizeof(u64))
Kim Phillipse13af182012-06-22 19:48:51 -0500432 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
433 dma_set_mask(dev, DMA_BIT_MASK(40));
434 else
435 dma_set_mask(dev, DMA_BIT_MASK(36));
436 else
437 dma_set_mask(dev, DMA_BIT_MASK(32));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800438
Kim Phillips8e8ec592011-03-13 16:54:26 +0800439 /*
440 * Detect and enable JobRs
441 * First, find out how many ring spec'ed, allocate references
442 * for all, then go probe each one.
443 */
444 rspec = 0;
Kim Phillips54e198d2011-03-23 21:15:44 +0800445 for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring")
Kim Phillips8e8ec592011-03-13 16:54:26 +0800446 rspec++;
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800447 if (!rspec) {
448 /* for backward compatible with device trees */
449 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring")
450 rspec++;
451 }
452
Kim Phillips8e8ec592011-03-13 16:54:26 +0800453 ctrlpriv->jrdev = kzalloc(sizeof(struct device *) * rspec, GFP_KERNEL);
454 if (ctrlpriv->jrdev == NULL) {
455 iounmap(&topregs->ctrl);
456 return -ENOMEM;
457 }
458
459 ring = 0;
460 ctrlpriv->total_jobrs = 0;
Kim Phillips54e198d2011-03-23 21:15:44 +0800461 for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring") {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800462 caam_jr_probe(pdev, np, ring);
463 ctrlpriv->total_jobrs++;
464 ring++;
465 }
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800466 if (!ring) {
467 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring") {
468 caam_jr_probe(pdev, np, ring);
469 ctrlpriv->total_jobrs++;
470 ring++;
471 }
472 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800473
474 /* Check to see if QI present. If so, enable */
475 ctrlpriv->qi_present = !!(rd_reg64(&topregs->ctrl.perfmon.comp_parms) &
476 CTPR_QI_MASK);
477 if (ctrlpriv->qi_present) {
478 ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
479 /* This is all that's required to physically enable QI */
480 wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
481 }
482
483 /* If no QI and no rings specified, quit and go home */
484 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
485 dev_err(dev, "no queues configured, terminating\n");
486 caam_remove(pdev);
487 return -ENOMEM;
488 }
489
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530490 cha_vid = rd_reg64(&topregs->ctrl.perfmon.cha_id);
491
Kim Phillips281922a2012-06-22 19:48:52 -0500492 /*
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530493 * If SEC has RNG version >= 4 and RNG state handle has not been
Alex Porosanu84cf4822013-09-09 18:56:30 +0300494 * already instantiated, do RNG instantiation
Kim Phillips281922a2012-06-22 19:48:52 -0500495 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300496 if ((cha_vid & CHA_ID_RNG_MASK) >> CHA_ID_RNG_SHIFT >= 4) {
497 ctrlpriv->rng4_sh_init =
498 rd_reg32(&topregs->ctrl.r4tst[0].rdsta);
499 /*
500 * If the secure keys (TDKEK, JDKEK, TDSK), were already
501 * generated, signal this to the function that is instantiating
502 * the state handles. An error would occur if RNG4 attempts
503 * to regenerate these keys before the next POR.
504 */
505 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
506 ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300507 do {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300508 int inst_handles =
509 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) &
510 RDSTA_IFMASK;
511 /*
512 * If either SH were instantiated by somebody else
513 * (e.g. u-boot) then it is assumed that the entropy
514 * parameters are properly set and thus the function
515 * setting these (kick_trng(...)) is skipped.
516 * Also, if a handle was instantiated, do not change
517 * the TRNG parameters.
518 */
519 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
520 kick_trng(pdev, ent_delay);
521 ent_delay += 400;
522 }
523 /*
524 * if instantiate_rng(...) fails, the loop will rerun
525 * and the kick_trng(...) function will modfiy the
526 * upper and lower limits of the entropy sampling
527 * interval, leading to a sucessful initialization of
528 * the RNG.
529 */
530 ret = instantiate_rng(dev, inst_handles,
531 gen_sk);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300532 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
Kim Phillips281922a2012-06-22 19:48:52 -0500533 if (ret) {
Alex Porosanu84cf4822013-09-09 18:56:30 +0300534 dev_err(dev, "failed to instantiate RNG");
Kim Phillips281922a2012-06-22 19:48:52 -0500535 caam_remove(pdev);
536 return ret;
537 }
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300538 /*
539 * Set handles init'ed by this module as the complement of the
540 * already initialized ones
541 */
542 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
Alex Porosanub1f996e02013-09-09 18:56:32 +0300543
Vakul Garg575c1bd2013-03-12 13:55:21 +0530544 /* Enable RDB bit so that RNG works faster */
545 setbits32(&topregs->ctrl.scfgr, SCFGR_RDBENABLE);
Kim Phillips281922a2012-06-22 19:48:52 -0500546 }
547
Kim Phillips8e8ec592011-03-13 16:54:26 +0800548 /* NOTE: RTIC detection ought to go here, around Si time */
549
Alex Porosanu82c2f962012-07-11 11:06:11 +0800550 caam_id = rd_reg64(&topregs->ctrl.perfmon.caam_id);
551
Kim Phillips8e8ec592011-03-13 16:54:26 +0800552 /* Report "alive" for developer to see */
Alex Porosanu82c2f962012-07-11 11:06:11 +0800553 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
554 caam_get_era(caam_id));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800555 dev_info(dev, "job rings = %d, qi = %d\n",
556 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
557
558#ifdef CONFIG_DEBUG_FS
559 /*
560 * FIXME: needs better naming distinction, as some amalgamation of
561 * "caam" and nprop->full_name. The OF name isn't distinctive,
562 * but does separate instances
563 */
564 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
565
566 ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
567 ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
568
569 /* Controller-level - performance monitor counters */
570 ctrlpriv->ctl_rq_dequeued =
571 debugfs_create_u64("rq_dequeued",
Al Viroeda65cc2011-07-24 04:32:53 -0400572 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800573 ctrlpriv->ctl, &perfmon->req_dequeued);
574 ctrlpriv->ctl_ob_enc_req =
575 debugfs_create_u64("ob_rq_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400576 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800577 ctrlpriv->ctl, &perfmon->ob_enc_req);
578 ctrlpriv->ctl_ib_dec_req =
579 debugfs_create_u64("ib_rq_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400580 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800581 ctrlpriv->ctl, &perfmon->ib_dec_req);
582 ctrlpriv->ctl_ob_enc_bytes =
583 debugfs_create_u64("ob_bytes_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400584 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800585 ctrlpriv->ctl, &perfmon->ob_enc_bytes);
586 ctrlpriv->ctl_ob_prot_bytes =
587 debugfs_create_u64("ob_bytes_protected",
Al Viroeda65cc2011-07-24 04:32:53 -0400588 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800589 ctrlpriv->ctl, &perfmon->ob_prot_bytes);
590 ctrlpriv->ctl_ib_dec_bytes =
591 debugfs_create_u64("ib_bytes_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400592 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800593 ctrlpriv->ctl, &perfmon->ib_dec_bytes);
594 ctrlpriv->ctl_ib_valid_bytes =
595 debugfs_create_u64("ib_bytes_validated",
Al Viroeda65cc2011-07-24 04:32:53 -0400596 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800597 ctrlpriv->ctl, &perfmon->ib_valid_bytes);
598
599 /* Controller level - global status values */
600 ctrlpriv->ctl_faultaddr =
601 debugfs_create_u64("fault_addr",
Al Viroeda65cc2011-07-24 04:32:53 -0400602 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800603 ctrlpriv->ctl, &perfmon->faultaddr);
604 ctrlpriv->ctl_faultdetail =
605 debugfs_create_u32("fault_detail",
Al Viroeda65cc2011-07-24 04:32:53 -0400606 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800607 ctrlpriv->ctl, &perfmon->faultdetail);
608 ctrlpriv->ctl_faultstatus =
609 debugfs_create_u32("fault_status",
Al Viroeda65cc2011-07-24 04:32:53 -0400610 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800611 ctrlpriv->ctl, &perfmon->status);
612
613 /* Internal covering keys (useful in non-secure mode only) */
614 ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
615 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
616 ctrlpriv->ctl_kek = debugfs_create_blob("kek",
Al Viroeda65cc2011-07-24 04:32:53 -0400617 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800618 S_IRGRP | S_IROTH,
619 ctrlpriv->ctl,
620 &ctrlpriv->ctl_kek_wrap);
621
622 ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
623 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
624 ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
Al Viroeda65cc2011-07-24 04:32:53 -0400625 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800626 S_IRGRP | S_IROTH,
627 ctrlpriv->ctl,
628 &ctrlpriv->ctl_tkek_wrap);
629
630 ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
631 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
632 ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
Al Viroeda65cc2011-07-24 04:32:53 -0400633 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800634 S_IRGRP | S_IROTH,
635 ctrlpriv->ctl,
636 &ctrlpriv->ctl_tdsk_wrap);
637#endif
638 return 0;
639}
640
641static struct of_device_id caam_match[] = {
642 {
Kim Phillips54e198d2011-03-23 21:15:44 +0800643 .compatible = "fsl,sec-v4.0",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800644 },
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800645 {
646 .compatible = "fsl,sec4.0",
647 },
Kim Phillips8e8ec592011-03-13 16:54:26 +0800648 {},
649};
650MODULE_DEVICE_TABLE(of, caam_match);
651
Kim Phillips2930d492011-05-14 22:07:55 -0500652static struct platform_driver caam_driver = {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800653 .driver = {
654 .name = "caam",
655 .owner = THIS_MODULE,
656 .of_match_table = caam_match,
657 },
658 .probe = caam_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800659 .remove = caam_remove,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800660};
661
Axel Lin741e8c22011-11-26 21:26:19 +0800662module_platform_driver(caam_driver);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800663
664MODULE_LICENSE("GPL");
665MODULE_DESCRIPTION("FSL CAAM request backend");
666MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");