V4L/DVB (8913): cx18: Create cx18_ specific wrappers for all pci mmio accessesors.
[linux-2.6-block.git] / drivers / media / video / cx18 / cx18-av-firmware.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 ADEC firmware functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 */
21
22#include "cx18-driver.h"
b1526421 23#include "cx18-io.h"
1c1e45d1
HV
24#include <linux/firmware.h>
25
81cb727d 26#define CX18_AUDIO_ENABLE 0xc72014
1c1e45d1
HV
27#define FWFILE "v4l-cx23418-dig.fw"
28
29int cx18_av_loadfw(struct cx18 *cx)
30{
31 const struct firmware *fw = NULL;
32 u32 size;
33 u32 v;
9b8a3e4c 34 const u8 *ptr;
1c1e45d1 35 int i;
c6eb8eaf 36 int retries1 = 0;
1c1e45d1
HV
37
38 if (request_firmware(&fw, FWFILE, &cx->dev->dev) != 0) {
39 CX18_ERR("unable to open firmware %s\n", FWFILE);
40 return -EINVAL;
41 }
42
f313da11
HV
43 /* The firmware load often has byte errors, so allow for several
44 retries, both at byte level and at the firmware load level. */
c6eb8eaf 45 while (retries1 < 5) {
f313da11
HV
46 cx18_av_write4(cx, CXADEC_CHIP_CTRL, 0x00010000);
47 cx18_av_write(cx, CXADEC_STD_DET_CTL, 0xf6);
1c1e45d1 48
f313da11
HV
49 /* Reset the Mako core (Register is undocumented.) */
50 cx18_av_write4(cx, 0x8100, 0x00010000);
1c1e45d1 51
f313da11
HV
52 /* Put the 8051 in reset and enable firmware upload */
53 cx18_av_write4(cx, CXADEC_DL_CTL, 0x0F000000);
1c1e45d1 54
f313da11
HV
55 ptr = fw->data;
56 size = fw->size;
1c1e45d1 57
f313da11
HV
58 for (i = 0; i < size; i++) {
59 u32 dl_control = 0x0F000000 | i | ((u32)ptr[i] << 16);
60 u32 value = 0;
c6eb8eaf 61 int retries2;
1c1e45d1 62
c6eb8eaf 63 for (retries2 = 0; retries2 < 5; retries2++) {
f313da11
HV
64 cx18_av_write4(cx, CXADEC_DL_CTL, dl_control);
65 udelay(10);
66 value = cx18_av_read4(cx, CXADEC_DL_CTL);
67 if (value == dl_control)
68 break;
69 /* Check if we can correct the byte by changing
70 the address. We can only write the lower
71 address byte of the address. */
72 if ((value & 0x3F00) != (dl_control & 0x3F00)) {
c6eb8eaf 73 retries2 = 5;
f313da11
HV
74 break;
75 }
76 }
c6eb8eaf 77 if (retries2 >= 5)
1c1e45d1
HV
78 break;
79 }
f313da11
HV
80 if (i == size)
81 break;
c6eb8eaf 82 retries1++;
f313da11 83 }
c6eb8eaf 84 if (retries1 >= 5) {
f313da11
HV
85 CX18_ERR("unable to load firmware %s\n", FWFILE);
86 release_firmware(fw);
87 return -EIO;
1c1e45d1
HV
88 }
89
90 cx18_av_write4(cx, CXADEC_DL_CTL, 0x13000000 | fw->size);
91
92 /* Output to the 416 */
93 cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x78000);
94
95 /* Audio input control 1 set to Sony mode */
96 /* Audio output input 2 is 0 for slave operation input */
97 /* 0xC4000914[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
98 /* 0xC4000914[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
99 after WS transition for first bit of audio word. */
100 cx18_av_write4(cx, CXADEC_I2S_IN_CTL, 0x000000A0);
101
102 /* Audio output control 1 is set to Sony mode */
103 /* Audio output control 2 is set to 1 for master mode */
104 /* 0xC4000918[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
105 /* 0xC4000918[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
106 after WS transition for first bit of audio word. */
107 /* 0xC4000918[8]: 0 = slave operation, 1 = master (SCK_OUT and WS_OUT
108 are generated) */
109 cx18_av_write4(cx, CXADEC_I2S_OUT_CTL, 0x000001A0);
110
111 /* set alt I2s master clock to /16 and enable alt divider i2s
112 passthrough */
113 cx18_av_write4(cx, CXADEC_PIN_CFG3, 0x5000B687);
114
115 cx18_av_write4(cx, CXADEC_STD_DET_CTL, 0x000000F6);
116 /* CxDevWrReg(CXADEC_STD_DET_CTL, 0x000000FF); */
117
118 /* Set bit 0 in register 0x9CC to signify that this is MiniMe. */
119 /* Register 0x09CC is defined by the Merlin firmware, and doesn't
120 have a name in the spec. */
121 cx18_av_write4(cx, 0x09CC, 1);
122
b1526421
AW
123 v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
124 /* If bit 11 is 1, clear bit 10 */
1c1e45d1 125 if (v & 0x800)
b1526421 126 cx18_write_reg(cx, v & 0xFFFFFBFF, CX18_AUDIO_ENABLE);
1c1e45d1
HV
127
128 /* Enable WW auto audio standard detection */
129 v = cx18_av_read4(cx, CXADEC_STD_DET_CTL);
130 v |= 0xFF; /* Auto by default */
131 v |= 0x400; /* Stereo by default */
132 v |= 0x14000000;
133 cx18_av_write4(cx, CXADEC_STD_DET_CTL, v);
134
135 release_firmware(fw);
136
137 CX18_INFO("loaded %s firmware (%d bytes)\n", FWFILE, size);
138 return 0;
139}