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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* Copyright (C) 2011 Samsung Electronics.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#ifdef CONFIG_OF
#include <linux/of_gpio.h>
#endif
#include <linux/wakelock.h>
#include <linux/delay.h>
#include <linux/spi/spi.h>
#include "modem_prj.h"
#define SPI_XMIT_DELEY 100
int check_cp_status(unsigned int gpio_cp_status, unsigned int count)
{
int ret = 0;
int cnt = 0;
int val;
while (1) {
val = gpio_get_value(gpio_cp_status);
mif_err("CP2AP_WAKEUP == %d (cnt %d)\n", val, cnt);
if (val != 0) {
ret = 0;
break;
}
cnt++;
if (cnt >= count) {
mif_err("ERR! CP2AP_WAKEUP == 0 (cnt %d)\n", cnt);
ret = -EFAULT;
break;
}
msleep(20);
}
if (ret == 0)
mif_info("CP2AP_WAKEUP == 1 cnt: %d\n", cnt);
else
mif_info("ERR: Checking count after sending bootloader: %d\n", cnt);
if (cnt == 0)
msleep(10);
return ret;
}
int check_cp_status_low(unsigned int gpio_cp_status, unsigned int count)
{
int ret = 0;
int cnt = 0;
mif_info("Checking CP2AP_WAKEUP LOW before sending bootloader\n");
while (1) {
if (gpio_get_value(gpio_cp_status) == 0) {
ret = 0;
break;
}
cnt++;
if (cnt >= count) {
mif_err("ERR! CP2AP_WAKEUP != 0 (cnt %d)\n", cnt);
ret = -EFAULT;
break;
}
msleep(20);
}
mif_info("CP2AP_WAKEUP == 0 : Checking count before sending bootloader: %d\n", cnt);
if (cnt == 0)
msleep(10);
return ret;
}
static inline int spi_send(struct modem_boot_spi *loader, const char *buff,
unsigned len)
{
int ret;
struct spi_message msg;
struct spi_transfer xfer = {
.len = len,
.tx_buf = buff,
};
printk("Send CP bootloader with SPI : 0x%x bytes\n", len);
spi_message_init(&msg);
spi_message_add_tail(&xfer, &msg);
ret = spi_sync(loader->spi_dev, &msg);
if (ret < 0)
mif_err("ERR! spi_sync fail (err %d)\n", ret);
return ret;
}
static int spi_boot_write(struct modem_boot_spi *loader, const char *addr,
const long len)
{
int ret = 0;
char *buff = NULL;
mif_err("+++\n");
buff = kzalloc(len, GFP_KERNEL);
if (!buff) {
mif_err("ERR! kzalloc(%ld) fail\n", len);
ret = -ENOMEM;
goto exit;
}
ret = copy_from_user(buff, (const void __user *)addr, len);
if (ret) {
mif_err("ERR! copy_from_user fail (err %d)\n", ret);
ret = -EFAULT;
goto exit;
}
printk("Send bootloader by SPI (Len:%ld)\n", len);
ret = spi_send(loader, buff, len);
if (ret < 0) {
mif_err("ERR! spi_send fail (err %d)\n", ret);
goto exit;
}
exit:
if (buff)
kfree(buff);
mif_err("---\n");
return ret;
}
static int spi_boot_open(struct inode *inode, struct file *filp)
{
struct modem_boot_spi *loader = to_modem_boot_spi(filp->private_data);
filp->private_data = loader;
return 0;
}
static long spi_boot_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
int ret = 0;
struct modem_firmware img;
struct modem_boot_spi *loader = filp->private_data;
mutex_lock(&loader->lock);
switch (cmd) {
case IOCTL_MODEM_XMIT_BOOT:
ret = copy_from_user(&img, (const void __user *)arg,
sizeof(struct modem_firmware));
if (ret) {
mif_err("ERR! copy_from_user fail (err %d)\n", ret);
ret = -EFAULT;
goto exit_err;
}
mif_info("IOCTL_MODEM_XMIT_BOOT (size %d)\n", img.size);
if (img.size == 0) {
mif_err("ERR! img.size is 0\n");
break;
}
if (!loader->gpio_cp_status) {
mif_err("ERR! gpio_cp_status is null\n");
break;
}
mif_info("Ignore CP2AP_WAKEUP Low\n");
msleep(30);
ret = spi_boot_write(loader, (char *)img.binary, img.size);
if (ret < 0) {
mif_err("ERR! spi_boot_write fail (err %d)\n", ret);
break;
}
ret = check_cp_status(loader->gpio_cp_status, 200);
if (ret < 0)
mif_err("ERR! check_cp_status fail (err %d)\n", ret);
break;
default:
mif_err("ioctl cmd error\n");
ret = -ENOIOCTLCMD;
break;
}
exit_err:
mutex_unlock(&loader->lock);
return ret;
}
static const struct file_operations modem_spi_boot_fops = {
.owner = THIS_MODULE,
.open = spi_boot_open,
.unlocked_ioctl = spi_boot_ioctl,
};
static int __init modem_spi_boot_probe(struct spi_device *spi)
{
int ret;
struct device *dev = &spi->dev;
struct modem_boot_spi *loader;
mif_err("+++ SPI boot +++\n");
loader = kzalloc(sizeof(*loader), GFP_KERNEL);
if (!loader) {
mif_err("failed to allocate for modem_boot_spi\n");
ret = -ENOMEM;
goto err_alloc;
}
mutex_init(&loader->lock);
spi->bits_per_word = 8;
if (spi_setup(spi)) {
mif_err("ERR! spi_setup fail\n");
ret = -EINVAL;
goto err_setup;
}
loader->spi_dev = spi;
if (dev->of_node) {
struct device_node *np;
unsigned gpio_cp_status;
np = of_find_compatible_node(NULL, NULL,
"sec_modem,modem_pdata");
if (!np) {
mif_err("DT, failed to get node\n");
ret = -EINVAL;
goto err_setup;
}
gpio_cp_status = of_get_named_gpio(np, "gpio_cp2ap_wake_up", 0);
if (!gpio_is_valid(gpio_cp_status)) {
mif_err("gpio_cp_status: Invalied gpio pins\n");
ret = -EINVAL;
goto err_setup;
}
gpio_request(gpio_cp_status, "gpio_cp_status");
mif_err("gpio_cp_status: %d\n", gpio_cp_status);
loader->gpio_cp_status = gpio_cp_status;
} else {
struct modem_boot_spi_platform_data *pdata;
pdata = dev->platform_data;
if (!pdata) {
mif_err("Non-DT, incorrect pdata!\n");
ret = -EINVAL;
goto err_setup;
}
loader->gpio_cp_status = pdata->gpio_cp_status;
}
spi_set_drvdata(spi, loader);
loader->misc_dev.minor = MISC_DYNAMIC_MINOR;
loader->misc_dev.name = "modem_boot_spi";
loader->misc_dev.fops = &modem_spi_boot_fops;
ret = misc_register(&loader->misc_dev);
if (ret) {
mif_err("ERR! misc_register fail (err %d)\n", ret);
goto err_setup;
}
#ifdef AIRPLANE_MODE_TEST
lte_airplane_mode = 0;
#endif
mif_err("---\n");
return 0;
err_setup:
mutex_destroy(&loader->lock);
kfree(loader);
err_alloc:
mif_err("xxx\n");
return ret;
}
static int modem_spi_boot_remove(struct spi_device *spi)
{
struct modem_boot_spi *loader = spi_get_drvdata(spi);
misc_deregister(&loader->misc_dev);
mutex_destroy(&loader->lock);
kfree(loader);
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id modem_boot_spi_dt_match[] = {
{ .compatible = "modem_boot_spi" },
{},
};
MODULE_DEVICE_TABLE(of, modem_boot_spi_dt_match);
#endif
static struct spi_driver modem_spi_boot_driver = {
.driver = {
.name = "modem_boot_spi",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(modem_boot_spi_dt_match),
},
.probe = modem_spi_boot_probe,
.remove =modem_spi_boot_remove,
};
static int __init modem_spi_boot_init(void)
{
int err;
mif_err("+++\n");
err = spi_register_driver(&modem_spi_boot_driver);
if (err) {
mif_err("spi_register_driver fail (err %d)\n", err);
mif_err("xxx\n");
return err;
}
mif_err("---\n");
return 0;
}
static void __exit modem_spi_boot_exit(void)
{
spi_unregister_driver(&modem_spi_boot_driver);
}
module_init(modem_spi_boot_init);
module_exit(modem_spi_boot_exit);
MODULE_DESCRIPTION("SPI Driver for Downloading Modem Bootloader");
MODULE_LICENSE("GPL");
|