Changes between Version 3 and Version 4 of SPI


Ignore:
Timestamp:
11/30/2017 10:06:15 PM (7 years ago)
Author:
Tim Harvey
Comment:

clean up kernel and spidev details

Legend:

Unmodified
Added
Removed
Modified
  • SPI

    v3 v4  
    1616== On-Board SPI controllers ==
    1717
     18[=#spitable]
    1819=== Newport / Ventana ===
    1920Various Gateworks boards support 3.3V TTL SPI to an off-board expansion connector with a single CS#:
    20 ||= Family =||= Board   =||= Device   =||= Connector =||
    21 || Newport  || GW630x    || SPI0       || J8          ||
    22 ||          || GW640x    || SPI0       || J8          ||
     21||= Family =||= Board   =||= Device Tree Node =||= Connector =||
     22|| Newport  || GW630x    || spi_7_0            || J8          ||
     23||          || GW640x    || spi_7_0            || J8          ||
    2324|| ||
    24 || Ventana  || GW552x    || ECSPI3     || J24         ||
    25 ||          || GW54xx-E+ || ECSPI2     || J32         ||
     25|| Ventana  || GW552x    || ecspi3             || J24         ||
     26||          || GW54xx-E+ || ecspi2             || J32         ||
    2627
    2728Pinout (all signals are 3.3V TTL):
     
    4546
    4647[=#usb]
    47 == USB Expansion devices ==
     48== USB SPI controllers ==
    4849A SPI master can also be added via USB expansion. For example:
    4950 * [http://trac.gateworks.com/wiki/expansion/gw16113 GW16113 firmware-flexible USB 2.0 FS expansion]
     
    5152
    5253
    53 [=#spidev]
    54 == Adding Kernel Support for Specific SPI Devices ==
    55 In order to support the specific device you plan to connect an addition to the GW5220 device tree must be made.
     54[=#linux]
     55== Using a Linux SPI kernel driver ==
    5656
    57 1. Follow [http://trac.gateworks.com/wiki/linux/kernel#BuildingtheLinuxkernelout-of-tree these instructions] up to and including step 5 of "Building the Linux Kernel"
    58 2. Open the file at {{{.../linux-imx6/arch/arm/boot/dts/imx6qdl-gw52xx.dtsi}}} and find the node labeled "&ecspi3" (if absent, jump to the above section before continuing)
    59 3. In the same location as the comment in step 5 of the above section, add your appropriate device configuration in a node beginning with "flash: "
    60 4. Continue the instructions from step 1 to finish building your new kernel
     57=== device-tree: Newport / Ventana ===
     58Linux kernels that utilize the device-tree need to add a device-tree node to the SPI controller to use a kernel driver. Note that you can also access SPI from userspace using {{{spidev}}} (see [#spidev below])
    6159
    62 An example of a properly made configuration node for a m25p32-vme6g nor flash device would be:
     60An example device-tree child node for a m25p32-vme6g nor flash device would be:
    6361{{{#!c
    6462flash: m25p80@0 {                         /* "m25p80@0" parameter is the driver responsible for controlling your device */
     
    7674};
    7775}}}
     76
     77Note that this node must appear within the SPI host controller. For a list of device-tree nodes [#spitable see above].
     78
    7879Note that the values shown in the above node are specific to the m25p32-vme6g, and can vary greatly from the actual device you are using. Replace attribute values as necessary.
    7980
    80 When searching for your device ID string or its controlling driver, searching via a LXR site like the one at [https://lxr.missinglinkelectronics.com/linux missing link electronics] for your device name can be helpful.
     81When searching for your device ID string or its controlling driver, searching via a Linux LXR site like the one at [https://lxr.missinglinkelectronics.com/linux missing link electronics] for your device name can be helpful.
    8182
    82 == spidev - Userspace SPI API ==
     83=== non-device-tree: Laguna ===
     84Linux kernels that do not utilize device-tree need to register a {{{spi_board_info}}} struct with the kernel via {{{spi_register_board_info()}}}.
    8385
    84 SPI devices have a limited userspace API, supporting basic half-duplex read() and write() access to SPI slave devices. Using ioctl() requests, full duplex transfers and device I/O configuration are also available.
    85 {{{
    86         #include <fcntl.h>
    87         #include <unistd.h>
    88         #include <sys/ioctl.h>
    89         #include <linux/types.h>
    90         #include <linux/spi/spidev.h>
    91 }}}
     86To add a SPI device to Laguna you would add it to the spi_board_info array in {{{laguna.c}}}. Be sure to specify chip_select=1 to use CS1 as CS0 is used for the on-board SPI FLASH device.
     87
     88References:
     89 * [https://github.com/Gateworks/openwrt/blob/16.02/target/linux/cns3xxx/files/arch/arm/mach-cns3xxx/laguna.c#L188 spi_board_info struct]
     90 * [https://github.com/Gateworks/openwrt/blob/16.02/target/linux/cns3xxx/files/arch/arm/mach-cns3xxx/laguna.c#L1097 call to spi_register_board_info()]
     91
     92
     93
     94[=#spidev]
     95== Linux spidev userspace API ==
     96SPI devices have a limited userspace API, supporting basic half-duplex read() and write() access to SPI slave devices referred to as {{{spidev}}}. Using ioctl() requests, full duplex transfers and device I/O configuration are also available.
     97
    9298Some reasons you might want to use this programming interface include:
    93 
    94 * Prototyping in an environment that's not crash-prone; stray pointers in userspace won't normally bring down any Linux system.
    95 * Developing simple protocols used to talk to microcontrollers acting as SPI slaves, which you may need to change quite often.
     99 * Prototyping in an environment that's not crash-prone; stray pointers in userspace won't normally bring down any Linux system.
     100 * Developing simple protocols used to talk to micro-controllers acting as SPI slaves, which you may need to change quite often.
    96101
    97102Of course there are drivers that can never be written in userspace, because they need to access kernel interfaces (such as IRQ handlers or other layers of the driver stack) that are not accessible to userspace.
     
    99104Userspace access to SPI devices is done through the /dev/spidev<bus>.<chip-select> device interface. In order to use this you must have spidev enabled in the kernel (CONFIG_SPI_SPIDEV) and have a spidev node defined under the SPI controller in the device-tree.
    100105
     106In order to support {{{spidev}}} a spidev child node needs to be present in the device-tree under the SPI host controller. For example, a GW54xx which brings out the ecspi2 host interface:
     107{{{#!c
     108&ecspi2 {
     109        cs-gpios = <&gpio2 26 GPIO_ACTIVE_HIGH>;
     110        pinctrl-names = "default";
     111        pinctrl-0 = <&pinctrl_ecspi2>;
     112        status = "okay";
     113
     114        spidev0: spidev@0 {
     115                compatible = "rohm,dh2228fv";
     116                reg = <0>;
     117                spi-max-frequency = <60000000>;
     118        };
     119};
     120}}}
     121
     122For instructions on compiling and updating a device-tree see [wiki:linux/devicetree linux/devicetree]
     123
     124An application using {{{spidev}}} would include the {{{<linux/spi/spidev.h>}}} header file.
     125
    101126For more info see:
    102127* [https://web.archive.org/web/20170619204541/https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/spi/spidev spidev Kernel Documentation]
    103128* [https://web.archive.org/web/20170619204541/https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/spi/spidev spidev examples]
    104129* [https://web.archive.org/web/20170619204541/https://lxr.missinglinkelectronics.com/linux/drivers/spi/spidev.c spidev kernel driver]
     130