xref: /openwifi/driver/openofdm_tx/openofdm_tx.c (revision 855b59fd6a93401097234a1948a6fe61b591cced)
1 /*
2  * axi lite register access driver
3  * Xianjun jiao. [email protected]; [email protected]
4  */
5 
6 #include <linux/bitops.h>
7 #include <linux/dmapool.h>
8 #include <linux/dma/xilinx_dma.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/iopoll.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_dma.h>
16 #include <linux/of_platform.h>
17 #include <linux/of_irq.h>
18 #include <linux/slab.h>
19 #include <linux/clk.h>
20 #include <linux/io-64-nonatomic-lo-hi.h>
21 #include <linux/delay.h>
22 
23 #include "../hw_def.h"
24 
25 static void __iomem *base_addr; // to store driver specific base address needed for mmu to translate virtual address to physical address in our FPGA design
26 
27 /* IO accessors */
28 static inline u32 reg_read(u32 reg)
29 {
30 	return ioread32(base_addr + reg);
31 }
32 
33 static inline void reg_write(u32 reg, u32 value)
34 {
35 	iowrite32(value, base_addr + reg);
36 }
37 
38 static inline void OPENOFDM_TX_REG_MULTI_RST_write(u32 Data) {
39 	reg_write(OPENOFDM_TX_REG_MULTI_RST_ADDR, Data);
40 }
41 
42 static inline void OPENOFDM_TX_REG_INIT_PILOT_STATE_write(u32 Data) {
43 	reg_write(OPENOFDM_TX_REG_INIT_PILOT_STATE_ADDR, Data);
44 }
45 
46 static inline void OPENOFDM_TX_REG_INIT_DATA_STATE_write(u32 Data) {
47 	reg_write(OPENOFDM_TX_REG_INIT_DATA_STATE_ADDR, Data);
48 }
49 
50 static const struct of_device_id dev_of_ids[] = {
51 	{ .compatible = "sdr,openofdm_tx", },
52 	{}
53 };
54 MODULE_DEVICE_TABLE(of, dev_of_ids);
55 
56 static struct openofdm_tx_driver_api openofdm_tx_driver_api_inst;
57 static struct openofdm_tx_driver_api *openofdm_tx_api = &openofdm_tx_driver_api_inst;
58 EXPORT_SYMBOL(openofdm_tx_api);
59 
60 static inline u32 hw_init(enum openofdm_tx_mode mode){
61 	int err=0, i;
62 
63 	printk("%s hw_init mode %d\n", openofdm_tx_compatible_str, mode);
64 
65 	switch(mode)
66 	{
67 		case OPENOFDM_TX_TEST:
68 			printk("%s hw_init mode OPENOFDM_TX_TEST\n", openofdm_tx_compatible_str);
69 			break;
70 
71 		case OPENOFDM_TX_NORMAL:
72 			printk("%s hw_init mode OPENOFDM_TX_NORMAL\n", openofdm_tx_compatible_str);
73 			break;
74 
75 		default:
76 			printk("%s hw_init mode %d is wrong!\n", openofdm_tx_compatible_str, mode);
77 			err=1;
78 	}
79 
80 	//rst
81 	for (i=0;i<8;i++)
82 		openofdm_tx_api->OPENOFDM_TX_REG_MULTI_RST_write(0);
83 	for (i=0;i<32;i++)
84 		openofdm_tx_api->OPENOFDM_TX_REG_MULTI_RST_write(0xFFFFFFFF);
85 	for (i=0;i<8;i++)
86 		openofdm_tx_api->OPENOFDM_TX_REG_MULTI_RST_write(0);
87 
88 	openofdm_tx_api->OPENOFDM_TX_REG_INIT_PILOT_STATE_write(0x7F);
89 	openofdm_tx_api->OPENOFDM_TX_REG_INIT_DATA_STATE_write(0x7F);
90 
91 	printk("%s hw_init err %d\n", openofdm_tx_compatible_str, err);
92 	return(err);
93 }
94 
95 static int dev_probe(struct platform_device *pdev)
96 {
97 	struct device_node *np = pdev->dev.of_node;
98 	struct resource *io;
99 	int err=1;
100 
101 	printk("\n");
102 
103 	if (np) {
104 		const struct of_device_id *match;
105 
106 		match = of_match_node(dev_of_ids, np);
107 		if (match) {
108 			printk("%s dev_probe match!\n", openofdm_tx_compatible_str);
109 			err = 0;
110 		}
111 	}
112 
113 	if (err)
114 		return err;
115 
116 	openofdm_tx_api->hw_init=hw_init;
117 
118 	openofdm_tx_api->reg_read=reg_read;
119 	openofdm_tx_api->reg_write=reg_write;
120 
121 	openofdm_tx_api->OPENOFDM_TX_REG_MULTI_RST_write=OPENOFDM_TX_REG_MULTI_RST_write;
122 	openofdm_tx_api->OPENOFDM_TX_REG_INIT_PILOT_STATE_write=OPENOFDM_TX_REG_INIT_PILOT_STATE_write;
123 	openofdm_tx_api->OPENOFDM_TX_REG_INIT_DATA_STATE_write=OPENOFDM_TX_REG_INIT_DATA_STATE_write;
124 
125 	/* Request and map I/O memory */
126 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 	base_addr = devm_ioremap_resource(&pdev->dev, io);
128 	if (IS_ERR(base_addr))
129 		return PTR_ERR(base_addr);
130 
131 	printk("%s dev_probe io start 0x%08x end 0x%08x name %s flags 0x%08x desc 0x%08x\n", openofdm_tx_compatible_str,io->start,io->end,io->name,(u32)io->flags,(u32)io->desc);
132 	printk("%s dev_probe base_addr 0x%08x\n", openofdm_tx_compatible_str,(u32)base_addr);
133 	printk("%s dev_probe openofdm_tx_driver_api_inst 0x%08x\n", openofdm_tx_compatible_str, (u32)&openofdm_tx_driver_api_inst);
134 	printk("%s dev_probe             openofdm_tx_api 0x%08x\n", openofdm_tx_compatible_str, (u32)openofdm_tx_api);
135 
136 	printk("%s dev_probe succeed!\n", openofdm_tx_compatible_str);
137 
138 	err = hw_init(OPENOFDM_TX_NORMAL);
139 
140 	return err;
141 }
142 
143 static int dev_remove(struct platform_device *pdev)
144 {
145 	printk("\n");
146 
147 	printk("%s dev_remove base_addr 0x%08x\n", openofdm_tx_compatible_str,(u32)base_addr);
148 	printk("%s dev_remove openofdm_tx_driver_api_inst 0x%08x\n", openofdm_tx_compatible_str, (u32)&openofdm_tx_driver_api_inst);
149 	printk("%s dev_remove             openofdm_tx_api 0x%08x\n", openofdm_tx_compatible_str, (u32)openofdm_tx_api);
150 
151 	printk("%s dev_remove succeed!\n", openofdm_tx_compatible_str);
152 	return 0;
153 }
154 
155 static struct platform_driver dev_driver = {
156 	.driver = {
157 		.name = "sdr,openofdm_tx",
158 		.owner = THIS_MODULE,
159 		.of_match_table = dev_of_ids,
160 	},
161 	.probe = dev_probe,
162 	.remove = dev_remove,
163 };
164 
165 module_platform_driver(dev_driver);
166 
167 MODULE_AUTHOR("Xianjun Jiao");
168 MODULE_DESCRIPTION("sdr,openofdm_tx");
169 MODULE_LICENSE("GPL v2");
170