blob: 522a035b2e8fb5c065e334ad3fc2482af654c000 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
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"
Andy Wallsb1526422008-08-30 16:03:44 -030023#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030024#include <linux/firmware.h>
25
Hans Verkuil81cb727d2008-06-28 12:49:20 -030026#define CX18_AUDIO_ENABLE 0xc72014
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#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;
David Howells9b8a3e42008-07-08 17:38:56 +010034 const u8 *ptr;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030035 int i;
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030036 int retries1 = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030037
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
Hans Verkuilf313da12008-06-28 08:03:02 -030043 /* The firmware load often has byte errors, so allow for several
44 retries, both at byte level and at the firmware load level. */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030045 while (retries1 < 5) {
Hans Verkuilf313da12008-06-28 08:03:02 -030046 cx18_av_write4(cx, CXADEC_CHIP_CTRL, 0x00010000);
47 cx18_av_write(cx, CXADEC_STD_DET_CTL, 0xf6);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030048
Hans Verkuilf313da12008-06-28 08:03:02 -030049 /* Reset the Mako core (Register is undocumented.) */
50 cx18_av_write4(cx, 0x8100, 0x00010000);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030051
Hans Verkuilf313da12008-06-28 08:03:02 -030052 /* Put the 8051 in reset and enable firmware upload */
Andy Wallsd267d852008-09-28 21:46:02 -030053 cx18_av_write4_noretry(cx, CXADEC_DL_CTL, 0x0F000000);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030054
Hans Verkuilf313da12008-06-28 08:03:02 -030055 ptr = fw->data;
56 size = fw->size;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030057
Hans Verkuilf313da12008-06-28 08:03:02 -030058 for (i = 0; i < size; i++) {
59 u32 dl_control = 0x0F000000 | i | ((u32)ptr[i] << 16);
60 u32 value = 0;
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030061 int retries2;
Andy Wallsd267d852008-09-28 21:46:02 -030062 int unrec_err = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030063
Andy Wallsd267d852008-09-28 21:46:02 -030064 for (retries2 = 0; retries2 < CX18_MAX_MMIO_RETRIES;
65 retries2++) {
66 cx18_av_write4_noretry(cx, CXADEC_DL_CTL,
67 dl_control);
Hans Verkuilf313da12008-06-28 08:03:02 -030068 udelay(10);
Andy Wallsd267d852008-09-28 21:46:02 -030069 value = cx18_av_read4_noretry(cx,
70 CXADEC_DL_CTL);
Hans Verkuilf313da12008-06-28 08:03:02 -030071 if (value == dl_control)
72 break;
73 /* Check if we can correct the byte by changing
74 the address. We can only write the lower
75 address byte of the address. */
76 if ((value & 0x3F00) != (dl_control & 0x3F00)) {
Andy Wallsd267d852008-09-28 21:46:02 -030077 unrec_err = 1;
Hans Verkuilf313da12008-06-28 08:03:02 -030078 break;
79 }
80 }
Andy Wallsd267d852008-09-28 21:46:02 -030081 cx18_log_write_retries(cx, retries2,
82 cx->reg_mem + 0xc40000 + CXADEC_DL_CTL);
83 if (unrec_err || retries2 >= CX18_MAX_MMIO_RETRIES)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030084 break;
85 }
Hans Verkuilf313da12008-06-28 08:03:02 -030086 if (i == size)
87 break;
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030088 retries1++;
Hans Verkuilf313da12008-06-28 08:03:02 -030089 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030090 if (retries1 >= 5) {
Hans Verkuilf313da12008-06-28 08:03:02 -030091 CX18_ERR("unable to load firmware %s\n", FWFILE);
92 release_firmware(fw);
93 return -EIO;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094 }
95
96 cx18_av_write4(cx, CXADEC_DL_CTL, 0x13000000 | fw->size);
97
98 /* Output to the 416 */
99 cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x78000);
100
101 /* Audio input control 1 set to Sony mode */
102 /* Audio output input 2 is 0 for slave operation input */
103 /* 0xC4000914[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
104 /* 0xC4000914[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
105 after WS transition for first bit of audio word. */
106 cx18_av_write4(cx, CXADEC_I2S_IN_CTL, 0x000000A0);
107
108 /* Audio output control 1 is set to Sony mode */
109 /* Audio output control 2 is set to 1 for master mode */
110 /* 0xC4000918[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
111 /* 0xC4000918[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
112 after WS transition for first bit of audio word. */
113 /* 0xC4000918[8]: 0 = slave operation, 1 = master (SCK_OUT and WS_OUT
114 are generated) */
115 cx18_av_write4(cx, CXADEC_I2S_OUT_CTL, 0x000001A0);
116
117 /* set alt I2s master clock to /16 and enable alt divider i2s
118 passthrough */
119 cx18_av_write4(cx, CXADEC_PIN_CFG3, 0x5000B687);
120
121 cx18_av_write4(cx, CXADEC_STD_DET_CTL, 0x000000F6);
122 /* CxDevWrReg(CXADEC_STD_DET_CTL, 0x000000FF); */
123
124 /* Set bit 0 in register 0x9CC to signify that this is MiniMe. */
125 /* Register 0x09CC is defined by the Merlin firmware, and doesn't
126 have a name in the spec. */
127 cx18_av_write4(cx, 0x09CC, 1);
128
Andy Wallsb1526422008-08-30 16:03:44 -0300129 v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
130 /* If bit 11 is 1, clear bit 10 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300131 if (v & 0x800)
Andy Wallsb1526422008-08-30 16:03:44 -0300132 cx18_write_reg(cx, v & 0xFFFFFBFF, CX18_AUDIO_ENABLE);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300133
134 /* Enable WW auto audio standard detection */
135 v = cx18_av_read4(cx, CXADEC_STD_DET_CTL);
136 v |= 0xFF; /* Auto by default */
137 v |= 0x400; /* Stereo by default */
138 v |= 0x14000000;
139 cx18_av_write4(cx, CXADEC_STD_DET_CTL, v);
140
141 release_firmware(fw);
142
143 CX18_INFO("loaded %s firmware (%d bytes)\n", FWFILE, size);
144 return 0;
145}