1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Driver for Realtek PCI-Express card reader
4 *
5 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
6 *
7 * Author:
8 *   Wei WANG (wei_wang@realsil.com.cn)
9 *   Micky Ching (micky_ching@realsil.com.cn)
10 */
11
12#ifndef __REALTEK_RTSX_H
13#define __REALTEK_RTSX_H
14
15#include <linux/io.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/slab.h>
23#include <linux/pci.h>
24#include <linux/mutex.h>
25#include <linux/cdrom.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/time64.h>
29
30#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_devinfo.h>
34#include <scsi/scsi_eh.h>
35#include <scsi/scsi_host.h>
36
37#define CR_DRIVER_NAME		"rts5208"
38
39/*
40 * macros for easy use
41 */
42#define wait_timeout_x(task_state, msecs)	\
43do {						\
44	set_current_state((task_state));	\
45	schedule_timeout((msecs) * HZ / 1000);	\
46} while (0)
47#define wait_timeout(msecs)	wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
48
49#define STATE_TRANS_NONE	0
50#define STATE_TRANS_CMD		1
51#define STATE_TRANS_BUF		2
52#define STATE_TRANS_SG		3
53
54#define TRANS_NOT_READY		0
55#define TRANS_RESULT_OK		1
56#define TRANS_RESULT_FAIL	2
57
58#define SCSI_LUN(srb)		((srb)->device->lun)
59
60struct rtsx_chip;
61
62struct rtsx_dev {
63	struct pci_dev *pci;
64
65	/* pci resources */
66	unsigned long		addr;
67	void __iomem		*remap_addr;
68	int irq;
69
70	/* locks */
71	spinlock_t		reg_lock;
72
73	struct task_struct	*ctl_thread;	 /* the control thread   */
74	struct task_struct	*polling_thread; /* the polling thread   */
75
76	/* mutual exclusion and synchronization structures */
77	struct completion	cmnd_ready;	 /* to sleep thread on	    */
78	struct completion	control_exit;	 /* control thread exit	    */
79	struct completion	polling_exit;	 /* polling thread exit	    */
80	struct completion	notify;		 /* thread begin/end	    */
81	struct completion	scanning_done;	 /* wait for scan thread    */
82
83	wait_queue_head_t	delay_wait;	 /* wait during scan, reset */
84	struct mutex		dev_mutex;
85
86	/* host reserved buffer */
87	void			*rtsx_resv_buf;
88	dma_addr_t		rtsx_resv_buf_addr;
89
90	char			trans_result;
91	char			trans_state;
92
93	struct completion	*done;
94	/* Whether interrupt handler should care card cd info */
95	u32			check_card_cd;
96
97	struct rtsx_chip	*chip;
98};
99
100/* Convert between rtsx_dev and the corresponding Scsi_Host */
101static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
102{
103	return container_of((void *)dev, struct Scsi_Host, hostdata);
104}
105
106static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
107{
108	return (struct rtsx_dev *)host->hostdata;
109}
110
111#define lock_state(chip)	spin_lock_irq(&((chip)->rtsx->reg_lock))
112#define unlock_state(chip)	spin_unlock_irq(&((chip)->rtsx->reg_lock))
113
114/* struct scsi_cmnd transfer buffer access utilities */
115enum xfer_buf_dir	{TO_XFER_BUF, FROM_XFER_BUF};
116
117#include "rtsx_chip.h"
118#include "rtsx_transport.h"
119#include "rtsx_scsi.h"
120#include "rtsx_card.h"
121#include "rtsx_sys.h"
122#include "general.h"
123
124static inline void rtsx_writel(struct rtsx_chip *chip, u32 reg, u32 value)
125{
126	iowrite32(value, chip->rtsx->remap_addr + reg);
127}
128
129static inline u32 rtsx_readl(struct rtsx_chip *chip, u32 reg)
130{
131	return ioread32(chip->rtsx->remap_addr + reg);
132}
133
134static inline void rtsx_writew(struct rtsx_chip *chip, u32 reg, u16 value)
135{
136	iowrite16(value, chip->rtsx->remap_addr + reg);
137}
138
139static inline u16 rtsx_readw(struct rtsx_chip *chip, u32 reg)
140{
141	return ioread16(chip->rtsx->remap_addr + reg);
142}
143
144static inline void rtsx_writeb(struct rtsx_chip *chip, u32 reg, u8 value)
145{
146	iowrite8(value, chip->rtsx->remap_addr + reg);
147}
148
149static inline u8 rtsx_readb(struct rtsx_chip *chip, u32 reg)
150{
151	return ioread8((chip)->rtsx->remap_addr + reg);
152}
153
154static inline int rtsx_read_config_byte(struct rtsx_chip *chip, int where, u8 *val)
155{
156	return pci_read_config_byte(chip->rtsx->pci, where, val);
157}
158
159static inline int rtsx_write_config_byte(struct rtsx_chip *chip, int where, u8 val)
160{
161	return pci_write_config_byte(chip->rtsx->pci, where, val);
162}
163
164#endif  /* __REALTEK_RTSX_H */
165