Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.34
      1  1.34   thorpej /* $NetBSD: spi.c,v 1.34 2025/09/13 17:51:08 thorpej Exp $ */
      2   1.1   gdamore 
      3   1.1   gdamore /*-
      4   1.1   gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5   1.1   gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6   1.1   gdamore  * All rights reserved.
      7   1.1   gdamore  *
      8   1.1   gdamore  * Portions of this code were written by Garrett D'Amore for the
      9   1.1   gdamore  * Champaign-Urbana Community Wireless Network Project.
     10   1.1   gdamore  *
     11   1.1   gdamore  * Redistribution and use in source and binary forms, with or
     12   1.1   gdamore  * without modification, are permitted provided that the following
     13   1.1   gdamore  * conditions are met:
     14   1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     15   1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     16   1.1   gdamore  * 2. Redistributions in binary form must reproduce the above
     17   1.1   gdamore  *    copyright notice, this list of conditions and the following
     18   1.1   gdamore  *    disclaimer in the documentation and/or other materials provided
     19   1.1   gdamore  *    with the distribution.
     20   1.1   gdamore  * 3. All advertising materials mentioning features or use of this
     21   1.1   gdamore  *    software must display the following acknowledgements:
     22   1.1   gdamore  *      This product includes software developed by the Urbana-Champaign
     23   1.1   gdamore  *      Independent Media Center.
     24   1.1   gdamore  *	This product includes software developed by Garrett D'Amore.
     25   1.1   gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26   1.1   gdamore  *    D'Amore's name may not be used to endorse or promote products
     27   1.1   gdamore  *    derived from this software without specific prior written permission.
     28   1.1   gdamore  *
     29   1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30   1.1   gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31   1.1   gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32   1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33   1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34   1.1   gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35   1.1   gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36   1.1   gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37   1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38   1.1   gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1   gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40   1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41   1.1   gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42   1.1   gdamore  */
     43   1.1   gdamore 
     44  1.27   thorpej #include "opt_fdt.h"		/* XXX */
     45  1.27   thorpej 
     46   1.1   gdamore #include <sys/cdefs.h>
     47  1.34   thorpej __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.34 2025/09/13 17:51:08 thorpej Exp $");
     48   1.1   gdamore 
     49   1.1   gdamore #include "locators.h"
     50   1.1   gdamore 
     51   1.1   gdamore #include <sys/param.h>
     52   1.1   gdamore #include <sys/systm.h>
     53   1.1   gdamore #include <sys/device.h>
     54  1.10   mlelstv #include <sys/conf.h>
     55   1.1   gdamore #include <sys/malloc.h>
     56   1.5     rmind #include <sys/mutex.h>
     57   1.5     rmind #include <sys/condvar.h>
     58   1.1   gdamore #include <sys/errno.h>
     59   1.1   gdamore 
     60   1.1   gdamore #include <dev/spi/spivar.h>
     61  1.10   mlelstv #include <dev/spi/spi_io.h>
     62  1.10   mlelstv 
     63  1.27   thorpej #ifdef FDT
     64  1.27   thorpej #include <dev/fdt/fdt_spi.h>	/* XXX */
     65  1.29   thorpej #include <dev/ofw/openfirm.h>	/* XXX */
     66  1.27   thorpej #endif
     67  1.27   thorpej 
     68  1.10   mlelstv #include "ioconf.h"
     69  1.10   mlelstv #include "locators.h"
     70   1.1   gdamore 
     71   1.1   gdamore struct spi_softc {
     72  1.22   thorpej 	device_t		sc_dev;
     73  1.28   thorpej 	const struct spi_controller *sc_controller;
     74   1.1   gdamore 	int			sc_mode;
     75   1.1   gdamore 	int			sc_speed;
     76  1.10   mlelstv 	int			sc_slave;
     77   1.1   gdamore 	int			sc_nslaves;
     78  1.33   thorpej 	spi_handle_t		sc_slaves;
     79  1.34   thorpej 	kmutex_t		sc_slave_state_lock;
     80  1.10   mlelstv 	kmutex_t		sc_lock;
     81  1.10   mlelstv 	kcondvar_t		sc_cv;
     82  1.18   mlelstv 	kmutex_t		sc_dev_lock;
     83  1.10   mlelstv 	int			sc_flags;
     84  1.10   mlelstv #define SPIC_BUSY		1
     85  1.10   mlelstv };
     86  1.10   mlelstv 
     87  1.10   mlelstv static dev_type_open(spi_open);
     88  1.10   mlelstv static dev_type_close(spi_close);
     89  1.10   mlelstv static dev_type_ioctl(spi_ioctl);
     90  1.10   mlelstv 
     91  1.10   mlelstv const struct cdevsw spi_cdevsw = {
     92  1.10   mlelstv 	.d_open = spi_open,
     93  1.10   mlelstv 	.d_close = spi_close,
     94  1.10   mlelstv 	.d_read = noread,
     95  1.10   mlelstv 	.d_write = nowrite,
     96  1.10   mlelstv 	.d_ioctl = spi_ioctl,
     97  1.10   mlelstv 	.d_stop = nostop,
     98  1.10   mlelstv 	.d_tty = notty,
     99  1.10   mlelstv 	.d_poll = nopoll,
    100  1.10   mlelstv 	.d_mmap = nommap,
    101  1.10   mlelstv 	.d_kqfilter = nokqfilter,
    102  1.10   mlelstv 	.d_discard = nodiscard,
    103  1.18   mlelstv 	.d_flag = D_OTHER | D_MPSAFE
    104   1.1   gdamore };
    105   1.1   gdamore 
    106   1.1   gdamore /*
    107   1.1   gdamore  * SPI slave device.  We have one of these per slave.
    108   1.1   gdamore  */
    109   1.1   gdamore struct spi_handle {
    110  1.34   thorpej 	struct spi_softc	*sh_sc;		    /* static */
    111  1.34   thorpej 	const struct spi_controller *sh_controller; /* static */
    112  1.34   thorpej 	int			sh_slave;	    /* static */
    113  1.34   thorpej 	int			sh_mode;	/* locked by owning child */
    114  1.34   thorpej 	int			sh_speed;	/* locked by owning child */
    115  1.34   thorpej 	int			sh_flags;	/* vv slave_state_lock vv */
    116  1.34   thorpej #define SPIH_ATTACHED		__BIT(0)
    117  1.34   thorpej #define	SPIH_DIRECT		__BIT(1)
    118  1.34   thorpej 	device_t		sh_dev;		/* ^^ slave_state_lock ^^ */
    119   1.1   gdamore };
    120   1.1   gdamore 
    121  1.10   mlelstv #define SPI_MAXDATA 4096
    122  1.10   mlelstv 
    123   1.1   gdamore /*
    124   1.1   gdamore  * API for bus drivers.
    125   1.1   gdamore  */
    126   1.1   gdamore 
    127   1.1   gdamore int
    128   1.1   gdamore spibus_print(void *aux, const char *pnp)
    129   1.1   gdamore {
    130   1.1   gdamore 
    131   1.1   gdamore 	if (pnp != NULL)
    132   1.1   gdamore 		aprint_normal("spi at %s", pnp);
    133   1.1   gdamore 
    134   1.1   gdamore 	return (UNCONF);
    135   1.1   gdamore }
    136   1.1   gdamore 
    137   1.1   gdamore 
    138   1.1   gdamore static int
    139   1.3   xtraeme spi_match(device_t parent, cfdata_t cf, void *aux)
    140   1.1   gdamore {
    141   1.5     rmind 
    142   1.1   gdamore 	return 1;
    143   1.1   gdamore }
    144   1.1   gdamore 
    145   1.1   gdamore static int
    146   1.1   gdamore spi_print(void *aux, const char *pnp)
    147   1.1   gdamore {
    148   1.1   gdamore 	struct spi_attach_args *sa = aux;
    149   1.1   gdamore 
    150   1.1   gdamore 	if (sa->sa_handle->sh_slave != -1)
    151   1.1   gdamore 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    152   1.1   gdamore 
    153   1.1   gdamore 	return (UNCONF);
    154   1.1   gdamore }
    155   1.1   gdamore 
    156  1.34   thorpej static void
    157  1.34   thorpej spi_attach_child(struct spi_softc *sc, struct spi_attach_args *sa,
    158  1.34   thorpej     int chip_select, cfdata_t cf)
    159  1.34   thorpej {
    160  1.34   thorpej 	spi_handle_t sh;
    161  1.34   thorpej 	device_t newdev = NULL;
    162  1.34   thorpej 	bool is_direct = cf == NULL;
    163  1.34   thorpej 	const int skip_flags = is_direct ? SPIH_ATTACHED
    164  1.34   thorpej 					 : (SPIH_ATTACHED | SPIH_DIRECT);
    165  1.34   thorpej 	const int claim_flags = skip_flags ^ SPIH_DIRECT;
    166  1.34   thorpej 	int locs[SPICF_NLOCS] = { 0 };
    167  1.34   thorpej 
    168  1.34   thorpej 	if (chip_select < 0 ||
    169  1.34   thorpej 	    chip_select >= sc->sc_controller->sct_nslaves) {
    170  1.34   thorpej 		return;
    171  1.34   thorpej 	}
    172  1.34   thorpej 
    173  1.34   thorpej 	sh = &sc->sc_slaves[chip_select];
    174  1.34   thorpej 
    175  1.34   thorpej 	mutex_enter(&sc->sc_slave_state_lock);
    176  1.34   thorpej 	if (ISSET(sh->sh_flags, skip_flags)) {
    177  1.34   thorpej 		mutex_exit(&sc->sc_slave_state_lock);
    178  1.34   thorpej 		return;
    179  1.34   thorpej 	}
    180  1.34   thorpej 
    181  1.34   thorpej 	/* Keep others off of this chip select. */
    182  1.34   thorpej 	SET(sh->sh_flags, claim_flags);
    183  1.34   thorpej 	mutex_exit(&sc->sc_slave_state_lock);
    184  1.34   thorpej 
    185  1.34   thorpej 	locs[SPICF_SLAVE] = chip_select;
    186  1.34   thorpej 	sa->sa_handle = sh;
    187  1.34   thorpej 
    188  1.34   thorpej 	if (is_direct) {
    189  1.34   thorpej 		newdev = config_found(sc->sc_dev, sa, spi_print,
    190  1.34   thorpej 		    CFARGS(.submatch = config_stdsubmatch,
    191  1.34   thorpej 			   .locators = locs,
    192  1.34   thorpej 			   .devhandle = sa->sa_devhandle));
    193  1.34   thorpej 	} else {
    194  1.34   thorpej 		if (config_probe(sc->sc_dev, cf, &sa)) {
    195  1.34   thorpej 			newdev = config_attach(sc->sc_dev, cf, &sa, spi_print,
    196  1.34   thorpej 			    CFARGS(.locators = locs));
    197  1.34   thorpej 		}
    198  1.34   thorpej 	}
    199  1.34   thorpej 
    200  1.34   thorpej 	mutex_enter(&sc->sc_slave_state_lock);
    201  1.34   thorpej 	if (newdev == NULL) {
    202  1.34   thorpej 		/*
    203  1.34   thorpej 		 * Clear our claim on this chip select (yes, just
    204  1.34   thorpej 		 * the ATTACHED flag; we want to keep indirects off
    205  1.34   thorpej 		 * of chip selects for which there is a device tree
    206  1.34   thorpej 		 * node).
    207  1.34   thorpej 		 */
    208  1.34   thorpej 		CLR(sh->sh_flags, SPIH_ATTACHED);
    209  1.34   thorpej 	} else {
    210  1.34   thorpej 		/* Record the child for posterity. */
    211  1.34   thorpej 		sh->sh_dev = newdev;
    212  1.34   thorpej 	}
    213  1.34   thorpej 	mutex_exit(&sc->sc_slave_state_lock);
    214  1.34   thorpej }
    215  1.34   thorpej 
    216   1.1   gdamore static int
    217   1.3   xtraeme spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    218   1.1   gdamore {
    219   1.3   xtraeme 	struct spi_softc *sc = device_private(parent);
    220   1.1   gdamore 
    221  1.34   thorpej 	if (cf->cf_loc[SPICF_SLAVE] == SPICF_SLAVE_DEFAULT) {
    222  1.34   thorpej 		/* No wildcards for indirect on SPI. */
    223  1.34   thorpej 		return 0;
    224   1.1   gdamore 	}
    225   1.1   gdamore 
    226  1.34   thorpej 	struct spi_attach_args sa = { 0 };
    227  1.34   thorpej 	spi_attach_child(sc, &sa, cf->cf_loc[SPICF_SLAVE], cf);
    228   1.1   gdamore 
    229   1.1   gdamore 	return 0;
    230   1.1   gdamore }
    231   1.1   gdamore 
    232   1.1   gdamore /*
    233  1.12       tnn  * XXX this is the same as i2c_fill_compat. It could be refactored into a
    234  1.12       tnn  * common fill_compat function with pointers to compat & ncompat instead
    235  1.12       tnn  * of attach_args as the first parameter.
    236  1.12       tnn  */
    237  1.12       tnn static void
    238  1.12       tnn spi_fill_compat(struct spi_attach_args *sa, const char *compat, size_t len,
    239  1.12       tnn 	char **buffer)
    240  1.12       tnn {
    241  1.12       tnn 	int count, i;
    242  1.12       tnn 	const char *c, *start, **ptr;
    243  1.12       tnn 
    244  1.12       tnn 	*buffer = NULL;
    245  1.12       tnn 	for (i = count = 0, c = compat; i < len; i++, c++)
    246  1.12       tnn 		if (*c == 0)
    247  1.12       tnn 			count++;
    248  1.12       tnn 	count += 2;
    249  1.12       tnn 	ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
    250  1.12       tnn 	if (!ptr)
    251  1.12       tnn 		return;
    252  1.12       tnn 
    253  1.12       tnn 	for (i = count = 0, start = c = compat; i < len; i++, c++) {
    254  1.12       tnn 		if (*c == 0) {
    255  1.12       tnn 			ptr[count++] = start;
    256  1.12       tnn 			start = c + 1;
    257  1.12       tnn 		}
    258  1.12       tnn 	}
    259  1.12       tnn 	if (start < compat + len) {
    260  1.12       tnn 		/* last string not 0 terminated */
    261  1.12       tnn 		size_t l = c - start;
    262  1.12       tnn 		*buffer = malloc(l + 1, M_TEMP, M_WAITOK);
    263  1.12       tnn 		memcpy(*buffer, start, l);
    264  1.12       tnn 		(*buffer)[l] = 0;
    265  1.12       tnn 		ptr[count++] = *buffer;
    266  1.12       tnn 	}
    267  1.12       tnn 	ptr[count] = NULL;
    268  1.12       tnn 
    269  1.12       tnn 	sa->sa_compat = ptr;
    270  1.12       tnn 	sa->sa_ncompat = count;
    271  1.12       tnn }
    272  1.12       tnn 
    273  1.12       tnn static void
    274  1.29   thorpej spi_direct_attach_child_devices(struct spi_softc *sc)
    275  1.12       tnn {
    276  1.12       tnn 	unsigned int count;
    277  1.12       tnn 	prop_dictionary_t child;
    278  1.29   thorpej 	prop_array_t child_devices;
    279  1.12       tnn 	prop_data_t cdata;
    280  1.30   thorpej 	devhandle_t parent_handle = device_handle(sc->sc_dev);
    281  1.30   thorpej 	devhandle_t child_handle;
    282  1.34   thorpej 	uint32_t chip_select;
    283  1.12       tnn 	uint64_t cookie;
    284  1.12       tnn 	struct spi_attach_args sa;
    285  1.12       tnn 	int loc[SPICF_NLOCS];
    286  1.12       tnn 	char *buf;
    287  1.12       tnn 	int i;
    288  1.12       tnn 
    289  1.29   thorpej 	/* XXX A better way is coming, I promise... */
    290  1.30   thorpej 	switch (devhandle_type(parent_handle)) {
    291  1.29   thorpej #ifdef FDT
    292  1.29   thorpej 	case DEVHANDLE_TYPE_OF:
    293  1.29   thorpej 		child_devices = of_copy_spi_devs(sc->sc_dev);
    294  1.29   thorpej 		break;
    295  1.29   thorpej #endif
    296  1.29   thorpej 	default:
    297  1.29   thorpej 		child_devices = NULL;
    298  1.29   thorpej 		break;
    299  1.29   thorpej 	}
    300  1.29   thorpej 
    301  1.29   thorpej 	if (child_devices == NULL) {
    302  1.29   thorpej 		return;
    303  1.29   thorpej 	}
    304  1.29   thorpej 
    305  1.12       tnn 	memset(loc, 0, sizeof loc);
    306  1.12       tnn 	count = prop_array_count(child_devices);
    307  1.12       tnn 	for (i = 0; i < count; i++) {
    308  1.12       tnn 		child = prop_array_get(child_devices, i);
    309  1.12       tnn 		if (!child)
    310  1.12       tnn 			continue;
    311  1.34   thorpej 		if (!prop_dictionary_get_uint32(child, "slave", &chip_select))
    312  1.12       tnn 			continue;
    313  1.12       tnn 		if (!prop_dictionary_get_uint64(child, "cookie", &cookie))
    314  1.12       tnn 			continue;
    315  1.12       tnn 		if (!(cdata = prop_dictionary_get(child, "compatible")))
    316  1.12       tnn 			continue;
    317  1.34   thorpej 		loc[SPICF_SLAVE] = chip_select;
    318  1.12       tnn 
    319  1.12       tnn 		memset(&sa, 0, sizeof sa);
    320  1.34   thorpej 		sa.sa_handle = &sc->sc_slaves[chip_select];
    321  1.30   thorpej 
    322  1.30   thorpej 		/* XXX Really, I promise, it'll get better... */
    323  1.30   thorpej 		switch (devhandle_type(parent_handle)) {
    324  1.30   thorpej #ifdef FDT
    325  1.30   thorpej 		case DEVHANDLE_TYPE_OF:
    326  1.30   thorpej 			child_handle = devhandle_from_of(parent_handle,
    327  1.30   thorpej 							 (int)cookie);
    328  1.30   thorpej 			break;
    329  1.30   thorpej #endif
    330  1.30   thorpej 		default:
    331  1.30   thorpej 			child_handle = devhandle_invalid();
    332  1.30   thorpej 		}
    333  1.34   thorpej 		sa.sa_devhandle = child_handle;
    334  1.12       tnn 
    335  1.12       tnn 		buf = NULL;
    336  1.12       tnn 		spi_fill_compat(&sa,
    337  1.14   thorpej 				prop_data_value(cdata),
    338  1.12       tnn 				prop_data_size(cdata), &buf);
    339  1.34   thorpej 
    340  1.34   thorpej 		spi_attach_child(sc, &sa, chip_select, NULL);
    341  1.12       tnn 
    342  1.12       tnn 		if (sa.sa_compat)
    343  1.12       tnn 			free(sa.sa_compat, M_TEMP);
    344  1.12       tnn 		if (buf)
    345  1.12       tnn 			free(buf, M_TEMP);
    346  1.12       tnn 	}
    347  1.29   thorpej 	prop_object_release(child_devices);
    348  1.12       tnn }
    349  1.12       tnn 
    350  1.12       tnn int
    351  1.32   thorpej spi_compatible_match(const struct spi_attach_args *sa,
    352  1.32   thorpej 		     const cfdata_t cf __unused,
    353  1.12       tnn 		     const struct device_compatible_entry *compats)
    354  1.12       tnn {
    355  1.32   thorpej 	int match_result;
    356  1.32   thorpej 
    357  1.32   thorpej 	match_result = device_compatible_match(sa->sa_compat, sa->sa_ncompat,
    358  1.16   thorpej 					       compats);
    359  1.32   thorpej 	if (match_result) {
    360  1.32   thorpej 		match_result = SPI_MATCH_DIRECT_COMPATIBLE + match_result - 1;
    361  1.32   thorpej 	}
    362  1.12       tnn 
    363  1.32   thorpej 	return match_result ? match_result : SPI_MATCH_DEFAULT /* XXX */;
    364  1.12       tnn }
    365  1.12       tnn 
    366  1.23   thorpej const struct device_compatible_entry *
    367  1.23   thorpej spi_compatible_lookup(const struct spi_attach_args *sa,
    368  1.32   thorpej 		      const struct device_compatible_entry *compats)
    369  1.23   thorpej {
    370  1.23   thorpej 	return device_compatible_lookup(sa->sa_compat, sa->sa_ncompat,
    371  1.23   thorpej 					compats);
    372  1.23   thorpej }
    373  1.23   thorpej 
    374  1.32   thorpej bool
    375  1.32   thorpej spi_use_direct_match(const struct spi_attach_args *sa,
    376  1.32   thorpej 		     const struct device_compatible_entry *compats,
    377  1.32   thorpej 		     int *match_resultp)
    378  1.32   thorpej {
    379  1.32   thorpej 	KASSERT(match_resultp != NULL);
    380  1.32   thorpej 
    381  1.32   thorpej 	if (sa->sa_ncompat > 0 && sa->sa_compat != NULL) {
    382  1.32   thorpej 		*match_resultp = spi_compatible_match(sa, NULL, compats);
    383  1.32   thorpej 		return true;
    384  1.32   thorpej 	}
    385  1.32   thorpej 
    386  1.32   thorpej 	return false;
    387  1.32   thorpej }
    388  1.32   thorpej 
    389  1.12       tnn /*
    390   1.1   gdamore  * API for device drivers.
    391   1.1   gdamore  *
    392   1.1   gdamore  * We provide wrapper routines to decouple the ABI for the SPI
    393   1.1   gdamore  * device drivers from the ABI for the SPI bus drivers.
    394   1.1   gdamore  */
    395   1.1   gdamore static void
    396   1.3   xtraeme spi_attach(device_t parent, device_t self, void *aux)
    397   1.1   gdamore {
    398   1.1   gdamore 	struct spi_softc *sc = device_private(self);
    399   1.1   gdamore 	struct spibus_attach_args *sba = aux;
    400   1.1   gdamore 	int i;
    401   1.1   gdamore 
    402   1.1   gdamore 	aprint_naive(": SPI bus\n");
    403   1.1   gdamore 	aprint_normal(": SPI bus\n");
    404   1.1   gdamore 
    405  1.18   mlelstv 	mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NONE);
    406  1.15    kardel 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    407  1.34   thorpej 	mutex_init(&sc->sc_slave_state_lock, MUTEX_DEFAULT, IPL_NONE);
    408  1.10   mlelstv 	cv_init(&sc->sc_cv, "spictl");
    409  1.10   mlelstv 
    410  1.22   thorpej 	sc->sc_dev = self;
    411  1.28   thorpej 	sc->sc_controller = sba->sba_controller;
    412   1.8   rkujawa 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    413   1.1   gdamore 	/* allocate slave structures */
    414  1.33   thorpej 	sc->sc_slaves = malloc(sizeof(*sc->sc_slaves) * sc->sc_nslaves,
    415   1.1   gdamore 	    M_DEVBUF, M_WAITOK | M_ZERO);
    416   1.1   gdamore 
    417   1.1   gdamore 	sc->sc_speed = 0;
    418   1.2   gdamore 	sc->sc_mode = -1;
    419  1.10   mlelstv 	sc->sc_slave = -1;
    420   1.1   gdamore 
    421   1.1   gdamore 	/*
    422   1.1   gdamore 	 * Initialize slave handles
    423   1.1   gdamore 	 */
    424   1.1   gdamore 	for (i = 0; i < sc->sc_nslaves; i++) {
    425   1.1   gdamore 		sc->sc_slaves[i].sh_slave = i;
    426   1.1   gdamore 		sc->sc_slaves[i].sh_sc = sc;
    427  1.28   thorpej 		sc->sc_slaves[i].sh_controller = sc->sc_controller;
    428   1.1   gdamore 	}
    429   1.1   gdamore 
    430  1.29   thorpej 	/* XXX Need a better way for this. */
    431  1.29   thorpej 	switch (devhandle_type(device_handle(sc->sc_dev))) {
    432  1.29   thorpej #ifdef FDT
    433  1.29   thorpej 	case DEVHANDLE_TYPE_OF:
    434  1.29   thorpej 		fdtbus_register_spi_controller(self, sc->sc_controller);
    435  1.29   thorpej 		break;
    436  1.27   thorpej #endif /* FDT */
    437  1.29   thorpej 	default:
    438  1.29   thorpej 		break;
    439  1.29   thorpej 	}
    440  1.29   thorpej 
    441  1.29   thorpej 	/* First attach devices known to be present via the device tree. */
    442  1.29   thorpej 	spi_direct_attach_child_devices(sc);
    443  1.27   thorpej 
    444  1.12       tnn 	/* Then do any other devices the user may have manually wired */
    445  1.17   thorpej 	config_search(self, NULL,
    446  1.19   thorpej 	    CFARGS(.search = spi_search));
    447   1.1   gdamore }
    448   1.1   gdamore 
    449  1.10   mlelstv static int
    450  1.10   mlelstv spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
    451  1.10   mlelstv {
    452  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    453  1.10   mlelstv 
    454  1.10   mlelstv 	if (sc == NULL)
    455  1.10   mlelstv 		return ENXIO;
    456  1.10   mlelstv 
    457  1.10   mlelstv 	return 0;
    458  1.10   mlelstv }
    459  1.10   mlelstv 
    460  1.10   mlelstv static int
    461  1.10   mlelstv spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
    462  1.10   mlelstv {
    463  1.10   mlelstv 
    464  1.10   mlelstv 	return 0;
    465  1.10   mlelstv }
    466  1.10   mlelstv 
    467  1.10   mlelstv static int
    468  1.10   mlelstv spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    469  1.10   mlelstv {
    470  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    471  1.33   thorpej 	spi_handle_t sh;
    472  1.10   mlelstv 	spi_ioctl_configure_t *sic;
    473  1.10   mlelstv 	spi_ioctl_transfer_t *sit;
    474  1.10   mlelstv 	uint8_t *sbuf, *rbuf;
    475  1.10   mlelstv 	int error;
    476  1.10   mlelstv 
    477  1.10   mlelstv 	if (sc == NULL)
    478  1.10   mlelstv 		return ENXIO;
    479  1.10   mlelstv 
    480  1.18   mlelstv 	mutex_enter(&sc->sc_dev_lock);
    481  1.18   mlelstv 
    482  1.10   mlelstv 	switch (cmd) {
    483  1.10   mlelstv 	case SPI_IOCTL_CONFIGURE:
    484  1.10   mlelstv 		sic = (spi_ioctl_configure_t *)data;
    485  1.10   mlelstv 		if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
    486  1.10   mlelstv 			error = EINVAL;
    487  1.10   mlelstv 			break;
    488  1.10   mlelstv 		}
    489  1.10   mlelstv 		sh = &sc->sc_slaves[sic->sic_addr];
    490  1.22   thorpej 		error = spi_configure(sc->sc_dev, sh, sic->sic_mode,
    491  1.22   thorpej 		    sic->sic_speed);
    492  1.10   mlelstv 		break;
    493  1.10   mlelstv 	case SPI_IOCTL_TRANSFER:
    494  1.10   mlelstv 		sit = (spi_ioctl_transfer_t *)data;
    495  1.10   mlelstv 		if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
    496  1.10   mlelstv 			error = EINVAL;
    497  1.10   mlelstv 			break;
    498  1.10   mlelstv 		}
    499  1.11   mlelstv 		if ((sit->sit_send && sit->sit_sendlen == 0)
    500  1.24   mlelstv 		    || (sit->sit_recv && sit->sit_recvlen == 0)) {
    501  1.11   mlelstv 			error = EINVAL;
    502  1.11   mlelstv 			break;
    503  1.11   mlelstv 		}
    504  1.10   mlelstv 		sh = &sc->sc_slaves[sit->sit_addr];
    505  1.10   mlelstv 		sbuf = rbuf = NULL;
    506  1.10   mlelstv 		error = 0;
    507  1.11   mlelstv 		if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
    508  1.10   mlelstv 			sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
    509  1.10   mlelstv 			error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
    510  1.10   mlelstv 		}
    511  1.11   mlelstv 		if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
    512  1.10   mlelstv 			rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
    513  1.10   mlelstv 		}
    514  1.10   mlelstv 		if (error == 0) {
    515  1.10   mlelstv 			if (sbuf && rbuf)
    516  1.10   mlelstv 				error = spi_send_recv(sh,
    517  1.10   mlelstv 					sit->sit_sendlen, sbuf,
    518  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    519  1.10   mlelstv 			else if (sbuf)
    520  1.10   mlelstv 				error = spi_send(sh,
    521  1.10   mlelstv 					sit->sit_sendlen, sbuf);
    522  1.10   mlelstv 			else if (rbuf)
    523  1.10   mlelstv 				error = spi_recv(sh,
    524  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    525  1.10   mlelstv 		}
    526  1.10   mlelstv 		if (rbuf) {
    527  1.10   mlelstv 			if (error == 0)
    528  1.10   mlelstv 				error = copyout(rbuf, sit->sit_recv,
    529  1.10   mlelstv 						sit->sit_recvlen);
    530  1.10   mlelstv 			free(rbuf, M_DEVBUF);
    531  1.10   mlelstv 		}
    532  1.10   mlelstv 		if (sbuf) {
    533  1.10   mlelstv 			free(sbuf, M_DEVBUF);
    534  1.10   mlelstv 		}
    535  1.10   mlelstv 		break;
    536  1.10   mlelstv 	default:
    537  1.10   mlelstv 		error = ENODEV;
    538  1.10   mlelstv 		break;
    539  1.10   mlelstv 	}
    540  1.10   mlelstv 
    541  1.18   mlelstv 	mutex_exit(&sc->sc_dev_lock);
    542  1.18   mlelstv 
    543  1.10   mlelstv 	return error;
    544  1.10   mlelstv }
    545  1.10   mlelstv 
    546   1.3   xtraeme CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
    547   1.1   gdamore     spi_match, spi_attach, NULL, NULL);
    548   1.1   gdamore 
    549   1.1   gdamore /*
    550   1.1   gdamore  * Configure.  This should be the first thing that the SPI driver
    551   1.1   gdamore  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    552   1.1   gdamore  * same as Philips Microwire mode), and speed.  If the bus driver
    553   1.1   gdamore  * cannot run fast enough, then it should just configure the fastest
    554   1.1   gdamore  * mode that it can support.  If the bus driver cannot run slow
    555   1.1   gdamore  * enough, then the device is incompatible and an error should be
    556   1.1   gdamore  * returned.
    557   1.1   gdamore  */
    558   1.1   gdamore int
    559  1.33   thorpej spi_configure(device_t dev __unused, spi_handle_t sh, int mode, int speed)
    560   1.1   gdamore {
    561   1.1   gdamore 
    562  1.10   mlelstv 	sh->sh_mode = mode;
    563  1.10   mlelstv 	sh->sh_speed = speed;
    564  1.20   thorpej 
    565  1.20   thorpej 	/* No need to report errors; no failures. */
    566  1.20   thorpej 
    567  1.10   mlelstv 	return 0;
    568  1.10   mlelstv }
    569  1.10   mlelstv 
    570  1.10   mlelstv /*
    571  1.10   mlelstv  * Acquire controller
    572  1.10   mlelstv  */
    573  1.10   mlelstv static void
    574  1.33   thorpej spi_acquire(spi_handle_t sh)
    575  1.10   mlelstv {
    576  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    577  1.10   mlelstv 
    578  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    579  1.10   mlelstv 	while ((sc->sc_flags & SPIC_BUSY) != 0)
    580  1.10   mlelstv 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    581  1.10   mlelstv 	sc->sc_flags |= SPIC_BUSY;
    582  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    583  1.10   mlelstv }
    584  1.10   mlelstv 
    585  1.10   mlelstv /*
    586  1.10   mlelstv  * Release controller
    587  1.10   mlelstv  */
    588  1.10   mlelstv static void
    589  1.33   thorpej spi_release(spi_handle_t sh)
    590  1.10   mlelstv {
    591  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    592  1.10   mlelstv 
    593  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    594  1.10   mlelstv 	sc->sc_flags &= ~SPIC_BUSY;
    595  1.10   mlelstv 	cv_broadcast(&sc->sc_cv);
    596  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    597   1.1   gdamore }
    598   1.1   gdamore 
    599   1.1   gdamore void
    600   1.1   gdamore spi_transfer_init(struct spi_transfer *st)
    601   1.1   gdamore {
    602   1.1   gdamore 
    603  1.15    kardel 	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_VM);
    604  1.10   mlelstv 	cv_init(&st->st_cv, "spixfr");
    605   1.5     rmind 
    606   1.1   gdamore 	st->st_flags = 0;
    607   1.1   gdamore 	st->st_errno = 0;
    608   1.1   gdamore 	st->st_done = NULL;
    609   1.1   gdamore 	st->st_chunks = NULL;
    610   1.1   gdamore 	st->st_private = NULL;
    611   1.1   gdamore 	st->st_slave = -1;
    612   1.1   gdamore }
    613   1.1   gdamore 
    614   1.1   gdamore void
    615   1.1   gdamore spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    616   1.1   gdamore     uint8_t *rptr)
    617   1.1   gdamore {
    618   1.1   gdamore 
    619   1.1   gdamore 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    620   1.6       mrg 	chunk->chunk_read = chunk->chunk_rptr = rptr;
    621   1.1   gdamore 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    622   1.1   gdamore 	chunk->chunk_next = NULL;
    623   1.1   gdamore }
    624   1.1   gdamore 
    625   1.1   gdamore void
    626   1.1   gdamore spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    627   1.1   gdamore {
    628   1.1   gdamore 	struct spi_chunk **cpp;
    629   1.1   gdamore 
    630   1.1   gdamore 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    631   1.1   gdamore 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    632   1.1   gdamore 	*cpp = chunk;
    633   1.1   gdamore }
    634   1.1   gdamore 
    635   1.1   gdamore int
    636  1.33   thorpej spi_transfer(spi_handle_t sh, struct spi_transfer *st)
    637   1.1   gdamore {
    638  1.10   mlelstv 	struct spi_softc	*sc = sh->sh_sc;
    639  1.28   thorpej 	const struct spi_controller *tag = sh->sh_controller;
    640   1.1   gdamore 	struct spi_chunk	*chunk;
    641  1.10   mlelstv 	int error;
    642   1.1   gdamore 
    643   1.1   gdamore 	/*
    644   1.1   gdamore 	 * Initialize "resid" counters and pointers, so that callers
    645   1.1   gdamore 	 * and bus drivers don't have to.
    646   1.1   gdamore 	 */
    647   1.1   gdamore 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    648   1.1   gdamore 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    649   1.1   gdamore 		chunk->chunk_wptr = chunk->chunk_write;
    650   1.1   gdamore 		chunk->chunk_rptr = chunk->chunk_read;
    651   1.1   gdamore 	}
    652   1.1   gdamore 
    653   1.1   gdamore 	/*
    654  1.10   mlelstv 	 * Match slave and parameters to handle
    655   1.1   gdamore 	 */
    656   1.1   gdamore 	st->st_slave = sh->sh_slave;
    657   1.1   gdamore 
    658  1.10   mlelstv 	/*
    659  1.10   mlelstv 	 * Reserve controller during transaction
    660  1.10   mlelstv  	 */
    661  1.10   mlelstv 	spi_acquire(sh);
    662  1.10   mlelstv 
    663  1.10   mlelstv 	st->st_spiprivate = (void *)sh;
    664  1.25     skrll 
    665  1.10   mlelstv 	/*
    666  1.10   mlelstv 	 * Reconfigure controller
    667  1.10   mlelstv 	 *
    668  1.10   mlelstv 	 * XXX backends don't configure per-slave parameters
    669  1.10   mlelstv 	 * Whenever we switch slaves or change mode or speed, we
    670  1.10   mlelstv 	 * need to tell the backend.
    671  1.10   mlelstv 	 */
    672  1.10   mlelstv 	if (sc->sc_slave != sh->sh_slave
    673  1.10   mlelstv 	    || sc->sc_mode != sh->sh_mode
    674  1.10   mlelstv 	    || sc->sc_speed != sh->sh_speed) {
    675  1.10   mlelstv 		error = (*tag->sct_configure)(tag->sct_cookie,
    676  1.10   mlelstv 				sh->sh_slave, sh->sh_mode, sh->sh_speed);
    677  1.10   mlelstv 		if (error)
    678  1.10   mlelstv 			return error;
    679  1.10   mlelstv 	}
    680  1.10   mlelstv 	sc->sc_mode = sh->sh_mode;
    681  1.10   mlelstv 	sc->sc_speed = sh->sh_speed;
    682  1.10   mlelstv 	sc->sc_slave = sh->sh_slave;
    683  1.10   mlelstv 
    684  1.10   mlelstv 	error = (*tag->sct_transfer)(tag->sct_cookie, st);
    685  1.10   mlelstv 
    686  1.10   mlelstv 	return error;
    687   1.1   gdamore }
    688   1.1   gdamore 
    689   1.1   gdamore void
    690   1.1   gdamore spi_wait(struct spi_transfer *st)
    691   1.1   gdamore {
    692  1.33   thorpej 	spi_handle_t sh = st->st_spiprivate;
    693   1.1   gdamore 
    694   1.5     rmind 	mutex_enter(&st->st_lock);
    695   1.4       jym 	while (!(st->st_flags & SPI_F_DONE)) {
    696   1.5     rmind 		cv_wait(&st->st_cv, &st->st_lock);
    697   1.1   gdamore 	}
    698   1.5     rmind 	mutex_exit(&st->st_lock);
    699   1.7  jakllsch 	cv_destroy(&st->st_cv);
    700   1.7  jakllsch 	mutex_destroy(&st->st_lock);
    701  1.10   mlelstv 
    702  1.10   mlelstv 	/*
    703  1.10   mlelstv 	 * End transaction
    704  1.10   mlelstv 	 */
    705  1.10   mlelstv 	spi_release(sh);
    706   1.1   gdamore }
    707   1.1   gdamore 
    708   1.1   gdamore void
    709   1.1   gdamore spi_done(struct spi_transfer *st, int err)
    710   1.1   gdamore {
    711   1.1   gdamore 
    712   1.5     rmind 	mutex_enter(&st->st_lock);
    713   1.1   gdamore 	if ((st->st_errno = err) != 0) {
    714   1.1   gdamore 		st->st_flags |= SPI_F_ERROR;
    715   1.1   gdamore 	}
    716   1.1   gdamore 	st->st_flags |= SPI_F_DONE;
    717   1.1   gdamore 	if (st->st_done != NULL) {
    718   1.1   gdamore 		(*st->st_done)(st);
    719   1.1   gdamore 	} else {
    720   1.5     rmind 		cv_broadcast(&st->st_cv);
    721   1.1   gdamore 	}
    722   1.5     rmind 	mutex_exit(&st->st_lock);
    723   1.1   gdamore }
    724   1.1   gdamore 
    725   1.1   gdamore /*
    726   1.1   gdamore  * Some convenience routines.  These routines block until the work
    727   1.1   gdamore  * is done.
    728   1.1   gdamore  *
    729   1.1   gdamore  * spi_recv - receives data from the bus
    730   1.1   gdamore  *
    731   1.1   gdamore  * spi_send - sends data to the bus
    732   1.1   gdamore  *
    733   1.1   gdamore  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    734   1.1   gdamore  * done synchronously, i.e. send a command and get the response.  This is
    735  1.26    andvar  * not full duplex.  If you want full duplex, you can't use these convenience
    736   1.1   gdamore  * wrappers.
    737   1.1   gdamore  */
    738   1.1   gdamore int
    739  1.33   thorpej spi_recv(spi_handle_t sh, int cnt, uint8_t *data)
    740   1.1   gdamore {
    741   1.1   gdamore 	struct spi_transfer	trans;
    742   1.1   gdamore 	struct spi_chunk	chunk;
    743   1.1   gdamore 
    744   1.1   gdamore 	spi_transfer_init(&trans);
    745   1.1   gdamore 	spi_chunk_init(&chunk, cnt, NULL, data);
    746   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    747   1.1   gdamore 
    748   1.1   gdamore 	/* enqueue it and wait for it to complete */
    749   1.1   gdamore 	spi_transfer(sh, &trans);
    750   1.1   gdamore 	spi_wait(&trans);
    751   1.1   gdamore 
    752   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    753   1.1   gdamore 		return trans.st_errno;
    754   1.1   gdamore 
    755   1.1   gdamore 	return 0;
    756   1.1   gdamore }
    757   1.1   gdamore 
    758   1.1   gdamore int
    759  1.33   thorpej spi_send(spi_handle_t sh, int cnt, const uint8_t *data)
    760   1.1   gdamore {
    761   1.1   gdamore 	struct spi_transfer	trans;
    762   1.1   gdamore 	struct spi_chunk	chunk;
    763   1.1   gdamore 
    764   1.1   gdamore 	spi_transfer_init(&trans);
    765   1.1   gdamore 	spi_chunk_init(&chunk, cnt, data, NULL);
    766   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    767   1.1   gdamore 
    768   1.1   gdamore 	/* enqueue it and wait for it to complete */
    769   1.1   gdamore 	spi_transfer(sh, &trans);
    770   1.1   gdamore 	spi_wait(&trans);
    771   1.1   gdamore 
    772   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    773   1.1   gdamore 		return trans.st_errno;
    774   1.1   gdamore 
    775   1.1   gdamore 	return 0;
    776   1.1   gdamore }
    777   1.1   gdamore 
    778   1.1   gdamore int
    779  1.33   thorpej spi_send_recv(spi_handle_t sh, int scnt, const uint8_t *snd,
    780   1.1   gdamore     int rcnt, uint8_t *rcv)
    781   1.1   gdamore {
    782   1.1   gdamore 	struct spi_transfer	trans;
    783   1.1   gdamore 	struct spi_chunk	chunk1, chunk2;
    784   1.1   gdamore 
    785   1.1   gdamore 	spi_transfer_init(&trans);
    786   1.1   gdamore 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    787   1.1   gdamore 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    788   1.1   gdamore 	spi_transfer_add(&trans, &chunk1);
    789   1.1   gdamore 	spi_transfer_add(&trans, &chunk2);
    790   1.1   gdamore 
    791   1.1   gdamore 	/* enqueue it and wait for it to complete */
    792   1.1   gdamore 	spi_transfer(sh, &trans);
    793   1.1   gdamore 	spi_wait(&trans);
    794   1.1   gdamore 
    795   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    796   1.1   gdamore 		return trans.st_errno;
    797   1.1   gdamore 
    798   1.1   gdamore 	return 0;
    799   1.1   gdamore }
    800