Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
sjur.brandeland@stericsson.com | c2cd0a5 | 2013-04-22 23:57:02 +0000 | [diff] [blame] | 3 | * Author: Daniel Martensson |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 4 | * License terms: GNU General Public License (GPL) version 2. |
| 5 | */ |
| 6 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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"); |
sjur.brandeland@stericsson.com | c2cd0a5 | 2013-04-22 23:57:02 +0000 | [diff] [blame] | 31 | MODULE_AUTHOR("Daniel Martensson"); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 32 | MODULE_DESCRIPTION("CAIF SPI driver"); |
| 33 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 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 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 37 | static bool spi_loop; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 38 | module_param(spi_loop, bool, S_IRUGO); |
| 39 | MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode."); |
| 40 | |
| 41 | /* SPI frame alignment. */ |
| 42 | module_param(spi_frm_align, int, S_IRUGO); |
| 43 | MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment."); |
| 44 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 45 | /* |
| 46 | * SPI padding options. |
| 47 | * Warning: must be a base of 2 (& operation used) and can not be zero ! |
| 48 | */ |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 49 | module_param(spi_up_head_align, int, S_IRUGO); |
| 50 | MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment."); |
| 51 | |
| 52 | module_param(spi_up_tail_align, int, S_IRUGO); |
| 53 | MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment."); |
| 54 | |
| 55 | module_param(spi_down_head_align, int, S_IRUGO); |
| 56 | MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment."); |
| 57 | |
| 58 | module_param(spi_down_tail_align, int, S_IRUGO); |
| 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 | #ifdef CONFIG_UML |
| 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(dma_addr_t *daddr) |
| 83 | { |
| 84 | return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL); |
| 85 | } |
| 86 | |
| 87 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 88 | { |
| 89 | kfree(cpu_addr); |
| 90 | } |
| 91 | |
| 92 | #else |
| 93 | |
| 94 | static inline void *dma_alloc(dma_addr_t *daddr) |
| 95 | { |
| 96 | return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr, |
| 97 | GFP_KERNEL); |
| 98 | } |
| 99 | |
| 100 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 101 | { |
| 102 | dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle); |
| 103 | } |
| 104 | #endif /* CONFIG_UML */ |
| 105 | |
| 106 | #ifdef CONFIG_DEBUG_FS |
| 107 | |
| 108 | #define DEBUGFS_BUF_SIZE 4096 |
| 109 | |
| 110 | static struct dentry *dbgfs_root; |
| 111 | |
| 112 | static inline void driver_debugfs_create(void) |
| 113 | { |
| 114 | dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL); |
| 115 | } |
| 116 | |
| 117 | static inline void driver_debugfs_remove(void) |
| 118 | { |
| 119 | debugfs_remove(dbgfs_root); |
| 120 | } |
| 121 | |
| 122 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 123 | { |
| 124 | debugfs_remove(cfspi->dbgfs_frame); |
| 125 | debugfs_remove(cfspi->dbgfs_state); |
| 126 | debugfs_remove(cfspi->dbgfs_dir); |
| 127 | } |
| 128 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 129 | static ssize_t dbgfs_state(struct file *file, char __user *user_buf, |
| 130 | size_t count, loff_t *ppos) |
| 131 | { |
| 132 | char *buf; |
| 133 | int len = 0; |
| 134 | ssize_t size; |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 135 | struct cfspi *cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 136 | |
| 137 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 138 | if (!buf) |
| 139 | return 0; |
| 140 | |
| 141 | /* Print out debug information. */ |
| 142 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 143 | "CAIF SPI debug information:\n"); |
| 144 | |
| 145 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR); |
| 146 | |
| 147 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 148 | "STATE: %d\n", cfspi->dbg_state); |
| 149 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 150 | "Previous CMD: 0x%x\n", cfspi->pcmd); |
| 151 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 152 | "Current CMD: 0x%x\n", cfspi->cmd); |
| 153 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 154 | "Previous TX len: %d\n", cfspi->tx_ppck_len); |
| 155 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 156 | "Previous RX len: %d\n", cfspi->rx_ppck_len); |
| 157 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 158 | "Current TX len: %d\n", cfspi->tx_cpck_len); |
| 159 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 160 | "Current RX len: %d\n", cfspi->rx_cpck_len); |
| 161 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 162 | "Next TX len: %d\n", cfspi->tx_npck_len); |
| 163 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 164 | "Next RX len: %d\n", cfspi->rx_npck_len); |
| 165 | |
Dan Carpenter | 7b7b0b9 | 2010-07-25 21:23:59 +0000 | [diff] [blame] | 166 | if (len > DEBUGFS_BUF_SIZE) |
| 167 | len = DEBUGFS_BUF_SIZE; |
| 168 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 169 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 170 | kfree(buf); |
| 171 | |
| 172 | return size; |
| 173 | } |
| 174 | |
| 175 | static ssize_t print_frame(char *buf, size_t size, char *frm, |
| 176 | size_t count, size_t cut) |
| 177 | { |
| 178 | int len = 0; |
| 179 | int i; |
| 180 | for (i = 0; i < count; i++) { |
| 181 | len += snprintf((buf + len), (size - len), |
| 182 | "[0x" BYTE_HEX_FMT "]", |
| 183 | frm[i]); |
| 184 | if ((i == cut) && (count > (cut * 2))) { |
| 185 | /* Fast forward. */ |
| 186 | i = count - cut; |
| 187 | len += snprintf((buf + len), (size - len), |
| 188 | "--- %u bytes skipped ---\n", |
| 189 | (int)(count - (cut * 2))); |
| 190 | } |
| 191 | |
| 192 | if ((!(i % 10)) && i) { |
| 193 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 194 | "\n"); |
| 195 | } |
| 196 | } |
| 197 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n"); |
| 198 | return len; |
| 199 | } |
| 200 | |
| 201 | static ssize_t dbgfs_frame(struct file *file, char __user *user_buf, |
| 202 | size_t count, loff_t *ppos) |
| 203 | { |
| 204 | char *buf; |
| 205 | int len = 0; |
| 206 | ssize_t size; |
| 207 | struct cfspi *cfspi; |
| 208 | |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 209 | cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 210 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 211 | if (!buf) |
| 212 | return 0; |
| 213 | |
| 214 | /* Print out debug information. */ |
| 215 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 216 | "Current frame:\n"); |
| 217 | |
| 218 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 219 | "Tx data (Len: %d):\n", cfspi->tx_cpck_len); |
| 220 | |
| 221 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 222 | cfspi->xfer.va_tx[0], |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 223 | (cfspi->tx_cpck_len + SPI_CMD_SZ), 100); |
| 224 | |
| 225 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 226 | "Rx data (Len: %d):\n", cfspi->rx_cpck_len); |
| 227 | |
| 228 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 229 | cfspi->xfer.va_rx, |
| 230 | (cfspi->rx_cpck_len + SPI_CMD_SZ), 100); |
| 231 | |
| 232 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 233 | kfree(buf); |
| 234 | |
| 235 | return size; |
| 236 | } |
| 237 | |
| 238 | static const struct file_operations dbgfs_state_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 239 | .open = simple_open, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 240 | .read = dbgfs_state, |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 241 | .owner = THIS_MODULE |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | static const struct file_operations dbgfs_frame_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 245 | .open = simple_open, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 246 | .read = dbgfs_frame, |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 247 | .owner = THIS_MODULE |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 251 | { |
| 252 | cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root); |
| 253 | cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO, |
| 254 | cfspi->dbgfs_dir, cfspi, |
| 255 | &dbgfs_state_fops); |
| 256 | cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO, |
| 257 | cfspi->dbgfs_dir, cfspi, |
| 258 | &dbgfs_frame_fops); |
| 259 | } |
| 260 | |
| 261 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 262 | { |
| 263 | cfspi->dbg_state = state; |
| 264 | }; |
| 265 | #else |
| 266 | |
| 267 | static inline void driver_debugfs_create(void) |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | static inline void driver_debugfs_remove(void) |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 284 | { |
| 285 | } |
| 286 | #endif /* CONFIG_DEBUG_FS */ |
| 287 | |
| 288 | static LIST_HEAD(cfspi_list); |
| 289 | static spinlock_t cfspi_list_lock; |
| 290 | |
| 291 | /* SPI uplink head alignment. */ |
| 292 | static ssize_t show_up_head_align(struct device_driver *driver, char *buf) |
| 293 | { |
| 294 | return sprintf(buf, "%d\n", spi_up_head_align); |
| 295 | } |
| 296 | |
| 297 | static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL); |
| 298 | |
| 299 | /* SPI uplink tail alignment. */ |
| 300 | static ssize_t show_up_tail_align(struct device_driver *driver, char *buf) |
| 301 | { |
| 302 | return sprintf(buf, "%d\n", spi_up_tail_align); |
| 303 | } |
| 304 | |
| 305 | static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL); |
| 306 | |
| 307 | /* SPI downlink head alignment. */ |
| 308 | static ssize_t show_down_head_align(struct device_driver *driver, char *buf) |
| 309 | { |
| 310 | return sprintf(buf, "%d\n", spi_down_head_align); |
| 311 | } |
| 312 | |
| 313 | static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL); |
| 314 | |
| 315 | /* SPI downlink tail alignment. */ |
| 316 | static ssize_t show_down_tail_align(struct device_driver *driver, char *buf) |
| 317 | { |
| 318 | return sprintf(buf, "%d\n", spi_down_tail_align); |
| 319 | } |
| 320 | |
| 321 | static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL); |
| 322 | |
| 323 | /* SPI frame alignment. */ |
| 324 | static ssize_t show_frame_align(struct device_driver *driver, char *buf) |
| 325 | { |
| 326 | return sprintf(buf, "%d\n", spi_frm_align); |
| 327 | } |
| 328 | |
| 329 | static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL); |
| 330 | |
| 331 | int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 332 | { |
| 333 | u8 *dst = buf; |
| 334 | caif_assert(buf); |
| 335 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 336 | if (cfspi->slave && !cfspi->slave_talked) |
| 337 | cfspi->slave_talked = true; |
| 338 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 339 | do { |
| 340 | struct sk_buff *skb; |
| 341 | struct caif_payload_info *info; |
| 342 | int spad = 0; |
| 343 | int epad; |
| 344 | |
| 345 | skb = skb_dequeue(&cfspi->chead); |
| 346 | if (!skb) |
| 347 | break; |
| 348 | |
| 349 | /* |
| 350 | * Calculate length of frame including SPI padding. |
| 351 | * The payload position is found in the control buffer. |
| 352 | */ |
| 353 | info = (struct caif_payload_info *)&skb->cb; |
| 354 | |
| 355 | /* |
| 356 | * Compute head offset i.e. number of bytes to add to |
| 357 | * get the start of the payload aligned. |
| 358 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 359 | if (spi_up_head_align > 1) { |
| 360 | spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 361 | *dst = (u8)(spad - 1); |
| 362 | dst += spad; |
| 363 | } |
| 364 | |
| 365 | /* Copy in CAIF frame. */ |
| 366 | skb_copy_bits(skb, 0, dst, skb->len); |
| 367 | dst += skb->len; |
| 368 | cfspi->ndev->stats.tx_packets++; |
| 369 | cfspi->ndev->stats.tx_bytes += skb->len; |
| 370 | |
| 371 | /* |
| 372 | * Compute tail offset i.e. number of bytes to add to |
| 373 | * get the complete CAIF frame aligned. |
| 374 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 375 | epad = PAD_POW2((skb->len + spad), spi_up_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 376 | dst += epad; |
| 377 | |
| 378 | dev_kfree_skb(skb); |
| 379 | |
| 380 | } while ((dst - buf) < len); |
| 381 | |
| 382 | return dst - buf; |
| 383 | } |
| 384 | |
| 385 | int cfspi_xmitlen(struct cfspi *cfspi) |
| 386 | { |
| 387 | struct sk_buff *skb = NULL; |
| 388 | int frm_len = 0; |
| 389 | int pkts = 0; |
| 390 | |
| 391 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 392 | * Decommit previously committed frames. |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 393 | * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead) |
| 394 | */ |
| 395 | while (skb_peek(&cfspi->chead)) { |
| 396 | skb = skb_dequeue_tail(&cfspi->chead); |
| 397 | skb_queue_head(&cfspi->qhead, skb); |
| 398 | } |
| 399 | |
| 400 | do { |
| 401 | struct caif_payload_info *info = NULL; |
| 402 | int spad = 0; |
| 403 | int epad = 0; |
| 404 | |
| 405 | skb = skb_dequeue(&cfspi->qhead); |
| 406 | if (!skb) |
| 407 | break; |
| 408 | |
| 409 | /* |
| 410 | * Calculate length of frame including SPI padding. |
| 411 | * The payload position is found in the control buffer. |
| 412 | */ |
| 413 | info = (struct caif_payload_info *)&skb->cb; |
| 414 | |
| 415 | /* |
| 416 | * Compute head offset i.e. number of bytes to add to |
| 417 | * get the start of the payload aligned. |
| 418 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 419 | if (spi_up_head_align > 1) |
| 420 | spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * Compute tail offset i.e. number of bytes to add to |
| 424 | * get the complete CAIF frame aligned. |
| 425 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 426 | epad = PAD_POW2((skb->len + spad), spi_up_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 427 | |
| 428 | if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) { |
| 429 | skb_queue_tail(&cfspi->chead, skb); |
| 430 | pkts++; |
| 431 | frm_len += skb->len + spad + epad; |
| 432 | } else { |
| 433 | /* Put back packet. */ |
| 434 | skb_queue_head(&cfspi->qhead, skb); |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 435 | break; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 436 | } |
| 437 | } while (pkts <= CAIF_MAX_SPI_PKTS); |
| 438 | |
| 439 | /* |
| 440 | * Send flow on if previously sent flow off |
| 441 | * and now go below the low water mark |
| 442 | */ |
| 443 | if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark && |
| 444 | cfspi->cfdev.flowctrl) { |
| 445 | cfspi->flow_off_sent = 0; |
| 446 | cfspi->cfdev.flowctrl(cfspi->ndev, 1); |
| 447 | } |
| 448 | |
| 449 | return frm_len; |
| 450 | } |
| 451 | |
| 452 | static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc) |
| 453 | { |
| 454 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 455 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 456 | /* |
| 457 | * The slave device is the master on the link. Interrupts before the |
| 458 | * slave has transmitted are considered spurious. |
| 459 | */ |
| 460 | if (cfspi->slave && !cfspi->slave_talked) { |
| 461 | printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n"); |
| 462 | return; |
| 463 | } |
| 464 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 465 | if (!in_interrupt()) |
| 466 | spin_lock(&cfspi->lock); |
| 467 | if (assert) { |
| 468 | set_bit(SPI_SS_ON, &cfspi->state); |
| 469 | set_bit(SPI_XFER, &cfspi->state); |
| 470 | } else { |
| 471 | set_bit(SPI_SS_OFF, &cfspi->state); |
| 472 | } |
| 473 | if (!in_interrupt()) |
| 474 | spin_unlock(&cfspi->lock); |
| 475 | |
| 476 | /* Wake up the xfer thread. */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 477 | if (assert) |
| 478 | wake_up_interruptible(&cfspi->wait); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc) |
| 482 | { |
| 483 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 484 | |
| 485 | /* Transfer done, complete work queue */ |
| 486 | complete(&cfspi->comp); |
| 487 | } |
| 488 | |
| 489 | static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev) |
| 490 | { |
| 491 | struct cfspi *cfspi = NULL; |
| 492 | unsigned long flags; |
| 493 | if (!dev) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | cfspi = netdev_priv(dev); |
| 497 | |
| 498 | skb_queue_tail(&cfspi->qhead, skb); |
| 499 | |
| 500 | spin_lock_irqsave(&cfspi->lock, flags); |
| 501 | if (!test_and_set_bit(SPI_XFER, &cfspi->state)) { |
| 502 | /* Wake up xfer thread. */ |
| 503 | wake_up_interruptible(&cfspi->wait); |
| 504 | } |
| 505 | spin_unlock_irqrestore(&cfspi->lock, flags); |
| 506 | |
| 507 | /* Send flow off if number of bytes is above high water mark */ |
| 508 | if (!cfspi->flow_off_sent && |
| 509 | cfspi->qhead.qlen > cfspi->qd_high_mark && |
| 510 | cfspi->cfdev.flowctrl) { |
| 511 | cfspi->flow_off_sent = 1; |
| 512 | cfspi->cfdev.flowctrl(cfspi->ndev, 0); |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 519 | { |
| 520 | u8 *src = buf; |
| 521 | |
| 522 | caif_assert(buf != NULL); |
| 523 | |
| 524 | do { |
| 525 | int res; |
| 526 | struct sk_buff *skb = NULL; |
| 527 | int spad = 0; |
| 528 | int epad = 0; |
| 529 | u8 *dst = NULL; |
| 530 | int pkt_len = 0; |
| 531 | |
| 532 | /* |
| 533 | * Compute head offset i.e. number of bytes added to |
| 534 | * get the start of the payload aligned. |
| 535 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 536 | if (spi_down_head_align > 1) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 537 | spad = 1 + *src; |
| 538 | src += spad; |
| 539 | } |
| 540 | |
| 541 | /* Read length of CAIF frame (little endian). */ |
| 542 | pkt_len = *src; |
| 543 | pkt_len |= ((*(src+1)) << 8) & 0xFF00; |
| 544 | pkt_len += 2; /* Add FCS fields. */ |
| 545 | |
| 546 | /* Get a suitable caif packet and copy in data. */ |
| 547 | |
| 548 | skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1); |
| 549 | caif_assert(skb != NULL); |
| 550 | |
| 551 | dst = skb_put(skb, pkt_len); |
| 552 | memcpy(dst, src, pkt_len); |
| 553 | src += pkt_len; |
| 554 | |
| 555 | skb->protocol = htons(ETH_P_CAIF); |
| 556 | skb_reset_mac_header(skb); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 576 | epad = PAD_POW2((pkt_len + spad), spi_down_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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 | } |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 594 | |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 595 | static int cfspi_init(struct net_device *dev) |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 596 | { |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 597 | int res = 0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 598 | struct cfspi *cfspi = netdev_priv(dev); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 599 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 600 | /* Set flow info. */ |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 601 | cfspi->flow_off_sent = 0; |
| 602 | cfspi->qd_low_mark = LOW_WATER_MARK; |
| 603 | cfspi->qd_high_mark = HIGH_WATER_MARK; |
| 604 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 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 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 614 | /* Allocate DMA buffers. */ |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 615 | cfspi->xfer.va_tx[0] = dma_alloc(&cfspi->xfer.pa_tx[0]); |
| 616 | if (!cfspi->xfer.va_tx[0]) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 617 | res = -ENODEV; |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 618 | goto err_dma_alloc_tx_0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx); |
| 622 | |
| 623 | if (!cfspi->xfer.va_rx) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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 | |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 667 | return 0; |
| 668 | |
| 669 | err_create_wq: |
| 670 | dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx); |
| 671 | err_dma_alloc_rx: |
| 672 | dma_free(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->xfer.va_rx, cfspi->xfer.pa_rx); |
| 689 | dma_free(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; |
Phil Sutter | 4676a15 | 2015-08-18 10:30:46 +0200 | [diff] [blame] | 713 | dev->priv_flags |= IFF_NO_QUEUE; |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 714 | dev->mtu = SPI_MAX_PAYLOAD_SIZE; |
| 715 | dev->destructor = free_netdev; |
| 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 | |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 733 | if (!dev) |
| 734 | return -ENODEV; |
| 735 | |
Rasmus Villemoes | cfb76d7 | 2015-11-09 13:19:10 +0100 | [diff] [blame] | 736 | ndev = alloc_netdev(sizeof(struct cfspi), "cfspi%d", |
| 737 | NET_NAME_UNKNOWN, cfspi_setup); |
| 738 | if (!ndev) |
| 739 | return -ENOMEM; |
| 740 | |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 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 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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: |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 760 | free_netdev(ndev); |
| 761 | |
| 762 | return res; |
| 763 | } |
| 764 | |
| 765 | int cfspi_spi_remove(struct platform_device *pdev) |
| 766 | { |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 767 | /* Everything is done in cfspi_uninit(). */ |
| 768 | return 0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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); |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 779 | unregister_netdev(cfspi->ndev); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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: |
Wei Yongjun | e56db27 | 2013-04-25 15:15:23 +0800 | [diff] [blame] | 868 | platform_driver_unregister(&cfspi_spi_driver); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 869 | err_dev_register: |
| 870 | return result; |
| 871 | } |
| 872 | |
| 873 | module_init(cfspi_init_module); |
| 874 | module_exit(cfspi_exit_module); |