1/*-
2 * Copyright (c) 2017, Adrian Chadd <adrian@FreeBSD.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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include "opt_ar71xx.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35
36#include <sys/bus.h>
37#include <sys/interrupt.h>
38#include <sys/malloc.h>
39#include <sys/kernel.h>
40#include <sys/module.h>
41#include <sys/rman.h>
42#include <sys/lock.h>
43
44#include <vm/vm.h>
45#include <vm/pmap.h>
46#include <vm/vm_extern.h>
47
48#include <machine/bus.h>
49#include <machine/cpu.h>
50#include <machine/intr_machdep.h>
51
52#include <sys/linker.h>
53#include <sys/firmware.h>
54
55struct ar71xx_caldata_softc {
56	device_t		sc_dev;
57};
58
59static int
60ar71xx_caldata_probe(device_t dev)
61{
62
63	return (BUS_PROBE_NOWILDCARD);
64}
65
66/* XXX TODO: unify with what's in ar71xx_fixup.c */
67
68/*
69 * Create a calibration block from memory mapped SPI data for use by
70 * various drivers.  Right now it's just ath(4) but later board support
71 * will include 802.11ac NICs with calibration data in NOR flash.
72 *
73 * (Yes, there are a handful of QCA MIPS boards with QCA9880v2 802.11ac chips
74 * with calibration data in flash..)
75 */
76static void
77ar71xx_platform_create_cal_data(device_t dev, int id, long int flash_addr,
78    int size)
79{
80	char buf[64];
81	uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
82	void *eeprom = NULL;
83	const struct firmware *fw = NULL;
84
85	device_printf(dev, "EEPROM firmware: 0x%lx @ %d bytes\n",
86	    flash_addr, size);
87
88	eeprom = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
89	if (! eeprom) {
90		device_printf(dev, "%s: malloc failed for '%s', aborting EEPROM\n",
91		__func__, buf);
92		return;
93	}
94
95	memcpy(eeprom, cal_data, size);
96
97	/*
98	 * Generate a flash EEPROM 'firmware' from the given memory
99	 * region.  Since the SPI controller will eventually
100	 * go into port-IO mode instead of memory-mapped IO
101	 * mode, a copy of the EEPROM contents is required.
102	 */
103
104	snprintf(buf, sizeof(buf), "%s.%d.map.%d.eeprom_firmware",
105	    device_get_name(dev),
106	    device_get_unit(dev),
107	    id);
108
109	fw = firmware_register(buf, eeprom, size, 1, NULL);
110	if (fw == NULL) {
111		device_printf(dev, "%s: firmware_register (%s) failed\n",
112		    __func__, buf);
113		free(eeprom, M_DEVBUF);
114		return;
115	}
116	device_printf(dev, "device EEPROM '%s' registered\n", buf);
117}
118
119/*
120 * Iterate through a list of early-boot hints creating calibration
121 * data firmware chunks for AHB (ie, non-PCI) devices with calibration
122 * data.
123 */
124static int
125ar71xx_platform_check_eeprom_hints(device_t dev)
126{
127	char buf[64];
128	long int addr;
129	int size;
130	int i;
131
132	for (i = 0; i < 8; i++) {
133		snprintf(buf, sizeof(buf), "map.%d.ath_fixup_addr", i);
134		if (resource_long_value(device_get_name(dev),
135		    device_get_unit(dev), buf, &addr) != 0)
136			break;
137		snprintf(buf, sizeof(buf), "map.%d.ath_fixup_size", i);
138		if (resource_int_value(device_get_name(dev),
139		    device_get_unit(dev), buf, &size) != 0)
140			break;
141		device_printf(dev, "map.%d.ath_fixup_addr=0x%08x; size=%d\n",
142		    i, (int) addr, size);
143		(void) ar71xx_platform_create_cal_data(dev, i, addr, size);
144        }
145
146        return (0);
147}
148
149static int
150ar71xx_caldata_attach(device_t dev)
151{
152
153	device_add_child(dev, "nexus", -1);
154	ar71xx_platform_check_eeprom_hints(dev);
155	return (bus_generic_attach(dev));
156}
157
158static device_method_t ar71xx_caldata_methods[] = {
159	/* Device interface */
160	DEVMETHOD(device_probe,		ar71xx_caldata_probe),
161	DEVMETHOD(device_attach,	ar71xx_caldata_attach),
162	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
163	DEVMETHOD(device_suspend,	bus_generic_suspend),
164	DEVMETHOD(device_resume,	bus_generic_resume),
165	DEVMETHOD_END
166};
167
168static driver_t ar71xx_caldata_driver = {
169	"ar71xx_caldata",
170	ar71xx_caldata_methods,
171	sizeof(struct ar71xx_caldata_softc),
172};
173
174static devclass_t ar71xx_caldata_devclass;
175
176DRIVER_MODULE(ar71xx_caldata, nexus, ar71xx_caldata_driver, ar71xx_caldata_devclass, 0, 0);
177