18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2010 - Maxim Levitsky
48c2ecf20Sopenharmony_ci * driver for Ricoh memstick readers
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef R592_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/memstick.h>
108c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
138c2ecf20Sopenharmony_ci#include <linux/kfifo.h>
148c2ecf20Sopenharmony_ci#include <linux/ctype.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* write to this reg (number,len) triggers TPC execution */
178c2ecf20Sopenharmony_ci#define R592_TPC_EXEC			0x00
188c2ecf20Sopenharmony_ci#define R592_TPC_EXEC_LEN_SHIFT		16		/* Bits 16..25 are TPC len */
198c2ecf20Sopenharmony_ci#define R592_TPC_EXEC_BIG_FIFO		(1 << 26)	/* If bit 26 is set, large fifo is used (reg 48) */
208c2ecf20Sopenharmony_ci#define R592_TPC_EXEC_TPC_SHIFT		28		/* Bits 28..31 are the TPC number */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Window for small TPC fifo (big endian)*/
248c2ecf20Sopenharmony_ci/* reads and writes always are done in  8 byte chunks */
258c2ecf20Sopenharmony_ci/* Not used in driver, because large fifo does better job */
268c2ecf20Sopenharmony_ci#define R592_SFIFO			0x08
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Status register (ms int, small fifo, IO)*/
308c2ecf20Sopenharmony_ci#define R592_STATUS			0x10
318c2ecf20Sopenharmony_ci							/* Parallel INT bits */
328c2ecf20Sopenharmony_ci#define R592_STATUS_P_CMDNACK		(1 << 16)	/* INT reg: NACK (parallel mode) */
338c2ecf20Sopenharmony_ci#define R592_STATUS_P_BREQ		(1 << 17)	/* INT reg: card ready (parallel mode)*/
348c2ecf20Sopenharmony_ci#define R592_STATUS_P_INTERR		(1 << 18)	/* INT reg: int error (parallel mode)*/
358c2ecf20Sopenharmony_ci#define R592_STATUS_P_CED		(1 << 19)	/* INT reg: command done (parallel mode) */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci							/* Fifo status */
388c2ecf20Sopenharmony_ci#define R592_STATUS_SFIFO_FULL		(1 << 20)	/* Small Fifo almost full (last chunk is written) */
398c2ecf20Sopenharmony_ci#define R592_STATUS_SFIFO_EMPTY		(1 << 21)	/* Small Fifo empty */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci							/* Error detection via CRC */
428c2ecf20Sopenharmony_ci#define R592_STATUS_SEND_ERR		(1 << 24)	/* Send failed */
438c2ecf20Sopenharmony_ci#define R592_STATUS_RECV_ERR		(1 << 25)	/* Receive failed */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci							/* Card state */
468c2ecf20Sopenharmony_ci#define R592_STATUS_RDY			(1 << 28)	/* RDY signal received */
478c2ecf20Sopenharmony_ci#define R592_STATUS_CED			(1 << 29)	/* INT: Command done (serial mode)*/
488c2ecf20Sopenharmony_ci#define R592_STATUS_SFIFO_INPUT		(1 << 30)	/* Small fifo received data*/
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define R592_SFIFO_SIZE			32		/* total size of small fifo is 32 bytes */
518c2ecf20Sopenharmony_ci#define R592_SFIFO_PACKET		8		/* packet size of small fifo */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* IO control */
548c2ecf20Sopenharmony_ci#define R592_IO				0x18
558c2ecf20Sopenharmony_ci#define	R592_IO_16			(1 << 16)	/* Set by default, can be cleared */
568c2ecf20Sopenharmony_ci#define	R592_IO_18			(1 << 18)	/* Set by default, can be cleared */
578c2ecf20Sopenharmony_ci#define	R592_IO_SERIAL1			(1 << 20)	/* Set by default, can be cleared, (cleared on parallel) */
588c2ecf20Sopenharmony_ci#define	R592_IO_22			(1 << 22)	/* Set by default, can be cleared */
598c2ecf20Sopenharmony_ci#define R592_IO_DIRECTION		(1 << 24)	/* TPC direction (1 write 0 read) */
608c2ecf20Sopenharmony_ci#define	R592_IO_26			(1 << 26)	/* Set by default, can be cleared */
618c2ecf20Sopenharmony_ci#define	R592_IO_SERIAL2			(1 << 30)	/* Set by default, can be cleared (cleared on parallel), serial doesn't work if unset */
628c2ecf20Sopenharmony_ci#define R592_IO_RESET			(1 << 31)	/* Reset, sets defaults*/
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* Turns hardware on/off */
668c2ecf20Sopenharmony_ci#define R592_POWER			0x20		/* bits 0-7 writeable */
678c2ecf20Sopenharmony_ci#define R592_POWER_0			(1 << 0)	/* set on start, cleared on stop - must be set*/
688c2ecf20Sopenharmony_ci#define R592_POWER_1			(1 << 1)	/* set on start, cleared on stop - must be set*/
698c2ecf20Sopenharmony_ci#define R592_POWER_3			(1 << 3)	/* must be clear */
708c2ecf20Sopenharmony_ci#define R592_POWER_20			(1 << 5)	/* set before switch to parallel */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* IO mode*/
738c2ecf20Sopenharmony_ci#define R592_IO_MODE			0x24
748c2ecf20Sopenharmony_ci#define R592_IO_MODE_SERIAL		1
758c2ecf20Sopenharmony_ci#define R592_IO_MODE_PARALLEL		3
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* IRQ,card detection,large fifo (first word irq status, second enable) */
798c2ecf20Sopenharmony_ci/* IRQs are ACKed by clearing the bits */
808c2ecf20Sopenharmony_ci#define R592_REG_MSC			0x28
818c2ecf20Sopenharmony_ci#define R592_REG_MSC_PRSNT		(1 << 1)	/* card present (only status)*/
828c2ecf20Sopenharmony_ci#define R592_REG_MSC_IRQ_INSERT		(1 << 8)	/* detect insert / card insered */
838c2ecf20Sopenharmony_ci#define R592_REG_MSC_IRQ_REMOVE		(1 << 9)	/* detect removal / card removed */
848c2ecf20Sopenharmony_ci#define R592_REG_MSC_FIFO_EMPTY		(1 << 10)	/* fifo is empty */
858c2ecf20Sopenharmony_ci#define R592_REG_MSC_FIFO_DMA_DONE	(1 << 11)	/* dma enable / dma done */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define R592_REG_MSC_FIFO_USER_ORN	(1 << 12)	/* set if software reads empty fifo (if R592_REG_MSC_FIFO_EMPTY is set) */
888c2ecf20Sopenharmony_ci#define R592_REG_MSC_FIFO_MISMATH	(1 << 13)	/* set if amount of data in fifo doesn't match amount in TPC */
898c2ecf20Sopenharmony_ci#define R592_REG_MSC_FIFO_DMA_ERR	(1 << 14)	/* IO failure */
908c2ecf20Sopenharmony_ci#define R592_REG_MSC_LED		(1 << 15)	/* clear to turn led off (only status)*/
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define DMA_IRQ_ACK_MASK \
938c2ecf20Sopenharmony_ci	(R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define DMA_IRQ_EN_MASK (DMA_IRQ_ACK_MASK << 16)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define IRQ_ALL_ACK_MASK 0x00007F00
988c2ecf20Sopenharmony_ci#define IRQ_ALL_EN_MASK (IRQ_ALL_ACK_MASK << 16)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* DMA address for large FIFO read/writes*/
1018c2ecf20Sopenharmony_ci#define R592_FIFO_DMA			0x2C
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* PIO access to large FIFO (512 bytes) (big endian)*/
1048c2ecf20Sopenharmony_ci#define R592_FIFO_PIO			0x30
1058c2ecf20Sopenharmony_ci#define R592_LFIFO_SIZE			512		/* large fifo size */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* large FIFO DMA settings */
1098c2ecf20Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS		0x34
1108c2ecf20Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_EN	(1 << 0)	/* DMA enabled */
1118c2ecf20Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_DIR	(1 << 1)	/* Dma direction (1 read, 0 write) */
1128c2ecf20Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_CAP	(1 << 24)	/* Dma is aviable */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* Maybe just an delay */
1158c2ecf20Sopenharmony_ci/* Bits 17..19 are just number */
1168c2ecf20Sopenharmony_ci/* bit 16 is set, then bit 20 is waited */
1178c2ecf20Sopenharmony_ci/* time to wait is about 50 spins * 2 ^ (bits 17..19) */
1188c2ecf20Sopenharmony_ci/* seems to be possible just to ignore */
1198c2ecf20Sopenharmony_ci/* Probably debug register */
1208c2ecf20Sopenharmony_ci#define R592_REG38			0x38
1218c2ecf20Sopenharmony_ci#define R592_REG38_CHANGE		(1 << 16)	/* Start bit */
1228c2ecf20Sopenharmony_ci#define R592_REG38_DONE			(1 << 20)	/* HW set this after the delay */
1238c2ecf20Sopenharmony_ci#define R592_REG38_SHIFT		17
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* Debug register, written (0xABCDEF00) when error happens - not used*/
1268c2ecf20Sopenharmony_ci#define R592_REG_3C			0x3C
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistruct r592_device {
1298c2ecf20Sopenharmony_ci	struct pci_dev *pci_dev;
1308c2ecf20Sopenharmony_ci	struct memstick_host	*host;		/* host backpointer */
1318c2ecf20Sopenharmony_ci	struct memstick_request *req;		/* current request */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Registers, IRQ */
1348c2ecf20Sopenharmony_ci	void __iomem *mmio;
1358c2ecf20Sopenharmony_ci	int irq;
1368c2ecf20Sopenharmony_ci	spinlock_t irq_lock;
1378c2ecf20Sopenharmony_ci	spinlock_t io_thread_lock;
1388c2ecf20Sopenharmony_ci	struct timer_list detect_timer;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	struct task_struct *io_thread;
1418c2ecf20Sopenharmony_ci	bool parallel_mode;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	DECLARE_KFIFO(pio_fifo, u8, sizeof(u32));
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* DMA area */
1468c2ecf20Sopenharmony_ci	int dma_capable;
1478c2ecf20Sopenharmony_ci	int dma_error;
1488c2ecf20Sopenharmony_ci	struct completion dma_done;
1498c2ecf20Sopenharmony_ci	void *dummy_dma_page;
1508c2ecf20Sopenharmony_ci	dma_addr_t dummy_dma_page_physical_address;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define DRV_NAME "r592"
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define message(format, ...) \
1588c2ecf20Sopenharmony_ci	printk(KERN_INFO DRV_NAME ": " format "\n", ## __VA_ARGS__)
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define __dbg(level, format, ...) \
1618c2ecf20Sopenharmony_ci	do { \
1628c2ecf20Sopenharmony_ci		if (debug >= level) \
1638c2ecf20Sopenharmony_ci			printk(KERN_DEBUG DRV_NAME \
1648c2ecf20Sopenharmony_ci				": " format "\n", ## __VA_ARGS__); \
1658c2ecf20Sopenharmony_ci	} while (0)
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define dbg(format, ...)		__dbg(1, format, ## __VA_ARGS__)
1698c2ecf20Sopenharmony_ci#define dbg_verbose(format, ...)	__dbg(2, format, ## __VA_ARGS__)
1708c2ecf20Sopenharmony_ci#define dbg_reg(format, ...)		__dbg(3, format, ## __VA_ARGS__)
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#endif
173