blob: 9fa1a4ab721b5fa1775c6568e2f54e1c96c716e5 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/io.h>
19#include <linux/mutex.h>
20#include <linux/miscdevice.h>
21#include <linux/fs.h>
22#include <linux/gpio.h>
23#include <linux/kernel.h>
24#include <linux/irq.h>
25#include <linux/ioctl.h>
26#include <linux/delay.h>
27#include <linux/reboot.h>
28#include <linux/debugfs.h>
29#include <linux/completion.h>
30#include <linux/workqueue.h>
31#include <linux/clk.h>
Willie Ruan20667832011-08-03 13:36:31 -070032#include <linux/mfd/pmic8058.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <asm/mach-types.h>
34#include <asm/uaccess.h>
35#include <mach/mdm.h>
36#include <mach/restart.h>
37#include <mach/subsystem_notif.h>
38#include <mach/subsystem_restart.h>
39#include <linux/msm_charm.h>
40#include "msm_watchdog.h"
41#include "devices.h"
42#include "clock.h"
43
44#define CHARM_MODEM_TIMEOUT 6000
45#define CHARM_HOLD_TIME 4000
46#define CHARM_MODEM_DELTA 100
47
48static void (*power_on_charm)(void);
49static void (*power_down_charm)(void);
50
51static int charm_debug_on;
52static int charm_status_irq;
53static int charm_errfatal_irq;
54static int charm_ready;
55static enum charm_boot_type boot_type = CHARM_NORMAL_BOOT;
56static int charm_boot_status;
57static int charm_ram_dump_status;
58static struct workqueue_struct *charm_queue;
59
60#define CHARM_DBG(...) do { if (charm_debug_on) \
61 pr_info(__VA_ARGS__); \
62 } while (0);
63
64
65DECLARE_COMPLETION(charm_needs_reload);
66DECLARE_COMPLETION(charm_boot);
67DECLARE_COMPLETION(charm_ram_dumps);
68
69static void charm_disable_irqs(void)
70{
71 disable_irq_nosync(charm_errfatal_irq);
72 disable_irq_nosync(charm_status_irq);
73
74}
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076static int charm_subsys_shutdown(const struct subsys_data *crashed_subsys)
77{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 charm_ready = 0;
Laura Abbottd161daf2011-08-10 17:24:48 -070079 power_down_charm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080 return 0;
81}
82
83static int charm_subsys_powerup(const struct subsys_data *crashed_subsys)
84{
85 power_on_charm();
86 boot_type = CHARM_NORMAL_BOOT;
87 complete(&charm_needs_reload);
88 wait_for_completion(&charm_boot);
89 pr_info("%s: charm modem has been restarted\n", __func__);
90 INIT_COMPLETION(charm_boot);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 return charm_boot_status;
92}
93
94static int charm_subsys_ramdumps(int want_dumps,
95 const struct subsys_data *crashed_subsys)
96{
97 charm_ram_dump_status = 0;
98 if (want_dumps) {
99 boot_type = CHARM_RAM_DUMPS;
100 complete(&charm_needs_reload);
101 wait_for_completion(&charm_ram_dumps);
102 INIT_COMPLETION(charm_ram_dumps);
103 power_down_charm();
104 }
105 return charm_ram_dump_status;
106}
107
108static struct subsys_data charm_subsystem = {
109 .shutdown = charm_subsys_shutdown,
110 .ramdump = charm_subsys_ramdumps,
111 .powerup = charm_subsys_powerup,
112 .name = "external_modem",
113};
114
115static int charm_panic_prep(struct notifier_block *this,
116 unsigned long event, void *ptr)
117{
Laura Abbott63215122011-08-25 14:43:01 -0700118 int i;
119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 CHARM_DBG("%s: setting AP2MDM_ERRFATAL high for a non graceful reset\n",
121 __func__);
122 charm_disable_irqs();
123 gpio_set_value(AP2MDM_ERRFATAL, 1);
Laura Abbotte31d1332011-08-24 17:12:07 -0700124 gpio_set_value(AP2MDM_WAKEUP, 1);
Laura Abbott63215122011-08-25 14:43:01 -0700125 for (i = CHARM_MODEM_TIMEOUT; i > 0; i -= CHARM_MODEM_DELTA) {
126 pet_watchdog();
127 mdelay(CHARM_MODEM_DELTA);
128 if (gpio_get_value(MDM2AP_STATUS) == 0)
129 break;
130 }
131 if (i <= 0)
132 pr_err("%s: MDM2AP_STATUS never went low\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 return NOTIFY_DONE;
134}
135
136static struct notifier_block charm_panic_blk = {
137 .notifier_call = charm_panic_prep,
138};
139
Laura Abbottd42d1202011-07-20 14:49:57 -0700140static int first_boot = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141
142static long charm_modem_ioctl(struct file *filp, unsigned int cmd,
143 unsigned long arg)
144{
145
146 int status, ret = 0;
147
148 if (_IOC_TYPE(cmd) != CHARM_CODE) {
149 pr_err("%s: invalid ioctl code\n", __func__);
150 return -EINVAL;
151 }
152
153 CHARM_DBG("%s: Entering ioctl cmd = %d\n", __func__, _IOC_NR(cmd));
154 switch (cmd) {
155 case WAKE_CHARM:
156 CHARM_DBG("%s: Powering on\n", __func__);
157 power_on_charm();
158 break;
159 case CHECK_FOR_BOOT:
160 if (gpio_get_value(MDM2AP_STATUS) == 0)
161 put_user(1, (unsigned long __user *) arg);
162 else
163 put_user(0, (unsigned long __user *) arg);
164 break;
165 case NORMAL_BOOT_DONE:
166 CHARM_DBG("%s: check if charm is booted up\n", __func__);
167 get_user(status, (unsigned long __user *) arg);
168 if (status)
169 charm_boot_status = -EIO;
170 else
171 charm_boot_status = 0;
Laura Abbotta267ea92011-07-15 19:53:05 -0700172 charm_ready = 1;
Laura Abbottd42d1202011-07-20 14:49:57 -0700173
Laura Abbott943af572011-08-24 17:28:45 -0700174 gpio_set_value(AP2MDM_KPDPWR_N, 0);
Laura Abbottd42d1202011-07-20 14:49:57 -0700175 if (!first_boot)
176 complete(&charm_boot);
177 else
178 first_boot = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 break;
180 case RAM_DUMP_DONE:
181 CHARM_DBG("%s: charm done collecting RAM dumps\n", __func__);
182 get_user(status, (unsigned long __user *) arg);
183 if (status)
184 charm_ram_dump_status = -EIO;
185 else
186 charm_ram_dump_status = 0;
187 complete(&charm_ram_dumps);
188 break;
189 case WAIT_FOR_RESTART:
190 CHARM_DBG("%s: wait for charm to need images reloaded\n",
191 __func__);
192 ret = wait_for_completion_interruptible(&charm_needs_reload);
193 if (!ret)
194 put_user(boot_type, (unsigned long __user *) arg);
195 INIT_COMPLETION(charm_needs_reload);
196 break;
197 default:
198 pr_err("%s: invalid ioctl cmd = %d\n", __func__, _IOC_NR(cmd));
199 ret = -EINVAL;
200 break;
201 }
202
203 return ret;
204}
205
206static int charm_modem_open(struct inode *inode, struct file *file)
207{
208 return 0;
209}
210
211static const struct file_operations charm_modem_fops = {
212 .owner = THIS_MODULE,
213 .open = charm_modem_open,
214 .unlocked_ioctl = charm_modem_ioctl,
215};
216
217
218struct miscdevice charm_modem_misc = {
219 .minor = MISC_DYNAMIC_MINOR,
220 .name = "mdm",
221 .fops = &charm_modem_fops
222};
223
224
225
226static void charm_status_fn(struct work_struct *work)
227{
228 pr_info("Reseting the charm because status changed\n");
229 subsystem_restart("external_modem");
230}
231
232static DECLARE_WORK(charm_status_work, charm_status_fn);
233
234static void charm_fatal_fn(struct work_struct *work)
235{
236 pr_info("Reseting the charm due to an errfatal\n");
Willie Ruan20667832011-08-03 13:36:31 -0700237 if (get_restart_level() == RESET_SOC)
238 pm8058_stay_on();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 subsystem_restart("external_modem");
240}
241
242static DECLARE_WORK(charm_fatal_work, charm_fatal_fn);
243
244static irqreturn_t charm_errfatal(int irq, void *dev_id)
245{
246 CHARM_DBG("%s: charm got errfatal interrupt\n", __func__);
Laura Abbottd161daf2011-08-10 17:24:48 -0700247 if (charm_ready && (gpio_get_value(MDM2AP_STATUS) == 1)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 CHARM_DBG("%s: scheduling work now\n", __func__);
249 queue_work(charm_queue, &charm_fatal_work);
250 }
251 return IRQ_HANDLED;
252}
253
254static irqreturn_t charm_status_change(int irq, void *dev_id)
255{
256 CHARM_DBG("%s: charm sent status change interrupt\n", __func__);
257 if ((gpio_get_value(MDM2AP_STATUS) == 0) && charm_ready) {
258 CHARM_DBG("%s: scheduling work now\n", __func__);
259 queue_work(charm_queue, &charm_status_work);
260 } else if (gpio_get_value(MDM2AP_STATUS) == 1) {
261 CHARM_DBG("%s: charm is now ready\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 }
263 return IRQ_HANDLED;
264}
265
266static int charm_debug_on_set(void *data, u64 val)
267{
268 charm_debug_on = val;
269 return 0;
270}
271
272static int charm_debug_on_get(void *data, u64 *val)
273{
274 *val = charm_debug_on;
275 return 0;
276}
277
278DEFINE_SIMPLE_ATTRIBUTE(charm_debug_on_fops,
279 charm_debug_on_get,
280 charm_debug_on_set, "%llu\n");
281
282static int charm_debugfs_init(void)
283{
284 struct dentry *dent;
285
286 dent = debugfs_create_dir("charm_dbg", 0);
287 if (IS_ERR(dent))
288 return PTR_ERR(dent);
289
290 debugfs_create_file("debug_on", 0644, dent, NULL,
291 &charm_debug_on_fops);
292 return 0;
293}
294
295static int gsbi9_uart_notifier_cb(struct notifier_block *this,
296 unsigned long code, void *_cmd)
297{
298 switch (code) {
299 case SUBSYS_AFTER_SHUTDOWN:
300 platform_device_unregister(msm_device_uart_gsbi9);
301 msm_device_uart_gsbi9 = msm_add_gsbi9_uart();
302 if (IS_ERR(msm_device_uart_gsbi9))
303 pr_err("%s(): Failed to create uart gsbi9 device\n",
304 __func__);
305 default:
306 break;
307 }
308 return NOTIFY_DONE;
309}
310
311static struct notifier_block gsbi9_nb = {
312 .notifier_call = gsbi9_uart_notifier_cb,
313};
314
315static int __init charm_modem_probe(struct platform_device *pdev)
316{
317 int ret, irq;
318 struct charm_platform_data *d = pdev->dev.platform_data;
319
320 gpio_request(AP2MDM_STATUS, "AP2MDM_STATUS");
321 gpio_request(AP2MDM_ERRFATAL, "AP2MDM_ERRFATAL");
322 gpio_request(AP2MDM_KPDPWR_N, "AP2MDM_KPDPWR_N");
323 gpio_request(AP2MDM_PMIC_RESET_N, "AP2MDM_PMIC_RESET_N");
324 gpio_request(MDM2AP_STATUS, "MDM2AP_STATUS");
325 gpio_request(MDM2AP_ERRFATAL, "MDM2AP_ERRFATAL");
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700326 gpio_request(AP2MDM_WAKEUP, "AP2MDM_WAKEUP");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
328 gpio_direction_output(AP2MDM_STATUS, 1);
329 gpio_direction_output(AP2MDM_ERRFATAL, 0);
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700330 gpio_direction_output(AP2MDM_WAKEUP, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 gpio_direction_input(MDM2AP_STATUS);
332 gpio_direction_input(MDM2AP_ERRFATAL);
333
334 power_on_charm = d->charm_modem_on;
335 power_down_charm = d->charm_modem_off;
336
337 charm_queue = create_singlethread_workqueue("charm_queue");
338 if (!charm_queue) {
339 pr_err("%s: could not create workqueue. All charm \
340 functionality will be disabled\n",
341 __func__);
342 ret = -ENOMEM;
343 goto fatal_err;
344 }
345
346 atomic_notifier_chain_register(&panic_notifier_list, &charm_panic_blk);
347 charm_debugfs_init();
348
349 ssr_register_subsystem(&charm_subsystem);
350
351 irq = platform_get_irq(pdev, 0);
352 if (irq < 0) {
353 pr_err("%s: could not get MDM2AP_ERRFATAL IRQ resource. \
354 error=%d No IRQ will be generated on errfatal.",
355 __func__, irq);
356 goto errfatal_err;
357 }
358
359 ret = request_irq(irq, charm_errfatal,
360 IRQF_TRIGGER_RISING , "charm errfatal", NULL);
361
362 if (ret < 0) {
363 pr_err("%s: MDM2AP_ERRFATAL IRQ#%d request failed with error=%d\
364 . No IRQ will be generated on errfatal.",
365 __func__, irq, ret);
366 goto errfatal_err;
367 }
368 charm_errfatal_irq = irq;
369
370errfatal_err:
371
372 irq = platform_get_irq(pdev, 1);
373 if (irq < 0) {
374 pr_err("%s: could not get MDM2AP_STATUS IRQ resource. \
375 error=%d No IRQ will be generated on status change.",
376 __func__, irq);
377 goto status_err;
378 }
379
380 ret = request_threaded_irq(irq, NULL, charm_status_change,
381 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
382 "charm status", NULL);
383
384 if (ret < 0) {
385 pr_err("%s: MDM2AP_STATUS IRQ#%d request failed with error=%d\
386 . No IRQ will be generated on status change.",
387 __func__, irq, ret);
388 goto status_err;
389 }
390 charm_status_irq = irq;
391
392status_err:
393 subsys_notif_register_notifier("external_modem", &gsbi9_nb);
394
395 pr_info("%s: Registering charm modem\n", __func__);
396
397 return misc_register(&charm_modem_misc);
398
399fatal_err:
400 gpio_free(AP2MDM_STATUS);
401 gpio_free(AP2MDM_ERRFATAL);
402 gpio_free(AP2MDM_KPDPWR_N);
403 gpio_free(AP2MDM_PMIC_RESET_N);
404 gpio_free(MDM2AP_STATUS);
405 gpio_free(MDM2AP_ERRFATAL);
406 return ret;
407
408}
409
410
411static int __devexit charm_modem_remove(struct platform_device *pdev)
412{
413 gpio_free(AP2MDM_STATUS);
414 gpio_free(AP2MDM_ERRFATAL);
415 gpio_free(AP2MDM_KPDPWR_N);
416 gpio_free(AP2MDM_PMIC_RESET_N);
417 gpio_free(MDM2AP_STATUS);
418 gpio_free(MDM2AP_ERRFATAL);
419
420 return misc_deregister(&charm_modem_misc);
421}
422
423static void charm_modem_shutdown(struct platform_device *pdev)
424{
425 int i;
426
427 CHARM_DBG("%s: setting AP2MDM_STATUS low for a graceful restart\n",
428 __func__);
429
430 charm_disable_irqs();
431
432 gpio_set_value(AP2MDM_STATUS, 0);
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700433 gpio_set_value(AP2MDM_WAKEUP, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434
435 for (i = CHARM_MODEM_TIMEOUT; i > 0; i -= CHARM_MODEM_DELTA) {
436 pet_watchdog();
437 msleep(CHARM_MODEM_DELTA);
438 if (gpio_get_value(MDM2AP_STATUS) == 0)
439 break;
440 }
441
442 if (i <= 0) {
443 pr_err("%s: MDM2AP_STATUS never went low.\n",
444 __func__);
445 gpio_direction_output(AP2MDM_PMIC_RESET_N, 1);
446 for (i = CHARM_HOLD_TIME; i > 0; i -= CHARM_MODEM_DELTA) {
447 pet_watchdog();
448 msleep(CHARM_MODEM_DELTA);
449 }
450 gpio_direction_output(AP2MDM_PMIC_RESET_N, 0);
451 }
Laura Abbottd2a3bb52011-07-21 12:39:22 -0700452 gpio_set_value(AP2MDM_WAKEUP, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453}
454
455static struct platform_driver charm_modem_driver = {
456 .remove = charm_modem_remove,
457 .shutdown = charm_modem_shutdown,
458 .driver = {
459 .name = "charm_modem",
460 .owner = THIS_MODULE
461 },
462};
463
464static int __init charm_modem_init(void)
465{
466 return platform_driver_probe(&charm_modem_driver, charm_modem_probe);
467}
468
469static void __exit charm_modem_exit(void)
470{
471 platform_driver_unregister(&charm_modem_driver);
472}
473
474module_init(charm_modem_init);
475module_exit(charm_modem_exit);
476
477MODULE_LICENSE("GPL v2");
478MODULE_DESCRIPTION("msm8660 charm modem driver");
479MODULE_VERSION("1.0");
480MODULE_ALIAS("charm_modem");