Skip to contentSkip to conversation

Adding support for CAN busses via SPI interface

PATCH In review Stefano Babic 3 participants 16 messages Jul 28, 2014 – Nov 6, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover Adding support for CAN busses via SPI interface

Hi all,

after some time, I post an updated version of the spi_can driver. Sorry for that, but I had to wait for the requested modifications on the microcontroller's firmware to test it again.

The majork change is the GET_CFG message to query the remote CAN microcontroller for the CAN bittiming. I hope also I have fixed all issues from previous reviews.

Changes in v5:
- drop Patch 2/3, already applied.
- sort include headers
- match open parenthesis (globally fixed)
- add newline to dev_err() in insert_cfg_msg()
- use sizeof(*p)
- drop unused variable val in ISR
- proper return code instead of -1
- move device ids after probe/remove
- drop #ifdef CONFIG_OF (not needed anymore)
- use devm_ API
- use module_spi_driver()

Changes in v4:
- added GET_CFG message to query bit timing to the remote controller.
- implement GET_CFG message to ask the microcontroller
for bittiming consts.
- drop set_mode (never called)
- drop echo_index (never used)
- fix inconsistencies using int variable (int/u32)
- add reference to documentation in Kconfig help
- s/refTime/ref_time/
- move module parameters on the top
- use variable to get sizeof inside kzalloc/memset
- fix missing close_candev() in open entry point
- fix return values (spi_can_fill_skb_msg())
- not access skb after calling net_receive_skb()
- fix minor coding style issues
- add missing free_irq() and gpio_free() in probe when fails

Changes in v3:
- format documentation, check for lines > 80 chars (O. Hartkopp)
- patch 2/3 already aqpplied to can-next, removed from patchset
- spican.h renamed to spi_can.h
- drop further references to i.MX and HCS12, not yet cleaned
- drop CAN_DEV depend from Kconfig
- drop debug stuff via sysfs, not required in production code
- drop debug module parameter, use CAN_DEBUG_DEVICES
- drop unused bittiming constant
- chksum on as default. It could still be disabled via DT/pdata, but not via module parameter.

Changes in v2:
- drop all references to i.MX35 and HCS12

Stefano Babic (2):
Add documentation for SPI to CAN driver
CAN: CAN driver to support multiple CAN bus on SPI interface

 Documentation/networking/spi_can.txt |  774 +++++++++++++++++
 drivers/net/can/spi/Kconfig          |   11 +
 drivers/net/can/spi/Makefile         |    1 +
 drivers/net/can/spi/spi_can.c        | 1506 ++++++++++++++++++++++++++++++++++
 include/linux/can/platform/spi_can.h |   33 +
 5 files changed, 2325 insertions(+)
 create mode 100644 Documentation/networking/spi_can.txt
 create mode 100644 drivers/net/can/spi/spi_can.c
 create mode 100644 include/linux/can/platform/spi_can.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v5 1/2 Add documentation for SPI to CAN driver

---

Changes in v5:
- drop Patch 2/3, already applied.

Changes in v4:
- added GET_CFG message to query bit timing to the remote controller.

Changes in v3:
- format documentation, check for lines > 80 chars (O. Hartkopp)
- patch 2/3 already aqpplied to can-next, removed from patchset

Changes in v2: None

 Documentation/networking/spi_can.txt | 774 +++++++++++++++++++++++++++++++++++
 1 file changed, 774 insertions(+)
 create mode 100644 Documentation/networking/spi_can.txt
Documentation/networking/spi_can.txt new file +774 -0
new file mode 100644
index 0000000..82f33e7
@@ -0,0 +1,774 @@
+The spi_can driver is intended to provide a general protocol
+to support CAN interface that are not directly connected or
+part of the main controller, but they are available on a
+separate controller, such as a low cost/low power microcontroller
+(for example, very simple 16 bit controllers providing multiple CAN
+ busses).
+
+This document is intended to describe the internal protocol
+on the SPI interface between the linux system (main controller)
+and the remote CAN controller(s).
+
+1. Overview
+----------------------
+
+The CAN controller processors supports up to N Can Busses. The Main Controller
+driver should be able to manage all CAN busses. It is then
+responsibility of the SW Main Application to decide which controllers
+(which CAN channels) should be opened and used.
+
+There is the requirement to configure the CAN controller, sending some
+parameters or commands not strictly related to the CAN interfaces. For example,
+it is required that the application sends periodically a message to the CAN
+controller, that has the responsibility to supervise the Main Controller. If the
+message is not received in time, the CAN controller must perform a reset of the
+Main Controller controller (Watchdog message).
+Another known use case is to configure the wake-up mechanism, that is
+under which circumstances the CAN controller must awake the Main Controller
+(ignition, opened door,...). The protocol foresees an additional channel (CFG)
+used to exchange data between the application layer and the CAN controller.
+
+The document sets the rules to exchange messages between the Main Controller and
+the CAN controller and allows to extend the functionalities with new messages
+(see Chapter 5) in the future.
+
+2. Interface to upper layer
+------------------------------
+
+The driver must provide a socket CAN interface. In this way, the
+application is able to communicate with CAN via usual sockets.
+A detailed description can be found in the kernel documentation
+(Documentation/networking/can.txt).
+
+On the application level there will be network interfaces, and
+they can be set up with Linux tools such as "ifconfig" and "ip".
+
+For the CAN controller configuration it is proposed to add a further network
+device ("cfg") to have a consistent interface. In this way, the
+driver must not expone itself in a different way to the application SW.
+Configuration packets are simply forward to the CAN controller exactly as it is
+done for the other channels. The application fills data for the cfg
+interface and the Main Controller driver does not need to look inside.
+Configuration data are treated by the CAN driver exactly as all other CAN
+packets.
+Only the SW application and CAN controller-SW are aware of it.
+
+The driver must be able to mux/demux packets from the socket can interface.
+Indeed, there is only one SPI interface to to communicate with the CAN
+controller, and the driver instantiates automatically multiple network
+interfaces.
+On the downstream direction, the driver must take packets from all interfaces
+(hcan0,..hcan4), set up a frame header (message type,
+channel number, etc.) and send it to CAN controller via the SPI interface.
+
+
+             hcan0  hcan1 hcan2  hcan3  hcan4 cfg
+               |     |    |       |       |    |
+               |     |    |       |       |    |
+            ,__|_____|____|_______|_______|____|_
+            |                                    |
+            |                                    |
+            |      SOCKET CAN (Kernel)           |
+            |                                    |
+            L________________,.__________________|
+                            ,!!;
+                            ,!!;
+                            `!!.
+                            ;!!.
+            ,'''''''''''''''''''''''''''''''''''`.
+            |  +------------------------------.  |
+            |  |    Channel Mux/Demux         |  |
+            |  L______________________________|  |
+            |                                    |
+            |  +------------------------------.  |
+            |  |       Can to SPI controller  |  |
+            |  L______________________________|  |
+            |..................................../
+
+
+On the upstream direction, the driver must be able to demux the received
+frames and forward them to the related network interface.
+
+The cfg is a special CAN interface used only to set up the
+CAN controller behavior and does not map to a physical CAN channel, as the other
+CAN interfaces do. The scope of such interface is to provide a way to
+exchange messages with the CAN controller processor that are not strictly
+related to the CAN bus. As example for such messages, a watchdog message must
+be sent regularly by the Main Controller to the CAN controller, and it is sent
+as CAN data to channel that matches the imxhcs12.  The driver is not aware of
+these messages, as they are seen as normal data messages for a CAN channel.
+This guarantees that it is possible in future to extend the list of these
+configuration messages without changing the driver. The partners that must know
+their format are the application software in user space and the CAN controller
+Software.
+
+3. Interface to HW
+---------------------------
+
+The CAN controller is connected to the Main Controller via the SPI interface.
+
+3.1 SPI Hardware Connection
+---------------------------
+
+ - Main Controller is always the Master and the CAN controller in slave mode.
+
+ - Maximal frequency:
+   The limit is set by the CAN controller, because it must serve an interrupt
+   request before the next two bytes are sent.
+
+   The CAN controller has two operational modes: "User Mode" and
+   "Supervisor / Maintenance" Mode. The last one is used to update
+   the software on the CAN controller.
+   In Supervisor Mode, the CAN controller runs with little support for its
+   peripherals. The maximum SPI frequency is limited to 1 Mhz.
+   The driver must support two different operational frequencies,
+   and must be able to switch between them.
+   This is done using the CFG_MSG_SET for the CFG channel, see details in chapter 6.
+
+ - bit x words : it was tested with 32 bits. This means that the Main Controller
+   deactive the chip select automatically (without intervention by Software)
+   each 4 bytes. No interrupts are generated on the Main Controller because they
+   are ruled by the internal FIFO.
+
+ - GPIO(s):
+	IRQ GPIO: set active by CAN controller when it has something to send
+		it raises an interrupt on Main Controller side.
+
+To transfer data, some frame header will be included. If a field contains more
+as one byte, a big endian convention is used (MSB is transmitted first).
+
+The SPI interface is full duplex. This means, both Main Controller and CAN
+controller are transmitting data at the same time. It is possible to transfer
+CAN messages to and from the CAN controller in the same SPI transfer.
+
+4. SPI Protocol between Main Controller and CAN controller
+----------------------------------------------------------
+
+The format of the frames exchanged between Main Controller and CAN controller
+is:
+
+
+    ,'''''''','''''''''''''''','''''''''''''''''''''''','''''''''''''|
+    |MSG ID  |  Length(16 bit)|    DATA                |  CHECKSUM   |
+    L________|________________|________________________|_____________|
+
+The checksum is thought to be used in the development phase to check
+for implementation's error. It should be possible to disable it after
+the development is finished. The SPI interface ensures that data are
+transfered without modification between the two processors.
+
+The checksum is computed as 16 bit 2's complement of the sum (module 16)
+of  all bytes in the message, excluded the checksum itself.
+On the receive side, it is enough to sum all bytes including the checksum
+to verify if the message was received correctly (the sum must be zero).
+
+The MSG-ID (1 byte) identifies the type of exchange data. For basic exchange
+of frames, the following messages are used:
+
+	SEND_DATA_MSG
+	STATUS_MSG
+	SYNC_MSG
+	CFG_MSG_SET
+	CFG_MSG_GET
+	REQ_DATA_MSG
+
+The SEND_DATA_MSG is used when one of the two processors needs to send a CAN
+message.
+
+The STATUS_MSG is used to inform the partner about the number of bytes ready
+to be transfered. This is used by the Main Controller when it has several
+messages to send, and it needs a longer frame as usual to sen all CAN messages
+back.
+
+The SYNC_MSG is used to start up the communication and in case an error is
+detected. It is the first message that the Main Controller will send to the
+CAN controller.
+Because the Main Controller will always send the MSG ID as first byte in the
+frame, the SYNC_MSG is used to signalize the start of a frame to the CAN
+controller.
+The SYNC_MSG is used also to initialize or reinitialize the time on the
+CAN controller. It is not required to the CAN controller to have an absolute
+time synchronized to the Main Controller. It is enough to have a relative time,
+and the Main Controller computes the absolute time to add it as timestamp to
+the received packets.
+
+The CFG_MSG_SET is sent to configure the CAN channel for bitrate and timing.
+The driver on the Main Controller does not need to parse the parameters and it
+forwards them to the corresponding CAN controller's CAN interface.
+
+The CFG_MSG_GET is sent at the start up to query the CAN controller about its
+internal timing, making them available to the netlink interface. CAN drivers
+have usually fest values for can_bittiming_const. In case of a remote
+CAN controller driven via SPI, the Main Controller cannot know the timing
+and must ask them to the CAN controller before instantiating a CAN device.
+For this reason, the message is sent only once in the probe() entry point.
+
+The REQ_DATA is sent only by the Main Controller when the Main Controller
+signals it has packets to be transfered, but the Main Controller has nothing
+to sent. Its scope is mainly to signalize to the Main Controller the Start of
+the Frame and to pass in the LENGTH field the number of bytes of the frame
+itself.
+After receiving the REQ_DATA message, the CAN controller knows how many bytes
+is allowed to send without fragmenting frames on the Main Controller's side.
+
+4.1 Start of the comunication
+-----------------------------
+
+The Main Controller does not poll continuosly the CAN controller.
+If it has nothing to send, it waits to get an interrupt on the GPIO-line.
+
+If the CAN controller has packets to be sent, it sets the GPIO-IRQ,
+and this raises an interrupt on Main Controller side.
+
+The CAN controller can sends enough messages as they can fit inside
+the SPI transfer. The number of bytes (=SPI clock cycles)
+to be transfered is set by the Main Controller inside the Status Message.
+
+               |                               |
+               |GPIO(IRQ for Main Controller)  |
+               |<------------------------------'
+               |                               |
+               |  First SPI Transfer started   |
+               |      SPI:REQ_DATA             |
+               |------------------------------>|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |      SPI: Status              |
+               |<------------------------------|
+               |                               |
+               |  New SPI Transfer started     |
+               |      SPI:Send Data            |
+               |------------------------------>|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |                               |
+               |                               |
+               |                               |
+	Example of SPI transfer.
+
+The CAN controller sets the GPIO-IRQ, and this raises an interrupt on the
+Main Controller.
+The protocol uses several Message-IDs to identify what is really exchanged.
+
+From the Main Controller point of view, a SPI Transfer is initiated when the
+first byte is written into the internal FIFO and it is finished when the all
+bytes are transmitted. The protocol relies on the positional place of the
+bytes, that means there is no need to wait for a start-frame byte.
+
+On the receive side, due to CAN controller limitations, the Main Controller
+must drop the first 4 bytes received from the CAN controller, as they have
+no meaning.
+It must be ensured by the CAN controller software that the first valid byte
+is the fifth in the SPI transfer.
+
+The Main Controller has no knowledge at the beginning how many messages the
+CAN controller will send and it starts with a 32-bytes transfer because this
+matches the internal FIFO and raises only one interrupt.
+If the CAN controller has communicated with the status message
+the number of bytes it needs for the next SPI frame, the Main Controller will
+start a SPI transfer for a number of bytes equal or greater
+as the received value.
+Sending imediately a new SPI transfer should minimize the delay to transfer
+CAN messages into the socket CAN layer, and setting the transfer to
+a multiple of 32 is a best-effort for the CPU load on the Main Controller side,
+because it matches the internal FIFO size (tested on i.MX35). However, there is
+no restrictions about the number of bytes to be transfered, and it is duty of
+the Main Controller driver to find the most valuable length to be used.
+
+A SPI transfer is *NOT* delimited by changes of the chip select signal. Indeed,
+the chip select is ruled internally by the bits x word setup, and it is made
+suitable for both CAN controller and Main Controller. Bits x words must be a
+multiple of 2, because the CAN controller gets an interrupt each 2 bytes.
+
+
+5. SPI Communication scenarios
+-------------------------------
+
+5.1 The CAN controller wants to send data, no data from Main Controller
+-----------------------------------------------------------------------
+
+The CAN controller sets the GPIO to raise an interrupt on the Main Controller.
+The CAN controller sets always the GPIO if it has something to sent, and it is
+duty of the Main Controller to disable and enable the interrupt source depending
+on the status of the transmission.
+
+After getting the interrupt, the Main Controller (that has no knowledge about
+the amount of data to be transfered from the CAN controller), will start a SPI
+transfer for a total of 32 bytes (default), sending a REQ_DATA message.
+
+This is the best-effort way for the Main Controller, as it will transfer so many
+bytes as the internal FIFO can, and only one ISR is raised to terminate the
+transfer.
+Taking into account that the header requires 3 bytes (MSG-ID + length) and the
+checksum further two bytes, there are still 27 bytes available for the CAN
+messages.
+This is always enough to send at least one CAN message.
+
+The CAN controller answers with the SEND_DATA_MSG. The Main Controller knows
+that there is no message from the Main Controller, because the MSG-ID is not
+set to SEND_DATA, and acquires the number of bytes to be transfered from the
+length field.
+The CAN controller packs so many CAN messages inside the SEND_DATA_MSG
+(usually 1 or 2), delimiting the packet with the checksum as described before.
+
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                         |
+                       |__________________________________________________|
+
+
+
+ CAN Contr.,'''''''''''''''''''''',''''''''''''''''''''''''''''|
+ (MISO)    |Do not care (4 bytes) |  Send DATA MSG             |
+           |                      |                            |
+           '`''''''''''''''''''''''`''''''''''''''''''''''''''''
+                              32 bytes long transfer
+   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
+
+There are two options at this point:
+
+5.2 The CAN controller has sent all CAN messages.
+-------------------------------------------------
+
+In this case, the CAN controller deasserts the GPIO line to avoid further
+interrupts for the Main Controller.
+
+5.3 The CAN controller has more data to be sent.
+------------------------------------------------
+
+It can send a STATUS_MSG message, if it fits into the current transfer.
+There is no rule about when sending this message. The main constraint for
+the CAN controller is that the packets must not be fragmented, that is, there is
+still enough place inside the current frame to send the STATUS_MSG.
+The CAN controller can decide also to send the Status Message before the
+SEND_DATA_MSG.
+Anyway, because the length of a STATUS_MSG is fixed (5 bytes), it is sure that
+the CAN controller is always able to send a SEND_DATA_MSG and a STATUS_MSG
+inside a standard (=32 bytes) SPI transfer.
+
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                         |
+                       |__________________________________________________|
+
+
+CAN Controller   ,'''''''''''''''''''''''''''''',''''''''''''''|
+                 |   SEND DATA                  |  STATUS      |
+                 |                              |              |
+                  `''''''''''''''''''''''''''''''`'''''''''''''
+
+
+After receiving the STATUS_MSG, the Main Controller is aware of the number of
+bytes the S12 wants to send. It starts without waiting for the GPIO-ISR
+and sends (if it has no messages for the CAN controller) a new REQ_DATA
+with the length of the SPI transfer, followed by dummies 0x00.
+The length could be changed by the Main Controller, but it must not be smaller
+as the value required by the Main Controller in the STATUS_MSG.
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                             |
+                       |______________________________________________________|
+
+
+                  ,''''''''''''''''''''''''''''  ,''''''''''''''''''''''''''''',
+                  |   SEND DATA                  |   SEND DATA                 |
+                  |                              |                             |
+                   `'''''''''''''''''''''''''''   `'''''''''''''''''''''''''''''
+
+The CAN controller has now enough SPI clock cycles to send all data
+to the Main Controller. After sending all data, it will fill also any
+remaining bytes until the end of transfer with dummy bytes.
+
+In case there is no place for the STATUS_MSG and because it is not allowed
+to fragment packets, the CAN controller must maintain assert the GPIO-IRQ line
+and wait until the Main Controller will start a new transfer again.
+
+5.4 Both CAN controller and Main Controller has data to be sent
+----------------------------------------------------------------
+
+The CAN controller asserts the GPIO line to raise an interrupt. The interrupt
+can be served or not, depending if the Main Controller has already recognized
+that it must send data. The GPIO is used from the Main Controller as
+level-interrupt, and the Main Controller is able to activate it when no transfer
+is running, and to deactivate it when it already knows there are messages
+to be exchanged.
+
+The scenario is quite the same as previously:
+
+
+
+      ,'''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+Main  |   SEND DATA                                             | ...........
+      |                                                         |
+       `''''''''''''''''''''''''''''''`''''''''''''''''''''''''''
+
+                  ,'''''''''''''''''''''''''''',
+CAN Controller    |   SEND DATA                |
+                  |                            |
+                   `'''''''''''''''''''''''''''
+
+The Main Controller starts with the SEND_DATA_MSG. If it has a lot of CAN
+messages to be sent, it sets accordingly the length field and it packs all CAN
+messages inside the same single SEND_DATA_MSG.
+
+The CAN Controller, that has something to send, will answer with the
+SEND_DATA_MSG, too.
+Because it has already received the length of the incoming message, it is
+aware about how many bytes are transfered and can act as in 5.3 if it has more
+messages to send.
+
+5.5 The CAN controller gets new data during a transfer
+------------------------------------------------------
+
+In this case, there are the following options:
+
+- the new messages can be fit inside the actual transfer.
+Then the CAN controller will append a new SEND_DATA_MSG to the end of the
+data that it is currently sending on the SPI line.
+
+ Main     ,''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI) '''| REQ_DATA                                                          |
+           |___________________________________________________________________|
+
+
+                  ,''''''''''''''''''''''''''''  ,''''''''''''''''''''''''''''',
+CAN Controller    |   SEND DATA                  |   SEND DATA                 |
+                  |                              |                             |
+                   `'''''''''''''''''''''''''''   `'''''''''''''''''''''''''''''
+			^--at this point, new can message(s) is(are) received
+
+- the new messages cannot be fit inside the actual SPI transfer
+but there is place for a STATUS_MSG.
+
+The CAN controller will append a STATUS_MSG after the SEND_DATA as in 5.3.
+After receiving STATUS_MSG, the Main Controller will start a new SPI transfer
+for the requested amout of data.
+
+
+ Main      ,'''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI) '''| REQ_DATA                                          |
+           |___________________________________________________|
+
+
+
+CAN Controller   ,'''''''''''''''''''''''''''''',''''''''''''''|
+                 |   SEND DATA                  |  STATUS      |
+                 |                              |              |
+                  `''''''''''''''''''''''''''''''`'''''''''''''
+
+- There is no place for the new message and/or the STATUS_MSG
+
+In this case, and because it is not allowed to fragment packets, the CAN
+controller must maintain assert the GPIO-IRQ line and wait until the Main
+Controller will start a new transfer again.
+
+5.6 Communication with upper layer
+----------------------------------
+
+After reception of data, the CAN-Main Controller driver should demux data and
+send them to the correct CAN interface via the API provided by socket CAN.
+
+6. Time synchronization
+----------------------------
+
+As the CAN controller sends a timestamp to the Main Controller, it is required
+to have the same time base. The CAN controller inserts timestamp with 1mSec
+resolution, because it is programmed with 1mSec timer interrupt.
+This resolution is enough for diagnostic protocols and higher resolution
+timestamps are not reliable inside the CAN controller itself.
+
+To allow to the Main Controller to compute the absolute time, the Main
+Controller and the CAN controller must compute elapsed time from the same base.
+
+The SYNC_MSG is used to reset the CAN controller's internal counter.
+The Main Controller sends this message at the start up, and it can send it
+when there is no traffic to maintain time synchronized.
+The following actions are taken:
+
+1. The Main Controller sends a SYNC_MSG, and stores its absolute time.
+   The SYNC_MSG is sent as single message in a 32 byte transfer.
+   The remaining bytes are filled with zeros by the Main Controller.
+   This makes easy to recognize the SYNC_MSG if the snchronization is lost.
+2. After receiving the SYNC_MSG, the CAN controller resets its internal
+   counter to zero.
+   As the SYNC_MSG is used to signalize the start of frame, the CAN controller
+   synchronize itself for the next transfer.
+
+When the CAN controller will send data, it will add the elapsed milliseconds
+from the receiving of the SYNC_MSG as timestamps. As 4 bytes are used for
+timestamp, the timestamp will roll over after 1193 days.
+
+7. CAN configuration
+----------------------------------
+
+It is foreseen that the CAN controller's CAN channels are configured in the
+usual way under Linux via the "ip" command. In fact, the CAN interface cannot
+be set to "up" if it was not configured before.
+
+With ip, the following parameters can be set:
+
+ip link set DEVICE type can
+	[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |
+	[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1
+	  phase-seg2 PHASE-SEG2 [ sjw SJW ] ]
+
+	[ loopback { on | off } ]
+	[ listen-only { on | off } ]
+	[ triple-sampling { on | off } ]
+	[ one-shot { on | off } ]
+	[ berr-reporting { on | off } ]
+
+	[ restart-ms TIME-MS ]
+	[ restart ]
+
+	Where: BITRATE       := { 1..1000000 }
+	       SAMPLE-POINT  := { 0.000..0.999 }
+	       TQ            := { NUMBER }
+	       PROP-SEG      := { 1..8 }
+	       PHASE-SEG1    := { 1..8 }
+	       PHASE-SEG2    := { 1..8 }
+	       SJW           := { 1..4 }
+	       RESTART-MS    := { 0 | NUMBER }
+
+After using the ip command, the driver will receive from the socketCAN layer
+the following structure, defined in include/linux/can/netlink.h:
+
+/*
+ * CAN bit-timing parameters
+ *
+ * For futher information, please read chapter "8 BIT TIMING
+ * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
+ * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
+ */
+struct can_bittiming {
+        __u32 bitrate;          /* Bit-rate in bits/second */
+        __u32 sample_point;     /* Sample point in one-tenth of a percent */
+        __u32 tq;               /* Time quanta (TQ) in nanoseconds */
+        __u32 prop_seg;         /* Propagation segment in TQs */
+        __u32 phase_seg1;       /* Phase buffer segment 1 in TQs */
+        __u32 phase_seg2;       /* Phase buffer segment 2 in TQs */
+        __u32 sjw;              /* Synchronisation jump width in TQs */
+        __u32 brp;              /* Bit-rate prescaler */
+};
+
+The parameters are not interpreted by the driver, but they are encapsulated
+as they are in a SPI frame with MSG-ID=CFG_MSG_SET and sent to the corresponding CAN
+channel.
+It is then duty of the CAN controller to set up the hardware on base of
+the received parameters. According to the rules set in this document,
+single parameters are sent in a big-endian order (MSB first).
+
+A special case is the setup for the CFG channel. This channel is not mapped to
+a real CAN bus, but it is used to instantiate a communication
+between the CAN controller software and the Application in User Space.
+Data are not interpreted at all by the driver, and anybody can set its internal
+protocol to add commands and features to the CAN controller software.
+One example is the Watchdog triggering already mentioned.
+The CAN_MSG is ignored by the CAN controller for the CFG channel, and its values
+do not change the hardware setup.
+Using this message it is possible to switch the CAN controller between two
+operational modes:
+	- Supervisor / Maintenance mode:	slow, max SPI frequency 1 Mhz
+	- User / Normal mode			max SPI frequency set at startup
+
+The driver will start setting the SPI frequency to the lower value.
+When the CFG channel is opened, the driver checks the bitrate of the
+can_bittiming structure, and takes the following actions:
+
+	- bitrate = 125000		sets SPI frequency to low value
+	- bitrate > 125000		sets SPI frequency to high value
+
+In user space it is then possible to switch the CAN controller between the two
+operational modes simply issueing the "ip link" command and setting
+the bitrate for the CFG channel.
+
+To set the CAN controller in supervisor mode:
+	ip link set cfg type can bitrate 125000
+
+To set the CAN controller in user mode:
+	ip link set cfg type can bitrate 500000
+
+
+8. SPI Frame definition and Messages
+----------------------------------------
+
+A SPI Frame begins with a 4-bytes header:
+
+bytes:
+	0		Message ID
+	1-2		Message Length.
+			Length is computed including 2 bytes for checksum,
+			but without Message ID and Length itself.
+	3 .. N-2	Depending on Message-ID
+	N-1 .. N	Checksum
+
+Command codes:
+	0x01	Status Request
+	0x02	Send Data
+	0x03	Sync message
+	0x04	Configuration Message
+	....	t.b.d.
+
+8.1 Format of Send Data Message
+-------------------------------
+
+  _____,________________
+ |     |                ,''''''''''''''''',''''''''''''|       ,'''''''|
+ |ID=2 | LENGTH         | CAN MESSAGE     |CAN MESSAGE |       |CHECKSUM
+ |_____L________________|_________________|____________|       L_______|
+                     _.-'                  `--._
+                _,,-'                           ``-.._
+            _.-'                                      `--._
+       _,--'                                               ``-.._
+   ,.-'                                                          `-.._
+   ,'''''',''''''''''''','''''''''''''','''''',''''''|     ,'''''''''|
+   | CH   |TIMESTAMP    |  CAN ID      |DLC   |D[0]  |     |D[dlc-1] |
+   L______L_____________|______________L______|______|     L_________|
+
+
+	Offset
+	0		0x02 (SEND_DATA_MSG)
+	1 - 2		Length of message
+
+
+Each CAN message has the following format
+
+	0		Channel Number
+			Values:
+			1-n	CAN Channel
+			0xFF	CFG channel
+	1 - 4		Timestamp
+	5 - 8		can_id
+	9		dlc (length of can message)
+	10..N		Variable number of data (max 8)
+
+Note: Channel 0 is reserved for configuration.
+
+The flag for Standard and Extended frame format (SFF and EFF
+in socketCAN framework) is a flag in the can_id.
+
+8.2 Format of Status Message
+----------------------------
+
+  _____,________________
+ |     |                ,''''''''''''''''''|'''''''|
+ |ID=1 | LENGTH=4       | Length (2 bytes) |CHECKSUM
+ |_____L________________|__________________|_______|
+
+
+	Offset
+	0	0x01 (STATUS_MSG)
+	1-2	4
+	3-4	Desired mionimal length for next transfer
+	5-6	Checksum
+
+8.3 Format of Sync Message
+--------------------------
+
+  _____,________________
+ |     |                ,'''''''''''''''''''|'''''''|
+ |ID=3 | LENGTH=6       | AA | 55 | 55 | AA |CHECKSUM
+ |_____L________________|____|____|____|____|_______|
+
+
+	Offset
+	0	0x02 (SYNC_MSG)
+	1-2	6
+	3	0xAA
+	4	0x55
+	5	0x55
+	6	0xAA
+	7-8	Checksum
+
+8.5 Format of Set Configuration Message
+----------------------------------------
+  _____,________
+ |     |        ,'''''''''''''''''''''''''''''''''''''''''|'''''''''|..
+ |ID=4 | LEN=40 | channel | Enabled  |  bitrate | sample  | tq      |  |CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..
+
+
+	Offset
+	0	0x04 (CFG_MSG_SET)
+	1-2	36
+	3	channel number
+		0xFF = CFG
+		0..n = CAN controller CAN channel
+	4	Enabled/disabled
+		Bit 0 is defined.
+			0 = disabled
+			1 = enabled
+		bit 1..7 = reserved (do not care)
+	5-8	bitrate
+	9-12	sample point
+	13-16	Time quanta (tq)
+	17-20	Propagation segment
+	21-24	Phase Buffer segment 1
+	25-28	Phase Buffer segment 2
+	29-32	Sync jump width
+	33-36	Bit rate prescaler
+	37-40	ctrlmode
+	41-42	Checksum
+
+There is no answer from the CAN controller after receiving a CFG_MSG_SET, and the
+CAN controller is not allowed to send a data frame in answer to a CFG_MSG_SET. After
+setting the channel, the communication will go on as usual.
+
+8.6 Format of REQ_DATA Message
+------------------------------
+
+  _____,________________
+ |     |                ,''''''''''''''''''''''''''|'''''''|
+ |ID=5 | LENGTH=n       | 00 | 00 | 00 |           |CHECKSUM
+ |_____L________________|____|____|____|___________|_______|
+
+
+	Offset
+	0	0x05 (REQ_DATA)
+	1-2	Length of the message.
+		This value is not fiixed and tells the CAN controller
+		how many bytes it can send back
+	3:(n-2)	Filled with zero
+	(n-1):n	Checksum
+
+8.6 Format of Get Configuration Message
+----------------------------------------
+
+Format of the frame sent by Main Controller:
+  _____,________
+ |     |        ,'''''''''|'''''''''|
+ |ID=6 | LEN=3  | channel |CHECKSUM |
+ |_____L________|_________|_________|
+
+Answer from CAN Controller:
+  _____,________
+ |     |        ,''''''''''''''''''''|''''''''''|'''''''''|'''''''''|..
+ |ID=6 | LEN=25 | channel |tseg1_min |tseg2_max |tseg2_min|         |CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..
+
+	Offset
+	0	0x06 (CFG_MSG_GET)
+	1-2	25
+	3	channel number
+		0xFF = CFG
+		0..n = CAN controller CAN channel
+	5	tseg1_min
+	6	tseg1_max
+	7	tseg2_min
+	8	tseg2_max
+	9	sjw_max
+	10-13	brp_min
+	14-17	brp_max
+	18-21	brp_inc
+	22	ctrlmode
+	23-27	clock
+	28-29	Checksum
+
+The CAN controller answers after receiving a CFG_MSG_GET with the can bittiming and
+capabilities. The meaning of bittiming is as explained in netlink.h for
+struct can_bittiming_const.
+
+ctrlmode contains the CAN controller capabilities. It is a wired-or byte, whose bits
+are defined in netlink.h.
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v5 1/2] Add documentation for SPI to CAN driver

Hi Stefano,

sorry that I did not find some time to review your driver again :-(

I found some small issues:

Documentation/networking/spi_can.txt

Quoted 6 lines · Stefano Babic
Documentation/networking/spi_can.txt new file +3 -0
+The CFG_MSG_GET is sent at the start up to query the CAN controller about its
+internal timing, making them available to the netlink interface. CAN drivers
+have usually fest values for can_bittiming_const.

fest -> fixed

Documentation/networking/spi_can.txt

Quoted 8 lines

In case of a remote

Documentation/networking/spi_can.txt new file +7 -0
+CAN controller driven via SPI, the Main Controller cannot know the timing
+and must ask them to the CAN controller before instantiating a CAN device.
+For this reason, the message is sent only once in the probe() entry point.
+
+The REQ_DATA is sent only by the Main Controller when the Main Controller
+signals it has packets to be transfered, but the Main Controller has nothing
+to sent.

?? Didn't understand this - really ;-)

Quoted 20 lines

+8.6 Format of Get Configuration Message
+----------------------------------------

Documentation/networking/spi_can.txt new file +18 -0
+
+Format of the frame sent by Main Controller:
+  _____,________
+ |     |        ,'''''''''|'''''''''|
+ |ID=6 | LEN=3  | channel |CHECKSUM |
+ |_____L________|_________|_________|
+
+Answer from CAN Controller:
+  _____,________
+ |     |        ,''''''''''''''''''''|''''''''''|'''''''''|'''''''''|..
+ |ID=6 | LEN=25 | channel |tseg1_min |tseg2_max |tseg2_min|         |CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..
+
+	Offset
+	0	0x06 (CFG_MSG_GET)
+	1-2	25
+	3	channel number
+		0xFF = CFG

Documentation/networking/spi_can.txt line 765

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+		0..n = CAN controller CAN channel
+	5	tseg1_min
+	6	tseg1_max
+	7	tseg2_min
+	8	tseg2_max
+	9	sjw_max
+	10-13	brp_min
+	14-17	brp_max
+	18-21	brp_inc
+	22	ctrlmode

Why is ctrlmode not 32 bit?

Documentation/networking/spi_can.txt line 767

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+	23-27	clock
+	28-29	Checksum

Some more stuff in the other patch.

Regards,
Oliver

#3 lore raw

Re: [PATCH v5 1/2] Add documentation for SPI to CAN driver

Hi Oliver,

Quoted 3 lines · Oliver Hartkopp

Hi Stefano,

sorry that I did not find some time to review your driver again :-(

Do not worry, I understand ;-)

Quoted 7 lines

I found some small issues:

Quoted 1 line · Stefano Babic
Quoted 7 lines
Quoted 5 lines

+The CFG_MSG_GET is sent at the start up to query the CAN controller about its
+internal timing, making them available to the netlink interface. CAN drivers
+have usually fest values for can_bittiming_const.

fest -> fixed

ooppps...wrong language, I fix it !

Quoted 16 lines
Quoted 10 lines

In case of a remote
+CAN controller driven via SPI, the Main Controller cannot know the timing
+and must ask them to the CAN controller before instantiating a CAN device.
+For this reason, the message is sent only once in the probe() entry point.

(quoted diff) +2 -0
+
+The REQ_DATA is sent only by the Main Controller when the Main
Quoted 4 lines

Controller
+signals it has packets to be transfered, but the Main Controller has nothing
+to sent.

?? Didn't understand this - really ;-)

Well, I read the phrase again and I confess I cannot understand it myself :-D

I try to rephrase it:

The REQ_DATA is a replacement for the SEND_DATA message. It is sent by the Main Controller when the Slave Controller has signalled in a previous frame that it has packets to be transfered, but the Main Controller has nothing to sent.

Quoted 27 lines
Quoted 27 lines

+8.6 Format of Get Configuration Message
+----------------------------------------

Documentation/networking/spi_can.txt new file +11 -0
+
+Format of the frame sent by Main Controller:
+  _____,________
+ |     |        ,'''''''''|'''''''''|
+ |ID=6 | LEN=3  | channel |CHECKSUM |
+ |_____L________|_________|_________|
+
+Answer from CAN Controller:
+  _____,________
+ |     |        ,''''''''''''''''''''|''''''''''|'''''''''|'''''''''|..
+ |ID=6 | LEN=25 | channel |tseg1_min |tseg2_max |tseg2_min|        

|CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..

Documentation/networking/spi_can.txt new file +10 -0
+
+    Offset
+    0    0x06 (CFG_MSG_GET)
+    1-2    25
+    3    channel number
+        0xFF = CFG
+        0..n = CAN controller CAN channel
+    5    tseg1_min
+    6    tseg1_max
+    7    tseg2_min
Quoted 12 lines

Documentation/networking/spi_can.txt line 765

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+    8    tseg2_max
+    9    sjw_max
+    10-13    brp_min
+    14-17    brp_max
+    18-21    brp_inc
+    22    ctrlmode

Why is ctrlmode not 32 bit?

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+    23-27    clock
+    28-29    Checksum

I admit this is a limitation on the Slave Controller. I was asked to not send a configuration frame bigger as 32 bytes, and I have tried to save space in some ways. Indeed, only some bits in the ctrlmode are used.

Anyway, I was unsure if I can reduce the size for the brp_* fields. Using 32bit for brp_min looks like excessive, and I could drop its size instead of ctrlmode. Do you think I can use 8bit values for all brp_* fields ?

Quoted 1 line

Some more stuff in the other patch.

I check it, thanks for review !

Best regards,
Stefano

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#4 lore raw

Re: [PATCH v5 1/2] Add documentation for SPI to CAN driver

Quoted 8 lines · Stefano Babic
Quoted 5 lines · Oliver Hartkopp

Why is ctrlmode not 32 bit?

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+    23-27    clock
+    28-29    Checksum
Quoted 8 lines

I admit this is a limitation on the Slave Controller. I was asked to not send a configuration frame bigger as 32 bytes, and I have tried to save space in some ways. Indeed, only some bits in the ctrlmode are used.

Anyway, I was unsure if I can reduce the size for the brp_* fields. Using 32bit for brp_min looks like excessive, and I could drop its size instead of ctrlmode. Do you think I can use 8bit values for all brp_* fields ?

At least for CAN FD these values are increased remarkably. So we should probably leave it as-is.

Quoted 4 lines
Quoted 2 lines

Some more stuff in the other patch.

I check it, thanks for review !

Tnx!

Best regards,
Oliver

#5 lore raw

Re: [PATCH v5 1/2] Add documentation for SPI to CAN driver

In reply to Stefano Babic
Quoted 1 line · Stefano Babic

Here my review of the protocol documentation. As I'm not a native speaker, by remarks about language and grammar are probably not 100% correct :)

Quoted 25 lines

---

Changes in v5:
- drop Patch 2/3, already applied.

Changes in v4:
- added GET_CFG message to query bit timing to the remote controller.

Changes in v3:
- format documentation, check for lines > 80 chars (O. Hartkopp)
- patch 2/3 already aqpplied to can-next, removed from patchset

Changes in v2: None

 Documentation/networking/spi_can.txt | 774 +++++++++++++++++++++++++++++++++++
 1 file changed, 774 insertions(+)
 create mode 100644 Documentation/networking/spi_can.txt
Documentation/networking/spi_can.txt new file +0 -0
new file mode 100644
index 0000000..82f33e7

Documentation/networking/spi_can.txt line 3

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
@@ -0,0 +1,774 @@
+The spi_can driver is intended to provide a general protocol
+to support CAN interface that are not directly connected or
+part of the main controller, but they are available on a

M C

Documentation/networking/spi_can.txt line 4

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+separate controller, such as a low cost/low power microcontroller

(the CAN Controller),

Documentation/networking/spi_can.txt line 9

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+(for example, very simple 16 bit controllers providing multiple CAN
+ busses).
+
+This document is intended to describe the internal protocol
+on the SPI interface between the linux system (main controller)

M C

Documentation/networking/spi_can.txt line 10

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+and the remote CAN controller(s).

a C ^^^ remove this.

Documentation/networking/spi_can.txt line 14

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+1. Overview
+----------------------
+

Can you please define what it the:
- CAN Controller
- Main Controller

As far as I understand the Main Controller is the system running Linux, which has at least a SPI master controller. The CAN Controller is connected via SPI as a slave to the Main Controller.

Documentation/networking/spi_can.txt line 15

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+The CAN controller processors supports up to N Can Busses. The Main Controller

The CAN Controller's processor CAN

Documentation/networking/spi_can.txt line 16

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+driver should be able to manage all CAN busses. It is then

the

Documentation/networking/spi_can.txt line 20

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+responsibility of the SW Main Application to decide which controllers
+(which CAN channels) should be opened and used.
+
+There is the requirement to configure the CAN controller, sending some

C

Documentation/networking/spi_can.txt line 23

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+parameters or commands not strictly related to the CAN interfaces. For example,
+it is required that the application sends periodically a message to the CAN
+controller, that has the responsibility to supervise the Main Controller. If the

C

Documentation/networking/spi_can.txt line 25

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+message is not received in time, the CAN controller must perform a reset of the
+Main Controller controller (Watchdog message).

The CAN Controller will reset the Linux System? I think that should be out of the scope of the specification of this interface.

Quoted 13 lines
Documentation/networking/spi_can.txt new file +13 -0
+Another known use case is to configure the wake-up mechanism, that is
+under which circumstances the CAN controller must awake the Main Controller
+(ignition, opened door,...). The protocol foresees an additional channel (CFG)
+used to exchange data between the application layer and the CAN controller.
+
+The document sets the rules to exchange messages between the Main Controller and
+the CAN controller and allows to extend the functionalities with new messages
+(see Chapter 5) in the future.
+
+2. Interface to upper layer
+------------------------------
+
+The driver must provide a socket CAN interface. In this way, the

Documentation/networking/spi_can.txt line 48

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+application is able to communicate with CAN via usual sockets.
+A detailed description can be found in the kernel documentation
+(Documentation/networking/can.txt).
+
+On the application level there will be network interfaces, and
+they can be set up with Linux tools such as "ifconfig" and "ip".
+
+For the CAN controller configuration it is proposed to add a further network
+device ("cfg") to have a consistent interface. In this way, the
+driver must not expone itself in a different way to the application SW.
                   ^^^^^^

expose?

Documentation/networking/spi_can.txt line 49

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+Configuration packets are simply forward to the CAN controller exactly as it is
                                    ^^^^^^^

forwarded

Documentation/networking/spi_can.txt line 62

Quoted 13 lines
Documentation/networking/spi_can.txt new file +13 -0
+done for the other channels. The application fills data for the cfg
+interface and the Main Controller driver does not need to look inside.
+Configuration data are treated by the CAN driver exactly as all other CAN
+packets.
+Only the SW application and CAN controller-SW are aware of it.
+
+The driver must be able to mux/demux packets from the socket can interface.
+Indeed, there is only one SPI interface to to communicate with the CAN
+controller, and the driver instantiates automatically multiple network
+interfaces.
+On the downstream direction, the driver must take packets from all interfaces
+(hcan0,..hcan4), set up a frame header (message type,
+channel number, etc.) and send it to CAN controller via the SPI interface.

Why are the normal interfaces named "hcan%d"? Why not keep the usual name "can%d"?

Quoted 12 lines
Documentation/networking/spi_can.txt new file +12 -0
+
+
+             hcan0  hcan1 hcan2  hcan3  hcan4 cfg
+               |     |    |       |       |    |
+               |     |    |       |       |    |
+            ,__|_____|____|_______|_______|____|_
+            |                                    |
+            |                                    |
+            |      SOCKET CAN (Kernel)           |
+            |                                    |
+            L________________,.__________________|
+                            ,!!;

Documentation/networking/spi_can.txt line 84

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+                            ,!!;
+                            `!!.
+                            ;!!.
+            ,'''''''''''''''''''''''''''''''''''`.
+            |  +------------------------------.  |
+            |  |    Channel Mux/Demux         |  |
+            |  L______________________________|  |
+            |                                    |
+            |  +------------------------------.  |
+            |  |       Can to SPI controller  |  |
                          ^^^

CAN

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+            |  L______________________________|  |
+            |..................................../
+
+

Documentation/networking/spi_can.txt line 98

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+On the upstream direction, the driver must be able to demux the received
+frames and forward them to the related network interface.
+
+The cfg is a special CAN interface used only to set up the
+CAN controller behavior and does not map to a physical CAN channel, as the other
+CAN interfaces do. The scope of such interface is to provide a way to
+exchange messages with the CAN controller processor that are not strictly
+related to the CAN bus. As example for such messages, a watchdog message must
+be sent regularly by the Main Controller to the CAN controller, and it is sent
+as CAN data to channel that matches the imxhcs12.  The driver is not aware of
                                           ^^^^^^^^

Old name leftover?

Documentation/networking/spi_can.txt line 100

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+these messages, as they are seen as normal data messages for a CAN channel.
+This guarantees that it is possible in future to extend the list of these

^ the

Documentation/networking/spi_can.txt line 108

Quoted 8 lines
Documentation/networking/spi_can.txt new file +8 -0
+configuration messages without changing the driver. The partners that must know
+their format are the application software in user space and the CAN controller
+Software.
+
+3. Interface to HW
+---------------------------
+
+The CAN controller is connected to the Main Controller via the SPI interface.
                                                              ^^^

nitpick: "a", usually modern SoCs have more than one SPI controller :)

Documentation/networking/spi_can.txt line 113

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+
+3.1 SPI Hardware Connection
+---------------------------
+
+ - Main Controller is always the Master and the CAN controller in slave mode.

^m C

Documentation/networking/spi_can.txt line 117

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+ - Maximal frequency:
+   The limit is set by the CAN controller, because it must serve an interrupt
+   request before the next two bytes are sent.

The maximal frequency is limited by the CAN controller, ...
- or -
The limit is given by the CAN controller, ...

Documentation/networking/spi_can.txt line 123

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+
+   The CAN controller has two operational modes: "User Mode" and
+   "Supervisor / Maintenance" Mode. The last one is used to update
+   the software on the CAN controller.
+   In Supervisor Mode, the CAN controller runs with little support for its
+   peripherals. The maximum SPI frequency is limited to 1 Mhz.

The maximum frequency depends on the actual CAN controller, isn't it?

Documentation/networking/spi_can.txt line 129

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+   The driver must support two different operational frequencies,
+   and must be able to switch between them.
+   This is done using the CFG_MSG_SET for the CFG channel, see details in chapter 6.
+
+ - bit x words : it was tested with 32 bits. This means that the Main Controller
+   deactive the chip select automatically (without intervention by Software)
      ^^^^^^^^

deactivates

Documentation/networking/spi_can.txt line 131

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+   each 4 bytes. No interrupts are generated on the Main Controller because they
+   are ruled by the internal FIFO.

Why is this point mentioned here?

IIRC this is a non standard mode of SPI operation, usually the chip select stays active while the communication is taking place with the SPI slave. Your original code is based on a i.MX SoC, right? IIRC in the mx35 the chip select generated by the SPI controller has a big limitation (bug), so we always used the GPIO chip select.

Documentation/networking/spi_can.txt line 137

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+
+ - GPIO(s):
+	IRQ GPIO: set active by CAN controller when it has something to send
+		it raises an interrupt on Main Controller side.
+
+To transfer data, some frame header will be included. If a field contains more
                     ^^^^ a

Documentation/networking/spi_can.txt line 138

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+as one byte, a big endian convention is used (MSB is transmitted first).

^^
more than

Quoted 8 lines
Documentation/networking/spi_can.txt new file +8 -0
+
+The SPI interface is full duplex. This means, both Main Controller and CAN
+controller are transmitting data at the same time. It is possible to transfer
+CAN messages to and from the CAN controller in the same SPI transfer.
+
+4. SPI Protocol between Main Controller and CAN controller
+----------------------------------------------------------
+

Documentation/networking/spi_can.txt line 156

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+The format of the frames exchanged between Main Controller and CAN controller
+is:
+
+
+    ,'''''''','''''''''''''''','''''''''''''''''''''''','''''''''''''|
+    |MSG ID  |  Length(16 bit)|    DATA                |  CHECKSUM   |
+    L________|________________|________________________|_____________|
+
+The checksum is thought to be used in the development phase to check
+for implementation's error. It should be possible to disable it after
                     ^^

just: implementation

Documentation/networking/spi_can.txt line 158

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+the development is finished. The SPI interface ensures that data are
+transfered without modification between the two processors.

I'm not sure if the last sentence is correct. As far as I know there isn't any bit error detection or correction on a SPI bus.

Quoted 12 lines
Documentation/networking/spi_can.txt new file +12 -0
+
+The checksum is computed as 16 bit 2's complement of the sum (module 16)
+of  all bytes in the message, excluded the checksum itself.
+On the receive side, it is enough to sum all bytes including the checksum
+to verify if the message was received correctly (the sum must be zero).
+
+The MSG-ID (1 byte) identifies the type of exchange data. For basic exchange
+of frames, the following messages are used:
+
+	SEND_DATA_MSG
+	STATUS_MSG
+	SYNC_MSG

Documentation/networking/spi_can.txt line 180

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+	CFG_MSG_SET
+	CFG_MSG_GET
+	REQ_DATA_MSG
+
+The SEND_DATA_MSG is used when one of the two processors needs to send a CAN
+message.
+
+The STATUS_MSG is used to inform the partner about the number of bytes ready
+to be transfered. This is used by the Main Controller when it has several
+messages to send, and it needs a longer frame as usual to sen all CAN messages
                                                             ^^^ send

Documentation/networking/spi_can.txt line 181

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+back.

What do you mean by the back in: "send all CAN message _back_"?

Documentation/networking/spi_can.txt

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+The SYNC_MSG is used to start up the communication and in case an error is
+detected. It is the first message that the Main Controller will send to the
+CAN controller.                                            ^^^^

Can you use standard https://www.ietf.org/rfc/rfc2119.txt key words when describing you protocol?

That will translates to MUST.

Documentation/networking/spi_can.txt line 188

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+Because the Main Controller will always send the MSG ID as first byte in the
+frame, the SYNC_MSG is used to signalize the start of a frame to the CAN
+controller.

The Main Controller MUST send the MSG ID as the first byte due to the above defined framing, correct?

Documentation/networking/spi_can.txt line 189

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+The SYNC_MSG is used also to initialize or reinitialize the time on the
                   ^^^^^^^^^

also used

Documentation/networking/spi_can.txt line 193

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+CAN controller. It is not required to the CAN controller to have an absolute
+time synchronized to the Main Controller. It is enough to have a relative time,
+and the Main Controller computes the absolute time to add it as timestamp to
+the received packets.

Can you rephrase the above two sentences, they read a bit bumpy ;)

Documentation/networking/spi_can.txt line 195

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+
+The CFG_MSG_SET is sent to configure the CAN channel for bitrate and timing.

the bit rate and timing of the CAN channel
- or -
the CAN channel's bitrate and timing

Documentation/networking/spi_can.txt line 196

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+The driver on the Main Controller does not need to parse the parameters and it

replace the "and" by ","

Documentation/networking/spi_can.txt line 200

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+forwards them to the corresponding CAN controller's CAN interface.
+
+The CFG_MSG_GET is sent at the start up to query the CAN controller about its
+internal timing, making them available to the netlink interface. CAN drivers

^^^^ it (the timing - singular)

Documentation/networking/spi_can.txt line 201

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+have usually fest values for can_bittiming_const. In case of a remote

^^^^ fixed, as Oliver pointed out

Documentation/networking/spi_can.txt line 203

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+CAN controller driven via SPI, the Main Controller cannot know the timing
+and must ask them to the CAN controller before instantiating a CAN device.
                ^^^^^^^ remove

Documentation/networking/spi_can.txt line 208

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+For this reason, the message is sent only once in the probe() entry point.
+
+The REQ_DATA is sent only by the Main Controller when the Main Controller
+signals it has packets to be transfered, but the Main Controller has nothing
+to sent. Its scope is mainly to signalize to the Main Controller the Start of

The second and fourth Mail Controller has probably to be replaced by CAN Controller?

Documentation/networking/spi_can.txt line 210

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+the Frame and to pass in the LENGTH field the number of bytes of the frame
+itself.

Have described the LENGTH field so far? It's probably the size of the whole frame including MSG_ID and CHECKSUM (if appended).

Documentation/networking/spi_can.txt line 211

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+After receiving the REQ_DATA message, the CAN controller knows how many bytes

it is

Documentation/networking/spi_can.txt line 217

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+is allowed to send without fragmenting frames on the Main Controller's side.
+
+4.1 Start of the comunication
+-----------------------------
+
+The Main Controller does not poll continuosly the CAN controller.
                                     ^^^^^^^^^^^

continuously

Documentation/networking/spi_can.txt line 221

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+If it has nothing to send, it waits to get an interrupt on the GPIO-line.
+
+If the CAN controller has packets to be sent, it sets the GPIO-IRQ,
+and this raises an interrupt on Main Controller side.

^^^ remove

Documentation/networking/spi_can.txt line 222

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+

what do you mean by enough?
VVVVVV

Documentation/networking/spi_can.txt line 223

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+The CAN controller can sends enough messages as they can fit inside
                          ^^^^^ send

Documentation/networking/spi_can.txt line 224

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+the SPI transfer. The number of bytes (=SPI clock cycles)

Do you want to say:

The CAN controller can send as many CAN frames as fit inside a SPI transfer?

Documentation/networking/spi_can.txt line 226

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+to be transfered is set by the Main Controller inside the Status Message.
+

I assume on the left is the Main Controller and on the right we have the CAN Controller?

Quoted 15 lines
Documentation/networking/spi_can.txt new file +15 -0
+               |                               |
+               |GPIO(IRQ for Main Controller)  |
+               |<------------------------------'
+               |                               |
+               |  First SPI Transfer started   |
+               |      SPI:REQ_DATA             |
+               |------------------------------>|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |      SPI: Status              |
+               |<------------------------------|
+               |                               |
+               |  New SPI Transfer started     |

Documentation/networking/spi_can.txt line 251

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+               |      SPI:Send Data            |
+               |------------------------------>|
+               |      SPI:Send Data            |
+               |<------------------------------|
+               |                               |
+               |                               |
+               |                               |
+	Example of SPI transfer.
+
+The CAN controller sets the GPIO-IRQ, and this raises an interrupt on the
                                         ^^^ remove

Documentation/networking/spi_can.txt line 255

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+Main Controller.
+The protocol uses several Message-IDs to identify what is really exchanged.
+
+From the Main Controller point of view, a SPI Transfer is initiated when the

Main Controller's

Documentation/networking/spi_can.txt line 256

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+first byte is written into the internal FIFO and it is finished when the all

Under Linux a SPI transfer is usually started with spi_sync() or it's asynchronous variant.

Documentation/networking/spi_can.txt line 258

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+bytes are transmitted. The protocol relies on the positional place of the
+bytes, that means there is no need to wait for a start-frame byte.

I don't get the last sentence, maybe it's get more clear later....

Documentation/networking/spi_can.txt line 268

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+
+On the receive side, due to CAN controller limitations, the Main Controller
+must drop the first 4 bytes received from the CAN controller, as they have
+no meaning.
+It must be ensured by the CAN controller software that the first valid byte
+is the fifth in the SPI transfer.
+
+The Main Controller has no knowledge at the beginning how many messages the
+CAN controller will send and it starts with a 32-bytes transfer because this
+matches the internal FIFO and raises only one interrupt.

We should not put any assumptions about the Main Controller's hardware into the specification of the protocol. Under Linux you can run a (somewhat slow) SPI bus on bit-banged GPIOs. Just specify then length of the SPI frame.

Documentation/networking/spi_can.txt line 269

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+If the CAN controller has communicated with the status message

^^ After

Documentation/networking/spi_can.txt line 273

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+the number of bytes it needs for the next SPI frame, the Main Controller will
+start a SPI transfer for a number of bytes equal or greater
+as the received value.
+Sending imediately a new SPI transfer should minimize the delay to transfer
           ^^^^^^^^^^

immediately

Documentation/networking/spi_can.txt line 275

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+CAN messages into the socket CAN layer, and setting the transfer to
+a multiple of 32 is a best-effort for the CPU load on the Main Controller side,

32 bytes

Documentation/networking/spi_can.txt line 276

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+because it matches the internal FIFO size (tested on i.MX35). However, there is
                                                                there are ^^^^^^^^

Remove the hardware assumption here please.

Documentation/networking/spi_can.txt line 278

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+no restrictions about the number of bytes to be transfered, and it is duty of
+the Main Controller driver to find the most valuable length to be used.

Earlier you say you have to transfer equal or more bytes as requested, now you state there aren't any restrictions.

Documentation/networking/spi_can.txt line 283

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+
+A SPI transfer is *NOT* delimited by changes of the chip select signal. Indeed,
+the chip select is ruled internally by the bits x word setup, and it is made
+suitable for both CAN controller and Main Controller. Bits x words must be a
+multiple of 2, because the CAN controller gets an interrupt each 2 bytes.

Sorry I don't get what you mean...The chip select under Linux is usually controlled on a struct spi_transfer base with the cs_change member of that struct. What you describe sounds like a special hardware feature of the imx35.

Documentation/networking/spi_can.txt line 292

Quoted 9 lines
Documentation/networking/spi_can.txt new file +9 -0
+
+
+5. SPI Communication scenarios
+-------------------------------
+
+5.1 The CAN controller wants to send data, no data from Main Controller
+-----------------------------------------------------------------------
+
+The CAN controller sets the GPIO to raise an interrupt on the Main Controller.

C

Documentation/networking/spi_can.txt line 293

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+The CAN controller sets always the GPIO if it has something to sent, and it is

C

the duty

Documentation/networking/spi_can.txt line 295

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+duty of the Main Controller to disable and enable the interrupt source depending
+on the status of the transmission.

Transmission of the SPI messages?

Documentation/networking/spi_can.txt line 299

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+After getting the interrupt, the Main Controller (that has no knowledge about
+the amount of data to be transfered from the CAN controller), will start a SPI
+transfer for a total of 32 bytes (default), sending a REQ_DATA message.

Specify the length or give it a name that is specified later, e.g. in some kind of table.

Documentation/networking/spi_can.txt line 303

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+This is the best-effort way for the Main Controller, as it will transfer so many
+bytes as the internal FIFO can, and only one ISR is raised to terminate the
+transfer.

Remove the assumptions about the hardware on the Main Controller.

Documentation/networking/spi_can.txt line 306

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+Taking into account that the header requires 3 bytes (MSG-ID + length) and the
+checksum further two bytes, there are still 27 bytes available for the CAN
+messages.

Is the checksum optional or not?
What about the 4 bogus bytes in the beginning of the message?

Documentation/networking/spi_can.txt line 310

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+This is always enough to send at least one CAN message.
+
+The CAN controller answers with the SEND_DATA_MSG. The Main Controller knows
+that there is no message from the Main Controller, because the MSG-ID is not

The Main Controller knows that there is no message from the Main Controller?

Documentation/networking/spi_can.txt line 313

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+set to SEND_DATA, and acquires the number of bytes to be transfered from the
+length field.
+The CAN controller packs so many CAN messages inside the SEND_DATA_MSG

^^ who many? as many as possible/fit?

Documentation/networking/spi_can.txt line 314

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+(usually 1 or 2), delimiting the packet with the checksum as described before.

The checksum was specified as optional?

Quoted 11 lines
Documentation/networking/spi_can.txt new file +11 -0
+
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                         |
+                       |__________________________________________________|
+
+
+
+ CAN Contr.,'''''''''''''''''''''',''''''''''''''''''''''''''''|
+ (MISO)    |Do not care (4 bytes) |  Send DATA MSG             |
+           |                      |                            |

Documentation/networking/spi_can.txt line 335

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+           '`''''''''''''''''''''''`''''''''''''''''''''''''''''
+                              32 bytes long transfer
+   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
+
+There are two options at this point:
+
+5.2 The CAN controller has sent all CAN messages.
+-------------------------------------------------
+
+In this case, the CAN controller deasserts the GPIO line to avoid further
                                                  ^^^^

Better name it IRQ line, it's just an implementation details that it's a GPIO on the Can Controller :)

Documentation/networking/spi_can.txt line 336

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+interrupts for the Main Controller.

This means the IRQ is level triggered, please specify this when describing the IRQ.

Documentation/networking/spi_can.txt line 342

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+
+5.3 The CAN controller has more data to be sent.
+------------------------------------------------
+
+It can send a STATUS_MSG message, if it fits into the current transfer.
+There is no rule about when sending this message. The main constraint for

^^^^^^^^^^^^
when to send

Documentation/networking/spi_can.txt line 343

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+the CAN controller is that the packets must not be fragmented, that is, there is
                                                      this means ^^^^^^^

Documentation/networking/spi_can.txt line 349

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+still enough place inside the current frame to send the STATUS_MSG.
+The CAN controller can decide also to send the Status Message before the
+SEND_DATA_MSG.
+Anyway, because the length of a STATUS_MSG is fixed (5 bytes), it is sure that
+the CAN controller is always able to send a SEND_DATA_MSG and a STATUS_MSG
+inside a standard (=32 bytes) SPI transfer.

Note: above was the length of 32 a bit vague, now you assume that it's 32.

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                         |
+                       |__________________________________________________|

Documentation/networking/spi_can.txt line 364

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+
+
+CAN Controller   ,'''''''''''''''''''''''''''''',''''''''''''''|
+                 |   SEND DATA                  |  STATUS      |
+                 |                              |              |
+                  `''''''''''''''''''''''''''''''`'''''''''''''
+
+
+After receiving the STATUS_MSG, the Main Controller is aware of the number of
+bytes the S12 wants to send. It starts without waiting for the GPIO-ISR
             ^^^

CAN Controller

Documentation/networking/spi_can.txt line 368

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+and sends (if it has no messages for the CAN controller) a new REQ_DATA
+with the length of the SPI transfer, followed by dummies 0x00.
+The length could be changed by the Main Controller, but it must not be smaller
+as the value required by the Main Controller in the STATUS_MSG.

^^ than ^^^^^^^^^^^^^^^ CAN Controller???

Documentation/networking/spi_can.txt line 380

Quoted 12 lines
Documentation/networking/spi_can.txt new file +12 -0
+
+ Main Controller       ,''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI)             '''| REQ_DATA                                             |
+                       |______________________________________________________|
+
+
+                  ,''''''''''''''''''''''''''''  ,''''''''''''''''''''''''''''',
+                  |   SEND DATA                  |   SEND DATA                 |
+                  |                              |                             |
+                   `'''''''''''''''''''''''''''   `'''''''''''''''''''''''''''''
+
+The CAN controller has now enough SPI clock cycles to send all data

C

Documentation/networking/spi_can.txt line 382

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+to the Main Controller. After sending all data, it will fill also any
+remaining bytes until the end of transfer with dummy bytes.
                                                  ^^^^^^^^^^^ 

just "0x0", sounds more professional than dummy bytes.

Documentation/networking/spi_can.txt line 385

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+
+In case there is no place for the STATUS_MSG and because it is not allowed
+to fragment packets, the CAN controller must maintain assert the GPIO-IRQ line
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

must maintain the IRQ line asserted

Documentation/networking/spi_can.txt line 388

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+and wait until the Main Controller will start a new transfer again.
+
+5.4 Both CAN controller and Main Controller has data to be sent
                                               ^^^^^^^^^^^^^^^^^^^ have data to send

Documentation/networking/spi_can.txt line 391

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+----------------------------------------------------------------
+
+The CAN controller asserts the GPIO line to raise an interrupt. The interrupt
                                  ^^^^ IRQ
Quoted 12 lines
Documentation/networking/spi_can.txt new file +12 -0
+can be served or not, depending if the Main Controller has already recognized
+that it must send data. The GPIO is used from the Main Controller as
+level-interrupt, and the Main Controller is able to activate it when no transfer
+is running, and to deactivate it when it already knows there are messages
+to be exchanged.
+
+The scenario is quite the same as previously:
+
+
+
+      ,'''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+Main  |   SEND DATA                                             | ...........

Documentation/networking/spi_can.txt line 413

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+      |                                                         |
+       `''''''''''''''''''''''''''''''`''''''''''''''''''''''''''
+
+                  ,'''''''''''''''''''''''''''',
+CAN Controller    |   SEND DATA                |
+                  |                            |
+                   `'''''''''''''''''''''''''''
+
+The Main Controller starts with the SEND_DATA_MSG. If it has a lot of CAN
+messages to be sent, it sets accordingly the length field and it packs all CAN
            ^^^^^^^^^^ to send

Documentation/networking/spi_can.txt line 417

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+messages inside the same single SEND_DATA_MSG.
+
+The CAN Controller, that has something to send, will answer with the
+SEND_DATA_MSG, too.

If the CAN Controller has something to send, it will ....

Documentation/networking/spi_can.txt line 422

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+Because it has already received the length of the incoming message, it is
+aware about how many bytes are transfered and can act as in 5.3 if it has more
+messages to send.
+
+5.5 The CAN controller gets new data during a transfer

C

Documentation/networking/spi_can.txt line 427

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+------------------------------------------------------
+
+In this case, there are the following options:
+
+- the new messages can be fit inside the actual transfer.
                      ^^^^^^^^^^ fits

Documentation/networking/spi_can.txt line 429

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+Then the CAN controller will append a new SEND_DATA_MSG to the end of the
+data that it is currently sending on the SPI line.

What will happen if a new CAN frame comes in while REQ DATA is still running, but the first SEND_DATA is already done and the CAN Controller is already inserting stuffing/dummy 0x0?

Quoted 37 lines
Documentation/networking/spi_can.txt new file +37 -0
+
+ Main     ,''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI) '''| REQ_DATA                                                          |
+           |___________________________________________________________________|
+
+
+                  ,''''''''''''''''''''''''''''  ,''''''''''''''''''''''''''''',
+CAN Controller    |   SEND DATA                  |   SEND DATA                 |
+                  |                              |                             |
+                   `'''''''''''''''''''''''''''   `'''''''''''''''''''''''''''''
+			^--at this point, new can message(s) is(are) received
+
+- the new messages cannot be fit inside the actual SPI transfer
+but there is place for a STATUS_MSG.
+
+The CAN controller will append a STATUS_MSG after the SEND_DATA as in 5.3.
+After receiving STATUS_MSG, the Main Controller will start a new SPI transfer
+for the requested amout of data.
+
+
+ Main      ,'''''''''''''''''''''''''''''''''''''''''''''''''''|
+ (MOSI) '''| REQ_DATA                                          |
+           |___________________________________________________|
+
+
+
+CAN Controller   ,'''''''''''''''''''''''''''''',''''''''''''''|
+                 |   SEND DATA                  |  STATUS      |
+                 |                              |              |
+                  `''''''''''''''''''''''''''''''`'''''''''''''
+
+- There is no place for the new message and/or the STATUS_MSG
+
+In this case, and because it is not allowed to fragment packets, the CAN
+controller must maintain assert the GPIO-IRQ line and wait until the Main
+Controller will start a new transfer again.
+

Documentation/networking/spi_can.txt line 476

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+5.6 Communication with upper layer
+----------------------------------
+
+After reception of data, the CAN-Main Controller driver should demux data and
+send them to the correct CAN interface via the API provided by socket CAN.
+
+6. Time synchronization
+----------------------------
+
+As the CAN controller sends a timestamp to the Main Controller, it is required

C

Documentation/networking/spi_can.txt line 477

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+to have the same time base. The CAN controller inserts timestamp with 1mSec

C ^^^^^ 1ms

Documentation/networking/spi_can.txt line 478

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+resolution, because it is programmed with 1mSec timer interrupt.

In the protocol we should not car about the internals of the CAN Controller.

Documentation/networking/spi_can.txt line 483

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+This resolution is enough for diagnostic protocols and higher resolution
+timestamps are not reliable inside the CAN controller itself.
+
+To allow to the Main Controller to compute the absolute time, the Main
+Controller and the CAN controller must compute elapsed time from the same base.

C

Documentation/networking/spi_can.txt line 485

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+
+The SYNC_MSG is used to reset the CAN controller's internal counter.

C

Documentation/networking/spi_can.txt line 490

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+The Main Controller sends this message at the start up, and it can send it
+when there is no traffic to maintain time synchronized.
+The following actions are taken:
+
+1. The Main Controller sends a SYNC_MSG, and stores its absolute time.

How is the absolute time encoded? ms since 1970? Or ms since an arbitrary value?

Documentation/networking/spi_can.txt line 493

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+   The SYNC_MSG is sent as single message in a 32 byte transfer.
+   The remaining bytes are filled with zeros by the Main Controller.
+   This makes easy to recognize the SYNC_MSG if the snchronization is lost.

synchronization

Documentation/networking/spi_can.txt line 500

Quoted 7 lines
Documentation/networking/spi_can.txt new file +7 -0
+2. After receiving the SYNC_MSG, the CAN controller resets its internal
+   counter to zero.
+   As the SYNC_MSG is used to signalize the start of frame, the CAN controller
+   synchronize itself for the next transfer.
+
+When the CAN controller will send data, it will add the elapsed milliseconds
+from the receiving of the SYNC_MSG as timestamps. As 4 bytes are used for

^^^^ since

Documentation/networking/spi_can.txt line 501

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+timestamp, the timestamp will roll over after 1193 days.

-> 2^32 / 1000 / 60 / 60 / 24
= 49.710270

I've calculated 49 days.

Documentation/networking/spi_can.txt line 510

Quoted 9 lines
Documentation/networking/spi_can.txt new file +9 -0
+
+7. CAN configuration
+----------------------------------
+
+It is foreseen that the CAN controller's CAN channels are configured in the
+usual way under Linux via the "ip" command. In fact, the CAN interface cannot
+be set to "up" if it was not configured before.
+
+With ip, the following parameters can be set:

This should not be scope of the protocol documentation, just refer the existing CAN documentation in the kernel.

Quoted 38 lines
Documentation/networking/spi_can.txt new file +38 -0
+
+ip link set DEVICE type can
+	[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |
+	[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1
+	  phase-seg2 PHASE-SEG2 [ sjw SJW ] ]
+
+	[ loopback { on | off } ]
+	[ listen-only { on | off } ]
+	[ triple-sampling { on | off } ]
+	[ one-shot { on | off } ]
+	[ berr-reporting { on | off } ]
+
+	[ restart-ms TIME-MS ]
+	[ restart ]
+
+	Where: BITRATE       := { 1..1000000 }
+	       SAMPLE-POINT  := { 0.000..0.999 }
+	       TQ            := { NUMBER }
+	       PROP-SEG      := { 1..8 }
+	       PHASE-SEG1    := { 1..8 }
+	       PHASE-SEG2    := { 1..8 }
+	       SJW           := { 1..4 }
+	       RESTART-MS    := { 0 | NUMBER }
+
+After using the ip command, the driver will receive from the socketCAN layer
+the following structure, defined in include/linux/can/netlink.h:
+
+/*
+ * CAN bit-timing parameters
+ *
+ * For futher information, please read chapter "8 BIT TIMING
+ * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
+ * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
+ */
+struct can_bittiming {
+        __u32 bitrate;          /* Bit-rate in bits/second */
+        __u32 sample_point;     /* Sample point in one-tenth of a percent */
+        __u32 tq;               /* Time quanta (TQ) in nanoseconds */

Documentation/networking/spi_can.txt line 558

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+        __u32 prop_seg;         /* Propagation segment in TQs */
+        __u32 phase_seg1;       /* Phase buffer segment 1 in TQs */
+        __u32 phase_seg2;       /* Phase buffer segment 2 in TQs */
+        __u32 sjw;              /* Synchronisation jump width in TQs */
+        __u32 brp;              /* Bit-rate prescaler */
+};
+
+The parameters are not interpreted by the driver, but they are encapsulated
+as they are in a SPI frame with MSG-ID=CFG_MSG_SET and sent to the corresponding CAN
+channel.

This is not a good idea, as a kernel internal change will break your firmware.

Quoted 13 lines
Documentation/networking/spi_can.txt new file +13 -0
+It is then duty of the CAN controller to set up the hardware on base of
+the received parameters. According to the rules set in this document,
+single parameters are sent in a big-endian order (MSB first).
+
+A special case is the setup for the CFG channel. This channel is not mapped to
+a real CAN bus, but it is used to instantiate a communication
+between the CAN controller software and the Application in User Space.
+Data are not interpreted at all by the driver, and anybody can set its internal
+protocol to add commands and features to the CAN controller software.
+One example is the Watchdog triggering already mentioned.
+The CAN_MSG is ignored by the CAN controller for the CFG channel, and its values
+do not change the hardware setup.
+Using this message it is possible to switch the CAN controller between two

Documentation/networking/spi_can.txt line 581

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+operational modes:
+	- Supervisor / Maintenance mode:	slow, max SPI frequency 1 Mhz
+	- User / Normal mode			max SPI frequency set at startup
+
+The driver will start setting the SPI frequency to the lower value.
+When the CFG channel is opened, the driver checks the bitrate of the
+can_bittiming structure, and takes the following actions:
+
+	- bitrate = 125000		sets SPI frequency to low value
+	- bitrate > 125000		sets SPI frequency to high value

125 kbit/s? Why this arbitrary value?

Documentation/networking/spi_can.txt line 591

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+
+In user space it is then possible to switch the CAN controller between the two
+operational modes simply issueing the "ip link" command and setting
+the bitrate for the CFG channel.
+
+To set the CAN controller in supervisor mode:
+	ip link set cfg type can bitrate 125000
+
+To set the CAN controller in user mode:
+	ip link set cfg type can bitrate 500000

Can you come up with a cleaner solution for this problem?

Documentation/networking/spi_can.txt line 603

Quoted 12 lines
Documentation/networking/spi_can.txt new file +12 -0
+
+
+8. SPI Frame definition and Messages
+----------------------------------------
+
+A SPI Frame begins with a 4-bytes header:
+
+bytes:
+	0		Message ID
+	1-2		Message Length.
+			Length is computed including 2 bytes for checksum,
+			but without Message ID and Length itself.

So N in this example?

Documentation/networking/spi_can.txt line 605

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+	3 .. N-2	Depending on Message-ID
+	N-1 .. N	Checksum

Is the checksum optional or not?

Documentation/networking/spi_can.txt line 612

Quoted 7 lines
Documentation/networking/spi_can.txt new file +7 -0
+
+Command codes:
+	0x01	Status Request
+	0x02	Send Data
+	0x03	Sync message
+	0x04	Configuration Message
+	....	t.b.d.

Can you please reuse the name you've established before? Like SEND_DATA_MSG. In the beginning of this document there are 6 messages defined.

Quoted 25 lines
Documentation/networking/spi_can.txt new file +25 -0
+
+8.1 Format of Send Data Message
+-------------------------------
+
+  _____,________________
+ |     |                ,''''''''''''''''',''''''''''''|       ,'''''''|
+ |ID=2 | LENGTH         | CAN MESSAGE     |CAN MESSAGE |       |CHECKSUM
+ |_____L________________|_________________|____________|       L_______|
+                     _.-'                  `--._
+                _,,-'                           ``-.._
+            _.-'                                      `--._
+       _,--'                                               ``-.._
+   ,.-'                                                          `-.._
+   ,'''''',''''''''''''','''''''''''''','''''',''''''|     ,'''''''''|
+   | CH   |TIMESTAMP    |  CAN ID      |DLC   |D[0]  |     |D[dlc-1] |
+   L______L_____________|______________L______|______|     L_________|
+
+
+	Offset
+	0		0x02 (SEND_DATA_MSG)
+	1 - 2		Length of message
+
+
+Each CAN message has the following format
+

Documentation/networking/spi_can.txt line 647

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+	0		Channel Number
+			Values:
+			1-n	CAN Channel
+			0xFF	CFG channel
+	1 - 4		Timestamp
+	5 - 8		can_id
+	9		dlc (length of can message)
+	10..N		Variable number of data (max 8)
+
+Note: Channel 0 is reserved for configuration.

What's Channel 0xff then?

Documentation/networking/spi_can.txt line 650

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+
+The flag for Standard and Extended frame format (SFF and EFF
+in socketCAN framework) is a flag in the can_id.

Better use a separate define, although these flags are Linux userspace API/ABI

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+8.2 Format of Status Message
+----------------------------
+

Documentation/networking/spi_can.txt line 664

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+  _____,________________
+ |     |                ,''''''''''''''''''|'''''''|
+ |ID=1 | LENGTH=4       | Length (2 bytes) |CHECKSUM
+ |_____L________________|__________________|_______|
+
+
+	Offset
+	0	0x01 (STATUS_MSG)
+	1-2	4
+	3-4	Desired mionimal length for next transfer

minimal

Quoted 24 lines
Documentation/networking/spi_can.txt new file +24 -0
+	5-6	Checksum
+
+8.3 Format of Sync Message
+--------------------------
+
+  _____,________________
+ |     |                ,'''''''''''''''''''|'''''''|
+ |ID=3 | LENGTH=6       | AA | 55 | 55 | AA |CHECKSUM
+ |_____L________________|____|____|____|____|_______|
+
+
+	Offset
+	0	0x02 (SYNC_MSG)
+	1-2	6
+	3	0xAA
+	4	0x55
+	5	0x55
+	6	0xAA
+	7-8	Checksum
+
+8.5 Format of Set Configuration Message
+----------------------------------------
+  _____,________
+ |     |        ,'''''''''''''''''''''''''''''''''''''''''|'''''''''|..

Documentation/networking/spi_can.txt line 698

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+ |ID=4 | LEN=40 | channel | Enabled  |  bitrate | sample  | tq      |  |CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..
+
+
+	Offset
+	0	0x04 (CFG_MSG_SET)
+	1-2	36
+	3	channel number
+		0xFF = CFG
+		0..n = CAN controller CAN channel

Above you've written that 0x0 is special

Quoted 23 lines
Documentation/networking/spi_can.txt new file +23 -0
+	4	Enabled/disabled
+		Bit 0 is defined.
+			0 = disabled
+			1 = enabled
+		bit 1..7 = reserved (do not care)
+	5-8	bitrate
+	9-12	sample point
+	13-16	Time quanta (tq)
+	17-20	Propagation segment
+	21-24	Phase Buffer segment 1
+	25-28	Phase Buffer segment 2
+	29-32	Sync jump width
+	33-36	Bit rate prescaler
+	37-40	ctrlmode
+	41-42	Checksum
+
+There is no answer from the CAN controller after receiving a CFG_MSG_SET, and the
+CAN controller is not allowed to send a data frame in answer to a CFG_MSG_SET. After
+setting the channel, the communication will go on as usual.
+
+8.6 Format of REQ_DATA Message
+------------------------------
+

Documentation/networking/spi_can.txt line 731

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+  _____,________________
+ |     |                ,''''''''''''''''''''''''''|'''''''|
+ |ID=5 | LENGTH=n       | 00 | 00 | 00 |           |CHECKSUM
+ |_____L________________|____|____|____|___________|_______|
+
+
+	Offset
+	0	0x05 (REQ_DATA)
+	1-2	Length of the message.
+		This value is not fiixed and tells the CAN controller

fixed C

Documentation/networking/spi_can.txt line 732

Quoted 1 line
Documentation/networking/spi_can.txt new file +1 -0
+		how many bytes it can send back

Specify a minimum length here.

Quoted 33 lines
Documentation/networking/spi_can.txt new file +32 -0
+	3:(n-2)	Filled with zero
+	(n-1):n	Checksum
+
+8.6 Format of Get Configuration Message
+----------------------------------------
+
+Format of the frame sent by Main Controller:
+  _____,________
+ |     |        ,'''''''''|'''''''''|
+ |ID=6 | LEN=3  | channel |CHECKSUM |
+ |_____L________|_________|_________|
+
+Answer from CAN Controller:
+  _____,________
+ |     |        ,''''''''''''''''''''|''''''''''|'''''''''|'''''''''|..
+ |ID=6 | LEN=25 | channel |tseg1_min |tseg2_max |tseg2_min|         |CHECKSUM
+ |_____L________|_________|__________|__________|_________|_________..
+
+	Offset
+	0	0x06 (CFG_MSG_GET)
+	1-2	25
+	3	channel number
+		0xFF = CFG
+		0..n = CAN controller CAN channel
+	5	tseg1_min
+	6	tseg1_max
+	7	tseg2_min
+	8	tseg2_max
+	9	sjw_max
+	10-13	brp_min
+	14-17	brp_max
+	18-21	brp_inc

Documentation/networking/spi_can.txt line 774

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+	22	ctrlmode
+	23-27	clock
+	28-29	Checksum
+
+The CAN controller answers after receiving a CFG_MSG_GET with the can bittiming and
+capabilities. The meaning of bittiming is as explained in netlink.h for
+struct can_bittiming_const.
+
+ctrlmode contains the CAN controller capabilities. It is a wired-or byte, whose bits
+are defined in netlink.h.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#6 lore raw

Re: [PATCH v5 1/2] Add documentation for SPI to CAN driver

Hi Marc,

Quoted 5 lines · Marc Kleine-Budde
Quoted 1 line · Stefano Babic

Here my review of the protocol documentation. As I'm not a native speaker, by remarks about language and grammar are probably not 100% correct :)

Thanks a lot ! I fix them - here only the open points:

Quoted 9 lines

Documentation/networking/spi_can.txt line 14

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+1. Overview
+----------------------
+

Can you please define what it the:
- CAN Controller
- Main Controller

Quoted 3 lines

As far as I understand the Main Controller is the system running Linux, which has at least a SPI master controller. The CAN Controller is connected via SPI as a slave to the Main Controller.

Right. I will add their definition in the next version.

Quoted 5 lines

Documentation/networking/spi_can.txt line 25

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+message is not received in time, the CAN controller must perform a reset of the
+Main Controller controller (Watchdog message).

The CAN Controller will reset the Linux System? I think that should be out of the scope of the specification of this interface.

Absolutely. This is a specific part of the project and it has nothing to do with the protocol.

Quoted 6 lines

Documentation/networking/spi_can.txt line 62

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+On the downstream direction, the driver must take packets from all interfaces
+(hcan0,..hcan4), set up a frame header (message type,
+channel number, etc.) and send it to CAN controller via the SPI interface.

Why are the normal interfaces named "hcan%d"? Why not keep the usual name "can%d"?

It was a requirement for my driver to have a different name (all "h" channels are on the slave), but I agree this is uncommon. I will change in the doc and in the code to use the default "can%d".

Quoted 3 lines
Quoted 1 line

+as CAN data to channel that matches the imxhcs12. The driver is not aware of

                                           ^^^^^^^^

Old name leftover?

Yes, it is ;-) !

Quoted 4 lines

Documentation/networking/spi_can.txt line 131

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+   each 4 bytes. No interrupts are generated on the Main Controller because they
+   are ruled by the internal FIFO.

Why is this point mentioned here?

This part must be also be removed. It contains incomplete details about the spi master driver that have no sense here. I will drop it.

Quoted 6 lines

IIRC this is a non standard mode of SPI operation, usually the chip select stays active while the communication is taking place with the SPI slave. Your original code is based on a i.MX SoC, right? IIRC in the mx35 the chip select generated by the SPI controller has a big limitation (bug),

It seems we had to fight in the past with the same *feature* on i.MX31/MX35 :-)

SPI works as usual with this driver, even if my document let think the opposite ;-)

Quoted 1 line

so we always used the GPIO chip select.

Right. And this driver does not uses any chip select on its own - this is as usual part of the binding with the SPI master driver. I will dop this confusing phrases.

Quoted 6 lines

Documentation/networking/spi_can.txt line 158

Quoted 3 lines
Documentation/networking/spi_can.txt new file +2 -0
+the development is finished. The SPI interface ensures that data are
+transfered without modification between the two processors.

I'm not sure if the last sentence is correct. As far as I know there isn't any bit error detection or correction on a SPI bus.

I admit, the sentence is misleading. What I wanted to say is that we have a local bus, and there is in principle no "external" agent that damage the comunication (noise, collision, and so on).

Really, checksum is used to detect if something is going wrong on the CAN controller. It is also a helpful tool for the firmware developers, because it helps them to find soon criticity in the firmware, such as missing interrupts (what I saw) and so on.

Quoted 22 lines
Quoted 11 lines
Documentation/networking/spi_can.txt new file +11 -0
+The checksum is computed as 16 bit 2's complement of the sum (module 16)
+of  all bytes in the message, excluded the checksum itself.
+On the receive side, it is enough to sum all bytes including the checksum
+to verify if the message was received correctly (the sum must be zero).
+
+The MSG-ID (1 byte) identifies the type of exchange data. For basic exchange
+of frames, the following messages are used:
+
+	SEND_DATA_MSG
+	STATUS_MSG
+	SYNC_MSG

Documentation/networking/spi_can.txt line 180

Quoted 10 lines
Documentation/networking/spi_can.txt new file +10 -0
+	CFG_MSG_SET
+	CFG_MSG_GET
+	REQ_DATA_MSG
+
+The SEND_DATA_MSG is used when one of the two processors needs to send a CAN
+message.
+
+The STATUS_MSG is used to inform the partner about the number of bytes ready
+to be transfered. This is used by the Main Controller when it has several
+messages to send, and it needs a longer frame as usual to sen all CAN messages
                                                             ^^^ send
Quoted 3 lines
Quoted 1 line

+back.

What do you mean by the back in: "send all CAN message _back_"?

I see I have wrongly exchanged (and not only here) "Main Controller" with "CAN Controller (slave)". STATUS_MSG is sent by the slave to inform the master how long the next frame should be. I will rephrase it.

Quoted 11 lines

Documentation/networking/spi_can.txt line 203

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+CAN controller driven via SPI, the Main Controller cannot know the timing
+and must ask them to the CAN controller before instantiating a CAN device.
                ^^^^^^^ remove

Documentation/networking/spi_can.txt line 208

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+For this reason, the message is sent only once in the probe() entry point.
+
+The REQ_DATA is sent only by the Main Controller when the Main Controller
+signals it has packets to be transfered, but the Main Controller has nothing
+to sent. Its scope is mainly to signalize to the Main Controller the Start of

The second and fourth Mail Controller has probably to be replaced by CAN Controller?

Right.

Quoted 10 lines
Quoted 1 line

+

what do you mean by enough?
VVVVVV

Quoted 1 line

+The CAN controller can sends enough messages as they can fit inside

                          ^^^^^ send
Quoted 1 line

+the SPI transfer. The number of bytes (=SPI clock cycles)

Do you want to say:

The CAN controller can send as many CAN frames as fit inside a SPI transfer?

Exactly, I used only a *very* confused way to say this ;-).

Quoted 6 lines

Documentation/networking/spi_can.txt line 226

Quoted 3 lines
Documentation/networking/spi_can.txt new file +2 -0
+to be transfered is set by the Main Controller inside the Status Message.
+

I assume on the left is the Main Controller and on the right we have the CAN Controller?

I will add labels on the top to define the actors of the transfer.

Quoted 8 lines

Documentation/networking/spi_can.txt line 251

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+
+The CAN controller sets the GPIO-IRQ, and this raises an interrupt on the
                                         ^^^ remove

Documentation/networking/spi_can.txt line 255

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+Main Controller.
+The protocol uses several Message-IDs to identify what is really exchanged.
+
+From the Main Controller point of view, a SPI Transfer is initiated when the

Main Controller's

Quoted 4 lines
Quoted 1 line

+first byte is written into the internal FIFO and it is finished when the all

Under Linux a SPI transfer is usually started with spi_sync() or it's asynchronous variant.

Yes, but I had to inform Firmware's developers when and how a byte is put on the SPI bus, and they preferred a description as near as possible to the hardware. I agree that this part should be dropped or changed - not all SPI controllers have a FIFO (like your example with bitbanged SPI).

Quoted 5 lines

Documentation/networking/spi_can.txt line 258

Quoted 3 lines
Documentation/networking/spi_can.txt new file +2 -0
+bytes are transmitted. The protocol relies on the positional place of the
+bytes, that means there is no need to wait for a start-frame byte.

I don't get the last sentence, maybe it's get more clear later....

I wanted only to say that there is no special start-of-frame character. The message-id (first byte in the protocol, always != 0) acts as start of frame. The driver scans the received frame searching for the first not-zero byte.

Quoted 5 lines
Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+On the receive side, due to CAN controller limitations, the Main Controller
+must drop the first 4 bytes received from the CAN controller, as they have
+no meaning.
+It must be ensured by the CAN controller software that the first valid byte
+is the fifth in the SPI transfer.
Quoted 8 lines

Documentation/networking/spi_can.txt line 268

Quoted 4 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+The Main Controller has no knowledge at the beginning how many messages the
+CAN controller will send and it starts with a 32-bytes transfer because this
+matches the internal FIFO and raises only one interrupt.

We should not put any assumptions about the Main Controller's hardware into the specification of the protocol. Under Linux you can run a (somewhat slow) SPI bus on bit-banged GPIOs. Just specify then length of the SPI frame.

Agree. I will check then in the whole document, I understand now there are several places with reference to the hardware.

Quoted 7 lines

Documentation/networking/spi_can.txt line 278

Quoted 3 lines
Documentation/networking/spi_can.txt new file +2 -0
+no restrictions about the number of bytes to be transfered, and it is duty of
+the Main Controller driver to find the most valuable length to be used.

Earlier you say you have to transfer equal or more bytes as requested, now you state there aren't any restrictions.

Quoted 8 lines

Documentation/networking/spi_can.txt line 283

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+
+A SPI transfer is *NOT* delimited by changes of the chip select signal. Indeed,
+the chip select is ruled internally by the bits x word setup, and it is made
+suitable for both CAN controller and Main Controller. Bits x words must be a
+multiple of 2, because the CAN controller gets an interrupt each 2 bytes.

Sorry I don't get what you mean...The chip select under Linux is usually controlled on a struct spi_transfer base with the cs_change member of that struct.

Right.

Quoted 1 line

What you describe sounds like a special hardware feature of the imx35.

Indeed, it must be removed.

Quoted 4 lines

Documentation/networking/spi_can.txt line 295

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+duty of the Main Controller to disable and enable the interrupt source depending
+on the status of the transmission.

Transmission of the SPI messages?

No, status of the communication. The driver checks if the line is asserted after each iteration (=after each SPI transfer) and enables the interrupt only if the line is not asserted (it means that the Slave has nothing more to send and it is currently idle), to be waked up later when the slave has new CAN messages to send.

The driver verifies that the GPIO is not continuosly asserted, meaning that maybe the slave is stucked (in any case, this is not a normal behavior), and also in that case the IRQ is disabled.

Quoted 7 lines

Documentation/networking/spi_can.txt line 299

Quoted 5 lines
Documentation/networking/spi_can.txt new file +4 -0
+
+After getting the interrupt, the Main Controller (that has no knowledge about
+the amount of data to be transfered from the CAN controller), will start a SPI
+transfer for a total of 32 bytes (default), sending a REQ_DATA message.

Specify the length or give it a name that is specified later, e.g. in some kind of table.

ok

Quoted 7 lines

Documentation/networking/spi_can.txt line 306

Quoted 5 lines
Documentation/networking/spi_can.txt new file +3 -0
+Taking into account that the header requires 3 bytes (MSG-ID + length) and the
+checksum further two bytes, there are still 27 bytes available for the CAN
+messages.

Is the checksum optional or not?

The two bytes for checksum are always sent on the bus and they are part of the protocol. However, if checksum is disabled, these two bytes are set to zero.

Quoted 1 line

What about the 4 bogus bytes in the beginning of the message?

Well, this is only a remark about how the firmware works on the slave I have, but without any effects in the code of the driver. The firmware on the slave adds always these 4 bogus bytes at the beginning of a frame, because it requires some time before being able to send an answer. However, my driver does not rely on that, and
the received bytes are scanned until a message-id is recognized (=start of a message).

Quoted 7 lines

Documentation/networking/spi_can.txt line 310

Quoted 5 lines
Documentation/networking/spi_can.txt new file +4 -0
+This is always enough to send at least one CAN message.
+
+The CAN controller answers with the SEND_DATA_MSG. The Main Controller knows
+that there is no message from the Main Controller, because the MSG-ID is not

The Main Controller knows that there is no message from the Main Controller?

:-D

Ok, I used one time more the wrong actor !

Quoted 8 lines

Documentation/networking/spi_can.txt line 313

Quoted 4 lines
Documentation/networking/spi_can.txt new file +3 -0
+set to SEND_DATA, and acquires the number of bytes to be transfered from the
+length field.
+The CAN controller packs so many CAN messages inside the SEND_DATA_MSG

^^ who many? as many as possible/fit?

Quoted 1 line

+(usually 1 or 2), delimiting the packet with the checksum as described before.

The checksum was specified as optional?

Maybe I should explain that computation and verification of the checksum is optional, but bytes for checksum, even if zeroed, are always put on the bus.

Quoted 4 lines

Documentation/networking/spi_can.txt line 335

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+
+In this case, the CAN controller deasserts the GPIO line to avoid further
                                                  ^^^^

Better name it IRQ line, it's just an implementation details that it's a GPIO on the Can Controller :)

ok

Quoted 4 lines
Quoted 2 lines

+interrupts for the Main Controller.

This means the IRQ is level triggered, please specify this when describing the IRQ.

ok

Quoted 6 lines
Quoted 1 line

+The CAN controller has now enough SPI clock cycles to send all data

C

Documentation/networking/spi_can.txt line 382

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+to the Main Controller. After sending all data, it will fill also any
+remaining bytes until the end of transfer with dummy bytes.
                                                  ^^^^^^^^^^^ 

just "0x0", sounds more professional than dummy bytes.

;-)

Quoted 12 lines

Documentation/networking/spi_can.txt line 422

Quoted 5 lines
Documentation/networking/spi_can.txt new file +5 -0
+Because it has already received the length of the incoming message, it is
+aware about how many bytes are transfered and can act as in 5.3 if it has more
+messages to send.
+
+5.5 The CAN controller gets new data during a transfer

C

Documentation/networking/spi_can.txt line 427

Quoted 5 lines

+------------------------------------------------------

Documentation/networking/spi_can.txt new file +4 -0
+
+In this case, there are the following options:
+
+- the new messages can be fit inside the actual transfer.
                      ^^^^^^^^^^ fits
Quoted 6 lines

Documentation/networking/spi_can.txt line 429

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+Then the CAN controller will append a new SEND_DATA_MSG to the end of the
+data that it is currently sending on the SPI line.

What will happen if a new CAN frame comes in while REQ DATA is still running, but the first SEND_DATA is already done and the CAN Controller is already inserting stuffing/dummy 0x0?

This is a use case. The slave/CAN controller knows how many bytes are still available
until the end of the SPI frame, and can decide if there is enough space to send a new SEND_DATA
message, stopping to fill with zeroes. The driver checks in any case the whole SPI frame and extracts each single SEND_DATA message.

Multiple SEND_DATA messages are not only allowed, but desired, because it increase the throughput.

Quoted 2 lines

In the protocol we should not car about the internals of the CAN Controller.

Agree, I will check those points and remove them.

Quoted 2 lines

How is the absolute time encoded? ms since 1970? Or ms since an arbitrary value?

ms since 1970.

Quoted 10 lines

Documentation/networking/spi_can.txt line 500

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+
+When the CAN controller will send data, it will add the elapsed milliseconds
+from the receiving of the SYNC_MSG as timestamps. As 4 bytes are used for

^^^^ since

Quoted 1 line

+timestamp, the timestamp will roll over after 1193 days.

-> 2^32 / 1000 / 60 / 60 / 24
= 49.710270

I've calculated 49 days.

You're right. Missed a factor, thanks !

Quoted 12 lines

Documentation/networking/spi_can.txt line 510

Quoted 10 lines
Documentation/networking/spi_can.txt new file +9 -0
+
+7. CAN configuration
+----------------------------------
+
+It is foreseen that the CAN controller's CAN channels are configured in the
+usual way under Linux via the "ip" command. In fact, the CAN interface cannot
+be set to "up" if it was not configured before.
+
+With ip, the following parameters can be set:

This should not be scope of the protocol documentation, just refer the existing CAN documentation in the kernel.

ok

Quoted 5 lines

Documentation/networking/spi_can.txt line 558

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+The parameters are not interpreted by the driver, but they are encapsulated
+as they are in a SPI frame with MSG-ID=CFG_MSG_SET and sent to the corresponding CAN
+channel.

This is not a good idea, as a kernel internal change will break your firmware.

Well, it is not exactly so. The parameters are put into a CFG_MSG_SET, that is defined in the protocol. Of course, now fields in CFG_MSG_SET are exactly as the parameters in kernel (why doing differently ?). If in future there will be a kernel internal change, this will require that the driver must map the new parameters to fit again into a CFG_MSG_SET.

Quoted 17 lines
Quoted 17 lines
Documentation/networking/spi_can.txt new file +17 -0
+It is then duty of the CAN controller to set up the hardware on base of
+the received parameters. According to the rules set in this document,
+single parameters are sent in a big-endian order (MSB first).
+
+A special case is the setup for the CFG channel. This channel is not mapped to
+a real CAN bus, but it is used to instantiate a communication
+between the CAN controller software and the Application in User Space.
+Data are not interpreted at all by the driver, and anybody can set its internal
+protocol to add commands and features to the CAN controller software.
+One example is the Watchdog triggering already mentioned.
+The CAN_MSG is ignored by the CAN controller for the CFG channel, and its values
+do not change the hardware setup.
+Using this message it is possible to switch the CAN controller between two
+operational modes:
+	- Supervisor / Maintenance mode:	slow, max SPI frequency 1 Mhz
+	- User / Normal mode			max SPI frequency set at startup
+
Quoted 8 lines

Documentation/networking/spi_can.txt line 581

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+The driver will start setting the SPI frequency to the lower value.
+When the CFG channel is opened, the driver checks the bitrate of the
+can_bittiming structure, and takes the following actions:
+
+	- bitrate = 125000		sets SPI frequency to low value
+	- bitrate > 125000		sets SPI frequency to high value

125 kbit/s? Why this arbitrary value?

I cannot correct answer to this. I get this requirement, but about the exact value I do not know.

Quoted 11 lines

Documentation/networking/spi_can.txt line 591

Quoted 9 lines
Documentation/networking/spi_can.txt new file +9 -0
+In user space it is then possible to switch the CAN controller between the two
+operational modes simply issueing the "ip link" command and setting
+the bitrate for the CFG channel.
+
+To set the CAN controller in supervisor mode:
+	ip link set cfg type can bitrate 125000
+
+To set the CAN controller in user mode:
+	ip link set cfg type can bitrate 500000

Can you come up with a cleaner solution for this problem?

Let's see. What I thought as alternative was to add .ndo_do_ioctl to perform the frequency change. Can this be a better way ?

Quoted 16 lines

Documentation/networking/spi_can.txt line 603

Quoted 13 lines
Documentation/networking/spi_can.txt new file +12 -0
+
+
+8. SPI Frame definition and Messages
+----------------------------------------
+
+A SPI Frame begins with a 4-bytes header:
+
+bytes:
+	0		Message ID
+	1-2		Message Length.
+			Length is computed including 2 bytes for checksum,
+			but without Message ID and Length itself.

So N in this example?

Quoted 4 lines

Documentation/networking/spi_can.txt line 605

Quoted 2 lines
Documentation/networking/spi_can.txt new file +2 -0
+	3 .. N-2	Depending on Message-ID
+	N-1 .. N	Checksum

Is the checksum optional or not?

In the frame, it is mandatory.

Quoted 11 lines

Documentation/networking/spi_can.txt line 612

Quoted 8 lines
Documentation/networking/spi_can.txt new file +7 -0
+
+Command codes:
+	0x01	Status Request
+	0x02	Send Data
+	0x03	Sync message
+	0x04	Configuration Message
+	....	t.b.d.

Can you please reuse the name you've established before? Like SEND_DATA_MSG. In the beginning of this document there are 6 messages defined.

ok

Quoted 6 lines
Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+Each CAN message has the following format
+
+	0		Channel Number
+			Values:
+			1-n	CAN Channel
+			0xFF	CFG channel
Quoted 8 lines

Documentation/networking/spi_can.txt line 647

Quoted 6 lines
Documentation/networking/spi_can.txt new file +6 -0
+	1 - 4		Timestamp
+	5 - 8		can_id
+	9		dlc (length of can message)
+	10..N		Variable number of data (max 8)
+
+Note: Channel 0 is reserved for configuration.

What's Channel 0xff then?

It is foreseen to have an additional logical channel that does not map to a physical CAN bus. The reason is to have a way for an application in user space to communicate (via CAN messages) to the slave microcontroller. Because the driver is transparent to this (it creates only an additional channel), changing in the protocol/messages between application and firmware does not require to modify the driver itself.

Some examples: the CAN controller has additional features that can be activated by the Application. LEDs, GPIOs, ... (I do not know all details). This channel can be also be used to make some configuration of the slave microcontroller itself.

Quoted 5 lines

Documentation/networking/spi_can.txt line 650

Quoted 3 lines
Documentation/networking/spi_can.txt new file +3 -0
+
+The flag for Standard and Extended frame format (SFF and EFF
+in socketCAN framework) is a flag in the can_id.

Better use a separate define, although these flags are Linux userspace API/ABI

ok

Regards,
Stefano

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#7 lore raw

PATCH v5 2/2 CAN: CAN driver to support multiple CAN bus on SPI interface

In reply to Stefano Babic

This driver implements a simple protocol
to transfer CAN messages to and from an external microcontroller via SPI interface.
Multiple busses can be tranfered on the same SPI channel.

It was tested on a i.MX35 connected to a Freescale's HCS12 16-bit controller with 5 CAN busses.

---

Changes in v5:
- sort include headers
- match open parenthesis (globally fixed)
- add newline to dev_err() in insert_cfg_msg()
- use sizeof(*p)
- drop unused variable val in ISR
- proper return code instead of -1
- move device ids after probe/remove
- drop #ifdef CONFIG_OF (not needed anymore)
- use devm_ API
- use module_spi_driver()

Changes in v4:
- implement GET_CFG message to ask the microcontroller for bittiming consts.
- drop set_mode (never called)
- drop echo_index (never used)
- fix inconsistencies using int variable (int/u32)
- add reference to documentation in Kconfig help
- s/refTime/ref_time/
- move module parameters on the top
- use variable to get sizeof inside kzalloc/memset
- fix missing close_candev() in open entry point
- fix return values (spi_can_fill_skb_msg())
- not access skb after calling net_receive_skb()
- fix minor coding style issues
- add missing free_irq() and gpio_free() in probe when fails

Changes in v3:
- spican.h renamed to spi_can.h
- drop further references to i.MX and HCS12, not yet cleaned
- drop CAN_DEV depend from Kconfig
- drop debug stuff via sysfs, not required in production code
- drop debug module parameter, use CAN_DEBUG_DEVICES
- drop unused bittiming constant
- chksum on as default. It could still be disabled via DT/pdata, but not via module parameter.

Changes in v2:
- drop all references to i.MX35 and HCS12

 drivers/net/can/spi/Kconfig          |   11 +
 drivers/net/can/spi/Makefile         |    1 +
 drivers/net/can/spi/spi_can.c        | 1506 ++++++++++++++++++++++++++++++++++
 include/linux/can/platform/spi_can.h |   33 +
 4 files changed, 1551 insertions(+)
 create mode 100644 drivers/net/can/spi/spi_can.c
 create mode 100644 include/linux/can/platform/spi_can.h
drivers/net/can/spi/Kconfig +11 -0
index 148cae5..e7323d2 100644
@@ -7,4 +7,15 @@ config CAN_MCP251X
 	---help---
 	  Driver for the Microchip MCP251x SPI CAN controllers.
 
+config CAN_SPI
+	tristate "Support for CAN over SPI"
+	---help---
+	  Driver to transfer CAN messages to a microcontroller
+	  connected via the SPI interface using a simple messaged based
+	  protocol.
+	  Further information and details on the protocol can be found
+	  in Documentation/networking/spi_can.txt
+
+	  Say Y here if you want to support for CAN to SPI.
+
 endmenu
drivers/net/can/spi/Makefile +1 -0
index 90bcacf..0fd2126 100644
@@ -4,5 +4,6 @@
 
 
 obj-$(CONFIG_CAN_MCP251X)	+= mcp251x.o
+obj-$(CONFIG_CAN_SPI)		+= spi_can.o
 
 ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/spi/spi_can.c new file +1506 -0

1,506 lines not on this page · raw message

include/linux/can/platform/spi_can.h new file +33 -0
new file mode 100644
index 0000000..95ee330
@@ -0,0 +1,33 @@
+/*
+ * (C) Copyright 2010-2014
+ * Stefano Babic, DENX Software Engineering, [email protected].
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef __CAN_PLATFORM_SPICAN_H__
+#define __CAN_PLATFORM_SPICAN_H__
+
+#include <linux/spi/spi.h>
+
+struct spi_can_platform_data {
+	u32 can_channels;
+	u32 gpio;
+	u32 active;
+	unsigned int slow_freq;
+	unsigned int chksum_en;
+};
+
+#endif
Signature · 1 line
1.9.1
#8 lore raw

Re: [PATCH v5 2/2] CAN: CAN driver to support multiple CAN bus on SPI interface

drivers/net/can/spi/Makefile line 9

Quoted 9 lines · Stefano Babic
drivers/net/can/spi/Makefile +1 -0
@@ -4,5 +4,6 @@
 
 
  obj-$(CONFIG_CAN_MCP251X)	+= mcp251x.o
+obj-$(CONFIG_CAN_SPI)		+= spi_can.o
 
  ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG

This ccflags stuff already has been remove - so it needs a tiny rebase here. Sorry.

Quoted 8 lines
drivers/net/can/spi/spi_can.c new file +1 -0
new file mode 100644
index 0000000..087e6b7
 
 
+struct msg_cfg_get_data {

drivers/net/can/spi/spi_can.c line 156

Quoted 10 lines
drivers/net/can/spi/spi_can.c new file +10 -0
+	u8	channel;
+	u8	tseg1_min;
+	u8	tseg1_max;
+	u8	tseg2_min;
+	u8	tseg2_max;
+	u8	sjw_max;
+	u32	brp_min;
+	u32	brp_max;
+	u32	brp_inc;
+	u8	ctrlmode;

u32 ?

Quoted 32 lines
drivers/net/can/spi/spi_can.c new file +32 -0
+static int spi_can_hwtstamp_ioctl(struct net_device *netdev,
+			struct ifreq *ifr, int cmd)
+{
+	struct hwtstamp_config config;
+
+	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+		return -EFAULT;
+
+	if (config.flags)
+		return -EINVAL;
+
+	switch (config.tx_type) {
+	case HWTSTAMP_TX_OFF:
+		break;
+	case HWTSTAMP_TX_ON:
+		break;
+	default:
+		return -ERANGE;
+	}
+
+	switch (config.rx_filter) {
+	case HWTSTAMP_FILTER_NONE:
+		break;
+	default:
+		config.rx_filter = HWTSTAMP_FILTER_ALL;
+		break;
+	}
+
+	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
+		-EFAULT : 0;
+}
+

drivers/net/can/spi/spi_can.c line 483

Quoted 10 lines
drivers/net/can/spi/spi_can.c new file +10 -0
+static int spi_can_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
+{
+	if (!netif_running(dev))
+		return -EINVAL;
+
+	if (cmd == SIOCSHWTSTAMP)
+		return spi_can_hwtstamp_ioctl(dev, rq, cmd);
+
+	return -EINVAL;
+}

What is this ioctl() intended for?

Can this be handled by the standard Linux configuration interface for hardware timestamping?

Finally I would like to ask you about IFF_ECHO support (again).
I know that you had some concerns about the SPI performance when putting the sent CAN frame into the SPI receive path.

But I would at least suggest to make the SPI message interface capable to support IFF_ECHO on the slave CPU. E.g. by sending some index (!=0) from an echo skb store to/from the slave to detect originated frames in the receive path.

The SPI slave should announce if it supports IFF_ECHO. And some 8 bit value which goes down to the slave and is received together with a CAN frame would help to detect local frames.

Probably we can also use the high nibble from the dlc for that.

Regards,
Oliver

#9 lore raw

Re: [PATCH v5 2/2] CAN: CAN driver to support multiple CAN bus on SPI interface

Hi Oliver,

Quoted 3 lines · Oliver Hartkopp

This ccflags stuff already has been remove - so it needs a tiny rebase here.
Sorry.

Dropped in next version, thanks !

Quoted 21 lines
Quoted 10 lines
drivers/net/can/spi/spi_can.c new file +1 -0
new file mode 100644
index 0000000..087e6b7
 
 
+struct msg_cfg_get_data {

drivers/net/can/spi/spi_can.c line 156

Quoted 10 lines
drivers/net/can/spi/spi_can.c new file +10 -0
+    u8    channel;
+    u8    tseg1_min;
+    u8    tseg1_max;
+    u8    tseg2_min;
+    u8    tseg2_max;
+    u8    sjw_max;
+    u32    brp_min;
+    u32    brp_max;
+    u32    brp_inc;
+    u8    ctrlmode;
Quoted 1 line

u32 ?

Yes, I will set it back to u32.

Quoted 5 lines

What is this ioctl() intended for?

Can this be handled by the standard Linux configuration interface for hardware timestamping?

Yes - I remove this ioctl.

Quoted 5 lines

Finally I would like to ask you about IFF_ECHO support (again). I know that you had some concerns about the SPI performance when putting the sent CAN frame into the SPI receive path.

Maybe because I have in mind my use case, and due also to the low SPI frequency allowed by the microcontroller (up to 6 Mhz), adding echo frames will strongly reduce the throughput. Anyway, I agree that a more powerful microcontroller could raise the frequency and reduce the gap.

Quoted 11 lines

But I would at least suggest to make the SPI message interface capable to support IFF_ECHO on the slave CPU. E.g. by sending some index (!=0) from an echo skb store to/from the slave to detect originated frames in the receive path.

The SPI slave should announce if it supports IFF_ECHO. And some 8 bit value which goes down to the slave and is received together with a CAN frame would help to detect local frames.

Probably we can also use the high nibble from the dlc for that.

I agree on that. Maybe in the high nibble we can use three bits to have up to 7 echoed messages. The index will be sent by the master, and the slave will answer sending back the index in the encoded dlc, with data length = 0. This is enough to understand if it is a local or remote frame. If we agree, I'll update the documentation.

Nevertheless I am asking if this can be implemented in a follow up patch, when a real use case arises. It is not a big change, but I will not get the firmware for the microcontroller supporting this and I cannot test this functionality.

Best regards,
Stefano

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#10 lore raw

Re: [PATCH v5 2/2] CAN: CAN driver to support multiple CAN bus on SPI interface

Quoted 20 lines · Stefano Babic
Quoted 20 lines
Quoted 19 lines
drivers/net/can/spi/spi_can.c new file +11 -0
new file mode 100644
index 0000000..087e6b7
 
 
+struct msg_cfg_get_data {
+    u8    channel;
+    u8    tseg1_min;
+    u8    tseg1_max;
+    u8    tseg2_min;
+    u8    tseg2_max;
+    u8    sjw_max;
+    u32    brp_min;
+    u32    brp_max;
+    u32    brp_inc;
+    u8    ctrlmode;
Quoted 3 lines
Quoted 1 line

u32 ?

Yes, I will set it back to u32.

Please add an additional

u8 capability;

or something like this to get the possible IFF_ECHO support information. When e.g. (capability & SPI_CAP_ECHO) is true at startup, dev->flags |= IFF_ECHO can be set.

Quoted 11 lines
Quoted 5 lines

Finally I would like to ask you about IFF_ECHO support (again). I know that you had some concerns about the SPI performance when putting the sent CAN frame into the SPI receive path.

Maybe because I have in mind my use case, and due also to the low SPI frequency allowed by the microcontroller (up to 6 Mhz), adding echo frames will strongly reduce the throughput. Anyway, I agree that a more powerful microcontroller could raise the frequency and reduce the gap.

Yep.

Quoted 7 lines
Quoted 7 lines

But I would at least suggest to make the SPI message interface capable to support IFF_ECHO on the slave CPU. E.g. by sending some index (!=0) from an echo skb store to/from the slave to detect originated frames in the receive path.

The SPI slave should announce if it supports IFF_ECHO.

(already addressed above)

Quoted 11 lines
Quoted 5 lines

And some 8 bit
value which goes down to the slave and is received together with a CAN frame would help to detect local frames.

Probably we can also use the high nibble from the dlc for that.

I agree on that. Maybe in the high nibble we can use three bits to have up to 7 echoed messages. The index will be sent by the master, and the slave will answer sending back the index in the encoded dlc, with data length = 0. This is enough to understand if it is a local or remote frame. If we agree, I'll update the documentation.

I would suggest to simply use the high nibble (as low nibble is always DLC - even in the CAN FD case).

When IFF_ECHO is supported a high nibble != 0 indicates the echo_skb index.

When you receive a frame and the high nibble is != 0 this was a locally sent frame.

Quoted 3 lines

Nevertheless I am asking if this can be implemented in a follow up patch, when a real use case arises.

Yes. When the documentation for dlc-handling and capability is describing the entire communication protocol the missing IFF_ECHO support should be documented as TODO in the comments IMO.

Quoted 3 lines

It is not a big change, but I will
not get the firmware for the microcontroller supporting this and I cannot test this functionality.

Ok. Changing struct msg_cfg_get_data is probably enough work for them ;-)

Best regards,
Oliver

#11 lore raw

Re: [PATCH v5 2/2] CAN: CAN driver to support multiple CAN bus on SPI interface

In reply to Stefano Babic
Quoted 9 lines · Stefano Babic

This driver implements a simple protocol
to transfer CAN messages to and from an external microcontroller via SPI interface.
Multiple busses can be tranfered on the same SPI channel.

It was tested on a i.MX35 connected to a Freescale's HCS12 16-bit controller with 5 CAN busses.

No line by line review, but some fundamental comments and questions:

Can you please use a consistent indention scheme for struct and enums, I favour a single space against aligning with tab, as it ony fits 95% of the time and breaks during upgrades.

Please only use u16, u32 and friends where the size of the register is important, use plain int, unsinged int otherwise.

I'm not sure of fiddling with the GPIO which you requested as an interrupt is a good idea. This is probably not available on all platforms.

Why do you have two functions calculating the checksum? Have you checked if the kernel offers you a library for this?

Can you explain me the endianess, I don't get why you need format_frame_output() and format_frame_input() and while your structs that go over the wire have no specific endianess (i.e. no __be32 in the struct msg_*).

Have you thought about using threaded interrupts? Or even better make use of the asynchronous SPI framework?

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#12 lore raw

Re: [PATCH v5 2/2] CAN: CAN driver to support multiple CAN bus on SPI interface

Hi Marc,

Quoted 6 lines · Marc Kleine-Budde
Quoted 5 lines · Stefano Babic

This driver implements a simple protocol
to transfer CAN messages to and from an external microcontroller via SPI interface.
Multiple busses can be tranfered on the same SPI channel.

Quoted 7 lines
Quoted 4 lines

It was tested on a i.MX35 connected to a Freescale's HCS12 16-bit controller with 5 CAN busses.

No line by line review, but some fundamental comments and questions:

Fine.

Quoted 3 lines

Can you please use a consistent indention scheme for struct and enums, I favour a single space against aligning with tab, as it ony fits 95% of the time and breaks during upgrades.

ok - but let see if I well understood. I confess I align always with tab, using the rule:

struct something {
<tab>.....<tab>/* comment if any */

Because this is largely rule in kernel. You mean:

struct something {
<spaces>....<spaces> /*comment if any */

but this is reported as warning by checkpatch as :
WARNING: please, no spaces at the start of a line

I see only (or mainly) <tab> for structs/enum in kernel. Can you better explain what are you meaning ? Thanks !

Quoted 4 lines

Please only use u16, u32 and friends where the size of the register is important, use plain int, unsinged int otherwise.

ok

Quoted 2 lines

I'm not sure of fiddling with the GPIO which you requested as an interrupt is a good idea. This is probably not available on all platforms.

I see in this way: SPI is a master/slave protocol and there is no standard way for the slave to send an event when it needs a transfer. Not only, the microcontroller is very simple and what it can generally do is to assert or to release a line - this can be done by all controllers.

The same way to operate is done by several touchscreen driver. Communication runs on the SPI/I2C bus, a GPIO is used to generate an interrupt. My driver makes exactly the same, no new invention.

Quoted 3 lines

Why do you have two functions calculating the checksum? Have you checked if the kernel offers you a library for this?

I have checked inside lib/, but I see only the functions for computing the checksum on a UDP header. But it looks very strange for me to call some ip_* function inside the driver code, because the drivers has nothing to do with IP. Anyway these functions sum only all bytes together - a very simple checksum.

Quoted 5 lines

Can you explain me the endianess, I don't get why you need format_frame_output() and format_frame_input() and while your structs that go over the wire have no specific endianess (i.e. no __be32 in the struct msg_*).

On the SPI bus, data are big endian. This is defined in the documentation of the protocol.
All structs in the driver are defined without endianess and the messages are converted before and after each transfer on the bus.

Quoted 3 lines

Have you thought about using threaded interrupts? Or even better make use of the asynchronous SPI framework?

Yes, I did, but I have to revert back.

The main issue is that I have to reduce the delay between the slave has asserted the IRQ line and the SPI frame is put on the bus. In fact, the slave controller has very limited way to store incoming CAN packets, and if the Master does not react very soon, packets are lost.

I see that moving to the async framework, the delay before the frame was put on the bus was not acceptable. The current design has given the best results, with a kernel thread dealing the SPI transfer and a work queue to process the received messages.

Stefano

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#13 lore raw
In reply to Stefano Babic

Hi everybody,

has the last version fixed all open issues ? Maybe is it ready to be applied ;-) ?

Thanks,
Stefano

Quoted 70 lines · Stefano Babic

Hi all,

after some time, I post an updated version of the spi_can driver. Sorry for that, but I had to wait for the requested modifications on the microcontroller's firmware to test it again.

The majork change is the GET_CFG message to query the remote CAN microcontroller for the CAN bittiming. I hope also I have fixed all issues from previous reviews.

Changes in v5:
- drop Patch 2/3, already applied.
- sort include headers
- match open parenthesis (globally fixed)
- add newline to dev_err() in insert_cfg_msg()
- use sizeof(*p)
- drop unused variable val in ISR
- proper return code instead of -1
- move device ids after probe/remove
- drop #ifdef CONFIG_OF (not needed anymore)
- use devm_ API
- use module_spi_driver()

Changes in v4:
- added GET_CFG message to query bit timing to the remote controller.
- implement GET_CFG message to ask the microcontroller
for bittiming consts.
- drop set_mode (never called)
- drop echo_index (never used)
- fix inconsistencies using int variable (int/u32)
- add reference to documentation in Kconfig help
- s/refTime/ref_time/
- move module parameters on the top
- use variable to get sizeof inside kzalloc/memset
- fix missing close_candev() in open entry point
- fix return values (spi_can_fill_skb_msg())
- not access skb after calling net_receive_skb()
- fix minor coding style issues
- add missing free_irq() and gpio_free() in probe when fails

Changes in v3:
- format documentation, check for lines > 80 chars (O. Hartkopp)
- patch 2/3 already aqpplied to can-next, removed from patchset
- spican.h renamed to spi_can.h
- drop further references to i.MX and HCS12, not yet cleaned
- drop CAN_DEV depend from Kconfig
- drop debug stuff via sysfs, not required in production code
- drop debug module parameter, use CAN_DEBUG_DEVICES
- drop unused bittiming constant
- chksum on as default. It could still be disabled via DT/pdata, but not via module parameter.

Changes in v2:
- drop all references to i.MX35 and HCS12

Stefano Babic (2):
Add documentation for SPI to CAN driver
CAN: CAN driver to support multiple CAN bus on SPI interface

 Documentation/networking/spi_can.txt |  774 +++++++++++++++++
 drivers/net/can/spi/Kconfig          |   11 +
 drivers/net/can/spi/Makefile         |    1 +
 drivers/net/can/spi/spi_can.c        | 1506 ++++++++++++++++++++++++++++++++++
 include/linux/can/platform/spi_can.h |   33 +
 5 files changed, 2325 insertions(+)
 create mode 100644 Documentation/networking/spi_can.txt
 create mode 100644 drivers/net/can/spi/spi_can.c
 create mode 100644 include/linux/can/platform/spi_can.h
Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#14 lore raw

Hi,

Quoted 8 lines · Stefano Babic

Hi everybody,

has the last version fixed all open issues ? Maybe is it ready to be applied ;-) ?

Thanks,
Stefano

Ping ?

Best regards,
Stefano Babic

Quoted 73 lines · Stefano Babic
Quoted 70 lines

Hi all,

after some time, I post an updated version of the spi_can driver. Sorry for that, but I had to wait for the requested modifications on the microcontroller's firmware to test it again.

The majork change is the GET_CFG message to query the remote CAN microcontroller for the CAN bittiming. I hope also I have fixed all issues from previous reviews.

Changes in v5:
- drop Patch 2/3, already applied.
- sort include headers
- match open parenthesis (globally fixed)
- add newline to dev_err() in insert_cfg_msg()
- use sizeof(*p)
- drop unused variable val in ISR
- proper return code instead of -1
- move device ids after probe/remove
- drop #ifdef CONFIG_OF (not needed anymore)
- use devm_ API
- use module_spi_driver()

Changes in v4:
- added GET_CFG message to query bit timing to the remote controller.
- implement GET_CFG message to ask the microcontroller
for bittiming consts.
- drop set_mode (never called)
- drop echo_index (never used)
- fix inconsistencies using int variable (int/u32)
- add reference to documentation in Kconfig help
- s/refTime/ref_time/
- move module parameters on the top
- use variable to get sizeof inside kzalloc/memset
- fix missing close_candev() in open entry point
- fix return values (spi_can_fill_skb_msg())
- not access skb after calling net_receive_skb()
- fix minor coding style issues
- add missing free_irq() and gpio_free() in probe when fails

Changes in v3:
- format documentation, check for lines > 80 chars (O. Hartkopp)
- patch 2/3 already aqpplied to can-next, removed from patchset
- spican.h renamed to spi_can.h
- drop further references to i.MX and HCS12, not yet cleaned
- drop CAN_DEV depend from Kconfig
- drop debug stuff via sysfs, not required in production code
- drop debug module parameter, use CAN_DEBUG_DEVICES
- drop unused bittiming constant
- chksum on as default. It could still be disabled via DT/pdata, but not via module parameter.

Changes in v2:
- drop all references to i.MX35 and HCS12

Stefano Babic (2):
Add documentation for SPI to CAN driver
CAN: CAN driver to support multiple CAN bus on SPI interface

 Documentation/networking/spi_can.txt |  774 +++++++++++++++++
 drivers/net/can/spi/Kconfig          |   11 +
 drivers/net/can/spi/Makefile         |    1 +
 drivers/net/can/spi/spi_can.c        | 1506 ++++++++++++++++++++++++++++++++++
 include/linux/can/platform/spi_can.h |   33 +
 5 files changed, 2325 insertions(+)
 create mode 100644 Documentation/networking/spi_can.txt
 create mode 100644 drivers/net/can/spi/spi_can.c
 create mode 100644 include/linux/can/platform/spi_can.h
Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================
#15 lore raw
In reply to Stefano Babic

Hi Marc,

Quoted 11 lines · Stefano Babic

Hi all,

after some time, I post an updated version of the spi_can driver. Sorry for that, but I had to wait for the requested modifications on the microcontroller's firmware to test it again.

The majork change is the GET_CFG message to query the remote CAN microcontroller for the CAN bittiming. I hope also I have fixed all issues from previous reviews.

Quoted 12 lines

Changes in v5:
- drop Patch 2/3, already applied.
- sort include headers
- match open parenthesis (globally fixed)
- add newline to dev_err() in insert_cfg_msg()
- use sizeof(*p)
- drop unused variable val in ISR
- proper return code instead of -1
- move device ids after probe/remove
- drop #ifdef CONFIG_OF (not needed anymore)
- use devm_ API
- use module_spi_driver()

The driver needs to be rebased on current tree, but before doing that I want kindly ask you which is the best way to proceed. As far as I can see in the previous threads, I have not found any open points for V5. Do you think there are any issue that block this driver to flow into mainline ? Or which is in your opinion the best way to do ? Maybe should I post it for inclusion in drivers/staging ?

Thanks,
Stefano Babic

Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: [email protected]
=====================================================================