1 1.9 thorpej /* $NetBSD: imx6_spi.c,v 1.9 2025/09/10 02:42:28 thorpej Exp $ */ 2 1.1 skrll 3 1.1 skrll /*- 4 1.1 skrll * Copyright (c) 2019 Genetec Corporation. All rights reserved. 5 1.1 skrll * Written by Hashimoto Kenichi for Genetec Corporation. 6 1.1 skrll * 7 1.1 skrll * Redistribution and use in source and binary forms, with or without 8 1.1 skrll * modification, are permitted provided that the following conditions 9 1.1 skrll * are met: 10 1.1 skrll * 1. Redistributions of source code must retain the above copyright 11 1.1 skrll * notice, this list of conditions and the following disclaimer. 12 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 skrll * notice, this list of conditions and the following disclaimer in the 14 1.1 skrll * documentation and/or other materials provided with the distribution. 15 1.1 skrll * 16 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 skrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 skrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 skrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 skrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 skrll * SUCH DAMAGE. 27 1.1 skrll */ 28 1.1 skrll 29 1.1 skrll #include <sys/cdefs.h> 30 1.9 thorpej __KERNEL_RCSID(0, "$NetBSD: imx6_spi.c,v 1.9 2025/09/10 02:42:28 thorpej Exp $"); 31 1.1 skrll 32 1.1 skrll #include "opt_imxspi.h" 33 1.1 skrll 34 1.1 skrll #include <sys/param.h> 35 1.1 skrll #include <sys/bus.h> 36 1.1 skrll #include <sys/device.h> 37 1.1 skrll #include <sys/kmem.h> 38 1.1 skrll #include <sys/gpio.h> 39 1.1 skrll 40 1.1 skrll #include <arm/imx/imxspivar.h> 41 1.1 skrll 42 1.1 skrll #include <dev/fdt/fdtvar.h> 43 1.1 skrll 44 1.1 skrll struct imxspi_fdt_softc { 45 1.1 skrll struct imxspi_softc sc_imxspi; /* Must be first */ 46 1.1 skrll 47 1.1 skrll struct spi_chipset_tag sc_tag; 48 1.1 skrll struct clk *sc_clk; 49 1.1 skrll 50 1.1 skrll struct fdtbus_gpio_pin **sc_pin_cs; 51 1.1 skrll }; 52 1.1 skrll 53 1.1 skrll struct imx_spi_config { 54 1.1 skrll bool enhanced; 55 1.1 skrll enum imxspi_type type; 56 1.1 skrll }; 57 1.1 skrll 58 1.1 skrll static const struct imx_spi_config imx6q_spi_config = { 59 1.1 skrll .enhanced = true, 60 1.1 skrll .type = IMX51_ECSPI, 61 1.1 skrll }; 62 1.1 skrll 63 1.4 thorpej static const struct device_compatible_entry compat_data[] = { 64 1.4 thorpej { .compat = "fsl,imx6q-ecspi", .data = &imx6q_spi_config }, 65 1.8 bouyer { .compat = "fsl,imx6sx-ecspi", .data = &imx6q_spi_config }, 66 1.6 thorpej DEVICE_COMPAT_EOL 67 1.1 skrll }; 68 1.1 skrll 69 1.1 skrll CFATTACH_DECL_NEW(imxspi_fdt, sizeof(struct imxspi_fdt_softc), 70 1.1 skrll imxspi_match, imxspi_attach, NULL, NULL); 71 1.1 skrll 72 1.1 skrll static int 73 1.1 skrll imxspi_cs_enable(void *arg, int slave) 74 1.1 skrll { 75 1.1 skrll struct imxspi_fdt_softc * const sc = arg; 76 1.1 skrll fdtbus_gpio_write(sc->sc_pin_cs[slave], 1); 77 1.1 skrll return 0; 78 1.1 skrll } 79 1.1 skrll 80 1.1 skrll static int 81 1.1 skrll imxspi_cs_disable(void *arg, int slave) 82 1.1 skrll { 83 1.1 skrll struct imxspi_fdt_softc * const sc = arg; 84 1.1 skrll fdtbus_gpio_write(sc->sc_pin_cs[slave], 0); 85 1.1 skrll return 0; 86 1.1 skrll } 87 1.1 skrll 88 1.1 skrll int 89 1.1 skrll imxspi_match(device_t parent, cfdata_t cf, void *aux) 90 1.1 skrll { 91 1.1 skrll struct fdt_attach_args * const faa = aux; 92 1.1 skrll 93 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 94 1.1 skrll } 95 1.1 skrll 96 1.1 skrll void 97 1.1 skrll imxspi_attach(device_t parent, device_t self, void *aux) 98 1.1 skrll { 99 1.1 skrll struct imxspi_fdt_softc * const ifsc = device_private(self); 100 1.1 skrll struct imxspi_softc * const sc = &ifsc->sc_imxspi; 101 1.1 skrll struct fdt_attach_args * const faa = aux; 102 1.1 skrll char intrstr[128]; 103 1.1 skrll const int phandle = faa->faa_phandle; 104 1.1 skrll bus_addr_t addr; 105 1.1 skrll bus_size_t size; 106 1.1 skrll int error; 107 1.1 skrll 108 1.2 skrll aprint_naive("\n"); 109 1.2 skrll aprint_normal(": SPI\n"); 110 1.2 skrll 111 1.1 skrll u_int nslaves; 112 1.1 skrll error = of_getprop_uint32(phandle, "fsl,spi-num-chipselects", &nslaves); 113 1.1 skrll if (error) 114 1.1 skrll nslaves = 4; 115 1.1 skrll 116 1.1 skrll ifsc->sc_pin_cs = kmem_alloc(sizeof(struct fdtbus_gpio_pin *) * nslaves, KM_SLEEP); 117 1.1 skrll 118 1.1 skrll for (int i = 0; i < nslaves; i++) { 119 1.1 skrll ifsc->sc_pin_cs[i] = fdtbus_gpio_acquire_index(phandle, "cs-gpios", i, 120 1.1 skrll GPIO_PIN_OUTPUT); 121 1.1 skrll } 122 1.1 skrll 123 1.1 skrll ifsc->sc_clk = fdtbus_clock_get_index(phandle, 0); 124 1.1 skrll if (ifsc->sc_clk == NULL) { 125 1.1 skrll aprint_error(": couldn't get clock\n"); 126 1.1 skrll return; 127 1.1 skrll } 128 1.1 skrll 129 1.1 skrll error = clk_enable(ifsc->sc_clk); 130 1.1 skrll if (error) { 131 1.1 skrll aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error); 132 1.1 skrll return; 133 1.1 skrll } 134 1.1 skrll 135 1.1 skrll ifsc->sc_tag.cookie = ifsc; 136 1.1 skrll ifsc->sc_tag.spi_cs_enable = imxspi_cs_enable; 137 1.1 skrll ifsc->sc_tag.spi_cs_disable = imxspi_cs_disable; 138 1.1 skrll 139 1.1 skrll sc->sc_iot = faa->faa_bst; 140 1.1 skrll 141 1.4 thorpej const struct imx_spi_config *config = 142 1.7 thorpej of_compatible_lookup(phandle, compat_data)->data; 143 1.1 skrll sc->sc_enhanced = config->enhanced; 144 1.1 skrll sc->sc_type = config->type; 145 1.1 skrll 146 1.1 skrll sc->sc_nslaves = nslaves; 147 1.1 skrll sc->sc_freq = clk_get_rate(ifsc->sc_clk); 148 1.1 skrll sc->sc_tag = &ifsc->sc_tag; 149 1.1 skrll 150 1.1 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 151 1.1 skrll aprint_error(": couldn't get iomux registers\n"); 152 1.1 skrll return; 153 1.1 skrll } 154 1.1 skrll 155 1.1 skrll if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) { 156 1.1 skrll aprint_error_dev(sc->sc_dev, "couldn't map registers\n"); 157 1.1 skrll return; 158 1.1 skrll } 159 1.1 skrll 160 1.1 skrll if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) { 161 1.1 skrll aprint_error_dev(self, "failed to decode interrupt\n"); 162 1.1 skrll return; 163 1.1 skrll } 164 1.1 skrll 165 1.3 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 166 1.3 jmcneill 0, imxspi_intr, &ifsc->sc_imxspi, device_xname(self)); 167 1.1 skrll if (sc->sc_ih == NULL) { 168 1.1 skrll aprint_error_dev(self, "couldn't establish interrupt on %s\n", 169 1.1 skrll intrstr); 170 1.1 skrll return; 171 1.1 skrll } 172 1.1 skrll aprint_normal_dev(self, "interrupting on %s\n", intrstr); 173 1.1 skrll 174 1.1 skrll imxspi_attach_common(self); 175 1.1 skrll } 176