-
Notifications
You must be signed in to change notification settings - Fork 11
/
platform.c
272 lines (223 loc) · 6.35 KB
/
platform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/ktime.h>
#include <linux/device.h>
#include <linux/hrtimer.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/spi/spi.h>
#include <linux/time.h>
#include "cc2520.h"
#include "radio.h"
#include "platform.h"
#include "debug.h"
//////////////////////////
// SPI Stuff
//////////////////////////
static int cc2520_spi_add_to_bus(void)
{
struct spi_master *spi_master;
struct spi_device *spi_device;
struct device *pdev;
char buff[64];
int status = 0;
spi_master = spi_busnum_to_master(SPI_BUS);
if (!spi_master) {
ERR((KERN_ALERT "[cc2520] - spi_busnum_to_master(%d) returned NULL\n",
SPI_BUS));
ERR((KERN_ALERT "[cc2520] - Missing modprobe spi-bcm2708?\n"));
return -1;
}
spi_device = spi_alloc_device(spi_master);
if (!spi_device) {
put_device(&spi_master->dev);
ERR((KERN_ALERT "[cc2520] - spi_alloc_device() failed\n"));
return -1;
}
spi_device->chip_select = SPI_BUS_CS0;
/* Check whether this SPI bus.cs is already claimed */
snprintf(buff, sizeof(buff), "%s.%u",
dev_name(&spi_device->master->dev),
spi_device->chip_select);
pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
if (pdev) {
if (pdev->driver != NULL) {
ERR((KERN_INFO
"[cc2520] - Driver [%s] already registered for %s. \
Nuking from orbit.\n",
pdev->driver->name, buff));
}
else {
ERR((KERN_INFO
"[cc2520] - Previous driver registered with no loaded module. \
Nuking from orbit.\n"));
}
device_unregister(pdev);
}
spi_device->max_speed_hz = SPI_BUS_SPEED;
spi_device->mode = SPI_MODE_0;
spi_device->bits_per_word = 8;
spi_device->irq = -1;
spi_device->controller_state = NULL;
spi_device->controller_data = NULL;
strlcpy(spi_device->modalias, cc2520_name, SPI_NAME_SIZE);
status = spi_add_device(spi_device);
if (status < 0) {
spi_dev_put(spi_device);
ERR((KERN_ALERT "[cc2520] - spi_add_device() failed: %d\n",
status));
}
put_device(&spi_master->dev);
return status;
}
static int cc2520_spi_probe(struct spi_device *spi_device)
{
ERR((KERN_INFO "[cc2520] - Inserting SPI protocol driver.\n"));
state.spi_device = spi_device;
return 0;
}
static int cc2520_spi_remove(struct spi_device *spi_device)
{
ERR((KERN_INFO "[cc2520] - Removing SPI protocol driver."));
state.spi_device = NULL;
return 0;
}
static struct spi_driver cc2520_spi_driver = {
.driver = {
.name = cc2520_name,
.owner = THIS_MODULE,
},
.probe = cc2520_spi_probe,
.remove = cc2520_spi_remove,
};
int cc2520_plat_spi_init()
{
int result;
result = cc2520_spi_add_to_bus();
if (result < 0)
goto error;
result = spi_register_driver(&cc2520_spi_driver);
if (result < 0)
goto error;
return 0;
error:
spi_unregister_driver(&cc2520_spi_driver);
return result;
}
void cc2520_plat_spi_free()
{
if (state.spi_device)
spi_unregister_device(state.spi_device);
spi_unregister_driver(&cc2520_spi_driver);
}
//////////////////////////
// Interrupt Handles
/////////////////////////
static irqreturn_t cc2520_sfd_handler(int irq, void *dev_id)
{
int gpio_val;
struct timespec ts;
s64 nanos;
// NOTE: For now we're assuming no delay between SFD called
// and actual SFD received. The TinyOS implementations call
// for a few uS of delay, but it's likely not needed.
getrawmonotonic(&ts);
nanos = timespec_to_ns(&ts);
gpio_val = gpio_get_value(CC2520_SFD);
//DBG((KERN_INFO "[cc2520] - sfd interrupt occurred at %lld, %d\n", (long long int)nanos, gpio_val));
cc2520_radio_sfd_occurred(nanos, gpio_val);
return IRQ_HANDLED;
}
static irqreturn_t cc2520_fifop_handler(int irq, void *dev_id)
{
if (gpio_get_value(CC2520_FIFOP) == 1) {
DBG((KERN_INFO "[cc2520] - fifop interrupt occurred\n"));
cc2520_radio_fifop_occurred();
}
return IRQ_HANDLED;
}
//////////////////////////////
// Interface Initialization
//////////////////////////////
// Sets up the GPIO pins needed for the CC2520
// and initializes any interrupt handlers needed.
int cc2520_plat_gpio_init()
{
int err = 0;
int irq = 0;
// Setup GPIO In/Out
err = gpio_request_one(CC2520_FIFO, GPIOF_DIR_IN, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_FIFOP, GPIOF_DIR_IN, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_CCA, GPIOF_DIR_IN, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_SFD, GPIOF_DIR_IN, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_RESET, GPIOF_DIR_OUT, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_DEBUG_0, GPIOF_DIR_OUT, NULL);
if (err)
goto fail;
err = gpio_request_one(CC2520_DEBUG_1, GPIOF_DIR_OUT, NULL);
if (err)
goto fail;
gpio_set_value(CC2520_DEBUG_0, 0);
// Setup FIFOP Interrupt
irq = gpio_to_irq(CC2520_FIFOP);
if (irq < 0) {
err = irq;
goto fail;
}
err = request_irq(
irq,
cc2520_fifop_handler,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"fifopHandler",
NULL
);
if (err)
goto fail;
state.gpios.fifop_irq = irq;
// Setup SFD Interrupt
irq = gpio_to_irq(CC2520_SFD);
if (irq < 0) {
err = irq;
goto fail;
}
err = request_irq(
irq,
cc2520_sfd_handler,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"sfdHandler",
NULL
);
if (err)
goto fail;
state.gpios.sfd_irq = irq;
return err;
fail:
ERR((KERN_ALERT "[cc2520] - failed to init GPIOs\n"));
cc2520_plat_gpio_free();
return err;
}
void cc2520_plat_gpio_free()
{
gpio_free(CC2520_CLOCK);
gpio_free(CC2520_FIFO);
gpio_free(CC2520_FIFOP);
gpio_free(CC2520_CCA);
gpio_free(CC2520_SFD);
gpio_free(CC2520_RESET);
gpio_free(CC2520_DEBUG_0);
gpio_free(CC2520_DEBUG_1);
free_irq(state.gpios.fifop_irq, NULL);
free_irq(state.gpios.sfd_irq, NULL);
}