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