Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add userland driver to spi framework.



details:   https://anonhg.NetBSD.org/src/rev/40c01e4dfd11
branches:  trunk
changeset: 449113:40c01e4dfd11
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Feb 23 10:43:25 2019 +0000

description:
Add userland driver to spi framework.

Previously spi would configure the controller to use the lowest speed of
all connected devices since the kernel started and to fail attempted mode
changes. This is now improved to keep individual modes and speeds for each
slave and to reconfigure the controller as necessary for each transfer.

Added man page for spi(9).

diffstat:

 distrib/sets/lists/base/mi |    3 +-
 distrib/sets/lists/comp/mi |    5 +-
 etc/MAKEDEV.tmpl           |    9 +-
 share/man/man4/spi.4       |    3 +-
 share/man/man9/spi.9       |  163 +++++++++++++++++++++++++++++++++
 sys/dev/spi/Makefile       |    6 +
 sys/dev/spi/spi.c          |  220 +++++++++++++++++++++++++++++++++++++++-----
 sys/dev/spi/spi_io.h       |   53 ++++++++++
 sys/dev/spi/spivar.h       |    3 +-
 9 files changed, 435 insertions(+), 30 deletions(-)

diffs (truncated from 685 to 300 lines):

diff -r d3bfabffb401 -r 40c01e4dfd11 distrib/sets/lists/base/mi
--- a/distrib/sets/lists/base/mi        Sat Feb 23 08:19:16 2019 +0000
+++ b/distrib/sets/lists/base/mi        Sat Feb 23 10:43:25 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1200 2019/02/02 22:39:32 mrg Exp $
+# $NetBSD: mi,v 1.1201 2019/02/23 10:43:25 mlelstv Exp $
 #
 # Note:        Don't delete entries from here - mark them as "obsolete" instead,
 #      unless otherwise stated below.
@@ -1112,6 +1112,7 @@
 ./usr/include/dev/rcons                                base-obsolete           obsolete
 ./usr/include/dev/sbus                         base-c-usr
 ./usr/include/dev/scsipi                       base-c-usr
+./usr/include/dev/spi                          base-c-usr
 ./usr/include/dev/sun                          base-c-usr
 ./usr/include/dev/tc                           base-c-usr
 ./usr/include/dev/usb                          base-c-usr
diff -r d3bfabffb401 -r 40c01e4dfd11 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Sat Feb 23 08:19:16 2019 +0000
+++ b/distrib/sets/lists/comp/mi        Sat Feb 23 10:43:25 2019 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2259 2019/02/23 03:10:05 kamil Exp $
+#      $NetBSD: mi,v 1.2260 2019/02/23 10:43:25 mlelstv Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -709,6 +709,7 @@
 ./usr/include/dev/scsipi/ss_mustek.h           comp-obsolete           obsolete
 ./usr/include/dev/scsipi/ssvar.h               comp-obsolete           obsolete
 ./usr/include/dev/spkrio.h                     comp-c-include
+./usr/include/dev/spi/spi_io.h                 comp-c-include
 ./usr/include/dev/sun/disklabel.h              comp-c-include
 ./usr/include/dev/sun/eeprom.h                 comp-c-include
 ./usr/include/dev/sun/event_var.h              comp-obsolete           obsolete
@@ -19594,6 +19595,7 @@
 ./usr/share/man/html9/specificdata_key_delete.html comp-sys-htmlman    html
 ./usr/share/man/html9/specificdata_setspecific.html comp-sys-htmlman   html
 ./usr/share/man/html9/specificdata_setspecific_nowait.html comp-sys-htmlman html
+./usr/share/man/html9/spi.html                 comp-sys-htmlman        html
 ./usr/share/man/html9/spinlockinit.html                comp-sys-htmlman        html
 ./usr/share/man/html9/spinlockmgr.html         comp-sys-htmlman        html
 ./usr/share/man/html9/spl.html                 comp-sys-htmlman        html
@@ -27626,6 +27628,7 @@
 ./usr/share/man/man9/specificdata_key_delete.9 comp-sys-man            .man
 ./usr/share/man/man9/specificdata_setspecific.9        comp-sys-man            .man
 ./usr/share/man/man9/specificdata_setspecific_nowait.9 comp-sys-man    .man
+./usr/share/man/man9/spi.9                     comp-sys-man            .man
 ./usr/share/man/man9/spinlockinit.9            comp-sys-man            .man
 ./usr/share/man/man9/spinlockmgr.9             comp-sys-man            .man
 ./usr/share/man/man9/spl.9                     comp-sys-man            .man
diff -r d3bfabffb401 -r 40c01e4dfd11 etc/MAKEDEV.tmpl
--- a/etc/MAKEDEV.tmpl  Sat Feb 23 08:19:16 2019 +0000
+++ b/etc/MAKEDEV.tmpl  Sat Feb 23 10:43:25 2019 +0000
@@ -1,5 +1,5 @@
 #!/bin/sh -
-#      $NetBSD: MAKEDEV.tmpl,v 1.198 2019/02/23 03:10:06 kamil Exp $
+#      $NetBSD: MAKEDEV.tmpl,v 1.199 2019/02/23 10:43:25 mlelstv Exp $
 #
 # Copyright (c) 2003,2007,2008 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -272,6 +272,7 @@
 #      se*     SCSI Ethernet
 #      ses*    SES/SAF-TE SCSI Devices
 #      speaker PC speaker              (XXX - installed)
+#      spi*    SPI bus device
 #      sram    battery backuped memory (x68k)
 #      ss*     SCSI scanner
 #      stic*   PixelStamp interface chip
@@ -1584,6 +1585,12 @@
        mkdev iic$unit c %iic_chr% $unit 600
        ;;
 
+spi[0-9]*)
+       unit=${i#spi}
+       : ${unit:-0}
+       mkdev spi$unit c %spi_chr% $unit 600
+       ;;
+
 amr[0-9]*)
        unit=${i#amr}
        mkdev amr$unit c %amr_chr% $unit
diff -r d3bfabffb401 -r 40c01e4dfd11 share/man/man4/spi.4
--- a/share/man/man4/spi.4      Sat Feb 23 08:19:16 2019 +0000
+++ b/share/man/man4/spi.4      Sat Feb 23 10:43:25 2019 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: spi.4,v 1.5 2015/08/18 19:40:21 phx Exp $
+.\"    $NetBSD: spi.4,v 1.6 2019/02/23 10:43:25 mlelstv Exp $
 .\"
 .\" Copyright (c) 2006 Urbana-Champaign Independent Media Center.
 .\" Copyright (c) 2006 Garrett D'Amore.
@@ -107,6 +107,7 @@
 Texas Instruments TMP121 temperature sensor.
 .El
 .Sh SEE ALSO
+.Xr spi 9 ,
 .Xr m25p 4 ,
 .Xr mcp23s17gpio 4 ,
 .Xr mcp3kadc 4 ,
diff -r d3bfabffb401 -r 40c01e4dfd11 share/man/man9/spi.9
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man9/spi.9      Sat Feb 23 10:43:25 2019 +0000
@@ -0,0 +1,163 @@
+.\"    $NetBSD: spi.9,v 1.1 2019/02/23 10:43:25 mlelstv Exp $
+.\"
+.\" Copyright (c) 2019 The NetBSD Foundation
+.\" All rights reserved.
+.\"
+.\" Written by Michael van Elst
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd February 23, 2019
+.Dt SPI 9
+.Os
+.Sh NAME
+.Nm spi_configure ,
+.Nm spi_transfer ,
+.Nm spi_transfer_init ,
+.Nm spi_chunk_init ,
+.Nm spi_transfer_add ,
+.Nm spi_wait ,
+.Nm spi_done ,
+.Nm spi_send ,
+.Nm spi_recv ,
+.Nm spi_send_recv
+.Sh SYNOPSIS
+.In dev/spi/spivar.h
+.Ft int
+.Fo spi_configure
+.Fa "struct spi_handle *sh"
+.Fa "int mode"
+.Fa "int speed"
+.Fc
+.Ft int
+.Fo spi_transfer
+.Fa "struct spi_handle *sh"
+.Fa "struct spi_transfer *st"
+.Fc
+.Ft void
+.Fo spi_transfer_init
+.Fa "struct spi_transfer *st"
+.Fc
+.Ft void
+.Fo spi_chunk_init
+.Fa "struct spi_chunk *chunk"
+.Fa "int cnt"
+.Fa "const uint8_t *wptr"
+.Fa "uint8_t *rptr"
+.Fc
+.Ft void
+.Fo spi_transfer_add
+.Fa "struct spi_transfer *st"
+.Fa "struct spi_chunk *chunk"
+.Fc
+.Ft void
+.Fo spi_wait
+.Fa "struct spi_transfer *st"
+.Fc
+.Ft void
+.Fo spi_done
+.Fa "struct spi_transfer *st"
+.Fa "int err"
+.Fc
+.Ft int
+.Fo spi_recv
+.Fa "struct spi_handle *sh"
+.Fa "int cnt"
+.Fa "uint8_t *data"
+.Fc
+.Ft int
+.Fo spi_send
+.Fa "struct spi_handle *sh"
+.Fa "int cnt"
+.Fa "const uint8_t *data"
+.Fc
+.Ft int
+.Fo spi_send_recv
+.Fa "struct spi_handle *sh"
+.Fa "int scnt"
+.Fa "const uint8_t *snd"
+.Fa "int rcnt"
+.Fa "const uint8_t *rcv"
+.Fc
+.Sh DESCRIPTION
+SPI is a 4-wire synchronous full-duplex serial bus.
+It is commonly used for connecting devices such as EEPROMs,
+displays, and other types of integrated circuits.
+The
+.Nm spi
+interface provides a means of communicating with SPI-connected devices.
+.Sh FUNCTIONS
+The following functions comprise the API provided to drivers of
+SPI-connected devices.
+.Pp
+The
+.Fa struct spi_handle
+corresponding to the device is passed in the driver attachment.
+.Bl -tag -width spi_transfer_init
+.It Fn spi_configure "sh" "mode" "speed"
+Sets mode and speed for subsequent communication with a SPI slave.
+.It Fn spi_transfer "sh" "st"
+Queue transfer to SPI controller.
+.Fn spi_transfer
+returns an errno value when the transfer couldn't be queued.
+.It Fn spi_transfer_init "st"
+Initialize a transfer structure.
+.It Fn spi_chunk_init "chunk" "cnt" "wptr" rptr"
+Initialize a chunk structure, each chunk corresponds to
+a possibly bi-directional transfer where the same amount
+of bytes is shifted in and out.
+.It Fn spi_transfer_add "st" "chunk"
+Append a chunk to transfer structure.
+.It Fn spi_wait "st"
+Wait for a transfer to complete. When the transfer has failed
+for some reason, the field
+.Va st->st_errno
+is set to a non-zero value.
+.It Fn spi_done "st" "err"
+Called back machine-dependent backend to signal completion
+of a transfer.
+.El
+.Pp
+For simplicity there are convenience functions that combine
+common operations.
+These functions return an errno value when the transfer failed.
+.Bl -tag -width spi_transfer_init
+.It Fn spi_recv "sh" "cnt" "data"
+Prepares a chunk for receiving data, queues a transfer and
+waits for it to complete.
+.It Fn spi_send "sh" "cnt" "data"
+Prepares a chunk for sending data, queues a transfer and
+waits for it to complete.
+.It Fn spi_send_recv "sh" "scnt" "snd" "rcnt" "rcv"
+Prepares two chunks for sending data first and then receiving
+an answer,
+queues a transfer and waits for it to complete.
+This is not a full-duplex operation.
+.El
+.Sh SEE ALSO
+.Xr spi 4
+.Rs
+.Sh HISTORY
+The
+.Nm spi
+API first appeared in
+.Nx 4.0 .
diff -r d3bfabffb401 -r 40c01e4dfd11 sys/dev/spi/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/spi/Makefile      Sat Feb 23 10:43:25 2019 +0000
@@ -0,0 +1,6 @@
+# $NetBSD: Makefile,v 1.1 2019/02/23 10:43:25 mlelstv Exp $
+
+INCSDIR= /usr/include/dev/spi
+INCS=   spi_io.h
+
+.include <bsd.kinc.mk>
diff -r d3bfabffb401 -r 40c01e4dfd11 sys/dev/spi/spi.c
--- a/sys/dev/spi/spi.c Sat Feb 23 08:19:16 2019 +0000
+++ b/sys/dev/spi/spi.c Sat Feb 23 10:43:25 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: spi.c,v 1.9 2018/09/03 16:29:33 riastradh Exp $ */
+/* $NetBSD: spi.c,v 1.10 2019/02/23 10:43:25 mlelstv Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,26 +42,55 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.9 2018/09/03 16:29:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.10 2019/02/23 10:43:25 mlelstv Exp $");
 
 #include "locators.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/device.h>
+#include <sys/conf.h>
 #include <sys/malloc.h>
 #include <sys/mutex.h>
 #include <sys/condvar.h>



Home | Main Index | Thread Index | Old Index