caif_spi.c 21 KB

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