Path: blob/master/drivers/media/video/cx18/cx18-firmware.c
17778 views
/*1* cx18 firmware functions2*3* Copyright (C) 2007 Hans Verkuil <[email protected]>4* Copyright (C) 2008 Andy Walls <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (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 of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA19* 02111-1307 USA20*/2122#include "cx18-driver.h"23#include "cx18-io.h"24#include "cx18-scb.h"25#include "cx18-irq.h"26#include "cx18-firmware.h"27#include "cx18-cards.h"28#include <linux/firmware.h>2930#define CX18_PROC_SOFT_RESET 0xc7001031#define CX18_DDR_SOFT_RESET 0xc7001432#define CX18_CLOCK_SELECT1 0xc7100033#define CX18_CLOCK_SELECT2 0xc7100434#define CX18_HALF_CLOCK_SELECT1 0xc7100835#define CX18_HALF_CLOCK_SELECT2 0xc7100C36#define CX18_CLOCK_POLARITY1 0xc7101037#define CX18_CLOCK_POLARITY2 0xc7101438#define CX18_ADD_DELAY_ENABLE1 0xc7101839#define CX18_ADD_DELAY_ENABLE2 0xc7101C40#define CX18_CLOCK_ENABLE1 0xc7102041#define CX18_CLOCK_ENABLE2 0xc710244243#define CX18_REG_BUS_TIMEOUT_EN 0xc720244445#define CX18_FAST_CLOCK_PLL_INT 0xc7800046#define CX18_FAST_CLOCK_PLL_FRAC 0xc7800447#define CX18_FAST_CLOCK_PLL_POST 0xc7800848#define CX18_FAST_CLOCK_PLL_PRESCALE 0xc7800C49#define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc780105051#define CX18_SLOW_CLOCK_PLL_INT 0xc7801452#define CX18_SLOW_CLOCK_PLL_FRAC 0xc7801853#define CX18_SLOW_CLOCK_PLL_POST 0xc7801C54#define CX18_MPEG_CLOCK_PLL_INT 0xc7804055#define CX18_MPEG_CLOCK_PLL_FRAC 0xc7804456#define CX18_MPEG_CLOCK_PLL_POST 0xc7804857#define CX18_PLL_POWER_DOWN 0xc7808858#define CX18_SW1_INT_STATUS 0xc7310459#define CX18_SW1_INT_ENABLE_PCI 0xc7311C60#define CX18_SW2_INT_SET 0xc7314061#define CX18_SW2_INT_STATUS 0xc7314462#define CX18_ADEC_CONTROL 0xc781206364#define CX18_DDR_REQUEST_ENABLE 0xc8000065#define CX18_DDR_CHIP_CONFIG 0xc8000466#define CX18_DDR_REFRESH 0xc8000867#define CX18_DDR_TIMING1 0xc8000C68#define CX18_DDR_TIMING2 0xc8001069#define CX18_DDR_POWER_REG 0xc8001C7071#define CX18_DDR_TUNE_LANE 0xc8004872#define CX18_DDR_INITIAL_EMRS 0xc8005473#define CX18_DDR_MB_PER_ROW_7 0xc8009C74#define CX18_DDR_BASE_63_ADDR 0xc804FC7576#define CX18_WMB_CLIENT02 0xc9010877#define CX18_WMB_CLIENT05 0xc9011478#define CX18_WMB_CLIENT06 0xc9011879#define CX18_WMB_CLIENT07 0xc9011C80#define CX18_WMB_CLIENT08 0xc9012081#define CX18_WMB_CLIENT09 0xc9012482#define CX18_WMB_CLIENT10 0xc9012883#define CX18_WMB_CLIENT11 0xc9012C84#define CX18_WMB_CLIENT12 0xc9013085#define CX18_WMB_CLIENT13 0xc9013486#define CX18_WMB_CLIENT14 0xc901388788#define CX18_DSP0_INTERRUPT_MASK 0xd0004C8990#define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */91#define APU_ROM_SYNC2 0x72646548 /* "rdeH" */9293struct cx18_apu_rom_seghdr {94u32 sync1;95u32 sync2;96u32 addr;97u32 size;98};99100static int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx)101{102const struct firmware *fw = NULL;103int i, j;104unsigned size;105u32 __iomem *dst = (u32 __iomem *)mem;106const u32 *src;107108if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {109CX18_ERR("Unable to open firmware %s\n", fn);110CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");111return -ENOMEM;112}113114src = (const u32 *)fw->data;115116for (i = 0; i < fw->size; i += 4096) {117cx18_setup_page(cx, i);118for (j = i; j < fw->size && j < i + 4096; j += 4) {119/* no need for endianness conversion on the ppc */120cx18_raw_writel(cx, *src, dst);121if (cx18_raw_readl(cx, dst) != *src) {122CX18_ERR("Mismatch at offset %x\n", i);123release_firmware(fw);124cx18_setup_page(cx, 0);125return -EIO;126}127dst++;128src++;129}130}131if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))132CX18_INFO("loaded %s firmware (%zd bytes)\n", fn, fw->size);133size = fw->size;134release_firmware(fw);135cx18_setup_page(cx, SCB_OFFSET);136return size;137}138139static int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx,140u32 *entry_addr)141{142const struct firmware *fw = NULL;143int i, j;144unsigned size;145const u32 *src;146struct cx18_apu_rom_seghdr seghdr;147const u8 *vers;148u32 offset = 0;149u32 apu_version = 0;150int sz;151152if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {153CX18_ERR("unable to open firmware %s\n", fn);154CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");155cx18_setup_page(cx, 0);156return -ENOMEM;157}158159*entry_addr = 0;160src = (const u32 *)fw->data;161vers = fw->data + sizeof(seghdr);162sz = fw->size;163164apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];165while (offset + sizeof(seghdr) < fw->size) {166/* TODO: byteswapping */167memcpy(&seghdr, src + offset / 4, sizeof(seghdr));168offset += sizeof(seghdr);169if (seghdr.sync1 != APU_ROM_SYNC1 ||170seghdr.sync2 != APU_ROM_SYNC2) {171offset += seghdr.size;172continue;173}174CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,175seghdr.addr + seghdr.size - 1);176if (*entry_addr == 0)177*entry_addr = seghdr.addr;178if (offset + seghdr.size > sz)179break;180for (i = 0; i < seghdr.size; i += 4096) {181cx18_setup_page(cx, seghdr.addr + i);182for (j = i; j < seghdr.size && j < i + 4096; j += 4) {183/* no need for endianness conversion on the ppc */184cx18_raw_writel(cx, src[(offset + j) / 4],185dst + seghdr.addr + j);186if (cx18_raw_readl(cx, dst + seghdr.addr + j)187!= src[(offset + j) / 4]) {188CX18_ERR("Mismatch at offset %x\n",189offset + j);190release_firmware(fw);191cx18_setup_page(cx, 0);192return -EIO;193}194}195}196offset += seghdr.size;197}198if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))199CX18_INFO("loaded %s firmware V%08x (%zd bytes)\n",200fn, apu_version, fw->size);201size = fw->size;202release_firmware(fw);203cx18_setup_page(cx, 0);204return size;205}206207void cx18_halt_firmware(struct cx18 *cx)208{209CX18_DEBUG_INFO("Preparing for firmware halt.\n");210cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,2110x0000000F, 0x000F000F);212cx18_write_reg_expect(cx, 0x00020002, CX18_ADEC_CONTROL,2130x00000002, 0x00020002);214}215216void cx18_init_power(struct cx18 *cx, int lowpwr)217{218/* power-down Spare and AOM PLLs */219/* power-up fast, slow and mpeg PLLs */220cx18_write_reg(cx, 0x00000008, CX18_PLL_POWER_DOWN);221222/* ADEC out of sleep */223cx18_write_reg_expect(cx, 0x00020000, CX18_ADEC_CONTROL,2240x00000000, 0x00020002);225226/*227* The PLL parameters are based on the external crystal frequency that228* would ideally be:229*230* NTSC Color subcarrier freq * 8 =231* 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz232*233* The accidents of history and rationale that explain from where this234* combination of magic numbers originate can be found in:235*236* [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in237* the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80238*239* [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the240* NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83241*242* As Mike Bradley has rightly pointed out, it's not the exact crystal243* frequency that matters, only that all parts of the driver and244* firmware are using the same value (close to the ideal value).245*246* Since I have a strong suspicion that, if the firmware ever assumes a247* crystal value at all, it will assume 28.636360 MHz, the crystal248* freq used in calculations in this driver will be:249*250* xtal_freq = 28.636360 MHz251*252* an error of less than 0.13 ppm which is way, way better than any off253* the shelf crystal will have for accuracy anyway.254*255* Below I aim to run the PLLs' VCOs near 400 MHz to minimze errors.256*257* Many thanks to Jeff Campbell and Mike Bradley for their extensive258* investigation, experimentation, testing, and suggested solutions of259* of audio/video sync problems with SVideo and CVBS captures.260*/261262/* the fast clock is at 200/245 MHz */263/* 1 * xtal_freq * 0x0d.f7df9b8 / 2 = 200 MHz: 400 MHz pre post-divide*/264/* 1 * xtal_freq * 0x11.1c71eb8 / 2 = 245 MHz: 490 MHz pre post-divide*/265cx18_write_reg(cx, lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);266cx18_write_reg(cx, lowpwr ? 0x1EFBF37 : 0x038E3D7,267CX18_FAST_CLOCK_PLL_FRAC);268269cx18_write_reg(cx, 2, CX18_FAST_CLOCK_PLL_POST);270cx18_write_reg(cx, 1, CX18_FAST_CLOCK_PLL_PRESCALE);271cx18_write_reg(cx, 4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);272273/* set slow clock to 125/120 MHz */274/* xtal_freq * 0x0d.1861a20 / 3 = 125 MHz: 375 MHz before post-divide */275/* xtal_freq * 0x0c.92493f8 / 3 = 120 MHz: 360 MHz before post-divide */276cx18_write_reg(cx, lowpwr ? 0xD : 0xC, CX18_SLOW_CLOCK_PLL_INT);277cx18_write_reg(cx, lowpwr ? 0x30C344 : 0x124927F,278CX18_SLOW_CLOCK_PLL_FRAC);279cx18_write_reg(cx, 3, CX18_SLOW_CLOCK_PLL_POST);280281/* mpeg clock pll 54MHz */282/* xtal_freq * 0xf.15f17f0 / 8 = 54 MHz: 432 MHz before post-divide */283cx18_write_reg(cx, 0xF, CX18_MPEG_CLOCK_PLL_INT);284cx18_write_reg(cx, 0x2BE2FE, CX18_MPEG_CLOCK_PLL_FRAC);285cx18_write_reg(cx, 8, CX18_MPEG_CLOCK_PLL_POST);286287/* Defaults */288/* APU = SC or SC/2 = 125/62.5 */289/* EPU = SC = 125 */290/* DDR = FC = 180 */291/* ENC = SC = 125 */292/* AI1 = SC = 125 */293/* VIM2 = disabled */294/* PCI = FC/2 = 90 */295/* AI2 = disabled */296/* DEMUX = disabled */297/* AO = SC/2 = 62.5 */298/* SER = 54MHz */299/* VFC = disabled */300/* USB = disabled */301302if (lowpwr) {303cx18_write_reg_expect(cx, 0xFFFF0020, CX18_CLOCK_SELECT1,3040x00000020, 0xFFFFFFFF);305cx18_write_reg_expect(cx, 0xFFFF0004, CX18_CLOCK_SELECT2,3060x00000004, 0xFFFFFFFF);307} else {308/* This doesn't explicitly set every clock select */309cx18_write_reg_expect(cx, 0x00060004, CX18_CLOCK_SELECT1,3100x00000004, 0x00060006);311cx18_write_reg_expect(cx, 0x00060006, CX18_CLOCK_SELECT2,3120x00000006, 0x00060006);313}314315cx18_write_reg_expect(cx, 0xFFFF0002, CX18_HALF_CLOCK_SELECT1,3160x00000002, 0xFFFFFFFF);317cx18_write_reg_expect(cx, 0xFFFF0104, CX18_HALF_CLOCK_SELECT2,3180x00000104, 0xFFFFFFFF);319cx18_write_reg_expect(cx, 0xFFFF9026, CX18_CLOCK_ENABLE1,3200x00009026, 0xFFFFFFFF);321cx18_write_reg_expect(cx, 0xFFFF3105, CX18_CLOCK_ENABLE2,3220x00003105, 0xFFFFFFFF);323}324325void cx18_init_memory(struct cx18 *cx)326{327cx18_msleep_timeout(10, 0);328cx18_write_reg_expect(cx, 0x00010000, CX18_DDR_SOFT_RESET,3290x00000000, 0x00010001);330cx18_msleep_timeout(10, 0);331332cx18_write_reg(cx, cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);333334cx18_msleep_timeout(10, 0);335336cx18_write_reg(cx, cx->card->ddr.refresh, CX18_DDR_REFRESH);337cx18_write_reg(cx, cx->card->ddr.timing1, CX18_DDR_TIMING1);338cx18_write_reg(cx, cx->card->ddr.timing2, CX18_DDR_TIMING2);339340cx18_msleep_timeout(10, 0);341342/* Initialize DQS pad time */343cx18_write_reg(cx, cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);344cx18_write_reg(cx, cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);345346cx18_msleep_timeout(10, 0);347348cx18_write_reg_expect(cx, 0x00020000, CX18_DDR_SOFT_RESET,3490x00000000, 0x00020002);350cx18_msleep_timeout(10, 0);351352/* use power-down mode when idle */353cx18_write_reg(cx, 0x00000010, CX18_DDR_POWER_REG);354355cx18_write_reg_expect(cx, 0x00010001, CX18_REG_BUS_TIMEOUT_EN,3560x00000001, 0x00010001);357358cx18_write_reg(cx, 0x48, CX18_DDR_MB_PER_ROW_7);359cx18_write_reg(cx, 0xE0000, CX18_DDR_BASE_63_ADDR);360361cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT02); /* AO */362cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT09); /* AI2 */363cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT05); /* VIM1 */364cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT06); /* AI1 */365cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT07); /* 3D comb */366cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT10); /* ME */367cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT12); /* ENC */368cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT13); /* PK */369cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT11); /* RC */370cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT14); /* AVO */371}372373int cx18_firmware_init(struct cx18 *cx)374{375u32 fw_entry_addr;376int sz, retries;377u32 api_args[MAX_MB_ARGUMENTS];378379/* Allow chip to control CLKRUN */380cx18_write_reg(cx, 0x5, CX18_DSP0_INTERRUPT_MASK);381382/* Stop the firmware */383cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,3840x0000000F, 0x000F000F);385386cx18_msleep_timeout(1, 0);387388/* If the CPU is still running */389if ((cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 8) == 0) {390CX18_ERR("%s: couldn't stop CPU to load firmware\n", __func__);391return -EIO;392}393394cx18_sw1_irq_enable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);395cx18_sw2_irq_enable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);396397sz = load_cpu_fw_direct("v4l-cx23418-cpu.fw", cx->enc_mem, cx);398if (sz <= 0)399return sz;400401/* The SCB & IPC area *must* be correct before starting the firmwares */402cx18_init_scb(cx);403404fw_entry_addr = 0;405sz = load_apu_fw_direct("v4l-cx23418-apu.fw", cx->enc_mem, cx,406&fw_entry_addr);407if (sz <= 0)408return sz;409410/* Start the CPU. The CPU will take care of the APU for us. */411cx18_write_reg_expect(cx, 0x00080000, CX18_PROC_SOFT_RESET,4120x00000000, 0x00080008);413414/* Wait up to 500 ms for the APU to come out of reset */415for (retries = 0;416retries < 50 && (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1;417retries++)418cx18_msleep_timeout(10, 0);419420cx18_msleep_timeout(200, 0);421422if (retries == 50 &&423(cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1) {424CX18_ERR("Could not start the CPU\n");425return -EIO;426}427428/*429* The CPU had once before set up to receive an interrupt for it's430* outgoing IRQ_CPU_TO_EPU_ACK to us. If it ever does this, we get an431* interrupt when it sends us an ack, but by the time we process it,432* that flag in the SW2 status register has been cleared by the CPU433* firmware. We'll prevent that not so useful condition from happening434* by clearing the CPU's interrupt enables for Ack IRQ's we want to435* process.436*/437cx18_sw2_irq_disable_cpu(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);438439/* Try a benign command to see if the CPU is alive and well */440sz = cx18_vapi_result(cx, api_args, CX18_CPU_DEBUG_PEEK32, 1, 0);441if (sz < 0)442return sz;443444/* initialize GPIO */445cx18_write_reg_expect(cx, 0x14001400, 0xc78110, 0x00001400, 0x14001400);446return 0;447}448449450