dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / firmware / google / coreboot_table.c
blob078d3bbe632f4cd19e3d4b1bfa392c6c25cc3e2e
1 /*
2 * coreboot_table.c
4 * Module providing coreboot table access.
6 * Copyright 2017 Google Inc.
7 * Copyright 2017 Samuel Holland <samuel@sholland.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License v2.0 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/acpi.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/io.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/of.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
30 #include "coreboot_table.h"
32 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
33 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
35 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
37 struct coreboot_device *device = CB_DEV(dev);
38 struct coreboot_driver *driver = CB_DRV(drv);
40 return device->entry.tag == driver->tag;
43 static int coreboot_bus_probe(struct device *dev)
45 int ret = -ENODEV;
46 struct coreboot_device *device = CB_DEV(dev);
47 struct coreboot_driver *driver = CB_DRV(dev->driver);
49 if (driver->probe)
50 ret = driver->probe(device);
52 return ret;
55 static int coreboot_bus_remove(struct device *dev)
57 int ret = 0;
58 struct coreboot_device *device = CB_DEV(dev);
59 struct coreboot_driver *driver = CB_DRV(dev->driver);
61 if (driver->remove)
62 ret = driver->remove(device);
64 return ret;
67 static struct bus_type coreboot_bus_type = {
68 .name = "coreboot",
69 .match = coreboot_bus_match,
70 .probe = coreboot_bus_probe,
71 .remove = coreboot_bus_remove,
74 static void coreboot_device_release(struct device *dev)
76 struct coreboot_device *device = CB_DEV(dev);
78 kfree(device);
81 int coreboot_driver_register(struct coreboot_driver *driver)
83 driver->drv.bus = &coreboot_bus_type;
85 return driver_register(&driver->drv);
87 EXPORT_SYMBOL(coreboot_driver_register);
89 void coreboot_driver_unregister(struct coreboot_driver *driver)
91 driver_unregister(&driver->drv);
93 EXPORT_SYMBOL(coreboot_driver_unregister);
95 static int coreboot_table_populate(struct device *dev, void *ptr)
97 int i, ret;
98 void *ptr_entry;
99 struct coreboot_device *device;
100 struct coreboot_table_entry *entry;
101 struct coreboot_table_header *header = ptr;
103 ptr_entry = ptr + header->header_bytes;
104 for (i = 0; i < header->table_entries; i++) {
105 entry = ptr_entry;
107 device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
108 if (!device)
109 return -ENOMEM;
111 dev_set_name(&device->dev, "coreboot%d", i);
112 device->dev.parent = dev;
113 device->dev.bus = &coreboot_bus_type;
114 device->dev.release = coreboot_device_release;
115 memcpy(&device->entry, ptr_entry, entry->size);
117 ret = device_register(&device->dev);
118 if (ret) {
119 put_device(&device->dev);
120 return ret;
123 ptr_entry += entry->size;
126 return 0;
129 static int coreboot_table_probe(struct platform_device *pdev)
131 resource_size_t len;
132 struct coreboot_table_header *header;
133 struct resource *res;
134 struct device *dev = &pdev->dev;
135 void *ptr;
136 int ret;
138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 if (!res)
140 return -EINVAL;
142 len = resource_size(res);
143 if (!res->start || !len)
144 return -EINVAL;
146 /* Check just the header first to make sure things are sane */
147 header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
148 if (!header)
149 return -ENOMEM;
151 len = header->header_bytes + header->table_bytes;
152 ret = strncmp(header->signature, "LBIO", sizeof(header->signature));
153 memunmap(header);
154 if (ret) {
155 dev_warn(dev, "coreboot table missing or corrupt!\n");
156 return -ENODEV;
159 ptr = memremap(res->start, len, MEMREMAP_WB);
160 if (!ptr)
161 return -ENOMEM;
163 ret = bus_register(&coreboot_bus_type);
164 if (!ret) {
165 ret = coreboot_table_populate(dev, ptr);
166 if (ret)
167 bus_unregister(&coreboot_bus_type);
169 memunmap(ptr);
171 return ret;
174 static int coreboot_table_remove(struct platform_device *pdev)
176 bus_unregister(&coreboot_bus_type);
177 return 0;
180 #ifdef CONFIG_ACPI
181 static const struct acpi_device_id cros_coreboot_acpi_match[] = {
182 { "GOOGCB00", 0 },
183 { "BOOT0000", 0 },
186 MODULE_DEVICE_TABLE(acpi, cros_coreboot_acpi_match);
187 #endif
189 #ifdef CONFIG_OF
190 static const struct of_device_id coreboot_of_match[] = {
191 { .compatible = "coreboot" },
194 MODULE_DEVICE_TABLE(of, coreboot_of_match);
195 #endif
197 static struct platform_driver coreboot_table_driver = {
198 .probe = coreboot_table_probe,
199 .remove = coreboot_table_remove,
200 .driver = {
201 .name = "coreboot_table",
202 .acpi_match_table = ACPI_PTR(cros_coreboot_acpi_match),
203 .of_match_table = of_match_ptr(coreboot_of_match),
206 module_platform_driver(coreboot_table_driver);
207 MODULE_AUTHOR("Google, Inc.");
208 MODULE_LICENSE("GPL");