Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* 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> |
| 32 | #include <asm/mach-types.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <mach/mdm.h> |
| 35 | #include <mach/restart.h> |
| 36 | #include <mach/subsystem_notif.h> |
| 37 | #include <mach/subsystem_restart.h> |
| 38 | #include <linux/msm_charm.h> |
| 39 | #include "msm_watchdog.h" |
| 40 | #include "devices.h" |
| 41 | #include "clock.h" |
| 42 | |
| 43 | #define CHARM_MODEM_TIMEOUT 6000 |
| 44 | #define CHARM_HOLD_TIME 4000 |
| 45 | #define CHARM_MODEM_DELTA 100 |
| 46 | |
| 47 | static void (*power_on_charm)(void); |
| 48 | static void (*power_down_charm)(void); |
| 49 | |
| 50 | static int charm_debug_on; |
| 51 | static int charm_status_irq; |
| 52 | static int charm_errfatal_irq; |
| 53 | static int charm_ready; |
| 54 | static enum charm_boot_type boot_type = CHARM_NORMAL_BOOT; |
| 55 | static int charm_boot_status; |
| 56 | static int charm_ram_dump_status; |
| 57 | static struct workqueue_struct *charm_queue; |
| 58 | |
| 59 | #define CHARM_DBG(...) do { if (charm_debug_on) \ |
| 60 | pr_info(__VA_ARGS__); \ |
| 61 | } while (0); |
| 62 | |
| 63 | |
| 64 | DECLARE_COMPLETION(charm_needs_reload); |
| 65 | DECLARE_COMPLETION(charm_boot); |
| 66 | DECLARE_COMPLETION(charm_ram_dumps); |
| 67 | |
| 68 | static void charm_disable_irqs(void) |
| 69 | { |
| 70 | disable_irq_nosync(charm_errfatal_irq); |
| 71 | disable_irq_nosync(charm_status_irq); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | static void charm_enable_irqs(void) |
| 76 | { |
| 77 | enable_irq(charm_errfatal_irq); |
| 78 | enable_irq(charm_status_irq); |
| 79 | } |
| 80 | |
| 81 | static int charm_subsys_shutdown(const struct subsys_data *crashed_subsys) |
| 82 | { |
| 83 | charm_disable_irqs(); |
| 84 | power_down_charm(); |
| 85 | charm_ready = 0; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int charm_subsys_powerup(const struct subsys_data *crashed_subsys) |
| 90 | { |
| 91 | power_on_charm(); |
| 92 | boot_type = CHARM_NORMAL_BOOT; |
| 93 | complete(&charm_needs_reload); |
| 94 | wait_for_completion(&charm_boot); |
| 95 | pr_info("%s: charm modem has been restarted\n", __func__); |
| 96 | INIT_COMPLETION(charm_boot); |
| 97 | charm_enable_irqs(); |
| 98 | return charm_boot_status; |
| 99 | } |
| 100 | |
| 101 | static int charm_subsys_ramdumps(int want_dumps, |
| 102 | const struct subsys_data *crashed_subsys) |
| 103 | { |
| 104 | charm_ram_dump_status = 0; |
| 105 | if (want_dumps) { |
| 106 | boot_type = CHARM_RAM_DUMPS; |
| 107 | complete(&charm_needs_reload); |
| 108 | wait_for_completion(&charm_ram_dumps); |
| 109 | INIT_COMPLETION(charm_ram_dumps); |
| 110 | power_down_charm(); |
| 111 | } |
| 112 | return charm_ram_dump_status; |
| 113 | } |
| 114 | |
| 115 | static struct subsys_data charm_subsystem = { |
| 116 | .shutdown = charm_subsys_shutdown, |
| 117 | .ramdump = charm_subsys_ramdumps, |
| 118 | .powerup = charm_subsys_powerup, |
| 119 | .name = "external_modem", |
| 120 | }; |
| 121 | |
| 122 | static int charm_panic_prep(struct notifier_block *this, |
| 123 | unsigned long event, void *ptr) |
| 124 | { |
| 125 | CHARM_DBG("%s: setting AP2MDM_ERRFATAL high for a non graceful reset\n", |
| 126 | __func__); |
| 127 | charm_disable_irqs(); |
| 128 | gpio_set_value(AP2MDM_ERRFATAL, 1); |
| 129 | return NOTIFY_DONE; |
| 130 | } |
| 131 | |
| 132 | static struct notifier_block charm_panic_blk = { |
| 133 | .notifier_call = charm_panic_prep, |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | static long charm_modem_ioctl(struct file *filp, unsigned int cmd, |
| 138 | unsigned long arg) |
| 139 | { |
| 140 | |
| 141 | int status, ret = 0; |
| 142 | |
| 143 | if (_IOC_TYPE(cmd) != CHARM_CODE) { |
| 144 | pr_err("%s: invalid ioctl code\n", __func__); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
| 148 | CHARM_DBG("%s: Entering ioctl cmd = %d\n", __func__, _IOC_NR(cmd)); |
| 149 | switch (cmd) { |
| 150 | case WAKE_CHARM: |
| 151 | CHARM_DBG("%s: Powering on\n", __func__); |
| 152 | power_on_charm(); |
| 153 | break; |
| 154 | case CHECK_FOR_BOOT: |
| 155 | if (gpio_get_value(MDM2AP_STATUS) == 0) |
| 156 | put_user(1, (unsigned long __user *) arg); |
| 157 | else |
| 158 | put_user(0, (unsigned long __user *) arg); |
| 159 | break; |
| 160 | case NORMAL_BOOT_DONE: |
| 161 | CHARM_DBG("%s: check if charm is booted up\n", __func__); |
| 162 | get_user(status, (unsigned long __user *) arg); |
| 163 | if (status) |
| 164 | charm_boot_status = -EIO; |
| 165 | else |
| 166 | charm_boot_status = 0; |
Laura Abbott | a267ea9 | 2011-07-15 19:53:05 -0700 | [diff] [blame^] | 167 | charm_ready = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 168 | complete(&charm_boot); |
| 169 | break; |
| 170 | case RAM_DUMP_DONE: |
| 171 | CHARM_DBG("%s: charm done collecting RAM dumps\n", __func__); |
| 172 | get_user(status, (unsigned long __user *) arg); |
| 173 | if (status) |
| 174 | charm_ram_dump_status = -EIO; |
| 175 | else |
| 176 | charm_ram_dump_status = 0; |
| 177 | complete(&charm_ram_dumps); |
| 178 | break; |
| 179 | case WAIT_FOR_RESTART: |
| 180 | CHARM_DBG("%s: wait for charm to need images reloaded\n", |
| 181 | __func__); |
| 182 | ret = wait_for_completion_interruptible(&charm_needs_reload); |
| 183 | if (!ret) |
| 184 | put_user(boot_type, (unsigned long __user *) arg); |
| 185 | INIT_COMPLETION(charm_needs_reload); |
| 186 | break; |
| 187 | default: |
| 188 | pr_err("%s: invalid ioctl cmd = %d\n", __func__, _IOC_NR(cmd)); |
| 189 | ret = -EINVAL; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int charm_modem_open(struct inode *inode, struct file *file) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static const struct file_operations charm_modem_fops = { |
| 202 | .owner = THIS_MODULE, |
| 203 | .open = charm_modem_open, |
| 204 | .unlocked_ioctl = charm_modem_ioctl, |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | struct miscdevice charm_modem_misc = { |
| 209 | .minor = MISC_DYNAMIC_MINOR, |
| 210 | .name = "mdm", |
| 211 | .fops = &charm_modem_fops |
| 212 | }; |
| 213 | |
| 214 | |
| 215 | |
| 216 | static void charm_status_fn(struct work_struct *work) |
| 217 | { |
| 218 | pr_info("Reseting the charm because status changed\n"); |
| 219 | subsystem_restart("external_modem"); |
| 220 | } |
| 221 | |
| 222 | static DECLARE_WORK(charm_status_work, charm_status_fn); |
| 223 | |
| 224 | static void charm_fatal_fn(struct work_struct *work) |
| 225 | { |
| 226 | pr_info("Reseting the charm due to an errfatal\n"); |
| 227 | subsystem_restart("external_modem"); |
| 228 | } |
| 229 | |
| 230 | static DECLARE_WORK(charm_fatal_work, charm_fatal_fn); |
| 231 | |
| 232 | static irqreturn_t charm_errfatal(int irq, void *dev_id) |
| 233 | { |
| 234 | CHARM_DBG("%s: charm got errfatal interrupt\n", __func__); |
| 235 | if (charm_ready) { |
| 236 | CHARM_DBG("%s: scheduling work now\n", __func__); |
| 237 | queue_work(charm_queue, &charm_fatal_work); |
| 238 | } |
| 239 | return IRQ_HANDLED; |
| 240 | } |
| 241 | |
| 242 | static irqreturn_t charm_status_change(int irq, void *dev_id) |
| 243 | { |
| 244 | CHARM_DBG("%s: charm sent status change interrupt\n", __func__); |
| 245 | if ((gpio_get_value(MDM2AP_STATUS) == 0) && charm_ready) { |
| 246 | CHARM_DBG("%s: scheduling work now\n", __func__); |
| 247 | queue_work(charm_queue, &charm_status_work); |
| 248 | } else if (gpio_get_value(MDM2AP_STATUS) == 1) { |
| 249 | CHARM_DBG("%s: charm is now ready\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | } |
| 251 | return IRQ_HANDLED; |
| 252 | } |
| 253 | |
| 254 | static int charm_debug_on_set(void *data, u64 val) |
| 255 | { |
| 256 | charm_debug_on = val; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int charm_debug_on_get(void *data, u64 *val) |
| 261 | { |
| 262 | *val = charm_debug_on; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | DEFINE_SIMPLE_ATTRIBUTE(charm_debug_on_fops, |
| 267 | charm_debug_on_get, |
| 268 | charm_debug_on_set, "%llu\n"); |
| 269 | |
| 270 | static int charm_debugfs_init(void) |
| 271 | { |
| 272 | struct dentry *dent; |
| 273 | |
| 274 | dent = debugfs_create_dir("charm_dbg", 0); |
| 275 | if (IS_ERR(dent)) |
| 276 | return PTR_ERR(dent); |
| 277 | |
| 278 | debugfs_create_file("debug_on", 0644, dent, NULL, |
| 279 | &charm_debug_on_fops); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int gsbi9_uart_notifier_cb(struct notifier_block *this, |
| 284 | unsigned long code, void *_cmd) |
| 285 | { |
| 286 | switch (code) { |
| 287 | case SUBSYS_AFTER_SHUTDOWN: |
| 288 | platform_device_unregister(msm_device_uart_gsbi9); |
| 289 | msm_device_uart_gsbi9 = msm_add_gsbi9_uart(); |
| 290 | if (IS_ERR(msm_device_uart_gsbi9)) |
| 291 | pr_err("%s(): Failed to create uart gsbi9 device\n", |
| 292 | __func__); |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | return NOTIFY_DONE; |
| 297 | } |
| 298 | |
| 299 | static struct notifier_block gsbi9_nb = { |
| 300 | .notifier_call = gsbi9_uart_notifier_cb, |
| 301 | }; |
| 302 | |
| 303 | static int __init charm_modem_probe(struct platform_device *pdev) |
| 304 | { |
| 305 | int ret, irq; |
| 306 | struct charm_platform_data *d = pdev->dev.platform_data; |
| 307 | |
| 308 | gpio_request(AP2MDM_STATUS, "AP2MDM_STATUS"); |
| 309 | gpio_request(AP2MDM_ERRFATAL, "AP2MDM_ERRFATAL"); |
| 310 | gpio_request(AP2MDM_KPDPWR_N, "AP2MDM_KPDPWR_N"); |
| 311 | gpio_request(AP2MDM_PMIC_RESET_N, "AP2MDM_PMIC_RESET_N"); |
| 312 | gpio_request(MDM2AP_STATUS, "MDM2AP_STATUS"); |
| 313 | gpio_request(MDM2AP_ERRFATAL, "MDM2AP_ERRFATAL"); |
| 314 | |
| 315 | gpio_direction_output(AP2MDM_STATUS, 1); |
| 316 | gpio_direction_output(AP2MDM_ERRFATAL, 0); |
| 317 | gpio_direction_input(MDM2AP_STATUS); |
| 318 | gpio_direction_input(MDM2AP_ERRFATAL); |
| 319 | |
| 320 | power_on_charm = d->charm_modem_on; |
| 321 | power_down_charm = d->charm_modem_off; |
| 322 | |
| 323 | charm_queue = create_singlethread_workqueue("charm_queue"); |
| 324 | if (!charm_queue) { |
| 325 | pr_err("%s: could not create workqueue. All charm \ |
| 326 | functionality will be disabled\n", |
| 327 | __func__); |
| 328 | ret = -ENOMEM; |
| 329 | goto fatal_err; |
| 330 | } |
| 331 | |
| 332 | atomic_notifier_chain_register(&panic_notifier_list, &charm_panic_blk); |
| 333 | charm_debugfs_init(); |
| 334 | |
| 335 | ssr_register_subsystem(&charm_subsystem); |
| 336 | |
| 337 | irq = platform_get_irq(pdev, 0); |
| 338 | if (irq < 0) { |
| 339 | pr_err("%s: could not get MDM2AP_ERRFATAL IRQ resource. \ |
| 340 | error=%d No IRQ will be generated on errfatal.", |
| 341 | __func__, irq); |
| 342 | goto errfatal_err; |
| 343 | } |
| 344 | |
| 345 | ret = request_irq(irq, charm_errfatal, |
| 346 | IRQF_TRIGGER_RISING , "charm errfatal", NULL); |
| 347 | |
| 348 | if (ret < 0) { |
| 349 | pr_err("%s: MDM2AP_ERRFATAL IRQ#%d request failed with error=%d\ |
| 350 | . No IRQ will be generated on errfatal.", |
| 351 | __func__, irq, ret); |
| 352 | goto errfatal_err; |
| 353 | } |
| 354 | charm_errfatal_irq = irq; |
| 355 | |
| 356 | errfatal_err: |
| 357 | |
| 358 | irq = platform_get_irq(pdev, 1); |
| 359 | if (irq < 0) { |
| 360 | pr_err("%s: could not get MDM2AP_STATUS IRQ resource. \ |
| 361 | error=%d No IRQ will be generated on status change.", |
| 362 | __func__, irq); |
| 363 | goto status_err; |
| 364 | } |
| 365 | |
| 366 | ret = request_threaded_irq(irq, NULL, charm_status_change, |
| 367 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 368 | "charm status", NULL); |
| 369 | |
| 370 | if (ret < 0) { |
| 371 | pr_err("%s: MDM2AP_STATUS IRQ#%d request failed with error=%d\ |
| 372 | . No IRQ will be generated on status change.", |
| 373 | __func__, irq, ret); |
| 374 | goto status_err; |
| 375 | } |
| 376 | charm_status_irq = irq; |
| 377 | |
| 378 | status_err: |
| 379 | subsys_notif_register_notifier("external_modem", &gsbi9_nb); |
| 380 | |
| 381 | pr_info("%s: Registering charm modem\n", __func__); |
| 382 | |
| 383 | return misc_register(&charm_modem_misc); |
| 384 | |
| 385 | fatal_err: |
| 386 | gpio_free(AP2MDM_STATUS); |
| 387 | gpio_free(AP2MDM_ERRFATAL); |
| 388 | gpio_free(AP2MDM_KPDPWR_N); |
| 389 | gpio_free(AP2MDM_PMIC_RESET_N); |
| 390 | gpio_free(MDM2AP_STATUS); |
| 391 | gpio_free(MDM2AP_ERRFATAL); |
| 392 | return ret; |
| 393 | |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static int __devexit charm_modem_remove(struct platform_device *pdev) |
| 398 | { |
| 399 | gpio_free(AP2MDM_STATUS); |
| 400 | gpio_free(AP2MDM_ERRFATAL); |
| 401 | gpio_free(AP2MDM_KPDPWR_N); |
| 402 | gpio_free(AP2MDM_PMIC_RESET_N); |
| 403 | gpio_free(MDM2AP_STATUS); |
| 404 | gpio_free(MDM2AP_ERRFATAL); |
| 405 | |
| 406 | return misc_deregister(&charm_modem_misc); |
| 407 | } |
| 408 | |
| 409 | static void charm_modem_shutdown(struct platform_device *pdev) |
| 410 | { |
| 411 | int i; |
| 412 | |
| 413 | CHARM_DBG("%s: setting AP2MDM_STATUS low for a graceful restart\n", |
| 414 | __func__); |
| 415 | |
| 416 | charm_disable_irqs(); |
| 417 | |
| 418 | gpio_set_value(AP2MDM_STATUS, 0); |
| 419 | |
| 420 | for (i = CHARM_MODEM_TIMEOUT; i > 0; i -= CHARM_MODEM_DELTA) { |
| 421 | pet_watchdog(); |
| 422 | msleep(CHARM_MODEM_DELTA); |
| 423 | if (gpio_get_value(MDM2AP_STATUS) == 0) |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | if (i <= 0) { |
| 428 | pr_err("%s: MDM2AP_STATUS never went low.\n", |
| 429 | __func__); |
| 430 | gpio_direction_output(AP2MDM_PMIC_RESET_N, 1); |
| 431 | for (i = CHARM_HOLD_TIME; i > 0; i -= CHARM_MODEM_DELTA) { |
| 432 | pet_watchdog(); |
| 433 | msleep(CHARM_MODEM_DELTA); |
| 434 | } |
| 435 | gpio_direction_output(AP2MDM_PMIC_RESET_N, 0); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static struct platform_driver charm_modem_driver = { |
| 440 | .remove = charm_modem_remove, |
| 441 | .shutdown = charm_modem_shutdown, |
| 442 | .driver = { |
| 443 | .name = "charm_modem", |
| 444 | .owner = THIS_MODULE |
| 445 | }, |
| 446 | }; |
| 447 | |
| 448 | static int __init charm_modem_init(void) |
| 449 | { |
| 450 | return platform_driver_probe(&charm_modem_driver, charm_modem_probe); |
| 451 | } |
| 452 | |
| 453 | static void __exit charm_modem_exit(void) |
| 454 | { |
| 455 | platform_driver_unregister(&charm_modem_driver); |
| 456 | } |
| 457 | |
| 458 | module_init(charm_modem_init); |
| 459 | module_exit(charm_modem_exit); |
| 460 | |
| 461 | MODULE_LICENSE("GPL v2"); |
| 462 | MODULE_DESCRIPTION("msm8660 charm modem driver"); |
| 463 | MODULE_VERSION("1.0"); |
| 464 | MODULE_ALIAS("charm_modem"); |