Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/smd.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
| 4 | * Author: Brian Swetland <swetland@google.com> |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/cdev.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/debugfs.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/io.h> |
| 30 | |
| 31 | #include <mach/msm_smd.h> |
| 32 | #include <mach/msm_iomap.h> |
| 33 | #include <mach/system.h> |
| 34 | |
| 35 | #include "smd_private.h" |
| 36 | #include "proc_comm.h" |
| 37 | |
| 38 | void (*msm_hw_reset_hook)(void); |
| 39 | |
| 40 | #define MODULE_NAME "msm_smd" |
| 41 | |
| 42 | enum { |
| 43 | MSM_SMD_DEBUG = 1U << 0, |
| 44 | MSM_SMSM_DEBUG = 1U << 0, |
| 45 | }; |
| 46 | |
| 47 | static int msm_smd_debug_mask; |
| 48 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 49 | struct shared_info { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 50 | int ready; |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 51 | unsigned state; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 54 | static unsigned dummy_state[SMSM_STATE_COUNT]; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 55 | |
| 56 | static struct shared_info smd_info = { |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 57 | .state = (unsigned) &dummy_state, |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 60 | module_param_named(debug_mask, msm_smd_debug_mask, |
| 61 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 62 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 63 | void *smem_item(unsigned id, unsigned *size); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 64 | static void smd_diag(void); |
| 65 | |
| 66 | static unsigned last_heap_free = 0xffffffff; |
| 67 | |
| 68 | #define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4) |
| 69 | |
| 70 | static inline void notify_other_smsm(void) |
| 71 | { |
| 72 | writel(1, MSM_A2M_INT(5)); |
| 73 | } |
| 74 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 75 | static inline void notify_modem_smd(void) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 76 | { |
| 77 | writel(1, MSM_A2M_INT(0)); |
| 78 | } |
| 79 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 80 | static inline void notify_dsp_smd(void) |
| 81 | { |
| 82 | writel(1, MSM_A2M_INT(8)); |
| 83 | } |
| 84 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 85 | static void smd_diag(void) |
| 86 | { |
| 87 | char *x; |
| 88 | |
| 89 | x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG); |
| 90 | if (x != 0) { |
| 91 | x[SZ_DIAG_ERR_MSG - 1] = 0; |
| 92 | pr_info("smem: DIAG '%s'\n", x); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* call when SMSM_RESET flag is set in the A9's smsm_state */ |
| 97 | static void handle_modem_crash(void) |
| 98 | { |
| 99 | pr_err("ARM9 has CRASHED\n"); |
| 100 | smd_diag(); |
| 101 | |
| 102 | /* hard reboot if possible */ |
| 103 | if (msm_hw_reset_hook) |
| 104 | msm_hw_reset_hook(); |
| 105 | |
| 106 | /* in this case the modem or watchdog should reboot us */ |
| 107 | for (;;) |
| 108 | ; |
| 109 | } |
| 110 | |
| 111 | extern int (*msm_check_for_modem_crash)(void); |
| 112 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 113 | uint32_t raw_smsm_get_state(enum smsm_state_item item) |
| 114 | { |
| 115 | return readl(smd_info.state + item * 4); |
| 116 | } |
| 117 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 118 | static int check_for_modem_crash(void) |
| 119 | { |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 120 | if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) { |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 121 | handle_modem_crash(); |
| 122 | return -1; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 123 | } |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 124 | return 0; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 127 | /* the spinlock is used to synchronize between the |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 128 | * irq handler and code that mutates the channel |
| 129 | * list or fiddles with channel state |
| 130 | */ |
| 131 | DEFINE_SPINLOCK(smd_lock); |
| 132 | DEFINE_SPINLOCK(smem_lock); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 133 | |
| 134 | /* the mutex is used during open() and close() |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 135 | * operations to avoid races while creating or |
| 136 | * destroying smd_channel structures |
| 137 | */ |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 138 | static DEFINE_MUTEX(smd_creation_mutex); |
| 139 | |
| 140 | static int smd_initialized; |
| 141 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 142 | LIST_HEAD(smd_ch_closed_list); |
| 143 | LIST_HEAD(smd_ch_list); /* todo: per-target lists */ |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 144 | |
| 145 | static unsigned char smd_ch_allocated[64]; |
| 146 | static struct work_struct probe_work; |
| 147 | |
| 148 | static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type); |
| 149 | |
| 150 | static void smd_channel_probe_worker(struct work_struct *work) |
| 151 | { |
| 152 | struct smd_alloc_elm *shared; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 153 | unsigned type; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 154 | unsigned n; |
| 155 | |
| 156 | shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64); |
Brian Swetland | 4d4fb26 | 2009-01-28 20:25:40 -0800 | [diff] [blame] | 157 | if (!shared) { |
| 158 | pr_err("smd: cannot find allocation table\n"); |
| 159 | return; |
| 160 | } |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 161 | for (n = 0; n < 64; n++) { |
| 162 | if (smd_ch_allocated[n]) |
| 163 | continue; |
| 164 | if (!shared[n].ref_count) |
| 165 | continue; |
| 166 | if (!shared[n].name[0]) |
| 167 | continue; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 168 | type = shared[n].ctype & SMD_TYPE_MASK; |
| 169 | if ((type == SMD_TYPE_APPS_MODEM) || |
| 170 | (type == SMD_TYPE_APPS_DSP)) |
| 171 | smd_alloc_channel(shared[n].name, |
| 172 | shared[n].cid, |
| 173 | shared[n].ctype); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 174 | smd_ch_allocated[n] = 1; |
| 175 | } |
| 176 | } |
| 177 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 178 | /* how many bytes are available for reading */ |
| 179 | static int smd_stream_read_avail(struct smd_channel *ch) |
| 180 | { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 181 | return (ch->recv->head - ch->recv->tail) & ch->fifo_mask; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* how many bytes we are free to write */ |
| 185 | static int smd_stream_write_avail(struct smd_channel *ch) |
| 186 | { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 187 | return ch->fifo_mask - |
| 188 | ((ch->send->head - ch->send->tail) & ch->fifo_mask); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int smd_packet_read_avail(struct smd_channel *ch) |
| 192 | { |
| 193 | if (ch->current_packet) { |
| 194 | int n = smd_stream_read_avail(ch); |
| 195 | if (n > ch->current_packet) |
| 196 | n = ch->current_packet; |
| 197 | return n; |
| 198 | } else { |
| 199 | return 0; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static int smd_packet_write_avail(struct smd_channel *ch) |
| 204 | { |
| 205 | int n = smd_stream_write_avail(ch); |
| 206 | return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0; |
| 207 | } |
| 208 | |
| 209 | static int ch_is_open(struct smd_channel *ch) |
| 210 | { |
| 211 | return (ch->recv->state == SMD_SS_OPENED) && |
| 212 | (ch->send->state == SMD_SS_OPENED); |
| 213 | } |
| 214 | |
| 215 | /* provide a pointer and length to readable data in the fifo */ |
| 216 | static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr) |
| 217 | { |
| 218 | unsigned head = ch->recv->head; |
| 219 | unsigned tail = ch->recv->tail; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 220 | *ptr = (void *) (ch->recv_data + tail); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 221 | |
| 222 | if (tail <= head) |
| 223 | return head - tail; |
| 224 | else |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 225 | return ch->fifo_size - tail; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* advance the fifo read pointer after data from ch_read_buffer is consumed */ |
| 229 | static void ch_read_done(struct smd_channel *ch, unsigned count) |
| 230 | { |
| 231 | BUG_ON(count > smd_stream_read_avail(ch)); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 232 | ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 233 | ch->recv->fTAIL = 1; |
| 234 | } |
| 235 | |
| 236 | /* basic read interface to ch_read_{buffer,done} used |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 237 | * by smd_*_read() and update_packet_state() |
| 238 | * will read-and-discard if the _data pointer is null |
| 239 | */ |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 240 | static int ch_read(struct smd_channel *ch, void *_data, int len) |
| 241 | { |
| 242 | void *ptr; |
| 243 | unsigned n; |
| 244 | unsigned char *data = _data; |
| 245 | int orig_len = len; |
| 246 | |
| 247 | while (len > 0) { |
| 248 | n = ch_read_buffer(ch, &ptr); |
| 249 | if (n == 0) |
| 250 | break; |
| 251 | |
| 252 | if (n > len) |
| 253 | n = len; |
| 254 | if (_data) |
| 255 | memcpy(data, ptr, n); |
| 256 | |
| 257 | data += n; |
| 258 | len -= n; |
| 259 | ch_read_done(ch, n); |
| 260 | } |
| 261 | |
| 262 | return orig_len - len; |
| 263 | } |
| 264 | |
| 265 | static void update_stream_state(struct smd_channel *ch) |
| 266 | { |
| 267 | /* streams have no special state requiring updating */ |
| 268 | } |
| 269 | |
| 270 | static void update_packet_state(struct smd_channel *ch) |
| 271 | { |
| 272 | unsigned hdr[5]; |
| 273 | int r; |
| 274 | |
| 275 | /* can't do anything if we're in the middle of a packet */ |
| 276 | if (ch->current_packet != 0) |
| 277 | return; |
| 278 | |
| 279 | /* don't bother unless we can get the full header */ |
| 280 | if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE) |
| 281 | return; |
| 282 | |
| 283 | r = ch_read(ch, hdr, SMD_HEADER_SIZE); |
| 284 | BUG_ON(r != SMD_HEADER_SIZE); |
| 285 | |
| 286 | ch->current_packet = hdr[0]; |
| 287 | } |
| 288 | |
| 289 | /* provide a pointer and length to next free space in the fifo */ |
| 290 | static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr) |
| 291 | { |
| 292 | unsigned head = ch->send->head; |
| 293 | unsigned tail = ch->send->tail; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 294 | *ptr = (void *) (ch->send_data + head); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 295 | |
| 296 | if (head < tail) { |
| 297 | return tail - head - 1; |
| 298 | } else { |
| 299 | if (tail == 0) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 300 | return ch->fifo_size - head - 1; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 301 | else |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 302 | return ch->fifo_size - head; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | /* advace the fifo write pointer after freespace |
| 307 | * from ch_write_buffer is filled |
| 308 | */ |
| 309 | static void ch_write_done(struct smd_channel *ch, unsigned count) |
| 310 | { |
| 311 | BUG_ON(count > smd_stream_write_avail(ch)); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 312 | ch->send->head = (ch->send->head + count) & ch->fifo_mask; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 313 | ch->send->fHEAD = 1; |
| 314 | } |
| 315 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 316 | static void ch_set_state(struct smd_channel *ch, unsigned n) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 317 | { |
| 318 | if (n == SMD_SS_OPENED) { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 319 | ch->send->fDSR = 1; |
| 320 | ch->send->fCTS = 1; |
| 321 | ch->send->fCD = 1; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 322 | } else { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 323 | ch->send->fDSR = 0; |
| 324 | ch->send->fCTS = 0; |
| 325 | ch->send->fCD = 0; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 326 | } |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 327 | ch->send->state = n; |
| 328 | ch->send->fSTATE = 1; |
| 329 | ch->notify_other_cpu(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static void do_smd_probe(void) |
| 333 | { |
| 334 | struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE; |
| 335 | if (shared->heap_info.free_offset != last_heap_free) { |
| 336 | last_heap_free = shared->heap_info.free_offset; |
| 337 | schedule_work(&probe_work); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void smd_state_change(struct smd_channel *ch, |
| 342 | unsigned last, unsigned next) |
| 343 | { |
| 344 | ch->last_state = next; |
| 345 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 346 | pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 347 | |
| 348 | switch (next) { |
| 349 | case SMD_SS_OPENING: |
| 350 | ch->recv->tail = 0; |
| 351 | case SMD_SS_OPENED: |
| 352 | if (ch->send->state != SMD_SS_OPENED) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 353 | ch_set_state(ch, SMD_SS_OPENED); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 354 | ch->notify(ch->priv, SMD_EVENT_OPEN); |
| 355 | break; |
| 356 | case SMD_SS_FLUSHING: |
| 357 | case SMD_SS_RESET: |
| 358 | /* we should force them to close? */ |
| 359 | default: |
| 360 | ch->notify(ch->priv, SMD_EVENT_CLOSE); |
| 361 | } |
| 362 | } |
| 363 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 364 | static void handle_smd_irq(struct list_head *list, void (*notify)(void)) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 365 | { |
| 366 | unsigned long flags; |
| 367 | struct smd_channel *ch; |
| 368 | int do_notify = 0; |
| 369 | unsigned ch_flags; |
| 370 | unsigned tmp; |
| 371 | |
| 372 | spin_lock_irqsave(&smd_lock, flags); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 373 | list_for_each_entry(ch, list, ch_list) { |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 374 | ch_flags = 0; |
| 375 | if (ch_is_open(ch)) { |
| 376 | if (ch->recv->fHEAD) { |
| 377 | ch->recv->fHEAD = 0; |
| 378 | ch_flags |= 1; |
| 379 | do_notify |= 1; |
| 380 | } |
| 381 | if (ch->recv->fTAIL) { |
| 382 | ch->recv->fTAIL = 0; |
| 383 | ch_flags |= 2; |
| 384 | do_notify |= 1; |
| 385 | } |
| 386 | if (ch->recv->fSTATE) { |
| 387 | ch->recv->fSTATE = 0; |
| 388 | ch_flags |= 4; |
| 389 | do_notify |= 1; |
| 390 | } |
| 391 | } |
| 392 | tmp = ch->recv->state; |
| 393 | if (tmp != ch->last_state) |
| 394 | smd_state_change(ch, ch->last_state, tmp); |
| 395 | if (ch_flags) { |
| 396 | ch->update_state(ch); |
| 397 | ch->notify(ch->priv, SMD_EVENT_DATA); |
| 398 | } |
| 399 | } |
| 400 | if (do_notify) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 401 | notify(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 402 | spin_unlock_irqrestore(&smd_lock, flags); |
| 403 | do_smd_probe(); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static irqreturn_t smd_irq_handler(int irq, void *data) |
| 407 | { |
| 408 | handle_smd_irq(&smd_ch_list, notify_modem_smd); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 409 | return IRQ_HANDLED; |
| 410 | } |
| 411 | |
| 412 | static void smd_fake_irq_handler(unsigned long arg) |
| 413 | { |
| 414 | smd_irq_handler(0, NULL); |
| 415 | } |
| 416 | |
| 417 | static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0); |
| 418 | |
| 419 | void smd_sleep_exit(void) |
| 420 | { |
| 421 | unsigned long flags; |
| 422 | struct smd_channel *ch; |
| 423 | unsigned tmp; |
| 424 | int need_int = 0; |
| 425 | |
| 426 | spin_lock_irqsave(&smd_lock, flags); |
| 427 | list_for_each_entry(ch, &smd_ch_list, ch_list) { |
| 428 | if (ch_is_open(ch)) { |
| 429 | if (ch->recv->fHEAD) { |
| 430 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) |
| 431 | pr_info("smd_sleep_exit ch %d fHEAD " |
| 432 | "%x %x %x\n", |
| 433 | ch->n, ch->recv->fHEAD, |
| 434 | ch->recv->head, ch->recv->tail); |
| 435 | need_int = 1; |
| 436 | break; |
| 437 | } |
| 438 | if (ch->recv->fTAIL) { |
| 439 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) |
| 440 | pr_info("smd_sleep_exit ch %d fTAIL " |
| 441 | "%x %x %x\n", |
| 442 | ch->n, ch->recv->fTAIL, |
| 443 | ch->send->head, ch->send->tail); |
| 444 | need_int = 1; |
| 445 | break; |
| 446 | } |
| 447 | if (ch->recv->fSTATE) { |
| 448 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) |
| 449 | pr_info("smd_sleep_exit ch %d fSTATE %x" |
| 450 | "\n", ch->n, ch->recv->fSTATE); |
| 451 | need_int = 1; |
| 452 | break; |
| 453 | } |
| 454 | tmp = ch->recv->state; |
| 455 | if (tmp != ch->last_state) { |
| 456 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) |
| 457 | pr_info("smd_sleep_exit ch %d " |
| 458 | "state %x != %x\n", |
| 459 | ch->n, tmp, ch->last_state); |
| 460 | need_int = 1; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | spin_unlock_irqrestore(&smd_lock, flags); |
| 466 | do_smd_probe(); |
| 467 | if (need_int) { |
| 468 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) |
| 469 | pr_info("smd_sleep_exit need interrupt\n"); |
| 470 | tasklet_schedule(&smd_fake_irq_tasklet); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void smd_kick(smd_channel_t *ch) |
| 476 | { |
| 477 | unsigned long flags; |
| 478 | unsigned tmp; |
| 479 | |
| 480 | spin_lock_irqsave(&smd_lock, flags); |
| 481 | ch->update_state(ch); |
| 482 | tmp = ch->recv->state; |
| 483 | if (tmp != ch->last_state) { |
| 484 | ch->last_state = tmp; |
| 485 | if (tmp == SMD_SS_OPENED) |
| 486 | ch->notify(ch->priv, SMD_EVENT_OPEN); |
| 487 | else |
| 488 | ch->notify(ch->priv, SMD_EVENT_CLOSE); |
| 489 | } |
| 490 | ch->notify(ch->priv, SMD_EVENT_DATA); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 491 | ch->notify_other_cpu(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 492 | spin_unlock_irqrestore(&smd_lock, flags); |
| 493 | } |
| 494 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 495 | static int smd_is_packet(int chn, unsigned type) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 496 | { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 497 | type &= SMD_KIND_MASK; |
| 498 | if (type == SMD_KIND_PACKET) |
| 499 | return 1; |
| 500 | if (type == SMD_KIND_STREAM) |
| 501 | return 0; |
| 502 | |
| 503 | /* older AMSS reports SMD_KIND_UNKNOWN always */ |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 504 | if ((chn > 4) || (chn == 1)) |
| 505 | return 1; |
| 506 | else |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int smd_stream_write(smd_channel_t *ch, const void *_data, int len) |
| 511 | { |
| 512 | void *ptr; |
| 513 | const unsigned char *buf = _data; |
| 514 | unsigned xfer; |
| 515 | int orig_len = len; |
| 516 | |
| 517 | if (len < 0) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | while ((xfer = ch_write_buffer(ch, &ptr)) != 0) { |
| 521 | if (!ch_is_open(ch)) |
| 522 | break; |
| 523 | if (xfer > len) |
| 524 | xfer = len; |
| 525 | memcpy(ptr, buf, xfer); |
| 526 | ch_write_done(ch, xfer); |
| 527 | len -= xfer; |
| 528 | buf += xfer; |
| 529 | if (len == 0) |
| 530 | break; |
| 531 | } |
| 532 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 533 | ch->notify_other_cpu(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 534 | |
| 535 | return orig_len - len; |
| 536 | } |
| 537 | |
| 538 | static int smd_packet_write(smd_channel_t *ch, const void *_data, int len) |
| 539 | { |
| 540 | unsigned hdr[5]; |
| 541 | |
| 542 | if (len < 0) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE)) |
| 546 | return -ENOMEM; |
| 547 | |
| 548 | hdr[0] = len; |
| 549 | hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0; |
| 550 | |
| 551 | smd_stream_write(ch, hdr, sizeof(hdr)); |
| 552 | smd_stream_write(ch, _data, len); |
| 553 | |
| 554 | return len; |
| 555 | } |
| 556 | |
| 557 | static int smd_stream_read(smd_channel_t *ch, void *data, int len) |
| 558 | { |
| 559 | int r; |
| 560 | |
| 561 | if (len < 0) |
| 562 | return -EINVAL; |
| 563 | |
| 564 | r = ch_read(ch, data, len); |
| 565 | if (r > 0) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 566 | ch->notify_other_cpu(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 567 | |
| 568 | return r; |
| 569 | } |
| 570 | |
| 571 | static int smd_packet_read(smd_channel_t *ch, void *data, int len) |
| 572 | { |
| 573 | unsigned long flags; |
| 574 | int r; |
| 575 | |
| 576 | if (len < 0) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | if (len > ch->current_packet) |
| 580 | len = ch->current_packet; |
| 581 | |
| 582 | r = ch_read(ch, data, len); |
| 583 | if (r > 0) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 584 | ch->notify_other_cpu(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 585 | |
| 586 | spin_lock_irqsave(&smd_lock, flags); |
| 587 | ch->current_packet -= r; |
| 588 | update_packet_state(ch); |
| 589 | spin_unlock_irqrestore(&smd_lock, flags); |
| 590 | |
| 591 | return r; |
| 592 | } |
| 593 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 594 | static int smd_alloc_v2(struct smd_channel *ch) |
| 595 | { |
| 596 | struct smd_shared_v2 *shared2; |
| 597 | void *buffer; |
| 598 | unsigned buffer_sz; |
| 599 | |
| 600 | shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2)); |
| 601 | buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz); |
| 602 | |
| 603 | if (!buffer) |
| 604 | return -1; |
| 605 | |
| 606 | /* buffer must be a power-of-two size */ |
| 607 | if (buffer_sz & (buffer_sz - 1)) |
| 608 | return -1; |
| 609 | |
| 610 | buffer_sz /= 2; |
| 611 | ch->send = &shared2->ch0; |
| 612 | ch->recv = &shared2->ch1; |
| 613 | ch->send_data = buffer; |
| 614 | ch->recv_data = buffer + buffer_sz; |
| 615 | ch->fifo_size = buffer_sz; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int smd_alloc_v1(struct smd_channel *ch) |
| 620 | { |
| 621 | struct smd_shared_v1 *shared1; |
| 622 | shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1)); |
| 623 | if (!shared1) { |
| 624 | pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n); |
| 625 | return -1; |
| 626 | } |
| 627 | ch->send = &shared1->ch0; |
| 628 | ch->recv = &shared1->ch1; |
| 629 | ch->send_data = shared1->data0; |
| 630 | ch->recv_data = shared1->data1; |
| 631 | ch->fifo_size = SMD_BUF_SIZE; |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 636 | static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type) |
| 637 | { |
| 638 | struct smd_channel *ch; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 639 | |
| 640 | ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL); |
| 641 | if (ch == 0) { |
| 642 | pr_err("smd_alloc_channel() out of memory\n"); |
| 643 | return; |
| 644 | } |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 645 | ch->n = cid; |
| 646 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 647 | if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) { |
| 648 | kfree(ch); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | ch->fifo_mask = ch->fifo_size - 1; |
| 653 | ch->type = type; |
| 654 | |
| 655 | if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM) |
| 656 | ch->notify_other_cpu = notify_modem_smd; |
| 657 | else |
| 658 | ch->notify_other_cpu = notify_dsp_smd; |
| 659 | |
| 660 | if (smd_is_packet(cid, type)) { |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 661 | ch->read = smd_packet_read; |
| 662 | ch->write = smd_packet_write; |
| 663 | ch->read_avail = smd_packet_read_avail; |
| 664 | ch->write_avail = smd_packet_write_avail; |
| 665 | ch->update_state = update_packet_state; |
| 666 | } else { |
| 667 | ch->read = smd_stream_read; |
| 668 | ch->write = smd_stream_write; |
| 669 | ch->read_avail = smd_stream_read_avail; |
| 670 | ch->write_avail = smd_stream_write_avail; |
| 671 | ch->update_state = update_stream_state; |
| 672 | } |
| 673 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 674 | if ((type & 0xff) == 0) |
| 675 | memcpy(ch->name, "SMD_", 4); |
| 676 | else |
| 677 | memcpy(ch->name, "DSP_", 4); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 678 | memcpy(ch->name + 4, name, 20); |
| 679 | ch->name[23] = 0; |
| 680 | ch->pdev.name = ch->name; |
| 681 | ch->pdev.id = -1; |
| 682 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 683 | pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n", |
| 684 | ch->n, ch->fifo_size, ch->name); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 685 | |
| 686 | mutex_lock(&smd_creation_mutex); |
| 687 | list_add(&ch->ch_list, &smd_ch_closed_list); |
| 688 | mutex_unlock(&smd_creation_mutex); |
| 689 | |
| 690 | platform_device_register(&ch->pdev); |
| 691 | } |
| 692 | |
| 693 | static void do_nothing_notify(void *priv, unsigned flags) |
| 694 | { |
| 695 | } |
| 696 | |
| 697 | struct smd_channel *smd_get_channel(const char *name) |
| 698 | { |
| 699 | struct smd_channel *ch; |
| 700 | |
| 701 | mutex_lock(&smd_creation_mutex); |
| 702 | list_for_each_entry(ch, &smd_ch_closed_list, ch_list) { |
| 703 | if (!strcmp(name, ch->name)) { |
| 704 | list_del(&ch->ch_list); |
| 705 | mutex_unlock(&smd_creation_mutex); |
| 706 | return ch; |
| 707 | } |
| 708 | } |
| 709 | mutex_unlock(&smd_creation_mutex); |
| 710 | |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | int smd_open(const char *name, smd_channel_t **_ch, |
| 715 | void *priv, void (*notify)(void *, unsigned)) |
| 716 | { |
| 717 | struct smd_channel *ch; |
| 718 | unsigned long flags; |
| 719 | |
| 720 | if (smd_initialized == 0) { |
| 721 | pr_info("smd_open() before smd_init()\n"); |
| 722 | return -ENODEV; |
| 723 | } |
| 724 | |
| 725 | ch = smd_get_channel(name); |
| 726 | if (!ch) |
| 727 | return -ENODEV; |
| 728 | |
| 729 | if (notify == 0) |
| 730 | notify = do_nothing_notify; |
| 731 | |
| 732 | ch->notify = notify; |
| 733 | ch->current_packet = 0; |
| 734 | ch->last_state = SMD_SS_CLOSED; |
| 735 | ch->priv = priv; |
| 736 | |
| 737 | *_ch = ch; |
| 738 | |
| 739 | spin_lock_irqsave(&smd_lock, flags); |
| 740 | list_add(&ch->ch_list, &smd_ch_list); |
| 741 | |
| 742 | /* If the remote side is CLOSING, we need to get it to |
| 743 | * move to OPENING (which we'll do by moving from CLOSED to |
| 744 | * OPENING) and then get it to move from OPENING to |
| 745 | * OPENED (by doing the same state change ourselves). |
| 746 | * |
| 747 | * Otherwise, it should be OPENING and we can move directly |
| 748 | * to OPENED so that it will follow. |
| 749 | */ |
| 750 | if (ch->recv->state == SMD_SS_CLOSING) { |
| 751 | ch->send->head = 0; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 752 | ch_set_state(ch, SMD_SS_OPENING); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 753 | } else { |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 754 | ch_set_state(ch, SMD_SS_OPENED); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 755 | } |
| 756 | spin_unlock_irqrestore(&smd_lock, flags); |
| 757 | smd_kick(ch); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | int smd_close(smd_channel_t *ch) |
| 763 | { |
| 764 | unsigned long flags; |
| 765 | |
| 766 | pr_info("smd_close(%p)\n", ch); |
| 767 | |
| 768 | if (ch == 0) |
| 769 | return -1; |
| 770 | |
| 771 | spin_lock_irqsave(&smd_lock, flags); |
| 772 | ch->notify = do_nothing_notify; |
| 773 | list_del(&ch->ch_list); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 774 | ch_set_state(ch, SMD_SS_CLOSED); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 775 | spin_unlock_irqrestore(&smd_lock, flags); |
| 776 | |
| 777 | mutex_lock(&smd_creation_mutex); |
| 778 | list_add(&ch->ch_list, &smd_ch_closed_list); |
| 779 | mutex_unlock(&smd_creation_mutex); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | int smd_read(smd_channel_t *ch, void *data, int len) |
| 785 | { |
| 786 | return ch->read(ch, data, len); |
| 787 | } |
| 788 | |
| 789 | int smd_write(smd_channel_t *ch, const void *data, int len) |
| 790 | { |
| 791 | return ch->write(ch, data, len); |
| 792 | } |
| 793 | |
| 794 | int smd_read_avail(smd_channel_t *ch) |
| 795 | { |
| 796 | return ch->read_avail(ch); |
| 797 | } |
| 798 | |
| 799 | int smd_write_avail(smd_channel_t *ch) |
| 800 | { |
| 801 | return ch->write_avail(ch); |
| 802 | } |
| 803 | |
| 804 | int smd_wait_until_readable(smd_channel_t *ch, int bytes) |
| 805 | { |
| 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | int smd_wait_until_writable(smd_channel_t *ch, int bytes) |
| 810 | { |
| 811 | return -1; |
| 812 | } |
| 813 | |
| 814 | int smd_cur_packet_size(smd_channel_t *ch) |
| 815 | { |
| 816 | return ch->current_packet; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /* ------------------------------------------------------------------------- */ |
| 821 | |
| 822 | void *smem_alloc(unsigned id, unsigned size) |
| 823 | { |
| 824 | return smem_find(id, size); |
| 825 | } |
| 826 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 827 | void *smem_item(unsigned id, unsigned *size) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 828 | { |
| 829 | struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE; |
| 830 | struct smem_heap_entry *toc = shared->heap_toc; |
| 831 | |
| 832 | if (id >= SMEM_NUM_ITEMS) |
| 833 | return 0; |
| 834 | |
| 835 | if (toc[id].allocated) { |
| 836 | *size = toc[id].size; |
| 837 | return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 838 | } else { |
| 839 | *size = 0; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | void *smem_find(unsigned id, unsigned size_in) |
| 846 | { |
| 847 | unsigned size; |
| 848 | void *ptr; |
| 849 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 850 | ptr = smem_item(id, &size); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 851 | if (!ptr) |
| 852 | return 0; |
| 853 | |
| 854 | size_in = ALIGN(size_in, 8); |
| 855 | if (size_in != size) { |
| 856 | pr_err("smem_find(%d, %d): wrong size %d\n", |
| 857 | id, size_in, size); |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | return ptr; |
| 862 | } |
| 863 | |
| 864 | static irqreturn_t smsm_irq_handler(int irq, void *data) |
| 865 | { |
| 866 | unsigned long flags; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 867 | unsigned apps, modm; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 868 | |
| 869 | spin_lock_irqsave(&smem_lock, flags); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 870 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 871 | apps = raw_smsm_get_state(SMSM_STATE_APPS); |
| 872 | modm = raw_smsm_get_state(SMSM_STATE_MODEM); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 873 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 874 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) |
| 875 | pr_info("<SM %08x %08x>\n", apps, modm); |
| 876 | if (modm & SMSM_RESET) { |
| 877 | handle_modem_crash(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 878 | } |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 879 | do_smd_probe(); |
| 880 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 881 | spin_unlock_irqrestore(&smem_lock, flags); |
| 882 | return IRQ_HANDLED; |
| 883 | } |
| 884 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 885 | int smsm_change_state(enum smsm_state_item item, |
| 886 | uint32_t clear_mask, uint32_t set_mask) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 887 | { |
| 888 | unsigned long flags; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 889 | unsigned state; |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 890 | unsigned addr = smd_info.state + item * 4; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 891 | |
| 892 | if (!smd_info.ready) |
| 893 | return -EIO; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 894 | |
| 895 | spin_lock_irqsave(&smem_lock, flags); |
| 896 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 897 | if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 898 | handle_modem_crash(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 899 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 900 | state = (readl(addr) & ~clear_mask) | set_mask; |
| 901 | writel(state, addr); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 902 | |
| 903 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 904 | pr_info("smsm_change_state %d %x\n", item, state); |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 905 | notify_other_smsm(); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 906 | |
| 907 | spin_unlock_irqrestore(&smem_lock, flags); |
| 908 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 912 | uint32_t smsm_get_state(enum smsm_state_item item) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 913 | { |
| 914 | unsigned long flags; |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 915 | uint32_t rv; |
| 916 | |
| 917 | spin_lock_irqsave(&smem_lock, flags); |
| 918 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 919 | rv = readl(smd_info.state + item * 4); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 920 | |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 921 | if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET)) |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 922 | handle_modem_crash(); |
| 923 | |
| 924 | spin_unlock_irqrestore(&smem_lock, flags); |
| 925 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 926 | return rv; |
| 927 | } |
| 928 | |
Arve Hjønnevåg | ec9d3d1 | 2009-06-16 14:48:21 -0700 | [diff] [blame] | 929 | #ifdef CONFIG_ARCH_MSM_SCORPION |
| 930 | |
| 931 | int smsm_set_sleep_duration(uint32_t delay) |
| 932 | { |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 933 | struct msm_dem_slave_data *ptr; |
| 934 | |
| 935 | ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr)); |
Arve Hjønnevåg | ec9d3d1 | 2009-06-16 14:48:21 -0700 | [diff] [blame] | 936 | if (ptr == NULL) { |
| 937 | pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n"); |
| 938 | return -EIO; |
| 939 | } |
| 940 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) |
| 941 | pr_info("smsm_set_sleep_duration %d -> %d\n", |
| 942 | ptr->sleep_time, delay); |
| 943 | ptr->sleep_time = delay; |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | #else |
| 948 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 949 | int smsm_set_sleep_duration(uint32_t delay) |
| 950 | { |
| 951 | uint32_t *ptr; |
| 952 | |
Brian Swetland | 03e00cd | 2009-07-01 17:58:37 -0700 | [diff] [blame^] | 953 | ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr)); |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 954 | if (ptr == NULL) { |
| 955 | pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n"); |
| 956 | return -EIO; |
| 957 | } |
| 958 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) |
| 959 | pr_info("smsm_set_sleep_duration %d -> %d\n", |
| 960 | *ptr, delay); |
| 961 | *ptr = delay; |
| 962 | return 0; |
| 963 | } |
| 964 | |
Arve Hjønnevåg | ec9d3d1 | 2009-06-16 14:48:21 -0700 | [diff] [blame] | 965 | #endif |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 966 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 967 | int smd_core_init(void) |
| 968 | { |
| 969 | int r; |
| 970 | pr_info("smd_core_init()\n"); |
| 971 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 972 | /* wait for essential items to be initialized */ |
| 973 | for (;;) { |
| 974 | unsigned size; |
| 975 | void *state; |
| 976 | state = smem_item(SMEM_SMSM_SHARED_STATE, &size); |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 977 | if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) { |
| 978 | smd_info.state = (unsigned)state; |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | smd_info.ready = 1; |
| 984 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 985 | r = request_irq(INT_A9_M2A_0, smd_irq_handler, |
| 986 | IRQF_TRIGGER_RISING, "smd_dev", 0); |
| 987 | if (r < 0) |
| 988 | return r; |
| 989 | r = enable_irq_wake(INT_A9_M2A_0); |
| 990 | if (r < 0) |
| 991 | pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n"); |
| 992 | |
| 993 | r = request_irq(INT_A9_M2A_5, smsm_irq_handler, |
| 994 | IRQF_TRIGGER_RISING, "smsm_dev", 0); |
| 995 | if (r < 0) { |
| 996 | free_irq(INT_A9_M2A_0, 0); |
| 997 | return r; |
| 998 | } |
| 999 | r = enable_irq_wake(INT_A9_M2A_5); |
| 1000 | if (r < 0) |
| 1001 | pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n"); |
| 1002 | |
Brian Swetland | 5b0f5a3 | 2009-04-26 18:38:49 -0700 | [diff] [blame] | 1003 | /* check for any SMD channels that may already exist */ |
| 1004 | do_smd_probe(); |
| 1005 | |
| 1006 | /* indicate that we're up and running */ |
Arve Hjønnevåg | 2837941 | 2009-05-20 16:52:36 -0700 | [diff] [blame] | 1007 | smsm_change_state(SMSM_STATE_APPS, |
Arve Hjønnevåg | ec9d3d1 | 2009-06-16 14:48:21 -0700 | [diff] [blame] | 1008 | ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN); |
| 1009 | #ifdef CONFIG_ARCH_MSM_SCORPION |
| 1010 | smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0); |
| 1011 | #endif |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 1012 | |
| 1013 | pr_info("smd_core_init() done\n"); |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 1018 | static int __init msm_smd_probe(struct platform_device *pdev) |
| 1019 | { |
| 1020 | pr_info("smd_init()\n"); |
| 1021 | |
| 1022 | INIT_WORK(&probe_work, smd_channel_probe_worker); |
| 1023 | |
| 1024 | if (smd_core_init()) { |
| 1025 | pr_err("smd_core_init() failed\n"); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | do_smd_probe(); |
| 1030 | |
| 1031 | msm_check_for_modem_crash = check_for_modem_crash; |
| 1032 | |
Brian Swetland | 2eb44eb | 2008-09-29 16:00:48 -0700 | [diff] [blame] | 1033 | smd_initialized = 1; |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | static struct platform_driver msm_smd_driver = { |
| 1039 | .probe = msm_smd_probe, |
| 1040 | .driver = { |
| 1041 | .name = MODULE_NAME, |
| 1042 | .owner = THIS_MODULE, |
| 1043 | }, |
| 1044 | }; |
| 1045 | |
| 1046 | static int __init msm_smd_init(void) |
| 1047 | { |
| 1048 | return platform_driver_register(&msm_smd_driver); |
| 1049 | } |
| 1050 | |
| 1051 | module_init(msm_smd_init); |
| 1052 | |
| 1053 | MODULE_DESCRIPTION("MSM Shared Memory Core"); |
| 1054 | MODULE_AUTHOR("Brian Swetland <swetland@google.com>"); |
| 1055 | MODULE_LICENSE("GPL"); |