xref: /openbmc/linux/drivers/media/pci/cx18/cx18-av-firmware.c (revision 6beb1388a0b5730337360818d9385808e50aa870)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  cx18 ADEC firmware functions
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
5b285192aSMauro Carvalho Chehab  *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
6b285192aSMauro Carvalho Chehab  *
7b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or
8b285192aSMauro Carvalho Chehab  *  modify it under the terms of the GNU General Public License
9b285192aSMauro Carvalho Chehab  *  as published by the Free Software Foundation; either version 2
10b285192aSMauro Carvalho Chehab  *  of the License, or (at your option) any later version.
11b285192aSMauro Carvalho Chehab  *
12b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
13b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
16b285192aSMauro Carvalho Chehab  *
17b285192aSMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
18b285192aSMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
19b285192aSMauro Carvalho Chehab  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20b285192aSMauro Carvalho Chehab  *  02110-1301, USA.
21b285192aSMauro Carvalho Chehab  */
22b285192aSMauro Carvalho Chehab 
23b285192aSMauro Carvalho Chehab #include "cx18-driver.h"
24b285192aSMauro Carvalho Chehab #include "cx18-io.h"
25b285192aSMauro Carvalho Chehab #include <linux/firmware.h>
26b285192aSMauro Carvalho Chehab 
27b285192aSMauro Carvalho Chehab #define CX18_AUDIO_ENABLE    0xc72014
28b285192aSMauro Carvalho Chehab #define CX18_AI1_MUX_MASK    0x30
29b285192aSMauro Carvalho Chehab #define CX18_AI1_MUX_I2S1    0x00
30b285192aSMauro Carvalho Chehab #define CX18_AI1_MUX_I2S2    0x10
31b285192aSMauro Carvalho Chehab #define CX18_AI1_MUX_843_I2S 0x20
32b285192aSMauro Carvalho Chehab #define CX18_AI1_MUX_INVALID 0x30
33b285192aSMauro Carvalho Chehab 
34b285192aSMauro Carvalho Chehab #define FWFILE "v4l-cx23418-dig.fw"
35b285192aSMauro Carvalho Chehab 
36b285192aSMauro Carvalho Chehab static int cx18_av_verifyfw(struct cx18 *cx, const struct firmware *fw)
37b285192aSMauro Carvalho Chehab {
38b285192aSMauro Carvalho Chehab 	struct v4l2_subdev *sd = &cx->av_state.sd;
39b285192aSMauro Carvalho Chehab 	int ret = 0;
40b285192aSMauro Carvalho Chehab 	const u8 *data;
41b285192aSMauro Carvalho Chehab 	u32 size;
42b285192aSMauro Carvalho Chehab 	int addr;
43b285192aSMauro Carvalho Chehab 	u32 expected, dl_control;
44b285192aSMauro Carvalho Chehab 
45b285192aSMauro Carvalho Chehab 	/* Ensure we put the 8051 in reset and enable firmware upload mode */
46b285192aSMauro Carvalho Chehab 	dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
47b285192aSMauro Carvalho Chehab 	do {
48b285192aSMauro Carvalho Chehab 		dl_control &= 0x00ffffff;
49b285192aSMauro Carvalho Chehab 		dl_control |= 0x0f000000;
50b285192aSMauro Carvalho Chehab 		cx18_av_write4_noretry(cx, CXADEC_DL_CTL, dl_control);
51b285192aSMauro Carvalho Chehab 		dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
52b285192aSMauro Carvalho Chehab 	} while ((dl_control & 0xff000000) != 0x0f000000);
53b285192aSMauro Carvalho Chehab 
54b285192aSMauro Carvalho Chehab 	/* Read and auto increment until at address 0x0000 */
55b285192aSMauro Carvalho Chehab 	while (dl_control & 0x3fff)
56b285192aSMauro Carvalho Chehab 		dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
57b285192aSMauro Carvalho Chehab 
58b285192aSMauro Carvalho Chehab 	data = fw->data;
59b285192aSMauro Carvalho Chehab 	size = fw->size;
60b285192aSMauro Carvalho Chehab 	for (addr = 0; addr < size; addr++) {
61b285192aSMauro Carvalho Chehab 		dl_control &= 0xffff3fff; /* ignore top 2 bits of address */
62b285192aSMauro Carvalho Chehab 		expected = 0x0f000000 | ((u32)data[addr] << 16) | addr;
63b285192aSMauro Carvalho Chehab 		if (expected != dl_control) {
64*6beb1388SMauro Carvalho Chehab 			CX18_ERR_DEV(sd, "verification of %s firmware load failed: expected %#010x got %#010x\n",
65b285192aSMauro Carvalho Chehab 				     FWFILE, expected, dl_control);
66b285192aSMauro Carvalho Chehab 			ret = -EIO;
67b285192aSMauro Carvalho Chehab 			break;
68b285192aSMauro Carvalho Chehab 		}
69b285192aSMauro Carvalho Chehab 		dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
70b285192aSMauro Carvalho Chehab 	}
71b285192aSMauro Carvalho Chehab 	if (ret == 0)
72b285192aSMauro Carvalho Chehab 		CX18_INFO_DEV(sd, "verified load of %s firmware (%d bytes)\n",
73b285192aSMauro Carvalho Chehab 			      FWFILE, size);
74b285192aSMauro Carvalho Chehab 	return ret;
75b285192aSMauro Carvalho Chehab }
76b285192aSMauro Carvalho Chehab 
77b285192aSMauro Carvalho Chehab int cx18_av_loadfw(struct cx18 *cx)
78b285192aSMauro Carvalho Chehab {
79b285192aSMauro Carvalho Chehab 	struct v4l2_subdev *sd = &cx->av_state.sd;
80b285192aSMauro Carvalho Chehab 	const struct firmware *fw = NULL;
81b285192aSMauro Carvalho Chehab 	u32 size;
82b285192aSMauro Carvalho Chehab 	u32 u, v;
83b285192aSMauro Carvalho Chehab 	const u8 *ptr;
84b285192aSMauro Carvalho Chehab 	int i;
85b285192aSMauro Carvalho Chehab 	int retries1 = 0;
86b285192aSMauro Carvalho Chehab 
87b285192aSMauro Carvalho Chehab 	if (request_firmware(&fw, FWFILE, &cx->pci_dev->dev) != 0) {
88b285192aSMauro Carvalho Chehab 		CX18_ERR_DEV(sd, "unable to open firmware %s\n", FWFILE);
89b285192aSMauro Carvalho Chehab 		return -EINVAL;
90b285192aSMauro Carvalho Chehab 	}
91b285192aSMauro Carvalho Chehab 
92b285192aSMauro Carvalho Chehab 	/* The firmware load often has byte errors, so allow for several
93b285192aSMauro Carvalho Chehab 	   retries, both at byte level and at the firmware load level. */
94b285192aSMauro Carvalho Chehab 	while (retries1 < 5) {
95b285192aSMauro Carvalho Chehab 		cx18_av_write4_expect(cx, CXADEC_CHIP_CTRL, 0x00010000,
96b285192aSMauro Carvalho Chehab 					  0x00008430, 0xffffffff); /* cx25843 */
97b285192aSMauro Carvalho Chehab 		cx18_av_write_expect(cx, CXADEC_STD_DET_CTL, 0xf6, 0xf6, 0xff);
98b285192aSMauro Carvalho Chehab 
99b285192aSMauro Carvalho Chehab 		/* Reset the Mako core, Register is alias of CXADEC_CHIP_CTRL */
100b285192aSMauro Carvalho Chehab 		cx18_av_write4_expect(cx, 0x8100, 0x00010000,
101b285192aSMauro Carvalho Chehab 					  0x00008430, 0xffffffff); /* cx25843 */
102b285192aSMauro Carvalho Chehab 
103b285192aSMauro Carvalho Chehab 		/* Put the 8051 in reset and enable firmware upload */
104b285192aSMauro Carvalho Chehab 		cx18_av_write4_noretry(cx, CXADEC_DL_CTL, 0x0F000000);
105b285192aSMauro Carvalho Chehab 
106b285192aSMauro Carvalho Chehab 		ptr = fw->data;
107b285192aSMauro Carvalho Chehab 		size = fw->size;
108b285192aSMauro Carvalho Chehab 
109b285192aSMauro Carvalho Chehab 		for (i = 0; i < size; i++) {
110b285192aSMauro Carvalho Chehab 			u32 dl_control = 0x0F000000 | i | ((u32)ptr[i] << 16);
111b285192aSMauro Carvalho Chehab 			u32 value = 0;
112b285192aSMauro Carvalho Chehab 			int retries2;
113b285192aSMauro Carvalho Chehab 			int unrec_err = 0;
114b285192aSMauro Carvalho Chehab 
115b285192aSMauro Carvalho Chehab 			for (retries2 = 0; retries2 < CX18_MAX_MMIO_WR_RETRIES;
116b285192aSMauro Carvalho Chehab 			     retries2++) {
117b285192aSMauro Carvalho Chehab 				cx18_av_write4_noretry(cx, CXADEC_DL_CTL,
118b285192aSMauro Carvalho Chehab 						       dl_control);
119b285192aSMauro Carvalho Chehab 				udelay(10);
120b285192aSMauro Carvalho Chehab 				value = cx18_av_read4(cx, CXADEC_DL_CTL);
121b285192aSMauro Carvalho Chehab 				if (value == dl_control)
122b285192aSMauro Carvalho Chehab 					break;
123b285192aSMauro Carvalho Chehab 				/* Check if we can correct the byte by changing
124b285192aSMauro Carvalho Chehab 				   the address.  We can only write the lower
125b285192aSMauro Carvalho Chehab 				   address byte of the address. */
126b285192aSMauro Carvalho Chehab 				if ((value & 0x3F00) != (dl_control & 0x3F00)) {
127b285192aSMauro Carvalho Chehab 					unrec_err = 1;
128b285192aSMauro Carvalho Chehab 					break;
129b285192aSMauro Carvalho Chehab 				}
130b285192aSMauro Carvalho Chehab 			}
131b285192aSMauro Carvalho Chehab 			if (unrec_err || retries2 >= CX18_MAX_MMIO_WR_RETRIES)
132b285192aSMauro Carvalho Chehab 				break;
133b285192aSMauro Carvalho Chehab 		}
134b285192aSMauro Carvalho Chehab 		if (i == size)
135b285192aSMauro Carvalho Chehab 			break;
136b285192aSMauro Carvalho Chehab 		retries1++;
137b285192aSMauro Carvalho Chehab 	}
138b285192aSMauro Carvalho Chehab 	if (retries1 >= 5) {
139b285192aSMauro Carvalho Chehab 		CX18_ERR_DEV(sd, "unable to load firmware %s\n", FWFILE);
140b285192aSMauro Carvalho Chehab 		release_firmware(fw);
141b285192aSMauro Carvalho Chehab 		return -EIO;
142b285192aSMauro Carvalho Chehab 	}
143b285192aSMauro Carvalho Chehab 
144b285192aSMauro Carvalho Chehab 	cx18_av_write4_expect(cx, CXADEC_DL_CTL,
145b285192aSMauro Carvalho Chehab 				0x03000000 | fw->size, 0x03000000, 0x13000000);
146b285192aSMauro Carvalho Chehab 
147b285192aSMauro Carvalho Chehab 	CX18_INFO_DEV(sd, "loaded %s firmware (%d bytes)\n", FWFILE, size);
148b285192aSMauro Carvalho Chehab 
149b285192aSMauro Carvalho Chehab 	if (cx18_av_verifyfw(cx, fw) == 0)
150b285192aSMauro Carvalho Chehab 		cx18_av_write4_expect(cx, CXADEC_DL_CTL,
151b285192aSMauro Carvalho Chehab 				0x13000000 | fw->size, 0x13000000, 0x13000000);
152b285192aSMauro Carvalho Chehab 
153b285192aSMauro Carvalho Chehab 	/* Output to the 416 */
154b285192aSMauro Carvalho Chehab 	cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x78000);
155b285192aSMauro Carvalho Chehab 
156b285192aSMauro Carvalho Chehab 	/* Audio input control 1 set to Sony mode */
157b285192aSMauro Carvalho Chehab 	/* Audio output input 2 is 0 for slave operation input */
158b285192aSMauro Carvalho Chehab 	/* 0xC4000914[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
159b285192aSMauro Carvalho Chehab 	/* 0xC4000914[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
160b285192aSMauro Carvalho Chehab 	   after WS transition for first bit of audio word. */
161b285192aSMauro Carvalho Chehab 	cx18_av_write4(cx, CXADEC_I2S_IN_CTL, 0x000000A0);
162b285192aSMauro Carvalho Chehab 
163b285192aSMauro Carvalho Chehab 	/* Audio output control 1 is set to Sony mode */
164b285192aSMauro Carvalho Chehab 	/* Audio output control 2 is set to 1 for master mode */
165b285192aSMauro Carvalho Chehab 	/* 0xC4000918[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
166b285192aSMauro Carvalho Chehab 	/* 0xC4000918[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
167b285192aSMauro Carvalho Chehab 	   after WS transition for first bit of audio word. */
168b285192aSMauro Carvalho Chehab 	/* 0xC4000918[8]: 0 = slave operation, 1 = master (SCK_OUT and WS_OUT
169b285192aSMauro Carvalho Chehab 	   are generated) */
170b285192aSMauro Carvalho Chehab 	cx18_av_write4(cx, CXADEC_I2S_OUT_CTL, 0x000001A0);
171b285192aSMauro Carvalho Chehab 
172b285192aSMauro Carvalho Chehab 	/* set alt I2s master clock to /0x16 and enable alt divider i2s
173b285192aSMauro Carvalho Chehab 	   passthrough */
174b285192aSMauro Carvalho Chehab 	cx18_av_write4(cx, CXADEC_PIN_CFG3, 0x5600B687);
175b285192aSMauro Carvalho Chehab 
176b285192aSMauro Carvalho Chehab 	cx18_av_write4_expect(cx, CXADEC_STD_DET_CTL, 0x000000F6, 0x000000F6,
177b285192aSMauro Carvalho Chehab 								  0x3F00FFFF);
178b285192aSMauro Carvalho Chehab 	/* CxDevWrReg(CXADEC_STD_DET_CTL, 0x000000FF); */
179b285192aSMauro Carvalho Chehab 
180b285192aSMauro Carvalho Chehab 	/* Set bit 0 in register 0x9CC to signify that this is MiniMe. */
181b285192aSMauro Carvalho Chehab 	/* Register 0x09CC is defined by the Merlin firmware, and doesn't
182b285192aSMauro Carvalho Chehab 	   have a name in the spec. */
183b285192aSMauro Carvalho Chehab 	cx18_av_write4(cx, 0x09CC, 1);
184b285192aSMauro Carvalho Chehab 
185b285192aSMauro Carvalho Chehab 	v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
186b285192aSMauro Carvalho Chehab 	/* If bit 11 is 1, clear bit 10 */
187b285192aSMauro Carvalho Chehab 	if (v & 0x800)
188b285192aSMauro Carvalho Chehab 		cx18_write_reg_expect(cx, v & 0xFFFFFBFF, CX18_AUDIO_ENABLE,
189b285192aSMauro Carvalho Chehab 				      0, 0x400);
190b285192aSMauro Carvalho Chehab 
191b285192aSMauro Carvalho Chehab 	/* Toggle the AI1 MUX */
192b285192aSMauro Carvalho Chehab 	v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
193b285192aSMauro Carvalho Chehab 	u = v & CX18_AI1_MUX_MASK;
194b285192aSMauro Carvalho Chehab 	v &= ~CX18_AI1_MUX_MASK;
195b285192aSMauro Carvalho Chehab 	if (u == CX18_AI1_MUX_843_I2S || u == CX18_AI1_MUX_INVALID) {
196b285192aSMauro Carvalho Chehab 		/* Switch to I2S1 */
197b285192aSMauro Carvalho Chehab 		v |= CX18_AI1_MUX_I2S1;
198b285192aSMauro Carvalho Chehab 		cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
199b285192aSMauro Carvalho Chehab 				      v, CX18_AI1_MUX_MASK);
200b285192aSMauro Carvalho Chehab 		/* Switch back to the A/V decoder core I2S output */
201b285192aSMauro Carvalho Chehab 		v = (v & ~CX18_AI1_MUX_MASK) | CX18_AI1_MUX_843_I2S;
202b285192aSMauro Carvalho Chehab 	} else {
203b285192aSMauro Carvalho Chehab 		/* Switch to the A/V decoder core I2S output */
204b285192aSMauro Carvalho Chehab 		v |= CX18_AI1_MUX_843_I2S;
205b285192aSMauro Carvalho Chehab 		cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
206b285192aSMauro Carvalho Chehab 				      v, CX18_AI1_MUX_MASK);
207b285192aSMauro Carvalho Chehab 		/* Switch back to I2S1 or I2S2 */
208b285192aSMauro Carvalho Chehab 		v = (v & ~CX18_AI1_MUX_MASK) | u;
209b285192aSMauro Carvalho Chehab 	}
210b285192aSMauro Carvalho Chehab 	cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
211b285192aSMauro Carvalho Chehab 			      v, CX18_AI1_MUX_MASK);
212b285192aSMauro Carvalho Chehab 
213b285192aSMauro Carvalho Chehab 	/* Enable WW auto audio standard detection */
214b285192aSMauro Carvalho Chehab 	v = cx18_av_read4(cx, CXADEC_STD_DET_CTL);
215b285192aSMauro Carvalho Chehab 	v |= 0xFF;   /* Auto by default */
216b285192aSMauro Carvalho Chehab 	v |= 0x400;  /* Stereo by default */
217b285192aSMauro Carvalho Chehab 	v |= 0x14000000;
218b285192aSMauro Carvalho Chehab 	cx18_av_write4_expect(cx, CXADEC_STD_DET_CTL, v, v, 0x3F00FFFF);
219b285192aSMauro Carvalho Chehab 
220b285192aSMauro Carvalho Chehab 	release_firmware(fw);
221b285192aSMauro Carvalho Chehab 	return 0;
222b285192aSMauro Carvalho Chehab }
223b285192aSMauro Carvalho Chehab 
224b285192aSMauro Carvalho Chehab MODULE_FIRMWARE(FWFILE);
225