Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Sample fifo dma implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * This module shows how to handle fifo dma operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) /* fifo size in elements (bytes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define FIFO_SIZE	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static struct kfifo fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static int __init example_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	unsigned int		ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int		nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct scatterlist	sg[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	printk(KERN_INFO "DMA fifo test start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		printk(KERN_WARNING "error kfifo_alloc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	kfifo_in(&fifo, "test", 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	for (i = 0; i != 9; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		kfifo_put(&fifo, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/* kick away first byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	kfifo_skip(&fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * Configure the kfifo buffer to receive data from DMA input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 *  .--------------------------------------.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 *  | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 *  |---|------------------|---------------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 *   \_/ \________________/ \_____________/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 *    \          \                  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 *     \          \_allocated data   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 *      \_*free space*                \_*free space*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * We need two different SG entries: one for the free space area at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * end of the kfifo buffer (19 bytes) and another for the first free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * byte at the beginning, after the kfifo_skip().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	sg_init_table(sg, ARRAY_SIZE(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	nents = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	printk(KERN_INFO "DMA sgl entries: %d\n", nents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (!nents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		/* fifo is full and no sgl was created */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		printk(KERN_WARNING "error kfifo_dma_in_prepare\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* receive data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	printk(KERN_INFO "scatterlist for receive:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	for (i = 0; i < nents; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		"sg[%d] -> "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		"page %p offset 0x%.8x length 0x%.8x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			i, sg_page(&sg[i]), sg[i].offset, sg[i].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (sg_is_last(&sg[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/* put here your code to setup and exectute the dma operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	/* example: zero bytes received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* finish the dma operation and update the received data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	kfifo_dma_in_finish(&fifo, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/* Prepare to transmit data, example: 8 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	nents = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	printk(KERN_INFO "DMA sgl entries: %d\n", nents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!nents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		/* no data was available and no sgl was created */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		printk(KERN_WARNING "error kfifo_dma_out_prepare\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	printk(KERN_INFO "scatterlist for transmit:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	for (i = 0; i < nents; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		"sg[%d] -> "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		"page %p offset 0x%.8x length 0x%.8x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			i, sg_page(&sg[i]), sg[i].offset, sg[i].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (sg_is_last(&sg[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	/* put here your code to setup and exectute the dma operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* example: 5 bytes transmitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ret = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* finish the dma operation and update the transmitted data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	kfifo_dma_out_finish(&fifo, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ret = kfifo_len(&fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (ret != 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		printk(KERN_WARNING "size mismatch: test failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	printk(KERN_INFO "test passed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void __exit example_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	kfifo_free(&fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) module_init(example_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) module_exit(example_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");