1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * ChipCommon SPROM driver.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/systm.h>
44 
45 #include <dev/bhnd/bhnd.h>
46 #include <dev/bhnd/nvram/bhnd_nvram.h>
47 #include <dev/bhnd/nvram/bhnd_spromvar.h>
48 
49 #include <dev/bhnd/cores/chipc/chipc.h>
50 
51 #include "bhnd_nvram_if.h"
52 
53 #define	CHIPC_VALID_SPROM_SRC(_src)	\
54 	((_src) == BHND_NVRAM_SRC_SPROM || (_src) == BHND_NVRAM_SRC_OTP)
55 
56 static int
57 chipc_sprom_probe(device_t dev)
58 {
59 	struct chipc_caps	*caps;
60 	device_t		 chipc;
61 	int			 error;
62 
63 	chipc = device_get_parent(dev);
64 	caps = BHND_CHIPC_GET_CAPS(chipc);
65 
66 	/* Only match on SPROM/OTP devices */
67 	if (!CHIPC_VALID_SPROM_SRC(caps->nvram_src))
68 		return (ENXIO);
69 
70 	/* Defer to default driver implementation */
71 	if ((error = bhnd_sprom_probe(dev)) > 0)
72 		return (error);
73 
74 	return (BUS_PROBE_NOWILDCARD);
75 }
76 
77 static int
78 chipc_sprom_attach(device_t dev)
79 {
80 	struct chipc_caps	*caps;
81 	device_t		 chipc;
82 	int			 error;
83 
84 	chipc = device_get_parent(dev);
85 	caps = BHND_CHIPC_GET_CAPS(chipc);
86 
87 	/* Request that ChipCommon enable access to SPROM hardware before
88 	 * delegating attachment (and SPROM parsing) to the common driver */
89 	if ((error = BHND_CHIPC_ENABLE_SPROM(chipc)))
90 		return (error);
91 
92 	error = bhnd_sprom_attach(dev, caps->sprom_offset);
93 	BHND_CHIPC_DISABLE_SPROM(chipc);
94 	return (error);
95 }
96 
97 static device_method_t chipc_sprom_methods[] = {
98 	/* Device interface */
99 	DEVMETHOD(device_probe,			chipc_sprom_probe),
100 	DEVMETHOD(device_attach,		chipc_sprom_attach),
101 	DEVMETHOD_END
102 };
103 
104 DEFINE_CLASS_1(bhnd_nvram, chipc_sprom_driver, chipc_sprom_methods, sizeof(struct bhnd_sprom_softc), bhnd_sprom_driver);
105 DRIVER_MODULE(bhnd_chipc_sprom, bhnd_chipc, chipc_sprom_driver, bhnd_nvram_devclass, NULL, NULL);
106 
107 MODULE_DEPEND(bhnd_chipc_sprom, bhnd, 1, 1, 1);
108 MODULE_DEPEND(bhnd_chipc_sprom, bhnd_chipc, 1, 1, 1);
109 MODULE_DEPEND(bhnd_chipc_sprom, bhnd_sprom, 1, 1, 1);
110 MODULE_VERSION(bhnd_chipc_sprom, 1);
111