7bac2f7d8b3f12f3eee555ea83c7d86180d14852
[linux-2.6.git] / 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/device.h>
20
21 #include <asm/hardware.h>
22 #include <asm/irq.h>
23
24 #include <asm/hardware/scoop.h>
25 #include <asm/arch/corgi.h>
26 #include <asm/arch/pxa-regs.h>
27
28 #include "soc_common.h"
29
30 #define NO_KEEP_VS 0x0001
31
32 static unsigned char keep_vs;
33 static unsigned char keep_rd;
34
35 static struct pcmcia_irqs irqs[] = {
36         { 0, CORGI_IRQ_GPIO_CF_CD, "PCMCIA0 CD"},
37 };
38
39 static void sharpsl_pcmcia_init_reset(void)
40 {
41         reset_scoop(&corgiscoop_device.dev);
42         keep_vs = NO_KEEP_VS;
43         keep_rd = 0;
44 }
45
46 static int sharpsl_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
47 {
48         int ret;
49
50         /*
51          * Setup default state of GPIO outputs
52          * before we enable them as outputs.
53          */
54         GPSR(GPIO48_nPOE) =
55                 GPIO_bit(GPIO48_nPOE) |
56                 GPIO_bit(GPIO49_nPWE) |
57                 GPIO_bit(GPIO50_nPIOR) |
58                 GPIO_bit(GPIO51_nPIOW) |
59                 GPIO_bit(GPIO52_nPCE_1) |
60                 GPIO_bit(GPIO53_nPCE_2);
61
62         pxa_gpio_mode(GPIO48_nPOE_MD);
63         pxa_gpio_mode(GPIO49_nPWE_MD);
64         pxa_gpio_mode(GPIO50_nPIOR_MD);
65         pxa_gpio_mode(GPIO51_nPIOW_MD);
66         pxa_gpio_mode(GPIO52_nPCE_1_MD);
67         pxa_gpio_mode(GPIO53_nPCE_2_MD);
68         pxa_gpio_mode(GPIO54_pSKTSEL_MD);
69         pxa_gpio_mode(GPIO55_nPREG_MD);
70         pxa_gpio_mode(GPIO56_nPWAIT_MD);
71         pxa_gpio_mode(GPIO57_nIOIS16_MD);
72
73         /* Register interrupts */
74         ret = soc_pcmcia_request_irqs(skt, irqs, ARRAY_SIZE(irqs));
75
76         if (ret) {
77                 printk(KERN_ERR "Request for Compact Flash IRQ failed\n");
78                 return ret;
79         }
80
81         /* Enable interrupt */
82         write_scoop_reg(&corgiscoop_device.dev, SCOOP_IMR, 0x00C0);
83         write_scoop_reg(&corgiscoop_device.dev, SCOOP_MCR, 0x0101);
84         keep_vs = NO_KEEP_VS;
85
86         skt->irq = CORGI_IRQ_GPIO_CF_IRQ;
87
88         return 0;
89 }
90
91 static void sharpsl_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
92 {
93         soc_pcmcia_free_irqs(skt, irqs, ARRAY_SIZE(irqs));
94
95         /* CF_BUS_OFF */
96         sharpsl_pcmcia_init_reset();
97 }
98
99
100 static void sharpsl_pcmcia_socket_state(struct soc_pcmcia_socket *skt,
101                                     struct pcmcia_state *state)
102 {
103         unsigned short cpr, csr;
104
105         cpr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_CPR);
106
107         write_scoop_reg(&corgiscoop_device.dev, SCOOP_IRM, 0x00FF);
108         write_scoop_reg(&corgiscoop_device.dev, SCOOP_ISR, 0x0000);
109         write_scoop_reg(&corgiscoop_device.dev, SCOOP_IRM, 0x0000);
110         csr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_CSR);
111         if (csr & 0x0004) {
112                 /* card eject */
113                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CDR, 0x0000);
114                 keep_vs = NO_KEEP_VS;
115         }
116         else if (!(keep_vs & NO_KEEP_VS)) {
117                 /* keep vs1,vs2 */
118                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CDR, 0x0000);
119                 csr |= keep_vs;
120         }
121         else if (cpr & 0x0003) {
122                 /* power on */
123                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CDR, 0x0000);
124                 keep_vs = (csr & 0x00C0);
125         }
126         else {
127                 /* card detect */
128                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CDR, 0x0002);
129         }
130
131         state->detect = (csr & 0x0004) ? 0 : 1;
132         state->ready  = (csr & 0x0002) ? 1 : 0;
133         state->bvd1   = (csr & 0x0010) ? 1 : 0;
134         state->bvd2   = (csr & 0x0020) ? 1 : 0;
135         state->wrprot = (csr & 0x0008) ? 1 : 0;
136         state->vs_3v  = (csr & 0x0040) ? 0 : 1;
137         state->vs_Xv  = (csr & 0x0080) ? 0 : 1;
138
139         if ((cpr & 0x0080) && ((cpr & 0x8040) != 0x8040)) {
140                 printk(KERN_ERR "sharpsl_pcmcia_socket_state(): CPR=%04X, Low voltage!\n", cpr);
141         }
142
143 }
144
145
146 static int sharpsl_pcmcia_configure_socket(struct soc_pcmcia_socket *skt,
147                                        const socket_state_t *state)
148 {
149         unsigned long flags;
150
151         unsigned short cpr, ncpr, ccr, nccr, mcr, nmcr, imr, nimr;
152
153         switch (state->Vcc) {
154         case    0:      break;
155         case    33:     break;
156         case    50:     break;
157         default:
158                  printk(KERN_ERR "sharpsl_pcmcia_configure_socket(): bad Vcc %u\n", state->Vcc);
159                  return -1;
160         }
161
162         if ((state->Vpp!=state->Vcc) && (state->Vpp!=0)) {
163                 printk(KERN_ERR "CF slot cannot support Vpp %u\n", state->Vpp);
164                 return -1;
165         }
166
167         local_irq_save(flags);
168
169         nmcr = (mcr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_MCR)) & ~0x0010;
170         ncpr = (cpr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_CPR)) & ~0x0083;
171         nccr = (ccr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_CCR)) & ~0x0080;
172         nimr = (imr = read_scoop_reg(&corgiscoop_device.dev, SCOOP_IMR)) & ~0x003E;
173
174         ncpr |= (state->Vcc == 33) ? 0x0001 :
175                                 (state->Vcc == 50) ? 0x0002 : 0;
176         nmcr |= (state->flags&SS_IOCARD) ? 0x0010 : 0;
177         ncpr |= (state->flags&SS_OUTPUT_ENA) ? 0x0080 : 0;
178         nccr |= (state->flags&SS_RESET)? 0x0080: 0;
179         nimr |= ((skt->status&SS_DETECT) ? 0x0004 : 0)|
180                         ((skt->status&SS_READY)  ? 0x0002 : 0)|
181                         ((skt->status&SS_BATDEAD)? 0x0010 : 0)|
182                         ((skt->status&SS_BATWARN)? 0x0020 : 0)|
183                         ((skt->status&SS_STSCHG) ? 0x0010 : 0)|
184                         ((skt->status&SS_WRPROT) ? 0x0008 : 0);
185
186         if (!(ncpr & 0x0003)) {
187                 keep_rd = 0;
188         } else if (!keep_rd) {
189                 if (nccr & 0x0080)
190                         keep_rd = 1;
191                 else
192                         nccr |= 0x0080;
193         }
194
195         if (mcr != nmcr)
196                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_MCR, nmcr);
197         if (cpr != ncpr)
198                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CPR, ncpr);
199         if (ccr != nccr)
200                 write_scoop_reg(&corgiscoop_device.dev, SCOOP_CCR, nccr);
201         if (imr != nimr)
202                 write_scoop_reg(&corgiscoop_device.dev, 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 }
212
213 static void sharpsl_pcmcia_socket_suspend(struct soc_pcmcia_socket *skt)
214 {
215 }
216
217 static struct pcmcia_low_level sharpsl_pcmcia_ops = {
218         .owner                          = THIS_MODULE,
219         .hw_init                        = sharpsl_pcmcia_hw_init,
220         .hw_shutdown            = sharpsl_pcmcia_hw_shutdown,
221         .socket_state           = sharpsl_pcmcia_socket_state,
222         .configure_socket       = sharpsl_pcmcia_configure_socket,
223         .socket_init            = sharpsl_pcmcia_socket_init,
224         .socket_suspend         = sharpsl_pcmcia_socket_suspend,
225         .first                          = 0,
226         .nr                                     = 1,
227 };
228
229 static struct platform_device *sharpsl_pcmcia_device;
230
231 static int __init sharpsl_pcmcia_init(void)
232 {
233         int ret;
234
235         sharpsl_pcmcia_device = kmalloc(sizeof(*sharpsl_pcmcia_device), GFP_KERNEL);
236         if (!sharpsl_pcmcia_device)
237                 return -ENOMEM;
238         memset(sharpsl_pcmcia_device, 0, sizeof(*sharpsl_pcmcia_device));
239         sharpsl_pcmcia_device->name = "pxa2xx-pcmcia";
240         sharpsl_pcmcia_device->dev.platform_data = &sharpsl_pcmcia_ops;
241
242         ret = platform_device_register(sharpsl_pcmcia_device);
243         if (ret)
244                 kfree(sharpsl_pcmcia_device);
245
246         return ret;
247 }
248
249 static void __exit sharpsl_pcmcia_exit(void)
250 {
251         /*
252          * This call is supposed to free our sharpsl_pcmcia_device.
253          * Unfortunately platform_device don't have a free method, and
254          * we can't assume it's free of any reference at this point so we
255          * can't free it either.
256          */
257         platform_device_unregister(sharpsl_pcmcia_device);
258 }
259
260 fs_initcall(sharpsl_pcmcia_init);
261 module_exit(sharpsl_pcmcia_exit);
262
263 MODULE_DESCRIPTION("Sharp SL Series PCMCIA Support");
264 MODULE_LICENSE("GPL");