2 * Copyright (c) 2017, Adrian Chadd <adrian@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_ar71xx.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/interrupt.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
46 #include <vm/vm_extern.h>
48 #include <machine/bus.h>
49 #include <machine/cpu.h>
50 #include <machine/intr_machdep.h>
52 #include <sys/linker.h>
53 #include <sys/firmware.h>
55 struct ar71xx_caldata_softc {
60 ar71xx_caldata_probe(device_t dev)
63 return (BUS_PROBE_NOWILDCARD);
66 /* XXX TODO: unify with what's in ar71xx_fixup.c */
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.
73 * (Yes, there are a handful of QCA MIPS boards with QCA9880v2 802.11ac chips
74 * with calibration data in flash..)
77 ar71xx_platform_create_cal_data(device_t dev, int id, long int flash_addr,
81 uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
83 const struct firmware *fw = NULL;
85 device_printf(dev, "EEPROM firmware: 0x%lx @ %d bytes\n",
88 eeprom = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
90 device_printf(dev, "%s: malloc failed for '%s', aborting EEPROM\n",
95 memcpy(eeprom, cal_data, size);
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.
104 snprintf(buf, sizeof(buf), "%s.%d.map.%d.eeprom_firmware",
105 device_get_name(dev),
106 device_get_unit(dev),
109 fw = firmware_register(buf, eeprom, size, 1, NULL);
111 device_printf(dev, "%s: firmware_register (%s) failed\n",
113 free(eeprom, M_DEVBUF);
116 device_printf(dev, "device EEPROM '%s' registered\n", buf);
120 * Iterate through a list of early-boot hints creating calibration
121 * data firmware chunks for AHB (ie, non-PCI) devices with calibration
125 ar71xx_platform_check_eeprom_hints(device_t dev)
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)
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)
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);
150 ar71xx_caldata_attach(device_t dev)
153 device_add_child(dev, "nexus", -1);
154 ar71xx_platform_check_eeprom_hints(dev);
155 return (bus_generic_attach(dev));
158 static 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),
168 static driver_t ar71xx_caldata_driver = {
170 ar71xx_caldata_methods,
171 sizeof(struct ar71xx_caldata_softc),
174 static devclass_t ar71xx_caldata_devclass;
176 DRIVER_MODULE(ar71xx_caldata, nexus, ar71xx_caldata_driver, ar71xx_caldata_devclass, 0, 0);