xref: /openbmc/linux/drivers/staging/rts5208/rtsx.h (revision 95db3b25)
1 /* Driver for Realtek PCI-Express card reader
2  * Header file
3  *
4  * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * Author:
20  *   Wei WANG (wei_wang@realsil.com.cn)
21  *   Micky Ching (micky_ching@realsil.com.cn)
22  */
23 
24 #ifndef __REALTEK_RTSX_H
25 #define __REALTEK_RTSX_H
26 
27 #include <linux/io.h>
28 #include <linux/bitops.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/slab.h>
35 #include <linux/pci.h>
36 #include <linux/mutex.h>
37 #include <linux/cdrom.h>
38 #include <linux/workqueue.h>
39 #include <linux/timer.h>
40 #include <linux/time64.h>
41 
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_devinfo.h>
46 #include <scsi/scsi_eh.h>
47 #include <scsi/scsi_host.h>
48 
49 #define CR_DRIVER_NAME		"rts5208"
50 
51 /*
52  * macros for easy use
53  */
54 #define rtsx_writel(chip, reg, value) \
55 	iowrite32(value, (chip)->rtsx->remap_addr + reg)
56 #define rtsx_readl(chip, reg) \
57 	ioread32((chip)->rtsx->remap_addr + reg)
58 #define rtsx_writew(chip, reg, value) \
59 	iowrite16(value, (chip)->rtsx->remap_addr + reg)
60 #define rtsx_readw(chip, reg) \
61 	ioread16((chip)->rtsx->remap_addr + reg)
62 #define rtsx_writeb(chip, reg, value) \
63 	iowrite8(value, (chip)->rtsx->remap_addr + reg)
64 #define rtsx_readb(chip, reg) \
65 	ioread8((chip)->rtsx->remap_addr + reg)
66 
67 #define rtsx_read_config_byte(chip, where, val) \
68 	pci_read_config_byte((chip)->rtsx->pci, where, val)
69 
70 #define rtsx_write_config_byte(chip, where, val) \
71 	pci_write_config_byte((chip)->rtsx->pci, where, val)
72 
73 #define wait_timeout_x(task_state, msecs)		\
74 do {							\
75 		set_current_state((task_state));	\
76 		schedule_timeout((msecs) * HZ / 1000);	\
77 } while (0)
78 #define wait_timeout(msecs)	wait_timeout_x(TASK_INTERRUPTIBLE, (msecs))
79 
80 
81 #define STATE_TRANS_NONE	0
82 #define STATE_TRANS_CMD		1
83 #define STATE_TRANS_BUF		2
84 #define STATE_TRANS_SG		3
85 
86 #define TRANS_NOT_READY		0
87 #define TRANS_RESULT_OK		1
88 #define TRANS_RESULT_FAIL	2
89 
90 #define SCSI_LUN(srb)		((srb)->device->lun)
91 
92 typedef unsigned long DELAY_PARA_T;
93 
94 struct rtsx_chip;
95 
96 struct rtsx_dev {
97 	struct pci_dev *pci;
98 
99 	/* pci resources */
100 	unsigned long		addr;
101 	void __iomem		*remap_addr;
102 	int irq;
103 
104 	/* locks */
105 	spinlock_t		reg_lock;
106 
107 	struct task_struct	*ctl_thread;	 /* the control thread   */
108 	struct task_struct	*polling_thread; /* the polling thread   */
109 
110 	/* mutual exclusion and synchronization structures */
111 	struct completion	cmnd_ready;	 /* to sleep thread on	    */
112 	struct completion	control_exit;	 /* control thread exit	    */
113 	struct completion	polling_exit;	 /* polling thread exit	    */
114 	struct completion	notify;		 /* thread begin/end	    */
115 	struct completion	scanning_done;	 /* wait for scan thread    */
116 
117 	wait_queue_head_t	delay_wait;	 /* wait during scan, reset */
118 	struct mutex		dev_mutex;
119 
120 	/* host reserved buffer */
121 	void			*rtsx_resv_buf;
122 	dma_addr_t		rtsx_resv_buf_addr;
123 
124 	char			trans_result;
125 	char			trans_state;
126 
127 	struct completion	*done;
128 	/* Whether interrupt handler should care card cd info */
129 	u32			check_card_cd;
130 
131 	struct rtsx_chip	*chip;
132 };
133 
134 typedef struct rtsx_dev rtsx_dev_t;
135 
136 /* Convert between rtsx_dev and the corresponding Scsi_Host */
137 static inline struct Scsi_Host *rtsx_to_host(struct rtsx_dev *dev)
138 {
139 	return container_of((void *) dev, struct Scsi_Host, hostdata);
140 }
141 static inline struct rtsx_dev *host_to_rtsx(struct Scsi_Host *host)
142 {
143 	return (struct rtsx_dev *) host->hostdata;
144 }
145 
146 static inline void get_current_time(u8 *timeval_buf, int buf_len)
147 {
148 	struct timespec64 ts64;
149 	u32 tv_usec;
150 
151 	if (!timeval_buf || (buf_len < 8))
152 		return;
153 
154 	getnstimeofday64(&ts64);
155 
156 	tv_usec = ts64.tv_nsec/NSEC_PER_USEC;
157 
158 	timeval_buf[0] = (u8)(ts64.tv_sec >> 24);
159 	timeval_buf[1] = (u8)(ts64.tv_sec >> 16);
160 	timeval_buf[2] = (u8)(ts64.tv_sec >> 8);
161 	timeval_buf[3] = (u8)(ts64.tv_sec);
162 	timeval_buf[4] = (u8)(tv_usec >> 24);
163 	timeval_buf[5] = (u8)(tv_usec >> 16);
164 	timeval_buf[6] = (u8)(tv_usec >> 8);
165 	timeval_buf[7] = (u8)(tv_usec);
166 }
167 
168 /* The scsi_lock() and scsi_unlock() macros protect the sm_state and the
169  * single queue element srb for write access */
170 #define scsi_unlock(host)	spin_unlock_irq(host->host_lock)
171 #define scsi_lock(host)		spin_lock_irq(host->host_lock)
172 
173 #define lock_state(chip)	spin_lock_irq(&((chip)->rtsx->reg_lock))
174 #define unlock_state(chip)	spin_unlock_irq(&((chip)->rtsx->reg_lock))
175 
176 /* struct scsi_cmnd transfer buffer access utilities */
177 enum xfer_buf_dir	{TO_XFER_BUF, FROM_XFER_BUF};
178 
179 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val);
180 
181 #define _MSG_TRACE
182 
183 #include "trace.h"
184 #include "rtsx_chip.h"
185 #include "rtsx_transport.h"
186 #include "rtsx_scsi.h"
187 #include "rtsx_card.h"
188 #include "rtsx_sys.h"
189 #include "general.h"
190 
191 #endif  /* __REALTEK_RTSX_H */
192