blob: eefe459ac16062dfd2e098c5123c22aff71a9a51 [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/******************************************************************************
2
3 AudioScience HPI driver
4 Copyright (C) 1997-2010 AudioScience Inc. <support@audioscience.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of version 2 of the GNU General Public License as
8 published by the Free Software Foundation;
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 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Hardware Programming Interface (HPI) for AudioScience
20 ASI50xx, AS51xx, ASI6xxx, ASI87xx ASI89xx series adapters.
21 These PCI and PCIe bus adapters are based on a
22 TMS320C6205 PCI bus mastering DSP,
23 and (except ASI50xx) TI TMS320C6xxx floating point DSP
24
25 Exported function:
26 void HPI_6205(struct hpi_message *phm, struct hpi_response *phr)
27
28(C) Copyright AudioScience Inc. 1998-2010
29*******************************************************************************/
30#define SOURCEFILE_NAME "hpi6205.c"
31
32#include "hpi_internal.h"
33#include "hpimsginit.h"
34#include "hpidebug.h"
35#include "hpi6205.h"
36#include "hpidspcd.h"
37#include "hpicmn.h"
38
39/*****************************************************************************/
40/* HPI6205 specific error codes */
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +130041#define HPI6205_ERROR_BASE 1000 /* not actually used anywhere */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130042
43/* operational/messaging errors */
44#define HPI6205_ERROR_MSG_RESP_IDLE_TIMEOUT 1015
45#define HPI6205_ERROR_MSG_RESP_TIMEOUT 1016
46
47/* initialization/bootload errors */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020048#define HPI6205_ERROR_6205_NO_IRQ 1002
49#define HPI6205_ERROR_6205_INIT_FAILED 1003
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020050#define HPI6205_ERROR_6205_REG 1006
51#define HPI6205_ERROR_6205_DSPPAGE 1007
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020052#define HPI6205_ERROR_C6713_HPIC 1009
53#define HPI6205_ERROR_C6713_HPIA 1010
54#define HPI6205_ERROR_C6713_PLL 1011
55#define HPI6205_ERROR_DSP_INTMEM 1012
56#define HPI6205_ERROR_DSP_EXTMEM 1013
57#define HPI6205_ERROR_DSP_PLD 1014
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020058#define HPI6205_ERROR_6205_EEPROM 1017
59#define HPI6205_ERROR_DSP_EMIF 1018
60
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020061/*****************************************************************************/
62/* for C6205 PCI i/f */
63/* Host Status Register (HSR) bitfields */
64#define C6205_HSR_INTSRC 0x01
65#define C6205_HSR_INTAVAL 0x02
66#define C6205_HSR_INTAM 0x04
67#define C6205_HSR_CFGERR 0x08
68#define C6205_HSR_EEREAD 0x10
69/* Host-to-DSP Control Register (HDCR) bitfields */
70#define C6205_HDCR_WARMRESET 0x01
71#define C6205_HDCR_DSPINT 0x02
72#define C6205_HDCR_PCIBOOT 0x04
73/* DSP Page Register (DSPP) bitfields, */
74/* defines 4 Mbyte page that BAR0 points to */
75#define C6205_DSPP_MAP1 0x400
76
77/* BAR0 maps to prefetchable 4 Mbyte memory block set by DSPP.
78 * BAR1 maps to non-prefetchable 8 Mbyte memory block
79 * of DSP memory mapped registers (starting at 0x01800000).
80 * 0x01800000 is hardcoded in the PCI i/f, so that only the offset from this
81 * needs to be added to the BAR1 base address set in the PCI config reg
82 */
83#define C6205_BAR1_PCI_IO_OFFSET (0x027FFF0L)
84#define C6205_BAR1_HSR (C6205_BAR1_PCI_IO_OFFSET)
85#define C6205_BAR1_HDCR (C6205_BAR1_PCI_IO_OFFSET+4)
86#define C6205_BAR1_DSPP (C6205_BAR1_PCI_IO_OFFSET+8)
87
88/* used to control LED (revA) and reset C6713 (revB) */
89#define C6205_BAR0_TIMER1_CTL (0x01980000L)
90
91/* For first 6713 in CE1 space, using DA17,16,2 */
92#define HPICL_ADDR 0x01400000L
93#define HPICH_ADDR 0x01400004L
94#define HPIAL_ADDR 0x01410000L
95#define HPIAH_ADDR 0x01410004L
96#define HPIDIL_ADDR 0x01420000L
97#define HPIDIH_ADDR 0x01420004L
98#define HPIDL_ADDR 0x01430000L
99#define HPIDH_ADDR 0x01430004L
100
101#define C6713_EMIF_GCTL 0x01800000
102#define C6713_EMIF_CE1 0x01800004
103#define C6713_EMIF_CE0 0x01800008
104#define C6713_EMIF_CE2 0x01800010
105#define C6713_EMIF_CE3 0x01800014
106#define C6713_EMIF_SDRAMCTL 0x01800018
107#define C6713_EMIF_SDRAMTIMING 0x0180001C
108#define C6713_EMIF_SDRAMEXT 0x01800020
109
110struct hpi_hw_obj {
111 /* PCI registers */
112 __iomem u32 *prHSR;
113 __iomem u32 *prHDCR;
114 __iomem u32 *prDSPP;
115
116 u32 dsp_page;
117
118 struct consistent_dma_area h_locked_mem;
119 struct bus_master_interface *p_interface_buffer;
120
121 u16 flag_outstream_just_reset[HPI_MAX_STREAMS];
122 /* a non-NULL handle means there is an HPI allocated buffer */
123 struct consistent_dma_area instream_host_buffers[HPI_MAX_STREAMS];
124 struct consistent_dma_area outstream_host_buffers[HPI_MAX_STREAMS];
125 /* non-zero size means a buffer exists, may be external */
126 u32 instream_host_buffer_size[HPI_MAX_STREAMS];
127 u32 outstream_host_buffer_size[HPI_MAX_STREAMS];
128
129 struct consistent_dma_area h_control_cache;
130 struct consistent_dma_area h_async_event_buffer;
131/* struct hpi_control_cache_single *pControlCache; */
132 struct hpi_async_event *p_async_event_buffer;
133 struct hpi_control_cache *p_cache;
134};
135
136/*****************************************************************************/
137/* local prototypes */
138
139#define check_before_bbm_copy(status, p_bbm_data, l_first_write, l_second_write)
140
141static int wait_dsp_ack(struct hpi_hw_obj *phw, int state, int timeout_us);
142
143static void send_dsp_command(struct hpi_hw_obj *phw, int cmd);
144
145static u16 adapter_boot_load_dsp(struct hpi_adapter_obj *pao,
146 u32 *pos_error_code);
147
148static u16 message_response_sequence(struct hpi_adapter_obj *pao,
149 struct hpi_message *phm, struct hpi_response *phr);
150
151static void hw_message(struct hpi_adapter_obj *pao, struct hpi_message *phm,
152 struct hpi_response *phr);
153
154#define HPI6205_TIMEOUT 1000000
155
156static void subsys_create_adapter(struct hpi_message *phm,
157 struct hpi_response *phr);
158static void subsys_delete_adapter(struct hpi_message *phm,
159 struct hpi_response *phr);
160
161static u16 create_adapter_obj(struct hpi_adapter_obj *pao,
162 u32 *pos_error_code);
163
164static void delete_adapter_obj(struct hpi_adapter_obj *pao);
165
166static void outstream_host_buffer_allocate(struct hpi_adapter_obj *pao,
167 struct hpi_message *phm, struct hpi_response *phr);
168
169static void outstream_host_buffer_get_info(struct hpi_adapter_obj *pao,
170 struct hpi_message *phm, struct hpi_response *phr);
171
172static void outstream_host_buffer_free(struct hpi_adapter_obj *pao,
173 struct hpi_message *phm, struct hpi_response *phr);
174static void outstream_write(struct hpi_adapter_obj *pao,
175 struct hpi_message *phm, struct hpi_response *phr);
176
177static void outstream_get_info(struct hpi_adapter_obj *pao,
178 struct hpi_message *phm, struct hpi_response *phr);
179
180static void outstream_start(struct hpi_adapter_obj *pao,
181 struct hpi_message *phm, struct hpi_response *phr);
182
183static void outstream_open(struct hpi_adapter_obj *pao,
184 struct hpi_message *phm, struct hpi_response *phr);
185
186static void outstream_reset(struct hpi_adapter_obj *pao,
187 struct hpi_message *phm, struct hpi_response *phr);
188
189static void instream_host_buffer_allocate(struct hpi_adapter_obj *pao,
190 struct hpi_message *phm, struct hpi_response *phr);
191
192static void instream_host_buffer_get_info(struct hpi_adapter_obj *pao,
193 struct hpi_message *phm, struct hpi_response *phr);
194
195static void instream_host_buffer_free(struct hpi_adapter_obj *pao,
196 struct hpi_message *phm, struct hpi_response *phr);
197
198static void instream_read(struct hpi_adapter_obj *pao,
199 struct hpi_message *phm, struct hpi_response *phr);
200
201static void instream_get_info(struct hpi_adapter_obj *pao,
202 struct hpi_message *phm, struct hpi_response *phr);
203
204static void instream_start(struct hpi_adapter_obj *pao,
205 struct hpi_message *phm, struct hpi_response *phr);
206
207static u32 boot_loader_read_mem32(struct hpi_adapter_obj *pao, int dsp_index,
208 u32 address);
209
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300210static void boot_loader_write_mem32(struct hpi_adapter_obj *pao,
211 int dsp_index, u32 address, u32 data);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200212
213static u16 boot_loader_config_emif(struct hpi_adapter_obj *pao,
214 int dsp_index);
215
216static u16 boot_loader_test_memory(struct hpi_adapter_obj *pao, int dsp_index,
217 u32 address, u32 length);
218
219static u16 boot_loader_test_internal_memory(struct hpi_adapter_obj *pao,
220 int dsp_index);
221
222static u16 boot_loader_test_external_memory(struct hpi_adapter_obj *pao,
223 int dsp_index);
224
225static u16 boot_loader_test_pld(struct hpi_adapter_obj *pao, int dsp_index);
226
227/*****************************************************************************/
228
229static void subsys_message(struct hpi_message *phm, struct hpi_response *phr)
230{
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231 switch (phm->function) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200232 case HPI_SUBSYS_CREATE_ADAPTER:
233 subsys_create_adapter(phm, phr);
234 break;
235 case HPI_SUBSYS_DELETE_ADAPTER:
236 subsys_delete_adapter(phm, phr);
237 break;
238 default:
239 phr->error = HPI_ERROR_INVALID_FUNC;
240 break;
241 }
242}
243
244static void control_message(struct hpi_adapter_obj *pao,
245 struct hpi_message *phm, struct hpi_response *phr)
246{
247
248 struct hpi_hw_obj *phw = pao->priv;
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300249 u16 pending_cache_error = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200250
251 switch (phm->function) {
252 case HPI_CONTROL_GET_STATE:
253 if (pao->has_control_cache) {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300254 rmb(); /* make sure we see updates DMAed from DSP */
255 if (hpi_check_control_cache(phw->p_cache, phm, phr)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256 break;
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300257 } else if (phm->u.c.attribute == HPI_METER_PEAK) {
258 pending_cache_error =
259 HPI_ERROR_CONTROL_CACHING;
260 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261 }
262 hw_message(pao, phm, phr);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300263 if (pending_cache_error && !phr->error)
264 phr->error = pending_cache_error;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200265 break;
266 case HPI_CONTROL_GET_INFO:
267 hw_message(pao, phm, phr);
268 break;
269 case HPI_CONTROL_SET_STATE:
270 hw_message(pao, phm, phr);
271 if (pao->has_control_cache)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300272 hpi_cmn_control_cache_sync_to_msg(phw->p_cache, phm,
273 phr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200274 break;
275 default:
276 phr->error = HPI_ERROR_INVALID_FUNC;
277 break;
278 }
279}
280
281static void adapter_message(struct hpi_adapter_obj *pao,
282 struct hpi_message *phm, struct hpi_response *phr)
283{
284 switch (phm->function) {
285 default:
286 hw_message(pao, phm, phr);
287 break;
288 }
289}
290
291static void outstream_message(struct hpi_adapter_obj *pao,
292 struct hpi_message *phm, struct hpi_response *phr)
293{
294
295 if (phm->obj_index >= HPI_MAX_STREAMS) {
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +1300296 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200297 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300298 "Message referencing invalid stream %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200299 "on adapter index %d\n", phm->obj_index,
300 phm->adapter_index);
301 return;
302 }
303
304 switch (phm->function) {
305 case HPI_OSTREAM_WRITE:
306 outstream_write(pao, phm, phr);
307 break;
308 case HPI_OSTREAM_GET_INFO:
309 outstream_get_info(pao, phm, phr);
310 break;
311 case HPI_OSTREAM_HOSTBUFFER_ALLOC:
312 outstream_host_buffer_allocate(pao, phm, phr);
313 break;
314 case HPI_OSTREAM_HOSTBUFFER_GET_INFO:
315 outstream_host_buffer_get_info(pao, phm, phr);
316 break;
317 case HPI_OSTREAM_HOSTBUFFER_FREE:
318 outstream_host_buffer_free(pao, phm, phr);
319 break;
320 case HPI_OSTREAM_START:
321 outstream_start(pao, phm, phr);
322 break;
323 case HPI_OSTREAM_OPEN:
324 outstream_open(pao, phm, phr);
325 break;
326 case HPI_OSTREAM_RESET:
327 outstream_reset(pao, phm, phr);
328 break;
329 default:
330 hw_message(pao, phm, phr);
331 break;
332 }
333}
334
335static void instream_message(struct hpi_adapter_obj *pao,
336 struct hpi_message *phm, struct hpi_response *phr)
337{
338
339 if (phm->obj_index >= HPI_MAX_STREAMS) {
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +1300340 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200341 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300342 "Message referencing invalid stream %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200343 "on adapter index %d\n", phm->obj_index,
344 phm->adapter_index);
345 return;
346 }
347
348 switch (phm->function) {
349 case HPI_ISTREAM_READ:
350 instream_read(pao, phm, phr);
351 break;
352 case HPI_ISTREAM_GET_INFO:
353 instream_get_info(pao, phm, phr);
354 break;
355 case HPI_ISTREAM_HOSTBUFFER_ALLOC:
356 instream_host_buffer_allocate(pao, phm, phr);
357 break;
358 case HPI_ISTREAM_HOSTBUFFER_GET_INFO:
359 instream_host_buffer_get_info(pao, phm, phr);
360 break;
361 case HPI_ISTREAM_HOSTBUFFER_FREE:
362 instream_host_buffer_free(pao, phm, phr);
363 break;
364 case HPI_ISTREAM_START:
365 instream_start(pao, phm, phr);
366 break;
367 default:
368 hw_message(pao, phm, phr);
369 break;
370 }
371}
372
373/*****************************************************************************/
374/** Entry point to this HPI backend
375 * All calls to the HPI start here
376 */
377void HPI_6205(struct hpi_message *phm, struct hpi_response *phr)
378{
379 struct hpi_adapter_obj *pao = NULL;
380
381 /* subsytem messages are processed by every HPI.
382 * All other messages are ignored unless the adapter index matches
383 * an adapter in the HPI
384 */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300385 /* HPI_DEBUG_LOG(DEBUG, "HPI Obj=%d, Func=%d\n", phm->wObject,
386 phm->wFunction); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200387
388 /* if Dsp has crashed then do not communicate with it any more */
389 if (phm->object != HPI_OBJ_SUBSYSTEM) {
390 pao = hpi_find_adapter(phm->adapter_index);
391 if (!pao) {
392 HPI_DEBUG_LOG(DEBUG,
393 " %d,%d refused, for another HPI?\n",
394 phm->object, phm->function);
395 return;
396 }
397
398 if ((pao->dsp_crashed >= 10)
399 && (phm->function != HPI_ADAPTER_DEBUG_READ)) {
400 /* allow last resort debug read even after crash */
401 hpi_init_response(phr, phm->object, phm->function,
402 HPI_ERROR_DSP_HARDWARE);
403 HPI_DEBUG_LOG(WARNING, " %d,%d dsp crashed.\n",
404 phm->object, phm->function);
405 return;
406 }
407 }
408
409 /* Init default response */
410 if (phm->function != HPI_SUBSYS_CREATE_ADAPTER)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300411 phr->error = HPI_ERROR_PROCESSING_MESSAGE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200412
413 HPI_DEBUG_LOG(VERBOSE, "start of switch\n");
414 switch (phm->type) {
415 case HPI_TYPE_MESSAGE:
416 switch (phm->object) {
417 case HPI_OBJ_SUBSYSTEM:
418 subsys_message(phm, phr);
419 break;
420
421 case HPI_OBJ_ADAPTER:
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200422 adapter_message(pao, phm, phr);
423 break;
424
425 case HPI_OBJ_CONTROLEX:
426 case HPI_OBJ_CONTROL:
427 control_message(pao, phm, phr);
428 break;
429
430 case HPI_OBJ_OSTREAM:
431 outstream_message(pao, phm, phr);
432 break;
433
434 case HPI_OBJ_ISTREAM:
435 instream_message(pao, phm, phr);
436 break;
437
438 default:
439 hw_message(pao, phm, phr);
440 break;
441 }
442 break;
443
444 default:
445 phr->error = HPI_ERROR_INVALID_TYPE;
446 break;
447 }
448}
449
450/*****************************************************************************/
451/* SUBSYSTEM */
452
453/** Create an adapter object and initialise it based on resource information
454 * passed in in the message
455 * *** NOTE - you cannot use this function AND the FindAdapters function at the
456 * same time, the application must use only one of them to get the adapters ***
457 */
458static void subsys_create_adapter(struct hpi_message *phm,
459 struct hpi_response *phr)
460{
461 /* create temp adapter obj, because we don't know what index yet */
462 struct hpi_adapter_obj ao;
463 u32 os_error_code;
464 u16 err;
465
466 HPI_DEBUG_LOG(DEBUG, " subsys_create_adapter\n");
467
468 memset(&ao, 0, sizeof(ao));
469
Julia Lawall550a8b62010-05-13 21:58:37 +0200470 ao.priv = kzalloc(sizeof(struct hpi_hw_obj), GFP_KERNEL);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200471 if (!ao.priv) {
472 HPI_DEBUG_LOG(ERROR, "cant get mem for adapter object\n");
473 phr->error = HPI_ERROR_MEMORY_ALLOC;
474 return;
475 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200476
477 ao.pci = *phm->u.s.resource.r.pci;
478 err = create_adapter_obj(&ao, &os_error_code);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 if (err) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200480 delete_adapter_obj(&ao);
Eliot Blennerhassett0a000442011-02-10 17:26:05 +1300481 if (err >= HPI_ERROR_BACKEND_BASE) {
482 phr->error = HPI_ERROR_DSP_BOOTLOAD;
483 phr->specific_error = err;
484 } else {
485 phr->error = err;
486 }
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300487 phr->u.s.data = os_error_code;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200488 return;
489 }
490
491 phr->u.s.aw_adapter_list[ao.index] = ao.adapter_type;
492 phr->u.s.adapter_index = ao.index;
493 phr->u.s.num_adapters++;
494 phr->error = 0;
495}
496
497/** delete an adapter - required by WDM driver */
498static void subsys_delete_adapter(struct hpi_message *phm,
499 struct hpi_response *phr)
500{
501 struct hpi_adapter_obj *pao;
502 struct hpi_hw_obj *phw;
503
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300504 pao = hpi_find_adapter(phm->obj_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200505 if (!pao) {
506 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
507 return;
508 }
509 phw = (struct hpi_hw_obj *)pao->priv;
510 /* reset adapter h/w */
511 /* Reset C6713 #1 */
512 boot_loader_write_mem32(pao, 0, C6205_BAR0_TIMER1_CTL, 0);
513 /* reset C6205 */
514 iowrite32(C6205_HDCR_WARMRESET, phw->prHDCR);
515
516 delete_adapter_obj(pao);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300517 hpi_delete_adapter(pao);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200518 phr->error = 0;
519}
520
521/** Create adapter object
522 allocate buffers, bootload DSPs, initialise control cache
523*/
524static u16 create_adapter_obj(struct hpi_adapter_obj *pao,
525 u32 *pos_error_code)
526{
527 struct hpi_hw_obj *phw = pao->priv;
528 struct bus_master_interface *interface;
529 u32 phys_addr;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200530 int i;
531 u16 err;
532
533 /* init error reporting */
534 pao->dsp_crashed = 0;
535
536 for (i = 0; i < HPI_MAX_STREAMS; i++)
537 phw->flag_outstream_just_reset[i] = 1;
538
539 /* The C6205 memory area 1 is 8Mbyte window into DSP registers */
540 phw->prHSR =
541 pao->pci.ap_mem_base[1] +
542 C6205_BAR1_HSR / sizeof(*pao->pci.ap_mem_base[1]);
543 phw->prHDCR =
544 pao->pci.ap_mem_base[1] +
545 C6205_BAR1_HDCR / sizeof(*pao->pci.ap_mem_base[1]);
546 phw->prDSPP =
547 pao->pci.ap_mem_base[1] +
548 C6205_BAR1_DSPP / sizeof(*pao->pci.ap_mem_base[1]);
549
550 pao->has_control_cache = 0;
551
552 if (hpios_locked_mem_alloc(&phw->h_locked_mem,
553 sizeof(struct bus_master_interface),
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300554 pao->pci.pci_dev))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200555 phw->p_interface_buffer = NULL;
556 else if (hpios_locked_mem_get_virt_addr(&phw->h_locked_mem,
557 (void *)&phw->p_interface_buffer))
558 phw->p_interface_buffer = NULL;
559
560 HPI_DEBUG_LOG(DEBUG, "interface buffer address %p\n",
561 phw->p_interface_buffer);
562
563 if (phw->p_interface_buffer) {
564 memset((void *)phw->p_interface_buffer, 0,
565 sizeof(struct bus_master_interface));
566 phw->p_interface_buffer->dsp_ack = H620_HIF_UNKNOWN;
567 }
568
569 err = adapter_boot_load_dsp(pao, pos_error_code);
570 if (err)
571 /* no need to clean up as SubSysCreateAdapter */
572 /* calls DeleteAdapter on error. */
573 return err;
574
575 HPI_DEBUG_LOG(INFO, "load DSP code OK\n");
576
577 /* allow boot load even if mem alloc wont work */
578 if (!phw->p_interface_buffer)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300579 return HPI_ERROR_MEMORY_ALLOC;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200580
581 interface = phw->p_interface_buffer;
582
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583 /* make sure the DSP has started ok */
584 if (!wait_dsp_ack(phw, H620_HIF_RESET, HPI6205_TIMEOUT * 10)) {
585 HPI_DEBUG_LOG(ERROR, "timed out waiting reset state \n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300586 return HPI6205_ERROR_6205_INIT_FAILED;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200587 }
588 /* Note that *pao, *phw are zeroed after allocation,
589 * so pointers and flags are NULL by default.
590 * Allocate bus mastering control cache buffer and tell the DSP about it
591 */
592 if (interface->control_cache.number_of_controls) {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300593 u8 *p_control_cache_virtual;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200594
595 err = hpios_locked_mem_alloc(&phw->h_control_cache,
596 interface->control_cache.size_in_bytes,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300597 pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200598 if (!err)
599 err = hpios_locked_mem_get_virt_addr(&phw->
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300600 h_control_cache,
601 (void *)&p_control_cache_virtual);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602 if (!err) {
603 memset(p_control_cache_virtual, 0,
604 interface->control_cache.size_in_bytes);
605
606 phw->p_cache =
607 hpi_alloc_control_cache(interface->
608 control_cache.number_of_controls,
609 interface->control_cache.size_in_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200610 p_control_cache_virtual);
Jesper Juhlfd0977d2010-10-29 21:35:25 +0200611 if (!phw->p_cache)
612 err = HPI_ERROR_MEMORY_ALLOC;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200613 }
614 if (!err) {
615 err = hpios_locked_mem_get_phys_addr(&phw->
616 h_control_cache, &phys_addr);
617 interface->control_cache.physical_address32 =
618 phys_addr;
619 }
620
621 if (!err)
622 pao->has_control_cache = 1;
623 else {
624 if (hpios_locked_mem_valid(&phw->h_control_cache))
625 hpios_locked_mem_free(&phw->h_control_cache);
626 pao->has_control_cache = 0;
627 }
628 }
629 /* allocate bus mastering async buffer and tell the DSP about it */
630 if (interface->async_buffer.b.size) {
631 err = hpios_locked_mem_alloc(&phw->h_async_event_buffer,
632 interface->async_buffer.b.size *
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300633 sizeof(struct hpi_async_event), pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200634 if (!err)
635 err = hpios_locked_mem_get_virt_addr
636 (&phw->h_async_event_buffer, (void *)
637 &phw->p_async_event_buffer);
638 if (!err)
639 memset((void *)phw->p_async_event_buffer, 0,
640 interface->async_buffer.b.size *
641 sizeof(struct hpi_async_event));
642 if (!err) {
643 err = hpios_locked_mem_get_phys_addr
644 (&phw->h_async_event_buffer, &phys_addr);
645 interface->async_buffer.physical_address32 =
646 phys_addr;
647 }
648 if (err) {
649 if (hpios_locked_mem_valid(&phw->
650 h_async_event_buffer)) {
651 hpios_locked_mem_free
652 (&phw->h_async_event_buffer);
653 phw->p_async_event_buffer = NULL;
654 }
655 }
656 }
657 send_dsp_command(phw, H620_HIF_IDLE);
658
659 {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300660 struct hpi_message hm;
661 struct hpi_response hr;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200662 u32 max_streams;
663
664 HPI_DEBUG_LOG(VERBOSE, "init ADAPTER_GET_INFO\n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300665 memset(&hm, 0, sizeof(hm));
666 hm.type = HPI_TYPE_MESSAGE;
667 hm.size = sizeof(hm);
668 hm.object = HPI_OBJ_ADAPTER;
669 hm.function = HPI_ADAPTER_GET_INFO;
670 hm.adapter_index = 0;
671 memset(&hr, 0, sizeof(hr));
672 hr.size = sizeof(hr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200673
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300674 err = message_response_sequence(pao, &hm, &hr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 if (err) {
676 HPI_DEBUG_LOG(ERROR, "message transport error %d\n",
677 err);
678 return err;
679 }
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300680 if (hr.error)
681 return hr.error;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200682
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300683 pao->adapter_type = hr.u.ax.info.adapter_type;
684 pao->index = hr.u.ax.info.adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300686 max_streams =
687 hr.u.ax.info.num_outstreams +
688 hr.u.ax.info.num_instreams;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200689
690 hpios_locked_mem_prepare((max_streams * 6) / 10, max_streams,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300691 65536, pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200692
693 HPI_DEBUG_LOG(VERBOSE,
694 "got adapter info type %x index %d serial %d\n",
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300695 hr.u.ax.info.adapter_type, hr.u.ax.info.adapter_index,
696 hr.u.ax.info.serial_number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200697 }
698
699 pao->open = 0; /* upon creation the adapter is closed */
700
Eliot Blennerhassettffdb5782011-02-10 17:26:00 +1300701 if (phw->p_cache)
702 phw->p_cache->adap_idx = pao->index;
703
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200704 HPI_DEBUG_LOG(INFO, "bootload DSP OK\n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300705
706 return hpi_add_adapter(pao);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707}
708
709/** Free memory areas allocated by adapter
710 * this routine is called from SubSysDeleteAdapter,
711 * and SubSysCreateAdapter if duplicate index
712*/
713static void delete_adapter_obj(struct hpi_adapter_obj *pao)
714{
715 struct hpi_hw_obj *phw;
716 int i;
717
718 phw = pao->priv;
719
720 if (hpios_locked_mem_valid(&phw->h_async_event_buffer)) {
721 hpios_locked_mem_free(&phw->h_async_event_buffer);
722 phw->p_async_event_buffer = NULL;
723 }
724
725 if (hpios_locked_mem_valid(&phw->h_control_cache)) {
726 hpios_locked_mem_free(&phw->h_control_cache);
727 hpi_free_control_cache(phw->p_cache);
728 }
729
730 if (hpios_locked_mem_valid(&phw->h_locked_mem)) {
731 hpios_locked_mem_free(&phw->h_locked_mem);
732 phw->p_interface_buffer = NULL;
733 }
734
735 for (i = 0; i < HPI_MAX_STREAMS; i++)
736 if (hpios_locked_mem_valid(&phw->instream_host_buffers[i])) {
737 hpios_locked_mem_free(&phw->instream_host_buffers[i]);
738 /*?phw->InStreamHostBuffers[i] = NULL; */
739 phw->instream_host_buffer_size[i] = 0;
740 }
741
742 for (i = 0; i < HPI_MAX_STREAMS; i++)
743 if (hpios_locked_mem_valid(&phw->outstream_host_buffers[i])) {
744 hpios_locked_mem_free(&phw->outstream_host_buffers
745 [i]);
746 phw->outstream_host_buffer_size[i] = 0;
747 }
748
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300749 hpios_locked_mem_unprepare(pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200750
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751 kfree(phw);
752}
753
754/*****************************************************************************/
Eliot Blennerhassett1d595d22011-02-10 17:26:08 +1300755/* Adapter functions */
756
757/*****************************************************************************/
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200758/* OutStream Host buffer functions */
759
760/** Allocate or attach buffer for busmastering
761*/
762static void outstream_host_buffer_allocate(struct hpi_adapter_obj *pao,
763 struct hpi_message *phm, struct hpi_response *phr)
764{
765 u16 err = 0;
766 u32 command = phm->u.d.u.buffer.command;
767 struct hpi_hw_obj *phw = pao->priv;
768 struct bus_master_interface *interface = phw->p_interface_buffer;
769
770 hpi_init_response(phr, phm->object, phm->function, 0);
771
772 if (command == HPI_BUFFER_CMD_EXTERNAL
773 || command == HPI_BUFFER_CMD_INTERNAL_ALLOC) {
774 /* ALLOC phase, allocate a buffer with power of 2 size,
775 get its bus address for PCI bus mastering
776 */
777 phm->u.d.u.buffer.buffer_size =
778 roundup_pow_of_two(phm->u.d.u.buffer.buffer_size);
779 /* return old size and allocated size,
780 so caller can detect change */
781 phr->u.d.u.stream_info.data_available =
782 phw->outstream_host_buffer_size[phm->obj_index];
783 phr->u.d.u.stream_info.buffer_size =
784 phm->u.d.u.buffer.buffer_size;
785
786 if (phw->outstream_host_buffer_size[phm->obj_index] ==
787 phm->u.d.u.buffer.buffer_size) {
788 /* Same size, no action required */
789 return;
790 }
791
792 if (hpios_locked_mem_valid(&phw->outstream_host_buffers[phm->
793 obj_index]))
794 hpios_locked_mem_free(&phw->outstream_host_buffers
795 [phm->obj_index]);
796
797 err = hpios_locked_mem_alloc(&phw->outstream_host_buffers
798 [phm->obj_index], phm->u.d.u.buffer.buffer_size,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300799 pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200800
801 if (err) {
802 phr->error = HPI_ERROR_INVALID_DATASIZE;
803 phw->outstream_host_buffer_size[phm->obj_index] = 0;
804 return;
805 }
806
807 err = hpios_locked_mem_get_phys_addr
808 (&phw->outstream_host_buffers[phm->obj_index],
809 &phm->u.d.u.buffer.pci_address);
810 /* get the phys addr into msg for single call alloc caller
811 * needs to do this for split alloc (or use the same message)
812 * return the phy address for split alloc in the respose too
813 */
814 phr->u.d.u.stream_info.auxiliary_data_available =
815 phm->u.d.u.buffer.pci_address;
816
817 if (err) {
818 hpios_locked_mem_free(&phw->outstream_host_buffers
819 [phm->obj_index]);
820 phw->outstream_host_buffer_size[phm->obj_index] = 0;
821 phr->error = HPI_ERROR_MEMORY_ALLOC;
822 return;
823 }
824 }
825
826 if (command == HPI_BUFFER_CMD_EXTERNAL
827 || command == HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER) {
828 /* GRANT phase. Set up the BBM status, tell the DSP about
829 the buffer so it can start using BBM.
830 */
831 struct hpi_hostbuffer_status *status;
832
833 if (phm->u.d.u.buffer.buffer_size & (phm->u.d.u.buffer.
834 buffer_size - 1)) {
835 HPI_DEBUG_LOG(ERROR,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300836 "Buffer size must be 2^N not %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200837 phm->u.d.u.buffer.buffer_size);
838 phr->error = HPI_ERROR_INVALID_DATASIZE;
839 return;
840 }
841 phw->outstream_host_buffer_size[phm->obj_index] =
842 phm->u.d.u.buffer.buffer_size;
843 status = &interface->outstream_host_buffer_status[phm->
844 obj_index];
845 status->samples_processed = 0;
846 status->stream_state = HPI_STATE_STOPPED;
847 status->dSP_index = 0;
848 status->host_index = status->dSP_index;
849 status->size_in_bytes = phm->u.d.u.buffer.buffer_size;
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +1300850 status->auxiliary_data_available = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200851
852 hw_message(pao, phm, phr);
853
854 if (phr->error
855 && hpios_locked_mem_valid(&phw->
856 outstream_host_buffers[phm->obj_index])) {
857 hpios_locked_mem_free(&phw->outstream_host_buffers
858 [phm->obj_index]);
859 phw->outstream_host_buffer_size[phm->obj_index] = 0;
860 }
861 }
862}
863
864static void outstream_host_buffer_get_info(struct hpi_adapter_obj *pao,
865 struct hpi_message *phm, struct hpi_response *phr)
866{
867 struct hpi_hw_obj *phw = pao->priv;
868 struct bus_master_interface *interface = phw->p_interface_buffer;
869 struct hpi_hostbuffer_status *status;
870 u8 *p_bbm_data;
871
872 if (hpios_locked_mem_valid(&phw->outstream_host_buffers[phm->
873 obj_index])) {
874 if (hpios_locked_mem_get_virt_addr(&phw->
875 outstream_host_buffers[phm->obj_index],
876 (void *)&p_bbm_data)) {
877 phr->error = HPI_ERROR_INVALID_OPERATION;
878 return;
879 }
880 status = &interface->outstream_host_buffer_status[phm->
881 obj_index];
882 hpi_init_response(phr, HPI_OBJ_OSTREAM,
883 HPI_OSTREAM_HOSTBUFFER_GET_INFO, 0);
884 phr->u.d.u.hostbuffer_info.p_buffer = p_bbm_data;
885 phr->u.d.u.hostbuffer_info.p_status = status;
886 } else {
887 hpi_init_response(phr, HPI_OBJ_OSTREAM,
888 HPI_OSTREAM_HOSTBUFFER_GET_INFO,
889 HPI_ERROR_INVALID_OPERATION);
890 }
891}
892
893static void outstream_host_buffer_free(struct hpi_adapter_obj *pao,
894 struct hpi_message *phm, struct hpi_response *phr)
895{
896 struct hpi_hw_obj *phw = pao->priv;
897 u32 command = phm->u.d.u.buffer.command;
898
899 if (phw->outstream_host_buffer_size[phm->obj_index]) {
900 if (command == HPI_BUFFER_CMD_EXTERNAL
901 || command == HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER) {
902 phw->outstream_host_buffer_size[phm->obj_index] = 0;
903 hw_message(pao, phm, phr);
904 /* Tell adapter to stop using the host buffer. */
905 }
906 if (command == HPI_BUFFER_CMD_EXTERNAL
907 || command == HPI_BUFFER_CMD_INTERNAL_FREE)
908 hpios_locked_mem_free(&phw->outstream_host_buffers
909 [phm->obj_index]);
910 }
911 /* Should HPI_ERROR_INVALID_OPERATION be returned
912 if no host buffer is allocated? */
913 else
914 hpi_init_response(phr, HPI_OBJ_OSTREAM,
915 HPI_OSTREAM_HOSTBUFFER_FREE, 0);
916
917}
918
Eliot Blennerhassett60f1deb2010-08-28 19:52:24 +1200919static u32 outstream_get_space_available(struct hpi_hostbuffer_status *status)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200920{
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +1200921 return status->size_in_bytes - (status->host_index -
922 status->dSP_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200923}
924
925static void outstream_write(struct hpi_adapter_obj *pao,
926 struct hpi_message *phm, struct hpi_response *phr)
927{
928 struct hpi_hw_obj *phw = pao->priv;
929 struct bus_master_interface *interface = phw->p_interface_buffer;
930 struct hpi_hostbuffer_status *status;
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +1200931 u32 space_available;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200932
933 if (!phw->outstream_host_buffer_size[phm->obj_index]) {
934 /* there is no BBM buffer, write via message */
935 hw_message(pao, phm, phr);
936 return;
937 }
938
939 hpi_init_response(phr, phm->object, phm->function, 0);
940 status = &interface->outstream_host_buffer_status[phm->obj_index];
941
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200942 space_available = outstream_get_space_available(status);
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +1200943 if (space_available < phm->u.d.u.data.data_size) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200944 phr->error = HPI_ERROR_INVALID_DATASIZE;
945 return;
946 }
947
948 /* HostBuffers is used to indicate host buffer is internally allocated.
949 otherwise, assumed external, data written externally */
950 if (phm->u.d.u.data.pb_data
951 && hpios_locked_mem_valid(&phw->outstream_host_buffers[phm->
952 obj_index])) {
953 u8 *p_bbm_data;
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +1200954 u32 l_first_write;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200955 u8 *p_app_data = (u8 *)phm->u.d.u.data.pb_data;
956
957 if (hpios_locked_mem_get_virt_addr(&phw->
958 outstream_host_buffers[phm->obj_index],
959 (void *)&p_bbm_data)) {
960 phr->error = HPI_ERROR_INVALID_OPERATION;
961 return;
962 }
963
964 /* either all data,
965 or enough to fit from current to end of BBM buffer */
966 l_first_write =
967 min(phm->u.d.u.data.data_size,
968 status->size_in_bytes -
969 (status->host_index & (status->size_in_bytes - 1)));
970
971 memcpy(p_bbm_data +
972 (status->host_index & (status->size_in_bytes - 1)),
973 p_app_data, l_first_write);
974 /* remaining data if any */
975 memcpy(p_bbm_data, p_app_data + l_first_write,
976 phm->u.d.u.data.data_size - l_first_write);
977 }
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300978
979 /*
980 * This version relies on the DSP code triggering an OStream buffer
981 * update immediately following a SET_FORMAT call. The host has
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +1300982 * already written data into the BBM buffer, but the DSP won't know
983 * about it until dwHostIndex is adjusted.
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300984 */
985 if (phw->flag_outstream_just_reset[phm->obj_index]) {
986 /* Format can only change after reset. Must tell DSP. */
987 u16 function = phm->function;
988 phw->flag_outstream_just_reset[phm->obj_index] = 0;
989 phm->function = HPI_OSTREAM_SET_FORMAT;
990 hw_message(pao, phm, phr); /* send the format to the DSP */
991 phm->function = function;
992 if (phr->error)
993 return;
994 }
995
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200996 status->host_index += phm->u.d.u.data.data_size;
997}
998
999static void outstream_get_info(struct hpi_adapter_obj *pao,
1000 struct hpi_message *phm, struct hpi_response *phr)
1001{
1002 struct hpi_hw_obj *phw = pao->priv;
1003 struct bus_master_interface *interface = phw->p_interface_buffer;
1004 struct hpi_hostbuffer_status *status;
1005
1006 if (!phw->outstream_host_buffer_size[phm->obj_index]) {
1007 hw_message(pao, phm, phr);
1008 return;
1009 }
1010
1011 hpi_init_response(phr, phm->object, phm->function, 0);
1012
1013 status = &interface->outstream_host_buffer_status[phm->obj_index];
1014
1015 phr->u.d.u.stream_info.state = (u16)status->stream_state;
1016 phr->u.d.u.stream_info.samples_transferred =
1017 status->samples_processed;
1018 phr->u.d.u.stream_info.buffer_size = status->size_in_bytes;
1019 phr->u.d.u.stream_info.data_available =
1020 status->size_in_bytes - outstream_get_space_available(status);
1021 phr->u.d.u.stream_info.auxiliary_data_available =
1022 status->auxiliary_data_available;
1023}
1024
1025static void outstream_start(struct hpi_adapter_obj *pao,
1026 struct hpi_message *phm, struct hpi_response *phr)
1027{
1028 hw_message(pao, phm, phr);
1029}
1030
1031static void outstream_reset(struct hpi_adapter_obj *pao,
1032 struct hpi_message *phm, struct hpi_response *phr)
1033{
1034 struct hpi_hw_obj *phw = pao->priv;
1035 phw->flag_outstream_just_reset[phm->obj_index] = 1;
1036 hw_message(pao, phm, phr);
1037}
1038
1039static void outstream_open(struct hpi_adapter_obj *pao,
1040 struct hpi_message *phm, struct hpi_response *phr)
1041{
1042 outstream_reset(pao, phm, phr);
1043}
1044
1045/*****************************************************************************/
1046/* InStream Host buffer functions */
1047
1048static void instream_host_buffer_allocate(struct hpi_adapter_obj *pao,
1049 struct hpi_message *phm, struct hpi_response *phr)
1050{
1051 u16 err = 0;
1052 u32 command = phm->u.d.u.buffer.command;
1053 struct hpi_hw_obj *phw = pao->priv;
1054 struct bus_master_interface *interface = phw->p_interface_buffer;
1055
1056 hpi_init_response(phr, phm->object, phm->function, 0);
1057
1058 if (command == HPI_BUFFER_CMD_EXTERNAL
1059 || command == HPI_BUFFER_CMD_INTERNAL_ALLOC) {
1060
1061 phm->u.d.u.buffer.buffer_size =
1062 roundup_pow_of_two(phm->u.d.u.buffer.buffer_size);
1063 phr->u.d.u.stream_info.data_available =
1064 phw->instream_host_buffer_size[phm->obj_index];
1065 phr->u.d.u.stream_info.buffer_size =
1066 phm->u.d.u.buffer.buffer_size;
1067
1068 if (phw->instream_host_buffer_size[phm->obj_index] ==
1069 phm->u.d.u.buffer.buffer_size) {
1070 /* Same size, no action required */
1071 return;
1072 }
1073
1074 if (hpios_locked_mem_valid(&phw->instream_host_buffers[phm->
1075 obj_index]))
1076 hpios_locked_mem_free(&phw->instream_host_buffers
1077 [phm->obj_index]);
1078
1079 err = hpios_locked_mem_alloc(&phw->instream_host_buffers[phm->
1080 obj_index], phm->u.d.u.buffer.buffer_size,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001081 pao->pci.pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001082
1083 if (err) {
1084 phr->error = HPI_ERROR_INVALID_DATASIZE;
1085 phw->instream_host_buffer_size[phm->obj_index] = 0;
1086 return;
1087 }
1088
1089 err = hpios_locked_mem_get_phys_addr
1090 (&phw->instream_host_buffers[phm->obj_index],
1091 &phm->u.d.u.buffer.pci_address);
1092 /* get the phys addr into msg for single call alloc. Caller
1093 needs to do this for split alloc so return the phy address */
1094 phr->u.d.u.stream_info.auxiliary_data_available =
1095 phm->u.d.u.buffer.pci_address;
1096 if (err) {
1097 hpios_locked_mem_free(&phw->instream_host_buffers
1098 [phm->obj_index]);
1099 phw->instream_host_buffer_size[phm->obj_index] = 0;
1100 phr->error = HPI_ERROR_MEMORY_ALLOC;
1101 return;
1102 }
1103 }
1104
1105 if (command == HPI_BUFFER_CMD_EXTERNAL
1106 || command == HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER) {
1107 struct hpi_hostbuffer_status *status;
1108
1109 if (phm->u.d.u.buffer.buffer_size & (phm->u.d.u.buffer.
1110 buffer_size - 1)) {
1111 HPI_DEBUG_LOG(ERROR,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001112 "Buffer size must be 2^N not %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001113 phm->u.d.u.buffer.buffer_size);
1114 phr->error = HPI_ERROR_INVALID_DATASIZE;
1115 return;
1116 }
1117
1118 phw->instream_host_buffer_size[phm->obj_index] =
1119 phm->u.d.u.buffer.buffer_size;
1120 status = &interface->instream_host_buffer_status[phm->
1121 obj_index];
1122 status->samples_processed = 0;
1123 status->stream_state = HPI_STATE_STOPPED;
1124 status->dSP_index = 0;
1125 status->host_index = status->dSP_index;
1126 status->size_in_bytes = phm->u.d.u.buffer.buffer_size;
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +13001127 status->auxiliary_data_available = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001128
1129 hw_message(pao, phm, phr);
1130 if (phr->error
1131 && hpios_locked_mem_valid(&phw->
1132 instream_host_buffers[phm->obj_index])) {
1133 hpios_locked_mem_free(&phw->instream_host_buffers
1134 [phm->obj_index]);
1135 phw->instream_host_buffer_size[phm->obj_index] = 0;
1136 }
1137 }
1138}
1139
1140static void instream_host_buffer_get_info(struct hpi_adapter_obj *pao,
1141 struct hpi_message *phm, struct hpi_response *phr)
1142{
1143 struct hpi_hw_obj *phw = pao->priv;
1144 struct bus_master_interface *interface = phw->p_interface_buffer;
1145 struct hpi_hostbuffer_status *status;
1146 u8 *p_bbm_data;
1147
1148 if (hpios_locked_mem_valid(&phw->instream_host_buffers[phm->
1149 obj_index])) {
1150 if (hpios_locked_mem_get_virt_addr(&phw->
1151 instream_host_buffers[phm->obj_index],
1152 (void *)&p_bbm_data)) {
1153 phr->error = HPI_ERROR_INVALID_OPERATION;
1154 return;
1155 }
1156 status = &interface->instream_host_buffer_status[phm->
1157 obj_index];
1158 hpi_init_response(phr, HPI_OBJ_ISTREAM,
1159 HPI_ISTREAM_HOSTBUFFER_GET_INFO, 0);
1160 phr->u.d.u.hostbuffer_info.p_buffer = p_bbm_data;
1161 phr->u.d.u.hostbuffer_info.p_status = status;
1162 } else {
1163 hpi_init_response(phr, HPI_OBJ_ISTREAM,
1164 HPI_ISTREAM_HOSTBUFFER_GET_INFO,
1165 HPI_ERROR_INVALID_OPERATION);
1166 }
1167}
1168
1169static void instream_host_buffer_free(struct hpi_adapter_obj *pao,
1170 struct hpi_message *phm, struct hpi_response *phr)
1171{
1172 struct hpi_hw_obj *phw = pao->priv;
1173 u32 command = phm->u.d.u.buffer.command;
1174
1175 if (phw->instream_host_buffer_size[phm->obj_index]) {
1176 if (command == HPI_BUFFER_CMD_EXTERNAL
1177 || command == HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER) {
1178 phw->instream_host_buffer_size[phm->obj_index] = 0;
1179 hw_message(pao, phm, phr);
1180 }
1181
1182 if (command == HPI_BUFFER_CMD_EXTERNAL
1183 || command == HPI_BUFFER_CMD_INTERNAL_FREE)
1184 hpios_locked_mem_free(&phw->instream_host_buffers
1185 [phm->obj_index]);
1186
1187 } else {
1188 /* Should HPI_ERROR_INVALID_OPERATION be returned
1189 if no host buffer is allocated? */
1190 hpi_init_response(phr, HPI_OBJ_ISTREAM,
1191 HPI_ISTREAM_HOSTBUFFER_FREE, 0);
1192
1193 }
1194
1195}
1196
1197static void instream_start(struct hpi_adapter_obj *pao,
1198 struct hpi_message *phm, struct hpi_response *phr)
1199{
1200 hw_message(pao, phm, phr);
1201}
1202
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +12001203static u32 instream_get_bytes_available(struct hpi_hostbuffer_status *status)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001204{
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +12001205 return status->dSP_index - status->host_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001206}
1207
1208static void instream_read(struct hpi_adapter_obj *pao,
1209 struct hpi_message *phm, struct hpi_response *phr)
1210{
1211 struct hpi_hw_obj *phw = pao->priv;
1212 struct bus_master_interface *interface = phw->p_interface_buffer;
1213 struct hpi_hostbuffer_status *status;
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +12001214 u32 data_available;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001215 u8 *p_bbm_data;
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +12001216 u32 l_first_read;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001217 u8 *p_app_data = (u8 *)phm->u.d.u.data.pb_data;
1218
1219 if (!phw->instream_host_buffer_size[phm->obj_index]) {
1220 hw_message(pao, phm, phr);
1221 return;
1222 }
1223 hpi_init_response(phr, phm->object, phm->function, 0);
1224
1225 status = &interface->instream_host_buffer_status[phm->obj_index];
1226 data_available = instream_get_bytes_available(status);
Eliot Blennerhassett2a383cb2010-06-17 17:08:36 +12001227 if (data_available < phm->u.d.u.data.data_size) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001228 phr->error = HPI_ERROR_INVALID_DATASIZE;
1229 return;
1230 }
1231
1232 if (hpios_locked_mem_valid(&phw->instream_host_buffers[phm->
1233 obj_index])) {
1234 if (hpios_locked_mem_get_virt_addr(&phw->
1235 instream_host_buffers[phm->obj_index],
1236 (void *)&p_bbm_data)) {
1237 phr->error = HPI_ERROR_INVALID_OPERATION;
1238 return;
1239 }
1240
1241 /* either all data,
1242 or enough to fit from current to end of BBM buffer */
1243 l_first_read =
1244 min(phm->u.d.u.data.data_size,
1245 status->size_in_bytes -
1246 (status->host_index & (status->size_in_bytes - 1)));
1247
1248 memcpy(p_app_data,
1249 p_bbm_data +
1250 (status->host_index & (status->size_in_bytes - 1)),
1251 l_first_read);
1252 /* remaining data if any */
1253 memcpy(p_app_data + l_first_read, p_bbm_data,
1254 phm->u.d.u.data.data_size - l_first_read);
1255 }
1256 status->host_index += phm->u.d.u.data.data_size;
1257}
1258
1259static void instream_get_info(struct hpi_adapter_obj *pao,
1260 struct hpi_message *phm, struct hpi_response *phr)
1261{
1262 struct hpi_hw_obj *phw = pao->priv;
1263 struct bus_master_interface *interface = phw->p_interface_buffer;
1264 struct hpi_hostbuffer_status *status;
1265 if (!phw->instream_host_buffer_size[phm->obj_index]) {
1266 hw_message(pao, phm, phr);
1267 return;
1268 }
1269
1270 status = &interface->instream_host_buffer_status[phm->obj_index];
1271
1272 hpi_init_response(phr, phm->object, phm->function, 0);
1273
1274 phr->u.d.u.stream_info.state = (u16)status->stream_state;
1275 phr->u.d.u.stream_info.samples_transferred =
1276 status->samples_processed;
1277 phr->u.d.u.stream_info.buffer_size = status->size_in_bytes;
1278 phr->u.d.u.stream_info.data_available =
1279 instream_get_bytes_available(status);
1280 phr->u.d.u.stream_info.auxiliary_data_available =
1281 status->auxiliary_data_available;
1282}
1283
1284/*****************************************************************************/
1285/* LOW-LEVEL */
1286#define HPI6205_MAX_FILES_TO_LOAD 2
1287
1288static u16 adapter_boot_load_dsp(struct hpi_adapter_obj *pao,
1289 u32 *pos_error_code)
1290{
1291 struct hpi_hw_obj *phw = pao->priv;
1292 struct dsp_code dsp_code;
1293 u16 boot_code_id[HPI6205_MAX_FILES_TO_LOAD];
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001294 u16 firmware_id = pao->pci.pci_dev->subsystem_device;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001295 u32 temp;
1296 int dsp = 0, i = 0;
1297 u16 err = 0;
1298
1299 boot_code_id[0] = HPI_ADAPTER_ASI(0x6205);
1300
1301 /* special cases where firmware_id != subsys ID */
1302 switch (firmware_id) {
1303 case HPI_ADAPTER_FAMILY_ASI(0x5000):
1304 boot_code_id[0] = firmware_id;
1305 firmware_id = 0;
1306 break;
1307 case HPI_ADAPTER_FAMILY_ASI(0x5300):
1308 case HPI_ADAPTER_FAMILY_ASI(0x5400):
1309 case HPI_ADAPTER_FAMILY_ASI(0x6300):
1310 firmware_id = HPI_ADAPTER_FAMILY_ASI(0x6400);
1311 break;
1312 case HPI_ADAPTER_FAMILY_ASI(0x5600):
1313 case HPI_ADAPTER_FAMILY_ASI(0x6500):
1314 firmware_id = HPI_ADAPTER_FAMILY_ASI(0x6600);
1315 break;
Eliot Blennerhassettcadae422010-05-27 17:53:54 +12001316 case HPI_ADAPTER_FAMILY_ASI(0x8800):
1317 firmware_id = HPI_ADAPTER_FAMILY_ASI(0x8900);
1318 break;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001319 }
1320 boot_code_id[1] = firmware_id;
1321
1322 /* reset DSP by writing a 1 to the WARMRESET bit */
1323 temp = C6205_HDCR_WARMRESET;
1324 iowrite32(temp, phw->prHDCR);
1325 hpios_delay_micro_seconds(1000);
1326
1327 /* check that PCI i/f was configured by EEPROM */
1328 temp = ioread32(phw->prHSR);
1329 if ((temp & (C6205_HSR_CFGERR | C6205_HSR_EEREAD)) !=
1330 C6205_HSR_EEREAD)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001331 return HPI6205_ERROR_6205_EEPROM;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001332 temp |= 0x04;
1333 /* disable PINTA interrupt */
1334 iowrite32(temp, phw->prHSR);
1335
1336 /* check control register reports PCI boot mode */
1337 temp = ioread32(phw->prHDCR);
1338 if (!(temp & C6205_HDCR_PCIBOOT))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001339 return HPI6205_ERROR_6205_REG;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001340
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001341 /* try writing a few numbers to the DSP page register */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001342 /* and reading them back. */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001343 temp = 3;
1344 iowrite32(temp, phw->prDSPP);
1345 if ((temp | C6205_DSPP_MAP1) != ioread32(phw->prDSPP))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001346 return HPI6205_ERROR_6205_DSPPAGE;
1347 temp = 2;
1348 iowrite32(temp, phw->prDSPP);
1349 if ((temp | C6205_DSPP_MAP1) != ioread32(phw->prDSPP))
1350 return HPI6205_ERROR_6205_DSPPAGE;
1351 temp = 1;
1352 iowrite32(temp, phw->prDSPP);
1353 if ((temp | C6205_DSPP_MAP1) != ioread32(phw->prDSPP))
1354 return HPI6205_ERROR_6205_DSPPAGE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001355 /* reset DSP page to the correct number */
1356 temp = 0;
1357 iowrite32(temp, phw->prDSPP);
1358 if ((temp | C6205_DSPP_MAP1) != ioread32(phw->prDSPP))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001359 return HPI6205_ERROR_6205_DSPPAGE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001360 phw->dsp_page = 0;
1361
1362 /* release 6713 from reset before 6205 is bootloaded.
1363 This ensures that the EMIF is inactive,
1364 and the 6713 HPI gets the correct bootmode etc
1365 */
1366 if (boot_code_id[1] != 0) {
1367 /* DSP 1 is a C6713 */
1368 /* CLKX0 <- '1' release the C6205 bootmode pulldowns */
1369 boot_loader_write_mem32(pao, 0, (0x018C0024L), 0x00002202);
1370 hpios_delay_micro_seconds(100);
1371 /* Reset the 6713 #1 - revB */
1372 boot_loader_write_mem32(pao, 0, C6205_BAR0_TIMER1_CTL, 0);
1373
1374 /* dummy read every 4 words for 6205 advisory 1.4.4 */
1375 boot_loader_read_mem32(pao, 0, 0);
1376
1377 hpios_delay_micro_seconds(100);
1378 /* Release C6713 from reset - revB */
1379 boot_loader_write_mem32(pao, 0, C6205_BAR0_TIMER1_CTL, 4);
1380 hpios_delay_micro_seconds(100);
1381 }
1382
1383 for (dsp = 0; dsp < HPI6205_MAX_FILES_TO_LOAD; dsp++) {
1384 /* is there a DSP to load? */
1385 if (boot_code_id[dsp] == 0)
1386 continue;
1387
1388 err = boot_loader_config_emif(pao, dsp);
1389 if (err)
1390 return err;
1391
1392 err = boot_loader_test_internal_memory(pao, dsp);
1393 if (err)
1394 return err;
1395
1396 err = boot_loader_test_external_memory(pao, dsp);
1397 if (err)
1398 return err;
1399
1400 err = boot_loader_test_pld(pao, dsp);
1401 if (err)
1402 return err;
1403
1404 /* write the DSP code down into the DSPs memory */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001405 dsp_code.ps_dev = pao->pci.pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001406 err = hpi_dsp_code_open(boot_code_id[dsp], &dsp_code,
1407 pos_error_code);
1408 if (err)
1409 return err;
1410
1411 while (1) {
1412 u32 length;
1413 u32 address;
1414 u32 type;
1415 u32 *pcode;
1416
1417 err = hpi_dsp_code_read_word(&dsp_code, &length);
1418 if (err)
1419 break;
1420 if (length == 0xFFFFFFFF)
1421 break; /* end of code */
1422
1423 err = hpi_dsp_code_read_word(&dsp_code, &address);
1424 if (err)
1425 break;
1426 err = hpi_dsp_code_read_word(&dsp_code, &type);
1427 if (err)
1428 break;
1429 err = hpi_dsp_code_read_block(length, &dsp_code,
1430 &pcode);
1431 if (err)
1432 break;
1433 for (i = 0; i < (int)length; i++) {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001434 boot_loader_write_mem32(pao, dsp, address,
1435 *pcode);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001436 /* dummy read every 4 words */
1437 /* for 6205 advisory 1.4.4 */
1438 if (i % 4 == 0)
1439 boot_loader_read_mem32(pao, dsp,
1440 address);
1441 pcode++;
1442 address += 4;
1443 }
1444
1445 }
1446 if (err) {
1447 hpi_dsp_code_close(&dsp_code);
1448 return err;
1449 }
1450
1451 /* verify code */
1452 hpi_dsp_code_rewind(&dsp_code);
1453 while (1) {
1454 u32 length = 0;
1455 u32 address = 0;
1456 u32 type = 0;
1457 u32 *pcode = NULL;
1458 u32 data = 0;
1459
1460 hpi_dsp_code_read_word(&dsp_code, &length);
1461 if (length == 0xFFFFFFFF)
1462 break; /* end of code */
1463
1464 hpi_dsp_code_read_word(&dsp_code, &address);
1465 hpi_dsp_code_read_word(&dsp_code, &type);
1466 hpi_dsp_code_read_block(length, &dsp_code, &pcode);
1467
1468 for (i = 0; i < (int)length; i++) {
1469 data = boot_loader_read_mem32(pao, dsp,
1470 address);
1471 if (data != *pcode) {
1472 err = 0;
1473 break;
1474 }
1475 pcode++;
1476 address += 4;
1477 }
1478 if (err)
1479 break;
1480 }
1481 hpi_dsp_code_close(&dsp_code);
1482 if (err)
1483 return err;
1484 }
1485
1486 /* After bootloading all DSPs, start DSP0 running
1487 * The DSP0 code will handle starting and synchronizing with its slaves
1488 */
1489 if (phw->p_interface_buffer) {
1490 /* we need to tell the card the physical PCI address */
1491 u32 physicalPC_iaddress;
1492 struct bus_master_interface *interface =
1493 phw->p_interface_buffer;
1494 u32 host_mailbox_address_on_dsp;
1495 u32 physicalPC_iaddress_verify = 0;
1496 int time_out = 10;
1497 /* set ack so we know when DSP is ready to go */
1498 /* (dwDspAck will be changed to HIF_RESET) */
1499 interface->dsp_ack = H620_HIF_UNKNOWN;
1500 wmb(); /* ensure ack is written before dsp writes back */
1501
1502 err = hpios_locked_mem_get_phys_addr(&phw->h_locked_mem,
1503 &physicalPC_iaddress);
1504
1505 /* locate the host mailbox on the DSP. */
1506 host_mailbox_address_on_dsp = 0x80000000;
1507 while ((physicalPC_iaddress != physicalPC_iaddress_verify)
1508 && time_out--) {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001509 boot_loader_write_mem32(pao, 0,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001510 host_mailbox_address_on_dsp,
1511 physicalPC_iaddress);
1512 physicalPC_iaddress_verify =
1513 boot_loader_read_mem32(pao, 0,
1514 host_mailbox_address_on_dsp);
1515 }
1516 }
1517 HPI_DEBUG_LOG(DEBUG, "starting DS_ps running\n");
1518 /* enable interrupts */
1519 temp = ioread32(phw->prHSR);
1520 temp &= ~(u32)C6205_HSR_INTAM;
1521 iowrite32(temp, phw->prHSR);
1522
1523 /* start code running... */
1524 temp = ioread32(phw->prHDCR);
1525 temp |= (u32)C6205_HDCR_DSPINT;
1526 iowrite32(temp, phw->prHDCR);
1527
1528 /* give the DSP 10ms to start up */
1529 hpios_delay_micro_seconds(10000);
1530 return err;
1531
1532}
1533
1534/*****************************************************************************/
1535/* Bootloader utility functions */
1536
1537static u32 boot_loader_read_mem32(struct hpi_adapter_obj *pao, int dsp_index,
1538 u32 address)
1539{
1540 struct hpi_hw_obj *phw = pao->priv;
1541 u32 data = 0;
1542 __iomem u32 *p_data;
1543
1544 if (dsp_index == 0) {
1545 /* DSP 0 is always C6205 */
1546 if ((address >= 0x01800000) & (address < 0x02000000)) {
1547 /* BAR1 register access */
1548 p_data = pao->pci.ap_mem_base[1] +
1549 (address & 0x007fffff) /
1550 sizeof(*pao->pci.ap_mem_base[1]);
1551 /* HPI_DEBUG_LOG(WARNING,
1552 "BAR1 access %08x\n", dwAddress); */
1553 } else {
1554 u32 dw4M_page = address >> 22L;
1555 if (dw4M_page != phw->dsp_page) {
1556 phw->dsp_page = dw4M_page;
1557 /* *INDENT OFF* */
1558 iowrite32(phw->dsp_page, phw->prDSPP);
1559 /* *INDENT-ON* */
1560 }
1561 address &= 0x3fffff; /* address within 4M page */
1562 /* BAR0 memory access */
1563 p_data = pao->pci.ap_mem_base[0] +
1564 address / sizeof(u32);
1565 }
1566 data = ioread32(p_data);
1567 } else if (dsp_index == 1) {
1568 /* DSP 1 is a C6713 */
1569 u32 lsb;
1570 boot_loader_write_mem32(pao, 0, HPIAL_ADDR, address);
1571 boot_loader_write_mem32(pao, 0, HPIAH_ADDR, address >> 16);
1572 lsb = boot_loader_read_mem32(pao, 0, HPIDL_ADDR);
1573 data = boot_loader_read_mem32(pao, 0, HPIDH_ADDR);
1574 data = (data << 16) | (lsb & 0xFFFF);
1575 }
1576 return data;
1577}
1578
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001579static void boot_loader_write_mem32(struct hpi_adapter_obj *pao,
1580 int dsp_index, u32 address, u32 data)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001581{
1582 struct hpi_hw_obj *phw = pao->priv;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001583 __iomem u32 *p_data;
1584 /* u32 dwVerifyData=0; */
1585
1586 if (dsp_index == 0) {
1587 /* DSP 0 is always C6205 */
1588 if ((address >= 0x01800000) & (address < 0x02000000)) {
1589 /* BAR1 - DSP register access using */
1590 /* Non-prefetchable PCI access */
1591 p_data = pao->pci.ap_mem_base[1] +
1592 (address & 0x007fffff) /
1593 sizeof(*pao->pci.ap_mem_base[1]);
1594 } else {
1595 /* BAR0 access - all of DSP memory using */
1596 /* pre-fetchable PCI access */
1597 u32 dw4M_page = address >> 22L;
1598 if (dw4M_page != phw->dsp_page) {
1599 phw->dsp_page = dw4M_page;
1600 /* *INDENT-OFF* */
1601 iowrite32(phw->dsp_page, phw->prDSPP);
1602 /* *INDENT-ON* */
1603 }
1604 address &= 0x3fffff; /* address within 4M page */
1605 p_data = pao->pci.ap_mem_base[0] +
1606 address / sizeof(u32);
1607 }
1608 iowrite32(data, p_data);
1609 } else if (dsp_index == 1) {
1610 /* DSP 1 is a C6713 */
1611 boot_loader_write_mem32(pao, 0, HPIAL_ADDR, address);
1612 boot_loader_write_mem32(pao, 0, HPIAH_ADDR, address >> 16);
1613
1614 /* dummy read every 4 words for 6205 advisory 1.4.4 */
1615 boot_loader_read_mem32(pao, 0, 0);
1616
1617 boot_loader_write_mem32(pao, 0, HPIDL_ADDR, data);
1618 boot_loader_write_mem32(pao, 0, HPIDH_ADDR, data >> 16);
1619
1620 /* dummy read every 4 words for 6205 advisory 1.4.4 */
1621 boot_loader_read_mem32(pao, 0, 0);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001622 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001623}
1624
1625static u16 boot_loader_config_emif(struct hpi_adapter_obj *pao, int dsp_index)
1626{
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001627 if (dsp_index == 0) {
1628 u32 setting;
1629
1630 /* DSP 0 is always C6205 */
1631
1632 /* Set the EMIF */
1633 /* memory map of C6205 */
1634 /* 00000000-0000FFFF 16Kx32 internal program */
1635 /* 00400000-00BFFFFF CE0 2Mx32 SDRAM running @ 100MHz */
1636
1637 /* EMIF config */
1638 /*------------ */
1639 /* Global EMIF control */
1640 boot_loader_write_mem32(pao, dsp_index, 0x01800000, 0x3779);
1641#define WS_OFS 28
1642#define WST_OFS 22
1643#define WH_OFS 20
1644#define RS_OFS 16
1645#define RST_OFS 8
1646#define MTYPE_OFS 4
1647#define RH_OFS 0
1648
1649 /* EMIF CE0 setup - 2Mx32 Sync DRAM on ASI5000 cards only */
1650 setting = 0x00000030;
1651 boot_loader_write_mem32(pao, dsp_index, 0x01800008, setting);
1652 if (setting != boot_loader_read_mem32(pao, dsp_index,
1653 0x01800008))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001654 return HPI6205_ERROR_DSP_EMIF;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001655
1656 /* EMIF CE1 setup - 32 bit async. This is 6713 #1 HPI, */
1657 /* which occupies D15..0. 6713 starts at 27MHz, so need */
1658 /* plenty of wait states. See dsn8701.rtf, and 6713 errata. */
1659 /* WST should be 71, but 63 is max possible */
1660 setting =
1661 (1L << WS_OFS) | (63L << WST_OFS) | (1L << WH_OFS) |
1662 (1L << RS_OFS) | (63L << RST_OFS) | (1L << RH_OFS) |
1663 (2L << MTYPE_OFS);
1664 boot_loader_write_mem32(pao, dsp_index, 0x01800004, setting);
1665 if (setting != boot_loader_read_mem32(pao, dsp_index,
1666 0x01800004))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001667 return HPI6205_ERROR_DSP_EMIF;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001668
1669 /* EMIF CE2 setup - 32 bit async. This is 6713 #2 HPI, */
1670 /* which occupies D15..0. 6713 starts at 27MHz, so need */
1671 /* plenty of wait states */
1672 setting =
1673 (1L << WS_OFS) | (28L << WST_OFS) | (1L << WH_OFS) |
1674 (1L << RS_OFS) | (63L << RST_OFS) | (1L << RH_OFS) |
1675 (2L << MTYPE_OFS);
1676 boot_loader_write_mem32(pao, dsp_index, 0x01800010, setting);
1677 if (setting != boot_loader_read_mem32(pao, dsp_index,
1678 0x01800010))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001679 return HPI6205_ERROR_DSP_EMIF;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001680
1681 /* EMIF CE3 setup - 32 bit async. */
1682 /* This is the PLD on the ASI5000 cards only */
1683 setting =
1684 (1L << WS_OFS) | (10L << WST_OFS) | (1L << WH_OFS) |
1685 (1L << RS_OFS) | (10L << RST_OFS) | (1L << RH_OFS) |
1686 (2L << MTYPE_OFS);
1687 boot_loader_write_mem32(pao, dsp_index, 0x01800014, setting);
1688 if (setting != boot_loader_read_mem32(pao, dsp_index,
1689 0x01800014))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001690 return HPI6205_ERROR_DSP_EMIF;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001691
1692 /* set EMIF SDRAM control for 2Mx32 SDRAM (512x32x4 bank) */
1693 /* need to use this else DSP code crashes? */
1694 boot_loader_write_mem32(pao, dsp_index, 0x01800018,
1695 0x07117000);
1696
1697 /* EMIF SDRAM Refresh Timing */
1698 /* EMIF SDRAM timing (orig = 0x410, emulator = 0x61a) */
1699 boot_loader_write_mem32(pao, dsp_index, 0x0180001C,
1700 0x00000410);
1701
1702 } else if (dsp_index == 1) {
1703 /* test access to the C6713s HPI registers */
1704 u32 write_data = 0, read_data = 0, i = 0;
1705
1706 /* Set up HPIC for little endian, by setiing HPIC:HWOB=1 */
1707 write_data = 1;
1708 boot_loader_write_mem32(pao, 0, HPICL_ADDR, write_data);
1709 boot_loader_write_mem32(pao, 0, HPICH_ADDR, write_data);
1710 /* C67 HPI is on lower 16bits of 32bit EMIF */
1711 read_data =
1712 0xFFF7 & boot_loader_read_mem32(pao, 0, HPICL_ADDR);
1713 if (write_data != read_data) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001714 HPI_DEBUG_LOG(ERROR, "HPICL %x %x\n", write_data,
1715 read_data);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001716 return HPI6205_ERROR_C6713_HPIC;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001717 }
1718 /* HPIA - walking ones test */
1719 write_data = 1;
1720 for (i = 0; i < 32; i++) {
1721 boot_loader_write_mem32(pao, 0, HPIAL_ADDR,
1722 write_data);
1723 boot_loader_write_mem32(pao, 0, HPIAH_ADDR,
1724 (write_data >> 16));
1725 read_data =
1726 0xFFFF & boot_loader_read_mem32(pao, 0,
1727 HPIAL_ADDR);
1728 read_data =
1729 read_data | ((0xFFFF &
1730 boot_loader_read_mem32(pao, 0,
1731 HPIAH_ADDR))
1732 << 16);
1733 if (read_data != write_data) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001734 HPI_DEBUG_LOG(ERROR, "HPIA %x %x\n",
1735 write_data, read_data);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001736 return HPI6205_ERROR_C6713_HPIA;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001737 }
1738 write_data = write_data << 1;
1739 }
1740
1741 /* setup C67x PLL
1742 * ** C6713 datasheet says we cannot program PLL from HPI,
1743 * and indeed if we try to set the PLL multiply from the HPI,
1744 * the PLL does not seem to lock, so we enable the PLL and
1745 * use the default multiply of x 7, which for a 27MHz clock
1746 * gives a DSP speed of 189MHz
1747 */
1748 /* bypass PLL */
1749 boot_loader_write_mem32(pao, dsp_index, 0x01B7C100, 0x0000);
1750 hpios_delay_micro_seconds(1000);
1751 /* EMIF = 189/3=63MHz */
1752 boot_loader_write_mem32(pao, dsp_index, 0x01B7C120, 0x8002);
1753 /* peri = 189/2 */
1754 boot_loader_write_mem32(pao, dsp_index, 0x01B7C11C, 0x8001);
1755 /* cpu = 189/1 */
1756 boot_loader_write_mem32(pao, dsp_index, 0x01B7C118, 0x8000);
1757 hpios_delay_micro_seconds(1000);
1758 /* ** SGT test to take GPO3 high when we start the PLL */
1759 /* and low when the delay is completed */
1760 /* FSX0 <- '1' (GPO3) */
1761 boot_loader_write_mem32(pao, 0, (0x018C0024L), 0x00002A0A);
1762 /* PLL not bypassed */
1763 boot_loader_write_mem32(pao, dsp_index, 0x01B7C100, 0x0001);
1764 hpios_delay_micro_seconds(1000);
1765 /* FSX0 <- '0' (GPO3) */
1766 boot_loader_write_mem32(pao, 0, (0x018C0024L), 0x00002A02);
1767
1768 /* 6205 EMIF CE1 resetup - 32 bit async. */
1769 /* Now 6713 #1 is running at 189MHz can reduce waitstates */
1770 boot_loader_write_mem32(pao, 0, 0x01800004, /* CE1 */
1771 (1L << WS_OFS) | (8L << WST_OFS) | (1L << WH_OFS) |
1772 (1L << RS_OFS) | (12L << RST_OFS) | (1L << RH_OFS) |
1773 (2L << MTYPE_OFS));
1774
1775 hpios_delay_micro_seconds(1000);
1776
1777 /* check that we can read one of the PLL registers */
1778 /* PLL should not be bypassed! */
1779 if ((boot_loader_read_mem32(pao, dsp_index, 0x01B7C100) & 0xF)
1780 != 0x0001) {
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001781 return HPI6205_ERROR_C6713_PLL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001782 }
1783 /* setup C67x EMIF (note this is the only use of
1784 BAR1 via BootLoader_WriteMem32) */
1785 boot_loader_write_mem32(pao, dsp_index, C6713_EMIF_GCTL,
1786 0x000034A8);
Eliot Blennerhassett1d595d22011-02-10 17:26:08 +13001787
1788 /* EMIF CE0 setup - 2Mx32 Sync DRAM
1789 31..28 Wr setup
1790 27..22 Wr strobe
1791 21..20 Wr hold
1792 19..16 Rd setup
1793 15..14 -
1794 13..8 Rd strobe
1795 7..4 MTYPE 0011 Sync DRAM 32bits
1796 3 Wr hold MSB
1797 2..0 Rd hold
1798 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001799 boot_loader_write_mem32(pao, dsp_index, C6713_EMIF_CE0,
1800 0x00000030);
Eliot Blennerhassett1d595d22011-02-10 17:26:08 +13001801
1802 /* EMIF SDRAM Extension
1803 0x00
1804 31-21 0000b 0000b 000b
1805 20 WR2RD = 2cycles-1 = 1b
1806
1807 19-18 WR2DEAC = 3cycle-1 = 10b
1808 17 WR2WR = 2cycle-1 = 1b
1809 16-15 R2WDQM = 4cycle-1 = 11b
1810 14-12 RD2WR = 6cycles-1 = 101b
1811
1812 11-10 RD2DEAC = 4cycle-1 = 11b
1813 9 RD2RD = 2cycle-1 = 1b
1814 8-7 THZP = 3cycle-1 = 10b
1815 6-5 TWR = 2cycle-1 = 01b (tWR = 17ns)
1816 4 TRRD = 2cycle = 0b (tRRD = 14ns)
1817 3-1 TRAS = 5cycle-1 = 100b (Tras=42ns)
1818 1 CAS latency = 3cyc = 1b
1819 (for Micron 2M32-7 operating at 100MHz)
1820 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001821 boot_loader_write_mem32(pao, dsp_index, C6713_EMIF_SDRAMEXT,
1822 0x001BDF29);
Eliot Blennerhassett1d595d22011-02-10 17:26:08 +13001823
1824 /* EMIF SDRAM control - set up for a 2Mx32 SDRAM (512x32x4 bank)
1825 31 - 0b -
1826 30 SDBSZ 1b 4 bank
1827 29..28 SDRSZ 00b 11 row address pins
1828
1829 27..26 SDCSZ 01b 8 column address pins
1830 25 RFEN 1b refersh enabled
1831 24 INIT 1b init SDRAM!
1832
1833 23..20 TRCD 0001b (Trcd/Tcyc)-1 = (20/10)-1 = 1
1834
1835 19..16 TRP 0001b (Trp/Tcyc)-1 = (20/10)-1 = 1
1836
1837 15..12 TRC 0110b (Trc/Tcyc)-1 = (70/10)-1 = 6
1838
1839 11..0 - 0000b 0000b 0000b
1840 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001841 boot_loader_write_mem32(pao, dsp_index, C6713_EMIF_SDRAMCTL,
Eliot Blennerhassett1d595d22011-02-10 17:26:08 +13001842 0x47116000);
1843
1844 /* SDRAM refresh timing
1845 Need 4,096 refresh cycles every 64ms = 15.625us = 1562cycles of 100MHz = 0x61A
1846 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001847 boot_loader_write_mem32(pao, dsp_index,
1848 C6713_EMIF_SDRAMTIMING, 0x00000410);
1849
1850 hpios_delay_micro_seconds(1000);
1851 } else if (dsp_index == 2) {
1852 /* DSP 2 is a C6713 */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001853 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001854
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001855 return 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001856}
1857
1858static u16 boot_loader_test_memory(struct hpi_adapter_obj *pao, int dsp_index,
1859 u32 start_address, u32 length)
1860{
1861 u32 i = 0, j = 0;
1862 u32 test_addr = 0;
1863 u32 test_data = 0, data = 0;
1864
1865 length = 1000;
1866
1867 /* for 1st word, test each bit in the 32bit word, */
1868 /* dwLength specifies number of 32bit words to test */
1869 /*for(i=0; i<dwLength; i++) */
1870 i = 0;
1871 {
1872 test_addr = start_address + i * 4;
1873 test_data = 0x00000001;
1874 for (j = 0; j < 32; j++) {
1875 boot_loader_write_mem32(pao, dsp_index, test_addr,
1876 test_data);
1877 data = boot_loader_read_mem32(pao, dsp_index,
1878 test_addr);
1879 if (data != test_data) {
1880 HPI_DEBUG_LOG(VERBOSE,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001881 "Memtest error details "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001882 "%08x %08x %08x %i\n", test_addr,
1883 test_data, data, dsp_index);
1884 return 1; /* error */
1885 }
1886 test_data = test_data << 1;
1887 } /* for(j) */
1888 } /* for(i) */
1889
1890 /* for the next 100 locations test each location, leaving it as zero */
1891 /* write a zero to the next word in memory before we read */
1892 /* the previous write to make sure every memory location is unique */
1893 for (i = 0; i < 100; i++) {
1894 test_addr = start_address + i * 4;
1895 test_data = 0xA5A55A5A;
1896 boot_loader_write_mem32(pao, dsp_index, test_addr, test_data);
1897 boot_loader_write_mem32(pao, dsp_index, test_addr + 4, 0);
1898 data = boot_loader_read_mem32(pao, dsp_index, test_addr);
1899 if (data != test_data) {
1900 HPI_DEBUG_LOG(VERBOSE,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001901 "Memtest error details "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001902 "%08x %08x %08x %i\n", test_addr, test_data,
1903 data, dsp_index);
1904 return 1; /* error */
1905 }
1906 /* leave location as zero */
1907 boot_loader_write_mem32(pao, dsp_index, test_addr, 0x0);
1908 }
1909
1910 /* zero out entire memory block */
1911 for (i = 0; i < length; i++) {
1912 test_addr = start_address + i * 4;
1913 boot_loader_write_mem32(pao, dsp_index, test_addr, 0x0);
1914 }
1915 return 0;
1916}
1917
1918static u16 boot_loader_test_internal_memory(struct hpi_adapter_obj *pao,
1919 int dsp_index)
1920{
1921 int err = 0;
1922 if (dsp_index == 0) {
1923 /* DSP 0 is a C6205 */
1924 /* 64K prog mem */
1925 err = boot_loader_test_memory(pao, dsp_index, 0x00000000,
1926 0x10000);
1927 if (!err)
1928 /* 64K data mem */
1929 err = boot_loader_test_memory(pao, dsp_index,
1930 0x80000000, 0x10000);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001931 } else if (dsp_index == 1) {
1932 /* DSP 1 is a C6713 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001933 /* 192K internal mem */
1934 err = boot_loader_test_memory(pao, dsp_index, 0x00000000,
1935 0x30000);
1936 if (!err)
1937 /* 64K internal mem / L2 cache */
1938 err = boot_loader_test_memory(pao, dsp_index,
1939 0x00030000, 0x10000);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001940 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001941
1942 if (err)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001943 return HPI6205_ERROR_DSP_INTMEM;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001944 else
1945 return 0;
1946}
1947
1948static u16 boot_loader_test_external_memory(struct hpi_adapter_obj *pao,
1949 int dsp_index)
1950{
1951 u32 dRAM_start_address = 0;
1952 u32 dRAM_size = 0;
1953
1954 if (dsp_index == 0) {
1955 /* only test for SDRAM if an ASI5000 card */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001956 if (pao->pci.pci_dev->subsystem_device == 0x5000) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001957 /* DSP 0 is always C6205 */
1958 dRAM_start_address = 0x00400000;
1959 dRAM_size = 0x200000;
1960 /*dwDRAMinc=1024; */
1961 } else
1962 return 0;
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001963 } else if (dsp_index == 1) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001964 /* DSP 1 is a C6713 */
1965 dRAM_start_address = 0x80000000;
1966 dRAM_size = 0x200000;
1967 /*dwDRAMinc=1024; */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001968 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001969
1970 if (boot_loader_test_memory(pao, dsp_index, dRAM_start_address,
1971 dRAM_size))
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001972 return HPI6205_ERROR_DSP_EXTMEM;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001973 return 0;
1974}
1975
1976static u16 boot_loader_test_pld(struct hpi_adapter_obj *pao, int dsp_index)
1977{
1978 u32 data = 0;
1979 if (dsp_index == 0) {
1980 /* only test for DSP0 PLD on ASI5000 card */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001981 if (pao->pci.pci_dev->subsystem_device == 0x5000) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001982 /* PLD is located at CE3=0x03000000 */
1983 data = boot_loader_read_mem32(pao, dsp_index,
1984 0x03000008);
1985 if ((data & 0xF) != 0x5)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001986 return HPI6205_ERROR_DSP_PLD;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001987 data = boot_loader_read_mem32(pao, dsp_index,
1988 0x0300000C);
1989 if ((data & 0xF) != 0xA)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001990 return HPI6205_ERROR_DSP_PLD;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001991 }
1992 } else if (dsp_index == 1) {
1993 /* DSP 1 is a C6713 */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001994 if (pao->pci.pci_dev->subsystem_device == 0x8700) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001995 /* PLD is located at CE1=0x90000000 */
1996 data = boot_loader_read_mem32(pao, dsp_index,
1997 0x90000010);
1998 if ((data & 0xFF) != 0xAA)
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13001999 return HPI6205_ERROR_DSP_PLD;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002000 /* 8713 - LED on */
2001 boot_loader_write_mem32(pao, dsp_index, 0x90000000,
2002 0x02);
2003 }
2004 }
2005 return 0;
2006}
2007
2008/** Transfer data to or from DSP
2009 nOperation = H620_H620_HIF_SEND_DATA or H620_HIF_GET_DATA
2010*/
2011static short hpi6205_transfer_data(struct hpi_adapter_obj *pao, u8 *p_data,
2012 u32 data_size, int operation)
2013{
2014 struct hpi_hw_obj *phw = pao->priv;
2015 u32 data_transferred = 0;
2016 u16 err = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002017 u32 temp2;
2018 struct bus_master_interface *interface = phw->p_interface_buffer;
2019
2020 if (!p_data)
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +13002021 return HPI_ERROR_INVALID_DATA_POINTER;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002022
2023 data_size &= ~3L; /* round data_size down to nearest 4 bytes */
2024
2025 /* make sure state is IDLE */
2026 if (!wait_dsp_ack(phw, H620_HIF_IDLE, HPI6205_TIMEOUT))
2027 return HPI_ERROR_DSP_HARDWARE;
2028
2029 while (data_transferred < data_size) {
2030 u32 this_copy = data_size - data_transferred;
2031
2032 if (this_copy > HPI6205_SIZEOF_DATA)
2033 this_copy = HPI6205_SIZEOF_DATA;
2034
2035 if (operation == H620_HIF_SEND_DATA)
2036 memcpy((void *)&interface->u.b_data[0],
2037 &p_data[data_transferred], this_copy);
2038
2039 interface->transfer_size_in_bytes = this_copy;
2040
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002041 /* DSP must change this back to nOperation */
2042 interface->dsp_ack = H620_HIF_IDLE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002043 send_dsp_command(phw, operation);
2044
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002045 temp2 = wait_dsp_ack(phw, operation, HPI6205_TIMEOUT);
2046 HPI_DEBUG_LOG(DEBUG, "spun %d times for data xfer of %d\n",
2047 HPI6205_TIMEOUT - temp2, this_copy);
2048
2049 if (!temp2) {
2050 /* timed out */
2051 HPI_DEBUG_LOG(ERROR,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002052 "Timed out waiting for " "state %d got %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002053 operation, interface->dsp_ack);
2054
2055 break;
2056 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002057 if (operation == H620_HIF_GET_DATA)
2058 memcpy(&p_data[data_transferred],
2059 (void *)&interface->u.b_data[0], this_copy);
2060
2061 data_transferred += this_copy;
2062 }
2063 if (interface->dsp_ack != operation)
2064 HPI_DEBUG_LOG(DEBUG, "interface->dsp_ack=%d, expected %d\n",
2065 interface->dsp_ack, operation);
2066 /* err=HPI_ERROR_DSP_HARDWARE; */
2067
2068 send_dsp_command(phw, H620_HIF_IDLE);
2069
2070 return err;
2071}
2072
2073/* wait for up to timeout_us microseconds for the DSP
2074 to signal state by DMA into dwDspAck
2075*/
2076static int wait_dsp_ack(struct hpi_hw_obj *phw, int state, int timeout_us)
2077{
2078 struct bus_master_interface *interface = phw->p_interface_buffer;
2079 int t = timeout_us / 4;
2080
2081 rmb(); /* ensure interface->dsp_ack is up to date */
2082 while ((interface->dsp_ack != state) && --t) {
2083 hpios_delay_micro_seconds(4);
2084 rmb(); /* DSP changes dsp_ack by DMA */
2085 }
2086
2087 /*HPI_DEBUG_LOG(VERBOSE, "Spun %d for %d\n", timeout_us/4-t, state); */
2088 return t * 4;
2089}
2090
2091/* set the busmaster interface to cmd, then interrupt the DSP */
2092static void send_dsp_command(struct hpi_hw_obj *phw, int cmd)
2093{
2094 struct bus_master_interface *interface = phw->p_interface_buffer;
2095
2096 u32 r;
2097
2098 interface->host_cmd = cmd;
2099 wmb(); /* DSP gets state by DMA, make sure it is written to memory */
2100 /* before we interrupt the DSP */
2101 r = ioread32(phw->prHDCR);
2102 r |= (u32)C6205_HDCR_DSPINT;
2103 iowrite32(r, phw->prHDCR);
2104 r &= ~(u32)C6205_HDCR_DSPINT;
2105 iowrite32(r, phw->prHDCR);
2106}
2107
2108static unsigned int message_count;
2109
2110static u16 message_response_sequence(struct hpi_adapter_obj *pao,
2111 struct hpi_message *phm, struct hpi_response *phr)
2112{
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002113 u32 time_out, time_out2;
2114 struct hpi_hw_obj *phw = pao->priv;
2115 struct bus_master_interface *interface = phw->p_interface_buffer;
2116 u16 err = 0;
2117
2118 message_count++;
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002119 if (phm->size > sizeof(interface->u)) {
Eliot Blennerhassettdeb21a22011-02-10 17:26:03 +13002120 phr->error = HPI_ERROR_MESSAGE_BUFFER_TOO_SMALL;
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002121 phr->specific_error = sizeof(interface->u);
2122 phr->size = sizeof(struct hpi_response_header);
2123 HPI_DEBUG_LOG(ERROR,
2124 "message len %d too big for buffer %ld \n", phm->size,
2125 sizeof(interface->u));
2126 return 0;
2127 }
2128
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002129 /* Assume buffer of type struct bus_master_interface
2130 is allocated "noncacheable" */
2131
2132 if (!wait_dsp_ack(phw, H620_HIF_IDLE, HPI6205_TIMEOUT)) {
2133 HPI_DEBUG_LOG(DEBUG, "timeout waiting for idle\n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002134 return HPI6205_ERROR_MSG_RESP_IDLE_TIMEOUT;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002135 }
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002136
2137 memcpy(&interface->u.message_buffer, phm, phm->size);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002138 /* signal we want a response */
2139 send_dsp_command(phw, H620_HIF_GET_RESP);
2140
2141 time_out2 = wait_dsp_ack(phw, H620_HIF_GET_RESP, HPI6205_TIMEOUT);
2142
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002143 if (!time_out2) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002144 HPI_DEBUG_LOG(ERROR,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002145 "(%u) Timed out waiting for " "GET_RESP state [%x]\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002146 message_count, interface->dsp_ack);
2147 } else {
2148 HPI_DEBUG_LOG(VERBOSE,
2149 "(%u) transition to GET_RESP after %u\n",
2150 message_count, HPI6205_TIMEOUT - time_out2);
2151 }
2152 /* spin waiting on HIF interrupt flag (end of msg process) */
2153 time_out = HPI6205_TIMEOUT;
2154
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002155 /* read the result */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002156 if (time_out) {
2157 if (interface->u.response_buffer.size <= phr->size)
2158 memcpy(phr, &interface->u.response_buffer,
2159 interface->u.response_buffer.size);
2160 else {
2161 HPI_DEBUG_LOG(ERROR,
2162 "response len %d too big for buffer %d\n",
2163 interface->u.response_buffer.size, phr->size);
2164 memcpy(phr, &interface->u.response_buffer,
2165 sizeof(struct hpi_response_header));
2166 phr->error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
2167 phr->specific_error =
2168 interface->u.response_buffer.size;
2169 phr->size = sizeof(struct hpi_response_header);
2170 }
2171 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002172 /* set interface back to idle */
2173 send_dsp_command(phw, H620_HIF_IDLE);
2174
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002175 if (!time_out || !time_out2) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002176 HPI_DEBUG_LOG(DEBUG, "something timed out!\n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002177 return HPI6205_ERROR_MSG_RESP_TIMEOUT;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002178 }
2179 /* special case for adapter close - */
2180 /* wait for the DSP to indicate it is idle */
2181 if (phm->function == HPI_ADAPTER_CLOSE) {
2182 if (!wait_dsp_ack(phw, H620_HIF_IDLE, HPI6205_TIMEOUT)) {
2183 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002184 "Timeout waiting for idle "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002185 "(on adapter_close)\n");
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +13002186 return HPI6205_ERROR_MSG_RESP_IDLE_TIMEOUT;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002187 }
2188 }
2189 err = hpi_validate_response(phm, phr);
2190 return err;
2191}
2192
2193static void hw_message(struct hpi_adapter_obj *pao, struct hpi_message *phm,
2194 struct hpi_response *phr)
2195{
2196
2197 u16 err = 0;
2198
2199 hpios_dsplock_lock(pao);
2200
2201 err = message_response_sequence(pao, phm, phr);
2202
2203 /* maybe an error response */
2204 if (err) {
2205 /* something failed in the HPI/DSP interface */
Eliot Blennerhassett0a000442011-02-10 17:26:05 +13002206 if (err >= HPI_ERROR_BACKEND_BASE) {
2207 phr->error = HPI_ERROR_DSP_COMMUNICATION;
2208 phr->specific_error = err;
2209 } else {
2210 phr->error = err;
2211 }
2212
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002213 pao->dsp_crashed++;
2214
2215 /* just the header of the response is valid */
2216 phr->size = sizeof(struct hpi_response_header);
2217 goto err;
2218 } else
2219 pao->dsp_crashed = 0;
2220
2221 if (phr->error != 0) /* something failed in the DSP */
2222 goto err;
2223
2224 switch (phm->function) {
2225 case HPI_OSTREAM_WRITE:
2226 case HPI_ISTREAM_ANC_WRITE:
2227 err = hpi6205_transfer_data(pao, phm->u.d.u.data.pb_data,
2228 phm->u.d.u.data.data_size, H620_HIF_SEND_DATA);
2229 break;
2230
2231 case HPI_ISTREAM_READ:
2232 case HPI_OSTREAM_ANC_READ:
2233 err = hpi6205_transfer_data(pao, phm->u.d.u.data.pb_data,
2234 phm->u.d.u.data.data_size, H620_HIF_GET_DATA);
2235 break;
2236
2237 case HPI_CONTROL_SET_STATE:
2238 if (phm->object == HPI_OBJ_CONTROLEX
2239 && phm->u.cx.attribute == HPI_COBRANET_SET_DATA)
2240 err = hpi6205_transfer_data(pao,
2241 phm->u.cx.u.cobranet_bigdata.pb_data,
2242 phm->u.cx.u.cobranet_bigdata.byte_count,
2243 H620_HIF_SEND_DATA);
2244 break;
2245
2246 case HPI_CONTROL_GET_STATE:
2247 if (phm->object == HPI_OBJ_CONTROLEX
2248 && phm->u.cx.attribute == HPI_COBRANET_GET_DATA)
2249 err = hpi6205_transfer_data(pao,
2250 phm->u.cx.u.cobranet_bigdata.pb_data,
2251 phr->u.cx.u.cobranet_data.byte_count,
2252 H620_HIF_GET_DATA);
2253 break;
2254 }
2255 phr->error = err;
2256
2257err:
2258 hpios_dsplock_unlock(pao);
2259
2260 return;
2261}