2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2014 Advanced Micro Devices, Inc.
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>
26 #include <linux/of_address.h>
27 #include <linux/acpi.h>
35 static int ccp_get_irq(struct ccp_device *ccp)
37 struct device *dev = ccp->dev;
38 struct platform_device *pdev = container_of(dev,
39 struct platform_device, dev);
42 ret = platform_get_irq(pdev, 0);
47 ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
49 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
56 static int ccp_get_irqs(struct ccp_device *ccp)
58 struct device *dev = ccp->dev;
61 ret = ccp_get_irq(ccp);
65 /* Couldn't get an interrupt */
66 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
71 static void ccp_free_irqs(struct ccp_device *ccp)
73 struct device *dev = ccp->dev;
75 free_irq(ccp->irq, dev);
78 static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
80 struct device *dev = ccp->dev;
81 struct platform_device *pdev = container_of(dev,
82 struct platform_device, dev);
85 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86 if (ior && (resource_size(ior) >= 0x800))
92 static int ccp_platform_probe(struct platform_device *pdev)
94 struct ccp_device *ccp;
95 struct ccp_platform *ccp_platform;
96 struct device *dev = &pdev->dev;
101 ccp = ccp_alloc_struct(dev);
105 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
109 ccp->dev_specific = ccp_platform;
110 ccp->get_irq = ccp_get_irqs;
111 ccp->free_irq = ccp_free_irqs;
113 ior = ccp_find_mmio_area(ccp);
114 ccp->io_map = devm_ioremap_resource(dev, ior);
115 if (IS_ERR(ccp->io_map)) {
116 ret = PTR_ERR(ccp->io_map);
119 ccp->io_regs = ccp->io_map;
121 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
123 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
127 ccp_platform->coherent = device_dma_is_coherent(ccp->dev);
128 if (ccp_platform->coherent)
129 ccp->axcache = CACHE_WB_NO_ALLOC;
131 ccp->axcache = CACHE_NONE;
133 dev_set_drvdata(dev, ccp);
139 dev_notice(dev, "enabled\n");
144 dev_notice(dev, "initialization failed\n");
148 static int ccp_platform_remove(struct platform_device *pdev)
150 struct device *dev = &pdev->dev;
151 struct ccp_device *ccp = dev_get_drvdata(dev);
155 dev_notice(dev, "disabled\n");
161 static int ccp_platform_suspend(struct platform_device *pdev,
164 struct device *dev = &pdev->dev;
165 struct ccp_device *ccp = dev_get_drvdata(dev);
169 spin_lock_irqsave(&ccp->cmd_lock, flags);
173 /* Wake all the queue kthreads to prepare for suspend */
174 for (i = 0; i < ccp->cmd_q_count; i++)
175 wake_up_process(ccp->cmd_q[i].kthread);
177 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
179 /* Wait for all queue kthreads to say they're done */
180 while (!ccp_queues_suspended(ccp))
181 wait_event_interruptible(ccp->suspend_queue,
182 ccp_queues_suspended(ccp));
187 static int ccp_platform_resume(struct platform_device *pdev)
189 struct device *dev = &pdev->dev;
190 struct ccp_device *ccp = dev_get_drvdata(dev);
194 spin_lock_irqsave(&ccp->cmd_lock, flags);
198 /* Wake up all the kthreads */
199 for (i = 0; i < ccp->cmd_q_count; i++) {
200 ccp->cmd_q[i].suspended = 0;
201 wake_up_process(ccp->cmd_q[i].kthread);
204 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
211 static const struct acpi_device_id ccp_acpi_match[] = {
215 MODULE_DEVICE_TABLE(acpi, ccp_acpi_match);
219 static const struct of_device_id ccp_of_match[] = {
220 { .compatible = "amd,ccp-seattle-v1a" },
223 MODULE_DEVICE_TABLE(of, ccp_of_match);
226 static struct platform_driver ccp_platform_driver = {
230 .acpi_match_table = ccp_acpi_match,
233 .of_match_table = ccp_of_match,
236 .probe = ccp_platform_probe,
237 .remove = ccp_platform_remove,
239 .suspend = ccp_platform_suspend,
240 .resume = ccp_platform_resume,
244 int ccp_platform_init(void)
246 return platform_driver_register(&ccp_platform_driver);
249 void ccp_platform_exit(void)
251 platform_driver_unregister(&ccp_platform_driver);