1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/*
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
56static int
57chipc_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
77static int
78chipc_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
97static 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
104DEFINE_CLASS_1(bhnd_nvram, chipc_sprom_driver, chipc_sprom_methods, sizeof(struct bhnd_sprom_softc), bhnd_sprom_driver);
105DRIVER_MODULE(bhnd_chipc_sprom, bhnd_chipc, chipc_sprom_driver, NULL, NULL);
106
107MODULE_DEPEND(bhnd_chipc_sprom, bhnd, 1, 1, 1);
108MODULE_DEPEND(bhnd_chipc_sprom, bhnd_chipc, 1, 1, 1);
109MODULE_DEPEND(bhnd_chipc_sprom, bhnd_sprom, 1, 1, 1);
110MODULE_VERSION(bhnd_chipc_sprom, 1);
111