blob: 066a4d4b62bf1253d6b8c1f4129485fc24d7123c [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;
91 setbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
92
93 while (!(rd_reg32(&topregs->ctrl.deco_rq) & DECORR_DEN0) &&
94 --timeout)
95 cpu_relax();
96
97 if (!timeout) {
98 dev_err(ctrldev, "failed to acquire DECO 0\n");
Alex Porosanu04cddbf2013-09-09 18:56:31 +030099 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
100 return -ENODEV;
Kim Phillips281922a2012-06-22 19:48:52 -0500101 }
102
Ruchika Gupta997ad292013-07-04 11:26:03 +0530103 for (i = 0; i < desc_len(desc); i++)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300104 wr_reg32(&topregs->deco.descbuf[i], *(desc + i));
Kim Phillips281922a2012-06-22 19:48:52 -0500105
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300106 flags = DECO_JQCR_WHL;
107 /*
108 * If the descriptor length is longer than 4 words, then the
109 * FOUR bit in JRCTRL register must be set.
110 */
111 if (desc_len(desc) >= 4)
112 flags |= DECO_JQCR_FOUR;
113
114 /* Instruct the DECO to execute it */
115 wr_reg32(&topregs->deco.jr_ctl_hi, flags);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530116
117 timeout = 10000000;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300118 do {
119 deco_dbg_reg = rd_reg32(&topregs->deco.desc_dbg);
120 /*
121 * If an error occured in the descriptor, then
122 * the DECO status field will be set to 0x0D
123 */
124 if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
125 DESC_DBG_DECO_STAT_HOST_ERR)
126 break;
Ruchika Gupta997ad292013-07-04 11:26:03 +0530127 cpu_relax();
Alex Porosanu84cf4822013-09-09 18:56:30 +0300128 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530129
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300130 *status = rd_reg32(&topregs->deco.op_status_hi) &
131 DECO_OP_STATUS_HI_ERR_MASK;
132
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300133 /* Mark the DECO as free */
Ruchika Gupta997ad292013-07-04 11:26:03 +0530134 clrbits32(&topregs->ctrl.deco_rq, DECORR_RQD0ENABLE);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300135
136 if (!timeout)
137 return -EAGAIN;
138
139 return 0;
140}
141
142/*
143 * instantiate_rng - builds and executes a descriptor on DECO0,
144 * which initializes the RNG block.
145 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300146 * @state_handle_mask - bitmask containing the instantiation status
147 * for the RNG4 state handles which exist in
148 * the RNG4 block: 1 if it's been instantiated
149 * by an external entry, 0 otherwise.
150 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
151 * Caution: this can be done only once; if the keys need to be
152 * regenerated, a POR is required
153 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300154 * Return: - 0 if no error occurred
155 * - -ENOMEM if there isn't enough memory to allocate the descriptor
156 * - -ENODEV if DECO0 couldn't be acquired
157 * - -EAGAIN if an error occurred when executing the descriptor
158 * f.i. there was a RNG hardware error due to not "good enough"
159 * entropy being aquired.
160 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300161static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
162 int gen_sk)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300163{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300164 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
165 struct caam_full __iomem *topregs;
166 struct rng4tst __iomem *r4tst;
167 u32 *desc, status, rdsta_val;
168 int ret = 0, sh_idx;
169
170 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
171 r4tst = &topregs->ctrl.r4tst[0];
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300172
173 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
174 if (!desc)
175 return -ENOMEM;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300176
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300177 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
178 /*
179 * If the corresponding bit is set, this state handle
180 * was initialized by somebody else, so it's left alone.
181 */
182 if ((1 << sh_idx) & state_handle_mask)
183 continue;
184
185 /* Create the descriptor for instantiating RNG State Handle */
186 build_instantiation_desc(desc, sh_idx, gen_sk);
187
188 /* Try to run it through DECO0 */
189 ret = run_descriptor_deco0(ctrldev, desc, &status);
190
191 /*
192 * If ret is not 0, or descriptor status is not 0, then
193 * something went wrong. No need to try the next state
194 * handle (if available), bail out here.
195 * Also, if for some reason, the State Handle didn't get
196 * instantiated although the descriptor has finished
197 * without any error (HW optimizations for later
198 * CAAM eras), then try again.
199 */
200 rdsta_val =
201 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) & RDSTA_IFMASK;
202 if (status || !(rdsta_val & (1 << sh_idx)))
203 ret = -EAGAIN;
204 if (ret)
205 break;
206
207 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
208 /* Clear the contents before recreating the descriptor */
209 memset(desc, 0x00, CAAM_CMD_SZ * 7);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530210 }
211
Kim Phillips281922a2012-06-22 19:48:52 -0500212 kfree(desc);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300213
Kim Phillips281922a2012-06-22 19:48:52 -0500214 return ret;
215}
216
217/*
Alex Porosanub1f996e02013-09-09 18:56:32 +0300218 * deinstantiate_rng - builds and executes a descriptor on DECO0,
219 * which deinitializes the RNG block.
220 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300221 * @state_handle_mask - bitmask containing the instantiation status
222 * for the RNG4 state handles which exist in
223 * the RNG4 block: 1 if it's been instantiated
Alex Porosanub1f996e02013-09-09 18:56:32 +0300224 *
225 * Return: - 0 if no error occurred
226 * - -ENOMEM if there isn't enough memory to allocate the descriptor
227 * - -ENODEV if DECO0 couldn't be acquired
228 * - -EAGAIN if an error occurred when executing the descriptor
Kim Phillips281922a2012-06-22 19:48:52 -0500229 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300230static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
Alex Porosanub1f996e02013-09-09 18:56:32 +0300231{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300232 u32 *desc, status;
233 int sh_idx, ret = 0;
Alex Porosanub1f996e02013-09-09 18:56:32 +0300234
235 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
236 if (!desc)
237 return -ENOMEM;
238
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300239 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
240 /*
241 * If the corresponding bit is set, then it means the state
242 * handle was initialized by us, and thus it needs to be
243 * deintialized as well
244 */
245 if ((1 << sh_idx) & state_handle_mask) {
246 /*
247 * Create the descriptor for deinstantating this state
248 * handle
249 */
250 build_deinstantiation_desc(desc, sh_idx);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300251
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300252 /* Try to run it through DECO0 */
253 ret = run_descriptor_deco0(ctrldev, desc, &status);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300254
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300255 if (ret || status) {
256 dev_err(ctrldev,
257 "Failed to deinstantiate RNG4 SH%d\n",
258 sh_idx);
259 break;
260 }
261 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
262 }
263 }
Alex Porosanub1f996e02013-09-09 18:56:32 +0300264
265 kfree(desc);
266
267 return ret;
268}
269
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300270static int caam_remove(struct platform_device *pdev)
271{
272 struct device *ctrldev;
273 struct caam_drv_private *ctrlpriv;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300274 struct caam_full __iomem *topregs;
275 int ring, ret = 0;
276
277 ctrldev = &pdev->dev;
278 ctrlpriv = dev_get_drvdata(ctrldev);
279 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
280
Ruchika Gupta313ea292013-10-25 12:01:01 +0530281 /* Remove platform devices for JobRs */
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300282 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530283 if (ctrlpriv->jrpdev[ring])
284 of_device_unregister(ctrlpriv->jrpdev[ring]);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300285 }
286
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300287 /* De-initialize RNG state handles initialized by this driver. */
288 if (ctrlpriv->rng4_sh_init)
289 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300290
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300291 /* Shut down debug views */
292#ifdef CONFIG_DEBUG_FS
293 debugfs_remove_recursive(ctrlpriv->dfs_root);
294#endif
295
296 /* Unmap controller region */
297 iounmap(&topregs->ctrl);
298
Kim Phillips281922a2012-06-22 19:48:52 -0500299 return ret;
300}
301
302/*
Alex Porosanu84cf4822013-09-09 18:56:30 +0300303 * kick_trng - sets the various parameters for enabling the initialization
304 * of the RNG4 block in CAAM
305 * @pdev - pointer to the platform device
306 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
Kim Phillips281922a2012-06-22 19:48:52 -0500307 */
Alex Porosanu84cf4822013-09-09 18:56:30 +0300308static void kick_trng(struct platform_device *pdev, int ent_delay)
Kim Phillips281922a2012-06-22 19:48:52 -0500309{
310 struct device *ctrldev = &pdev->dev;
311 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
312 struct caam_full __iomem *topregs;
313 struct rng4tst __iomem *r4tst;
314 u32 val;
315
316 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
317 r4tst = &topregs->ctrl.r4tst[0];
318
319 /* put RNG4 into program mode */
320 setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300321
322 /*
323 * Performance-wise, it does not make sense to
324 * set the delay to a value that is lower
325 * than the last one that worked (i.e. the state handles
326 * were instantiated properly. Thus, instead of wasting
327 * time trying to set the values controlling the sample
328 * frequency, the function simply returns.
329 */
330 val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
331 >> RTSDCTL_ENT_DLY_SHIFT;
332 if (ent_delay <= val) {
333 /* put RNG4 into run mode */
334 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
335 return;
336 }
337
Kim Phillips281922a2012-06-22 19:48:52 -0500338 val = rd_reg32(&r4tst->rtsdctl);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300339 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
340 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
Kim Phillips281922a2012-06-22 19:48:52 -0500341 wr_reg32(&r4tst->rtsdctl, val);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300342 /* min. freq. count, equal to 1/4 of the entropy sample length */
343 wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
344 /* max. freq. count, equal to 8 times the entropy sample length */
345 wr_reg32(&r4tst->rtfrqmax, ent_delay << 3);
Kim Phillips281922a2012-06-22 19:48:52 -0500346 /* put RNG4 into run mode */
347 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
348}
349
Alex Porosanu82c2f962012-07-11 11:06:11 +0800350/**
351 * caam_get_era() - Return the ERA of the SEC on SoC, based
Alex Porosanu883619a2014-02-06 10:27:19 +0200352 * on "sec-era" propery in the DTS. This property is updated by u-boot.
Alex Porosanu82c2f962012-07-11 11:06:11 +0800353 **/
Alex Porosanu883619a2014-02-06 10:27:19 +0200354int caam_get_era(void)
Alex Porosanu82c2f962012-07-11 11:06:11 +0800355{
Alex Porosanu883619a2014-02-06 10:27:19 +0200356 struct device_node *caam_node;
357 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
358 const uint32_t *prop = (uint32_t *)of_get_property(caam_node,
359 "fsl,sec-era",
360 NULL);
361 return prop ? *prop : -ENOTSUPP;
362 }
Alex Porosanu82c2f962012-07-11 11:06:11 +0800363
364 return -ENOTSUPP;
365}
366EXPORT_SYMBOL(caam_get_era);
367
Kim Phillips8e8ec592011-03-13 16:54:26 +0800368/* Probe routine for CAAM top (controller) level */
Kim Phillips2930d492011-05-14 22:07:55 -0500369static int caam_probe(struct platform_device *pdev)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800370{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300371 int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
Alex Porosanu82c2f962012-07-11 11:06:11 +0800372 u64 caam_id;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800373 struct device *dev;
374 struct device_node *nprop, *np;
375 struct caam_ctrl __iomem *ctrl;
376 struct caam_full __iomem *topregs;
377 struct caam_drv_private *ctrlpriv;
Kim Phillips23457bc2011-06-05 16:42:54 -0500378#ifdef CONFIG_DEBUG_FS
379 struct caam_perfmon *perfmon;
380#endif
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530381 u32 cha_vid_ls;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800382
Himangi Saraogi4776d382014-05-27 23:55:48 +0530383 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(struct caam_drv_private),
384 GFP_KERNEL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800385 if (!ctrlpriv)
386 return -ENOMEM;
387
388 dev = &pdev->dev;
389 dev_set_drvdata(dev, ctrlpriv);
390 ctrlpriv->pdev = pdev;
391 nprop = pdev->dev.of_node;
392
393 /* Get configuration properties from device tree */
394 /* First, get register page */
395 ctrl = of_iomap(nprop, 0);
396 if (ctrl == NULL) {
397 dev_err(dev, "caam: of_iomap() failed\n");
398 return -ENOMEM;
399 }
400 ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
401
402 /* topregs used to derive pointers to CAAM sub-blocks only */
403 topregs = (struct caam_full __iomem *)ctrl;
404
405 /* Get the IRQ of the controller (for security violations only) */
Thierry Redingf7578492013-09-18 15:24:44 +0200406 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800407
408 /*
409 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
Kim Phillipse13af182012-06-22 19:48:51 -0500410 * long pointers in master configuration register
Kim Phillips8e8ec592011-03-13 16:54:26 +0800411 */
412 setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
413 (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
414
415 if (sizeof(dma_addr_t) == sizeof(u64))
Kim Phillipse13af182012-06-22 19:48:51 -0500416 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
417 dma_set_mask(dev, DMA_BIT_MASK(40));
418 else
419 dma_set_mask(dev, DMA_BIT_MASK(36));
420 else
421 dma_set_mask(dev, DMA_BIT_MASK(32));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800422
Kim Phillips8e8ec592011-03-13 16:54:26 +0800423 /*
424 * Detect and enable JobRs
425 * First, find out how many ring spec'ed, allocate references
426 * for all, then go probe each one.
427 */
428 rspec = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800429 for_each_available_child_of_node(nprop, np)
430 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
431 of_device_is_compatible(np, "fsl,sec4.0-job-ring"))
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800432 rspec++;
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800433
Himangi Saraogi4776d382014-05-27 23:55:48 +0530434 ctrlpriv->jrpdev = devm_kzalloc(&pdev->dev,
435 sizeof(struct platform_device *) * rspec,
436 GFP_KERNEL);
Ruchika Gupta313ea292013-10-25 12:01:01 +0530437 if (ctrlpriv->jrpdev == NULL) {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800438 iounmap(&topregs->ctrl);
439 return -ENOMEM;
440 }
441
442 ring = 0;
443 ctrlpriv->total_jobrs = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800444 for_each_available_child_of_node(nprop, np)
445 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
446 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530447 ctrlpriv->jrpdev[ring] =
448 of_platform_device_create(np, NULL, dev);
449 if (!ctrlpriv->jrpdev[ring]) {
450 pr_warn("JR%d Platform device creation error\n",
451 ring);
452 continue;
453 }
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800454 ctrlpriv->total_jobrs++;
455 ring++;
456 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800457
458 /* Check to see if QI present. If so, enable */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530459 ctrlpriv->qi_present =
460 !!(rd_reg32(&topregs->ctrl.perfmon.comp_parms_ms) &
461 CTPR_MS_QI_MASK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800462 if (ctrlpriv->qi_present) {
463 ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
464 /* This is all that's required to physically enable QI */
465 wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
466 }
467
468 /* If no QI and no rings specified, quit and go home */
469 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
470 dev_err(dev, "no queues configured, terminating\n");
471 caam_remove(pdev);
472 return -ENOMEM;
473 }
474
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530475 cha_vid_ls = rd_reg32(&topregs->ctrl.perfmon.cha_id_ls);
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530476
Kim Phillips281922a2012-06-22 19:48:52 -0500477 /*
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530478 * If SEC has RNG version >= 4 and RNG state handle has not been
Alex Porosanu84cf4822013-09-09 18:56:30 +0300479 * already instantiated, do RNG instantiation
Kim Phillips281922a2012-06-22 19:48:52 -0500480 */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530481 if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300482 ctrlpriv->rng4_sh_init =
483 rd_reg32(&topregs->ctrl.r4tst[0].rdsta);
484 /*
485 * If the secure keys (TDKEK, JDKEK, TDSK), were already
486 * generated, signal this to the function that is instantiating
487 * the state handles. An error would occur if RNG4 attempts
488 * to regenerate these keys before the next POR.
489 */
490 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
491 ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300492 do {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300493 int inst_handles =
494 rd_reg32(&topregs->ctrl.r4tst[0].rdsta) &
495 RDSTA_IFMASK;
496 /*
497 * If either SH were instantiated by somebody else
498 * (e.g. u-boot) then it is assumed that the entropy
499 * parameters are properly set and thus the function
500 * setting these (kick_trng(...)) is skipped.
501 * Also, if a handle was instantiated, do not change
502 * the TRNG parameters.
503 */
504 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
505 kick_trng(pdev, ent_delay);
506 ent_delay += 400;
507 }
508 /*
509 * if instantiate_rng(...) fails, the loop will rerun
510 * and the kick_trng(...) function will modfiy the
511 * upper and lower limits of the entropy sampling
512 * interval, leading to a sucessful initialization of
513 * the RNG.
514 */
515 ret = instantiate_rng(dev, inst_handles,
516 gen_sk);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300517 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
Kim Phillips281922a2012-06-22 19:48:52 -0500518 if (ret) {
Alex Porosanu84cf4822013-09-09 18:56:30 +0300519 dev_err(dev, "failed to instantiate RNG");
Kim Phillips281922a2012-06-22 19:48:52 -0500520 caam_remove(pdev);
521 return ret;
522 }
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300523 /*
524 * Set handles init'ed by this module as the complement of the
525 * already initialized ones
526 */
527 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
Vakul Garg575c1bd2013-03-12 13:55:21 +0530528
529 /* Enable RDB bit so that RNG works faster */
530 setbits32(&topregs->ctrl.scfgr, SCFGR_RDBENABLE);
Kim Phillips281922a2012-06-22 19:48:52 -0500531 }
532
Kim Phillips8e8ec592011-03-13 16:54:26 +0800533 /* NOTE: RTIC detection ought to go here, around Si time */
534
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530535 caam_id = (u64)rd_reg32(&topregs->ctrl.perfmon.caam_id_ms) << 32 |
536 (u64)rd_reg32(&topregs->ctrl.perfmon.caam_id_ls);
Alex Porosanu82c2f962012-07-11 11:06:11 +0800537
Kim Phillips8e8ec592011-03-13 16:54:26 +0800538 /* Report "alive" for developer to see */
Alex Porosanu82c2f962012-07-11 11:06:11 +0800539 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
Alex Porosanu883619a2014-02-06 10:27:19 +0200540 caam_get_era());
Kim Phillips8e8ec592011-03-13 16:54:26 +0800541 dev_info(dev, "job rings = %d, qi = %d\n",
542 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
543
544#ifdef CONFIG_DEBUG_FS
545 /*
546 * FIXME: needs better naming distinction, as some amalgamation of
547 * "caam" and nprop->full_name. The OF name isn't distinctive,
548 * but does separate instances
549 */
550 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
551
552 ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
553 ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
554
555 /* Controller-level - performance monitor counters */
556 ctrlpriv->ctl_rq_dequeued =
557 debugfs_create_u64("rq_dequeued",
Al Viroeda65cc2011-07-24 04:32:53 -0400558 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800559 ctrlpriv->ctl, &perfmon->req_dequeued);
560 ctrlpriv->ctl_ob_enc_req =
561 debugfs_create_u64("ob_rq_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400562 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800563 ctrlpriv->ctl, &perfmon->ob_enc_req);
564 ctrlpriv->ctl_ib_dec_req =
565 debugfs_create_u64("ib_rq_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400566 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800567 ctrlpriv->ctl, &perfmon->ib_dec_req);
568 ctrlpriv->ctl_ob_enc_bytes =
569 debugfs_create_u64("ob_bytes_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400570 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800571 ctrlpriv->ctl, &perfmon->ob_enc_bytes);
572 ctrlpriv->ctl_ob_prot_bytes =
573 debugfs_create_u64("ob_bytes_protected",
Al Viroeda65cc2011-07-24 04:32:53 -0400574 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800575 ctrlpriv->ctl, &perfmon->ob_prot_bytes);
576 ctrlpriv->ctl_ib_dec_bytes =
577 debugfs_create_u64("ib_bytes_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400578 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800579 ctrlpriv->ctl, &perfmon->ib_dec_bytes);
580 ctrlpriv->ctl_ib_valid_bytes =
581 debugfs_create_u64("ib_bytes_validated",
Al Viroeda65cc2011-07-24 04:32:53 -0400582 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800583 ctrlpriv->ctl, &perfmon->ib_valid_bytes);
584
585 /* Controller level - global status values */
586 ctrlpriv->ctl_faultaddr =
587 debugfs_create_u64("fault_addr",
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->faultaddr);
590 ctrlpriv->ctl_faultdetail =
591 debugfs_create_u32("fault_detail",
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->faultdetail);
594 ctrlpriv->ctl_faultstatus =
595 debugfs_create_u32("fault_status",
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->status);
598
599 /* Internal covering keys (useful in non-secure mode only) */
600 ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
601 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
602 ctrlpriv->ctl_kek = debugfs_create_blob("kek",
Al Viroeda65cc2011-07-24 04:32:53 -0400603 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800604 S_IRGRP | S_IROTH,
605 ctrlpriv->ctl,
606 &ctrlpriv->ctl_kek_wrap);
607
608 ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
609 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
610 ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
Al Viroeda65cc2011-07-24 04:32:53 -0400611 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800612 S_IRGRP | S_IROTH,
613 ctrlpriv->ctl,
614 &ctrlpriv->ctl_tkek_wrap);
615
616 ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
617 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
618 ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
Al Viroeda65cc2011-07-24 04:32:53 -0400619 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800620 S_IRGRP | S_IROTH,
621 ctrlpriv->ctl,
622 &ctrlpriv->ctl_tdsk_wrap);
623#endif
624 return 0;
625}
626
627static struct of_device_id caam_match[] = {
628 {
Kim Phillips54e198d2011-03-23 21:15:44 +0800629 .compatible = "fsl,sec-v4.0",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800630 },
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800631 {
632 .compatible = "fsl,sec4.0",
633 },
Kim Phillips8e8ec592011-03-13 16:54:26 +0800634 {},
635};
636MODULE_DEVICE_TABLE(of, caam_match);
637
Kim Phillips2930d492011-05-14 22:07:55 -0500638static struct platform_driver caam_driver = {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800639 .driver = {
640 .name = "caam",
641 .owner = THIS_MODULE,
642 .of_match_table = caam_match,
643 },
644 .probe = caam_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800645 .remove = caam_remove,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800646};
647
Axel Lin741e8c22011-11-26 21:26:19 +0800648module_platform_driver(caam_driver);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800649
650MODULE_LICENSE("GPL");
651MODULE_DESCRIPTION("FSL CAAM request backend");
652MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");