Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 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 | #include <linux/kernel.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/reboot.h> |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/stringify.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/module.h> |
| 22 | |
| 23 | #include <mach/irqs.h> |
| 24 | #include <mach/scm.h> |
| 25 | #include <mach/peripheral-loader.h> |
| 26 | #include <mach/subsystem_restart.h> |
| 27 | #include <mach/subsystem_notif.h> |
| 28 | |
| 29 | #include "smd_private.h" |
| 30 | #include "modem_notifier.h" |
| 31 | #include "ramdump.h" |
| 32 | |
| 33 | #define MODEM_HWIO_MSS_RESET_ADDR 0x00902C48 |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 34 | #define MODULE_NAME "modem_8660" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #define MODEM_WDOG_ENABLE 0x10020008 |
Vikram Mulukutla | 8954956 | 2011-07-28 16:10:41 -0700 | [diff] [blame] | 36 | #define MODEM_CLEANUP_DELAY_MS 20 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | |
| 38 | #define SUBSYS_FATAL_DEBUG |
| 39 | |
| 40 | #if defined(SUBSYS_FATAL_DEBUG) |
| 41 | static void debug_crash_modem_fn(struct work_struct *); |
| 42 | static int reset_modem; |
Vikram Mulukutla | f4510d2 | 2011-12-16 11:40:21 -0800 | [diff] [blame^] | 43 | static int ignore_smsm_ack; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | |
| 45 | static DECLARE_DELAYED_WORK(debug_crash_modem_work, |
| 46 | debug_crash_modem_fn); |
| 47 | |
| 48 | module_param(reset_modem, int, 0644); |
| 49 | #endif |
| 50 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | /* Subsystem restart: Modem data, functions */ |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 52 | static void *modem_ramdump_dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 53 | static void modem_fatal_fn(struct work_struct *); |
| 54 | static void modem_unlock_timeout(struct work_struct *work); |
| 55 | static int modem_notif_handler(struct notifier_block *this, |
| 56 | unsigned long code, |
| 57 | void *_cmd); |
| 58 | static DECLARE_WORK(modem_fatal_work, modem_fatal_fn); |
| 59 | static DECLARE_DELAYED_WORK(modem_unlock_timeout_work, |
| 60 | modem_unlock_timeout); |
| 61 | |
| 62 | static struct notifier_block modem_notif_nb = { |
| 63 | .notifier_call = modem_notif_handler, |
| 64 | }; |
| 65 | |
| 66 | static void modem_unlock_timeout(struct work_struct *work) |
| 67 | { |
| 68 | void __iomem *hwio_modem_reset_addr = |
| 69 | ioremap_nocache(MODEM_HWIO_MSS_RESET_ADDR, 8); |
| 70 | pr_crit("%s: Timeout waiting for modem to unlock.\n", MODULE_NAME); |
| 71 | |
| 72 | /* Set MSS_MODEM_RESET to 0x0 since the unlock didn't work */ |
| 73 | writel_relaxed(0x0, hwio_modem_reset_addr); |
| 74 | /* Write needs to go through before the modem is restarted. */ |
| 75 | mb(); |
| 76 | iounmap(hwio_modem_reset_addr); |
| 77 | |
| 78 | subsystem_restart("modem"); |
Vikram Mulukutla | 93510c4 | 2011-08-19 19:04:40 -0700 | [diff] [blame] | 79 | enable_irq(MARM_WDOG_EXPIRED); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void modem_fatal_fn(struct work_struct *work) |
| 83 | { |
| 84 | uint32_t modem_state; |
| 85 | uint32_t panic_smsm_states = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD; |
| 86 | uint32_t reset_smsm_states = SMSM_SYSTEM_REBOOT_USR | |
| 87 | SMSM_SYSTEM_PWRDWN_USR; |
| 88 | |
| 89 | pr_err("%s: Watchdog bite received from modem!\n", MODULE_NAME); |
| 90 | |
| 91 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 92 | pr_err("%s: Modem SMSM state = 0x%x!", MODULE_NAME, modem_state); |
| 93 | |
| 94 | if (modem_state == 0 || modem_state & panic_smsm_states) { |
| 95 | |
| 96 | subsystem_restart("modem"); |
Vikram Mulukutla | 93510c4 | 2011-08-19 19:04:40 -0700 | [diff] [blame] | 97 | enable_irq(MARM_WDOG_EXPIRED); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | |
| 99 | } else if (modem_state & reset_smsm_states) { |
| 100 | |
| 101 | pr_err("%s: User-invoked system reset/powerdown.", |
| 102 | MODULE_NAME); |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 103 | kernel_restart(NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | |
| 105 | } else { |
| 106 | |
| 107 | int ret; |
| 108 | void *hwio_modem_reset_addr = |
| 109 | ioremap_nocache(MODEM_HWIO_MSS_RESET_ADDR, 8); |
| 110 | |
| 111 | pr_err("%s: Modem AHB locked up.\n", MODULE_NAME); |
| 112 | pr_err("%s: Trying to free up modem!\n", MODULE_NAME); |
| 113 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 114 | writel_relaxed(0x3, hwio_modem_reset_addr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | |
| 116 | /* If we are still alive after 6 seconds (allowing for |
| 117 | * the 5-second-delayed-panic-reboot), modem is either |
| 118 | * still wedged or SMSM didn't come through. Force panic |
| 119 | * in that case. |
| 120 | */ |
| 121 | ret = schedule_delayed_work(&modem_unlock_timeout_work, |
| 122 | msecs_to_jiffies(6000)); |
| 123 | |
| 124 | iounmap(hwio_modem_reset_addr); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static int modem_notif_handler(struct notifier_block *this, |
| 129 | unsigned long code, |
| 130 | void *_cmd) |
| 131 | { |
| 132 | if (code == MODEM_NOTIFIER_START_RESET) { |
Vikram Mulukutla | f4510d2 | 2011-12-16 11:40:21 -0800 | [diff] [blame^] | 133 | if (ignore_smsm_ack) { |
| 134 | ignore_smsm_ack = 0; |
| 135 | goto out; |
| 136 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 137 | pr_err("%s: Modem error fatal'ed.", MODULE_NAME); |
| 138 | subsystem_restart("modem"); |
| 139 | } |
Vikram Mulukutla | f4510d2 | 2011-12-16 11:40:21 -0800 | [diff] [blame^] | 140 | out: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | return NOTIFY_DONE; |
| 142 | } |
| 143 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 144 | static int modem_shutdown(const struct subsys_data *crashed_subsys) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | { |
| 146 | void __iomem *modem_wdog_addr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 147 | |
| 148 | /* If the modem didn't already crash, setting SMSM_RESET |
Vikram Mulukutla | f4510d2 | 2011-12-16 11:40:21 -0800 | [diff] [blame^] | 149 | * here will help flush caches etc. The ignore_smsm_ack |
| 150 | * flag is set to ignore the SMSM_RESET notification |
| 151 | * that is generated due to the modem settings its own |
| 152 | * SMSM_RESET bit in response to the apps setting the |
| 153 | * apps SMSM_RESET bit. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | */ |
| 155 | if (!(smsm_get_state(SMSM_MODEM_STATE) & SMSM_RESET)) { |
Vikram Mulukutla | f4510d2 | 2011-12-16 11:40:21 -0800 | [diff] [blame^] | 156 | ignore_smsm_ack = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | smsm_reset_modem(SMSM_RESET); |
| 158 | } |
| 159 | |
| 160 | /* Disable the modem watchdog to allow clean modem bootup */ |
| 161 | modem_wdog_addr = ioremap_nocache(MODEM_WDOG_ENABLE, 8); |
| 162 | writel_relaxed(0x0, modem_wdog_addr); |
| 163 | |
| 164 | /* |
| 165 | * The write above needs to go through before the modem is |
| 166 | * powered up again (subsystem restart). |
| 167 | */ |
| 168 | mb(); |
| 169 | iounmap(modem_wdog_addr); |
| 170 | |
Vikram Mulukutla | 8954956 | 2011-07-28 16:10:41 -0700 | [diff] [blame] | 171 | /* Wait here to allow the modem to clean up caches etc. */ |
| 172 | msleep(MODEM_CLEANUP_DELAY_MS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | pil_force_shutdown("modem"); |
| 174 | disable_irq_nosync(MARM_WDOG_EXPIRED); |
| 175 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 181 | static int modem_powerup(const struct subsys_data *crashed_subsys) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | ret = pil_force_boot("modem"); |
| 186 | enable_irq(MARM_WDOG_EXPIRED); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /* FIXME: Get address, size from PIL */ |
| 192 | static struct ramdump_segment modem_segments[] = { |
| 193 | {0x42F00000, 0x46000000 - 0x42F00000} }; |
| 194 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 195 | static int modem_ramdump(int enable, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | const struct subsys_data *crashed_subsys) |
| 197 | { |
| 198 | if (enable) |
| 199 | return do_ramdump(modem_ramdump_dev, modem_segments, |
| 200 | ARRAY_SIZE(modem_segments)); |
| 201 | else |
| 202 | return 0; |
| 203 | } |
| 204 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 205 | static void modem_crash_shutdown( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | const struct subsys_data *crashed_subsys) |
| 207 | { |
| 208 | /* If modem hasn't already crashed, send SMSM_RESET. */ |
| 209 | if (!(smsm_get_state(SMSM_MODEM_STATE) & SMSM_RESET)) { |
| 210 | modem_unregister_notifier(&modem_notif_nb); |
| 211 | smsm_reset_modem(SMSM_RESET); |
| 212 | } |
| 213 | |
Vikram Mulukutla | 6066a84 | 2011-09-16 11:14:50 -0700 | [diff] [blame] | 214 | /* Wait to allow the modem to clean up caches etc. */ |
| 215 | mdelay(5); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 218 | static irqreturn_t modem_wdog_bite_irq(int irq, void *dev_id) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 219 | { |
| 220 | int ret; |
| 221 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 222 | ret = schedule_work(&modem_fatal_work); |
| 223 | disable_irq_nosync(MARM_WDOG_EXPIRED); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | |
| 225 | return IRQ_HANDLED; |
| 226 | } |
| 227 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 228 | static struct subsys_data subsys_8660_modem = { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | .name = "modem", |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 230 | .shutdown = modem_shutdown, |
| 231 | .powerup = modem_powerup, |
| 232 | .ramdump = modem_ramdump, |
| 233 | .crash_shutdown = modem_crash_shutdown |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 236 | static int __init modem_8660_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | { |
| 238 | int ret; |
| 239 | |
| 240 | /* Need to listen for SMSM_RESET always */ |
| 241 | modem_register_notifier(&modem_notif_nb); |
| 242 | |
| 243 | #if defined(SUBSYS_FATAL_DEBUG) |
| 244 | schedule_delayed_work(&debug_crash_modem_work, msecs_to_jiffies(5000)); |
| 245 | #endif |
| 246 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 247 | ret = request_irq(MARM_WDOG_EXPIRED, modem_wdog_bite_irq, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 248 | IRQF_TRIGGER_RISING, "modem_wdog", NULL); |
| 249 | |
| 250 | if (ret < 0) { |
| 251 | pr_err("%s: Unable to request MARM_WDOG_EXPIRED irq.", |
| 252 | __func__); |
| 253 | goto out; |
| 254 | } |
| 255 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | modem_ramdump_dev = create_ramdump_device("modem"); |
| 257 | |
| 258 | if (!modem_ramdump_dev) { |
| 259 | ret = -ENOMEM; |
| 260 | goto out; |
| 261 | } |
| 262 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 263 | ret = ssr_register_subsystem(&subsys_8660_modem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 264 | out: |
| 265 | return ret; |
| 266 | } |
| 267 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 268 | static void __exit modem_8660_exit(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | { |
| 270 | free_irq(MARM_WDOG_EXPIRED, NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | #ifdef SUBSYS_FATAL_DEBUG |
| 274 | static void debug_crash_modem_fn(struct work_struct *work) |
| 275 | { |
| 276 | if (reset_modem == 1) |
| 277 | smsm_reset_modem(SMSM_RESET); |
| 278 | else if (reset_modem == 2) |
| 279 | subsystem_restart("lpass"); |
| 280 | |
| 281 | reset_modem = 0; |
| 282 | schedule_delayed_work(&debug_crash_modem_work, msecs_to_jiffies(1000)); |
| 283 | } |
| 284 | #endif |
| 285 | |
Vikram Mulukutla | ffa387e | 2011-09-13 15:14:35 -0700 | [diff] [blame] | 286 | module_init(modem_8660_init); |
| 287 | module_exit(modem_8660_exit); |
| 288 | |