1 /* arch/arm/mach-msm/smd.c
3 * Copyright (C) 2007 Google, Inc.
4 * Author: Brian Swetland <swetland@google.com>
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.
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.
17 #include <linux/platform_device.h>
18 #include <linux/module.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>
31 #include <mach/msm_smd.h>
32 #include <mach/msm_iomap.h>
33 #include <mach/system.h>
35 #include "smd_private.h"
36 #include "../../../../arch/arm/mach-msm/proc_comm.h"
38 void (*msm_hw_reset_hook
)(void);
40 #define MODULE_NAME "msm_smd"
43 MSM_SMD_DEBUG
= 1U << 0,
44 MSM_SMSM_DEBUG
= 1U << 0,
47 static int msm_smd_debug_mask
;
49 module_param_named(debug_mask
, msm_smd_debug_mask
,
50 int, S_IRUGO
| S_IWUSR
| S_IWGRP
);
52 void *smem_find(unsigned id
, unsigned size
);
53 static void smd_diag(void);
55 static unsigned last_heap_free
= 0xffffffff;
57 #define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
59 static inline void notify_other_smsm(void)
61 writel(1, MSM_A2M_INT(5));
64 static inline void notify_other_smd(void)
66 writel(1, MSM_A2M_INT(0));
69 static void smd_diag(void)
73 x
= smem_find(ID_DIAG_ERR_MSG
, SZ_DIAG_ERR_MSG
);
75 x
[SZ_DIAG_ERR_MSG
- 1] = 0;
76 pr_info("smem: DIAG '%s'\n", x
);
80 /* call when SMSM_RESET flag is set in the A9's smsm_state */
81 static void handle_modem_crash(void)
83 pr_err("ARM9 has CRASHED\n");
86 /* hard reboot if possible */
87 if (msm_hw_reset_hook
)
90 /* in this case the modem or watchdog should reboot us */
95 extern int (*msm_check_for_modem_crash
)(void);
97 static int check_for_modem_crash(void)
99 struct smsm_shared
*smsm
;
101 smsm
= smem_find(ID_SHARED_STATE
, 2 * sizeof(struct smsm_shared
));
103 /* if the modem's not ready yet, we have to hope for the best */
107 if (smsm
[1].state
& SMSM_RESET
) {
108 handle_modem_crash();
115 #define SMD_SS_CLOSED 0x00000000
116 #define SMD_SS_OPENING 0x00000001
117 #define SMD_SS_OPENED 0x00000002
118 #define SMD_SS_FLUSHING 0x00000003
119 #define SMD_SS_CLOSING 0x00000004
120 #define SMD_SS_RESET 0x00000005
121 #define SMD_SS_RESET_OPENING 0x00000006
123 #define SMD_BUF_SIZE 8192
124 #define SMD_CHANNELS 64
126 #define SMD_HEADER_SIZE 20
129 /* the spinlock is used to synchronize between the
130 ** irq handler and code that mutates the channel
131 ** list or fiddles with channel state
133 static DEFINE_SPINLOCK(smd_lock
);
134 static DEFINE_SPINLOCK(smem_lock
);
136 /* the mutex is used during open() and close()
137 ** operations to avoid races while creating or
138 ** destroying smd_channel structures
140 static DEFINE_MUTEX(smd_creation_mutex
);
142 static int smd_initialized
;
144 struct smd_alloc_elm
{
151 struct smd_half_channel
{
159 unsigned char fSTATE
;
160 unsigned char fUNUSED
;
163 unsigned char data
[SMD_BUF_SIZE
];
167 struct smd_half_channel ch0
;
168 struct smd_half_channel ch1
;
172 volatile struct smd_half_channel
*send
;
173 volatile struct smd_half_channel
*recv
;
174 struct list_head ch_list
;
176 unsigned current_packet
;
179 void (*notify
)(void *priv
, unsigned flags
);
181 int (*read
)(smd_channel_t
*ch
, void *data
, int len
);
182 int (*write
)(smd_channel_t
*ch
, const void *data
, int len
);
183 int (*read_avail
)(smd_channel_t
*ch
);
184 int (*write_avail
)(smd_channel_t
*ch
);
186 void (*update_state
)(smd_channel_t
*ch
);
190 struct platform_device pdev
;
193 static LIST_HEAD(smd_ch_closed_list
);
194 static LIST_HEAD(smd_ch_list
);
196 static unsigned char smd_ch_allocated
[64];
197 static struct work_struct probe_work
;
199 static void smd_alloc_channel(const char *name
, uint32_t cid
, uint32_t type
);
201 static void smd_channel_probe_worker(struct work_struct
*work
)
203 struct smd_alloc_elm
*shared
;
206 shared
= smem_find(ID_CH_ALLOC_TBL
, sizeof(*shared
) * 64);
208 for (n
= 0; n
< 64; n
++) {
209 if (smd_ch_allocated
[n
])
211 if (!shared
[n
].ref_count
)
213 if (!shared
[n
].name
[0])
215 smd_alloc_channel(shared
[n
].name
,
218 smd_ch_allocated
[n
] = 1;
222 static char *chstate(unsigned n
)
231 case SMD_SS_FLUSHING
:
237 case SMD_SS_RESET_OPENING
:
244 /* how many bytes are available for reading */
245 static int smd_stream_read_avail(struct smd_channel
*ch
)
247 return (ch
->recv
->head
- ch
->recv
->tail
) & (SMD_BUF_SIZE
- 1);
250 /* how many bytes we are free to write */
251 static int smd_stream_write_avail(struct smd_channel
*ch
)
253 return (SMD_BUF_SIZE
- 1) -
254 ((ch
->send
->head
- ch
->send
->tail
) & (SMD_BUF_SIZE
- 1));
257 static int smd_packet_read_avail(struct smd_channel
*ch
)
259 if (ch
->current_packet
) {
260 int n
= smd_stream_read_avail(ch
);
261 if (n
> ch
->current_packet
)
262 n
= ch
->current_packet
;
269 static int smd_packet_write_avail(struct smd_channel
*ch
)
271 int n
= smd_stream_write_avail(ch
);
272 return n
> SMD_HEADER_SIZE
? n
- SMD_HEADER_SIZE
: 0;
275 static int ch_is_open(struct smd_channel
*ch
)
277 return (ch
->recv
->state
== SMD_SS_OPENED
) &&
278 (ch
->send
->state
== SMD_SS_OPENED
);
281 /* provide a pointer and length to readable data in the fifo */
282 static unsigned ch_read_buffer(struct smd_channel
*ch
, void **ptr
)
284 unsigned head
= ch
->recv
->head
;
285 unsigned tail
= ch
->recv
->tail
;
286 *ptr
= (void *) (ch
->recv
->data
+ tail
);
291 return SMD_BUF_SIZE
- tail
;
294 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
295 static void ch_read_done(struct smd_channel
*ch
, unsigned count
)
297 BUG_ON(count
> smd_stream_read_avail(ch
));
298 ch
->recv
->tail
= (ch
->recv
->tail
+ count
) & (SMD_BUF_SIZE
- 1);
302 /* basic read interface to ch_read_{buffer,done} used
303 ** by smd_*_read() and update_packet_state()
304 ** will read-and-discard if the _data pointer is null
306 static int ch_read(struct smd_channel
*ch
, void *_data
, int len
)
310 unsigned char *data
= _data
;
314 n
= ch_read_buffer(ch
, &ptr
);
321 memcpy(data
, ptr
, n
);
328 return orig_len
- len
;
331 static void update_stream_state(struct smd_channel
*ch
)
333 /* streams have no special state requiring updating */
336 static void update_packet_state(struct smd_channel
*ch
)
341 /* can't do anything if we're in the middle of a packet */
342 if (ch
->current_packet
!= 0)
345 /* don't bother unless we can get the full header */
346 if (smd_stream_read_avail(ch
) < SMD_HEADER_SIZE
)
349 r
= ch_read(ch
, hdr
, SMD_HEADER_SIZE
);
350 BUG_ON(r
!= SMD_HEADER_SIZE
);
352 ch
->current_packet
= hdr
[0];
355 /* provide a pointer and length to next free space in the fifo */
356 static unsigned ch_write_buffer(struct smd_channel
*ch
, void **ptr
)
358 unsigned head
= ch
->send
->head
;
359 unsigned tail
= ch
->send
->tail
;
360 *ptr
= (void *) (ch
->send
->data
+ head
);
363 return tail
- head
- 1;
366 return SMD_BUF_SIZE
- head
- 1;
368 return SMD_BUF_SIZE
- head
;
372 /* advace the fifo write pointer after freespace
373 * from ch_write_buffer is filled
375 static void ch_write_done(struct smd_channel
*ch
, unsigned count
)
377 BUG_ON(count
> smd_stream_write_avail(ch
));
378 ch
->send
->head
= (ch
->send
->head
+ count
) & (SMD_BUF_SIZE
- 1);
382 static void hc_set_state(volatile struct smd_half_channel
*hc
, unsigned n
)
384 if (n
== SMD_SS_OPENED
) {
398 static void do_smd_probe(void)
400 struct smem_shared
*shared
= (void *) MSM_SHARED_RAM_BASE
;
401 if (shared
->heap_info
.free_offset
!= last_heap_free
) {
402 last_heap_free
= shared
->heap_info
.free_offset
;
403 schedule_work(&probe_work
);
407 static void smd_state_change(struct smd_channel
*ch
,
408 unsigned last
, unsigned next
)
410 ch
->last_state
= next
;
412 pr_info("SMD: ch %d %s -> %s\n", ch
->n
,
413 chstate(last
), chstate(next
));
419 if (ch
->send
->state
!= SMD_SS_OPENED
)
420 hc_set_state(ch
->send
, SMD_SS_OPENED
);
421 ch
->notify(ch
->priv
, SMD_EVENT_OPEN
);
423 case SMD_SS_FLUSHING
:
425 /* we should force them to close? */
427 ch
->notify(ch
->priv
, SMD_EVENT_CLOSE
);
431 static irqreturn_t
smd_irq_handler(int irq
, void *data
)
434 struct smd_channel
*ch
;
439 spin_lock_irqsave(&smd_lock
, flags
);
440 list_for_each_entry(ch
, &smd_ch_list
, ch_list
) {
442 if (ch_is_open(ch
)) {
443 if (ch
->recv
->fHEAD
) {
448 if (ch
->recv
->fTAIL
) {
453 if (ch
->recv
->fSTATE
) {
454 ch
->recv
->fSTATE
= 0;
459 tmp
= ch
->recv
->state
;
460 if (tmp
!= ch
->last_state
)
461 smd_state_change(ch
, ch
->last_state
, tmp
);
463 ch
->update_state(ch
);
464 ch
->notify(ch
->priv
, SMD_EVENT_DATA
);
469 spin_unlock_irqrestore(&smd_lock
, flags
);
474 static void smd_fake_irq_handler(unsigned long arg
)
476 smd_irq_handler(0, NULL
);
479 static DECLARE_TASKLET(smd_fake_irq_tasklet
, smd_fake_irq_handler
, 0);
481 void smd_sleep_exit(void)
484 struct smd_channel
*ch
;
488 spin_lock_irqsave(&smd_lock
, flags
);
489 list_for_each_entry(ch
, &smd_ch_list
, ch_list
) {
490 if (ch_is_open(ch
)) {
491 if (ch
->recv
->fHEAD
) {
492 if (msm_smd_debug_mask
& MSM_SMD_DEBUG
)
493 pr_info("smd_sleep_exit ch %d fHEAD "
495 ch
->n
, ch
->recv
->fHEAD
,
496 ch
->recv
->head
, ch
->recv
->tail
);
500 if (ch
->recv
->fTAIL
) {
501 if (msm_smd_debug_mask
& MSM_SMD_DEBUG
)
502 pr_info("smd_sleep_exit ch %d fTAIL "
504 ch
->n
, ch
->recv
->fTAIL
,
505 ch
->send
->head
, ch
->send
->tail
);
509 if (ch
->recv
->fSTATE
) {
510 if (msm_smd_debug_mask
& MSM_SMD_DEBUG
)
511 pr_info("smd_sleep_exit ch %d fSTATE %x"
512 "\n", ch
->n
, ch
->recv
->fSTATE
);
516 tmp
= ch
->recv
->state
;
517 if (tmp
!= ch
->last_state
) {
518 if (msm_smd_debug_mask
& MSM_SMD_DEBUG
)
519 pr_info("smd_sleep_exit ch %d "
521 ch
->n
, tmp
, ch
->last_state
);
527 spin_unlock_irqrestore(&smd_lock
, flags
);
530 if (msm_smd_debug_mask
& MSM_SMD_DEBUG
)
531 pr_info("smd_sleep_exit need interrupt\n");
532 tasklet_schedule(&smd_fake_irq_tasklet
);
537 void smd_kick(smd_channel_t
*ch
)
542 spin_lock_irqsave(&smd_lock
, flags
);
543 ch
->update_state(ch
);
544 tmp
= ch
->recv
->state
;
545 if (tmp
!= ch
->last_state
) {
546 ch
->last_state
= tmp
;
547 if (tmp
== SMD_SS_OPENED
)
548 ch
->notify(ch
->priv
, SMD_EVENT_OPEN
);
550 ch
->notify(ch
->priv
, SMD_EVENT_CLOSE
);
552 ch
->notify(ch
->priv
, SMD_EVENT_DATA
);
554 spin_unlock_irqrestore(&smd_lock
, flags
);
557 static int smd_is_packet(int chn
)
559 if ((chn
> 4) || (chn
== 1))
565 static int smd_stream_write(smd_channel_t
*ch
, const void *_data
, int len
)
568 const unsigned char *buf
= _data
;
575 while ((xfer
= ch_write_buffer(ch
, &ptr
)) != 0) {
580 memcpy(ptr
, buf
, xfer
);
581 ch_write_done(ch
, xfer
);
590 return orig_len
- len
;
593 static int smd_packet_write(smd_channel_t
*ch
, const void *_data
, int len
)
600 if (smd_stream_write_avail(ch
) < (len
+ SMD_HEADER_SIZE
))
604 hdr
[1] = hdr
[2] = hdr
[3] = hdr
[4] = 0;
606 smd_stream_write(ch
, hdr
, sizeof(hdr
));
607 smd_stream_write(ch
, _data
, len
);
612 static int smd_stream_read(smd_channel_t
*ch
, void *data
, int len
)
619 r
= ch_read(ch
, data
, len
);
626 static int smd_packet_read(smd_channel_t
*ch
, void *data
, int len
)
634 if (len
> ch
->current_packet
)
635 len
= ch
->current_packet
;
637 r
= ch_read(ch
, data
, len
);
641 spin_lock_irqsave(&smd_lock
, flags
);
642 ch
->current_packet
-= r
;
643 update_packet_state(ch
);
644 spin_unlock_irqrestore(&smd_lock
, flags
);
649 static void smd_alloc_channel(const char *name
, uint32_t cid
, uint32_t type
)
651 struct smd_channel
*ch
;
652 struct smd_shared
*shared
;
654 shared
= smem_alloc(ID_SMD_CHANNELS
+ cid
, sizeof(*shared
));
656 pr_err("smd_alloc_channel() cid %d does not exist\n", cid
);
660 ch
= kzalloc(sizeof(struct smd_channel
), GFP_KERNEL
);
662 pr_err("smd_alloc_channel() out of memory\n");
666 ch
->send
= &shared
->ch0
;
667 ch
->recv
= &shared
->ch1
;
670 if (smd_is_packet(cid
)) {
671 ch
->read
= smd_packet_read
;
672 ch
->write
= smd_packet_write
;
673 ch
->read_avail
= smd_packet_read_avail
;
674 ch
->write_avail
= smd_packet_write_avail
;
675 ch
->update_state
= update_packet_state
;
677 ch
->read
= smd_stream_read
;
678 ch
->write
= smd_stream_write
;
679 ch
->read_avail
= smd_stream_read_avail
;
680 ch
->write_avail
= smd_stream_write_avail
;
681 ch
->update_state
= update_stream_state
;
684 memcpy(ch
->name
, "SMD_", 4);
685 memcpy(ch
->name
+ 4, name
, 20);
687 ch
->pdev
.name
= ch
->name
;
690 pr_info("smd_alloc_channel() '%s' cid=%d, shared=%p\n",
691 ch
->name
, ch
->n
, shared
);
693 mutex_lock(&smd_creation_mutex
);
694 list_add(&ch
->ch_list
, &smd_ch_closed_list
);
695 mutex_unlock(&smd_creation_mutex
);
697 platform_device_register(&ch
->pdev
);
700 static void do_nothing_notify(void *priv
, unsigned flags
)
704 struct smd_channel
*smd_get_channel(const char *name
)
706 struct smd_channel
*ch
;
708 mutex_lock(&smd_creation_mutex
);
709 list_for_each_entry(ch
, &smd_ch_closed_list
, ch_list
) {
710 if (!strcmp(name
, ch
->name
)) {
711 list_del(&ch
->ch_list
);
712 mutex_unlock(&smd_creation_mutex
);
716 mutex_unlock(&smd_creation_mutex
);
721 int smd_open(const char *name
, smd_channel_t
**_ch
,
722 void *priv
, void (*notify
)(void *, unsigned))
724 struct smd_channel
*ch
;
727 if (smd_initialized
== 0) {
728 pr_info("smd_open() before smd_init()\n");
732 ch
= smd_get_channel(name
);
737 notify
= do_nothing_notify
;
740 ch
->current_packet
= 0;
741 ch
->last_state
= SMD_SS_CLOSED
;
746 spin_lock_irqsave(&smd_lock
, flags
);
747 list_add(&ch
->ch_list
, &smd_ch_list
);
749 /* If the remote side is CLOSING, we need to get it to
750 * move to OPENING (which we'll do by moving from CLOSED to
751 * OPENING) and then get it to move from OPENING to
752 * OPENED (by doing the same state change ourselves).
754 * Otherwise, it should be OPENING and we can move directly
755 * to OPENED so that it will follow.
757 if (ch
->recv
->state
== SMD_SS_CLOSING
) {
759 hc_set_state(ch
->send
, SMD_SS_OPENING
);
761 hc_set_state(ch
->send
, SMD_SS_OPENED
);
763 spin_unlock_irqrestore(&smd_lock
, flags
);
769 int smd_close(smd_channel_t
*ch
)
773 pr_info("smd_close(%p)\n", ch
);
778 spin_lock_irqsave(&smd_lock
, flags
);
779 ch
->notify
= do_nothing_notify
;
780 list_del(&ch
->ch_list
);
781 hc_set_state(ch
->send
, SMD_SS_CLOSED
);
782 spin_unlock_irqrestore(&smd_lock
, flags
);
784 mutex_lock(&smd_creation_mutex
);
785 list_add(&ch
->ch_list
, &smd_ch_closed_list
);
786 mutex_unlock(&smd_creation_mutex
);
791 int smd_read(smd_channel_t
*ch
, void *data
, int len
)
793 return ch
->read(ch
, data
, len
);
796 int smd_write(smd_channel_t
*ch
, const void *data
, int len
)
798 return ch
->write(ch
, data
, len
);
801 int smd_read_avail(smd_channel_t
*ch
)
803 return ch
->read_avail(ch
);
806 int smd_write_avail(smd_channel_t
*ch
)
808 return ch
->write_avail(ch
);
811 int smd_wait_until_readable(smd_channel_t
*ch
, int bytes
)
816 int smd_wait_until_writable(smd_channel_t
*ch
, int bytes
)
821 int smd_cur_packet_size(smd_channel_t
*ch
)
823 return ch
->current_packet
;
827 /* ------------------------------------------------------------------------- */
829 void *smem_alloc(unsigned id
, unsigned size
)
831 return smem_find(id
, size
);
834 static void *_smem_find(unsigned id
, unsigned *size
)
836 struct smem_shared
*shared
= (void *) MSM_SHARED_RAM_BASE
;
837 struct smem_heap_entry
*toc
= shared
->heap_toc
;
839 if (id
>= SMEM_NUM_ITEMS
)
842 if (toc
[id
].allocated
) {
843 *size
= toc
[id
].size
;
844 return (void *) (MSM_SHARED_RAM_BASE
+ toc
[id
].offset
);
850 void *smem_find(unsigned id
, unsigned size_in
)
855 ptr
= _smem_find(id
, &size
);
859 size_in
= ALIGN(size_in
, 8);
860 if (size_in
!= size
) {
861 pr_err("smem_find(%d, %d): wrong size %d\n",
869 static irqreturn_t
smsm_irq_handler(int irq
, void *data
)
872 struct smsm_shared
*smsm
;
874 spin_lock_irqsave(&smem_lock
, flags
);
875 smsm
= smem_alloc(ID_SHARED_STATE
,
876 2 * sizeof(struct smsm_shared
));
879 pr_info("<SM NO STATE>\n");
881 unsigned apps
= smsm
[0].state
;
882 unsigned modm
= smsm
[1].state
;
884 if (msm_smd_debug_mask
& MSM_SMSM_DEBUG
)
885 pr_info("<SM %08x %08x>\n", apps
, modm
);
886 if (modm
& SMSM_RESET
) {
887 handle_modem_crash();
890 if (modm
& SMSM_SMDINIT
)
891 apps
|= SMSM_SMDINIT
;
892 if (modm
& SMSM_RPCINIT
)
893 apps
|= SMSM_RPCINIT
;
896 if (smsm
[0].state
!= apps
) {
897 if (msm_smd_debug_mask
& MSM_SMSM_DEBUG
)
898 pr_info("<SM %08x NOTIFY>\n", apps
);
899 smsm
[0].state
= apps
;
904 spin_unlock_irqrestore(&smem_lock
, flags
);
908 int smsm_change_state(uint32_t clear_mask
, uint32_t set_mask
)
911 struct smsm_shared
*smsm
;
913 spin_lock_irqsave(&smem_lock
, flags
);
915 smsm
= smem_alloc(ID_SHARED_STATE
,
916 2 * sizeof(struct smsm_shared
));
919 if (smsm
[1].state
& SMSM_RESET
)
920 handle_modem_crash();
921 smsm
[0].state
= (smsm
[0].state
& ~clear_mask
) | set_mask
;
922 if (msm_smd_debug_mask
& MSM_SMSM_DEBUG
)
923 pr_info("smsm_change_state %x\n",
928 spin_unlock_irqrestore(&smem_lock
, flags
);
931 pr_err("smsm_change_state <SM NO STATE>\n");
937 uint32_t smsm_get_state(void)
940 struct smsm_shared
*smsm
;
943 spin_lock_irqsave(&smem_lock
, flags
);
945 smsm
= smem_alloc(ID_SHARED_STATE
,
946 2 * sizeof(struct smsm_shared
));
954 handle_modem_crash();
956 spin_unlock_irqrestore(&smem_lock
, flags
);
959 pr_err("smsm_get_state <SM NO STATE>\n");
963 int smsm_set_sleep_duration(uint32_t delay
)
967 ptr
= smem_alloc(SMEM_SMSM_SLEEP_DELAY
, sizeof(*ptr
));
969 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
972 if (msm_smd_debug_mask
& MSM_SMSM_DEBUG
)
973 pr_info("smsm_set_sleep_duration %d -> %d\n",
979 int smsm_set_interrupt_info(struct smsm_interrupt_info
*info
)
981 struct smsm_interrupt_info
*ptr
;
983 ptr
= smem_alloc(SMEM_SMSM_INT_INFO
, sizeof(*ptr
));
985 pr_err("smsm_set_sleep_duration <SM NO INT_INFO>\n");
988 if (msm_smd_debug_mask
& MSM_SMSM_DEBUG
)
989 pr_info("smsm_set_interrupt_info %x %x -> %x %x\n",
990 ptr
->aArm_en_mask
, ptr
->aArm_interrupts_pending
,
991 info
->aArm_en_mask
, info
->aArm_interrupts_pending
);
996 #define MAX_NUM_SLEEP_CLIENTS 64
997 #define MAX_SLEEP_NAME_LEN 8
999 #define NUM_GPIO_INT_REGISTERS 6
1000 #define GPIO_SMEM_NUM_GROUPS 2
1001 #define GPIO_SMEM_MAX_PC_INTERRUPTS 8
1003 struct tramp_gpio_save
{
1004 unsigned int enable
;
1005 unsigned int detect
;
1006 unsigned int polarity
;
1009 struct tramp_gpio_smem
{
1010 uint16_t num_fired
[GPIO_SMEM_NUM_GROUPS
];
1011 uint16_t fired
[GPIO_SMEM_NUM_GROUPS
][GPIO_SMEM_MAX_PC_INTERRUPTS
];
1012 uint32_t enabled
[NUM_GPIO_INT_REGISTERS
];
1013 uint32_t detection
[NUM_GPIO_INT_REGISTERS
];
1014 uint32_t polarity
[NUM_GPIO_INT_REGISTERS
];
1018 void smsm_print_sleep_info(void)
1020 unsigned long flags
;
1022 struct tramp_gpio_smem
*gpio
;
1023 struct smsm_interrupt_info
*int_info
;
1026 spin_lock_irqsave(&smem_lock
, flags
);
1028 ptr
= smem_alloc(SMEM_SMSM_SLEEP_DELAY
, sizeof(*ptr
));
1030 pr_info("SMEM_SMSM_SLEEP_DELAY: %x\n", *ptr
);
1032 ptr
= smem_alloc(SMEM_SMSM_LIMIT_SLEEP
, sizeof(*ptr
));
1034 pr_info("SMEM_SMSM_LIMIT_SLEEP: %x\n", *ptr
);
1036 ptr
= smem_alloc(SMEM_SLEEP_POWER_COLLAPSE_DISABLED
, sizeof(*ptr
));
1038 pr_info("SMEM_SLEEP_POWER_COLLAPSE_DISABLED: %x\n", *ptr
);
1040 int_info
= smem_alloc(SMEM_SMSM_INT_INFO
, sizeof(*int_info
));
1042 pr_info("SMEM_SMSM_INT_INFO %x %x %x\n",
1043 int_info
->aArm_en_mask
,
1044 int_info
->aArm_interrupts_pending
,
1045 int_info
->aArm_wakeup_reason
);
1047 gpio
= smem_alloc(SMEM_GPIO_INT
, sizeof(*gpio
));
1050 for (i
= 0; i
< NUM_GPIO_INT_REGISTERS
; i
++)
1051 pr_info("SMEM_GPIO_INT: %d: e %x d %x p %x\n",
1052 i
, gpio
->enabled
[i
], gpio
->detection
[i
],
1055 for (i
= 0; i
< GPIO_SMEM_NUM_GROUPS
; i
++)
1056 pr_info("SMEM_GPIO_INT: %d: f %d: %d %d...\n",
1057 i
, gpio
->num_fired
[i
], gpio
->fired
[i
][0],
1061 spin_unlock_irqrestore(&smem_lock
, flags
);
1064 int smd_core_init(void)
1067 pr_info("smd_core_init()\n");
1069 r
= request_irq(INT_A9_M2A_0
, smd_irq_handler
,
1070 IRQF_TRIGGER_RISING
, "smd_dev", 0);
1073 r
= enable_irq_wake(INT_A9_M2A_0
);
1075 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1077 r
= request_irq(INT_A9_M2A_5
, smsm_irq_handler
,
1078 IRQF_TRIGGER_RISING
, "smsm_dev", 0);
1080 free_irq(INT_A9_M2A_0
, 0);
1083 r
= enable_irq_wake(INT_A9_M2A_5
);
1085 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1087 /* we may have missed a signal while booting -- fake
1088 * an interrupt to make sure we process any existing
1091 smsm_irq_handler(0, 0);
1093 pr_info("smd_core_init() done\n");
1098 #if defined(CONFIG_DEBUG_FS)
1100 static int dump_ch(char *buf
, int max
, int n
,
1101 struct smd_half_channel
*s
,
1102 struct smd_half_channel
*r
)
1107 " %8s(%04d/%04d) %c%c%c%c%c%c%c <->"
1108 " %8s(%04d/%04d) %c%c%c%c%c%c%c\n", n
,
1109 chstate(s
->state
), s
->tail
, s
->head
,
1110 s
->fDSR
? 'D' : 'd',
1111 s
->fCTS
? 'C' : 'c',
1114 s
->fHEAD
? 'W' : 'w',
1115 s
->fTAIL
? 'R' : 'r',
1116 s
->fSTATE
? 'S' : 's',
1117 chstate(r
->state
), r
->tail
, r
->head
,
1118 r
->fDSR
? 'D' : 'd',
1119 r
->fCTS
? 'R' : 'r',
1122 r
->fHEAD
? 'W' : 'w',
1123 r
->fTAIL
? 'R' : 'r',
1124 r
->fSTATE
? 'S' : 's'
1128 static int debug_read_stat(char *buf
, int max
)
1130 struct smsm_shared
*smsm
;
1134 smsm
= smem_find(ID_SHARED_STATE
,
1135 2 * sizeof(struct smsm_shared
));
1137 msg
= smem_find(ID_DIAG_ERR_MSG
, SZ_DIAG_ERR_MSG
);
1140 if (smsm
[1].state
& SMSM_RESET
)
1141 i
+= scnprintf(buf
+ i
, max
- i
,
1142 "smsm: ARM9 HAS CRASHED\n");
1143 i
+= scnprintf(buf
+ i
, max
- i
, "smsm: a9: %08x a11: %08x\n",
1144 smsm
[0].state
, smsm
[1].state
);
1146 i
+= scnprintf(buf
+ i
, max
- i
, "smsm: cannot find\n");
1149 msg
[SZ_DIAG_ERR_MSG
- 1] = 0;
1150 i
+= scnprintf(buf
+ i
, max
- i
, "diag: '%s'\n", msg
);
1155 static int debug_read_mem(char *buf
, int max
)
1158 struct smem_shared
*shared
= (void *) MSM_SHARED_RAM_BASE
;
1159 struct smem_heap_entry
*toc
= shared
->heap_toc
;
1162 i
+= scnprintf(buf
+ i
, max
- i
,
1163 "heap: init=%d free=%d remain=%d\n",
1164 shared
->heap_info
.initialized
,
1165 shared
->heap_info
.free_offset
,
1166 shared
->heap_info
.heap_remaining
);
1168 for (n
= 0; n
< SMEM_NUM_ITEMS
; n
++) {
1169 if (toc
[n
].allocated
== 0)
1171 i
+= scnprintf(buf
+ i
, max
- i
,
1172 "%04d: offsed %08x size %08x\n",
1173 n
, toc
[n
].offset
, toc
[n
].size
);
1178 static int debug_read_ch(char *buf
, int max
)
1180 struct smd_shared
*shared
;
1183 for (n
= 0; n
< SMD_CHANNELS
; n
++) {
1184 shared
= smem_find(ID_SMD_CHANNELS
+ n
,
1185 sizeof(struct smd_shared
));
1188 i
+= dump_ch(buf
+ i
, max
- i
, n
, &shared
->ch0
, &shared
->ch1
);
1194 static int debug_read_version(char *buf
, int max
)
1196 struct smem_shared
*shared
= (void *) MSM_SHARED_RAM_BASE
;
1197 unsigned version
= shared
->version
[VERSION_MODEM
];
1198 return sprintf(buf
, "%d.%d\n", version
>> 16, version
& 0xffff);
1201 static int debug_read_build_id(char *buf
, int max
)
1206 data
= _smem_find(SMEM_HW_SW_BUILD_ID
, &size
);
1212 memcpy(buf
, data
, size
);
1217 static int debug_read_alloc_tbl(char *buf
, int max
)
1219 struct smd_alloc_elm
*shared
;
1222 shared
= smem_find(ID_CH_ALLOC_TBL
, sizeof(*shared
) * 64);
1224 for (n
= 0; n
< 64; n
++) {
1225 if (shared
[n
].ref_count
== 0)
1227 i
+= scnprintf(buf
+ i
, max
- i
,
1228 "%03d: %20s cid=%02d ctype=%d ref_count=%d\n",
1229 n
, shared
[n
].name
, shared
[n
].cid
,
1230 shared
[n
].ctype
, shared
[n
].ref_count
);
1236 static int debug_boom(char *buf
, int max
)
1239 msm_proc_comm(PCOM_RESET_MODEM
, &ms
, 0);
1243 #define DEBUG_BUFMAX 4096
1244 static char debug_buffer
[DEBUG_BUFMAX
];
1246 static ssize_t
debug_read(struct file
*file
, char __user
*buf
,
1247 size_t count
, loff_t
*ppos
)
1249 int (*fill
)(char *buf
, int max
) = file
->private_data
;
1250 int bsize
= fill(debug_buffer
, DEBUG_BUFMAX
);
1251 return simple_read_from_buffer(buf
, count
, ppos
, debug_buffer
, bsize
);
1254 static int debug_open(struct inode
*inode
, struct file
*file
)
1256 file
->private_data
= inode
->i_private
;
1260 static const struct file_operations debug_ops
= {
1265 static void debug_create(const char *name
, mode_t mode
,
1266 struct dentry
*dent
,
1267 int (*fill
)(char *buf
, int max
))
1269 debugfs_create_file(name
, mode
, dent
, fill
, &debug_ops
);
1272 static void smd_debugfs_init(void)
1274 struct dentry
*dent
;
1276 dent
= debugfs_create_dir("smd", 0);
1280 debug_create("ch", 0444, dent
, debug_read_ch
);
1281 debug_create("stat", 0444, dent
, debug_read_stat
);
1282 debug_create("mem", 0444, dent
, debug_read_mem
);
1283 debug_create("version", 0444, dent
, debug_read_version
);
1284 debug_create("tbl", 0444, dent
, debug_read_alloc_tbl
);
1285 debug_create("build", 0444, dent
, debug_read_build_id
);
1286 debug_create("boom", 0444, dent
, debug_boom
);
1289 static void smd_debugfs_init(void) {}
1292 static int __init
msm_smd_probe(struct platform_device
*pdev
)
1294 pr_info("smd_init()\n");
1296 INIT_WORK(&probe_work
, smd_channel_probe_worker
);
1298 if (smd_core_init()) {
1299 pr_err("smd_core_init() failed\n");
1305 msm_check_for_modem_crash
= check_for_modem_crash
;
1308 smd_initialized
= 1;
1313 static struct platform_driver msm_smd_driver
= {
1314 .probe
= msm_smd_probe
,
1316 .name
= MODULE_NAME
,
1317 .owner
= THIS_MODULE
,
1321 static int __init
msm_smd_init(void)
1323 return platform_driver_register(&msm_smd_driver
);
1326 module_init(msm_smd_init
);
1328 MODULE_DESCRIPTION("MSM Shared Memory Core");
1329 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1330 MODULE_LICENSE("GPL");