18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  cx18 firmware functions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
68c2ecf20Sopenharmony_ci *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "cx18-driver.h"
108c2ecf20Sopenharmony_ci#include "cx18-io.h"
118c2ecf20Sopenharmony_ci#include "cx18-scb.h"
128c2ecf20Sopenharmony_ci#include "cx18-irq.h"
138c2ecf20Sopenharmony_ci#include "cx18-firmware.h"
148c2ecf20Sopenharmony_ci#include "cx18-cards.h"
158c2ecf20Sopenharmony_ci#include <linux/firmware.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define CX18_PROC_SOFT_RESET		0xc70010
188c2ecf20Sopenharmony_ci#define CX18_DDR_SOFT_RESET		0xc70014
198c2ecf20Sopenharmony_ci#define CX18_CLOCK_SELECT1		0xc71000
208c2ecf20Sopenharmony_ci#define CX18_CLOCK_SELECT2		0xc71004
218c2ecf20Sopenharmony_ci#define CX18_HALF_CLOCK_SELECT1		0xc71008
228c2ecf20Sopenharmony_ci#define CX18_HALF_CLOCK_SELECT2		0xc7100C
238c2ecf20Sopenharmony_ci#define CX18_CLOCK_POLARITY1		0xc71010
248c2ecf20Sopenharmony_ci#define CX18_CLOCK_POLARITY2		0xc71014
258c2ecf20Sopenharmony_ci#define CX18_ADD_DELAY_ENABLE1		0xc71018
268c2ecf20Sopenharmony_ci#define CX18_ADD_DELAY_ENABLE2		0xc7101C
278c2ecf20Sopenharmony_ci#define CX18_CLOCK_ENABLE1		0xc71020
288c2ecf20Sopenharmony_ci#define CX18_CLOCK_ENABLE2		0xc71024
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define CX18_REG_BUS_TIMEOUT_EN		0xc72024
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define CX18_FAST_CLOCK_PLL_INT		0xc78000
338c2ecf20Sopenharmony_ci#define CX18_FAST_CLOCK_PLL_FRAC	0xc78004
348c2ecf20Sopenharmony_ci#define CX18_FAST_CLOCK_PLL_POST	0xc78008
358c2ecf20Sopenharmony_ci#define CX18_FAST_CLOCK_PLL_PRESCALE	0xc7800C
368c2ecf20Sopenharmony_ci#define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc78010
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define CX18_SLOW_CLOCK_PLL_INT		0xc78014
398c2ecf20Sopenharmony_ci#define CX18_SLOW_CLOCK_PLL_FRAC	0xc78018
408c2ecf20Sopenharmony_ci#define CX18_SLOW_CLOCK_PLL_POST	0xc7801C
418c2ecf20Sopenharmony_ci#define CX18_MPEG_CLOCK_PLL_INT		0xc78040
428c2ecf20Sopenharmony_ci#define CX18_MPEG_CLOCK_PLL_FRAC	0xc78044
438c2ecf20Sopenharmony_ci#define CX18_MPEG_CLOCK_PLL_POST	0xc78048
448c2ecf20Sopenharmony_ci#define CX18_PLL_POWER_DOWN		0xc78088
458c2ecf20Sopenharmony_ci#define CX18_SW1_INT_STATUS             0xc73104
468c2ecf20Sopenharmony_ci#define CX18_SW1_INT_ENABLE_PCI         0xc7311C
478c2ecf20Sopenharmony_ci#define CX18_SW2_INT_SET                0xc73140
488c2ecf20Sopenharmony_ci#define CX18_SW2_INT_STATUS             0xc73144
498c2ecf20Sopenharmony_ci#define CX18_ADEC_CONTROL		0xc78120
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define CX18_DDR_REQUEST_ENABLE		0xc80000
528c2ecf20Sopenharmony_ci#define CX18_DDR_CHIP_CONFIG		0xc80004
538c2ecf20Sopenharmony_ci#define CX18_DDR_REFRESH		0xc80008
548c2ecf20Sopenharmony_ci#define CX18_DDR_TIMING1		0xc8000C
558c2ecf20Sopenharmony_ci#define CX18_DDR_TIMING2		0xc80010
568c2ecf20Sopenharmony_ci#define CX18_DDR_POWER_REG		0xc8001C
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define CX18_DDR_TUNE_LANE		0xc80048
598c2ecf20Sopenharmony_ci#define CX18_DDR_INITIAL_EMRS		0xc80054
608c2ecf20Sopenharmony_ci#define CX18_DDR_MB_PER_ROW_7		0xc8009C
618c2ecf20Sopenharmony_ci#define CX18_DDR_BASE_63_ADDR		0xc804FC
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT02		0xc90108
648c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT05		0xc90114
658c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT06		0xc90118
668c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT07		0xc9011C
678c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT08		0xc90120
688c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT09		0xc90124
698c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT10		0xc90128
708c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT11		0xc9012C
718c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT12		0xc90130
728c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT13		0xc90134
738c2ecf20Sopenharmony_ci#define CX18_WMB_CLIENT14		0xc90138
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define CX18_DSP0_INTERRUPT_MASK	0xd0004C
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */
788c2ecf20Sopenharmony_ci#define APU_ROM_SYNC2 0x72646548 /* "rdeH" */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistruct cx18_apu_rom_seghdr {
818c2ecf20Sopenharmony_ci	u32 sync1;
828c2ecf20Sopenharmony_ci	u32 sync2;
838c2ecf20Sopenharmony_ci	u32 addr;
848c2ecf20Sopenharmony_ci	u32 size;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	const struct firmware *fw = NULL;
908c2ecf20Sopenharmony_ci	int i, j;
918c2ecf20Sopenharmony_ci	unsigned size;
928c2ecf20Sopenharmony_ci	u32 __iomem *dst = (u32 __iomem *)mem;
938c2ecf20Sopenharmony_ci	const u32 *src;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
968c2ecf20Sopenharmony_ci		CX18_ERR("Unable to open firmware %s\n", fn);
978c2ecf20Sopenharmony_ci		CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");
988c2ecf20Sopenharmony_ci		return -ENOMEM;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	src = (const u32 *)fw->data;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	for (i = 0; i < fw->size; i += 4096) {
1048c2ecf20Sopenharmony_ci		cx18_setup_page(cx, i);
1058c2ecf20Sopenharmony_ci		for (j = i; j < fw->size && j < i + 4096; j += 4) {
1068c2ecf20Sopenharmony_ci			/* no need for endianness conversion on the ppc */
1078c2ecf20Sopenharmony_ci			cx18_raw_writel(cx, *src, dst);
1088c2ecf20Sopenharmony_ci			if (cx18_raw_readl(cx, dst) != *src) {
1098c2ecf20Sopenharmony_ci				CX18_ERR("Mismatch at offset %x\n", i);
1108c2ecf20Sopenharmony_ci				release_firmware(fw);
1118c2ecf20Sopenharmony_ci				cx18_setup_page(cx, 0);
1128c2ecf20Sopenharmony_ci				return -EIO;
1138c2ecf20Sopenharmony_ci			}
1148c2ecf20Sopenharmony_ci			dst++;
1158c2ecf20Sopenharmony_ci			src++;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
1198c2ecf20Sopenharmony_ci		CX18_INFO("loaded %s firmware (%zu bytes)\n", fn, fw->size);
1208c2ecf20Sopenharmony_ci	size = fw->size;
1218c2ecf20Sopenharmony_ci	release_firmware(fw);
1228c2ecf20Sopenharmony_ci	cx18_setup_page(cx, SCB_OFFSET);
1238c2ecf20Sopenharmony_ci	return size;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx,
1278c2ecf20Sopenharmony_ci				u32 *entry_addr)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	const struct firmware *fw = NULL;
1308c2ecf20Sopenharmony_ci	int i, j;
1318c2ecf20Sopenharmony_ci	unsigned size;
1328c2ecf20Sopenharmony_ci	const u32 *src;
1338c2ecf20Sopenharmony_ci	struct cx18_apu_rom_seghdr seghdr;
1348c2ecf20Sopenharmony_ci	const u8 *vers;
1358c2ecf20Sopenharmony_ci	u32 offset = 0;
1368c2ecf20Sopenharmony_ci	u32 apu_version = 0;
1378c2ecf20Sopenharmony_ci	int sz;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
1408c2ecf20Sopenharmony_ci		CX18_ERR("unable to open firmware %s\n", fn);
1418c2ecf20Sopenharmony_ci		CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");
1428c2ecf20Sopenharmony_ci		cx18_setup_page(cx, 0);
1438c2ecf20Sopenharmony_ci		return -ENOMEM;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	*entry_addr = 0;
1478c2ecf20Sopenharmony_ci	src = (const u32 *)fw->data;
1488c2ecf20Sopenharmony_ci	vers = fw->data + sizeof(seghdr);
1498c2ecf20Sopenharmony_ci	sz = fw->size;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];
1528c2ecf20Sopenharmony_ci	while (offset + sizeof(seghdr) < fw->size) {
1538c2ecf20Sopenharmony_ci		const __le32 *shptr = (__force __le32 *)src + offset / 4;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		seghdr.sync1 = le32_to_cpu(shptr[0]);
1568c2ecf20Sopenharmony_ci		seghdr.sync2 = le32_to_cpu(shptr[1]);
1578c2ecf20Sopenharmony_ci		seghdr.addr = le32_to_cpu(shptr[2]);
1588c2ecf20Sopenharmony_ci		seghdr.size = le32_to_cpu(shptr[3]);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		offset += sizeof(seghdr);
1618c2ecf20Sopenharmony_ci		if (seghdr.sync1 != APU_ROM_SYNC1 ||
1628c2ecf20Sopenharmony_ci		    seghdr.sync2 != APU_ROM_SYNC2) {
1638c2ecf20Sopenharmony_ci			offset += seghdr.size;
1648c2ecf20Sopenharmony_ci			continue;
1658c2ecf20Sopenharmony_ci		}
1668c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,
1678c2ecf20Sopenharmony_ci				seghdr.addr + seghdr.size - 1);
1688c2ecf20Sopenharmony_ci		if (*entry_addr == 0)
1698c2ecf20Sopenharmony_ci			*entry_addr = seghdr.addr;
1708c2ecf20Sopenharmony_ci		if (offset + seghdr.size > sz)
1718c2ecf20Sopenharmony_ci			break;
1728c2ecf20Sopenharmony_ci		for (i = 0; i < seghdr.size; i += 4096) {
1738c2ecf20Sopenharmony_ci			cx18_setup_page(cx, seghdr.addr + i);
1748c2ecf20Sopenharmony_ci			for (j = i; j < seghdr.size && j < i + 4096; j += 4) {
1758c2ecf20Sopenharmony_ci				/* no need for endianness conversion on the ppc */
1768c2ecf20Sopenharmony_ci				cx18_raw_writel(cx, src[(offset + j) / 4],
1778c2ecf20Sopenharmony_ci						dst + seghdr.addr + j);
1788c2ecf20Sopenharmony_ci				if (cx18_raw_readl(cx, dst + seghdr.addr + j)
1798c2ecf20Sopenharmony_ci				    != src[(offset + j) / 4]) {
1808c2ecf20Sopenharmony_ci					CX18_ERR("Mismatch at offset %x\n",
1818c2ecf20Sopenharmony_ci						 offset + j);
1828c2ecf20Sopenharmony_ci					release_firmware(fw);
1838c2ecf20Sopenharmony_ci					cx18_setup_page(cx, 0);
1848c2ecf20Sopenharmony_ci					return -EIO;
1858c2ecf20Sopenharmony_ci				}
1868c2ecf20Sopenharmony_ci			}
1878c2ecf20Sopenharmony_ci		}
1888c2ecf20Sopenharmony_ci		offset += seghdr.size;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci	if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
1918c2ecf20Sopenharmony_ci		CX18_INFO("loaded %s firmware V%08x (%zu bytes)\n",
1928c2ecf20Sopenharmony_ci				fn, apu_version, fw->size);
1938c2ecf20Sopenharmony_ci	size = fw->size;
1948c2ecf20Sopenharmony_ci	release_firmware(fw);
1958c2ecf20Sopenharmony_ci	cx18_setup_page(cx, 0);
1968c2ecf20Sopenharmony_ci	return size;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_civoid cx18_halt_firmware(struct cx18 *cx)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("Preparing for firmware halt.\n");
2028c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
2038c2ecf20Sopenharmony_ci				  0x0000000F, 0x000F000F);
2048c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00020002, CX18_ADEC_CONTROL,
2058c2ecf20Sopenharmony_ci				  0x00000002, 0x00020002);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_civoid cx18_init_power(struct cx18 *cx, int lowpwr)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	/* power-down Spare and AOM PLLs */
2118c2ecf20Sopenharmony_ci	/* power-up fast, slow and mpeg PLLs */
2128c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000008, CX18_PLL_POWER_DOWN);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* ADEC out of sleep */
2158c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00020000, CX18_ADEC_CONTROL,
2168c2ecf20Sopenharmony_ci				  0x00000000, 0x00020002);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/*
2198c2ecf20Sopenharmony_ci	 * The PLL parameters are based on the external crystal frequency that
2208c2ecf20Sopenharmony_ci	 * would ideally be:
2218c2ecf20Sopenharmony_ci	 *
2228c2ecf20Sopenharmony_ci	 * NTSC Color subcarrier freq * 8 =
2238c2ecf20Sopenharmony_ci	 *	4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
2248c2ecf20Sopenharmony_ci	 *
2258c2ecf20Sopenharmony_ci	 * The accidents of history and rationale that explain from where this
2268c2ecf20Sopenharmony_ci	 * combination of magic numbers originate can be found in:
2278c2ecf20Sopenharmony_ci	 *
2288c2ecf20Sopenharmony_ci	 * [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in
2298c2ecf20Sopenharmony_ci	 * the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80
2308c2ecf20Sopenharmony_ci	 *
2318c2ecf20Sopenharmony_ci	 * [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the
2328c2ecf20Sopenharmony_ci	 * NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83
2338c2ecf20Sopenharmony_ci	 *
2348c2ecf20Sopenharmony_ci	 * As Mike Bradley has rightly pointed out, it's not the exact crystal
2358c2ecf20Sopenharmony_ci	 * frequency that matters, only that all parts of the driver and
2368c2ecf20Sopenharmony_ci	 * firmware are using the same value (close to the ideal value).
2378c2ecf20Sopenharmony_ci	 *
2388c2ecf20Sopenharmony_ci	 * Since I have a strong suspicion that, if the firmware ever assumes a
2398c2ecf20Sopenharmony_ci	 * crystal value at all, it will assume 28.636360 MHz, the crystal
2408c2ecf20Sopenharmony_ci	 * freq used in calculations in this driver will be:
2418c2ecf20Sopenharmony_ci	 *
2428c2ecf20Sopenharmony_ci	 *	xtal_freq = 28.636360 MHz
2438c2ecf20Sopenharmony_ci	 *
2448c2ecf20Sopenharmony_ci	 * an error of less than 0.13 ppm which is way, way better than any off
2458c2ecf20Sopenharmony_ci	 * the shelf crystal will have for accuracy anyway.
2468c2ecf20Sopenharmony_ci	 *
2478c2ecf20Sopenharmony_ci	 * Below I aim to run the PLLs' VCOs near 400 MHz to minimze errors.
2488c2ecf20Sopenharmony_ci	 *
2498c2ecf20Sopenharmony_ci	 * Many thanks to Jeff Campbell and Mike Bradley for their extensive
2508c2ecf20Sopenharmony_ci	 * investigation, experimentation, testing, and suggested solutions of
2518c2ecf20Sopenharmony_ci	 * of audio/video sync problems with SVideo and CVBS captures.
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* the fast clock is at 200/245 MHz */
2558c2ecf20Sopenharmony_ci	/* 1 * xtal_freq * 0x0d.f7df9b8 / 2 = 200 MHz: 400 MHz pre post-divide*/
2568c2ecf20Sopenharmony_ci	/* 1 * xtal_freq * 0x11.1c71eb8 / 2 = 245 MHz: 490 MHz pre post-divide*/
2578c2ecf20Sopenharmony_ci	cx18_write_reg(cx, lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);
2588c2ecf20Sopenharmony_ci	cx18_write_reg(cx, lowpwr ? 0x1EFBF37 : 0x038E3D7,
2598c2ecf20Sopenharmony_ci						CX18_FAST_CLOCK_PLL_FRAC);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 2, CX18_FAST_CLOCK_PLL_POST);
2628c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 1, CX18_FAST_CLOCK_PLL_PRESCALE);
2638c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	/* set slow clock to 125/120 MHz */
2668c2ecf20Sopenharmony_ci	/* xtal_freq * 0x0d.1861a20 / 3 = 125 MHz: 375 MHz before post-divide */
2678c2ecf20Sopenharmony_ci	/* xtal_freq * 0x0c.92493f8 / 3 = 120 MHz: 360 MHz before post-divide */
2688c2ecf20Sopenharmony_ci	cx18_write_reg(cx, lowpwr ? 0xD : 0xC, CX18_SLOW_CLOCK_PLL_INT);
2698c2ecf20Sopenharmony_ci	cx18_write_reg(cx, lowpwr ? 0x30C344 : 0x124927F,
2708c2ecf20Sopenharmony_ci						CX18_SLOW_CLOCK_PLL_FRAC);
2718c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 3, CX18_SLOW_CLOCK_PLL_POST);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* mpeg clock pll 54MHz */
2748c2ecf20Sopenharmony_ci	/* xtal_freq * 0xf.15f17f0 / 8 = 54 MHz: 432 MHz before post-divide */
2758c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0xF, CX18_MPEG_CLOCK_PLL_INT);
2768c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x2BE2FE, CX18_MPEG_CLOCK_PLL_FRAC);
2778c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 8, CX18_MPEG_CLOCK_PLL_POST);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* Defaults */
2808c2ecf20Sopenharmony_ci	/* APU = SC or SC/2 = 125/62.5 */
2818c2ecf20Sopenharmony_ci	/* EPU = SC = 125 */
2828c2ecf20Sopenharmony_ci	/* DDR = FC = 180 */
2838c2ecf20Sopenharmony_ci	/* ENC = SC = 125 */
2848c2ecf20Sopenharmony_ci	/* AI1 = SC = 125 */
2858c2ecf20Sopenharmony_ci	/* VIM2 = disabled */
2868c2ecf20Sopenharmony_ci	/* PCI = FC/2 = 90 */
2878c2ecf20Sopenharmony_ci	/* AI2 = disabled */
2888c2ecf20Sopenharmony_ci	/* DEMUX = disabled */
2898c2ecf20Sopenharmony_ci	/* AO = SC/2 = 62.5 */
2908c2ecf20Sopenharmony_ci	/* SER = 54MHz */
2918c2ecf20Sopenharmony_ci	/* VFC = disabled */
2928c2ecf20Sopenharmony_ci	/* USB = disabled */
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (lowpwr) {
2958c2ecf20Sopenharmony_ci		cx18_write_reg_expect(cx, 0xFFFF0020, CX18_CLOCK_SELECT1,
2968c2ecf20Sopenharmony_ci					  0x00000020, 0xFFFFFFFF);
2978c2ecf20Sopenharmony_ci		cx18_write_reg_expect(cx, 0xFFFF0004, CX18_CLOCK_SELECT2,
2988c2ecf20Sopenharmony_ci					  0x00000004, 0xFFFFFFFF);
2998c2ecf20Sopenharmony_ci	} else {
3008c2ecf20Sopenharmony_ci		/* This doesn't explicitly set every clock select */
3018c2ecf20Sopenharmony_ci		cx18_write_reg_expect(cx, 0x00060004, CX18_CLOCK_SELECT1,
3028c2ecf20Sopenharmony_ci					  0x00000004, 0x00060006);
3038c2ecf20Sopenharmony_ci		cx18_write_reg_expect(cx, 0x00060006, CX18_CLOCK_SELECT2,
3048c2ecf20Sopenharmony_ci					  0x00000006, 0x00060006);
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0xFFFF0002, CX18_HALF_CLOCK_SELECT1,
3088c2ecf20Sopenharmony_ci				  0x00000002, 0xFFFFFFFF);
3098c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0xFFFF0104, CX18_HALF_CLOCK_SELECT2,
3108c2ecf20Sopenharmony_ci				  0x00000104, 0xFFFFFFFF);
3118c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0xFFFF9026, CX18_CLOCK_ENABLE1,
3128c2ecf20Sopenharmony_ci				  0x00009026, 0xFFFFFFFF);
3138c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0xFFFF3105, CX18_CLOCK_ENABLE2,
3148c2ecf20Sopenharmony_ci				  0x00003105, 0xFFFFFFFF);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_civoid cx18_init_memory(struct cx18 *cx)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3208c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00010000, CX18_DDR_SOFT_RESET,
3218c2ecf20Sopenharmony_ci				  0x00000000, 0x00010001);
3228c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.refresh, CX18_DDR_REFRESH);
3298c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.timing1, CX18_DDR_TIMING1);
3308c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.timing2, CX18_DDR_TIMING2);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* Initialize DQS pad time */
3358c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);
3368c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00020000, CX18_DDR_SOFT_RESET,
3418c2ecf20Sopenharmony_ci				  0x00000000, 0x00020002);
3428c2ecf20Sopenharmony_ci	cx18_msleep_timeout(10, 0);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* use power-down mode when idle */
3458c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000010, CX18_DDR_POWER_REG);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00010001, CX18_REG_BUS_TIMEOUT_EN,
3488c2ecf20Sopenharmony_ci				  0x00000001, 0x00010001);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x48, CX18_DDR_MB_PER_ROW_7);
3518c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0xE0000, CX18_DDR_BASE_63_ADDR);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT02);  /* AO */
3548c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT09);  /* AI2 */
3558c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT05);  /* VIM1 */
3568c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT06);  /* AI1 */
3578c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT07);  /* 3D comb */
3588c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT10);  /* ME */
3598c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT12);  /* ENC */
3608c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT13);  /* PK */
3618c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT11);  /* RC */
3628c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT14);  /* AVO */
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci#define CX18_CPU_FIRMWARE "v4l-cx23418-cpu.fw"
3668c2ecf20Sopenharmony_ci#define CX18_APU_FIRMWARE "v4l-cx23418-apu.fw"
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ciint cx18_firmware_init(struct cx18 *cx)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	u32 fw_entry_addr;
3718c2ecf20Sopenharmony_ci	int sz, retries;
3728c2ecf20Sopenharmony_ci	u32 api_args[MAX_MB_ARGUMENTS];
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* Allow chip to control CLKRUN */
3758c2ecf20Sopenharmony_ci	cx18_write_reg(cx, 0x5, CX18_DSP0_INTERRUPT_MASK);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* Stop the firmware */
3788c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
3798c2ecf20Sopenharmony_ci				  0x0000000F, 0x000F000F);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	cx18_msleep_timeout(1, 0);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* If the CPU is still running */
3848c2ecf20Sopenharmony_ci	if ((cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 8) == 0) {
3858c2ecf20Sopenharmony_ci		CX18_ERR("%s: couldn't stop CPU to load firmware\n", __func__);
3868c2ecf20Sopenharmony_ci		return -EIO;
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	cx18_sw1_irq_enable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
3908c2ecf20Sopenharmony_ci	cx18_sw2_irq_enable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	sz = load_cpu_fw_direct(CX18_CPU_FIRMWARE, cx->enc_mem, cx);
3938c2ecf20Sopenharmony_ci	if (sz <= 0)
3948c2ecf20Sopenharmony_ci		return sz;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* The SCB & IPC area *must* be correct before starting the firmwares */
3978c2ecf20Sopenharmony_ci	cx18_init_scb(cx);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	fw_entry_addr = 0;
4008c2ecf20Sopenharmony_ci	sz = load_apu_fw_direct(CX18_APU_FIRMWARE, cx->enc_mem, cx,
4018c2ecf20Sopenharmony_ci				&fw_entry_addr);
4028c2ecf20Sopenharmony_ci	if (sz <= 0)
4038c2ecf20Sopenharmony_ci		return sz;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* Start the CPU. The CPU will take care of the APU for us. */
4068c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x00080000, CX18_PROC_SOFT_RESET,
4078c2ecf20Sopenharmony_ci				  0x00000000, 0x00080008);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* Wait up to 500 ms for the APU to come out of reset */
4108c2ecf20Sopenharmony_ci	for (retries = 0;
4118c2ecf20Sopenharmony_ci	     retries < 50 && (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1;
4128c2ecf20Sopenharmony_ci	     retries++)
4138c2ecf20Sopenharmony_ci		cx18_msleep_timeout(10, 0);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	cx18_msleep_timeout(200, 0);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (retries == 50 &&
4188c2ecf20Sopenharmony_ci	    (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1) {
4198c2ecf20Sopenharmony_ci		CX18_ERR("Could not start the CPU\n");
4208c2ecf20Sopenharmony_ci		return -EIO;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/*
4248c2ecf20Sopenharmony_ci	 * The CPU had once before set up to receive an interrupt for it's
4258c2ecf20Sopenharmony_ci	 * outgoing IRQ_CPU_TO_EPU_ACK to us.  If it ever does this, we get an
4268c2ecf20Sopenharmony_ci	 * interrupt when it sends us an ack, but by the time we process it,
4278c2ecf20Sopenharmony_ci	 * that flag in the SW2 status register has been cleared by the CPU
4288c2ecf20Sopenharmony_ci	 * firmware.  We'll prevent that not so useful condition from happening
4298c2ecf20Sopenharmony_ci	 * by clearing the CPU's interrupt enables for Ack IRQ's we want to
4308c2ecf20Sopenharmony_ci	 * process.
4318c2ecf20Sopenharmony_ci	 */
4328c2ecf20Sopenharmony_ci	cx18_sw2_irq_disable_cpu(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* Try a benign command to see if the CPU is alive and well */
4358c2ecf20Sopenharmony_ci	sz = cx18_vapi_result(cx, api_args, CX18_CPU_DEBUG_PEEK32, 1, 0);
4368c2ecf20Sopenharmony_ci	if (sz < 0)
4378c2ecf20Sopenharmony_ci		return sz;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* initialize GPIO */
4408c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, 0x14001400, 0xc78110, 0x00001400, 0x14001400);
4418c2ecf20Sopenharmony_ci	return 0;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ciMODULE_FIRMWARE(CX18_CPU_FIRMWARE);
4458c2ecf20Sopenharmony_ciMODULE_FIRMWARE(CX18_APU_FIRMWARE);
446