Home | History | Annotate | Line # | Download | only in nxp
imx6_spi.c revision 1.3
      1  1.3  jmcneill /*	$NetBSD: imx6_spi.c,v 1.3 2021/01/15 23:58:18 jmcneill 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: imx6_spi.c,v 1.3 2021/01/15 23:58:18 jmcneill 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.1     skrll static const struct of_compat_data compat_data[] = {
     64  1.1     skrll 	{ "fsl,imx6q-ecspi",		(uintptr_t)&imx6q_spi_config },
     65  1.1     skrll 	{ NULL }
     66  1.1     skrll };
     67  1.1     skrll 
     68  1.1     skrll CFATTACH_DECL_NEW(imxspi_fdt, sizeof(struct imxspi_fdt_softc),
     69  1.1     skrll     imxspi_match, imxspi_attach, NULL, NULL);
     70  1.1     skrll 
     71  1.1     skrll static int
     72  1.1     skrll imxspi_cs_enable(void *arg, int slave)
     73  1.1     skrll {
     74  1.1     skrll 	struct imxspi_fdt_softc * const sc = arg;
     75  1.1     skrll 	fdtbus_gpio_write(sc->sc_pin_cs[slave], 1);
     76  1.1     skrll 	return 0;
     77  1.1     skrll }
     78  1.1     skrll 
     79  1.1     skrll static int
     80  1.1     skrll imxspi_cs_disable(void *arg, int slave)
     81  1.1     skrll {
     82  1.1     skrll 	struct imxspi_fdt_softc * const sc = arg;
     83  1.1     skrll 	fdtbus_gpio_write(sc->sc_pin_cs[slave], 0);
     84  1.1     skrll 	return 0;
     85  1.1     skrll }
     86  1.1     skrll 
     87  1.1     skrll int
     88  1.1     skrll imxspi_match(device_t parent, cfdata_t cf, void *aux)
     89  1.1     skrll {
     90  1.1     skrll 	struct fdt_attach_args * const faa = aux;
     91  1.1     skrll 
     92  1.1     skrll 	return of_match_compat_data(faa->faa_phandle, compat_data);
     93  1.1     skrll }
     94  1.1     skrll 
     95  1.1     skrll void
     96  1.1     skrll imxspi_attach(device_t parent, device_t self, void *aux)
     97  1.1     skrll {
     98  1.1     skrll 	struct imxspi_fdt_softc * const ifsc = device_private(self);
     99  1.1     skrll 	struct imxspi_softc * const sc = &ifsc->sc_imxspi;
    100  1.1     skrll 	struct fdt_attach_args * const faa = aux;
    101  1.1     skrll 	char intrstr[128];
    102  1.1     skrll 	const int phandle = faa->faa_phandle;
    103  1.1     skrll 	bus_addr_t addr;
    104  1.1     skrll 	bus_size_t size;
    105  1.1     skrll 	int error;
    106  1.1     skrll 
    107  1.2     skrll 	aprint_naive("\n");
    108  1.2     skrll 	aprint_normal(": SPI\n");
    109  1.2     skrll 
    110  1.1     skrll 	u_int nslaves;
    111  1.1     skrll 	error = of_getprop_uint32(phandle, "fsl,spi-num-chipselects", &nslaves);
    112  1.1     skrll 	if (error)
    113  1.1     skrll 		nslaves = 4;
    114  1.1     skrll 
    115  1.1     skrll 	ifsc->sc_pin_cs = kmem_alloc(sizeof(struct fdtbus_gpio_pin *) * nslaves, KM_SLEEP);
    116  1.1     skrll 
    117  1.1     skrll 	for (int i = 0; i < nslaves; i++) {
    118  1.1     skrll 		ifsc->sc_pin_cs[i] = fdtbus_gpio_acquire_index(phandle, "cs-gpios", i,
    119  1.1     skrll 		    GPIO_PIN_OUTPUT);
    120  1.1     skrll 	}
    121  1.1     skrll 
    122  1.1     skrll 	ifsc->sc_clk = fdtbus_clock_get_index(phandle, 0);
    123  1.1     skrll 	if (ifsc->sc_clk == NULL) {
    124  1.1     skrll 		aprint_error(": couldn't get clock\n");
    125  1.1     skrll 		return;
    126  1.1     skrll 	}
    127  1.1     skrll 
    128  1.1     skrll 	error = clk_enable(ifsc->sc_clk);
    129  1.1     skrll 	if (error) {
    130  1.1     skrll 		aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
    131  1.1     skrll 		return;
    132  1.1     skrll 	}
    133  1.1     skrll 
    134  1.1     skrll 	ifsc->sc_tag.cookie = ifsc;
    135  1.1     skrll 	ifsc->sc_tag.spi_cs_enable = imxspi_cs_enable;
    136  1.1     skrll 	ifsc->sc_tag.spi_cs_disable = imxspi_cs_disable;
    137  1.1     skrll 
    138  1.1     skrll 	sc->sc_phandle = phandle;
    139  1.1     skrll 	sc->sc_iot = faa->faa_bst;
    140  1.1     skrll 
    141  1.1     skrll 	struct imx_spi_config *config = (void *)of_search_compatible(phandle, compat_data)->data;
    142  1.1     skrll 	sc->sc_enhanced = config->enhanced;
    143  1.1     skrll 	sc->sc_type = config->type;
    144  1.1     skrll 
    145  1.1     skrll 	sc->sc_nslaves = nslaves;
    146  1.1     skrll 	sc->sc_freq = clk_get_rate(ifsc->sc_clk);
    147  1.1     skrll 	sc->sc_tag = &ifsc->sc_tag;
    148  1.1     skrll 
    149  1.1     skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    150  1.1     skrll 		aprint_error(": couldn't get iomux registers\n");
    151  1.1     skrll 		return;
    152  1.1     skrll 	}
    153  1.1     skrll 
    154  1.1     skrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
    155  1.1     skrll 		aprint_error_dev(sc->sc_dev, "couldn't map registers\n");
    156  1.1     skrll 		return;
    157  1.1     skrll 	}
    158  1.1     skrll 
    159  1.1     skrll 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    160  1.1     skrll 		aprint_error_dev(self, "failed to decode interrupt\n");
    161  1.1     skrll 		return;
    162  1.1     skrll 	}
    163  1.1     skrll 
    164  1.3  jmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
    165  1.3  jmcneill 	    0, imxspi_intr, &ifsc->sc_imxspi, device_xname(self));
    166  1.1     skrll 	if (sc->sc_ih == NULL) {
    167  1.1     skrll 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    168  1.1     skrll 		    intrstr);
    169  1.1     skrll 		return;
    170  1.1     skrll 	}
    171  1.1     skrll 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    172  1.1     skrll 
    173  1.1     skrll 	imxspi_attach_common(self);
    174  1.1     skrll }
    175