Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / fwd / fwd.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <OTUS/OTUS_soc.h>
36 #include <cmnos_api.h>
37 #include <hif_api.h>
38 #include <Magpie_api.h>
39 #include <dma_lib.h>
40 #include <hif_pci.h>
41 #include <vbuf_api.h>
42
43 #include "fwd.h"
44
45 fwd_tgt_softc_t fwd_sc;
46
47 void    fwd_tgt_recv(VBUF *hdr_buf, VBUF *buf, void *ctx );
48 void    fwd_retbuf_handler(VBUF *buf, void *ServiceCtx);
49
50 hif_handle_t fwd_init() 
51  {
52     HIF_CALLBACK    hifconfig;
53     A_UINT32      size, res_headroom;
54     
55     hifconfig.send_buf_done = fwd_retbuf_handler;
56     hifconfig.recv_buf      = fwd_tgt_recv;
57     hifconfig.context       = &fwd_sc;
58     
59     res_headroom = HIF_get_reserved_headroom(NULL);
60     
61     size = sizeof(fwd_rsp_t) + res_headroom;
62
63     HIF_register_callback(NULL, &hifconfig);
64     
65     HIF_get_default_pipe(NULL, &fwd_sc.rx_pipe, &fwd_sc.tx_pipe);
66     
67     return NULL;
68  }
69  
70 void
71 fwd_retbuf_handler(VBUF *buf, void *ServiceCtx)
72 {
73  HIF_return_recv_buf(fwd_sc.hif_handle, fwd_sc.rx_pipe, buf);
74 }
75
76 a_status_t
77 fwd_tgt_process_last(A_UINT32 size, A_UINT32 cksum)
78 {
79     int i, checksum   = 0;
80     A_UINT32   *image = (A_UINT32 *)fwd_sc.addr;
81     
82     for (i = 0 ; i < size; i += 4, image++) 
83         checksum  =   checksum ^ *image;
84         
85     if (checksum == cksum)
86         return A_STATUS_OK;
87     else
88         return A_STATUS_FAILED;
89 }        
90         
91    
92
93 void 
94 fwd_tgt_recv(VBUF *hdr_buf, VBUF *buf, void *ctx )
95 {
96     volatile a_uint8_t *data;
97     A_UINT32 len, seglen, offset, i, more, eloc;
98     volatile A_UINT32 *image, *daddr;
99     volatile fwd_cmd_t *c;  
100     volatile fwd_rsp_t *r;
101     jmp_func funcptr;
102     a_status_t  status;
103     VDESC *desc = NULL;
104
105     data = buf->desc_list->buf_addr + buf->desc_list->data_offset;
106     seglen  = buf->desc_list->data_size;
107
108     c      =  (fwd_cmd_t *)data;
109     len    =  c->len;
110     offset =  c->offset;
111     more   =  c->more_data;
112     image  =  (A_UINT32 *)(c + 1);
113
114     if (offset == 0) {
115         fwd_sc.addr  = (A_UINT32)(*image);
116         image ++;
117     }
118
119     daddr = (A_UINT32 *)(fwd_sc.addr + offset);
120
121     if (!more) {
122         len -= 4;
123         }
124  
125     for (i = 0 ; i < len; i += 4) {
126         *daddr       =   *image; 
127         image ++;
128         daddr ++;
129     }
130     
131     desc = buf->desc_list; 
132      while(desc->next_desc != NULL)
133         desc = desc->next_desc;
134     desc->data_size -= seglen;
135     buf->buf_length -= seglen;
136
137     r = (fwd_rsp_t *)(desc->buf_addr + desc->data_offset + desc->data_size);
138     desc->data_size += sizeof(fwd_rsp_t); 
139     buf->buf_length += sizeof(fwd_rsp_t); 
140
141     r->offset = c->offset;
142
143     if (more) {
144         r->rsp = FWD_RSP_ACK;
145         goto done;
146     }
147     
148     status = fwd_tgt_process_last(offset + len,  *image);
149  
150     /* reach to the jump location */
151     image++;
152     eloc   =  *image;
153         
154     if (status == A_STATUS_OK)
155         r->rsp = FWD_RSP_SUCCESS;
156     else
157         r->rsp = FWD_RSP_FAILED;
158     
159      
160 done:       
161     HIF_send_buffer(fwd_sc.hif_handle, fwd_sc.tx_pipe, buf);
162
163     if (!more && (status == A_STATUS_OK)) {
164         funcptr = (jmp_func)eloc;
165         funcptr();
166     }
167 }