18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  cx18 driver PCI memory mapped IO access routines
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-irq.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civoid cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	u8 __iomem *dst = addr;
168c2ecf20Sopenharmony_ci	u16 val2 = val | (val << 8);
178c2ecf20Sopenharmony_ci	u32 val4 = val2 | (val2 << 16);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	/* Align writes on the CX23418's addresses */
208c2ecf20Sopenharmony_ci	if ((count > 0) && ((unsigned long)dst & 1)) {
218c2ecf20Sopenharmony_ci		cx18_writeb(cx, (u8) val, dst);
228c2ecf20Sopenharmony_ci		count--;
238c2ecf20Sopenharmony_ci		dst++;
248c2ecf20Sopenharmony_ci	}
258c2ecf20Sopenharmony_ci	if ((count > 1) && ((unsigned long)dst & 2)) {
268c2ecf20Sopenharmony_ci		cx18_writew(cx, val2, dst);
278c2ecf20Sopenharmony_ci		count -= 2;
288c2ecf20Sopenharmony_ci		dst += 2;
298c2ecf20Sopenharmony_ci	}
308c2ecf20Sopenharmony_ci	while (count > 3) {
318c2ecf20Sopenharmony_ci		cx18_writel(cx, val4, dst);
328c2ecf20Sopenharmony_ci		count -= 4;
338c2ecf20Sopenharmony_ci		dst += 4;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci	if (count > 1) {
368c2ecf20Sopenharmony_ci		cx18_writew(cx, val2, dst);
378c2ecf20Sopenharmony_ci		count -= 2;
388c2ecf20Sopenharmony_ci		dst += 2;
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci	if (count > 0)
418c2ecf20Sopenharmony_ci		cx18_writeb(cx, (u8) val, dst);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_civoid cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, val, SW1_INT_STATUS, ~val, val);
478c2ecf20Sopenharmony_ci	cx->sw1_irq_mask = cx18_read_reg(cx, SW1_INT_ENABLE_PCI) | val;
488c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->sw1_irq_mask, SW1_INT_ENABLE_PCI);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_civoid cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	cx->sw1_irq_mask = cx18_read_reg(cx, SW1_INT_ENABLE_PCI) & ~val;
548c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->sw1_irq_mask, SW1_INT_ENABLE_PCI);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_civoid cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	cx18_write_reg_expect(cx, val, SW2_INT_STATUS, ~val, val);
608c2ecf20Sopenharmony_ci	cx->sw2_irq_mask = cx18_read_reg(cx, SW2_INT_ENABLE_PCI) | val;
618c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->sw2_irq_mask, SW2_INT_ENABLE_PCI);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_civoid cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	cx->sw2_irq_mask = cx18_read_reg(cx, SW2_INT_ENABLE_PCI) & ~val;
678c2ecf20Sopenharmony_ci	cx18_write_reg(cx, cx->sw2_irq_mask, SW2_INT_ENABLE_PCI);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_civoid cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	u32 r;
738c2ecf20Sopenharmony_ci	r = cx18_read_reg(cx, SW2_INT_ENABLE_CPU);
748c2ecf20Sopenharmony_ci	cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_CPU);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_civoid cx18_setup_page(struct cx18 *cx, u32 addr)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	u32 val;
808c2ecf20Sopenharmony_ci	val = cx18_read_reg(cx, 0xD000F8);
818c2ecf20Sopenharmony_ci	val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
828c2ecf20Sopenharmony_ci	cx18_write_reg(cx, val, 0xD000F8);
838c2ecf20Sopenharmony_ci}
84