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 "bhnd_nvram_if.h"
50
51#include "chipcvar.h"
52#include "chipc_private.h"
53
54#define	CHIPC_VALID_SPROM_SRC(_src)	\
55	((_src) == BHND_NVRAM_SRC_SPROM || (_src) == BHND_NVRAM_SRC_OTP)
56
57static int
58chipc_sprom_probe(device_t dev)
59{
60	struct chipc_caps	*caps;
61	device_t		 chipc;
62	int			 error;
63
64	chipc = device_get_parent(dev);
65	caps = BHND_CHIPC_GET_CAPS(chipc);
66
67	/* Only match on SPROM/OTP devices */
68	if (!CHIPC_VALID_SPROM_SRC(caps->nvram_src))
69		return (ENXIO);
70
71	/* Defer to default driver implementation */
72	if ((error = bhnd_sprom_probe(dev)) > 0)
73		return (error);
74
75	return (BUS_PROBE_NOWILDCARD);
76}
77
78static int
79chipc_sprom_attach(device_t dev)
80{
81	struct chipc_caps	*caps;
82	device_t		 chipc;
83	int			 error;
84
85	chipc = device_get_parent(dev);
86	caps = BHND_CHIPC_GET_CAPS(chipc);
87
88	/* Request that ChipCommon enable access to SPROM hardware before
89	 * delegating attachment (and SPROM parsing) to the common driver */
90	if ((error = BHND_CHIPC_ENABLE_SPROM(chipc)))
91		return (error);
92
93	error = bhnd_sprom_attach(dev, caps->sprom_offset);
94	BHND_CHIPC_DISABLE_SPROM(chipc);
95	return (error);
96}
97
98static device_method_t chipc_sprom_methods[] = {
99	/* Device interface */
100	DEVMETHOD(device_probe,			chipc_sprom_probe),
101	DEVMETHOD(device_attach,		chipc_sprom_attach),
102	DEVMETHOD_END
103};
104
105DEFINE_CLASS_1(bhnd_nvram, chipc_sprom_driver, chipc_sprom_methods, sizeof(struct bhnd_sprom_softc), bhnd_sprom_driver);
106DRIVER_MODULE(bhnd_chipc_sprom, bhnd_chipc, chipc_sprom_driver, bhnd_nvram_devclass, NULL, NULL);
107
108MODULE_DEPEND(bhnd_chipc_sprom, bhnd, 1, 1, 1);
109MODULE_DEPEND(bhnd_chipc_sprom, bhnd_chipc, 1, 1, 1);
110MODULE_DEPEND(bhnd_chipc_sprom, bhnd_sprom, 1, 1, 1);
111MODULE_VERSION(bhnd_chipc_sprom, 1);
112