Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
| 3 | * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com |
| 4 | * Author: Daniel Martensson / Daniel.Martensson@stericsson.com |
| 5 | * License terms: GNU General Public License (GPL) version 2. |
| 6 | */ |
| 7 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/workqueue.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/if_arp.h> |
| 22 | #include <net/caif/caif_layer.h> |
| 23 | #include <net/caif/caif_spi.h> |
| 24 | |
| 25 | #ifndef CONFIG_CAIF_SPI_SYNC |
| 26 | #define FLAVOR "Flavour: Vanilla.\n" |
| 27 | #else |
| 28 | #define FLAVOR "Flavour: Master CMD&LEN at start.\n" |
| 29 | #endif /* CONFIG_CAIF_SPI_SYNC */ |
| 30 | |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>"); |
| 33 | MODULE_DESCRIPTION("CAIF SPI driver"); |
| 34 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 35 | /* Returns the number of padding bytes for alignment. */ |
| 36 | #define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1))))) |
| 37 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 38 | static bool spi_loop; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 39 | module_param(spi_loop, bool, S_IRUGO); |
| 40 | MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode."); |
| 41 | |
| 42 | /* SPI frame alignment. */ |
| 43 | module_param(spi_frm_align, int, S_IRUGO); |
| 44 | MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment."); |
| 45 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 46 | /* |
| 47 | * SPI padding options. |
| 48 | * Warning: must be a base of 2 (& operation used) and can not be zero ! |
| 49 | */ |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 50 | module_param(spi_up_head_align, int, S_IRUGO); |
| 51 | MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment."); |
| 52 | |
| 53 | module_param(spi_up_tail_align, int, S_IRUGO); |
| 54 | MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment."); |
| 55 | |
| 56 | module_param(spi_down_head_align, int, S_IRUGO); |
| 57 | MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment."); |
| 58 | |
| 59 | module_param(spi_down_tail_align, int, S_IRUGO); |
| 60 | MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment."); |
| 61 | |
| 62 | #ifdef CONFIG_ARM |
| 63 | #define BYTE_HEX_FMT "%02X" |
| 64 | #else |
| 65 | #define BYTE_HEX_FMT "%02hhX" |
| 66 | #endif |
| 67 | |
| 68 | #define SPI_MAX_PAYLOAD_SIZE 4096 |
| 69 | /* |
| 70 | * Threshold values for the SPI packet queue. Flowcontrol will be asserted |
| 71 | * when the number of packets exceeds HIGH_WATER_MARK. It will not be |
| 72 | * deasserted before the number of packets drops below LOW_WATER_MARK. |
| 73 | */ |
| 74 | #define LOW_WATER_MARK 100 |
| 75 | #define HIGH_WATER_MARK (LOW_WATER_MARK*5) |
| 76 | |
| 77 | #ifdef CONFIG_UML |
| 78 | |
| 79 | /* |
| 80 | * We sometimes use UML for debugging, but it cannot handle |
| 81 | * dma_alloc_coherent so we have to wrap it. |
| 82 | */ |
| 83 | static inline void *dma_alloc(dma_addr_t *daddr) |
| 84 | { |
| 85 | return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL); |
| 86 | } |
| 87 | |
| 88 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 89 | { |
| 90 | kfree(cpu_addr); |
| 91 | } |
| 92 | |
| 93 | #else |
| 94 | |
| 95 | static inline void *dma_alloc(dma_addr_t *daddr) |
| 96 | { |
| 97 | return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr, |
| 98 | GFP_KERNEL); |
| 99 | } |
| 100 | |
| 101 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 102 | { |
| 103 | dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle); |
| 104 | } |
| 105 | #endif /* CONFIG_UML */ |
| 106 | |
| 107 | #ifdef CONFIG_DEBUG_FS |
| 108 | |
| 109 | #define DEBUGFS_BUF_SIZE 4096 |
| 110 | |
| 111 | static struct dentry *dbgfs_root; |
| 112 | |
| 113 | static inline void driver_debugfs_create(void) |
| 114 | { |
| 115 | dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL); |
| 116 | } |
| 117 | |
| 118 | static inline void driver_debugfs_remove(void) |
| 119 | { |
| 120 | debugfs_remove(dbgfs_root); |
| 121 | } |
| 122 | |
| 123 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 124 | { |
| 125 | debugfs_remove(cfspi->dbgfs_frame); |
| 126 | debugfs_remove(cfspi->dbgfs_state); |
| 127 | debugfs_remove(cfspi->dbgfs_dir); |
| 128 | } |
| 129 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 130 | static ssize_t dbgfs_state(struct file *file, char __user *user_buf, |
| 131 | size_t count, loff_t *ppos) |
| 132 | { |
| 133 | char *buf; |
| 134 | int len = 0; |
| 135 | ssize_t size; |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 136 | struct cfspi *cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 137 | |
| 138 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 139 | if (!buf) |
| 140 | return 0; |
| 141 | |
| 142 | /* Print out debug information. */ |
| 143 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 144 | "CAIF SPI debug information:\n"); |
| 145 | |
| 146 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR); |
| 147 | |
| 148 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 149 | "STATE: %d\n", cfspi->dbg_state); |
| 150 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 151 | "Previous CMD: 0x%x\n", cfspi->pcmd); |
| 152 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 153 | "Current CMD: 0x%x\n", cfspi->cmd); |
| 154 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 155 | "Previous TX len: %d\n", cfspi->tx_ppck_len); |
| 156 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 157 | "Previous RX len: %d\n", cfspi->rx_ppck_len); |
| 158 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 159 | "Current TX len: %d\n", cfspi->tx_cpck_len); |
| 160 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 161 | "Current RX len: %d\n", cfspi->rx_cpck_len); |
| 162 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 163 | "Next TX len: %d\n", cfspi->tx_npck_len); |
| 164 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 165 | "Next RX len: %d\n", cfspi->rx_npck_len); |
| 166 | |
Dan Carpenter | 7b7b0b9 | 2010-07-25 21:23:59 +0000 | [diff] [blame] | 167 | if (len > DEBUGFS_BUF_SIZE) |
| 168 | len = DEBUGFS_BUF_SIZE; |
| 169 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 170 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 171 | kfree(buf); |
| 172 | |
| 173 | return size; |
| 174 | } |
| 175 | |
| 176 | static ssize_t print_frame(char *buf, size_t size, char *frm, |
| 177 | size_t count, size_t cut) |
| 178 | { |
| 179 | int len = 0; |
| 180 | int i; |
| 181 | for (i = 0; i < count; i++) { |
| 182 | len += snprintf((buf + len), (size - len), |
| 183 | "[0x" BYTE_HEX_FMT "]", |
| 184 | frm[i]); |
| 185 | if ((i == cut) && (count > (cut * 2))) { |
| 186 | /* Fast forward. */ |
| 187 | i = count - cut; |
| 188 | len += snprintf((buf + len), (size - len), |
| 189 | "--- %u bytes skipped ---\n", |
| 190 | (int)(count - (cut * 2))); |
| 191 | } |
| 192 | |
| 193 | if ((!(i % 10)) && i) { |
| 194 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 195 | "\n"); |
| 196 | } |
| 197 | } |
| 198 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n"); |
| 199 | return len; |
| 200 | } |
| 201 | |
| 202 | static ssize_t dbgfs_frame(struct file *file, char __user *user_buf, |
| 203 | size_t count, loff_t *ppos) |
| 204 | { |
| 205 | char *buf; |
| 206 | int len = 0; |
| 207 | ssize_t size; |
| 208 | struct cfspi *cfspi; |
| 209 | |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 210 | cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 211 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 212 | if (!buf) |
| 213 | return 0; |
| 214 | |
| 215 | /* Print out debug information. */ |
| 216 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 217 | "Current frame:\n"); |
| 218 | |
| 219 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 220 | "Tx data (Len: %d):\n", cfspi->tx_cpck_len); |
| 221 | |
| 222 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 223 | cfspi->xfer.va_tx[0], |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 224 | (cfspi->tx_cpck_len + SPI_CMD_SZ), 100); |
| 225 | |
| 226 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 227 | "Rx data (Len: %d):\n", cfspi->rx_cpck_len); |
| 228 | |
| 229 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 230 | cfspi->xfer.va_rx, |
| 231 | (cfspi->rx_cpck_len + SPI_CMD_SZ), 100); |
| 232 | |
| 233 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 234 | kfree(buf); |
| 235 | |
| 236 | return size; |
| 237 | } |
| 238 | |
| 239 | static const struct file_operations dbgfs_state_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 240 | .open = simple_open, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 241 | .read = dbgfs_state, |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 242 | .owner = THIS_MODULE |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static const struct file_operations dbgfs_frame_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 246 | .open = simple_open, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 247 | .read = dbgfs_frame, |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 248 | .owner = THIS_MODULE |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 252 | { |
| 253 | cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root); |
| 254 | cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO, |
| 255 | cfspi->dbgfs_dir, cfspi, |
| 256 | &dbgfs_state_fops); |
| 257 | cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO, |
| 258 | cfspi->dbgfs_dir, cfspi, |
| 259 | &dbgfs_frame_fops); |
| 260 | } |
| 261 | |
| 262 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 263 | { |
| 264 | cfspi->dbg_state = state; |
| 265 | }; |
| 266 | #else |
| 267 | |
| 268 | static inline void driver_debugfs_create(void) |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | static inline void driver_debugfs_remove(void) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 281 | { |
| 282 | } |
| 283 | |
| 284 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 285 | { |
| 286 | } |
| 287 | #endif /* CONFIG_DEBUG_FS */ |
| 288 | |
| 289 | static LIST_HEAD(cfspi_list); |
| 290 | static spinlock_t cfspi_list_lock; |
| 291 | |
| 292 | /* SPI uplink head alignment. */ |
| 293 | static ssize_t show_up_head_align(struct device_driver *driver, char *buf) |
| 294 | { |
| 295 | return sprintf(buf, "%d\n", spi_up_head_align); |
| 296 | } |
| 297 | |
| 298 | static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL); |
| 299 | |
| 300 | /* SPI uplink tail alignment. */ |
| 301 | static ssize_t show_up_tail_align(struct device_driver *driver, char *buf) |
| 302 | { |
| 303 | return sprintf(buf, "%d\n", spi_up_tail_align); |
| 304 | } |
| 305 | |
| 306 | static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL); |
| 307 | |
| 308 | /* SPI downlink head alignment. */ |
| 309 | static ssize_t show_down_head_align(struct device_driver *driver, char *buf) |
| 310 | { |
| 311 | return sprintf(buf, "%d\n", spi_down_head_align); |
| 312 | } |
| 313 | |
| 314 | static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL); |
| 315 | |
| 316 | /* SPI downlink tail alignment. */ |
| 317 | static ssize_t show_down_tail_align(struct device_driver *driver, char *buf) |
| 318 | { |
| 319 | return sprintf(buf, "%d\n", spi_down_tail_align); |
| 320 | } |
| 321 | |
| 322 | static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL); |
| 323 | |
| 324 | /* SPI frame alignment. */ |
| 325 | static ssize_t show_frame_align(struct device_driver *driver, char *buf) |
| 326 | { |
| 327 | return sprintf(buf, "%d\n", spi_frm_align); |
| 328 | } |
| 329 | |
| 330 | static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL); |
| 331 | |
| 332 | int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 333 | { |
| 334 | u8 *dst = buf; |
| 335 | caif_assert(buf); |
| 336 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 337 | if (cfspi->slave && !cfspi->slave_talked) |
| 338 | cfspi->slave_talked = true; |
| 339 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 340 | do { |
| 341 | struct sk_buff *skb; |
| 342 | struct caif_payload_info *info; |
| 343 | int spad = 0; |
| 344 | int epad; |
| 345 | |
| 346 | skb = skb_dequeue(&cfspi->chead); |
| 347 | if (!skb) |
| 348 | break; |
| 349 | |
| 350 | /* |
| 351 | * Calculate length of frame including SPI padding. |
| 352 | * The payload position is found in the control buffer. |
| 353 | */ |
| 354 | info = (struct caif_payload_info *)&skb->cb; |
| 355 | |
| 356 | /* |
| 357 | * Compute head offset i.e. number of bytes to add to |
| 358 | * get the start of the payload aligned. |
| 359 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 360 | if (spi_up_head_align > 1) { |
| 361 | spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 362 | *dst = (u8)(spad - 1); |
| 363 | dst += spad; |
| 364 | } |
| 365 | |
| 366 | /* Copy in CAIF frame. */ |
| 367 | skb_copy_bits(skb, 0, dst, skb->len); |
| 368 | dst += skb->len; |
| 369 | cfspi->ndev->stats.tx_packets++; |
| 370 | cfspi->ndev->stats.tx_bytes += skb->len; |
| 371 | |
| 372 | /* |
| 373 | * Compute tail offset i.e. number of bytes to add to |
| 374 | * get the complete CAIF frame aligned. |
| 375 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 376 | epad = PAD_POW2((skb->len + spad), spi_up_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 377 | dst += epad; |
| 378 | |
| 379 | dev_kfree_skb(skb); |
| 380 | |
| 381 | } while ((dst - buf) < len); |
| 382 | |
| 383 | return dst - buf; |
| 384 | } |
| 385 | |
| 386 | int cfspi_xmitlen(struct cfspi *cfspi) |
| 387 | { |
| 388 | struct sk_buff *skb = NULL; |
| 389 | int frm_len = 0; |
| 390 | int pkts = 0; |
| 391 | |
| 392 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 393 | * Decommit previously committed frames. |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 394 | * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead) |
| 395 | */ |
| 396 | while (skb_peek(&cfspi->chead)) { |
| 397 | skb = skb_dequeue_tail(&cfspi->chead); |
| 398 | skb_queue_head(&cfspi->qhead, skb); |
| 399 | } |
| 400 | |
| 401 | do { |
| 402 | struct caif_payload_info *info = NULL; |
| 403 | int spad = 0; |
| 404 | int epad = 0; |
| 405 | |
| 406 | skb = skb_dequeue(&cfspi->qhead); |
| 407 | if (!skb) |
| 408 | break; |
| 409 | |
| 410 | /* |
| 411 | * Calculate length of frame including SPI padding. |
| 412 | * The payload position is found in the control buffer. |
| 413 | */ |
| 414 | info = (struct caif_payload_info *)&skb->cb; |
| 415 | |
| 416 | /* |
| 417 | * Compute head offset i.e. number of bytes to add to |
| 418 | * get the start of the payload aligned. |
| 419 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 420 | if (spi_up_head_align > 1) |
| 421 | spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Compute tail offset i.e. number of bytes to add to |
| 425 | * get the complete CAIF frame aligned. |
| 426 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 427 | epad = PAD_POW2((skb->len + spad), spi_up_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 428 | |
| 429 | if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) { |
| 430 | skb_queue_tail(&cfspi->chead, skb); |
| 431 | pkts++; |
| 432 | frm_len += skb->len + spad + epad; |
| 433 | } else { |
| 434 | /* Put back packet. */ |
| 435 | skb_queue_head(&cfspi->qhead, skb); |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 436 | break; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 437 | } |
| 438 | } while (pkts <= CAIF_MAX_SPI_PKTS); |
| 439 | |
| 440 | /* |
| 441 | * Send flow on if previously sent flow off |
| 442 | * and now go below the low water mark |
| 443 | */ |
| 444 | if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark && |
| 445 | cfspi->cfdev.flowctrl) { |
| 446 | cfspi->flow_off_sent = 0; |
| 447 | cfspi->cfdev.flowctrl(cfspi->ndev, 1); |
| 448 | } |
| 449 | |
| 450 | return frm_len; |
| 451 | } |
| 452 | |
| 453 | static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc) |
| 454 | { |
| 455 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 456 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 457 | /* |
| 458 | * The slave device is the master on the link. Interrupts before the |
| 459 | * slave has transmitted are considered spurious. |
| 460 | */ |
| 461 | if (cfspi->slave && !cfspi->slave_talked) { |
| 462 | printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n"); |
| 463 | return; |
| 464 | } |
| 465 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 466 | if (!in_interrupt()) |
| 467 | spin_lock(&cfspi->lock); |
| 468 | if (assert) { |
| 469 | set_bit(SPI_SS_ON, &cfspi->state); |
| 470 | set_bit(SPI_XFER, &cfspi->state); |
| 471 | } else { |
| 472 | set_bit(SPI_SS_OFF, &cfspi->state); |
| 473 | } |
| 474 | if (!in_interrupt()) |
| 475 | spin_unlock(&cfspi->lock); |
| 476 | |
| 477 | /* Wake up the xfer thread. */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 478 | if (assert) |
| 479 | wake_up_interruptible(&cfspi->wait); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc) |
| 483 | { |
| 484 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 485 | |
| 486 | /* Transfer done, complete work queue */ |
| 487 | complete(&cfspi->comp); |
| 488 | } |
| 489 | |
| 490 | static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev) |
| 491 | { |
| 492 | struct cfspi *cfspi = NULL; |
| 493 | unsigned long flags; |
| 494 | if (!dev) |
| 495 | return -EINVAL; |
| 496 | |
| 497 | cfspi = netdev_priv(dev); |
| 498 | |
| 499 | skb_queue_tail(&cfspi->qhead, skb); |
| 500 | |
| 501 | spin_lock_irqsave(&cfspi->lock, flags); |
| 502 | if (!test_and_set_bit(SPI_XFER, &cfspi->state)) { |
| 503 | /* Wake up xfer thread. */ |
| 504 | wake_up_interruptible(&cfspi->wait); |
| 505 | } |
| 506 | spin_unlock_irqrestore(&cfspi->lock, flags); |
| 507 | |
| 508 | /* Send flow off if number of bytes is above high water mark */ |
| 509 | if (!cfspi->flow_off_sent && |
| 510 | cfspi->qhead.qlen > cfspi->qd_high_mark && |
| 511 | cfspi->cfdev.flowctrl) { |
| 512 | cfspi->flow_off_sent = 1; |
| 513 | cfspi->cfdev.flowctrl(cfspi->ndev, 0); |
| 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 520 | { |
| 521 | u8 *src = buf; |
| 522 | |
| 523 | caif_assert(buf != NULL); |
| 524 | |
| 525 | do { |
| 526 | int res; |
| 527 | struct sk_buff *skb = NULL; |
| 528 | int spad = 0; |
| 529 | int epad = 0; |
| 530 | u8 *dst = NULL; |
| 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 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 537 | if (spi_down_head_align > 1) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 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 | dst = skb_put(skb, pkt_len); |
| 553 | memcpy(dst, src, pkt_len); |
| 554 | src += pkt_len; |
| 555 | |
| 556 | skb->protocol = htons(ETH_P_CAIF); |
| 557 | skb_reset_mac_header(skb); |
| 558 | skb->dev = cfspi->ndev; |
| 559 | |
| 560 | /* |
| 561 | * Push received packet up the stack. |
| 562 | */ |
| 563 | if (!spi_loop) |
| 564 | res = netif_rx_ni(skb); |
| 565 | else |
| 566 | res = cfspi_xmit(skb, cfspi->ndev); |
| 567 | |
| 568 | if (!res) { |
| 569 | cfspi->ndev->stats.rx_packets++; |
| 570 | cfspi->ndev->stats.rx_bytes += pkt_len; |
| 571 | } else |
| 572 | cfspi->ndev->stats.rx_dropped++; |
| 573 | |
| 574 | /* |
| 575 | * Compute tail offset i.e. number of bytes added to |
| 576 | * get the complete CAIF frame aligned. |
| 577 | */ |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 578 | epad = PAD_POW2((pkt_len + spad), spi_down_tail_align); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 579 | src += epad; |
| 580 | } while ((src - buf) < len); |
| 581 | |
| 582 | return src - buf; |
| 583 | } |
| 584 | |
| 585 | static int cfspi_open(struct net_device *dev) |
| 586 | { |
| 587 | netif_wake_queue(dev); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int cfspi_close(struct net_device *dev) |
| 592 | { |
| 593 | netif_stop_queue(dev); |
| 594 | return 0; |
| 595 | } |
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 | static int cfspi_init(struct net_device *dev) |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 598 | { |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 599 | int res = 0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 600 | struct cfspi *cfspi = netdev_priv(dev); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 601 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 602 | /* Set flow info. */ |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 603 | cfspi->flow_off_sent = 0; |
| 604 | cfspi->qd_low_mark = LOW_WATER_MARK; |
| 605 | cfspi->qd_high_mark = HIGH_WATER_MARK; |
| 606 | |
Sjur Brændeland | 2c24a5d | 2010-11-01 11:52:48 +0000 | [diff] [blame] | 607 | /* Set slave info. */ |
| 608 | if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) { |
| 609 | cfspi->slave = true; |
| 610 | cfspi->slave_talked = false; |
| 611 | } else { |
| 612 | cfspi->slave = false; |
| 613 | cfspi->slave_talked = false; |
| 614 | } |
| 615 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 616 | /* Allocate DMA buffers. */ |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 617 | cfspi->xfer.va_tx[0] = dma_alloc(&cfspi->xfer.pa_tx[0]); |
| 618 | if (!cfspi->xfer.va_tx[0]) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 619 | res = -ENODEV; |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 620 | goto err_dma_alloc_tx_0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx); |
| 624 | |
| 625 | if (!cfspi->xfer.va_rx) { |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 626 | res = -ENODEV; |
| 627 | goto err_dma_alloc_rx; |
| 628 | } |
| 629 | |
| 630 | /* Initialize the work queue. */ |
| 631 | INIT_WORK(&cfspi->work, cfspi_xfer); |
| 632 | |
| 633 | /* Initialize spin locks. */ |
| 634 | spin_lock_init(&cfspi->lock); |
| 635 | |
| 636 | /* Initialize flow control state. */ |
| 637 | cfspi->flow_stop = false; |
| 638 | |
| 639 | /* Initialize wait queue. */ |
| 640 | init_waitqueue_head(&cfspi->wait); |
| 641 | |
| 642 | /* Create work thread. */ |
| 643 | cfspi->wq = create_singlethread_workqueue(dev->name); |
| 644 | if (!cfspi->wq) { |
| 645 | printk(KERN_WARNING "CFSPI: failed to create work queue.\n"); |
| 646 | res = -ENODEV; |
| 647 | goto err_create_wq; |
| 648 | } |
| 649 | |
| 650 | /* Initialize work queue. */ |
| 651 | init_completion(&cfspi->comp); |
| 652 | |
| 653 | /* Create debugfs entries. */ |
| 654 | dev_debugfs_add(cfspi); |
| 655 | |
| 656 | /* Set up the ifc. */ |
| 657 | cfspi->ifc.ss_cb = cfspi_ss_cb; |
| 658 | cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb; |
| 659 | cfspi->ifc.priv = cfspi; |
| 660 | |
| 661 | /* Add CAIF SPI device to list. */ |
| 662 | spin_lock(&cfspi_list_lock); |
| 663 | list_add_tail(&cfspi->list, &cfspi_list); |
| 664 | spin_unlock(&cfspi_list_lock); |
| 665 | |
| 666 | /* Schedule the work queue. */ |
| 667 | queue_work(cfspi->wq, &cfspi->work); |
| 668 | |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 669 | return 0; |
| 670 | |
| 671 | err_create_wq: |
| 672 | dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx); |
| 673 | err_dma_alloc_rx: |
| 674 | dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]); |
| 675 | err_dma_alloc_tx_0: |
| 676 | return res; |
| 677 | } |
| 678 | |
| 679 | static void cfspi_uninit(struct net_device *dev) |
| 680 | { |
| 681 | struct cfspi *cfspi = netdev_priv(dev); |
| 682 | |
| 683 | /* Remove from list. */ |
| 684 | spin_lock(&cfspi_list_lock); |
| 685 | list_del(&cfspi->list); |
| 686 | spin_unlock(&cfspi_list_lock); |
| 687 | |
| 688 | cfspi->ndev = NULL; |
| 689 | /* Free DMA buffers. */ |
| 690 | dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx); |
| 691 | dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]); |
| 692 | set_bit(SPI_TERMINATE, &cfspi->state); |
| 693 | wake_up_interruptible(&cfspi->wait); |
| 694 | destroy_workqueue(cfspi->wq); |
| 695 | /* Destroy debugfs directory and files. */ |
| 696 | dev_debugfs_rem(cfspi); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | static const struct net_device_ops cfspi_ops = { |
| 701 | .ndo_open = cfspi_open, |
| 702 | .ndo_stop = cfspi_close, |
| 703 | .ndo_init = cfspi_init, |
| 704 | .ndo_uninit = cfspi_uninit, |
| 705 | .ndo_start_xmit = cfspi_xmit |
| 706 | }; |
| 707 | |
| 708 | static void cfspi_setup(struct net_device *dev) |
| 709 | { |
| 710 | struct cfspi *cfspi = netdev_priv(dev); |
| 711 | dev->features = 0; |
| 712 | dev->netdev_ops = &cfspi_ops; |
| 713 | dev->type = ARPHRD_CAIF; |
| 714 | dev->flags = IFF_NOARP | IFF_POINTOPOINT; |
| 715 | dev->tx_queue_len = 0; |
| 716 | dev->mtu = SPI_MAX_PAYLOAD_SIZE; |
| 717 | dev->destructor = free_netdev; |
| 718 | skb_queue_head_init(&cfspi->qhead); |
| 719 | skb_queue_head_init(&cfspi->chead); |
| 720 | cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW; |
| 721 | cfspi->cfdev.use_frag = false; |
| 722 | cfspi->cfdev.use_stx = false; |
| 723 | cfspi->cfdev.use_fcs = false; |
| 724 | cfspi->ndev = dev; |
| 725 | } |
| 726 | |
| 727 | int cfspi_spi_probe(struct platform_device *pdev) |
| 728 | { |
| 729 | struct cfspi *cfspi = NULL; |
| 730 | struct net_device *ndev; |
| 731 | struct cfspi_dev *dev; |
| 732 | int res; |
| 733 | dev = (struct cfspi_dev *)pdev->dev.platform_data; |
| 734 | |
| 735 | ndev = alloc_netdev(sizeof(struct cfspi), |
| 736 | "cfspi%d", cfspi_setup); |
| 737 | if (!dev) |
| 738 | return -ENODEV; |
| 739 | |
| 740 | cfspi = netdev_priv(ndev); |
| 741 | netif_stop_queue(ndev); |
| 742 | cfspi->ndev = ndev; |
| 743 | cfspi->pdev = pdev; |
| 744 | |
| 745 | /* Assign the SPI device. */ |
| 746 | cfspi->dev = dev; |
| 747 | /* Assign the device ifc to this SPI interface. */ |
| 748 | dev->ifc = &cfspi->ifc; |
| 749 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 750 | /* Register network device. */ |
| 751 | res = register_netdev(ndev); |
| 752 | if (res) { |
| 753 | printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res); |
| 754 | goto err_net_reg; |
| 755 | } |
| 756 | return res; |
| 757 | |
| 758 | err_net_reg: |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 759 | free_netdev(ndev); |
| 760 | |
| 761 | return res; |
| 762 | } |
| 763 | |
| 764 | int cfspi_spi_remove(struct platform_device *pdev) |
| 765 | { |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 766 | /* Everything is done in cfspi_uninit(). */ |
| 767 | return 0; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void __exit cfspi_exit_module(void) |
| 771 | { |
| 772 | struct list_head *list_node; |
| 773 | struct list_head *n; |
| 774 | struct cfspi *cfspi = NULL; |
| 775 | |
| 776 | list_for_each_safe(list_node, n, &cfspi_list) { |
| 777 | cfspi = list_entry(list_node, struct cfspi, list); |
Erwan Bracq | d5f43c1 | 2011-12-06 07:25:05 +0000 | [diff] [blame] | 778 | unregister_netdev(cfspi->ndev); |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /* Destroy sysfs files. */ |
| 782 | driver_remove_file(&cfspi_spi_driver.driver, |
| 783 | &driver_attr_up_head_align); |
| 784 | driver_remove_file(&cfspi_spi_driver.driver, |
| 785 | &driver_attr_up_tail_align); |
| 786 | driver_remove_file(&cfspi_spi_driver.driver, |
| 787 | &driver_attr_down_head_align); |
| 788 | driver_remove_file(&cfspi_spi_driver.driver, |
| 789 | &driver_attr_down_tail_align); |
| 790 | driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align); |
| 791 | /* Unregister platform driver. */ |
| 792 | platform_driver_unregister(&cfspi_spi_driver); |
| 793 | /* Destroy debugfs root directory. */ |
| 794 | driver_debugfs_remove(); |
| 795 | } |
| 796 | |
| 797 | static int __init cfspi_init_module(void) |
| 798 | { |
| 799 | int result; |
| 800 | |
| 801 | /* Initialize spin lock. */ |
| 802 | spin_lock_init(&cfspi_list_lock); |
| 803 | |
| 804 | /* Register platform driver. */ |
| 805 | result = platform_driver_register(&cfspi_spi_driver); |
| 806 | if (result) { |
| 807 | printk(KERN_ERR "Could not register platform SPI driver.\n"); |
| 808 | goto err_dev_register; |
| 809 | } |
| 810 | |
| 811 | /* Create sysfs files. */ |
| 812 | result = |
| 813 | driver_create_file(&cfspi_spi_driver.driver, |
| 814 | &driver_attr_up_head_align); |
| 815 | if (result) { |
| 816 | printk(KERN_ERR "Sysfs creation failed 1.\n"); |
| 817 | goto err_create_up_head_align; |
| 818 | } |
| 819 | |
| 820 | result = |
| 821 | driver_create_file(&cfspi_spi_driver.driver, |
| 822 | &driver_attr_up_tail_align); |
| 823 | if (result) { |
| 824 | printk(KERN_ERR "Sysfs creation failed 2.\n"); |
| 825 | goto err_create_up_tail_align; |
| 826 | } |
| 827 | |
| 828 | result = |
| 829 | driver_create_file(&cfspi_spi_driver.driver, |
| 830 | &driver_attr_down_head_align); |
| 831 | if (result) { |
| 832 | printk(KERN_ERR "Sysfs creation failed 3.\n"); |
| 833 | goto err_create_down_head_align; |
| 834 | } |
| 835 | |
| 836 | result = |
| 837 | driver_create_file(&cfspi_spi_driver.driver, |
| 838 | &driver_attr_down_tail_align); |
| 839 | if (result) { |
| 840 | printk(KERN_ERR "Sysfs creation failed 4.\n"); |
| 841 | goto err_create_down_tail_align; |
| 842 | } |
| 843 | |
| 844 | result = |
| 845 | driver_create_file(&cfspi_spi_driver.driver, |
| 846 | &driver_attr_frame_align); |
| 847 | if (result) { |
| 848 | printk(KERN_ERR "Sysfs creation failed 5.\n"); |
| 849 | goto err_create_frame_align; |
| 850 | } |
| 851 | driver_debugfs_create(); |
| 852 | return result; |
| 853 | |
| 854 | err_create_frame_align: |
| 855 | driver_remove_file(&cfspi_spi_driver.driver, |
| 856 | &driver_attr_down_tail_align); |
| 857 | err_create_down_tail_align: |
| 858 | driver_remove_file(&cfspi_spi_driver.driver, |
| 859 | &driver_attr_down_head_align); |
| 860 | err_create_down_head_align: |
| 861 | driver_remove_file(&cfspi_spi_driver.driver, |
| 862 | &driver_attr_up_tail_align); |
| 863 | err_create_up_tail_align: |
| 864 | driver_remove_file(&cfspi_spi_driver.driver, |
| 865 | &driver_attr_up_head_align); |
| 866 | err_create_up_head_align: |
| 867 | err_dev_register: |
| 868 | return result; |
| 869 | } |
| 870 | |
| 871 | module_init(cfspi_init_module); |
| 872 | module_exit(cfspi_exit_module); |