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