18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci	Mantis PCI bridge driver
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci*/
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/signal.h>
118c2ecf20Sopenharmony_ci#include <linux/sched.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <asm/io.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <media/dmxdev.h>
178c2ecf20Sopenharmony_ci#include <media/dvbdev.h>
188c2ecf20Sopenharmony_ci#include <media/dvb_demux.h>
198c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h>
208c2ecf20Sopenharmony_ci#include <media/dvb_net.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "mantis_common.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "mantis_hif.h"
258c2ecf20Sopenharmony_ci#include "mantis_link.h" /* temporary due to physical layer stuff */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "mantis_reg.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int mantis_hif_sbuf_opdone_wait(struct mantis_ca *ca)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
338c2ecf20Sopenharmony_ci	int rc = 0;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (wait_event_timeout(ca->hif_opdone_wq,
368c2ecf20Sopenharmony_ci			       ca->hif_event & MANTIS_SBUF_OPDONE,
378c2ecf20Sopenharmony_ci			       msecs_to_jiffies(500)) == -ERESTARTSYS) {
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): Smart buffer operation timeout !", mantis->num);
408c2ecf20Sopenharmony_ci		rc = -EREMOTEIO;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Smart Buffer Operation complete");
438c2ecf20Sopenharmony_ci	ca->hif_event &= ~MANTIS_SBUF_OPDONE;
448c2ecf20Sopenharmony_ci	return rc;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int mantis_hif_write_wait(struct mantis_ca *ca)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
508c2ecf20Sopenharmony_ci	u32 opdone = 0, timeout = 0;
518c2ecf20Sopenharmony_ci	int rc = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (wait_event_timeout(ca->hif_write_wq,
548c2ecf20Sopenharmony_ci			       mantis->gpif_status & MANTIS_GPIF_WRACK,
558c2ecf20Sopenharmony_ci			       msecs_to_jiffies(500)) == -ERESTARTSYS) {
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): Write ACK timed out !", mantis->num);
588c2ecf20Sopenharmony_ci		rc = -EREMOTEIO;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Write Acknowledged");
618c2ecf20Sopenharmony_ci	mantis->gpif_status &= ~MANTIS_GPIF_WRACK;
628c2ecf20Sopenharmony_ci	while (!opdone) {
638c2ecf20Sopenharmony_ci		opdone = (mmread(MANTIS_GPIF_STATUS) & MANTIS_SBUF_OPDONE);
648c2ecf20Sopenharmony_ci		udelay(500);
658c2ecf20Sopenharmony_ci		timeout++;
668c2ecf20Sopenharmony_ci		if (timeout > 100) {
678c2ecf20Sopenharmony_ci			dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): Write operation timed out!", mantis->num);
688c2ecf20Sopenharmony_ci			rc = -ETIMEDOUT;
698c2ecf20Sopenharmony_ci			break;
708c2ecf20Sopenharmony_ci		}
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "HIF Write success");
738c2ecf20Sopenharmony_ci	return rc;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciint mantis_hif_read_mem(struct mantis_ca *ca, u32 addr)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
808c2ecf20Sopenharmony_ci	u32 hif_addr = 0, data, count = 4;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF Mem Read", mantis->num);
838c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
848c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
858c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAIOM;
868c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_HIF_STATUS;
878c2ecf20Sopenharmony_ci	hif_addr |=  addr;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	mmwrite(hif_addr, MANTIS_GPIF_BRADDR);
908c2ecf20Sopenharmony_ci	mmwrite(count, MANTIS_GPIF_BRBYTES);
918c2ecf20Sopenharmony_ci	udelay(20);
928c2ecf20Sopenharmony_ci	mmwrite(hif_addr | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (mantis_hif_sbuf_opdone_wait(ca) != 0) {
958c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): GPIF Smart Buffer operation failed", mantis->num);
968c2ecf20Sopenharmony_ci		mutex_unlock(&ca->ca_lock);
978c2ecf20Sopenharmony_ci		return -EREMOTEIO;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	data = mmread(MANTIS_GPIF_DIN);
1008c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
1018c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Mem Read: 0x%02x", data);
1028c2ecf20Sopenharmony_ci	return (data >> 24) & 0xff;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciint mantis_hif_write_mem(struct mantis_ca *ca, u32 addr, u8 data)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct mantis_slot *slot = ca->slot;
1088c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
1098c2ecf20Sopenharmony_ci	u32 hif_addr = 0;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF Mem Write", mantis->num);
1128c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
1138c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_HIFRDWRN;
1148c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
1158c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAIOM;
1168c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_HIF_STATUS;
1178c2ecf20Sopenharmony_ci	hif_addr |=  addr;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	mmwrite(slot->slave_cfg, MANTIS_GPIF_CFGSLA); /* Slot0 alone for now */
1208c2ecf20Sopenharmony_ci	mmwrite(hif_addr, MANTIS_GPIF_ADDR);
1218c2ecf20Sopenharmony_ci	mmwrite(data, MANTIS_GPIF_DOUT);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (mantis_hif_write_wait(ca) != 0) {
1248c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
1258c2ecf20Sopenharmony_ci		mutex_unlock(&ca->ca_lock);
1268c2ecf20Sopenharmony_ci		return -EREMOTEIO;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Mem Write: (0x%02x to 0x%02x)", data, addr);
1298c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return 0;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint mantis_hif_read_iom(struct mantis_ca *ca, u32 addr)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
1378c2ecf20Sopenharmony_ci	u32 data, hif_addr = 0;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF I/O Read", mantis->num);
1408c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
1418c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
1428c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_GPIF_PCMCIAIOM;
1438c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_HIF_STATUS;
1448c2ecf20Sopenharmony_ci	hif_addr |=  addr;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	mmwrite(hif_addr, MANTIS_GPIF_BRADDR);
1478c2ecf20Sopenharmony_ci	mmwrite(1, MANTIS_GPIF_BRBYTES);
1488c2ecf20Sopenharmony_ci	udelay(20);
1498c2ecf20Sopenharmony_ci	mmwrite(hif_addr | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (mantis_hif_sbuf_opdone_wait(ca) != 0) {
1528c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
1538c2ecf20Sopenharmony_ci		mutex_unlock(&ca->ca_lock);
1548c2ecf20Sopenharmony_ci		return -EREMOTEIO;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci	data = mmread(MANTIS_GPIF_DIN);
1578c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "I/O Read: 0x%02x", data);
1588c2ecf20Sopenharmony_ci	udelay(50);
1598c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return (u8) data;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciint mantis_hif_write_iom(struct mantis_ca *ca, u32 addr, u8 data)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
1678c2ecf20Sopenharmony_ci	u32 hif_addr = 0;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF I/O Write", mantis->num);
1708c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
1718c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
1728c2ecf20Sopenharmony_ci	hif_addr &= ~MANTIS_GPIF_HIFRDWRN;
1738c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_GPIF_PCMCIAIOM;
1748c2ecf20Sopenharmony_ci	hif_addr |=  MANTIS_HIF_STATUS;
1758c2ecf20Sopenharmony_ci	hif_addr |=  addr;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	mmwrite(hif_addr, MANTIS_GPIF_ADDR);
1788c2ecf20Sopenharmony_ci	mmwrite(data, MANTIS_GPIF_DOUT);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	if (mantis_hif_write_wait(ca) != 0) {
1818c2ecf20Sopenharmony_ci		dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
1828c2ecf20Sopenharmony_ci		mutex_unlock(&ca->ca_lock);
1838c2ecf20Sopenharmony_ci		return -EREMOTEIO;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci	dprintk(MANTIS_DEBUG, 1, "I/O Write: (0x%02x to 0x%02x)", data, addr);
1868c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
1878c2ecf20Sopenharmony_ci	udelay(50);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return 0;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciint mantis_hif_init(struct mantis_ca *ca)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct mantis_slot *slot = ca->slot;
1958c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
1968c2ecf20Sopenharmony_ci	u32 irqcfg;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	slot[0].slave_cfg = 0x70773028;
1998c2ecf20Sopenharmony_ci	dprintk(MANTIS_ERROR, 1, "Adapter(%d) Initializing Mantis Host Interface", mantis->num);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
2028c2ecf20Sopenharmony_ci	irqcfg = mmread(MANTIS_GPIF_IRQCFG);
2038c2ecf20Sopenharmony_ci	irqcfg = MANTIS_MASK_BRRDY	|
2048c2ecf20Sopenharmony_ci		 MANTIS_MASK_WRACK	|
2058c2ecf20Sopenharmony_ci		 MANTIS_MASK_EXTIRQ	|
2068c2ecf20Sopenharmony_ci		 MANTIS_MASK_WSTO	|
2078c2ecf20Sopenharmony_ci		 MANTIS_MASK_OTHERR	|
2088c2ecf20Sopenharmony_ci		 MANTIS_MASK_OVFLW;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	mmwrite(irqcfg, MANTIS_GPIF_IRQCFG);
2118c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return 0;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_civoid mantis_hif_exit(struct mantis_ca *ca)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct mantis_pci *mantis = ca->ca_priv;
2198c2ecf20Sopenharmony_ci	u32 irqcfg;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	dprintk(MANTIS_ERROR, 1, "Adapter(%d) Exiting Mantis Host Interface", mantis->num);
2228c2ecf20Sopenharmony_ci	mutex_lock(&ca->ca_lock);
2238c2ecf20Sopenharmony_ci	irqcfg = mmread(MANTIS_GPIF_IRQCFG);
2248c2ecf20Sopenharmony_ci	irqcfg &= ~MANTIS_MASK_BRRDY;
2258c2ecf20Sopenharmony_ci	mmwrite(irqcfg, MANTIS_GPIF_IRQCFG);
2268c2ecf20Sopenharmony_ci	mutex_unlock(&ca->ca_lock);
2278c2ecf20Sopenharmony_ci}
228