2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2014 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/ioport.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/kthread.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <linux/delay.h>
24 #include <linux/ccp.h>
30 static int ccp_get_irq(struct ccp_device *ccp)
32 struct device *dev = ccp->dev;
33 struct platform_device *pdev = container_of(dev,
34 struct platform_device, dev);
37 ret = platform_get_irq(pdev, 0);
42 ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
44 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
51 static int ccp_get_irqs(struct ccp_device *ccp)
53 struct device *dev = ccp->dev;
56 ret = ccp_get_irq(ccp);
60 /* Couldn't get an interrupt */
61 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
66 static void ccp_free_irqs(struct ccp_device *ccp)
68 struct device *dev = ccp->dev;
70 free_irq(ccp->irq, dev);
73 static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
75 struct device *dev = ccp->dev;
76 struct platform_device *pdev = container_of(dev,
77 struct platform_device, dev);
80 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81 if (ior && (resource_size(ior) >= 0x800))
87 static int ccp_platform_probe(struct platform_device *pdev)
89 struct ccp_device *ccp;
90 struct device *dev = &pdev->dev;
95 ccp = ccp_alloc_struct(dev);
99 ccp->dev_specific = NULL;
100 ccp->get_irq = ccp_get_irqs;
101 ccp->free_irq = ccp_free_irqs;
103 ior = ccp_find_mmio_area(ccp);
104 ccp->io_map = devm_ioremap_resource(dev, ior);
105 if (IS_ERR(ccp->io_map)) {
106 ret = PTR_ERR(ccp->io_map);
109 ccp->io_regs = ccp->io_map;
112 dev->dma_mask = &dev->coherent_dma_mask;
113 *(dev->dma_mask) = DMA_BIT_MASK(48);
114 dev->coherent_dma_mask = DMA_BIT_MASK(48);
116 if (of_property_read_bool(dev->of_node, "dma-coherent"))
117 ccp->axcache = CACHE_WB_NO_ALLOC;
119 ccp->axcache = CACHE_NONE;
121 dev_set_drvdata(dev, ccp);
127 dev_notice(dev, "enabled\n");
135 dev_notice(dev, "initialization failed\n");
139 static int ccp_platform_remove(struct platform_device *pdev)
141 struct device *dev = &pdev->dev;
142 struct ccp_device *ccp = dev_get_drvdata(dev);
148 dev_notice(dev, "disabled\n");
154 static int ccp_platform_suspend(struct platform_device *pdev,
157 struct device *dev = &pdev->dev;
158 struct ccp_device *ccp = dev_get_drvdata(dev);
162 spin_lock_irqsave(&ccp->cmd_lock, flags);
166 /* Wake all the queue kthreads to prepare for suspend */
167 for (i = 0; i < ccp->cmd_q_count; i++)
168 wake_up_process(ccp->cmd_q[i].kthread);
170 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
172 /* Wait for all queue kthreads to say they're done */
173 while (!ccp_queues_suspended(ccp))
174 wait_event_interruptible(ccp->suspend_queue,
175 ccp_queues_suspended(ccp));
180 static int ccp_platform_resume(struct platform_device *pdev)
182 struct device *dev = &pdev->dev;
183 struct ccp_device *ccp = dev_get_drvdata(dev);
187 spin_lock_irqsave(&ccp->cmd_lock, flags);
191 /* Wake up all the kthreads */
192 for (i = 0; i < ccp->cmd_q_count; i++) {
193 ccp->cmd_q[i].suspended = 0;
194 wake_up_process(ccp->cmd_q[i].kthread);
197 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
203 static const struct of_device_id ccp_platform_ids[] = {
204 { .compatible = "amd,ccp-seattle-v1a" },
208 static struct platform_driver ccp_platform_driver = {
210 .name = "AMD Cryptographic Coprocessor",
211 .owner = THIS_MODULE,
212 .of_match_table = ccp_platform_ids,
214 .probe = ccp_platform_probe,
215 .remove = ccp_platform_remove,
217 .suspend = ccp_platform_suspend,
218 .resume = ccp_platform_resume,
222 int ccp_platform_init(void)
224 return platform_driver_register(&ccp_platform_driver);
227 void ccp_platform_exit(void)
229 platform_driver_unregister(&ccp_platform_driver);