blob: 9a2db9c30630e6c2caf1de55190a9e67ec9e8264 [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 *
5 * Copyright 2008-2011 Freescale Semiconductor, Inc.
6 */
7
8#include "compat.h"
9#include "regs.h"
10#include "intern.h"
11#include "jr.h"
12
13static int caam_remove(struct platform_device *pdev)
14{
15 struct device *ctrldev;
16 struct caam_drv_private *ctrlpriv;
17 struct caam_drv_private_jr *jrpriv;
18 struct caam_full __iomem *topregs;
19 int ring, ret = 0;
20
21 ctrldev = &pdev->dev;
22 ctrlpriv = dev_get_drvdata(ctrldev);
23 topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
24
25 /* shut down JobRs */
26 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
27 ret |= caam_jr_shutdown(ctrlpriv->jrdev[ring]);
28 jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
29 irq_dispose_mapping(jrpriv->irq);
30 }
31
32 /* Shut down debug views */
33#ifdef CONFIG_DEBUG_FS
34 debugfs_remove_recursive(ctrlpriv->dfs_root);
35#endif
36
37 /* Unmap controller region */
38 iounmap(&topregs->ctrl);
39
40 kfree(ctrlpriv->jrdev);
41 kfree(ctrlpriv);
42
43 return ret;
44}
45
46/* Probe routine for CAAM top (controller) level */
Kim Phillips2930d492011-05-14 22:07:55 -050047static int caam_probe(struct platform_device *pdev)
Kim Phillips8e8ec592011-03-13 16:54:26 +080048{
Kim Phillips65a4a572012-01-18 18:34:34 -060049 int ring, rspec;
Kim Phillips8e8ec592011-03-13 16:54:26 +080050 struct device *dev;
51 struct device_node *nprop, *np;
52 struct caam_ctrl __iomem *ctrl;
53 struct caam_full __iomem *topregs;
54 struct caam_drv_private *ctrlpriv;
Kim Phillips23457bc2011-06-05 16:42:54 -050055#ifdef CONFIG_DEBUG_FS
56 struct caam_perfmon *perfmon;
57#endif
Kim Phillips8e8ec592011-03-13 16:54:26 +080058
59 ctrlpriv = kzalloc(sizeof(struct caam_drv_private), GFP_KERNEL);
60 if (!ctrlpriv)
61 return -ENOMEM;
62
63 dev = &pdev->dev;
64 dev_set_drvdata(dev, ctrlpriv);
65 ctrlpriv->pdev = pdev;
66 nprop = pdev->dev.of_node;
67
68 /* Get configuration properties from device tree */
69 /* First, get register page */
70 ctrl = of_iomap(nprop, 0);
71 if (ctrl == NULL) {
72 dev_err(dev, "caam: of_iomap() failed\n");
73 return -ENOMEM;
74 }
75 ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
76
77 /* topregs used to derive pointers to CAAM sub-blocks only */
78 topregs = (struct caam_full __iomem *)ctrl;
79
80 /* Get the IRQ of the controller (for security violations only) */
81 ctrlpriv->secvio_irq = of_irq_to_resource(nprop, 0, NULL);
82
83 /*
84 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
Kim Phillipse13af182012-06-22 19:48:51 -050085 * long pointers in master configuration register
Kim Phillips8e8ec592011-03-13 16:54:26 +080086 */
87 setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
88 (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
89
90 if (sizeof(dma_addr_t) == sizeof(u64))
Kim Phillipse13af182012-06-22 19:48:51 -050091 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
92 dma_set_mask(dev, DMA_BIT_MASK(40));
93 else
94 dma_set_mask(dev, DMA_BIT_MASK(36));
95 else
96 dma_set_mask(dev, DMA_BIT_MASK(32));
Kim Phillips8e8ec592011-03-13 16:54:26 +080097
Kim Phillips8e8ec592011-03-13 16:54:26 +080098 /*
99 * Detect and enable JobRs
100 * First, find out how many ring spec'ed, allocate references
101 * for all, then go probe each one.
102 */
103 rspec = 0;
Kim Phillips54e198d2011-03-23 21:15:44 +0800104 for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring")
Kim Phillips8e8ec592011-03-13 16:54:26 +0800105 rspec++;
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800106 if (!rspec) {
107 /* for backward compatible with device trees */
108 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring")
109 rspec++;
110 }
111
Kim Phillips8e8ec592011-03-13 16:54:26 +0800112 ctrlpriv->jrdev = kzalloc(sizeof(struct device *) * rspec, GFP_KERNEL);
113 if (ctrlpriv->jrdev == NULL) {
114 iounmap(&topregs->ctrl);
115 return -ENOMEM;
116 }
117
118 ring = 0;
119 ctrlpriv->total_jobrs = 0;
Kim Phillips54e198d2011-03-23 21:15:44 +0800120 for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring") {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800121 caam_jr_probe(pdev, np, ring);
122 ctrlpriv->total_jobrs++;
123 ring++;
124 }
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800125 if (!ring) {
126 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring") {
127 caam_jr_probe(pdev, np, ring);
128 ctrlpriv->total_jobrs++;
129 ring++;
130 }
131 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800132
133 /* Check to see if QI present. If so, enable */
134 ctrlpriv->qi_present = !!(rd_reg64(&topregs->ctrl.perfmon.comp_parms) &
135 CTPR_QI_MASK);
136 if (ctrlpriv->qi_present) {
137 ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
138 /* This is all that's required to physically enable QI */
139 wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
140 }
141
142 /* If no QI and no rings specified, quit and go home */
143 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
144 dev_err(dev, "no queues configured, terminating\n");
145 caam_remove(pdev);
146 return -ENOMEM;
147 }
148
149 /* NOTE: RTIC detection ought to go here, around Si time */
150
151 /* Initialize queue allocator lock */
152 spin_lock_init(&ctrlpriv->jr_alloc_lock);
153
154 /* Report "alive" for developer to see */
155 dev_info(dev, "device ID = 0x%016llx\n",
156 rd_reg64(&topregs->ctrl.perfmon.caam_id));
157 dev_info(dev, "job rings = %d, qi = %d\n",
158 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
159
160#ifdef CONFIG_DEBUG_FS
161 /*
162 * FIXME: needs better naming distinction, as some amalgamation of
163 * "caam" and nprop->full_name. The OF name isn't distinctive,
164 * but does separate instances
165 */
166 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
167
168 ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
169 ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
170
171 /* Controller-level - performance monitor counters */
172 ctrlpriv->ctl_rq_dequeued =
173 debugfs_create_u64("rq_dequeued",
Al Viroeda65cc2011-07-24 04:32:53 -0400174 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800175 ctrlpriv->ctl, &perfmon->req_dequeued);
176 ctrlpriv->ctl_ob_enc_req =
177 debugfs_create_u64("ob_rq_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400178 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800179 ctrlpriv->ctl, &perfmon->ob_enc_req);
180 ctrlpriv->ctl_ib_dec_req =
181 debugfs_create_u64("ib_rq_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400182 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800183 ctrlpriv->ctl, &perfmon->ib_dec_req);
184 ctrlpriv->ctl_ob_enc_bytes =
185 debugfs_create_u64("ob_bytes_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400186 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800187 ctrlpriv->ctl, &perfmon->ob_enc_bytes);
188 ctrlpriv->ctl_ob_prot_bytes =
189 debugfs_create_u64("ob_bytes_protected",
Al Viroeda65cc2011-07-24 04:32:53 -0400190 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800191 ctrlpriv->ctl, &perfmon->ob_prot_bytes);
192 ctrlpriv->ctl_ib_dec_bytes =
193 debugfs_create_u64("ib_bytes_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400194 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800195 ctrlpriv->ctl, &perfmon->ib_dec_bytes);
196 ctrlpriv->ctl_ib_valid_bytes =
197 debugfs_create_u64("ib_bytes_validated",
Al Viroeda65cc2011-07-24 04:32:53 -0400198 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800199 ctrlpriv->ctl, &perfmon->ib_valid_bytes);
200
201 /* Controller level - global status values */
202 ctrlpriv->ctl_faultaddr =
203 debugfs_create_u64("fault_addr",
Al Viroeda65cc2011-07-24 04:32:53 -0400204 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800205 ctrlpriv->ctl, &perfmon->faultaddr);
206 ctrlpriv->ctl_faultdetail =
207 debugfs_create_u32("fault_detail",
Al Viroeda65cc2011-07-24 04:32:53 -0400208 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800209 ctrlpriv->ctl, &perfmon->faultdetail);
210 ctrlpriv->ctl_faultstatus =
211 debugfs_create_u32("fault_status",
Al Viroeda65cc2011-07-24 04:32:53 -0400212 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800213 ctrlpriv->ctl, &perfmon->status);
214
215 /* Internal covering keys (useful in non-secure mode only) */
216 ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
217 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
218 ctrlpriv->ctl_kek = debugfs_create_blob("kek",
Al Viroeda65cc2011-07-24 04:32:53 -0400219 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800220 S_IRGRP | S_IROTH,
221 ctrlpriv->ctl,
222 &ctrlpriv->ctl_kek_wrap);
223
224 ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
225 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
226 ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
Al Viroeda65cc2011-07-24 04:32:53 -0400227 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800228 S_IRGRP | S_IROTH,
229 ctrlpriv->ctl,
230 &ctrlpriv->ctl_tkek_wrap);
231
232 ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
233 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
234 ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
Al Viroeda65cc2011-07-24 04:32:53 -0400235 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800236 S_IRGRP | S_IROTH,
237 ctrlpriv->ctl,
238 &ctrlpriv->ctl_tdsk_wrap);
239#endif
240 return 0;
241}
242
243static struct of_device_id caam_match[] = {
244 {
Kim Phillips54e198d2011-03-23 21:15:44 +0800245 .compatible = "fsl,sec-v4.0",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800246 },
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800247 {
248 .compatible = "fsl,sec4.0",
249 },
Kim Phillips8e8ec592011-03-13 16:54:26 +0800250 {},
251};
252MODULE_DEVICE_TABLE(of, caam_match);
253
Kim Phillips2930d492011-05-14 22:07:55 -0500254static struct platform_driver caam_driver = {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800255 .driver = {
256 .name = "caam",
257 .owner = THIS_MODULE,
258 .of_match_table = caam_match,
259 },
260 .probe = caam_probe,
261 .remove = __devexit_p(caam_remove),
262};
263
Axel Lin741e8c22011-11-26 21:26:19 +0800264module_platform_driver(caam_driver);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800265
266MODULE_LICENSE("GPL");
267MODULE_DESCRIPTION("FSL CAAM request backend");
268MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");