[brcm63xx] request gpio pins and set direction correctly for pcmcia
[openwrt.git] / target / linux / brcm63xx / files / drivers / pcmcia / bcm63xx_pcmcia.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/ioport.h>
12 #include <linux/timer.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/pci.h>
16 #include <linux/gpio.h>
17
18 #include <bcm63xx_regs.h>
19 #include <bcm63xx_io.h>
20 #include "bcm63xx_pcmcia.h"
21
22 #define PFX     "bcm63xx_pcmcia: "
23
24 #ifdef CONFIG_CARDBUS
25 /* if cardbus is used, platform device needs reference to actual pci
26  * device */
27 static struct pci_dev *bcm63xx_cb_dev;
28 #endif
29
30 /*
31  * read/write helper for pcmcia regs
32  */
33 static inline u32 pcmcia_readl(struct bcm63xx_pcmcia_socket *skt, u32 off)
34 {
35         return bcm_readl(skt->base + off);
36 }
37
38 static inline void pcmcia_writel(struct bcm63xx_pcmcia_socket *skt,
39                                  u32 val, u32 off)
40 {
41         bcm_writel(val, skt->base + off);
42 }
43
44 /*
45  * (Re-)Initialise the socket, turning on status interrupts and PCMCIA
46  * bus.  This must wait for power to stabilise so that the card status
47  * signals report correctly.
48  */
49 static int bcm63xx_pcmcia_sock_init(struct pcmcia_socket *sock)
50 {
51         struct bcm63xx_pcmcia_socket *skt;
52         skt = sock->driver_data;
53         return 0;
54 }
55
56 /*
57  * Remove power on the socket, disable IRQs from the card.
58  * Turn off status interrupts, and disable the PCMCIA bus.
59  */
60 static int bcm63xx_pcmcia_suspend(struct pcmcia_socket *sock)
61 {
62         struct bcm63xx_pcmcia_socket *skt;
63         skt = sock->driver_data;
64         return 0;
65 }
66
67 /*
68  * Implements the set_socket() operation for the in-kernel PCMCIA
69  * service (formerly SS_SetSocket in Card Services). We more or
70  * less punt all of this work and let the kernel handle the details
71  * of power configuration, reset, &c. We also record the value of
72  * `state' in order to regurgitate it to the PCMCIA core later.
73  */
74 static int bcm63xx_pcmcia_set_socket(struct pcmcia_socket *sock,
75                                      socket_state_t *state)
76 {
77         struct bcm63xx_pcmcia_socket *skt;
78         unsigned long flags;
79         u32 val;
80
81         skt = sock->driver_data;
82
83         spin_lock_irqsave(&skt->lock, flags);
84
85         /* apply requested socket power */
86         /* FIXME: hardware can't do this */
87
88         /* apply socket reset */
89         val = pcmcia_readl(skt, PCMCIA_C1_REG);
90         if (state->flags & SS_RESET)
91                 val |= PCMCIA_C1_RESET_MASK;
92         else
93                 val &= ~PCMCIA_C1_RESET_MASK;
94
95         /* reverse reset logic for cardbus card */
96         if (skt->card_detected && (skt->card_type & CARD_CARDBUS))
97                 val ^= PCMCIA_C1_RESET_MASK;
98
99         pcmcia_writel(skt, val, PCMCIA_C1_REG);
100
101         /* keep requested state for event reporting */
102         skt->requested_state = *state;
103
104         spin_unlock_irqrestore(&skt->lock, flags);
105
106         return 0;
107 }
108
109 /*
110  * identity cardtype from VS[12] input, CD[12] input while only VS2 is
111  * floating, and CD[12] input while only VS1 is floating
112  */
113 enum {
114         IN_VS1 = (1 << 0),
115         IN_VS2 = (1 << 1),
116         IN_CD1_VS2H = (1 << 2),
117         IN_CD2_VS2H = (1 << 3),
118         IN_CD1_VS1H = (1 << 4),
119         IN_CD2_VS1H = (1 << 5),
120 };
121
122 static const u8 vscd_to_cardtype[] = {
123
124         /* VS1 float, VS2 float */
125         [IN_VS1 | IN_VS2] = (CARD_PCCARD | CARD_5V),
126
127         /* VS1 grounded, VS2 float */
128         [IN_VS2] = (CARD_PCCARD | CARD_5V | CARD_3V),
129
130         /* VS1 grounded, VS2 grounded */
131         [0] = (CARD_PCCARD | CARD_5V | CARD_3V | CARD_XV),
132
133         /* VS1 tied to CD1, VS2 float */
134         [IN_VS1 | IN_VS2 | IN_CD1_VS1H] = (CARD_CARDBUS | CARD_3V),
135
136         /* VS1 grounded, VS2 tied to CD2 */
137         [IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V | CARD_XV),
138
139         /* VS1 tied to CD2, VS2 grounded */
140         [IN_VS1 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_3V | CARD_XV | CARD_YV),
141
142         /* VS1 float, VS2 grounded */
143         [IN_VS1] = (CARD_PCCARD | CARD_XV),
144
145         /* VS1 float, VS2 tied to CD2 */
146         [IN_VS1 | IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V),
147
148         /* VS1 float, VS2 tied to CD1 */
149         [IN_VS1 | IN_VS2 | IN_CD1_VS2H] = (CARD_CARDBUS | CARD_XV | CARD_YV),
150
151         /* VS1 tied to CD2, VS2 float */
152         [IN_VS1 | IN_VS2 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_YV),
153
154         /* VS2 grounded, VS1 is tied to CD1, CD2 is grounded */
155         [IN_VS1 | IN_CD1_VS1H] = 0, /* ignore cardbay */
156 };
157
158 /*
159  * poll hardware to check card insertion status
160  */
161 static unsigned int __get_socket_status(struct bcm63xx_pcmcia_socket *skt)
162 {
163         unsigned int stat;
164         u32 val;
165
166         stat = 0;
167
168         /* check CD for card presence */
169         val = pcmcia_readl(skt, PCMCIA_C1_REG);
170
171         if (!(val & PCMCIA_C1_CD1_MASK) && !(val & PCMCIA_C1_CD2_MASK))
172                 stat |= SS_DETECT;
173
174         /* if new insertion, detect cardtype */
175         if ((stat & SS_DETECT) && !skt->card_detected) {
176                 unsigned int stat = 0;
177
178                 /* float VS1, float VS2 */
179                 val |= PCMCIA_C1_VS1OE_MASK;
180                 val |= PCMCIA_C1_VS2OE_MASK;
181                 pcmcia_writel(skt, val, PCMCIA_C1_REG);
182
183                 /* wait for output to stabilize and read VS[12] */
184                 udelay(10);
185                 val = pcmcia_readl(skt, PCMCIA_C1_REG);
186                 stat |= (val & PCMCIA_C1_VS1_MASK) ? IN_VS1 : 0;
187                 stat |= (val & PCMCIA_C1_VS2_MASK) ? IN_VS2 : 0;
188
189                 /* drive VS1 low, float VS2 */
190                 val &= ~PCMCIA_C1_VS1OE_MASK;
191                 val |= PCMCIA_C1_VS2OE_MASK;
192                 pcmcia_writel(skt, val, PCMCIA_C1_REG);
193
194                 /* wait for output to stabilize and read CD[12] */
195                 udelay(10);
196                 val = pcmcia_readl(skt, PCMCIA_C1_REG);
197                 stat |= (val & PCMCIA_C1_CD1_MASK) ? IN_CD1_VS2H : 0;
198                 stat |= (val & PCMCIA_C1_CD2_MASK) ? IN_CD2_VS2H : 0;
199
200                 /* float VS1, drive VS2 low */
201                 val |= PCMCIA_C1_VS1OE_MASK;
202                 val &= ~PCMCIA_C1_VS2OE_MASK;
203                 pcmcia_writel(skt, val, PCMCIA_C1_REG);
204
205                 /* wait for output to stabilize and read CD[12] */
206                 udelay(10);
207                 val = pcmcia_readl(skt, PCMCIA_C1_REG);
208                 stat |= (val & PCMCIA_C1_CD1_MASK) ? IN_CD1_VS1H : 0;
209                 stat |= (val & PCMCIA_C1_CD2_MASK) ? IN_CD2_VS1H : 0;
210
211                 /* guess cardtype from all this */
212                 skt->card_type = vscd_to_cardtype[stat];
213                 if (!skt->card_type)
214                         printk(KERN_ERR PFX "unsupported card type\n");
215
216                 /* drive both VS pin to 0 again */
217                 val &= ~(PCMCIA_C1_VS1OE_MASK | PCMCIA_C1_VS2OE_MASK);
218
219                 /* enable correct logic */
220                 val &= ~(PCMCIA_C1_EN_PCMCIA_MASK | PCMCIA_C1_EN_CARDBUS_MASK);
221                 if (skt->card_type & CARD_PCCARD)
222                         val |= PCMCIA_C1_EN_PCMCIA_MASK;
223                 else
224                         val |= PCMCIA_C1_EN_CARDBUS_MASK;
225
226                 pcmcia_writel(skt, val, PCMCIA_C1_REG);
227         }
228         skt->card_detected = (stat & SS_DETECT) ? 1 : 0;
229
230         /* report card type/voltage */
231         if (skt->card_type & CARD_CARDBUS)
232                 stat |= SS_CARDBUS;
233         if (skt->card_type & CARD_3V)
234                 stat |= SS_3VCARD;
235         if (skt->card_type & CARD_XV)
236                 stat |= SS_XVCARD;
237         stat |= SS_POWERON;
238
239         if (gpio_get_value(skt->pd->ready_gpio))
240                 stat |= SS_READY;
241
242         return stat;
243 }
244
245 /*
246  * core request to get current socket status
247  */
248 static int bcm63xx_pcmcia_get_status(struct pcmcia_socket *sock,
249                                      unsigned int *status)
250 {
251         struct bcm63xx_pcmcia_socket *skt;
252
253         skt = sock->driver_data;
254
255         spin_lock_bh(&skt->lock);
256         *status = __get_socket_status(skt);
257         spin_unlock_bh(&skt->lock);
258
259         return 0;
260 }
261
262 /*
263  * socket polling timer callback
264  */
265 static void bcm63xx_pcmcia_poll(unsigned long data)
266 {
267         struct bcm63xx_pcmcia_socket *skt;
268         unsigned int stat, events;
269
270         skt = (struct bcm63xx_pcmcia_socket *)data;
271
272         spin_lock_bh(&skt->lock);
273
274         stat = __get_socket_status(skt);
275
276         /* keep only changed bits, and mask with required one from the
277          * core */
278         events = (stat ^ skt->old_status) & skt->requested_state.csc_mask;
279         skt->old_status = stat;
280         spin_unlock_bh(&skt->lock);
281
282         if (events)
283                 pcmcia_parse_events(&skt->socket, events);
284
285         mod_timer(&skt->timer,
286                   jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
287 }
288
289 static int bcm63xx_pcmcia_set_io_map(struct pcmcia_socket *sock,
290                                      struct pccard_io_map *map)
291 {
292         /* this doesn't seem to be called by pcmcia layer if static
293          * mapping is used */
294         return 0;
295 }
296
297 static int bcm63xx_pcmcia_set_mem_map(struct pcmcia_socket *sock,
298                                       struct pccard_mem_map *map)
299 {
300         struct bcm63xx_pcmcia_socket *skt;
301         struct resource *res;
302
303         skt = sock->driver_data;
304         if (map->flags & MAP_ATTRIB)
305                 res = skt->attr_res;
306         else
307                 res = skt->common_res;
308
309         map->static_start = res->start + map->card_start;
310         return 0;
311 }
312
313 static struct pccard_operations bcm63xx_pcmcia_operations = {
314         .init                   = bcm63xx_pcmcia_sock_init,
315         .suspend                = bcm63xx_pcmcia_suspend,
316         .get_status             = bcm63xx_pcmcia_get_status,
317         .set_socket             = bcm63xx_pcmcia_set_socket,
318         .set_io_map             = bcm63xx_pcmcia_set_io_map,
319         .set_mem_map            = bcm63xx_pcmcia_set_mem_map,
320 };
321
322 /*
323  * register pcmcia socket to core
324  */
325 static int bcm63xx_drv_pcmcia_probe(struct platform_device *pdev)
326 {
327         struct bcm63xx_pcmcia_socket *skt;
328         struct pcmcia_socket *sock;
329         struct resource *res, *irq_res;
330         unsigned int regmem_size = 0, iomem_size = 0;
331         u32 val;
332         int ret;
333
334         skt = kzalloc(sizeof(*skt), GFP_KERNEL);
335         if (!skt)
336                 return -ENOMEM;
337         spin_lock_init(&skt->lock);
338         sock = &skt->socket;
339         sock->driver_data = skt;
340
341         /* make sure we have all resources we need */
342         skt->common_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
343         skt->attr_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
344         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
345         skt->pd = pdev->dev.platform_data;
346         if (!skt->common_res || !skt->attr_res || !irq_res || !skt->pd) {
347                 ret = -EINVAL;
348                 goto err;
349         }
350
351         /* remap pcmcia registers */
352         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
353         regmem_size = res->end - res->start + 1;
354         if (!request_mem_region(res->start, regmem_size, "bcm63xx_pcmcia")) {
355                 ret = -EINVAL;
356                 goto err;
357         }
358         skt->reg_res = res;
359
360         skt->base = ioremap(res->start, regmem_size);
361         if (!skt->base) {
362                 ret = -ENOMEM;
363                 goto err;
364         }
365
366         /* remap io registers */
367         res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
368         iomem_size = res->end - res->start + 1;
369         skt->io_base = ioremap(res->start, iomem_size);
370         if (!skt->io_base) {
371                 ret = -ENOMEM;
372                 goto err;
373         }
374
375         /* resources are static */
376         sock->resource_ops = &pccard_static_ops;
377         sock->ops = &bcm63xx_pcmcia_operations;
378         sock->owner = THIS_MODULE;
379         sock->dev.parent = &pdev->dev;
380         sock->features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
381         sock->io_offset = (unsigned long)skt->io_base;
382         sock->pci_irq = irq_res->start;
383
384 #ifdef CONFIG_CARDBUS
385         sock->cb_dev = bcm63xx_cb_dev;
386         if (bcm63xx_cb_dev)
387                 sock->features |= SS_CAP_CARDBUS;
388 #endif
389
390         /* assume common & attribute memory have the same size */
391         sock->map_size = skt->common_res->end - skt->common_res->start + 1;
392
393         /* initialize polling timer */
394         setup_timer(&skt->timer, bcm63xx_pcmcia_poll, (unsigned long)skt);
395
396         /* initialize  pcmcia  control register,  drive  VS[12] to  0,
397          * leave CB IDSEL to the old  value since it is set by the PCI
398          * layer */
399         val = pcmcia_readl(skt, PCMCIA_C1_REG);
400         val &= PCMCIA_C1_CBIDSEL_MASK;
401         val |= PCMCIA_C1_EN_PCMCIA_GPIO_MASK;
402         pcmcia_writel(skt, val, PCMCIA_C1_REG);
403
404         /* FIXME set correct pcmcia timings */
405         val = PCMCIA_C2_DATA16_MASK;
406         val |= 10 << PCMCIA_C2_RWCOUNT_SHIFT;
407         val |= 6 << PCMCIA_C2_INACTIVE_SHIFT;
408         val |= 3 << PCMCIA_C2_SETUP_SHIFT;
409         val |= 3 << PCMCIA_C2_HOLD_SHIFT;
410         pcmcia_writel(skt, val, PCMCIA_C2_REG);
411
412         /* request and setup ready gpio */
413         ret = gpio_request(skt->pd->ready_gpio, "bcm63xx_pcmcia");
414         if (ret < 0)
415                 goto err;
416
417         ret = gpio_direction_input(skt->pd->ready_gpio);
418         if (ret < 0)
419                 goto err_gpio;
420
421         ret = pcmcia_register_socket(sock);
422         if (ret)
423                 goto err_gpio;
424
425         /* start polling socket */
426         mod_timer(&skt->timer,
427                   jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
428
429         platform_set_drvdata(pdev, skt);
430         return 0;
431
432 err_gpio:
433         gpio_free(skt->pd->ready_gpio);
434
435 err:
436         if (skt->io_base)
437                 iounmap(skt->io_base);
438         if (skt->base)
439                 iounmap(skt->base);
440         if (skt->reg_res)
441                 release_mem_region(skt->reg_res->start, regmem_size);
442         kfree(skt);
443         return ret;
444 }
445
446 static int bcm63xx_drv_pcmcia_remove(struct platform_device *pdev)
447 {
448         struct bcm63xx_pcmcia_socket *skt;
449         struct resource *res;
450
451         skt = platform_get_drvdata(pdev);
452         del_timer_sync(&skt->timer);
453         iounmap(skt->base);
454         iounmap(skt->io_base);
455         res = skt->reg_res;
456         release_mem_region(res->start, res->end - res->start + 1);
457         gpio_free(skt->pd->ready_gpio);
458         kfree(skt);
459         return 0;
460 }
461
462 struct platform_driver bcm63xx_pcmcia_driver = {
463         .probe  = bcm63xx_drv_pcmcia_probe,
464         .remove = __devexit_p(bcm63xx_drv_pcmcia_remove),
465         .driver = {
466                 .name   = "bcm63xx_pcmcia",
467                 .owner  = THIS_MODULE,
468         },
469 };
470
471 #ifdef CONFIG_CARDBUS
472 static int __devinit bcm63xx_cb_probe(struct pci_dev *dev,
473                                       const struct pci_device_id *id)
474 {
475         /* keep pci device */
476         bcm63xx_cb_dev = dev;
477         return platform_driver_register(&bcm63xx_pcmcia_driver);
478 }
479
480 static void __devexit bcm63xx_cb_exit(struct pci_dev *dev)
481 {
482         platform_driver_unregister(&bcm63xx_pcmcia_driver);
483         bcm63xx_cb_dev = NULL;
484 }
485
486 static struct pci_device_id bcm63xx_cb_table[] = {
487         {
488                 .vendor         = PCI_VENDOR_ID_BROADCOM,
489                 .device         = PCI_ANY_ID,
490                 .subvendor      = PCI_VENDOR_ID_BROADCOM,
491                 .subdevice      = PCI_ANY_ID,
492                 .class          = PCI_CLASS_BRIDGE_CARDBUS << 8,
493                 .class_mask     = ~0,
494         },
495         {}
496 };
497
498 MODULE_DEVICE_TABLE(pci, bcm63xx_cb_table);
499
500 static struct pci_driver bcm63xx_cardbus_driver = {
501         .name           = "yenta_cardbus",
502         .id_table       = bcm63xx_cb_table,
503         .probe          = bcm63xx_cb_probe,
504         .remove         = __devexit_p(bcm63xx_cb_exit),
505 };
506 #endif
507
508 /*
509  * if cardbus support is enabled, register our platform device after
510  * our fake cardbus bridge has been registered
511  */
512 static int __init bcm63xx_pcmcia_init(void)
513 {
514 #ifdef CONFIG_CARDBUS
515         return pci_register_driver(&bcm63xx_cardbus_driver);
516 #else
517         return platform_driver_register(&bcm63xx_pcmcia_driver);
518 #endif
519 }
520
521 static void __exit bcm63xx_pcmcia_exit(void)
522 {
523 #ifdef CONFIG_CARDBUS
524         return pci_unregister_driver(&bcm63xx_cardbus_driver);
525 #else
526         platform_driver_unregister(&bcm63xx_pcmcia_driver);
527 #endif
528 }
529
530 module_init(bcm63xx_pcmcia_init);
531 module_exit(bcm63xx_pcmcia_exit);
532
533 MODULE_LICENSE("GPL");
534 MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
535 MODULE_DESCRIPTION("Linux PCMCIA Card Services: bcm63xx Socket Controller");