[PATCH] md: initial sysfs support for md
[linux-2.6] / drivers / pcmcia / pxa2xx_sharpsl.c
1 /*
2  * Sharp SL-C7xx Series PCMCIA routines
3  *
4  * Copyright (c) 2004-2005 Richard Purdie
5  *
6  * Based on Sharp's 2.4 kernel patches and pxa2xx_mainstone.c
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20
21 #include <asm/mach-types.h>
22 #include <asm/hardware.h>
23 #include <asm/irq.h>
24 #include <asm/hardware/scoop.h>
25
26 #include "soc_common.h"
27
28 #define NO_KEEP_VS 0x0001
29
30 /* PCMCIA to Scoop linkage
31
32    There is no easy way to link multiple scoop devices into one
33    single entity for the pxa2xx_pcmcia device so this structure
34    is used which is setup by the platform code
35 */
36 struct scoop_pcmcia_config *platform_scoop_config;
37 #define SCOOP_DEV platform_scoop_config->devs
38
39 static void sharpsl_pcmcia_init_reset(struct scoop_pcmcia_dev *scoopdev)
40 {
41         reset_scoop(scoopdev->dev);
42         scoopdev->keep_vs = NO_KEEP_VS;
43         scoopdev->keep_rd = 0;
44 }
45
46 static int sharpsl_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
47 {
48         int ret;
49
50         if (platform_scoop_config->pcmcia_init)
51                 platform_scoop_config->pcmcia_init();
52
53         /* Register interrupts */
54         if (SCOOP_DEV[skt->nr].cd_irq >= 0) {
55                 struct pcmcia_irqs cd_irq;
56
57                 cd_irq.sock = skt->nr;
58                 cd_irq.irq  = SCOOP_DEV[skt->nr].cd_irq;
59                 cd_irq.str  = SCOOP_DEV[skt->nr].cd_irq_str;
60                 ret = soc_pcmcia_request_irqs(skt, &cd_irq, 1);
61
62                 if (ret) {
63                         printk(KERN_ERR "Request for Compact Flash IRQ failed\n");
64                         return ret;
65                 }
66         }
67
68         skt->irq = SCOOP_DEV[skt->nr].irq;
69
70         return 0;
71 }
72
73 static void sharpsl_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
74 {
75         if (SCOOP_DEV[skt->nr].cd_irq >= 0) {
76                 struct pcmcia_irqs cd_irq;
77
78                 cd_irq.sock = skt->nr;
79                 cd_irq.irq  = SCOOP_DEV[skt->nr].cd_irq;
80                 cd_irq.str  = SCOOP_DEV[skt->nr].cd_irq_str;
81                 soc_pcmcia_free_irqs(skt, &cd_irq, 1);
82         }
83 }
84
85
86 static void sharpsl_pcmcia_socket_state(struct soc_pcmcia_socket *skt,
87                                     struct pcmcia_state *state)
88 {
89         unsigned short cpr, csr;
90         struct device *scoop = SCOOP_DEV[skt->nr].dev;
91
92         cpr = read_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR);
93
94         write_scoop_reg(scoop, SCOOP_IRM, 0x00FF);
95         write_scoop_reg(scoop, SCOOP_ISR, 0x0000);
96         write_scoop_reg(scoop, SCOOP_IRM, 0x0000);
97         csr = read_scoop_reg(scoop, SCOOP_CSR);
98         if (csr & 0x0004) {
99                 /* card eject */
100                 write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
101                 SCOOP_DEV[skt->nr].keep_vs = NO_KEEP_VS;
102         }
103         else if (!(SCOOP_DEV[skt->nr].keep_vs & NO_KEEP_VS)) {
104                 /* keep vs1,vs2 */
105                 write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
106                 csr |= SCOOP_DEV[skt->nr].keep_vs;
107         }
108         else if (cpr & 0x0003) {
109                 /* power on */
110                 write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
111                 SCOOP_DEV[skt->nr].keep_vs = (csr & 0x00C0);
112         }
113         else {
114                 /* card detect */
115                 if ((machine_is_spitz() || machine_is_borzoi()) && skt->nr == 1) {
116                         write_scoop_reg(scoop, SCOOP_CDR, 0x0000);
117                 } else {
118                         write_scoop_reg(scoop, SCOOP_CDR, 0x0002);
119                 }
120         }
121
122         state->detect = (csr & 0x0004) ? 0 : 1;
123         state->ready  = (csr & 0x0002) ? 1 : 0;
124         state->bvd1   = (csr & 0x0010) ? 1 : 0;
125         state->bvd2   = (csr & 0x0020) ? 1 : 0;
126         state->wrprot = (csr & 0x0008) ? 1 : 0;
127         state->vs_3v  = (csr & 0x0040) ? 0 : 1;
128         state->vs_Xv  = (csr & 0x0080) ? 0 : 1;
129
130         if ((cpr & 0x0080) && ((cpr & 0x8040) != 0x8040)) {
131                 printk(KERN_ERR "sharpsl_pcmcia_socket_state(): CPR=%04X, Low voltage!\n", cpr);
132         }
133 }
134
135
136 static int sharpsl_pcmcia_configure_socket(struct soc_pcmcia_socket *skt,
137                                        const socket_state_t *state)
138 {
139         unsigned long flags;
140         struct device *scoop = SCOOP_DEV[skt->nr].dev;
141
142         unsigned short cpr, ncpr, ccr, nccr, mcr, nmcr, imr, nimr;
143
144         switch (state->Vcc) {
145         case    0:      break;
146         case    33:     break;
147         case    50:     break;
148         default:
149                  printk(KERN_ERR "sharpsl_pcmcia_configure_socket(): bad Vcc %u\n", state->Vcc);
150                  return -1;
151         }
152
153         if ((state->Vpp!=state->Vcc) && (state->Vpp!=0)) {
154                 printk(KERN_ERR "CF slot cannot support Vpp %u\n", state->Vpp);
155                 return -1;
156         }
157
158         local_irq_save(flags);
159
160         nmcr = (mcr = read_scoop_reg(scoop, SCOOP_MCR)) & ~0x0010;
161         ncpr = (cpr = read_scoop_reg(scoop, SCOOP_CPR)) & ~0x0083;
162         nccr = (ccr = read_scoop_reg(scoop, SCOOP_CCR)) & ~0x0080;
163         nimr = (imr = read_scoop_reg(scoop, SCOOP_IMR)) & ~0x003E;
164
165         if ((machine_is_spitz() || machine_is_borzoi() || machine_is_akita()) && skt->nr == 0) {
166                 ncpr |= (state->Vcc == 33) ? 0x0002 :
167                         (state->Vcc == 50) ? 0x0002 : 0;
168         } else {
169                 ncpr |= (state->Vcc == 33) ? 0x0001 :
170                         (state->Vcc == 50) ? 0x0002 : 0;
171         }
172         nmcr |= (state->flags&SS_IOCARD) ? 0x0010 : 0;
173         ncpr |= (state->flags&SS_OUTPUT_ENA) ? 0x0080 : 0;
174         nccr |= (state->flags&SS_RESET)? 0x0080: 0;
175         nimr |= ((skt->status&SS_DETECT) ? 0x0004 : 0)|
176                         ((skt->status&SS_READY)  ? 0x0002 : 0)|
177                         ((skt->status&SS_BATDEAD)? 0x0010 : 0)|
178                         ((skt->status&SS_BATWARN)? 0x0020 : 0)|
179                         ((skt->status&SS_STSCHG) ? 0x0010 : 0)|
180                         ((skt->status&SS_WRPROT) ? 0x0008 : 0);
181
182         if (!(ncpr & 0x0003)) {
183                 SCOOP_DEV[skt->nr].keep_rd = 0;
184         } else if (!SCOOP_DEV[skt->nr].keep_rd) {
185                 if (nccr & 0x0080)
186                         SCOOP_DEV[skt->nr].keep_rd = 1;
187                 else
188                         nccr |= 0x0080;
189         }
190
191         if (mcr != nmcr)
192                 write_scoop_reg(scoop, SCOOP_MCR, nmcr);
193         if (cpr != ncpr) {
194                 if (platform_scoop_config->power_ctrl)
195                         platform_scoop_config->power_ctrl(scoop, ncpr , skt->nr);
196                 else
197                         write_scoop_reg(scoop, SCOOP_CPR, ncpr);
198         }
199         if (ccr != nccr)
200                 write_scoop_reg(scoop, SCOOP_CCR, nccr);
201         if (imr != nimr)
202                 write_scoop_reg(scoop, SCOOP_IMR, nimr);
203
204         local_irq_restore(flags);
205
206         return 0;
207 }
208
209 static void sharpsl_pcmcia_socket_init(struct soc_pcmcia_socket *skt)
210 {
211         sharpsl_pcmcia_init_reset(&SCOOP_DEV[skt->nr]);
212
213         /* Enable interrupt */
214         write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_IMR, 0x00C0);
215         write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_MCR, 0x0101);
216         SCOOP_DEV[skt->nr].keep_vs = NO_KEEP_VS;
217
218         if (machine_is_collie())
219                 /* We need to disable SS_OUTPUT_ENA here. */
220                 write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR, read_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR) & ~0x0080);
221 }
222
223 static void sharpsl_pcmcia_socket_suspend(struct soc_pcmcia_socket *skt)
224 {
225         /* CF_BUS_OFF */
226         sharpsl_pcmcia_init_reset(&SCOOP_DEV[skt->nr]);
227
228         if (machine_is_collie())
229                 /* We need to disable SS_OUTPUT_ENA here. */
230                 write_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR, read_scoop_reg(SCOOP_DEV[skt->nr].dev, SCOOP_CPR) & ~0x0080);
231 }
232
233 static struct pcmcia_low_level sharpsl_pcmcia_ops = {
234         .owner                  = THIS_MODULE,
235         .hw_init                = sharpsl_pcmcia_hw_init,
236         .hw_shutdown            = sharpsl_pcmcia_hw_shutdown,
237         .socket_state           = sharpsl_pcmcia_socket_state,
238         .configure_socket       = sharpsl_pcmcia_configure_socket,
239         .socket_init            = sharpsl_pcmcia_socket_init,
240         .socket_suspend         = sharpsl_pcmcia_socket_suspend,
241         .first                  = 0,
242         .nr                     = 0,
243 };
244
245 #ifdef CONFIG_SA1100_COLLIE
246 #include "sa11xx_base.h"
247
248 int __init pcmcia_collie_init(struct device *dev)
249 {
250        int ret = -ENODEV;
251
252        if (machine_is_collie())
253                ret = sa11xx_drv_pcmcia_probe(dev, &sharpsl_pcmcia_ops, 0, 1);
254
255        return ret;
256 }
257
258 #else
259
260 static struct platform_device *sharpsl_pcmcia_device;
261
262 static int __init sharpsl_pcmcia_init(void)
263 {
264         int ret;
265
266         sharpsl_pcmcia_ops.nr=platform_scoop_config->num_devs;
267         sharpsl_pcmcia_device = kmalloc(sizeof(*sharpsl_pcmcia_device), GFP_KERNEL);
268         if (!sharpsl_pcmcia_device)
269                 return -ENOMEM;
270
271         memset(sharpsl_pcmcia_device, 0, sizeof(*sharpsl_pcmcia_device));
272         sharpsl_pcmcia_device->name = "pxa2xx-pcmcia";
273         sharpsl_pcmcia_device->dev.platform_data = &sharpsl_pcmcia_ops;
274         sharpsl_pcmcia_device->dev.parent=platform_scoop_config->devs[0].dev;
275
276         ret = platform_device_register(sharpsl_pcmcia_device);
277         if (ret)
278                 kfree(sharpsl_pcmcia_device);
279
280         return ret;
281 }
282
283 static void __exit sharpsl_pcmcia_exit(void)
284 {
285         /*
286          * This call is supposed to free our sharpsl_pcmcia_device.
287          * Unfortunately platform_device don't have a free method, and
288          * we can't assume it's free of any reference at this point so we
289          * can't free it either.
290          */
291         platform_device_unregister(sharpsl_pcmcia_device);
292 }
293
294 fs_initcall(sharpsl_pcmcia_init);
295 module_exit(sharpsl_pcmcia_exit);
296 #endif
297
298 MODULE_DESCRIPTION("Sharp SL Series PCMCIA Support");
299 MODULE_LICENSE("GPL");