GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / mcb / mcb-lpc.c
1 /*
2  * MEN Chameleon Bus.
3  *
4  * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5  * Author: Andreas Werner <andreas.werner@men.de>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; version 2 of the License.
10  */
11
12 #include <linux/platform_device.h>
13 #include <linux/module.h>
14 #include <linux/dmi.h>
15 #include <linux/mcb.h>
16 #include <linux/io.h>
17 #include "mcb-internal.h"
18
19 struct priv {
20         struct mcb_bus *bus;
21         struct resource *mem;
22         void __iomem *base;
23 };
24
25 static int mcb_lpc_probe(struct platform_device *pdev)
26 {
27         struct resource *res;
28         struct priv *priv;
29         int ret = 0, table_size;
30
31         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
32         if (!priv)
33                 return -ENOMEM;
34
35         priv->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
36         if (!priv->mem) {
37                 dev_err(&pdev->dev, "No Memory resource\n");
38                 return -ENODEV;
39         }
40
41         res = devm_request_mem_region(&pdev->dev, priv->mem->start,
42                                       resource_size(priv->mem),
43                                       KBUILD_MODNAME);
44         if (!res) {
45                 dev_err(&pdev->dev, "Failed to request IO memory\n");
46                 return -EBUSY;
47         }
48
49         priv->base = devm_ioremap(&pdev->dev, priv->mem->start,
50                                   resource_size(priv->mem));
51         if (!priv->base) {
52                 dev_err(&pdev->dev, "Cannot ioremap\n");
53                 return -ENOMEM;
54         }
55
56         platform_set_drvdata(pdev, priv);
57
58         priv->bus = mcb_alloc_bus(&pdev->dev);
59         if (IS_ERR(priv->bus))
60                 return PTR_ERR(priv->bus);
61
62         ret = chameleon_parse_cells(priv->bus, priv->mem->start, priv->base);
63         if (ret < 0) {
64                 goto out_mcb_bus;
65         }
66
67         table_size = ret;
68
69         if (table_size < CHAM_HEADER_SIZE) {
70                 /* Release the previous resources */
71                 devm_iounmap(&pdev->dev, priv->base);
72                 devm_release_mem_region(&pdev->dev, priv->mem->start, resource_size(priv->mem));
73
74                 /* Then, allocate it again with the actual chameleon table size */
75                 res = devm_request_mem_region(&pdev->dev, priv->mem->start,
76                                               table_size,
77                                               KBUILD_MODNAME);
78                 if (!res) {
79                         dev_err(&pdev->dev, "Failed to request PCI memory\n");
80                         ret = -EBUSY;
81                         goto out_mcb_bus;
82                 }
83
84                 priv->base = devm_ioremap(&pdev->dev, priv->mem->start, table_size);
85                 if (!priv->base) {
86                         dev_err(&pdev->dev, "Cannot ioremap\n");
87                         ret = -ENOMEM;
88                         goto out_mcb_bus;
89                 }
90
91                 platform_set_drvdata(pdev, priv);
92         }
93
94         mcb_bus_add_devices(priv->bus);
95
96         return 0;
97
98 out_mcb_bus:
99         mcb_release_bus(priv->bus);
100         return ret;
101 }
102
103 static int mcb_lpc_remove(struct platform_device *pdev)
104 {
105         struct priv *priv = platform_get_drvdata(pdev);
106
107         mcb_release_bus(priv->bus);
108
109         return 0;
110 }
111
112 static struct platform_device *mcb_lpc_pdev;
113
114 static int mcb_lpc_create_platform_device(const struct dmi_system_id *id)
115 {
116         struct resource *res = id->driver_data;
117         int ret;
118
119         mcb_lpc_pdev = platform_device_alloc("mcb-lpc", -1);
120         if (!mcb_lpc_pdev)
121                 return -ENOMEM;
122
123         ret = platform_device_add_resources(mcb_lpc_pdev, res, 1);
124         if (ret)
125                 goto out_put;
126
127         ret = platform_device_add(mcb_lpc_pdev);
128         if (ret)
129                 goto out_put;
130
131         return 0;
132
133 out_put:
134         platform_device_put(mcb_lpc_pdev);
135         return ret;
136 }
137
138 static struct resource sc24_fpga_resource = {
139         .start = 0xe000e000,
140         .end = 0xe000e000 + CHAM_HEADER_SIZE,
141         .flags = IORESOURCE_MEM,
142 };
143
144 static struct resource sc31_fpga_resource = {
145         .start = 0xf000e000,
146         .end = 0xf000e000 + CHAM_HEADER_SIZE,
147         .flags = IORESOURCE_MEM,
148 };
149
150 static struct platform_driver mcb_lpc_driver = {
151         .driver         = {
152                 .name = "mcb-lpc",
153         },
154         .probe          = mcb_lpc_probe,
155         .remove         = mcb_lpc_remove,
156 };
157
158 static const struct dmi_system_id mcb_lpc_dmi_table[] = {
159         {
160                 .ident = "SC24",
161                 .matches = {
162                         DMI_MATCH(DMI_SYS_VENDOR, "MEN"),
163                         DMI_MATCH(DMI_PRODUCT_VERSION, "14SC24"),
164                 },
165                 .driver_data = (void *)&sc24_fpga_resource,
166                 .callback = mcb_lpc_create_platform_device,
167         },
168         {
169                 .ident = "SC31",
170                 .matches = {
171                         DMI_MATCH(DMI_SYS_VENDOR, "MEN"),
172                         DMI_MATCH(DMI_PRODUCT_VERSION, "14SC31"),
173                 },
174                 .driver_data = (void *)&sc31_fpga_resource,
175                 .callback = mcb_lpc_create_platform_device,
176         },
177         {}
178 };
179 MODULE_DEVICE_TABLE(dmi, mcb_lpc_dmi_table);
180
181 static int __init mcb_lpc_init(void)
182 {
183         if (!dmi_check_system(mcb_lpc_dmi_table))
184                 return -ENODEV;
185
186         return platform_driver_register(&mcb_lpc_driver);
187 }
188
189 static void __exit mcb_lpc_exit(void)
190 {
191         platform_device_unregister(mcb_lpc_pdev);
192         platform_driver_unregister(&mcb_lpc_driver);
193 }
194
195 module_init(mcb_lpc_init);
196 module_exit(mcb_lpc_exit);
197
198 MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
199 MODULE_LICENSE("GPL");
200 MODULE_DESCRIPTION("MCB over LPC support");