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