Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: caif: Remove unused caif SPI driver

While chasing in_interrupt() (ab)use in drivers it turned out that the
caif_spi driver has never been in use since the driver was merged 10 years
ago. There never was any matching code which provides a platform device.

The driver has not seen any update (asided of treewide changes and
cleanups) since 8 years and the maintainers vanished from the planet.

So analysing the potential contexts and the (in)correctness of
in_interrupt() usage is just a pointless exercise.

Remove the cruft.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Thomas Gleixner and committed by
David S. Miller
f2bf88c4 a53b59ec

-1536
-1
Documentation/networking/caif/index.rst
··· 10 10 11 11 linux_caif 12 12 caif 13 - spi_porting
-229
Documentation/networking/caif/spi_porting.rst
··· 1 - .. SPDX-License-Identifier: GPL-2.0 2 - 3 - ================ 4 - CAIF SPI porting 5 - ================ 6 - 7 - CAIF SPI basics 8 - =============== 9 - 10 - Running CAIF over SPI needs some extra setup, owing to the nature of SPI. 11 - Two extra GPIOs have been added in order to negotiate the transfers 12 - between the master and the slave. The minimum requirement for running 13 - CAIF over SPI is a SPI slave chip and two GPIOs (more details below). 14 - Please note that running as a slave implies that you need to keep up 15 - with the master clock. An overrun or underrun event is fatal. 16 - 17 - CAIF SPI framework 18 - ================== 19 - 20 - To make porting as easy as possible, the CAIF SPI has been divided in 21 - two parts. The first part (called the interface part) deals with all 22 - generic functionality such as length framing, SPI frame negotiation 23 - and SPI frame delivery and transmission. The other part is the CAIF 24 - SPI slave device part, which is the module that you have to write if 25 - you want to run SPI CAIF on a new hardware. This part takes care of 26 - the physical hardware, both with regard to SPI and to GPIOs. 27 - 28 - - Implementing a CAIF SPI device: 29 - 30 - - Functionality provided by the CAIF SPI slave device: 31 - 32 - In order to implement a SPI device you will, as a minimum, 33 - need to implement the following 34 - functions: 35 - 36 - :: 37 - 38 - int (*init_xfer) (struct cfspi_xfer * xfer, struct cfspi_dev *dev): 39 - 40 - This function is called by the CAIF SPI interface to give 41 - you a chance to set up your hardware to be ready to receive 42 - a stream of data from the master. The xfer structure contains 43 - both physical and logical addresses, as well as the total length 44 - of the transfer in both directions.The dev parameter can be used 45 - to map to different CAIF SPI slave devices. 46 - 47 - :: 48 - 49 - void (*sig_xfer) (bool xfer, struct cfspi_dev *dev): 50 - 51 - This function is called by the CAIF SPI interface when the output 52 - (SPI_INT) GPIO needs to change state. The boolean value of the xfer 53 - variable indicates whether the GPIO should be asserted (HIGH) or 54 - deasserted (LOW). The dev parameter can be used to map to different CAIF 55 - SPI slave devices. 56 - 57 - - Functionality provided by the CAIF SPI interface: 58 - 59 - :: 60 - 61 - void (*ss_cb) (bool assert, struct cfspi_ifc *ifc); 62 - 63 - This function is called by the CAIF SPI slave device in order to 64 - signal a change of state of the input GPIO (SS) to the interface. 65 - Only active edges are mandatory to be reported. 66 - This function can be called from IRQ context (recommended in order 67 - not to introduce latency). The ifc parameter should be the pointer 68 - returned from the platform probe function in the SPI device structure. 69 - 70 - :: 71 - 72 - void (*xfer_done_cb) (struct cfspi_ifc *ifc); 73 - 74 - This function is called by the CAIF SPI slave device in order to 75 - report that a transfer is completed. This function should only be 76 - called once both the transmission and the reception are completed. 77 - This function can be called from IRQ context (recommended in order 78 - not to introduce latency). The ifc parameter should be the pointer 79 - returned from the platform probe function in the SPI device structure. 80 - 81 - - Connecting the bits and pieces: 82 - 83 - - Filling in the SPI slave device structure: 84 - 85 - Connect the necessary callback functions. 86 - 87 - Indicate clock speed (used to calculate toggle delays). 88 - 89 - Chose a suitable name (helps debugging if you use several CAIF 90 - SPI slave devices). 91 - 92 - Assign your private data (can be used to map to your 93 - structure). 94 - 95 - - Filling in the SPI slave platform device structure: 96 - 97 - Add name of driver to connect to ("cfspi_sspi"). 98 - 99 - Assign the SPI slave device structure as platform data. 100 - 101 - Padding 102 - ======= 103 - 104 - In order to optimize throughput, a number of SPI padding options are provided. 105 - Padding can be enabled independently for uplink and downlink transfers. 106 - Padding can be enabled for the head, the tail and for the total frame size. 107 - The padding needs to be correctly configured on both sides of the link. 108 - The padding can be changed via module parameters in cfspi_sspi.c or via 109 - the sysfs directory of the cfspi_sspi driver (before device registration). 110 - 111 - - CAIF SPI device template:: 112 - 113 - /* 114 - * Copyright (C) ST-Ericsson AB 2010 115 - * Author: Daniel Martensson / Daniel.Martensson@stericsson.com 116 - * License terms: GNU General Public License (GPL), version 2. 117 - * 118 - */ 119 - 120 - #include <linux/init.h> 121 - #include <linux/module.h> 122 - #include <linux/device.h> 123 - #include <linux/wait.h> 124 - #include <linux/interrupt.h> 125 - #include <linux/dma-mapping.h> 126 - #include <net/caif/caif_spi.h> 127 - 128 - MODULE_LICENSE("GPL"); 129 - 130 - struct sspi_struct { 131 - struct cfspi_dev sdev; 132 - struct cfspi_xfer *xfer; 133 - }; 134 - 135 - static struct sspi_struct slave; 136 - static struct platform_device slave_device; 137 - 138 - static irqreturn_t sspi_irq(int irq, void *arg) 139 - { 140 - /* You only need to trigger on an edge to the active state of the 141 - * SS signal. Once a edge is detected, the ss_cb() function should be 142 - * called with the parameter assert set to true. It is OK 143 - * (and even advised) to call the ss_cb() function in IRQ context in 144 - * order not to add any delay. */ 145 - 146 - return IRQ_HANDLED; 147 - } 148 - 149 - static void sspi_complete(void *context) 150 - { 151 - /* Normally the DMA or the SPI framework will call you back 152 - * in something similar to this. The only thing you need to 153 - * do is to call the xfer_done_cb() function, providing the pointer 154 - * to the CAIF SPI interface. It is OK to call this function 155 - * from IRQ context. */ 156 - } 157 - 158 - static int sspi_init_xfer(struct cfspi_xfer *xfer, struct cfspi_dev *dev) 159 - { 160 - /* Store transfer info. For a normal implementation you should 161 - * set up your DMA here and make sure that you are ready to 162 - * receive the data from the master SPI. */ 163 - 164 - struct sspi_struct *sspi = (struct sspi_struct *)dev->priv; 165 - 166 - sspi->xfer = xfer; 167 - 168 - return 0; 169 - } 170 - 171 - void sspi_sig_xfer(bool xfer, struct cfspi_dev *dev) 172 - { 173 - /* If xfer is true then you should assert the SPI_INT to indicate to 174 - * the master that you are ready to receive the data from the master 175 - * SPI. If xfer is false then you should de-assert SPI_INT to indicate 176 - * that the transfer is done. 177 - */ 178 - 179 - struct sspi_struct *sspi = (struct sspi_struct *)dev->priv; 180 - } 181 - 182 - static void sspi_release(struct device *dev) 183 - { 184 - /* 185 - * Here you should release your SPI device resources. 186 - */ 187 - } 188 - 189 - static int __init sspi_init(void) 190 - { 191 - /* Here you should initialize your SPI device by providing the 192 - * necessary functions, clock speed, name and private data. Once 193 - * done, you can register your device with the 194 - * platform_device_register() function. This function will return 195 - * with the CAIF SPI interface initialized. This is probably also 196 - * the place where you should set up your GPIOs, interrupts and SPI 197 - * resources. */ 198 - 199 - int res = 0; 200 - 201 - /* Initialize slave device. */ 202 - slave.sdev.init_xfer = sspi_init_xfer; 203 - slave.sdev.sig_xfer = sspi_sig_xfer; 204 - slave.sdev.clk_mhz = 13; 205 - slave.sdev.priv = &slave; 206 - slave.sdev.name = "spi_sspi"; 207 - slave_device.dev.release = sspi_release; 208 - 209 - /* Initialize platform device. */ 210 - slave_device.name = "cfspi_sspi"; 211 - slave_device.dev.platform_data = &slave.sdev; 212 - 213 - /* Register platform device. */ 214 - res = platform_device_register(&slave_device); 215 - if (res) { 216 - printk(KERN_WARNING "sspi_init: failed to register dev.\n"); 217 - return -ENODEV; 218 - } 219 - 220 - return res; 221 - } 222 - 223 - static void __exit sspi_exit(void) 224 - { 225 - platform_device_del(&slave_device); 226 - } 227 - 228 - module_init(sspi_init); 229 - module_exit(sspi_exit);
-19
drivers/net/caif/Kconfig
··· 20 20 identified as N_CAIF. When this ldisc is opened from user space 21 21 it will redirect the TTY's traffic into the CAIF stack. 22 22 23 - config CAIF_SPI_SLAVE 24 - tristate "CAIF SPI transport driver for slave interface" 25 - depends on CAIF && HAS_DMA 26 - default n 27 - help 28 - The CAIF Link layer SPI Protocol driver for Slave SPI interface. 29 - This driver implements a platform driver to accommodate for a 30 - platform specific SPI device. A sample CAIF SPI Platform device is 31 - provided in <file:Documentation/networking/caif/spi_porting.rst>. 32 - 33 - config CAIF_SPI_SYNC 34 - bool "Next command and length in start of frame" 35 - depends on CAIF_SPI_SLAVE 36 - default n 37 - help 38 - Putting the next command and length in the start of the frame can 39 - help to synchronize to the next transfer in case of over or under-runs. 40 - This option also needs to be enabled on the modem. 41 - 42 23 config CAIF_HSI 43 24 tristate "CAIF HSI transport driver" 44 25 depends on CAIF
-4
drivers/net/caif/Makefile
··· 4 4 # Serial interface 5 5 obj-$(CONFIG_CAIF_TTY) += caif_serial.o 6 6 7 - # SPI slave physical interfaces module 8 - cfspi_slave-objs := caif_spi.o caif_spi_slave.o 9 - obj-$(CONFIG_CAIF_SPI_SLAVE) += cfspi_slave.o 10 - 11 7 # HSI interface 12 8 obj-$(CONFIG_CAIF_HSI) += caif_hsi.o 13 9
-874
drivers/net/caif/caif_spi.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * Copyright (C) ST-Ericsson AB 2010 4 - * Author: Daniel Martensson 5 - */ 6 - 7 - #include <linux/init.h> 8 - #include <linux/module.h> 9 - #include <linux/device.h> 10 - #include <linux/platform_device.h> 11 - #include <linux/string.h> 12 - #include <linux/workqueue.h> 13 - #include <linux/completion.h> 14 - #include <linux/list.h> 15 - #include <linux/interrupt.h> 16 - #include <linux/dma-mapping.h> 17 - #include <linux/delay.h> 18 - #include <linux/sched.h> 19 - #include <linux/debugfs.h> 20 - #include <linux/if_arp.h> 21 - #include <net/caif/caif_layer.h> 22 - #include <net/caif/caif_spi.h> 23 - 24 - #ifndef CONFIG_CAIF_SPI_SYNC 25 - #define FLAVOR "Flavour: Vanilla.\n" 26 - #else 27 - #define FLAVOR "Flavour: Master CMD&LEN at start.\n" 28 - #endif /* CONFIG_CAIF_SPI_SYNC */ 29 - 30 - MODULE_LICENSE("GPL"); 31 - MODULE_AUTHOR("Daniel Martensson"); 32 - MODULE_DESCRIPTION("CAIF SPI driver"); 33 - 34 - /* Returns the number of padding bytes for alignment. */ 35 - #define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1))))) 36 - 37 - static bool spi_loop; 38 - module_param(spi_loop, bool, 0444); 39 - MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode."); 40 - 41 - /* SPI frame alignment. */ 42 - module_param(spi_frm_align, int, 0444); 43 - MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment."); 44 - 45 - /* 46 - * SPI padding options. 47 - * Warning: must be a base of 2 (& operation used) and can not be zero ! 48 - */ 49 - module_param(spi_up_head_align, int, 0444); 50 - MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment."); 51 - 52 - module_param(spi_up_tail_align, int, 0444); 53 - MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment."); 54 - 55 - module_param(spi_down_head_align, int, 0444); 56 - MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment."); 57 - 58 - module_param(spi_down_tail_align, int, 0444); 59 - MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment."); 60 - 61 - #ifdef CONFIG_ARM 62 - #define BYTE_HEX_FMT "%02X" 63 - #else 64 - #define BYTE_HEX_FMT "%02hhX" 65 - #endif 66 - 67 - #define SPI_MAX_PAYLOAD_SIZE 4096 68 - /* 69 - * Threshold values for the SPI packet queue. Flowcontrol will be asserted 70 - * when the number of packets exceeds HIGH_WATER_MARK. It will not be 71 - * deasserted before the number of packets drops below LOW_WATER_MARK. 72 - */ 73 - #define LOW_WATER_MARK 100 74 - #define HIGH_WATER_MARK (LOW_WATER_MARK*5) 75 - 76 - #ifndef CONFIG_HAS_DMA 77 - 78 - /* 79 - * We sometimes use UML for debugging, but it cannot handle 80 - * dma_alloc_coherent so we have to wrap it. 81 - */ 82 - static inline void *dma_alloc(struct cfspi *cfspi, dma_addr_t *daddr) 83 - { 84 - return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL); 85 - } 86 - 87 - static inline void dma_free(struct cfspi *cfspi, void *cpu_addr, 88 - dma_addr_t handle) 89 - { 90 - kfree(cpu_addr); 91 - } 92 - 93 - #else 94 - 95 - static inline void *dma_alloc(struct cfspi *cfspi, dma_addr_t *daddr) 96 - { 97 - return dma_alloc_coherent(&cfspi->pdev->dev, SPI_DMA_BUF_LEN, daddr, 98 - GFP_KERNEL); 99 - } 100 - 101 - static inline void dma_free(struct cfspi *cfspi, void *cpu_addr, 102 - dma_addr_t handle) 103 - { 104 - dma_free_coherent(&cfspi->pdev->dev, SPI_DMA_BUF_LEN, cpu_addr, handle); 105 - } 106 - #endif /* CONFIG_HAS_DMA */ 107 - 108 - #ifdef CONFIG_DEBUG_FS 109 - 110 - #define DEBUGFS_BUF_SIZE 4096 111 - 112 - static struct dentry *dbgfs_root; 113 - 114 - static inline void driver_debugfs_create(void) 115 - { 116 - dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL); 117 - } 118 - 119 - static inline void driver_debugfs_remove(void) 120 - { 121 - debugfs_remove(dbgfs_root); 122 - } 123 - 124 - static inline void dev_debugfs_rem(struct cfspi *cfspi) 125 - { 126 - debugfs_remove(cfspi->dbgfs_frame); 127 - debugfs_remove(cfspi->dbgfs_state); 128 - debugfs_remove(cfspi->dbgfs_dir); 129 - } 130 - 131 - static ssize_t dbgfs_state(struct file *file, char __user *user_buf, 132 - size_t count, loff_t *ppos) 133 - { 134 - char *buf; 135 - int len = 0; 136 - ssize_t size; 137 - struct cfspi *cfspi = file->private_data; 138 - 139 - buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); 140 - if (!buf) 141 - return 0; 142 - 143 - /* Print out debug information. */ 144 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 145 - "CAIF SPI debug information:\n"); 146 - 147 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR); 148 - 149 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 150 - "STATE: %d\n", cfspi->dbg_state); 151 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 152 - "Previous CMD: 0x%x\n", cfspi->pcmd); 153 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 154 - "Current CMD: 0x%x\n", cfspi->cmd); 155 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 156 - "Previous TX len: %d\n", cfspi->tx_ppck_len); 157 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 158 - "Previous RX len: %d\n", cfspi->rx_ppck_len); 159 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 160 - "Current TX len: %d\n", cfspi->tx_cpck_len); 161 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 162 - "Current RX len: %d\n", cfspi->rx_cpck_len); 163 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 164 - "Next TX len: %d\n", cfspi->tx_npck_len); 165 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 166 - "Next RX len: %d\n", cfspi->rx_npck_len); 167 - 168 - if (len > DEBUGFS_BUF_SIZE) 169 - len = DEBUGFS_BUF_SIZE; 170 - 171 - size = simple_read_from_buffer(user_buf, count, ppos, buf, len); 172 - kfree(buf); 173 - 174 - return size; 175 - } 176 - 177 - static ssize_t print_frame(char *buf, size_t size, char *frm, 178 - size_t count, size_t cut) 179 - { 180 - int len = 0; 181 - int i; 182 - for (i = 0; i < count; i++) { 183 - len += scnprintf((buf + len), (size - len), 184 - "[0x" BYTE_HEX_FMT "]", 185 - frm[i]); 186 - if ((i == cut) && (count > (cut * 2))) { 187 - /* Fast forward. */ 188 - i = count - cut; 189 - len += scnprintf((buf + len), (size - len), 190 - "--- %zu bytes skipped ---\n", 191 - count - (cut * 2)); 192 - } 193 - 194 - if ((!(i % 10)) && i) { 195 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 196 - "\n"); 197 - } 198 - } 199 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n"); 200 - return len; 201 - } 202 - 203 - static ssize_t dbgfs_frame(struct file *file, char __user *user_buf, 204 - size_t count, loff_t *ppos) 205 - { 206 - char *buf; 207 - int len = 0; 208 - ssize_t size; 209 - struct cfspi *cfspi; 210 - 211 - cfspi = file->private_data; 212 - buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); 213 - if (!buf) 214 - return 0; 215 - 216 - /* Print out debug information. */ 217 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 218 - "Current frame:\n"); 219 - 220 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 221 - "Tx data (Len: %d):\n", cfspi->tx_cpck_len); 222 - 223 - len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), 224 - cfspi->xfer.va_tx[0], 225 - (cfspi->tx_cpck_len + SPI_CMD_SZ), 100); 226 - 227 - len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), 228 - "Rx data (Len: %d):\n", cfspi->rx_cpck_len); 229 - 230 - len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), 231 - cfspi->xfer.va_rx, 232 - (cfspi->rx_cpck_len + SPI_CMD_SZ), 100); 233 - 234 - size = simple_read_from_buffer(user_buf, count, ppos, buf, len); 235 - kfree(buf); 236 - 237 - return size; 238 - } 239 - 240 - static const struct file_operations dbgfs_state_fops = { 241 - .open = simple_open, 242 - .read = dbgfs_state, 243 - .owner = THIS_MODULE 244 - }; 245 - 246 - static const struct file_operations dbgfs_frame_fops = { 247 - .open = simple_open, 248 - .read = dbgfs_frame, 249 - .owner = THIS_MODULE 250 - }; 251 - 252 - static inline void dev_debugfs_add(struct cfspi *cfspi) 253 - { 254 - cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root); 255 - cfspi->dbgfs_state = debugfs_create_file("state", 0444, 256 - cfspi->dbgfs_dir, cfspi, 257 - &dbgfs_state_fops); 258 - cfspi->dbgfs_frame = debugfs_create_file("frame", 0444, 259 - cfspi->dbgfs_dir, cfspi, 260 - &dbgfs_frame_fops); 261 - } 262 - 263 - inline void cfspi_dbg_state(struct cfspi *cfspi, int state) 264 - { 265 - cfspi->dbg_state = state; 266 - }; 267 - #else 268 - 269 - static inline void driver_debugfs_create(void) 270 - { 271 - } 272 - 273 - static inline void driver_debugfs_remove(void) 274 - { 275 - } 276 - 277 - static inline void dev_debugfs_add(struct cfspi *cfspi) 278 - { 279 - } 280 - 281 - static inline void dev_debugfs_rem(struct cfspi *cfspi) 282 - { 283 - } 284 - 285 - inline void cfspi_dbg_state(struct cfspi *cfspi, int state) 286 - { 287 - } 288 - #endif /* CONFIG_DEBUG_FS */ 289 - 290 - static LIST_HEAD(cfspi_list); 291 - static spinlock_t cfspi_list_lock; 292 - 293 - /* SPI uplink head alignment. */ 294 - static ssize_t up_head_align_show(struct device_driver *driver, char *buf) 295 - { 296 - return sprintf(buf, "%d\n", spi_up_head_align); 297 - } 298 - 299 - static DRIVER_ATTR_RO(up_head_align); 300 - 301 - /* SPI uplink tail alignment. */ 302 - static ssize_t up_tail_align_show(struct device_driver *driver, char *buf) 303 - { 304 - return sprintf(buf, "%d\n", spi_up_tail_align); 305 - } 306 - 307 - static DRIVER_ATTR_RO(up_tail_align); 308 - 309 - /* SPI downlink head alignment. */ 310 - static ssize_t down_head_align_show(struct device_driver *driver, char *buf) 311 - { 312 - return sprintf(buf, "%d\n", spi_down_head_align); 313 - } 314 - 315 - static DRIVER_ATTR_RO(down_head_align); 316 - 317 - /* SPI downlink tail alignment. */ 318 - static ssize_t down_tail_align_show(struct device_driver *driver, char *buf) 319 - { 320 - return sprintf(buf, "%d\n", spi_down_tail_align); 321 - } 322 - 323 - static DRIVER_ATTR_RO(down_tail_align); 324 - 325 - /* SPI frame alignment. */ 326 - static ssize_t frame_align_show(struct device_driver *driver, char *buf) 327 - { 328 - return sprintf(buf, "%d\n", spi_frm_align); 329 - } 330 - 331 - static DRIVER_ATTR_RO(frame_align); 332 - 333 - int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len) 334 - { 335 - u8 *dst = buf; 336 - caif_assert(buf); 337 - 338 - if (cfspi->slave && !cfspi->slave_talked) 339 - cfspi->slave_talked = true; 340 - 341 - do { 342 - struct sk_buff *skb; 343 - struct caif_payload_info *info; 344 - int spad = 0; 345 - int epad; 346 - 347 - skb = skb_dequeue(&cfspi->chead); 348 - if (!skb) 349 - break; 350 - 351 - /* 352 - * Calculate length of frame including SPI padding. 353 - * The payload position is found in the control buffer. 354 - */ 355 - info = (struct caif_payload_info *)&skb->cb; 356 - 357 - /* 358 - * Compute head offset i.e. number of bytes to add to 359 - * get the start of the payload aligned. 360 - */ 361 - if (spi_up_head_align > 1) { 362 - spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); 363 - *dst = (u8)(spad - 1); 364 - dst += spad; 365 - } 366 - 367 - /* Copy in CAIF frame. */ 368 - skb_copy_bits(skb, 0, dst, skb->len); 369 - dst += skb->len; 370 - cfspi->ndev->stats.tx_packets++; 371 - cfspi->ndev->stats.tx_bytes += skb->len; 372 - 373 - /* 374 - * Compute tail offset i.e. number of bytes to add to 375 - * get the complete CAIF frame aligned. 376 - */ 377 - epad = PAD_POW2((skb->len + spad), spi_up_tail_align); 378 - dst += epad; 379 - 380 - dev_kfree_skb(skb); 381 - 382 - } while ((dst - buf) < len); 383 - 384 - return dst - buf; 385 - } 386 - 387 - int cfspi_xmitlen(struct cfspi *cfspi) 388 - { 389 - struct sk_buff *skb = NULL; 390 - int frm_len = 0; 391 - int pkts = 0; 392 - 393 - /* 394 - * Decommit previously committed frames. 395 - * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead) 396 - */ 397 - while (skb_peek(&cfspi->chead)) { 398 - skb = skb_dequeue_tail(&cfspi->chead); 399 - skb_queue_head(&cfspi->qhead, skb); 400 - } 401 - 402 - do { 403 - struct caif_payload_info *info = NULL; 404 - int spad = 0; 405 - int epad = 0; 406 - 407 - skb = skb_dequeue(&cfspi->qhead); 408 - if (!skb) 409 - break; 410 - 411 - /* 412 - * Calculate length of frame including SPI padding. 413 - * The payload position is found in the control buffer. 414 - */ 415 - info = (struct caif_payload_info *)&skb->cb; 416 - 417 - /* 418 - * Compute head offset i.e. number of bytes to add to 419 - * get the start of the payload aligned. 420 - */ 421 - if (spi_up_head_align > 1) 422 - spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); 423 - 424 - /* 425 - * Compute tail offset i.e. number of bytes to add to 426 - * get the complete CAIF frame aligned. 427 - */ 428 - epad = PAD_POW2((skb->len + spad), spi_up_tail_align); 429 - 430 - if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) { 431 - skb_queue_tail(&cfspi->chead, skb); 432 - pkts++; 433 - frm_len += skb->len + spad + epad; 434 - } else { 435 - /* Put back packet. */ 436 - skb_queue_head(&cfspi->qhead, skb); 437 - break; 438 - } 439 - } while (pkts <= CAIF_MAX_SPI_PKTS); 440 - 441 - /* 442 - * Send flow on if previously sent flow off 443 - * and now go below the low water mark 444 - */ 445 - if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark && 446 - cfspi->cfdev.flowctrl) { 447 - cfspi->flow_off_sent = 0; 448 - cfspi->cfdev.flowctrl(cfspi->ndev, 1); 449 - } 450 - 451 - return frm_len; 452 - } 453 - 454 - static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc) 455 - { 456 - struct cfspi *cfspi = (struct cfspi *)ifc->priv; 457 - 458 - /* 459 - * The slave device is the master on the link. Interrupts before the 460 - * slave has transmitted are considered spurious. 461 - */ 462 - if (cfspi->slave && !cfspi->slave_talked) { 463 - printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n"); 464 - return; 465 - } 466 - 467 - if (!in_interrupt()) 468 - spin_lock(&cfspi->lock); 469 - if (assert) { 470 - set_bit(SPI_SS_ON, &cfspi->state); 471 - set_bit(SPI_XFER, &cfspi->state); 472 - } else { 473 - set_bit(SPI_SS_OFF, &cfspi->state); 474 - } 475 - if (!in_interrupt()) 476 - spin_unlock(&cfspi->lock); 477 - 478 - /* Wake up the xfer thread. */ 479 - if (assert) 480 - wake_up_interruptible(&cfspi->wait); 481 - } 482 - 483 - static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc) 484 - { 485 - struct cfspi *cfspi = (struct cfspi *)ifc->priv; 486 - 487 - /* Transfer done, complete work queue */ 488 - complete(&cfspi->comp); 489 - } 490 - 491 - static netdev_tx_t cfspi_xmit(struct sk_buff *skb, struct net_device *dev) 492 - { 493 - struct cfspi *cfspi = NULL; 494 - unsigned long flags; 495 - if (!dev) 496 - return -EINVAL; 497 - 498 - cfspi = netdev_priv(dev); 499 - 500 - skb_queue_tail(&cfspi->qhead, skb); 501 - 502 - spin_lock_irqsave(&cfspi->lock, flags); 503 - if (!test_and_set_bit(SPI_XFER, &cfspi->state)) { 504 - /* Wake up xfer thread. */ 505 - wake_up_interruptible(&cfspi->wait); 506 - } 507 - spin_unlock_irqrestore(&cfspi->lock, flags); 508 - 509 - /* Send flow off if number of bytes is above high water mark */ 510 - if (!cfspi->flow_off_sent && 511 - cfspi->qhead.qlen > cfspi->qd_high_mark && 512 - cfspi->cfdev.flowctrl) { 513 - cfspi->flow_off_sent = 1; 514 - cfspi->cfdev.flowctrl(cfspi->ndev, 0); 515 - } 516 - 517 - return NETDEV_TX_OK; 518 - } 519 - 520 - int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len) 521 - { 522 - u8 *src = buf; 523 - 524 - caif_assert(buf != NULL); 525 - 526 - do { 527 - int res; 528 - struct sk_buff *skb = NULL; 529 - int spad = 0; 530 - int epad = 0; 531 - int pkt_len = 0; 532 - 533 - /* 534 - * Compute head offset i.e. number of bytes added to 535 - * get the start of the payload aligned. 536 - */ 537 - if (spi_down_head_align > 1) { 538 - spad = 1 + *src; 539 - src += spad; 540 - } 541 - 542 - /* Read length of CAIF frame (little endian). */ 543 - pkt_len = *src; 544 - pkt_len |= ((*(src+1)) << 8) & 0xFF00; 545 - pkt_len += 2; /* Add FCS fields. */ 546 - 547 - /* Get a suitable caif packet and copy in data. */ 548 - 549 - skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1); 550 - caif_assert(skb != NULL); 551 - 552 - skb_put_data(skb, src, pkt_len); 553 - src += pkt_len; 554 - 555 - skb->protocol = htons(ETH_P_CAIF); 556 - skb_reset_mac_header(skb); 557 - 558 - /* 559 - * Push received packet up the stack. 560 - */ 561 - if (!spi_loop) 562 - res = netif_rx_ni(skb); 563 - else 564 - res = cfspi_xmit(skb, cfspi->ndev); 565 - 566 - if (!res) { 567 - cfspi->ndev->stats.rx_packets++; 568 - cfspi->ndev->stats.rx_bytes += pkt_len; 569 - } else 570 - cfspi->ndev->stats.rx_dropped++; 571 - 572 - /* 573 - * Compute tail offset i.e. number of bytes added to 574 - * get the complete CAIF frame aligned. 575 - */ 576 - epad = PAD_POW2((pkt_len + spad), spi_down_tail_align); 577 - src += epad; 578 - } while ((src - buf) < len); 579 - 580 - return src - buf; 581 - } 582 - 583 - static int cfspi_open(struct net_device *dev) 584 - { 585 - netif_wake_queue(dev); 586 - return 0; 587 - } 588 - 589 - static int cfspi_close(struct net_device *dev) 590 - { 591 - netif_stop_queue(dev); 592 - return 0; 593 - } 594 - 595 - static int cfspi_init(struct net_device *dev) 596 - { 597 - int res = 0; 598 - struct cfspi *cfspi = netdev_priv(dev); 599 - 600 - /* Set flow info. */ 601 - cfspi->flow_off_sent = 0; 602 - cfspi->qd_low_mark = LOW_WATER_MARK; 603 - cfspi->qd_high_mark = HIGH_WATER_MARK; 604 - 605 - /* Set slave info. */ 606 - if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) { 607 - cfspi->slave = true; 608 - cfspi->slave_talked = false; 609 - } else { 610 - cfspi->slave = false; 611 - cfspi->slave_talked = false; 612 - } 613 - 614 - /* Allocate DMA buffers. */ 615 - cfspi->xfer.va_tx[0] = dma_alloc(cfspi, &cfspi->xfer.pa_tx[0]); 616 - if (!cfspi->xfer.va_tx[0]) { 617 - res = -ENODEV; 618 - goto err_dma_alloc_tx_0; 619 - } 620 - 621 - cfspi->xfer.va_rx = dma_alloc(cfspi, &cfspi->xfer.pa_rx); 622 - 623 - if (!cfspi->xfer.va_rx) { 624 - res = -ENODEV; 625 - goto err_dma_alloc_rx; 626 - } 627 - 628 - /* Initialize the work queue. */ 629 - INIT_WORK(&cfspi->work, cfspi_xfer); 630 - 631 - /* Initialize spin locks. */ 632 - spin_lock_init(&cfspi->lock); 633 - 634 - /* Initialize flow control state. */ 635 - cfspi->flow_stop = false; 636 - 637 - /* Initialize wait queue. */ 638 - init_waitqueue_head(&cfspi->wait); 639 - 640 - /* Create work thread. */ 641 - cfspi->wq = create_singlethread_workqueue(dev->name); 642 - if (!cfspi->wq) { 643 - printk(KERN_WARNING "CFSPI: failed to create work queue.\n"); 644 - res = -ENODEV; 645 - goto err_create_wq; 646 - } 647 - 648 - /* Initialize work queue. */ 649 - init_completion(&cfspi->comp); 650 - 651 - /* Create debugfs entries. */ 652 - dev_debugfs_add(cfspi); 653 - 654 - /* Set up the ifc. */ 655 - cfspi->ifc.ss_cb = cfspi_ss_cb; 656 - cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb; 657 - cfspi->ifc.priv = cfspi; 658 - 659 - /* Add CAIF SPI device to list. */ 660 - spin_lock(&cfspi_list_lock); 661 - list_add_tail(&cfspi->list, &cfspi_list); 662 - spin_unlock(&cfspi_list_lock); 663 - 664 - /* Schedule the work queue. */ 665 - queue_work(cfspi->wq, &cfspi->work); 666 - 667 - return 0; 668 - 669 - err_create_wq: 670 - dma_free(cfspi, cfspi->xfer.va_rx, cfspi->xfer.pa_rx); 671 - err_dma_alloc_rx: 672 - dma_free(cfspi, cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]); 673 - err_dma_alloc_tx_0: 674 - return res; 675 - } 676 - 677 - static void cfspi_uninit(struct net_device *dev) 678 - { 679 - struct cfspi *cfspi = netdev_priv(dev); 680 - 681 - /* Remove from list. */ 682 - spin_lock(&cfspi_list_lock); 683 - list_del(&cfspi->list); 684 - spin_unlock(&cfspi_list_lock); 685 - 686 - cfspi->ndev = NULL; 687 - /* Free DMA buffers. */ 688 - dma_free(cfspi, cfspi->xfer.va_rx, cfspi->xfer.pa_rx); 689 - dma_free(cfspi, cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]); 690 - set_bit(SPI_TERMINATE, &cfspi->state); 691 - wake_up_interruptible(&cfspi->wait); 692 - destroy_workqueue(cfspi->wq); 693 - /* Destroy debugfs directory and files. */ 694 - dev_debugfs_rem(cfspi); 695 - return; 696 - } 697 - 698 - static const struct net_device_ops cfspi_ops = { 699 - .ndo_open = cfspi_open, 700 - .ndo_stop = cfspi_close, 701 - .ndo_init = cfspi_init, 702 - .ndo_uninit = cfspi_uninit, 703 - .ndo_start_xmit = cfspi_xmit 704 - }; 705 - 706 - static void cfspi_setup(struct net_device *dev) 707 - { 708 - struct cfspi *cfspi = netdev_priv(dev); 709 - dev->features = 0; 710 - dev->netdev_ops = &cfspi_ops; 711 - dev->type = ARPHRD_CAIF; 712 - dev->flags = IFF_NOARP | IFF_POINTOPOINT; 713 - dev->priv_flags |= IFF_NO_QUEUE; 714 - dev->mtu = SPI_MAX_PAYLOAD_SIZE; 715 - dev->needs_free_netdev = true; 716 - skb_queue_head_init(&cfspi->qhead); 717 - skb_queue_head_init(&cfspi->chead); 718 - cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW; 719 - cfspi->cfdev.use_frag = false; 720 - cfspi->cfdev.use_stx = false; 721 - cfspi->cfdev.use_fcs = false; 722 - cfspi->ndev = dev; 723 - } 724 - 725 - int cfspi_spi_probe(struct platform_device *pdev) 726 - { 727 - struct cfspi *cfspi = NULL; 728 - struct net_device *ndev; 729 - struct cfspi_dev *dev; 730 - int res; 731 - dev = (struct cfspi_dev *)pdev->dev.platform_data; 732 - 733 - if (!dev) 734 - return -ENODEV; 735 - 736 - ndev = alloc_netdev(sizeof(struct cfspi), "cfspi%d", 737 - NET_NAME_UNKNOWN, cfspi_setup); 738 - if (!ndev) 739 - return -ENOMEM; 740 - 741 - cfspi = netdev_priv(ndev); 742 - netif_stop_queue(ndev); 743 - cfspi->ndev = ndev; 744 - cfspi->pdev = pdev; 745 - 746 - /* Assign the SPI device. */ 747 - cfspi->dev = dev; 748 - /* Assign the device ifc to this SPI interface. */ 749 - dev->ifc = &cfspi->ifc; 750 - 751 - /* Register network device. */ 752 - res = register_netdev(ndev); 753 - if (res) { 754 - printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res); 755 - goto err_net_reg; 756 - } 757 - return res; 758 - 759 - err_net_reg: 760 - free_netdev(ndev); 761 - 762 - return res; 763 - } 764 - 765 - int cfspi_spi_remove(struct platform_device *pdev) 766 - { 767 - /* Everything is done in cfspi_uninit(). */ 768 - return 0; 769 - } 770 - 771 - static void __exit cfspi_exit_module(void) 772 - { 773 - struct list_head *list_node; 774 - struct list_head *n; 775 - struct cfspi *cfspi = NULL; 776 - 777 - list_for_each_safe(list_node, n, &cfspi_list) { 778 - cfspi = list_entry(list_node, struct cfspi, list); 779 - unregister_netdev(cfspi->ndev); 780 - } 781 - 782 - /* Destroy sysfs files. */ 783 - driver_remove_file(&cfspi_spi_driver.driver, 784 - &driver_attr_up_head_align); 785 - driver_remove_file(&cfspi_spi_driver.driver, 786 - &driver_attr_up_tail_align); 787 - driver_remove_file(&cfspi_spi_driver.driver, 788 - &driver_attr_down_head_align); 789 - driver_remove_file(&cfspi_spi_driver.driver, 790 - &driver_attr_down_tail_align); 791 - driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align); 792 - /* Unregister platform driver. */ 793 - platform_driver_unregister(&cfspi_spi_driver); 794 - /* Destroy debugfs root directory. */ 795 - driver_debugfs_remove(); 796 - } 797 - 798 - static int __init cfspi_init_module(void) 799 - { 800 - int result; 801 - 802 - /* Initialize spin lock. */ 803 - spin_lock_init(&cfspi_list_lock); 804 - 805 - /* Register platform driver. */ 806 - result = platform_driver_register(&cfspi_spi_driver); 807 - if (result) { 808 - printk(KERN_ERR "Could not register platform SPI driver.\n"); 809 - goto err_dev_register; 810 - } 811 - 812 - /* Create sysfs files. */ 813 - result = 814 - driver_create_file(&cfspi_spi_driver.driver, 815 - &driver_attr_up_head_align); 816 - if (result) { 817 - printk(KERN_ERR "Sysfs creation failed 1.\n"); 818 - goto err_create_up_head_align; 819 - } 820 - 821 - result = 822 - driver_create_file(&cfspi_spi_driver.driver, 823 - &driver_attr_up_tail_align); 824 - if (result) { 825 - printk(KERN_ERR "Sysfs creation failed 2.\n"); 826 - goto err_create_up_tail_align; 827 - } 828 - 829 - result = 830 - driver_create_file(&cfspi_spi_driver.driver, 831 - &driver_attr_down_head_align); 832 - if (result) { 833 - printk(KERN_ERR "Sysfs creation failed 3.\n"); 834 - goto err_create_down_head_align; 835 - } 836 - 837 - result = 838 - driver_create_file(&cfspi_spi_driver.driver, 839 - &driver_attr_down_tail_align); 840 - if (result) { 841 - printk(KERN_ERR "Sysfs creation failed 4.\n"); 842 - goto err_create_down_tail_align; 843 - } 844 - 845 - result = 846 - driver_create_file(&cfspi_spi_driver.driver, 847 - &driver_attr_frame_align); 848 - if (result) { 849 - printk(KERN_ERR "Sysfs creation failed 5.\n"); 850 - goto err_create_frame_align; 851 - } 852 - driver_debugfs_create(); 853 - return result; 854 - 855 - err_create_frame_align: 856 - driver_remove_file(&cfspi_spi_driver.driver, 857 - &driver_attr_down_tail_align); 858 - err_create_down_tail_align: 859 - driver_remove_file(&cfspi_spi_driver.driver, 860 - &driver_attr_down_head_align); 861 - err_create_down_head_align: 862 - driver_remove_file(&cfspi_spi_driver.driver, 863 - &driver_attr_up_tail_align); 864 - err_create_up_tail_align: 865 - driver_remove_file(&cfspi_spi_driver.driver, 866 - &driver_attr_up_head_align); 867 - err_create_up_head_align: 868 - platform_driver_unregister(&cfspi_spi_driver); 869 - err_dev_register: 870 - return result; 871 - } 872 - 873 - module_init(cfspi_init_module); 874 - module_exit(cfspi_exit_module);
-254
drivers/net/caif/caif_spi_slave.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * Copyright (C) ST-Ericsson AB 2010 4 - * Author: Daniel Martensson 5 - */ 6 - #include <linux/module.h> 7 - #include <linux/device.h> 8 - #include <linux/platform_device.h> 9 - #include <linux/string.h> 10 - #include <linux/semaphore.h> 11 - #include <linux/workqueue.h> 12 - #include <linux/completion.h> 13 - #include <linux/list.h> 14 - #include <linux/interrupt.h> 15 - #include <linux/dma-mapping.h> 16 - #include <linux/delay.h> 17 - #include <linux/sched.h> 18 - #include <linux/debugfs.h> 19 - #include <net/caif/caif_spi.h> 20 - 21 - #ifndef CONFIG_CAIF_SPI_SYNC 22 - #define SPI_DATA_POS 0 23 - static inline int forward_to_spi_cmd(struct cfspi *cfspi) 24 - { 25 - return cfspi->rx_cpck_len; 26 - } 27 - #else 28 - #define SPI_DATA_POS SPI_CMD_SZ 29 - static inline int forward_to_spi_cmd(struct cfspi *cfspi) 30 - { 31 - return 0; 32 - } 33 - #endif 34 - 35 - int spi_frm_align = 2; 36 - 37 - /* 38 - * SPI padding options. 39 - * Warning: must be a base of 2 (& operation used) and can not be zero ! 40 - */ 41 - int spi_up_head_align = 1 << 1; 42 - int spi_up_tail_align = 1 << 0; 43 - int spi_down_head_align = 1 << 2; 44 - int spi_down_tail_align = 1 << 1; 45 - 46 - #ifdef CONFIG_DEBUG_FS 47 - static inline void debugfs_store_prev(struct cfspi *cfspi) 48 - { 49 - /* Store previous command for debugging reasons.*/ 50 - cfspi->pcmd = cfspi->cmd; 51 - /* Store previous transfer. */ 52 - cfspi->tx_ppck_len = cfspi->tx_cpck_len; 53 - cfspi->rx_ppck_len = cfspi->rx_cpck_len; 54 - } 55 - #else 56 - static inline void debugfs_store_prev(struct cfspi *cfspi) 57 - { 58 - } 59 - #endif 60 - 61 - void cfspi_xfer(struct work_struct *work) 62 - { 63 - struct cfspi *cfspi; 64 - u8 *ptr = NULL; 65 - unsigned long flags; 66 - int ret; 67 - cfspi = container_of(work, struct cfspi, work); 68 - 69 - /* Initialize state. */ 70 - cfspi->cmd = SPI_CMD_EOT; 71 - 72 - for (;;) { 73 - 74 - cfspi_dbg_state(cfspi, CFSPI_STATE_WAITING); 75 - 76 - /* Wait for master talk or transmit event. */ 77 - wait_event_interruptible(cfspi->wait, 78 - test_bit(SPI_XFER, &cfspi->state) || 79 - test_bit(SPI_TERMINATE, &cfspi->state)); 80 - 81 - if (test_bit(SPI_TERMINATE, &cfspi->state)) 82 - return; 83 - 84 - #if CFSPI_DBG_PREFILL 85 - /* Prefill buffers for easier debugging. */ 86 - memset(cfspi->xfer.va_tx, 0xFF, SPI_DMA_BUF_LEN); 87 - memset(cfspi->xfer.va_rx, 0xFF, SPI_DMA_BUF_LEN); 88 - #endif /* CFSPI_DBG_PREFILL */ 89 - 90 - cfspi_dbg_state(cfspi, CFSPI_STATE_AWAKE); 91 - 92 - /* Check whether we have a committed frame. */ 93 - if (cfspi->tx_cpck_len) { 94 - int len; 95 - 96 - cfspi_dbg_state(cfspi, CFSPI_STATE_FETCH_PKT); 97 - 98 - /* Copy committed SPI frames after the SPI indication. */ 99 - ptr = (u8 *) cfspi->xfer.va_tx; 100 - ptr += SPI_IND_SZ; 101 - len = cfspi_xmitfrm(cfspi, ptr, cfspi->tx_cpck_len); 102 - WARN_ON(len != cfspi->tx_cpck_len); 103 - } 104 - 105 - cfspi_dbg_state(cfspi, CFSPI_STATE_GET_NEXT); 106 - 107 - /* Get length of next frame to commit. */ 108 - cfspi->tx_npck_len = cfspi_xmitlen(cfspi); 109 - 110 - WARN_ON(cfspi->tx_npck_len > SPI_DMA_BUF_LEN); 111 - 112 - /* 113 - * Add indication and length at the beginning of the frame, 114 - * using little endian. 115 - */ 116 - ptr = (u8 *) cfspi->xfer.va_tx; 117 - *ptr++ = SPI_CMD_IND; 118 - *ptr++ = (SPI_CMD_IND & 0xFF00) >> 8; 119 - *ptr++ = cfspi->tx_npck_len & 0x00FF; 120 - *ptr++ = (cfspi->tx_npck_len & 0xFF00) >> 8; 121 - 122 - /* Calculate length of DMAs. */ 123 - cfspi->xfer.tx_dma_len = cfspi->tx_cpck_len + SPI_IND_SZ; 124 - cfspi->xfer.rx_dma_len = cfspi->rx_cpck_len + SPI_CMD_SZ; 125 - 126 - /* Add SPI TX frame alignment padding, if necessary. */ 127 - if (cfspi->tx_cpck_len && 128 - (cfspi->xfer.tx_dma_len % spi_frm_align)) { 129 - 130 - cfspi->xfer.tx_dma_len += spi_frm_align - 131 - (cfspi->xfer.tx_dma_len % spi_frm_align); 132 - } 133 - 134 - /* Add SPI RX frame alignment padding, if necessary. */ 135 - if (cfspi->rx_cpck_len && 136 - (cfspi->xfer.rx_dma_len % spi_frm_align)) { 137 - 138 - cfspi->xfer.rx_dma_len += spi_frm_align - 139 - (cfspi->xfer.rx_dma_len % spi_frm_align); 140 - } 141 - 142 - cfspi_dbg_state(cfspi, CFSPI_STATE_INIT_XFER); 143 - 144 - /* Start transfer. */ 145 - ret = cfspi->dev->init_xfer(&cfspi->xfer, cfspi->dev); 146 - WARN_ON(ret); 147 - 148 - cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_ACTIVE); 149 - 150 - /* 151 - * TODO: We might be able to make an assumption if this is the 152 - * first loop. Make sure that minimum toggle time is respected. 153 - */ 154 - udelay(MIN_TRANSITION_TIME_USEC); 155 - 156 - cfspi_dbg_state(cfspi, CFSPI_STATE_SIG_ACTIVE); 157 - 158 - /* Signal that we are ready to receive data. */ 159 - cfspi->dev->sig_xfer(true, cfspi->dev); 160 - 161 - cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_XFER_DONE); 162 - 163 - /* Wait for transfer completion. */ 164 - wait_for_completion(&cfspi->comp); 165 - 166 - cfspi_dbg_state(cfspi, CFSPI_STATE_XFER_DONE); 167 - 168 - if (cfspi->cmd == SPI_CMD_EOT) { 169 - /* 170 - * Clear the master talk bit. A xfer is always at 171 - * least two bursts. 172 - */ 173 - clear_bit(SPI_SS_ON, &cfspi->state); 174 - } 175 - 176 - cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_INACTIVE); 177 - 178 - /* Make sure that the minimum toggle time is respected. */ 179 - if (SPI_XFER_TIME_USEC(cfspi->xfer.tx_dma_len, 180 - cfspi->dev->clk_mhz) < 181 - MIN_TRANSITION_TIME_USEC) { 182 - 183 - udelay(MIN_TRANSITION_TIME_USEC - 184 - SPI_XFER_TIME_USEC 185 - (cfspi->xfer.tx_dma_len, cfspi->dev->clk_mhz)); 186 - } 187 - 188 - cfspi_dbg_state(cfspi, CFSPI_STATE_SIG_INACTIVE); 189 - 190 - /* De-assert transfer signal. */ 191 - cfspi->dev->sig_xfer(false, cfspi->dev); 192 - 193 - /* Check whether we received a CAIF packet. */ 194 - if (cfspi->rx_cpck_len) { 195 - int len; 196 - 197 - cfspi_dbg_state(cfspi, CFSPI_STATE_DELIVER_PKT); 198 - 199 - /* Parse SPI frame. */ 200 - ptr = ((u8 *)(cfspi->xfer.va_rx + SPI_DATA_POS)); 201 - 202 - len = cfspi_rxfrm(cfspi, ptr, cfspi->rx_cpck_len); 203 - WARN_ON(len != cfspi->rx_cpck_len); 204 - } 205 - 206 - /* Check the next SPI command and length. */ 207 - ptr = (u8 *) cfspi->xfer.va_rx; 208 - 209 - ptr += forward_to_spi_cmd(cfspi); 210 - 211 - cfspi->cmd = *ptr++; 212 - cfspi->cmd |= ((*ptr++) << 8) & 0xFF00; 213 - cfspi->rx_npck_len = *ptr++; 214 - cfspi->rx_npck_len |= ((*ptr++) << 8) & 0xFF00; 215 - 216 - WARN_ON(cfspi->rx_npck_len > SPI_DMA_BUF_LEN); 217 - WARN_ON(cfspi->cmd > SPI_CMD_EOT); 218 - 219 - debugfs_store_prev(cfspi); 220 - 221 - /* Check whether the master issued an EOT command. */ 222 - if (cfspi->cmd == SPI_CMD_EOT) { 223 - /* Reset state. */ 224 - cfspi->tx_cpck_len = 0; 225 - cfspi->rx_cpck_len = 0; 226 - } else { 227 - /* Update state. */ 228 - cfspi->tx_cpck_len = cfspi->tx_npck_len; 229 - cfspi->rx_cpck_len = cfspi->rx_npck_len; 230 - } 231 - 232 - /* 233 - * Check whether we need to clear the xfer bit. 234 - * Spin lock needed for packet insertion. 235 - * Test and clear of different bits 236 - * are not supported. 237 - */ 238 - spin_lock_irqsave(&cfspi->lock, flags); 239 - if (cfspi->cmd == SPI_CMD_EOT && !cfspi_xmitlen(cfspi) 240 - && !test_bit(SPI_SS_ON, &cfspi->state)) 241 - clear_bit(SPI_XFER, &cfspi->state); 242 - 243 - spin_unlock_irqrestore(&cfspi->lock, flags); 244 - } 245 - } 246 - 247 - struct platform_driver cfspi_spi_driver = { 248 - .probe = cfspi_spi_probe, 249 - .remove = cfspi_spi_remove, 250 - .driver = { 251 - .name = "cfspi_sspi", 252 - .owner = THIS_MODULE, 253 - }, 254 - };
-155
include/net/caif/caif_spi.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * Copyright (C) ST-Ericsson AB 2010 4 - * Author: Daniel Martensson / Daniel.Martensson@stericsson.com 5 - */ 6 - 7 - #ifndef CAIF_SPI_H_ 8 - #define CAIF_SPI_H_ 9 - 10 - #include <net/caif/caif_device.h> 11 - 12 - #define SPI_CMD_WR 0x00 13 - #define SPI_CMD_RD 0x01 14 - #define SPI_CMD_EOT 0x02 15 - #define SPI_CMD_IND 0x04 16 - 17 - #define SPI_DMA_BUF_LEN 8192 18 - 19 - #define WL_SZ 2 /* 16 bits. */ 20 - #define SPI_CMD_SZ 4 /* 32 bits. */ 21 - #define SPI_IND_SZ 4 /* 32 bits. */ 22 - 23 - #define SPI_XFER 0 24 - #define SPI_SS_ON 1 25 - #define SPI_SS_OFF 2 26 - #define SPI_TERMINATE 3 27 - 28 - /* Minimum time between different levels is 50 microseconds. */ 29 - #define MIN_TRANSITION_TIME_USEC 50 30 - 31 - /* Defines for calculating duration of SPI transfers for a particular 32 - * number of bytes. 33 - */ 34 - #define SPI_MASTER_CLK_MHZ 13 35 - #define SPI_XFER_TIME_USEC(bytes, clk) (((bytes) * 8) / clk) 36 - 37 - /* Normally this should be aligned on the modem in order to benefit from full 38 - * duplex transfers. However a size of 8188 provokes errors when running with 39 - * the modem. These errors occur when packet sizes approaches 4 kB of data. 40 - */ 41 - #define CAIF_MAX_SPI_FRAME 4092 42 - 43 - /* Maximum number of uplink CAIF frames that can reside in the same SPI frame. 44 - * This number should correspond with the modem setting. The application side 45 - * CAIF accepts any number of embedded downlink CAIF frames. 46 - */ 47 - #define CAIF_MAX_SPI_PKTS 9 48 - 49 - /* Decides if SPI buffers should be prefilled with 0xFF pattern for easier 50 - * debugging. Both TX and RX buffers will be filled before the transfer. 51 - */ 52 - #define CFSPI_DBG_PREFILL 0 53 - 54 - /* Structure describing a SPI transfer. */ 55 - struct cfspi_xfer { 56 - u16 tx_dma_len; 57 - u16 rx_dma_len; 58 - void *va_tx[2]; 59 - dma_addr_t pa_tx[2]; 60 - void *va_rx; 61 - dma_addr_t pa_rx; 62 - }; 63 - 64 - /* Structure implemented by the SPI interface. */ 65 - struct cfspi_ifc { 66 - void (*ss_cb) (bool assert, struct cfspi_ifc *ifc); 67 - void (*xfer_done_cb) (struct cfspi_ifc *ifc); 68 - void *priv; 69 - }; 70 - 71 - /* Structure implemented by SPI clients. */ 72 - struct cfspi_dev { 73 - int (*init_xfer) (struct cfspi_xfer *xfer, struct cfspi_dev *dev); 74 - void (*sig_xfer) (bool xfer, struct cfspi_dev *dev); 75 - struct cfspi_ifc *ifc; 76 - char *name; 77 - u32 clk_mhz; 78 - void *priv; 79 - }; 80 - 81 - /* Enumeration describing the CAIF SPI state. */ 82 - enum cfspi_state { 83 - CFSPI_STATE_WAITING = 0, 84 - CFSPI_STATE_AWAKE, 85 - CFSPI_STATE_FETCH_PKT, 86 - CFSPI_STATE_GET_NEXT, 87 - CFSPI_STATE_INIT_XFER, 88 - CFSPI_STATE_WAIT_ACTIVE, 89 - CFSPI_STATE_SIG_ACTIVE, 90 - CFSPI_STATE_WAIT_XFER_DONE, 91 - CFSPI_STATE_XFER_DONE, 92 - CFSPI_STATE_WAIT_INACTIVE, 93 - CFSPI_STATE_SIG_INACTIVE, 94 - CFSPI_STATE_DELIVER_PKT, 95 - CFSPI_STATE_MAX, 96 - }; 97 - 98 - /* Structure implemented by SPI physical interfaces. */ 99 - struct cfspi { 100 - struct caif_dev_common cfdev; 101 - struct net_device *ndev; 102 - struct platform_device *pdev; 103 - struct sk_buff_head qhead; 104 - struct sk_buff_head chead; 105 - u16 cmd; 106 - u16 tx_cpck_len; 107 - u16 tx_npck_len; 108 - u16 rx_cpck_len; 109 - u16 rx_npck_len; 110 - struct cfspi_ifc ifc; 111 - struct cfspi_xfer xfer; 112 - struct cfspi_dev *dev; 113 - unsigned long state; 114 - struct work_struct work; 115 - struct workqueue_struct *wq; 116 - struct list_head list; 117 - int flow_off_sent; 118 - u32 qd_low_mark; 119 - u32 qd_high_mark; 120 - struct completion comp; 121 - wait_queue_head_t wait; 122 - spinlock_t lock; 123 - bool flow_stop; 124 - bool slave; 125 - bool slave_talked; 126 - #ifdef CONFIG_DEBUG_FS 127 - enum cfspi_state dbg_state; 128 - u16 pcmd; 129 - u16 tx_ppck_len; 130 - u16 rx_ppck_len; 131 - struct dentry *dbgfs_dir; 132 - struct dentry *dbgfs_state; 133 - struct dentry *dbgfs_frame; 134 - #endif /* CONFIG_DEBUG_FS */ 135 - }; 136 - 137 - extern int spi_frm_align; 138 - extern int spi_up_head_align; 139 - extern int spi_up_tail_align; 140 - extern int spi_down_head_align; 141 - extern int spi_down_tail_align; 142 - extern struct platform_driver cfspi_spi_driver; 143 - 144 - void cfspi_dbg_state(struct cfspi *cfspi, int state); 145 - int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len); 146 - int cfspi_xmitlen(struct cfspi *cfspi); 147 - int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len); 148 - int cfspi_spi_remove(struct platform_device *pdev); 149 - int cfspi_spi_probe(struct platform_device *pdev); 150 - int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len); 151 - int cfspi_xmitlen(struct cfspi *cfspi); 152 - int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len); 153 - void cfspi_xfer(struct work_struct *work); 154 - 155 - #endif /* CAIF_SPI_H_ */