1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sharp SL-C7xx Series PCMCIA routines
4  *
5  * Copyright (c) 2004-2005 Richard Purdie
6  *
7  * Based on Sharp's 2.4 kernel patches and pxa2xx_mainstone.c
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 
17 #include <asm/mach-types.h>
18 #include <asm/irq.h>
19 #include <asm/hardware/scoop.h>
20 
21 #include <pcmcia/soc_common.h>
22 
23 #define	NO_KEEP_VS 0x0001
24 #define SCOOP_DEV platform_scoop_config->devs
25 
26 static void sharpsl_pcmcia_init_reset(struct soc_pcmcia_socket *skt)
27 {
28 	struct scoop_pcmcia_dev *scoopdev = &SCOOP_DEV[skt->nr];
29 
30 	reset_scoop(scoopdev->dev);
31 
32 	/* Shared power controls need to be handled carefully */
33 	if (platform_scoop_config->power_ctrl)
34 		platform_scoop_config->power_ctrl(scoopdev->dev, 0x0000, skt->nr);
35 	else
36 		write_scoop_reg(scoopdev->dev, SCOOP_CPR, 0x0000);
37 
38 	scoopdev->keep_vs = NO_KEEP_VS;
39 	scoopdev->keep_rd = 0;
40 }
41 
42 static int sharpsl_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
43 {
44 	if (SCOOP_DEV[skt->nr].cd_irq >= 0) {
45 		skt->stat[SOC_STAT_CD].irq = SCOOP_DEV[skt->nr].cd_irq;
46 		skt->stat[SOC_STAT_CD].name = SCOOP_DEV[skt->nr].cd_irq_str;
47 	}
48 
49 	skt->socket.pci_irq = SCOOP_DEV[skt->nr].irq;
50 
51 	return 0;
52 }
53 
54 static void sharpsl_pcmcia_socket_state(struct soc_pcmcia_socket *skt,
55 				    struct pcmcia_state *state)
56 {
57 	unsigned short cpr, csr;
58 	struct device *scoop = SCOOP_DEV[skt->nr].dev;
59 
60 	cpr = read_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR);
61 
62 	write_scoop_reg(scoop, SCOOP_IRM, 0x00FF);
63 	write_scoop_reg(scoop, SCOOP_ISR, 0x0000);
64 	write_scoop_reg(scoop, SCOOP_IRM, 0x0000);
65 	csr = read_scoop_reg(scoop, SCOOP_CSR);
66 	if (csr & 0x0004) {
67 		/* card eject */
68 		write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
69 		SCOOP_DEV[skt->nr].keep_vs = NO_KEEP_VS;
70 	}
71 	else if (!(SCOOP_DEV[skt->nr].keep_vs & NO_KEEP_VS)) {
72 		/* keep vs1,vs2 */
73 		write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
74 		csr |= SCOOP_DEV[skt->nr].keep_vs;
75 	}
76 	else if (cpr & 0x0003) {
77 		/* power on */
78 		write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
79 		SCOOP_DEV[skt->nr].keep_vs = (csr & 0x00C0);
80 	}
81 	else {
82 		/* card detect */
83 	        if ((machine_is_spitz() || machine_is_borzoi()) && skt->nr == 1) {
84 	                write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
85 	        } else {
86 		        write_scoop_reg(scoop, SCOOP_CDR, 0x0002);
87 	        }
88 	}
89 
90 	state->detect = (csr & 0x0004) ? 0 : 1;
91 	state->ready  = (csr & 0x0002) ? 1 : 0;
92 	state->bvd1   = (csr & 0x0010) ? 1 : 0;
93 	state->bvd2   = (csr & 0x0020) ? 1 : 0;
94 	state->wrprot = (csr & 0x0008) ? 1 : 0;
95 	state->vs_3v  = (csr & 0x0040) ? 0 : 1;
96 	state->vs_Xv  = (csr & 0x0080) ? 0 : 1;
97 
98 	if ((cpr & 0x0080) && ((cpr & 0x8040) != 0x8040)) {
99 		printk(KERN_ERR "sharpsl_pcmcia_socket_state(): CPR=%04X, Low voltage!\n", cpr);
100 	}
101 }
102 
103 
104 static int sharpsl_pcmcia_configure_socket(struct soc_pcmcia_socket *skt,
105 				       const socket_state_t *state)
106 {
107 	unsigned long flags;
108 	struct device *scoop = SCOOP_DEV[skt->nr].dev;
109 
110 	unsigned short cpr, ncpr, ccr, nccr, mcr, nmcr, imr, nimr;
111 
112 	switch (state->Vcc) {
113 	case	0:  	break;
114 	case 	33: 	break;
115 	case	50: 	break;
116 	default:
117 		 printk(KERN_ERR "sharpsl_pcmcia_configure_socket(): bad Vcc %u\n", state->Vcc);
118 		 return -1;
119 	}
120 
121 	if ((state->Vpp!=state->Vcc) && (state->Vpp!=0)) {
122 		printk(KERN_ERR "CF slot cannot support Vpp %u\n", state->Vpp);
123 		return -1;
124 	}
125 
126 	local_irq_save(flags);
127 
128 	nmcr = (mcr = read_scoop_reg(scoop, SCOOP_MCR)) & ~0x0010;
129 	ncpr = (cpr = read_scoop_reg(scoop, SCOOP_CPR)) & ~0x0083;
130 	nccr = (ccr = read_scoop_reg(scoop, SCOOP_CCR)) & ~0x0080;
131 	nimr = (imr = read_scoop_reg(scoop, SCOOP_IMR)) & ~0x003E;
132 
133 	if ((machine_is_spitz() || machine_is_borzoi() || machine_is_akita()) && skt->nr == 0) {
134 	        ncpr |= (state->Vcc == 33) ? 0x0002 :
135 		        (state->Vcc == 50) ? 0x0002 : 0;
136 	} else {
137 	        ncpr |= (state->Vcc == 33) ? 0x0001 :
138 		        (state->Vcc == 50) ? 0x0002 : 0;
139 	}
140 	nmcr |= (state->flags&SS_IOCARD) ? 0x0010 : 0;
141 	ncpr |= (state->flags&SS_OUTPUT_ENA) ? 0x0080 : 0;
142 	nccr |= (state->flags&SS_RESET)? 0x0080: 0;
143 	nimr |=	((skt->status&SS_DETECT) ? 0x0004 : 0)|
144 			((skt->status&SS_READY)  ? 0x0002 : 0)|
145 			((skt->status&SS_BATDEAD)? 0x0010 : 0)|
146 			((skt->status&SS_BATWARN)? 0x0020 : 0)|
147 			((skt->status&SS_STSCHG) ? 0x0010 : 0)|
148 			((skt->status&SS_WRPROT) ? 0x0008 : 0);
149 
150 	if (!(ncpr & 0x0003)) {
151 		SCOOP_DEV[skt->nr].keep_rd = 0;
152 	} else if (!SCOOP_DEV[skt->nr].keep_rd) {
153 		if (nccr & 0x0080)
154 			SCOOP_DEV[skt->nr].keep_rd = 1;
155 		else
156 			nccr |= 0x0080;
157 	}
158 
159 	if (mcr != nmcr)
160 		write_scoop_reg(scoop, SCOOP_MCR, nmcr);
161 	if (cpr != ncpr) {
162 		if (platform_scoop_config->power_ctrl)
163 			platform_scoop_config->power_ctrl(scoop, ncpr , skt->nr);
164 		else
165 		        write_scoop_reg(scoop, SCOOP_CPR, ncpr);
166 	}
167 	if (ccr != nccr)
168 		write_scoop_reg(scoop, SCOOP_CCR, nccr);
169 	if (imr != nimr)
170 		write_scoop_reg(scoop, SCOOP_IMR, nimr);
171 
172 	local_irq_restore(flags);
173 
174 	return 0;
175 }
176 
177 static void sharpsl_pcmcia_socket_init(struct soc_pcmcia_socket *skt)
178 {
179 	sharpsl_pcmcia_init_reset(skt);
180 
181 	/* Enable interrupt */
182 	write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_IMR, 0x00C0);
183 	write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_MCR, 0x0101);
184 	SCOOP_DEV[skt->nr].keep_vs = NO_KEEP_VS;
185 }
186 
187 static void sharpsl_pcmcia_socket_suspend(struct soc_pcmcia_socket *skt)
188 {
189 	sharpsl_pcmcia_init_reset(skt);
190 }
191 
192 static struct pcmcia_low_level sharpsl_pcmcia_ops = {
193 	.owner                  = THIS_MODULE,
194 	.hw_init                = sharpsl_pcmcia_hw_init,
195 	.socket_state           = sharpsl_pcmcia_socket_state,
196 	.configure_socket       = sharpsl_pcmcia_configure_socket,
197 	.socket_init            = sharpsl_pcmcia_socket_init,
198 	.socket_suspend         = sharpsl_pcmcia_socket_suspend,
199 	.first                  = 0,
200 	.nr                     = 0,
201 };
202 
203 #ifdef CONFIG_SA1100_COLLIE
204 #include "sa11xx_base.h"
205 
206 int pcmcia_collie_init(struct device *dev)
207 {
208        int ret = -ENODEV;
209 
210        if (machine_is_collie())
211                ret = sa11xx_drv_pcmcia_probe(dev, &sharpsl_pcmcia_ops, 0, 1);
212 
213        return ret;
214 }
215 
216 #else
217 
218 static struct platform_device *sharpsl_pcmcia_device;
219 
220 static int __init sharpsl_pcmcia_init(void)
221 {
222 	int ret;
223 
224 	if (!platform_scoop_config)
225 		return -ENODEV;
226 
227 	sharpsl_pcmcia_ops.nr = platform_scoop_config->num_devs;
228 	sharpsl_pcmcia_device = platform_device_alloc("pxa2xx-pcmcia", -1);
229 
230 	if (!sharpsl_pcmcia_device)
231 		return -ENOMEM;
232 
233 	ret = platform_device_add_data(sharpsl_pcmcia_device,
234 			&sharpsl_pcmcia_ops, sizeof(sharpsl_pcmcia_ops));
235 	if (ret == 0) {
236 		sharpsl_pcmcia_device->dev.parent = platform_scoop_config->devs[0].dev;
237 		ret = platform_device_add(sharpsl_pcmcia_device);
238 	}
239 
240 	if (ret)
241 		platform_device_put(sharpsl_pcmcia_device);
242 
243 	return ret;
244 }
245 
246 static void __exit sharpsl_pcmcia_exit(void)
247 {
248 	platform_device_unregister(sharpsl_pcmcia_device);
249 }
250 
251 fs_initcall(sharpsl_pcmcia_init);
252 module_exit(sharpsl_pcmcia_exit);
253 #endif
254 
255 MODULE_DESCRIPTION("Sharp SL Series PCMCIA Support");
256 MODULE_LICENSE("GPL");
257 MODULE_ALIAS("platform:pxa2xx-pcmcia");
258