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