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