162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2010 - Maxim Levitsky
462306a36Sopenharmony_ci * driver for Ricoh memstick readers
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef R592_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/memstick.h>
1062306a36Sopenharmony_ci#include <linux/spinlock.h>
1162306a36Sopenharmony_ci#include <linux/interrupt.h>
1262306a36Sopenharmony_ci#include <linux/workqueue.h>
1362306a36Sopenharmony_ci#include <linux/kfifo.h>
1462306a36Sopenharmony_ci#include <linux/ctype.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* write to this reg (number,len) triggers TPC execution */
1762306a36Sopenharmony_ci#define R592_TPC_EXEC			0x00
1862306a36Sopenharmony_ci#define R592_TPC_EXEC_LEN_SHIFT		16		/* Bits 16..25 are TPC len */
1962306a36Sopenharmony_ci#define R592_TPC_EXEC_BIG_FIFO		(1 << 26)	/* If bit 26 is set, large fifo is used (reg 48) */
2062306a36Sopenharmony_ci#define R592_TPC_EXEC_TPC_SHIFT		28		/* Bits 28..31 are the TPC number */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* Window for small TPC fifo (big endian)*/
2462306a36Sopenharmony_ci/* reads and writes always are done in  8 byte chunks */
2562306a36Sopenharmony_ci/* Not used in driver, because large fifo does better job */
2662306a36Sopenharmony_ci#define R592_SFIFO			0x08
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/* Status register (ms int, small fifo, IO)*/
3062306a36Sopenharmony_ci#define R592_STATUS			0x10
3162306a36Sopenharmony_ci							/* Parallel INT bits */
3262306a36Sopenharmony_ci#define R592_STATUS_P_CMDNACK		(1 << 16)	/* INT reg: NACK (parallel mode) */
3362306a36Sopenharmony_ci#define R592_STATUS_P_BREQ		(1 << 17)	/* INT reg: card ready (parallel mode)*/
3462306a36Sopenharmony_ci#define R592_STATUS_P_INTERR		(1 << 18)	/* INT reg: int error (parallel mode)*/
3562306a36Sopenharmony_ci#define R592_STATUS_P_CED		(1 << 19)	/* INT reg: command done (parallel mode) */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci							/* Fifo status */
3862306a36Sopenharmony_ci#define R592_STATUS_SFIFO_FULL		(1 << 20)	/* Small Fifo almost full (last chunk is written) */
3962306a36Sopenharmony_ci#define R592_STATUS_SFIFO_EMPTY		(1 << 21)	/* Small Fifo empty */
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci							/* Error detection via CRC */
4262306a36Sopenharmony_ci#define R592_STATUS_SEND_ERR		(1 << 24)	/* Send failed */
4362306a36Sopenharmony_ci#define R592_STATUS_RECV_ERR		(1 << 25)	/* Receive failed */
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci							/* Card state */
4662306a36Sopenharmony_ci#define R592_STATUS_RDY			(1 << 28)	/* RDY signal received */
4762306a36Sopenharmony_ci#define R592_STATUS_CED			(1 << 29)	/* INT: Command done (serial mode)*/
4862306a36Sopenharmony_ci#define R592_STATUS_SFIFO_INPUT		(1 << 30)	/* Small fifo received data*/
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define R592_SFIFO_SIZE			32		/* total size of small fifo is 32 bytes */
5162306a36Sopenharmony_ci#define R592_SFIFO_PACKET		8		/* packet size of small fifo */
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/* IO control */
5462306a36Sopenharmony_ci#define R592_IO				0x18
5562306a36Sopenharmony_ci#define	R592_IO_16			(1 << 16)	/* Set by default, can be cleared */
5662306a36Sopenharmony_ci#define	R592_IO_18			(1 << 18)	/* Set by default, can be cleared */
5762306a36Sopenharmony_ci#define	R592_IO_SERIAL1			(1 << 20)	/* Set by default, can be cleared, (cleared on parallel) */
5862306a36Sopenharmony_ci#define	R592_IO_22			(1 << 22)	/* Set by default, can be cleared */
5962306a36Sopenharmony_ci#define R592_IO_DIRECTION		(1 << 24)	/* TPC direction (1 write 0 read) */
6062306a36Sopenharmony_ci#define	R592_IO_26			(1 << 26)	/* Set by default, can be cleared */
6162306a36Sopenharmony_ci#define	R592_IO_SERIAL2			(1 << 30)	/* Set by default, can be cleared (cleared on parallel), serial doesn't work if unset */
6262306a36Sopenharmony_ci#define R592_IO_RESET			(1 << 31)	/* Reset, sets defaults*/
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/* Turns hardware on/off */
6662306a36Sopenharmony_ci#define R592_POWER			0x20		/* bits 0-7 writeable */
6762306a36Sopenharmony_ci#define R592_POWER_0			(1 << 0)	/* set on start, cleared on stop - must be set*/
6862306a36Sopenharmony_ci#define R592_POWER_1			(1 << 1)	/* set on start, cleared on stop - must be set*/
6962306a36Sopenharmony_ci#define R592_POWER_3			(1 << 3)	/* must be clear */
7062306a36Sopenharmony_ci#define R592_POWER_20			(1 << 5)	/* set before switch to parallel */
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* IO mode*/
7362306a36Sopenharmony_ci#define R592_IO_MODE			0x24
7462306a36Sopenharmony_ci#define R592_IO_MODE_SERIAL		1
7562306a36Sopenharmony_ci#define R592_IO_MODE_PARALLEL		3
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/* IRQ,card detection,large fifo (first word irq status, second enable) */
7962306a36Sopenharmony_ci/* IRQs are ACKed by clearing the bits */
8062306a36Sopenharmony_ci#define R592_REG_MSC			0x28
8162306a36Sopenharmony_ci#define R592_REG_MSC_PRSNT		(1 << 1)	/* card present (only status)*/
8262306a36Sopenharmony_ci#define R592_REG_MSC_IRQ_INSERT		(1 << 8)	/* detect insert / card insered */
8362306a36Sopenharmony_ci#define R592_REG_MSC_IRQ_REMOVE		(1 << 9)	/* detect removal / card removed */
8462306a36Sopenharmony_ci#define R592_REG_MSC_FIFO_EMPTY		(1 << 10)	/* fifo is empty */
8562306a36Sopenharmony_ci#define R592_REG_MSC_FIFO_DMA_DONE	(1 << 11)	/* dma enable / dma done */
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define R592_REG_MSC_FIFO_USER_ORN	(1 << 12)	/* set if software reads empty fifo (if R592_REG_MSC_FIFO_EMPTY is set) */
8862306a36Sopenharmony_ci#define R592_REG_MSC_FIFO_MISMATH	(1 << 13)	/* set if amount of data in fifo doesn't match amount in TPC */
8962306a36Sopenharmony_ci#define R592_REG_MSC_FIFO_DMA_ERR	(1 << 14)	/* IO failure */
9062306a36Sopenharmony_ci#define R592_REG_MSC_LED		(1 << 15)	/* clear to turn led off (only status)*/
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define DMA_IRQ_ACK_MASK \
9362306a36Sopenharmony_ci	(R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#define DMA_IRQ_EN_MASK (DMA_IRQ_ACK_MASK << 16)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#define IRQ_ALL_ACK_MASK 0x00007F00
9862306a36Sopenharmony_ci#define IRQ_ALL_EN_MASK (IRQ_ALL_ACK_MASK << 16)
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* DMA address for large FIFO read/writes*/
10162306a36Sopenharmony_ci#define R592_FIFO_DMA			0x2C
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci/* PIO access to large FIFO (512 bytes) (big endian)*/
10462306a36Sopenharmony_ci#define R592_FIFO_PIO			0x30
10562306a36Sopenharmony_ci#define R592_LFIFO_SIZE			512		/* large fifo size */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/* large FIFO DMA settings */
10962306a36Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS		0x34
11062306a36Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_EN	(1 << 0)	/* DMA enabled */
11162306a36Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_DIR	(1 << 1)	/* Dma direction (1 read, 0 write) */
11262306a36Sopenharmony_ci#define R592_FIFO_DMA_SETTINGS_CAP	(1 << 24)	/* Dma is aviable */
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/* Maybe just an delay */
11562306a36Sopenharmony_ci/* Bits 17..19 are just number */
11662306a36Sopenharmony_ci/* bit 16 is set, then bit 20 is waited */
11762306a36Sopenharmony_ci/* time to wait is about 50 spins * 2 ^ (bits 17..19) */
11862306a36Sopenharmony_ci/* seems to be possible just to ignore */
11962306a36Sopenharmony_ci/* Probably debug register */
12062306a36Sopenharmony_ci#define R592_REG38			0x38
12162306a36Sopenharmony_ci#define R592_REG38_CHANGE		(1 << 16)	/* Start bit */
12262306a36Sopenharmony_ci#define R592_REG38_DONE			(1 << 20)	/* HW set this after the delay */
12362306a36Sopenharmony_ci#define R592_REG38_SHIFT		17
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/* Debug register, written (0xABCDEF00) when error happens - not used*/
12662306a36Sopenharmony_ci#define R592_REG_3C			0x3C
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistruct r592_device {
12962306a36Sopenharmony_ci	struct pci_dev *pci_dev;
13062306a36Sopenharmony_ci	struct memstick_host	*host;		/* host backpointer */
13162306a36Sopenharmony_ci	struct memstick_request *req;		/* current request */
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* Registers, IRQ */
13462306a36Sopenharmony_ci	void __iomem *mmio;
13562306a36Sopenharmony_ci	int irq;
13662306a36Sopenharmony_ci	spinlock_t irq_lock;
13762306a36Sopenharmony_ci	spinlock_t io_thread_lock;
13862306a36Sopenharmony_ci	struct timer_list detect_timer;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	struct task_struct *io_thread;
14162306a36Sopenharmony_ci	bool parallel_mode;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	DECLARE_KFIFO(pio_fifo, u8, sizeof(u32));
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	/* DMA area */
14662306a36Sopenharmony_ci	int dma_capable;
14762306a36Sopenharmony_ci	int dma_error;
14862306a36Sopenharmony_ci	struct completion dma_done;
14962306a36Sopenharmony_ci	void *dummy_dma_page;
15062306a36Sopenharmony_ci	dma_addr_t dummy_dma_page_physical_address;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci#define DRV_NAME "r592"
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#define message(format, ...) \
15862306a36Sopenharmony_ci	printk(KERN_INFO DRV_NAME ": " format "\n", ## __VA_ARGS__)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci#define __dbg(level, format, ...) \
16162306a36Sopenharmony_ci	do { \
16262306a36Sopenharmony_ci		if (debug >= level) \
16362306a36Sopenharmony_ci			printk(KERN_DEBUG DRV_NAME \
16462306a36Sopenharmony_ci				": " format "\n", ## __VA_ARGS__); \
16562306a36Sopenharmony_ci	} while (0)
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci#define dbg(format, ...)		__dbg(1, format, ## __VA_ARGS__)
16962306a36Sopenharmony_ci#define dbg_verbose(format, ...)	__dbg(2, format, ## __VA_ARGS__)
17062306a36Sopenharmony_ci#define dbg_reg(format, ...)		__dbg(3, format, ## __VA_ARGS__)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci#endif
173