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